summaryrefslogtreecommitdiffstats
path: root/tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp')
-rw-r--r--tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp133
1 files changed, 101 insertions, 32 deletions
diff --git a/tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp b/tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp
index 6d51bf7af..7d82a5640 100644
--- a/tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp
+++ b/tests/auto/core/qwebengineclientcertificatestore/tst_qwebengineclientcertificatestore.cpp
@@ -1,34 +1,14 @@
-/****************************************************************************
-**
-** Copyright (C) 2019 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtWebEngine module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2019 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+#include <httpsserver.h>
+#include <util.h>
#include <QtTest/QtTest>
#include <QtWebEngineCore/qwebengineclientcertificatestore.h>
-#include <QtWebEngineWidgets/qwebengineprofile.h>
+#include <QtWebEngineCore/qwebenginepage.h>
+#include <QtWebEngineCore/qwebengineprofile.h>
+#include <QtWebEngineCore/qwebenginecertificateerror.h>
+#include <QtWebEngineCore/qwebenginesettings.h>
class tst_QWebEngineClientCertificateStore : public QObject
{
@@ -39,8 +19,12 @@ public:
~tst_QWebEngineClientCertificateStore();
private Q_SLOTS:
+ void init();
+ void cleanup();
void addAndListCertificates();
void removeAndClearCertificates();
+ void clientAuthentication_data();
+ void clientAuthentication();
};
tst_QWebEngineClientCertificateStore::tst_QWebEngineClientCertificateStore()
@@ -51,6 +35,19 @@ tst_QWebEngineClientCertificateStore::~tst_QWebEngineClientCertificateStore()
{
}
+void tst_QWebEngineClientCertificateStore::init()
+{
+ QCOMPARE(0,
+ QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
+}
+
+void tst_QWebEngineClientCertificateStore::cleanup()
+{
+ QWebEngineProfile::defaultProfile()->clientCertificateStore()->clear();
+ QCOMPARE(0,
+ QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
+}
+
void tst_QWebEngineClientCertificateStore::addAndListCertificates()
{
// Load QSslCertificate
@@ -77,21 +74,93 @@ void tst_QWebEngineClientCertificateStore::addAndListCertificates()
QWebEngineProfile::defaultProfile()->clientCertificateStore()->add(cert, sslKey);
QWebEngineProfile::defaultProfile()->clientCertificateStore()->add(certSecond, sslKeySecond);
- QCOMPARE(2, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().length());
+ QCOMPARE(2, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
}
void tst_QWebEngineClientCertificateStore::removeAndClearCertificates()
{
- QCOMPARE(2, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().length());
+ addAndListCertificates();
+ QCOMPARE(2, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
// Remove one certificate from in-memory store
auto list = QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates();
QWebEngineProfile::defaultProfile()->clientCertificateStore()->remove(list[0]);
- QCOMPARE(1, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().length());
+ QCOMPARE(1, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
// Remove all certificates in-memory store
QWebEngineProfile::defaultProfile()->clientCertificateStore()->clear();
- QCOMPARE(0, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().length());
+ QCOMPARE(0, QWebEngineProfile::defaultProfile()->clientCertificateStore()->certificates().size());
+}
+
+void tst_QWebEngineClientCertificateStore::clientAuthentication_data()
+{
+ QTest::addColumn<QString>("client_certificate");
+ QTest::addColumn<QString>("client_key");
+ QTest::addColumn<bool>("in_memory");
+ QTest::addColumn<bool>("add_more_in_memory_certificates");
+ QTest::newRow("in_memory") << ":/resources/client.pem"
+ << ":/resources/client.key" << true << false;
+#if defined(TEST_NSS)
+ QTest::newRow("nss") << ":/resources/client2.pem"
+ << ":/resources/client2.key" << false << false;
+ QTest::newRow("in_memory + nss") << ":/resources/client2.pem"
+ << ":/resources/client2.key" << false << true;
+#endif
+}
+
+void tst_QWebEngineClientCertificateStore::clientAuthentication()
+{
+ QFETCH(QString, client_certificate);
+ QFETCH(QString, client_key);
+ QFETCH(bool, in_memory);
+ QFETCH(bool, add_more_in_memory_certificates);
+
+ HttpsServer server(":/resources/server.pem", ":/resources/server.key", ":resources/ca.pem");
+ server.setExpectError(false);
+ QVERIFY(server.start());
+
+ connect(&server, &HttpsServer::newRequest, [&](HttpReqRep *rr) {
+ rr->setResponseBody(QByteArrayLiteral("<html><body>TEST</body></html>"));
+ rr->sendResponse();
+ });
+
+ QFile certFile(client_certificate);
+ certFile.open(QIODevice::ReadOnly);
+ const QSslCertificate cert(certFile.readAll(), QSsl::Pem);
+
+ QFile keyFile(client_key);
+ keyFile.open(QIODevice::ReadOnly);
+ const QSslKey sslKey(keyFile.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "");
+
+ if (in_memory)
+ QWebEngineProfile::defaultProfile()->clientCertificateStore()->add(cert, sslKey);
+
+ if (add_more_in_memory_certificates)
+ addAndListCertificates();
+
+ QWebEnginePage page;
+ connect(&page, &QWebEnginePage::certificateError, [](QWebEngineCertificateError e) {
+ // ca is self signed in this test simply accept the certificate error
+ e.acceptCertificate();
+ });
+ connect(&page, &QWebEnginePage::selectClientCertificate, &page,
+ [&cert](QWebEngineClientCertificateSelection selection) {
+ QVERIFY(!selection.certificates().isEmpty());
+ for (const QSslCertificate &sCert : selection.certificates()) {
+ if (cert == sCert) {
+ selection.select(sCert);
+ return;
+ }
+ }
+ QFAIL("No certificate found.");
+ });
+ QSignalSpy loadFinishedSpy(&page, SIGNAL(loadFinished(bool)));
+ page.settings()->setAttribute(QWebEngineSettings::ErrorPageEnabled, false);
+ page.setUrl(server.url());
+ QTRY_COMPARE_WITH_TIMEOUT(loadFinishedSpy.size() > 0, true, 20000);
+ QCOMPARE(loadFinishedSpy.takeFirst().at(0).toBool(), true);
+ QCOMPARE(toPlainTextSync(&page), QStringLiteral("TEST"));
+ QVERIFY(server.stop());
}
QTEST_MAIN(tst_QWebEngineClientCertificateStore)