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 <qcoreapplication.h>
44 #include <QtTest/QtTest>
45 #include <qhostaddress.h>
46 #include <qplatformdefs.h>
47 #include <qdebug.h>
48 #include <qhash.h>
49 #include <qbytearray.h>
50 #include <qdatastream.h>
51 
52 //TESTED_CLASS=
53 //TESTED_FILES=
54 
55 class tst_QHostAddress : public QObject
56 {
57     Q_OBJECT
58 
59 public:
60     tst_QHostAddress();
61     virtual ~tst_QHostAddress();
62 
63 
64 public slots:
65     void init();
66     void cleanup();
67 private slots:
68     void constructor_QString_data();
69     void constructor_QString();
70     void setAddress_QString_data();
71     void setAddress_QString();
72     void specialAddresses_data();
73     void specialAddresses();
74     void compare_data();
75     void compare();
76     void assignment();
77     void scopeId();
78     void hashKey();
79     void streaming_data();
80     void streaming();
81     void parseSubnet_data();
82     void parseSubnet();
83     void isInSubnet_data();
84     void isInSubnet();
85 };
86 
87 QT_BEGIN_NAMESPACE
88 namespace QTest {
89     template<>
toString(const QHostAddress & addr)90     char *toString(const QHostAddress &addr)
91     {
92         if (addr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol)
93             return qstrdup("<invalid>");
94         return qstrdup(addr.toString().toLatin1());
95     }
96 }
97 QT_END_NAMESPACE
98 
tst_QHostAddress()99 tst_QHostAddress::tst_QHostAddress()
100 {
101 }
102 
~tst_QHostAddress()103 tst_QHostAddress::~tst_QHostAddress()
104 {
105 }
106 
Q_DECLARE_METATYPE(QHostAddress)107 Q_DECLARE_METATYPE(QHostAddress)
108 
109 void tst_QHostAddress::init()
110 {
111     qRegisterMetaType<QHostAddress>("QHostAddress");
112 }
113 
cleanup()114 void tst_QHostAddress::cleanup()
115 {
116     // No cleanup is required.
117 }
118 
constructor_QString_data()119 void tst_QHostAddress::constructor_QString_data()
120 {
121     setAddress_QString_data();
122 }
123 
constructor_QString()124 void tst_QHostAddress::constructor_QString()
125 {
126     QFETCH(QString, address);
127     QFETCH(bool, ok);
128     QFETCH(int, protocol);
129 
130     QHostAddress hostAddr(address);
131 
132     if (address == "0.0.0.0" || address == "::") {
133         QVERIFY(ok);
134     } else {
135         QVERIFY(hostAddr.isNull() != ok);
136     }
137 
138     if (ok)
139         QTEST(hostAddr.toString(), "resAddr");
140 
141     if ( protocol == 4 ) {
142         QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
143         QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol );
144     } else if ( protocol == 6 ) {
145         QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol );
146         QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol );
147     } else {
148         QVERIFY( hostAddr.isNull() );
149         QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
150     }
151 }
152 
setAddress_QString_data()153 void tst_QHostAddress::setAddress_QString_data()
154 {
155     QTest::addColumn<QString>("address");
156     QTest::addColumn<bool>("ok");
157     QTest::addColumn<QString>("resAddr");
158     QTest::addColumn<int>("protocol"); // 4: IPv4, 6: IPv6, other: undefined
159 
160     //next we fill it with data
161     QTest::newRow("ip4_00")  << QString("127.0.0.1") << (bool)TRUE << QString("127.0.0.1") << 4;
162     QTest::newRow("ip4_01")  << QString("255.3.2.1") << (bool)TRUE << QString("255.3.2.1") << 4;
163     QTest::newRow("ip4_03")  << QString(" 255.3.2.1") << (bool)TRUE << QString("255.3.2.1") << 4;
164     QTest::newRow("ip4_04")  << QString("255.3.2.1\r ") << (bool)TRUE << QString("255.3.2.1") << 4;
165     QTest::newRow("ip4_05")  << QString("0.0.0.0") << (bool)TRUE << QString("0.0.0.0") << 4;
166 
167     // for the format of IPv6 addresses see also RFC 5952
168     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;
169     QTest::newRow("ip6_01")  << QString("1080:0000:0000:0000:0008:0800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6;
170     QTest::newRow("ip6_02")  << QString("1080:0:0:0:8:800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6;
171     QTest::newRow("ip6_03")  << QString("1080::8:800:200C:417A") << (bool)TRUE << QString("1080::8:800:200C:417A") << 6;
172     QTest::newRow("ip6_04")  << QString("FF01::43") << (bool)TRUE << QString("FF01::43") << 6;
173     QTest::newRow("ip6_05")  << QString("::1") << (bool)TRUE << QString("::1") << 6;
174     QTest::newRow("ip6_06")  << QString("1::") << (bool)TRUE << QString("1::") << 6;
175     QTest::newRow("ip6_07")  << QString("::") << (bool)TRUE << QString("::") << 6;
176     QTest::newRow("ip6_08")  << QString("0:0:0:0:0:0:13.1.68.3") << (bool)TRUE << QString("::D01:4403") << 6;
177     QTest::newRow("ip6_09")  << QString("::13.1.68.3") << (bool)TRUE <<  QString("::D01:4403") << 6;
178     QTest::newRow("ip6_10")  << QString("0:0:0:0:0:FFFF:129.144.52.38") << (bool)TRUE << QString("::FFFF:8190:3426") << 6;
179     QTest::newRow("ip6_11")  << QString("::FFFF:129.144.52.38") << (bool)TRUE << QString("::FFFF:8190:3426") << 6;
180     QTest::newRow("ip6_12")  << QString("1::FFFF:129.144.52.38") << (bool)TRUE << QString("1::FFFF:8190:3426") << 6;
181     QTest::newRow("ip6_13")  << QString("A:B::D:E") << (bool)TRUE << QString("A:B::D:E") << 6;
182     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;
183     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;
184     QTest::newRow("ip6_16")  << QString("1080:0:1:0:8:800:0:0") << (bool)TRUE << QString("1080:0:1:0:8:800::") << 6;
185     QTest::newRow("ip6_17")  << QString("1080:0:0:0:8:800:0:0") << (bool)TRUE << QString("1080::8:800:0:0") << 6;
186     QTest::newRow("ip6_18")  << QString("0:1:1:1:8:800:0:0") << (bool)TRUE << QString("0:1:1:1:8:800::") << 6;
187     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;
188 
189     QTest::newRow("error_00")  << QString("foobarcom") << (bool)FALSE << QString() << 0;
190     QTest::newRow("error_01")  << QString("foo.bar.com") << (bool)FALSE << QString() << 0;
191     QTest::newRow("error_02")  << QString("") << (bool)FALSE << QString() << 0;
192     QTest::newRow("error_03")  << QString() << (bool)FALSE << QString() << 0;
193     QTest::newRow("error_04")  << QString(" \t\r") << (bool)FALSE << QString() << 0;
194 
195     QTest::newRow("error_ip4_00")  << QString("256.9.9.9") << (bool)FALSE << QString() << 0;
196     QTest::newRow("error_ip4_01")  << QString("-1.9.9.9") << (bool)FALSE << QString() << 0;
197     QTest::newRow("error_ip4_02")  << QString("123.0.0") << (bool)FALSE << QString() << 0;
198     QTest::newRow("error_ip4_03")  << QString("123.0.0.0.0") << (bool)FALSE << QString() << 0;
199     QTest::newRow("error_ip4_04")  << QString("255.2 3.2.1") << (bool)FALSE << QString() << 0;
200 
201     QTest::newRow("error_ip6_00")  << QString(":") << (bool)FALSE << QString() << 0;
202     QTest::newRow("error_ip6_01")  << QString(":::") << (bool)FALSE << QString() << 0;
203     QTest::newRow("error_ip6_02")  << QString("::AAAA:") << (bool)FALSE << QString() << 0;
204     QTest::newRow("error_ip6_03")  << QString(":AAAA::") << (bool)FALSE << QString() << 0;
205     QTest::newRow("error_ip6_04")  << QString("FFFF:::129.144.52.38") << (bool)FALSE << QString() << 0;
206     QTest::newRow("error_ip6_05")  << QString("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210:1234") << (bool)FALSE << QString() << 0;
207     QTest::newRow("error_ip6_06")  << QString("129.144.52.38::") << (bool)FALSE << QString() << 0;
208     QTest::newRow("error_ip6_07")  << QString("::129.144.52.38:129.144.52.38") << (bool)FALSE << QString() << 0;
209     QTest::newRow("error_ip6_08")  << QString(":::129.144.52.38") << (bool)FALSE << QString() << 0;
210     QTest::newRow("error_ip6_09")  << QString("1FEDC:BA98:7654:3210:FEDC:BA98:7654:3210") << (bool)FALSE << QString() << 0;
211     QTest::newRow("error_ip6_10")  << QString("::FFFFFFFF") << (bool)FALSE << QString() << 0;
212     QTest::newRow("error_ip6_11")  << QString("::EFGH") << (bool)FALSE << QString() << 0;
213     QTest::newRow("error_ip6_12")  << QString("ABCD:ABCD:ABCD") << (bool)FALSE << QString() << 0;
214     QTest::newRow("error_ip6_13")  << QString("::ABCD:ABCD::") << (bool)FALSE << QString() << 0;
215     QTest::newRow("error_ip6_14")  << QString("1::2::3") << (bool)FALSE << QString() << 0;
216     QTest::newRow("error_ip6_15")  << QString("1:2:::") << (bool)FALSE << QString() << 0;
217     QTest::newRow("error_ip6_16")  << QString(":::1:2") << (bool)FALSE << QString() << 0;
218     QTest::newRow("error_ip6_17")  << QString("1:::2") << (bool)FALSE << QString() << 0;
219     QTest::newRow("error_ip6_18")  << QString("FEDC::7654:3210:FEDC:BA98::3210") << (bool)FALSE << QString() << 0;
220     QTest::newRow("error_ip6_19")  << QString("ABCD:ABCD:ABCD:1.2.3.4") << (bool)FALSE << QString() << 0;
221     QTest::newRow("error_ip6_20")  << QString("ABCD::ABCD::ABCD:1.2.3.4") << (bool)FALSE << QString() << 0;
222 
223 }
224 
setAddress_QString()225 void tst_QHostAddress::setAddress_QString()
226 {
227     QFETCH(QString, address);
228     QFETCH(bool, ok);
229     QFETCH(int, protocol);
230 
231     QHostAddress hostAddr;
232     QVERIFY(hostAddr.setAddress(address) == ok);
233 
234     if (ok)
235         QTEST(hostAddr.toString(), "resAddr");
236 
237     if ( protocol == 4 ) {
238         QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv4Protocol || hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
239         QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv6Protocol );
240     } else if ( protocol == 6 ) {
241         QVERIFY( hostAddr.protocol() != QAbstractSocket::IPv4Protocol && hostAddr.protocol() != QAbstractSocket::UnknownNetworkLayerProtocol );
242         QVERIFY( hostAddr.protocol() == QAbstractSocket::IPv6Protocol );
243     } else {
244         QVERIFY( hostAddr.isNull() );
245         QVERIFY( hostAddr.protocol() == QAbstractSocket::UnknownNetworkLayerProtocol );
246     }
247 }
248 
specialAddresses_data()249 void tst_QHostAddress::specialAddresses_data()
250 {
251     QTest::addColumn<QString>("text");
252     QTest::addColumn<int>("address");
253     QTest::addColumn<bool>("result");
254 
255     QTest::newRow("localhost_1") << QString("127.0.0.1") << (int)QHostAddress::LocalHost << true;
256     QTest::newRow("localhost_2") << QString("127.0.0.2") << (int)QHostAddress::LocalHost << false;
257     QTest::newRow("localhost_3") << QString("127.0.0.2") << (int)QHostAddress::LocalHostIPv6 << false;
258 
259     QTest::newRow("localhost_ipv6_4") << QString("::1") << (int)QHostAddress::LocalHostIPv6 << true;
260     QTest::newRow("localhost_ipv6_5") << QString("::2") << (int)QHostAddress::LocalHostIPv6 << false;
261     QTest::newRow("localhost_ipv6_6") << QString("::1") << (int)QHostAddress::LocalHost << false;
262 
263     QTest::newRow("null_1") << QString("") << (int)QHostAddress::Null << true;
264     QTest::newRow("null_2") << QString("bjarne") << (int)QHostAddress::Null << true;
265 
266     QTest::newRow("compare_from_null") << QString("") << (int)QHostAddress::Broadcast << false;
267 
268     QTest::newRow("broadcast_1") << QString("255.255.255.255") << (int)QHostAddress::Any << false;
269     QTest::newRow("broadcast_2") << QString("255.255.255.255") << (int)QHostAddress::Broadcast << true;
270 
271     QTest::newRow("any_ipv6") << QString("::") << (int)QHostAddress::AnyIPv6 << true;
272     QTest::newRow("any_ipv4") << QString("0.0.0.0") << (int)QHostAddress::Any << true;
273 }
274 
275 
specialAddresses()276 void tst_QHostAddress::specialAddresses()
277 {
278     QFETCH(QString, text);
279     QFETCH(int, address);
280     QFETCH(bool, result);
281     QVERIFY((QHostAddress(text) == (QHostAddress::SpecialAddress)address) == result);
282 
283     QHostAddress setter;
284     setter.setAddress(text);
285     if (result) {
286         QVERIFY(setter == (QHostAddress::SpecialAddress) address);
287     } else {
288         QVERIFY(!((QHostAddress::SpecialAddress) address == setter));
289     }
290 }
291 
292 
compare_data()293 void tst_QHostAddress::compare_data()
294 {
295     QTest::addColumn<QHostAddress>("first");
296     QTest::addColumn<QHostAddress>("second");
297     QTest::addColumn<bool>("result");
298 
299     QTest::newRow("1") << QHostAddress() << QHostAddress() << true;
300     QTest::newRow("2") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::Any) << true;
301     QTest::newRow("3") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6) << true;
302     QTest::newRow("4") << QHostAddress(QHostAddress::Broadcast) << QHostAddress(QHostAddress::Broadcast) << true;
303     QTest::newRow("5") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Broadcast) << false;
304     QTest::newRow("6") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHostIPv6) << false;
305     QTest::newRow("7") << QHostAddress() << QHostAddress(QHostAddress::LocalHostIPv6) << false;
306 }
307 
compare()308 void tst_QHostAddress::compare()
309 {
310     QFETCH(QHostAddress, first);
311     QFETCH(QHostAddress, second);
312     QFETCH(bool, result);
313 
314     QCOMPARE(first == second, result);
315 }
316 
assignment()317 void tst_QHostAddress::assignment()
318 {
319     QHostAddress address;
320     address = "127.0.0.1";
321     QCOMPARE(address, QHostAddress("127.0.0.1"));
322 
323     address = "::1";
324     QCOMPARE(address, QHostAddress("::1"));
325 
326     QHostAddress addr("4.2.2.1");
327     sockaddr_in sockAddr;
328     sockAddr.sin_family = AF_INET;
329     sockAddr.sin_addr.s_addr = htonl(addr.toIPv4Address());
330     address.setAddress((sockaddr *)&sockAddr);
331     QCOMPARE(address, addr);
332 }
333 
scopeId()334 void tst_QHostAddress::scopeId()
335 {
336     QHostAddress address("fe80::2e0:4cff:fefb:662a%eth0");
337     QCOMPARE(address.scopeId(), QString("eth0"));
338     QCOMPARE(address.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0"));
339 
340     QHostAddress address2("fe80::2e0:4cff:fefb:662a");
341     QCOMPARE(address2.scopeId(), QString());
342     address2.setScopeId(QString("en0"));
343     QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%en0"));
344 
345     address2 = address;
346     QCOMPARE(address2.scopeId(), QString("eth0"));
347     QCOMPARE(address2.toString().toLower(), QString("fe80::2e0:4cff:fefb:662a%eth0"));
348 }
349 
hashKey()350 void tst_QHostAddress::hashKey()
351 {
352     QHash<QHostAddress, QString> hostHash;
353     hostHash.insert(QHostAddress(), "ole");
354 }
355 
streaming_data()356 void tst_QHostAddress::streaming_data()
357 {
358     QTest::addColumn<QHostAddress>("address");
359     QTest::newRow("1") << QHostAddress();
360     QTest::newRow("2") << QHostAddress(0xDEADBEEF);
361     QTest::newRow("3") << QHostAddress("127.128.129.130");
362     QTest::newRow("4") << QHostAddress("1080:0000:0000:0000:0008:0800:200C:417A");
363     QTest::newRow("5") << QHostAddress("fe80::2e0:4cff:fefb:662a%eth0");
364     QTest::newRow("6") << QHostAddress(QHostAddress::Null);
365     QTest::newRow("7") << QHostAddress(QHostAddress::LocalHost);
366     QTest::newRow("8") << QHostAddress(QHostAddress::LocalHostIPv6);
367     QTest::newRow("9") << QHostAddress(QHostAddress::Broadcast);
368     QTest::newRow("10") << QHostAddress(QHostAddress::Any);
369     QTest::newRow("11") << QHostAddress(QHostAddress::AnyIPv6);
370     QTest::newRow("12") << QHostAddress("foo.bar.com");
371 }
372 
streaming()373 void tst_QHostAddress::streaming()
374 {
375     QFETCH(QHostAddress, address);
376     QByteArray ba;
377     QDataStream ds1(&ba, QIODevice::WriteOnly);
378     ds1 << address;
379     QVERIFY(ds1.status() == QDataStream::Ok);
380     QDataStream ds2(&ba, QIODevice::ReadOnly);
381     QHostAddress address2;
382     ds2 >> address2;
383     QVERIFY(ds2.status() == QDataStream::Ok);
384     QCOMPARE(address, address2);
385 }
386 
parseSubnet_data()387 void tst_QHostAddress::parseSubnet_data()
388 {
389     QTest::addColumn<QString>("subnet");
390     QTest::addColumn<QHostAddress>("prefix");
391     QTest::addColumn<int>("prefixLength");
392 
393     // invalid/error values
394     QTest::newRow("empty") << QString() << QHostAddress() << -1;
395     QTest::newRow("invalid_01") << "foobar" << QHostAddress() << -1;
396     QTest::newRow("invalid_02") << "   " << QHostAddress() << -1;
397     QTest::newRow("invalid_03") << "1.2.3.a" << QHostAddress() << -1;
398     QTest::newRow("invalid_04") << "1.2.3.4.5" << QHostAddress() << -1;
399     QTest::newRow("invalid_05") << "1.2.3.4:80" << QHostAddress() << -1;
400     QTest::newRow("invalid_06") << "1.2.3.4/33" << QHostAddress() << -1;
401     QTest::newRow("invalid_07") << "1.2.3.4/-1" << QHostAddress() << -1;
402     QTest::newRow("invalid_08") << "1.2.3.4/256.0.0.0" << QHostAddress() << -1;
403     QTest::newRow("invalid_09") << "1.2.3.4/255.253.0.0" << QHostAddress() << -1;
404     QTest::newRow("invalid_10") << "1.2.3.4/255.0.0.255" << QHostAddress() << -1;
405     QTest::newRow("invalid_11") << "1.2.3.4." << QHostAddress() << -1;
406     QTest::newRow("invalid_20") << "ffff::/-1" << QHostAddress() << -1;
407     QTest::newRow("invalid_21") << "ffff::/129" << QHostAddress() << -1;
408     QTest::newRow("invalid_22") << "ffff::/255.255.0.0" << QHostAddress() << -1;
409     QTest::newRow("invalid_23") << "ffff::/ff00::" << QHostAddress() << -1;
410 
411     // correct IPv4 with netmask
412     QTest::newRow("netmask_0") << "0.0.0.0/0.0.0.0" << QHostAddress(QHostAddress::Any) << 0;
413     QTest::newRow("netmask_1") << "0.0.0.0/255.128.0.0" << QHostAddress(QHostAddress::Any) << 9;
414     QTest::newRow("netmask_2") << "0.0.0.0/255.192.0.0" << QHostAddress(QHostAddress::Any) << 10;
415     QTest::newRow("netmask_3") << "0.0.0.0/255.224.0.0" << QHostAddress(QHostAddress::Any) << 11;
416     QTest::newRow("netmask_4") << "0.0.0.0/255.240.0.0" << QHostAddress(QHostAddress::Any) << 12;
417     QTest::newRow("netmask_5") << "0.0.0.0/255.248.0.0" << QHostAddress(QHostAddress::Any) << 13;
418     QTest::newRow("netmask_6") << "0.0.0.0/255.252.0.0" << QHostAddress(QHostAddress::Any) << 14;
419     QTest::newRow("netmask_7") << "0.0.0.0/255.254.0.0" << QHostAddress(QHostAddress::Any) << 15;
420     QTest::newRow("netmask_8") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::Any) << 16;
421     QTest::newRow("netmask_16") << "0.0.0.0/255.255.0.0" << QHostAddress(QHostAddress::Any) << 16;
422     QTest::newRow("netmask_24") << "0.0.0.0/255.255.255.0" << QHostAddress(QHostAddress::Any) << 24;
423     QTest::newRow("netmask_31") << "0.0.0.0/255.255.255.254" << QHostAddress(QHostAddress::Any) << 31;
424     QTest::newRow("netmask_32") << "0.0.0.0/255.255.255.255" << QHostAddress(QHostAddress::Any) << 32;
425 
426     // correct IPv4 with prefix
427     QTest::newRow("prefix_0") << "0.0.0.0/0" << QHostAddress(QHostAddress::Any) << 0;
428     QTest::newRow("prefix_1") << "0.0.0.0/1" << QHostAddress(QHostAddress::Any) << 1;
429     QTest::newRow("prefix_9") << "0.0.0.0/9" << QHostAddress(QHostAddress::Any) << 9;
430     QTest::newRow("prefix_31") << "0.0.0.0/31" << QHostAddress(QHostAddress::Any) << 31;
431     QTest::newRow("prefix_32") << "0.0.0.0/32" << QHostAddress(QHostAddress::Any) << 32;
432 
433     // correct IPv4 without prefix or netmask
434     QTest::newRow("classA") << "10" << QHostAddress("10.0.0.0") << 8;
435     QTest::newRow("classA+dot") << "10." << QHostAddress("10.0.0.0") << 8;
436     QTest::newRow("classB") << "172.16" << QHostAddress("172.16.0.0") << 16;
437     QTest::newRow("classB+dot") << "172.16." << QHostAddress("172.16.0.0") << 16;
438     QTest::newRow("classC") << "192.168.0" << QHostAddress("192.168.0.0") << 24;
439     QTest::newRow("classC+dot") << "192.168.0" << QHostAddress("192.168.0.0") << 24;
440     QTest::newRow("full-ipv4") << "192.168.0.1" << QHostAddress("192.168.0.1") << 32;
441 
442     // correct IPv6 with prefix
443     QTest::newRow("ipv6_01") << "::/0" << QHostAddress(QHostAddress::AnyIPv6) << 0;
444     QTest::newRow("ipv6_03") << "::/3" << QHostAddress(QHostAddress::AnyIPv6) << 3;
445     QTest::newRow("ipv6_16") << "::/16" << QHostAddress(QHostAddress::AnyIPv6) << 16;
446     QTest::newRow("ipv6_48") << "::/48" << QHostAddress(QHostAddress::AnyIPv6) << 48;
447     QTest::newRow("ipv6_127") << "::/127" << QHostAddress(QHostAddress::AnyIPv6) << 127;
448     QTest::newRow("ipv6_128") << "::/128" << QHostAddress(QHostAddress::AnyIPv6) << 128;
449 
450     // tail bit clearing:
451     QTest::newRow("clear_01") << "255.255.255.255/31" << QHostAddress("255.255.255.254") << 31;
452     QTest::newRow("clear_08") << "255.255.255.255/24" << QHostAddress("255.255.255.0") << 24;
453     QTest::newRow("clear_09") << "255.255.255.255/23" << QHostAddress("255.255.254.0") << 23;
454     QTest::newRow("clear_10") << "255.255.255.255/22" << QHostAddress("255.255.252.0") << 22;
455     QTest::newRow("clear_11") << "255.255.255.255/21" << QHostAddress("255.255.248.0") << 21;
456     QTest::newRow("clear_12") << "255.255.255.255/20" << QHostAddress("255.255.240.0") << 20;
457     QTest::newRow("clear_13") << "255.255.255.255/19" << QHostAddress("255.255.224.0") << 19;
458     QTest::newRow("clear_14") << "255.255.255.255/18" << QHostAddress("255.255.192.0") << 18;
459     QTest::newRow("clear_15") << "255.255.255.255/17" << QHostAddress("255.255.128.0") << 17;
460     QTest::newRow("clear_16") << "255.255.255.255/16" << QHostAddress("255.255.0.0") << 16;
461     QTest::newRow("clear_24") << "255.255.255.255/8" << QHostAddress("255.0.0.0") << 8;
462     QTest::newRow("clear_31") << "255.255.255.255/1" << QHostAddress("128.0.0.0") << 1;
463     QTest::newRow("clear_32") << "255.255.255.255/0" << QHostAddress("0.0.0.0") << 0;
464 
465     // same for IPv6:
466     QTest::newRow("ipv6_clear_01") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/127"
467                                    << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")
468                                    << 127;
469     QTest::newRow("ipv6_clear_07") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/121"
470                                    << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff80")
471                                    << 121;
472     QTest::newRow("ipv6_clear_08") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/120"
473                                    << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00")
474                                    << 120;
475     QTest::newRow("ipv6_clear_16") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/112"
476                                    << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:ffff:0")
477                                    << 112;
478     QTest::newRow("ipv6_clear_80") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/48"
479                                    << QHostAddress("ffff:ffff:ffff::")
480                                    << 48;
481     QTest::newRow("ipv6_clear_81") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/47"
482                                    << QHostAddress("ffff:ffff:fffe::")
483                                    << 47;
484     QTest::newRow("ipv6_clear_82") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/46"
485                                    << QHostAddress("ffff:ffff:fffc::")
486                                    << 46;
487     QTest::newRow("ipv6_clear_83") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/45"
488                                    << QHostAddress("ffff:ffff:fff8::")
489                                    << 45;
490     QTest::newRow("ipv6_clear_84") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/44"
491                                    << QHostAddress("ffff:ffff:fff0::")
492                                    << 44;
493     QTest::newRow("ipv6_clear_85") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/43"
494                                    << QHostAddress("ffff:ffff:ffe0::")
495                                    << 43;
496     QTest::newRow("ipv6_clear_86") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/42"
497                                    << QHostAddress("ffff:ffff:ffc0::")
498                                    << 42;
499     QTest::newRow("ipv6_clear_87") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/41"
500                                    << QHostAddress("ffff:ffff:ff80::")
501                                    << 41;
502     QTest::newRow("ipv6_clear_88") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/40"
503                                    << QHostAddress("ffff:ffff:ff00::")
504                                    << 40;
505     QTest::newRow("ipv6_clear_125") << "3fff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/3"
506                                     << QHostAddress("2000::")
507                                     << 3;
508     QTest::newRow("ipv6_clear_127") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/1"
509                                     << QHostAddress("8000::")
510                                     << 1;
511     QTest::newRow("ipv6_clear_128") << "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0"
512                                     << QHostAddress(QHostAddress::AnyIPv6)
513                                     << 0;
514 }
515 
parseSubnet()516 void tst_QHostAddress::parseSubnet()
517 {
518     QFETCH(QString, subnet);
519     QFETCH(QHostAddress, prefix);
520     QFETCH(int, prefixLength);
521 
522     QPair<QHostAddress, int> result = QHostAddress::parseSubnet(subnet);
523     QCOMPARE(result.first, prefix);
524     QCOMPARE(result.second, prefixLength);
525 }
526 
isInSubnet_data()527 void tst_QHostAddress::isInSubnet_data()
528 {
529     QTest::addColumn<QHostAddress>("address");
530     QTest::addColumn<QHostAddress>("prefix");
531     QTest::addColumn<int>("prefixLength");
532     QTest::addColumn<bool>("result");
533 
534     // invalid QHostAddresses are never in any subnets
535     QTest::newRow("invalid_01") << QHostAddress() << QHostAddress() << 32 << false;
536     QTest::newRow("invalid_02") << QHostAddress() << QHostAddress(QHostAddress::Any) << 32 << false;
537     QTest::newRow("invalid_03") << QHostAddress() << QHostAddress(QHostAddress::Any) << 8 << false;
538     QTest::newRow("invalid_04") << QHostAddress() << QHostAddress(QHostAddress::Any) << 0 << false;
539     QTest::newRow("invalid_05") << QHostAddress() << QHostAddress("255.255.255.0") << 24 << false;
540     QTest::newRow("invalid_06") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 0 << false;
541     QTest::newRow("invalid_07") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 32 << false;
542     QTest::newRow("invalid_08") << QHostAddress() << QHostAddress(QHostAddress::AnyIPv6) << 128<< false;
543 
544     // and no host address can be in a subnet whose prefix is invalid
545     QTest::newRow("invalid_20") << QHostAddress(QHostAddress::Any) << QHostAddress() << 16 << false;
546     QTest::newRow("invalid_21") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress() << 16 << false;
547     QTest::newRow("invalid_22") << QHostAddress(QHostAddress::LocalHost) << QHostAddress() << 16 << false;
548     QTest::newRow("invalid_23") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress() << 16 << false;
549 
550     // negative netmasks don't make sense:
551     QTest::newRow("invalid_30") << QHostAddress(QHostAddress::Any) << QHostAddress(QHostAddress::Any) << -1 << false;
552     QTest::newRow("invalid_31") << QHostAddress(QHostAddress::AnyIPv6) << QHostAddress(QHostAddress::AnyIPv6) << -1 << false;
553 
554     // we don't support IPv4 belonging in an IPv6 netmask and vice-versa
555     QTest::newRow("v4-in-v6") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::AnyIPv6) << 0 << false;
556     QTest::newRow("v6-in-v4") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::Any) << 0 << false;
557     QTest::newRow("v4-in-v6mapped") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("ffff:ffff:ffff:ffff:ffff:ffff:255.0.0.0") << 113 << false;
558     QTest::newRow("v4-in-v6mapped2") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("::ffff:255.0.0.0") << 113 << false;
559 
560     // IPv4 correct ones
561     QTest::newRow("netmask_0") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Any) << 0 << true;
562     QTest::newRow("netmask_0bis") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("255.255.0.0") << 0 << true;
563     QTest::newRow("netmask_0ter") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("1.2.3.4") << 0 << true;
564     QTest::newRow("netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::Any) << 1 << true;
565     QTest::newRow("~netmask_1") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("128.0.0.0") << 1 << false;
566     QTest::newRow("netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("128.0.0.0") << 1 << true;
567     QTest::newRow("~netmask_1bis") << QHostAddress("224.0.0.1") << QHostAddress("0.0.0.0") << 1 << false;
568     QTest::newRow("netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("127.0.0.0") << 8 << true;
569     QTest::newRow("~netmask_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress("126.0.0.0") << 8 << false;
570     QTest::newRow("netmask_15") << QHostAddress("10.0.1.255") << QHostAddress("10.0.0.0") << 15 << true;
571     QTest::newRow("netmask_16") << QHostAddress("172.16.0.1") << QHostAddress("172.16.0.0") << 16 << true;
572 
573     // the address is always in the subnet containing its address, regardless of length:
574     QTest::newRow("same_01") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 1 << true;
575     QTest::newRow("same_07") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 7 << true;
576     QTest::newRow("same_8") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 8 << true;
577     QTest::newRow("same_24") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 23 << true;
578     QTest::newRow("same_31") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 31 << true;
579     QTest::newRow("same_32") << QHostAddress(QHostAddress::LocalHost) << QHostAddress(QHostAddress::LocalHost) << 32 << true;
580 
581     // IPv6 correct ones:
582     QTest::newRow("ipv6_netmask_0") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 0 << true;
583     QTest::newRow("ipv6_netmask_0bis") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::LocalHostIPv6) << 0 << true;
584     QTest::newRow("ipv6_netmask_0ter") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("ffff::") << 0 << true;
585     QTest::newRow("ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress(QHostAddress::AnyIPv6) << 1 << true;
586     QTest::newRow("ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("8000::") << 1 << true;
587     QTest::newRow("~ipv6_netmask_1") << QHostAddress(QHostAddress::LocalHostIPv6) << QHostAddress("8000::") << 1 << false;
588     QTest::newRow("~ipv6_netmask_1bis") << QHostAddress("fec0::1") << QHostAddress("::") << 1 << false;
589     QTest::newRow("ipv6_netmask_47") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 47 << true;
590     QTest::newRow("ipv6_netmask_48") << QHostAddress("2:3:4::1") << QHostAddress("2:3:4::") << 48 << true;
591     QTest::newRow("~ipv6_netmask_48") << QHostAddress("2:3:5::1") << QHostAddress("2:3:4::") << 48 << false;
592     QTest::newRow("ipv6_netmask_127") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::") << 127 << true;
593     QTest::newRow("ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::1") << 128 << true;
594     QTest::newRow("~ipv6_netmask_128") << QHostAddress("2:3:4:5::1") << QHostAddress("2:3:4:5::0") << 128 << false;
595 }
596 
isInSubnet()597 void tst_QHostAddress::isInSubnet()
598 {
599     QFETCH(QHostAddress, address);
600     QFETCH(QHostAddress, prefix);
601     QFETCH(int, prefixLength);
602 
603     QTEST(address.isInSubnet(prefix, prefixLength), "result");
604 }
605 
606 QTEST_MAIN(tst_QHostAddress)
607 #include "tst_qhostaddress.moc"
608