tests/auto/qtcpsocket/tst_qtcpsocket.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
child 33 3e2da88830cd
--- a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp	Wed Jun 23 19:07:03 2010 +0300
+++ b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp	Tue Jul 06 15:10:48 2010 +0300
@@ -144,6 +144,7 @@
     void blockingIMAP();
     void nonBlockingIMAP();
     void hostNotFound();
+    void timeoutConnect_data();
     void timeoutConnect();
     void delayedClose();
     void partialRead();
@@ -193,6 +194,7 @@
     void increaseReadBufferSize();
     void taskQtBug5799ConnectionErrorWaitForConnected();
     void taskQtBug5799ConnectionErrorEventLoop();
+    void taskQtBug7054TimeoutErrorResetting();
 
     void invalidProxy_data();
     void invalidProxy();
@@ -543,19 +545,36 @@
 }
 
 //----------------------------------------------------------------------------------
+void tst_QTcpSocket::timeoutConnect_data()
+{
+    QTest::addColumn<QString>("address");
+    QTest::newRow("host") << QtNetworkSettings::serverName();
+    QTest::newRow("ip") << QtNetworkSettings::serverIP().toString();
+}
 
 void tst_QTcpSocket::timeoutConnect()
 {
+    QFETCH(QString, address);
     QTcpSocket *socket = newSocket();
 
-    // Outgoing port 53 is firewalled in the Oslo office.
-    socket->connectToHost("cisco.com", 53);
+    QElapsedTimer timer;
+    timer.start();
+
+    // Port 1357 is configured to drop packets on the test server
+    socket->connectToHost(address, 1357);
+    QVERIFY(timer.elapsed() < 50);
     QVERIFY(!socket->waitForConnected(200));
     QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
     QCOMPARE(int(socket->error()), int(QTcpSocket::SocketTimeoutError));
 
-    socket->connectToHost("cisco.com", 53);
-    QTest::qSleep(50);
+    timer.start();
+    socket->connectToHost(address, 1357);
+    QVERIFY(timer.elapsed() < 50);
+    QTimer::singleShot(50, &QTestEventLoop::instance(), SLOT(exitLoop()));
+    QTestEventLoop::instance().enterLoop(5);
+    QVERIFY(!QTestEventLoop::instance().timeout());
+    QVERIFY(socket->state() == QTcpSocket::ConnectingState
+            || socket->state() == QTcpSocket::HostLookupState);
     socket->abort();
     QCOMPARE(socket->state(), QTcpSocket::UnconnectedState);
     QCOMPARE(socket->openMode(), QIODevice::NotOpen);
@@ -2245,6 +2264,30 @@
              QString("Could not reach server: %1").arg(socket.errorString()).toLocal8Bit());
 }
 
+void tst_QTcpSocket::taskQtBug7054TimeoutErrorResetting()
+{
+    QTcpSocket *socket = newSocket();
+
+    socket->connectToHost(QtNetworkSettings::serverName(), 443);
+    QVERIFY(socket->waitForConnected(5*1000));
+    QVERIFY(socket->error() == QAbstractSocket::UnknownSocketError);
+
+    // We connected to the HTTPS port. Wait two seconds to receive data. We will receive
+    // nothing because we would need to start the SSL handshake
+    QVERIFY(!socket->waitForReadyRead(2*1000));
+    QVERIFY(socket->error() == QAbstractSocket::SocketTimeoutError);
+
+    // Now write some crap to make the server disconnect us. 4 lines are enough.
+    socket->write("a\r\nb\r\nc\r\nd\r\n");
+    socket->waitForBytesWritten(2*1000);
+
+    // we try to waitForReadyRead another time, but this time instead of a timeout we
+    // should get a better error since the server disconnected us
+    QVERIFY(!socket->waitForReadyRead(2*1000));
+    // It must NOT be the SocketTimeoutError that had been set before
+    QVERIFY(socket->error() == QAbstractSocket::RemoteHostClosedError);
+}
+
 void tst_QTcpSocket::invalidProxy_data()
 {
     QTest::addColumn<int>("type");