diff --git a/oslo_utils/netutils.py b/oslo_utils/netutils.py index 28888233..9e808b62 100644 --- a/oslo_utils/netutils.py +++ b/oslo_utils/netutils.py @@ -107,8 +107,17 @@ def is_valid_ipv6(address): .. versionadded:: 1.1 """ + if not address: + return False + + parts = address.rsplit("%", 1) + address = parts[0] + scope = parts[1] if len(parts) > 1 else None + if scope is not None and (len(scope) < 1 or len(scope) > 15): + return False + try: - return netaddr.valid_ipv6(address) + return netaddr.valid_ipv6(address, netaddr.core.INET_PTON) except netaddr.AddrFormatError: return False diff --git a/oslo_utils/tests/test_netutils.py b/oslo_utils/tests/test_netutils.py index 48ca163a..bba7c546 100644 --- a/oslo_utils/tests/test_netutils.py +++ b/oslo_utils/tests/test_netutils.py @@ -163,6 +163,10 @@ class NetworkUtilsTest(test_base.BaseTestCase): def test_is_valid_ipv6(self): self.assertTrue(netutils.is_valid_ipv6('::1')) + self.assertTrue(netutils.is_valid_ipv6('fe80::1%eth0')) + + self.assertFalse(netutils.is_valid_ip('fe%80::1%eth0')) + self.assertFalse(netutils.is_valid_ipv6( '1fff::a88:85a3::172.31.128.1')) @@ -173,12 +177,16 @@ class NetworkUtilsTest(test_base.BaseTestCase): self.assertTrue(netutils.is_valid_ip('2001:db8::ff00:42:8329')) + self.assertTrue(netutils.is_valid_ip('fe80::1%eth0')) + self.assertFalse(netutils.is_valid_ip('256.0.0.0')) self.assertFalse(netutils.is_valid_ip('::1.2.3.')) self.assertFalse(netutils.is_valid_ip('')) + self.assertFalse(netutils.is_valid_ip(None)) + def test_is_valid_mac(self): self.assertTrue(netutils.is_valid_mac("52:54:00:cf:2d:31")) self.assertTrue(netutils.is_valid_mac(u"52:54:00:cf:2d:31"))