void testEndpoint () { testcase ("Endpoint"); { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 0); expect (to_string (result.first) == "1.2.3.4"); } } { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4:5")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 5); expect (to_string (result.first) == "1.2.3.4:5"); } } Endpoint ep; ep = Endpoint (AddressV4 (127,0,0,1), 80); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect ( is_loopback (ep)); expect (to_string (ep) == "127.0.0.1:80"); ep = Endpoint (AddressV4 (10,0,0,1)); expect (AddressV4::get_class (ep.to_v4()) == 'A'); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "10.0.0.1"); ep = Endpoint (AddressV4 (166,78,151,147)); expect (! is_unspecified (ep)); expect ( is_public (ep)); expect (! is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "166.78.151.147"); }
void testAddress () { testcase ("Address"); std::pair <Address, bool> result ( Address::from_string ("1.2.3.4")); expect (result.second); if (expect (result.first.is_v4 ())) expect (result.first.to_v4() == AddressV4 (1, 2, 3, 4)); }
void testAddressV4 () { testcase ("AddressV4"); expect (AddressV4().value == 0); expect (is_unspecified (AddressV4())); expect (AddressV4(0x01020304).value == 0x01020304); expect (AddressV4(1, 2, 3, 4).value == 0x01020304); unexpected (is_unspecified (AddressV4(1, 2, 3, 4))); AddressV4 const v1 (1); expect (AddressV4(v1).value == 1); { AddressV4 v; v = v1; expect (v.value == v1.value); } { AddressV4 v; v [0] = 1; v [1] = 2; v [2] = 3; v [3] = 4; expect (v.value == 0x01020304); } expect (to_string (AddressV4(0x01020304)) == "1.2.3.4"); shouldParseV4 ("1.2.3.4", 0x01020304); shouldParseV4 ("255.255.255.255", 0xffffffff); shouldParseV4 ("0.0.0.0", 0); failParseV4 ("."); failParseV4 (".."); failParseV4 ("..."); failParseV4 ("...."); failParseV4 ("1"); failParseV4 ("1."); failParseV4 ("1.2"); failParseV4 ("1.2."); failParseV4 ("1.2.3"); failParseV4 ("1.2.3."); failParseV4 ("256.0.0.0"); failParseV4 ("-1.2.3.4"); }
void testEndpoint () { testcase ("Endpoint"); { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 0); expect (to_string (result.first) == "1.2.3.4"); } } { std::pair <Endpoint, bool> result ( Endpoint::from_string_checked ("1.2.3.4:5")); expect (result.second); if (expect (result.first.address().is_v4 ())) { expect (result.first.address().to_v4() == AddressV4 (1, 2, 3, 4)); expect (result.first.port() == 5); expect (to_string (result.first) == "1.2.3.4:5"); } } Endpoint ep; ep = Endpoint (AddressV4 (127,0,0,1), 80); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect ( is_loopback (ep)); expect (to_string (ep) == "127.0.0.1:80"); ep = Endpoint (AddressV4 (10,0,0,1)); expect (AddressV4::get_class (ep.to_v4()) == 'A'); expect (! is_unspecified (ep)); expect (! is_public (ep)); expect ( is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "10.0.0.1"); ep = Endpoint (AddressV4 (166,78,151,147)); expect (! is_unspecified (ep)); expect ( is_public (ep)); expect (! is_private (ep)); expect (! is_multicast (ep)); expect (! is_loopback (ep)); expect (to_string (ep) == "166.78.151.147"); { ep = Endpoint::from_string ("192.0.2.112"); expect (! is_unspecified (ep)); expect (ep == Endpoint::from_string_altform ("192.0.2.112")); auto const ep1 = Endpoint::from_string ("192.0.2.112:2016"); expect (! is_unspecified (ep1)); expect (ep.address() == ep1.address()); expect (ep1.port() == 2016); auto const ep2 = Endpoint::from_string_altform ("192.0.2.112:2016"); expect (! is_unspecified (ep2)); expect (ep.address() == ep2.address()); expect (ep2.port() == 2016); expect (ep1 == ep2); auto const ep3 = Endpoint::from_string_altform ("192.0.2.112 2016"); expect (! is_unspecified (ep3)); expect (ep.address() == ep3.address()); expect (ep3.port() == 2016); expect (ep2 == ep3); auto const ep4 = Endpoint::from_string_altform ("192.0.2.112 2016"); expect (! is_unspecified (ep4)); expect (ep.address() == ep4.address()); expect (ep4.port() == 2016); expect (ep3 == ep4); expect (to_string(ep1) == to_string(ep2)); expect (to_string(ep1) == to_string(ep3)); expect (to_string(ep1) == to_string(ep4)); } // Failures: expect (is_unspecified ( Endpoint::from_string ("192.0.2.112:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("192.0.2.112:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("192.0.2.112 port"))); expect (is_unspecified ( Endpoint::from_string ("ip:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("ip:port"))); expect (is_unspecified ( Endpoint::from_string_altform ("ip port"))); expect (is_unspecified ( Endpoint::from_string(""))); expect (is_unspecified ( Endpoint::from_string_altform(""))); expect (is_unspecified ( Endpoint::from_string("255"))); expect (is_unspecified ( Endpoint::from_string_altform("255"))); expect (is_unspecified ( Endpoint::from_string("512"))); expect (is_unspecified ( Endpoint::from_string_altform("512"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.256"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3.256"))); expect (is_unspecified ( Endpoint::from_string("1.2.3:80"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:80"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 80"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.4:65536"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:65536"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 65536"))); expect (is_unspecified ( Endpoint::from_string("1.2.3.4:89119"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3:89119"))); expect (is_unspecified ( Endpoint::from_string_altform("1.2.3 89119"))); }