From e3f0e1be08aeeb73956391683b71c996891f2fe4 Mon Sep 17 00:00:00 2001 From: hjk Date: Thu, 28 Sep 2017 18:46:16 +0200 Subject: [PATCH] Qnx: Use simpler portsgatherer command As indicated by the removed comment, the original version was way too complicated. Change-Id: Ie5d8d8fbe7bd8e44e0538117cd2aaa73958f7d8c Reviewed-by: Christian Kandeler --- src/plugins/qnx/qnxdevice.cpp | 35 +++++-------------- .../utils/stringutils/tst_stringutils.cpp | 12 +++++++ 2 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/plugins/qnx/qnxdevice.cpp b/src/plugins/qnx/qnxdevice.cpp index 31a8d9064d2..01d6fec0f49 100644 --- a/src/plugins/qnx/qnxdevice.cpp +++ b/src/plugins/qnx/qnxdevice.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -59,37 +60,19 @@ class QnxPortsGatheringMethod : public PortsGatheringMethod { Q_UNUSED(protocol); StandardRunnable runnable; - // FIXME: Is this extra shell needed? - runnable.executable = "/bin/sh"; - runnable.commandLineArguments = "-c \"" - "netstat -na " - "| sed 's/[a-z]\\+\\s\\+[0-9]\\+\\s\\+[0-9]\\+\\s\\+\\(\\*\\|[0-9\\.]\\+\\)\\.\\([0-9]\\+\\).*/\\2/g' " - "| while read line; do " - "if [[ $line != udp* ]] && [[ $line != Active* ]]; then " - "printf '%x\n' $line; " - "fi; " - "done" - "\""; + runnable.executable = "netstat"; + runnable.commandLineArguments = "-na"; return runnable; } QList usedPorts(const QByteArray &output) const override { - QList ports; - QList portStrings = output.split('\n'); - portStrings.removeFirst(); - foreach (const QByteArray &portString, portStrings) { - if (portString.isEmpty()) - continue; - bool ok; - const Port port(portString.toInt(&ok, 16)); - if (ok) { - if (!ports.contains(port)) - ports << port; - } else { - qWarning("%s: Unexpected string '%s' is not a port.", - Q_FUNC_INFO, portString.data()); - } + QList ports; + const QList lines = output.split('\n'); + for (const QByteArray &line : lines) { + const Port port(Utils::parseUsedPortFromNetstatOutput(line)); + if (port.isValid() && !ports.contains(port)) + ports.append(port); } return ports; } diff --git a/tests/auto/utils/stringutils/tst_stringutils.cpp b/tests/auto/utils/stringutils/tst_stringutils.cpp index 6fbdfb4326c..8e51c893009 100644 --- a/tests/auto/utils/stringutils/tst_stringutils.cpp +++ b/tests/auto/utils/stringutils/tst_stringutils.cpp @@ -243,6 +243,18 @@ void tst_StringUtils::testParseUsedPortFromNetstatOutput_data() QTest::newRow("Mac6") << "tcp6 0 0 *.631 *.* LISTEN" << 631; QTest::newRow("Mac7") << "udp4 0 0 192.168.79.1.123 *.*" << 123; QTest::newRow("Mac9") << "udp4 0 0 192.168.8.1.123 *.*" << 123; + + // QNX + QTest::newRow("Qnx1") << "Active Internet connections (including servers)" << -1; + QTest::newRow("Qnx2") << "Proto Recv-Q Send-Q Local Address Foreign Address State " << -1; + QTest::newRow("Qnx3") << "tcp 0 0 10.9.7.5.22 10.9.7.4.46592 ESTABLISHED" << 22; + QTest::newRow("Qnx4") << "tcp 0 0 *.8000 *.* LISTEN " << 8000; + QTest::newRow("Qnx5") << "tcp 0 0 *.22 *.* LISTEN " << 22; + QTest::newRow("Qnx6") << "udp 0 0 *.* *.* " << -1; + QTest::newRow("Qnx7") << "udp 0 0 *.* *.* " << -1; + QTest::newRow("Qnx8") << "Active Internet6 connections (including servers)" << -1; + QTest::newRow("Qnx9") << "Proto Recv-Q Send-Q Local Address Foreign Address (state) " << -1; + QTest::newRow("QnxA") << "tcp6 0 0 *.22 *.* LISTEN " << 22; } QTEST_MAIN(tst_StringUtils)