diff options
-rw-r--r-- | nova/tests/test_utils.py | 5 | ||||
-rw-r--r-- | nova/utils.py | 17 |
2 files changed, 10 insertions, 12 deletions
diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py index 5c5c226f9..dea6f12d9 100644 --- a/nova/tests/test_utils.py +++ b/nova/tests/test_utils.py @@ -476,6 +476,11 @@ class GenericUtilsTestCase(test.TestCase): self.assertFalse(utils.is_valid_boolstr('maybe')) self.assertFalse(utils.is_valid_boolstr('only on tuesdays')) + def test_is_valid_ipv4(self): + self.assertTrue(utils.is_valid_ipv4('127.0.0.1')) + self.assertFalse(utils.is_valid_ipv4('::1')) + self.assertFalse(utils.is_valid_ipv4('bacon')) + class MonkeyPatchTestCase(test.TestCase): """Unit test for utils.monkey_patch().""" diff --git a/nova/utils.py b/nova/utils.py index e4d708f5c..2386e6aa8 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -881,19 +881,12 @@ def is_valid_boolstr(val): def is_valid_ipv4(address): - """valid the address strictly as per format xxx.xxx.xxx.xxx. - where xxx is a value between 0 and 255. - """ - parts = address.split(".") - if len(parts) != 4: + """Verify that address represents a valid IPv4 address.""" + try: + addr = netaddr.IPAddress(address) + return addr.version == 4 + except Exception: return False - for item in parts: - try: - if not 0 <= int(item) <= 255: - return False - except ValueError: - return False - return True def is_valid_cidr(address): |