diff options
author | Jo Asplin <jo.asplin@nokia.com> | 2011-09-06 13:46:40 +0200 |
---|---|---|
committer | Jo Asplin <jo.asplin@nokia.com> | 2011-09-09 09:32:17 +0200 |
commit | c59f9ad7768a007ca7a49ea11b16617529e86d52 (patch) | |
tree | d481be2a727f8461a76c2b729fc7e93784a3cff1 /tests/auto/network/kernel | |
parent | 2d41aff1e8557a43268bce631df834bfa79593cf (diff) |
Moved network autotests into new directory structure
Task-number: QTBUG-21223
Change-Id: I55dbf5c42a1c5d938b9e0c9bf7d90457a6c26bbc
Reviewed-on: http://codereview.qt-project.org/4259
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Sergio Ahumada <sergio.ahumada@nokia.com>
Reviewed-by: Rohan McGovern <rohan.mcgovern@nokia.com>
Diffstat (limited to 'tests/auto/network/kernel')
21 files changed, 2341 insertions, 0 deletions
diff --git a/tests/auto/network/kernel/kernel.pro b/tests/auto/network/kernel/kernel.pro new file mode 100644 index 0000000000..d1c901d529 --- /dev/null +++ b/tests/auto/network/kernel/kernel.pro @@ -0,0 +1,14 @@ +TEMPLATE=subdirs +SUBDIRS=\ + qhostinfo \ +# qnetworkproxyfactory \ # Uses a hardcoded proxy configuration + qauthenticator \ + qnetworkproxy \ + qnetworkinterface \ + qnetworkaddressentry \ + qhostaddress \ + +!contains(QT_CONFIG, private_tests): SUBDIRS -= \ + qauthenticator \ + qhostinfo \ + diff --git a/tests/auto/network/kernel/qauthenticator/qauthenticator.pro b/tests/auto/network/kernel/qauthenticator/qauthenticator.pro new file mode 100644 index 0000000000..8ddb13b0df --- /dev/null +++ b/tests/auto/network/kernel/qauthenticator/qauthenticator.pro @@ -0,0 +1,5 @@ +load(qttest_p4) +requires(contains(QT_CONFIG,private_tests)) +QT = core network-private +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 new file mode 100644 index 0000000000..ae78e10440 --- /dev/null +++ b/tests/auto/network/kernel/qauthenticator/tst_qauthenticator.cpp @@ -0,0 +1,155 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the FOO module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtCore/QString> +#include <QtTest/QtTest> +#include <QtCore/QCoreApplication> +#include <QtNetwork/QAuthenticator> + +#include <private/qauthenticator_p.h> + +class tst_QAuthenticator : public QObject +{ + Q_OBJECT + +public: + tst_QAuthenticator(); + +private Q_SLOTS: + void basicAuth(); + void basicAuth_data(); + + void ntlmAuth_data(); + void ntlmAuth(); +}; + +tst_QAuthenticator::tst_QAuthenticator() +{ +} + +void tst_QAuthenticator::basicAuth_data() +{ + QTest::addColumn<QString>("data"); + QTest::addColumn<QString>("realm"); + QTest::addColumn<QString>("user"); + QTest::addColumn<QString>("password"); + QTest::addColumn<QByteArray>("expectedReply"); + + QTest::newRow("just-user") << "" << "" << "foo" << "" << QByteArray("foo:").toBase64(); + QTest::newRow("user-password") << "" << "" << "foo" << "bar" << QByteArray("foo:bar").toBase64(); + QTest::newRow("user-password-realm") << "realm=\"secure area\"" << "secure area" << "foo" << "bar" << QByteArray("foo:bar").toBase64(); +} + +void tst_QAuthenticator::basicAuth() +{ + QFETCH(QString, data); + QFETCH(QString, realm); + QFETCH(QString, user); + QFETCH(QString, password); + QFETCH(QByteArray, expectedReply); + + QAuthenticator auth; + auth.detach(); + QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth); + QVERIFY(priv->phase == QAuthenticatorPrivate::Start); + + QList<QPair<QByteArray, QByteArray> > headers; + headers << qMakePair(QByteArray("WWW-Authenticate"), "Basic " + data.toUtf8()); + priv->parseHttpResponse(headers, /*isProxy = */ false); + + QCOMPARE(auth.realm(), realm); + QCOMPARE(auth.option("realm").toString(), realm); + + auth.setUser(user); + auth.setPassword(password); + + QVERIFY(priv->phase == QAuthenticatorPrivate::Start); + + QCOMPARE(priv->calculateResponse("GET", "/").constData(), ("Basic " + expectedReply).constData()); +} + +void tst_QAuthenticator::ntlmAuth_data() +{ + QTest::addColumn<QString>("data"); + QTest::addColumn<QString>("realm"); + + QTest::newRow("no-realm") << "TlRMTVNTUAACAAAAHAAcADAAAAAFAoEATFZ3OLRQADIAAAAAAAAAAJYAlgBMAAAAUQBUAC0AVABFAFMAVAAtAEQATwBNAEEASQBOAAIAHABRAFQALQBUAEUAUwBUAC0ARABPAE0AQQBJAE4AAQAcAFEAVAAtAFQARQBTAFQALQBTAEUAUgBWAEUAUgAEABYAcQB0AC0AdABlAHMAdAAtAG4AZQB0AAMANABxAHQALQB0AGUAcwB0AC0AcwBlAHIAdgBlAHIALgBxAHQALQB0AGUAcwB0AC0AbgBlAHQAAAAAAA==" << ""; + QTest::newRow("with-realm") << "TlRMTVNTUAACAAAADAAMADgAAAAFAoECWCZkccFFAzwAAAAAAAAAAL4AvgBEAAAABQLODgAAAA9NAEcARABOAE8ASwACAAwATQBHAEQATgBPAEsAAQAcAE4ATwBLAC0AQQBNAFMAUwBTAEYARQAtADAAMQAEACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQADAD4AbgBvAGsALQBhAG0AcwBzAHMAZgBlAC0AMAAxAC4AbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAFACAAbQBnAGQAbgBvAGsALgBuAG8AawBpAGEALgBjAG8AbQAAAAAA" << "NOE"; +} + +void tst_QAuthenticator::ntlmAuth() +{ + QFETCH(QString, data); + QFETCH(QString, realm); + + QAuthenticator auth; + auth.detach(); + QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(auth); + QVERIFY(priv->phase == QAuthenticatorPrivate::Start); + + QList<QPair<QByteArray, QByteArray> > 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); + QCOMPARE(priv->calculateResponse("GET", "/").constData(), "NTLM TlRMTVNTUAABAAAABQIAAAAAAAAAAAAAAAAAAAAAAAA="); + + // NTLM phase 2: challenge + headers.clear(); + headers << qMakePair(QByteArray("WWW-Authenticate"), "NTLM " + data.toUtf8()); + priv->parseHttpResponse(headers, /*isProxy = */ false); + + QEXPECT_FAIL("with-realm", "NTLM authentication code doesn't extract the realm", Continue); + QCOMPARE(auth.realm(), realm); + + auth.setUser("unimportant"); + auth.setPassword("unimportant"); + + QVERIFY(!priv->calculateResponse("GET", "/").isEmpty()); +} + +QTEST_MAIN(tst_QAuthenticator); + +#include "tst_qauthenticator.moc" diff --git a/tests/auto/network/kernel/qhostaddress/.gitignore b/tests/auto/network/kernel/qhostaddress/.gitignore new file mode 100644 index 0000000000..dc39ac12d4 --- /dev/null +++ b/tests/auto/network/kernel/qhostaddress/.gitignore @@ -0,0 +1 @@ +tst_qhostaddress diff --git a/tests/auto/network/kernel/qhostaddress/qhostaddress.pro b/tests/auto/network/kernel/qhostaddress/qhostaddress.pro new file mode 100644 index 0000000000..7bcbfb0165 --- /dev/null +++ b/tests/auto/network/kernel/qhostaddress/qhostaddress.pro @@ -0,0 +1,15 @@ +load(qttest_p4) +SOURCES += tst_qhostaddress.cpp + + +QT = core network + +win32: { +wince*: { + LIBS += -lws2 +} else { + LIBS += -lws2_32 +} +} + +symbian: TARGET.CAPABILITY = NetworkServices diff --git a/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp new file mode 100644 index 0000000000..b20e07be03 --- /dev/null +++ b/tests/auto/network/kernel/qhostaddress/tst_qhostaddress.cpp @@ -0,0 +1,611 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <qcoreapplication.h> +#include <QtTest/QtTest> +#include <qhostaddress.h> +#include <qplatformdefs.h> +#include <qdebug.h> +#include <qhash.h> +#include <qbytearray.h> +#include <qdatastream.h> + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QHostAddress : public QObject +{ + Q_OBJECT + +public: + tst_QHostAddress(); + virtual ~tst_QHostAddress(); + + +public slots: + void init(); + void cleanup(); +private slots: + void constructor_QString_data(); + void constructor_QString(); + void setAddress_QString_data(); + void setAddress_QString(); + void specialAddresses_data(); + void specialAddresses(); + void compare_data(); + void compare(); + void assignment(); + void scopeId(); + void hashKey(); + void streaming_data(); + void streaming(); + void parseSubnet_data(); + void parseSubnet(); + void isInSubnet_data(); + void isInSubnet(); +}; + +QT_BEGIN_NAMESPACE +namespace QTest { + template<> + char *toString(const QHostAddress &addr) + { + if (addr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol) + return qstrdup("<invalid>"); + return qstrdup(addr.toString().toLatin1()); + } +} +QT_END_NAMESPACE + +tst_QHostAddress::tst_QHostAddress() +{ +} + +tst_QHostAddress::~tst_QHostAddress() +{ +} + +Q_DECLARE_METATYPE(QHostAddress) + +void tst_QHostAddress::init() +{ + qRegisterMetaType<QHostAddress>("QHostAddress"); +} + +void tst_QHostAddress::cleanup() +{ + // No cleanup is required. +} + +void tst_QHostAddress::constructor_QString_data() +{ + setAddress_QString_data(); +} + +void tst_QHostAddress::constructor_QString() +{ + QFETCH(QString, address); + QFETCH(bool, ok); + QFETCH(int, protocol); + + QHostAddress hostAddr(address); + + if (address == "0.0.0.0" || address == "::") { + QVERIFY(ok); + } else { + QVERIFY(hostAddr.isNull() != ok); + } + + if (ok) + QTEST(hostAddr.toString(), "resAddr"); + + if ( protocol == 4 ) { + QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol ); + QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol ); + } else if ( protocol == 6 ) { + QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol ); + QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol ); + } else { + QVERIFY( hostAddr.isNull() ); + QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol ); + } +} + +void tst_QHostAddress::setAddress_QString_data() +{ + QTest::addColumn<QString>("address"); + QTest::addColumn<bool>("ok"); + QTest::addColumn<QString>("resAddr"); + QTest::addColumn<int>("protocol"); // 4: IPv4, 6: IPv6, other: undefined + + //next we fill it with data + QTest::newRow("ip4_00") << QString("127.0.0.1") << (bool)TRUE << QString("127.0.0.1") << 4; + QTest::newRow("ip4_01") << QString("255.3.2.1") << (bool)TRUE << QString("255.3.2.1") << 4; + QTest::newRow("ip4_03") << QString(" 255.3.2.1") << (bool)TRUE << QString("255.3.2.1") << 4; + QTest::newRow("ip4_04") << QString("255.3.2.1\r ") << (bool)TRUE << QString("255.3.2.1") << 4; + QTest::newRow("ip4_05") << QString("0.0.0.0") << (bool)TRUE << QString("0.0.0.0") << 4; + + // for the format of IPv6 addresses see also RFC 5952 + QTest::newRow("ip6_00") << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)TRUE << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << 6; + QTest::newRow("ip6_01") << QString("1080:0000:0000:0000:0008:0800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_02") << QString("1080:0:0:0:8:800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_03") << QString("1080::8:800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6; + QTest::newRow("ip6_04") << QString("FF01::43") << (bool)TRUE << QString("FF01::43") << 6; + QTest::newRow("ip6_05") << QString("::1") << (bool)TRUE << QString("::1") << 6; + QTest::newRow("ip6_06") << QString("1::") << (bool)TRUE << QString("1::") << 6; + QTest::newRow("ip6_07") << QString("::") << (bool)TRUE << QString("::") << 6; + QTest::newRow("ip6_08") << QString("0:0:0:0:0:0:13.1.68.3") << (bool)TRUE << QString("::D01:4403") << 6; + QTest::newRow("ip6_09") << QString("::13.1.68.3") << (bool)TRUE << QString("::D01:4403") << 6; + QTest::newRow("ip6_10") << QString("0:0:0:0:0:FFFF:129.144.52.38") << (bool)TRUE << QString("::FFFF:8190:3426") << 6; + QTest::newRow("ip6_11") << QString("::FFFF:129.144.52.38") << (bool)TRUE << QString("::FFFF:8190:3426") << 6; + QTest::newRow("ip6_12") << QString("1::FFFF:129.144.52.38") << (bool)TRUE << QString("1::FFFF:8190:3426") << 6; + QTest::newRow("ip6_13") << QString("A:B::D:E") << (bool)TRUE << QString("A:B::D:E") << 6; + QTest::newRow("ip6_14") << QString("1080:0:1:0:8:800:200C:417A") << (bool)TRUE << QString("1080:0:1:0:8:800:200C:417A") << 6; + QTest::newRow("ip6_15") << QString("1080:0:1:0:8:800:200C:0") << (bool)TRUE << QString("1080:0:1:0:8:800:200C:0") << 6; + QTest::newRow("ip6_16") << QString("1080:0:1:0:8:800:0:0") << (bool)TRUE << QString("1080:0:1:0:8:800::") << 6; + QTest::newRow("ip6_17") << QString("1080:0:0:0:8:800:0:0") << (bool)TRUE << QString("1080::8:800:0:0") << 6; + QTest::newRow("ip6_18") << QString("0:1:1:1:8:800:0:0") << (bool)TRUE << QString("0:1:1:1:8:800::") << 6; + QTest::newRow("ip6_19") << QString("0:1:1:1:8:800:0:1") << (bool)TRUE << QString("0:1:1:1:8:800:0:1") << 6; + + QTest::newRow("error_00") << QString("foobarcom") << (bool)FALSE << QString() << 0; + QTest::newRow("error_01") << QString("foo.bar.com") << (bool)FALSE << QString() << 0; + QTest::newRow("error_02") << QString("") << (bool)FALSE << QString() << 0; + QTest::newRow("error_03") << QString() << (bool)FALSE << QString() << 0; + QTest::newRow("error_04") << QString(" \t\r") << (bool)FALSE << QString() << 0; + + QTest::newRow("error_ip4_00") << QString("256.9.9.9") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip4_01") << QString("-1.9.9.9") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip4_02") << QString("123.0.0") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip4_03") << QString("123.0.0.0.0") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip4_04") << QString("255.2 3.2.1") << (bool)FALSE << QString() << 0; + + QTest::newRow("error_ip6_00") << QString(":") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_01") << QString(":::") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_02") << QString("::AAAA:") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_03") << QString(":AAAA::") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_04") << QString("FFFF:::129.144.52.38") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_05") << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:1234") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_06") << QString("129.144.52.38::") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_07") << QString("::129.144.52.38:129.144.52.38") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_08") << QString(":::129.144.52.38") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_09") << QString("1FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_10") << QString("::FFFFFFFF") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_11") << QString("::EFGH") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_12") << QString("ABCD:ABCD:ABCD") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_13") << QString("::ABCD:ABCD::") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_14") << QString("1::2::3") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_15") << QString("1:2:::") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_16") << QString(":::1:2") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_17") << QString("1:::2") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_18") << QString("FEDC::7654:3210:FEDC:BA98::3210") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_19") << QString("ABCD:ABCD:ABCD:1.2.3.4") << (bool)FALSE << QString() << 0; + QTest::newRow("error_ip6_20") << QString("ABCD::ABCD::ABCD:1.2.3.4") << (bool)FALSE << QString() << 0; + +} + +void tst_QHostAddress::setAddress_QString() +{ + QFETCH(QString, address); + QFETCH(bool, ok); + QFETCH(int, protocol); + + QHostAddress hostAddr; + QVERIFY(hostAddr.setAddress(address) == ok); + + if (ok) + QTEST(hostAddr.toString(), "resAddr"); + + if ( protocol == 4 ) { + QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol ); + QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol ); + } else if ( protocol == 6 ) { + QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol ); + QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol ); + } else { + QVERIFY( hostAddr.isNull() ); + QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol ); + } +} + +void tst_QHostAddress::specialAddresses_data() +{ + QTest::addColumn<QString>("text"); + QTest::addColumn<int>("address"); + QTest::addColumn<bool>("result"); + + QTest::newRow("localhost_1") << QString("127.0.0.1") << (int)QHostAddress::LocalHost << true; + QTest::newRow("localhost_2") << QString("127.0.0.2") << (int)QHostAddress::LocalHost << false; + QTest::newRow("localhost_3") << QString("127.0.0.2") << (int)QHostAddress::LocalHostIPv6 << false; + + QTest::newRow("localhost_ipv6_4") << QString("::1") << (int)QHostAddress::LocalHostIPv6 << true; + QTest::newRow("localhost_ipv6_5") << QString("::2") << (int)QHostAddress::LocalHostIPv6 << false; + QTest::newRow("localhost_ipv6_6") << QString("::1") << (int)QHostAddress::LocalHost << false; + + QTest::newRow("null_1") << QString("") << (int)QHostAddress::Null << true; + QTest::newRow("null_2") << QString("bjarne") << (int)QHostAddress::Null << true; + + QTest::newRow("compare_from_null") << QString("") << (int)QHostAddress::Broadcast << false; + + QTest::newRow("broadcast_1") << QString("255.255.255.255") << (int)QHostAddress::Any << false; + QTest::newRow("broadcast_2") << QString("255.255.255.255") << (int)QHostAddress::Broadcast << true; + + QTest::newRow("any_ipv6") << QString("::") << (int)QHostAddress::AnyIPv6 << true; + QTest::newRow("any_ipv4") << QString("0.0.0.0") << (int)QHostAddress::AnyIPv4 << true; + + QTest::newRow("dual_not_ipv6") << QString("::") << (int)QHostAddress::Any << false; + QTest::newRow("dual_not_ipv4") << QString("0.0.0.0") << (int)QHostAddress::Any << false; +} + + +void tst_QHostAddress::specialAddresses() +{ + QFETCH(QString, text); + QFETCH(int, address); + QFETCH(bool, result); + QVERIFY((QHostAddress(text) == (QHostAddress::SpecialAddress)address) == result); + + QHostAddress setter; + setter.setAddress(text); + if (result) { + QVERIFY(setter == (QHostAddress::SpecialAddress) address); + } else { + QVERIFY(!((QHostAddress::SpecialAddress) address == setter)); + } +} + + +void tst_QHostAddress::compare_data() +{ + QTest::addColumn<QHostAddress>("first"); + QTest::addColumn<QHostAddress>("second"); + QTest::addColumn<bool>("result"); + + QTest::newRow("1") << QHostAddress() << QHostAddress() << true; + QTest::newRow("2") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::Any) << true; + QTest::newRow("3") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6) << true; + QTest::newRow("4") << QHostAddress(QHostAddress::Broadcast) << QHostAddress(QHostAddress::Broadcast) << true; + QTest::newRow("5") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Broadcast) << false; + QTest::newRow("6") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHostIPv6) << false; + QTest::newRow("7") << QHostAddress() << QHostAddress(QHostAddress::LocalHostIPv6) << false; +} + +void tst_QHostAddress::compare() +{ + QFETCH(QHostAddress, first); + QFETCH(QHostAddress, second); + QFETCH(bool, result); + + QCOMPARE(first == second, result); +} + +void tst_QHostAddress::assignment() +{ + QHostAddress address; + address = "127.0.0.1"; + QCOMPARE(address, QHostAddress("127.0.0.1")); + + address = "::1"; + QCOMPARE(address, QHostAddress("::1")); + + 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); +} + +void tst_QHostAddress::scopeId() +{ + QHostAddress address("fe80::2e0:4cff:fefb:662a%eth0"); + QCOMPARE(address.scopeId(), QString("eth0")); + QCOMPARE(address.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0")); + + QHostAddress address2("fe80::2e0:4cff:fefb:662a"); + QCOMPARE(address2.scopeId(), QString()); + address2.setScopeId(QString("en0")); + QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%en0")); + + address2 = address; + QCOMPARE(address2.scopeId(), QString("eth0")); + QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0")); +} + +void tst_QHostAddress::hashKey() +{ + QHash<QHostAddress, QString> hostHash; + hostHash.insert(QHostAddress(), "ole"); +} + +void tst_QHostAddress::streaming_data() +{ + QTest::addColumn<QHostAddress>("address"); + QTest::newRow("1") << QHostAddress(); + QTest::newRow("2") << QHostAddress(0xDEADBEEF); + QTest::newRow("3") << QHostAddress("127.128.129.130"); + QTest::newRow("4") << QHostAddress("1080:0000:0000:0000:0008:0800:200C:417A"); + QTest::newRow("5") << QHostAddress("fe80::2e0:4cff:fefb:662a%eth0"); + QTest::newRow("6") << QHostAddress(QHostAddress::Null); + QTest::newRow("7") << QHostAddress(QHostAddress::LocalHost); + QTest::newRow("8") << QHostAddress(QHostAddress::LocalHostIPv6); + QTest::newRow("9") << QHostAddress(QHostAddress::Broadcast); + QTest::newRow("10") << QHostAddress(QHostAddress::Any); + QTest::newRow("11") << QHostAddress(QHostAddress::AnyIPv4); + QTest::newRow("12") << QHostAddress(QHostAddress::AnyIPv6); + QTest::newRow("13") << QHostAddress("foo.bar.com"); +} + +void tst_QHostAddress::streaming() +{ + QFETCH(QHostAddress, address); + QByteArray ba; + QDataStream ds1(&ba, QIODevice::WriteOnly); + ds1 << address; + QVERIFY(ds1.status() == QDataStream::Ok); + QDataStream ds2(&ba, QIODevice::ReadOnly); + QHostAddress address2; + ds2 >> address2; + QVERIFY(ds2.status() == QDataStream::Ok); + QCOMPARE(address, address2); +} + +void tst_QHostAddress::parseSubnet_data() +{ + QTest::addColumn<QString>("subnet"); + QTest::addColumn<QHostAddress>("prefix"); + QTest::addColumn<int>("prefixLength"); + + // invalid/error values + QTest::newRow("empty") << QString() << QHostAddress() << -1; + QTest::newRow("invalid_01") << "foobar" << QHostAddress() << -1; + QTest::newRow("invalid_02") << " " << QHostAddress() << -1; + QTest::newRow("invalid_03") << "1.2.3.a" << QHostAddress() << -1; + QTest::newRow("invalid_04") << "1.2.3.4.5" << QHostAddress() << -1; + QTest::newRow("invalid_05") << "1.2.3.4:80" << QHostAddress() << -1; + QTest::newRow("invalid_06") << "1.2.3.4/33" << QHostAddress() << -1; + QTest::newRow("invalid_07") << "1.2.3.4/-1" << QHostAddress() << -1; + QTest::newRow("invalid_08") << "1.2.3.4/256.0.0.0" << QHostAddress() << -1; + QTest::newRow("invalid_09") << "1.2.3.4/255.253.0.0" << QHostAddress() << -1; + QTest::newRow("invalid_10") << "1.2.3.4/255.0.0.255" << QHostAddress() << -1; + QTest::newRow("invalid_11") << "1.2.3.4." << QHostAddress() << -1; + QTest::newRow("invalid_20") << "ffff::/-1" << QHostAddress() << -1; + QTest::newRow("invalid_21") << "ffff::/129" << QHostAddress() << -1; + QTest::newRow("invalid_22") << "ffff::/255.255.0.0" << QHostAddress() << -1; + QTest::newRow("invalid_23") << "ffff::/ff00::" << QHostAddress() << -1; + + // correct IPv4 with netmask + QTest::newRow("netmask_0") << "0.0.0.0/0.0.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 0; + QTest::newRow("netmask_1") << "0.0.0.0/255.128.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 9; + QTest::newRow("netmask_2") << "0.0.0.0/255.192.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 10; + QTest::newRow("netmask_3") << "0.0.0.0/255.224.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 11; + QTest::newRow("netmask_4") << "0.0.0.0/255.240.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 12; + QTest::newRow("netmask_5") << "0.0.0.0/255.248.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 13; + QTest::newRow("netmask_6") << "0.0.0.0/255.252.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 14; + QTest::newRow("netmask_7") << "0.0.0.0/255.254.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 15; + QTest::newRow("netmask_8") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 16; + QTest::newRow("netmask_16") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::AnyIPv4) << 16; + QTest::newRow("netmask_24") << "0.0.0.0/255.255.255.0" << QHostAddress(QHostAddress::AnyIPv4) << 24; + QTest::newRow("netmask_31") << "0.0.0.0/255.255.255.254" << QHostAddress(QHostAddress::AnyIPv4) << 31; + QTest::newRow("netmask_32") << "0.0.0.0/255.255.255.255" << QHostAddress(QHostAddress::AnyIPv4) << 32; + + // correct IPv4 with prefix + QTest::newRow("prefix_0") << "0.0.0.0/0" << QHostAddress(QHostAddress::AnyIPv4) << 0; + QTest::newRow("prefix_1") << "0.0.0.0/1" << QHostAddress(QHostAddress::AnyIPv4) << 1; + QTest::newRow("prefix_9") << "0.0.0.0/9" << QHostAddress(QHostAddress::AnyIPv4) << 9; + QTest::newRow("prefix_31") << "0.0.0.0/31" << QHostAddress(QHostAddress::AnyIPv4) << 31; + QTest::newRow("prefix_32") << "0.0.0.0/32" << QHostAddress(QHostAddress::AnyIPv4) << 32; + + // correct IPv4 without prefix or netmask + QTest::newRow("classA") << "10" << QHostAddress("10.0.0.0") << 8; + QTest::newRow("classA+dot") << "10." << QHostAddress("10.0.0.0") << 8; + QTest::newRow("classB") << "172.16" << QHostAddress("172.16.0.0") << 16; + QTest::newRow("classB+dot") << "172.16." << QHostAddress("172.16.0.0") << 16; + QTest::newRow("classC") << "192.168.0" << QHostAddress("192.168.0.0") << 24; + QTest::newRow("classC+dot") << "192.168.0" << QHostAddress("192.168.0.0") << 24; + QTest::newRow("full-ipv4") << "192.168.0.1" << QHostAddress("192.168.0.1") << 32; + + // correct IPv6 with prefix + QTest::newRow("ipv6_01") << "::/0" << QHostAddress(QHostAddress::AnyIPv6) << 0; + QTest::newRow("ipv6_03") << "::/3" << QHostAddress(QHostAddress::AnyIPv6) << 3; + QTest::newRow("ipv6_16") << "::/16" << QHostAddress(QHostAddress::AnyIPv6) << 16; + QTest::newRow("ipv6_48") << "::/48" << QHostAddress(QHostAddress::AnyIPv6) << 48; + QTest::newRow("ipv6_127") << "::/127" << QHostAddress(QHostAddress::AnyIPv6) << 127; + QTest::newRow("ipv6_128") << "::/128" << QHostAddress(QHostAddress::AnyIPv6) << 128; + + // tail bit clearing: + QTest::newRow("clear_01") << "255.255.255.255/31" << QHostAddress("255.255.255.254") << 31; + QTest::newRow("clear_08") << "255.255.255.255/24" << QHostAddress("255.255.255.0") << 24; + QTest::newRow("clear_09") << "255.255.255.255/23" << QHostAddress("255.255.254.0") << 23; + QTest::newRow("clear_10") << "255.255.255.255/22" << QHostAddress("255.255.252.0") << 22; + QTest::newRow("clear_11") << "255.255.255.255/21" << QHostAddress("255.255.248.0") << 21; + QTest::newRow("clear_12") << "255.255.255.255/20" << QHostAddress("255.255.240.0") << 20; + QTest::newRow("clear_13") << "255.255.255.255/19" << QHostAddress("255.255.224.0") << 19; + QTest::newRow("clear_14") << "255.255.255.255/18" << QHostAddress("255.255.192.0") << 18; + QTest::newRow("clear_15") << "255.255.255.255/17" << QHostAddress("255.255.128.0") << 17; + QTest::newRow("clear_16") << "255.255.255.255/16" << QHostAddress("255.255.0.0") << 16; + QTest::newRow("clear_24") << "255.255.255.255/8" << QHostAddress("255.0.0.0") << 8; + QTest::newRow("clear_31") << "255.255.255.255/1" << QHostAddress("128.0.0.0") << 1; + QTest::newRow("clear_32") << "255.255.255.255/0" << QHostAddress("0.0.0.0") << 0; + + // same for IPv6: + QTest::newRow("ipv6_clear_01") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127" + << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe") + << 127; + QTest::newRow("ipv6_clear_07") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/121" + << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80") + << 121; + QTest::newRow("ipv6_clear_08") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/120" + << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00") + << 120; + QTest::newRow("ipv6_clear_16") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/112" + << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0") + << 112; + QTest::newRow("ipv6_clear_80") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/48" + << QHostAddress("ffff:ffff:ffff::") + << 48; + QTest::newRow("ipv6_clear_81") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/47" + << QHostAddress("ffff:ffff:fffe::") + << 47; + QTest::newRow("ipv6_clear_82") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/46" + << QHostAddress("ffff:ffff:fffc::") + << 46; + QTest::newRow("ipv6_clear_83") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/45" + << QHostAddress("ffff:ffff:fff8::") + << 45; + QTest::newRow("ipv6_clear_84") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/44" + << QHostAddress("ffff:ffff:fff0::") + << 44; + QTest::newRow("ipv6_clear_85") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/43" + << QHostAddress("ffff:ffff:ffe0::") + << 43; + QTest::newRow("ipv6_clear_86") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/42" + << QHostAddress("ffff:ffff:ffc0::") + << 42; + QTest::newRow("ipv6_clear_87") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/41" + << QHostAddress("ffff:ffff:ff80::") + << 41; + QTest::newRow("ipv6_clear_88") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/40" + << QHostAddress("ffff:ffff:ff00::") + << 40; + QTest::newRow("ipv6_clear_125") << "3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/3" + << QHostAddress("2000::") + << 3; + QTest::newRow("ipv6_clear_127") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1" + << QHostAddress("8000::") + << 1; + QTest::newRow("ipv6_clear_128") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0" + << QHostAddress(QHostAddress::AnyIPv6) + << 0; +} + +void tst_QHostAddress::parseSubnet() +{ + QFETCH(QString, subnet); + QFETCH(QHostAddress, prefix); + QFETCH(int, prefixLength); + + QPair<QHostAddress, int> result = QHostAddress::parseSubnet(subnet); + QCOMPARE(result.first, prefix); + QCOMPARE(result.second, prefixLength); +} + +void tst_QHostAddress::isInSubnet_data() +{ + QTest::addColumn<QHostAddress>("address"); + QTest::addColumn<QHostAddress>("prefix"); + QTest::addColumn<int>("prefixLength"); + QTest::addColumn<bool>("result"); + + // invalid QHostAddresses are never in any subnets + QTest::newRow("invalid_01") << QHostAddress() << QHostAddress() << 32 << false; + QTest::newRow("invalid_02") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv4) << 32 << false; + QTest::newRow("invalid_03") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv4) << 8 << false; + QTest::newRow("invalid_04") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv4) << 0 << false; + QTest::newRow("invalid_05") << QHostAddress() << QHostAddress("255.255.255.0") << 24 << false; + QTest::newRow("invalid_06") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 0 << false; + QTest::newRow("invalid_07") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 32 << false; + QTest::newRow("invalid_08") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 128<< false; + + // and no host address can be in a subnet whose prefix is invalid + QTest::newRow("invalid_20") << QHostAddress(QHostAddress::AnyIPv4) << QHostAddress() << 16 << false; + QTest::newRow("invalid_21") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress() << 16 << false; + QTest::newRow("invalid_22") << QHostAddress(QHostAddress::LocalHost) << QHostAddress() << 16 << false; + QTest::newRow("invalid_23") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress() << 16 << false; + + // negative netmasks don't make sense: + QTest::newRow("invalid_30") << QHostAddress(QHostAddress::AnyIPv4) << QHostAddress(QHostAddress::Any) << -1 << false; + QTest::newRow("invalid_31") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6) << -1 << false; + + // we don't support IPv4 belonging in an IPv6 netmask and vice-versa + QTest::newRow("v4-in-v6") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv6) << 0 << false; + QTest::newRow("v6-in-v4") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::Any) << 0 << false; + QTest::newRow("v4-in-v6mapped") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:255.0.0.0") << 113 << false; + QTest::newRow("v4-in-v6mapped2") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("::ffff:255.0.0.0") << 113 << false; + + // IPv4 correct ones + QTest::newRow("netmask_0") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv4) << 0 << true; + QTest::newRow("netmask_0bis") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("255.255.0.0") << 0 << true; + QTest::newRow("netmask_0ter") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("1.2.3.4") << 0 << true; + QTest::newRow("netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv4) << 1 << true; + QTest::newRow("~netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("128.0.0.0") << 1 << false; + QTest::newRow("netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("128.0.0.0") << 1 << true; + QTest::newRow("~netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("0.0.0.0") << 1 << false; + QTest::newRow("netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("127.0.0.0") << 8 << true; + QTest::newRow("~netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("126.0.0.0") << 8 << false; + QTest::newRow("netmask_15") << QHostAddress("10.0.1.255") << QHostAddress("10.0.0.0") << 15 << true; + QTest::newRow("netmask_16") << QHostAddress("172.16.0.1") << QHostAddress("172.16.0.0") << 16 << true; + + // the address is always in the subnet containing its address, regardless of length: + QTest::newRow("same_01") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 1 << true; + QTest::newRow("same_07") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 7 << true; + QTest::newRow("same_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 8 << true; + QTest::newRow("same_24") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 23 << true; + QTest::newRow("same_31") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 31 << true; + QTest::newRow("same_32") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 32 << true; + + // IPv6 correct ones: + QTest::newRow("ipv6_netmask_0") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 0 << true; + QTest::newRow("ipv6_netmask_0bis") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::LocalHostIPv6) << 0 << true; + QTest::newRow("ipv6_netmask_0ter") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("ffff::") << 0 << true; + QTest::newRow("ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 1 << true; + QTest::newRow("ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("8000::") << 1 << true; + QTest::newRow("~ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("8000::") << 1 << false; + QTest::newRow("~ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("::") << 1 << false; + QTest::newRow("ipv6_netmask_47") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 47 << true; + QTest::newRow("ipv6_netmask_48") << QHostAddress("2:3:4::1") << QHostAddress("2:3:4::") << 48 << true; + QTest::newRow("~ipv6_netmask_48") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 48 << false; + QTest::newRow("ipv6_netmask_127") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::") << 127 << true; + QTest::newRow("ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::1") << 128 << true; + QTest::newRow("~ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::0") << 128 << false; +} + +void tst_QHostAddress::isInSubnet() +{ + QFETCH(QHostAddress, address); + QFETCH(QHostAddress, prefix); + QFETCH(int, prefixLength); + + QTEST(address.isInSubnet(prefix, prefixLength), "result"); +} + +QTEST_MAIN(tst_QHostAddress) +#include "tst_qhostaddress.moc" diff --git a/tests/auto/network/kernel/qhostinfo/.gitignore b/tests/auto/network/kernel/qhostinfo/.gitignore new file mode 100644 index 0000000000..432954e8d8 --- /dev/null +++ b/tests/auto/network/kernel/qhostinfo/.gitignore @@ -0,0 +1 @@ +tst_qhostinfo diff --git a/tests/auto/network/kernel/qhostinfo/qhostinfo.pro b/tests/auto/network/kernel/qhostinfo/qhostinfo.pro new file mode 100644 index 0000000000..77ce1867d1 --- /dev/null +++ b/tests/auto/network/kernel/qhostinfo/qhostinfo.pro @@ -0,0 +1,16 @@ +load(qttest_p4) + +SOURCES += tst_qhostinfo.cpp + +QT = core-private network-private + +wince*: { + LIBS += ws2.lib +} else { + win32:LIBS += -lws2_32 +} + +symbian: TARGET.CAPABILITY = NetworkServices +symbian: { + INCLUDEPATH *= $$MW_LAYER_SYSTEMINCLUDE +} diff --git a/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp new file mode 100644 index 0000000000..58b51bcb69 --- /dev/null +++ b/tests/auto/network/kernel/qhostinfo/tst_qhostinfo.cpp @@ -0,0 +1,665 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +// 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> + +// MinGW doesn't provide getaddrinfo(), so we test for Q_OS_WIN +// and Q_CC_GNU, which indirectly tells us whether we're using MinGW. +#if defined(Q_OS_WIN) && defined(Q_CC_GNU) +# define QT_NO_GETADDRINFO +#endif + +#if defined(Q_OS_WIN) && !defined(QT_NO_GETADDRINFO) +# include <winsock2.h> +# include <ws2tcpip.h> +#endif + +#include <QtTest/QtTest> +#include <qcoreapplication.h> +#include <QDebug> +#include <QTcpSocket> +#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 <time.h> +#include <qlibrary.h> +#if defined(Q_OS_WIN32) || defined(Q_OS_WINCE) +#include <windows.h> +#else +#include <unistd.h> +#include <signal.h> +#endif + +#include <qhostinfo.h> +#include "private/qhostinfo_p.h" + +#if !defined(QT_NO_GETADDRINFO) +# if !defined(Q_OS_WINCE) +# include <sys/types.h> +# else +# include <types.h> +# endif +# if defined(Q_OS_UNIX) +# include <sys/socket.h> +# endif +# if !defined(Q_OS_WIN) +# include <netdb.h> +# endif +#endif + +#include "../../../network-settings.h" +#include "../../../../shared/util.h" + +//TESTED_CLASS= +//TESTED_FILES= + +const char * const lupinellaIp = "10.3.4.6"; + + +class tst_QHostInfo : public QObject +{ + Q_OBJECT + +public: + tst_QHostInfo(); + virtual ~tst_QHostInfo(); + + +public slots: + void init(); + void cleanup(); + void initTestCase(); + +private slots: + void getSetCheck(); + void staticInformation(); + void lookupIPv4_data(); + void lookupIPv4(); + void lookupIPv6_data(); + void lookupIPv6(); + void reverseLookup_data(); + void reverseLookup(); + + void blockingLookup_data(); + void blockingLookup(); + + void raceCondition(); + void threadSafety(); + void threadSafetyAsynchronousAPI(); + + void multipleSameLookups(); + void multipleDifferentLookups_data(); + void multipleDifferentLookups(); + + void cache(); + + void abortHostLookup(); + void abortHostLookupInDifferentThread(); +protected slots: + void resultsReady(const QHostInfo &); + +private: + bool ipv6LookupsAvailable; + bool ipv6Available; + bool lookupDone; + int lookupsDoneCounter; + QHostInfo lookupResults; +#ifndef QT_NO_BEARERMANAGEMENT + QNetworkConfigurationManager *netConfMan; + QNetworkConfiguration networkConfiguration; + QScopedPointer<QNetworkSession> networkSession; +#endif +}; + +// Testing get/set functions +void tst_QHostInfo::getSetCheck() +{ + QHostInfo obj1; + // HostInfoError QHostInfo::error() + // void QHostInfo::setError(HostInfoError) + obj1.setError(QHostInfo::HostInfoError(0)); + QCOMPARE(QHostInfo::HostInfoError(0), obj1.error()); + obj1.setError(QHostInfo::HostInfoError(1)); + QCOMPARE(QHostInfo::HostInfoError(1), obj1.error()); + + // int QHostInfo::lookupId() + // void QHostInfo::setLookupId(int) + obj1.setLookupId(0); + QCOMPARE(0, obj1.lookupId()); + obj1.setLookupId(INT_MIN); + QCOMPARE(INT_MIN, obj1.lookupId()); + obj1.setLookupId(INT_MAX); + QCOMPARE(INT_MAX, obj1.lookupId()); +} + +void tst_QHostInfo::staticInformation() +{ + qDebug() << "Hostname:" << QHostInfo::localHostName(); + qDebug() << "Domain name:" << QHostInfo::localDomainName(); +} + +tst_QHostInfo::tst_QHostInfo() +{ + Q_SET_DEFAULT_IAP +} + +tst_QHostInfo::~tst_QHostInfo() +{ +} + +void tst_QHostInfo::initTestCase() +{ +#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(); + QVERIFY(networkSession->waitForOpened(30000)); + } +#endif + +#ifdef Q_OS_SYMBIAN + ipv6Available = true; + ipv6LookupsAvailable = true; +#else + ipv6Available = false; + ipv6LookupsAvailable = false; + + QTcpServer server; + if (server.listen(QHostAddress("::1"))) { + // We have IPv6 support + ipv6Available = true; + } + +#if !defined(QT_NO_GETADDRINFO) + // check if the system getaddrinfo can do IPv6 lookups + struct addrinfo hint, *result = 0; + memset(&hint, 0, sizeof hint); + hint.ai_family = AF_UNSPEC; +# ifdef AI_ADDRCONFIG + hint.ai_flags = AI_ADDRCONFIG; +# endif + + int res = getaddrinfo("::1", "80", &hint, &result); + if (res == 0) { + // this test worked + freeaddrinfo(result); + res = getaddrinfo("ipv6-test.dev.troll.no", "80", &hint, &result); + if (res == 0 && result != 0 && result->ai_family != AF_INET) { + freeaddrinfo(result); + ipv6LookupsAvailable = true; + } + } +#endif +#endif + + // run each testcase with and without test enabled + QTest::addColumn<bool>("cache"); + QTest::newRow("WithCache") << true; + QTest::newRow("WithoutCache") << false; +} + +void tst_QHostInfo::init() +{ + // delete the cache so inidividual testcase results are independent from each other + qt_qhostinfo_clear_cache(); + + QFETCH_GLOBAL(bool, cache); + qt_qhostinfo_enable_cache(cache); +} + +void tst_QHostInfo::cleanup() +{ +} + +void tst_QHostInfo::lookupIPv4_data() +{ + QTest::addColumn<QString>("hostname"); + 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") << "lupinella.troll.no" << lupinellaIp << int(QHostInfo::NoError); + QTest::newRow("multiple_ip4") << "multi.dev.troll.no" << "1.2.3.4 1.2.3.5 10.3.3.31" << int(QHostInfo::NoError); + QTest::newRow("literal_ip4") << lupinellaIp << lupinellaIp << int(QHostInfo::NoError); + + QTest::newRow("notfound") << "this-name-does-not-exist-hopefully." << "" << int(QHostInfo::HostNotFound); + + QTest::newRow("idn-ace") << "xn--alqualond-34a.troll.no" << "10.3.3.55" << int(QHostInfo::NoError); + QTest::newRow("idn-unicode") << QString::fromLatin1("alqualond\353.troll.no") << "10.3.3.55" << int(QHostInfo::NoError); +} + +void tst_QHostInfo::lookupIPv4() +{ + QFETCH(QString, hostname); + QFETCH(int, err); + QFETCH(QString, addresses); + + lookupDone = false; + QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(const 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); + + 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(" ")); +} + +void tst_QHostInfo::lookupIPv6_data() +{ + QTest::addColumn<QString>("hostname"); + QTest::addColumn<QString>("addresses"); + QTest::addColumn<int>("err"); + + QTest::newRow("ipv6-net") << "www.ipv6-net.org" << "62.93.217.177 2001:618:1401::4" << int(QHostInfo::NoError); + QTest::newRow("ipv6-test") << "ipv6-test.dev.troll.no" << "2001:638:a00:2::2" << int(QHostInfo::NoError); + QTest::newRow("dns6-test") << "dns6-test-dev.troll.no" << "2001:470:1f01:115::10" << int(QHostInfo::NoError); + QTest::newRow("multi-dns6") << "multi-dns6-test-dev.troll.no" << "2001:470:1f01:115::11 2001:470:1f01:115::12" << int(QHostInfo::NoError); + QTest::newRow("dns46-test") << "dns46-test-dev.troll.no" << "10.3.4.90 2001:470:1f01:115::13" << int(QHostInfo::NoError); + + // avoid using real IPv6 addresses here because this will do a DNS query + // real addresses are between 2000:: and 3fff:ffff:ffff:ffff:ffff:ffff:ffff + QTest::newRow("literal_ip6") << "f001:6b0:1:ea:202:a5ff:fecd:13a6" << "f001:6b0:1:ea:202:a5ff:fecd:13a6" << int(QHostInfo::NoError); + QTest::newRow("literal_shortip6") << "f001:618:1401::4" << "f001:618:1401::4" << int(QHostInfo::NoError); +} + +void tst_QHostInfo::lookupIPv6() +{ + QFETCH(QString, hostname); + QFETCH(int, err); + QFETCH(QString, addresses); + + if (!ipv6LookupsAvailable) + QSKIP("This platform does not support IPv6 lookups", SkipAll); + + lookupDone = false; + QHostInfo::lookupHost(hostname, this, SLOT(resultsReady(const 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()); +} + +void tst_QHostInfo::reverseLookup_data() +{ + QTest::addColumn<QString>("address"); + QTest::addColumn<QStringList>("hostNames"); + QTest::addColumn<int>("err"); + + QTest::newRow("trolltech.com") << QString("62.70.27.69") << QStringList(QString("diverse.troll.no")) << 0; + + // ### Use internal DNS instead. Discussed with Andreas. + //QTest::newRow("classical.hexago.com") << QString("2001:5c0:0:2::24") << QStringList(QString("classical.hexago.com")) << 0; + QTest::newRow("gitorious.org") << QString("87.238.52.168") << QStringList(QString("gitorious.org")) << 0; + QTest::newRow("bogus-name") << QString("1::2::3::4") << QStringList() << 1; +} + +void tst_QHostInfo::reverseLookup() +{ + QFETCH(QString, address); + QFETCH(QStringList, hostNames); + QFETCH(int, err); + + if (!ipv6LookupsAvailable && hostNames.contains("classical.hexago.com")) { + QSKIP("IPv6 lookups are not supported on this platform", SkipSingle); + } +#if defined(Q_OS_HPUX) && defined(__ia64) + if (hostNames.contains("classical.hexago.com")) + QSKIP("HP-UX 11i does not support IPv6 reverse lookups.", SkipSingle); +#endif + + QHostInfo info = QHostInfo::fromName(address); + + if (err == 0) { + QVERIFY(hostNames.contains(info.hostName())); + QCOMPARE(info.addresses().first(), QHostAddress(address)); + } else { + QCOMPARE(info.hostName(), address); + QCOMPARE(info.error(), QHostInfo::HostNotFound); + } + +} + +void tst_QHostInfo::blockingLookup_data() +{ + lookupIPv4_data(); + if (ipv6LookupsAvailable) + lookupIPv6_data(); +} + +void tst_QHostInfo::blockingLookup() +{ + QFETCH(QString, hostname); + 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()); +} + +void tst_QHostInfo::raceCondition() +{ + for (int i = 0; i < 1000; ++i) { + QTcpSocket socket; + socket.connectToHost("notavalidname.troll.no", 80); + } +} + +class LookupThread : public QThread +{ +protected: + inline void run() + { + QHostInfo info = QHostInfo::fromName("qt.nokia.com"); + QCOMPARE(info.error(), QHostInfo::NoError); + QVERIFY(info.addresses().count() > 0); + QCOMPARE(info.addresses().at(0).toString(), QString("87.238.50.178")); + } +}; + +void tst_QHostInfo::threadSafety() +{ + const int nattempts = 5; +#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN) + const int runs = 10; +#else + const int runs = 100; +#endif + LookupThread thr[nattempts]; + for (int j = 0; j < runs; ++j) { + for (int i = 0; i < nattempts; ++i) + thr[i].start(); + for (int k = nattempts - 1; k >= 0; --k) + thr[k].wait(); + } +} + +class LookupReceiver : public QObject +{ + Q_OBJECT +public slots: + void start(); + void resultsReady(const QHostInfo&); +public: + QHostInfo result; + int numrequests; +}; + +void LookupReceiver::start() +{ + for (int i=0;i<numrequests;i++) + QHostInfo::lookupHost(QString("qt.nokia.com"), this, SLOT(resultsReady(const QHostInfo&))); +} + +void LookupReceiver::resultsReady(const QHostInfo &info) +{ + result = info; + numrequests--; + if (numrequests == 0 || info.error() != QHostInfo::NoError) + QThread::currentThread()->quit(); +} + +void tst_QHostInfo::threadSafetyAsynchronousAPI() +{ + const int nattempts = 10; + const int lookupsperthread = 10; + QList<QThread*> threads; + QList<LookupReceiver*> receivers; + for (int i = 0; i < nattempts; ++i) { + QThread* thread = new QThread; + LookupReceiver* receiver = new LookupReceiver; + 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("87.238.50.178")); + QCOMPARE(receiver->numrequests, 0); + } +} + +// this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all, +// not about getting correct IPs +void tst_QHostInfo::multipleSameLookups() +{ + const int COUNT = 10; + lookupsDoneCounter = 0; + + for (int i = 0; i < COUNT; i++) + QHostInfo::lookupHost("localhost", this, SLOT(resultsReady(const QHostInfo))); + + QElapsedTimer timer; + timer.start(); + while (timer.elapsed() < 10000 && lookupsDoneCounter < COUNT) { + QTestEventLoop::instance().enterLoop(2); + } + QCOMPARE(lookupsDoneCounter, COUNT); +} + +// this test is for the multi-threaded QHostInfo rewrite. It is about getting results at all, +// not about getting correct IPs +void tst_QHostInfo::multipleDifferentLookups_data() +{ + QTest::addColumn<int>("repeats"); + QTest::newRow("1") << 1; + QTest::newRow("2") << 2; + QTest::newRow("5") << 5; + QTest::newRow("10") << 10; +} + +void tst_QHostInfo::multipleDifferentLookups() +{ + QStringList hostnameList; + hostnameList << "www.ovi.com" << "www.nokia.com" << "qt.nokia.com" << "www.trolltech.com" << "troll.no" + << "www.qtcentre.org" << "forum.nokia.com" << "www.nokia.com" << "wiki.forum.nokia.com" + << "www.nokia.com" << "nokia.de" << "127.0.0.1" << "----"; + + QFETCH(int, repeats); + const int COUNT = hostnameList.size(); + lookupsDoneCounter = 0; + + for (int i = 0; i < hostnameList.size(); i++) + for (int j = 0; j < repeats; ++j) + QHostInfo::lookupHost(hostnameList.at(i), this, SLOT(resultsReady(const QHostInfo))); + + QElapsedTimer timer; + timer.start(); + while (timer.elapsed() < 60000 && lookupsDoneCounter < repeats*COUNT) { + QTestEventLoop::instance().enterLoop(2); + //qDebug() << "t:" << timer.elapsed(); + } + QCOMPARE(lookupsDoneCounter, repeats*COUNT); +} + +void tst_QHostInfo::cache() +{ + QFETCH_GLOBAL(bool, cache); + if (!cache) + return; // test makes only sense when cache enabled + + // reset slot counter + lookupsDoneCounter = 0; + + // 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); + QTestEventLoop::instance().enterLoop(5); + QVERIFY(!QTestEventLoop::instance().timeout()); + QVERIFY(valid == false); + QVERIFY(result.addresses().isEmpty()); + + // loopkup second time, result should come directly + valid = false; + result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id); + QVERIFY(valid == true); + QVERIFY(!result.addresses().isEmpty()); + + // clear the cache + qt_qhostinfo_clear_cache(); + + // lookup third time, result should not come directly. + valid = true; + result = qt_qhostinfo_lookup("localhost", this, SLOT(resultsReady(QHostInfo)), &valid, &id); + QTestEventLoop::instance().enterLoop(5); + QVERIFY(!QTestEventLoop::instance().timeout()); + QVERIFY(valid == false); + QVERIFY(result.addresses().isEmpty()); + + // the slot should have been called 2 times. + QCOMPARE(lookupsDoneCounter, 2); +} + +void tst_QHostInfo::resultsReady(const QHostInfo &hi) +{ + lookupDone = true; + lookupResults = hi; + lookupsDoneCounter++; + QTestEventLoop::instance().exitLoop(); +} + +void tst_QHostInfo::abortHostLookup() +{ + //reset counter + lookupsDoneCounter = 0; + bool valid = false; + int id = -1; + QHostInfo result = qt_qhostinfo_lookup("qt.nokia.com", this, 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); +} + +class LookupAborter : public QObject +{ + Q_OBJECT +public slots: + void abort() + { + QHostInfo::abortHostLookup(id); + QThread::currentThread()->quit(); + } +public: + int id; +}; + +void tst_QHostInfo::abortHostLookupInDifferentThread() +{ + //reset counter + lookupsDoneCounter = 0; + bool valid = false; + int id = -1; + QHostInfo result = qt_qhostinfo_lookup("qt.nokia.com", this, SLOT(resultsReady(QHostInfo)), &valid, &id); + QVERIFY(!valid); + QThread thread; + LookupAborter aborter; + aborter.id = id; + aborter.moveToThread(&thread); + connect(&thread, SIGNAL(started()), &aborter, SLOT(abort())); + //it is assumed that the DNS request/response in the backend is slower than it takes to schedule the thread and call abort + thread.start(); + QVERIFY(thread.wait(5000)); + QTestEventLoop::instance().enterLoop(5); + QCOMPARE(lookupsDoneCounter, 0); +} + +QTEST_MAIN(tst_QHostInfo) +#include "tst_qhostinfo.moc" diff --git a/tests/auto/network/kernel/qnetworkaddressentry/.gitignore b/tests/auto/network/kernel/qnetworkaddressentry/.gitignore new file mode 100644 index 0000000000..375dbcfd0f --- /dev/null +++ b/tests/auto/network/kernel/qnetworkaddressentry/.gitignore @@ -0,0 +1 @@ +tst_qnetworkaddressentry diff --git a/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro b/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro new file mode 100644 index 0000000000..885dbf796c --- /dev/null +++ b/tests/auto/network/kernel/qnetworkaddressentry/qnetworkaddressentry.pro @@ -0,0 +1,6 @@ +load(qttest_p4) +SOURCES += tst_qnetworkaddressentry.cpp + +QT = core network + +symbian: TARGET.CAPABILITY = NetworkServices diff --git a/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp b/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp new file mode 100644 index 0000000000..42b9af8dd3 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkaddressentry/tst_qnetworkaddressentry.cpp @@ -0,0 +1,185 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> +#include <qcoreapplication.h> +#include <qnetworkinterface.h> + +// TESTED_FILES=qnetworkinterface.cpp qnetworkinterface.h +Q_DECLARE_METATYPE(QHostAddress) + +class tst_QNetworkAddressEntry: public QObject +{ + Q_OBJECT +private slots: + void getSetCheck(); + void prefixAndNetmask_data(); + void prefixAndNetmask(); +}; + +void tst_QNetworkAddressEntry::getSetCheck() +{ + QNetworkAddressEntry entry; + + QVERIFY(entry.ip().isNull()); + QVERIFY(entry.netmask().isNull()); + QVERIFY(entry.broadcast().isNull()); + QCOMPARE(entry.prefixLength(), -1); + + entry.setIp(QHostAddress::LocalHost); + QCOMPARE(entry.ip(), QHostAddress(QHostAddress::LocalHost)); + entry.setIp(QHostAddress()); + QVERIFY(entry.ip().isNull()); + + entry.setBroadcast(QHostAddress::LocalHost); + QCOMPARE(entry.broadcast(), QHostAddress(QHostAddress::LocalHost)); + entry.setBroadcast(QHostAddress()); + QVERIFY(entry.broadcast().isNull()); + + // netmask and prefix length tested in the next test + entry.setIp(QHostAddress::LocalHost); + entry.setBroadcast(QHostAddress::LocalHost); + + QNetworkAddressEntry entry2; + QVERIFY(entry != entry2); + QVERIFY(!(entry == entry2)); + + entry = entry2; + QCOMPARE(entry, entry2); + QVERIFY(entry == entry); + QVERIFY(!(entry != entry2)); +} + +void tst_QNetworkAddressEntry::prefixAndNetmask_data() +{ + QTest::addColumn<QHostAddress>("ip"); + QTest::addColumn<QHostAddress>("netmask"); + QTest::addColumn<int>("prefix"); + + // IPv4 set: + QHostAddress ipv4(QHostAddress::LocalHost); + QTest::newRow("v4/0") << ipv4 << QHostAddress(QHostAddress::AnyIPv4) << 0; + QTest::newRow("v4/32") << ipv4 << QHostAddress("255.255.255.255") << 32; + QTest::newRow("v4/24") << ipv4 << QHostAddress("255.255.255.0") << 24; + QTest::newRow("v4/23") << ipv4 << QHostAddress("255.255.254.0") << 23; + QTest::newRow("v4/20") << ipv4 << QHostAddress("255.255.240.0") << 20; + QTest::newRow("v4/invalid1") << ipv4 << QHostAddress(QHostAddress::LocalHost) << -1; + QTest::newRow("v4/invalid2") << ipv4 << QHostAddress(QHostAddress::AnyIPv6) << -1; + QTest::newRow("v4/invalid3") << ipv4 << QHostAddress("255.255.253.0") << -1; + QTest::newRow("v4/invalid4") << ipv4 << QHostAddress() << -2; + QTest::newRow("v4/invalid5") << ipv4 << QHostAddress() << 33; + + // IPv6 set: + QHostAddress ipv6(QHostAddress::LocalHostIPv6); + QTest::newRow("v6/0") << ipv6 << QHostAddress(QHostAddress::AnyIPv6) << 0; + QTest::newRow("v6/128") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") << 128; + QTest::newRow("v6/64") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff::") << 64; + QTest::newRow("v6/63") << ipv6 << QHostAddress("ffff:ffff:ffff:fffe::") << 63; + QTest::newRow("v6/60") << ipv6 << QHostAddress("ffff:ffff:ffff:fff0::") << 60; + QTest::newRow("v6/48") << ipv6 << QHostAddress("ffff:ffff:ffff::") << 48; + QTest::newRow("v6/3") << ipv6 << QHostAddress("e000::") << 3; + QTest::newRow("v6/invalid1") << ipv6 << QHostAddress(QHostAddress::LocalHostIPv6) << -1; + QTest::newRow("v6/invalid2") << ipv6 << QHostAddress(QHostAddress::Any) << -1; + QTest::newRow("v6/invalid3") << ipv6 << QHostAddress("fffd::") << -1; + QTest::newRow("v6/invalid4") << ipv6 << QHostAddress() << -2; + QTest::newRow("v6/invalid5") << ipv6 << QHostAddress() << 129; +} + +void tst_QNetworkAddressEntry::prefixAndNetmask() +{ + QFETCH(QHostAddress, ip); + QFETCH(QHostAddress, netmask); + QFETCH(int, prefix); + + QNetworkAddressEntry entry; + + // first, without setting the IP, all must be invalid: + entry.setNetmask(netmask); + QVERIFY(entry.netmask().isNull()); + entry.setPrefixLength(prefix); + QCOMPARE(entry.prefixLength(), -1); + + // set the IP: + entry.setIp(ip); + + // set the netmask: + if (!netmask.isNull()) { + entry.setNetmask(netmask); + + // was it a valid one? + if (prefix != -1) { + QVERIFY(!entry.netmask().isNull()); + QCOMPARE(entry.netmask(), netmask); + QCOMPARE(entry.prefixLength(), prefix); + } else { + // not valid + QVERIFY(entry.netmask().isNull()); + QCOMPARE(entry.prefixLength(), -1); + } + } + entry.setNetmask(QHostAddress()); + QVERIFY(entry.netmask().isNull()); + QCOMPARE(entry.prefixLength(), -1); + + // set the prefix + if (prefix != -1) { + entry.setPrefixLength(prefix); + + // was it a valid one? + if (!netmask.isNull()) { + QVERIFY(!entry.netmask().isNull()); + QCOMPARE(entry.netmask(), netmask); + QCOMPARE(entry.prefixLength(), prefix); + } else { + // not valid + QVERIFY(entry.netmask().isNull()); + QCOMPARE(entry.prefixLength(), -1); + } + } + entry.setPrefixLength(-1); + QVERIFY(entry.netmask().isNull()); + QCOMPARE(entry.prefixLength(), -1); +} + +QTEST_MAIN(tst_QNetworkAddressEntry) +#include "tst_qnetworkaddressentry.moc" + diff --git a/tests/auto/network/kernel/qnetworkinterface/.gitignore b/tests/auto/network/kernel/qnetworkinterface/.gitignore new file mode 100644 index 0000000000..237bc6d0db --- /dev/null +++ b/tests/auto/network/kernel/qnetworkinterface/.gitignore @@ -0,0 +1 @@ +tst_qnetworkinterface diff --git a/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro b/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro new file mode 100644 index 0000000000..1c5feee3f2 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkinterface/qnetworkinterface.pro @@ -0,0 +1,7 @@ +load(qttest_p4) +SOURCES += tst_qnetworkinterface.cpp + +QT = core network + +symbian: TARGET.CAPABILITY = NetworkServices + diff --git a/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp b/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp new file mode 100644 index 0000000000..7a2f0e578e --- /dev/null +++ b/tests/auto/network/kernel/qnetworkinterface/tst_qnetworkinterface.cpp @@ -0,0 +1,241 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <qcoreapplication.h> +#include <qnetworkinterface.h> +#include <qtcpsocket.h> +#include <QNetworkConfigurationManager> +#include <QNetworkSession> +#include "../../../network-settings.h" + +//TESTED_FILES=qnetworkinterface.cpp qnetworkinterface.h qnetworkinterface_unix.cpp qnetworkinterface_win.cpp + +class tst_QNetworkInterface : public QObject +{ + Q_OBJECT + +public: + tst_QNetworkInterface(); + virtual ~tst_QNetworkInterface(); + +private slots: + void initTestCase(); + void cleanupTestCase(); + void dump(); + void loopbackIPv4(); + void loopbackIPv6(); + void localAddress(); + void interfaceFromXXX(); + void copyInvalidInterface(); + +private: +#ifndef QT_NO_BEARER_MANAGEMENT + QNetworkConfigurationManager *netConfMan; + QNetworkConfiguration networkConfiguration; + QScopedPointer<QNetworkSession> networkSession; +#endif +}; + +tst_QNetworkInterface::tst_QNetworkInterface() +{ +} + +tst_QNetworkInterface::~tst_QNetworkInterface() +{ +} + +void tst_QNetworkInterface::initTestCase() +{ +#ifndef QT_NO_BEARERMANAGEMENT + netConfMan = new QNetworkConfigurationManager(this); + networkConfiguration = netConfMan->defaultConfiguration(); + networkSession.reset(new QNetworkSession(networkConfiguration)); + if (!networkSession->isOpen()) { + networkSession->open(); + QVERIFY(networkSession->waitForOpened(30000)); + } +#endif +} + +void tst_QNetworkInterface::cleanupTestCase() +{ +#ifndef QT_NO_BEARERMANAGEMENT + if (networkSession && networkSession->isOpen()) { + networkSession->close(); + } +#endif +} + +void tst_QNetworkInterface::dump() +{ + // This is for manual testing: + QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces(); + foreach (const QNetworkInterface &i, allInterfaces) { + QString flags; + if (i.flags() & QNetworkInterface::IsUp) flags += "Up,"; + if (i.flags() & QNetworkInterface::IsRunning) flags += "Running,"; + if (i.flags() & QNetworkInterface::CanBroadcast) flags += "Broadcast,"; + if (i.flags() & QNetworkInterface::IsLoopBack) flags += "Loopback,"; + if (i.flags() & QNetworkInterface::IsPointToPoint) flags += "PointToPoint,"; + if (i.flags() & QNetworkInterface::CanMulticast) flags += "Multicast,"; + flags.chop(1); // drop last comma + + QString friendlyName = i.humanReadableName(); + if (friendlyName != i.name()) { + friendlyName.prepend('('); + friendlyName.append(')'); + } else { + friendlyName.clear(); + } + qDebug() << "Interface: " << i.name() << qPrintable(friendlyName); + QVERIFY(i.isValid()); + + qDebug() << " index: " << i.index(); + qDebug() << " flags: " << qPrintable(flags); + qDebug() << " hw address:" << qPrintable(i.hardwareAddress()); + + int count = 0; + foreach (const QNetworkAddressEntry &e, i.addressEntries()) { + QDebug s = qDebug(); + s.nospace() << " address " + << qSetFieldWidth(2) << count++ << qSetFieldWidth(0); + s.nospace() << ": " << qPrintable(e.ip().toString()); + if (!e.netmask().isNull()) + s.nospace() << '/' << e.prefixLength() + << " (" << qPrintable(e.netmask().toString()) << ")"; + if (!e.broadcast().isNull()) + s.nospace() << " broadcast " << qPrintable(e.broadcast().toString()); + } + } +} + +void tst_QNetworkInterface::loopbackIPv4() +{ + QList<QHostAddress> all = QNetworkInterface::allAddresses(); + QVERIFY(all.contains(QHostAddress(QHostAddress::LocalHost))); +} + +void tst_QNetworkInterface::loopbackIPv6() +{ + QList<QHostAddress> all = QNetworkInterface::allAddresses(); + + bool loopbackfound = false; + bool anyIPv6 = false; + foreach (QHostAddress addr, all) + if (addr == QHostAddress::LocalHostIPv6) { + loopbackfound = true; + anyIPv6 = true; + break; + } else if (addr.protocol() == QAbstractSocket::IPv6Protocol) + anyIPv6 = true; + + QVERIFY(!anyIPv6 || loopbackfound); +} + +void tst_QNetworkInterface::localAddress() +{ + QTcpSocket socket; + socket.connectToHost(QtNetworkSettings::serverName(), 80); + QVERIFY(socket.waitForConnected(5000)); + + QHostAddress local = socket.localAddress(); + + // make Apache happy on fluke + socket.write("GET / HTTP/1.0\r\n\r\n"); + socket.waitForBytesWritten(1000); + socket.close(); + + // test that we can find the address that QTcpSocket reported + QList<QHostAddress> all = QNetworkInterface::allAddresses(); + QVERIFY(all.contains(local)); +} + +void tst_QNetworkInterface::interfaceFromXXX() +{ + QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces(); + + foreach (QNetworkInterface iface, allInterfaces) { + QVERIFY(QNetworkInterface::interfaceFromName(iface.name()).isValid()); + foreach (QNetworkAddressEntry entry, iface.addressEntries()) { + QVERIFY(!entry.ip().isNull()); + + if (!entry.netmask().isNull()) { + QCOMPARE(entry.netmask().protocol(), entry.ip().protocol()); + + // if the netmask is known, the broadcast is known + // but only for IPv4 (there is no such thing as broadcast in IPv6) + if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) { + QVERIFY(!entry.broadcast().isNull()); + + // verify that the broadcast address is correct + quint32 ip = entry.ip().toIPv4Address(); + quint32 mask = entry.netmask().toIPv4Address(); + quint32 bcast = entry.broadcast().toIPv4Address(); + + QCOMPARE(bcast, ip | ~mask); + } + } + + if (!entry.broadcast().isNull()) + QCOMPARE(entry.broadcast().protocol(), entry.ip().protocol()); + } + } +} + +void tst_QNetworkInterface::copyInvalidInterface() +{ + // Force a copy of an interfaces that isn't likely to exist + QNetworkInterface i = QNetworkInterface::interfaceFromName("plopp"); + QVERIFY(!i.isValid()); + + QCOMPARE(i.index(), 0); + QVERIFY(i.name().isEmpty()); + QVERIFY(i.humanReadableName().isEmpty()); + QVERIFY(i.hardwareAddress().isEmpty()); + QCOMPARE(int(i.flags()), 0); + QVERIFY(i.addressEntries().isEmpty()); +} + +QTEST_MAIN(tst_QNetworkInterface) +#include "tst_qnetworkinterface.moc" diff --git a/tests/auto/network/kernel/qnetworkproxy/.gitignore b/tests/auto/network/kernel/qnetworkproxy/.gitignore new file mode 100644 index 0000000000..74dcafa759 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxy/.gitignore @@ -0,0 +1 @@ +tst_qnetworkproxy diff --git a/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro b/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro new file mode 100644 index 0000000000..fc0a216a15 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxy/qnetworkproxy.pro @@ -0,0 +1,11 @@ +############################################################ +# Project file for autotest for file qnetworkproxy.h +############################################################ + +load(qttest_p4) +QT = core network + +SOURCES += tst_qnetworkproxy.cpp + +symbian: TARGET.CAPABILITY = NetworkServices + diff --git a/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp b/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp new file mode 100644 index 0000000000..884ed0573f --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxy/tst_qnetworkproxy.cpp @@ -0,0 +1,118 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QtTest> + +#include <qcoreapplication.h> +#include <qdebug.h> +#include <qnetworkproxy.h> + +//TESTED_CLASS= +//TESTED_FILES= + +class tst_QNetworkProxy : public QObject +{ +Q_OBJECT + +public: + tst_QNetworkProxy(); + virtual ~tst_QNetworkProxy(); + +private slots: + void getSetCheck(); + void capabilitiesPerType(); +}; + +tst_QNetworkProxy::tst_QNetworkProxy() +{ +} + +tst_QNetworkProxy::~tst_QNetworkProxy() +{ +} + +// Testing get/set functions +void tst_QNetworkProxy::getSetCheck() +{ + QNetworkProxy obj1; + // quint16 QNetworkProxy::port() + // void QNetworkProxy::setPort(quint16) + obj1.setPort(quint16(0)); + QCOMPARE(quint16(0), obj1.port()); + obj1.setPort(quint16(0xffff)); + QCOMPARE(quint16(0xffff), obj1.port()); + + obj1.setType(QNetworkProxy::DefaultProxy); + QCOMPARE(obj1.type(), QNetworkProxy::DefaultProxy); + obj1.setType(QNetworkProxy::HttpProxy); + QCOMPARE(obj1.type(), QNetworkProxy::HttpProxy); + obj1.setType(QNetworkProxy::Socks5Proxy); + QCOMPARE(obj1.type(), QNetworkProxy::Socks5Proxy); +} + +void tst_QNetworkProxy::capabilitiesPerType() +{ + QNetworkProxy proxy(QNetworkProxy::Socks5Proxy); + QVERIFY(proxy.capabilities() & QNetworkProxy::TunnelingCapability); + QVERIFY(proxy.capabilities() & QNetworkProxy::HostNameLookupCapability); + QVERIFY(proxy.capabilities() & QNetworkProxy::UdpTunnelingCapability); + + proxy.setType(QNetworkProxy::NoProxy); + // verify that the capabilities changed + QVERIFY(!(proxy.capabilities() & QNetworkProxy::HostNameLookupCapability)); + QVERIFY(proxy.capabilities() & QNetworkProxy::UdpTunnelingCapability); + + proxy.setType(QNetworkProxy::HttpProxy); + QVERIFY(proxy.capabilities() & QNetworkProxy::HostNameLookupCapability); + QVERIFY(!(proxy.capabilities() & QNetworkProxy::UdpTunnelingCapability)); + + // now set the capabilities on stone: + proxy.setCapabilities(QNetworkProxy::TunnelingCapability | QNetworkProxy::UdpTunnelingCapability); + QCOMPARE(proxy.capabilities(), QNetworkProxy::TunnelingCapability | QNetworkProxy::UdpTunnelingCapability); + + // changing the type shouldn't change the capabilities any more + proxy.setType(QNetworkProxy::Socks5Proxy); + QCOMPARE(proxy.capabilities(), QNetworkProxy::TunnelingCapability | QNetworkProxy::UdpTunnelingCapability); +} + +QTEST_MAIN(tst_QNetworkProxy) +#include "tst_qnetworkproxy.moc" diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/.gitignore b/tests/auto/network/kernel/qnetworkproxyfactory/.gitignore new file mode 100644 index 0000000000..9be26bbda1 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxyfactory/.gitignore @@ -0,0 +1 @@ +tst_qnetworkproxyfactory_symbian diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro b/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro new file mode 100644 index 0000000000..17ad403ba7 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxyfactory/qnetworkproxyfactory.pro @@ -0,0 +1,11 @@ +############################################################ +# Project file for autotest for file qnetworkproxy.h (proxy factory part) +############################################################ + +load(qttest_p4) +QT = core network + +SOURCES += tst_qnetworkproxyfactory.cpp + +symbian: TARGET.CAPABILITY = NetworkServices ReadUserData + diff --git a/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp b/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp new file mode 100644 index 0000000000..1df6c24c10 --- /dev/null +++ b/tests/auto/network/kernel/qnetworkproxyfactory/tst_qnetworkproxyfactory.cpp @@ -0,0 +1,275 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QtTest/QTest> +#include <QtTest/QTestEventLoop> + +#include <qcoreapplication.h> +#include <qdebug.h> +#include <qnetworkproxy.h> + +#include <QNetworkConfiguration> +#include <QNetworkConfigurationManager> +#include <QNetworkSession> +#include <QNetworkAccessManager> +#include <QNetworkReply> +#include <QNetworkRequest> +#include <QList> + +Q_DECLARE_METATYPE(QNetworkConfiguration); +Q_DECLARE_METATYPE(QList<QNetworkProxy>); + +#include <QThread> + +class tst_QNetworkProxyFactory : public QObject { + Q_OBJECT + +public: + tst_QNetworkProxyFactory(); + + class QDebugProxyFactory : public QNetworkProxyFactory + { + public: + virtual QList<QNetworkProxy> queryProxy(const QNetworkProxyQuery &query = QNetworkProxyQuery()) + { + returnedList = QNetworkProxyFactory::systemProxyForQuery(query); + requestCounter++; + return returnedList; + } + QList<QNetworkProxy> returnedList; + int requestCounter; + }; + +private slots: + void systemProxyForQueryCalledFromThread(); + void systemProxyForQuery() const; +#ifndef QT_NO_BEARERMANAGEMENT + void fromConfigurations(); + void inNetworkAccessManager_data(); + void inNetworkAccessManager(); +#endif + +private: + QString formatProxyName(const QNetworkProxy & proxy) const; + QDebugProxyFactory *factory; +}; + +tst_QNetworkProxyFactory::tst_QNetworkProxyFactory() +{ + factory = new QDebugProxyFactory; + QNetworkProxyFactory::setApplicationProxyFactory(factory); +} + +QString tst_QNetworkProxyFactory::formatProxyName(const QNetworkProxy & proxy) const +{ + QString proxyName; + if (!proxy.user().isNull()) + proxyName.append("%1:%2@").arg(proxy.user(), proxy.password()); + proxyName.append(QString("%1:%2").arg(proxy.hostName()).arg(proxy.port())); + proxyName.append(QString(" (type=%1, capabilities=%2)").arg(proxy.type()).arg(proxy.capabilities())); + + return proxyName; +} + +void tst_QNetworkProxyFactory::systemProxyForQuery() const +{ + QNetworkProxyQuery query(QUrl(QString("http://www.abc.com")), QNetworkProxyQuery::UrlRequest); + QList<QNetworkProxy> systemProxyList = QNetworkProxyFactory::systemProxyForQuery(query); + bool pass = true; + QNetworkProxy proxy; + + QList<QNetworkProxy> nativeProxyList; + nativeProxyList << QNetworkProxy(QNetworkProxy::HttpProxy, QString("http://test.proxy.com"), 8080) << QNetworkProxy::NoProxy; + + foreach (proxy, systemProxyList) { + if (!nativeProxyList.contains(proxy)) { + qWarning() << "System proxy not found in native proxy list: " << + formatProxyName(proxy); + pass = false; + } + } + + foreach (proxy, nativeProxyList) { + if (!systemProxyList.contains(proxy)) { + qWarning() << "Native proxy not found in system proxy list: " << + formatProxyName(proxy); + pass = false; + } + } + + if (!pass) + QFAIL("One or more system proxy lookup failures occurred."); +} + +#ifndef QT_NO_BEARERMANAGEMENT + +//Purpose of this test is just to check systemProxyForQuery doesn't hang or crash +//with any given configuration including no configuration. +//We can't test it returns the right proxies without implementing the native proxy code +//again here, which would be testing our implementation against itself. +//Therefore it's just testing that something valid is returned (at least a NoProxy entry) +void tst_QNetworkProxyFactory::fromConfigurations() +{ + QNetworkConfigurationManager manager; + QList<QNetworkProxy> proxies; + QUrl url(QLatin1String("http://qt.nokia.com")); + //get from known configurations + foreach (QNetworkConfiguration config, manager.allConfigurations()) { + QNetworkProxyQuery query(config, url, QNetworkProxyQuery::UrlRequest); + proxies = QNetworkProxyFactory::systemProxyForQuery(query); + QVERIFY(!proxies.isEmpty()); + foreach (QNetworkProxy proxy, proxies) { + qDebug() << config.name() << " - " << config.identifier() << " - " << formatProxyName(proxy); + } + } + + //get from default configuration + QNetworkProxyQuery defaultquery(url, QNetworkProxyQuery::UrlRequest); + proxies = QNetworkProxyFactory::systemProxyForQuery(defaultquery); + QVERIFY(!proxies.isEmpty()); + foreach (QNetworkProxy proxy, proxies) { + qDebug() << "default - " << formatProxyName(proxy); + } + + //get from active configuration + QNetworkSession session(manager.defaultConfiguration()); + session.open(); + QVERIFY(session.waitForOpened(30000)); + proxies = QNetworkProxyFactory::systemProxyForQuery(defaultquery); + QVERIFY(!proxies.isEmpty()); + foreach (QNetworkProxy proxy, proxies) { + qDebug() << "active - " << formatProxyName(proxy); + } + + //get from known configurations while there is one active + foreach (QNetworkConfiguration config, manager.allConfigurations()) { + QNetworkProxyQuery query(config, url, QNetworkProxyQuery::UrlRequest); + proxies = QNetworkProxyFactory::systemProxyForQuery(query); + QVERIFY(!proxies.isEmpty()); + foreach (QNetworkProxy proxy, proxies) { + qDebug() << config.name() << " - " << config.identifier() << " - " << formatProxyName(proxy); + } + } +} + +void tst_QNetworkProxyFactory::inNetworkAccessManager_data() +{ + QTest::addColumn<QNetworkConfiguration>("config"); + QTest::addColumn<QList<QNetworkProxy> >("proxies"); + QNetworkConfigurationManager manager; + //get from known configurations + foreach (QNetworkConfiguration config, manager.allConfigurations()) { + QNetworkProxyQuery query(config, QUrl(QString("http://qt.nokia.com")), QNetworkProxyQuery::UrlRequest); + QList<QNetworkProxy> proxies = QNetworkProxyFactory::systemProxyForQuery(query); + QTest::newRow(config.name().toUtf8()) << config << proxies; + } +} + +//Purpose of this test is to check that QNetworkAccessManager uses the proxy from the configuration it +//has been given. Needs two or more working configurations to be a good test. +void tst_QNetworkProxyFactory::inNetworkAccessManager() +{ + QFETCH(QNetworkConfiguration, config); + QFETCH(QList<QNetworkProxy>, proxies); + + int count = factory->requestCounter; + + QNetworkAccessManager manager; + manager.setConfiguration(config); + + //using an internet server, because cellular APs won't have a route to the test server. + QNetworkRequest req(QUrl(QString("http://qt.nokia.com"))); + QNetworkReply *reply = manager.get(req); + connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + QTestEventLoop::instance().enterLoop(30); + delete reply; + + if (count == factory->requestCounter) { + //RND phones are preconfigured with several test access points which won't work without a matching SIM + //If the network fails to start, QNAM won't ask the factory for proxies so we can't test. + QSKIP("network configuration didn't start", SkipSingle); + } + + qDebug() << "testing network configuration for" << config.name(); + foreach (QNetworkProxy proxy, factory->returnedList) { + qDebug() << formatProxyName(proxy); + } + qDebug() << " <vs> "; + foreach (QNetworkProxy proxy, proxies) { + qDebug() << formatProxyName(proxy); + } + if (config.type() != QNetworkConfiguration::InternetAccessPoint) + QEXPECT_FAIL("","QNetworkProxyFactory::systemProxyForQuery doesn't work for service networks yet", Continue); + QCOMPARE(factory->returnedList, proxies); +} + +#endif //QT_NO_BEARERMANAGEMENT + + +class QSPFQThread : public QThread +{ +protected: + virtual void run() + { + proxies = QNetworkProxyFactory::systemProxyForQuery(query); + } +public: + QNetworkProxyQuery query; + QList<QNetworkProxy> proxies; +}; + +//regression test for QTBUG-18799 +void tst_QNetworkProxyFactory::systemProxyForQueryCalledFromThread() +{ + QUrl url(QLatin1String("http://qt.nokia.com")); + QNetworkProxyQuery query(url); + QSPFQThread thread; + thread.query = query; + connect(&thread, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop())); + thread.start(); + QTestEventLoop::instance().enterLoop(5); + QVERIFY(thread.isFinished()); + QCOMPARE(thread.proxies, QNetworkProxyFactory::systemProxyForQuery(query)); +} + +QTEST_MAIN(tst_QNetworkProxyFactory) +#include "tst_qnetworkproxyfactory.moc" |