author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 14 May 2010 16:40:13 +0300 | |
changeset 22 | 79de32ba3296 |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
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 |
// Just to get Q_OS_SYMBIAN |
|
43 |
#include <qglobal.h> |
|
44 |
#if defined(_WIN32) && !defined(Q_OS_SYMBIAN) |
|
45 |
#include <winsock2.h> |
|
46 |
#else |
|
47 |
#include <sys/types.h> |
|
48 |
#include <sys/socket.h> |
|
49 |
#define SOCKET int |
|
50 |
#define INVALID_SOCKET -1 |
|
51 |
#endif |
|
52 |
||
53 |
#include <QtTest/QtTest> |
|
54 |
||
55 |
#ifndef Q_OS_WIN |
|
56 |
#include <unistd.h> |
|
57 |
#include <sys/ioctl.h> |
|
58 |
#endif |
|
59 |
||
60 |
#include <qcoreapplication.h> |
|
61 |
#include <qtcpsocket.h> |
|
62 |
#include <qtcpserver.h> |
|
63 |
#include <qhostaddress.h> |
|
64 |
#include <qprocess.h> |
|
65 |
#include <qstringlist.h> |
|
66 |
#include <qplatformdefs.h> |
|
67 |
#include <qhostinfo.h> |
|
68 |
||
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
69 |
#include <QNetworkProxy> |
0 | 70 |
Q_DECLARE_METATYPE(QNetworkProxy) |
71 |
Q_DECLARE_METATYPE(QList<QNetworkProxy>) |
|
72 |
||
73 |
#include "../network-settings.h" |
|
74 |
||
75 |
//TESTED_CLASS= |
|
76 |
//TESTED_FILES= |
|
77 |
||
78 |
class tst_QTcpServer : public QObject |
|
79 |
{ |
|
80 |
Q_OBJECT |
|
81 |
||
82 |
public: |
|
83 |
tst_QTcpServer(); |
|
84 |
virtual ~tst_QTcpServer(); |
|
85 |
||
86 |
||
87 |
public slots: |
|
88 |
void initTestCase_data(); |
|
89 |
void init(); |
|
90 |
void cleanup(); |
|
91 |
private slots: |
|
92 |
void getSetCheck(); |
|
93 |
void constructing(); |
|
94 |
void clientServerLoop(); |
|
95 |
void ipv6Server(); |
|
96 |
void crashTests(); |
|
97 |
void maxPendingConnections(); |
|
98 |
void listenError(); |
|
99 |
void waitForConnectionTest(); |
|
100 |
void setSocketDescriptor(); |
|
101 |
void listenWhileListening(); |
|
102 |
void addressReusable(); |
|
103 |
void setNewSocketDescriptorBlocking(); |
|
104 |
void invalidProxy_data(); |
|
105 |
void invalidProxy(); |
|
106 |
void proxyFactory_data(); |
|
107 |
void proxyFactory(); |
|
108 |
}; |
|
109 |
||
110 |
// Testing get/set functions |
|
111 |
void tst_QTcpServer::getSetCheck() |
|
112 |
{ |
|
113 |
QTcpServer obj1; |
|
114 |
// int QTcpServer::maxPendingConnections() |
|
115 |
// void QTcpServer::setMaxPendingConnections(int) |
|
116 |
obj1.setMaxPendingConnections(0); |
|
117 |
QCOMPARE(0, obj1.maxPendingConnections()); |
|
118 |
obj1.setMaxPendingConnections(INT_MIN); |
|
119 |
QCOMPARE(INT_MIN, obj1.maxPendingConnections()); |
|
120 |
obj1.setMaxPendingConnections(INT_MAX); |
|
121 |
QCOMPARE(INT_MAX, obj1.maxPendingConnections()); |
|
122 |
} |
|
123 |
||
124 |
tst_QTcpServer::tst_QTcpServer() |
|
125 |
{ |
|
126 |
Q_SET_DEFAULT_IAP |
|
127 |
} |
|
128 |
||
129 |
tst_QTcpServer::~tst_QTcpServer() |
|
130 |
{ |
|
131 |
} |
|
132 |
||
133 |
void tst_QTcpServer::initTestCase_data() |
|
134 |
{ |
|
135 |
QTest::addColumn<bool>("setProxy"); |
|
136 |
QTest::addColumn<int>("proxyType"); |
|
137 |
||
138 |
QTest::newRow("WithoutProxy") << false << 0; |
|
139 |
QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy); |
|
140 |
} |
|
141 |
||
142 |
void tst_QTcpServer::init() |
|
143 |
{ |
|
144 |
QFETCH_GLOBAL(bool, setProxy); |
|
145 |
if (setProxy) { |
|
146 |
QFETCH_GLOBAL(int, proxyType); |
|
147 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
148 |
QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080)); |
|
149 |
} |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
void tst_QTcpServer::cleanup() |
|
154 |
{ |
|
155 |
QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
|
156 |
} |
|
157 |
||
158 |
//---------------------------------------------------------------------------------- |
|
159 |
||
160 |
void tst_QTcpServer::constructing() |
|
161 |
{ |
|
162 |
QTcpServer socket; |
|
163 |
||
164 |
// Check the initial state of the QTcpSocket. |
|
165 |
QCOMPARE(socket.isListening(), false); |
|
166 |
QCOMPARE((int)socket.serverPort(), 0); |
|
167 |
QCOMPARE(socket.serverAddress(), QHostAddress()); |
|
168 |
QCOMPARE(socket.maxPendingConnections(), 30); |
|
169 |
QCOMPARE(socket.hasPendingConnections(), false); |
|
170 |
QCOMPARE(socket.socketDescriptor(), -1); |
|
171 |
QCOMPARE(socket.serverError(), QAbstractSocket::UnknownSocketError); |
|
172 |
||
173 |
// Check the state of the socket layer? |
|
174 |
} |
|
175 |
||
176 |
//---------------------------------------------------------------------------------- |
|
177 |
void tst_QTcpServer::clientServerLoop() |
|
178 |
{ |
|
179 |
QTcpServer server; |
|
180 |
||
181 |
QSignalSpy spy(&server, SIGNAL(newConnection())); |
|
182 |
||
183 |
QVERIFY(!server.isListening()); |
|
184 |
QVERIFY(!server.hasPendingConnections()); |
|
185 |
QVERIFY(server.listen(QHostAddress::Any, 11423)); |
|
186 |
QVERIFY(server.isListening()); |
|
187 |
||
188 |
QTcpSocket client; |
|
189 |
||
190 |
QHostAddress serverAddress = QHostAddress::LocalHost; |
|
191 |
if (!(server.serverAddress() == QHostAddress::Any)) |
|
192 |
serverAddress = server.serverAddress(); |
|
193 |
||
194 |
client.connectToHost(serverAddress, server.serverPort()); |
|
195 |
QVERIFY(client.waitForConnected(5000)); |
|
196 |
||
197 |
QVERIFY(server.waitForNewConnection(5000)); |
|
198 |
QVERIFY(server.hasPendingConnections()); |
|
199 |
||
200 |
QCOMPARE(spy.count(), 1); |
|
201 |
||
202 |
QTcpSocket *serverSocket = server.nextPendingConnection(); |
|
203 |
QVERIFY(serverSocket != 0); |
|
204 |
||
205 |
QVERIFY(serverSocket->write("Greetings, client!\n", 19) == 19); |
|
206 |
serverSocket->flush(); |
|
207 |
||
208 |
QVERIFY(client.waitForReadyRead(5000)); |
|
209 |
QByteArray arr = client.readAll(); |
|
210 |
QCOMPARE(arr.constData(), "Greetings, client!\n"); |
|
211 |
||
212 |
QVERIFY(client.write("Well, hello to you!\n", 20) == 20); |
|
213 |
client.flush(); |
|
214 |
||
215 |
QVERIFY(serverSocket->waitForReadyRead(5000)); |
|
216 |
arr = serverSocket->readAll(); |
|
217 |
QCOMPARE(arr.constData(), "Well, hello to you!\n"); |
|
218 |
} |
|
219 |
||
220 |
//---------------------------------------------------------------------------------- |
|
221 |
void tst_QTcpServer::ipv6Server() |
|
222 |
{ |
|
223 |
#if defined(Q_OS_SYMBIAN) |
|
224 |
QSKIP("Symbian: IPv6 is not yet supported", SkipAll); |
|
225 |
#endif |
|
226 |
//### need to enter the event loop for the server to get the connection ?? ( windows) |
|
227 |
QTcpServer server; |
|
228 |
if (!server.listen(QHostAddress::LocalHostIPv6, 8944)) { |
|
229 |
QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError); |
|
230 |
return; |
|
231 |
} |
|
232 |
||
233 |
QVERIFY(server.serverPort() == 8944); |
|
234 |
QVERIFY(server.serverAddress() == QHostAddress::LocalHostIPv6); |
|
235 |
||
236 |
QTcpSocket client; |
|
237 |
client.connectToHost("::1", 8944); |
|
238 |
QVERIFY(client.waitForConnected(5000)); |
|
239 |
||
240 |
QVERIFY(server.waitForNewConnection()); |
|
241 |
QVERIFY(server.hasPendingConnections()); |
|
242 |
||
243 |
QTcpSocket *serverSocket = 0; |
|
244 |
QVERIFY((serverSocket = server.nextPendingConnection())); |
|
245 |
} |
|
246 |
||
247 |
//---------------------------------------------------------------------------------- |
|
248 |
void tst_QTcpServer::crashTests() |
|
249 |
{ |
|
250 |
QTcpServer server; |
|
251 |
server.close(); |
|
252 |
QVERIFY(server.listen()); |
|
253 |
} |
|
254 |
||
255 |
//---------------------------------------------------------------------------------- |
|
256 |
void tst_QTcpServer::maxPendingConnections() |
|
257 |
{ |
|
258 |
QFETCH_GLOBAL(bool, setProxy); |
|
259 |
if (setProxy) { |
|
260 |
QFETCH_GLOBAL(int, proxyType); |
|
261 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
262 |
QSKIP("With socks5 only 1 connection is allowed ever", SkipAll); |
|
263 |
} |
|
264 |
} |
|
265 |
//### sees to fail sometimes ... a timing issue with the test on windows |
|
266 |
QTcpServer server; |
|
267 |
server.setMaxPendingConnections(2); |
|
268 |
||
269 |
QTcpSocket socket1; |
|
270 |
QTcpSocket socket2; |
|
271 |
QTcpSocket socket3; |
|
272 |
||
273 |
QVERIFY(server.listen()); |
|
274 |
||
275 |
socket1.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
276 |
socket2.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
277 |
socket3.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
278 |
||
279 |
QVERIFY(server.waitForNewConnection(5000)); |
|
280 |
||
281 |
QVERIFY(server.hasPendingConnections()); |
|
282 |
QVERIFY(server.nextPendingConnection()); |
|
283 |
QVERIFY(server.hasPendingConnections()); |
|
284 |
QVERIFY(server.nextPendingConnection()); |
|
285 |
QVERIFY(!server.hasPendingConnections()); |
|
286 |
QCOMPARE(server.nextPendingConnection(), (QTcpSocket*)0); |
|
287 |
||
288 |
QVERIFY(server.waitForNewConnection(5000)); |
|
289 |
||
290 |
QVERIFY(server.hasPendingConnections()); |
|
291 |
QVERIFY(server.nextPendingConnection()); |
|
292 |
} |
|
293 |
||
294 |
//---------------------------------------------------------------------------------- |
|
295 |
void tst_QTcpServer::listenError() |
|
296 |
{ |
|
297 |
QFETCH_GLOBAL(bool, setProxy); |
|
298 |
if (setProxy) { |
|
299 |
QFETCH_GLOBAL(int, proxyType); |
|
300 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
301 |
QSKIP("With socks5 we can not make hard requirements on the address or port", SkipAll); |
|
302 |
} |
|
303 |
} |
|
304 |
QTcpServer server; |
|
305 |
QVERIFY(!server.listen(QHostAddress("1.2.3.4"), 0)); |
|
306 |
QCOMPARE(server.serverError(), QAbstractSocket::SocketAddressNotAvailableError); |
|
307 |
QCOMPARE(server.errorString().toLatin1().constData(), "The address is not available"); |
|
308 |
} |
|
309 |
||
310 |
class ThreadConnector : public QThread |
|
311 |
{ |
|
312 |
public: |
|
313 |
ThreadConnector(const QHostAddress &host, quint16 port) |
|
314 |
: host(host), port(port) |
|
315 |
{ } |
|
316 |
||
317 |
~ThreadConnector() |
|
318 |
{ |
|
319 |
wait(); |
|
320 |
} |
|
321 |
||
322 |
protected: |
|
323 |
void run() |
|
324 |
{ |
|
325 |
sleep(2); |
|
326 |
||
327 |
QTcpSocket socket; |
|
328 |
socket.connectToHost(host, port); |
|
329 |
||
330 |
QEventLoop loop; |
|
331 |
QTimer::singleShot(5000, &loop, SLOT(quit())); |
|
332 |
loop.exec(); |
|
333 |
} |
|
334 |
||
335 |
private: |
|
336 |
QHostAddress host; |
|
337 |
quint16 port; |
|
338 |
}; |
|
339 |
||
340 |
//---------------------------------------------------------------------------------- |
|
341 |
void tst_QTcpServer::waitForConnectionTest() |
|
342 |
{ |
|
343 |
||
344 |
QFETCH_GLOBAL(bool, setProxy); |
|
345 |
if (setProxy) { |
|
346 |
QFETCH_GLOBAL(int, proxyType); |
|
347 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
348 |
QSKIP("Localhost servers don't work well with SOCKS5", SkipAll); |
|
349 |
} |
|
350 |
} |
|
351 |
||
352 |
QTcpSocket findLocalIpSocket; |
|
353 |
findLocalIpSocket.connectToHost(QtNetworkSettings::serverName(), 143); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |
QVERIFY(findLocalIpSocket.waitForConnected(5000)); |
0 | 355 |
|
356 |
QTcpServer server; |
|
357 |
bool timeout = false; |
|
358 |
QVERIFY(server.listen(findLocalIpSocket.localAddress())); |
|
359 |
QVERIFY(!server.waitForNewConnection(1000, &timeout)); |
|
360 |
QCOMPARE(server.serverError(), QAbstractSocket::SocketTimeoutError); |
|
361 |
QVERIFY(timeout); |
|
362 |
||
363 |
ThreadConnector connector(findLocalIpSocket.localAddress(), server.serverPort()); |
|
364 |
connector.start(); |
|
365 |
||
366 |
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
|
367 |
QVERIFY(server.waitForNewConnection(9000, &timeout)); |
|
368 |
#else |
|
369 |
QVERIFY(server.waitForNewConnection(3000, &timeout)); |
|
370 |
#endif |
|
371 |
QVERIFY(!timeout); |
|
372 |
} |
|
373 |
||
374 |
//---------------------------------------------------------------------------------- |
|
375 |
void tst_QTcpServer::setSocketDescriptor() |
|
376 |
{ |
|
377 |
QTcpServer server; |
|
378 |
QVERIFY(!server.setSocketDescriptor(42)); |
|
379 |
QCOMPARE(server.serverError(), QAbstractSocket::UnsupportedSocketOperationError); |
|
380 |
||
381 |
#ifdef Q_OS_WIN |
|
382 |
// ensure winsock is started |
|
383 |
WSADATA wsaData; |
|
384 |
QVERIFY(WSAStartup(MAKEWORD(2,0), &wsaData) == NO_ERROR); |
|
385 |
#endif |
|
386 |
||
387 |
SOCKET sock = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
|
388 |
||
389 |
QVERIFY(sock != INVALID_SOCKET); |
|
390 |
||
391 |
sockaddr_in sin; |
|
392 |
memset(&sin, 0, sizeof(sockaddr_in)); |
|
393 |
sin.sin_family = AF_INET; |
|
394 |
sin.sin_port = 0; |
|
395 |
sin.sin_addr.s_addr = 0x00000000; |
|
396 |
QVERIFY(::bind(sock, (sockaddr*)&sin, sizeof(sockaddr_in)) == 0); |
|
397 |
QVERIFY(::listen(sock, 10) == 0); |
|
398 |
QVERIFY(server.setSocketDescriptor(sock)); |
|
399 |
||
400 |
#ifdef Q_OS_WIN |
|
401 |
WSACleanup(); |
|
402 |
#endif |
|
403 |
} |
|
404 |
||
405 |
//---------------------------------------------------------------------------------- |
|
406 |
void tst_QTcpServer::listenWhileListening() |
|
407 |
{ |
|
408 |
QTcpServer server; |
|
409 |
QVERIFY(server.listen()); |
|
410 |
QTest::ignoreMessage(QtWarningMsg, "QTcpServer::listen() called when already listening"); |
|
411 |
QVERIFY(!server.listen()); |
|
412 |
} |
|
413 |
||
414 |
//---------------------------------------------------------------------------------- |
|
415 |
||
416 |
class SeverWithBlockingSockets : public QTcpServer |
|
417 |
{ |
|
418 |
public: |
|
419 |
SeverWithBlockingSockets() |
|
420 |
: ok(false) { } |
|
421 |
||
422 |
bool ok; |
|
423 |
||
424 |
protected: |
|
425 |
void incomingConnection(int socketDescriptor) |
|
426 |
{ |
|
427 |
// how a user woulddo it (qabstractsocketengine is not public) |
|
428 |
unsigned long arg = 0; |
|
429 |
#if defined(Q_OS_SYMBIAN) |
|
430 |
arg = fcntl(socketDescriptor, F_GETFL, NULL); |
|
431 |
arg &= (~O_NONBLOCK); |
|
432 |
ok = ::fcntl(socketDescriptor, F_SETFL, arg) != -1; |
|
433 |
#elif defined(Q_OS_WIN) |
|
434 |
ok = ::ioctlsocket(socketDescriptor, FIONBIO, &arg) == 0; |
|
435 |
::closesocket(socketDescriptor); |
|
436 |
#else |
|
437 |
ok = ::ioctl(socketDescriptor, FIONBIO, &arg) == 0; |
|
438 |
::close(socketDescriptor); |
|
439 |
#endif |
|
440 |
} |
|
441 |
}; |
|
442 |
||
443 |
void tst_QTcpServer::addressReusable() |
|
444 |
{ |
|
445 |
#if defined(Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
446 |
QSKIP("Symbian: Emulator does not support process launching", SkipAll ); |
|
447 |
#endif |
|
448 |
||
449 |
#if defined(QT_NO_PROCESS) |
|
450 |
QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll); |
|
451 |
#else |
|
452 |
||
453 |
QFETCH_GLOBAL(bool, setProxy); |
|
454 |
if (setProxy) { |
|
455 |
QFETCH_GLOBAL(int, proxyType); |
|
456 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
457 |
QSKIP("With socks5 this test does not make senans at the momment", SkipAll); |
|
458 |
} |
|
459 |
} |
|
460 |
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
|
461 |
QString signalName = QString::fromLatin1("/test_signal.txt"); |
|
462 |
QFile::remove(signalName); |
|
463 |
// The crashingServer process will crash once it gets a connection. |
|
464 |
QProcess process; |
|
465 |
process.start("crashingServer/crashingServer"); |
|
466 |
int waitCount = 5; |
|
467 |
while (waitCount-- && !QFile::exists(signalName)) |
|
468 |
QTest::qWait(1000); |
|
469 |
QVERIFY(QFile::exists(signalName)); |
|
470 |
QFile::remove(signalName); |
|
471 |
#else |
|
472 |
// The crashingServer process will crash once it gets a connection. |
|
473 |
QProcess process; |
|
474 |
process.start("crashingServer/crashingServer"); |
|
475 |
QVERIFY(process.waitForReadyRead(5000)); |
|
476 |
#endif |
|
477 |
||
478 |
QTcpSocket socket; |
|
479 |
socket.connectToHost(QHostAddress::LocalHost, 49199); |
|
480 |
QVERIFY(socket.waitForConnected(5000)); |
|
481 |
||
482 |
QVERIFY(process.waitForFinished(5000)); |
|
483 |
||
484 |
// Give the system some time. |
|
485 |
QTest::qSleep(10); |
|
486 |
||
487 |
QTcpServer server; |
|
488 |
QVERIFY(server.listen(QHostAddress::LocalHost, 49199)); |
|
489 |
#endif |
|
490 |
} |
|
491 |
||
492 |
void tst_QTcpServer::setNewSocketDescriptorBlocking() |
|
493 |
{ |
|
494 |
QFETCH_GLOBAL(bool, setProxy); |
|
495 |
if (setProxy) { |
|
496 |
QFETCH_GLOBAL(int, proxyType); |
|
497 |
if (proxyType == QNetworkProxy::Socks5Proxy) { |
|
498 |
QSKIP("With socks5 we can not make the socket descripter blocking", SkipAll); |
|
499 |
} |
|
500 |
} |
|
501 |
SeverWithBlockingSockets server; |
|
502 |
QVERIFY(server.listen()); |
|
503 |
||
504 |
QTcpSocket socket; |
|
505 |
socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
506 |
QVERIFY(server.waitForNewConnection(5000)); |
|
507 |
QVERIFY(server.ok); |
|
508 |
} |
|
509 |
||
510 |
void tst_QTcpServer::invalidProxy_data() |
|
511 |
{ |
|
512 |
QTest::addColumn<int>("type"); |
|
513 |
QTest::addColumn<QString>("host"); |
|
514 |
QTest::addColumn<int>("port"); |
|
515 |
QTest::addColumn<int>("expectedError"); |
|
516 |
||
517 |
QString fluke = QHostInfo::fromName(QtNetworkSettings::serverName()).addresses().first().toString(); |
|
518 |
QTest::newRow("ftp-proxy") << int(QNetworkProxy::FtpCachingProxy) << fluke << 143 |
|
519 |
<< int(QAbstractSocket::UnsupportedSocketOperationError); |
|
520 |
QTest::newRow("http-proxy") << int(QNetworkProxy::HttpProxy) << fluke << 3128 |
|
521 |
<< int(QAbstractSocket::UnsupportedSocketOperationError); |
|
522 |
||
523 |
QTest::newRow("no-such-host") << int(QNetworkProxy::Socks5Proxy) |
|
524 |
<< "this-host-will-never-exist.troll.no" << 1080 |
|
525 |
<< int(QAbstractSocket::ProxyNotFoundError); |
|
526 |
QTest::newRow("socks5-on-http") << int(QNetworkProxy::Socks5Proxy) << fluke << 3128 |
|
527 |
<< int(QAbstractSocket::SocketTimeoutError); |
|
528 |
} |
|
529 |
||
530 |
void tst_QTcpServer::invalidProxy() |
|
531 |
{ |
|
532 |
QFETCH_GLOBAL(bool, setProxy); |
|
533 |
if (setProxy) |
|
534 |
return; |
|
535 |
||
536 |
QFETCH(int, type); |
|
537 |
QFETCH(QString, host); |
|
538 |
QFETCH(int, port); |
|
539 |
QNetworkProxy::ProxyType proxyType = QNetworkProxy::ProxyType(type); |
|
540 |
QNetworkProxy proxy(proxyType, host, port); |
|
541 |
||
542 |
QTcpServer server; |
|
543 |
server.setProxy(proxy); |
|
544 |
bool listenResult = server.listen(); |
|
545 |
||
546 |
QVERIFY(!listenResult); |
|
547 |
QVERIFY(!server.errorString().isEmpty()); |
|
548 |
||
549 |
// note: the following test is not a hard failure. |
|
550 |
// Sometimes, error codes change for the better |
|
551 |
QTEST(int(server.serverError()), "expectedError"); |
|
552 |
} |
|
553 |
||
554 |
// copied from tst_qnetworkreply.cpp |
|
555 |
class MyProxyFactory: public QNetworkProxyFactory |
|
556 |
{ |
|
557 |
public: |
|
558 |
int callCount; |
|
559 |
QList<QNetworkProxy> toReturn; |
|
560 |
QNetworkProxyQuery lastQuery; |
|
561 |
inline MyProxyFactory() { clear(); } |
|
562 |
||
563 |
inline void clear() |
|
564 |
{ |
|
565 |
callCount = 0; |
|
566 |
toReturn = QList<QNetworkProxy>() << QNetworkProxy::DefaultProxy; |
|
567 |
lastQuery = QNetworkProxyQuery(); |
|
568 |
} |
|
569 |
||
570 |
virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query) |
|
571 |
{ |
|
572 |
lastQuery = query; |
|
573 |
++callCount; |
|
574 |
return toReturn; |
|
575 |
} |
|
576 |
}; |
|
577 |
||
578 |
void tst_QTcpServer::proxyFactory_data() |
|
579 |
{ |
|
580 |
QTest::addColumn<QList<QNetworkProxy> >("proxyList"); |
|
581 |
QTest::addColumn<QNetworkProxy>("proxyUsed"); |
|
582 |
QTest::addColumn<bool>("fails"); |
|
583 |
QTest::addColumn<int>("expectedError"); |
|
584 |
||
585 |
QList<QNetworkProxy> proxyList; |
|
586 |
||
587 |
// tests that do get to listen |
|
588 |
||
589 |
proxyList << QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080); |
|
590 |
QTest::newRow("socks5") |
|
591 |
<< proxyList << proxyList.at(0) |
|
592 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
593 |
||
594 |
proxyList.clear(); |
|
595 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3128) |
|
596 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080); |
|
597 |
QTest::newRow("cachinghttp+socks5") |
|
598 |
<< proxyList << proxyList.at(1) |
|
599 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
600 |
||
601 |
proxyList.clear(); |
|
602 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
603 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3128) |
|
604 |
<< QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080); |
|
605 |
QTest::newRow("ftp+cachinghttp+socks5") |
|
606 |
<< proxyList << proxyList.at(2) |
|
607 |
<< false << int(QAbstractSocket::UnknownSocketError); |
|
608 |
||
609 |
// tests that fail to listen |
|
610 |
proxyList.clear(); |
|
611 |
proxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QtNetworkSettings::serverName(), 3128); |
|
612 |
QTest::newRow("http") |
|
613 |
<< proxyList << proxyList.at(0) |
|
614 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
615 |
||
616 |
proxyList.clear(); |
|
617 |
proxyList << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3128); |
|
618 |
QTest::newRow("cachinghttp") |
|
619 |
<< proxyList << QNetworkProxy() |
|
620 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
621 |
||
622 |
proxyList.clear(); |
|
623 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121); |
|
624 |
QTest::newRow("ftp") |
|
625 |
<< proxyList << QNetworkProxy() |
|
626 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
627 |
||
628 |
proxyList.clear(); |
|
629 |
proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::serverName(), 2121) |
|
630 |
<< QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::serverName(), 3128); |
|
631 |
QTest::newRow("ftp+cachinghttp") |
|
632 |
<< proxyList << QNetworkProxy() |
|
633 |
<< true << int(QAbstractSocket::UnsupportedSocketOperationError); |
|
634 |
} |
|
635 |
||
636 |
void tst_QTcpServer::proxyFactory() |
|
637 |
{ |
|
638 |
QFETCH_GLOBAL(bool, setProxy); |
|
639 |
if (setProxy) |
|
640 |
return; |
|
641 |
||
642 |
QFETCH(QList<QNetworkProxy>, proxyList); |
|
643 |
QFETCH(QNetworkProxy, proxyUsed); |
|
644 |
QFETCH(bool, fails); |
|
645 |
||
646 |
MyProxyFactory *factory = new MyProxyFactory; |
|
647 |
factory->toReturn = proxyList; |
|
648 |
QNetworkProxyFactory::setApplicationProxyFactory(factory); |
|
649 |
||
650 |
QTcpServer server; |
|
651 |
bool listenResult = server.listen(); |
|
652 |
||
653 |
// Verify that the factory was called properly |
|
654 |
QCOMPARE(factory->callCount, 1); |
|
655 |
QCOMPARE(factory->lastQuery, QNetworkProxyQuery(0, QString(), QNetworkProxyQuery::TcpServer)); |
|
656 |
||
657 |
QCOMPARE(listenResult, !fails); |
|
658 |
QCOMPARE(server.errorString().isEmpty(), !fails); |
|
659 |
||
660 |
// note: the following test is not a hard failure. |
|
661 |
// Sometimes, error codes change for the better |
|
662 |
QTEST(int(server.serverError()), "expectedError"); |
|
663 |
} |
|
664 |
||
665 |
QTEST_MAIN(tst_QTcpServer) |
|
666 |
#include "tst_qtcpserver.moc" |