summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/network/kernel')
-rw-r--r--tests/auto/network/kernel/CMakeLists.txt27
-rw-r--r--tests/auto/network/kernel/kernel.pro24
-rw-r--r--tests/auto/network/kernel/qauthenticator/CMakeLists.txt23
-rw-r--r--tests/auto/network/kernel/qauthenticator/qauthenticator.pro6
-rw-r--r--tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp105
-rw-r--r--tests/auto/network/kernel/qdnslookup/BLACKLIST2
-rw-r--r--tests/auto/network/kernel/qdnslookup/CMakeLists.txt25
-rw-r--r--tests/auto/network/kernel/qdnslookup/qdnslookup.pro7
-rw-r--r--tests/auto/network/kernel/qdnslookup/tst_qdnslookup.cpp935
-rw-r--r--tests/auto/network/kernel/qdnslookup_appless/CMakeLists.txt19
-rw-r--r--tests/auto/network/kernel/qdnslookup_appless/qdnslookup_appless.pro7
-rw-r--r--tests/auto/network/kernel/qdnslookup_appless/tst_qdnslookup_appless.cpp32
-rw-r--r--tests/auto/network/kernel/qhostaddress/CMakeLists.txt27
-rw-r--r--tests/auto/network/kernel/qhostaddress/qhostaddress.pro7
-rw-r--r--tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp84
-rw-r--r--tests/auto/network/kernel/qhostinfo/CMakeLists.txt32
-rw-r--r--tests/auto/network/kernel/qhostinfo/qhostinfo.pro11
-rw-r--r--tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp433
-rw-r--r--tests/auto/network/kernel/qnetworkaddressentry/CMakeLists.txt19
-rw-r--r--tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro5
-rw-r--r--tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp35
-rw-r--r--tests/auto/network/kernel/qnetworkdatagram/CMakeLists.txt19
-rw-r--r--tests/auto/network/kernel/qnetworkdatagram/qnetworkdatagram.pro4
-rw-r--r--tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp44
-rw-r--r--tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt15
-rw-r--r--tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp255
-rw-r--r--tests/auto/network/kernel/qnetworkinformation_appless/CMakeLists.txt15
-rw-r--r--tests/auto/network/kernel/qnetworkinformation_appless/tst_qnetworkinformation_appless.cpp42
-rw-r--r--tests/auto/network/kernel/qnetworkinterface/BLACKLIST4
-rw-r--r--tests/auto/network/kernel/qnetworkinterface/CMakeLists.txt20
-rw-r--r--tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro7
-rw-r--r--tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp174
-rw-r--r--tests/auto/network/kernel/qnetworkproxy/CMakeLists.txt19
-rw-r--r--tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro9
-rw-r--r--tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp35
-rw-r--r--tests/auto/network/kernel/qnetworkproxyfactory/CMakeLists.txt19
-rw-r--r--tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro9
-rw-r--r--tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp57
38 files changed, 1633 insertions, 979 deletions
diff --git a/tests/auto/network/kernel/CMakeLists.txt b/tests/auto/network/kernel/CMakeLists.txt
new file mode 100644
index 0000000000..df87e9d58e
--- /dev/null
+++ b/tests/auto/network/kernel/CMakeLists.txt
@@ -0,0 +1,27 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+if(QT_FEATURE_dnslookup AND (QT_FEATURE_libresolv OR WIN32))
+ add_subdirectory(qdnslookup)
+ add_subdirectory(qdnslookup_appless)
+endif()
+if(QT_FEATURE_networkinterface)
+ add_subdirectory(qnetworkaddressentry)
+ add_subdirectory(qnetworkproxyfactory)
+ add_subdirectory(qnetworkinterface)
+endif()
+if(QT_FEATURE_networkproxy)
+ add_subdirectory(qnetworkproxy)
+endif()
+if(QT_FEATURE_udpsocket)
+ add_subdirectory(qnetworkdatagram)
+endif()
+add_subdirectory(qhostaddress)
+if(QT_FEATURE_private_tests AND NOT MACOS AND NOT INTEGRITY)
+ add_subdirectory(qhostinfo)
+endif()
+if(QT_FEATURE_private_tests)
+ add_subdirectory(qauthenticator)
+ add_subdirectory(qnetworkinformation)
+ add_subdirectory(qnetworkinformation_appless)
+endif()
diff --git a/tests/auto/network/kernel/kernel.pro b/tests/auto/network/kernel/kernel.pro
deleted file mode 100644
index 42df80dfa1..0000000000
--- a/tests/auto/network/kernel/kernel.pro
+++ /dev/null
@@ -1,24 +0,0 @@
-TEMPLATE=subdirs
-SUBDIRS=\
- qdnslookup \
- qdnslookup_appless \
- qhostinfo \
- qnetworkproxyfactory \
- qauthenticator \
- qnetworkproxy \
- qnetworkinterface \
- qnetworkdatagram \
- qnetworkaddressentry \
- qhostaddress \
-
-winrt: SUBDIRS -= \
- qnetworkproxy \
- qnetworkproxyfactory \
-
-osx: SUBDIRS -= \ # QTBUG-41847
- qhostinfo \
-
-!qtConfig(private_tests): SUBDIRS -= \
- qauthenticator \
- qhostinfo \
-
diff --git a/tests/auto/network/kernel/qauthenticator/CMakeLists.txt b/tests/auto/network/kernel/qauthenticator/CMakeLists.txt
new file mode 100644
index 0000000000..552e9065ed
--- /dev/null
+++ b/tests/auto/network/kernel/qauthenticator/CMakeLists.txt
@@ -0,0 +1,23 @@
+# 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_qauthenticator LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if(NOT QT_FEATURE_private_tests)
+ return()
+endif()
+
+#####################################################################
+## tst_qauthenticator Test:
+#####################################################################
+
+qt_internal_add_test(tst_qauthenticator
+ SOURCES
+ tst_qauthenticator.cpp
+ LIBRARIES
+ Qt::NetworkPrivate
+)
diff --git a/tests/auto/network/kernel/qauthenticator/qauthenticator.pro b/tests/auto/network/kernel/qauthenticator/qauthenticator.pro
deleted file mode 100644
index 5038eea9af..0000000000
--- a/tests/auto/network/kernel/qauthenticator/qauthenticator.pro
+++ /dev/null
@@ -1,6 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qauthenticator
-requires(qtConfig(private_tests))
-QT = core network-private testlib
-SOURCES += tst_qauthenticator.cpp
-DEFINES += SRCDIR=\\\"$$PWD/\\\"
diff --git a/tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp b/tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp
index 55053842dc..1cd1b6a63b 100644
--- a/tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp
+++ b/tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp
@@ -1,36 +1,12 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the FOO module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QString>
-#include <QtTest/QtTest>
+#include <QTest>
#include <QtCore/QCoreApplication>
#include <QtNetwork/QAuthenticator>
+#include <QtNetwork/QHttpHeaders>
#include <private/qauthenticator_p.h>
@@ -48,7 +24,11 @@ private Q_SLOTS:
void ntlmAuth_data();
void ntlmAuth();
+ void sha256AndMd5Digest();
+
void equalityOperators();
+
+ void isMethodSupported();
};
tst_QAuthenticator::tst_QAuthenticator()
@@ -81,9 +61,9 @@ void tst_QAuthenticator::basicAuth()
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth);
QCOMPARE(priv->phase, QAuthenticatorPrivate::Start);
- QList<QPair<QByteArray, QByteArray> > headers;
- headers << qMakePair<QByteArray, QByteArray>(QByteArray("WWW-Authenticate"), "Basic " + data.toUtf8());
- priv->parseHttpResponse(headers, /*isProxy = */ false);
+ QHttpHeaders headers;
+ headers.append(QByteArray("WWW-Authenticate"), "Basic " + data.toUtf8());
+ priv->parseHttpResponse(headers, /*isProxy = */ false, {});
QCOMPARE(auth.realm(), realm);
QCOMPARE(auth.option("realm").toString(), realm);
@@ -93,7 +73,7 @@ void tst_QAuthenticator::basicAuth()
QCOMPARE(priv->phase, QAuthenticatorPrivate::Start);
- QCOMPARE(priv->calculateResponse("GET", "/").constData(), QByteArray("Basic " + expectedReply).constData());
+ QCOMPARE(priv->calculateResponse("GET", "/", u"").constData(), QByteArray("Basic " + expectedReply).constData());
}
void tst_QAuthenticator::ntlmAuth_data()
@@ -124,29 +104,58 @@ void tst_QAuthenticator::ntlmAuth()
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth);
QCOMPARE(priv->phase, QAuthenticatorPrivate::Start);
- QList<QPair<QByteArray, QByteArray> > headers;
+ QHttpHeaders headers;
// NTLM phase 1: negotiate
// This phase of NTLM contains no information, other than what we're willing to negotiate
// Current implementation uses flags:
// NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_REQUEST_TARGET
- headers << qMakePair<QByteArray, QByteArray>("WWW-Authenticate", "NTLM");
- priv->parseHttpResponse(headers, /*isProxy = */ false);
+ headers.append(QByteArrayLiteral("WWW-Authenticate"), QByteArrayLiteral("NTLM"));
+ priv->parseHttpResponse(headers, /*isProxy = */ false, {});
if (sso)
- QVERIFY(priv->calculateResponse("GET", "/").startsWith("NTLM "));
+ QVERIFY(priv->calculateResponse("GET", "/", u"").startsWith("NTLM "));
else
- QCOMPARE(priv->calculateResponse("GET", "/").constData(), "NTLM TlRMTVNTUAABAAAABYIIAAAAAAAAAAAAAAAAAAAAAAA=");
+ QCOMPARE(priv->calculateResponse("GET", "/", u"").constData(), "NTLM TlRMTVNTUAABAAAABYIIAAAAAAAAAAAAAAAAAAAAAAA=");
// NTLM phase 2: challenge
headers.clear();
- headers << qMakePair<QByteArray, QByteArray>(QByteArray("WWW-Authenticate"), "NTLM " + data.toUtf8());
- priv->parseHttpResponse(headers, /*isProxy = */ false);
+ headers.append(QByteArray("WWW-Authenticate"), "NTLM " + data.toUtf8());
+ priv->parseHttpResponse(headers, /*isProxy = */ false, {});
QEXPECT_FAIL("with-realm", "NTLM authentication code doesn't extract the realm", Continue);
QEXPECT_FAIL("with-realm-sso", "NTLM authentication code doesn't extract the realm", Continue);
QCOMPARE(auth.realm(), realm);
- QVERIFY(priv->calculateResponse("GET", "/").startsWith("NTLM "));
+ QVERIFY(priv->calculateResponse("GET", "/", u"").startsWith("NTLM "));
+}
+
+// We don't (currently) support SHA256. So, when presented with the option of MD5 or SHA256,
+// we should always pick MD5.
+void tst_QAuthenticator::sha256AndMd5Digest()
+{
+ QByteArray md5 = "Digest realm=\"\", nonce=\"\", algorithm=MD5, qop=\"auth\"";
+ QByteArray sha256 = "Digest realm=\"\", nonce=\"\", algorithm=SHA-256, qop=\"auth\"";
+
+ QAuthenticator auth;
+ auth.setUser("unimportant");
+ auth.setPassword("unimportant");
+
+ QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth);
+ QVERIFY(priv->isMethodSupported("digest")); // sanity check
+
+ QCOMPARE(priv->phase, QAuthenticatorPrivate::Start);
+ QHttpHeaders headers;
+ // Put sha256 first, so that its parsed first...
+ headers.append("WWW-Authenticate", sha256);
+ headers.append("WWW-Authenticate", md5);
+ priv->parseHttpResponse(headers, false, QString());
+
+ QByteArray response = priv->calculateResponse("GET", "/index", {});
+ QCOMPARE(priv->phase, QAuthenticatorPrivate::Done);
+
+ QVERIFY(!response.isEmpty());
+ QVERIFY(!response.contains("algorithm=SHA-256"));
+ QVERIFY(response.contains("algorithm=MD5"));
}
void tst_QAuthenticator::equalityOperators()
@@ -163,6 +172,22 @@ void tst_QAuthenticator::equalityOperators()
QVERIFY(s2 != s1);
}
+void tst_QAuthenticator::isMethodSupported()
+{
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("basic"));
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("Basic realm=\"Shadow\""));
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("DIgesT"));
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("NTLM"));
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("ntlm"));
+#if QT_CONFIG(sspi) || QT_CONFIG(gssapi)
+ QVERIFY(QAuthenticatorPrivate::isMethodSupported("negotiate"));
+#else
+ QVERIFY(!QAuthenticatorPrivate::isMethodSupported("negotiate"));
+#endif
+
+ QVERIFY(!QAuthenticatorPrivate::isMethodSupported("Bearer"));
+}
+
QTEST_MAIN(tst_QAuthenticator);
#include "tst_qauthenticator.moc"
diff --git a/tests/auto/network/kernel/qdnslookup/BLACKLIST b/tests/auto/network/kernel/qdnslookup/BLACKLIST
deleted file mode 100644
index edf6436384..0000000000
--- a/tests/auto/network/kernel/qdnslookup/BLACKLIST
+++ /dev/null
@@ -1,2 +0,0 @@
-[lookup]
-ci
diff --git a/tests/auto/network/kernel/qdnslookup/CMakeLists.txt b/tests/auto/network/kernel/qdnslookup/CMakeLists.txt
new file mode 100644
index 0000000000..ea539ecbe0
--- /dev/null
+++ b/tests/auto/network/kernel/qdnslookup/CMakeLists.txt
@@ -0,0 +1,25 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qdnslookup Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qdnslookup LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qdnslookup
+ SOURCES
+ tst_qdnslookup.cpp
+ LIBRARIES
+ Qt::Network
+ Qt::TestPrivate
+)
+
+qt_internal_extend_target(tst_qdnslookup CONDITION WIN32
+ LIBRARIES
+ iphlpapi
+)
diff --git a/tests/auto/network/kernel/qdnslookup/qdnslookup.pro b/tests/auto/network/kernel/qdnslookup/qdnslookup.pro
deleted file mode 100644
index f14ffd003e..0000000000
--- a/tests/auto/network/kernel/qdnslookup/qdnslookup.pro
+++ /dev/null
@@ -1,7 +0,0 @@
-CONFIG += testcase
-
-TARGET = tst_qdnslookup
-
-SOURCES += tst_qdnslookup.cpp
-
-QT = core network testlib
diff --git a/tests/auto/network/kernel/qdnslookup/tst_qdnslookup.cpp b/tests/auto/network/kernel/qdnslookup/tst_qdnslookup.cpp
index d72e92b859..af3a74a498 100644
--- a/tests/auto/network/kernel/qdnslookup/tst_qdnslookup.cpp
+++ b/tests/auto/network/kernel/qdnslookup/tst_qdnslookup.cpp
@@ -1,61 +1,250 @@
-/****************************************************************************
-**
-** Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
+#include <QSignalSpy>
+#include <QtTest/private/qpropertytesthelper_p.h>
+
#include <QtNetwork/QDnsLookup>
+
+#include <QtCore/QRandomGenerator>
#include <QtNetwork/QHostAddress>
+#include <QtNetwork/QNetworkDatagram>
+#include <QtNetwork/QUdpSocket>
+
+#if QT_CONFIG(networkproxy)
+# include <QtNetwork/QNetworkProxyFactory>
+#endif
+#if QT_CONFIG(ssl)
+# include <QtNetwork/QSslSocket>
+#endif
+
+#ifdef Q_OS_UNIX
+# include <QtCore/QFile>
+#else
+# include <winsock2.h>
+# include <iphlpapi.h>
+#endif
+using namespace Qt::StringLiterals;
static const int Timeout = 15000; // 15s
class tst_QDnsLookup: public QObject
{
Q_OBJECT
+ const QString normalDomain = u".test.qt-project.org"_s;
+ const QString idnDomain = u".alqualondë.test.qt-project.org"_s;
+ bool usingIdnDomain = false;
+ bool dnsServersMustWork = false;
+
QString domainName(const QString &input);
QString domainNameList(const QString &input);
QStringList domainNameListAlternatives(const QString &input);
+
+ std::unique_ptr<QDnsLookup> lookupCommon(QDnsLookup::Type type, const QString &domain,
+ const QHostAddress &server = {}, quint16 port = 0,
+ QDnsLookup::Protocol protocol = QDnsLookup::Standard);
+ QStringList formatReply(const QDnsLookup *lookup) const;
+
+ void setNameserver_helper(QDnsLookup::Protocol protocol);
public slots:
void initTestCase();
private slots:
+ void lookupLocalhost();
+ void lookupRoot();
+ void lookupNxDomain_data();
+ void lookupNxDomain();
void lookup_data();
void lookup();
+ void lookupIdn_data() { lookup_data(); }
+ void lookupIdn();
+
void lookupReuse();
void lookupAbortRetry();
+ void setNameserverLoopback();
+ void setNameserver_data();
+ void setNameserver();
+ void dnsOverTls_data();
+ void dnsOverTls();
+ void bindingsAndProperties();
+ void automatedBindings();
};
+static constexpr qsizetype HeaderSize = 6 * sizeof(quint16);
+static const char preparedDnsQuery[] =
+ // header
+ "\x00\x00" // transaction ID, we'll replace
+ "\x01\x20" // flags
+ "\x00\x01" // qdcount
+ "\x00\x00" // ancount
+ "\x00\x00" // nscount
+ "\x00\x00" // arcount
+ // query:
+ "\x00\x00\x06\x00\x01" // <root domain> IN SOA
+ ;
+
+static QList<QHostAddress> systemNameservers(QDnsLookup::Protocol protocol)
+{
+ QList<QHostAddress> result;
+ if (protocol != QDnsLookup::Standard)
+ return result;
+
+#ifdef Q_OS_WIN
+ ULONG infosize = 0;
+ DWORD r = GetNetworkParams(nullptr, &infosize);
+ auto buffer = std::make_unique<uchar[]>(infosize);
+ auto info = new (buffer.get()) FIXED_INFO;
+ r = GetNetworkParams(info, &infosize);
+ if (r == NO_ERROR) {
+ for (PIP_ADDR_STRING ptr = &info->DnsServerList; ptr; ptr = ptr->Next) {
+ QLatin1StringView addr(ptr->IpAddress.String);
+ result.emplaceBack(addr);
+ }
+ }
+#else
+ auto parseFile = [&](QLatin1StringView path) {
+ QFile f(path);
+ if (!f.open(QIODevice::ReadOnly))
+ return;
+
+ while (!f.atEnd()) {
+ static const char command[] = "nameserver";
+ QByteArray line = f.readLine().simplified();
+ if (!line.startsWith(command))
+ continue;
+
+ QString addr = QLatin1StringView(line).mid(sizeof(command));
+ result.emplaceBack(addr);
+ }
+ };
+ parseFile("/etc/resolv.conf"_L1);
+ parseFile("/run/systemd/resolve/resolv.conf"_L1);
+#endif
+
+ return result;
+}
+
+static QList<QHostAddress> globalPublicNameservers(QDnsLookup::Protocol proto)
+{
+ const char *const candidates[] = {
+ // Google's dns.google
+ "8.8.8.8", "2001:4860:4860::8888",
+ //"8.8.4.4", "2001:4860:4860::8844",
+
+ // CloudFare's one.one.one.one
+ "1.1.1.1", "2606:4700:4700::1111",
+ //"1.0.0.1", "2606:4700:4700::1001",
+
+ // Quad9's dns9
+ //"9.9.9.9", "2620:fe::9",
+ };
+
+ auto udpSendAndReceive = [](const QHostAddress &addr, QByteArray &data) {
+ QUdpSocket socket;
+ socket.connectToHost(addr, 53);
+ if (socket.waitForConnected(1))
+ socket.write(data);
+
+ if (!socket.waitForReadyRead(1000))
+ return socket.errorString();
+
+ QNetworkDatagram dgram = socket.receiveDatagram();
+ if (!dgram.isValid())
+ return socket.errorString();
+
+ data = dgram.data();
+ return QString();
+ };
+
+ auto tlsSendAndReceive = [](const QHostAddress &addr, QByteArray &data) {
+#if QT_CONFIG(ssl)
+ QSslSocket socket;
+ QDeadlineTimer timeout(2000);
+ socket.connectToHostEncrypted(addr.toString(), 853);
+ if (!socket.waitForEncrypted(2000))
+ return socket.errorString();
+
+ quint16 size = qToBigEndian<quint16>(data.size());
+ socket.write(reinterpret_cast<char *>(&size), sizeof(size));
+ socket.write(data);
+
+ if (!socket.waitForReadyRead(timeout.remainingTime()))
+ return socket.errorString();
+ if (socket.bytesAvailable() < 2)
+ return u"protocol error"_s;
+
+ socket.read(reinterpret_cast<char *>(&size), sizeof(size));
+ size = qFromBigEndian(size);
+
+ while (socket.bytesAvailable() < size) {
+ int remaining = timeout.remainingTime();
+ if (remaining < 0 || !socket.waitForReadyRead(remaining))
+ return socket.errorString();
+ }
+
+ data = socket.readAll();
+ return QString();
+#else
+ return u"SSL/TLS support not compiled in"_s;
+#endif
+ };
+
+ QList<QHostAddress> result;
+ QRandomGenerator &rng = *QRandomGenerator::system();
+ for (auto name : candidates) {
+ // check the candidates for reachability
+ QHostAddress addr{QLatin1StringView(name)};
+ quint16 id = quint16(rng());
+ QByteArray data(preparedDnsQuery, sizeof(preparedDnsQuery));
+ char *ptr = data.data();
+ qToBigEndian(id, ptr);
+
+ QString errorString = [&] {
+ switch (proto) {
+ case QDnsLookup::Standard: return udpSendAndReceive(addr, data);
+ case QDnsLookup::DnsOverTls: return tlsSendAndReceive(addr, data);
+ }
+ Q_UNREACHABLE();
+ }();
+ if (!errorString.isEmpty()) {
+ qDebug() << addr << "discarded:" << errorString;
+ continue;
+ }
+
+ ptr = data.data();
+ if (data.size() < HeaderSize) {
+ qDebug() << addr << "discarded: reply too small";
+ continue;
+ }
+
+ bool ok = qFromBigEndian<quint16>(ptr) == id
+ && (ptr[2] & 0x80) // is a reply
+ && (ptr[3] & 0xf) == 0 // rcode NOERROR
+ && qFromBigEndian<quint16>(ptr + 4) == 1 // qdcount
+ && qFromBigEndian<quint16>(ptr + 6) >= 1; // ancount
+ if (!ok) {
+ qDebug() << addr << "discarded: invalid reply";
+ continue;
+ }
+
+ result.emplaceBack(std::move(addr));
+ }
+
+ return result;
+}
+
void tst_QDnsLookup::initTestCase()
{
- QTest::addColumn<QString>("tld");
- QTest::newRow("normal") << ".test.qt-project.org";
- QTest::newRow("idn") << ".alqualond\xc3\xab.test.qt-project.org";
+ if (qgetenv("QTEST_ENVIRONMENT") == "ci")
+ dnsServersMustWork = true;
+
+#if QT_CONFIG(networkproxy)
+ // for DNS-over-TLS
+ QNetworkProxyFactory::setUseSystemConfiguration(true);
+#endif
}
QString tst_QDnsLookup::domainName(const QString &input)
@@ -69,15 +258,16 @@ QString tst_QDnsLookup::domainName(const QString &input)
return nodot;
}
- QFETCH_GLOBAL(QString, tld);
- return input + tld;
+ if (usingIdnDomain)
+ return input + idnDomain;
+ return input + normalDomain;
}
QString tst_QDnsLookup::domainNameList(const QString &input)
{
- QStringList list = input.split(QLatin1Char(';'));
+ const QStringList list = input.split(QLatin1Char(';'));
QString result;
- foreach (const QString &s, list) {
+ for (const QString &s : list) {
if (!result.isEmpty())
result += ';';
result += domainName(s);
@@ -88,229 +278,289 @@ QString tst_QDnsLookup::domainNameList(const QString &input)
QStringList tst_QDnsLookup::domainNameListAlternatives(const QString &input)
{
QStringList alternatives = input.split('|');
- for (int i = 0; i < alternatives.length(); ++i)
+ for (int i = 0; i < alternatives.size(); ++i)
alternatives[i] = domainNameList(alternatives[i]);
return alternatives;
}
+std::unique_ptr<QDnsLookup>
+tst_QDnsLookup::lookupCommon(QDnsLookup::Type type, const QString &domain,
+ const QHostAddress &server, quint16 port,
+ QDnsLookup::Protocol protocol)
+{
+ auto lookup = std::make_unique<QDnsLookup>(type, domainName(domain), protocol, server, port);
+ QObject::connect(lookup.get(), &QDnsLookup::finished,
+ &QTestEventLoop::instance(), &QTestEventLoop::exitLoop);
+ lookup->lookup();
+ QTestEventLoop::instance().enterLoopMSecs(Timeout);
+
+ QDnsLookup::Error error = lookup->error();
+ if (QTestEventLoop::instance().timeout())
+ error = QDnsLookup::TimeoutError;
+
+ if (!dnsServersMustWork && (error == QDnsLookup::ServerFailureError
+ || error == QDnsLookup::ServerRefusedError
+ || error == QDnsLookup::TimeoutError)) {
+ // It's not a QDnsLookup problem if the server refuses to answer the query.
+ // This happens for queries of type ANY through Dnsmasq, for example.
+ [&] {
+ auto me = QMetaEnum::fromType<QDnsLookup::Type>();
+ QString msg = u"Server refused or was unable to answer query; %1 type %3: %2"_s
+ .arg(domain, lookup->errorString(), QString(me.valueToKey(int(type))));
+ QSKIP(msg.toLocal8Bit());
+ }();
+ return {};
+ }
+
+ return lookup;
+}
+
+QStringList tst_QDnsLookup::formatReply(const QDnsLookup *lookup) const
+{
+ QStringList result;
+ QString domain = lookup->name();
+
+ auto shorter = [this](QString value) {
+ const QString &ending = usingIdnDomain ? idnDomain : normalDomain;
+ if (value.endsWith(ending))
+ value.chop(ending.size());
+ else
+ value += u'.';
+ return value;
+ };
+
+ for (const QDnsMailExchangeRecord &rr : lookup->mailExchangeRecords()) {
+ QString entry = u"MX %1 %2"_s.arg(rr.preference(), 5).arg(shorter(rr.exchange()));
+ if (rr.name() != domain)
+ entry = "MX unexpected label to "_L1 + rr.name();
+ result.append(std::move(entry));
+ }
+
+ for (const QDnsServiceRecord &rr : lookup->serviceRecords()) {
+ QString entry = u"SRV %1 %2 %3 %4"_s.arg(rr.priority(), 5).arg(rr.weight())
+ .arg(rr.port()).arg(shorter(rr.target()));
+ if (rr.name() != domain)
+ entry = "SRV unexpected label to "_L1 + rr.name();
+ result.append(std::move(entry));
+ }
+
+ auto addNameRecords = [&](QLatin1StringView rrtype, const QList<QDnsDomainNameRecord> &rrset) {
+ for (const QDnsDomainNameRecord &rr : rrset) {
+ QString entry = u"%1 %2"_s.arg(rrtype, shorter(rr.value()));
+ if (rr.name() != domain)
+ entry = rrtype + " unexpected label to "_L1 + rr.name();
+ result.append(std::move(entry));
+ }
+ };
+ addNameRecords("NS"_L1, lookup->nameServerRecords());
+ addNameRecords("PTR"_L1, lookup->pointerRecords());
+ addNameRecords("CNAME"_L1, lookup->canonicalNameRecords());
+
+ for (const QDnsHostAddressRecord &rr : lookup->hostAddressRecords()) {
+ if (rr.name() != domain)
+ continue; // A and AAAA may appear as extra records in the answer section
+ QHostAddress addr = rr.value();
+ result.append(u"%1 %2"_s
+ .arg(addr.protocol() == QHostAddress::IPv6Protocol ? "AAAA" : "A",
+ addr.toString()));
+ }
+
+ for (const QDnsTextRecord &rr : lookup->textRecords()) {
+ QString entry = "TXT"_L1;
+ for (const QByteArray &data : rr.values()) {
+ entry += u' ';
+ entry += QDebug::toString(data);
+ }
+ result.append(std::move(entry));
+ }
+
+ for (const QDnsTlsAssociationRecord &rr : lookup->tlsAssociationRecords()) {
+ QString entry = u"TLSA %1 %2 %3 %4"_s.arg(int(rr.usage())).arg(int(rr.selector()))
+ .arg(int(rr.matchType())).arg(rr.value().toHex().toUpper());
+ if (rr.name() != domain)
+ entry = "TLSA unexpected label to "_L1 + rr.name();
+ result.append(std::move(entry));
+ }
+
+ result.sort();
+ return result;
+}
+
+void tst_QDnsLookup::lookupLocalhost()
+{
+ auto lookup = lookupCommon(QDnsLookup::Type::A, u"localhost."_s);
+ QVERIFY(lookup);
+ QCOMPARE(lookup->error(), QDnsLookup::NoError);
+
+ QList<QDnsHostAddressRecord> hosts = lookup->hostAddressRecords();
+ QCOMPARE(hosts.size(), 1);
+ QCOMPARE(hosts.at(0).value(), QHostAddress::LocalHost);
+ QVERIFY2(hosts.at(0).name().startsWith(lookup->name()),
+ qPrintable(hosts.at(0).name()));
+}
+
+void tst_QDnsLookup::lookupRoot()
+{
+#ifdef Q_OS_WIN
+ QSKIP("This test fails on Windows as it seems to treat the lookup as a local one.");
+#else
+ auto lookup = lookupCommon(QDnsLookup::Type::NS, u""_s);
+ if (!lookup)
+ return;
+ QCOMPARE(lookup->error(), QDnsLookup::NoError);
+
+ const QList<QDnsDomainNameRecord> servers = lookup->nameServerRecords();
+ QVERIFY(!servers.isEmpty());
+ for (const QDnsDomainNameRecord &ns : servers) {
+ QCOMPARE(ns.name(), QString());
+ QVERIFY(ns.value().endsWith(".root-servers.net"));
+ }
+#endif
+}
+
+void tst_QDnsLookup::lookupNxDomain_data()
+{
+ QTest::addColumn<QDnsLookup::Type>("type");
+ QTest::addColumn<QString>("domain");
+
+ QTest::newRow("a") << QDnsLookup::A << "invalid.invalid";
+ QTest::newRow("aaaa") << QDnsLookup::AAAA << "invalid.invalid";
+ QTest::newRow("any") << QDnsLookup::ANY << "invalid.invalid";
+ QTest::newRow("mx") << QDnsLookup::MX << "invalid.invalid";
+ QTest::newRow("ns") << QDnsLookup::NS << "invalid.invalid";
+ QTest::newRow("ptr") << QDnsLookup::PTR << "invalid.invalid";
+ QTest::newRow("srv") << QDnsLookup::SRV << "invalid.invalid";
+ QTest::newRow("txt") << QDnsLookup::TXT << "invalid.invalid";
+}
+
+void tst_QDnsLookup::lookupNxDomain()
+{
+ QFETCH(QDnsLookup::Type, type);
+ QFETCH(QString, domain);
+
+ auto lookup = lookupCommon(type, domain);
+ if (!lookup)
+ return;
+ QCOMPARE(lookup->name(), domainName(domain));
+ QCOMPARE(lookup->type(), type);
+ QCOMPARE(lookup->error(), QDnsLookup::NotFoundError);
+}
+
void tst_QDnsLookup::lookup_data()
{
- QTest::addColumn<int>("type");
+ QTest::addColumn<QDnsLookup::Type>("type");
QTest::addColumn<QString>("domain");
- QTest::addColumn<int>("error");
- QTest::addColumn<QString>("cname");
- QTest::addColumn<QString>("host");
- QTest::addColumn<QString>("mx");
- QTest::addColumn<QString>("ns");
- QTest::addColumn<QString>("ptr");
- QTest::addColumn<QString>("srv");
- QTest::addColumn<QString>("txt");
-
- QTest::newRow("a-empty") << int(QDnsLookup::A) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << ""<< "" << "";
- QTest::newRow("a-notfound") << int(QDnsLookup::A) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("a-single") << int(QDnsLookup::A) << "a-single" << int(QDnsLookup::NoError) << "" << "192.0.2.1" << "" << "" << "" << "" << "";
- QTest::newRow("a-multi") << int(QDnsLookup::A) << "a-multi" << int(QDnsLookup::NoError) << "" << "192.0.2.1;192.0.2.2;192.0.2.3" << "" << "" << "" << "" << "";
- QTest::newRow("aaaa-empty") << int(QDnsLookup::AAAA) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("aaaa-notfound") << int(QDnsLookup::AAAA) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("aaaa-single") << int(QDnsLookup::AAAA) << "aaaa-single" << int(QDnsLookup::NoError) << "" << "2001:db8::1" << "" << "" << "" << "" << "";
- QTest::newRow("aaaa-multi") << int(QDnsLookup::AAAA) << "aaaa-multi" << int(QDnsLookup::NoError) << "" << "2001:db8::1;2001:db8::2;2001:db8::3" << "" << "" << "" << "" << "";
-
- QTest::newRow("any-empty") << int(QDnsLookup::ANY) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("any-notfound") << int(QDnsLookup::ANY) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("any-a-single") << int(QDnsLookup::ANY) << "a-single" << int(QDnsLookup::NoError) << "" << "192.0.2.1" << "" << "" << "" << "" << "";
- QTest::newRow("any-a-plus-aaaa") << int(QDnsLookup::ANY) << "a-plus-aaaa" << int(QDnsLookup::NoError) << "" << "198.51.100.1;2001:db8::1:1" << "" << "" << "" << "" << "";
- QTest::newRow("any-multi") << int(QDnsLookup::ANY) << "multi" << int(QDnsLookup::NoError) << "" << "198.51.100.1;198.51.100.2;198.51.100.3;2001:db8::1:1;2001:db8::1:2" << "" << "" << "" << "" << "";
-
- QTest::newRow("mx-empty") << int(QDnsLookup::MX) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("mx-notfound") << int(QDnsLookup::MX) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("mx-single") << int(QDnsLookup::MX) << "mx-single" << int(QDnsLookup::NoError) << "" << "" << "10 multi" << "" << "" << "" << "";
- QTest::newRow("mx-single-cname") << int(QDnsLookup::MX) << "mx-single-cname" << int(QDnsLookup::NoError) << "" << "" << "10 cname" << "" << "" << "" << "";
- QTest::newRow("mx-multi") << int(QDnsLookup::MX) << "mx-multi" << int(QDnsLookup::NoError) << "" << "" << "10 multi;20 a-single" << "" << "" << "" << "";
- QTest::newRow("mx-multi-sameprio") << int(QDnsLookup::MX) << "mx-multi-sameprio" << int(QDnsLookup::NoError) << "" << ""
- << "10 multi;10 a-single|"
- "10 a-single;10 multi" << "" << "" << "" << "";
-
- QTest::newRow("ns-empty") << int(QDnsLookup::NS) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("ns-notfound") << int(QDnsLookup::NS) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("ns-single") << int(QDnsLookup::NS) << "ns-single" << int(QDnsLookup::NoError) << "" << "" << "" << "ns11.cloudns.net." << "" << "" << "";
- QTest::newRow("ns-multi") << int(QDnsLookup::NS) << "ns-multi" << int(QDnsLookup::NoError) << "" << "" << "" << "ns11.cloudns.net.;ns12.cloudns.net." << "" << "" << "";
-
- QTest::newRow("ptr-empty") << int(QDnsLookup::PTR) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("ptr-notfound") << int(QDnsLookup::PTR) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
+ QTest::addColumn<QString>("expected");
+
+ QTest::newRow("a-single") << QDnsLookup::A << "a-single"
+ << "A 192.0.2.1";
+ QTest::newRow("a-multi") << QDnsLookup::A << "a-multi"
+ << "A 192.0.2.1;A 192.0.2.2;A 192.0.2.3";
+ QTest::newRow("aaaa-single") << QDnsLookup::AAAA << "aaaa-single"
+ << "AAAA 2001:db8::1";
+ QTest::newRow("aaaa-multi") << QDnsLookup::AAAA << "aaaa-multi"
+ << "AAAA 2001:db8::1;AAAA 2001:db8::2;AAAA 2001:db8::3";
+
+ QTest::newRow("any-a-single") << QDnsLookup::ANY << "a-single"
+ << "A 192.0.2.1";
+ QTest::newRow("any-a-plus-aaaa") << QDnsLookup::ANY << "a-plus-aaaa"
+ << "A 198.51.100.1;AAAA 2001:db8::1:1";
+ QTest::newRow("any-multi") << QDnsLookup::ANY << "multi"
+ << "A 198.51.100.1;A 198.51.100.2;A 198.51.100.3;"
+ "AAAA 2001:db8::1:1;AAAA 2001:db8::1:2" ;
+
+ QTest::newRow("mx-single") << QDnsLookup::MX << "mx-single"
+ << "MX 10 multi";
+ QTest::newRow("mx-single-cname") << QDnsLookup::MX << "mx-single-cname"
+ << "MX 10 cname";
+ QTest::newRow("mx-multi") << QDnsLookup::MX << "mx-multi"
+ << "MX 10 multi;MX 20 a-single";
+ QTest::newRow("mx-multi-sameprio") << QDnsLookup::MX << "mx-multi-sameprio"
+ << "MX 10 a-single;MX 10 multi";
+
+ QTest::newRow("ns-single") << QDnsLookup::NS << "ns-single"
+ << "NS ns11.cloudns.net.";
+ QTest::newRow("ns-multi") << QDnsLookup::NS << "ns-multi"
+ << "NS ns11.cloudns.net.;NS ns12.cloudns.net.";
+
#if 0
// temporarily disabled since the new hosting provider can't insert
// PTR records outside of the in-addr.arpa zone
- QTest::newRow("ptr-single") << int(QDnsLookup::PTR) << "ptr-single" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "a-single" << "" << "";
+ QTest::newRow("ptr-single") << QDnsLookup::PTR << "ptr-single"
+ << "PTR a-single";
#endif
-
- QTest::newRow("srv-empty") << int(QDnsLookup::SRV) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("srv-notfound") << int(QDnsLookup::SRV) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("srv-single") << int(QDnsLookup::SRV) << "_echo._tcp.srv-single" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "" << "5 0 7 multi" << "";
- QTest::newRow("srv-prio") << int(QDnsLookup::SRV) << "_echo._tcp.srv-prio" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "" << "1 0 7 multi;2 0 7 a-plus-aaaa" << "";
- QTest::newRow("srv-weighted") << int(QDnsLookup::SRV) << "_echo._tcp.srv-weighted" << int(QDnsLookup::NoError) << "" << "" << "" << "" << ""
- << "5 75 7 multi;5 25 7 a-plus-aaaa|"
- "5 25 7 a-plus-aaaa;5 75 7 multi" << "";
- QTest::newRow("srv-multi") << int(QDnsLookup::SRV) << "_echo._tcp.srv-multi" << int(QDnsLookup::NoError) << "" << "" << "" << "" << ""
- << "1 50 7 multi;2 50 7 a-single;2 50 7 aaaa-single;3 50 7 a-multi|"
- "1 50 7 multi;2 50 7 aaaa-single;2 50 7 a-single;3 50 7 a-multi" << "";
-
- QTest::newRow("txt-empty") << int(QDnsLookup::TXT) << "" << int(QDnsLookup::InvalidRequestError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("txt-notfound") << int(QDnsLookup::TXT) << "invalid.invalid" << int(QDnsLookup::NotFoundError) << "" << "" << "" << "" << "" << "" << "";
- QTest::newRow("txt-single") << int(QDnsLookup::TXT) << "txt-single" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "" << "" << "Hello";
- QTest::newRow("txt-multi-onerr") << int(QDnsLookup::TXT) << "txt-multi-onerr" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "" << ""
- << QString::fromLatin1("Hello\0World", sizeof("Hello\0World") - 1);
- QTest::newRow("txt-multi-multirr") << int(QDnsLookup::TXT) << "txt-multi-multirr" << int(QDnsLookup::NoError) << "" << "" << "" << "" << "" << "" << "Hello;World";
-}
-
-static QByteArray msgDnsLookup(QDnsLookup::Error actualError,
- int expectedError,
- const QString &domain,
- const QString &cname,
- const QString &host,
- const QString &srv,
- const QString &mx,
- const QString &ns,
- const QString &ptr,
- const QString &errorString)
-{
- QString result;
- QTextStream str(&result);
- str << "Actual error: " << actualError;
- if (!errorString.isEmpty())
- str << " (" << errorString << ')';
- str << ", expected: " << expectedError;
- str << ", domain: " << domain;
- if (!cname.isEmpty())
- str << ", cname: " << cname;
- str << ", host: " << host;
- if (!srv.isEmpty())
- str << " server: " << srv;
- if (!mx.isEmpty())
- str << " mx: " << mx;
- if (!ns.isEmpty())
- str << " ns: " << ns;
- if (!ptr.isEmpty())
- str << " ptr: " << ptr;
- return result.toLocal8Bit();
+ QTest::newRow("ptr-1.1.1.1") << QDnsLookup::PTR << "1.1.1.1.in-addr.arpa."
+ << "PTR one.one.one.one.";
+ QTest::newRow("ptr-8.8.8.8") << QDnsLookup::PTR << "8.8.8.8.in-addr.arpa."
+ << "PTR dns.google.";
+ QTest::newRow("ptr-2001:4860:4860::8888")
+ << QDnsLookup::PTR << "8.8.8.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.6.8.4.0.6.8.4.1.0.0.2.ip6.arpa."
+ << "PTR dns.google.";
+ QTest::newRow("ptr-2606:4700:4700::1111")
+ << QDnsLookup::PTR << "1.1.1.1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.7.4.0.0.7.4.6.0.6.2.ip6.arpa."
+ << "PTR one.one.one.one.";
+
+ QTest::newRow("srv-single") << QDnsLookup::SRV << "_echo._tcp.srv-single"
+ << "SRV 5 0 7 multi";
+ QTest::newRow("srv-prio") << QDnsLookup::SRV << "_echo._tcp.srv-prio"
+ << "SRV 1 0 7 multi;SRV 2 0 7 a-plus-aaaa";
+ QTest::newRow("srv-weighted") << QDnsLookup::SRV << "_echo._tcp.srv-weighted"
+ << "SRV 5 25 7 a-plus-aaaa;SRV 5 75 7 multi";
+ QTest::newRow("srv-multi") << QDnsLookup::SRV << "_echo._tcp.srv-multi"
+ << "SRV 1 50 7 multi;"
+ "SRV 2 50 7 a-single;"
+ "SRV 2 50 7 aaaa-single;"
+ "SRV 3 50 7 a-multi";
+
+ QTest::newRow("tlsa") << QDnsLookup::Type::TLSA << "_25._tcp.multi"
+ << "TLSA 3 1 1 0123456789ABCDEFFEDCBA9876543210"
+ "0123456789ABCDEFFEDCBA9876543210";
+
+ QTest::newRow("txt-single") << QDnsLookup::TXT << "txt-single"
+ << "TXT \"Hello\"";
+ QTest::newRow("txt-multi-onerr") << QDnsLookup::TXT << "txt-multi-onerr"
+ << "TXT \"Hello\" \"World\"";
+ QTest::newRow("txt-multi-multirr") << QDnsLookup::TXT << "txt-multi-multirr"
+ << "TXT \"Hello\";TXT \"World\"";
}
void tst_QDnsLookup::lookup()
{
- QFETCH(int, type);
+ QFETCH(QDnsLookup::Type, type);
QFETCH(QString, domain);
- QFETCH(int, error);
- QFETCH(QString, cname);
- QFETCH(QString, host);
- QFETCH(QString, mx);
- QFETCH(QString, ns);
- QFETCH(QString, ptr);
- QFETCH(QString, srv);
- QFETCH(QString, txt);
-
- // transform the inputs
- domain = domainName(domain);
- cname = domainName(cname);
- ns = domainNameList(ns);
- ptr = domainNameList(ptr);
-
- // SRV and MX have reply entries that can change order
- // and we can't sort
- QStringList mx_alternatives = domainNameListAlternatives(mx);
- QStringList srv_alternatives = domainNameListAlternatives(srv);
+ QFETCH(QString, expected);
- QDnsLookup lookup;
- lookup.setType(static_cast<QDnsLookup::Type>(type));
- lookup.setName(domain);
- lookup.lookup();
- QTRY_VERIFY_WITH_TIMEOUT(lookup.isFinished(), Timeout);
+ std::unique_ptr<QDnsLookup> lookup = lookupCommon(type, domain);
+ if (!lookup)
+ return;
-#if defined(Q_OS_ANDROID)
- if (lookup.errorString() == QStringLiteral("Not yet supported on Android"))
- QEXPECT_FAIL("", "Not yet supported on Android", Abort);
+#ifdef Q_OS_WIN
+ if (QTest::currentDataTag() == "tlsa"_L1)
+ QSKIP("WinDNS doesn't work properly with TLSA records and we don't know why");
#endif
+ QCOMPARE(lookup->errorString(), QString());
+ QCOMPARE(lookup->error(), QDnsLookup::NoError);
+ QCOMPARE(lookup->type(), type);
+ QCOMPARE(lookup->name(), domainName(domain));
+
+ QString result = formatReply(lookup.get()).join(u';');
+ QCOMPARE(result, expected);
+
+ // confirm that MX and SRV records are properly sorted
+ const QList<QDnsMailExchangeRecord> mx = lookup->mailExchangeRecords();
+ for (qsizetype i = 1; i < mx.size(); ++i)
+ QCOMPARE_GE(mx[i].preference(), mx[i - 1].preference());
+
+ const QList<QDnsServiceRecord> srv = lookup->serviceRecords();
+ for (qsizetype i = 1; i < srv.size(); ++i)
+ QCOMPARE_GE(srv[i].priority(), srv[i - 1].priority());
+}
- QVERIFY2(int(lookup.error()) == error,
- msgDnsLookup(lookup.error(), error, domain, cname, host, srv, mx, ns, ptr, lookup.errorString()));
- if (error == QDnsLookup::NoError)
- QVERIFY(lookup.errorString().isEmpty());
- QCOMPARE(int(lookup.type()), type);
- QCOMPARE(lookup.name(), domain);
-
- // canonical names
- if (!cname.isEmpty()) {
- QVERIFY(!lookup.canonicalNameRecords().isEmpty());
- const QDnsDomainNameRecord cnameRecord = lookup.canonicalNameRecords().first();
- QCOMPARE(cnameRecord.name(), domain);
- QCOMPARE(cnameRecord.value(), cname);
- } else {
- QVERIFY(lookup.canonicalNameRecords().isEmpty());
- }
-
- // host addresses
- const QString hostName = cname.isEmpty() ? domain : cname;
- QStringList addresses;
- foreach (const QDnsHostAddressRecord &record, lookup.hostAddressRecords()) {
- //reply may include A & AAAA records for nameservers, ignore them and only look at records matching the query
- if (record.name() == hostName)
- addresses << record.value().toString().toLower();
- }
- addresses.sort();
- QCOMPARE(addresses.join(';'), host);
-
- // mail exchanges
- QStringList mailExchanges;
- foreach (const QDnsMailExchangeRecord &record, lookup.mailExchangeRecords()) {
- QCOMPARE(record.name(), domain);
- mailExchanges << QString::number(record.preference()) + QLatin1Char(' ') + record.exchange();
- }
- QVERIFY2(mx_alternatives.contains(mailExchanges.join(';')),
- qPrintable("Actual: " + mailExchanges.join(';') + "\nExpected one of:\n" + mx_alternatives.join('\n')));
-
- // name servers
- QStringList nameServers;
- foreach (const QDnsDomainNameRecord &record, lookup.nameServerRecords()) {
- //reply may include NS records for authoritative nameservers, ignore them and only look at records matching the query
- if (record.name() == domain)
- nameServers << record.value();
- }
- nameServers.sort();
- QCOMPARE(nameServers.join(';'), ns);
-
- // pointers
- if (!ptr.isEmpty()) {
- QVERIFY(!lookup.pointerRecords().isEmpty());
- const QDnsDomainNameRecord ptrRecord = lookup.pointerRecords().first();
- QCOMPARE(ptrRecord.name(), domain);
- QCOMPARE(ptrRecord.value(), ptr);
- } else {
- QVERIFY(lookup.pointerRecords().isEmpty());
- }
-
- // services
- QStringList services;
- foreach (const QDnsServiceRecord &record, lookup.serviceRecords()) {
- QCOMPARE(record.name(), domain);
- services << (QString::number(record.priority()) + QLatin1Char(' ')
- + QString::number(record.weight()) + QLatin1Char(' ')
- + QString::number(record.port()) + QLatin1Char(' ') + record.target());
- }
- QVERIFY2(srv_alternatives.contains(services.join(';')),
- qPrintable("Actual: " + services.join(';') + "\nExpected one of:\n" + srv_alternatives.join('\n')));
-
- // text
- QStringList texts;
- foreach (const QDnsTextRecord &record, lookup.textRecords()) {
- QCOMPARE(record.name(), domain);
- QString text;
- foreach (const QByteArray &ba, record.values()) {
- if (!text.isEmpty())
- text += '\0';
- text += QString::fromLatin1(ba);
- }
- texts << text;
- }
- texts.sort();
- QCOMPARE(texts.join(';'), txt);
+void tst_QDnsLookup::lookupIdn()
+{
+ usingIdnDomain = true;
+ lookup();
+ usingIdnDomain = false;
}
void tst_QDnsLookup::lookupReuse()
@@ -323,11 +573,6 @@ void tst_QDnsLookup::lookupReuse()
lookup.lookup();
QTRY_VERIFY_WITH_TIMEOUT(lookup.isFinished(), Timeout);
-#if defined(Q_OS_ANDROID)
- if (lookup.errorString() == QStringLiteral("Not yet supported on Android"))
- QEXPECT_FAIL("", "Not yet supported on Android", Abort);
-#endif
-
QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError));
QVERIFY(!lookup.hostAddressRecords().isEmpty());
QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("a-single"));
@@ -364,16 +609,204 @@ void tst_QDnsLookup::lookupAbortRetry()
lookup.lookup();
QTRY_VERIFY_WITH_TIMEOUT(lookup.isFinished(), Timeout);
-#if defined(Q_OS_ANDROID)
- if (lookup.errorString() == QStringLiteral("Not yet supported on Android"))
- QEXPECT_FAIL("", "Not yet supported on Android", Abort);
-#endif
-
QCOMPARE(int(lookup.error()), int(QDnsLookup::NoError));
QVERIFY(!lookup.hostAddressRecords().isEmpty());
QCOMPARE(lookup.hostAddressRecords().first().name(), domainName("aaaa-single"));
QCOMPARE(lookup.hostAddressRecords().first().value(), QHostAddress("2001:db8::1"));
}
+void tst_QDnsLookup::setNameserverLoopback()
+{
+#ifdef Q_OS_WIN
+ // Windows doesn't like sending DNS requests to ports other than 53, so
+ // let's try it first.
+ constexpr quint16 DesiredPort = 53;
+#else
+ // Trying to bind to port 53 will fail on Unix systems unless this test is
+ // run as root, so we try mDNS's port (to help decoding in a packet capture).
+ constexpr quint16 DesiredPort = 5353; // mDNS
+#endif
+ // random loopback address so multiple copies of this test can run
+ QHostAddress desiredAddress(0x7f000000 | QRandomGenerator::system()->bounded(0xffffff));
+
+ QUdpSocket server;
+ if (!server.bind(desiredAddress, DesiredPort)) {
+ // port in use, try a random one
+ server.bind(QHostAddress::LocalHost, 0);
+ }
+ QCOMPARE(server.state(), QUdpSocket::BoundState);
+
+ QDnsLookup lookup(QDnsLookup::Type::A, u"somelabel.somedomain"_s);
+ QSignalSpy spy(&lookup, SIGNAL(finished()));
+ lookup.setNameserver(server.localAddress(), server.localPort());
+
+ // QDnsLookup is threaded, so we can answer on the main thread
+ QObject::connect(&server, &QUdpSocket::readyRead,
+ &QTestEventLoop::instance(), &QTestEventLoop::exitLoop);
+ QObject::connect(&lookup, &QDnsLookup::finished,
+ &QTestEventLoop::instance(), &QTestEventLoop::exitLoop);
+ lookup.lookup();
+ QTestEventLoop::instance().enterLoop(5);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+ QVERIFY2(spy.isEmpty(), qPrintable(lookup.errorString()));
+
+ QNetworkDatagram dgram = server.receiveDatagram();
+ QByteArray data = dgram.data();
+ QCOMPARE_GT(data.size(), HeaderSize);
+
+ quint8 opcode = (quint8(data.at(2)) >> 3) & 0xF;
+ QCOMPARE(opcode, 0); // standard query
+
+ // send an NXDOMAIN reply to release the lookup thread
+ QByteArray reply = data;
+ reply[2] = 0x80U; // header->qr = true;
+ reply[3] = 3; // header->rcode = NXDOMAIN;
+ server.writeDatagram(reply.constData(), reply.size(), dgram.senderAddress(),
+ dgram.senderPort());
+ server.close();
+
+ // now check that the QDnsLookup finished
+ QTestEventLoop::instance().enterLoop(5);
+ QVERIFY(!QTestEventLoop::instance().timeout());
+ QCOMPARE(spy.size(), 1);
+ QCOMPARE(lookup.error(), QDnsLookup::NotFoundError);
+}
+
+template <QDnsLookup::Protocol Protocol>
+static void setNameserver_data_helper(const QByteArray &protoName)
+{
+ if (!QDnsLookup::isProtocolSupported(Protocol))
+ QSKIP(protoName + " not supported");
+
+ static QList<QHostAddress> servers = systemNameservers(Protocol)
+ + globalPublicNameservers(Protocol);
+ QTest::addColumn<QHostAddress>("server");
+
+ if (servers.isEmpty()) {
+ QSKIP("No reachable " + protoName + " servers were found");
+ } else {
+ for (const QHostAddress &h : std::as_const(servers))
+ QTest::addRow("%s", qUtf8Printable(h.toString())) << h;
+ }
+}
+
+void tst_QDnsLookup::setNameserver_data()
+{
+ setNameserver_data_helper<QDnsLookup::Standard>("DNS");
+}
+
+void tst_QDnsLookup::setNameserver_helper(QDnsLookup::Protocol protocol)
+{
+ QFETCH(QHostAddress, server);
+ QElapsedTimer timer;
+ timer.start();
+ std::unique_ptr<QDnsLookup> lookup =
+ lookupCommon(QDnsLookup::Type::A, "a-single", server, 0, protocol);
+ if (!lookup)
+ return;
+ qDebug() << "Lookup took" << timer.elapsed() << "ms";
+ QCOMPARE(lookup->error(), QDnsLookup::NoError);
+ QString result = formatReply(lookup.get()).join(';');
+ QCOMPARE(result, "A 192.0.2.1");
+}
+
+void tst_QDnsLookup::setNameserver()
+{
+ setNameserver_helper(QDnsLookup::Standard);
+}
+
+void tst_QDnsLookup::dnsOverTls_data()
+{
+ setNameserver_data_helper<QDnsLookup::DnsOverTls>("DNS-over-TLS");
+}
+
+void tst_QDnsLookup::dnsOverTls()
+{
+ setNameserver_helper(QDnsLookup::DnsOverTls);
+}
+
+void tst_QDnsLookup::bindingsAndProperties()
+{
+ QDnsLookup lookup;
+
+ lookup.setType(QDnsLookup::A);
+ QProperty<QDnsLookup::Type> dnsTypeProp;
+ lookup.bindableType().setBinding(Qt::makePropertyBinding(dnsTypeProp));
+ const QSignalSpy typeChangeSpy(&lookup, &QDnsLookup::typeChanged);
+
+ dnsTypeProp = QDnsLookup::AAAA;
+ QCOMPARE(typeChangeSpy.size(), 1);
+ QCOMPARE(lookup.type(), QDnsLookup::AAAA);
+
+ dnsTypeProp.setBinding(lookup.bindableType().makeBinding());
+ lookup.setType(QDnsLookup::A);
+ QCOMPARE(dnsTypeProp.value(), QDnsLookup::A);
+
+ QProperty<QString> nameProp;
+ lookup.bindableName().setBinding(Qt::makePropertyBinding(nameProp));
+ const QSignalSpy nameChangeSpy(&lookup, &QDnsLookup::nameChanged);
+
+ nameProp = QStringLiteral("a-plus-aaaa");
+ QCOMPARE(nameChangeSpy.size(), 1);
+ QCOMPARE(lookup.name(), QStringLiteral("a-plus-aaaa"));
+
+ nameProp.setBinding(lookup.bindableName().makeBinding());
+ lookup.setName(QStringLiteral("a-single"));
+ QCOMPARE(nameProp.value(), QStringLiteral("a-single"));
+
+ QProperty<QHostAddress> nameserverProp;
+ lookup.bindableNameserver().setBinding(Qt::makePropertyBinding(nameserverProp));
+ const QSignalSpy nameserverChangeSpy(&lookup, &QDnsLookup::nameserverChanged);
+ const QSignalSpy nameserverPortChangeSpy(&lookup, &QDnsLookup::nameserverPortChanged);
+
+ nameserverProp = QHostAddress::LocalHost;
+ QCOMPARE(nameserverChangeSpy.size(), 1);
+ QCOMPARE(nameserverPortChangeSpy.size(), 0);
+ QCOMPARE(lookup.nameserver(), QHostAddress::LocalHost);
+
+ nameserverProp.setBinding(lookup.bindableNameserver().makeBinding());
+ lookup.setNameserver(QHostAddress::Any);
+ QCOMPARE(nameserverProp.value(), QHostAddress::Any);
+ QCOMPARE(nameserverChangeSpy.size(), 2);
+ QCOMPARE(nameserverPortChangeSpy.size(), 0);
+
+ lookup.setNameserver(QHostAddress::LocalHostIPv6, 10053);
+ QCOMPARE(nameserverProp.value(), QHostAddress::LocalHostIPv6);
+ QCOMPARE(nameserverChangeSpy.size(), 3);
+ QCOMPARE(nameserverPortChangeSpy.size(), 1);
+}
+
+void tst_QDnsLookup::automatedBindings()
+{
+ QDnsLookup lookup;
+
+ QTestPrivate::testReadWritePropertyBasics(lookup, u"aaaa"_s, u"txt"_s, "name");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QDnsLookup::name");
+ return;
+ }
+
+ QTestPrivate::testReadWritePropertyBasics(lookup, QDnsLookup::AAAA, QDnsLookup::TXT, "type");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QDnsLookup::type");
+ return;
+ }
+
+ QTestPrivate::testReadWritePropertyBasics(lookup, QHostAddress{QHostAddress::Any},
+ QHostAddress{QHostAddress::LocalHost},
+ "nameserver");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QDnsLookup::nameserver");
+ return;
+ }
+
+ QTestPrivate::testReadWritePropertyBasics(lookup, quint16(123), quint16(456),
+ "nameserverPort");
+ if (QTest::currentTestFailed()) {
+ qDebug("Failed property test for QDnsLookup::nameserverPort");
+ return;
+ }
+}
+
QTEST_MAIN(tst_QDnsLookup)
#include "tst_qdnslookup.moc"
diff --git a/tests/auto/network/kernel/qdnslookup_appless/CMakeLists.txt b/tests/auto/network/kernel/qdnslookup_appless/CMakeLists.txt
new file mode 100644
index 0000000000..41cf19753f
--- /dev/null
+++ b/tests/auto/network/kernel/qdnslookup_appless/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qdnslookup_appless Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qdnslookup_appless LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qdnslookup_appless
+ SOURCES
+ tst_qdnslookup_appless.cpp
+ LIBRARIES
+ Qt::Network
+)
diff --git a/tests/auto/network/kernel/qdnslookup_appless/qdnslookup_appless.pro b/tests/auto/network/kernel/qdnslookup_appless/qdnslookup_appless.pro
deleted file mode 100644
index 25d76b5739..0000000000
--- a/tests/auto/network/kernel/qdnslookup_appless/qdnslookup_appless.pro
+++ /dev/null
@@ -1,7 +0,0 @@
-CONFIG += testcase
-
-TARGET = tst_qdnslookup_appless
-
-SOURCES += tst_qdnslookup_appless.cpp
-
-QT = core network testlib
diff --git a/tests/auto/network/kernel/qdnslookup_appless/tst_qdnslookup_appless.cpp b/tests/auto/network/kernel/qdnslookup_appless/tst_qdnslookup_appless.cpp
index e302fe8c74..21393ee628 100644
--- a/tests/auto/network/kernel/qdnslookup_appless/tst_qdnslookup_appless.cpp
+++ b/tests/auto/network/kernel/qdnslookup_appless/tst_qdnslookup_appless.cpp
@@ -1,34 +1,10 @@
-/****************************************************************************
-**
-** Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2012 Jeremy Lainé <jeremy.laine@m4x.org>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtCore/QCoreApplication>
#include <QtNetwork/QDnsLookup>
-#include <QtTest/QtTest>
+#include <QTest>
+#include <QTestEventLoop>
class tst_QDnsLookup_Appless : public QObject
{
diff --git a/tests/auto/network/kernel/qhostaddress/CMakeLists.txt b/tests/auto/network/kernel/qhostaddress/CMakeLists.txt
new file mode 100644
index 0000000000..e11a600b60
--- /dev/null
+++ b/tests/auto/network/kernel/qhostaddress/CMakeLists.txt
@@ -0,0 +1,27 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qhostaddress Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qhostaddress LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qhostaddress
+ SOURCES
+ tst_qhostaddress.cpp
+ LIBRARIES
+ Qt::NetworkPrivate
+)
+
+## Scopes:
+#####################################################################
+
+qt_internal_extend_target(tst_qhostaddress CONDITION WIN32
+ LIBRARIES
+ ws2_32
+)
diff --git a/tests/auto/network/kernel/qhostaddress/qhostaddress.pro b/tests/auto/network/kernel/qhostaddress/qhostaddress.pro
deleted file mode 100644
index b5d6ea6459..0000000000
--- a/tests/auto/network/kernel/qhostaddress/qhostaddress.pro
+++ /dev/null
@@ -1,7 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qhostaddress
-SOURCES += tst_qhostaddress.cpp
-
-QT = core network-private testlib
-
-win32:LIBS += -lws2_32
diff --git a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp
index 224e4d61a9..18d1c04a85 100644
--- a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp
+++ b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp
@@ -1,36 +1,11 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <qhostaddress.h>
#include <private/qhostaddress_p.h>
#include <qcoreapplication.h>
-#include <QtTest/QtTest>
+#include <QTest>
#include <qplatformdefs.h>
#include <qdebug.h>
#include <qhash.h>
@@ -38,12 +13,9 @@
#include <qdatastream.h>
#ifdef Q_OS_WIN
# include <qt_windows.h>
-# if defined(Q_OS_WINRT)
-# include <winsock2.h>
-# endif
#endif
-#ifdef Q_OS_ANDROID
+#if defined(Q_OS_ANDROID) || defined(Q_OS_WASM) || defined(Q_OS_VXWORKS)
# include <netinet/in.h>
#endif
@@ -113,14 +85,14 @@ void tst_QHostAddress::constructor_QString()
QTEST(hostAddr.toString(), "resAddr");
if ( protocol == 4 ) {
- QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
- QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::IPv4Protocol || hostAddr.protocol() == QHostAddress::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() != QHostAddress::IPv6Protocol );
} else if ( protocol == 6 ) {
- QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol );
- QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol );
+ QVERIFY( hostAddr.protocol() != QHostAddress::IPv4Protocol && hostAddr.protocol() != QHostAddress::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::IPv6Protocol );
} else {
QVERIFY( hostAddr.isNull() );
- QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::UnknownNetworkLayerProtocol );
}
}
@@ -220,14 +192,14 @@ void tst_QHostAddress::setAddress_QString()
QTEST(hostAddr.toString(), "resAddr");
if ( protocol == 4 ) {
- QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
- QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::IPv4Protocol || hostAddr.protocol() == QHostAddress::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() != QHostAddress::IPv6Protocol );
} else if ( protocol == 6 ) {
- QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol );
- QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol );
+ QVERIFY( hostAddr.protocol() != QHostAddress::IPv4Protocol && hostAddr.protocol() != QHostAddress::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::IPv6Protocol );
} else {
QVERIFY( hostAddr.isNull() );
- QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
+ QVERIFY( hostAddr.protocol() == QHostAddress::UnknownNetworkLayerProtocol );
}
}
@@ -354,6 +326,12 @@ void tst_QHostAddress::isEqual_data()
QTest::newRow("anyv6-anyv4-local") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv4) << (int)QHostAddress::ConvertLocalHost << false;
QTest::newRow("any-anyv4-local") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::AnyIPv4) << (int)QHostAddress::ConvertLocalHost << false;
QTest::newRow("any-anyv6-local") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::AnyIPv6) << (int)QHostAddress::ConvertLocalHost << false;
+ QTest::newRow("localhostv6-any-tolerant") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::Any) << (int)QHostAddress::TolerantConversion << false;
+ QTest::newRow("localhostv4-any-tolerant") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Any) << (int)QHostAddress::TolerantConversion << false;
+ QTest::newRow("localhostv6-anyv6-tolerant") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << (int)QHostAddress::TolerantConversion << false;
+ QTest::newRow("localhostv4-anyv6-tolerant") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv6) << (int)QHostAddress::TolerantConversion << false;
+ QTest::newRow("localhostv6-anyv4-tolerant") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv4) << (int)QHostAddress::TolerantConversion << false;
+ QTest::newRow("localhostv4-anyv4-tolerant") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv4) << (int)QHostAddress::TolerantConversion << false;
}
void tst_QHostAddress::isEqual()
@@ -367,33 +345,17 @@ void tst_QHostAddress::isEqual()
QCOMPARE(second.isEqual(first, QHostAddress::ConversionModeFlag(flags)), result);
}
-QT_WARNING_PUSH
-#ifdef QT_WARNING_DISABLE_DEPRECATED
-QT_WARNING_DISABLE_DEPRECATED
-#endif
-
void tst_QHostAddress::assignment()
{
QHostAddress address;
- address = "127.0.0.1";
- QCOMPARE(address, QHostAddress("127.0.0.1"));
-
- address = "::1";
- QCOMPARE(address, QHostAddress("::1"));
-
- // WinRT does not support sockaddr_in
-#ifndef Q_OS_WINRT
QHostAddress addr("4.2.2.1");
sockaddr_in sockAddr;
sockAddr.sin_family = AF_INET;
sockAddr.sin_addr.s_addr = htonl(addr.toIPv4Address());
address.setAddress((sockaddr *)&sockAddr);
QCOMPARE(address, addr);
-#endif // !Q_OS_WINRT
}
-QT_WARNING_POP
-
void tst_QHostAddress::scopeId()
{
QHostAddress address("fe80::2e0:4cff:fefb:662a%eth0");
@@ -741,6 +703,7 @@ void tst_QHostAddress::classification()
bool isUniqueLocalAddress = (result == UniqueLocalAddress);
bool isMulticast = (result == MulticastAddress);
bool isBroadcast = (result == BroadcastAddress);
+ bool isPrivateUse = (result == PrivateNetworkAddress || result == UniqueLocalAddress);
QCOMPARE(address.isLoopback(), isLoopback);
QCOMPARE(address.isGlobal(), isGlobal);
@@ -749,6 +712,7 @@ void tst_QHostAddress::classification()
QCOMPARE(address.isUniqueLocalUnicast(), isUniqueLocalAddress);
QCOMPARE(address.isMulticast(), isMulticast);
QCOMPARE(address.isBroadcast(), isBroadcast);
+ QCOMPARE(address.isPrivateUse(), isPrivateUse);
}
void tst_QHostAddress::convertv4v6_data()
@@ -789,7 +753,7 @@ void tst_QHostAddress::convertv4v6()
if (protocol == 4) {
bool ok;
quint32 v4 = source.toIPv4Address(&ok);
- QCOMPARE(ok, result.protocol() == QAbstractSocket::IPv4Protocol);
+ QCOMPARE(ok, result.protocol() == QHostAddress::IPv4Protocol);
if (ok)
QCOMPARE(QHostAddress(v4), result);
} else if (protocol == 6) {
diff --git a/tests/auto/network/kernel/qhostinfo/CMakeLists.txt b/tests/auto/network/kernel/qhostinfo/CMakeLists.txt
new file mode 100644
index 0000000000..dc7ab3b221
--- /dev/null
+++ b/tests/auto/network/kernel/qhostinfo/CMakeLists.txt
@@ -0,0 +1,32 @@
+# 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_qhostinfo LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+if(NOT QT_FEATURE_private_tests)
+ return()
+endif()
+
+#####################################################################
+## tst_qhostinfo Test:
+#####################################################################
+
+qt_internal_add_test(tst_qhostinfo
+ SOURCES
+ tst_qhostinfo.cpp
+ LIBRARIES
+ Qt::CorePrivate
+ Qt::NetworkPrivate
+)
+
+## Scopes:
+#####################################################################
+
+qt_internal_extend_target(tst_qhostinfo CONDITION WIN32
+ LIBRARIES
+ ws2_32
+)
diff --git a/tests/auto/network/kernel/qhostinfo/qhostinfo.pro b/tests/auto/network/kernel/qhostinfo/qhostinfo.pro
deleted file mode 100644
index 3d8457dd46..0000000000
--- a/tests/auto/network/kernel/qhostinfo/qhostinfo.pro
+++ /dev/null
@@ -1,11 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qhostinfo
-
-SOURCES += tst_qhostinfo.cpp
-
-requires(qtConfig(private_tests))
-QT = core-private network-private testlib
-
-win32:LIBS += -lws2_32
-
-winrt: WINRT_MANIFEST.capabilities += internetClientServer
diff --git a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp
index 0a130d363e..bd4bb7ef81 100644
--- a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp
+++ b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp
@@ -1,79 +1,52 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
// When using WinSock2 on Windows, it's the first thing that can be included
// (except qglobal.h), or else you'll get tons of compile errors
#include <qglobal.h>
-// To prevent windows system header files from re-defining min/max
-#define NOMINMAX 1
-
#if defined(Q_OS_WIN)
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
-#include <QtTest/QtTest>
-#include <qcoreapplication.h>
+#include <qhostinfo.h>
+#include "private/qhostinfo_p.h"
+#include "private/qnativesocketengine_p.h"
+
+#include <QCoreApplication>
#include <QDebug>
+#include <QTcpServer>
#include <QTcpSocket>
+#include <QTest>
+#include <QTestEventLoop>
+
#include <private/qthread_p.h>
-#include <QTcpServer>
-#ifndef QT_NO_BEARERMANAGEMENT
-#include <QtNetwork/qnetworkconfigmanager.h>
-#include <QtNetwork/qnetworkconfiguration.h>
-#include <QtNetwork/qnetworksession.h>
-#endif
+#include <sys/types.h>
-#include <time.h>
#if defined(Q_OS_WIN)
-#include <windows.h>
+# include <qt_windows.h>
+# ifdef gai_strerror
+# undef gai_strerror
+# define gai_strerror gai_strerrorA
+# endif
#else
-#include <unistd.h>
-#include <signal.h>
-#endif
-
-#include <qhostinfo.h>
-#include "private/qhostinfo_p.h"
-
-#include <sys/types.h>
-#if defined(Q_OS_UNIX)
-# include <sys/socket.h>
# include <netdb.h>
+# include <sys/socket.h>
+# include <unistd.h>
+#endif
+#ifndef NI_MAXHOST
+# define NI_MAXHOST 1025
#endif
#include "../../../network-settings.h"
#define TEST_DOMAIN ".test.qt-project.org"
+using namespace std::chrono_literals;
class tst_QHostInfo : public QObject
{
@@ -92,6 +65,7 @@ private slots:
void lookupIPv6();
void lookupConnectToFunctionPointer_data();
void lookupConnectToFunctionPointer();
+ void lookupConnectToFunctionPointerDeleted();
void lookupConnectToLambda_data();
void lookupConnectToLambda();
void reverseLookup_data();
@@ -111,20 +85,55 @@ private slots:
void cache();
void abortHostLookup();
-protected slots:
- void resultsReady(const QHostInfo &);
private:
bool ipv6LookupsAvailable;
bool ipv6Available;
- bool lookupDone;
- int lookupsDoneCounter;
+};
+
+class tst_QHostInfo_Helper : public QObject
+{
+ Q_OBJECT
+protected slots:
+ void resultsReady(const QHostInfo &);
+public:
+ tst_QHostInfo_Helper(const QString &hostname)
+ : hostname(hostname)
+ {}
+
+ QString hostname;
+ bool lookupDone = false;
+ int lookupsDoneCounter = 0;
QHostInfo lookupResults;
-#ifndef QT_NO_BEARERMANAGEMENT
- QNetworkConfigurationManager *netConfMan;
- QNetworkConfiguration networkConfiguration;
- QScopedPointer<QNetworkSession> networkSession;
-#endif
+
+ void blockingLookup()
+ {
+ lookupResults = QHostInfo::fromName(hostname);
+ lookupDone = true;
+ ++lookupsDoneCounter;
+ }
+ void lookupHostOldStyle()
+ {
+ QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
+ }
+ void lookupHostNewStyle()
+ {
+ QHostInfo::lookupHost(hostname, this, &tst_QHostInfo_Helper::resultsReady);
+ }
+ void lookupHostLambda()
+ {
+ QHostInfo::lookupHost(hostname, this, [this](const QHostInfo &hostInfo) {
+ resultsReady(hostInfo);
+ });
+ }
+
+ bool waitForResults(std::chrono::milliseconds timeout = 15s)
+ {
+ QTestEventLoop::instance().enterLoop(timeout);
+ return !QTestEventLoop::instance().timeout() && lookupDone;
+ }
+
+ void checkResults(QHostInfo::HostInfoError err, const QString &addresses);
};
void tst_QHostInfo::swapFunction()
@@ -179,18 +188,6 @@ void tst_QHostInfo::staticInformation()
void tst_QHostInfo::initTestCase()
{
- QVERIFY(QtNetworkSettings::verifyTestNetworkSettings());
-#ifndef QT_NO_BEARERMANAGEMENT
- //start the default network
- netConfMan = new QNetworkConfigurationManager(this);
- networkConfiguration = netConfMan->defaultConfiguration();
- networkSession.reset(new QNetworkSession(networkConfiguration));
- if (!networkSession->isOpen()) {
- networkSession->open();
- networkSession->waitForOpened(30000);
- }
-#endif
-
ipv6Available = false;
ipv6LookupsAvailable = false;
@@ -240,8 +237,6 @@ void tst_QHostInfo::lookupIPv4_data()
QTest::addColumn<QString>("addresses");
QTest::addColumn<int>("err");
- // Test server lookup
- QTest::newRow("lookup_01") << QtNetworkSettings::serverName() << QtNetworkSettings::serverIP().toString() << int(QHostInfo::NoError);
QTest::newRow("empty") << "" << "" << int(QHostInfo::HostNotFound);
QTest::newRow("single_ip4") << "a-single" TEST_DOMAIN << "192.0.2.1" << int(QHostInfo::NoError);
@@ -254,26 +249,15 @@ void tst_QHostInfo::lookupIPv4_data()
QTest::newRow("idn-unicode") << QString::fromLatin1("a-single.alqualond\353" TEST_DOMAIN) << "192.0.2.1" << int(QHostInfo::NoError);
}
-void tst_QHostInfo::lookupIPv4()
+void tst_QHostInfo_Helper::checkResults(QHostInfo::HostInfoError err, const QString &addresses)
{
- QFETCH(QString, hostname);
- QFETCH(int, err);
- QFETCH(QString, addresses);
-
- lookupDone = false;
- QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
-
- QTestEventLoop::instance().enterLoop(10);
- QVERIFY(!QTestEventLoop::instance().timeout());
- QVERIFY(lookupDone);
-
if ((int)lookupResults.error() != (int)err) {
qWarning() << hostname << "=>" << lookupResults.errorString();
}
- QCOMPARE((int)lookupResults.error(), (int)err);
+ QCOMPARE(lookupResults.error(), err);
QStringList tmp;
- for (int i = 0; i < lookupResults.addresses().count(); ++i)
+ for (int i = 0; i < lookupResults.addresses().size(); ++i)
tmp.append(lookupResults.addresses().at(i).toString());
tmp.sort();
@@ -283,6 +267,18 @@ void tst_QHostInfo::lookupIPv4()
QCOMPARE(tmp.join(' '), expected.join(' '));
}
+void tst_QHostInfo::lookupIPv4()
+{
+ QFETCH(QString, hostname);
+ QFETCH(int, err);
+ QFETCH(QString, addresses);
+
+ tst_QHostInfo_Helper helper(hostname);
+ helper.lookupHostOldStyle();
+ QVERIFY(helper.waitForResults());
+ helper.checkResults(QHostInfo::HostInfoError(err), addresses);
+}
+
void tst_QHostInfo::lookupIPv6_data()
{
QTest::addColumn<QString>("hostname");
@@ -308,24 +304,10 @@ void tst_QHostInfo::lookupIPv6()
if (!ipv6LookupsAvailable)
QSKIP("This platform does not support IPv6 lookups");
- lookupDone = false;
- QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(QHostInfo)));
-
- QTestEventLoop::instance().enterLoop(10);
- QVERIFY(!QTestEventLoop::instance().timeout());
- QVERIFY(lookupDone);
-
- QCOMPARE((int)lookupResults.error(), (int)err);
-
- QStringList tmp;
- for (int i = 0; i < lookupResults.addresses().count(); ++i)
- tmp.append(lookupResults.addresses().at(i).toString());
- tmp.sort();
-
- QStringList expected = addresses.split(' ');
- expected.sort();
-
- QCOMPARE(tmp.join(' ').toLower(), expected.join(' ').toLower());
+ tst_QHostInfo_Helper helper(hostname);
+ helper.lookupHostOldStyle();
+ QVERIFY(helper.waitForResults());
+ helper.checkResults(QHostInfo::HostInfoError(err), addresses);
}
void tst_QHostInfo::lookupConnectToFunctionPointer_data()
@@ -339,26 +321,21 @@ void tst_QHostInfo::lookupConnectToFunctionPointer()
QFETCH(int, err);
QFETCH(QString, addresses);
- lookupDone = false;
- QHostInfo::lookupHost(hostname, this, &tst_QHostInfo::resultsReady);
-
- QTestEventLoop::instance().enterLoop(10);
- QVERIFY(!QTestEventLoop::instance().timeout());
- QVERIFY(lookupDone);
-
- if (int(lookupResults.error()) != int(err))
- qWarning() << hostname << "=>" << lookupResults.errorString();
- QCOMPARE(int(lookupResults.error()), int(err));
-
- QStringList tmp;
- for (const auto &result : lookupResults.addresses())
- tmp.append(result.toString());
- tmp.sort();
-
- QStringList expected = addresses.split(' ');
- expected.sort();
+ tst_QHostInfo_Helper helper(hostname);
+ helper.lookupHostNewStyle();
+ QVERIFY(helper.waitForResults());
+ helper.checkResults(QHostInfo::HostInfoError(err), addresses);
+}
- QCOMPARE(tmp.join(' '), expected.join(' '));
+void tst_QHostInfo::lookupConnectToFunctionPointerDeleted()
+{
+ {
+ QObject contextObject;
+ QHostInfo::lookupHost("localhost", &contextObject, [](const QHostInfo){
+ QFAIL("This should never be called!");
+ });
+ }
+ QTestEventLoop::instance().enterLoop(3);
}
void tst_QHostInfo::lookupConnectToLambda_data()
@@ -372,89 +349,38 @@ void tst_QHostInfo::lookupConnectToLambda()
QFETCH(int, err);
QFETCH(QString, addresses);
- lookupDone = false;
- QHostInfo::lookupHost(hostname, [=](const QHostInfo &hostInfo) {
- resultsReady(hostInfo);
- });
-
- QTestEventLoop::instance().enterLoop(10);
- QVERIFY(!QTestEventLoop::instance().timeout());
- QVERIFY(lookupDone);
-
- if (int(lookupResults.error()) != int(err))
- qWarning() << hostname << "=>" << lookupResults.errorString();
- QCOMPARE(int(lookupResults.error()), int(err));
-
- QStringList tmp;
- for (int i = 0; i < lookupResults.addresses().count(); ++i)
- tmp.append(lookupResults.addresses().at(i).toString());
- tmp.sort();
-
- QStringList expected = addresses.split(' ');
- expected.sort();
-
- QCOMPARE(tmp.join(' '), expected.join(' '));
+ tst_QHostInfo_Helper helper(hostname);
+ helper.lookupHostLambda();
+ QVERIFY(helper.waitForResults());
+ helper.checkResults(QHostInfo::HostInfoError(err), addresses);
}
static QStringList reverseLookupHelper(const QString &ip)
{
QStringList results;
-
- const QString pythonCode =
- "import socket;"
- "import sys;"
- "print (socket.getnameinfo((sys.argv[1], 0), 0)[0]);";
-
- QList<QByteArray> lines;
- QProcess python;
- python.setProcessChannelMode(QProcess::ForwardedErrorChannel);
- python.start("python", QStringList() << QString("-c") << pythonCode << ip);
- if (python.waitForFinished()) {
- if (python.exitStatus() == QProcess::NormalExit && python.exitCode() == 0)
- lines = python.readAllStandardOutput().split('\n');
- for (QByteArray line : lines) {
- if (!line.isEmpty())
- results << line.trimmed();
- }
- if (!results.isEmpty())
- return results;
- }
-
- qDebug() << "Python failed, falling back to nslookup";
- QProcess lookup;
- lookup.setProcessChannelMode(QProcess::ForwardedErrorChannel);
- lookup.start("nslookup", QStringList(ip));
- if (!lookup.waitForFinished()) {
- results << "nslookup failure";
- qDebug() << "nslookup failure";
+ union qt_sockaddr {
+ sockaddr a;
+ sockaddr_in a4;
+ sockaddr_in6 a6;
+ } sa = {};
+
+ QHostAddress addr(ip);
+ if (addr.isNull()) {
+ qWarning("Could not parse IP address: %ls", qUtf16Printable(ip));
return results;
}
- lines = lookup.readAllStandardOutput().split('\n');
-
- QByteArray name;
-
- const QByteArray nameMarkerNix("name =");
- const QByteArray nameMarkerWin("Name:");
- const QByteArray addressMarkerWin("Address:");
-
- for (QByteArray line : lines) {
- int index = -1;
- if ((index = line.indexOf(nameMarkerNix)) != -1) { // Linux and macOS
- name = line.mid(index + nameMarkerNix.length()).chopped(1).trimmed();
- results << name;
- } else if (line.startsWith(nameMarkerWin)) { // Windows formatting
- name = line.mid(line.lastIndexOf(" ")).trimmed();
- } else if (line.startsWith(addressMarkerWin)) {
- QByteArray address = line.mid(addressMarkerWin.length()).trimmed();
- if (address == ip) {
- results << name;
- }
- }
- }
- if (results.isEmpty()) {
- qDebug() << "Failure to parse nslookup output: " << lines;
+ // from qnativesocketengine_p.h:
+ QT_SOCKLEN_T len = setSockaddr(&sa.a, addr, /*port = */ 0);
+
+ QByteArray name(NI_MAXHOST, Qt::Uninitialized);
+ int ni_flags = NI_NAMEREQD | NI_NUMERICSERV;
+ if (int r = getnameinfo(&sa.a, len, name.data(), name.size(), nullptr, 0, ni_flags)) {
+ qWarning("Failed to reverse look up '%ls': %s", qUtf16Printable(ip), gai_strerror(r));
+ } else {
+ results << QString::fromLatin1(name, qstrnlen(name, name.size()));
}
+
return results;
}
@@ -467,7 +393,11 @@ void tst_QHostInfo::reverseLookup_data()
QTest::newRow("dns.google") << QString("8.8.8.8") << reverseLookupHelper("8.8.8.8") << 0 << false;
QTest::newRow("one.one.one.one") << QString("1.1.1.1") << reverseLookupHelper("1.1.1.1") << 0 << false;
- QTest::newRow("bogus-name") << QString("1::2::3::4") << QStringList() << 1 << true;
+ if (QStringList hostNames = reverseLookupHelper("2001:4860:4860::8888"); !hostNames.isEmpty())
+ QTest::newRow("dns.google IPv6") << QString("2001:4860:4860::8888") << std::move(hostNames) << 0 << true;
+ if (QStringList hostNames = reverseLookupHelper("2606:4700:4700::1111"); !hostNames.isEmpty())
+ QTest::newRow("cloudflare IPv6") << QString("2606:4700:4700::1111") << std::move(hostNames) << 0 << true;
+ QTest::newRow("bogus-name IPv6") << QString("1::2::3::4") << QStringList() << 1 << true;
}
void tst_QHostInfo::reverseLookup()
@@ -475,11 +405,6 @@ void tst_QHostInfo::reverseLookup()
QFETCH(QString, address);
QFETCH(QStringList, hostNames);
QFETCH(int, err);
- QFETCH(bool, ipv6);
-
- if (ipv6 && !ipv6LookupsAvailable) {
- QSKIP("IPv6 reverse lookups are not supported on this platform");
- }
QHostInfo info = QHostInfo::fromName(address);
@@ -487,7 +412,7 @@ void tst_QHostInfo::reverseLookup()
if (!hostNames.contains(info.hostName()))
qDebug() << "Failure: expecting" << hostNames << ",got " << info.hostName();
QVERIFY(hostNames.contains(info.hostName()));
- QCOMPARE(info.addresses().first(), QHostAddress(address));
+ QCOMPARE(info.addresses().constFirst(), QHostAddress(address));
} else {
QCOMPARE(info.hostName(), address);
QCOMPARE(info.error(), QHostInfo::HostNotFound);
@@ -508,21 +433,9 @@ void tst_QHostInfo::blockingLookup()
QFETCH(int, err);
QFETCH(QString, addresses);
- QHostInfo hostInfo = QHostInfo::fromName(hostname);
- QStringList tmp;
- for (int i = 0; i < hostInfo.addresses().count(); ++i)
- tmp.append(hostInfo.addresses().at(i).toString());
- tmp.sort();
-
- if ((int)hostInfo.error() != (int)err) {
- qWarning() << hostname << "=>" << lookupResults.errorString();
- }
- QCOMPARE((int)hostInfo.error(), (int)err);
-
- QStringList expected = addresses.split(' ');
- expected.sort();
-
- QCOMPARE(tmp.join(' ').toUpper(), expected.join(' ').toUpper());
+ tst_QHostInfo_Helper helper(hostname);
+ helper.blockingLookup();
+ helper.checkResults(QHostInfo::HostInfoError(err), addresses);
}
void tst_QHostInfo::raceCondition()
@@ -536,11 +449,12 @@ void tst_QHostInfo::raceCondition()
class LookupThread : public QThread
{
protected:
- inline void run()
+ inline void run() override
{
QHostInfo info = QHostInfo::fromName("a-single" TEST_DOMAIN);
+ QCOMPARE(info.errorString(), "Unknown error"); // no error
QCOMPARE(info.error(), QHostInfo::NoError);
- QVERIFY(info.addresses().count() > 0);
+ QVERIFY(info.addresses().size() > 0);
QCOMPARE(info.addresses().at(0).toString(), QString("192.0.2.1"));
}
};
@@ -587,24 +501,22 @@ void tst_QHostInfo::threadSafetyAsynchronousAPI()
{
const int nattempts = 10;
const int lookupsperthread = 10;
- QList<QThread*> threads;
- QList<LookupReceiver*> receivers;
+ QThread threads[nattempts];
+ LookupReceiver receivers[nattempts];
for (int i = 0; i < nattempts; ++i) {
- QThread* thread = new QThread;
- LookupReceiver* receiver = new LookupReceiver;
+ QThread *thread = &threads[i];
+ LookupReceiver *receiver = &receivers[i];
receiver->numrequests = lookupsperthread;
- receivers.append(receiver);
receiver->moveToThread(thread);
connect(thread, SIGNAL(started()), receiver, SLOT(start()));
thread->start();
- threads.append(thread);
}
- for (int k = threads.count() - 1; k >= 0; --k)
- QVERIFY(threads.at(k)->wait(60000));
- foreach (LookupReceiver* receiver, receivers) {
- QCOMPARE(receiver->result.error(), QHostInfo::NoError);
- QCOMPARE(receiver->result.addresses().at(0).toString(), QString("192.0.2.1"));
- QCOMPARE(receiver->numrequests, 0);
+ for (int k = nattempts - 1; k >= 0; --k)
+ QVERIFY(threads[k].wait(60000));
+ for (LookupReceiver &receiver : receivers) {
+ QCOMPARE(receiver.result.error(), QHostInfo::NoError);
+ QCOMPARE(receiver.result.addresses().at(0).toString(), QString("192.0.2.1"));
+ QCOMPARE(receiver.numrequests, 0);
}
}
@@ -613,17 +525,11 @@ void tst_QHostInfo::threadSafetyAsynchronousAPI()
void tst_QHostInfo::multipleSameLookups()
{
const int COUNT = 10;
- lookupsDoneCounter = 0;
-
+ tst_QHostInfo_Helper helper("localhost");
for (int i = 0; i < COUNT; i++)
- QHostInfo::lookupHost("localhost", this, SLOT(resultsReady(QHostInfo)));
+ helper.lookupHostOldStyle();
- QElapsedTimer timer;
- timer.start();
- while (timer.elapsed() < 10000 && lookupsDoneCounter < COUNT) {
- QTestEventLoop::instance().enterLoop(2);
- }
- QCOMPARE(lookupsDoneCounter, COUNT);
+ QTRY_COMPARE_WITH_TIMEOUT(helper.lookupsDoneCounter, COUNT, 10s);
}
// this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all,
@@ -652,19 +558,15 @@ void tst_QHostInfo::multipleDifferentLookups()
QFETCH(int, repeats);
const int COUNT = hostnameList.size();
- lookupsDoneCounter = 0;
+ tst_QHostInfo_Helper helper(QString{});
for (int i = 0; i < hostnameList.size(); i++)
- for (int j = 0; j < repeats; ++j)
- QHostInfo::lookupHost(hostnameList.at(i), this, SLOT(resultsReady(QHostInfo)));
-
- QElapsedTimer timer;
- timer.start();
- while (timer.elapsed() < 60000 && lookupsDoneCounter < repeats*COUNT) {
- QTestEventLoop::instance().enterLoop(2);
- //qDebug() << "t:" << timer.elapsed();
- }
- QCOMPARE(lookupsDoneCounter, repeats*COUNT);
+ for (int j = 0; j < repeats; ++j) {
+ helper.hostname = hostnameList.at(i);
+ helper.lookupHostOldStyle();
+ }
+
+ QTRY_COMPARE_WITH_TIMEOUT(helper.lookupsDoneCounter, repeats*COUNT, 60s);
}
void tst_QHostInfo::cache()
@@ -673,13 +575,12 @@ void tst_QHostInfo::cache()
if (!cache)
return; // test makes only sense when cache enabled
- // reset slot counter
- lookupsDoneCounter = 0;
+ tst_QHostInfo_Helper helper("localhost");
// lookup once, wait in event loop, result should not come directly.
bool valid = true;
int id = -1;
- QHostInfo result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
+ QHostInfo result = qt_qhostinfo_lookup(helper.hostname, &helper, SLOT(resultsReady(QHostInfo)), &valid, &id);
QTestEventLoop::instance().enterLoop(5);
QVERIFY(!QTestEventLoop::instance().timeout());
QVERIFY(!valid);
@@ -687,7 +588,7 @@ void tst_QHostInfo::cache()
// loopkup second time, result should come directly
valid = false;
- result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
+ result = qt_qhostinfo_lookup(helper.hostname, &helper, SLOT(resultsReady(QHostInfo)), &valid, &id);
QVERIFY(valid);
QVERIFY(!result.addresses().isEmpty());
@@ -696,18 +597,19 @@ void tst_QHostInfo::cache()
// lookup third time, result should not come directly.
valid = true;
- result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id);
+ result = qt_qhostinfo_lookup(helper.hostname, &helper, SLOT(resultsReady(QHostInfo)), &valid, &id);
QTestEventLoop::instance().enterLoop(5);
QVERIFY(!QTestEventLoop::instance().timeout());
QVERIFY(!valid);
QVERIFY(result.addresses().isEmpty());
// the slot should have been called 2 times.
- QCOMPARE(lookupsDoneCounter, 2);
+ QCOMPARE(helper.lookupsDoneCounter, 2);
}
-void tst_QHostInfo::resultsReady(const QHostInfo &hi)
+void tst_QHostInfo_Helper::resultsReady(const QHostInfo &hi)
{
+ QVERIFY(QThread::currentThread() == thread());
lookupDone = true;
lookupResults = hi;
lookupsDoneCounter++;
@@ -716,16 +618,15 @@ void tst_QHostInfo::resultsReady(const QHostInfo &hi)
void tst_QHostInfo::abortHostLookup()
{
- //reset counter
- lookupsDoneCounter = 0;
+ tst_QHostInfo_Helper helper("a-single" TEST_DOMAIN);
bool valid = false;
int id = -1;
- QHostInfo result = qt_qhostinfo_lookup("a-single" TEST_DOMAIN, this, SLOT(resultsReady(QHostInfo)), &valid, &id);
+ QHostInfo result = qt_qhostinfo_lookup(helper.hostname, &helper, SLOT(resultsReady(QHostInfo)), &valid, &id);
QVERIFY(!valid);
//it is assumed that the DNS request/response in the backend is slower than it takes to call abort
QHostInfo::abortHostLookup(id);
QTestEventLoop::instance().enterLoop(5);
- QCOMPARE(lookupsDoneCounter, 0);
+ QCOMPARE(helper.lookupsDoneCounter, 0);
}
class LookupAborter : public QObject
diff --git a/tests/auto/network/kernel/qnetworkaddressentry/CMakeLists.txt b/tests/auto/network/kernel/qnetworkaddressentry/CMakeLists.txt
new file mode 100644
index 0000000000..02bf37880a
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkaddressentry/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qnetworkaddressentry Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qnetworkaddressentry LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkaddressentry
+ SOURCES
+ tst_qnetworkaddressentry.cpp
+ LIBRARIES
+ Qt::Network
+)
diff --git a/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro b/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro
deleted file mode 100644
index 427f2e0f19..0000000000
--- a/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro
+++ /dev/null
@@ -1,5 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qnetworkaddressentry
-SOURCES += tst_qnetworkaddressentry.cpp
-
-QT = core network testlib
diff --git a/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp b/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp
index f8583d0aba..801eb58931 100644
--- a/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp
+++ b/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp
@@ -1,33 +1,8 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+
+#include <QTest>
#include <qcoreapplication.h>
#include <qnetworkinterface.h>
diff --git a/tests/auto/network/kernel/qnetworkdatagram/CMakeLists.txt b/tests/auto/network/kernel/qnetworkdatagram/CMakeLists.txt
new file mode 100644
index 0000000000..5bfc2edc6a
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkdatagram/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qnetworkdatagram Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qnetworkdatagram LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkdatagram
+ SOURCES
+ tst_qnetworkdatagram.cpp
+ LIBRARIES
+ Qt::Network
+)
diff --git a/tests/auto/network/kernel/qnetworkdatagram/qnetworkdatagram.pro b/tests/auto/network/kernel/qnetworkdatagram/qnetworkdatagram.pro
deleted file mode 100644
index 23d57f3fbf..0000000000
--- a/tests/auto/network/kernel/qnetworkdatagram/qnetworkdatagram.pro
+++ /dev/null
@@ -1,4 +0,0 @@
-CONFIG += testcase console
-TARGET = tst_qnetworkdatagram
-SOURCES += tst_qnetworkdatagram.cpp
-QT = core network testlib
diff --git a/tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp b/tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp
index 5eedd1043b..df473133a6 100644
--- a/tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp
+++ b/tests/auto/network/kernel/qnetworkdatagram/tst_qnetworkdatagram.cpp
@@ -1,33 +1,8 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QNetworkDatagram>
-#include <QtTest>
+#include <QTest>
#include <QCoreApplication>
class tst_QNetworkDatagram : public QObject
@@ -83,6 +58,17 @@ void tst_QNetworkDatagram::getSetCheck()
dg.setDestination(QHostAddress::Broadcast, 137);
QCOMPARE(dg.destinationAddress(), QHostAddress(QHostAddress::Broadcast));
QCOMPARE(dg.destinationPort(), 137);
+
+ auto dg2 = dg;
+ QCOMPARE(dg2.hopLimit(), dg.hopLimit());
+ QCOMPARE(dg2.interfaceIndex(), dg.interfaceIndex());
+ QCOMPARE(dg2.senderAddress(), dg.senderAddress());
+ QCOMPARE(dg2.senderPort(), dg.senderPort());
+ QCOMPARE(dg2.destinationAddress(), dg.destinationAddress());
+ QCOMPARE(dg2.destinationPort(), dg.destinationPort());
+
+ dg.clear();
+ QVERIFY(dg.isNull());
}
void tst_QNetworkDatagram::makeReply_data()
@@ -131,7 +117,7 @@ void tst_QNetworkDatagram::makeReply()
QNetworkDatagram copy = dgram;
copy.setData(copy.data());
{
- QNetworkDatagram reply = qMove(copy).makeReply("World");
+ QNetworkDatagram reply = std::move(copy).makeReply("World");
QCOMPARE(reply.data(), QByteArray("World"));
QCOMPARE(reply.senderAddress(), QHostAddress(localAddress));
QCOMPARE(reply.senderPort(), localAddress.isEmpty() ? -1 : dgram.destinationPort());
diff --git a/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt b/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt
new file mode 100644
index 0000000000..354cefc993
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkinformation/CMakeLists.txt
@@ -0,0 +1,15 @@
+# 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_qnetworkinformation LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkinformation
+ SOURCES
+ tst_qnetworkinformation.cpp
+ LIBRARIES
+ Qt::NetworkPrivate
+)
diff --git a/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
new file mode 100644
index 0000000000..daf81823e8
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkinformation/tst_qnetworkinformation.cpp
@@ -0,0 +1,255 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QtNetwork/private/qnetworkinformation_p.h>
+#include <QtNetwork/qnetworkinformation.h>
+#include <QtTest/qtest.h>
+#include <QtTest/qsignalspy.h>
+
+#include <limits>
+#include <memory>
+
+class MockFactory;
+class tst_QNetworkInformation : public QObject
+{
+ Q_OBJECT
+private slots:
+ void initTestCase();
+ void supportedFeatures();
+ void reachability();
+ void behindCaptivePortal();
+ void transportMedium();
+ void isMetered();
+ void cleanupTestCase();
+
+private:
+ std::unique_ptr<MockFactory> mockFactory;
+};
+
+static const QString mockName = QStringLiteral("mock");
+class MockBackend : public QNetworkInformationBackend
+{
+ Q_OBJECT
+public:
+ MockBackend()
+ {
+ Q_ASSERT(!instance);
+ instance = this;
+ setReachability(QNetworkInformation::Reachability::Online);
+ setNewBehindCaptivePortal(false);
+ }
+ ~MockBackend() { instance = nullptr; }
+
+ QString name() const override { return mockName; }
+
+ QNetworkInformation::Features featuresSupported() const override
+ {
+ return featuresSupportedStatic();
+ }
+
+ static void setNewReachability(QNetworkInformation::Reachability value)
+ {
+ Q_ASSERT(instance);
+ instance->setReachability(value);
+ }
+
+ static void setNewBehindCaptivePortal(bool value)
+ {
+ Q_ASSERT(instance);
+ instance->setBehindCaptivePortal(value);
+ }
+
+ static void setNewTransportMedium(QNetworkInformation::TransportMedium medium)
+ {
+ Q_ASSERT(instance);
+ instance->setTransportMedium(medium);
+ }
+
+ static void setNewMetered(bool metered)
+ {
+ Q_ASSERT(instance);
+ instance->setMetered(metered);
+ }
+
+ static QNetworkInformation::Features featuresSupportedStatic()
+ {
+ return { QNetworkInformation::Feature::Reachability
+ | QNetworkInformation::Feature::CaptivePortal
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered };
+ }
+
+private:
+ static inline MockBackend *instance = nullptr;
+};
+
+class MockFactory : public QNetworkInformationBackendFactory
+{
+ Q_OBJECT
+public:
+ QString name() const override { return mockName; }
+ QNetworkInformationBackend *
+ create(QNetworkInformation::Features requiredFeatures) const override
+ {
+ if ((requiredFeatures & featuresSupported()) != requiredFeatures)
+ return nullptr;
+ return new MockBackend();
+ }
+ QNetworkInformation::Features featuresSupported() const override
+ {
+ return MockBackend::featuresSupportedStatic();
+ }
+};
+
+void tst_QNetworkInformation::initTestCase()
+{
+ auto prevBackends = QNetworkInformation::availableBackends();
+ qDebug() << "available backends:" << prevBackends;
+ // Creating the factory registers it as a backend
+ mockFactory = std::make_unique<MockFactory>();
+ auto backends = QNetworkInformation::availableBackends();
+ QVERIFY(backends.size() > prevBackends.size());
+ QVERIFY(backends.contains(u"mock"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mock"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mock"));
+ QVERIFY(QNetworkInformation::loadBackendByName(u"mOcK"));
+ QVERIFY(!QNetworkInformation::loadBackendByName(u"mocks"));
+}
+
+void tst_QNetworkInformation::cleanupTestCase()
+{
+ // Make sure the factory gets unregistered on destruction:
+ mockFactory.reset();
+ auto backends = QNetworkInformation::availableBackends();
+ QVERIFY(!backends.contains(u"mock"));
+}
+
+void tst_QNetworkInformation::supportedFeatures()
+{
+ auto info = QNetworkInformation::instance();
+
+ auto allFeatures = QNetworkInformation::Features(QNetworkInformation::Feature::CaptivePortal
+ | QNetworkInformation::Feature::Reachability
+ | QNetworkInformation::Feature::TransportMedium
+ | QNetworkInformation::Feature::Metered);
+
+ QCOMPARE(info->supportedFeatures(), allFeatures);
+
+ QVERIFY(info->supports(allFeatures));
+ QVERIFY(info->supports(QNetworkInformation::Feature::CaptivePortal));
+ QVERIFY(info->supports(QNetworkInformation::Feature::Reachability));
+ QVERIFY(info->supports(QNetworkInformation::Feature::TransportMedium));
+ QVERIFY(info->supports(QNetworkInformation::Feature::Metered));
+}
+
+void tst_QNetworkInformation::reachability()
+{
+ auto info = QNetworkInformation::instance();
+ QNetworkInformation::Reachability boundIsOnline = QNetworkInformation::Reachability::Unknown;
+ bool signalEmitted = false;
+
+ connect(info, &QNetworkInformation::reachabilityChanged, this, [&, info]() {
+ signalEmitted = true;
+ boundIsOnline = info->reachability();
+ });
+ QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Online);
+ MockBackend::setNewReachability(QNetworkInformation::Reachability::Disconnected);
+ QCoreApplication::processEvents();
+ QVERIFY(signalEmitted);
+ QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Disconnected);
+ QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Disconnected);
+
+ // Set the same value again, signal should not be emitted again
+ signalEmitted = false;
+ MockBackend::setNewReachability(QNetworkInformation::Reachability::Disconnected);
+ QCoreApplication::processEvents();
+ QVERIFY(!signalEmitted);
+
+ MockBackend::setNewReachability(QNetworkInformation::Reachability::Local);
+ QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Local);
+ QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Local);
+ MockBackend::setNewReachability(QNetworkInformation::Reachability::Site);
+ QCOMPARE(info->reachability(), QNetworkInformation::Reachability::Site);
+ QCOMPARE(boundIsOnline, QNetworkInformation::Reachability::Site);
+}
+
+void tst_QNetworkInformation::behindCaptivePortal()
+{
+ auto info = QNetworkInformation::instance();
+ bool behindPortal = false;
+ bool signalEmitted = false;
+
+ connect(info, &QNetworkInformation::isBehindCaptivePortalChanged, this,
+ [&, info](bool state) {
+ signalEmitted = true;
+ QCOMPARE(state, info->isBehindCaptivePortal());
+ behindPortal = info->isBehindCaptivePortal();
+ });
+ QVERIFY(!info->isBehindCaptivePortal());
+ MockBackend::setNewBehindCaptivePortal(true);
+ QCoreApplication::processEvents();
+ QVERIFY(signalEmitted);
+ QVERIFY(info->isBehindCaptivePortal());
+ QVERIFY(behindPortal);
+
+ // Set the same value again, signal should not be emitted again
+ signalEmitted = false;
+ MockBackend::setNewBehindCaptivePortal(true);
+ QCoreApplication::processEvents();
+ QVERIFY(!signalEmitted);
+}
+
+void tst_QNetworkInformation::transportMedium()
+{
+ auto info = QNetworkInformation::instance();
+ using TransportMedium = QNetworkInformation::TransportMedium;
+ TransportMedium medium = TransportMedium::Unknown;
+ bool signalEmitted = false;
+
+ connect(info, &QNetworkInformation::transportMediumChanged, this, [&, info](TransportMedium tm) {
+ signalEmitted = true;
+ QCOMPARE(tm, info->transportMedium());
+ medium = info->transportMedium();
+ });
+ QCOMPARE(info->transportMedium(), TransportMedium::Unknown); // Default is unknown
+
+ auto transportMediumEnum = QMetaEnum::fromType<TransportMedium>();
+ auto mediumCount = transportMediumEnum.keyCount();
+ // Verify index 0 is Unknown and skip it in the loop, it's the default.
+ QCOMPARE(TransportMedium(transportMediumEnum.value(0)), TransportMedium::Unknown);
+ for (int i = 1; i < mediumCount; ++i) {
+ signalEmitted = false;
+ TransportMedium m = TransportMedium(transportMediumEnum.value(i));
+ MockBackend::setNewTransportMedium(m);
+ QCoreApplication::processEvents();
+ QVERIFY(signalEmitted);
+ QCOMPARE(info->transportMedium(), m);
+ QCOMPARE(medium, m);
+ }
+
+ // Set the current value again, signal should not be emitted again
+ signalEmitted = false;
+ MockBackend::setNewTransportMedium(medium);
+ QCoreApplication::processEvents();
+ QVERIFY(!signalEmitted);
+}
+
+void tst_QNetworkInformation::isMetered()
+{
+ auto info = QNetworkInformation::instance();
+
+ QSignalSpy spy(info, &QNetworkInformation::isMeteredChanged);
+ QVERIFY(!info->isMetered());
+ MockBackend::setNewMetered(true);
+ QCOMPARE(spy.size(), 1);
+ QVERIFY(info->isMetered());
+ QVERIFY(spy[0][0].toBool());
+ spy.clear();
+
+ // Set the same value again, signal should not be emitted again
+ MockBackend::setNewMetered(true);
+ QCOMPARE(spy.size(), 0);
+}
+
+QTEST_MAIN(tst_QNetworkInformation);
+#include "tst_qnetworkinformation.moc"
diff --git a/tests/auto/network/kernel/qnetworkinformation_appless/CMakeLists.txt b/tests/auto/network/kernel/qnetworkinformation_appless/CMakeLists.txt
new file mode 100644
index 0000000000..d4a2e486bd
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkinformation_appless/CMakeLists.txt
@@ -0,0 +1,15 @@
+# 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_qnetworkinformation_appless LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkinformation_appless
+ SOURCES
+ tst_qnetworkinformation_appless.cpp
+ LIBRARIES
+ Qt::Network
+)
diff --git a/tests/auto/network/kernel/qnetworkinformation_appless/tst_qnetworkinformation_appless.cpp b/tests/auto/network/kernel/qnetworkinformation_appless/tst_qnetworkinformation_appless.cpp
new file mode 100644
index 0000000000..b08843314a
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkinformation_appless/tst_qnetworkinformation_appless.cpp
@@ -0,0 +1,42 @@
+// Copyright (C) 2021 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QtCore/qcoreapplication.h>
+#include <QtNetwork/qnetworkinformation.h>
+#include <QtTest/qtest.h>
+
+class tst_QNetworkInformation_appless : public QObject
+{
+ Q_OBJECT
+private slots:
+ void reinit();
+};
+
+void tst_QNetworkInformation_appless::reinit()
+{
+ int argc = 1;
+ char name[] = "./test";
+ char *argv[] = { name, nullptr };
+
+ {
+ QCoreApplication app(argc, argv);
+ if (QNetworkInformation::availableBackends().isEmpty())
+ QSKIP("No backends available!");
+
+ QVERIFY(QNetworkInformation::loadDefaultBackend());
+ auto info = QNetworkInformation::instance();
+ QVERIFY(info);
+ }
+
+ QVERIFY(!QNetworkInformation::instance());
+
+ {
+ QCoreApplication app(argc, argv);
+ QVERIFY(QNetworkInformation::loadDefaultBackend());
+ auto info = QNetworkInformation::instance();
+ QVERIFY(info);
+ }
+}
+
+QTEST_APPLESS_MAIN(tst_QNetworkInformation_appless);
+#include "tst_qnetworkinformation_appless.moc"
diff --git a/tests/auto/network/kernel/qnetworkinterface/BLACKLIST b/tests/auto/network/kernel/qnetworkinterface/BLACKLIST
deleted file mode 100644
index 33bdf540b6..0000000000
--- a/tests/auto/network/kernel/qnetworkinterface/BLACKLIST
+++ /dev/null
@@ -1,4 +0,0 @@
-# QTBUG-65667
-[localAddress:linklocal-ipv4]
-msvc-2015 ci
-msvc-2017 ci
diff --git a/tests/auto/network/kernel/qnetworkinterface/CMakeLists.txt b/tests/auto/network/kernel/qnetworkinterface/CMakeLists.txt
new file mode 100644
index 0000000000..3e5dab63e0
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkinterface/CMakeLists.txt
@@ -0,0 +1,20 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qnetworkinterface Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qnetworkinterface LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkinterface
+ SOURCES
+ tst_qnetworkinterface.cpp
+ LIBRARIES
+ Qt::NetworkPrivate
+ QT_TEST_SERVER_LIST "apache2"
+)
diff --git a/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro b/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro
deleted file mode 100644
index bbcf60b828..0000000000
--- a/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro
+++ /dev/null
@@ -1,7 +0,0 @@
-CONFIG += testcase
-TARGET = tst_qnetworkinterface
-SOURCES += tst_qnetworkinterface.cpp
-INCLUDEPATH += ../../../../shared/
-HEADERS += ../../../../shared/emulationdetector.h
-
-QT = core network testlib
diff --git a/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp b/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp
index 0c7ba99be5..e5bbf3467c 100644
--- a/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp
+++ b/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp
@@ -1,44 +1,17 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Intel Corporation.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Intel Corporation.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+#include <QTest>
+#include <QtEndian>
+#include <QSet>
#include <qcoreapplication.h>
#include <qnetworkinterface.h>
#include <qudpsocket.h>
-#ifndef QT_NO_BEARERMANAGEMENT
-#include <QNetworkConfigurationManager>
-#include <QNetworkSession>
-#endif
#include "../../../network-settings.h"
-#include "emulationdetector.h"
+
+#include <private/qtnetwork-config_p.h>
Q_DECLARE_METATYPE(QHostAddress)
@@ -54,7 +27,6 @@ public:
private slots:
void initTestCase();
- void cleanupTestCase();
void dump();
void consistencyCheck();
void loopbackIPv4();
@@ -64,13 +36,8 @@ private slots:
void interfaceFromXXX_data();
void interfaceFromXXX();
void copyInvalidInterface();
-
private:
-#ifndef QT_NO_BEARERMANAGEMENT
- QNetworkConfigurationManager *netConfMan;
- QNetworkConfiguration networkConfiguration;
- QScopedPointer<QNetworkSession> networkSession;
-#endif
+ bool hasNetworkServer = false;
};
tst_QNetworkInterface::tst_QNetworkInterface()
@@ -83,43 +50,29 @@ tst_QNetworkInterface::~tst_QNetworkInterface()
bool tst_QNetworkInterface::isIPv6Working()
{
- QUdpSocket socket;
- socket.connectToHost(QHostAddress::LocalHostIPv6, 1234);
- return socket.state() == QAbstractSocket::ConnectedState || socket.waitForConnected(100);
-}
-
-void tst_QNetworkInterface::initTestCase()
-{
- if (!QtNetworkSettings::verifyTestNetworkSettings())
- QSKIP("No network test server available");
-#ifndef QT_NO_BEARERMANAGEMENT
- netConfMan = new QNetworkConfigurationManager(this);
- if (netConfMan->capabilities()
- & QNetworkConfigurationManager::NetworkSessionRequired) {
- networkConfiguration = netConfMan->defaultConfiguration();
- networkSession.reset(new QNetworkSession(networkConfiguration));
- if (!networkSession->isOpen()) {
- networkSession->open();
- QVERIFY(networkSession->waitForOpened(30000));
- }
+ // QNetworkInterface may be unable to detect IPv6 addresses even if they
+ // are there, due to limitations of the implementation.
+ if (QOperatingSystemVersion::currentType() == QOperatingSystemVersion::Windows ||
+ QT_CONFIG(linux_netlink) || (QT_CONFIG(getifaddrs) && QT_CONFIG(ipv6ifname))) {
+ return QtNetworkSettings::hasIPv6();
}
-#endif
+ return false;
}
-void tst_QNetworkInterface::cleanupTestCase()
+void tst_QNetworkInterface::initTestCase()
{
-#ifndef QT_NO_BEARERMANAGEMENT
- if (networkSession && networkSession->isOpen()) {
- networkSession->close();
- }
+#ifdef QT_TEST_SERVER
+ hasNetworkServer = QtNetworkSettings::verifyConnection(QtNetworkSettings::httpServerName(), 80);
+#else
+ hasNetworkServer = QtNetworkSettings::verifyTestNetworkSettings();
#endif
}
void tst_QNetworkInterface::dump()
{
// This is for manual testing:
- QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
- foreach (const QNetworkInterface &i, allInterfaces) {
+ const QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
+ for (const QNetworkInterface &i : allInterfaces) {
QString flags;
if (i.flags() & QNetworkInterface::IsUp) flags += "Up,";
if (i.flags() & QNetworkInterface::IsRunning) flags += "Running,";
@@ -146,7 +99,8 @@ void tst_QNetworkInterface::dump()
qDebug() << " MTU: " << i.maximumTransmissionUnit();
int count = 0;
- foreach (const QNetworkAddressEntry &e, i.addressEntries()) {
+ const auto entries = i.addressEntries();
+ for (const QNetworkAddressEntry &e : entries) {
QDebug s = qDebug();
s.nospace() << " address "
<< qSetFieldWidth(2) << count++ << qSetFieldWidth(0);
@@ -172,11 +126,11 @@ void tst_QNetworkInterface::dump()
void tst_QNetworkInterface::consistencyCheck()
{
- QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
+ const QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
QSet<QString> interfaceNames;
- QVector<int> interfaceIndexes;
+ QList<int> interfaceIndexes;
- foreach (const QNetworkInterface &iface, ifaces) {
+ for (const QNetworkInterface &iface : ifaces) {
QVERIFY(iface.isValid());
QVERIFY2(!interfaceNames.contains(iface.name()),
"duplicate name = " + iface.name().toLocal8Bit());
@@ -215,31 +169,54 @@ void tst_QNetworkInterface::loopbackIPv6()
}
void tst_QNetworkInterface::localAddress_data()
{
+ bool ipv6 = isIPv6Working();
QTest::addColumn<QHostAddress>("target");
QTest::newRow("localhost-ipv4") << QHostAddress(QHostAddress::LocalHost);
- if (isIPv6Working())
+ if (ipv6)
QTest::newRow("localhost-ipv6") << QHostAddress(QHostAddress::LocalHostIPv6);
- QTest::newRow("test-server") << QtNetworkSettings::serverIP();
-
- // Since we don't actually transmit anything, we can list any IPv4 address
- // and it should work. But we're using a linklocal address so that this
- // test can pass even machines that failed to reach a DHCP server.
- QTest::newRow("linklocal-ipv4") << QHostAddress("169.254.0.1");
-
- if (isIPv6Working()) {
- // On the other hand, we can't list just any IPv6 here. It's very
- // likely that this machine has not received a route via ICMPv6-RA or
- // DHCPv6, so it won't have a global route. On some OSes, IPv6 may be
- // enabled per interface, so we need to know which ones work.
- const QList<QHostAddress> addrs = QNetworkInterface::allAddresses();
- for (const QHostAddress &addr : addrs) {
- QString scope = addr.scopeId();
- if (scope.isEmpty())
+ if (hasNetworkServer)
+ QTest::newRow("test-server") << QtNetworkSettings::httpServerIp();
+
+ QSet<QHostAddress> added;
+ const QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
+ for (const QNetworkInterface &iface : ifaces) {
+ if ((iface.flags() & QNetworkInterface::IsUp) == 0)
+ continue;
+ const QList<QNetworkAddressEntry> addrs = iface.addressEntries();
+ for (const QNetworkAddressEntry &entry : addrs) {
+ QHostAddress addr = entry.ip();
+ if (addr.isLoopback())
+ continue; // added above
+
+ if (addr.protocol() == QAbstractSocket::IPv4Protocol) {
+ // add an IPv4 address with bits in the host portion of the address flipped
+ quint32 ip4 = entry.ip().toIPv4Address();
+ addr.setAddress(ip4 ^ ~entry.netmask().toIPv4Address());
+ } else if (!ipv6 || entry.prefixLength() != 64) {
+ continue;
+ } else {
+#ifdef Q_OS_ANDROID
+ // Android seem to not allow IPv6 connection from interfaces other than wlan,
+ // if it's connected, and wlan is connected by default on Android emulators,
+ // so prefer selecting wlan in this test.
+ const QString scopeId = addr.scopeId();
+ if (!scopeId.isEmpty() && !scopeId.startsWith("wlan"))
+ continue;
+#endif
+ // add a random node in this IPv6 network
+ quint64 randomid = qFromBigEndian(Q_UINT64_C(0x8f41f072e5733caa));
+ QIPv6Address ip6 = addr.toIPv6Address();
+ memcpy(&ip6[8], &randomid, sizeof(randomid));
+ addr.setAddress(ip6);
+ }
+
+ if (added.contains(addr))
continue;
- QTest::addRow("linklocal-ipv6-%s", qPrintable(scope))
- << QHostAddress("fe80::1234%" + scope);
+ added.insert(addr);
+
+ QTest::addRow("%s-%s", qPrintable(iface.name()), qPrintable(addr.toString())) << addr;
}
}
}
@@ -282,10 +259,10 @@ void tst_QNetworkInterface::interfaceFromXXX_data()
{
QTest::addColumn<QNetworkInterface>("iface");
- QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
- if (allInterfaces.count() == 0)
+ const QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
+ if (allInterfaces.size() == 0)
QSKIP("No interfaces to test!");
- foreach (QNetworkInterface iface, allInterfaces)
+ for (const QNetworkInterface &iface : allInterfaces)
QTest::newRow(iface.name().toLocal8Bit()) << iface;
}
@@ -296,12 +273,11 @@ void tst_QNetworkInterface::interfaceFromXXX()
QVERIFY(QNetworkInterface::interfaceFromName(iface.name()).isValid());
if (int idx = iface.index()) {
QVERIFY(QNetworkInterface::interfaceFromIndex(idx).isValid());
- if (EmulationDetector::isRunningArmOnX86())
- QEXPECT_FAIL("", "SIOCGIFNAME fails on QEMU", Continue);
QCOMPARE(QNetworkInterface::interfaceNameFromIndex(idx), iface.name());
QCOMPARE(QNetworkInterface::interfaceIndexFromName(iface.name()), idx);
}
- foreach (QNetworkAddressEntry entry, iface.addressEntries()) {
+ const auto entries = iface.addressEntries();
+ for (const QNetworkAddressEntry &entry : entries) {
QVERIFY(!entry.ip().isNull());
if (!entry.netmask().isNull()) {
diff --git a/tests/auto/network/kernel/qnetworkproxy/CMakeLists.txt b/tests/auto/network/kernel/qnetworkproxy/CMakeLists.txt
new file mode 100644
index 0000000000..629bfb0a8a
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkproxy/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qnetworkproxy Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qnetworkproxy LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkproxy
+ SOURCES
+ tst_qnetworkproxy.cpp
+ LIBRARIES
+ Qt::Network
+)
diff --git a/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro b/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro
deleted file mode 100644
index 996f9e3691..0000000000
--- a/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro
+++ /dev/null
@@ -1,9 +0,0 @@
-############################################################
-# Project file for autotest for file qnetworkproxy.h
-############################################################
-
-CONFIG += testcase
-TARGET = tst_qnetworkproxy
-QT = core network testlib
-
-SOURCES += tst_qnetworkproxy.cpp
diff --git a/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp b/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp
index b3e3568321..1f00f8d054 100644
--- a/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp
+++ b/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp
@@ -1,33 +1,8 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-
-#include <QtTest/QtTest>
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
+
+
+#include <QTest>
#include <qcoreapplication.h>
#include <qdebug.h>
diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/CMakeLists.txt b/tests/auto/network/kernel/qnetworkproxyfactory/CMakeLists.txt
new file mode 100644
index 0000000000..41dae6a02a
--- /dev/null
+++ b/tests/auto/network/kernel/qnetworkproxyfactory/CMakeLists.txt
@@ -0,0 +1,19 @@
+# Copyright (C) 2022 The Qt Company Ltd.
+# SPDX-License-Identifier: BSD-3-Clause
+
+#####################################################################
+## tst_qnetworkproxyfactory Test:
+#####################################################################
+
+if(NOT QT_BUILD_STANDALONE_TESTS AND NOT QT_BUILDING_QT)
+ cmake_minimum_required(VERSION 3.16)
+ project(tst_qnetworkproxyfactory LANGUAGES CXX)
+ find_package(Qt6BuildInternals REQUIRED COMPONENTS STANDALONE_TEST)
+endif()
+
+qt_internal_add_test(tst_qnetworkproxyfactory
+ SOURCES
+ tst_qnetworkproxyfactory.cpp
+ LIBRARIES
+ Qt::NetworkPrivate
+)
diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro b/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro
deleted file mode 100644
index a7fa43015f..0000000000
--- a/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro
+++ /dev/null
@@ -1,9 +0,0 @@
-############################################################
-# Project file for autotest for file qnetworkproxy.h (proxy factory part)
-############################################################
-
-CONFIG += testcase
-TARGET = tst_qnetworkproxyfactory
-QT = core network-private testlib
-
-SOURCES += tst_qnetworkproxyfactory.cpp
diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp b/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp
index 90da0b64e2..c38a480766 100644
--- a/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp
+++ b/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp
@@ -1,31 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the test suite of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:GPL-EXCEPT$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3 as published by the Free Software
-** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/QTest>
#include <QtTest/QTestEventLoop>
@@ -53,7 +27,7 @@ public:
class QDebugProxyFactory : public QNetworkProxyFactory
{
public:
- virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query = QNetworkProxyQuery())
+ QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query = QNetworkProxyQuery()) override
{
returnedList = QNetworkProxyFactory::systemProxyForQuery(query);
requestCounter++;
@@ -119,7 +93,8 @@ void tst_QNetworkProxyFactory::systemProxyForQuery_data()
QTest::newRow("autobind-server") << (int)QNetworkProxyQuery::TcpServer << QUrl() << QString() << QString() << 0 << (int)QNetworkProxy::ListeningCapability;
QTest::newRow("web-server") << (int)QNetworkProxyQuery::TcpServer << QUrl() << QString() << QString() << 80 << (int)QNetworkProxy::ListeningCapability;
//windows: these should be bypassed if "bypass proxy server for local addresses" is ticked
- foreach (QHostAddress address, QNetworkInterface::allAddresses()) {
+ const auto addresses = QNetworkInterface::allAddresses();
+ for (const QHostAddress &address : addresses) {
QTest::newRow(qPrintable(address.toString())) << (int)QNetworkProxyQuery::TcpSocket << QUrl() << QString() << address.toString() << 0 << 0;
}
@@ -172,16 +147,15 @@ void tst_QNetworkProxyFactory::systemProxyForQuery() const
QElapsedTimer sw;
sw.start();
- QList<QNetworkProxy> systemProxyList = QNetworkProxyFactory::systemProxyForQuery(query);
+ const QList<QNetworkProxy> systemProxyList = QNetworkProxyFactory::systemProxyForQuery(query);
qDebug() << sw.elapsed() << "ms";
QVERIFY(!systemProxyList.isEmpty());
// for manual comparison with system
qDebug() << systemProxyList;
- foreach (const QNetworkProxy &proxy, systemProxyList) {
+ for (const QNetworkProxy &proxy : systemProxyList)
QVERIFY((requiredCapabilities == 0) || (proxy.capabilities() & requiredCapabilities));
- }
}
void tst_QNetworkProxyFactory::systemProxyForQuery_local()
@@ -256,8 +230,6 @@ void tst_QNetworkProxyFactory::genericSystemProxy()
QFETCH(QString, hostName);
QFETCH(int, port);
-// We can only use the generic system proxy where available:
-#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS) && !QT_CONFIG(libproxy)
qputenv(envVar, url);
const QList<QNetworkProxy> systemProxy = QNetworkProxyFactory::systemProxyForQuery();
QCOMPARE(systemProxy.size(), 1);
@@ -265,18 +237,14 @@ void tst_QNetworkProxyFactory::genericSystemProxy()
QCOMPARE(systemProxy.first().hostName(), hostName);
QCOMPARE(systemProxy.first().port(), static_cast<quint16>(port));
qunsetenv(envVar);
-#else
- Q_UNUSED(envVar)
- Q_UNUSED(url)
- Q_UNUSED(proxyType)
- Q_UNUSED(hostName)
- Q_UNUSED(port)
- QSKIP("Generic system proxy not available on this platform.");
-#endif
}
void tst_QNetworkProxyFactory::genericSystemProxy_data()
{
+ // We can only use the generic system proxy where available:
+#if defined(Q_OS_WIN) || defined(Q_OS_MACOS) || defined(Q_OS_ANDROID) || QT_CONFIG(libproxy)
+ QSKIP("Generic system proxy not available on this platform.");
+#else
QTest::addColumn<QByteArray>("envVar");
QTest::addColumn<QByteArray>("url");
QTest::addColumn<QNetworkProxy::ProxyType>("proxyType");
@@ -289,12 +257,13 @@ void tst_QNetworkProxyFactory::genericSystemProxy_data()
<< QNetworkProxy::Socks5Proxy << QString("127.0.0.1") << 4242;
QTest::newRow("http") << QByteArray("http_proxy") << QByteArray("http://example.com:666")
<< QNetworkProxy::HttpProxy << QString("example.com") << 666;
+#endif
}
class QSPFQThread : public QThread
{
protected:
- virtual void run()
+ void run() override
{
proxies = QNetworkProxyFactory::systemProxyForQuery(query);
}