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