void test_octets() { var ip = IPAddress.parse("10.1.2.3/8").unwrap(); TestIpAddress.Compare(ip.parts(), new List <uint> { 10, 1, 2, 3 }); }
void test_method_subnet() { Assert.True(setup().network.subnet(23).isErr()); Assert.True(setup().network.subnet(33).isErr()); Assert.True(setup().ip.subnet(30).isOk()); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.subnet(26).unwrap()), new List <String> { "172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", "172.16.10.192/26" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.subnet(25).unwrap()), new List <String> { "172.16.10.0/25", "172.16.10.128/25" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.subnet(24).unwrap()), new List <String> { "172.16.10.0/24" }); }
void test_summarize() { var netstr = new List <string>(); var ranges = new[] { Range(1, 10), Range(11, 127), Range(128, 169), Range(170, 172), Range(173, 192), Range(193, 224) }; foreach (var range in ranges) { for (var i = range.start; i < range.stop; i++) { netstr.Add(string.Format("{0}.0.0.0/8", i)); } } for (var i = 0; i < 256; i++) { if (i != 254) { netstr.Add(string.Format("169.{0}.0.0/16", i)); } } for (var i = 0; i < 256; i++) { if (i < 16 || 31 < i) { netstr.Add(string.Format("172.{0}.0.0/16", i)); } } for (var i = 0; i < 256; i++) { if (i != 168) { netstr.Add(string.Format("192.{0}.0.0/16", i)); } } var ip_addresses = new List <IPAddress>(); foreach (var net in netstr) { ip_addresses.Add(IPAddress.parse(net).unwrap()); } var empty_vec = new List <string>(); Assert.Equal(IPAddress.summarize_str(empty_vec).unwrap().Count, 0); var sone = IPAddress.summarize_str(new List <string> { "10.1.0.4/24" }).unwrap(); var one = IPAddress.to_string_vec(sone); TestIpAddress.Compare(one, new List <String> { "10.1.0.0/24" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "2000:1::4711/32" }).unwrap()), new List <String> { "2000:1::/32" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "10.1.0.4/24", "7.0.0.0/0", "1.2.3.4/4" }).unwrap()), new List <String> { "0.0.0.0/0" }); var tmp = IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "2000:1::/32", "3000:1::/32", "2000:2::/32", "2000:3::/32", "2000:4::/32", "2000:5::/32", "2000:6::/32", "2000:7::/32", "2000:8::/32" }) .unwrap()); TestIpAddress.Compare(tmp, new List <String> { "2000:1::/32", "2000:2::/31", "2000:4::/30", "2000:8::/32", "3000:1::/32" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "10.0.1.1/24", "30.0.1.0/16", "10.0.2.0/24", "10.0.3.0/24", "10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24", "10.0.7.0/24", "10.0.8.0/24" }).unwrap()), new List <String> { "10.0.1.0/24", "10.0.2.0/23", "10.0.4.0/22", "10.0.8.0/24", "30.0.0.0/16" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "10.0.0.0/23", "10.0.2.0/24" }).unwrap()), new List <String> { "10.0.0.0/23", "10.0.2.0/24" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/23" }) .unwrap()), new List <String> { "10.0.0.0/22" }); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize_str(new List <String> { "10.0.0.0/16", "10.0.2.0/24" }) .unwrap()), new List <String> { "10.0.0.0/16" }); var cnt = 10; for (var i = 0; i < cnt; i++) { TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(ip_addresses)), new List <String> { "1.0.0.0/8", "2.0.0.0/7", "4.0.0.0/6", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3", "64.0.0.0/3", "96.0.0.0/4", "112.0.0.0/5", "120.0.0.0/6", "124.0.0.0/7", "126.0.0.0/8", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/8", "169.0.0.0/9", "169.128.0.0/10", "169.192.0.0/11", "169.224.0.0/12", "169.240.0.0/13", "169.248.0.0/14", "169.252.0.0/15", "169.255.0.0/16", "170.0.0.0/7", "172.0.0.0/12", "172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7", "176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16", "192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10", "193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4" }); } // end // printer = RubyProf::GraphPrinter.new(result) // printer.print(STDOUT, {}) // test imutable input parameters var a1 = IPAddress.parse("10.0.0.1/24").unwrap(); var a2 = IPAddress.parse("10.0.1.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { a1, a2 })), new List <String> { "10.0.0.0/23" }); Assert.Equal("10.0.0.1/24", a1.to_string()); Assert.Equal("10.0.1.1/24", a2.to_string()); }
void test_classmethod_summarize() { // Should return self if only one network given TestIpAddress.Compare(IPAddress.summarize(new List <IPAddress> { setup().ip }), new List <IPAddress> { setup().ip.network() }); // Summarize homogeneous networks var ip1 = IPAddress.parse("172.16.10.1/24").unwrap(); var ip2 = IPAddress.parse("172.16.11.2/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2 })), new List <String> { "172.16.10.0/23" }); ip1 = IPAddress.parse("10.0.0.1/24").unwrap(); ip2 = IPAddress.parse("10.0.1.1/24").unwrap(); var ip3 = IPAddress.parse("10.0.2.1/24").unwrap(); var ip4 = IPAddress.parse("10.0.3.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2, ip3, ip4 })), new List <String> { "10.0.0.0/22" }); ip1 = IPAddress.parse("10.0.0.1/24").unwrap(); ip2 = IPAddress.parse("10.0.1.1/24").unwrap(); ip3 = IPAddress.parse("10.0.2.1/24").unwrap(); ip4 = IPAddress.parse("10.0.3.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip4, ip3, ip2, ip1 })), new List <String> { "10.0.0.0/22" }); // Summarize non homogeneous networks ip1 = IPAddress.parse("10.0.0.0/23").unwrap(); ip2 = IPAddress.parse("10.0.2.0/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2 })), new List <String> { "10.0.0.0/23", "10.0.2.0/24" }); ip1 = IPAddress.parse("10.0.0.0/16").unwrap(); ip2 = IPAddress.parse("10.0.2.0/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2 })), new List <String> { "10.0.0.0/16" }); ip1 = IPAddress.parse("10.0.0.0/23").unwrap(); ip2 = IPAddress.parse("10.1.0.0/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2 })), new List <String> { "10.0.0.0/23", "10.1.0.0/24" }); ip1 = IPAddress.parse("10.0.0.0/23").unwrap(); ip2 = IPAddress.parse("10.0.2.0/23").unwrap(); ip3 = IPAddress.parse("10.0.4.0/24").unwrap(); ip4 = IPAddress.parse("10.0.6.0/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2, ip3, ip4 })), new List <String> { "10.0.0.0/22", "10.0.4.0/24", "10.0.6.0/24" }); ip1 = IPAddress.parse("10.0.1.1/24").unwrap(); ip2 = IPAddress.parse("10.0.2.1/24").unwrap(); ip3 = IPAddress.parse("10.0.3.1/24").unwrap(); ip4 = IPAddress.parse("10.0.4.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2, ip3, ip4 })), new List <String> { "10.0.1.0/24", "10.0.2.0/23", "10.0.4.0/24" }); ip1 = IPAddress.parse("10.0.1.1/24").unwrap(); ip2 = IPAddress.parse("10.0.2.1/24").unwrap(); ip3 = IPAddress.parse("10.0.3.1/24").unwrap(); ip4 = IPAddress.parse("10.0.4.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip4, ip3, ip2, ip1 })), new List <String> { "10.0.1.0/24", "10.0.2.0/23", "10.0.4.0/24" }); ip1 = IPAddress.parse("10.0.1.1/24").unwrap(); ip2 = IPAddress.parse("10.10.2.1/24").unwrap(); ip3 = IPAddress.parse("172.16.0.1/24").unwrap(); ip4 = IPAddress.parse("172.16.1.1/24").unwrap(); TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(new List <IPAddress> { ip1, ip2, ip3, ip4 })), new List <String> { "10.0.1.0/24", "10.10.2.0/24", "172.16.0.0/23" }); var ips = new List <IPAddress> { IPAddress.parse("10.0.0.12/30").unwrap(), IPAddress.parse("10.0.100.0/24").unwrap() }; TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(ips)), new List <String> { "10.0.0.12/30", "10.0.100.0/24" }); ips = new List <IPAddress> { IPAddress.parse("172.16.0.0/31").unwrap(), IPAddress.parse("10.10.2.1/32").unwrap() }; TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(ips)), new List <String> { "10.10.2.1/32", "172.16.0.0/31" }); var xips = new List <IPAddress> { IPAddress.parse("172.16.0.0/32").unwrap(), IPAddress.parse("10.10.2.1/32").unwrap() }; TestIpAddress.Compare(IPAddress.to_string_vec(IPAddress.summarize(xips)), new List <String> { "10.10.2.1/32", "172.16.0.0/32" }); }
void test_method_split() { Assert.True(setup().ip.split(0).isErr()); Assert.True(setup().ip.split(257).isErr()); TestIpAddress.Compare(setup().ip.split(1).unwrap(), new List <IPAddress> { setup().ip.network() }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(8).unwrap()), new List <String> { "172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", "172.16.10.192/27", "172.16.10.224/27" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(7).unwrap()), new List <String> { "172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/27", "172.16.10.160/27", "172.16.10.192/26" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(6).unwrap()), new List <String> { "172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/26", "172.16.10.192/26" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(5).unwrap()), new List <String> { "172.16.10.0/27", "172.16.10.32/27", "172.16.10.64/27", "172.16.10.96/27", "172.16.10.128/25" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(4).unwrap()), new List <String> { "172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/26", "172.16.10.192/26" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(3).unwrap()), new List <String> { "172.16.10.0/26", "172.16.10.64/26", "172.16.10.128/25" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(2).unwrap()), new List <String> { "172.16.10.0/25", "172.16.10.128/25" }); TestIpAddress.Compare(IPAddress.to_string_vec(setup().network.split(1).unwrap()), new List <String> { "172.16.10.0/24" }); }