def test_disjointed_ipsets(): s1 = IPSet(['192.0.2.0', '192.0.2.1', '192.0.2.2']) s2 = IPSet(['192.0.2.2', '192.0.2.3', '192.0.2.4']) assert s1 & s2 == IPSet(['192.0.2.2/32']) assert not s1.isdisjoint(s2) s3 = IPSet(['192.0.2.0', '192.0.2.1']) s4 = IPSet(['192.0.2.3', '192.0.2.4']) assert s3 & s4 == IPSet([]) assert s3.isdisjoint(s4)
def _validate_addresses_helper(self, network, net): # Keep an IPSet of addresses/ranges that can be checked # against for overlaps current_set = IPSet() for address_list in net['addresses']: address = address_list.split('-') if len(address) == 1: try: ip_addr1 = IPAddress(address[0].strip()) except AddrFormatError: msg = ("Address %s in network %s is not a valid " "IP address." % (address[0], net['name'])) self.add_error(msg) self._valid = False else: if ip_addr1 not in network: msg = ("Address %s in network %s is not within " "the specified CIDR %s." % (address[0], net['name'], net['cidr'])) self.add_error(msg) self._valid = False elif ip_addr1 not in current_set: current_set.add(ip_addr1) else: try: ip_addr1 = IPAddress(address[0].strip()) ip_addr2 = IPAddress(address[1].strip()) except AddrFormatError: msg = ("The address range %s in network %s is not " "a range of valid IP addresses." % (address_list, net['name'])) self.add_error(msg) self._valid = False else: if ip_addr1 > ip_addr2: msg = ( "The address range %s specified in network %s " "is invalid. The specified first address %s " "is greater than the specified last address %s." % (address_list, net['name'], address[0], address[1])) self.add_error(msg) self._valid = False else: iprange = IPRange(ip_addr1, ip_addr2) if iprange not in network: msg = ( "Address range %s in network %s is not within " "the specified CIDR %s." % (address_list, net['name'], net['cidr'])) self.add_error(msg) self._valid = False else: if not current_set.isdisjoint(IPSet(iprange)): msg = ( "The address range %s in network %s overlaps " "with another range or address in the network." % (address_list, net['name'])) self.add_warning(msg) current_set.add(iprange) self._ipsets[net['name']] = current_set