diff options
Diffstat (limited to 'tests/auto/network/socket')
32 files changed, 276 insertions, 134 deletions
diff --git a/tests/auto/network/socket/CMakeLists.txt b/tests/auto/network/socket/CMakeLists.txt index c3f8e8f87f..7136017f39 100644 --- a/tests/auto/network/socket/CMakeLists.txt +++ b/tests/auto/network/socket/CMakeLists.txt @@ -7,14 +7,20 @@ if(QT_FEATURE_private_tests) add_subdirectory(qsocks5socketengine) add_subdirectory(platformsocketengine) endif() -add_subdirectory(qudpsocket) +if(QT_FEATURE_udpsocket) + add_subdirectory(qudpsocket) +endif() add_subdirectory(qabstractsocket) -if(NOT ANDROID) +if(QT_FEATURE_localserver AND NOT ANDROID) # QTBUG-87387 add_subdirectory(qlocalsocket) +endif() + +if(QT_FEATURE_networkinterface AND NOT ANDROID) # QTBUG-87388 add_subdirectory(qtcpserver) endif() + if(QT_FEATURE_sctp) add_subdirectory(qsctpsocket) endif() diff --git a/tests/auto/network/socket/platformsocketengine/BLACKLIST b/tests/auto/network/socket/platformsocketengine/BLACKLIST index f1f88d26d1..796f81a02a 100644 --- a/tests/auto/network/socket/platformsocketengine/BLACKLIST +++ b/tests/auto/network/socket/platformsocketengine/BLACKLIST @@ -1,11 +1,2 @@ [tcpLoopbackPerformance] -windows-10 msvc-2015 -windows-7sp1 -[receiveUrgentData] -windows-10 msvc-2015 -windows-7sp1 -[serverTest] -windows-10 msvc-2015 -windows-7sp1 -[tcpLoopbackPerformance] windows diff --git a/tests/auto/network/socket/platformsocketengine/CMakeLists.txt b/tests/auto/network/socket/platformsocketengine/CMakeLists.txt index d94c700770..891041df04 100644 --- a/tests/auto/network/socket/platformsocketengine/CMakeLists.txt +++ b/tests/auto/network/socket/platformsocketengine/CMakeLists.txt @@ -1,6 +1,12 @@ # 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_platformsocketengine LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + if(NOT QT_FEATURE_private_tests) return() endif() diff --git a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp index 7b1b0ad666..1f6ecf9200 100644 --- a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp +++ b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. -// 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 <QtTest/QTest> @@ -255,7 +255,7 @@ void tst_PlatformSocketEngine::broadcastTest() PLATFORMSOCKETENGINE broadcastSocket; // Initialize a regular Udp socket - QVERIFY(broadcastSocket.initialize(QAbstractSocket::UdpSocket, QAbstractSocket::AnyIPProtocol)); + QVERIFY(broadcastSocket.initialize(QAbstractSocket::UdpSocket, QAbstractSocket::IPv4Protocol)); // Bind to any port on all interfaces QVERIFY(broadcastSocket.bind(QHostAddress::Any, 0)); diff --git a/tests/auto/network/socket/qabstractsocket/CMakeLists.txt b/tests/auto/network/socket/qabstractsocket/CMakeLists.txt index 24e8af2bdc..3ca18aceef 100644 --- a/tests/auto/network/socket/qabstractsocket/CMakeLists.txt +++ b/tests/auto/network/socket/qabstractsocket/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qabstractsocket Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qabstractsocket LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qabstractsocket SOURCES tst_qabstractsocket.cpp diff --git a/tests/auto/network/socket/qabstractsocket/tst_qabstractsocket.cpp b/tests/auto/network/socket/qabstractsocket/tst_qabstractsocket.cpp index 984f411550..6b038acdfa 100644 --- a/tests/auto/network/socket/qabstractsocket/tst_qabstractsocket.cpp +++ b/tests/auto/network/socket/qabstractsocket/tst_qabstractsocket.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 <QTest> diff --git a/tests/auto/network/socket/qhttpsocketengine/BLACKLIST b/tests/auto/network/socket/qhttpsocketengine/BLACKLIST index ceb3b7862e..e0cd701636 100644 --- a/tests/auto/network/socket/qhttpsocketengine/BLACKLIST +++ b/tests/auto/network/socket/qhttpsocketengine/BLACKLIST @@ -1,5 +1,2 @@ -[downloadBigFile] -windows-10 msvc-2015 -windows-7sp1 [ensureEofTriggersNotification] windows diff --git a/tests/auto/network/socket/qhttpsocketengine/CMakeLists.txt b/tests/auto/network/socket/qhttpsocketengine/CMakeLists.txt index 7dfae9059d..6b4f904725 100644 --- a/tests/auto/network/socket/qhttpsocketengine/CMakeLists.txt +++ b/tests/auto/network/socket/qhttpsocketengine/CMakeLists.txt @@ -1,6 +1,12 @@ # 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_qhttpsocketengine LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + if(NOT QT_FEATURE_private_tests) return() endif() diff --git a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp index c9bf8b92f3..a52099b5b4 100644 --- a/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.cpp +++ b/tests/auto/network/socket/qhttpsocketengine/tst_qhttpsocketengine.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 <QtTest/QTest> @@ -18,6 +18,8 @@ #include "../../../network-settings.h" +using namespace std::chrono_literals; + class tst_QHttpSocketEngine : public QObject { Q_OBJECT @@ -323,7 +325,7 @@ void tst_QHttpSocketEngine::simpleErrorsAndStates() QCOMPARE(socketDevice.state(), QAbstractSocket::UnconnectedState); QVERIFY(!socketDevice.connectToHost(QHostAddress(QtNetworkSettings::socksProxyServerName()), 8088)); QCOMPARE(socketDevice.state(), QAbstractSocket::ConnectingState); - if (socketDevice.waitForWrite(30000)) { + if (socketDevice.waitForWrite(30s)) { QVERIFY(socketDevice.state() == QAbstractSocket::ConnectedState || socketDevice.state() == QAbstractSocket::UnconnectedState); } else { diff --git a/tests/auto/network/socket/qlocalsocket/CMakeLists.txt b/tests/auto/network/socket/qlocalsocket/CMakeLists.txt index 028f3633fd..4b886a02ee 100644 --- a/tests/auto/network/socket/qlocalsocket/CMakeLists.txt +++ b/tests/auto/network/socket/qlocalsocket/CMakeLists.txt @@ -1,8 +1,14 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -# add_subdirectory(test) # special case remove +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qlocalsocket LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + add_subdirectory(socketprocess) + qt_internal_add_test(tst_qlocalsocket SOURCES tst_qlocalsocket.cpp @@ -11,5 +17,6 @@ qt_internal_add_test(tst_qlocalsocket QLOCALSOCKET_DEBUG LIBRARIES Qt::Network + Qt::TestPrivate ) add_dependencies(tst_qlocalsocket socketprocess) diff --git a/tests/auto/network/socket/qlocalsocket/socketprocess/main.cpp b/tests/auto/network/socket/qlocalsocket/socketprocess/main.cpp index c1a78fae23..d254253aca 100644 --- a/tests/auto/network/socket/qlocalsocket/socketprocess/main.cpp +++ b/tests/auto/network/socket/qlocalsocket/socketprocess/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 <qcoreapplication.h> diff --git a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp index f240f3e832..30ffb50d23 100644 --- a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp +++ b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp @@ -1,10 +1,11 @@ // Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. -// 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 <QTest> #include <QSignalSpy> +#include <QtTest/private/qpropertytesthelper_p.h> #if QT_CONFIG(process) #include <QProcess> #endif @@ -123,6 +124,7 @@ private slots: void verifyListenWithDescriptor_data(); void serverBindingsAndProperties(); + void socketBindings(); protected slots: void socketClosedSlot(); @@ -1662,6 +1664,9 @@ void tst_QLocalSocket::asyncDisconnectNotify() void tst_QLocalSocket::verifySocketOptions_data() { #ifdef Q_OS_LINUX + if (::geteuid() == 0) + QSKIP("Running this test as root doesn't make sense"); + QTest::addColumn<QString>("service"); QTest::addColumn<QLocalServer::SocketOption>("opts"); QTest::addColumn<QFile::Permissions>("perms"); @@ -1707,7 +1712,7 @@ void tst_QLocalSocket::verifySocketOptions() void tst_QLocalSocket::verifyListenWithDescriptor() { -#ifdef Q_OS_UNIX +#if defined(Q_OS_UNIX) && !defined(Q_OS_VXWORKS) QFETCH(QString, path); QFETCH(bool, abstract); QFETCH(bool, bound); @@ -1807,14 +1812,18 @@ void tst_QLocalSocket::serverBindingsAndProperties() { CrashSafeLocalServer server; - QProperty<QLocalServer::SocketOptions> sockOpts; - server.bindableSocketOptions().setBinding(Qt::makePropertyBinding(sockOpts)); - sockOpts = QLocalServer::GroupAccessOption | QLocalServer::UserAccessOption; - QCOMPARE(server.socketOptions(), sockOpts.value()); + QTestPrivate::testReadWritePropertyBasics( + server, QLocalServer::SocketOptions{QLocalServer::GroupAccessOption}, + QLocalServer::SocketOptions{QLocalServer::OtherAccessOption}, "socketOptions"); +} + +void tst_QLocalSocket::socketBindings() +{ + QLocalSocket socket; - sockOpts.setBinding(server.bindableSocketOptions().makeBinding()); - server.setSocketOptions(QLocalServer::OtherAccessOption); - QCOMPARE(sockOpts.value(), QLocalServer::OtherAccessOption); + QTestPrivate::testReadWritePropertyBasics( + socket, QLocalSocket::SocketOptions{QLocalSocket::AbstractNamespaceOption}, + QLocalSocket::SocketOptions{QLocalSocket::NoOptions}, "socketOptions"); } QTEST_MAIN(tst_QLocalSocket) diff --git a/tests/auto/network/socket/qsctpsocket/CMakeLists.txt b/tests/auto/network/socket/qsctpsocket/CMakeLists.txt index fb08d03aad..4bf5438841 100644 --- a/tests/auto/network/socket/qsctpsocket/CMakeLists.txt +++ b/tests/auto/network/socket/qsctpsocket/CMakeLists.txt @@ -5,6 +5,12 @@ ## tst_qsctpsocket Test: ##################################################################### +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qsctpsocket LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + qt_internal_add_test(tst_qsctpsocket SOURCES tst_qsctpsocket.cpp diff --git a/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp b/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp index d661ad5405..2893053158 100644 --- a/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp +++ b/tests/auto/network/socket/qsctpsocket/tst_qsctpsocket.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2016 Alex Trotsenko <alex1973tr@gmail.com> -// 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 <QTest> #include <QDebug> @@ -127,12 +127,13 @@ void tst_QSctpSocket::bind_data() // these ranges are guaranteed to be reserved for 'documentation purposes', // and thus, should be unused in the real world. Not that I'm assuming the // world is full of competent administrators, or anything. - QStringList knownBad; - knownBad << "198.51.100.1"; - knownBad << "2001:0DB8::1"; - foreach (const QString &badAddress, knownBad) { + const QString knownBad[] = { + "198.51.100.1", + "2001:0DB8::1", + }; + + for (const QString &badAddress : knownBad) QTest::newRow(badAddress.toLatin1().constData()) << badAddress << false << QString(); - } } // Testing bind function diff --git a/tests/auto/network/socket/qsocks5socketengine/BLACKLIST b/tests/auto/network/socket/qsocks5socketengine/BLACKLIST index 479d0f878f..6b3a39ab5e 100644 --- a/tests/auto/network/socket/qsocks5socketengine/BLACKLIST +++ b/tests/auto/network/socket/qsocks5socketengine/BLACKLIST @@ -7,7 +7,4 @@ qnx ci # QTBUG-74162 [passwordAuth2] ubuntu -[downloadBigFile] -windows-10 msvc-2015 -windows-7sp1 diff --git a/tests/auto/network/socket/qsocks5socketengine/CMakeLists.txt b/tests/auto/network/socket/qsocks5socketengine/CMakeLists.txt index 9954084f39..dc0b87cd94 100644 --- a/tests/auto/network/socket/qsocks5socketengine/CMakeLists.txt +++ b/tests/auto/network/socket/qsocks5socketengine/CMakeLists.txt @@ -1,6 +1,12 @@ # 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_qsocks5socketengine LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + if(NOT QT_FEATURE_private_tests) return() endif() diff --git a/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp b/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp index aa860893bb..cc77ba2da3 100644 --- a/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.cpp +++ b/tests/auto/network/socket/qsocks5socketengine/tst_qsocks5socketengine.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 <QtTest/QTest> @@ -23,6 +23,8 @@ #include "../../../network-settings.h" +using namespace std::chrono_literals; + class tst_QSocks5SocketEngine : public QObject, public QAbstractSocketEngineReceiver { Q_OBJECT @@ -341,7 +343,7 @@ void tst_QSocks5SocketEngine::simpleErrorsAndStates() QCOMPARE(socketDevice.state(), QAbstractSocket::UnconnectedState); QVERIFY(!socketDevice.connectToHost(QHostInfo::fromName(QtNetworkSettings::socksProxyServerName()).addresses().first(), 8088)); QCOMPARE(socketDevice.state(), QAbstractSocket::ConnectingState); - if (socketDevice.waitForWrite(15000)) { + if (socketDevice.waitForWrite(15s)) { QVERIFY(socketDevice.state() == QAbstractSocket::UnconnectedState || socketDevice.state() == QAbstractSocket::ConnectedState); } else { diff --git a/tests/auto/network/socket/qtcpserver/BLACKLIST b/tests/auto/network/socket/qtcpserver/BLACKLIST index 339987046c..a8b5f5d137 100644 --- a/tests/auto/network/socket/qtcpserver/BLACKLIST +++ b/tests/auto/network/socket/qtcpserver/BLACKLIST @@ -1,16 +1,12 @@ [listenWhileListening:WithSocks5Proxy] linux windows -[ipv6Server] -windows-7sp1 -windows-10 msvc-2017 [ipv6Server:WithoutProxy] windows osx [serverAddress] # QTBUG-103056 qnx -windows-7sp1 windows-10 [linkLocal] diff --git a/tests/auto/network/socket/qtcpserver/CMakeLists.txt b/tests/auto/network/socket/qtcpserver/CMakeLists.txt index d87f18c4f0..b01a164302 100644 --- a/tests/auto/network/socket/qtcpserver/CMakeLists.txt +++ b/tests/auto/network/socket/qtcpserver/CMakeLists.txt @@ -1,5 +1,11 @@ # 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/main.cpp b/tests/auto/network/socket/qtcpserver/crashingServer/main.cpp index c14c429520..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> diff --git a/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp b/tests/auto/network/socket/qtcpserver/tst_qtcpserver.cpp index 4ff077c3c2..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()) @@ -735,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) @@ -742,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(); @@ -756,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") @@ -768,6 +770,7 @@ void tst_QTcpServer::proxyFactory_data() QTest::newRow("ftp+cachinghttp") << proxyList << QNetworkProxy() << true << int(QAbstractSocket::UnsupportedSocketOperationError); +#endif } void tst_QTcpServer::proxyFactory() @@ -862,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(); } } @@ -919,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) { @@ -939,7 +945,8 @@ void tst_QTcpServer::linkLocal() 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; @@ -958,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 @@ -966,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); @@ -981,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(); diff --git a/tests/auto/network/socket/qtcpsocket/CMakeLists.txt b/tests/auto/network/socket/qtcpsocket/CMakeLists.txt index 051e58f3ab..7b6bb4d881 100644 --- a/tests/auto/network/socket/qtcpsocket/CMakeLists.txt +++ b/tests/auto/network/socket/qtcpsocket/CMakeLists.txt @@ -1,6 +1,12 @@ # 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_qtcpsocket LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + if(NOT QT_FEATURE_private_tests) return() endif() diff --git a/tests/auto/network/socket/qtcpsocket/stressTest/Test.cpp b/tests/auto/network/socket/qtcpsocket/stressTest/Test.cpp index 4a7c36d1c1..8d6e470220 100644 --- a/tests/auto/network/socket/qtcpsocket/stressTest/Test.cpp +++ b/tests/auto/network/socket/qtcpsocket/stressTest/Test.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 // Qt #include <QByteArray> #include <QCoreApplication> diff --git a/tests/auto/network/socket/qtcpsocket/stressTest/Test.h b/tests/auto/network/socket/qtcpsocket/stressTest/Test.h index f5a19ae16a..495b90d733 100644 --- a/tests/auto/network/socket/qtcpsocket/stressTest/Test.h +++ b/tests/auto/network/socket/qtcpsocket/stressTest/Test.h @@ -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 #ifndef TEST_H #define TEST_H diff --git a/tests/auto/network/socket/qtcpsocket/stressTest/main.cpp b/tests/auto/network/socket/qtcpsocket/stressTest/main.cpp index 57fec691d6..f989a4de34 100644 --- a/tests/auto/network/socket/qtcpsocket/stressTest/main.cpp +++ b/tests/auto/network/socket/qtcpsocket/stressTest/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 "Test.h" #include <QCoreApplication> diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 50c9192ea2..4ec01a9d94 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -1,11 +1,9 @@ // Copyright (C) 2016 The Qt Company Ltd. // Copyright (C) 2017 Intel Corporation. -// 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 @@ -62,6 +60,8 @@ #include "../../../network-settings.h" +using namespace Qt::StringLiterals; + QT_FORWARD_DECLARE_CLASS(QTcpSocket) class SocketPair; @@ -283,7 +283,7 @@ tst_QTcpSocket::tst_QTcpSocket() tmpSocket = 0; //This code relates to the socketsConstructedBeforeEventLoop test case - earlyConstructedSockets = new SocketPair; + earlyConstructedSockets = new SocketPair(this); QVERIFY(earlyConstructedSockets->create()); earlyBytesWrittenCount = 0; earlyReadyReadCount = 0; @@ -332,7 +332,8 @@ void tst_QTcpSocket::initTestCase() //QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::firewallServerName(), 1357)); QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::socksProxyServerName(), 1080)); QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpServerName(), 21)); - QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpProxyServerName(), 2121)); + // FTP currently not supported: + // QVERIFY(QtNetworkSettings::verifyConnection(QtNetworkSettings::ftpProxyServerName(), 2121)); #else if (!QtNetworkSettings::verifyTestNetworkSettings()) QSKIP("No network test server available"); @@ -488,12 +489,13 @@ void tst_QTcpSocket::bind_data() bool testIpv6 = false; // iterate all interfaces, add all addresses on them as test data - QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); - foreach (const QNetworkInterface &netinterface, interfaces) { + const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &netinterface : interfaces) { if (!netinterface.isValid()) continue; - foreach (const QNetworkAddressEntry &entry, netinterface.addressEntries()) { + const auto entries = netinterface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { if (entry.ip().isInSubnet(QHostAddress::parseSubnet("fe80::/10")) || entry.ip().isInSubnet(QHostAddress::parseSubnet("169.254/16"))) continue; // link-local bind will fail, at least on Linux, so skip it. @@ -523,12 +525,12 @@ void tst_QTcpSocket::bind_data() // these ranges are guaranteed to be reserved for 'documentation purposes', // and thus, should be unused in the real world. Not that I'm assuming the // world is full of competent administrators, or anything. - QStringList knownBad; - knownBad << "198.51.100.1"; - knownBad << "2001:0DB8::1"; - foreach (const QString &badAddress, knownBad) { + const QString knownBad[] = { + u"198.51.100.1"_s, + u"2001:0DB8::1"_s + }; + for (const QString &badAddress : knownBad) QTest::addRow("%s:0", badAddress.toLatin1().constData()) << badAddress << 0 << false << QString(); - } // try to bind to a privileged ports // we should fail if we're not root (unless the ports are in use!) @@ -2581,7 +2583,7 @@ void tst_QTcpSocket::moveToThread0() { // Case 1: Moved after connecting, before waiting for connection. - QTcpSocket *socket = newSocket();; + QTcpSocket *socket = newSocket(); socket->connectToHost(QtNetworkSettings::imapServerName(), 143); socket->moveToThread(0); QVERIFY(socket->waitForConnected(5000)); @@ -2912,6 +2914,7 @@ void tst_QTcpSocket::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(), 3129) @@ -2919,6 +2922,7 @@ void tst_QTcpSocket::proxyFactory_data() QTest::newRow("ftp+cachinghttp+socks5") << proxyList << proxyList.at(2) << false << int(QAbstractSocket::UnknownSocketError); +#endif // tests that fail to connect proxyList.clear(); @@ -2927,6 +2931,7 @@ void tst_QTcpSocket::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") @@ -2939,6 +2944,7 @@ void tst_QTcpSocket::proxyFactory_data() QTest::newRow("ftp+cachinghttp") << proxyList << QNetworkProxy() << true << int(QAbstractSocket::UnsupportedSocketOperationError); +#endif } void tst_QTcpSocket::proxyFactory() diff --git a/tests/auto/network/socket/qudpsocket/BLACKLIST b/tests/auto/network/socket/qudpsocket/BLACKLIST index 704d83509d..f5f6a8e156 100644 --- a/tests/auto/network/socket/qudpsocket/BLACKLIST +++ b/tests/auto/network/socket/qudpsocket/BLACKLIST @@ -1,11 +1,7 @@ [writeDatagramToNonExistingPeer] windows -# QTBUG-85364 -windows-10 gcc cmake [readyReadForEmptyDatagram] opensuse-leap -[echo] -opensuse-42.3 [multicast] centos macos arm diff --git a/tests/auto/network/socket/qudpsocket/CMakeLists.txt b/tests/auto/network/socket/qudpsocket/CMakeLists.txt index 4d81f852d7..a7a2659340 100644 --- a/tests/auto/network/socket/qudpsocket/CMakeLists.txt +++ b/tests/auto/network/socket/qudpsocket/CMakeLists.txt @@ -1,5 +1,11 @@ # Copyright (C) 2022 The Qt Company Ltd. # SPDX-License-Identifier: BSD-3-Clause -add_subdirectory(test) +if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT) + cmake_minimum_required(VERSION 3.16) + project(tst_qudpsocket LANGUAGES CXX) + find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST) +endif() + add_subdirectory(clientserver) +add_subdirectory(test) diff --git a/tests/auto/network/socket/qudpsocket/clientserver/main.cpp b/tests/auto/network/socket/qudpsocket/clientserver/main.cpp index a83aa6d420..76bdf3aada 100644 --- a/tests/auto/network/socket/qudpsocket/clientserver/main.cpp +++ b/tests/auto/network/socket/qudpsocket/clientserver/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 <QtNetwork> class ClientServer : public QUdpSocket diff --git a/tests/auto/network/socket/qudpsocket/test/CMakeLists.txt b/tests/auto/network/socket/qudpsocket/test/CMakeLists.txt index 550ea53438..69b62c2f9f 100644 --- a/tests/auto/network/socket/qudpsocket/test/CMakeLists.txt +++ b/tests/auto/network/socket/qudpsocket/test/CMakeLists.txt @@ -14,3 +14,7 @@ qt_internal_add_test(tst_qudpsocket Qt::TestPrivate QT_TEST_SERVER_LIST "danted" "echo" ) + +if(QT_FEATURE_process) + add_dependencies(tst_qudpsocket clientserver) +endif() diff --git a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp index 4380433899..689ff452f9 100644 --- a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp +++ b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp @@ -1,7 +1,6 @@ // Copyright (C) 2021 The Qt Company Ltd. // Copyright (C) 2017 Intel Corporation. -// 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 <QTest> #include <QSignalSpy> @@ -11,6 +10,7 @@ #endif #include <QScopeGuard> #include <QVersionNumber> +#include <QSemaphore> #include <qcoreapplication.h> #include <qfileinfo.h> @@ -46,6 +46,8 @@ # define RELIABLE_BYTES_AVAILABLE #endif +using namespace Qt::StringLiterals; + Q_DECLARE_METATYPE(QHostAddress) QT_FORWARD_DECLARE_CLASS(QUdpSocket) @@ -103,6 +105,8 @@ private slots: void asyncReadDatagram(); void writeInHostLookupState(); + void readyReadConnectionThrottling(); + protected slots: void empty_readyReadSlot(); void empty_connectedSlot(); @@ -121,6 +125,7 @@ private: QList<QHostAddress> allAddresses; QHostAddress multicastGroup4, multicastGroup6; QList<QHostAddress> linklocalMulticastGroups; + QNetworkInterface ifaceWithIPv6; QUdpSocket *m_asyncSender; QUdpSocket *m_asyncReceiver; }; @@ -171,26 +176,7 @@ QNetworkInterface tst_QUdpSocket::interfaceForGroup(const QHostAddress &multicas if (!scope.isEmpty()) return QNetworkInterface::interfaceFromName(scope); - static QNetworkInterface ipv6if = [&]() { - // find any link local address in the allAddress list - for (const QHostAddress &addr: std::as_const(allAddresses)) { - if (addr.isLoopback()) - continue; - - QString scope = addr.scopeId(); - if (!scope.isEmpty()) { - QNetworkInterface iface = QNetworkInterface::interfaceFromName(scope); - qDebug() << "Will bind IPv6 sockets to" << iface; - return iface; - } - } - - qWarning("interfaceForGroup(%s) could not find any link-local IPv6 address! " - "Make sure this test is behind a check of QtNetworkSettings::hasIPv6().", - qUtf8Printable(multicastGroup.toString())); - return QNetworkInterface(); - }(); - return ipv6if; + return ifaceWithIPv6; } bool tst_QUdpSocket::shouldWorkaroundLinuxKernelBug() @@ -275,9 +261,16 @@ void tst_QUdpSocket::initTestCase() continue; llbase.setScopeId(scope); linklocalMulticastGroups << llbase; + if (!ifaceWithIPv6.isValid()) { + // Remember the first interface we've found that has IPv6 so we can + // bind non-link-local sockets to it (the first is least likely to + // be some weird virtual interface). + ifaceWithIPv6 = QNetworkInterface::interfaceFromName(scope); + } } qDebug() << "Will use multicast groups" << multicastGroup4 << multicastGroup6 << linklocalMulticastGroups; + qDebug() << "Will bind IPv6 sockets to" << ifaceWithIPv6; m_workaroundLinuxKernelBug = shouldWorkaroundLinuxKernelBug(); if (QTestPrivate::isRunningArmOnX86()) @@ -383,7 +376,8 @@ void tst_QUdpSocket::broadcasting() const char *message[] = {"Yo mista", "", "Yo", "Wassap"}; QList<QHostAddress> broadcastAddresses; - foreach (QNetworkInterface iface, QNetworkInterface::allInterfaces()) { + const auto ifaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &iface : ifaces) { if ((iface.flags() & QNetworkInterface::CanBroadcast) && iface.flags() & QNetworkInterface::IsUp) { for (int i=0;i<iface.addressEntries().size();i++) { @@ -411,7 +405,7 @@ void tst_QUdpSocket::broadcasting() for (int k = 0; k < 4; k++) { broadcastSocket.writeDatagram(message[i], strlen(message[i]), QHostAddress::Broadcast, serverPort); - foreach (QHostAddress addr, broadcastAddresses) + for (const QHostAddress &addr : std::as_const(broadcastAddresses)) broadcastSocket.writeDatagram(message[i], strlen(message[i]), addr, serverPort); } QTestEventLoop::instance().enterLoop(15); @@ -1241,11 +1235,12 @@ void tst_QUdpSocket::multicastTtlOption_data() QTest::addColumn<int>("ttl"); QTest::addColumn<int>("expected"); - QList<QHostAddress> addresses; - addresses += QHostAddress(QHostAddress::AnyIPv4); - addresses += QHostAddress(QHostAddress::AnyIPv6); + const QHostAddress addresses[] = { + QHostAddress(QHostAddress::AnyIPv4), + QHostAddress(QHostAddress::AnyIPv6), + }; - foreach (const QHostAddress &address, addresses) { + for (const QHostAddress &address : addresses) { const QByteArray addressB = address.toString().toLatin1(); QTest::newRow((addressB + " 0").constData()) << address << 0 << 0; QTest::newRow((addressB + " 1").constData()) << address << 1 << 1; @@ -1287,11 +1282,12 @@ void tst_QUdpSocket::multicastLoopbackOption_data() QTest::addColumn<int>("loopback"); QTest::addColumn<int>("expected"); - QList<QHostAddress> addresses; - addresses += QHostAddress(QHostAddress::AnyIPv4); - addresses += QHostAddress(QHostAddress::AnyIPv6); + const QHostAddress addresses[] = { + QHostAddress(QHostAddress::AnyIPv4), + QHostAddress(QHostAddress::AnyIPv6), + }; - foreach (const QHostAddress &address, addresses) { + for (const QHostAddress &address : addresses) { const QByteArray addressB = address.toString().toLatin1(); QTest::newRow((addressB + " 0").constData()) << address << 0 << 0; QTest::newRow((addressB + " 1").constData()) << address << 1 << 1; @@ -1391,11 +1387,12 @@ void tst_QUdpSocket::setMulticastInterface_data() { QTest::addColumn<QNetworkInterface>("iface"); QTest::addColumn<QHostAddress>("address"); - QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); - foreach (const QNetworkInterface &iface, interfaces) { + const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); + for (const QNetworkInterface &iface : interfaces) { if ((iface.flags() & QNetworkInterface::IsUp) == 0) continue; - foreach (const QNetworkAddressEntry &entry, iface.addressEntries()) { + const auto entries = iface.addressEntries(); + for (const QNetworkAddressEntry &entry : entries) { const QByteArray testName = iface.name().toLatin1() + ':' + entry.ip().toString().toLatin1(); QTest::newRow(testName.constData()) << iface << entry.ip(); } @@ -1499,15 +1496,16 @@ void tst_QUdpSocket::multicast() if (!joinResult) return; - QList<QByteArray> datagrams = QList<QByteArray>() - << QByteArray("0123") - << QByteArray("4567") - << QByteArray("89ab") - << QByteArray("cdef"); + const QByteArray datagrams[] = { + "0123"_ba, + "4567"_ba, + "89ab"_ba, + "cdef"_ba, + }; QUdpSocket sender; sender.bind(); - foreach (const QByteArray &datagram, datagrams) { + for (const QByteArray &datagram : datagrams) { QNetworkDatagram dgram(datagram, groupAddress, receiver.localPort()); dgram.setInterfaceIndex(interfaceForGroup(groupAddress).index()); QCOMPARE(int(sender.writeDatagram(dgram)), @@ -1600,7 +1598,8 @@ void tst_QUdpSocket::linkLocalIPv6() QList <QHostAddress> addresses; QSet <QString> scopes; QHostAddress localMask("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 if (iface.flags() & QNetworkInterface::IsUp) { @@ -1615,7 +1614,8 @@ void tst_QUdpSocket::linkLocalIPv6() continue; #endif - foreach (QNetworkAddressEntry addressEntry, iface.addressEntries()) { + const auto entries = iface.addressEntries(); + for (const QNetworkAddressEntry &addressEntry : entries) { QHostAddress addr(addressEntry.ip()); if (!addr.scopeId().isEmpty() && addr.isInSubnet(localMask, 64)) { scopes << addr.scopeId(); @@ -1630,7 +1630,7 @@ void tst_QUdpSocket::linkLocalIPv6() QList <QUdpSocket*> sockets; quint16 port = 0; - foreach (const QHostAddress& addr, addresses) { + for (const QHostAddress &addr : std::as_const(addresses)) { QUdpSocket *s = new QUdpSocket; QVERIFY2(s->bind(addr, port), addr.toString().toLatin1() + '/' + QByteArray::number(port) + ": " + qPrintable(s->errorString())); @@ -1639,7 +1639,7 @@ void tst_QUdpSocket::linkLocalIPv6() } QByteArray testData("hello"); - foreach (QUdpSocket *s, sockets) { + for (QUdpSocket *s : std::as_const(sockets)) { QUdpSocket neutral; QVERIFY(neutral.bind(QHostAddress(QHostAddress::AnyIPv6))); QSignalSpy neutralReadSpy(&neutral, SIGNAL(readyRead())); @@ -1665,9 +1665,8 @@ void tst_QUdpSocket::linkLocalIPv6() QCOMPARE(dgram.data(), testData); //sockets bound to other interfaces shouldn't have received anything - foreach (QUdpSocket *s2, sockets) { + for (QUdpSocket *s2 : std::as_const(sockets)) QCOMPARE((int)s2->bytesAvailable(), 0); - } //Sending to the same address with different scope should normally fail //However it will pass if there is a route between two interfaces, @@ -1686,7 +1685,8 @@ void tst_QUdpSocket::linkLocalIPv4() QList <QHostAddress> addresses; QHostAddress localMask("169.254.0.0"); - 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) { @@ -1700,7 +1700,8 @@ void tst_QUdpSocket::linkLocalIPv4() if (iface.name().startsWith("utun")) continue; #endif - foreach (QNetworkAddressEntry addr, iface.addressEntries()) { + const auto entries = iface.addressEntries(); + for (const QNetworkAddressEntry &addr : entries) { if (addr.ip().isInSubnet(localMask, 16)) { addresses << addr.ip(); qDebug() << "Found IPv4 link local address" << addr.ip(); @@ -1713,7 +1714,7 @@ void tst_QUdpSocket::linkLocalIPv4() QList <QUdpSocket*> sockets; quint16 port = 0; - foreach (const QHostAddress& addr, addresses) { + for (const QHostAddress &addr : std::as_const(addresses)) { QUdpSocket *s = new QUdpSocket; QVERIFY2(s->bind(addr, port), qPrintable(s->errorString())); port = s->localPort(); //bind same port, different networks @@ -1724,7 +1725,7 @@ void tst_QUdpSocket::linkLocalIPv4() QVERIFY(neutral.bind(QHostAddress(QHostAddress::AnyIPv4))); QByteArray testData("hello"); - foreach (QUdpSocket *s, sockets) { + for (QUdpSocket *s : std::as_const(sockets)) { QVERIFY(s->writeDatagram(testData, s->localAddress(), neutral.localPort())); QVERIFY2(neutral.waitForReadyRead(10000), QtNetworkSettings::msgSocketError(neutral).constData()); @@ -1756,9 +1757,8 @@ void tst_QUdpSocket::linkLocalIPv4() QCOMPARE(dgram.data(), testData); //sockets bound to other interfaces shouldn't have received anything - foreach (QUdpSocket *s2, sockets) { + for (QUdpSocket *s2 : std::as_const(sockets)) QCOMPARE((int)s2->bytesAvailable(), 0); - } } qDeleteAll(sockets); } @@ -1908,5 +1908,78 @@ void tst_QUdpSocket::writeInHostLookupState() QVERIFY(!socket.putChar('0')); } +void tst_QUdpSocket::readyReadConnectionThrottling() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + using namespace std::chrono_literals; + + // QTBUG-105871: + // We have some signal/slot connection throttling in QAbstractSocket, but it + // was caring about the bytes, not about the datagrams. + // Test that we don't disable read notifications until we have at least one + // datagram available. Otherwise our good users who use the datagram APIs + // can get into scenarios where they no longer get the readyRead signal + // unless they call a read function once in a while. + + QUdpSocket receiver; + QVERIFY(receiver.bind(QHostAddress(QHostAddress::LocalHost), 0)); + + QSemaphore semaphore; + + // Repro-ing deterministically eludes me, so we are bruteforcing it: + // The thread acts as a remote sender, flooding the receiver with datagrams, + // and at some point the receiver would get into the broken state mentioned + // earlier. + std::unique_ptr<QThread> thread(QThread::create([&semaphore, port = receiver.localPort()]() { + QUdpSocket sender; + sender.connectToHost(QHostAddress(QHostAddress::LocalHost), port); + QCOMPARE(sender.state(), QUdpSocket::ConnectedState); + + constexpr qsizetype PayloadSize = 242; + const QByteArray payload(PayloadSize, 'a'); + + semaphore.acquire(); // Wait for main thread to be ready + while (true) { + // We send 100 datagrams at a time, then sleep. + // This is mostly to let the main thread catch up between bursts so + // it doesn't get stuck in the loop. + for (int i = 0; i < 100; ++i) { + [[maybe_unused]] + qsizetype sent = sender.write(payload); + Q_ASSERT(sent > 0); + } + if (QThread::currentThread()->isInterruptionRequested()) + break; + QThread::sleep(20ms); + } + })); + thread->start(); + auto threadStopAndWaitGuard = qScopeGuard([&thread] { + thread->requestInterruption(); + thread->quit(); + thread->wait(); + }); + + qsizetype count = 0; + QObject::connect(&receiver, &QUdpSocket::readyRead, &receiver, + [&] { + while (receiver.hasPendingDatagrams()) { + receiver.readDatagram(nullptr, 0); + ++count; + } + // If this prints `false, xxxx` we were pretty much guaranteed + // that we would not get called again: + // qDebug() << receiver.hasPendingDatagrams() << receiver.bytesAvailable(); + }, + Qt::QueuedConnection); + + semaphore.release(); + constexpr qsizetype MaxCount = 500; + QVERIFY2(QTest::qWaitFor([&] { return count >= MaxCount; }, 10s), + QByteArray::number(count).constData()); +} + QTEST_MAIN(tst_QUdpSocket) #include "tst_qudpsocket.moc" diff --git a/tests/auto/network/socket/qudpsocket/udpServer/main.cpp b/tests/auto/network/socket/qudpsocket/udpServer/main.cpp index 72bbcfa71e..ba5bb3c0d0 100644 --- a/tests/auto/network/socket/qudpsocket/udpServer/main.cpp +++ b/tests/auto/network/socket/qudpsocket/udpServer/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 <QtNetwork> class Server : public QObject |