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