Merge "is_valid_ipv4: Enable strict check by default"
This commit is contained in:
commit
db58371f0e
@ -84,7 +84,7 @@ def parse_host_port(address, default_port=None):
|
||||
return (host, None if port is None else int(port))
|
||||
|
||||
|
||||
def is_valid_ipv4(address, strict=None):
|
||||
def is_valid_ipv4(address, strict=True):
|
||||
"""Verify that address represents a valid IPv4 address.
|
||||
|
||||
:param address: Value to verify
|
||||
@ -104,29 +104,12 @@ def is_valid_ipv4(address, strict=None):
|
||||
if not address:
|
||||
return False
|
||||
|
||||
if strict is not None:
|
||||
flag = INET_PTON if strict else INET_ATON
|
||||
try:
|
||||
return netaddr.valid_ipv4(address, flags=flag)
|
||||
except netaddr.AddrFormatError:
|
||||
return False
|
||||
|
||||
# non strict mode
|
||||
try:
|
||||
if netaddr.valid_ipv4(address, flags=INET_PTON):
|
||||
return True
|
||||
else:
|
||||
if netaddr.valid_ipv4(address, flags=INET_ATON):
|
||||
LOG.warning(
|
||||
'Converting in non strict mode is deprecated. '
|
||||
'You should pass strict=False if you want to '
|
||||
'preserve legacy behavior')
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
except netaddr.AddrFormatError:
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_ipv6(address):
|
||||
"""Verify that address represents a valid IPv6 address.
|
||||
@ -235,7 +218,7 @@ def get_ipv6_addr_by_EUI64(prefix, mac):
|
||||
msg = _("Prefix must be a string")
|
||||
raise TypeError(msg)
|
||||
# Check if the prefix is an IPv4 address
|
||||
if is_valid_ipv4(prefix):
|
||||
if is_valid_ipv4(prefix, False):
|
||||
msg = _("Unable to generate IP address by EUI64 for IPv4 prefix")
|
||||
raise ValueError(msg)
|
||||
try:
|
||||
@ -327,7 +310,7 @@ def is_valid_ip(address):
|
||||
|
||||
.. versionadded:: 1.1
|
||||
"""
|
||||
return is_valid_ipv4(address) or is_valid_ipv6(address)
|
||||
return is_valid_ipv4(address, False) or is_valid_ipv6(address)
|
||||
|
||||
|
||||
def is_valid_mac(address):
|
||||
|
@ -156,27 +156,19 @@ class NetworkUtilsTest(test_base.BaseTestCase):
|
||||
|
||||
@mock.patch.object(netutils, 'LOG', autospec=True)
|
||||
def test_is_valid_ipv4(self, mock_log):
|
||||
expected_log = 'Converting in non strict mode is deprecated. ' \
|
||||
'You should pass strict=False if you want to preserve ' \
|
||||
'legacy behavior'
|
||||
self.assertTrue(netutils.is_valid_ipv4('42.42.42.42'))
|
||||
|
||||
self.assertFalse(netutils.is_valid_ipv4('-1.11.11.11'))
|
||||
|
||||
self.assertFalse(netutils.is_valid_ipv4(''))
|
||||
|
||||
self.assertTrue(netutils.is_valid_ipv4('10'))
|
||||
mock_log.warning.assert_called_with(expected_log)
|
||||
mock_log.reset_mock()
|
||||
self.assertTrue(netutils.is_valid_ipv4('10.10'))
|
||||
mock_log.warning.assert_called_with(expected_log)
|
||||
mock_log.reset_mock()
|
||||
self.assertTrue(netutils.is_valid_ipv4('10.10.10'))
|
||||
mock_log.warning.assert_called_with(expected_log)
|
||||
mock_log.reset_mock()
|
||||
self.assertFalse(netutils.is_valid_ipv4('10'))
|
||||
self.assertFalse(netutils.is_valid_ipv4('10.10'))
|
||||
self.assertFalse(netutils.is_valid_ipv4('10.10.10'))
|
||||
self.assertTrue(netutils.is_valid_ipv4('10.10.10.10'))
|
||||
mock_log.warning.assert_not_called()
|
||||
mock_log.reset_mock()
|
||||
|
||||
self.assertFalse(
|
||||
netutils.is_valid_ipv4('10', strict=True)
|
||||
)
|
||||
|
@ -0,0 +1,6 @@
|
||||
---
|
||||
upgrade:
|
||||
- |
|
||||
The ``netutils.is_valid_ipv4`` method now enables the strict check by
|
||||
default. To preserve the previous behavior, set the ``strict`` argument to
|
||||
``False``.
|
Loading…
Reference in New Issue
Block a user