1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
44 #include <QtTest/QtTest>
46 #include <QNetworkProxy>
47 #include <QAuthenticator>
49 #include "private/qhostinfo_p.h"
51 #include "../../../network-settings.h"
54 typedef QSharedPointer<QSslSocket> QSslSocketPtr;
57 class tst_QSslSocket_onDemandCertificates_member : public QObject
64 tst_QSslSocket_onDemandCertificates_member();
65 virtual ~tst_QSslSocket_onDemandCertificates_member();
68 QSslSocketPtr newSocket();
72 void initTestCase_data();
76 void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth);
80 void onDemandRootCertLoadingMemberMethods();
84 #endif // QT_NO_OPENSSL
87 tst_QSslSocket_onDemandCertificates_member::tst_QSslSocket_onDemandCertificates_member()
91 tst_QSslSocket_onDemandCertificates_member::~tst_QSslSocket_onDemandCertificates_member()
107 void tst_QSslSocket_onDemandCertificates_member::initTestCase_data()
109 QTest::addColumn<bool>("setProxy");
110 QTest::addColumn<int>("proxyType");
112 QTest::newRow("WithoutProxy") << false << 0;
113 QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy);
114 QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic);
116 QTest::newRow("WithHttpProxy") << true << int(HttpProxy);
117 QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic);
118 // uncomment the line below when NTLM works
119 // QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm);
122 void tst_QSslSocket_onDemandCertificates_member::initTestCase()
124 QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
127 void tst_QSslSocket_onDemandCertificates_member::init()
129 QFETCH_GLOBAL(bool, setProxy);
131 QFETCH_GLOBAL(int, proxyType);
132 QString testServer = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString();
137 proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, testServer, 1080);
140 case Socks5Proxy | AuthBasic:
141 proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, testServer, 1081);
144 case HttpProxy | NoAuth:
145 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3128);
148 case HttpProxy | AuthBasic:
149 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3129);
152 case HttpProxy | AuthNtlm:
153 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, testServer, 3130);
156 QNetworkProxy::setApplicationProxy(proxy);
159 qt_qhostinfo_clear_cache();
162 void tst_QSslSocket_onDemandCertificates_member::cleanup()
164 QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
167 #ifndef QT_NO_OPENSSL
168 QSslSocketPtr tst_QSslSocket_onDemandCertificates_member::newSocket()
170 QSslSocket *socket = new QSslSocket;
173 connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
174 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
175 Qt::DirectConnection);
177 return QSslSocketPtr(socket);
181 void tst_QSslSocket_onDemandCertificates_member::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
184 auth->setUser("qsockstest");
185 auth->setPassword("password");
188 #ifndef QT_NO_OPENSSL
190 void tst_QSslSocket_onDemandCertificates_member::onDemandRootCertLoadingMemberMethods()
192 QString host("qt.nokia.com");
194 // not using any root certs -> should not work
195 QSslSocketPtr socket2 = newSocket();
196 this->socket = socket2.data();
197 socket2->setCaCertificates(QList<QSslCertificate>());
198 socket2->connectToHostEncrypted(host, 443);
199 QVERIFY(!socket2->waitForEncrypted());
201 // default: using on demand loading -> should work
202 QSslSocketPtr socket = newSocket();
203 this->socket = socket.data();
204 socket->connectToHostEncrypted(host, 443);
205 QEXPECT_FAIL("", "QTBUG-20983 fails", Abort);
206 QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString()));
208 // not using any root certs again -> should not work
209 QSslSocketPtr socket3 = newSocket();
210 this->socket = socket3.data();
211 socket3->setCaCertificates(QList<QSslCertificate>());
212 socket3->connectToHostEncrypted(host, 443);
213 QVERIFY(!socket3->waitForEncrypted());
215 // setting empty SSL configuration explicitly -> should not work
216 QSslSocketPtr socket4 = newSocket();
217 this->socket = socket4.data();
218 socket4->setSslConfiguration(QSslConfiguration());
219 socket4->connectToHostEncrypted(host, 443);
220 QVERIFY(!socket4->waitForEncrypted());
223 #endif // QT_NO_OPENSSL
225 QTEST_MAIN(tst_QSslSocket_onDemandCertificates_member)
226 #include "tst_qsslsocket_onDemandCertificates_member.moc"