def test_range(self): for i in range(5): rng = AddressRange(IP(i)) self.assertEqual(rng.start, i) self.assertEqual(rng.end, i) self.assertEqual(rng.family, IPv4) for j in range(5): if i > j: with self.assertRaises(AssertionError): AddressRange(IP(i), IP(j)) else: rng = AddressRange(IP(i), IP(j)) self.assertEqual(rng.start, i) self.assertEqual(rng.end, j) self.assertEqual(rng.family, IPv4) with self.assertRaises(AssertionError): rng.end = IP() v4rangestr = '0.0.0.0-255.255.255.255' v6rangestr = '::-ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff' v4range = AddressRange.from_string(v4rangestr) v6range = AddressRange.from_string(v6rangestr) self.assertEqual(str(v4range), v4rangestr) self.assertEqual(str(v6range), v6rangestr) self.assertEqual(v4range, AddressRange(IPv4(0), IPv4(IPV4MAX))) self.assertEqual(v6range, AddressRange(IPv6(0), IPv6(IPV6MAX)))
def test_addrlist(self): alist = AddrList() self.assertSetEqual(set(alist.ranges.keys()), {IPv4, IPv6}) alist.add_ip('127.0.0.1') self.assertIn('127.0.0.1', alist) self.assertNotIn('127.0.0.0', alist) alist.add_range('127.0.0.2-127.0.0.255') self.assertListEqual( alist.ranges[IPv4], [AddressRange.from_string('127.0.0.1-127.0.0.255')]) alist.add_ip('127.0.0.0') self.assertListEqual(alist.ranges[IPv4], [Subnet.from_string('127.0.0.0/24')]) alist.add_subnet('127.0.2.0/23') self.assertListEqual(alist.ranges[IPv4], [ Subnet.from_string('127.0.0.0/24'), Subnet.from_string('127.0.2.0/23') ]) # Unify disjoint ranges with single addition sub = Subnet.from_string('127.0.1.0/24') self.assertNotIn(sub.start, alist) self.assertNotIn(sub.end, alist) alist.add_addressrange(sub) self.assertListEqual(alist.ranges[IPv4], [Subnet.from_string('127.0.0.0/22')])
def test_addrlist(self): alist = AddrList() self.assertSetEqual(set(alist.ranges.keys()), {IPv4, IPv6}) alist.add_ip('127.0.0.1') self.assertIn('127.0.0.1', alist) self.assertNotIn('127.0.0.0', alist) alist.add_range('127.0.0.2-127.0.0.255') self.assertListEqual( alist.ranges[IPv4], [AddressRange.from_string('127.0.0.1-127.0.0.255')]) alist.add_ip('127.0.0.0') self.assertListEqual(alist.ranges[IPv4], [Subnet.from_string('127.0.0.0/24')]) alist.add_subnet('127.0.2.0/23') self.assertListEqual(alist.ranges[IPv4], [Subnet.from_string('127.0.0.0/24'), Subnet.from_string('127.0.2.0/23')]) # Unify disjoint ranges with single addition sub = Subnet.from_string('127.0.1.0/24') self.assertNotIn(sub.start, alist) self.assertNotIn(sub.end, alist) alist.add_addressrange(sub) self.assertListEqual(alist.ranges[IPv4], [Subnet.from_string('127.0.0.0/22')])
def test_ordering(self): def expect(A1, A2, B1, B2): assert A2 >= A1 and B2 >= B1 lt = A2 + 1 < B1 eq = A1 == B1 and A2 == B2 gt = A1 > B2 + 1 a_in_b = B1 <= A1 and A2 <= B2 b_in_a = A1 <= B1 and B2 <= A2 return (lt, eq, gt, a_in_b, b_in_a) def test(A, B): return (A < B, A == B, A > B, A in B, B in A) y, z = 3, 6 B = AddressRange(IPv4(y), IPv4(z)) for w in range(10): for x in range(w, 10): A = AddressRange(IPv4(w), IPv4(x)) self.assertEqual(test(A, B), expect(w, x, y, z)) self.assertEqual(test(B, A), expect(y, z, w, x))
def test_addition(self): range1 = AddressRange.from_string('127.0.0.0-127.0.0.255') range2 = AddressRange.from_string('127.0.1.0-127.0.1.255') range3 = AddressRange.from_string('127.0.1.1-127.0.1.255') range4 = AddressRange.from_string('127.0.0.128-127.0.1.128') range5 = AddressRange.from_string('127.0.0.0-127.0.1.255') # Adjacent ranges self.assertEqual(range1 + range2, range5) self.assertEqual(range2 + range1, range5) # Disjoint ranges self.assertEqual(range1 + range3, (range1, range3)) self.assertEqual(range3 + range1, (range1, range3)) # Nested ranges self.assertEqual(range2 + range3, range2) self.assertEqual(range3 + range2, range2) # Overlapping ranges self.assertEqual(range1 + range4, AddressRange.from_string('127.0.0.0-127.0.1.128')) self.assertEqual(range2 + range4, AddressRange.from_string('127.0.0.128-127.0.1.255'))