Debian Codegraph @

tests/auto/network/ssl/qsslsocket_onDemandCertificates_member/tst_qsslsocket_onDemandCertificates_member.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29
30 #include <QtNetwork>
31 #include <QtTest/QtTest>
32
33 #include <QNetworkProxy>
34 #include <QAuthenticator>
35
36 #ifdef QT_BUILD_INTERNAL
37 # include "private/qhostinfo_p.h"
38 # ifndef QT_NO_OPENSSL
39 # include "private/qsslsocket_p.h"
40 # endif // !QT_NO_OPENSSL
41 #endif // QT_BUILD_INTERNAL
42 #include "../../../network-settings.h"
43
44 #ifndef QT_NO_OPENSSL
45 typedef QSharedPointer<QSslSocket> QSslSocketPtr;
46
47 QT_BEGIN_NAMESPACE
48 void qt_ForceTlsSecurityLevel();
49 QT_END_NAMESPACE
50
51 #endif
52
53 class tst_QSslSocket_onDemandCertificates_member : public QObject
54 {
55 Q_OBJECT
56
57 int proxyAuthCalled;
58
59 public:
60
61 #ifndef QT_NO_OPENSSL
62 tst_QSslSocket_onDemandCertificates_member()
63 {
64 QT_PREPEND_NAMESPACE(qt_ForceTlsSecurityLevel)();
65 }
66 QSslSocketPtr newSocket();
67 #endif
68
69 public slots:
70 void initTestCase_data();
71 void initTestCase();
72 void init();
73 void cleanup();
74 void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth);
75
76 #ifndef QT_NO_OPENSSL
77 private slots:
78 void onDemandRootCertLoadingMemberMethods();
79
80 private:
81 QSslSocket *socket = nullptr;
82 #endif // QT_NO_OPENSSL
83 };
84
85 enum ProxyTests {
86 NoProxy = 0x00,
87 Socks5Proxy = 0x01,
88 HttpProxy = 0x02,
89 TypeMask = 0x0f,
90
91 NoAuth = 0x00,
92 AuthBasic = 0x10,
93 AuthNtlm = 0x20,
94 AuthMask = 0xf0
95 };
96
97 void tst_QSslSocket_onDemandCertificates_member::initTestCase_data()
98 {
99 QTest::addColumn<bool>("setProxy");
100 QTest::addColumn<int>("proxyType");
101
102 QTest::newRow("WithoutProxy") << false << 0;
103 QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy);
104 QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic);
105
106 QTest::newRow("WithHttpProxy") << true << int(HttpProxy);
107 QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic);
108 // uncomment the line below when NTLM works
109 // QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm);
110 }
111
112 void tst_QSslSocket_onDemandCertificates_member::initTestCase()
113 {
114 #ifdef QT_TEST_SERVER
115 QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1080));
116 QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1081));
117 QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3128));
118 QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3129));
119 QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3130));
120 #else
121 if (!QtNetworkSettings::verifyTestNetworkSettings())
122 QSKIP("No network test server available");
123 #endif // QT_TEST_SERVER
124 }
125
126 void tst_QSslSocket_onDemandCertificates_member::init()
127 {
128 QFETCH_GLOBAL(bool, setProxy);
129 if (setProxy) {
130 QFETCH_GLOBAL(int, proxyType);
131 const auto socksAddr = QtNetworkSettings::socksProxyServerIp().toString();
132 const auto squidAddr = QtNetworkSettings::httpProxyServerIp().toString();
133 QNetworkProxy proxy;
134
135 switch (proxyType) {
136 case Socks5Proxy:
137 proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, socksAddr, 1080);
138 break;
139
140 case Socks5Proxy | AuthBasic:
141 proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, socksAddr, 1081);
142 break;
143
144 case HttpProxy | NoAuth:
145 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3128);
146 break;
147
148 case HttpProxy | AuthBasic:
149 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3129);
150 break;
151
152 case HttpProxy | AuthNtlm:
153 proxy = QNetworkProxy(QNetworkProxy::HttpProxy, squidAddr, 3130);
154 break;
155 }
156 QNetworkProxy::setApplicationProxy(proxy);
157 }
158
159 qt_qhostinfo_clear_cache();
160 }
161
162 void tst_QSslSocket_onDemandCertificates_member::cleanup()
163 {
164 QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy);
165 }
166
167 #ifndef QT_NO_OPENSSL
168 QSslSocketPtr tst_QSslSocket_onDemandCertificates_member::newSocket()
169 {
170 QSslSocket *socket = new QSslSocket;
171
172 proxyAuthCalled = 0;
173 connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
174 SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
175 Qt::DirectConnection);
176
177 return QSslSocketPtr(socket);
178 }
179 #endif
180
181 void tst_QSslSocket_onDemandCertificates_member::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth)
182 {
183 ++proxyAuthCalled;
184 auth->setUser("qsockstest");
185 auth->setPassword("password");
186 }
187
188 #ifndef QT_NO_OPENSSL
189
190 static bool waitForEncrypted(QSslSocket *socket)
191 {
192 Q_ASSERT(socket);
193
194 QEventLoop eventLoop;
195
196 QTimer connectionTimeoutWatcher;
197 connectionTimeoutWatcher.setSingleShot(true);
198 connectionTimeoutWatcher.connect(&connectionTimeoutWatcher, &QTimer::timeout,
199 [&eventLoop]() {
200 eventLoop.exit();
201 });
202
203 bool encrypted = false;
204 socket->connect(socket, &QSslSocket::encrypted, [&eventLoop, &encrypted](){
205 eventLoop.exit();
206 encrypted = true;
207 });
208
209 socket->connect(socket, QOverload<const QList<QSslError>&>::of(&QSslSocket::sslErrors),
210 [&eventLoop](){
211 eventLoop.exit();
212 });
213
214 // Wait for 30 s. maximum - the default timeout in our QSslSocket::waitForEncrypted ...
215 connectionTimeoutWatcher.start(30000);
216 eventLoop.exec();
217 return encrypted;
218 }
219
220 void tst_QSslSocket_onDemandCertificates_member::onDemandRootCertLoadingMemberMethods()
221 {
222 #define ERR(socket) socket->errorString().toLatin1()
223 const QString host("www.qt.io");
224
225 // not using any root certs -> should not work
226 QSslSocketPtr socket2 = newSocket();
227 this->socket = socket2.data();
228 auto sslConfig = socket2->sslConfiguration();
229 sslConfig.setCaCertificates(QList<QSslCertificate>());
230 socket2->setSslConfiguration(sslConfig);
231 socket2->connectToHostEncrypted(host, 443);
232 QVERIFY2(!waitForEncrypted(socket2.data()), ERR(socket2));
233
234 // default: using on demand loading -> should work
235 QSslSocketPtr socket = newSocket();
236 this->socket = socket.data();
237 socket->connectToHostEncrypted(host, 443);
238 QVERIFY2(waitForEncrypted(socket.data()), ERR(socket));
239
240 // not using any root certs again -> should not work
241 QSslSocketPtr socket3 = newSocket();
242 this->socket = socket3.data();
243 sslConfig = socket3->sslConfiguration();
244 sslConfig.setCaCertificates(QList<QSslCertificate>());
245 socket3->setSslConfiguration(sslConfig);
246 socket3->connectToHostEncrypted(host, 443);
247 QVERIFY2(!waitForEncrypted(socket3.data()), ERR(socket3));
248
249 // setting empty SSL configuration explicitly -> depends on on-demand loading
250 QSslSocketPtr socket4 = newSocket();
251 this->socket = socket4.data();
252 QSslConfiguration conf;
253 socket4->setSslConfiguration(conf);
254 socket4->connectToHostEncrypted(host, 443);
255 #ifdef QT_BUILD_INTERNAL
256 const bool works = QSslSocketPrivate::rootCertOnDemandLoadingSupported();
257 #if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
258 QVERIFY2(works, ERR(socket4));
259 #elif defined(Q_OS_MAC)
260 QVERIFY2(!works, ERR(socket4));
261 #endif // other platforms: undecided.
262 // When we *allow* on-demand loading, we enable it by default; so, on Unix,
263 // it will work without setting any certificates. Otherwise, the configuration
264 // contains an empty set of certificates, so on-demand loading shall fail.
265 const bool result = waitForEncrypted(socket4.data());
266 if (result != works)
267 qDebug() << socket4->errorString();
268 QCOMPARE(waitForEncrypted(socket4.data()), works);
269 #endif // QT_BUILD_INTERNAL
270 }
271 #undef ERR
272
273 #endif // QT_NO_OPENSSL
274
275 QTEST_MAIN(tst_QSslSocket_onDemandCertificates_member)
276 #include "tst_qsslsocket_onDemandCertificates_member.moc"