author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
||
43 |
#include <QtCore/qthread.h> |
|
44 |
#include <QtNetwork/qhostaddress.h> |
|
45 |
#include <QtNetwork/qhostinfo.h> |
|
46 |
#include <QtNetwork/qnetworkproxy.h> |
|
47 |
#include <QtNetwork/qsslcipher.h> |
|
48 |
#include <QtNetwork/qsslconfiguration.h> |
|
49 |
#include <QtNetwork/qsslkey.h> |
|
50 |
#include <QtNetwork/qsslsocket.h> |
|
51 |
#include <QtNetwork/qtcpserver.h> |
|
52 |
#include <QtTest/QtTest> |
|
53 |
||
54 |
#include <QNetworkProxy> |
|
55 |
#include <QAuthenticator> |
|
56 |
||
57 |
#include "../network-settings.h" |
|
58 |
||
59 |
Q_DECLARE_METATYPE(QAbstractSocket::SocketState) |
|
60 |
Q_DECLARE_METATYPE(QAbstractSocket::SocketError) |
|
61 |
#ifndef QT_NO_OPENSSL |
|
62 |
Q_DECLARE_METATYPE(QSslSocket::SslMode) |
|
63 |
typedef QList<QSslError::SslError> SslErrorList; |
|
64 |
Q_DECLARE_METATYPE(SslErrorList) |
|
65 |
Q_DECLARE_METATYPE(QSslError) |
|
66 |
#endif |
|
67 |
||
68 |
#if defined Q_OS_HPUX && defined Q_CC_GNU |
|
69 |
// This error is delivered every time we try to use the fluke CA |
|
70 |
// certificate. For now we work around this bug. Task 202317. |
|
71 |
#define QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
72 |
#endif |
|
73 |
||
74 |
#ifdef Q_OS_SYMBIAN |
|
75 |
#define SRCDIR "" |
|
76 |
#endif |
|
77 |
||
78 |
#ifndef QT_NO_OPENSSL |
|
79 |
class QSslSocketPtr: public QSharedPointer<QSslSocket> |
|
80 |
{ |
|
81 |
public: |
|
82 |
inline QSslSocketPtr(QSslSocket *ptr = 0) |
|
83 |
: QSharedPointer<QSslSocket>(ptr) |
|
84 |
{ } |
|
85 |
||
86 |
inline operator QSslSocket *() const { return data(); } |
|
87 |
}; |
|
88 |
#endif |
|
89 |
||
90 |
class tst_QSslSocket : public QObject |
|
91 |
{ |
|
92 |
Q_OBJECT |
|
93 |
||
94 |
int proxyAuthCalled; |
|
95 |
||
96 |
public: |
|
97 |
tst_QSslSocket(); |
|
98 |
virtual ~tst_QSslSocket(); |
|
99 |
||
100 |
static void enterLoop(int secs) |
|
101 |
{ |
|
102 |
++loopLevel; |
|
103 |
QTestEventLoop::instance().enterLoop(secs); |
|
104 |
} |
|
105 |
||
106 |
static bool timeout() |
|
107 |
{ |
|
108 |
return QTestEventLoop::instance().timeout(); |
|
109 |
} |
|
110 |
||
111 |
#ifndef QT_NO_OPENSSL |
|
112 |
QSslSocketPtr newSocket(); |
|
113 |
#endif |
|
114 |
||
115 |
public slots: |
|
116 |
void initTestCase_data(); |
|
117 |
void init(); |
|
118 |
void cleanup(); |
|
119 |
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth); |
|
120 |
||
121 |
#ifndef QT_NO_OPENSSL |
|
122 |
private slots: |
|
123 |
void constructing(); |
|
124 |
void simpleConnect(); |
|
125 |
void simpleConnectWithIgnore(); |
|
126 |
||
127 |
// API tests |
|
128 |
void sslErrors_data(); |
|
129 |
void sslErrors(); |
|
130 |
void addCaCertificate(); |
|
131 |
void addCaCertificates(); |
|
132 |
void addCaCertificates2(); |
|
133 |
void ciphers(); |
|
134 |
void connectToHostEncrypted(); |
|
135 |
void connectToHostEncryptedWithVerificationPeerName(); |
|
136 |
void sessionCipher(); |
|
137 |
void flush(); |
|
138 |
void isEncrypted(); |
|
139 |
void localCertificate(); |
|
140 |
void mode(); |
|
141 |
void peerCertificate(); |
|
142 |
void peerCertificateChain(); |
|
143 |
void privateKey(); |
|
144 |
void protocol(); |
|
145 |
void setCaCertificates(); |
|
146 |
void setLocalCertificate(); |
|
147 |
void setPrivateKey(); |
|
148 |
void setProtocol(); |
|
149 |
void setSocketDescriptor(); |
|
150 |
void waitForEncrypted(); |
|
151 |
void waitForConnectedEncryptedReadyRead(); |
|
152 |
void startClientEncryption(); |
|
153 |
void startServerEncryption(); |
|
154 |
void addDefaultCaCertificate(); |
|
155 |
void addDefaultCaCertificates(); |
|
156 |
void addDefaultCaCertificates2(); |
|
157 |
void defaultCaCertificates(); |
|
158 |
void defaultCiphers(); |
|
159 |
void resetDefaultCiphers(); |
|
160 |
void setDefaultCaCertificates(); |
|
161 |
void setDefaultCiphers(); |
|
162 |
void supportedCiphers(); |
|
163 |
void systemCaCertificates(); |
|
164 |
void wildcard(); |
|
165 |
void setEmptyKey(); |
|
166 |
void spontaneousWrite(); |
|
167 |
void setReadBufferSize(); |
|
168 |
void setReadBufferSize_task_250027(); |
|
169 |
void waitForMinusOne(); |
|
170 |
void verifyMode(); |
|
171 |
void verifyDepth(); |
|
172 |
void peerVerifyError(); |
|
173 |
void disconnectFromHostWhenConnecting(); |
|
174 |
void disconnectFromHostWhenConnected(); |
|
175 |
void resetProxy(); |
|
176 |
void ignoreSslErrorsList_data(); |
|
177 |
void ignoreSslErrorsList(); |
|
178 |
void ignoreSslErrorsListWithSlot_data(); |
|
179 |
void ignoreSslErrorsListWithSlot(); |
|
180 |
void readFromClosedSocket(); |
|
181 |
void writeBigChunk(); |
|
182 |
||
183 |
static void exitLoop() |
|
184 |
{ |
|
185 |
// Safe exit - if we aren't in an event loop, don't |
|
186 |
// exit one. |
|
187 |
if (loopLevel > 0) { |
|
188 |
--loopLevel; |
|
189 |
QTestEventLoop::instance().exitLoop(); |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
protected slots: |
|
194 |
void ignoreErrorSlot() |
|
195 |
{ |
|
196 |
socket->ignoreSslErrors(); |
|
197 |
} |
|
198 |
void untrustedWorkaroundSlot(const QList<QSslError> &errors) |
|
199 |
{ |
|
200 |
if (errors.size() == 1 && |
|
201 |
(errors.first().error() == QSslError::CertificateUntrusted || |
|
202 |
errors.first().error() == QSslError::SelfSignedCertificate)) |
|
203 |
socket->ignoreSslErrors(); |
|
204 |
} |
|
205 |
void ignoreErrorListSlot(const QList<QSslError> &errors); |
|
206 |
||
207 |
private: |
|
208 |
QSslSocket *socket; |
|
209 |
QList<QSslError> storedExpectedSslErrors; |
|
210 |
#endif // QT_NO_OPENSSL |
|
211 |
private: |
|
212 |
static int loopLevel; |
|
213 |
}; |
|
214 |
||
215 |
int tst_QSslSocket::loopLevel = 0; |
|
216 |
||
217 |
tst_QSslSocket::tst_QSslSocket() |
|
218 |
{ |
|
219 |
#ifndef QT_NO_OPENSSL |
|
220 |
qRegisterMetaType<QList<QSslError> >("QList<QSslError>"); |
|
221 |
qRegisterMetaType<QSslError>("QSslError"); |
|
222 |
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState"); |
|
223 |
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); |
|
224 |
qRegisterMetaType<QAbstractSocket::SocketState>("QSslSocket::SslMode"); |
|
225 |
#endif |
|
226 |
Q_SET_DEFAULT_IAP |
|
227 |
} |
|
228 |
||
229 |
tst_QSslSocket::~tst_QSslSocket() |
|
230 |
{ |
|
231 |
} |
|
232 |
||
233 |
enum ProxyTests { |
|
234 |
NoProxy = 0x00, |
|
235 |
Socks5Proxy = 0x01, |
|
236 |
HttpProxy = 0x02, |
|
237 |
TypeMask = 0x0f, |
|
238 |
||
239 |
NoAuth = 0x00, |
|
240 |
AuthBasic = 0x10, |
|
241 |
AuthNtlm = 0x20, |
|
242 |
AuthMask = 0xf0 |
|
243 |
}; |
|
244 |
||
245 |
void tst_QSslSocket::initTestCase_data() |
|
246 |
{ |
|
247 |
QTest::addColumn<bool>("setProxy"); |
|
248 |
QTest::addColumn<int>("proxyType"); |
|
249 |
||
250 |
QTest::newRow("WithoutProxy") << false << 0; |
|
251 |
QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy); |
|
252 |
QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic); |
|
253 |
||
254 |
QTest::newRow("WithHttpProxy") << true << int(HttpProxy); |
|
255 |
QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic); |
|
256 |
// uncomment the line below when NTLM works |
|
257 |
// QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm); |
|
258 |
} |
|
259 |
||
260 |
void tst_QSslSocket::init() |
|
261 |
{ |
|
262 |
QFETCH_GLOBAL(bool, setProxy); |
|
263 |
if (setProxy) { |
|
264 |
QFETCH_GLOBAL(int, proxyType); |
|
265 |
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(); |
|
266 |
QNetworkProxy proxy; |
|
267 |
||
268 |
switch (proxyType) { |
|
269 |
case Socks5Proxy: |
|
270 |
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1080); |
|
271 |
break; |
|
272 |
||
273 |
case Socks5Proxy | AuthBasic: |
|
274 |
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1081); |
|
275 |
break; |
|
276 |
||
277 |
case HttpProxy | NoAuth: |
|
278 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3128); |
|
279 |
break; |
|
280 |
||
281 |
case HttpProxy | AuthBasic: |
|
282 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3129); |
|
283 |
break; |
|
284 |
||
285 |
case HttpProxy | AuthNtlm: |
|
286 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3130); |
|
287 |
break; |
|
288 |
} |
|
289 |
QNetworkProxy::setApplicationProxy(proxy); |
|
290 |
} |
|
291 |
} |
|
292 |
||
293 |
void tst_QSslSocket::cleanup() |
|
294 |
{ |
|
295 |
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
|
296 |
} |
|
297 |
||
298 |
#ifndef QT_NO_OPENSSL |
|
299 |
QSslSocketPtr tst_QSslSocket::newSocket() |
|
300 |
{ |
|
301 |
QSslSocket *socket = new QSslSocket; |
|
302 |
||
303 |
proxyAuthCalled = 0; |
|
304 |
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
305 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
306 |
Qt::DirectConnection); |
|
307 |
||
308 |
return QSslSocketPtr(socket); |
|
309 |
} |
|
310 |
#endif |
|
311 |
||
312 |
void tst_QSslSocket::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) |
|
313 |
{ |
|
314 |
++proxyAuthCalled; |
|
315 |
auth->setUser("qsockstest"); |
|
316 |
auth->setPassword("password"); |
|
317 |
} |
|
318 |
||
319 |
#ifndef QT_NO_OPENSSL |
|
320 |
||
321 |
void tst_QSslSocket::constructing() |
|
322 |
{ |
|
323 |
if (!QSslSocket::supportsSsl()) |
|
324 |
return; |
|
325 |
||
326 |
QSslSocket socket; |
|
327 |
||
328 |
QCOMPARE(socket.state(), QSslSocket::UnconnectedState); |
|
329 |
QCOMPARE(socket.mode(), QSslSocket::UnencryptedMode); |
|
330 |
QVERIFY(!socket.isEncrypted()); |
|
331 |
QCOMPARE(socket.bytesAvailable(), qint64(0)); |
|
332 |
QCOMPARE(socket.bytesToWrite(), qint64(0)); |
|
333 |
QVERIFY(!socket.canReadLine()); |
|
334 |
QVERIFY(socket.atEnd()); |
|
335 |
QCOMPARE(socket.localCertificate(), QSslCertificate()); |
|
336 |
QCOMPARE(socket.sslConfiguration(), QSslConfiguration::defaultConfiguration()); |
|
337 |
QCOMPARE(socket.errorString(), QString("Unknown error")); |
|
338 |
char c = '\0'; |
|
339 |
QVERIFY(!socket.getChar(&c)); |
|
340 |
QCOMPARE(c, '\0'); |
|
341 |
QVERIFY(!socket.isOpen()); |
|
342 |
QVERIFY(!socket.isReadable()); |
|
343 |
QVERIFY(socket.isSequential()); |
|
344 |
QVERIFY(!socket.isTextModeEnabled()); |
|
345 |
QVERIFY(!socket.isWritable()); |
|
346 |
QCOMPARE(socket.openMode(), QIODevice::NotOpen); |
|
347 |
QVERIFY(socket.peek(2).isEmpty()); |
|
348 |
QCOMPARE(socket.pos(), qint64(0)); |
|
349 |
QVERIFY(!socket.putChar('c')); |
|
350 |
QVERIFY(socket.read(2).isEmpty()); |
|
351 |
QCOMPARE(socket.read(0, 0), qint64(-1)); |
|
352 |
QVERIFY(socket.readAll().isEmpty()); |
|
353 |
QTest::ignoreMessage(QtWarningMsg, "QIODevice::readLine: Called with maxSize < 2"); |
|
354 |
QCOMPARE(socket.readLine(0, 0), qint64(-1)); |
|
355 |
char buf[10]; |
|
356 |
QCOMPARE(socket.readLine(buf, sizeof(buf)), qint64(-1)); |
|
357 |
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: The device is not open"); |
|
358 |
QVERIFY(!socket.reset()); |
|
359 |
QTest::ignoreMessage(QtWarningMsg, "QIODevice::seek: The device is not open"); |
|
360 |
QVERIFY(!socket.seek(2)); |
|
361 |
QCOMPARE(socket.size(), qint64(0)); |
|
362 |
QVERIFY(!socket.waitForBytesWritten(10)); |
|
363 |
QVERIFY(!socket.waitForReadyRead(10)); |
|
364 |
QCOMPARE(socket.write(0, 0), qint64(-1)); |
|
365 |
QCOMPARE(socket.write(QByteArray()), qint64(-1)); |
|
366 |
QCOMPARE(socket.error(), QAbstractSocket::UnknownSocketError); |
|
367 |
QVERIFY(!socket.flush()); |
|
368 |
QVERIFY(!socket.isValid()); |
|
369 |
QCOMPARE(socket.localAddress(), QHostAddress()); |
|
370 |
QCOMPARE(socket.localPort(), quint16(0)); |
|
371 |
QCOMPARE(socket.peerAddress(), QHostAddress()); |
|
372 |
QVERIFY(socket.peerName().isEmpty()); |
|
373 |
QCOMPARE(socket.peerPort(), quint16(0)); |
|
374 |
QCOMPARE(socket.proxy().type(), QNetworkProxy::DefaultProxy); |
|
375 |
QCOMPARE(socket.readBufferSize(), qint64(0)); |
|
376 |
QCOMPARE(socket.socketDescriptor(), -1); |
|
377 |
QCOMPARE(socket.socketType(), QAbstractSocket::TcpSocket); |
|
378 |
QVERIFY(!socket.waitForConnected(10)); |
|
379 |
QTest::ignoreMessage(QtWarningMsg, "QSslSocket::waitForDisconnected() is not allowed in UnconnectedState"); |
|
380 |
QVERIFY(!socket.waitForDisconnected(10)); |
|
381 |
QCOMPARE(socket.protocol(), QSsl::SslV3); |
|
382 |
||
383 |
QSslConfiguration savedDefault = QSslConfiguration::defaultConfiguration(); |
|
384 |
||
385 |
// verify that changing the default config doesn't affect this socket |
|
386 |
QSslSocket::setDefaultCaCertificates(QList<QSslCertificate>()); |
|
387 |
QSslSocket::setDefaultCiphers(QList<QSslCipher>()); |
|
388 |
QVERIFY(!socket.caCertificates().isEmpty()); |
|
389 |
QVERIFY(!socket.ciphers().isEmpty()); |
|
390 |
||
391 |
// verify the default as well: |
|
392 |
QVERIFY(QSslConfiguration::defaultConfiguration().caCertificates().isEmpty()); |
|
393 |
QVERIFY(QSslConfiguration::defaultConfiguration().ciphers().isEmpty()); |
|
394 |
||
395 |
QSslConfiguration::setDefaultConfiguration(savedDefault); |
|
396 |
} |
|
397 |
||
398 |
void tst_QSslSocket::simpleConnect() |
|
399 |
{ |
|
400 |
if (!QSslSocket::supportsSsl()) |
|
401 |
return; |
|
402 |
||
403 |
QFETCH_GLOBAL(bool, setProxy); |
|
404 |
if (setProxy) |
|
405 |
return; |
|
406 |
||
407 |
QSslSocket socket; |
|
408 |
QSignalSpy connectedSpy(&socket, SIGNAL(connected())); |
|
409 |
QSignalSpy hostFoundSpy(&socket, SIGNAL(hostFound())); |
|
410 |
QSignalSpy disconnectedSpy(&socket, SIGNAL(disconnected())); |
|
411 |
QSignalSpy connectionEncryptedSpy(&socket, SIGNAL(encrypted())); |
|
412 |
QSignalSpy sslErrorsSpy(&socket, SIGNAL(sslErrors(const QList<QSslError> &))); |
|
413 |
||
414 |
connect(&socket, SIGNAL(connected()), this, SLOT(exitLoop())); |
|
415 |
connect(&socket, SIGNAL(disconnected()), this, SLOT(exitLoop())); |
|
416 |
connect(&socket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(exitLoop())); |
|
417 |
connect(&socket, SIGNAL(encrypted()), this, SLOT(exitLoop())); |
|
418 |
connect(&socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(exitLoop())); |
|
419 |
connect(&socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(exitLoop())); |
|
420 |
||
421 |
// Start connecting |
|
422 |
socket.connectToHost(QtNetworkSettings::serverName(), 993); |
|
423 |
QCOMPARE(socket.state(), QAbstractSocket::HostLookupState); |
|
424 |
enterLoop(10); |
|
425 |
||
426 |
// Entered connecting state |
|
427 |
#ifndef Q_OS_SYMBIAN |
|
428 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectingState); |
|
429 |
QCOMPARE(connectedSpy.count(), 0); |
|
430 |
#endif |
|
431 |
QCOMPARE(hostFoundSpy.count(), 1); |
|
432 |
QCOMPARE(disconnectedSpy.count(), 0); |
|
433 |
enterLoop(10); |
|
434 |
||
435 |
// Entered connected state |
|
436 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
437 |
QCOMPARE(socket.mode(), QSslSocket::UnencryptedMode); |
|
438 |
QVERIFY(!socket.isEncrypted()); |
|
439 |
QCOMPARE(connectedSpy.count(), 1); |
|
440 |
QCOMPARE(hostFoundSpy.count(), 1); |
|
441 |
QCOMPARE(disconnectedSpy.count(), 0); |
|
442 |
||
443 |
// Enter encrypted mode |
|
444 |
socket.startClientEncryption(); |
|
445 |
QCOMPARE(socket.mode(), QSslSocket::SslClientMode); |
|
446 |
QVERIFY(!socket.isEncrypted()); |
|
447 |
QCOMPARE(connectionEncryptedSpy.count(), 0); |
|
448 |
QCOMPARE(sslErrorsSpy.count(), 0); |
|
449 |
||
450 |
// Starting handshake |
|
451 |
enterLoop(10); |
|
452 |
QCOMPARE(sslErrorsSpy.count(), 1); |
|
453 |
QCOMPARE(connectionEncryptedSpy.count(), 0); |
|
454 |
QVERIFY(!socket.isEncrypted()); |
|
455 |
QCOMPARE(socket.state(), QAbstractSocket::UnconnectedState); |
|
456 |
} |
|
457 |
||
458 |
void tst_QSslSocket::simpleConnectWithIgnore() |
|
459 |
{ |
|
460 |
if (!QSslSocket::supportsSsl()) |
|
461 |
return; |
|
462 |
||
463 |
QFETCH_GLOBAL(bool, setProxy); |
|
464 |
if (setProxy) |
|
465 |
return; |
|
466 |
||
467 |
QSslSocket socket; |
|
468 |
this->socket = &socket; |
|
469 |
QSignalSpy encryptedSpy(&socket, SIGNAL(encrypted())); |
|
470 |
QSignalSpy sslErrorsSpy(&socket, SIGNAL(sslErrors(const QList<QSslError> &))); |
|
471 |
||
472 |
connect(&socket, SIGNAL(readyRead()), this, SLOT(exitLoop())); |
|
473 |
connect(&socket, SIGNAL(encrypted()), this, SLOT(exitLoop())); |
|
474 |
connect(&socket, SIGNAL(connected()), this, SLOT(exitLoop())); |
|
475 |
connect(&socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
476 |
connect(&socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(exitLoop())); |
|
477 |
||
478 |
// Start connecting |
|
479 |
socket.connectToHost(QtNetworkSettings::serverName(), 993); |
|
480 |
QCOMPARE(socket.state(), QAbstractSocket::HostLookupState); |
|
481 |
enterLoop(10); |
|
482 |
||
483 |
// Start handshake |
|
484 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
485 |
socket.startClientEncryption(); |
|
486 |
enterLoop(10); |
|
487 |
||
488 |
// Done; encryption should be enabled. |
|
489 |
QCOMPARE(sslErrorsSpy.count(), 1); |
|
490 |
QVERIFY(socket.isEncrypted()); |
|
491 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
492 |
QCOMPARE(encryptedSpy.count(), 1); |
|
493 |
||
494 |
// Wait for incoming data |
|
495 |
if (!socket.canReadLine()) |
|
496 |
enterLoop(10); |
|
497 |
||
498 |
QCOMPARE(socket.readAll(), QtNetworkSettings::expectedReplySSL()); |
|
499 |
socket.disconnectFromHost(); |
|
500 |
} |
|
501 |
||
502 |
void tst_QSslSocket::sslErrors_data() |
|
503 |
{ |
|
504 |
QTest::addColumn<QString>("host"); |
|
505 |
QTest::addColumn<int>("port"); |
|
506 |
QTest::addColumn<SslErrorList>("expected"); |
|
507 |
||
508 |
QTest::newRow(qPrintable(QtNetworkSettings::serverLocalName())) |
|
509 |
<< QtNetworkSettings::serverLocalName() |
|
510 |
<< 993 |
|
511 |
<< (SslErrorList() << QSslError::HostNameMismatch |
|
512 |
<< QSslError::SelfSignedCertificate); |
|
513 |
||
514 |
QTest::newRow("imap.trolltech.com") |
|
515 |
<< "imap.trolltech.com" |
|
516 |
<< 993 |
|
517 |
<< (SslErrorList() << QSslError::SelfSignedCertificateInChain); |
|
518 |
} |
|
519 |
||
520 |
void tst_QSslSocket::sslErrors() |
|
521 |
{ |
|
522 |
QFETCH(QString, host); |
|
523 |
QFETCH(int, port); |
|
524 |
QFETCH(SslErrorList, expected); |
|
525 |
||
526 |
QSslSocketPtr socket = newSocket(); |
|
527 |
socket->connectToHostEncrypted(host, port); |
|
528 |
socket->waitForEncrypted(5000); |
|
529 |
||
530 |
SslErrorList output; |
|
531 |
foreach (QSslError error, socket->sslErrors()) { |
|
532 |
output << error.error(); |
|
533 |
} |
|
534 |
||
535 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
536 |
if (output.last() == QSslError::CertificateUntrusted) |
|
537 |
output.takeLast(); |
|
538 |
#endif |
|
539 |
QCOMPARE(output, expected); |
|
540 |
} |
|
541 |
||
542 |
void tst_QSslSocket::addCaCertificate() |
|
543 |
{ |
|
544 |
if (!QSslSocket::supportsSsl()) |
|
545 |
return; |
|
546 |
} |
|
547 |
||
548 |
void tst_QSslSocket::addCaCertificates() |
|
549 |
{ |
|
550 |
if (!QSslSocket::supportsSsl()) |
|
551 |
return; |
|
552 |
} |
|
553 |
||
554 |
void tst_QSslSocket::addCaCertificates2() |
|
555 |
{ |
|
556 |
if (!QSslSocket::supportsSsl()) |
|
557 |
return; |
|
558 |
} |
|
559 |
||
560 |
void tst_QSslSocket::ciphers() |
|
561 |
{ |
|
562 |
if (!QSslSocket::supportsSsl()) |
|
563 |
return; |
|
564 |
||
565 |
QSslSocket socket; |
|
566 |
QCOMPARE(socket.ciphers(), QSslSocket::supportedCiphers()); |
|
567 |
socket.setCiphers(QList<QSslCipher>()); |
|
568 |
QVERIFY(socket.ciphers().isEmpty()); |
|
569 |
socket.setCiphers(socket.defaultCiphers()); |
|
570 |
QCOMPARE(socket.ciphers(), QSslSocket::supportedCiphers()); |
|
571 |
socket.setCiphers(socket.defaultCiphers()); |
|
572 |
QCOMPARE(socket.ciphers(), QSslSocket::supportedCiphers()); |
|
573 |
||
574 |
// Task 164356 |
|
575 |
socket.setCiphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); |
|
576 |
} |
|
577 |
||
578 |
void tst_QSslSocket::connectToHostEncrypted() |
|
579 |
{ |
|
580 |
if (!QSslSocket::supportsSsl()) |
|
581 |
return; |
|
582 |
||
583 |
QSslSocketPtr socket = newSocket(); |
|
584 |
this->socket = socket; |
|
585 |
QVERIFY(socket->addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem"))); |
|
586 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
587 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
588 |
this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); |
|
589 |
#endif |
|
590 |
||
591 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
592 |
||
593 |
// This should pass unconditionally when using fluke's CA certificate. |
|
594 |
// or use untrusted certificate workaround |
|
595 |
QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); |
|
596 |
||
597 |
socket->disconnectFromHost(); |
|
598 |
QVERIFY(socket->waitForDisconnected()); |
|
599 |
||
600 |
QCOMPARE(socket->mode(), QSslSocket::SslClientMode); |
|
601 |
||
602 |
socket->connectToHost(QtNetworkSettings::serverName(), 13); |
|
603 |
||
604 |
QCOMPARE(socket->mode(), QSslSocket::UnencryptedMode); |
|
605 |
||
606 |
QVERIFY(socket->waitForDisconnected()); |
|
607 |
} |
|
608 |
||
609 |
void tst_QSslSocket::connectToHostEncryptedWithVerificationPeerName() |
|
610 |
{ |
|
611 |
if (!QSslSocket::supportsSsl()) |
|
612 |
return; |
|
613 |
||
614 |
QSslSocketPtr socket = newSocket(); |
|
615 |
this->socket = socket; |
|
616 |
||
617 |
socket->addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
618 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
619 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
620 |
this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); |
|
621 |
#endif |
|
622 |
||
623 |
// connect to the server with its local name, but use the full name for verification. |
|
624 |
socket->connectToHostEncrypted(QtNetworkSettings::serverLocalName(), 443, QtNetworkSettings::serverName()); |
|
625 |
||
626 |
// This should pass unconditionally when using fluke's CA certificate. |
|
627 |
QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); |
|
628 |
||
629 |
socket->disconnectFromHost(); |
|
630 |
QVERIFY(socket->waitForDisconnected()); |
|
631 |
||
632 |
QCOMPARE(socket->mode(), QSslSocket::SslClientMode); |
|
633 |
} |
|
634 |
||
635 |
void tst_QSslSocket::sessionCipher() |
|
636 |
{ |
|
637 |
if (!QSslSocket::supportsSsl()) |
|
638 |
return; |
|
639 |
||
640 |
QSslSocketPtr socket = newSocket(); |
|
641 |
this->socket = socket; |
|
642 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(ignoreErrorSlot())); |
|
643 |
QVERIFY(socket->sessionCipher().isNull()); |
|
644 |
socket->connectToHost(QtNetworkSettings::serverName(), 443 /* https */); |
|
645 |
QVERIFY(socket->waitForConnected(5000)); |
|
646 |
QVERIFY(socket->sessionCipher().isNull()); |
|
647 |
socket->startClientEncryption(); |
|
648 |
QVERIFY(socket->waitForEncrypted(5000)); |
|
649 |
QVERIFY(!socket->sessionCipher().isNull()); |
|
650 |
QVERIFY(QSslSocket::supportedCiphers().contains(socket->sessionCipher())); |
|
651 |
socket->disconnectFromHost(); |
|
652 |
QVERIFY(socket->waitForDisconnected()); |
|
653 |
} |
|
654 |
||
655 |
void tst_QSslSocket::flush() |
|
656 |
{ |
|
657 |
} |
|
658 |
||
659 |
void tst_QSslSocket::isEncrypted() |
|
660 |
{ |
|
661 |
} |
|
662 |
||
663 |
void tst_QSslSocket::localCertificate() |
|
664 |
{ |
|
665 |
} |
|
666 |
||
667 |
void tst_QSslSocket::mode() |
|
668 |
{ |
|
669 |
} |
|
670 |
||
671 |
void tst_QSslSocket::peerCertificate() |
|
672 |
{ |
|
673 |
} |
|
674 |
||
675 |
void tst_QSslSocket::peerCertificateChain() |
|
676 |
{ |
|
677 |
if (!QSslSocket::supportsSsl()) |
|
678 |
return; |
|
679 |
||
680 |
QSslSocketPtr socket = newSocket(); |
|
681 |
this->socket = socket; |
|
682 |
||
683 |
QList<QSslCertificate> caCertificates = QSslCertificate::fromPath(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
684 |
QVERIFY(caCertificates.count() == 1); |
|
685 |
socket->addCaCertificates(caCertificates); |
|
686 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
687 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
688 |
this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); |
|
689 |
#endif |
|
690 |
||
691 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
692 |
QCOMPARE(socket->mode(), QSslSocket::UnencryptedMode); |
|
693 |
QVERIFY(socket->peerCertificateChain().isEmpty()); |
|
694 |
QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); |
|
695 |
||
696 |
QList<QSslCertificate> certChain = socket->peerCertificateChain(); |
|
697 |
QVERIFY(certChain.count() > 0); |
|
698 |
QCOMPARE(certChain.first(), socket->peerCertificate()); |
|
699 |
||
700 |
socket->disconnectFromHost(); |
|
701 |
QVERIFY(socket->waitForDisconnected()); |
|
702 |
||
703 |
// connect again to a different server |
|
704 |
socket->connectToHostEncrypted("trolltech.com", 443); |
|
705 |
socket->ignoreSslErrors(); |
|
706 |
QCOMPARE(socket->mode(), QSslSocket::UnencryptedMode); |
|
707 |
QVERIFY(socket->peerCertificateChain().isEmpty()); |
|
708 |
QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); |
|
709 |
||
710 |
QCOMPARE(socket->peerCertificateChain().first(), socket->peerCertificate()); |
|
711 |
QVERIFY(socket->peerCertificateChain() != certChain); |
|
712 |
||
713 |
socket->disconnectFromHost(); |
|
714 |
QVERIFY(socket->waitForDisconnected()); |
|
715 |
||
716 |
// now do it again back to the original server |
|
717 |
socket->connectToHost(QtNetworkSettings::serverName(), 443); |
|
718 |
QCOMPARE(socket->mode(), QSslSocket::UnencryptedMode); |
|
719 |
QVERIFY(socket->peerCertificateChain().isEmpty()); |
|
720 |
QVERIFY2(socket->waitForConnected(10000), "Network timeout"); |
|
721 |
||
722 |
socket->startClientEncryption(); |
|
723 |
QVERIFY2(socket->waitForEncrypted(10000), qPrintable(socket->errorString())); |
|
724 |
||
725 |
QCOMPARE(socket->peerCertificateChain().first(), socket->peerCertificate()); |
|
726 |
QVERIFY(socket->peerCertificateChain() == certChain); |
|
727 |
||
728 |
socket->disconnectFromHost(); |
|
729 |
QVERIFY(socket->waitForDisconnected()); |
|
730 |
} |
|
731 |
||
732 |
void tst_QSslSocket::privateKey() |
|
733 |
{ |
|
734 |
} |
|
735 |
||
736 |
void tst_QSslSocket::protocol() |
|
737 |
{ |
|
738 |
if (!QSslSocket::supportsSsl()) |
|
739 |
return; |
|
740 |
||
741 |
QSslSocketPtr socket = newSocket(); |
|
742 |
this->socket = socket; |
|
743 |
QList<QSslCertificate> certs = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); |
|
744 |
||
745 |
// qDebug() << "certs:" << certs.at(0).issuerInfo(QSslCertificate::CommonName); |
|
746 |
socket->setCaCertificates(certs); |
|
747 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
748 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
749 |
this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); |
|
750 |
#endif |
|
751 |
||
752 |
// qDebug() << "socket cert:" << socket->caCertificates().at(0).issuerInfo(QSslCertificate::CommonName); |
|
753 |
QCOMPARE(socket->protocol(), QSsl::SslV3); |
|
754 |
{ |
|
755 |
// Fluke allows SSLv3. |
|
756 |
socket->setProtocol(QSsl::SslV3); |
|
757 |
QCOMPARE(socket->protocol(), QSsl::SslV3); |
|
758 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
759 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
760 |
QCOMPARE(socket->protocol(), QSsl::SslV3); |
|
761 |
socket->abort(); |
|
762 |
QCOMPARE(socket->protocol(), QSsl::SslV3); |
|
763 |
socket->connectToHost(QtNetworkSettings::serverName(), 443); |
|
764 |
QVERIFY2(socket->waitForConnected(), qPrintable(socket->errorString())); |
|
765 |
socket->startClientEncryption(); |
|
766 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
767 |
QCOMPARE(socket->protocol(), QSsl::SslV3); |
|
768 |
socket->abort(); |
|
769 |
} |
|
770 |
{ |
|
771 |
// Fluke allows TLSV1. |
|
772 |
socket->setProtocol(QSsl::TlsV1); |
|
773 |
QCOMPARE(socket->protocol(), QSsl::TlsV1); |
|
774 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
775 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
776 |
QCOMPARE(socket->protocol(), QSsl::TlsV1); |
|
777 |
socket->abort(); |
|
778 |
QCOMPARE(socket->protocol(), QSsl::TlsV1); |
|
779 |
socket->connectToHost(QtNetworkSettings::serverName(), 443); |
|
780 |
QVERIFY2(socket->waitForConnected(), qPrintable(socket->errorString())); |
|
781 |
socket->startClientEncryption(); |
|
782 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
783 |
QCOMPARE(socket->protocol(), QSsl::TlsV1); |
|
784 |
socket->abort(); |
|
785 |
} |
|
786 |
{ |
|
787 |
// Fluke allows SSLV2. |
|
788 |
socket->setProtocol(QSsl::SslV2); |
|
789 |
QCOMPARE(socket->protocol(), QSsl::SslV2); |
|
790 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
791 |
QVERIFY(socket->waitForEncrypted()); |
|
792 |
QCOMPARE(socket->protocol(), QSsl::SslV2); |
|
793 |
socket->abort(); |
|
794 |
QCOMPARE(socket->protocol(), QSsl::SslV2); |
|
795 |
socket->connectToHost(QtNetworkSettings::serverName(), 443); |
|
796 |
QVERIFY2(socket->waitForConnected(), qPrintable(socket->errorString())); |
|
797 |
socket->startClientEncryption(); |
|
798 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
799 |
socket->abort(); |
|
800 |
} |
|
801 |
{ |
|
802 |
// Fluke allows SSLV3, so it allows AnyProtocol. |
|
803 |
socket->setProtocol(QSsl::AnyProtocol); |
|
804 |
QCOMPARE(socket->protocol(), QSsl::AnyProtocol); |
|
805 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
806 |
QVERIFY(socket->waitForEncrypted()); |
|
807 |
QCOMPARE(socket->protocol(), QSsl::AnyProtocol); |
|
808 |
socket->abort(); |
|
809 |
QCOMPARE(socket->protocol(), QSsl::AnyProtocol); |
|
810 |
socket->connectToHost(QtNetworkSettings::serverName(), 443); |
|
811 |
QVERIFY2(socket->waitForConnected(), qPrintable(socket->errorString())); |
|
812 |
socket->startClientEncryption(); |
|
813 |
QVERIFY2(socket->waitForEncrypted(), qPrintable(socket->errorString())); |
|
814 |
QCOMPARE(socket->protocol(), QSsl::AnyProtocol); |
|
815 |
socket->abort(); |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
void tst_QSslSocket::setCaCertificates() |
|
820 |
{ |
|
821 |
if (!QSslSocket::supportsSsl()) |
|
822 |
return; |
|
823 |
||
824 |
QSslSocket socket; |
|
825 |
QCOMPARE(socket.caCertificates(), QSslSocket::defaultCaCertificates()); |
|
826 |
socket.setCaCertificates(QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
827 |
QCOMPARE(socket.caCertificates().size(), 1); |
|
828 |
socket.setCaCertificates(socket.defaultCaCertificates()); |
|
829 |
QCOMPARE(socket.caCertificates(), QSslSocket::defaultCaCertificates()); |
|
830 |
} |
|
831 |
||
832 |
void tst_QSslSocket::setLocalCertificate() |
|
833 |
{ |
|
834 |
} |
|
835 |
||
836 |
void tst_QSslSocket::setPrivateKey() |
|
837 |
{ |
|
838 |
} |
|
839 |
||
840 |
void tst_QSslSocket::setProtocol() |
|
841 |
{ |
|
842 |
} |
|
843 |
||
844 |
class SslServer : public QTcpServer |
|
845 |
{ |
|
846 |
Q_OBJECT |
|
847 |
public: |
|
848 |
SslServer() : socket(0) { } |
|
849 |
QSslSocket *socket; |
|
850 |
||
851 |
protected: |
|
852 |
void incomingConnection(int socketDescriptor) |
|
853 |
{ |
|
854 |
socket = new QSslSocket(this); |
|
855 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
856 |
||
857 |
QFile file(SRCDIR "certs/fluke.key"); |
|
858 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
859 |
QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); |
|
860 |
QVERIFY(!key.isNull()); |
|
861 |
socket->setPrivateKey(key); |
|
862 |
||
863 |
QList<QSslCertificate> localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); |
|
864 |
QVERIFY(!localCert.isEmpty()); |
|
865 |
QVERIFY(localCert.first().handle()); |
|
866 |
socket->setLocalCertificate(localCert.first()); |
|
867 |
||
868 |
QVERIFY(socket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState)); |
|
869 |
QVERIFY(!socket->peerAddress().isNull()); |
|
870 |
QVERIFY(socket->peerPort() != 0); |
|
871 |
QVERIFY(!socket->localAddress().isNull()); |
|
872 |
QVERIFY(socket->localPort() != 0); |
|
873 |
||
874 |
socket->startServerEncryption(); |
|
875 |
} |
|
876 |
||
877 |
protected slots: |
|
878 |
void ignoreErrorSlot() |
|
879 |
{ |
|
880 |
socket->ignoreSslErrors(); |
|
881 |
} |
|
882 |
}; |
|
883 |
||
884 |
void tst_QSslSocket::setSocketDescriptor() |
|
885 |
{ |
|
886 |
if (!QSslSocket::supportsSsl()) |
|
887 |
return; |
|
888 |
||
889 |
QFETCH_GLOBAL(bool, setProxy); |
|
890 |
if (setProxy) |
|
891 |
return; |
|
892 |
||
893 |
SslServer server; |
|
894 |
QVERIFY(server.listen()); |
|
895 |
||
896 |
QEventLoop loop; |
|
897 |
QTimer::singleShot(5000, &loop, SLOT(quit())); |
|
898 |
||
899 |
QSslSocketPtr client = new QSslSocket; |
|
900 |
socket = client; |
|
901 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
902 |
connect(client, SIGNAL(encrypted()), &loop, SLOT(quit())); |
|
903 |
||
904 |
client->connectToHostEncrypted(QHostAddress(QHostAddress::LocalHost).toString(), server.serverPort()); |
|
905 |
||
906 |
loop.exec(); |
|
907 |
||
908 |
QCOMPARE(client->state(), QAbstractSocket::ConnectedState); |
|
909 |
QVERIFY(client->isEncrypted()); |
|
910 |
QVERIFY(!client->peerAddress().isNull()); |
|
911 |
QVERIFY(client->peerPort() != 0); |
|
912 |
QVERIFY(!client->localAddress().isNull()); |
|
913 |
QVERIFY(client->localPort() != 0); |
|
914 |
} |
|
915 |
||
916 |
void tst_QSslSocket::waitForEncrypted() |
|
917 |
{ |
|
918 |
if (!QSslSocket::supportsSsl()) |
|
919 |
return; |
|
920 |
||
921 |
QSslSocketPtr socket = newSocket(); |
|
922 |
this->socket = socket; |
|
923 |
||
924 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
925 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
926 |
||
927 |
QVERIFY(socket->waitForEncrypted(10000)); |
|
928 |
} |
|
929 |
||
930 |
void tst_QSslSocket::waitForConnectedEncryptedReadyRead() |
|
931 |
{ |
|
932 |
if (!QSslSocket::supportsSsl()) |
|
933 |
return; |
|
934 |
||
935 |
QSslSocketPtr socket = newSocket(); |
|
936 |
this->socket = socket; |
|
937 |
||
938 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
939 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 993); |
|
940 |
||
941 |
#ifdef Q_OS_SYMBIAN |
|
942 |
QVERIFY(socket->waitForConnected(10000)); |
|
943 |
QVERIFY(socket->waitForEncrypted(10000)); |
|
944 |
||
945 |
// dont forget to login |
|
946 |
QCOMPARE((int) socket->write("USER ftptest\r\n"), 14); |
|
947 |
QCOMPARE((int) socket->write("PASS ftP2Ptf\r\n"), 14); |
|
948 |
||
949 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
950 |
QVERIFY(!socket->peerCertificate().isNull()); |
|
951 |
QVERIFY(!socket->peerCertificateChain().isEmpty()); |
|
952 |
#else |
|
953 |
QVERIFY(socket->waitForConnected(10000)); |
|
954 |
QVERIFY(socket->waitForEncrypted(10000)); |
|
955 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
956 |
QVERIFY(!socket->peerCertificate().isNull()); |
|
957 |
QVERIFY(!socket->peerCertificateChain().isEmpty()); |
|
958 |
#endif |
|
959 |
} |
|
960 |
||
961 |
void tst_QSslSocket::startClientEncryption() |
|
962 |
{ |
|
963 |
} |
|
964 |
||
965 |
void tst_QSslSocket::startServerEncryption() |
|
966 |
{ |
|
967 |
} |
|
968 |
||
969 |
void tst_QSslSocket::addDefaultCaCertificate() |
|
970 |
{ |
|
971 |
if (!QSslSocket::supportsSsl()) |
|
972 |
return; |
|
973 |
||
974 |
// Reset the global CA chain |
|
975 |
QSslSocket::setDefaultCaCertificates(QSslSocket::systemCaCertificates()); |
|
976 |
||
977 |
QList<QSslCertificate> flukeCerts = QSslCertificate::fromPath(SRCDIR "certs/qt-test-server-cacert.pem"); |
|
978 |
QCOMPARE(flukeCerts.size(), 1); |
|
979 |
QList<QSslCertificate> globalCerts = QSslSocket::defaultCaCertificates(); |
|
980 |
QVERIFY(!globalCerts.contains(flukeCerts.first())); |
|
981 |
QSslSocket::addDefaultCaCertificate(flukeCerts.first()); |
|
982 |
QCOMPARE(QSslSocket::defaultCaCertificates().size(), globalCerts.size() + 1); |
|
983 |
QVERIFY(QSslSocket::defaultCaCertificates().contains(flukeCerts.first())); |
|
984 |
||
985 |
// Restore the global CA chain |
|
986 |
QSslSocket::setDefaultCaCertificates(QSslSocket::systemCaCertificates()); |
|
987 |
} |
|
988 |
||
989 |
void tst_QSslSocket::addDefaultCaCertificates() |
|
990 |
{ |
|
991 |
} |
|
992 |
||
993 |
void tst_QSslSocket::addDefaultCaCertificates2() |
|
994 |
{ |
|
995 |
} |
|
996 |
||
997 |
void tst_QSslSocket::defaultCaCertificates() |
|
998 |
{ |
|
999 |
if (!QSslSocket::supportsSsl()) |
|
1000 |
return; |
|
1001 |
||
1002 |
QList<QSslCertificate> certs = QSslSocket::defaultCaCertificates(); |
|
1003 |
QVERIFY(certs.size() > 1); |
|
1004 |
QCOMPARE(certs, QSslSocket::systemCaCertificates()); |
|
1005 |
} |
|
1006 |
||
1007 |
void tst_QSslSocket::defaultCiphers() |
|
1008 |
{ |
|
1009 |
} |
|
1010 |
||
1011 |
void tst_QSslSocket::resetDefaultCiphers() |
|
1012 |
{ |
|
1013 |
} |
|
1014 |
||
1015 |
void tst_QSslSocket::setDefaultCaCertificates() |
|
1016 |
{ |
|
1017 |
} |
|
1018 |
||
1019 |
void tst_QSslSocket::setDefaultCiphers() |
|
1020 |
{ |
|
1021 |
} |
|
1022 |
||
1023 |
void tst_QSslSocket::supportedCiphers() |
|
1024 |
{ |
|
1025 |
if (!QSslSocket::supportsSsl()) |
|
1026 |
return; |
|
1027 |
||
1028 |
QList<QSslCipher> ciphers = QSslSocket::supportedCiphers(); |
|
1029 |
QVERIFY(ciphers.size() > 1); |
|
1030 |
||
1031 |
QSslSocket socket; |
|
1032 |
QCOMPARE(socket.supportedCiphers(), ciphers); |
|
1033 |
QCOMPARE(socket.defaultCiphers(), ciphers); |
|
1034 |
QCOMPARE(socket.ciphers(), ciphers); |
|
1035 |
} |
|
1036 |
||
1037 |
void tst_QSslSocket::systemCaCertificates() |
|
1038 |
{ |
|
1039 |
if (!QSslSocket::supportsSsl()) |
|
1040 |
return; |
|
1041 |
||
1042 |
QList<QSslCertificate> certs = QSslSocket::systemCaCertificates(); |
|
1043 |
QVERIFY(certs.size() > 1); |
|
1044 |
QCOMPARE(certs, QSslSocket::defaultCaCertificates()); |
|
1045 |
} |
|
1046 |
||
1047 |
void tst_QSslSocket::wildcard() |
|
1048 |
{ |
|
1049 |
QSKIP("TODO: solve wildcard problem", SkipAll); |
|
1050 |
||
1051 |
if (!QSslSocket::supportsSsl()) |
|
1052 |
return; |
|
1053 |
||
1054 |
// Fluke runs an apache server listening on port 4443, serving the |
|
1055 |
// wildcard fluke.*.troll.no. The DNS entry for |
|
1056 |
// fluke.wildcard.dev.troll.no, served by ares (root for dev.troll.no), |
|
1057 |
// returns the CNAME fluke.troll.no for this domain. The web server |
|
1058 |
// responds with the wildcard, and QSslSocket should accept that as a |
|
1059 |
// valid connection. This was broken in 4.3.0. |
|
1060 |
QSslSocketPtr socket = newSocket(); |
|
1061 |
socket->addCaCertificates(QLatin1String("certs/aspiriniks.ca.crt")); |
|
1062 |
this->socket = socket; |
|
1063 |
#ifdef QSSLSOCKET_CERTUNTRUSTED_WORKAROUND |
|
1064 |
connect(socket, SIGNAL(sslErrors(QList<QSslError>)), |
|
1065 |
this, SLOT(untrustedWorkaroundSlot(QList<QSslError>))); |
|
1066 |
#endif |
|
1067 |
socket->connectToHostEncrypted(QtNetworkSettings::wildcardServerName(), 4443); |
|
1068 |
||
1069 |
QVERIFY2(socket->waitForEncrypted(3000), qPrintable(socket->errorString())); |
|
1070 |
||
1071 |
QSslCertificate certificate = socket->peerCertificate(); |
|
1072 |
QCOMPARE(certificate.subjectInfo(QSslCertificate::CommonName), QString(QtNetworkSettings::serverLocalName() + ".*." + QtNetworkSettings::serverDomainName())); |
|
1073 |
QCOMPARE(certificate.issuerInfo(QSslCertificate::CommonName), QtNetworkSettings::serverName()); |
|
1074 |
||
1075 |
socket->close(); |
|
1076 |
} |
|
1077 |
||
1078 |
class SslServer2 : public QTcpServer |
|
1079 |
{ |
|
1080 |
protected: |
|
1081 |
void incomingConnection(int socketDescriptor) |
|
1082 |
{ |
|
1083 |
QSslSocket *socket = new QSslSocket(this); |
|
1084 |
socket->ignoreSslErrors(); |
|
1085 |
||
1086 |
// Only set the certificate |
|
1087 |
QList<QSslCertificate> localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); |
|
1088 |
QVERIFY(!localCert.isEmpty()); |
|
1089 |
QVERIFY(localCert.first().handle()); |
|
1090 |
socket->setLocalCertificate(localCert.first()); |
|
1091 |
||
1092 |
QVERIFY(socket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState)); |
|
1093 |
||
1094 |
socket->startServerEncryption(); |
|
1095 |
} |
|
1096 |
}; |
|
1097 |
||
1098 |
void tst_QSslSocket::setEmptyKey() |
|
1099 |
{ |
|
1100 |
if (!QSslSocket::supportsSsl()) |
|
1101 |
return; |
|
1102 |
||
1103 |
QFETCH_GLOBAL(bool, setProxy); |
|
1104 |
if (setProxy) |
|
1105 |
return; |
|
1106 |
||
1107 |
SslServer2 server; |
|
1108 |
server.listen(); |
|
1109 |
||
1110 |
QSslSocket socket; |
|
1111 |
socket.connectToHostEncrypted("127.0.0.1", server.serverPort()); |
|
1112 |
||
1113 |
QTestEventLoop::instance().enterLoop(2); |
|
1114 |
||
1115 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
1116 |
QCOMPARE(socket.error(), QAbstractSocket::UnknownSocketError); |
|
1117 |
} |
|
1118 |
||
1119 |
void tst_QSslSocket::spontaneousWrite() |
|
1120 |
{ |
|
1121 |
QFETCH_GLOBAL(bool, setProxy); |
|
1122 |
if (setProxy) |
|
1123 |
return; |
|
1124 |
||
1125 |
SslServer server; |
|
1126 |
QSslSocket *receiver = new QSslSocket(this); |
|
1127 |
connect(receiver, SIGNAL(readyRead()), SLOT(exitLoop())); |
|
1128 |
||
1129 |
// connect two sockets to each other: |
|
1130 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
1131 |
receiver->connectToHost("127.0.0.1", server.serverPort()); |
|
1132 |
QVERIFY(receiver->waitForConnected(5000)); |
|
1133 |
QVERIFY(server.waitForNewConnection(0)); |
|
1134 |
||
1135 |
QSslSocket *sender = server.socket; |
|
1136 |
QVERIFY(sender); |
|
1137 |
QVERIFY(sender->state() == QAbstractSocket::ConnectedState); |
|
1138 |
receiver->setObjectName("receiver"); |
|
1139 |
sender->setObjectName("sender"); |
|
1140 |
receiver->ignoreSslErrors(); |
|
1141 |
receiver->startClientEncryption(); |
|
1142 |
||
1143 |
// SSL handshake: |
|
1144 |
connect(receiver, SIGNAL(encrypted()), SLOT(exitLoop())); |
|
1145 |
enterLoop(1); |
|
1146 |
QVERIFY(!timeout()); |
|
1147 |
QVERIFY(sender->isEncrypted()); |
|
1148 |
QVERIFY(receiver->isEncrypted()); |
|
1149 |
||
1150 |
// make sure there's nothing to be received on the sender: |
|
1151 |
while (sender->waitForReadyRead(10) || receiver->waitForBytesWritten(10)) {} |
|
1152 |
||
1153 |
// spontaneously write something: |
|
1154 |
QByteArray data("Hello World"); |
|
1155 |
sender->write(data); |
|
1156 |
||
1157 |
// check if the other side receives it: |
|
1158 |
enterLoop(1); |
|
1159 |
QVERIFY(!timeout()); |
|
1160 |
QCOMPARE(receiver->bytesAvailable(), qint64(data.size())); |
|
1161 |
QCOMPARE(receiver->readAll(), data); |
|
1162 |
} |
|
1163 |
||
1164 |
void tst_QSslSocket::setReadBufferSize() |
|
1165 |
{ |
|
1166 |
QFETCH_GLOBAL(bool, setProxy); |
|
1167 |
if (setProxy) |
|
1168 |
return; |
|
1169 |
||
1170 |
SslServer server; |
|
1171 |
QSslSocket *receiver = new QSslSocket(this); |
|
1172 |
connect(receiver, SIGNAL(readyRead()), SLOT(exitLoop())); |
|
1173 |
||
1174 |
// connect two sockets to each other: |
|
1175 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
1176 |
receiver->connectToHost("127.0.0.1", server.serverPort()); |
|
1177 |
QVERIFY(receiver->waitForConnected(5000)); |
|
1178 |
QVERIFY(server.waitForNewConnection(0)); |
|
1179 |
||
1180 |
QSslSocket *sender = server.socket; |
|
1181 |
QVERIFY(sender); |
|
1182 |
QVERIFY(sender->state() == QAbstractSocket::ConnectedState); |
|
1183 |
receiver->setObjectName("receiver"); |
|
1184 |
sender->setObjectName("sender"); |
|
1185 |
receiver->ignoreSslErrors(); |
|
1186 |
receiver->startClientEncryption(); |
|
1187 |
||
1188 |
// SSL handshake: |
|
1189 |
connect(receiver, SIGNAL(encrypted()), SLOT(exitLoop())); |
|
1190 |
enterLoop(1); |
|
1191 |
QVERIFY(!timeout()); |
|
1192 |
QVERIFY(sender->isEncrypted()); |
|
1193 |
QVERIFY(receiver->isEncrypted()); |
|
1194 |
||
1195 |
QByteArray data(2048, 'b'); |
|
1196 |
receiver->setReadBufferSize(39 * 1024); // make it a non-multiple of the data.size() |
|
1197 |
||
1198 |
// saturate the incoming buffer |
|
1199 |
while (sender->state() == QAbstractSocket::ConnectedState && |
|
1200 |
receiver->state() == QAbstractSocket::ConnectedState && |
|
1201 |
receiver->bytesAvailable() < receiver->readBufferSize()) { |
|
1202 |
sender->write(data); |
|
1203 |
//qDebug() << receiver->bytesAvailable() << "<" << receiver->readBufferSize() << (receiver->bytesAvailable() < receiver->readBufferSize()); |
|
1204 |
||
1205 |
while (sender->bytesToWrite()) |
|
1206 |
QVERIFY(sender->waitForBytesWritten(10)); |
|
1207 |
||
1208 |
// drain it: |
|
1209 |
while (receiver->bytesAvailable() < receiver->readBufferSize() && |
|
1210 |
receiver->waitForReadyRead(10)) {} |
|
1211 |
} |
|
1212 |
||
1213 |
//qDebug() << sender->bytesToWrite() << "bytes to write"; |
|
1214 |
//qDebug() << receiver->bytesAvailable() << "bytes available"; |
|
1215 |
||
1216 |
// send a bit more |
|
1217 |
sender->write(data); |
|
1218 |
sender->write(data); |
|
1219 |
sender->write(data); |
|
1220 |
sender->write(data); |
|
1221 |
QVERIFY(sender->waitForBytesWritten(10)); |
|
1222 |
||
1223 |
qint64 oldBytesAvailable = receiver->bytesAvailable(); |
|
1224 |
||
1225 |
// now unset the read buffer limit and iterate |
|
1226 |
receiver->setReadBufferSize(0); |
|
1227 |
enterLoop(1); |
|
1228 |
QVERIFY(!timeout()); |
|
1229 |
||
1230 |
QVERIFY(receiver->bytesAvailable() > oldBytesAvailable); |
|
1231 |
} |
|
1232 |
||
1233 |
class SetReadBufferSize_task_250027_handler : public QObject { |
|
1234 |
Q_OBJECT |
|
1235 |
public slots: |
|
1236 |
void readyReadSlot() { |
|
1237 |
QTestEventLoop::instance().exitLoop(); |
|
1238 |
} |
|
1239 |
void waitSomeMore(QSslSocket *socket) { |
|
1240 |
QTime t; |
|
1241 |
t.start(); |
|
1242 |
while (!socket->encryptedBytesAvailable()) { |
|
1243 |
QCoreApplication::processEvents(QEventLoop::AllEvents | QEventLoop::WaitForMoreEvents, 250); |
|
1244 |
if (t.elapsed() > 1000 || socket->state() != QAbstractSocket::ConnectedState) |
|
1245 |
return; |
|
1246 |
} |
|
1247 |
} |
|
1248 |
}; |
|
1249 |
||
1250 |
void tst_QSslSocket::setReadBufferSize_task_250027() |
|
1251 |
{ |
|
1252 |
// do not execute this when a proxy is set. |
|
1253 |
QFETCH_GLOBAL(bool, setProxy); |
|
1254 |
if (setProxy) |
|
1255 |
return; |
|
1256 |
||
1257 |
QSslSocketPtr socket = newSocket(); |
|
1258 |
socket->setReadBufferSize(1000); // limit to 1 kb/sec |
|
1259 |
socket->ignoreSslErrors(); |
|
1260 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1261 |
socket->ignoreSslErrors(); |
|
1262 |
QVERIFY(socket->waitForConnected(10*1000)); |
|
1263 |
QVERIFY(socket->waitForEncrypted(10*1000)); |
|
1264 |
||
1265 |
// exit the event loop as soon as we receive a readyRead() |
|
1266 |
SetReadBufferSize_task_250027_handler setReadBufferSize_task_250027_handler; |
|
1267 |
connect(socket, SIGNAL(readyRead()), &setReadBufferSize_task_250027_handler, SLOT(readyReadSlot())); |
|
1268 |
||
1269 |
// provoke a response by sending a request |
|
1270 |
socket->write("GET /gif/fluke.gif HTTP/1.0\n"); // this file is 27 KB |
|
1271 |
socket->write("Host: "); |
|
1272 |
socket->write(QtNetworkSettings::serverName().toLocal8Bit().constData()); |
|
1273 |
socket->write("\n"); |
|
1274 |
socket->write("Connection: close\n"); |
|
1275 |
socket->write("\n"); |
|
1276 |
socket->flush(); |
|
1277 |
||
1278 |
QTestEventLoop::instance().enterLoop(10); |
|
1279 |
setReadBufferSize_task_250027_handler.waitSomeMore(socket); |
|
1280 |
QByteArray firstRead = socket->readAll(); |
|
1281 |
// First read should be some data, but not the whole file |
|
1282 |
QVERIFY(firstRead.size() > 0 && firstRead.size() < 20*1024); |
|
1283 |
||
1284 |
QTestEventLoop::instance().enterLoop(10); |
|
1285 |
setReadBufferSize_task_250027_handler.waitSomeMore(socket); |
|
1286 |
QByteArray secondRead = socket->readAll(); |
|
1287 |
// second read should be some more data |
|
1288 |
QVERIFY(secondRead.size() > 0); |
|
1289 |
||
1290 |
socket->close(); |
|
1291 |
} |
|
1292 |
||
1293 |
class SslServer3 : public QTcpServer |
|
1294 |
{ |
|
1295 |
Q_OBJECT |
|
1296 |
public: |
|
1297 |
SslServer3() : socket(0) { } |
|
1298 |
QSslSocket *socket; |
|
1299 |
||
1300 |
protected: |
|
1301 |
void incomingConnection(int socketDescriptor) |
|
1302 |
{ |
|
1303 |
socket = new QSslSocket(this); |
|
1304 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
1305 |
||
1306 |
QFile file(SRCDIR "certs/fluke.key"); |
|
1307 |
QVERIFY(file.open(QIODevice::ReadOnly)); |
|
1308 |
QSslKey key(file.readAll(), QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey); |
|
1309 |
QVERIFY(!key.isNull()); |
|
1310 |
socket->setPrivateKey(key); |
|
1311 |
||
1312 |
QList<QSslCertificate> localCert = QSslCertificate::fromPath(SRCDIR "certs/fluke.cert"); |
|
1313 |
QVERIFY(!localCert.isEmpty()); |
|
1314 |
QVERIFY(localCert.first().handle()); |
|
1315 |
socket->setLocalCertificate(localCert.first()); |
|
1316 |
||
1317 |
QVERIFY(socket->setSocketDescriptor(socketDescriptor, QAbstractSocket::ConnectedState)); |
|
1318 |
QVERIFY(!socket->peerAddress().isNull()); |
|
1319 |
QVERIFY(socket->peerPort() != 0); |
|
1320 |
QVERIFY(!socket->localAddress().isNull()); |
|
1321 |
QVERIFY(socket->localPort() != 0); |
|
1322 |
} |
|
1323 |
||
1324 |
protected slots: |
|
1325 |
void ignoreErrorSlot() |
|
1326 |
{ |
|
1327 |
socket->ignoreSslErrors(); |
|
1328 |
} |
|
1329 |
}; |
|
1330 |
||
1331 |
class ThreadedSslServer: public QThread |
|
1332 |
{ |
|
1333 |
Q_OBJECT |
|
1334 |
public: |
|
1335 |
QSemaphore dataReadSemaphore; |
|
1336 |
int serverPort; |
|
1337 |
bool ok; |
|
1338 |
||
1339 |
ThreadedSslServer() : serverPort(-1), ok(false) |
|
1340 |
{ } |
|
1341 |
||
1342 |
~ThreadedSslServer() |
|
1343 |
{ |
|
1344 |
if (isRunning()) wait(2000); |
|
1345 |
QVERIFY(ok); |
|
1346 |
} |
|
1347 |
||
1348 |
signals: |
|
1349 |
void listening(); |
|
1350 |
||
1351 |
protected: |
|
1352 |
void run() |
|
1353 |
{ |
|
1354 |
// if all goes well (no timeouts), this thread will sleep for a total of 500 ms |
|
1355 |
// (i.e., 5 times 100 ms, one sleep for each operation) |
|
1356 |
||
1357 |
SslServer3 server; |
|
1358 |
server.listen(QHostAddress::LocalHost); |
|
1359 |
serverPort = server.serverPort(); |
|
1360 |
emit listening(); |
|
1361 |
||
1362 |
// delayed acceptance: |
|
1363 |
QTest::qSleep(100); |
|
1364 |
#ifndef Q_OS_SYMBIAN |
|
1365 |
bool ret = server.waitForNewConnection(2000); |
|
1366 |
#else |
|
1367 |
bool ret = server.waitForNewConnection(20000); |
|
1368 |
#endif |
|
1369 |
||
1370 |
// delayed start of encryption |
|
1371 |
QTest::qSleep(100); |
|
1372 |
QSslSocket *socket = server.socket; |
|
1373 |
Q_ASSERT(socket); |
|
1374 |
Q_ASSERT(socket->isValid()); |
|
1375 |
socket->ignoreSslErrors(); |
|
1376 |
socket->startServerEncryption(); |
|
1377 |
if (!socket->waitForEncrypted(2000)) |
|
1378 |
return; // error |
|
1379 |
||
1380 |
// delayed reading data |
|
1381 |
QTest::qSleep(100); |
|
1382 |
if (!socket->waitForReadyRead(2000)) |
|
1383 |
return; // error |
|
1384 |
socket->readAll(); |
|
1385 |
dataReadSemaphore.release(); |
|
1386 |
||
1387 |
// delayed sending data |
|
1388 |
QTest::qSleep(100); |
|
1389 |
socket->write("Hello, World"); |
|
1390 |
while (socket->bytesToWrite()) |
|
1391 |
if (!socket->waitForBytesWritten(2000)) |
|
1392 |
return; // error |
|
1393 |
||
1394 |
// delayed replying (reading then sending) |
|
1395 |
QTest::qSleep(100); |
|
1396 |
if (!socket->waitForReadyRead(2000)) |
|
1397 |
return; // error |
|
1398 |
socket->write("Hello, World"); |
|
1399 |
while (socket->bytesToWrite()) |
|
1400 |
if (!socket->waitForBytesWritten(2000)) |
|
1401 |
return; // error |
|
1402 |
||
1403 |
// delayed disconnection: |
|
1404 |
QTest::qSleep(100); |
|
1405 |
socket->disconnectFromHost(); |
|
1406 |
if (!socket->waitForDisconnected(2000)) |
|
1407 |
return; // error |
|
1408 |
||
1409 |
delete socket; |
|
1410 |
ok = true; |
|
1411 |
} |
|
1412 |
}; |
|
1413 |
||
1414 |
void tst_QSslSocket::waitForMinusOne() |
|
1415 |
{ |
|
1416 |
QFETCH_GLOBAL(bool, setProxy); |
|
1417 |
if (setProxy) |
|
1418 |
return; |
|
1419 |
||
1420 |
ThreadedSslServer server; |
|
1421 |
connect(&server, SIGNAL(listening()), SLOT(exitLoop())); |
|
1422 |
||
1423 |
// start the thread and wait for it to be ready |
|
1424 |
server.start(); |
|
1425 |
enterLoop(1); |
|
1426 |
QVERIFY(!timeout()); |
|
1427 |
||
1428 |
// connect to the server |
|
1429 |
QSslSocket socket; |
|
1430 |
QTest::qSleep(100); |
|
1431 |
socket.connectToHost("127.0.0.1", server.serverPort); |
|
1432 |
QVERIFY(socket.waitForConnected(-1)); |
|
1433 |
socket.ignoreSslErrors(); |
|
1434 |
socket.startClientEncryption(); |
|
1435 |
||
1436 |
// first verification: this waiting should take 200 ms |
|
1437 |
QVERIFY2(socket.waitForEncrypted(-1), qPrintable(socket.errorString())); |
|
1438 |
QVERIFY(socket.isEncrypted()); |
|
1439 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
1440 |
QCOMPARE(socket.bytesAvailable(), Q_INT64_C(0)); |
|
1441 |
||
1442 |
// second verification: write and make sure the other side got it (100 ms) |
|
1443 |
socket.write("How are you doing?"); |
|
1444 |
QVERIFY(socket.bytesToWrite() != 0); |
|
1445 |
QVERIFY(socket.waitForBytesWritten(-1)); |
|
1446 |
QVERIFY(server.dataReadSemaphore.tryAcquire(1, 2000)); |
|
1447 |
||
1448 |
// third verification: it should wait for 100 ms: |
|
1449 |
QVERIFY(socket.waitForReadyRead(-1)); |
|
1450 |
QVERIFY(socket.isEncrypted()); |
|
1451 |
QCOMPARE(socket.state(), QAbstractSocket::ConnectedState); |
|
1452 |
QVERIFY(socket.bytesAvailable() != 0); |
|
1453 |
||
1454 |
// fourth verification: deadlock prevention: |
|
1455 |
// we write and then wait for reading; the other side needs to receive before |
|
1456 |
// replying (100 ms delay) |
|
1457 |
socket.write("I'm doing just fine!"); |
|
1458 |
QVERIFY(socket.bytesToWrite() != 0); |
|
1459 |
QVERIFY(socket.waitForReadyRead(-1)); |
|
1460 |
||
1461 |
// fifth verification: it should wait for 200 ms more |
|
1462 |
QVERIFY(socket.waitForDisconnected(-1)); |
|
1463 |
} |
|
1464 |
||
1465 |
class VerifyServer : public QTcpServer |
|
1466 |
{ |
|
1467 |
Q_OBJECT |
|
1468 |
public: |
|
1469 |
VerifyServer() : socket(0) { } |
|
1470 |
QSslSocket *socket; |
|
1471 |
||
1472 |
protected: |
|
1473 |
void incomingConnection(int socketDescriptor) |
|
1474 |
{ |
|
1475 |
socket = new QSslSocket(this); |
|
1476 |
||
1477 |
socket->setPrivateKey(SRCDIR "certs/fluke.key"); |
|
1478 |
socket->setLocalCertificate(SRCDIR "certs/fluke.cert"); |
|
1479 |
socket->setSocketDescriptor(socketDescriptor); |
|
1480 |
socket->startServerEncryption(); |
|
1481 |
} |
|
1482 |
}; |
|
1483 |
||
1484 |
void tst_QSslSocket::verifyMode() |
|
1485 |
{ |
|
1486 |
QFETCH_GLOBAL(bool, setProxy); |
|
1487 |
if (setProxy) |
|
1488 |
return; |
|
1489 |
||
1490 |
QSslSocket socket; |
|
1491 |
QCOMPARE(socket.peerVerifyMode(), QSslSocket::AutoVerifyPeer); |
|
1492 |
socket.setPeerVerifyMode(QSslSocket::VerifyNone); |
|
1493 |
QCOMPARE(socket.peerVerifyMode(), QSslSocket::VerifyNone); |
|
1494 |
socket.setPeerVerifyMode(QSslSocket::VerifyNone); |
|
1495 |
socket.setPeerVerifyMode(QSslSocket::VerifyPeer); |
|
1496 |
QCOMPARE(socket.peerVerifyMode(), QSslSocket::VerifyPeer); |
|
1497 |
||
1498 |
socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1499 |
QVERIFY(!socket.waitForEncrypted()); |
|
1500 |
||
1501 |
QList<QSslError> expectedErrors = QList<QSslError>() |
|
1502 |
<< QSslError(QSslError::SelfSignedCertificate, socket.peerCertificate()); |
|
1503 |
QCOMPARE(socket.sslErrors(), expectedErrors); |
|
1504 |
socket.abort(); |
|
1505 |
||
1506 |
VerifyServer server; |
|
1507 |
server.listen(); |
|
1508 |
||
1509 |
QSslSocket clientSocket; |
|
1510 |
clientSocket.connectToHostEncrypted("127.0.0.1", server.serverPort()); |
|
1511 |
clientSocket.ignoreSslErrors(); |
|
1512 |
||
1513 |
QEventLoop loop; |
|
1514 |
QTimer::singleShot(5000, &loop, SLOT(quit())); |
|
1515 |
connect(&clientSocket, SIGNAL(encrypted()), &loop, SLOT(quit())); |
|
1516 |
loop.exec(); |
|
1517 |
||
1518 |
QVERIFY(clientSocket.isEncrypted()); |
|
1519 |
QVERIFY(server.socket->sslErrors().isEmpty()); |
|
1520 |
} |
|
1521 |
||
1522 |
void tst_QSslSocket::verifyDepth() |
|
1523 |
{ |
|
1524 |
QSslSocket socket; |
|
1525 |
QCOMPARE(socket.peerVerifyDepth(), 0); |
|
1526 |
socket.setPeerVerifyDepth(1); |
|
1527 |
QCOMPARE(socket.peerVerifyDepth(), 1); |
|
1528 |
QTest::ignoreMessage(QtWarningMsg, "QSslSocket::setPeerVerifyDepth: cannot set negative depth of -1"); |
|
1529 |
socket.setPeerVerifyDepth(-1); |
|
1530 |
QCOMPARE(socket.peerVerifyDepth(), 1); |
|
1531 |
} |
|
1532 |
||
1533 |
void tst_QSslSocket::peerVerifyError() |
|
1534 |
{ |
|
1535 |
QSslSocketPtr socket = newSocket(); |
|
1536 |
QSignalSpy sslErrorsSpy(socket, SIGNAL(sslErrors(QList<QSslError>))); |
|
1537 |
QSignalSpy peerVerifyErrorSpy(socket, SIGNAL(peerVerifyError(QSslError))); |
|
1538 |
||
1539 |
socket->connectToHostEncrypted(QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(), 443); |
|
1540 |
QVERIFY(!socket->waitForEncrypted(10000)); |
|
1541 |
QVERIFY(!peerVerifyErrorSpy.isEmpty()); |
|
1542 |
QVERIFY(!sslErrorsSpy.isEmpty()); |
|
1543 |
QCOMPARE(qVariantValue<QSslError>(peerVerifyErrorSpy.last().at(0)).error(), QSslError::HostNameMismatch); |
|
1544 |
QCOMPARE(qVariantValue<QList<QSslError> >(sslErrorsSpy.at(0).at(0)).size(), peerVerifyErrorSpy.size()); |
|
1545 |
} |
|
1546 |
||
1547 |
void tst_QSslSocket::disconnectFromHostWhenConnecting() |
|
1548 |
{ |
|
1549 |
QSslSocketPtr socket = newSocket(); |
|
1550 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 993); |
|
1551 |
socket->ignoreSslErrors(); |
|
1552 |
socket->write("XXXX LOGOUT\r\n"); |
|
1553 |
QAbstractSocket::SocketState state = socket->state(); |
|
1554 |
// without proxy, the state will be HostLookupState; |
|
1555 |
// with proxy, the state will be ConnectingState. |
|
1556 |
QVERIFY(socket->state() == QAbstractSocket::HostLookupState || |
|
1557 |
socket->state() == QAbstractSocket::ConnectingState); |
|
1558 |
socket->disconnectFromHost(); |
|
1559 |
// the state of the socket must be the same before and after calling |
|
1560 |
// disconnectFromHost() |
|
1561 |
QCOMPARE(state, socket->state()); |
|
1562 |
QVERIFY(socket->state() == QAbstractSocket::HostLookupState || |
|
1563 |
socket->state() == QAbstractSocket::ConnectingState); |
|
1564 |
QVERIFY(socket->waitForDisconnected(5000)); |
|
1565 |
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState); |
|
1566 |
// we did not call close, so the socket must be still open |
|
1567 |
QVERIFY(socket->isOpen()); |
|
1568 |
QCOMPARE(socket->bytesToWrite(), qint64(0)); |
|
1569 |
||
1570 |
// dont forget to login |
|
1571 |
QCOMPARE((int) socket->write("USER ftptest\r\n"), 14); |
|
1572 |
||
1573 |
} |
|
1574 |
||
1575 |
void tst_QSslSocket::disconnectFromHostWhenConnected() |
|
1576 |
{ |
|
1577 |
QSslSocketPtr socket = newSocket(); |
|
1578 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 993); |
|
1579 |
socket->ignoreSslErrors(); |
|
1580 |
#ifndef Q_OS_SYMBIAN |
|
1581 |
QVERIFY(socket->waitForEncrypted(5000)); |
|
1582 |
#else |
|
1583 |
QVERIFY(socket->waitForEncrypted(10000)); |
|
1584 |
#endif |
|
1585 |
socket->write("XXXX LOGOUT\r\n"); |
|
1586 |
QCOMPARE(socket->state(), QAbstractSocket::ConnectedState); |
|
1587 |
socket->disconnectFromHost(); |
|
1588 |
QCOMPARE(socket->state(), QAbstractSocket::ClosingState); |
|
1589 |
#ifdef Q_OS_SYMBIAN |
|
1590 |
// I don't understand how socket->waitForDisconnected can work on other platforms |
|
1591 |
// since socket->write will end to: |
|
1592 |
// QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection); |
|
1593 |
// In order that _q_flushWriteBuffer will be called the eventloop need to run |
|
1594 |
// If we just call waitForDisconnected, which blocks the whole thread how that can happen? |
|
1595 |
connect(socket, SIGNAL(disconnected()), this, SLOT(exitLoop())); |
|
1596 |
enterLoop(5); |
|
1597 |
QVERIFY(!timeout()); |
|
1598 |
#else |
|
1599 |
QVERIFY(socket->waitForDisconnected(5000)); |
|
1600 |
#endif |
|
1601 |
QCOMPARE(socket->bytesToWrite(), qint64(0)); |
|
1602 |
} |
|
1603 |
||
1604 |
void tst_QSslSocket::resetProxy() |
|
1605 |
{ |
|
1606 |
QFETCH_GLOBAL(bool, setProxy); |
|
1607 |
if (setProxy) |
|
1608 |
return; |
|
1609 |
||
1610 |
// check fix for bug 199941 |
|
1611 |
||
1612 |
QNetworkProxy goodProxy(QNetworkProxy::NoProxy); |
|
1613 |
QNetworkProxy badProxy(QNetworkProxy::HttpProxy, "thisCannotWorkAbsolutelyNotForSure", 333); |
|
1614 |
||
1615 |
// make sure the connection works, and then set a nonsense proxy, and then |
|
1616 |
// make sure it does not work anymore |
|
1617 |
QSslSocket socket; |
|
1618 |
socket.addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
1619 |
socket.setProxy(goodProxy); |
|
1620 |
socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1621 |
QVERIFY2(socket.waitForConnected(10000), qPrintable(socket.errorString())); |
|
1622 |
socket.abort(); |
|
1623 |
socket.setProxy(badProxy); |
|
1624 |
socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1625 |
QVERIFY(! socket.waitForConnected(10000)); |
|
1626 |
||
1627 |
// dont forget to login |
|
1628 |
QCOMPARE((int) socket.write("USER ftptest\r\n"), 14); |
|
1629 |
QCOMPARE((int) socket.write("PASS password\r\n"), 15); |
|
1630 |
||
1631 |
enterLoop(10); |
|
1632 |
||
1633 |
// now the other way round: |
|
1634 |
// set the nonsense proxy and make sure the connection does not work, |
|
1635 |
// and then set the right proxy and make sure it works |
|
1636 |
QSslSocket socket2; |
|
1637 |
socket2.addCaCertificates(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
1638 |
socket2.setProxy(badProxy); |
|
1639 |
socket2.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1640 |
QVERIFY(! socket2.waitForConnected(10000)); |
|
1641 |
socket2.abort(); |
|
1642 |
socket2.setProxy(goodProxy); |
|
1643 |
socket2.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1644 |
QVERIFY2(socket2.waitForConnected(10000), qPrintable(socket.errorString())); |
|
1645 |
} |
|
1646 |
||
1647 |
void tst_QSslSocket::ignoreSslErrorsList_data() |
|
1648 |
{ |
|
1649 |
QTest::addColumn<QList<QSslError> >("expectedSslErrors"); |
|
1650 |
QTest::addColumn<int>("expectedSslErrorSignalCount"); |
|
1651 |
||
1652 |
// construct the list of errors that we will get with the SSL handshake and that we will ignore |
|
1653 |
QList<QSslError> expectedSslErrors; |
|
1654 |
// fromPath gives us a list of certs, but it actually only contains one |
|
1655 |
QList<QSslCertificate> certs = QSslCertificate::fromPath(QLatin1String(SRCDIR "certs/qt-test-server-cacert.pem")); |
|
1656 |
QSslError rightError(QSslError::SelfSignedCertificate, certs.at(0)); |
|
1657 |
QSslError wrongError(QSslError::SelfSignedCertificate); |
|
1658 |
||
1659 |
||
1660 |
QTest::newRow("SSL-failure-empty-list") << expectedSslErrors << 1; |
|
1661 |
expectedSslErrors.append(wrongError); |
|
1662 |
QTest::newRow("SSL-failure-wrong-error") << expectedSslErrors << 1; |
|
1663 |
expectedSslErrors.append(rightError); |
|
1664 |
QTest::newRow("allErrorsInExpectedList1") << expectedSslErrors << 0; |
|
1665 |
expectedSslErrors.removeAll(wrongError); |
|
1666 |
QTest::newRow("allErrorsInExpectedList2") << expectedSslErrors << 0; |
|
1667 |
expectedSslErrors.removeAll(rightError); |
|
1668 |
QTest::newRow("SSL-failure-empty-list-again") << expectedSslErrors << 1; |
|
1669 |
} |
|
1670 |
||
1671 |
void tst_QSslSocket::ignoreSslErrorsList() |
|
1672 |
{ |
|
1673 |
QSslSocket socket; |
|
1674 |
connect(&socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1675 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1676 |
||
1677 |
// this->socket = &socket; |
|
1678 |
QSslCertificate cert; |
|
1679 |
||
1680 |
QFETCH(QList<QSslError>, expectedSslErrors); |
|
1681 |
socket.ignoreSslErrors(expectedSslErrors); |
|
1682 |
||
1683 |
QFETCH(int, expectedSslErrorSignalCount); |
|
1684 |
QSignalSpy sslErrorsSpy(&socket, SIGNAL(error(QAbstractSocket::SocketError))); |
|
1685 |
||
1686 |
socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1687 |
||
1688 |
bool expectEncryptionSuccess = (expectedSslErrorSignalCount == 0); |
|
1689 |
QCOMPARE(socket.waitForEncrypted(10000), expectEncryptionSuccess); |
|
1690 |
QCOMPARE(sslErrorsSpy.count(), expectedSslErrorSignalCount); |
|
1691 |
} |
|
1692 |
||
1693 |
void tst_QSslSocket::ignoreSslErrorsListWithSlot_data() |
|
1694 |
{ |
|
1695 |
ignoreSslErrorsList_data(); |
|
1696 |
} |
|
1697 |
||
1698 |
// this is not a test, just a slot called in the test below |
|
1699 |
void tst_QSslSocket::ignoreErrorListSlot(const QList<QSslError> &) |
|
1700 |
{ |
|
1701 |
socket->ignoreSslErrors(storedExpectedSslErrors); |
|
1702 |
} |
|
1703 |
||
1704 |
void tst_QSslSocket::ignoreSslErrorsListWithSlot() |
|
1705 |
{ |
|
1706 |
QSslSocket socket; |
|
1707 |
this->socket = &socket; |
|
1708 |
||
1709 |
QFETCH(QList<QSslError>, expectedSslErrors); |
|
1710 |
// store the errors to ignore them later in the slot connected below |
|
1711 |
storedExpectedSslErrors = expectedSslErrors; |
|
1712 |
connect(&socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1713 |
this, SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1714 |
connect(&socket, SIGNAL(sslErrors(const QList<QSslError> &)), |
|
1715 |
this, SLOT(ignoreErrorListSlot(const QList<QSslError> &))); |
|
1716 |
socket.connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1717 |
||
1718 |
QFETCH(int, expectedSslErrorSignalCount); |
|
1719 |
bool expectEncryptionSuccess = (expectedSslErrorSignalCount == 0); |
|
1720 |
QCOMPARE(socket.waitForEncrypted(10000), expectEncryptionSuccess); |
|
1721 |
} |
|
1722 |
||
1723 |
// make sure a closed socket has no bytesAvailable() |
|
1724 |
// related to https://bugs.webkit.org/show_bug.cgi?id=28016 |
|
1725 |
void tst_QSslSocket::readFromClosedSocket() |
|
1726 |
{ |
|
1727 |
QSslSocketPtr socket = newSocket(); |
|
1728 |
socket->ignoreSslErrors(); |
|
1729 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1730 |
socket->ignoreSslErrors(); |
|
1731 |
socket->waitForConnected(); |
|
1732 |
socket->waitForEncrypted(); |
|
1733 |
// provoke a response by sending a request |
|
1734 |
socket->write("GET /gif/fluke.gif HTTP/1.1\n"); |
|
1735 |
socket->write("Host: "); |
|
1736 |
socket->write(QtNetworkSettings::serverName().toLocal8Bit().constData()); |
|
1737 |
socket->write("\n"); |
|
1738 |
socket->write("\n"); |
|
1739 |
socket->waitForBytesWritten(); |
|
1740 |
socket->waitForReadyRead(); |
|
1741 |
QVERIFY(socket->state() == QAbstractSocket::ConnectedState); |
|
1742 |
QVERIFY(socket->bytesAvailable()); |
|
1743 |
socket->close(); |
|
1744 |
QVERIFY(!socket->bytesAvailable()); |
|
1745 |
QVERIFY(!socket->bytesToWrite()); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1746 |
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState); |
0 | 1747 |
} |
1748 |
||
1749 |
void tst_QSslSocket::writeBigChunk() |
|
1750 |
{ |
|
1751 |
if (!QSslSocket::supportsSsl()) |
|
1752 |
return; |
|
1753 |
||
1754 |
QSslSocketPtr socket = newSocket(); |
|
1755 |
this->socket = socket; |
|
1756 |
||
1757 |
connect(socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(ignoreErrorSlot())); |
|
1758 |
socket->connectToHostEncrypted(QtNetworkSettings::serverName(), 443); |
|
1759 |
||
1760 |
QByteArray data; |
|
1761 |
data.resize(1024*1024*10); // 10 MB |
|
1762 |
// init with garbage. needed so ssl cannot compress it in an efficient way. |
|
1763 |
for (int i = 0; i < data.size() / sizeof(int); i++) { |
|
1764 |
int r = qrand(); |
|
1765 |
data.data()[i*sizeof(int)] = r; |
|
1766 |
} |
|
1767 |
||
1768 |
QVERIFY(socket->waitForEncrypted(10000)); |
|
1769 |
QString errorBefore = socket->errorString(); |
|
1770 |
||
1771 |
int ret = socket->write(data.constData(), data.size()); |
|
1772 |
QVERIFY(data.size() == ret); |
|
1773 |
||
1774 |
// spin the event loop once so QSslSocket::transmit() gets called |
|
1775 |
QCoreApplication::processEvents(); |
|
1776 |
QString errorAfter = socket->errorString(); |
|
1777 |
||
1778 |
// no better way to do this right now since the error is the same as the default error. |
|
1779 |
if (socket->errorString().startsWith(QLatin1String("Unable to write data"))) |
|
1780 |
{ |
|
1781 |
qWarning() << socket->error() << socket->errorString(); |
|
1782 |
QFAIL("Error while writing! Check if the OpenSSL BIO size is limited?!"); |
|
1783 |
} |
|
1784 |
// also check the error string. If another error (than UnknownError) occured, it should be different than before |
|
1785 |
QVERIFY(errorBefore == errorAfter); |
|
1786 |
||
1787 |
// check that everything has been written to OpenSSL |
|
1788 |
QVERIFY(socket->bytesToWrite() == 0); |
|
1789 |
||
1790 |
socket->close(); |
|
1791 |
} |
|
1792 |
||
1793 |
#endif // QT_NO_OPENSSL |
|
1794 |
||
1795 |
QTEST_MAIN(tst_QSslSocket) |
|
1796 |
#include "tst_qsslsocket.moc" |