tests/auto/qnetworkaddressentry/tst_qnetworkaddressentry.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 #include <qcoreapplication.h>
       
    45 #include <qnetworkinterface.h>
       
    46 
       
    47 // TESTED_FILES=qnetworkinterface.cpp qnetworkinterface.h
       
    48 Q_DECLARE_METATYPE(QHostAddress)
       
    49 
       
    50 class tst_QNetworkAddressEntry: public QObject
       
    51 {
       
    52     Q_OBJECT
       
    53 private slots:
       
    54     void getSetCheck();
       
    55     void prefixAndNetmask_data();
       
    56     void prefixAndNetmask();
       
    57 };
       
    58 
       
    59 void tst_QNetworkAddressEntry::getSetCheck()
       
    60 {
       
    61     QNetworkAddressEntry entry;
       
    62 
       
    63     QVERIFY(entry.ip().isNull());
       
    64     QVERIFY(entry.netmask().isNull());
       
    65     QVERIFY(entry.broadcast().isNull());
       
    66     QCOMPARE(entry.prefixLength(), -1);
       
    67 
       
    68     entry.setIp(QHostAddress::LocalHost);
       
    69     QCOMPARE(entry.ip(), QHostAddress(QHostAddress::LocalHost));
       
    70     entry.setIp(QHostAddress());
       
    71     QVERIFY(entry.ip().isNull());
       
    72 
       
    73     entry.setBroadcast(QHostAddress::LocalHost);
       
    74     QCOMPARE(entry.broadcast(), QHostAddress(QHostAddress::LocalHost));
       
    75     entry.setBroadcast(QHostAddress());
       
    76     QVERIFY(entry.broadcast().isNull());
       
    77 
       
    78     // netmask and prefix length tested in the next test
       
    79     entry.setIp(QHostAddress::LocalHost);
       
    80     entry.setBroadcast(QHostAddress::LocalHost);
       
    81 
       
    82     QNetworkAddressEntry entry2;
       
    83     QVERIFY(entry != entry2);
       
    84     QVERIFY(!(entry == entry2));
       
    85 
       
    86     entry = entry2;
       
    87     QCOMPARE(entry, entry2);
       
    88     QVERIFY(entry == entry);
       
    89     QVERIFY(!(entry != entry2));
       
    90 }
       
    91 
       
    92 void tst_QNetworkAddressEntry::prefixAndNetmask_data()
       
    93 {
       
    94     QTest::addColumn<QHostAddress>("ip");
       
    95     QTest::addColumn<QHostAddress>("netmask");
       
    96     QTest::addColumn<int>("prefix");
       
    97 
       
    98     // IPv4 set:
       
    99     QHostAddress ipv4(QHostAddress::LocalHost);
       
   100     QTest::newRow("v4/0") << ipv4 << QHostAddress(QHostAddress::Any) << 0;
       
   101     QTest::newRow("v4/32") << ipv4 << QHostAddress("255.255.255.255") << 32;
       
   102     QTest::newRow("v4/24") << ipv4 << QHostAddress("255.255.255.0") << 24;
       
   103     QTest::newRow("v4/23") << ipv4 << QHostAddress("255.255.254.0") << 23;
       
   104     QTest::newRow("v4/20") << ipv4 << QHostAddress("255.255.240.0") << 20;
       
   105     QTest::newRow("v4/invalid1") << ipv4 << QHostAddress(QHostAddress::LocalHost) << -1;
       
   106     QTest::newRow("v4/invalid2") << ipv4 << QHostAddress(QHostAddress::AnyIPv6) << -1;
       
   107     QTest::newRow("v4/invalid3") << ipv4 << QHostAddress("255.255.253.0") << -1;
       
   108     QTest::newRow("v4/invalid4") << ipv4 << QHostAddress() << -2;
       
   109     QTest::newRow("v4/invalid5") << ipv4 << QHostAddress() << 33;
       
   110 
       
   111     // IPv6 set:
       
   112     QHostAddress ipv6(QHostAddress::LocalHostIPv6);
       
   113     QTest::newRow("v6/0") << ipv6 << QHostAddress(QHostAddress::AnyIPv6) << 0;
       
   114     QTest::newRow("v6/128") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff") << 128;
       
   115     QTest::newRow("v6/64") << ipv6 << QHostAddress("ffff:ffff:ffff:ffff::") << 64;
       
   116     QTest::newRow("v6/63") << ipv6 << QHostAddress("ffff:ffff:ffff:fffe::") << 63;
       
   117     QTest::newRow("v6/60") << ipv6 << QHostAddress("ffff:ffff:ffff:fff0::") << 60;
       
   118     QTest::newRow("v6/48") << ipv6 << QHostAddress("ffff:ffff:ffff::") << 48;
       
   119     QTest::newRow("v6/3") << ipv6 << QHostAddress("e000::") << 3;
       
   120     QTest::newRow("v6/invalid1") << ipv6 << QHostAddress(QHostAddress::LocalHostIPv6) << -1;
       
   121     QTest::newRow("v6/invalid2") << ipv6 << QHostAddress(QHostAddress::Any) << -1;
       
   122     QTest::newRow("v6/invalid3") << ipv6 << QHostAddress("fffd::") << -1;
       
   123     QTest::newRow("v6/invalid4") << ipv6 << QHostAddress() << -2;
       
   124     QTest::newRow("v6/invalid5") << ipv6 << QHostAddress() << 129;
       
   125 }
       
   126 
       
   127 void tst_QNetworkAddressEntry::prefixAndNetmask()
       
   128 {
       
   129     QFETCH(QHostAddress, ip);
       
   130     QFETCH(QHostAddress, netmask);
       
   131     QFETCH(int, prefix);
       
   132 
       
   133     QNetworkAddressEntry entry;
       
   134 
       
   135     // first, without setting the IP, all must be invalid:
       
   136     entry.setNetmask(netmask);
       
   137     QVERIFY(entry.netmask().isNull());
       
   138     entry.setPrefixLength(prefix);
       
   139     QCOMPARE(entry.prefixLength(), -1);
       
   140 
       
   141     // set the IP:
       
   142     entry.setIp(ip);
       
   143 
       
   144     // set the netmask:
       
   145     if (!netmask.isNull()) {
       
   146         entry.setNetmask(netmask);
       
   147 
       
   148         // was it a valid one?
       
   149         if (prefix != -1) {
       
   150             QVERIFY(!entry.netmask().isNull());
       
   151             QCOMPARE(entry.netmask(), netmask);
       
   152             QCOMPARE(entry.prefixLength(), prefix);
       
   153         } else {
       
   154             // not valid
       
   155             QVERIFY(entry.netmask().isNull());
       
   156             QCOMPARE(entry.prefixLength(), -1);
       
   157         }
       
   158     }
       
   159     entry.setNetmask(QHostAddress());
       
   160     QVERIFY(entry.netmask().isNull());
       
   161     QCOMPARE(entry.prefixLength(), -1);
       
   162 
       
   163     // set the prefix
       
   164     if (prefix != -1) {
       
   165         entry.setPrefixLength(prefix);
       
   166 
       
   167         // was it a valid one?
       
   168         if (!netmask.isNull()) {
       
   169             QVERIFY(!entry.netmask().isNull());
       
   170             QCOMPARE(entry.netmask(), netmask);
       
   171             QCOMPARE(entry.prefixLength(), prefix);
       
   172         } else {
       
   173             // not valid
       
   174             QVERIFY(entry.netmask().isNull());
       
   175             QCOMPARE(entry.prefixLength(), -1);
       
   176         }
       
   177     }
       
   178     entry.setPrefixLength(-1);
       
   179     QVERIFY(entry.netmask().isNull());
       
   180     QCOMPARE(entry.prefixLength(), -1);
       
   181 }
       
   182 
       
   183 QTEST_MAIN(tst_QNetworkAddressEntry)
       
   184 #include "tst_qnetworkaddressentry.moc"
       
   185