1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
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 
getSetCheck()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 
prefixAndNetmask_data()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 
prefixAndNetmask()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 
186