def test_duplicates(self): t1 = Zone("internet", "0.0.0.0/0") t2 = Zone("test1", "192.168.22.0/24") t3 = Zone("test2", "192.168.22.31/32") t4 = Zone("test3", "192.168.0.0/16") t5 = Zone("test4", "192.168.22.30/31") self.assertRaises(ZoneException, Zone, "test5", "192.168.22.0/24") self.assertRaises(ZoneException, Zone, "test6", "192.168.22.31") self.assertRaises(ZoneException, Zone, "test7", "192.168.22.31/32") self.assertRaises(ZoneException, Zone, "test8", "192.168.22.15/24")
def test_dynamic_zone_conflicts_with_static_zone(self): Zone('static_zone', addrs=[MyResolverCache.host_one_ipv4_addr, ]) zone = Zone('dynamic_only_zone_conflicts_with_static_zone_ipv4_address', hostnames=[MyResolverCache.host_one_hostname, ]) updated_zones = { 'dynamic_only_zone_conflicts_with_static_zone_ipv4_address' : set(self.__get_subnets_from_addresses([MyResolverCache.host_one_ipv6_addr])), } self.__check_zone_update_messages(MyResolverCache.host_one_hostname, updated_zones)
def create_zone_update_messages(self, expired_hostname): updatable_zone = Zone.lookupByHostname(expired_hostname) if updatable_zone is None: return [] delete_zone_messages = [ KZorpDeleteZoneMessage(updatable_zone.name), ] add_zone_subnet_messages_for_static_addresses = self._create_add_zone_subnet_messages_from_zone( updatable_zone) add_zone_subnet_messages_for_dynamic_addresses = self._create_add_zone_subnet_messages_of_hostnames( updatable_zone) hostname_address_to_zone_map = self._create_hostname_address_to_zone_map( updatable_zone) conflicting_zone_addresses_map = self._create_conflicting_zone_to_address_map( add_zone_subnet_messages_for_dynamic_addresses, hostname_address_to_zone_map, updatable_zone) add_zone_subnet_messages_for_dynamic_addresses = filter( lambda msg: socket.inet_ntop( msg.family, msg.address) not in conflicting_zone_addresses_map, add_zone_subnet_messages_for_dynamic_addresses) for (conflicting_zone, conflicting_addresses ) in conflicting_zone_addresses_map.iteritems(): delete_zone_messages += [ KZorpDeleteZoneMessage(conflicting_zone.name), ] add_zone_subnet_messages_for_static_addresses += self._create_add_zone_subnet_messages_from_zone( conflicting_zone) add_zone_subnet_messages = self._create_add_zone_subnet_messages_of_hostnames( conflicting_zone) add_zone_subnet_messages_for_dynamic_addresses = \ filter(lambda msg: socket.inet_ntop(msg.family, msg.address) not in conflicting_addresses, add_zone_subnet_messages_for_dynamic_addresses) + \ filter(lambda msg: socket.inet_ntop(msg.family, msg.address) not in conflicting_addresses, add_zone_subnet_messages) zone_names = set([msg.name for msg in delete_zone_messages]) add_zone_messages = [] for zone_name in zone_names: subnet_num = len(filter(lambda msg: msg.zone_name == zone_name, add_zone_subnet_messages_for_static_addresses + \ add_zone_subnet_messages_for_dynamic_addresses)) zone = Zone.lookupByName(zone_name) parent_name = zone.admin_parent.name if zone.admin_parent is not None else None add_zone_messages += [ KZorpAddZoneMessage(zone.name, parent_name, subnet_num), ] return delete_zone_messages + add_zone_messages + \ add_zone_subnet_messages_for_static_addresses + \ add_zone_subnet_messages_for_dynamic_addresses
def test_lookup(self): t1 = Zone("test1", "192.168.0.0/24") t2 = Zone("test2", "192.168.0.32/27") t3 = Zone("test3", "192.168.0.0/26") t4 = Zone("test4", "192.168.0.64/27") t5 = Zone("test5", "192.168.0.96/27") t6 = Zone("test6", "192.168.0.0/25") t7 = Zone("test7", "192.168.0.0/16") t8 = Zone("test8", "192.168.1.1/32") t9 = Zone("test9", "192.168.1.2/32") t10 = Zone("test10", "192.168.1.3/32") t11 = Zone("test11", "192.168.1.4/32") t12 = Zone("test12", "192.168.1.5/32") t13 = Zone("test13", "192.168.1.6/32") t14 = Zone("test14", "192.168.0.184") t15 = Zone("test15", "dead:beef:baad:c0ff:ee00:1122:3344:5566/127") self.assertEqual(Zone.lookup(SockAddrInet('192.168.0.1', 10)), t3) self.assertEqual(self.doLookup('192.168.0.1'), t3) self.assertEqual(self.doLookup('192.168.0.33'), t2) self.assertEqual(self.doLookup('192.168.0.65'), t4) self.assertEqual(self.doLookup('192.168.0.97'), t5) self.assertEqual(self.doLookup('192.168.0.129'), t1) self.assertEqual(self.doLookup('192.168.1.129'), t7) self.assertEqual(self.doLookup('192.168.0.184'), t14) self.assertEqual(self.doLookup('dead:beef:baad:c0ff:ee00:1122:3344:5567'), t15)
def test_mixed_zone_conflicts_with_dynamic_only_zone(self): BaseZone('dynamic_zone', addrs=[ MyResolverCache.non_conflicting_ipv4_addr, MyResolverCache.non_conflicting_ipv6_addr ], hostnames=[ MyResolverCache.host_one_hostname, ]) zone = Zone('conflicting_dynamic_zone', hostnames=[ MyResolverCache.conflicting_host_hostname, ]) updated_zones = { 'dynamic_zone': set( self.__get_subnets_from_addresses([ MyResolverCache.non_conflicting_ipv4_addr, MyResolverCache.non_conflicting_ipv6_addr, ])), 'conflicting_dynamic_zone': set(), } self.__check_zone_update_messages( MyResolverCache.conflicting_host_hostname, updated_zones)
def test_dynamic_conflicts_in_same_zone(self): zone = Zone('self_conflicting_dynamic_zone', hostnames=[ MyResolverCache.host_one_hostname, MyResolverCache.conflicting_host_hostname, ]) updated_zones = { 'self_conflicting_dynamic_zone': set(), } self.__check_zone_update_messages(MyResolverCache.conflicting_host_hostname, updated_zones)
def doLookup(self, address): zone = Zone.lookup(Subnet.create(address)) if zone is None: raise ZoneException(address) return zone
def test_lookup(self): t1 = Zone("test1", "192.168.0.0/24") t2 = Zone("test2", "192.168.0.32/27") t3 = Zone("test3", "192.168.0.0/26") t4 = Zone("test4", "192.168.0.64/27") t5 = Zone("test5", "192.168.0.96/27") t6 = Zone("test6", "192.168.0.0/25") t7 = Zone("test7", "192.168.0.0/16") t8 = Zone("test8", "192.168.1.1/32") t9 = Zone("test9", "192.168.1.2/32") t10 = Zone("test10", "192.168.1.3/32") t11 = Zone("test11", "192.168.1.4/32") t12 = Zone("test12", "192.168.1.5/32") t13 = Zone("test13", "192.168.1.6/32") t14 = Zone("test14", "192.168.0.184") t15 = Zone("test15", "dead:beef:baad:c0ff:ee00:1122:3344:5566/127") self.assertEqual(Zone.lookup(SockAddrInet('192.168.0.1', 10)), t3) self.assertEqual(self.doLookup('192.168.0.1'), t3) self.assertEqual(self.doLookup('192.168.0.33'), t2) self.assertEqual(self.doLookup('192.168.0.65'), t4) self.assertEqual(self.doLookup('192.168.0.97'), t5) self.assertEqual(self.doLookup('192.168.0.129'), t1) self.assertEqual(self.doLookup('192.168.1.129'), t7) self.assertEqual(self.doLookup('192.168.0.184'), t14) self.assertEqual( self.doLookup('dead:beef:baad:c0ff:ee00:1122:3344:5567'), t15)
def checkZone(self, zone_name, expected): self.session.server_zone = Zone.lookup_by_name(zone_name) self.assertEqual(expected, self.session.isServerPermitted())
def has_zone_with_static_address(address): subnet = Subnet.create(address) zone = Zone.lookupByStaticAddressExactly(subnet) return zone is not None
def init(names, virtual_name, is_master): try: t1 = Zone("test1", "192.168.0.0/24", inbound_services=["s1"], outbound_services=["s2"]) t2 = Zone("test2", "192.168.0.32/27") t3 = Zone("test3", "192.168.0.0/26") t4 = Zone("test4", "192.168.0.64/27") t5 = Zone("test5", "192.168.0.96/27") t6 = Zone("test6", "192.168.0.0/25") t7 = Zone("test7", "192.168.0.0/16") t8 = Zone("test8", "192.168.1.1/32", admin_parent="test1") t9 = Zone("test9", "192.168.1.2/32", admin_parent="test8") t10 = Zone("test10", "192.168.1.3/32", admin_parent="test9", umbrella=1) t11 = Zone("test11", "192.168.1.4/32", admin_parent="test9") t12 = Zone("test12", "192.168.1.5/32", inbound_services=['*']) t13 = Zone("test13", "192.168.1.6/32", outbound_services=['*']) t14 = Zone("test14", "192.168.0.184", outbound_services=['*']) t15 = Zone("test15", "dead:beef:baad:c0ff:ee00:1122:3344:5566/127", outbound_services=['*']) test('192.168.0.1', Zone.lookup(SockAddrInet('192.168.0.1', 10)), t3) test('192.168.0.33', Zone.lookup(SockAddrInet('192.168.0.33', 10)), t2) test('192.168.0.65', Zone.lookup(SockAddrInet('192.168.0.65', 10)), t4) test('192.168.0.97', Zone.lookup(SockAddrInet('192.168.0.97', 10)), t5) test('192.168.0.129', Zone.lookup(SockAddrInet('192.168.0.129', 10)), t1) test('192.168.1.129', Zone.lookup(SockAddrInet('192.168.1.129', 10)), t7) test('192.168.0.184', Zone.lookup(SockAddrInet('192.168.0.184', 10)), t14) test('dead:beef:baad:c0ff:ee00:1122:3344:5566', Zone.lookup(SockAddrInet6('dead:beef:baad:c0ff:ee00:1122:3344:5566', 10)), t15) test('dead:beef:baad:c0ff:ee00:1122:3344:5566', Zone.lookup(SockAddrInet6('dead:beef:baad:c0ff:ee00:1122:3344:5567', 10)), t15) inet = Zone("internet", "0.0.0.0/0", inbound_services=["s2"], outbound_services=["s1"]) test('1.1.1.1', Zone.lookup(SockAddrInet('1.1.1.1', 10)), inet) s = MasterSession() s.setService(Service("s1", None)) s.setServer(SockAddrInet('192.168.1.2', 9999)) test('service s1#1', t1.isInboundServicePermitted(s.service), ZV_ACCEPT) test('service s1#2', t1.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s1#3', inet.isInboundServicePermitted(s.service), ZV_REJECT) test('service s1#4', inet.isOutboundServicePermitted(s.service), ZV_ACCEPT) ### test('service s1#5', t10.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s1#6', t10.isInboundServicePermitted(s.service), ZV_REJECT) test('service s1#7', t11.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s1#8', t11.isInboundServicePermitted(s.service), ZV_ACCEPT) test('service s1#9', t12.isInboundServicePermitted(s.service), ZV_ACCEPT) test('service s1#10', t12.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s1#11', t13.isOutboundServicePermitted(s.service), ZV_ACCEPT) test('service s1#12', t13.isInboundServicePermitted(s.service), ZV_REJECT) s.service = Service("s2", None) test('service s2#1', t1.isInboundServicePermitted(s.service), ZV_REJECT) test('service s2#2', t1.isOutboundServicePermitted(s.service), ZV_ACCEPT) test('service s2#3', inet.isInboundServicePermitted(s.service), ZV_ACCEPT) test('service s2#4', inet.isOutboundServicePermitted(s.service), ZV_REJECT) ### test('service s2#5', t10.isInboundServicePermitted(s.service), ZV_REJECT) test('service s2#6', t10.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s2#7', t11.isOutboundServicePermitted(s.service), ZV_ACCEPT) test('service s2#8', t11.isInboundServicePermitted(s.service), ZV_REJECT) test('service s2#9', t12.isInboundServicePermitted(s.service), ZV_ACCEPT) test('service s2#10', t12.isOutboundServicePermitted(s.service), ZV_REJECT) test('service s2#11', t13.isOutboundServicePermitted(s.service), ZV_ACCEPT) test('service s2#12', t13.isInboundServicePermitted(s.service), ZV_REJECT) except Exception, e: print_exc() quit(1) return 1