Commit 7df2c4f0 authored by Andrea Spadaccini's avatar Andrea Spadaccini
Browse files

Add ValidateNetmask and GetClass IPAddress methods



Add the following methods to netutils.IPAddress:
* ValidateNetmask
* GetClassFromIpVersion
* GetClassFromIpFamily

Also, add related tests to the test suite.
Signed-off-by: default avatarAndrea Spadaccini <spadaccio@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent ea9c753d
......@@ -361,6 +361,20 @@ class IPAddress(object):
except socket.error:
return False
@classmethod
def ValidateNetmask(cls, netmask):
"""Validate a netmask suffix in CIDR notation.
@type netmask: int
@param netmask: netmask suffix to validate
@rtype: bool
@return: True if valid, False otherwise
"""
assert (isinstance(netmask, (int, long)))
return 0 < netmask <= cls.iplen
@classmethod
def Own(cls, address):
"""Check if the current host has the the given IP address.
......@@ -487,6 +501,36 @@ class IPAddress(object):
raise errors.ProgrammerError("%s is not a valid IP version" % version)
@staticmethod
def GetClassFromIpVersion(version):
"""Return the IPAddress subclass for the given IP version.
@type version: int
@param version: IP version, one of L{constants.IP4_VERSION} or
L{constants.IP6_VERSION}
@return: a subclass of L{netutils.IPAddress}
@raise errors.ProgrammerError: for unknowo IP versions
"""
if version == constants.IP4_VERSION:
return IP4Address
elif version == constants.IP6_VERSION:
return IP6Address
raise errors.ProgrammerError("%s is not a valid IP version" % version)
@staticmethod
def GetClassFromIpFamily(family):
"""Return the IPAddress subclass for the given IP family.
@param family: IP family (one of C{socket.AF_INET} or C{socket.AF_INET6}
@return: a subclass of L{netutils.IPAddress}
@raise errors.ProgrammerError: for unknowo IP versions
"""
return IPAddress.GetClassFromIpVersion(
IPAddress.GetVersionFromAddressFamily(family))
@classmethod
def IsLoopback(cls, address):
"""Determine whether it is a loopback address.
......
......@@ -161,6 +161,33 @@ class TestIPAddress(unittest.TestCase):
self.assertEqual(fn("2001:db8::1"), socket.AF_INET6)
self.assertRaises(errors.IPAddressError, fn, "0")
def testValidateNetmask(self):
for netmask in [0, 33]:
self.assertFalse(netutils.IP4Address.ValidateNetmask(netmask))
for netmask in [1, 32]:
self.assertTrue(netutils.IP4Address.ValidateNetmask(netmask))
for netmask in [0, 129]:
self.assertFalse(netutils.IP6Address.ValidateNetmask(netmask))
for netmask in [1, 128]:
self.assertTrue(netutils.IP6Address.ValidateNetmask(netmask))
def testGetClassFromX(self):
self.assert_(
netutils.IPAddress.GetClassFromIpVersion(constants.IP4_VERSION) ==
netutils.IP4Address)
self.assert_(
netutils.IPAddress.GetClassFromIpVersion(constants.IP6_VERSION) ==
netutils.IP6Address)
self.assert_(
netutils.IPAddress.GetClassFromIpFamily(socket.AF_INET) ==
netutils.IP4Address)
self.assert_(
netutils.IPAddress.GetClassFromIpFamily(socket.AF_INET6) ==
netutils.IP6Address)
def testOwnLoopback(self):
# FIXME: In a pure IPv6 environment this is no longer true
self.assert_(netutils.IPAddress.Own("127.0.0.1"),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment