93 private slots: |
91 private slots: |
94 void getSetCheck(); |
92 void getSetCheck(); |
95 void constructing(); |
93 void constructing(); |
96 void clientServerLoop(); |
94 void clientServerLoop(); |
97 void ipv6Server(); |
95 void ipv6Server(); |
98 void ipv4LoopbackPerformanceTest(); |
|
99 void ipv6LoopbackPerformanceTest(); |
|
100 void ipv4PerformanceTest(); |
|
101 void crashTests(); |
96 void crashTests(); |
102 void maxPendingConnections(); |
97 void maxPendingConnections(); |
103 void listenError(); |
98 void listenError(); |
104 void waitForConnectionTest(); |
99 void waitForConnectionTest(); |
105 void setSocketDescriptor(); |
100 void setSocketDescriptor(); |
106 void listenWhileListening(); |
101 void listenWhileListening(); |
107 void addressReusable(); |
102 void addressReusable(); |
108 void setNewSocketDescriptorBlocking(); |
103 void setNewSocketDescriptorBlocking(); |
109 #ifdef TEST_QNETWORK_PROXY |
|
110 void invalidProxy_data(); |
104 void invalidProxy_data(); |
111 void invalidProxy(); |
105 void invalidProxy(); |
112 void proxyFactory_data(); |
106 void proxyFactory_data(); |
113 void proxyFactory(); |
107 void proxyFactory(); |
114 #endif |
|
115 }; |
108 }; |
116 |
109 |
117 // Testing get/set functions |
110 // Testing get/set functions |
118 void tst_QTcpServer::getSetCheck() |
111 void tst_QTcpServer::getSetCheck() |
119 { |
112 { |
141 { |
134 { |
142 QTest::addColumn<bool>("setProxy"); |
135 QTest::addColumn<bool>("setProxy"); |
143 QTest::addColumn<int>("proxyType"); |
136 QTest::addColumn<int>("proxyType"); |
144 |
137 |
145 QTest::newRow("WithoutProxy") << false << 0; |
138 QTest::newRow("WithoutProxy") << false << 0; |
146 #ifdef TEST_QNETWORK_PROXY |
|
147 QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy); |
139 QTest::newRow("WithSocks5Proxy") << true << int(QNetworkProxy::Socks5Proxy); |
148 #endif |
|
149 } |
140 } |
150 |
141 |
151 void tst_QTcpServer::init() |
142 void tst_QTcpServer::init() |
152 { |
143 { |
153 QFETCH_GLOBAL(bool, setProxy); |
144 QFETCH_GLOBAL(bool, setProxy); |
154 if (setProxy) { |
145 if (setProxy) { |
155 #ifdef TEST_QNETWORK_PROXY |
|
156 QFETCH_GLOBAL(int, proxyType); |
146 QFETCH_GLOBAL(int, proxyType); |
157 if (proxyType == QNetworkProxy::Socks5Proxy) { |
147 if (proxyType == QNetworkProxy::Socks5Proxy) { |
158 QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080)); |
148 QNetworkProxy::setApplicationProxy(QNetworkProxy(QNetworkProxy::Socks5Proxy, QtNetworkSettings::serverName(), 1080)); |
159 } |
149 } |
160 #endif |
|
161 } |
150 } |
162 } |
151 } |
163 |
152 |
164 void tst_QTcpServer::cleanup() |
153 void tst_QTcpServer::cleanup() |
165 { |
154 { |
166 #ifdef TEST_QNETWORK_PROXY |
|
167 QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
155 QNetworkProxy::setApplicationProxy(QNetworkProxy::DefaultProxy); |
168 #endif |
|
169 } |
156 } |
170 |
157 |
171 //---------------------------------------------------------------------------------- |
158 //---------------------------------------------------------------------------------- |
172 |
159 |
173 void tst_QTcpServer::constructing() |
160 void tst_QTcpServer::constructing() |
256 QTcpSocket *serverSocket = 0; |
243 QTcpSocket *serverSocket = 0; |
257 QVERIFY((serverSocket = server.nextPendingConnection())); |
244 QVERIFY((serverSocket = server.nextPendingConnection())); |
258 } |
245 } |
259 |
246 |
260 //---------------------------------------------------------------------------------- |
247 //---------------------------------------------------------------------------------- |
261 void tst_QTcpServer::ipv4LoopbackPerformanceTest() |
|
262 { |
|
263 QFETCH_GLOBAL(bool, setProxy); |
|
264 if (setProxy) |
|
265 return; |
|
266 |
|
267 QTcpServer server; |
|
268 QVERIFY(server.listen(QHostAddress::LocalHost)); |
|
269 |
|
270 QVERIFY(server.isListening()); |
|
271 |
|
272 QTcpSocket clientA; |
|
273 clientA.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
|
274 QVERIFY(clientA.waitForConnected(5000)); |
|
275 QVERIFY(clientA.state() == QAbstractSocket::ConnectedState); |
|
276 |
|
277 QVERIFY(server.waitForNewConnection()); |
|
278 QTcpSocket *clientB = server.nextPendingConnection(); |
|
279 QVERIFY(clientB); |
|
280 |
|
281 QByteArray buffer(16384, '@'); |
|
282 QTime stopWatch; |
|
283 stopWatch.start(); |
|
284 qlonglong totalWritten = 0; |
|
285 while (stopWatch.elapsed() < 5000) { |
|
286 QVERIFY(clientA.write(buffer.data(), buffer.size()) > 0); |
|
287 clientA.flush(); |
|
288 totalWritten += buffer.size(); |
|
289 while (clientB->waitForReadyRead(100)) { |
|
290 if (clientB->bytesAvailable() == 16384) |
|
291 break; |
|
292 } |
|
293 clientB->read(buffer.data(), buffer.size()); |
|
294 clientB->write(buffer.data(), buffer.size()); |
|
295 clientB->flush(); |
|
296 totalWritten += buffer.size(); |
|
297 while (clientA.waitForReadyRead(100)) { |
|
298 if (clientA.bytesAvailable() == 16384) |
|
299 break; |
|
300 } |
|
301 clientA.read(buffer.data(), buffer.size()); |
|
302 } |
|
303 |
|
304 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
305 server.serverAddress().toString().toLatin1().constData(), |
|
306 totalWritten / (1024.0 * 1024.0), |
|
307 stopWatch.elapsed() / 1000.0, |
|
308 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
309 |
|
310 delete clientB; |
|
311 } |
|
312 |
|
313 //---------------------------------------------------------------------------------- |
|
314 void tst_QTcpServer::ipv6LoopbackPerformanceTest() |
|
315 { |
|
316 #if defined(Q_OS_SYMBIAN) |
|
317 QSKIP("Symbian: IPv6 is not yet supported", SkipAll); |
|
318 #endif |
|
319 QTcpServer server; |
|
320 if (!server.listen(QHostAddress::LocalHostIPv6, 0)) { |
|
321 QVERIFY(server.serverError() == QAbstractSocket::UnsupportedSocketOperationError); |
|
322 } else { |
|
323 QTcpSocket clientA; |
|
324 clientA.connectToHost(server.serverAddress(), server.serverPort()); |
|
325 QVERIFY(clientA.waitForConnected(5000)); |
|
326 |
|
327 QVERIFY(server.waitForNewConnection(5000)); |
|
328 QTcpSocket *clientB = server.nextPendingConnection(); |
|
329 QVERIFY(clientB); |
|
330 |
|
331 QByteArray buffer(16384, '@'); |
|
332 QTime stopWatch; |
|
333 stopWatch.start(); |
|
334 qlonglong totalWritten = 0; |
|
335 while (stopWatch.elapsed() < 5000) { |
|
336 clientA.write(buffer.data(), buffer.size()); |
|
337 clientA.flush(); |
|
338 totalWritten += buffer.size(); |
|
339 while (clientB->waitForReadyRead(100)) { |
|
340 if (clientB->bytesAvailable() == 16384) |
|
341 break; |
|
342 } |
|
343 clientB->read(buffer.data(), buffer.size()); |
|
344 clientB->write(buffer.data(), buffer.size()); |
|
345 clientB->flush(); |
|
346 totalWritten += buffer.size(); |
|
347 while (clientA.waitForReadyRead(100)) { |
|
348 if (clientA.bytesAvailable() == 16384) |
|
349 break; |
|
350 } |
|
351 clientA.read(buffer.data(), buffer.size()); |
|
352 } |
|
353 |
|
354 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
355 server.serverAddress().toString().toLatin1().constData(), |
|
356 totalWritten / (1024.0 * 1024.0), |
|
357 stopWatch.elapsed() / 1000.0, |
|
358 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
359 delete clientB; |
|
360 } |
|
361 } |
|
362 |
|
363 //---------------------------------------------------------------------------------- |
|
364 void tst_QTcpServer::ipv4PerformanceTest() |
|
365 { |
|
366 QTcpSocket probeSocket; |
|
367 probeSocket.connectToHost(QtNetworkSettings::serverName(), 143); |
|
368 QVERIFY(probeSocket.waitForConnected(5000)); |
|
369 |
|
370 QTcpServer server; |
|
371 QVERIFY(server.listen(probeSocket.localAddress(), 0)); |
|
372 |
|
373 QTcpSocket clientA; |
|
374 clientA.connectToHost(server.serverAddress(), server.serverPort()); |
|
375 QVERIFY(clientA.waitForConnected(5000)); |
|
376 |
|
377 QVERIFY(server.waitForNewConnection(5000)); |
|
378 QTcpSocket *clientB = server.nextPendingConnection(); |
|
379 QVERIFY(clientB); |
|
380 |
|
381 QByteArray buffer(16384, '@'); |
|
382 QTime stopWatch; |
|
383 stopWatch.start(); |
|
384 qlonglong totalWritten = 0; |
|
385 while (stopWatch.elapsed() < 5000) { |
|
386 qlonglong writtenA = clientA.write(buffer.data(), buffer.size()); |
|
387 clientA.flush(); |
|
388 totalWritten += buffer.size(); |
|
389 while (clientB->waitForReadyRead(100)) { |
|
390 if (clientB->bytesAvailable() == writtenA) |
|
391 break; |
|
392 } |
|
393 clientB->read(buffer.data(), buffer.size()); |
|
394 qlonglong writtenB = clientB->write(buffer.data(), buffer.size()); |
|
395 clientB->flush(); |
|
396 totalWritten += buffer.size(); |
|
397 while (clientA.waitForReadyRead(100)) { |
|
398 if (clientA.bytesAvailable() == writtenB) |
|
399 break; |
|
400 } |
|
401 clientA.read(buffer.data(), buffer.size()); |
|
402 } |
|
403 |
|
404 qDebug("\t\t%s: %.1fMB/%.1fs: %.1fMB/s", |
|
405 probeSocket.localAddress().toString().toLatin1().constData(), |
|
406 totalWritten / (1024.0 * 1024.0), |
|
407 stopWatch.elapsed() / 1000.0, |
|
408 (totalWritten / (stopWatch.elapsed() / 1000.0)) / (1024 * 1024)); |
|
409 |
|
410 delete clientB; |
|
411 } |
|
412 |
|
413 //---------------------------------------------------------------------------------- |
|
414 void tst_QTcpServer::crashTests() |
248 void tst_QTcpServer::crashTests() |
415 { |
249 { |
416 QTcpServer server; |
250 QTcpServer server; |
417 server.close(); |
251 server.close(); |
418 QVERIFY(server.listen()); |
252 QVERIFY(server.listen()); |
462 //---------------------------------------------------------------------------------- |
294 //---------------------------------------------------------------------------------- |
463 void tst_QTcpServer::listenError() |
295 void tst_QTcpServer::listenError() |
464 { |
296 { |
465 QFETCH_GLOBAL(bool, setProxy); |
297 QFETCH_GLOBAL(bool, setProxy); |
466 if (setProxy) { |
298 if (setProxy) { |
467 #ifdef TEST_QNETWORK_PROXY |
|
468 QFETCH_GLOBAL(int, proxyType); |
299 QFETCH_GLOBAL(int, proxyType); |
469 if (proxyType == QNetworkProxy::Socks5Proxy) { |
300 if (proxyType == QNetworkProxy::Socks5Proxy) { |
470 QSKIP("With socks5 we can not make hard requirements on the address or port", SkipAll); |
301 QSKIP("With socks5 we can not make hard requirements on the address or port", SkipAll); |
471 } |
302 } |
472 #endif |
|
473 } |
303 } |
474 QTcpServer server; |
304 QTcpServer server; |
475 QVERIFY(!server.listen(QHostAddress("1.2.3.4"), 0)); |
305 QVERIFY(!server.listen(QHostAddress("1.2.3.4"), 0)); |
476 QCOMPARE(server.serverError(), QAbstractSocket::SocketAddressNotAvailableError); |
306 QCOMPARE(server.serverError(), QAbstractSocket::SocketAddressNotAvailableError); |
477 QCOMPARE(server.errorString().toLatin1().constData(), "The address is not available"); |
307 QCOMPARE(server.errorString().toLatin1().constData(), "The address is not available"); |
622 QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll); |
450 QSKIP("Qt was compiled with QT_NO_PROCESS", SkipAll); |
623 #else |
451 #else |
624 |
452 |
625 QFETCH_GLOBAL(bool, setProxy); |
453 QFETCH_GLOBAL(bool, setProxy); |
626 if (setProxy) { |
454 if (setProxy) { |
627 #ifdef TEST_QNETWORK_PROXY |
|
628 QFETCH_GLOBAL(int, proxyType); |
455 QFETCH_GLOBAL(int, proxyType); |
629 if (proxyType == QNetworkProxy::Socks5Proxy) { |
456 if (proxyType == QNetworkProxy::Socks5Proxy) { |
630 QSKIP("With socks5 this test does not make senans at the momment", SkipAll); |
457 QSKIP("With socks5 this test does not make senans at the momment", SkipAll); |
631 } |
458 } |
632 #endif |
|
633 } |
459 } |
634 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
460 #if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) |
635 QString signalName = QString::fromLatin1("/test_signal.txt"); |
461 QString signalName = QString::fromLatin1("/test_signal.txt"); |
636 QFile::remove(signalName); |
462 QFile::remove(signalName); |
637 // The crashingServer process will crash once it gets a connection. |
463 // The crashingServer process will crash once it gets a connection. |
665 |
491 |
666 void tst_QTcpServer::setNewSocketDescriptorBlocking() |
492 void tst_QTcpServer::setNewSocketDescriptorBlocking() |
667 { |
493 { |
668 QFETCH_GLOBAL(bool, setProxy); |
494 QFETCH_GLOBAL(bool, setProxy); |
669 if (setProxy) { |
495 if (setProxy) { |
670 #ifdef TEST_QNETWORK_PROXY |
|
671 QFETCH_GLOBAL(int, proxyType); |
496 QFETCH_GLOBAL(int, proxyType); |
672 if (proxyType == QNetworkProxy::Socks5Proxy) { |
497 if (proxyType == QNetworkProxy::Socks5Proxy) { |
673 QSKIP("With socks5 we can not make the socket descripter blocking", SkipAll); |
498 QSKIP("With socks5 we can not make the socket descripter blocking", SkipAll); |
674 } |
499 } |
675 #endif |
|
676 } |
500 } |
677 SeverWithBlockingSockets server; |
501 SeverWithBlockingSockets server; |
678 QVERIFY(server.listen()); |
502 QVERIFY(server.listen()); |
679 |
503 |
680 QTcpSocket socket; |
504 QTcpSocket socket; |
681 socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
505 socket.connectToHost(QHostAddress::LocalHost, server.serverPort()); |
682 QVERIFY(server.waitForNewConnection(5000)); |
506 QVERIFY(server.waitForNewConnection(5000)); |
683 QVERIFY(server.ok); |
507 QVERIFY(server.ok); |
684 } |
508 } |
685 |
509 |
686 #ifdef TEST_QNETWORK_PROXY |
|
687 void tst_QTcpServer::invalidProxy_data() |
510 void tst_QTcpServer::invalidProxy_data() |
688 { |
511 { |
689 QTest::addColumn<int>("type"); |
512 QTest::addColumn<int>("type"); |
690 QTest::addColumn<QString>("host"); |
513 QTest::addColumn<QString>("host"); |
691 QTest::addColumn<int>("port"); |
514 QTest::addColumn<int>("port"); |