Esempio n. 1
0
    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')])
Esempio n. 2
0
    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')])
Esempio n. 3
0
    def test_addition(self):
        range1 = Subnet.from_string('127.0.0.0/24')
        range2 = Subnet.from_string('127.0.1.0/24')
        range3 = Subnet.from_string('127.0.0.0/23')

        # Adjacent ranges
        self.assertEqual(range1 + range2, range3)
        self.assertEqual(range2 + range1, range3)
        self.assertIsInstance(range1 + range2, AddressRange)
        self.assertFalse(isinstance(range1 + range2, Subnet))
Esempio n. 4
0
    def test_addition(self):
        range1 = Subnet.from_string('127.0.0.0/24')
        range2 = Subnet.from_string('127.0.1.0/24')
        range3 = Subnet.from_string('127.0.0.0/23')

        # Adjacent ranges
        self.assertEqual(range1 + range2, range3)
        self.assertEqual(range2 + range1, range3)
        self.assertIsInstance(range1 + range2, AddressRange)
        self.assertFalse(isinstance(range1 + range2, Subnet))
Esempio n. 5
0
    def test_subnet(self):
        randomaddr4 = IPv4(random.randint(0, IPV4MAX))
        randomaddr6 = IPv6(random.randint(0, IPV6MAX))
        for cidr in range(128):
            if cidr <= 32:
                teststr4 = '{!s}/{:d}'.format(randomaddr4, cidr)
                subnet4 = Subnet(randomaddr4, cidr)
                self.assertIn(randomaddr4, subnet4)
                self.assertEqual(Subnet.from_string(teststr4), subnet4)
            teststr6 = '{!s}/{:d}'.format(randomaddr6, cidr)
            subnet6 = Subnet(randomaddr6, cidr)
            self.assertIn(randomaddr6, subnet6)
            self.assertEqual(Subnet.from_string(teststr6), subnet6)

        self.assertIn(Subnet(randomaddr4, 24), Subnet(randomaddr4, 16))
        self.assertIn(Subnet(randomaddr6, 96), Subnet(randomaddr6, 64))
Esempio n. 6
0
    def test_subnet(self):
        randomaddr4 = IPv4(random.randint(0, IPV4MAX))
        randomaddr6 = IPv6(random.randint(0, IPV6MAX))
        for cidr in range(128):
            if cidr <= 32:
                teststr4 = '{!s}/{:d}'.format(randomaddr4, cidr)
                subnet4 = Subnet(randomaddr4, cidr)
                self.assertIn(randomaddr4, subnet4)
                self.assertEqual(Subnet.from_string(teststr4), subnet4)
            teststr6 = '{!s}/{:d}'.format(randomaddr6, cidr)
            subnet6 = Subnet(randomaddr6, cidr)
            self.assertIn(randomaddr6, subnet6)
            self.assertEqual(Subnet.from_string(teststr6), subnet6)

        self.assertIn(Subnet(randomaddr4, 24), Subnet(randomaddr4, 16))
        self.assertIn(Subnet(randomaddr6, 96), Subnet(randomaddr6, 64))