diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-03-28 13:43:50 +0100 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2019-04-05 05:50:18 +0000 |
commit | 7935d86e7dc4ad994455aba604c8a51b01470cd5 (patch) | |
tree | d5fd285bbbaf83b185306156be010c6855d1e1e3 /tests | |
parent | 808758ad14ab3cc752cb69e9ae791b4fbd462380 (diff) |
Populate test data for reverse lookups using system tools
Hardcoding IP addresses and their respective DNS records is fragile.
We care about Qt producing the same result as other DNS querying tools,
so testing that instead.
Running a python script for this is easiest, and assumed to be quite
reliable.
In case where python fails/is not present, fall back to nslookup.
That tool is available on Linux, macOS, and Windows, although the
output it produces varies. This change implements very basic
line-parsing that can interpret the various results encountered
during testing on those platforms.
This also reverts commit bbaceff253fae13d8e56691bc9de7e1981db5118,
which blacklisted the tests that failed due to changes in DNS
records.
Use the opportunity to replace usage of gitorious.org.
Change-Id: I967de226bd603c805df7fe3ed4e871d92d2d0750
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/network/kernel/qhostinfo/BLACKLIST | 2 | ||||
-rw-r--r-- | tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp | 68 |
2 files changed, 66 insertions, 4 deletions
diff --git a/tests/auto/network/kernel/qhostinfo/BLACKLIST b/tests/auto/network/kernel/qhostinfo/BLACKLIST index cd4d4eb03c..87c5fe991f 100644 --- a/tests/auto/network/kernel/qhostinfo/BLACKLIST +++ b/tests/auto/network/kernel/qhostinfo/BLACKLIST @@ -4,5 +4,3 @@ windows ci [blockingLookup:a-plus-aaaa] windows ci -[reverseLookup:google-public-dns-a.google.com] -ci diff --git a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp index 82825f608c..0a130d363e 100644 --- a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp +++ b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp @@ -396,6 +396,68 @@ void tst_QHostInfo::lookupConnectToLambda() QCOMPARE(tmp.join(' '), expected.join(' ')); } +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"; + 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; + } + return results; +} + void tst_QHostInfo::reverseLookup_data() { QTest::addColumn<QString>("address"); @@ -403,8 +465,8 @@ void tst_QHostInfo::reverseLookup_data() QTest::addColumn<int>("err"); QTest::addColumn<bool>("ipv6"); - QTest::newRow("google-public-dns-a.google.com") << QString("8.8.8.8") << QStringList(QString("google-public-dns-a.google.com")) << 0 << false; - QTest::newRow("gitorious.org") << QString("87.238.52.168") << QStringList(QString("gitorious.org")) << 0 << false; + 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; } @@ -422,6 +484,8 @@ void tst_QHostInfo::reverseLookup() QHostInfo info = QHostInfo::fromName(address); if (err == 0) { + if (!hostNames.contains(info.hostName())) + qDebug() << "Failure: expecting" << hostNames << ",got " << info.hostName(); QVERIFY(hostNames.contains(info.hostName())); QCOMPARE(info.addresses().first(), QHostAddress(address)); } else { |