Ejemplo n.º 1
0
    def test_contains(self):
        self.assertTrue(IPv4('1.2.3.4') in Prefix('1.2.3.0/24'))
        self.assertTrue(IPv4('1.2.3.0') in Prefix('1.2.3.0/24'))
        self.assertTrue(IPv4('1.2.3.255') in Prefix('1.2.3.0/24'))
        self.assertFalse(IPv4('1.2.4.1') in Prefix('1.2.3.0/24'))

        self.assertFalse(IPv6('::1.2.3.4') in Prefix('1.2.3.0/24'))
Ejemplo n.º 2
0
    def network(self):
        """The network for this IP.

        The value is an `aplib.net.range.Prefix` instance that represents the
        network for this IP.
        """
        return Prefix(self)
Ejemplo n.º 3
0
    def test_size(self):
        r = IPRange(IPv4('1.2.3.4'), IPv4('1.2.3.10'))
        self.assertEqual(r.size(), 7)

        r = IPRange(IPv4('0.0.0.0'), IPv4('255.255.255.255'))
        self.assertEqual(r.size(), 2**32)

        r = IPRange(IPv6('1::'), IPv6('2::'))
        self.assertEqual(r.size(), 2**112 + 1)

        r = IPGlob('1.2.3.4')
        self.assertEqual(r.size(), 1)

        r = Prefix('1.2.3.4')
        self.assertEqual(r.size(), 1)

        r = Prefix('2001:db8::/32')
        self.assertEqual(r.size(), 2**96)
Ejemplo n.º 4
0
    def test_comparison(self):
        expected_eq = [
            ('1.2.3.4', '1.2.3.4'),
            ('1.0.0.0/8', '1.0.0.0/8'),
            ('0.0.0.0', '0.0.0.0'),
            ('255.255.255.255', '255.255.255.255'),
            ('::', '::'),
        ]

        for a, b in expected_eq:
            ra = Prefix(a)
            rb = Prefix(b)
            self.assertTrue(ra == rb)
            self.assertTrue(ra <= rb)
            self.assertTrue(ra >= rb)
            self.assertFalse(ra != rb)
            self.assertFalse(ra < rb)
            self.assertFalse(ra > rb)

        expected_lt = [
            ('1.0.0.0/8', '2.0.0.0/8'),
            ('1.0.0.0/8', '1.0.0.0/16'),
            ('1.0.0.0/8', '1.192.0.0/10'),
            ('1.0.0.0/8', '1.64.0.0/10'),
            ('1.2.3.0-9', '1.2.3.5-14'),
            ('1.2.3.0-9', '1.2.3.5-100'),
        ]

        for a, b in expected_lt:
            if '-' in a:
                ra = IPGlob(a)
            else:
                ra = Prefix(a)
            if '-' in b:
                rb = IPGlob(b)
            else:
                rb = Prefix(b)
            self.assertTrue(ra < rb)
            self.assertTrue(ra <= rb)
            self.assertTrue(ra != rb)
            self.assertFalse(ra > rb)
            self.assertFalse(ra >= rb)
            self.assertFalse(ra == rb)
Ejemplo n.º 5
0
    def test_getitem(self):
        # This also implicitly tests iterator().
        r = Prefix('1.2.3.0/24')
        self.assertEqual(r[0], IPv4('1.2.3.0'))
        self.assertEqual(r[-1], IPv4('1.2.3.255'))
        self.assertEqual(list(r[:5]), [
            IPv4('1.2.3.0'),
            IPv4('1.2.3.1'),
            IPv4('1.2.3.2'),
            IPv4('1.2.3.3'),
            IPv4('1.2.3.4'),
        ])
        self.assertEqual(list(r[254:]), [IPv4('1.2.3.254'), IPv4('1.2.3.255')])
        self.assertEqual(list(r[3:5]), [IPv4('1.2.3.3'), IPv4('1.2.3.4')])
        self.assertEqual(list(r[3:20:5]), [
            IPv4('1.2.3.3'),
            IPv4('1.2.3.8'),
            IPv4('1.2.3.13'),
            IPv4('1.2.3.18'),
        ])

        r = Prefix('2001:db8::/32')
        self.assertEqual(r[0], IPv6('2001:db8::'))
        self.assertEqual(r[-1], IPv6('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff'))
Ejemplo n.º 6
0
    def test_network(self):
        self.assertEqual(IPv4('1.2.3.4/32').network, Prefix('1.2.3.4/32'))
        self.assertEqual(IPv4('1.2.3.4/24').network, Prefix('1.2.3.0/24'))
        self.assertEqual(IPv4('1.2.3.4/0').network, Prefix('0.0.0.0/0'))

        self.assertEqual(
            IPv6('1:2:3:4:5:6:7:8/128').network, Prefix('1:2:3:4:5:6:7:8'))
        self.assertEqual(
            IPv6('1:2:3:4:5:6:7:8/64').network, Prefix('1:2:3:4::/64'))
        self.assertEqual(IPv6('1:2:3:4:5:6:7:8/0').network, Prefix('::/0'))
Ejemplo n.º 7
0
 def test_is_supernet(self):
     self.assertFalse(
         Prefix('1.2.3.0/27').is_supernet(Prefix('1.2.3.0/24')))
     self.assertTrue(Prefix('1.2.3.0/16').is_supernet(Prefix('1.2.0.0/24')))
Ejemplo n.º 8
0
 def test_overlaps(self):
     self.assertTrue(Prefix('1.0.0.0/24').overlaps(Prefix('1.0.0.0/8')))
     self.assertTrue(Prefix('1.0.0.0/8').overlaps(Prefix('1.0.0.0/24')))
     self.assertTrue(Prefix('1.2.3.4').overlaps(Prefix('1.2.3.4')))
     self.assertTrue(Prefix('::').overlaps(Prefix('::')))
Ejemplo n.º 9
0
 def test_adjacent(self):
     self.assertTrue(Prefix('1.0.0.0/8').is_adjacent(Prefix('2.0.0.0/8')))
     self.assertFalse(Prefix('1.0.0.0/8').is_adjacent(Prefix('1.0.0.0/8')))
     self.assertFalse(
         Prefix('1.0.0.0/8').is_adjacent(Prefix('::2.0.0.0/88')))
     self.assertTrue(Prefix('1.2.3.4').is_adjacent(Prefix('1.2.3.5')))
Ejemplo n.º 10
0
 def is_link_local(self):
     return self in Prefix('fe80::/10')
Ejemplo n.º 11
0
 def is_multicast(self):
     return self in Prefix('ff00::/8')
Ejemplo n.º 12
0
 def is_private(self):
     return self in Prefix('fc00::/7')
Ejemplo n.º 13
0
 def is_link_local(self):
     return self in Prefix('169.254.0.0/16')
Ejemplo n.º 14
0
 def is_loopback(self):
     return self in Prefix('127.0.0.0/8')
Ejemplo n.º 15
0
 def is_multicast(self):
     return self in Prefix('224.0.0.0/4')
Ejemplo n.º 16
0
 def is_private(self):
     return (self in Prefix('10.0.0.0/8') or self in Prefix('172.16.0.0/12')
             or self in Prefix('192.168.0.0/16'))