Autotest: adapt to certain OSes always allowing binding to low ports

Apple changed on macOS 10.14 (Mojave). Windows has always allowed.

Fixes: QTBUG-81905
Change-Id: I572733186b73423b89e5fffd15f12fee3f03c055
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Thiago Macieira 2020-02-07 09:47:24 -08:00
parent 5ff37f13ee
commit 210fd52e0d
3 changed files with 33 additions and 12 deletions

View File

@ -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()

View File

@ -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));

View File

@ -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()