1 | // |
2 | // IPAddressTest.cpp |
3 | // |
4 | // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "IPAddressTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/Net/IPAddress.h" |
15 | #include "Poco/Net/NetException.h" |
16 | |
17 | |
18 | using Poco::Net::IPAddress; |
19 | using Poco::Net::InvalidAddressException; |
20 | |
21 | |
22 | IPAddressTest::IPAddressTest(const std::string& name): CppUnit::TestCase(name) |
23 | { |
24 | } |
25 | |
26 | |
27 | IPAddressTest::~IPAddressTest() |
28 | { |
29 | } |
30 | |
31 | |
32 | void IPAddressTest::testStringConv() |
33 | { |
34 | IPAddress ia1("127.0.0.1" ); |
35 | assertTrue (ia1.family() == IPAddress::IPv4); |
36 | assertTrue (ia1.toString() == "127.0.0.1" ); |
37 | |
38 | IPAddress ia2("192.168.1.120" ); |
39 | assertTrue (ia2.family() == IPAddress::IPv4); |
40 | assertTrue (ia2.toString() == "192.168.1.120" ); |
41 | |
42 | IPAddress ia3("255.255.255.255" ); |
43 | assertTrue (ia3.family() == IPAddress::IPv4); |
44 | assertTrue (ia3.toString() == "255.255.255.255" ); |
45 | |
46 | IPAddress ia4("0.0.0.0" ); |
47 | assertTrue (ia4.family() == IPAddress::IPv4); |
48 | assertTrue (ia4.toString() == "0.0.0.0" ); |
49 | |
50 | IPAddress ia5(24, IPAddress::IPv4); |
51 | assertTrue (ia5.family() == IPAddress::IPv4); |
52 | assertTrue (ia5.toString() == "255.255.255.0" ); |
53 | } |
54 | |
55 | |
56 | void IPAddressTest::testStringConv6() |
57 | { |
58 | #ifdef POCO_HAVE_IPv6 |
59 | IPAddress ia0("::1" ); |
60 | assertTrue (ia0.family() == IPAddress::IPv6); |
61 | assertTrue (ia0.toString() == "::1" ); |
62 | |
63 | IPAddress ia1("1080:0:0:0:8:600:200a:425c" ); |
64 | assertTrue (ia1.family() == IPAddress::IPv6); |
65 | assertTrue (ia1.toString() == "1080::8:600:200a:425c" ); |
66 | |
67 | IPAddress ia2("1080::8:600:200A:425C" ); |
68 | assertTrue (ia2.family() == IPAddress::IPv6); |
69 | assertTrue (ia2.toString() == "1080::8:600:200a:425c" ); |
70 | |
71 | IPAddress ia3("::192.168.1.120" ); |
72 | assertTrue (ia3.family() == IPAddress::IPv6); |
73 | assertTrue (ia3.toString() == "::192.168.1.120" ); |
74 | |
75 | IPAddress ia4("::ffff:192.168.1.120" ); |
76 | assertTrue (ia4.family() == IPAddress::IPv6); |
77 | assertTrue (ia4.toString() == "::ffff:192.168.1.120" ); |
78 | |
79 | IPAddress ia5(64, IPAddress::IPv6); |
80 | assertTrue (ia5.family() == IPAddress::IPv6); |
81 | assertTrue (ia5.toString() == "ffff:ffff:ffff:ffff::" ); |
82 | |
83 | IPAddress ia6(32, IPAddress::IPv6); |
84 | assertTrue (ia6.family() == IPAddress::IPv6); |
85 | assertTrue (ia6.toString() == "ffff:ffff::" ); |
86 | |
87 | IPAddress ia7("::" ); |
88 | assertTrue (ia7.family() == IPAddress::IPv6); |
89 | assertTrue (ia7.toString() == "::" ); |
90 | #endif |
91 | } |
92 | |
93 | |
94 | void IPAddressTest::testParse() |
95 | { |
96 | IPAddress ip; |
97 | assertTrue (IPAddress::tryParse("192.168.1.120" , ip)); |
98 | |
99 | assertTrue (!IPAddress::tryParse("192.168.1.280" , ip)); |
100 | |
101 | ip = IPAddress::parse("192.168.1.120" ); |
102 | try |
103 | { |
104 | ip = IPAddress::parse("192.168.1.280" ); |
105 | fail("bad address - must throw" ); |
106 | } |
107 | catch (InvalidAddressException&) |
108 | { |
109 | } |
110 | } |
111 | |
112 | |
113 | void IPAddressTest::testClassification() |
114 | { |
115 | IPAddress ip1("0.0.0.0" ); // wildcard |
116 | assertTrue (ip1.isWildcard()); |
117 | assertTrue (!ip1.isBroadcast()); |
118 | assertTrue (!ip1.isLoopback()); |
119 | assertTrue (!ip1.isMulticast()); |
120 | assertTrue (!ip1.isUnicast()); |
121 | assertTrue (!ip1.isLinkLocal()); |
122 | assertTrue (!ip1.isSiteLocal()); |
123 | assertTrue (!ip1.isWellKnownMC()); |
124 | assertTrue (!ip1.isNodeLocalMC()); |
125 | assertTrue (!ip1.isLinkLocalMC()); |
126 | assertTrue (!ip1.isSiteLocalMC()); |
127 | assertTrue (!ip1.isOrgLocalMC()); |
128 | assertTrue (!ip1.isGlobalMC()); |
129 | |
130 | IPAddress ip2("255.255.255.255" ); // broadcast |
131 | assertTrue (!ip2.isWildcard()); |
132 | assertTrue (ip2.isBroadcast()); |
133 | assertTrue (!ip2.isLoopback()); |
134 | assertTrue (!ip2.isMulticast()); |
135 | assertTrue (!ip2.isUnicast()); |
136 | assertTrue (!ip2.isLinkLocal()); |
137 | assertTrue (!ip2.isSiteLocal()); |
138 | assertTrue (!ip2.isWellKnownMC()); |
139 | assertTrue (!ip2.isNodeLocalMC()); |
140 | assertTrue (!ip2.isLinkLocalMC()); |
141 | assertTrue (!ip2.isSiteLocalMC()); |
142 | assertTrue (!ip2.isOrgLocalMC()); |
143 | assertTrue (!ip2.isGlobalMC()); |
144 | |
145 | IPAddress ip3("127.0.0.1" ); // loopback |
146 | assertTrue (!ip3.isWildcard()); |
147 | assertTrue (!ip3.isBroadcast()); |
148 | assertTrue (ip3.isLoopback()); |
149 | assertTrue (!ip3.isMulticast()); |
150 | assertTrue (ip3.isUnicast()); |
151 | assertTrue (!ip3.isLinkLocal()); |
152 | assertTrue (!ip3.isSiteLocal()); |
153 | assertTrue (!ip3.isWellKnownMC()); |
154 | assertTrue (!ip3.isNodeLocalMC()); |
155 | assertTrue (!ip3.isLinkLocalMC()); |
156 | assertTrue (!ip3.isSiteLocalMC()); |
157 | assertTrue (!ip3.isOrgLocalMC()); |
158 | assertTrue (!ip3.isGlobalMC()); |
159 | |
160 | IPAddress ip4("80.122.195.86" ); // unicast |
161 | assertTrue (!ip4.isWildcard()); |
162 | assertTrue (!ip4.isBroadcast()); |
163 | assertTrue (!ip4.isLoopback()); |
164 | assertTrue (!ip4.isMulticast()); |
165 | assertTrue (ip4.isUnicast()); |
166 | assertTrue (!ip4.isLinkLocal()); |
167 | assertTrue (!ip4.isSiteLocal()); |
168 | assertTrue (!ip4.isWellKnownMC()); |
169 | assertTrue (!ip4.isNodeLocalMC()); |
170 | assertTrue (!ip4.isLinkLocalMC()); |
171 | assertTrue (!ip4.isSiteLocalMC()); |
172 | assertTrue (!ip4.isOrgLocalMC()); |
173 | assertTrue (!ip4.isGlobalMC()); |
174 | |
175 | IPAddress ip5("169.254.1.20" ); // link local unicast |
176 | assertTrue (!ip5.isWildcard()); |
177 | assertTrue (!ip5.isBroadcast()); |
178 | assertTrue (!ip5.isLoopback()); |
179 | assertTrue (!ip5.isMulticast()); |
180 | assertTrue (ip5.isUnicast()); |
181 | assertTrue (ip5.isLinkLocal()); |
182 | assertTrue (!ip5.isSiteLocal()); |
183 | assertTrue (!ip5.isWellKnownMC()); |
184 | assertTrue (!ip5.isNodeLocalMC()); |
185 | assertTrue (!ip5.isLinkLocalMC()); |
186 | assertTrue (!ip5.isSiteLocalMC()); |
187 | assertTrue (!ip5.isOrgLocalMC()); |
188 | assertTrue (!ip5.isGlobalMC()); |
189 | |
190 | IPAddress ip6("192.168.1.120" ); // site local unicast |
191 | assertTrue (!ip6.isWildcard()); |
192 | assertTrue (!ip6.isBroadcast()); |
193 | assertTrue (!ip6.isLoopback()); |
194 | assertTrue (!ip6.isMulticast()); |
195 | assertTrue (ip6.isUnicast()); |
196 | assertTrue (!ip6.isLinkLocal()); |
197 | assertTrue (ip6.isSiteLocal()); |
198 | assertTrue (!ip6.isWellKnownMC()); |
199 | assertTrue (!ip6.isNodeLocalMC()); |
200 | assertTrue (!ip6.isLinkLocalMC()); |
201 | assertTrue (!ip6.isSiteLocalMC()); |
202 | assertTrue (!ip6.isOrgLocalMC()); |
203 | assertTrue (!ip6.isGlobalMC()); |
204 | |
205 | IPAddress ip7("10.0.0.138" ); // site local unicast |
206 | assertTrue (!ip7.isWildcard()); |
207 | assertTrue (!ip7.isBroadcast()); |
208 | assertTrue (!ip7.isLoopback()); |
209 | assertTrue (!ip7.isMulticast()); |
210 | assertTrue (ip7.isUnicast()); |
211 | assertTrue (!ip7.isLinkLocal()); |
212 | assertTrue (ip7.isSiteLocal()); |
213 | assertTrue (!ip7.isWellKnownMC()); |
214 | assertTrue (!ip7.isNodeLocalMC()); |
215 | assertTrue (!ip7.isLinkLocalMC()); |
216 | assertTrue (!ip7.isSiteLocalMC()); |
217 | assertTrue (!ip7.isOrgLocalMC()); |
218 | assertTrue (!ip7.isGlobalMC()); |
219 | |
220 | IPAddress ip8("172.18.1.200" ); // site local unicast |
221 | assertTrue (!ip8.isWildcard()); |
222 | assertTrue (!ip8.isBroadcast()); |
223 | assertTrue (!ip8.isLoopback()); |
224 | assertTrue (!ip8.isMulticast()); |
225 | assertTrue (ip8.isUnicast()); |
226 | assertTrue (!ip8.isLinkLocal()); |
227 | assertTrue (ip8.isSiteLocal()); |
228 | assertTrue (!ip8.isWellKnownMC()); |
229 | assertTrue (!ip8.isNodeLocalMC()); |
230 | assertTrue (!ip8.isLinkLocalMC()); |
231 | assertTrue (!ip8.isSiteLocalMC()); |
232 | assertTrue (!ip8.isOrgLocalMC()); |
233 | assertTrue (!ip8.isGlobalMC()); |
234 | } |
235 | |
236 | |
237 | void IPAddressTest::testMCClassification() |
238 | { |
239 | IPAddress ip1("224.0.0.100" ); // well-known multicast |
240 | assertTrue (!ip1.isWildcard()); |
241 | assertTrue (!ip1.isBroadcast()); |
242 | assertTrue (!ip1.isLoopback()); |
243 | assertTrue (ip1.isMulticast()); |
244 | assertTrue (!ip1.isUnicast()); |
245 | assertTrue (!ip1.isLinkLocal()); |
246 | assertTrue (!ip1.isSiteLocal()); |
247 | assertTrue (ip1.isWellKnownMC()); |
248 | assertTrue (!ip1.isNodeLocalMC()); |
249 | assertTrue (ip1.isLinkLocalMC()); // well known are in the range of link local |
250 | assertTrue (!ip1.isSiteLocalMC()); |
251 | assertTrue (!ip1.isOrgLocalMC()); |
252 | assertTrue (!ip1.isGlobalMC()); |
253 | |
254 | IPAddress ip2("224.1.0.100" ); // link local unicast |
255 | assertTrue (!ip2.isWildcard()); |
256 | assertTrue (!ip2.isBroadcast()); |
257 | assertTrue (!ip2.isLoopback()); |
258 | assertTrue (ip2.isMulticast()); |
259 | assertTrue (!ip2.isUnicast()); |
260 | assertTrue (!ip2.isLinkLocal()); |
261 | assertTrue (!ip2.isSiteLocal()); |
262 | assertTrue (!ip2.isWellKnownMC()); |
263 | assertTrue (!ip2.isNodeLocalMC()); |
264 | assertTrue (ip2.isLinkLocalMC()); |
265 | assertTrue (!ip2.isSiteLocalMC()); |
266 | assertTrue (!ip2.isOrgLocalMC()); |
267 | assertTrue (ip2.isGlobalMC()); // link local fall in the range of global |
268 | |
269 | IPAddress ip3("239.255.0.100" ); // site local unicast |
270 | assertTrue (!ip3.isWildcard()); |
271 | assertTrue (!ip3.isBroadcast()); |
272 | assertTrue (!ip3.isLoopback()); |
273 | assertTrue (ip3.isMulticast()); |
274 | assertTrue (!ip3.isUnicast()); |
275 | assertTrue (!ip3.isLinkLocal()); |
276 | assertTrue (!ip3.isSiteLocal()); |
277 | assertTrue (!ip3.isWellKnownMC()); |
278 | assertTrue (!ip3.isNodeLocalMC()); |
279 | assertTrue (!ip3.isLinkLocalMC()); |
280 | assertTrue (ip3.isSiteLocalMC()); |
281 | assertTrue (!ip3.isOrgLocalMC()); |
282 | assertTrue (!ip3.isGlobalMC()); |
283 | |
284 | IPAddress ip4("239.192.0.100" ); // org local unicast |
285 | assertTrue (!ip4.isWildcard()); |
286 | assertTrue (!ip4.isBroadcast()); |
287 | assertTrue (!ip4.isLoopback()); |
288 | assertTrue (ip4.isMulticast()); |
289 | assertTrue (!ip4.isUnicast()); |
290 | assertTrue (!ip4.isLinkLocal()); |
291 | assertTrue (!ip4.isSiteLocal()); |
292 | assertTrue (!ip4.isWellKnownMC()); |
293 | assertTrue (!ip4.isNodeLocalMC()); |
294 | assertTrue (!ip4.isLinkLocalMC()); |
295 | assertTrue (!ip4.isSiteLocalMC()); |
296 | assertTrue (ip4.isOrgLocalMC()); |
297 | assertTrue (!ip4.isGlobalMC()); |
298 | |
299 | IPAddress ip5("224.2.127.254" ); // global unicast |
300 | assertTrue (!ip5.isWildcard()); |
301 | assertTrue (!ip5.isBroadcast()); |
302 | assertTrue (!ip5.isLoopback()); |
303 | assertTrue (ip5.isMulticast()); |
304 | assertTrue (!ip5.isUnicast()); |
305 | assertTrue (!ip5.isLinkLocal()); |
306 | assertTrue (!ip5.isSiteLocal()); |
307 | assertTrue (!ip5.isWellKnownMC()); |
308 | assertTrue (!ip5.isNodeLocalMC()); |
309 | assertTrue (ip5.isLinkLocalMC()); // link local fall in the range of global |
310 | assertTrue (!ip5.isSiteLocalMC()); |
311 | assertTrue (!ip5.isOrgLocalMC()); |
312 | assertTrue (ip5.isGlobalMC()); |
313 | } |
314 | |
315 | |
316 | void IPAddressTest::testClassification6() |
317 | { |
318 | #ifdef POCO_HAVE_IPv6 |
319 | IPAddress ip1("::" ); // wildcard |
320 | assertTrue (ip1.isWildcard()); |
321 | assertTrue (!ip1.isBroadcast()); |
322 | assertTrue (!ip1.isLoopback()); |
323 | assertTrue (!ip1.isMulticast()); |
324 | assertTrue (!ip1.isUnicast()); |
325 | assertTrue (!ip1.isLinkLocal()); |
326 | assertTrue (!ip1.isSiteLocal()); |
327 | assertTrue (!ip1.isWellKnownMC()); |
328 | assertTrue (!ip1.isNodeLocalMC()); |
329 | assertTrue (!ip1.isLinkLocalMC()); |
330 | assertTrue (!ip1.isSiteLocalMC()); |
331 | assertTrue (!ip1.isOrgLocalMC()); |
332 | assertTrue (!ip1.isGlobalMC()); |
333 | |
334 | IPAddress ip3("::1" ); // loopback |
335 | assertTrue (!ip3.isWildcard()); |
336 | assertTrue (!ip3.isBroadcast()); |
337 | assertTrue (ip3.isLoopback()); |
338 | assertTrue (!ip3.isMulticast()); |
339 | assertTrue (ip3.isUnicast()); |
340 | assertTrue (!ip3.isLinkLocal()); |
341 | assertTrue (!ip3.isSiteLocal()); |
342 | assertTrue (!ip3.isWellKnownMC()); |
343 | assertTrue (!ip3.isNodeLocalMC()); |
344 | assertTrue (!ip3.isLinkLocalMC()); |
345 | assertTrue (!ip3.isSiteLocalMC()); |
346 | assertTrue (!ip3.isOrgLocalMC()); |
347 | assertTrue (!ip3.isGlobalMC()); |
348 | |
349 | IPAddress ip4("2001:0db8:85a3:0000:0000:8a2e:0370:7334" ); // unicast |
350 | assertTrue (!ip4.isWildcard()); |
351 | assertTrue (!ip4.isBroadcast()); |
352 | assertTrue (!ip4.isLoopback()); |
353 | assertTrue (!ip4.isMulticast()); |
354 | assertTrue (ip4.isUnicast()); |
355 | assertTrue (!ip4.isLinkLocal()); |
356 | assertTrue (!ip4.isSiteLocal()); |
357 | assertTrue (!ip4.isWellKnownMC()); |
358 | assertTrue (!ip4.isNodeLocalMC()); |
359 | assertTrue (!ip4.isLinkLocalMC()); |
360 | assertTrue (!ip4.isSiteLocalMC()); |
361 | assertTrue (!ip4.isOrgLocalMC()); |
362 | assertTrue (!ip4.isGlobalMC()); |
363 | |
364 | IPAddress ip5("fe80::21f:5bff:fec6:6707" ); // link local unicast |
365 | assertTrue (!ip5.isWildcard()); |
366 | assertTrue (!ip5.isBroadcast()); |
367 | assertTrue (!ip5.isLoopback()); |
368 | assertTrue (!ip5.isMulticast()); |
369 | assertTrue (ip5.isUnicast()); |
370 | assertTrue (ip5.isLinkLocal()); |
371 | assertTrue (!ip5.isSiteLocal()); |
372 | assertTrue (!ip5.isWellKnownMC()); |
373 | assertTrue (!ip5.isNodeLocalMC()); |
374 | assertTrue (!ip5.isLinkLocalMC()); |
375 | assertTrue (!ip5.isSiteLocalMC()); |
376 | assertTrue (!ip5.isOrgLocalMC()); |
377 | assertTrue (!ip5.isGlobalMC()); |
378 | |
379 | IPAddress ip10("fe80::12" ); // link local unicast |
380 | assertTrue (!ip10.isWildcard()); |
381 | assertTrue (!ip10.isBroadcast()); |
382 | assertTrue (!ip10.isLoopback()); |
383 | assertTrue (!ip10.isMulticast()); |
384 | assertTrue (ip10.isUnicast()); |
385 | assertTrue (ip10.isLinkLocal()); |
386 | assertTrue (!ip10.isSiteLocal()); |
387 | assertTrue (!ip10.isWellKnownMC()); |
388 | assertTrue (!ip10.isNodeLocalMC()); |
389 | assertTrue (!ip10.isLinkLocalMC()); |
390 | assertTrue (!ip10.isSiteLocalMC()); |
391 | assertTrue (!ip10.isOrgLocalMC()); |
392 | assertTrue (!ip10.isGlobalMC()); |
393 | |
394 | IPAddress ip6("fec0::21f:5bff:fec6:6707" ); // site local unicast (RFC 4291) |
395 | assertTrue (!ip6.isWildcard()); |
396 | assertTrue (!ip6.isBroadcast()); |
397 | assertTrue (!ip6.isLoopback()); |
398 | assertTrue (!ip6.isMulticast()); |
399 | assertTrue (ip6.isUnicast()); |
400 | assertTrue (!ip6.isLinkLocal()); |
401 | assertTrue (ip6.isSiteLocal()); |
402 | assertTrue (!ip6.isWellKnownMC()); |
403 | assertTrue (!ip6.isNodeLocalMC()); |
404 | assertTrue (!ip6.isLinkLocalMC()); |
405 | assertTrue (!ip6.isSiteLocalMC()); |
406 | assertTrue (!ip6.isOrgLocalMC()); |
407 | assertTrue (!ip6.isGlobalMC()); |
408 | |
409 | IPAddress ip7("fc00::21f:5bff:fec6:6707" ); // site local unicast (RFC 4193) |
410 | assertTrue (!ip7.isWildcard()); |
411 | assertTrue (!ip7.isBroadcast()); |
412 | assertTrue (!ip7.isLoopback()); |
413 | assertTrue (!ip7.isMulticast()); |
414 | assertTrue (ip7.isUnicast()); |
415 | assertTrue (!ip7.isLinkLocal()); |
416 | assertTrue (ip7.isSiteLocal()); |
417 | assertTrue (!ip7.isWellKnownMC()); |
418 | assertTrue (!ip7.isNodeLocalMC()); |
419 | assertTrue (!ip7.isLinkLocalMC()); |
420 | assertTrue (!ip7.isSiteLocalMC()); |
421 | assertTrue (!ip7.isOrgLocalMC()); |
422 | assertTrue (!ip7.isGlobalMC()); |
423 | #endif |
424 | } |
425 | |
426 | |
427 | void IPAddressTest::testMCClassification6() |
428 | { |
429 | #ifdef POCO_HAVE_IPv6 |
430 | IPAddress ip1("ff02:0:0:0:0:0:0:c" ); // well-known link-local multicast |
431 | assertTrue (!ip1.isWildcard()); |
432 | assertTrue (!ip1.isBroadcast()); |
433 | assertTrue (!ip1.isLoopback()); |
434 | assertTrue (ip1.isMulticast()); |
435 | assertTrue (!ip1.isUnicast()); |
436 | assertTrue (!ip1.isLinkLocal()); |
437 | assertTrue (!ip1.isSiteLocal()); |
438 | assertTrue (ip1.isWellKnownMC()); |
439 | assertTrue (!ip1.isNodeLocalMC()); |
440 | assertTrue (ip1.isLinkLocalMC()); |
441 | assertTrue (!ip1.isSiteLocalMC()); |
442 | assertTrue (!ip1.isOrgLocalMC()); |
443 | assertTrue (!ip1.isGlobalMC()); |
444 | |
445 | IPAddress ip2("ff01:0:0:0:0:0:0:FB" ); // node-local unicast |
446 | assertTrue (!ip2.isWildcard()); |
447 | assertTrue (!ip2.isBroadcast()); |
448 | assertTrue (!ip2.isLoopback()); |
449 | assertTrue (ip2.isMulticast()); |
450 | assertTrue (!ip2.isUnicast()); |
451 | assertTrue (!ip2.isLinkLocal()); |
452 | assertTrue (!ip2.isSiteLocal()); |
453 | assertTrue (ip2.isWellKnownMC()); |
454 | assertTrue (ip2.isNodeLocalMC()); |
455 | assertTrue (!ip2.isLinkLocalMC()); |
456 | assertTrue (!ip2.isSiteLocalMC()); |
457 | assertTrue (!ip2.isOrgLocalMC()); |
458 | assertTrue (!ip2.isGlobalMC()); |
459 | |
460 | IPAddress ip3("ff05:0:0:0:0:0:0:FB" ); // site local unicast |
461 | assertTrue (!ip3.isWildcard()); |
462 | assertTrue (!ip3.isBroadcast()); |
463 | assertTrue (!ip3.isLoopback()); |
464 | assertTrue (ip3.isMulticast()); |
465 | assertTrue (!ip3.isUnicast()); |
466 | assertTrue (!ip3.isLinkLocal()); |
467 | assertTrue (!ip3.isSiteLocal()); |
468 | assertTrue (ip3.isWellKnownMC()); |
469 | assertTrue (!ip3.isNodeLocalMC()); |
470 | assertTrue (!ip3.isLinkLocalMC()); |
471 | assertTrue (ip3.isSiteLocalMC()); |
472 | assertTrue (!ip3.isOrgLocalMC()); |
473 | assertTrue (!ip3.isGlobalMC()); |
474 | |
475 | IPAddress ip4("ff18:0:0:0:0:0:0:FB" ); // org local unicast |
476 | assertTrue (!ip4.isWildcard()); |
477 | assertTrue (!ip4.isBroadcast()); |
478 | assertTrue (!ip4.isLoopback()); |
479 | assertTrue (ip4.isMulticast()); |
480 | assertTrue (!ip4.isUnicast()); |
481 | assertTrue (!ip4.isLinkLocal()); |
482 | assertTrue (!ip4.isSiteLocal()); |
483 | assertTrue (!ip4.isWellKnownMC()); |
484 | assertTrue (!ip4.isNodeLocalMC()); |
485 | assertTrue (!ip4.isLinkLocalMC()); |
486 | assertTrue (!ip4.isSiteLocalMC()); |
487 | assertTrue (ip4.isOrgLocalMC()); |
488 | assertTrue (!ip4.isGlobalMC()); |
489 | |
490 | IPAddress ip5("ff1f:0:0:0:0:0:0:FB" ); // global unicast |
491 | assertTrue (!ip5.isWildcard()); |
492 | assertTrue (!ip5.isBroadcast()); |
493 | assertTrue (!ip5.isLoopback()); |
494 | assertTrue (ip5.isMulticast()); |
495 | assertTrue (!ip5.isUnicast()); |
496 | assertTrue (!ip5.isLinkLocal()); |
497 | assertTrue (!ip5.isSiteLocal()); |
498 | assertTrue (!ip5.isWellKnownMC()); |
499 | assertTrue (!ip5.isNodeLocalMC()); |
500 | assertTrue (!ip5.isLinkLocalMC()); |
501 | assertTrue (!ip5.isSiteLocalMC()); |
502 | assertTrue (!ip5.isOrgLocalMC()); |
503 | assertTrue (ip5.isGlobalMC()); |
504 | #endif |
505 | } |
506 | |
507 | |
508 | void IPAddressTest::testRelationals() |
509 | { |
510 | IPAddress ip1("192.168.1.120" ); |
511 | IPAddress ip2(ip1); |
512 | IPAddress ip3; |
513 | IPAddress ip4("10.0.0.138" ); |
514 | |
515 | assertTrue (ip1 != ip4); |
516 | assertTrue (ip1 == ip2); |
517 | assertTrue (!(ip1 != ip2)); |
518 | assertTrue (!(ip1 == ip4)); |
519 | assertTrue (ip1 > ip4); |
520 | assertTrue (ip1 >= ip4); |
521 | assertTrue (ip4 < ip1); |
522 | assertTrue (ip4 <= ip1); |
523 | assertTrue (!(ip1 < ip4)); |
524 | assertTrue (!(ip1 <= ip4)); |
525 | assertTrue (!(ip4 > ip1)); |
526 | assertTrue (!(ip4 >= ip1)); |
527 | |
528 | ip3 = ip1; |
529 | assertTrue (ip1 == ip3); |
530 | ip3 = ip4; |
531 | assertTrue (ip1 != ip3); |
532 | assertTrue (ip3 == ip4); |
533 | } |
534 | |
535 | |
536 | void IPAddressTest::testWildcard() |
537 | { |
538 | IPAddress wildcard = IPAddress::wildcard(); |
539 | assertTrue (wildcard.isWildcard()); |
540 | assertTrue (wildcard.toString() == "0.0.0.0" ); |
541 | } |
542 | |
543 | |
544 | void IPAddressTest::testBroadcast() |
545 | { |
546 | IPAddress broadcast = IPAddress::broadcast(); |
547 | assertTrue (broadcast.isBroadcast()); |
548 | assertTrue (broadcast.toString() == "255.255.255.255" ); |
549 | } |
550 | |
551 | |
552 | void IPAddressTest::testPrefixCons() |
553 | { |
554 | IPAddress ia1(15, IPAddress::IPv4); |
555 | assertTrue (ia1.toString() == "255.254.0.0" ); |
556 | |
557 | #ifdef POCO_HAVE_IPv6 |
558 | IPAddress ia2(62, IPAddress::IPv6); |
559 | assertTrue (ia2.toString() == "ffff:ffff:ffff:fffc::" ); |
560 | #endif |
561 | } |
562 | |
563 | |
564 | void IPAddressTest::testPrefixLen() |
565 | { |
566 | IPAddress ia1(15, IPAddress::IPv4); |
567 | assertTrue (ia1.prefixLength() == 15); |
568 | |
569 | IPAddress ia2(16, IPAddress::IPv4); |
570 | assertTrue (ia2.prefixLength() == 16); |
571 | |
572 | IPAddress ia3(23, IPAddress::IPv4); |
573 | assertTrue (ia3.prefixLength() == 23); |
574 | |
575 | IPAddress ia4(24, IPAddress::IPv4); |
576 | assertTrue (ia4.prefixLength() == 24); |
577 | |
578 | IPAddress ia5(25, IPAddress::IPv4); |
579 | assertTrue (ia5.prefixLength() == 25); |
580 | |
581 | #ifdef POCO_HAVE_IPv6 |
582 | IPAddress ia6(62, IPAddress::IPv6); |
583 | assertTrue (ia6.prefixLength() == 62); |
584 | |
585 | IPAddress ia7(63, IPAddress::IPv6); |
586 | assertTrue (ia7.prefixLength() == 63); |
587 | |
588 | IPAddress ia8(64, IPAddress::IPv6); |
589 | assertTrue (ia8.prefixLength() == 64); |
590 | |
591 | IPAddress ia9(65, IPAddress::IPv6); |
592 | assertTrue (ia9.prefixLength() == 65); |
593 | #endif |
594 | } |
595 | |
596 | |
597 | void IPAddressTest::testOperators() |
598 | { |
599 | IPAddress ip("10.0.0.51" ); |
600 | IPAddress mask(24, IPAddress::IPv4); |
601 | |
602 | IPAddress net = ip & mask; |
603 | assertTrue (net.toString() == "10.0.0.0" ); |
604 | |
605 | IPAddress host("0.0.0.51" ); |
606 | assertTrue ((net | host) == ip); |
607 | |
608 | assertTrue ((~mask).toString() == "0.0.0.255" ); |
609 | } |
610 | |
611 | |
612 | void IPAddressTest::testRelationals6() |
613 | { |
614 | #ifdef POCO_HAVE_IPv6 |
615 | #endif |
616 | } |
617 | |
618 | |
619 | void IPAddressTest::testByteOrderMacros() |
620 | { |
621 | Poco::UInt16 a16 = 0xDEAD; |
622 | assertTrue (poco_ntoh_16(a16) == ntohs(a16)); |
623 | assertTrue (poco_hton_16(a16) == htons(a16)); |
624 | Poco::UInt32 a32 = 0xDEADBEEF; |
625 | assertTrue (poco_ntoh_32(a32) == ntohl(a32)); |
626 | assertTrue (poco_hton_32(a32) == htonl(a32)); |
627 | } |
628 | |
629 | |
630 | void IPAddressTest::setUp() |
631 | { |
632 | } |
633 | |
634 | |
635 | void IPAddressTest::tearDown() |
636 | { |
637 | } |
638 | |
639 | |
640 | CppUnit::Test* IPAddressTest::suite() |
641 | { |
642 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("IPAddressTest" ); |
643 | |
644 | CppUnit_addTest(pSuite, IPAddressTest, testStringConv); |
645 | CppUnit_addTest(pSuite, IPAddressTest, testStringConv6); |
646 | CppUnit_addTest(pSuite, IPAddressTest, testParse); |
647 | CppUnit_addTest(pSuite, IPAddressTest, testClassification); |
648 | CppUnit_addTest(pSuite, IPAddressTest, testMCClassification); |
649 | CppUnit_addTest(pSuite, IPAddressTest, testClassification6); |
650 | CppUnit_addTest(pSuite, IPAddressTest, testMCClassification6); |
651 | CppUnit_addTest(pSuite, IPAddressTest, testRelationals); |
652 | CppUnit_addTest(pSuite, IPAddressTest, testRelationals6); |
653 | CppUnit_addTest(pSuite, IPAddressTest, testWildcard); |
654 | CppUnit_addTest(pSuite, IPAddressTest, testBroadcast); |
655 | CppUnit_addTest(pSuite, IPAddressTest, testPrefixCons); |
656 | CppUnit_addTest(pSuite, IPAddressTest, testPrefixLen); |
657 | CppUnit_addTest(pSuite, IPAddressTest, testOperators); |
658 | CppUnit_addTest(pSuite, IPAddressTest, testByteOrderMacros); |
659 | |
660 | return pSuite; |
661 | } |
662 | |