12
12
#include " CppUnit/TestCaller.h"
13
13
#include " CppUnit/TestSuite.h"
14
14
#include " Poco/Net/IPAddress.h"
15
+ #include " Poco/Net/NetworkInterface.h"
15
16
#include " Poco/Net/NetException.h"
17
+ #include " Poco/Format.h"
18
+ #include < iostream>
16
19
17
20
18
21
using Poco::Net::IPAddress;
22
+ using Poco::Net::NetworkInterface;
19
23
using Poco::Net::InvalidAddressException;
20
24
21
25
@@ -35,7 +39,7 @@ void IPAddressTest::testStringConv()
35
39
IPAddress ia1 (std::move (ia01));
36
40
assertTrue (ia1.family () == IPAddress::IPv4);
37
41
assertTrue (ia1.toString () == " 127.0.0.1" );
38
-
42
+
39
43
IPAddress ia02 = IPAddress (" 192.168.1.120" );
40
44
IPAddress ia2 (std::move (ia02));
41
45
assertTrue (ia2.family () == IPAddress::IPv4);
@@ -67,7 +71,7 @@ void IPAddressTest::testStringConv6()
67
71
IPAddress ia1 (std::move (ia01));
68
72
assertTrue (ia1.family () == IPAddress::IPv6);
69
73
assertTrue (ia1.toString () == " 1080::8:600:200a:425c" );
70
-
74
+
71
75
IPAddress ia02 = IPAddress (" 1080::8:600:200A:425C" );
72
76
IPAddress ia2 (std::move (ia02));
73
77
assertTrue (ia2.family () == IPAddress::IPv6);
@@ -424,6 +428,44 @@ void IPAddressTest::testClassification6()
424
428
assertTrue (!ip10.isOrgLocalMC ());
425
429
assertTrue (!ip10.isGlobalMC ());
426
430
431
+ NetworkInterface::Map m = NetworkInterface::map (false , false );
432
+ for (auto it = m.begin (); it != m.end (); ++it)
433
+ {
434
+ IPAddress ip11 (Poco::format (" fe80::1592:96a0:88bf:d2d7%%%s" ,
435
+ it->second .adapterName ())); // link local unicast scoped
436
+ assertEqual (ip11.scope (), it->second .index ());
437
+ assertTrue (!ip11.isWildcard ());
438
+ assertTrue (!ip11.isBroadcast ());
439
+ assertTrue (!ip11.isLoopback ());
440
+ assertTrue (!ip11.isMulticast ());
441
+ assertTrue (ip11.isUnicast ());
442
+ assertTrue (ip11.isLinkLocal ());
443
+ assertTrue (!ip11.isSiteLocal ());
444
+ assertTrue (!ip11.isWellKnownMC ());
445
+ assertTrue (!ip11.isNodeLocalMC ());
446
+ assertTrue (!ip11.isLinkLocalMC ());
447
+ assertTrue (!ip11.isSiteLocalMC ());
448
+ assertTrue (!ip11.isOrgLocalMC ());
449
+ assertTrue (!ip11.isGlobalMC ());
450
+
451
+ IPAddress ip12 (Poco::format (" [fe80::1592:96a0:88bf:d2d7%%%s]" ,
452
+ it->second .adapterName ())); // link local unicast scoped
453
+ assertEqual (ip12.scope (), it->second .index ());
454
+ assertTrue (!ip12.isWildcard ());
455
+ assertTrue (!ip12.isBroadcast ());
456
+ assertTrue (!ip12.isLoopback ());
457
+ assertTrue (!ip12.isMulticast ());
458
+ assertTrue (ip12.isUnicast ());
459
+ assertTrue (ip12.isLinkLocal ());
460
+ assertTrue (!ip12.isSiteLocal ());
461
+ assertTrue (!ip12.isWellKnownMC ());
462
+ assertTrue (!ip12.isNodeLocalMC ());
463
+ assertTrue (!ip12.isLinkLocalMC ());
464
+ assertTrue (!ip12.isSiteLocalMC ());
465
+ assertTrue (!ip12.isOrgLocalMC ());
466
+ assertTrue (!ip12.isGlobalMC ());
467
+ }
468
+
427
469
IPAddress ip6 (" fec0::21f:5bff:fec6:6707" ); // site local unicast (RFC 4291)
428
470
assertTrue (!ip6.isWildcard ());
429
471
assertTrue (!ip6.isBroadcast ());
@@ -690,6 +732,32 @@ void IPAddressTest::testByteOrderMacros()
690
732
}
691
733
692
734
735
+ void IPAddressTest::testScoped ()
736
+ {
737
+ #ifdef POCO_HAVE_IPv6
738
+ NetworkInterface::Map m = NetworkInterface::map (false , false );
739
+ if (m.size () == 0 )
740
+ {
741
+ std::cout << " No network interfaces found." << std::endl;
742
+ return ;
743
+ }
744
+
745
+ IPAddress ip;
746
+ assertFalse (IPAddress::tryParse (" fe80::1592:96a0:88bf:d2d7%xyzabc123" , ip));
747
+
748
+ std::string scope;
749
+ auto it = m.begin ();
750
+ auto end = m.end ();
751
+ for (; it != end; ++it)
752
+ {
753
+ scope = it->second .adapterName ();
754
+ assertTrue (IPAddress::tryParse (Poco::format (" [fe80::1592:96a0:88bf:d2d7%%%s]" , scope), ip));
755
+ assertTrue (IPAddress::tryParse (Poco::format (" fe80::1592:96a0:88bf:d2d7%%%s" , scope), ip));
756
+ }
757
+ #endif
758
+ }
759
+
760
+
693
761
void IPAddressTest::setUp ()
694
762
{
695
763
}
@@ -719,6 +787,7 @@ CppUnit::Test* IPAddressTest::suite()
719
787
CppUnit_addTest (pSuite, IPAddressTest, testPrefixLen);
720
788
CppUnit_addTest (pSuite, IPAddressTest, testOperators);
721
789
CppUnit_addTest (pSuite, IPAddressTest, testByteOrderMacros);
790
+ CppUnit_addTest (pSuite, IPAddressTest, testScoped);
722
791
723
792
return pSuite;
724
793
}
0 commit comments