summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/socket
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2019-07-02 14:28:58 +0200
committerVolker Hilsheimer <volker.hilsheimer@qt.io>2019-07-11 10:46:23 +0200
commitcc32226e647854a02ecd0775787bb08b85e2a014 (patch)
tree3a50004830c421d52f8c0138254f3e1c4a66791f /tests/auto/network/socket
parent2d597c0e1a32dda3f4d2f1c941b8a0d2a4be4735 (diff)
Windows: handle errors correctly when connecting to unreachable host
The previous code handled only some error codes, in a very inefficient way, for some code paths. This change standardizes error handling using a helper function that maps winsock WSAE* codes to Qt error codes. The test for connecting to unreachable hosts or ports is now more generic, and enabled on Windows, where it passes in local tests, but dependency on network configuration still makes it fragile, so ignoring some failures without completely skipping the test. [ChangeLog][Network][Windows] Correctly emit errors when trying to reach unreachable hosts or services Change-Id: Icaca3e6fef88621d683f6d6fa3016212847de4ea Fixes: QTBUG-42567 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'tests/auto/network/socket')
-rw-r--r--tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp39
1 files changed, 29 insertions, 10 deletions
diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp
index 2d64714f92..10b09629bc 100644
--- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp
+++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp
@@ -164,9 +164,8 @@ private slots:
void waitForReadyReadInASlot();
void remoteCloseError();
void nestedEventLoopInErrorSlot();
-#ifndef Q_OS_WIN
- void connectToLocalHostNoService();
-#endif
+ void connectToHostError_data();
+ void connectToHostError();
void waitForConnectedInHostLookupSlot();
void waitForConnectedInHostLookupSlot2();
void readyReadSignalsAfterWaitForReadyRead();
@@ -2040,18 +2039,38 @@ void tst_QTcpSocket::nestedEventLoopInErrorSlot()
}
//----------------------------------------------------------------------------------
-#ifndef Q_OS_WIN
-void tst_QTcpSocket::connectToLocalHostNoService()
+
+void tst_QTcpSocket::connectToHostError_data()
+{
+ QTest::addColumn<QString>("host");
+ QTest::addColumn<int>("port");
+ QTest::addColumn<QAbstractSocket::SocketError>("expectedError");
+
+ QTest::newRow("localhost no service") << QStringLiteral("localhost") << 31415 << QAbstractSocket::ConnectionRefusedError;
+ QTest::newRow("unreachable") << QStringLiteral("0.0.0.1") << 65000 << QAbstractSocket::NetworkError;
+}
+
+
+void tst_QTcpSocket::connectToHostError()
{
- // This test was created after we received a report that claimed
- // QTcpSocket would crash if trying to connect to "localhost" on a random
- // port with no service listening.
QTcpSocket *socket = newSocket();
- socket->connectToHost("localhost", 31415); // no service running here, one suspects
+
+ QAbstractSocket::SocketError error = QAbstractSocket::UnknownSocketError;
+
+ QFETCH(QString, host);
+ QFETCH(int, port);
+ QFETCH(QAbstractSocket::SocketError, expectedError);
+
+ connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),[&](QAbstractSocket::SocketError socketError){
+ error = socketError;
+ });
+ socket->connectToHost(host, port); // no service running here, one suspects
QTRY_COMPARE(socket->state(), QTcpSocket::UnconnectedState);
+ if (error != expectedError && error == QAbstractSocket::ConnectionRefusedError)
+ QEXPECT_FAIL("unreachable", "CI firewall interfers with this test", Continue);
+ QCOMPARE(error, expectedError);
delete socket;
}
-#endif
//----------------------------------------------------------------------------------
void tst_QTcpSocket::waitForConnectedInHostLookupSlot()