Added ICMP 'type' and 'code' checking capability to 'netutils' module

ICMP has 'type' and 'code' fields which are used while adding a security
group rule using Horizon. I have written functions in 'netutils' module
which validate ICMP 'type' and 'code' fields.

Change-Id: Ie1a67ea1afd69d5de4326f9e9a7243362b912582
Closes-Bug: #1511940
This commit is contained in:
Suraj Deshmukh 2015-10-31 18:37:45 +05:30
parent 7da2a5ba64
commit e77d38ca33
2 changed files with 80 additions and 0 deletions

View File

@ -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.

View File

@ -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):