diff options
Diffstat (limited to 'tests/auto/network/socket/qtcpserver')
6 files changed, 98 insertions, 53 deletions
diff --git a/tests/auto/network/socket/qtcpserver/BLACKLIST b/tests/auto/network/socket/qtcpserver/BLACKLIST index ecca156cef..a8b5f5d137 100644 --- a/tests/auto/network/socket/qtcpserver/BLACKLIST +++ b/tests/auto/network/socket/qtcpserver/BLACKLIST @@ -1,18 +1,13 @@ [listenWhileListening:WithSocks5Proxy] linux windows -[ipv6Server] -windows-7sp1 -windows-10 msvc-2017 [ipv6Server:WithoutProxy] windows osx [serverAddress] # QTBUG-103056 qnx -windows-7sp1 windows-10 [linkLocal] macos arm - diff --git a/tests/auto/network/socket/qtcpserver/CMakeLists.txt b/tests/auto/network/socket/qtcpserver/CMakeLists.txt index 4ca1cc79f0..b01a164302 100644 --- a/tests/auto/network/socket/qtcpserver/CMakeLists.txt +++ b/tests/auto/network/socket/qtcpserver/CMakeLists.txt @@ -1,4 +1,11 @@ -# Generated from qtcpserver.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qtcpserver LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() add_subdirectory(crashingServer) add_subdirectory(test) diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt b/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt index dd309ba605..bb1feb0237 100644 --- a/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt +++ b/tests/auto/network/socket/qtcpserver/crashingServer/CMakeLists.txt @@ -1,4 +1,5 @@ -# Generated from crashingServer.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## crashingServer Binary: @@ -8,7 +9,7 @@ qt_internal_add_executable(crashingServer OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/" SOURCES main.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Network ) diff --git a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp index 5e00a2bd20..1c41552eb5 100644 --- a/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp +++ b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QtCore> @@ -7,18 +7,45 @@ #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) # include <crtdbg.h> #endif +#ifdef Q_OS_UNIX +# include <sys/resource.h> +# include <unistd.h> +#endif int main(int argc, char *argv[]) { - // Windows: Suppress crash notification dialog. #if defined(Q_OS_WIN) && defined(Q_CC_MSVC) + // Windows: Suppress crash notification dialog. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG); +#elif defined(RLIMIT_CORE) + // Unix: set our core dump limit to zero to request no dialogs. + if (struct rlimit rlim; getrlimit(RLIMIT_CORE, &rlim) == 0) { + rlim.rlim_cur = 0; + setrlimit(RLIMIT_CORE, &rlim); + } #endif + QCoreApplication app(argc, argv); + if (argc < 1) { + fprintf(stderr, "Need a port number\n"); + return 1; + } + int port = QByteArrayView(argv[1]).toInt(); QTcpServer server; - if (!server.listen(QHostAddress::LocalHost, 49199)) { - qDebug("Failed to listen: %s", server.errorString().toLatin1().constData()); + if (!server.listen(QHostAddress::LocalHost, port)) { + fprintf(stderr, "Failed to listen: %s\n", server.errorString().toLatin1().constData()); + if (server.serverError() == QTcpSocket::AddressInUseError) { + // let's see if we can find the process that would be holding this + // still open +#ifdef Q_OS_LINUX + static const char *ss_args[] = { + "ss", "-nap", "sport", "=", argv[1], nullptr + }; + dup2(STDERR_FILENO, STDOUT_FILENO); + execvp(ss_args[0], const_cast<char **>(ss_args)); +#endif + } return 1; } diff --git a/tests/auto/network/socket/qtcpserver/test/CMakeLists.txt b/tests/auto/network/socket/qtcpserver/test/CMakeLists.txt index 47e566d268..55615bbae1 100644 --- a/tests/auto/network/socket/qtcpserver/test/CMakeLists.txt +++ b/tests/auto/network/socket/qtcpserver/test/CMakeLists.txt @@ -1,32 +1,27 @@ -# Generated from test.pro. +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause ##################################################################### ## tst_qtcpserver Test: ##################################################################### qt_internal_add_test(tst_qtcpserver - OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../" # special case + OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../" SOURCES ../tst_qtcpserver.cpp - PUBLIC_LIBRARIES + LIBRARIES Qt::Network - QT_TEST_SERVER_LIST "danted" "cyrus" "squid" "ftp-proxy" # special case + QT_TEST_SERVER_LIST "danted" "cyrus" "squid" "ftp-proxy" ) add_dependencies(tst_qtcpserver crashingServer ) -#### Keys ignored in scope 1:.:.:test.pro:<TRUE>: -# MOC_DIR = "tmp" - ## Scopes: ##################################################################### qt_internal_extend_target(tst_qtcpserver CONDITION WIN32 - PUBLIC_LIBRARIES + LIBRARIES ws2_32 ) - -#### Keys ignored in scope 6:.:.:test.pro:LINUX: -# QT_TEST_SERVER_LIST = "danted" "cyrus" "squid" "ftp-proxy" diff --git a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp index 52c7375a80..c03076d98e 100644 --- a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp +++ b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp @@ -1,9 +1,7 @@ // Copyright (C) 2016 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qglobal.h> -// To prevent windows system header files from re-defining min/max -#define NOMINMAX 1 #if defined(_WIN32) #include <winsock2.h> #else @@ -140,7 +138,8 @@ void tst_QTcpServer::initTestCase() #ifdef QT_TEST_SERVER QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1080)); QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::httpProxyServerName(), 3128)); - QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpProxyServerName(), 2121)); + // FTP currently not supported: + // QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpProxyServerName(), 2121)); QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::imapServerName(), 143)); #else if (!QtNetworkSettings::verifyTestNetworkSettings()) @@ -248,7 +247,7 @@ void tst_QTcpServer::clientServerLoop() QVERIFY(server.waitForNewConnection(5000)); QVERIFY(server.hasPendingConnections()); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); QTcpSocket *serverSocket = server.nextPendingConnection(); QVERIFY(serverSocket != 0); @@ -409,9 +408,9 @@ void tst_QTcpServer::maxPendingConnections() // two connections have been made. The second compare makes sure no // more are accepted. Creating connections happens multithreaded so // qWait must be used for that. - QTRY_COMPARE(spy.count(), 2); + QTRY_COMPARE(spy.size(), 2); QTest::qWait(100); - QCOMPARE(spy.count(), 2); + QCOMPARE(spy.size(), 2); QVERIFY(server.hasPendingConnections()); QVERIFY(server.nextPendingConnection()); @@ -460,7 +459,7 @@ public: protected: void run() override { - sleep(2); + sleep(std::chrono::seconds{2}); QTcpSocket socket; socket.connectToHost(host, port); @@ -578,9 +577,6 @@ void tst_QTcpServer::addressReusable() #if !QT_CONFIG(process) QSKIP("No qprocess support", SkipAll); #else -#ifdef Q_OS_LINUX - QSKIP("The addressReusable test is unstable on Linux. See QTBUG-39985."); -#endif QFETCH_GLOBAL(bool, setProxy); if (setProxy) { #ifndef QT_NO_NETWORKPROXY @@ -591,16 +587,25 @@ void tst_QTcpServer::addressReusable() QSKIP("No proxy support"); #endif // QT_NO_NETWORKPROXY } + + QTcpServer server; + QVERIFY(server.listen(QHostAddress::LocalHost, 0)); + quint16 serverPort = server.serverPort(); + qDebug() << "Got port" << serverPort; + server.close(); // cleanly close + + QTest::qSleep(10); + // The crashingServer process will crash once it gets a connection. QProcess process; QString processExe = crashingServerDir + "/crashingServer"; - process.start(processExe); + process.start(processExe, { QString::number(serverPort) }); QVERIFY2(process.waitForStarted(), qPrintable( QString::fromLatin1("Could not start %1: %2").arg(processExe, process.errorString()))); - QVERIFY(process.waitForReadyRead(5000)); + QVERIFY2(process.waitForReadyRead(5000), qPrintable(process.readAllStandardError())); QTcpSocket socket; - socket.connectToHost(QHostAddress::LocalHost, 49199); + socket.connectToHost(QHostAddress::LocalHost, serverPort); QVERIFY(socket.waitForConnected(5000)); QVERIFY(process.waitForFinished(30000)); @@ -608,8 +613,9 @@ void tst_QTcpServer::addressReusable() // Give the system some time. QTest::qSleep(10); - QTcpServer server; - QVERIFY(server.listen(QHostAddress::LocalHost, 49199)); + // listen again + QVERIFY2(server.listen(QHostAddress::LocalHost, serverPort), + qPrintable(server.errorString())); #endif } @@ -728,6 +734,7 @@ void tst_QTcpServer::proxyFactory_data() << proxyList << proxyList.at(1) << false << int(QAbstractSocket::UnknownSocketError); +#if 0 // ftp not currently supported proxyList.clear(); proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::ftpProxyServerName(), 2121) << QNetworkProxy(QNetworkProxy::HttpCachingProxy, QtNetworkSettings::httpProxyServerName(), 3128) @@ -735,6 +742,7 @@ void tst_QTcpServer::proxyFactory_data() QTest::newRow("ftp+cachinghttp+socks5") << proxyList << proxyList.at(2) << false << int(QAbstractSocket::UnknownSocketError); +#endif // tests that fail to listen proxyList.clear(); @@ -749,6 +757,7 @@ void tst_QTcpServer::proxyFactory_data() << proxyList << QNetworkProxy() << true << int(QAbstractSocket::UnsupportedSocketOperationError); +#if 0 // ftp not currently supported proxyList.clear(); proxyList << QNetworkProxy(QNetworkProxy::FtpCachingProxy, QtNetworkSettings::ftpProxyServerName(), 2121); QTest::newRow("ftp") @@ -761,6 +770,7 @@ void tst_QTcpServer::proxyFactory_data() QTest::newRow("ftp+cachinghttp") << proxyList << QNetworkProxy() << true << int(QAbstractSocket::UnsupportedSocketOperationError); +#endif } void tst_QTcpServer::proxyFactory() @@ -855,10 +865,12 @@ void tst_QTcpServer::serverAddress_data() QTest::newRow("AnyIPv4") << QHostAddress(QHostAddress::AnyIPv4) << QHostAddress(QHostAddress::AnyIPv4); if (QtNetworkSettings::hasIPv6()) QTest::newRow("AnyIPv6") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6); - foreach (const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) { + const auto ifaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &iface : ifaces) { if ((iface.flags() & QNetworkInterface::IsUp) == 0) continue; - foreach (const QNetworkAddressEntry &entry, iface.addressEntries()) { + const auto entries = iface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { QTest::newRow(qPrintable(entry.ip().toString())) << entry.ip() << entry.ip(); } } @@ -912,7 +924,8 @@ void tst_QTcpServer::linkLocal() QSet <QString> scopes; QHostAddress localMaskv4("169.254.0.0"); QHostAddress localMaskv6("fe80::"); - foreach (const QNetworkInterface& iface, QNetworkInterface::allInterfaces()) { + const auto ifaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &iface : ifaces) { //Windows preallocates link local addresses to interfaces that are down. //These may or may not work depending on network driver (they do not work for the Bluetooth PAN driver) if (iface.flags() & QNetworkInterface::IsUp) { @@ -925,8 +938,15 @@ void tst_QTcpServer::linkLocal() // (we don't know why) if (iface.name().startsWith("utun")) continue; + // Do not use the iBridge interfae + if (iface.hardwareAddress() == "AC:DE:48:00:11:22") + continue; + // Do no use the Apple Wireless Direct Link interfaces + if (iface.name().startsWith("awdl")) + continue; #endif - foreach (QNetworkAddressEntry addressEntry, iface.addressEntries()) { + const auto entries = iface.addressEntries(); + for (const QNetworkAddressEntry &addressEntry : entries) { QHostAddress addr = addressEntry.ip(); if (addr.isInSubnet(localMaskv4, 16)) { addresses << addr; @@ -945,7 +965,7 @@ void tst_QTcpServer::linkLocal() QList<QTcpServer*> servers; quint16 port = 0; - foreach (const QHostAddress& addr, addresses) { + for (const QHostAddress &addr : std::as_const(addresses)) { QTcpServer *server = new QTcpServer; QVERIFY(server->listen(addr, port)); port = server->serverPort(); //listen to same port on different interfaces @@ -953,7 +973,7 @@ void tst_QTcpServer::linkLocal() } QList<QTcpSocket*> clients; - foreach (const QHostAddress& addr, addresses) { + for (const QHostAddress &addr : std::as_const(addresses)) { //unbound socket QTcpSocket *socket = new QTcpSocket; socket->connectToHost(addr, port); @@ -968,7 +988,7 @@ void tst_QTcpServer::linkLocal() } //each server should have two connections - foreach (QTcpServer* server, servers) { + for (QTcpServer *server : std::as_const(servers)) { //qDebug() << "checking for connections" << server->serverAddress() << ":" << server->serverPort(); QVERIFY(server->waitForNewConnection(5000)); QTcpSocket* remote = server->nextPendingConnection(); @@ -1003,12 +1023,12 @@ void tst_QTcpServer::eagainBlockingAccept() QTcpSocket s; s.connectToHost(QHostAddress::LocalHost, 7896); QSignalSpy spy(&server, SIGNAL(newConnection())); - QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 1, 500); + QTRY_COMPARE_WITH_TIMEOUT(spy.size(), 1, 500); s.close(); // To test try again, should connect just fine. s.connectToHost(QHostAddress::LocalHost, 7896); - QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 2, 500); + QTRY_COMPARE_WITH_TIMEOUT(spy.size(), 2, 500); s.close(); server.close(); } @@ -1044,13 +1064,13 @@ void tst_QTcpServer::pauseAccepting() QTcpSocket sockets[NumSockets]; sockets[0].connectToHost(address, server.serverPort()); QVERIFY(spy.wait()); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); server.pauseAccepting(); for (int i = 1; i < NumSockets; ++i) sockets[i].connectToHost(address, server.serverPort()); QVERIFY(!spy.wait(400)); - QCOMPARE(spy.count(), 1); + QCOMPARE(spy.size(), 1); server.resumeAccepting(); if (setProxy) { @@ -1058,7 +1078,7 @@ void tst_QTcpServer::pauseAccepting() Abort); } QVERIFY(spy.wait()); - QCOMPARE(spy.count(), 6); + QCOMPARE(spy.size(), 6); } @@ -1122,11 +1142,11 @@ void tst_QTcpServer::pendingConnectionAvailable() QCOMPARE(socket.state(), QTcpSocket::ConnectedState); int expectedPendingConnections = useDerivedServer ? 0 : 1; - QCOMPARE(pendingConnectionSpy.count(), expectedPendingConnections); + QCOMPARE(pendingConnectionSpy.size(), expectedPendingConnections); if (useDerivedServer) static_cast<DerivedServer *>(server)->emitNextSocket(); - QCOMPARE(pendingConnectionSpy.count(), 1); + QCOMPARE(pendingConnectionSpy.size(), 1); } QTEST_MAIN(tst_QTcpServer) |