diff --git a/oslo_utils/netutils.py b/oslo_utils/netutils.py index bdb4632..c2544f2 100644 --- a/oslo_utils/netutils.py +++ b/oslo_utils/netutils.py @@ -178,6 +178,48 @@ def is_valid_port(port): return (val > 0 and val <= 65535) +def _is_int_in_range(value, start, end): + """Try to convert value to int and check if it lies within + range 'start' to 'end'. + + :param value: value to verify + :param start: start number of range + :param end: end number of range + :returns: bool + """ + try: + val = int(value) + except (ValueError, TypeError): + return False + return (start <= val <= end) + + +def is_valid_icmp_type(type): + """Verify if ICMP type is valid. + + :param type: ICMP *type* field can only be a valid integer + :returns: bool + + ICMP *type* field can be valid integer having a value of 0 + up to and including 255. + """ + return _is_int_in_range(type, 0, 255) + + +def is_valid_icmp_code(code): + """Verify if ICMP code is valid. + + :param code: ICMP *code* field can be valid integer or None + :returns: bool + + ICMP *code* field can be either None or valid integer having + a value of 0 up to and including 255. + """ + if code is None: + return True + return _is_int_in_range(code, 0, 255) + + def get_my_ipv4(): """Returns the actual ipv4 of the local machine. diff --git a/oslo_utils/tests/test_netutils.py b/oslo_utils/tests/test_netutils.py index afec104..06a930d 100644 --- a/oslo_utils/tests/test_netutils.py +++ b/oslo_utils/tests/test_netutils.py @@ -199,6 +199,44 @@ class NetworkUtilsTest(test_base.BaseTestCase): addr = netutils.get_my_ipv4() self.assertEqual(addr, '1.2.3.4') + def test_is_int_in_range(self): + valid_inputs = [(1, -100, 100), + ('1', -100, 100), + (100, -100, 100), + ('100', -100, 100), + (-100, -100, 100), + ('-100', -100, 100)] + for input_value in valid_inputs: + self.assertTrue(netutils._is_int_in_range(*input_value)) + + def test_is_int_not_in_range(self): + invalid_inputs = [(None, 1, 100), + ('ten', 1, 100), + (-1, 0, 255), + ('None', 1, 100)] + for input_value in invalid_inputs: + self.assertFalse(netutils._is_int_in_range(*input_value)) + + def test_valid_icmp_type(self): + valid_inputs = [1, '1', 0, '0', 255, '255'] + for input_value in valid_inputs: + self.assertTrue(netutils.is_valid_icmp_type(input_value)) + + def test_invalid_icmp_type(self): + invalid_inputs = [-1, '-1', 256, '256', None, 'None', 'five'] + for input_value in invalid_inputs: + self.assertFalse(netutils.is_valid_icmp_type(input_value)) + + def test_valid_icmp_code(self): + valid_inputs = [1, '1', 0, '0', 255, '255', None] + for input_value in valid_inputs: + self.assertTrue(netutils.is_valid_icmp_code(input_value)) + + def test_invalid_icmp_code(self): + invalid_inputs = [-1, '-1', 256, '256', 'None', 'zero'] + for input_value in invalid_inputs: + self.assertFalse(netutils.is_valid_icmp_code(input_value)) + @mock.patch('socket.socket') @mock.patch('oslo_utils.netutils._get_my_ipv4_address') def test_get_my_ip_socket_error(self, ip, mock_socket):