tests/auto/qtcpsocket/tst_qtcpsocket.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   190     void suddenRemoteDisconnect_data();
   190     void suddenRemoteDisconnect_data();
   191     void suddenRemoteDisconnect();
   191     void suddenRemoteDisconnect();
   192     void connectToMultiIP();
   192     void connectToMultiIP();
   193     void moveToThread0();
   193     void moveToThread0();
   194     void increaseReadBufferSize();
   194     void increaseReadBufferSize();
       
   195     void taskQtBug5799ConnectionErrorWaitForConnected();
       
   196     void taskQtBug5799ConnectionErrorEventLoop();
   195 #ifdef TEST_QNETWORK_PROXY
   197 #ifdef TEST_QNETWORK_PROXY
   196     void invalidProxy_data();
   198     void invalidProxy_data();
   197     void invalidProxy();
   199     void invalidProxy();
   198     void proxyFactory_data();
   200     void proxyFactory_data();
   199     void proxyFactory();
   201     void proxyFactory();
   367     QVERIFY(!socket->isOpen());
   369     QVERIFY(!socket->isOpen());
   368     QVERIFY(!socket->isValid());
   370     QVERIFY(!socket->isValid());
   369     QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket);
   371     QCOMPARE(socket->socketType(), QTcpSocket::TcpSocket);
   370 
   372 
   371     char c;
   373     char c;
   372     QTest::ignoreMessage(QtWarningMsg, "QIODevice::getChar: Closed device");
       
   373     QCOMPARE(socket->getChar(&c), false);
   374     QCOMPARE(socket->getChar(&c), false);
   374     QCOMPARE((int) socket->bytesAvailable(), 0);
   375     QCOMPARE((int) socket->bytesAvailable(), 0);
   375     QCOMPARE(socket->canReadLine(), false);
   376     QCOMPARE(socket->canReadLine(), false);
   376     QCOMPARE(socket->readLine(), QByteArray());
   377     QCOMPARE(socket->readLine(), QByteArray());
   377     QCOMPARE(socket->socketDescriptor(), -1);
   378     QCOMPARE(socket->socketDescriptor(), -1);
   773 //    QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready";
   774 //    QByteArray greeting = "* OK fluke Cyrus IMAP4 v2.2.12 server ready";
   774     QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE";
   775     QByteArray greeting = "* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE";
   775 
   776 
   776     for (int i = 0; i < 10; i += 2) {
   777     for (int i = 0; i < 10; i += 2) {
   777         while (socket->bytesAvailable() < 2)
   778         while (socket->bytesAvailable() < 2)
   778             QVERIFY(socket->waitForReadyRead(5000));
   779             QVERIFY(socket->waitForReadyRead(10000));
   779         int bA = socket->bytesAvailable();
   780         int bA = socket->bytesAvailable();
   780         QVERIFY(socket->read(buf, 2) == 2);
   781         QVERIFY(socket->read(buf, 2) == 2);
   781         buf[2] = '\0';
   782         buf[2] = '\0';
   782         QCOMPARE((char *)buf, greeting.mid(i, 2).data());
   783         QCOMPARE((char *)buf, greeting.mid(i, 2).data());
   783         QCOMPARE((int)socket->bytesAvailable(), bA - 2);
   784         QCOMPARE((int)socket->bytesAvailable(), bA - 2);
   826         QVERIFY(socket->isSequential());
   827         QVERIFY(socket->isSequential());
   827         QVERIFY(!socket->isOpen());
   828         QVERIFY(!socket->isOpen());
   828         QVERIFY(socket->socketType() == QTcpSocket::TcpSocket);
   829         QVERIFY(socket->socketType() == QTcpSocket::TcpSocket);
   829 
   830 
   830         char c;
   831         char c;
   831         QTest::ignoreMessage(QtWarningMsg, "QIODevice::getChar: Closed device");
       
   832         QCOMPARE(socket->getChar(&c), false);
   832         QCOMPARE(socket->getChar(&c), false);
   833         QCOMPARE((int) socket->bytesAvailable(), 0);
   833         QCOMPARE((int) socket->bytesAvailable(), 0);
   834         QCOMPARE(socket->canReadLine(), false);
   834         QCOMPARE(socket->canReadLine(), false);
   835         QCOMPARE(socket->readLine(), QByteArray());
   835         QCOMPARE(socket->readLine(), QByteArray());
   836         QCOMPARE(socket->socketDescriptor(), -1);
   836         QCOMPARE(socket->socketDescriptor(), -1);
  1955 
  1955 
  1956     // now the QIODevice is closed, which means getChar complains
  1956     // now the QIODevice is closed, which means getChar complains
  1957     QCOMPARE(socket->write("BLUBBER"), qint64(-1));
  1957     QCOMPARE(socket->write("BLUBBER"), qint64(-1));
  1958     QCOMPARE(socket->read(c, 16), qint64(-1));
  1958     QCOMPARE(socket->read(c, 16), qint64(-1));
  1959     QCOMPARE(socket->readLine(c, 16), qint64(-1));
  1959     QCOMPARE(socket->readLine(c, 16), qint64(-1));
  1960     QTest::ignoreMessage(QtWarningMsg, "QIODevice::getChar: Closed device");
       
  1961     QVERIFY(!socket->getChar(c));
  1960     QVERIFY(!socket->getChar(c));
  1962     QVERIFY(!socket->putChar('a'));
  1961     QVERIFY(!socket->putChar('a'));
  1963 
  1962 
  1964     delete socket;
  1963     delete socket;
  1965 }
  1964 }
  2094     QVERIFY(stopWatch.elapsed() < 70000);
  2093     QVERIFY(stopWatch.elapsed() < 70000);
  2095     socket->abort();
  2094     socket->abort();
  2096 
  2095 
  2097     stopWatch.restart();
  2096     stopWatch.restart();
  2098     socket->connectToHost("multi.dev.troll.no", 81);
  2097     socket->connectToHost("multi.dev.troll.no", 81);
  2099     QVERIFY(!socket->waitForConnected(1000));
  2098     QVERIFY(!socket->waitForConnected(2000));
  2100     QVERIFY(stopWatch.elapsed() < 2000);
  2099     QVERIFY(stopWatch.elapsed() < 2000);
  2101     QCOMPARE(socket->error(), QAbstractSocket::SocketTimeoutError);
  2100     QCOMPARE(socket->error(), QAbstractSocket::SocketTimeoutError);
  2102 
  2101 
  2103     delete socket;
  2102     delete socket;
  2104 #endif
  2103 #endif
  2114     {
  2113     {
  2115         // Case 1: Moved after connecting, before waiting for connection.
  2114         // Case 1: Moved after connecting, before waiting for connection.
  2116         QTcpSocket *socket = newSocket();;
  2115         QTcpSocket *socket = newSocket();;
  2117         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2116         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2118         socket->moveToThread(0);
  2117         socket->moveToThread(0);
  2119         QVERIFY(socket->waitForConnected(1000));
  2118         QVERIFY(socket->waitForConnected(5000));
  2120         socket->write("XXX LOGOUT\r\n");
  2119         socket->write("XXX LOGOUT\r\n");
  2121         QVERIFY(socket->waitForBytesWritten(5000));
  2120         QVERIFY(socket->waitForBytesWritten(5000));
  2122         QVERIFY(socket->waitForDisconnected());
  2121         QVERIFY(socket->waitForDisconnected());
  2123         delete socket;
  2122         delete socket;
  2124     }
  2123     }
  2125     {
  2124     {
  2126         // Case 2: Moved before connecting
  2125         // Case 2: Moved before connecting
  2127         QTcpSocket *socket = newSocket();
  2126         QTcpSocket *socket = newSocket();
  2128         socket->moveToThread(0);
  2127         socket->moveToThread(0);
  2129         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2128         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2130         QVERIFY(socket->waitForConnected(1000));
  2129         QVERIFY(socket->waitForConnected(5000));
  2131         socket->write("XXX LOGOUT\r\n");
  2130         socket->write("XXX LOGOUT\r\n");
  2132         QVERIFY(socket->waitForBytesWritten(5000));
  2131         QVERIFY(socket->waitForBytesWritten(5000));
  2133         QVERIFY(socket->waitForDisconnected());
  2132         QVERIFY(socket->waitForDisconnected());
  2134         delete socket;
  2133         delete socket;
  2135     }
  2134     }
  2136     {
  2135     {
  2137         // Case 3: Moved after writing, while waiting for bytes to be written.
  2136         // Case 3: Moved after writing, while waiting for bytes to be written.
  2138         QTcpSocket *socket = newSocket();
  2137         QTcpSocket *socket = newSocket();
  2139         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2138         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2140         QVERIFY(socket->waitForConnected(1000));
  2139         QVERIFY(socket->waitForConnected(5000));
  2141         socket->write("XXX LOGOUT\r\n");
  2140         socket->write("XXX LOGOUT\r\n");
  2142         socket->moveToThread(0);
  2141         socket->moveToThread(0);
  2143         QVERIFY(socket->waitForBytesWritten(5000));
  2142         QVERIFY(socket->waitForBytesWritten(5000));
  2144         QVERIFY(socket->waitForDisconnected());
  2143         QVERIFY(socket->waitForDisconnected());
  2145         delete socket;
  2144         delete socket;
  2146     }
  2145     }
  2147     {
  2146     {
  2148         // Case 4: Moved after writing, while waiting for response.
  2147         // Case 4: Moved after writing, while waiting for response.
  2149         QTcpSocket *socket = newSocket();
  2148         QTcpSocket *socket = newSocket();
  2150         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2149         socket->connectToHost(QtNetworkSettings::serverName(), 143);
  2151         QVERIFY(socket->waitForConnected(1000));
  2150         QVERIFY(socket->waitForConnected(5000));
  2152         socket->write("XXX LOGOUT\r\n");
  2151         socket->write("XXX LOGOUT\r\n");
  2153         QVERIFY(socket->waitForBytesWritten(5000));
  2152         QVERIFY(socket->waitForBytesWritten(5000));
  2154         socket->moveToThread(0);
  2153         socket->moveToThread(0);
  2155         QVERIFY(socket->waitForDisconnected());
  2154         QVERIFY(socket->waitForDisconnected());
  2156         delete socket;
  2155         delete socket;
  2211     QCOMPARE(active->bytesAvailable(), qint64(data.size()));
  2210     QCOMPARE(active->bytesAvailable(), qint64(data.size()));
  2212     QCOMPARE(active->readAll(), data);
  2211     QCOMPARE(active->readAll(), data);
  2213 
  2212 
  2214     delete active;
  2213     delete active;
  2215 }
  2214 }
       
  2215 
       
  2216 void tst_QTcpSocket::taskQtBug5799ConnectionErrorWaitForConnected()
       
  2217 {
       
  2218     QFETCH_GLOBAL(bool, setProxy);
       
  2219     if (setProxy)
       
  2220         return;
       
  2221 
       
  2222     // check that we get a proper error connecting to port 12346
       
  2223     // use waitForConnected, e.g. this should use a synchronous select() on the OS level
       
  2224 
       
  2225     QTcpSocket socket;
       
  2226     socket.connectToHost(QtNetworkSettings::serverName(), 12346);
       
  2227     QTime timer;
       
  2228     timer.start();
       
  2229     socket.waitForConnected(10000);
       
  2230     QVERIFY2(timer.elapsed() < 9900, "Connection to closed port timed out instead of refusing, something is wrong");
       
  2231     QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!");
       
  2232     QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError,
       
  2233              QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit());
       
  2234 }
       
  2235 
       
  2236 void tst_QTcpSocket::taskQtBug5799ConnectionErrorEventLoop()
       
  2237 {
       
  2238     QFETCH_GLOBAL(bool, setProxy);
       
  2239     if (setProxy)
       
  2240         return;
       
  2241 
       
  2242     // check that we get a proper error connecting to port 12346
       
  2243     // This testcase uses an event loop
       
  2244     QTcpSocket socket;
       
  2245     connect(&socket, SIGNAL(error(QAbstractSocket::SocketError)), &QTestEventLoop::instance(), SLOT(exitLoop()));
       
  2246     socket.connectToHost(QtNetworkSettings::serverName(), 12346);
       
  2247 
       
  2248     QTestEventLoop::instance().enterLoop(10);
       
  2249     QVERIFY2(!QTestEventLoop::instance().timeout(), "Connection to closed port timed out instead of refusing, something is wrong");
       
  2250     QVERIFY2(socket.state() == QAbstractSocket::UnconnectedState, "Socket connected unexpectedly!");
       
  2251     QVERIFY2(socket.error() == QAbstractSocket::ConnectionRefusedError,
       
  2252              QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit());
       
  2253 }
       
  2254 
       
  2255 
  2216 
  2256 
  2217 #ifdef TEST_QNETWORK_PROXY
  2257 #ifdef TEST_QNETWORK_PROXY
  2218 void tst_QTcpSocket::invalidProxy_data()
  2258 void tst_QTcpSocket::invalidProxy_data()
  2219 {
  2259 {
  2220     QTest::addColumn<int>("type");
  2260     QTest::addColumn<int>("type");
  2261 
  2301 
  2262     if (failsAtConnect) {
  2302     if (failsAtConnect) {
  2263         QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
  2303         QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
  2264     } else {
  2304     } else {
  2265         QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
  2305         QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
  2266         QVERIFY(!socket->waitForConnected(1000));
  2306         QVERIFY(!socket->waitForConnected(5000));
  2267     }
  2307     }
  2268     QVERIFY(!socket->errorString().isEmpty());
  2308     QVERIFY(!socket->errorString().isEmpty());
  2269 
  2309 
  2270     // note: the following test is not a hard failure.
  2310     // note: the following test is not a hard failure.
  2271     // Sometimes, error codes change for the better
  2311     // Sometimes, error codes change for the better
  2380 
  2420 
  2381     if (failsAtConnect) {
  2421     if (failsAtConnect) {
  2382         QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
  2422         QCOMPARE(socket->state(), QAbstractSocket::UnconnectedState);
  2383     } else {
  2423     } else {
  2384         QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
  2424         QCOMPARE(socket->state(), QAbstractSocket::ConnectingState);
  2385         QVERIFY(socket->waitForConnected(10000));
  2425         QVERIFY(socket->waitForConnected(5000));
  2386         QCOMPARE(proxyAuthCalled, 1);
  2426         QCOMPARE(proxyAuthCalled, 1);
  2387     }
  2427     }
  2388     QVERIFY(!socket->errorString().isEmpty());
  2428     QVERIFY(!socket->errorString().isEmpty());
  2389 
  2429 
  2390     // note: the following test is not a hard failure.
  2430     // note: the following test is not a hard failure.