From 210fd52e0d5e77105c676c064e7c3a69a00e2122 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 7 Feb 2020 09:47:24 -0800 Subject: [PATCH] Autotest: adapt to certain OSes always allowing binding to low ports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apple changed on macOS 10.14 (Mojave). Windows has always allowed. Fixes: QTBUG-81905 Change-Id: I572733186b73423b89e5fffd15f12fee3f03c055 Reviewed-by: Tor Arne Vestbø --- tests/auto/network-settings.h | 16 ++++++++++++++++ .../tst_platformsocketengine.cpp | 13 +++++++++---- .../network/socket/qtcpsocket/tst_qtcpsocket.cpp | 16 ++++++++-------- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/tests/auto/network-settings.h b/tests/auto/network-settings.h index 77298342af..4dd6ce897b 100644 --- a/tests/auto/network-settings.h +++ b/tests/auto/network-settings.h @@ -134,6 +134,22 @@ public: return true; } + static bool canBindToLowPorts() + { +#ifdef Q_OS_UNIX + if (geteuid() == 0) + return true; + if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSMojave) + return true; + // ### Which versions of iOS, watchOS and such does Apple's opening of + // all ports apply to? + return false; +#else + // Windows + return true; +#endif + } + #ifdef QT_NETWORK_LIB static bool verifyTestNetworkSettings() diff --git a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp index 5be00630ca..68c913ecfc 100644 --- a/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp +++ b/tests/auto/network/socket/platformsocketengine/tst_platformsocketengine.cpp @@ -529,12 +529,11 @@ void tst_PlatformSocketEngine::tooManySockets() //--------------------------------------------------------------------------- void tst_PlatformSocketEngine::bind() { -#if !defined Q_OS_WIN PLATFORMSOCKETENGINE binder; QVERIFY(binder.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); - QVERIFY(!binder.bind(QHostAddress::AnyIPv4, 82)); - QCOMPARE(binder.error(), QAbstractSocket::SocketAccessError); -#endif + QCOMPARE(binder.bind(QHostAddress::AnyIPv4, 82), QtNetworkSettings::canBindToLowPorts()); + if (!QtNetworkSettings::canBindToLowPorts()) + QCOMPARE(binder.error(), QAbstractSocket::SocketAccessError); PLATFORMSOCKETENGINE binder2; QVERIFY(binder2.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv4Protocol)); @@ -546,6 +545,12 @@ void tst_PlatformSocketEngine::bind() QCOMPARE(binder3.error(), QAbstractSocket::AddressInUseError); if (QtNetworkSettings::hasIPv6()) { + PLATFORMSOCKETENGINE binder; + QVERIFY(binder.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv6Protocol)); + QCOMPARE(binder.bind(QHostAddress::AnyIPv6, 82), QtNetworkSettings::canBindToLowPorts()); + if (!QtNetworkSettings::canBindToLowPorts()) + QCOMPARE(binder.error(), QAbstractSocket::SocketAccessError); + PLATFORMSOCKETENGINE binder4; QVERIFY(binder4.initialize(QAbstractSocket::TcpSocket, QAbstractSocket::IPv6Protocol)); QVERIFY(binder4.bind(QHostAddress::AnyIPv6, 31180)); diff --git a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp index 9374420f34..5b3a17bdd7 100644 --- a/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/network/socket/qtcpsocket/tst_qtcpsocket.cpp @@ -523,7 +523,7 @@ void tst_QTcpSocket::bind_data() continue; // link-local bind will fail, at least on Linux, so skip it. QString ip(entry.ip().toString()); - QTest::newRow(ip.toLatin1().constData()) << ip << 0 << true << ip; + QTest::addRow("%s:0", ip.toLatin1().constData()) << ip << 0 << true << ip; if (!testIpv6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) testIpv6 = true; @@ -531,9 +531,9 @@ void tst_QTcpSocket::bind_data() } // test binding to localhost - QTest::newRow("0.0.0.0") << "0.0.0.0" << 0 << true << "0.0.0.0"; + QTest::newRow("0.0.0.0:0") << "0.0.0.0" << 0 << true << "0.0.0.0"; if (testIpv6) - QTest::newRow("[::]") << "::" << 0 << true << "::"; + QTest::newRow("[::]:0") << "::" << 0 << true << "::"; // and binding with a port number... // Since we want to test that we got the port number we asked for, we need a random port number. @@ -551,16 +551,16 @@ void tst_QTcpSocket::bind_data() knownBad << "198.51.100.1"; knownBad << "2001:0DB8::1"; foreach (const QString &badAddress, knownBad) { - QTest::newRow(badAddress.toLatin1().constData()) << badAddress << 0 << false << QString(); + QTest::addRow("%s:0", badAddress.toLatin1().constData()) << badAddress << 0 << false << QString(); } -#ifdef Q_OS_UNIX // try to bind to a privileged ports // we should fail if we're not root (unless the ports are in use!) - QTest::newRow("127.0.0.1:1") << "127.0.0.1" << 1 << !geteuid() << (geteuid() ? QString() : "127.0.0.1"); + QTest::newRow("127.0.0.1:1") << "127.0.0.1" << 1 << QtNetworkSettings::canBindToLowPorts() + << (QtNetworkSettings::canBindToLowPorts() ? "127.0.0.1" : QString()); if (testIpv6) - QTest::newRow("[::]:1") << "::" << 1 << !geteuid() << (geteuid() ? QString() : "::"); -#endif + QTest::newRow("[::]:1") << "::" << 1 << QtNetworkSettings::canBindToLowPorts() + << (QtNetworkSettings::canBindToLowPorts() ? "::" : QString()); } void tst_QTcpSocket::bind()