def create_ip_address_pool(self): ip_address_list = [] if re.match("(?:[0-9]{1,3}\.){3}[0-9]{1,3}(-[0-9]{1,3})?", str(self.client_ip_range)): try: start_address = validators.ipv4( self.client_ip_range.split('-')[0]) end_address = self.client_ip_range.split('-')[-1] if re.match("[0-9]{1,3}", end_address) and len(end_address) < 4: end_address = start_address[:start_address.rfind('.') + 1] + end_address try: if end_address == start_address: ip_address_list.append(start_address) else: for ip_address in range( struct.unpack( '!I', socket.inet_pton(socket.AF_INET, start_address))[0], struct.unpack( '!I', socket.inet_pton(socket.AF_INET, end_address))[0]): ip_address_list.append( socket.inet_ntop(socket.AF_INET, struct.pack('!I', ip_address))) except AttributeError: for ip_address in range( struct.unpack('!I', socket.inet_aton(start_address))[0], struct.unpack('!I', socket.inet_aton(end_address))[0]): ip_address_list.append( socket.inet_ntop(socket.AF_INET, struct.pack('!I', ip_address))) for ip_address in ip_address_list: self.ip_address_pool[ip_address] = "" except validators.OptionValidationError: raise validators.OptionValidationError( "Option have to be valid IP address range.") except Exception: raise validators.OptionValidationError( "Option have to be valid IP address range.") else: raise validators.OptionValidationError( "Option have to be valid IP address range.")
def test_ipv4_no_inet_pton_invalid_address_1(self, mock_inet_pton): """ IP address with segment out of range. """ address = "127.256.0.1" mock_inet_pton.side_effect = AttributeError with self.assertRaises(OptionValidationError): validators.ipv4(address)
def test_ipv4_no_inet_pton_invalid_address_3(self, mock_inet_pton): """ IP address with extra segment """ address = "127.0.0.123.123" mock_inet_pton.side_effect = AttributeError with self.assertRaises(OptionValidationError): validators.ipv4(address)
def test_ipv4_no_inet_pton_valid_address(self, mock_inet_pton): address = "127.0.0.1" mock_inet_pton.side_effect = AttributeError self.assertEqual(validators.ipv4(address), "127.0.0.1")
def test_ipv4_invalid_address_3(self): """ IP address with extra segment """ address = "127.0.0.123.123" with self.assertRaises(OptionValidationError): validators.ipv4(address)
def test_ipv4_invalid_address_2(self): """ IP address with 4 digit segment. """ address = "127.0.0.1234" with self.assertRaises(OptionValidationError): validators.ipv4(address)
def test_ipv4_invalid_address_1(self): """ IP address with segment out of range. """ address = "127.256.0.1" with self.assertRaises(OptionValidationError): validators.ipv4(address)
def test_ipv4_valid_address(self): address = "127.0.0.1" self.assertEqual(validators.ipv4(address), address)