tests/auto/qtcpserver/tst_qtcpserver.cpp
branchRCL_3
changeset 4 3b1da2848fc7
parent 3 41300fa6a67c
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
    64 #include <qprocess.h>
    64 #include <qprocess.h>
    65 #include <qstringlist.h>
    65 #include <qstringlist.h>
    66 #include <qplatformdefs.h>
    66 #include <qplatformdefs.h>
    67 #include <qhostinfo.h>
    67 #include <qhostinfo.h>
    68 
    68 
    69 #ifdef TEST_QNETWORK_PROXY
    69 #include <QNetworkProxy>
    70 # include <QNetworkProxy>
       
    71 Q_DECLARE_METATYPE(QNetworkProxy)
    70 Q_DECLARE_METATYPE(QNetworkProxy)
    72 Q_DECLARE_METATYPE(QList<QNetworkProxy>)
    71 Q_DECLARE_METATYPE(QList<QNetworkProxy>)
    73 #endif
       
    74 
    72 
    75 #include "../network-settings.h"
    73 #include "../network-settings.h"
    76 
    74 
    77 //TESTED_CLASS=
    75 //TESTED_CLASS=
    78 //TESTED_FILES=
    76 //TESTED_FILES=
    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());
   421 //----------------------------------------------------------------------------------
   255 //----------------------------------------------------------------------------------
   422 void tst_QTcpServer::maxPendingConnections()
   256 void tst_QTcpServer::maxPendingConnections()
   423 {
   257 {
   424     QFETCH_GLOBAL(bool, setProxy);
   258     QFETCH_GLOBAL(bool, setProxy);
   425     if (setProxy) {
   259     if (setProxy) {
   426 #ifdef TEST_QNETWORK_PROXY
       
   427         QFETCH_GLOBAL(int, proxyType);
   260         QFETCH_GLOBAL(int, proxyType);
   428         if (proxyType == QNetworkProxy::Socks5Proxy) {
   261         if (proxyType == QNetworkProxy::Socks5Proxy) {
   429             QSKIP("With socks5 only 1 connection is allowed ever", SkipAll);
   262             QSKIP("With socks5 only 1 connection is allowed ever", SkipAll);
   430         }
   263         }
   431 #endif
       
   432     }
   264     }
   433     //### sees to fail sometimes ... a timing issue with the test on windows
   265     //### sees to fail sometimes ... a timing issue with the test on windows
   434     QTcpServer server;
   266     QTcpServer server;
   435     server.setMaxPendingConnections(2);
   267     server.setMaxPendingConnections(2);
   436 
   268 
   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");
   511 void tst_QTcpServer::waitForConnectionTest()
   341 void tst_QTcpServer::waitForConnectionTest()
   512 {
   342 {
   513 
   343 
   514     QFETCH_GLOBAL(bool, setProxy);
   344     QFETCH_GLOBAL(bool, setProxy);
   515     if (setProxy) {
   345     if (setProxy) {
   516 #ifdef TEST_QNETWORK_PROXY
       
   517         QFETCH_GLOBAL(int, proxyType);
   346         QFETCH_GLOBAL(int, proxyType);
   518         if (proxyType == QNetworkProxy::Socks5Proxy) {
   347         if (proxyType == QNetworkProxy::Socks5Proxy) {
   519             QSKIP("Localhost servers don't work well with SOCKS5", SkipAll);
   348             QSKIP("Localhost servers don't work well with SOCKS5", SkipAll);
   520         }
   349         }
   521 #endif
       
   522     }
   350     }
   523 
   351 
   524     QTcpSocket findLocalIpSocket;
   352     QTcpSocket findLocalIpSocket;
   525     findLocalIpSocket.connectToHost(QtNetworkSettings::serverName(), 143);
   353     findLocalIpSocket.connectToHost(QtNetworkSettings::serverName(), 143);
   526     QVERIFY(findLocalIpSocket.waitForConnected(5000));
   354     QVERIFY(findLocalIpSocket.waitForConnected(5000));
   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");
   836 
   659 
   837     // note: the following test is not a hard failure.
   660     // note: the following test is not a hard failure.
   838     // Sometimes, error codes change for the better
   661     // Sometimes, error codes change for the better
   839     QTEST(int(server.serverError()), "expectedError");
   662     QTEST(int(server.serverError()), "expectedError");
   840 }
   663 }
   841 #endif
       
   842 
   664 
   843 QTEST_MAIN(tst_QTcpServer)
   665 QTEST_MAIN(tst_QTcpServer)
   844 #include "tst_qtcpserver.moc"
   666 #include "tst_qtcpserver.moc"