author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 |
// Just to get Q_OS_SYMBIAN |
|
44 |
#include <qglobal.h> |
|
45 |
||
46 |
#if defined(_WIN32) && !defined(Q_OS_SYMBIAN) |
|
47 |
#include <winsock2.h> |
|
48 |
#else |
|
49 |
#include <sys/types.h> |
|
50 |
#include <sys/socket.h> |
|
51 |
#include <fcntl.h> |
|
52 |
#include <unistd.h> |
|
53 |
#define SOCKET int |
|
54 |
#define INVALID_SOCKET -1 |
|
55 |
#endif |
|
56 |
||
57 |
#include <qplatformdefs.h> |
|
58 |
||
59 |
#include <QtTest/QtTest> |
|
60 |
||
61 |
#include <QAuthenticator> |
|
62 |
#include <QCoreApplication> |
|
63 |
#include <QEventLoop> |
|
64 |
#include <QFile> |
|
65 |
#include <QHostAddress> |
|
66 |
#include <QHostInfo> |
|
67 |
#include <QMap> |
|
68 |
#ifndef Q_OS_VXWORKS |
|
69 |
#include <QMessageBox> |
|
70 |
#include <QPushButton> |
|
71 |
#endif |
|
72 |
#include <QPointer> |
|
73 |
#include <QProcess> |
|
74 |
#include <QStringList> |
|
75 |
#include <QTcpServer> |
|
76 |
#include <QTcpSocket> |
|
77 |
#ifndef QT_NO_OPENSSL |
|
78 |
#include <QSslSocket> |
|
79 |
#endif |
|
80 |
#include <QTextStream> |
|
81 |
#include <QThread> |
|
82 |
#include <QTime> |
|
83 |
#include <QTimer> |
|
84 |
#include <QDebug> |
|
85 |
#ifndef TEST_QNETWORK_PROXY |
|
86 |
#define TEST_QNETWORK_PROXY |
|
87 |
#endif |
|
88 |
// RVCT compiles also unused inline methods |
|
89 |
# include <QNetworkProxy> |
|
90 |
||
91 |
#ifdef Q_OS_LINUX |
|
92 |
#include <stdio.h> |
|
93 |
#include <stdlib.h> |
|
94 |
#include <sys/stat.h> |
|
95 |
#include <unistd.h> |
|
96 |
#endif |
|
97 |
||
98 |
#include "../network-settings.h" |
|
99 |
||
100 |
Q_DECLARE_METATYPE(QAbstractSocket::SocketError) |
|
101 |
Q_DECLARE_METATYPE(QAbstractSocket::SocketState) |
|
102 |
Q_DECLARE_METATYPE(QNetworkProxy) |
|
103 |
Q_DECLARE_METATYPE(QList<QNetworkProxy>) |
|
104 |
||
105 |
//TESTED_CLASS= |
|
106 |
//TESTED_FILES= |
|
107 |
||
108 |
QT_FORWARD_DECLARE_CLASS(QTcpSocket) |
|
109 |
||
110 |
class tst_QTcpSocket : public QObject |
|
111 |
{ |
|
112 |
Q_OBJECT |
|
113 |
||
114 |
public: |
|
115 |
tst_QTcpSocket(); |
|
116 |
virtual ~tst_QTcpSocket(); |
|
117 |
||
118 |
static void enterLoop(int secs) |
|
119 |
{ |
|
120 |
++loopLevel; |
|
121 |
QTestEventLoop::instance().enterLoop(secs); |
|
122 |
--loopLevel; |
|
123 |
} |
|
124 |
static void exitLoop() |
|
125 |
{ |
|
126 |
// Safe exit - if we aren't in an event loop, don't |
|
127 |
// exit one. |
|
128 |
if (loopLevel > 0) |
|
129 |
QTestEventLoop::instance().exitLoop(); |
|
130 |
} |
|
131 |
static bool timeout() |
|
132 |
{ |
|
133 |
return QTestEventLoop::instance().timeout(); |
|
134 |
} |
|
135 |
||
136 |
public slots: |
|
137 |
void initTestCase_data(); |
|
138 |
void init(); |
|
139 |
void cleanup(); |
|
140 |
private slots: |
|
141 |
void constructing(); |
|
142 |
void setInvalidSocketDescriptor(); |
|
143 |
void setSocketDescriptor(); |
|
144 |
void socketDescriptor(); |
|
145 |
void blockingIMAP(); |
|
146 |
void nonBlockingIMAP(); |
|
147 |
void hostNotFound(); |
|
148 |
void timeoutConnect(); |
|
149 |
void delayedClose(); |
|
150 |
void partialRead(); |
|
151 |
void unget(); |
|
152 |
void readAllAfterClose(); |
|
153 |
void openCloseOpenClose(); |
|
154 |
void connectDisconnectConnectDisconnect(); |
|
155 |
void disconnectWhileConnecting_data(); |
|
156 |
void disconnectWhileConnecting(); |
|
157 |
void disconnectWhileConnectingNoEventLoop_data(); |
|
158 |
void disconnectWhileConnectingNoEventLoop(); |
|
159 |
void disconnectWhileLookingUp_data(); |
|
160 |
void disconnectWhileLookingUp(); |
|
161 |
void downloadBigFile(); |
|
162 |
void readLine(); |
|
163 |
void readLineString(); |
|
164 |
void readChunks(); |
|
165 |
void waitForBytesWritten(); |
|
166 |
void waitForReadyRead(); |
|
167 |
void flush(); |
|
168 |
void synchronousApi(); |
|
169 |
void dontCloseOnTimeout(); |
|
170 |
void recursiveReadyRead(); |
|
171 |
void atEnd(); |
|
172 |
void socketInAThread(); |
|
173 |
void socketsInThreads(); |
|
174 |
void waitForReadyReadInASlot(); |
|
175 |
void remoteCloseError(); |
|
176 |
void openMessageBoxInErrorSlot(); |
|
177 |
#ifndef Q_OS_WIN |
|
178 |
void connectToLocalHostNoService(); |
|
179 |
#endif |
|
180 |
void waitForConnectedInHostLookupSlot(); |
|
181 |
void waitForConnectedInHostLookupSlot2(); |
|
182 |
void readyReadSignalsAfterWaitForReadyRead(); |
|
183 |
#ifdef Q_OS_LINUX |
|
184 |
void linuxKernelBugLocalSocket(); |
|
185 |
#endif |
|
186 |
void abortiveClose(); |
|
187 |
void localAddressEmptyOnBSD(); |
|
188 |
void zeroAndMinusOneReturns(); |
|
189 |
void connectionRefused(); |
|
190 |
void suddenRemoteDisconnect_data(); |
|
191 |
void suddenRemoteDisconnect(); |
|
192 |
void connectToMultiIP(); |
|
193 |
void moveToThread0(); |
|
194 |
void increaseReadBufferSize(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
195 |
void taskQtBug5799ConnectionErrorWaitForConnected(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
196 |
void taskQtBug5799ConnectionErrorEventLoop(); |
0 | 197 |
#ifdef TEST_QNETWORK_PROXY |
198 |
void invalidProxy_data(); |
|
199 |
void invalidProxy(); |
|
200 |
void proxyFactory_data(); |
|
201 |
void proxyFactory(); |
|
202 |
#endif |
|
203 |
||
204 |
protected slots: |
|
205 |
void nonBlockingIMAP_hostFound(); |
|
206 |
void nonBlockingIMAP_connected(); |
|
207 |
void nonBlockingIMAP_closed(); |
|
208 |
void nonBlockingIMAP_readyRead(); |
|
209 |
void nonBlockingIMAP_bytesWritten(qint64); |
|
210 |
void readRegularFile_readyRead(); |
|
211 |
void exitLoopSlot(); |
|
212 |
void downloadBigFileSlot(); |
|
213 |
void recursiveReadyReadSlot(); |
|
214 |
void waitForReadyReadInASlotSlot(); |
|
215 |
void messageBoxSlot(); |
|
216 |
void hostLookupSlot(); |
|
217 |
void abortiveClose_abortSlot(); |
|
218 |
void remoteCloseErrorSlot(); |
|
219 |
void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth); |
|
220 |
||
221 |
private: |
|
222 |
QTcpSocket *newSocket() const; |
|
223 |
QTcpSocket *nonBlockingIMAP_socket; |
|
224 |
QStringList nonBlockingIMAP_data; |
|
225 |
qint64 nonBlockingIMAP_totalWritten; |
|
226 |
||
227 |
QTcpSocket *tmpSocket; |
|
228 |
qint64 bytesAvailable; |
|
229 |
qint64 expectedLength; |
|
230 |
bool readingBody; |
|
231 |
QTime timer; |
|
232 |
||
233 |
mutable int proxyAuthCalled; |
|
234 |
||
235 |
bool gotClosedSignal; |
|
236 |
int numConnections; |
|
237 |
static int loopLevel; |
|
238 |
}; |
|
239 |
||
240 |
enum ProxyTests { |
|
241 |
NoProxy = 0x00, |
|
242 |
Socks5Proxy = 0x01, |
|
243 |
HttpProxy = 0x02, |
|
244 |
TypeMask = 0x0f, |
|
245 |
||
246 |
NoAuth = 0x00, |
|
247 |
AuthBasic = 0x10, |
|
248 |
AuthNtlm = 0x20, |
|
249 |
AuthMask = 0xf0 |
|
250 |
}; |
|
251 |
||
252 |
int tst_QTcpSocket::loopLevel = 0; |
|
253 |
||
254 |
tst_QTcpSocket::tst_QTcpSocket() |
|
255 |
{ |
|
256 |
Q_SET_DEFAULT_IAP |
|
257 |
tmpSocket = 0; |
|
258 |
} |
|
259 |
||
260 |
tst_QTcpSocket::~tst_QTcpSocket() |
|
261 |
{ |
|
262 |
||
263 |
} |
|
264 |
||
265 |
void tst_QTcpSocket::initTestCase_data() |
|
266 |
{ |
|
267 |
QTest::addColumn<bool>("setProxy"); |
|
268 |
QTest::addColumn<int>("proxyType"); |
|
269 |
QTest::addColumn<bool>("ssl"); |
|
270 |
||
271 |
qDebug() << QtNetworkSettings::serverName(); |
|
272 |
QTest::newRow("WithoutProxy") << false << 0 << false; |
|
273 |
#ifdef TEST_QNETWORK_PROXY |
|
274 |
QTest::newRow("WithSocks5Proxy") << true << int(Socks5Proxy) << false; |
|
275 |
QTest::newRow("WithSocks5ProxyAuth") << true << int(Socks5Proxy | AuthBasic) << false; |
|
276 |
||
277 |
QTest::newRow("WithHttpProxy") << true << int(HttpProxy) << false; |
|
278 |
QTest::newRow("WithHttpProxyBasicAuth") << true << int(HttpProxy | AuthBasic) << false; |
|
279 |
// QTest::newRow("WithHttpProxyNtlmAuth") << true << int(HttpProxy | AuthNtlm) << false; |
|
280 |
#endif |
|
281 |
#ifndef QT_NO_OPENSSL |
|
282 |
QTest::newRow("WithoutProxy SSL") << false << 0 << true; |
|
283 |
#ifdef TEST_QNETWORK_PROXY |
|
284 |
QTest::newRow("WithSocks5Proxy SSL") << true << int(Socks5Proxy) << true; |
|
285 |
QTest::newRow("WithSocks5AuthProxy SSL") << true << int(Socks5Proxy | AuthBasic) << true; |
|
286 |
||
287 |
QTest::newRow("WithHttpProxy SSL") << true << int(HttpProxy) << true; |
|
288 |
QTest::newRow("WithHttpProxyBasicAuth SSL") << true << int(HttpProxy | AuthBasic) << true; |
|
289 |
// QTest::newRow("WithHttpProxyNtlmAuth SSL") << true << int(HttpProxy | AuthNtlm) << true; |
|
290 |
#endif |
|
291 |
#endif |
|
292 |
} |
|
293 |
||
294 |
void tst_QTcpSocket::init() |
|
295 |
{ |
|
296 |
QFETCH_GLOBAL(bool, setProxy); |
|
297 |
if (setProxy) { |
|
298 |
#ifdef TEST_QNETWORK_PROXY |
|
299 |
QFETCH_GLOBAL(int, proxyType); |
|
300 |
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(); |
|
301 |
QNetworkProxy proxy; |
|
302 |
||
303 |
switch (proxyType) { |
|
304 |
case Socks5Proxy: |
|
305 |
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1080); |
|
306 |
break; |
|
307 |
||
308 |
case Socks5Proxy | AuthBasic: |
|
309 |
proxy = QNetworkProxy(QNetworkProxy::Socks5Proxy, fluke, 1081); |
|
310 |
break; |
|
311 |
||
312 |
case HttpProxy | NoAuth: |
|
313 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3128); |
|
314 |
break; |
|
315 |
||
316 |
case HttpProxy | AuthBasic: |
|
317 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3129); |
|
318 |
break; |
|
319 |
||
320 |
case HttpProxy | AuthNtlm: |
|
321 |
proxy = QNetworkProxy(QNetworkProxy::HttpProxy, fluke, 3130); |
|
322 |
break; |
|
323 |
} |
|
324 |
QNetworkProxy::setApplicationProxy(proxy); |
|
325 |
#endif |
|
326 |
} |
|
327 |
} |
|
328 |
||
329 |
QTcpSocket *tst_QTcpSocket::newSocket() const |
|
330 |
{ |
|
331 |
QTcpSocket *socket; |
|
332 |
#ifndef QT_NO_OPENSSL |
|
333 |
QFETCH_GLOBAL(bool, ssl); |
|
334 |
socket = ssl ? new QSslSocket : new QTcpSocket; |
|
335 |
#else |
|
336 |
socket = new QTcpSocket; |
|
337 |
#endif |
|
338 |
||
339 |
proxyAuthCalled = 0; |
|
340 |
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
341 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
342 |
Qt::DirectConnection); |
|
343 |
return socket; |
|
344 |
} |
|
345 |
||
346 |
void tst_QTcpSocket::cleanup() |
|
347 |
{ |
|
348 |
#ifdef TEST_QNETWORK_PROXY |
|
349 |
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
|
350 |
#endif |
|
351 |
} |
|
352 |
||
353 |
void tst_QTcpSocket::proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) |
|
354 |
{ |
|
355 |
++proxyAuthCalled; |
|
356 |
auth->setUser("qsockstest"); |
|
357 |
auth->setPassword("password"); |
|
358 |
} |
|
359 |
||
360 |
//---------------------------------------------------------------------------------- |
|
361 |
||
362 |
void tst_QTcpSocket::constructing() |
|
363 |
{ |
|
364 |
QTcpSocket *socket = newSocket(); |
|
365 |
||
366 |
// Check the initial state of the QTcpSocket. |
|
367 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
368 |
QVERIFY(socket->isSequential()); |
|
369 |
QVERIFY(!socket->isOpen()); |
|
370 |
QVERIFY(!socket->isValid()); |
|
371 |
QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket); |
|
372 |
||
373 |
char c; |
|
374 |
QCOMPARE(socket->getChar(&c), false); |
|
375 |
QCOMPARE((int) socket->bytesAvailable(), 0); |
|
376 |
QCOMPARE(socket->canReadLine(), false); |
|
377 |
QCOMPARE(socket->readLine(), QByteArray()); |
|
378 |
QCOMPARE(socket->socketDescriptor(), -1); |
|
379 |
QCOMPARE((int) socket->localPort(), 0); |
|
380 |
QVERIFY(socket->localAddress() == QHostAddress()); |
|
381 |
QCOMPARE((int) socket->peerPort(), 0); |
|
382 |
QVERIFY(socket->peerAddress() == QHostAddress()); |
|
383 |
QCOMPARE(socket->error(), QTcpSocket::UnknownSocketError); |
|
384 |
QCOMPARE(socket->errorString(), QString("Unknown error")); |
|
385 |
||
386 |
// Check the state of the socket layer? |
|
387 |
delete socket; |
|
388 |
} |
|
389 |
||
390 |
//---------------------------------------------------------------------------------- |
|
391 |
||
392 |
void tst_QTcpSocket::setInvalidSocketDescriptor() |
|
393 |
{ |
|
394 |
QTcpSocket *socket = newSocket(); |
|
395 |
QCOMPARE(socket->socketDescriptor(), -1); |
|
396 |
QVERIFY(!socket->setSocketDescriptor(-5, QTcpSocket::UnconnectedState)); |
|
397 |
QCOMPARE(socket->socketDescriptor(), -1); |
|
398 |
||
399 |
QCOMPARE(socket->error(), QTcpSocket::UnsupportedSocketOperationError); |
|
400 |
||
401 |
delete socket; |
|
402 |
} |
|
403 |
||
404 |
//---------------------------------------------------------------------------------- |
|
405 |
||
406 |
void tst_QTcpSocket::setSocketDescriptor() |
|
407 |
{ |
|
408 |
QFETCH_GLOBAL(bool, setProxy); |
|
409 |
if (setProxy) |
|
410 |
return; // this test doesn't make sense with proxies |
|
411 |
||
412 |
#ifdef Q_OS_WIN |
|
413 |
// need the dummy to ensure winsock is started |
|
414 |
QTcpSocket *dummy = newSocket(); |
|
415 |
dummy->connectToHost(QtNetworkSettings::serverName(), 143); |
|
416 |
QVERIFY(dummy->waitForConnected()); |
|
417 |
||
418 |
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
419 |
if (sock == INVALID_SOCKET) { |
|
420 |
qErrnoWarning(WSAGetLastError(), "INVALID_SOCKET"); |
|
421 |
} |
|
422 |
#else |
|
423 |
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, 0); |
|
424 |
||
425 |
// artificially increase the value of sock |
|
426 |
SOCKET sock2 = ::fcntl(sock, F_DUPFD, sock + 50); |
|
427 |
::close(sock); |
|
428 |
sock = sock2; |
|
429 |
#endif |
|
430 |
||
431 |
QVERIFY(sock != INVALID_SOCKET); |
|
432 |
QTcpSocket *socket = newSocket(); |
|
433 |
QVERIFY(socket->setSocketDescriptor(sock, QTcpSocket::UnconnectedState)); |
|
434 |
QCOMPARE(socket->socketDescriptor(), (int)sock); |
|
435 |
||
436 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
437 |
QCOMPARE(socket->state(), QTcpSocket::HostLookupState); |
|
438 |
QCOMPARE(socket->socketDescriptor(), (int)sock); |
|
439 |
QVERIFY(socket->waitForConnected(10000)); |
|
440 |
// skip this, it has been broken for years, see task 260735 |
|
441 |
// if somebody complains, consider fixing it, but it might break existing applications. |
|
442 |
QEXPECT_FAIL("", "bug has been around for years, will not fix without need", Continue); |
|
443 |
QCOMPARE(socket->socketDescriptor(), (int)sock); |
|
444 |
delete socket; |
|
445 |
#ifdef Q_OS_WIN |
|
446 |
delete dummy; |
|
447 |
#endif |
|
448 |
} |
|
449 |
||
450 |
//---------------------------------------------------------------------------------- |
|
451 |
||
452 |
void tst_QTcpSocket::socketDescriptor() |
|
453 |
{ |
|
454 |
QTcpSocket *socket = newSocket(); |
|
455 |
||
456 |
QCOMPARE(socket->socketDescriptor(), -1); |
|
457 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
458 |
QVERIFY((socket->state() == QAbstractSocket::HostLookupState && socket->socketDescriptor() == -1) || |
|
459 |
(socket->state() == QAbstractSocket::ConnectingState && socket->socketDescriptor() != -1)); |
|
460 |
QVERIFY(socket->waitForConnected(10000)); |
|
461 |
QVERIFY(socket->state() == QAbstractSocket::ConnectedState); |
|
462 |
QVERIFY(socket->socketDescriptor() != -1); |
|
463 |
||
464 |
delete socket; |
|
465 |
} |
|
466 |
||
467 |
//---------------------------------------------------------------------------------- |
|
468 |
||
469 |
void tst_QTcpSocket::blockingIMAP() |
|
470 |
{ |
|
471 |
QTcpSocket *socket = newSocket(); |
|
472 |
||
473 |
// Connect |
|
474 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
475 |
QVERIFY(socket->waitForConnected(10000)); |
|
476 |
QCOMPARE(socket->state(), QTcpSocket::ConnectedState); |
|
477 |
QVERIFY(socket->isValid()); |
|
478 |
||
479 |
// Read greeting |
|
480 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
481 |
QString s = socket->readLine(); |
|
482 |
// only test if an OK was returned, to make the test compatible between different |
|
483 |
// IMAP server versions |
|
484 |
QCOMPARE(s.left(4).toLatin1().constData(), "* OK"); |
|
485 |
||
486 |
// Write NOOP |
|
487 |
QCOMPARE((int) socket->write("1 NOOP\r\n", 8), 8); |
|
488 |
QCOMPARE((int) socket->write("2 NOOP\r\n", 8), 8); |
|
489 |
||
490 |
if (!socket->canReadLine()) |
|
491 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
492 |
||
493 |
// Read response |
|
494 |
s = socket->readLine(); |
|
495 |
QCOMPARE(s.toLatin1().constData(), "1 OK Completed\r\n"); |
|
496 |
||
497 |
// Write a third NOOP to verify that write doesn't clear the read buffer |
|
498 |
QCOMPARE((int) socket->write("3 NOOP\r\n", 8), 8); |
|
499 |
||
500 |
// Read second response |
|
501 |
if (!socket->canReadLine()) |
|
502 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
503 |
s = socket->readLine(); |
|
504 |
QCOMPARE(s.toLatin1().constData(), "2 OK Completed\r\n"); |
|
505 |
||
506 |
// Read third response |
|
507 |
if (!socket->canReadLine()) |
|
508 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
509 |
s = socket->readLine(); |
|
510 |
QCOMPARE(s.toLatin1().constData(), "3 OK Completed\r\n"); |
|
511 |
||
512 |
||
513 |
// Write LOGOUT |
|
514 |
QCOMPARE((int) socket->write("4 LOGOUT\r\n", 10), 10); |
|
515 |
||
516 |
if (!socket->canReadLine()) |
|
517 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
518 |
||
519 |
// Read two lines of respose |
|
520 |
s = socket->readLine(); |
|
521 |
QCOMPARE(s.toLatin1().constData(), "* BYE LOGOUT received\r\n"); |
|
522 |
||
523 |
if (!socket->canReadLine()) |
|
524 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
525 |
||
526 |
s = socket->readLine(); |
|
527 |
QCOMPARE(s.toLatin1().constData(), "4 OK Completed\r\n"); |
|
528 |
||
529 |
// Close the socket |
|
530 |
socket->close(); |
|
531 |
||
532 |
// Check that it's closed |
|
533 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
534 |
||
535 |
delete socket; |
|
536 |
} |
|
537 |
||
538 |
//---------------------------------------------------------------------------------- |
|
539 |
||
540 |
void tst_QTcpSocket::hostNotFound() |
|
541 |
{ |
|
542 |
QTcpSocket *socket = newSocket(); |
|
543 |
||
544 |
socket->connectToHost("nosuchserver.troll.no", 80); |
|
545 |
QVERIFY(!socket->waitForConnected()); |
|
546 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
547 |
QCOMPARE(int(socket->error()), int(QTcpSocket::HostNotFoundError)); |
|
548 |
||
549 |
delete socket; |
|
550 |
} |
|
551 |
||
552 |
//---------------------------------------------------------------------------------- |
|
553 |
||
554 |
void tst_QTcpSocket::timeoutConnect() |
|
555 |
{ |
|
556 |
QTcpSocket *socket = newSocket(); |
|
557 |
||
558 |
// Outgoing port 53 is firewalled in the Oslo office. |
|
559 |
socket->connectToHost("cisco.com", 53); |
|
560 |
QVERIFY(!socket->waitForConnected(200)); |
|
561 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
562 |
QCOMPARE(int(socket->error()), int(QTcpSocket::SocketTimeoutError)); |
|
563 |
||
564 |
socket->connectToHost("cisco.com", 53); |
|
565 |
QTest::qSleep(50); |
|
566 |
socket->abort(); |
|
567 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
568 |
QCOMPARE(socket->openMode(), QIODevice::NotOpen); |
|
569 |
||
570 |
delete socket; |
|
571 |
} |
|
572 |
||
573 |
//---------------------------------------------------------------------------------- |
|
574 |
||
575 |
void tst_QTcpSocket::nonBlockingIMAP() |
|
576 |
{ |
|
577 |
QTcpSocket *socket = newSocket(); |
|
578 |
connect(socket, SIGNAL(hostFound()), SLOT(nonBlockingIMAP_hostFound())); |
|
579 |
connect(socket, SIGNAL(connected()), SLOT(nonBlockingIMAP_connected())); |
|
580 |
connect(socket, SIGNAL(disconnected()), SLOT(nonBlockingIMAP_closed())); |
|
581 |
connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(nonBlockingIMAP_bytesWritten(qint64))); |
|
582 |
connect(socket, SIGNAL(readyRead()), SLOT(nonBlockingIMAP_readyRead())); |
|
583 |
nonBlockingIMAP_socket = socket; |
|
584 |
||
585 |
// Connect |
|
586 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
587 |
QVERIFY(socket->state() == QTcpSocket::HostLookupState || |
|
588 |
socket->state() == QTcpSocket::ConnectingState); |
|
589 |
||
590 |
enterLoop(30); |
|
591 |
if (timeout()) { |
|
592 |
QFAIL("Timed out"); |
|
593 |
} |
|
594 |
||
595 |
if (socket->state() == QTcpSocket::ConnectingState) { |
|
596 |
enterLoop(30); |
|
597 |
if (timeout()) { |
|
598 |
QFAIL("Timed out"); |
|
599 |
} |
|
600 |
} |
|
601 |
||
602 |
QCOMPARE(socket->state(), QTcpSocket::ConnectedState); |
|
603 |
||
604 |
enterLoop(30); |
|
605 |
if (timeout()) { |
|
606 |
QFAIL("Timed out"); |
|
607 |
} |
|
608 |
||
609 |
// Read greeting |
|
610 |
QVERIFY(!nonBlockingIMAP_data.isEmpty()); |
|
611 |
// QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(), |
|
612 |
// "* OK fluke Cyrus IMAP4 v2.2.12 server ready\r\n"); |
|
613 |
QCOMPARE(nonBlockingIMAP_data.at(0).left(4).toLatin1().constData(), "* OK"); |
|
614 |
nonBlockingIMAP_data.clear(); |
|
615 |
||
616 |
nonBlockingIMAP_totalWritten = 0; |
|
617 |
||
618 |
// Write NOOP |
|
619 |
QCOMPARE((int) socket->write("1 NOOP\r\n", 8), 8); |
|
620 |
||
621 |
||
622 |
enterLoop(30); |
|
623 |
if (timeout()) { |
|
624 |
QFAIL("Timed out"); |
|
625 |
} |
|
626 |
||
627 |
QVERIFY(nonBlockingIMAP_totalWritten == 8); |
|
628 |
||
629 |
||
630 |
enterLoop(30); |
|
631 |
if (timeout()) { |
|
632 |
QFAIL("Timed out"); |
|
633 |
} |
|
634 |
||
635 |
||
636 |
// Read response |
|
637 |
QVERIFY(!nonBlockingIMAP_data.isEmpty()); |
|
638 |
QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(), "1 OK Completed\r\n"); |
|
639 |
nonBlockingIMAP_data.clear(); |
|
640 |
||
641 |
||
642 |
nonBlockingIMAP_totalWritten = 0; |
|
643 |
||
644 |
// Write LOGOUT |
|
645 |
QCOMPARE((int) socket->write("2 LOGOUT\r\n", 10), 10); |
|
646 |
||
647 |
enterLoop(30); |
|
648 |
if (timeout()) { |
|
649 |
QFAIL("Timed out"); |
|
650 |
} |
|
651 |
||
652 |
QVERIFY(nonBlockingIMAP_totalWritten == 10); |
|
653 |
||
654 |
// Wait for greeting |
|
655 |
enterLoop(30); |
|
656 |
if (timeout()) { |
|
657 |
QFAIL("Timed out"); |
|
658 |
} |
|
659 |
||
660 |
// Read two lines of respose |
|
661 |
QCOMPARE(nonBlockingIMAP_data.at(0).toLatin1().constData(), "* BYE LOGOUT received\r\n"); |
|
662 |
QCOMPARE(nonBlockingIMAP_data.at(1).toLatin1().constData(), "2 OK Completed\r\n"); |
|
663 |
nonBlockingIMAP_data.clear(); |
|
664 |
||
665 |
// Close the socket |
|
666 |
socket->close(); |
|
667 |
||
668 |
// Check that it's closed |
|
669 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
670 |
||
671 |
delete socket; |
|
672 |
} |
|
673 |
||
674 |
void tst_QTcpSocket::nonBlockingIMAP_hostFound() |
|
675 |
{ |
|
676 |
exitLoop(); |
|
677 |
} |
|
678 |
||
679 |
void tst_QTcpSocket::nonBlockingIMAP_connected() |
|
680 |
{ |
|
681 |
exitLoop(); |
|
682 |
} |
|
683 |
||
684 |
void tst_QTcpSocket::nonBlockingIMAP_readyRead() |
|
685 |
{ |
|
686 |
while (nonBlockingIMAP_socket->canReadLine()) |
|
687 |
nonBlockingIMAP_data.append(nonBlockingIMAP_socket->readLine()); |
|
688 |
||
689 |
exitLoop(); |
|
690 |
} |
|
691 |
||
692 |
void tst_QTcpSocket::nonBlockingIMAP_bytesWritten(qint64 written) |
|
693 |
{ |
|
694 |
nonBlockingIMAP_totalWritten += written; |
|
695 |
exitLoop(); |
|
696 |
} |
|
697 |
||
698 |
void tst_QTcpSocket::nonBlockingIMAP_closed() |
|
699 |
{ |
|
700 |
} |
|
701 |
||
702 |
//---------------------------------------------------------------------------------- |
|
703 |
||
704 |
void tst_QTcpSocket::delayedClose() |
|
705 |
{ |
|
706 |
QTcpSocket *socket = newSocket(); |
|
707 |
connect(socket, SIGNAL(connected()), SLOT(nonBlockingIMAP_connected())); |
|
708 |
connect(socket, SIGNAL(disconnected()), SLOT(exitLoopSlot())); |
|
709 |
||
710 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
711 |
||
712 |
enterLoop(30); |
|
713 |
if (timeout()) |
|
714 |
QFAIL("Timed out"); |
|
715 |
||
716 |
QCOMPARE(socket->state(), QTcpSocket::ConnectedState); |
|
717 |
||
718 |
QCOMPARE((int) socket->write("1 LOGOUT\r\n", 10), 10); |
|
719 |
||
720 |
// Add a huge bulk of data to be written after the logout |
|
721 |
// command. The server will shut down after receiving the LOGOUT, |
|
722 |
// so this data will not be read. But our close call should |
|
723 |
// schedule a delayed close because all the data can not be |
|
724 |
// written in one go. |
|
725 |
QCOMPARE((int) socket->write(QByteArray(100000, '\n'), 100000), 100000); |
|
726 |
||
727 |
socket->close(); |
|
728 |
||
729 |
QCOMPARE((int) socket->state(), (int) QTcpSocket::ClosingState); |
|
730 |
||
731 |
enterLoop(10); |
|
732 |
if (timeout()) |
|
733 |
QFAIL("Timed out"); |
|
734 |
||
735 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
736 |
||
737 |
delete socket; |
|
738 |
} |
|
739 |
||
740 |
//---------------------------------------------------------------------------------- |
|
741 |
||
742 |
void tst_QTcpSocket::partialRead() |
|
743 |
{ |
|
744 |
QTcpSocket *socket = newSocket(); |
|
745 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
746 |
QVERIFY(socket->waitForConnected(10000)); |
|
747 |
QVERIFY(socket->state() == QTcpSocket::ConnectedState); |
|
748 |
char buf[512]; |
|
749 |
||
750 |
// QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready"; |
|
751 |
QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE"; |
|
752 |
||
753 |
for (int i = 0; i < 10; i += 2) { |
|
754 |
while (socket->bytesAvailable() < 2) |
|
755 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
756 |
QVERIFY(socket->read(buf, 2) == 2); |
|
757 |
buf[2] = '\0'; |
|
758 |
QCOMPARE((char *)buf, greeting.mid(i, 2).data()); |
|
759 |
} |
|
760 |
||
761 |
delete socket; |
|
762 |
} |
|
763 |
||
764 |
//---------------------------------------------------------------------------------- |
|
765 |
||
766 |
void tst_QTcpSocket::unget() |
|
767 |
{ |
|
768 |
QTcpSocket *socket = newSocket(); |
|
769 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
770 |
QVERIFY(socket->waitForConnected(10000)); |
|
771 |
QVERIFY(socket->state() == QTcpSocket::ConnectedState); |
|
772 |
char buf[512]; |
|
773 |
||
774 |
// QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready"; |
|
775 |
QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE"; |
|
776 |
||
777 |
for (int i = 0; i < 10; i += 2) { |
|
778 |
while (socket->bytesAvailable() < 2) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
779 |
QVERIFY(socket->waitForReadyRead(10000)); |
0 | 780 |
int bA = socket->bytesAvailable(); |
781 |
QVERIFY(socket->read(buf, 2) == 2); |
|
782 |
buf[2] = '\0'; |
|
783 |
QCOMPARE((char *)buf, greeting.mid(i, 2).data()); |
|
784 |
QCOMPARE((int)socket->bytesAvailable(), bA - 2); |
|
785 |
socket->ungetChar(buf[1]); |
|
786 |
socket->ungetChar(buf[0]); |
|
787 |
QCOMPARE((int)socket->bytesAvailable(), bA); |
|
788 |
QVERIFY(socket->read(buf, 2) == 2); |
|
789 |
buf[2] = '\0'; |
|
790 |
QCOMPARE((char *)buf, greeting.mid(i, 2).data()); |
|
791 |
} |
|
792 |
||
793 |
delete socket; |
|
794 |
} |
|
795 |
||
796 |
//---------------------------------------------------------------------------------- |
|
797 |
void tst_QTcpSocket::readRegularFile_readyRead() |
|
798 |
{ |
|
799 |
exitLoop(); |
|
800 |
} |
|
801 |
||
802 |
//---------------------------------------------------------------------------------- |
|
803 |
void tst_QTcpSocket::readAllAfterClose() |
|
804 |
{ |
|
805 |
QTcpSocket *socket = newSocket(); |
|
806 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
807 |
connect(socket, SIGNAL(readyRead()), SLOT(readRegularFile_readyRead())); |
|
808 |
enterLoop(10); |
|
809 |
if (timeout()) |
|
810 |
QFAIL("Network operation timed out"); |
|
811 |
||
812 |
socket->close(); |
|
813 |
QByteArray array = socket->readAll(); |
|
814 |
QCOMPARE(array.size(), 0); |
|
815 |
||
816 |
delete socket; |
|
817 |
} |
|
818 |
||
819 |
//---------------------------------------------------------------------------------- |
|
820 |
void tst_QTcpSocket::openCloseOpenClose() |
|
821 |
{ |
|
822 |
QTcpSocket *socket = newSocket(); |
|
823 |
||
824 |
for (int i = 0; i < 3; ++i) { |
|
825 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
826 |
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen)); |
|
827 |
QVERIFY(socket->isSequential()); |
|
828 |
QVERIFY(!socket->isOpen()); |
|
829 |
QVERIFY(socket->socketType() == QTcpSocket::TcpSocket); |
|
830 |
||
831 |
char c; |
|
832 |
QCOMPARE(socket->getChar(&c), false); |
|
833 |
QCOMPARE((int) socket->bytesAvailable(), 0); |
|
834 |
QCOMPARE(socket->canReadLine(), false); |
|
835 |
QCOMPARE(socket->readLine(), QByteArray()); |
|
836 |
QCOMPARE(socket->socketDescriptor(), -1); |
|
837 |
QCOMPARE((int) socket->localPort(), 0); |
|
838 |
QVERIFY(socket->localAddress() == QHostAddress()); |
|
839 |
QCOMPARE((int) socket->peerPort(), 0); |
|
840 |
QVERIFY(socket->peerAddress() == QHostAddress()); |
|
841 |
QCOMPARE(socket->error(), QTcpSocket::UnknownSocketError); |
|
842 |
QCOMPARE(socket->errorString(), QString("Unknown error")); |
|
843 |
||
844 |
QVERIFY(socket->state() == QTcpSocket::UnconnectedState); |
|
845 |
||
846 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
847 |
QVERIFY(socket->waitForConnected(10000)); |
|
848 |
socket->close(); |
|
849 |
} |
|
850 |
||
851 |
delete socket; |
|
852 |
} |
|
853 |
||
854 |
//---------------------------------------------------------------------------------- |
|
855 |
void tst_QTcpSocket::connectDisconnectConnectDisconnect() |
|
856 |
{ |
|
857 |
QTcpSocket *socket = newSocket(); |
|
858 |
||
859 |
for (int i = 0; i < 3; ++i) { |
|
860 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
861 |
QVERIFY(socket->socketType() == QTcpSocket::TcpSocket); |
|
862 |
||
863 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
864 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
865 |
QCOMPARE(QString::fromLatin1(socket->read(4)), QString("* OK")); |
|
866 |
||
867 |
socket->disconnectFromHost(); |
|
868 |
if (socket->state() != QTcpSocket::UnconnectedState) |
|
869 |
QVERIFY(socket->waitForDisconnected(10000)); |
|
870 |
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite)); |
|
871 |
} |
|
872 |
||
873 |
delete socket; |
|
874 |
} |
|
875 |
||
876 |
//---------------------------------------------------------------------------------- |
|
877 |
void tst_QTcpSocket::disconnectWhileConnecting_data() |
|
878 |
{ |
|
879 |
QTest::addColumn<QByteArray>("data"); |
|
880 |
QTest::addColumn<bool>("closeDirectly"); |
|
881 |
||
882 |
QTest::newRow("without-data") << QByteArray() << false; |
|
883 |
QTest::newRow("without-data+close") << QByteArray() << true; |
|
884 |
QTest::newRow("with-data") << QByteArray("Hello, world!") << false; |
|
885 |
QTest::newRow("with-data+close") << QByteArray("Hello, world!") << true; |
|
886 |
||
887 |
QByteArray bigData(1024*1024, '@'); |
|
888 |
QTest::newRow("with-big-data") << bigData << false; |
|
889 |
QTest::newRow("with-big-data+close") << bigData << true; |
|
890 |
} |
|
891 |
||
892 |
void tst_QTcpSocket::disconnectWhileConnecting() |
|
893 |
{ |
|
894 |
QFETCH(QByteArray, data); |
|
895 |
||
896 |
QTcpServer server; |
|
897 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
898 |
||
899 |
// proceed to the connect-write-disconnect |
|
900 |
QTcpSocket *socket = newSocket(); |
|
901 |
socket->connectToHost("127.0.0.1", server.serverPort()); |
|
902 |
if (!data.isEmpty()) |
|
903 |
socket->write(data); |
|
904 |
if (socket->state() == QAbstractSocket::ConnectedState) |
|
905 |
QSKIP("localhost connections are immediate, test case is invalid", SkipSingle); |
|
906 |
||
907 |
QFETCH(bool, closeDirectly); |
|
908 |
if (closeDirectly) { |
|
909 |
socket->close(); |
|
910 |
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen)); |
|
911 |
} else { |
|
912 |
socket->disconnectFromHost(); |
|
913 |
} |
|
914 |
||
915 |
connect(socket, SIGNAL(disconnected()), SLOT(exitLoopSlot())); |
|
916 |
#ifndef Q_OS_SYMBIAN |
|
917 |
enterLoop(10); |
|
918 |
#else |
|
919 |
enterLoop(30); |
|
920 |
#endif |
|
921 |
QVERIFY2(!timeout(), "Network timeout"); |
|
922 |
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState); |
|
923 |
if (!closeDirectly) { |
|
924 |
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite)); |
|
925 |
socket->close(); |
|
926 |
} |
|
927 |
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen)); |
|
928 |
||
929 |
// accept the other side and verify that it was sent properly: |
|
930 |
QVERIFY(server.hasPendingConnections() || server.waitForNewConnection(0)); |
|
931 |
QTcpSocket *othersocket = server.nextPendingConnection(); |
|
932 |
if (othersocket->state() != QAbstractSocket::UnconnectedState) |
|
933 |
QVERIFY2(othersocket->waitForDisconnected(10000), "Network timeout"); |
|
934 |
QVERIFY(othersocket->state() == QAbstractSocket::UnconnectedState); |
|
935 |
QCOMPARE(othersocket->readAll(), data); |
|
936 |
||
937 |
delete socket; |
|
938 |
delete othersocket; |
|
939 |
} |
|
940 |
||
941 |
//---------------------------------------------------------------------------------- |
|
942 |
class ReceiverThread: public QThread |
|
943 |
{ |
|
944 |
QTcpServer *server; |
|
945 |
public: |
|
946 |
int serverPort; |
|
947 |
bool ok; |
|
948 |
QByteArray receivedData; |
|
949 |
volatile bool quit; |
|
950 |
||
951 |
ReceiverThread() |
|
952 |
: server(0), ok(false), quit(false) |
|
953 |
{ } |
|
954 |
||
955 |
~ReceiverThread() { /*delete server;*/ terminate(); wait(); } |
|
956 |
||
957 |
bool listen() |
|
958 |
{ |
|
959 |
server = new QTcpServer; |
|
960 |
if (!server->listen(QHostAddress::LocalHost)) |
|
961 |
return false; |
|
962 |
serverPort = server->serverPort(); |
|
963 |
server->moveToThread(this); |
|
964 |
return true; |
|
965 |
} |
|
966 |
||
967 |
protected: |
|
968 |
void run() |
|
969 |
{ |
|
970 |
bool timedOut = false; |
|
971 |
while (!quit) { |
|
972 |
#ifndef Q_OS_SYMBIAN |
|
973 |
if (server->waitForNewConnection(500, &timedOut)) |
|
974 |
#else |
|
975 |
if (server->waitForNewConnection(5000, &timedOut)) |
|
976 |
#endif |
|
977 |
break; |
|
978 |
if (!timedOut) |
|
979 |
return; |
|
980 |
} |
|
981 |
||
982 |
QTcpSocket *socket = server->nextPendingConnection(); |
|
983 |
while (!quit) { |
|
984 |
#ifndef Q_OS_SYMBIAN |
|
985 |
if (socket->waitForDisconnected(500)) |
|
986 |
#else |
|
987 |
if (socket->waitForDisconnected(5000)) |
|
988 |
#endif |
|
989 |
break; |
|
990 |
if (socket->error() != QAbstractSocket::SocketTimeoutError) |
|
991 |
return; |
|
992 |
} |
|
993 |
||
994 |
if (!quit) { |
|
995 |
receivedData = socket->readAll(); |
|
996 |
ok = true; |
|
997 |
} |
|
998 |
delete socket; |
|
999 |
delete server; |
|
1000 |
server = 0; |
|
1001 |
} |
|
1002 |
}; |
|
1003 |
||
1004 |
void tst_QTcpSocket::disconnectWhileConnectingNoEventLoop_data() |
|
1005 |
{ |
|
1006 |
disconnectWhileConnecting_data(); |
|
1007 |
} |
|
1008 |
||
1009 |
void tst_QTcpSocket::disconnectWhileConnectingNoEventLoop() |
|
1010 |
{ |
|
1011 |
QFETCH(QByteArray, data); |
|
1012 |
||
1013 |
ReceiverThread thread; |
|
1014 |
QVERIFY(thread.listen()); |
|
1015 |
thread.start(); |
|
1016 |
||
1017 |
// proceed to the connect-write-disconnect |
|
1018 |
QTcpSocket *socket = newSocket(); |
|
1019 |
socket->connectToHost("127.0.0.1", thread.serverPort); |
|
1020 |
if (!data.isEmpty()) |
|
1021 |
socket->write(data); |
|
1022 |
if (socket->state() == QAbstractSocket::ConnectedState) { |
|
1023 |
thread.quit = true; |
|
1024 |
thread.wait(); |
|
1025 |
QSKIP("localhost connections are immediate, test case is invalid", SkipSingle); |
|
1026 |
} |
|
1027 |
||
1028 |
QFETCH(bool, closeDirectly); |
|
1029 |
if (closeDirectly) { |
|
1030 |
socket->close(); |
|
1031 |
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen)); |
|
1032 |
} else { |
|
1033 |
socket->disconnectFromHost(); |
|
1034 |
} |
|
1035 |
||
1036 |
#ifndef Q_OS_SYMBIAN |
|
1037 |
QVERIFY2(socket->waitForDisconnected(10000), "Network timeout"); |
|
1038 |
#else |
|
1039 |
QVERIFY2(socket->waitForDisconnected(30000), "Network timeout"); |
|
1040 |
#endif |
|
1041 |
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState); |
|
1042 |
if (!closeDirectly) { |
|
1043 |
QCOMPARE(int(socket->openMode()), int(QIODevice::ReadWrite)); |
|
1044 |
socket->close(); |
|
1045 |
} |
|
1046 |
QCOMPARE(int(socket->openMode()), int(QIODevice::NotOpen)); |
|
1047 |
delete socket; |
|
1048 |
||
1049 |
// check if the other side received everything ok |
|
1050 |
QVERIFY(thread.wait(30000)); |
|
1051 |
QVERIFY(thread.ok); |
|
1052 |
QCOMPARE(thread.receivedData, data); |
|
1053 |
} |
|
1054 |
||
1055 |
//---------------------------------------------------------------------------------- |
|
1056 |
void tst_QTcpSocket::disconnectWhileLookingUp_data() |
|
1057 |
{ |
|
1058 |
QTest::addColumn<bool>("doClose"); |
|
1059 |
||
1060 |
QTest::newRow("disconnect") << false; |
|
1061 |
QTest::newRow("close") << true; |
|
1062 |
} |
|
1063 |
||
1064 |
void tst_QTcpSocket::disconnectWhileLookingUp() |
|
1065 |
{ |
|
1066 |
QFETCH_GLOBAL(bool, setProxy); |
|
1067 |
if (setProxy) |
|
1068 |
return; // we let the proxies do the lookup now |
|
1069 |
||
1070 |
// just connect and disconnect, then make sure nothing weird happened |
|
1071 |
QTcpSocket *socket = newSocket(); |
|
1072 |
socket->connectToHost(QtNetworkSettings::serverName(), 21); |
|
1073 |
QVERIFY(socket->state() == QAbstractSocket::HostLookupState); |
|
1074 |
||
1075 |
QFETCH(bool, doClose); |
|
1076 |
if (doClose) { |
|
1077 |
socket->close(); |
|
1078 |
QVERIFY(socket->openMode() == QIODevice::NotOpen); |
|
1079 |
} else { |
|
1080 |
socket->disconnectFromHost(); |
|
1081 |
QVERIFY(socket->openMode() == QIODevice::ReadWrite); |
|
1082 |
} |
|
1083 |
||
1084 |
// let anything queued happen |
|
1085 |
QEventLoop loop; |
|
1086 |
#ifndef Q_OS_SYMBIAN |
|
1087 |
QTimer::singleShot(50, &loop, SLOT(quit())); |
|
1088 |
#else |
|
1089 |
QTimer::singleShot(5000, &loop, SLOT(quit())); |
|
1090 |
#endif |
|
1091 |
loop.exec(); |
|
1092 |
||
1093 |
// recheck |
|
1094 |
if (doClose) { |
|
1095 |
QVERIFY(socket->openMode() == QIODevice::NotOpen); |
|
1096 |
} else { |
|
1097 |
QVERIFY(socket->openMode() == QIODevice::ReadWrite); |
|
1098 |
} |
|
1099 |
||
1100 |
QVERIFY(socket->state() == QAbstractSocket::UnconnectedState); |
|
1101 |
} |
|
1102 |
||
1103 |
//---------------------------------------------------------------------------------- |
|
1104 |
void tst_QTcpSocket::downloadBigFile() |
|
1105 |
{ |
|
1106 |
if (tmpSocket) |
|
1107 |
delete tmpSocket; |
|
1108 |
tmpSocket = newSocket(); |
|
1109 |
||
1110 |
connect(tmpSocket, SIGNAL(connected()), SLOT(exitLoopSlot())); |
|
1111 |
connect(tmpSocket, SIGNAL(readyRead()), SLOT(downloadBigFileSlot())); |
|
1112 |
||
1113 |
tmpSocket->connectToHost(QtNetworkSettings::serverName(), 80); |
|
1114 |
||
1115 |
enterLoop(30); |
|
1116 |
if (timeout()) { |
|
1117 |
delete tmpSocket; |
|
1118 |
tmpSocket = 0; |
|
1119 |
QFAIL("Network operation timed out"); |
|
1120 |
} |
|
1121 |
||
1122 |
QByteArray hostName = QtNetworkSettings::serverName().toLatin1(); |
|
1123 |
QVERIFY(tmpSocket->state() == QAbstractSocket::ConnectedState); |
|
1124 |
QVERIFY(tmpSocket->write("GET /mediumfile HTTP/1.0\r\n") > 0); |
|
1125 |
QVERIFY(tmpSocket->write("HOST: ") > 0); |
|
1126 |
QVERIFY(tmpSocket->write(hostName.data()) > 0); |
|
1127 |
QVERIFY(tmpSocket->write("\r\n") > 0); |
|
1128 |
QVERIFY(tmpSocket->write("\r\n") > 0); |
|
1129 |
||
1130 |
bytesAvailable = 0; |
|
1131 |
expectedLength = 0; |
|
1132 |
readingBody = false; |
|
1133 |
||
1134 |
QTime stopWatch; |
|
1135 |
stopWatch.start(); |
|
1136 |
||
1137 |
enterLoop(600); |
|
1138 |
if (timeout()) { |
|
1139 |
delete tmpSocket; |
|
1140 |
tmpSocket = 0; |
|
1141 |
if (bytesAvailable > 0) |
|
1142 |
qDebug("Slow Connection, only downloaded %ld of %d", long(bytesAvailable), 10000281); |
|
1143 |
QFAIL("Network operation timed out"); |
|
1144 |
} |
|
1145 |
||
1146 |
QCOMPARE(bytesAvailable, expectedLength); |
|
1147 |
||
1148 |
qDebug("\t\t%.1fMB/%.1fs: %.1fMB/s", |
|
1149 |
bytesAvailable / (1024.0 * 1024.0), |
|
1150 |
stopWatch.elapsed() / 1024.0, |
|
1151 |
(bytesAvailable / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
1152 |
||
1153 |
delete tmpSocket; |
|
1154 |
tmpSocket = 0; |
|
1155 |
} |
|
1156 |
||
1157 |
//---------------------------------------------------------------------------------- |
|
1158 |
void tst_QTcpSocket::exitLoopSlot() |
|
1159 |
{ |
|
1160 |
exitLoop(); |
|
1161 |
} |
|
1162 |
||
1163 |
//---------------------------------------------------------------------------------- |
|
1164 |
void tst_QTcpSocket::downloadBigFileSlot() |
|
1165 |
{ |
|
1166 |
if (!readingBody) { |
|
1167 |
while (tmpSocket->canReadLine()) { |
|
1168 |
QByteArray array = tmpSocket->readLine(); |
|
1169 |
if (array.startsWith("Content-Length")) |
|
1170 |
expectedLength = array.simplified().split(' ').at(1).toInt(); |
|
1171 |
if (array == "\r\n") { |
|
1172 |
readingBody = true; |
|
1173 |
break; |
|
1174 |
} |
|
1175 |
} |
|
1176 |
} |
|
1177 |
if (readingBody) { |
|
1178 |
bytesAvailable += tmpSocket->readAll().size(); |
|
1179 |
if (bytesAvailable == expectedLength) |
|
1180 |
exitLoop(); |
|
1181 |
} |
|
1182 |
} |
|
1183 |
||
1184 |
//---------------------------------------------------------------------------------- |
|
1185 |
void tst_QTcpSocket::readLine() |
|
1186 |
{ |
|
1187 |
QTcpSocket *socket = newSocket(); |
|
1188 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1189 |
QVERIFY(socket->waitForConnected(5000)); |
|
1190 |
||
1191 |
while (!socket->canReadLine()) |
|
1192 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
1193 |
||
1194 |
char buffer[1024]; |
|
1195 |
int expectedReplySize = QtNetworkSettings::expectedReplyIMAP().size(); |
|
1196 |
Q_ASSERT(expectedReplySize >= 3); |
|
1197 |
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(expectedReplySize)); |
|
1198 |
||
1199 |
QCOMPARE((int) buffer[expectedReplySize-2], (int) '\r'); |
|
1200 |
QCOMPARE((int) buffer[expectedReplySize-1], (int) '\n'); |
|
1201 |
QCOMPARE((int) buffer[expectedReplySize], (int) '\0'); |
|
1202 |
||
1203 |
QCOMPARE(socket->write("1 NOOP\r\n"), qint64(8)); |
|
1204 |
||
1205 |
while (socket->bytesAvailable() < 10) |
|
1206 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
1207 |
||
1208 |
QCOMPARE(socket->readLine(buffer, 11), qint64(10)); |
|
1209 |
QCOMPARE((const char *)buffer, "1 OK Compl"); |
|
1210 |
||
1211 |
while (socket->bytesAvailable() < 6) |
|
1212 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
1213 |
||
1214 |
QCOMPARE(socket->readLine(buffer, 11), qint64(6)); |
|
1215 |
QCOMPARE((const char *)buffer, "eted\r\n"); |
|
1216 |
||
1217 |
QVERIFY(!socket->waitForReadyRead(100)); |
|
1218 |
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(0)); |
|
1219 |
QVERIFY(socket->error() == QAbstractSocket::SocketTimeoutError |
|
1220 |
|| socket->error() == QAbstractSocket::RemoteHostClosedError); |
|
1221 |
QCOMPARE(socket->bytesAvailable(), qint64(0)); |
|
1222 |
||
1223 |
socket->close(); |
|
1224 |
QCOMPARE(socket->readLine(buffer, sizeof(buffer)), qint64(-1)); |
|
1225 |
||
1226 |
delete socket; |
|
1227 |
} |
|
1228 |
||
1229 |
//---------------------------------------------------------------------------------- |
|
1230 |
void tst_QTcpSocket::readLineString() |
|
1231 |
{ |
|
1232 |
QTcpSocket *socket = newSocket(); |
|
1233 |
// QByteArray expected("* OK fluke Cyrus IMAP4 v2.2.12 server ready\r\n"); |
|
1234 |
QByteArray expected("* OK [CAPABILITY IMAP4 IMAP4rev1 LITERAL+ ID STARTTLS LOGINDISABLED] qt-test-server.qt-test-net Cyrus IMAP4 v2.3.11-Mandriva-RPM-2.3.11-6mdv2008.1 server ready\r\n"); |
|
1235 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1236 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
1237 |
||
1238 |
QByteArray arr = socket->readLine(); |
|
1239 |
QCOMPARE(arr, QtNetworkSettings::expectedReplyIMAP()); |
|
1240 |
||
1241 |
delete socket; |
|
1242 |
} |
|
1243 |
||
1244 |
//---------------------------------------------------------------------------------- |
|
1245 |
void tst_QTcpSocket::readChunks() |
|
1246 |
{ |
|
1247 |
QTcpSocket *socket = newSocket(); |
|
1248 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1249 |
QVERIFY(socket->waitForConnected(10000)); |
|
1250 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
1251 |
||
1252 |
char buf[4096]; |
|
1253 |
memset(buf, '@', sizeof(buf)); |
|
1254 |
qint64 dataLength = socket->read(buf, sizeof(buf)); |
|
1255 |
QVERIFY(dataLength > 0); |
|
1256 |
||
1257 |
QCOMPARE(buf[dataLength - 2], '\r'); |
|
1258 |
QCOMPARE(buf[dataLength - 1], '\n'); |
|
1259 |
QCOMPARE(buf[dataLength], '@'); |
|
1260 |
||
1261 |
delete socket; |
|
1262 |
} |
|
1263 |
||
1264 |
//---------------------------------------------------------------------------------- |
|
1265 |
void tst_QTcpSocket::waitForBytesWritten() |
|
1266 |
{ |
|
1267 |
QTcpSocket *socket = newSocket(); |
|
1268 |
socket->connectToHost(QtNetworkSettings::serverName(), 22); |
|
1269 |
QVERIFY(socket->waitForConnected(10000)); |
|
1270 |
||
1271 |
socket->write(QByteArray(10000, '@')); |
|
1272 |
qint64 toWrite = socket->bytesToWrite(); |
|
1273 |
QVERIFY(socket->waitForBytesWritten(5000)); |
|
1274 |
QVERIFY(toWrite > socket->bytesToWrite()); |
|
1275 |
||
1276 |
delete socket; |
|
1277 |
} |
|
1278 |
||
1279 |
//---------------------------------------------------------------------------------- |
|
1280 |
void tst_QTcpSocket::waitForReadyRead() |
|
1281 |
{ |
|
1282 |
QTcpSocket *socket = newSocket(); |
|
1283 |
socket->connectToHost(QtNetworkSettings::serverName(), 22); |
|
1284 |
socket->waitForReadyRead(0); |
|
1285 |
delete socket; |
|
1286 |
} |
|
1287 |
||
1288 |
//---------------------------------------------------------------------------------- |
|
1289 |
void tst_QTcpSocket::flush() |
|
1290 |
{ |
|
1291 |
QTcpSocket *socket = newSocket(); |
|
1292 |
socket->flush(); |
|
1293 |
||
1294 |
connect(socket, SIGNAL(connected()), SLOT(exitLoopSlot())); |
|
1295 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1296 |
enterLoop(5000); |
|
1297 |
QVERIFY(socket->isOpen()); |
|
1298 |
||
1299 |
socket->write("1 LOGOUT\r\n"); |
|
1300 |
QCOMPARE(socket->bytesToWrite(), qint64(10)); |
|
1301 |
socket->flush(); |
|
1302 |
QCOMPARE(socket->bytesToWrite(), qint64(0)); |
|
1303 |
socket->close(); |
|
1304 |
||
1305 |
delete socket; |
|
1306 |
} |
|
1307 |
||
1308 |
//---------------------------------------------------------------------------------- |
|
1309 |
void tst_QTcpSocket::synchronousApi() |
|
1310 |
{ |
|
1311 |
QTcpSocket *ftpSocket = newSocket(); |
|
1312 |
ftpSocket->connectToHost(QtNetworkSettings::serverName(), 21); |
|
1313 |
ftpSocket->write("QUIT\r\n"); |
|
1314 |
QVERIFY(ftpSocket->waitForDisconnected(10000)); |
|
1315 |
QVERIFY(ftpSocket->bytesAvailable() > 0); |
|
1316 |
QByteArray arr = ftpSocket->readAll(); |
|
1317 |
QVERIFY(arr.size() > 0); |
|
1318 |
delete ftpSocket; |
|
1319 |
} |
|
1320 |
||
1321 |
//---------------------------------------------------------------------------------- |
|
1322 |
void tst_QTcpSocket::dontCloseOnTimeout() |
|
1323 |
{ |
|
1324 |
QTcpServer server; |
|
1325 |
#ifdef TEST_QNETWORK_PROXY |
|
1326 |
server.setProxy(QNetworkProxy(QNetworkProxy::NoProxy)); |
|
1327 |
#endif |
|
1328 |
QVERIFY(server.listen()); |
|
1329 |
||
1330 |
QHostAddress serverAddress = QHostAddress::LocalHost; |
|
1331 |
if (!(server.serverAddress() == QHostAddress::Any)) |
|
1332 |
serverAddress = server.serverAddress(); |
|
1333 |
||
1334 |
QTcpSocket *socket = newSocket(); |
|
1335 |
socket->connectToHost(serverAddress, server.serverPort()); |
|
1336 |
#ifndef Q_OS_SYMBIAN |
|
1337 |
QVERIFY(!socket->waitForReadyRead(100)); |
|
1338 |
#else |
|
1339 |
QVERIFY(!socket->waitForReadyRead(5000)); |
|
1340 |
#endif |
|
1341 |
QCOMPARE(socket->error(), QTcpSocket::SocketTimeoutError); |
|
1342 |
QVERIFY(socket->isOpen()); |
|
1343 |
||
1344 |
#ifndef Q_OS_SYMBIAN |
|
1345 |
QVERIFY(!socket->waitForDisconnected(100)); |
|
1346 |
#else |
|
1347 |
QVERIFY(!socket->waitForDisconnected(5000)); |
|
1348 |
#endif |
|
1349 |
QCOMPARE(socket->error(), QTcpSocket::SocketTimeoutError); |
|
1350 |
QVERIFY(socket->isOpen()); |
|
1351 |
||
1352 |
delete socket; |
|
1353 |
} |
|
1354 |
||
1355 |
//---------------------------------------------------------------------------------- |
|
1356 |
void tst_QTcpSocket::recursiveReadyRead() |
|
1357 |
{ |
|
1358 |
QTcpSocket *smtp = newSocket(); |
|
1359 |
connect(smtp, SIGNAL(connected()), SLOT(exitLoopSlot())); |
|
1360 |
connect(smtp, SIGNAL(readyRead()), SLOT(recursiveReadyReadSlot())); |
|
1361 |
tmpSocket = smtp; |
|
1362 |
||
1363 |
QSignalSpy spy(smtp, SIGNAL(readyRead())); |
|
1364 |
||
1365 |
smtp->connectToHost("smtp.trolltech.com", 25); |
|
1366 |
enterLoop(30); |
|
1367 |
QVERIFY2(!timeout(), |
|
1368 |
"Timed out when connecting to smtp.trolltech.com:25"); |
|
1369 |
||
1370 |
enterLoop(30); |
|
1371 |
QVERIFY2(!timeout(), |
|
1372 |
"Timed out when waiting for the readyRead() signal"); |
|
1373 |
||
1374 |
QCOMPARE(spy.count(), 1); |
|
1375 |
||
1376 |
delete smtp; |
|
1377 |
} |
|
1378 |
||
1379 |
void tst_QTcpSocket::recursiveReadyReadSlot() |
|
1380 |
{ |
|
1381 |
// make sure the server spits out more data |
|
1382 |
tmpSocket->write("NOOP\r\n"); |
|
1383 |
tmpSocket->flush(); |
|
1384 |
||
1385 |
// indiscriminately enter the event loop and start processing |
|
1386 |
// events again. but oops! future socket notifications will cause |
|
1387 |
// undesired recursive behavior. Unless QTcpSocket is smart, which |
|
1388 |
// it of course is. :-) |
|
1389 |
QEventLoop loop; |
|
1390 |
for (int i = 0; i < 100; ++i) |
|
1391 |
loop.processEvents(); |
|
1392 |
||
1393 |
// all we really wanted to do was process some events, then exit |
|
1394 |
// the loop |
|
1395 |
exitLoop(); |
|
1396 |
} |
|
1397 |
||
1398 |
//---------------------------------------------------------------------------------- |
|
1399 |
void tst_QTcpSocket::atEnd() |
|
1400 |
{ |
|
1401 |
QTcpSocket *socket = newSocket(); |
|
1402 |
socket->connectToHost(QtNetworkSettings::serverName(), 21); |
|
1403 |
||
1404 |
QVERIFY(socket->waitForReadyRead(15000)); |
|
1405 |
QTextStream stream(socket); |
|
1406 |
QVERIFY(!stream.atEnd()); |
|
1407 |
QString greeting = stream.readLine(); |
|
1408 |
QVERIFY(stream.atEnd()); |
|
1409 |
// QCOMPARE(greeting, QString("220 (vsFTPd 2.0.4)")); |
|
1410 |
QCOMPARE(greeting, QString("220 (vsFTPd 2.0.5)")); |
|
1411 |
||
1412 |
delete socket; |
|
1413 |
} |
|
1414 |
||
1415 |
class TestThread : public QThread |
|
1416 |
{ |
|
1417 |
Q_OBJECT |
|
1418 |
||
1419 |
public: |
|
1420 |
inline QByteArray data() const |
|
1421 |
{ |
|
1422 |
return socketData; |
|
1423 |
} |
|
1424 |
||
1425 |
protected: |
|
1426 |
inline void run() |
|
1427 |
{ |
|
1428 |
#ifndef QT_NO_OPENSSL |
|
1429 |
QFETCH_GLOBAL(bool, ssl); |
|
1430 |
if (ssl) |
|
1431 |
socket = new QSslSocket; |
|
1432 |
else |
|
1433 |
#endif |
|
1434 |
socket = new QTcpSocket; |
|
1435 |
connect(socket, SIGNAL(readyRead()), this, SLOT(getData()), Qt::DirectConnection); |
|
1436 |
connect(socket, SIGNAL(disconnected()), this, SLOT(closed()), Qt::DirectConnection); |
|
1437 |
connect(socket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1438 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), Qt::DirectConnection); |
|
1439 |
||
1440 |
socket->connectToHost(QtNetworkSettings::serverName(), 21); |
|
1441 |
socket->write("QUIT\r\n"); |
|
1442 |
exec(); |
|
1443 |
||
1444 |
delete socket; |
|
1445 |
} |
|
1446 |
||
1447 |
private slots: |
|
1448 |
inline void getData() |
|
1449 |
{ |
|
1450 |
socketData += socket->readAll(); |
|
1451 |
} |
|
1452 |
||
1453 |
inline void closed() |
|
1454 |
{ |
|
1455 |
quit(); |
|
1456 |
} |
|
1457 |
inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) |
|
1458 |
{ |
|
1459 |
auth->setUser("qsockstest"); |
|
1460 |
auth->setPassword("password"); |
|
1461 |
} |
|
1462 |
private: |
|
1463 |
int exitCode; |
|
1464 |
QTcpSocket *socket; |
|
1465 |
QByteArray socketData; |
|
1466 |
}; |
|
1467 |
||
1468 |
//---------------------------------------------------------------------------------- |
|
1469 |
void tst_QTcpSocket::socketInAThread() |
|
1470 |
{ |
|
1471 |
for (int i = 0; i < 3; ++i) { |
|
1472 |
TestThread thread; |
|
1473 |
thread.start(); |
|
1474 |
QVERIFY(thread.wait(15000)); |
|
1475 |
QCOMPARE(thread.data(), QtNetworkSettings::expectedReplyFtp()); |
|
1476 |
} |
|
1477 |
} |
|
1478 |
||
1479 |
//---------------------------------------------------------------------------------- |
|
1480 |
void tst_QTcpSocket::socketsInThreads() |
|
1481 |
{ |
|
1482 |
for (int i = 0; i < 3; ++i) { |
|
1483 |
TestThread thread1; |
|
1484 |
TestThread thread2; |
|
1485 |
TestThread thread3; |
|
1486 |
||
1487 |
thread1.start(); |
|
1488 |
thread2.start(); |
|
1489 |
thread3.start(); |
|
1490 |
||
1491 |
QVERIFY(thread2.wait(15000)); |
|
1492 |
QVERIFY(thread3.wait(15000)); |
|
1493 |
QVERIFY(thread1.wait(15000)); |
|
1494 |
||
1495 |
QCOMPARE(thread1.data(),QtNetworkSettings::expectedReplyFtp()); |
|
1496 |
QCOMPARE(thread2.data(),QtNetworkSettings::expectedReplyFtp()); |
|
1497 |
QCOMPARE(thread3.data(),QtNetworkSettings::expectedReplyFtp()); |
|
1498 |
} |
|
1499 |
} |
|
1500 |
||
1501 |
//---------------------------------------------------------------------------------- |
|
1502 |
void tst_QTcpSocket::waitForReadyReadInASlot() |
|
1503 |
{ |
|
1504 |
QTcpSocket *socket = newSocket(); |
|
1505 |
tmpSocket = socket; |
|
1506 |
connect(socket, SIGNAL(connected()), this, SLOT(waitForReadyReadInASlotSlot())); |
|
1507 |
||
1508 |
socket->connectToHost(QtNetworkSettings::serverName(), 80); |
|
1509 |
socket->write("GET / HTTP/1.0\r\n\r\n"); |
|
1510 |
||
1511 |
enterLoop(30); |
|
1512 |
QVERIFY(!timeout()); |
|
1513 |
||
1514 |
delete socket; |
|
1515 |
} |
|
1516 |
||
1517 |
void tst_QTcpSocket::waitForReadyReadInASlotSlot() |
|
1518 |
{ |
|
1519 |
QVERIFY(tmpSocket->waitForReadyRead(10000)); |
|
1520 |
exitLoop(); |
|
1521 |
} |
|
1522 |
||
1523 |
class RemoteCloseErrorServer : public QTcpServer |
|
1524 |
{ |
|
1525 |
Q_OBJECT |
|
1526 |
public: |
|
1527 |
RemoteCloseErrorServer() |
|
1528 |
{ |
|
1529 |
connect(this, SIGNAL(newConnection()), |
|
1530 |
this, SLOT(getConnection())); |
|
1531 |
} |
|
1532 |
||
1533 |
private slots: |
|
1534 |
void getConnection() |
|
1535 |
{ |
|
1536 |
tst_QTcpSocket::exitLoop(); |
|
1537 |
} |
|
1538 |
}; |
|
1539 |
||
1540 |
//---------------------------------------------------------------------------------- |
|
1541 |
void tst_QTcpSocket::remoteCloseError() |
|
1542 |
{ |
|
1543 |
RemoteCloseErrorServer server; |
|
1544 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
1545 |
||
1546 |
QCoreApplication::instance()->processEvents(); |
|
1547 |
||
1548 |
QTcpSocket *clientSocket = newSocket(); |
|
1549 |
connect(clientSocket, SIGNAL(readyRead()), this, SLOT(exitLoopSlot())); |
|
1550 |
||
1551 |
clientSocket->connectToHost(server.serverAddress(), server.serverPort()); |
|
1552 |
||
1553 |
enterLoop(30); |
|
1554 |
QVERIFY(!timeout()); |
|
1555 |
||
1556 |
QVERIFY(server.hasPendingConnections()); |
|
1557 |
QTcpSocket *serverSocket = server.nextPendingConnection(); |
|
1558 |
connect(clientSocket, SIGNAL(disconnected()), this, SLOT(exitLoopSlot())); |
|
1559 |
||
1560 |
serverSocket->write("Hello"); |
|
1561 |
||
1562 |
enterLoop(30); |
|
1563 |
QVERIFY(!timeout()); |
|
1564 |
||
1565 |
QCOMPARE(clientSocket->bytesAvailable(), qint64(5)); |
|
1566 |
||
1567 |
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); |
|
1568 |
QSignalSpy errorSpy(clientSocket, SIGNAL(error(QAbstractSocket::SocketError))); |
|
1569 |
QSignalSpy disconnectedSpy(clientSocket, SIGNAL(disconnected())); |
|
1570 |
||
1571 |
clientSocket->write("World"); |
|
1572 |
serverSocket->disconnectFromHost(); |
|
1573 |
||
1574 |
tmpSocket = clientSocket; |
|
1575 |
connect(clientSocket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
1576 |
this, SLOT(remoteCloseErrorSlot())); |
|
1577 |
||
1578 |
enterLoop(30); |
|
1579 |
QVERIFY(!timeout()); |
|
1580 |
||
1581 |
QCOMPARE(disconnectedSpy.count(), 1); |
|
1582 |
QCOMPARE(errorSpy.count(), 1); |
|
1583 |
QCOMPARE(clientSocket->error(), QAbstractSocket::RemoteHostClosedError); |
|
1584 |
||
1585 |
delete serverSocket; |
|
1586 |
||
1587 |
clientSocket->connectToHost(server.serverAddress(), server.serverPort()); |
|
1588 |
||
1589 |
enterLoop(30); |
|
1590 |
QVERIFY(!timeout()); |
|
1591 |
||
1592 |
QVERIFY(server.hasPendingConnections()); |
|
1593 |
serverSocket = server.nextPendingConnection(); |
|
1594 |
serverSocket->disconnectFromHost(); |
|
1595 |
||
1596 |
enterLoop(30); |
|
1597 |
QVERIFY(!timeout()); |
|
1598 |
||
1599 |
QCOMPARE(clientSocket->state(), QAbstractSocket::UnconnectedState); |
|
1600 |
||
1601 |
delete clientSocket; |
|
1602 |
} |
|
1603 |
||
1604 |
void tst_QTcpSocket::remoteCloseErrorSlot() |
|
1605 |
{ |
|
1606 |
QCOMPARE(tmpSocket->state(), QAbstractSocket::ConnectedState); |
|
1607 |
static_cast<QTcpSocket *>(sender())->close(); |
|
1608 |
} |
|
1609 |
||
1610 |
void tst_QTcpSocket::messageBoxSlot() |
|
1611 |
{ |
|
1612 |
#if !defined(Q_OS_VXWORKS) // no gui |
|
1613 |
QTcpSocket *socket = qobject_cast<QTcpSocket *>(sender()); |
|
1614 |
socket->deleteLater(); |
|
1615 |
QMessageBox box; |
|
1616 |
QTimer::singleShot(100, &box, SLOT(close())); |
|
1617 |
||
1618 |
// This should not delete the socket |
|
1619 |
box.exec(); |
|
1620 |
||
1621 |
// Fire a non-0 singleshot to leave time for the delete |
|
1622 |
QTimer::singleShot(250, this, SLOT(exitLoopSlot())); |
|
1623 |
#endif |
|
1624 |
} |
|
1625 |
//---------------------------------------------------------------------------------- |
|
1626 |
void tst_QTcpSocket::openMessageBoxInErrorSlot() |
|
1627 |
{ |
|
1628 |
#if defined(Q_OS_VXWORKS) // no gui |
|
1629 |
QSKIP("no default gui available on VxWorks", SkipAll); |
|
1630 |
#else |
|
1631 |
QTcpSocket *socket = newSocket(); |
|
1632 |
QPointer<QTcpSocket> p(socket); |
|
1633 |
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(messageBoxSlot())); |
|
1634 |
||
1635 |
socket->connectToHost("hostnotfoundhostnotfound.troll.no", 9999); // Host not found, fyi |
|
1636 |
enterLoop(30); |
|
1637 |
QVERIFY(!p); |
|
1638 |
#endif |
|
1639 |
} |
|
1640 |
||
1641 |
//---------------------------------------------------------------------------------- |
|
1642 |
#ifndef Q_OS_WIN |
|
1643 |
void tst_QTcpSocket::connectToLocalHostNoService() |
|
1644 |
{ |
|
1645 |
// This test was created after we received a report that claimed |
|
1646 |
// QTcpSocket would crash if trying to connect to "localhost" on a random |
|
1647 |
// port with no service listening. |
|
1648 |
QTcpSocket *socket = newSocket(); |
|
1649 |
socket->connectToHost("localhost", 31415); // no service running here, one suspects |
|
1650 |
||
1651 |
while(socket->state() == QTcpSocket::HostLookupState || socket->state() == QTcpSocket::ConnectingState) { |
|
1652 |
QTest::qWait(100); |
|
1653 |
} |
|
1654 |
QCOMPARE(socket->state(), QTcpSocket::UnconnectedState); |
|
1655 |
delete socket; |
|
1656 |
} |
|
1657 |
#endif |
|
1658 |
||
1659 |
//---------------------------------------------------------------------------------- |
|
1660 |
void tst_QTcpSocket::waitForConnectedInHostLookupSlot() |
|
1661 |
{ |
|
1662 |
// This test tries to reproduce the problem where waitForConnected() is |
|
1663 |
// called at a point where the host lookup is already done. QTcpSocket |
|
1664 |
// will try to abort the "pending lookup", but since it's already done and |
|
1665 |
// the queued signal is already underway, we will receive the signal after |
|
1666 |
// waitForConnected() has returned, and control goes back to the event |
|
1667 |
// loop. When the signal has been received, the connection is torn down, |
|
1668 |
// then reopened. Yikes. If we reproduce this by calling |
|
1669 |
// waitForConnected() inside hostLookupSlot(), it will even crash. |
|
1670 |
tmpSocket = newSocket(); |
|
1671 |
QEventLoop loop; |
|
1672 |
connect(tmpSocket, SIGNAL(connected()), &loop, SLOT(quit())); |
|
1673 |
QTimer timer; |
|
1674 |
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); |
|
1675 |
QSignalSpy timerSpy(&timer, SIGNAL(timeout())); |
|
1676 |
timer.start(15000); |
|
1677 |
||
1678 |
connect(tmpSocket, SIGNAL(hostFound()), this, SLOT(hostLookupSlot())); |
|
1679 |
tmpSocket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1680 |
||
1681 |
loop.exec(); |
|
1682 |
QCOMPARE(timerSpy.count(), 0); |
|
1683 |
||
1684 |
delete tmpSocket; |
|
1685 |
} |
|
1686 |
||
1687 |
void tst_QTcpSocket::hostLookupSlot() |
|
1688 |
{ |
|
1689 |
// This will fail to cancel the pending signal |
|
1690 |
QVERIFY(tmpSocket->waitForConnected(10000)); |
|
1691 |
} |
|
1692 |
||
1693 |
class Foo : public QObject |
|
1694 |
{ |
|
1695 |
Q_OBJECT |
|
1696 |
QTcpSocket *sock; |
|
1697 |
public: |
|
1698 |
bool attemptedToConnect; |
|
1699 |
bool networkTimeout; |
|
1700 |
int count; |
|
1701 |
||
1702 |
inline Foo(QObject *parent = 0) : QObject(parent) |
|
1703 |
{ |
|
1704 |
attemptedToConnect = false; |
|
1705 |
networkTimeout = false; |
|
1706 |
count = 0; |
|
1707 |
#ifndef QT_NO_OPENSSL |
|
1708 |
QFETCH_GLOBAL(bool, ssl); |
|
1709 |
if (ssl) |
|
1710 |
sock = new QSslSocket; |
|
1711 |
else |
|
1712 |
#endif |
|
1713 |
sock = new QTcpSocket; |
|
1714 |
connect(sock, SIGNAL(connected()), this, SLOT(connectedToIt())); |
|
1715 |
connect(sock, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)), |
|
1716 |
SLOT(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*))); |
|
1717 |
} |
|
1718 |
||
1719 |
inline ~Foo() |
|
1720 |
{ |
|
1721 |
delete sock; |
|
1722 |
} |
|
1723 |
||
1724 |
public slots: |
|
1725 |
inline void connectedToIt() |
|
1726 |
{ count++; } |
|
1727 |
||
1728 |
inline void doIt() |
|
1729 |
{ |
|
1730 |
attemptedToConnect = true; |
|
1731 |
sock->connectToHost(QtNetworkSettings::serverName(), 80); |
|
1732 |
||
1733 |
#ifdef Q_OS_MAC |
|
1734 |
pthread_yield_np(); |
|
1735 |
#elif defined Q_OS_LINUX |
|
1736 |
pthread_yield(); |
|
1737 |
#endif |
|
1738 |
if (!sock->waitForConnected()) { |
|
1739 |
networkTimeout = true; |
|
1740 |
} |
|
1741 |
tst_QTcpSocket::exitLoop(); |
|
1742 |
} |
|
1743 |
||
1744 |
inline void exitLoop() |
|
1745 |
{ |
|
1746 |
tst_QTcpSocket::exitLoop(); |
|
1747 |
} |
|
1748 |
||
1749 |
inline void proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *auth) |
|
1750 |
{ |
|
1751 |
auth->setUser("qsockstest"); |
|
1752 |
auth->setPassword("password"); |
|
1753 |
} |
|
1754 |
}; |
|
1755 |
||
1756 |
//---------------------------------------------------------------------------------- |
|
1757 |
void tst_QTcpSocket::waitForConnectedInHostLookupSlot2() |
|
1758 |
{ |
|
1759 |
#if defined(Q_OS_WIN) || defined(Q_OS_VXWORKS) |
|
1760 |
QSKIP("waitForConnectedInHostLookupSlot2 is not run on Windows and VxWorks", SkipAll); |
|
1761 |
#else |
|
1762 |
||
1763 |
Foo foo; |
|
1764 |
QPushButton top("Go", 0); |
|
1765 |
top.show(); |
|
1766 |
connect(&top, SIGNAL(clicked()), &foo, SLOT(doIt())); |
|
1767 |
||
1768 |
QTimer::singleShot(100, &top, SLOT(animateClick())); |
|
1769 |
QTimer::singleShot(5000, &foo, SLOT(exitLoop())); |
|
1770 |
||
1771 |
enterLoop(30); |
|
1772 |
if (timeout() || foo.networkTimeout) |
|
1773 |
QFAIL("Network timeout"); |
|
1774 |
||
1775 |
QVERIFY(foo.attemptedToConnect); |
|
1776 |
QCOMPARE(foo.count, 1); |
|
1777 |
#endif |
|
1778 |
} |
|
1779 |
||
1780 |
//---------------------------------------------------------------------------------- |
|
1781 |
void tst_QTcpSocket::readyReadSignalsAfterWaitForReadyRead() |
|
1782 |
{ |
|
1783 |
QTcpSocket *socket = newSocket(); |
|
1784 |
||
1785 |
QSignalSpy readyReadSpy(socket, SIGNAL(readyRead())); |
|
1786 |
||
1787 |
// Connect |
|
1788 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
1789 |
||
1790 |
// Wait for the read |
|
1791 |
QVERIFY(socket->waitForReadyRead(10000)); |
|
1792 |
||
1793 |
QCOMPARE(readyReadSpy.count(), 1); |
|
1794 |
||
1795 |
QString s = socket->readLine(); |
|
1796 |
#ifdef TEST_QNETWORK_PROXY |
|
1797 |
QNetworkProxy::ProxyType proxyType = QNetworkProxy::applicationProxy().type(); |
|
1798 |
#endif |
|
1799 |
QCOMPARE(s.toLatin1().constData(), QtNetworkSettings::expectedReplyIMAP().constData()); |
|
1800 |
QCOMPARE(socket->bytesAvailable(), qint64(0)); |
|
1801 |
||
1802 |
QCoreApplication::instance()->processEvents(); |
|
1803 |
QCOMPARE(socket->bytesAvailable(), qint64(0)); |
|
1804 |
QCOMPARE(readyReadSpy.count(), 1); |
|
1805 |
||
1806 |
delete socket; |
|
1807 |
} |
|
1808 |
||
1809 |
class TestThread2 : public QThread |
|
1810 |
{ |
|
1811 |
Q_OBJECT |
|
1812 |
public: |
|
1813 |
void run() |
|
1814 |
{ |
|
1815 |
QFile fileWriter("fifo"); |
|
1816 |
QVERIFY(fileWriter.open(QFile::WriteOnly)); |
|
1817 |
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32)); |
|
1818 |
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32)); |
|
1819 |
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32)); |
|
1820 |
QCOMPARE(fileWriter.write(QByteArray(32, '@')), qint64(32)); |
|
1821 |
} |
|
1822 |
}; |
|
1823 |
||
1824 |
//---------------------------------------------------------------------------------- |
|
1825 |
#ifdef Q_OS_LINUX |
|
1826 |
void tst_QTcpSocket::linuxKernelBugLocalSocket() |
|
1827 |
{ |
|
1828 |
QFile::remove("fifo"); |
|
1829 |
mkfifo("fifo", 0666); |
|
1830 |
||
1831 |
TestThread2 test; |
|
1832 |
test.start(); |
|
1833 |
||
1834 |
QFile fileReader("fifo"); |
|
1835 |
QVERIFY(fileReader.open(QFile::ReadOnly)); |
|
1836 |
||
1837 |
test.wait(); |
|
1838 |
||
1839 |
QTcpSocket *socket = newSocket(); |
|
1840 |
socket->setSocketDescriptor(fileReader.handle()); |
|
1841 |
QVERIFY(socket->waitForReadyRead(5000)); |
|
1842 |
QCOMPARE(socket->bytesAvailable(), qint64(128)); |
|
1843 |
||
1844 |
QFile::remove("fifo"); |
|
1845 |
||
1846 |
delete socket; |
|
1847 |
} |
|
1848 |
#endif |
|
1849 |
||
1850 |
//---------------------------------------------------------------------------------- |
|
1851 |
void tst_QTcpSocket::abortiveClose() |
|
1852 |
{ |
|
1853 |
QTcpServer server; |
|
1854 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
1855 |
connect(&server, SIGNAL(newConnection()), this, SLOT(exitLoopSlot())); |
|
1856 |
||
1857 |
QTcpSocket *clientSocket = newSocket(); |
|
1858 |
clientSocket->connectToHost(server.serverAddress(), server.serverPort()); |
|
1859 |
||
1860 |
enterLoop(10); |
|
1861 |
QVERIFY(server.hasPendingConnections()); |
|
1862 |
||
1863 |
QVERIFY(tmpSocket = server.nextPendingConnection()); |
|
1864 |
||
1865 |
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); |
|
1866 |
QSignalSpy readyReadSpy(clientSocket, SIGNAL(readyRead())); |
|
1867 |
QSignalSpy errorSpy(clientSocket, SIGNAL(error(QAbstractSocket::SocketError))); |
|
1868 |
||
1869 |
connect(clientSocket, SIGNAL(disconnected()), this, SLOT(exitLoopSlot())); |
|
1870 |
QTimer::singleShot(0, this, SLOT(abortiveClose_abortSlot())); |
|
1871 |
||
1872 |
enterLoop(5); |
|
1873 |
||
1874 |
QCOMPARE(readyReadSpy.count(), 0); |
|
1875 |
QCOMPARE(errorSpy.count(), 1); |
|
1876 |
||
1877 |
QCOMPARE(*static_cast<const int *>(errorSpy.at(0).at(0).constData()), |
|
1878 |
int(QAbstractSocket::RemoteHostClosedError)); |
|
1879 |
||
1880 |
delete clientSocket; |
|
1881 |
} |
|
1882 |
||
1883 |
void tst_QTcpSocket::abortiveClose_abortSlot() |
|
1884 |
{ |
|
1885 |
tmpSocket->abort(); |
|
1886 |
} |
|
1887 |
||
1888 |
//---------------------------------------------------------------------------------- |
|
1889 |
void tst_QTcpSocket::localAddressEmptyOnBSD() |
|
1890 |
{ |
|
1891 |
QTcpServer server; |
|
1892 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
1893 |
||
1894 |
QTcpSocket *tcpSocket = 0; |
|
1895 |
// we try 10 times, but note that this doesn't always provoke the bug |
|
1896 |
for (int i = 0; i < 10; ++i) { |
|
1897 |
delete tcpSocket; |
|
1898 |
tcpSocket = newSocket(); |
|
1899 |
tcpSocket->connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
1900 |
if (!tcpSocket->waitForConnected(0)) { |
|
1901 |
// to provoke the bug, we need a local socket that connects immediately |
|
1902 |
// --i; |
|
1903 |
tcpSocket->abort(); |
|
1904 |
if (tcpSocket->state() != QTcpSocket::UnconnectedState) |
|
1905 |
QVERIFY(tcpSocket->waitForDisconnected(-1)); |
|
1906 |
continue; |
|
1907 |
} |
|
1908 |
QCOMPARE(tcpSocket->localAddress(), QHostAddress(QHostAddress::LocalHost)); |
|
1909 |
} |
|
1910 |
delete tcpSocket; |
|
1911 |
} |
|
1912 |
||
1913 |
//---------------------------------------------------------------------------------- |
|
1914 |
void tst_QTcpSocket::zeroAndMinusOneReturns() |
|
1915 |
{ |
|
1916 |
QTcpSocket *socket = newSocket(); |
|
1917 |
socket->connectToHost(QtNetworkSettings::serverName(), 80); |
|
1918 |
socket->write("GET / HTTP/1.0\r\nConnection: keep-alive\r\n\r\n"); |
|
1919 |
QVERIFY(socket->waitForReadyRead(15000)); |
|
1920 |
||
1921 |
char c[16]; |
|
1922 |
QVERIFY(socket->getChar(c)); |
|
1923 |
QCOMPARE(socket->read(c, 16), qint64(16)); |
|
1924 |
QVERIFY(socket->readLine(c, 16) > 0); |
|
1925 |
QVERIFY(!socket->readAll().isEmpty()); |
|
1926 |
||
1927 |
// the last operation emptied the read buffer |
|
1928 |
// all read operations from this point on should fail |
|
1929 |
// with return 0 because the socket is still open |
|
1930 |
QVERIFY(socket->readAll().isEmpty()); |
|
1931 |
QCOMPARE(socket->read(c, 16), qint64(0)); |
|
1932 |
QCOMPARE(socket->readLine(c, 16), qint64(0)); |
|
1933 |
QVERIFY(!socket->getChar(c)); |
|
1934 |
||
1935 |
socket->write("GET / HTTP/1.0\r\n\r\n"); |
|
1936 |
QVERIFY(socket->waitForDisconnected(15000)); |
|
1937 |
QCOMPARE(socket->error(), QAbstractSocket::RemoteHostClosedError); |
|
1938 |
||
1939 |
QCOMPARE(socket->write("BLUBBER"), qint64(-1)); |
|
1940 |
QVERIFY(socket->getChar(c)); |
|
1941 |
QCOMPARE(socket->read(c, 16), qint64(16)); |
|
1942 |
QVERIFY(socket->readLine(c, 16) > 0); |
|
1943 |
QVERIFY(!socket->readAll().isEmpty()); |
|
1944 |
||
1945 |
// the last operation emptied the read buffer |
|
1946 |
// all read operations from this point on should fail |
|
1947 |
// with return -1 because the socket is not connected |
|
1948 |
QVERIFY(socket->readAll().isEmpty()); |
|
1949 |
QCOMPARE(socket->read(c, 16), qint64(-1)); |
|
1950 |
QCOMPARE(socket->readLine(c, 16), qint64(-1)); |
|
1951 |
QVERIFY(!socket->getChar(c)); |
|
1952 |
QVERIFY(!socket->putChar('a')); |
|
1953 |
||
1954 |
socket->close(); |
|
1955 |
||
1956 |
// now the QIODevice is closed, which means getChar complains |
|
1957 |
QCOMPARE(socket->write("BLUBBER"), qint64(-1)); |
|
1958 |
QCOMPARE(socket->read(c, 16), qint64(-1)); |
|
1959 |
QCOMPARE(socket->readLine(c, 16), qint64(-1)); |
|
1960 |
QVERIFY(!socket->getChar(c)); |
|
1961 |
QVERIFY(!socket->putChar('a')); |
|
1962 |
||
1963 |
delete socket; |
|
1964 |
} |
|
1965 |
||
1966 |
//---------------------------------------------------------------------------------- |
|
1967 |
void tst_QTcpSocket::connectionRefused() |
|
1968 |
{ |
|
1969 |
qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); |
|
1970 |
qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState"); |
|
1971 |
||
1972 |
QTcpSocket *socket = newSocket(); |
|
1973 |
QSignalSpy stateSpy(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState))); |
|
1974 |
QSignalSpy errorSpy(socket, SIGNAL(error(QAbstractSocket::SocketError))); |
|
1975 |
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
1976 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1977 |
||
1978 |
socket->connectToHost(QtNetworkSettings::serverName(), 144); |
|
1979 |
||
1980 |
enterLoop(10); |
|
1981 |
disconnect(socket, SIGNAL(error(QAbstractSocket::SocketError)), |
|
1982 |
&QTestEventLoop::instance(), SLOT(exitLoop())); |
|
1983 |
QVERIFY2(!timeout(), "Network timeout"); |
|
1984 |
||
1985 |
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState); |
|
1986 |
QCOMPARE(socket->error(), QAbstractSocket::ConnectionRefusedError); |
|
1987 |
||
1988 |
QCOMPARE(stateSpy.count(), 3); |
|
1989 |
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(0).at(0)), QAbstractSocket::HostLookupState); |
|
1990 |
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(1).at(0)), QAbstractSocket::ConnectingState); |
|
1991 |
QCOMPARE(qVariantValue<QAbstractSocket::SocketState>(stateSpy.at(2).at(0)), QAbstractSocket::UnconnectedState); |
|
1992 |
QCOMPARE(errorSpy.count(), 1); |
|
1993 |
||
1994 |
delete socket; |
|
1995 |
} |
|
1996 |
||
1997 |
//---------------------------------------------------------------------------------- |
|
1998 |
void tst_QTcpSocket::suddenRemoteDisconnect_data() |
|
1999 |
{ |
|
2000 |
QTest::addColumn<QString>("client"); |
|
2001 |
QTest::addColumn<QString>("server"); |
|
2002 |
||
2003 |
#ifdef QT3_SUPPORT |
|
2004 |
QTest::newRow("Qt3 Client <-> Qt3 Server") << QString::fromLatin1("qt3client") << QString::fromLatin1("qt3server"); |
|
2005 |
QTest::newRow("Qt3 Client <-> Qt4 Server") << QString::fromLatin1("qt3client") << QString::fromLatin1("qt4server"); |
|
2006 |
QTest::newRow("Qt4 Client <-> Qt3 Server") << QString::fromLatin1("qt4client") << QString::fromLatin1("qt3server"); |
|
2007 |
#endif |
|
2008 |
||
2009 |
QTest::newRow("Qt4 Client <-> Qt4 Server") << QString::fromLatin1("qt4client") << QString::fromLatin1("qt4server"); |
|
2010 |
} |
|
2011 |
||
2012 |
void tst_QTcpSocket::suddenRemoteDisconnect() |
|
2013 |
{ |
|
2014 |
#if defined(Q_OS_WINCE) || defined(Q_OS_VXWORKS) |
|
2015 |
QSKIP("stressTest subprocess needs Qt3Support", SkipAll); |
|
2016 |
#elif defined( Q_OS_SYMBIAN ) |
|
2017 |
QSKIP("Symbian: QProcess IO is not yet supported, fix when supported", SkipAll); |
|
2018 |
#else |
|
2019 |
QFETCH(QString, client); |
|
2020 |
QFETCH(QString, server); |
|
2021 |
||
2022 |
QFETCH_GLOBAL(bool, setProxy); |
|
2023 |
if (setProxy) |
|
2024 |
return; |
|
2025 |
QFETCH_GLOBAL(bool, ssl); |
|
2026 |
if (ssl) |
|
2027 |
return; |
|
2028 |
||
2029 |
// Start server |
|
2030 |
QProcess serverProcess; |
|
2031 |
serverProcess.setReadChannel(QProcess::StandardError); |
|
2032 |
serverProcess.start(QString::fromLatin1("stressTest/stressTest %1").arg(server), |
|
2033 |
QIODevice::ReadWrite | QIODevice::Text); |
|
2034 |
while (!serverProcess.canReadLine()) |
|
2035 |
QVERIFY(serverProcess.waitForReadyRead(10000)); |
|
2036 |
QCOMPARE(serverProcess.readLine().data(), (server.toLatin1() + "\n").data()); |
|
2037 |
||
2038 |
// Start client |
|
2039 |
QProcess clientProcess; |
|
2040 |
clientProcess.setReadChannel(QProcess::StandardError); |
|
2041 |
clientProcess.start(QString::fromLatin1("stressTest/stressTest %1").arg(client), |
|
2042 |
QIODevice::ReadWrite | QIODevice::Text); |
|
2043 |
while (!clientProcess.canReadLine()) |
|
2044 |
QVERIFY(clientProcess.waitForReadyRead(10000)); |
|
2045 |
QCOMPARE(clientProcess.readLine().data(), (client.toLatin1() + "\n").data()); |
|
2046 |
||
2047 |
// Let them play for a while |
|
2048 |
qDebug("Running stress test for 5 seconds"); |
|
2049 |
QEventLoop loop; |
|
2050 |
connect(&serverProcess, SIGNAL(finished(int)), &loop, SLOT(quit())); |
|
2051 |
connect(&clientProcess, SIGNAL(finished(int)), &loop, SLOT(quit())); |
|
2052 |
QTime stopWatch; |
|
2053 |
stopWatch.start(); |
|
2054 |
QTimer::singleShot(20000, &loop, SLOT(quit())); |
|
2055 |
||
2056 |
while ((serverProcess.state() == QProcess::Running |
|
2057 |
|| clientProcess.state() == QProcess::Running) && stopWatch.elapsed() < 20000) |
|
2058 |
loop.exec(); |
|
2059 |
||
2060 |
QVERIFY(stopWatch.elapsed() < 20000); |
|
2061 |
||
2062 |
// Check that both exited normally. |
|
2063 |
QCOMPARE(clientProcess.readAll().constData(), "SUCCESS\n"); |
|
2064 |
QCOMPARE(serverProcess.readAll().constData(), "SUCCESS\n"); |
|
2065 |
#endif |
|
2066 |
} |
|
2067 |
||
2068 |
//---------------------------------------------------------------------------------- |
|
2069 |
void tst_QTcpSocket::connectToMultiIP() |
|
2070 |
{ |
|
2071 |
QSKIP("TODO: setup DNS in the new network", SkipAll); |
|
2072 |
||
2073 |
#if defined(Q_OS_VXWORKS) |
|
2074 |
QSKIP("VxSim in standard config doesn't even run a DNS resolver", SkipAll); |
|
2075 |
#else |
|
2076 |
QFETCH_GLOBAL(bool, ssl); |
|
2077 |
if (ssl) |
|
2078 |
return; |
|
2079 |
QFETCH_GLOBAL(bool, setProxy); |
|
2080 |
if (setProxy) |
|
2081 |
QSKIP("This test takes too long if we also add the proxies.", SkipSingle); |
|
2082 |
||
2083 |
qDebug("Please wait, this test can take a while..."); |
|
2084 |
||
2085 |
QTcpSocket *socket = newSocket(); |
|
2086 |
// rationale: this domain resolves to 3 A-records, 2 of them are |
|
2087 |
// invalid. QTcpSocket should never spend more than 30 seconds per IP, and |
|
2088 |
// 30s*2 = 60s. |
|
2089 |
QTime stopWatch; |
|
2090 |
stopWatch.start(); |
|
2091 |
socket->connectToHost("multi.dev.troll.no", 80); |
|
2092 |
QVERIFY(socket->waitForConnected(60500)); |
|
2093 |
QVERIFY(stopWatch.elapsed() < 70000); |
|
2094 |
socket->abort(); |
|
2095 |
||
2096 |
stopWatch.restart(); |
|
2097 |
socket->connectToHost("multi.dev.troll.no", 81); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2098 |
QVERIFY(!socket->waitForConnected(2000)); |
0 | 2099 |
QVERIFY(stopWatch.elapsed() < 2000); |
2100 |
QCOMPARE(socket->error(), QAbstractSocket::SocketTimeoutError); |
|
2101 |
||
2102 |
delete socket; |
|
2103 |
#endif |
|
2104 |
} |
|
2105 |
||
2106 |
//---------------------------------------------------------------------------------- |
|
2107 |
void tst_QTcpSocket::moveToThread0() |
|
2108 |
{ |
|
2109 |
QFETCH_GLOBAL(int, proxyType); |
|
2110 |
if (proxyType & AuthMask) |
|
2111 |
return; |
|
2112 |
||
2113 |
{ |
|
2114 |
// Case 1: Moved after connecting, before waiting for connection. |
|
2115 |
QTcpSocket *socket = newSocket();; |
|
2116 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
2117 |
socket->moveToThread(0); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2118 |
QVERIFY(socket->waitForConnected(5000)); |
0 | 2119 |
socket->write("XXX LOGOUT\r\n"); |
2120 |
QVERIFY(socket->waitForBytesWritten(5000)); |
|
2121 |
QVERIFY(socket->waitForDisconnected()); |
|
2122 |
delete socket; |
|
2123 |
} |
|
2124 |
{ |
|
2125 |
// Case 2: Moved before connecting |
|
2126 |
QTcpSocket *socket = newSocket(); |
|
2127 |
socket->moveToThread(0); |
|
2128 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2129 |
QVERIFY(socket->waitForConnected(5000)); |
0 | 2130 |
socket->write("XXX LOGOUT\r\n"); |
2131 |
QVERIFY(socket->waitForBytesWritten(5000)); |
|
2132 |
QVERIFY(socket->waitForDisconnected()); |
|
2133 |
delete socket; |
|
2134 |
} |
|
2135 |
{ |
|
2136 |
// Case 3: Moved after writing, while waiting for bytes to be written. |
|
2137 |
QTcpSocket *socket = newSocket(); |
|
2138 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2139 |
QVERIFY(socket->waitForConnected(5000)); |
0 | 2140 |
socket->write("XXX LOGOUT\r\n"); |
2141 |
socket->moveToThread(0); |
|
2142 |
QVERIFY(socket->waitForBytesWritten(5000)); |
|
2143 |
QVERIFY(socket->waitForDisconnected()); |
|
2144 |
delete socket; |
|
2145 |
} |
|
2146 |
{ |
|
2147 |
// Case 4: Moved after writing, while waiting for response. |
|
2148 |
QTcpSocket *socket = newSocket(); |
|
2149 |
socket->connectToHost(QtNetworkSettings::serverName(), 143); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2150 |
QVERIFY(socket->waitForConnected(5000)); |
0 | 2151 |
socket->write("XXX LOGOUT\r\n"); |
2152 |
QVERIFY(socket->waitForBytesWritten(5000)); |
|
2153 |
socket->moveToThread(0); |
|
2154 |
QVERIFY(socket->waitForDisconnected()); |
|
2155 |
delete socket; |
|
2156 |
} |
|
2157 |
} |
|
2158 |
||
2159 |
void tst_QTcpSocket::increaseReadBufferSize() |
|
2160 |
{ |
|
2161 |
QTcpServer server; |
|
2162 |
QTcpSocket *active = newSocket(); |
|
2163 |
connect(active, SIGNAL(readyRead()), SLOT(exitLoopSlot())); |
|
2164 |
||
2165 |
// connect two sockets to each other: |
|
2166 |
QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
2167 |
active->connectToHost("127.0.0.1", server.serverPort()); |
|
2168 |
QVERIFY(active->waitForConnected(5000)); |
|
2169 |
QVERIFY(server.waitForNewConnection(5000)); |
|
2170 |
||
2171 |
QTcpSocket *passive = server.nextPendingConnection(); |
|
2172 |
QVERIFY(passive); |
|
2173 |
||
2174 |
// now write 512 bytes of data on one end |
|
2175 |
QByteArray data(512, 'a'); |
|
2176 |
passive->write(data); |
|
2177 |
QVERIFY2(passive->waitForBytesWritten(5000), "Network timeout"); |
|
2178 |
||
2179 |
// set the read buffer size to less than what was written and iterate: |
|
2180 |
active->setReadBufferSize(256); |
|
2181 |
enterLoop(10); |
|
2182 |
QVERIFY2(!timeout(), "Network timeout"); |
|
2183 |
QCOMPARE(active->bytesAvailable(), active->readBufferSize()); |
|
2184 |
||
2185 |
// increase the buffer size and iterate again: |
|
2186 |
active->setReadBufferSize(384); |
|
2187 |
enterLoop(10); |
|
2188 |
QVERIFY2(!timeout(), "Network timeout"); |
|
2189 |
QCOMPARE(active->bytesAvailable(), active->readBufferSize()); |
|
2190 |
||
2191 |
// once more, but now it should read everything there was to read |
|
2192 |
active->setReadBufferSize(1024); |
|
2193 |
enterLoop(10); |
|
2194 |
QVERIFY2(!timeout(), "Network timeout"); |
|
2195 |
QCOMPARE(active->bytesAvailable(), qint64(data.size())); |
|
2196 |
||
2197 |
// drain it and compare |
|
2198 |
QCOMPARE(active->readAll(), data); |
|
2199 |
||
2200 |
// now one more test by setting the buffer size to unlimited: |
|
2201 |
passive->write(data); |
|
2202 |
QVERIFY2(passive->waitForBytesWritten(5000), "Network timeout"); |
|
2203 |
active->setReadBufferSize(256); |
|
2204 |
enterLoop(10); |
|
2205 |
QVERIFY2(!timeout(), "Network timeout"); |
|
2206 |
QCOMPARE(active->bytesAvailable(), active->readBufferSize()); |
|
2207 |
active->setReadBufferSize(0); |
|
2208 |
enterLoop(10); |
|
2209 |
QVERIFY2(!timeout(), "Network timeout"); |
|
2210 |
QCOMPARE(active->bytesAvailable(), qint64(data.size())); |
|
2211 |
QCOMPARE(active->readAll(), data); |
|
2212 |
||
2213 |
delete active; |
|
2214 |
} |
|
2215 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2216 |
void tst_QTcpSocket::taskQtBug5799ConnectionErrorWaitForConnected() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2217 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2218 |
QFETCH_GLOBAL(bool, setProxy); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2219 |
if (setProxy) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2220 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2221 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2222 |
// check that we get a proper error connecting to port 12346 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2223 |
// use waitForConnected, e.g. this should use a synchronous select() on the OS level |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2224 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2225 |
QTcpSocket socket; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2226 |
socket.connectToHost(QtNetworkSettings::serverName(), 12346); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2227 |
QTime timer; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2228 |
timer.start(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2229 |
socket.waitForConnected(10000); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2230 |
QVERIFY2(timer.elapsed() < 9900, "Connection to closed port timed out instead of refusing, something is wrong"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2231 |
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2232 |
QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2233 |
QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2234 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2235 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2236 |
void tst_QTcpSocket::taskQtBug5799ConnectionErrorEventLoop() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2237 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2238 |
QFETCH_GLOBAL(bool, setProxy); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2239 |
if (setProxy) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2240 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2241 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2242 |
// check that we get a proper error connecting to port 12346 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2243 |
// This testcase uses an event loop |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2244 |
QTcpSocket socket; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2245 |
connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), &QTestEventLoop::instance(), SLOT(exitLoop())); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2246 |
socket.connectToHost(QtNetworkSettings::serverName(), 12346); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2247 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2248 |
QTestEventLoop::instance().enterLoop(10); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2249 |
QVERIFY2(!QTestEventLoop::instance().timeout(), "Connection to closed port timed out instead of refusing, something is wrong"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2250 |
QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!"); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2251 |
QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2252 |
QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2253 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2254 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2255 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2256 |
|
0 | 2257 |
#ifdef TEST_QNETWORK_PROXY |
2258 |
void tst_QTcpSocket::invalidProxy_data() |
|
2259 |
{ |
|
2260 |
QTest::addColumn<int>("type"); |
|
2261 |
QTest::addColumn<QString>("host"); |
|
2262 |
QTest::addColumn<int>("port"); |
|
2263 |
QTest::addColumn<bool>("failsAtConnect"); |
|
2264 |
QTest::addColumn<int>("expectedError"); |
|
2265 |
||
2266 |
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(); |
|
2267 |
QTest::newRow("ftp-proxy") << int(QNetworkProxy::FtpCachingProxy) << fluke << 21 << true |
|
2268 |
<< int(QAbstractSocket::UnsupportedSocketOperationError); |
|
2269 |
QTest::newRow("http-caching-proxy") << int(QNetworkProxy::HttpCachingProxy) << fluke << 3128 << true |
|
2270 |
<< int(QAbstractSocket::UnsupportedSocketOperationError); |
|
2271 |
QTest::newRow("no-such-host-socks5") << int(QNetworkProxy::Socks5Proxy) |
|
2272 |
<< "this-host-will-never-exist.troll.no" << 1080 << false |
|
2273 |
<< int(QAbstractSocket::ProxyNotFoundError); |
|
2274 |
QTest::newRow("no-such-host-http") << int(QNetworkProxy::HttpProxy) |
|
2275 |
<< "this-host-will-never-exist.troll.no" << 3128 << false |
|
2276 |
<< int(QAbstractSocket::ProxyNotFoundError); |
|
2277 |
#if !defined(Q_OS_SYMBIAN) |
|
2278 |
QTest::newRow("http-on-socks5") << int(QNetworkProxy::HttpProxy) << fluke << 1080 << false |
|
2279 |
<< int(QAbstractSocket::ProxyConnectionClosedError); |
|
2280 |
QTest::newRow("socks5-on-http") << int(QNetworkProxy::Socks5Proxy) << fluke << 3128 << false |
|
2281 |
<< int(QAbstractSocket::SocketTimeoutError); |
|
2282 |
#endif |
|
2283 |
} |
|
2284 |
||
2285 |
void tst_QTcpSocket::invalidProxy() |
|
2286 |
{ |
|
2287 |
QFETCH_GLOBAL(bool, setProxy); |
|
2288 |
if (setProxy) |
|
2289 |
return; |
|
2290 |
||
2291 |
QFETCH(int, type); |
|
2292 |
QFETCH(QString, host); |
|
2293 |
QFETCH(int, port); |
|
2294 |
QFETCH(bool, failsAtConnect); |
|
2295 |
QNetworkProxy::ProxyType proxyType = QNetworkProxy::ProxyType(type); |
|
2296 |
QNetworkProxy proxy(proxyType, host, port); |
|
2297 |
||
2298 |
QTcpSocket *socket = newSocket(); |
|
2299 |
socket->setProxy(proxy); |
|
2300 |
socket->connectToHost(QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(), 80); |
|
2301 |
||
2302 |
if (failsAtConnect) { |
|
2303 |
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState); |
|
2304 |
} else { |
|
2305 |
QCOMPARE(socket->state(), QAbstractSocket::ConnectingState); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2306 |
QVERIFY(!socket->waitForConnected(5000)); |
0 | 2307 |
} |
2308 |
QVERIFY(!socket->errorString().isEmpty()); |
|
2309 |
||
2310 |
// note: the following test is not a hard failure. |
|
2311 |
// Sometimes, error codes change for the better |
|
2312 |
QTEST(int(socket->error()), "expectedError"); |
|
2313 |
||
2314 |
delete socket; |
|
2315 |
} |
|
2316 |
||
2317 |
// copied from tst_qnetworkreply.cpp |
|
2318 |
class MyProxyFactory: public QNetworkProxyFactory |
|
2319 |
{ |
|
2320 |
public: |
|
2321 |
int callCount; |
|
2322 |
QList<QNetworkProxy> toReturn; |
|
2323 |
QNetworkProxyQuery lastQuery; |
|
2324 |
inline MyProxyFactory() { clear(); } |
|
2325 |
||
2326 |
inline void clear() |
|
2327 |
{ |
|
2328 |
callCount = 0; |
|
2329 |
toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy; |
|
2330 |
lastQuery = QNetworkProxyQuery(); |
|
2331 |
} |
|
2332 |
||
2333 |
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query) |
|
2334 |
{ |
|
2335 |
lastQuery = query; |
|
2336 |
++callCount; |
|
2337 |
return toReturn; |
|
2338 |
} |
|
2339 |
}; |
|
2340 |
||
2341 |
void tst_QTcpSocket::proxyFactory_data() |
|
2342 |
{ |
|
2343 |
QTest::addColumn<QList<QNetworkProxy> >("proxyList"); |
|
2344 |
QTest::addColumn<QNetworkProxy>("proxyUsed"); |
|
2345 |
QTest::addColumn<bool>("failsAtConnect"); |
|
2346 |
QTest::addColumn<int>("expectedError"); |
|
2347 |
||
2348 |
QList<QNetworkProxy> proxyList; |
|
2349 |
||
2350 |
// tests that do connect |
|
2351 |
||
2352 |
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3129); |
|
2353 |
QTest::newRow("http") |
|
2354 |
<< proxyList << proxyList.at(0) |
|
2355 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
2356 |
||
2357 |
proxyList.clear(); |
|
2358 |
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2359 |
QTest::newRow("socks5") |
|
2360 |
<< proxyList << proxyList.at(0) |
|
2361 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
2362 |
||
2363 |
proxyList.clear(); |
|
2364 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2365 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2366 |
QTest::newRow("cachinghttp+socks5") |
|
2367 |
<< proxyList << proxyList.at(1) |
|
2368 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
2369 |
||
2370 |
proxyList.clear(); |
|
2371 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
2372 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129) |
|
2373 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1081); |
|
2374 |
QTest::newRow("ftp+cachinghttp+socks5") |
|
2375 |
<< proxyList << proxyList.at(2) |
|
2376 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
2377 |
||
2378 |
// tests that fail to connect |
|
2379 |
proxyList.clear(); |
|
2380 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2381 |
QTest::newRow("cachinghttp") |
|
2382 |
<< proxyList << QNetworkProxy() |
|
2383 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
2384 |
||
2385 |
proxyList.clear(); |
|
2386 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
2387 |
QTest::newRow("ftp") |
|
2388 |
<< proxyList << QNetworkProxy() |
|
2389 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
2390 |
||
2391 |
proxyList.clear(); |
|
2392 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
2393 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3129); |
|
2394 |
QTest::newRow("ftp+cachinghttp") |
|
2395 |
<< proxyList << QNetworkProxy() |
|
2396 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
2397 |
} |
|
2398 |
||
2399 |
void tst_QTcpSocket::proxyFactory() |
|
2400 |
{ |
|
2401 |
QFETCH_GLOBAL(bool, setProxy); |
|
2402 |
if (setProxy) |
|
2403 |
return; |
|
2404 |
||
2405 |
QFETCH(QList<QNetworkProxy>, proxyList); |
|
2406 |
QFETCH(QNetworkProxy, proxyUsed); |
|
2407 |
QFETCH(bool, failsAtConnect); |
|
2408 |
||
2409 |
MyProxyFactory *factory = new MyProxyFactory; |
|
2410 |
factory->toReturn = proxyList; |
|
2411 |
QNetworkProxyFactory::setApplicationProxyFactory(factory); |
|
2412 |
||
2413 |
QTcpSocket *socket = newSocket(); |
|
2414 |
QString host = QtNetworkSettings::serverName(); |
|
2415 |
socket->connectToHost(host, 80); |
|
2416 |
||
2417 |
// Verify that the factory was called properly |
|
2418 |
QCOMPARE(factory->callCount, 1); |
|
2419 |
QCOMPARE(factory->lastQuery, QNetworkProxyQuery(host, 80)); |
|
2420 |
||
2421 |
if (failsAtConnect) { |
|
2422 |
QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState); |
|
2423 |
} else { |
|
2424 |
QCOMPARE(socket->state(), QAbstractSocket::ConnectingState); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
2425 |
QVERIFY(socket->waitForConnected(5000)); |
0 | 2426 |
QCOMPARE(proxyAuthCalled, 1); |
2427 |
} |
|
2428 |
QVERIFY(!socket->errorString().isEmpty()); |
|
2429 |
||
2430 |
// note: the following test is not a hard failure. |
|
2431 |
// Sometimes, error codes change for the better |
|
2432 |
QTEST(int(socket->error()), "expectedError"); |
|
2433 |
||
2434 |
delete socket; |
|
2435 |
} |
|
2436 |
#endif |
|
2437 |
||
2438 |
||
2439 |
QTEST_MAIN(tst_QTcpSocket) |
|
2440 |
#include "tst_qtcpsocket.moc" |