Exemplo n.º 1
0
 def testAddressCleanupCorrect(self):
   unoptimized_addr = [nacaddr.IPv4('10.16.128.6/32', token='FOO'),
                       nacaddr.IPv4('10.16.128.7/32', token='BAR')]
   self.naming.GetNetAddr.return_value = unoptimized_addr
   pol = policy.ParsePolicy(HEADER + GOOD_TERM_2, self.naming)
   term = pol.filters[0][1][0]
   self.assertEqual(nacaddr.CollapseAddrList(unoptimized_addr),
                    term.source_address)
   pol = policy.ParsePolicy(HEADER_SRX + GOOD_TERM_2, self.naming)
   term = pol.filters[0][1][0]
   self.assertEqual(nacaddr.CollapseAddrListPreserveTokens(unoptimized_addr),
                    term.source_address)
Exemplo n.º 2
0
    def testCollapsing(self):
        ip1 = nacaddr.IPv4('1.1.0.0/24', 'foo')
        ip2 = nacaddr.IPv4('1.1.1.0/24', 'foo')
        ip3 = nacaddr.IPv4('1.1.2.0/24', 'baz')
        ip4 = nacaddr.IPv4('1.1.3.0/24')
        ip5 = nacaddr.IPv4('1.1.4.0/24')

        # stored in no particular order b/c we want CollapseAddr to call [].sort
        # and we want that sort to call nacaddr.IP.__cmp__() on our array members
        ip6 = nacaddr.IPv4('1.1.0.0/22')

        # check that addreses are subsumed properlly.
        collapsed = nacaddr.CollapseAddrList([ip1, ip2, ip3, ip4, ip5, ip6])
        self.assertEqual(len(collapsed), 2)
        # test that the comments are collapsed properlly, and that comments aren't
        # added to addresses that have no comments.
        self.assertListEqual([collapsed[0].text, collapsed[1].text],
                             ['foo, baz', ''])
        self.assertListEqual(
            collapsed,
            [nacaddr.IPv4('1.1.0.0/22'),
             nacaddr.IPv4('1.1.4.0/24')])

        # test that two addresses are supernet'ed properlly
        collapsed = nacaddr.CollapseAddrList([ip1, ip2])
        self.assertEqual(len(collapsed), 1)
        self.assertEqual(collapsed[0].text, 'foo')
        self.assertListEqual(collapsed, [nacaddr.IPv4('1.1.0.0/23')])

        ip_same1 = ip_same2 = nacaddr.IPv4('1.1.1.1/32')
        self.assertListEqual(nacaddr.CollapseAddrList([ip_same1, ip_same2]),
                             [ip_same1])
        ip1 = nacaddr.IPv6('::2001:1/100')
        ip2 = nacaddr.IPv6('::2002:1/120')
        ip3 = nacaddr.IPv6('::2001:1/96')
        # test that ipv6 addresses are subsumed properlly.
        collapsed = nacaddr.CollapseAddrList([ip1, ip2, ip3])
        self.assertListEqual(collapsed, [ip3])
Exemplo n.º 3
0
 def testOptimizedConsistency(self):
   pol = HEADER + GOOD_TERM_2 + GOOD_TERM_3
   unoptimized_addr = [nacaddr.IPv4('10.16.128.6/32'),
                       nacaddr.IPv4('10.16.128.7/32')]
   optimized_addr = nacaddr.CollapseAddrList(unoptimized_addr)
   self.naming.GetNetAddr.return_value = unoptimized_addr
   self.naming.GetServiceByProto.return_value = ['25']
   ret_unoptimized = policy.ParsePolicy(pol, self.naming, optimize=False)
   self.assertFalse(policy._OPTIMIZE)
   ret_optimized = policy.ParsePolicy(pol, self.naming)
   self.assertTrue(policy._OPTIMIZE)
   for _, terms in ret_unoptimized.filters:
     for term in terms:
       self.assertEqual(unoptimized_addr, term.source_address)
   for _, terms in ret_optimized.filters:
     for term in terms:
       self.assertEqual(optimized_addr, term.source_address)