From d1b783fd673448381445dce82e9160ba2f715a7c Mon Sep 17 00:00:00 2001 From: zhhuabj Date: Thu, 17 Oct 2013 22:58:48 +0800 Subject: [PATCH] Support uncompressed ipv6 address and abbreviated ipv4 address Now neutron supports zero-compressed ipv6 address like '2001:db8::10:10:10:0/120' and unabbreviated ipv4 address like '10.0.0.0/24'. This path will make neutron also support uncompressed ipv6 address and abbreviated ipv4 address. for example: 1, uncompressed ipv6 address like 'fe80:0:0:0:0:0:0:0/128' 2, ipv6 address with multiple consecutive zero like '2001:0db8:0:0:1::1/128' or '2001::0:1:0:0:1100/120' 3, abbreviated ipv4 address like '10/24' Change-Id: I755e38ea11c139e15488ec60e7d5682a244ab114 Closes-Bug: #1240790 --- neutron/api/v2/attributes.py | 5 ++-- neutron/tests/unit/test_attributes.py | 30 +++++++++++++++++++ neutron/tests/unit/test_extension_firewall.py | 5 +--- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/neutron/api/v2/attributes.py b/neutron/api/v2/attributes.py index d6842f21cbe..512255f0ca7 100644 --- a/neutron/api/v2/attributes.py +++ b/neutron/api/v2/attributes.py @@ -267,11 +267,10 @@ def _validate_subnet(data, valid_values=None): msg = None try: net = netaddr.IPNetwork(_validate_no_whitespace(data)) - cidr = str(net.cidr) - if (cidr != data): + if ('/' not in data or net.network != net.ip): msg = _("'%(data)s' isn't a recognized IP subnet cidr," " '%(cidr)s' is recommended") % {"data": data, - "cidr": cidr} + "cidr": net.cidr} else: return except Exception: diff --git a/neutron/tests/unit/test_attributes.py b/neutron/tests/unit/test_attributes.py index a763b54d17c..800c1f18c9f 100644 --- a/neutron/tests/unit/test_attributes.py +++ b/neutron/tests/unit/test_attributes.py @@ -397,6 +397,36 @@ class TestAttributes(base.BaseTestCase): None) self.assertIsNone(msg) + # Valid - uncompressed ipv6 address + cidr = "fe80:0:0:0:0:0:0:0/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001:0db8:0:0:1::1/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001:0db8::1:0:0:1/128" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - ipv6 address with multiple consecutive zero + cidr = "2001::0:1:0:0:1100/120" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + + # Valid - abbreviated ipv4 address + cidr = "10/24" + msg = attributes._validate_subnet(cidr, + None) + self.assertIsNone(msg) + # Invalid - IPv4 missing mask cidr = "10.0.2.0" msg = attributes._validate_subnet(cidr, diff --git a/neutron/tests/unit/test_extension_firewall.py b/neutron/tests/unit/test_extension_firewall.py index 7550a706fe5..486f20f03b8 100644 --- a/neutron/tests/unit/test_extension_firewall.py +++ b/neutron/tests/unit/test_extension_firewall.py @@ -531,10 +531,7 @@ class TestFirewallAttributeValidators(base.BaseTestCase): # Valid - IPv6 with final octets cidr = "fe80::0/24" msg = firewall._validate_ip_or_subnet_or_none(cidr, None) - self.assertEqual(msg, ("'%s' is not a valid IP address and " - "'%s' isn't a recognized IP subnet cidr," - " 'fe80::/24' is recommended") % (cidr, - cidr)) + self.assertIsNone(msg) cidr = "fe80::" msg = firewall._validate_ip_or_subnet_or_none(cidr, None)