diff options
author | pmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035> | 2009-09-03 05:17:56 +0000 |
---|---|---|
committer | pmoody@google.com <pmoody@google.com@09200d28-7f98-11dd-ad27-0f66e57d2035> | 2009-09-03 05:17:56 +0000 |
commit | 7b64be37bc31acc6e0e781d5f14b0361e65cdc7f (patch) | |
tree | faeb57ec61700c8ed898ad2e7ffc085766f21c73 /branches | |
parent | d6cb9b50c71ebb878e99526d12108f90e094b797 (diff) | |
download | ipaddr-py-7b64be37bc31acc6e0e781d5f14b0361e65cdc7f.tar.gz |
fix for issue31
git-svn-id: https://ipaddr-py.googlecode.com/svn@103 09200d28-7f98-11dd-ad27-0f66e57d2035
Diffstat (limited to 'branches')
-rw-r--r-- | branches/2.0.x/ipaddr.py | 222 | ||||
-rwxr-xr-x | branches/2.0.x/ipaddr_test.py | 108 |
2 files changed, 192 insertions, 138 deletions
diff --git a/branches/2.0.x/ipaddr.py b/branches/2.0.x/ipaddr.py index 79fc76c..dbd40a6 100644 --- a/branches/2.0.x/ipaddr.py +++ b/branches/2.0.x/ipaddr.py @@ -22,7 +22,7 @@ and networks. """ -__version__ = 'trunk' +__version__ = '2.0.0' import struct @@ -1067,6 +1067,49 @@ class BaseV4(object): def version(self): return self._version + @property + def is_private(self): + """Test if this address is allocated for private networks. + + Returns: + A boolean, True if the address is reserved per RFC 1918. + + """ + return (self in IPv4Network('10.0.0.0/8') or + self in IPv4Network('172.16.0.0/12') or + self in IPv4Network('192.168.0.0/16')) + + @property + def is_multicast(self): + """Test if the address is reserved for multicast use. + + Returns: + A boolean, True if the address is multicast. + See RFC 3171 for details. + + """ + return self in IPv4Network('224.0.0.0/4') + + @property + def is_loopback(self): + """Test if the address is a loopback adddress. + + Returns: + A boolean, True if the address is a loopback per RFC 3330. + + """ + return self in IPv4Network('127.0.0.0/8') + + @property + def is_link_local(self): + """Test if the address is reserved for link-local. + + Returns: + A boolean, True if the address is link-local per RFC 3927. + + """ + return self in IPv4Network('169.254.0.0/16') + class IPv4Address(BaseV4, BaseIP): @@ -1220,49 +1263,6 @@ class IPv4Network(BaseV4, BaseNet): self.netmask = IPv4Address(self._ip_int_from_prefix( self._prefixlen)) - @property - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per RFC 1918. - - """ - return (self in IPv4Network('10.0.0.0/8') or - self in IPv4Network('172.16.0.0/12') or - self in IPv4Network('192.168.0.0/16')) - - @property - def is_multicast(self): - """Test if the address is reserved for multicast use. - - Returns: - A boolean, True if the address is multicast. - See RFC 3171 for details. - - """ - return self in IPv4Network('224.0.0.0/4') - - @property - def is_loopback(self): - """Test if the address is a loopback adddress. - - Returns: - A boolean, True if the address is a loopback per RFC 3330. - - """ - return self in IPv4Network('127.0.0.0/8') - - @property - def is_link_local(self): - """Test if the address is reserved for link-local. - - Returns: - A boolean, True if the address is link-local per RFC 3927. - - """ - return self in IPv4Network('169.254.0.0/16') - def _is_hostmask(self, ip_str): """Test if the IP string is a hostmask (rather than a netmask). @@ -1539,6 +1539,73 @@ class BaseV6(object): def version(self): return self._version + @property + def is_multicast(self): + """Test if the address is reserved for multicast use. + + Returns: + A boolean, True if the address is a multicast address. + See RFC 2373 2.7 for details. + + """ + return self in IPv6Network('ff00::/8') + + @property + def is_unspecified(self): + """Test if the address is unspecified. + + Returns: + A boolean, True if this is the unspecified address as defined in + RFC 2373 2.5.2. + + """ + return self == IPv6Network('::') + + @property + def is_loopback(self): + """Test if the address is a loopback adddress. + + Returns: + A boolean, True if the address is a loopback address as defined in + RFC 2373 2.5.3. + + """ + return self == IPv6Network('::1') + + @property + def is_link_local(self): + """Test if the address is reserved for link-local. + + Returns: + A boolean, True if the address is reserved per RFC 4291. + + """ + return self in IPv6Network('fe80::/10') + + @property + def is_site_local(self): + """Test if the address is reserved for site-local. + + Note that the site-local address space has been deprecated by RFC 3879. + Use is_private to test if this address is in the space of unique local + addresses as defined by RFC 4193. + + Returns: + A boolean, True if the address is reserved per RFC 3513 2.5.6. + + """ + return self in IPv6Network('fec0::/10') + + @property + def is_private(self): + """Test if this address is allocated for private networks. + + Returns: + A boolean, True if the address is reserved per RFC 4193. + + """ + return self in IPv6Network('fc00::/7') + class IPv6Address(BaseV6, BaseIP): @@ -1682,73 +1749,6 @@ class IPv6Network(BaseV6, BaseNet): self.ip = IPv6Address(self._ip) - @property - def is_multicast(self): - """Test if the address is reserved for multicast use. - - Returns: - A boolean, True if the address is a multicast address. - See RFC 2373 2.7 for details. - - """ - return self in IPv6Network('ff00::/8') - - @property - def is_unspecified(self): - """Test if the address is unspecified. - - Returns: - A boolean, True if this is the unspecified address as defined in - RFC 2373 2.5.2. - - """ - return self == IPv6Network('::') - - @property - def is_loopback(self): - """Test if the address is a loopback adddress. - - Returns: - A boolean, True if the address is a loopback address as defined in - RFC 2373 2.5.3. - - """ - return self == IPv6Network('::1') - - @property - def is_link_local(self): - """Test if the address is reserved for link-local. - - Returns: - A boolean, True if the address is reserved per RFC 4291. - - """ - return self in IPv6Network('fe80::/10') - - @property - def is_site_local(self): - """Test if the address is reserved for site-local. - - Note that the site-local address space has been deprecated by RFC 3879. - Use is_private to test if this address is in the space of unique local - addresses as defined by RFC 4193. - - Returns: - A boolean, True if the address is reserved per RFC 3513 2.5.6. - - """ - return self in IPv6Network('fec0::/10') - - @property - def is_private(self): - """Test if this address is allocated for private networks. - - Returns: - A boolean, True if the address is reserved per RFC 4193. - - """ - return self in IPv6Network('fc00::/7') - def _is_valid_netmask(self, prefixlen): """Verify that the netmask/prefixlen is valid. diff --git a/branches/2.0.x/ipaddr_test.py b/branches/2.0.x/ipaddr_test.py index 6925ec3..b72466b 100755 --- a/branches/2.0.x/ipaddr_test.py +++ b/branches/2.0.x/ipaddr_test.py @@ -606,6 +606,7 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEquals(ipaddr.IPv6Address, type(ipv6addr)) def testReservedIpv4(self): + # test networks self.assertEquals(True, ipaddr.IPNetwork('224.1.1.1/31').is_multicast) self.assertEquals(False, ipaddr.IPNetwork('240.0.0.0').is_multicast) @@ -626,38 +627,91 @@ class IpaddrUnitTest(unittest.TestCase): self.assertEquals(True, ipaddr.IPNetwork('127.42.0.0/16').is_loopback) self.assertEquals(False, ipaddr.IPNetwork('128.0.0.0').is_loopback) - def testReservedIpv6(self): - ip = ipaddr.IPNetwork - - self.assertEquals(True, ip('ffff::').is_multicast) - self.assertEquals(True, ip(2**128-1).is_multicast) - self.assertEquals(True, ip('ff00::').is_multicast) - self.assertEquals(False, ip('fdff::').is_multicast) + # test addresses + self.assertEquals(True, ipaddr.IPAddress('224.1.1.1').is_multicast) + self.assertEquals(False, ipaddr.IPAddress('240.0.0.0').is_multicast) - self.assertEquals(True, ip('fecf::').is_site_local) - self.assertEquals(True, ip('feff:ffff:ffff:ffff::').is_site_local) - self.assertEquals(False, ip('fbf:ffff::').is_site_local) - self.assertEquals(False, ip('ff00::').is_site_local) + self.assertEquals(True, ipaddr.IPAddress('192.168.1.1').is_private) + self.assertEquals(False, ipaddr.IPAddress('192.169.0.0').is_private) + self.assertEquals(True, ipaddr.IPAddress('10.255.255.255').is_private) + self.assertEquals(False, ipaddr.IPAddress('11.0.0.0').is_private) + self.assertEquals(True, ipaddr.IPAddress('172.31.255.255').is_private) + self.assertEquals(False, ipaddr.IPAddress('172.32.0.0').is_private) - self.assertEquals(True, ip('fc00::').is_private) - self.assertEquals(True, ip('fc00:ffff:ffff:ffff::').is_private) - self.assertEquals(False, ip('fbff:ffff::').is_private) - self.assertEquals(False, ip('fe00::').is_private) - - self.assertEquals(True, ip('fea0::').is_link_local) - self.assertEquals(True, ip('febf:ffff::').is_link_local) - self.assertEquals(False, ip('fe7f:ffff::').is_link_local) - self.assertEquals(False, ip('fec0::').is_link_local) + self.assertEquals(True, + ipaddr.IPAddress('169.254.100.200').is_link_local) + self.assertEquals(False, + ipaddr.IPAddress('169.255.100.200').is_link_local) - self.assertEquals(True, ip('0:0::0:01').is_loopback) - self.assertEquals(False, ip('::1/127').is_loopback) - self.assertEquals(False, ip('::').is_loopback) - self.assertEquals(False, ip('::2').is_loopback) + self.assertEquals(True, + ipaddr.IPAddress('127.100.200.254').is_loopback) + self.assertEquals(True, ipaddr.IPAddress('127.42.0.0').is_loopback) + self.assertEquals(False, ipaddr.IPAddress('128.0.0.0').is_loopback) - self.assertEquals(True, ip('0::0').is_unspecified) - self.assertEquals(False, ip('::1').is_unspecified) - self.assertEquals(False, ip('::/127').is_unspecified) + + def testReservedIpv6(self): + self.assertEquals(True, ipaddr.IPNetwork('ffff::').is_multicast) + self.assertEquals(True, ipaddr.IPNetwork(2**128-1).is_multicast) + self.assertEquals(True, ipaddr.IPNetwork('ff00::').is_multicast) + self.assertEquals(False, ipaddr.IPNetwork('fdff::').is_multicast) + + self.assertEquals(True, ipaddr.IPNetwork('fecf::').is_site_local) + self.assertEquals(True, ipaddr.IPNetwork( + 'feff:ffff:ffff:ffff::').is_site_local) + self.assertEquals(False, ipaddr.IPNetwork('fbf:ffff::').is_site_local) + self.assertEquals(False, ipaddr.IPNetwork('ff00::').is_site_local) + + self.assertEquals(True, ipaddr.IPNetwork('fc00::').is_private) + self.assertEquals(True, ipaddr.IPNetwork( + 'fc00:ffff:ffff:ffff::').is_private) + self.assertEquals(False, ipaddr.IPNetwork('fbff:ffff::').is_private) + self.assertEquals(False, ipaddr.IPNetwork('fe00::').is_private) + + self.assertEquals(True, ipaddr.IPNetwork('fea0::').is_link_local) + self.assertEquals(True, ipaddr.IPNetwork('febf:ffff::').is_link_local) + self.assertEquals(False, ipaddr.IPNetwork('fe7f:ffff::').is_link_local) + self.assertEquals(False, ipaddr.IPNetwork('fec0::').is_link_local) + + self.assertEquals(True, ipaddr.IPNetwork('0:0::0:01').is_loopback) + self.assertEquals(False, ipaddr.IPNetwork('::1/127').is_loopback) + self.assertEquals(False, ipaddr.IPNetwork('::').is_loopback) + self.assertEquals(False, ipaddr.IPNetwork('::2').is_loopback) + + self.assertEquals(True, ipaddr.IPNetwork('0::0').is_unspecified) + self.assertEquals(False, ipaddr.IPNetwork('::1').is_unspecified) + self.assertEquals(False, ipaddr.IPNetwork('::/127').is_unspecified) + + # test addresses + self.assertEquals(True, ipaddr.IPAddress('ffff::').is_multicast) + self.assertEquals(True, ipaddr.IPAddress(2**128-1).is_multicast) + self.assertEquals(True, ipaddr.IPAddress('ff00::').is_multicast) + self.assertEquals(False, ipaddr.IPAddress('fdff::').is_multicast) + + self.assertEquals(True, ipaddr.IPAddress('fecf::').is_site_local) + self.assertEquals(True, ipaddr.IPAddress( + 'feff:ffff:ffff:ffff::').is_site_local) + self.assertEquals(False, ipaddr.IPAddress('fbf:ffff::').is_site_local) + self.assertEquals(False, ipaddr.IPAddress('ff00::').is_site_local) + + self.assertEquals(True, ipaddr.IPAddress('fc00::').is_private) + self.assertEquals(True, ipaddr.IPAddress( + 'fc00:ffff:ffff:ffff::').is_private) + self.assertEquals(False, ipaddr.IPAddress('fbff:ffff::').is_private) + self.assertEquals(False, ipaddr.IPAddress('fe00::').is_private) + + self.assertEquals(True, ipaddr.IPAddress('fea0::').is_link_local) + self.assertEquals(True, ipaddr.IPAddress('febf:ffff::').is_link_local) + self.assertEquals(False, ipaddr.IPAddress('fe7f:ffff::').is_link_local) + self.assertEquals(False, ipaddr.IPAddress('fec0::').is_link_local) + + self.assertEquals(True, ipaddr.IPAddress('0:0::0:01').is_loopback) + self.assertEquals(True, ipaddr.IPAddress('::1').is_loopback) + self.assertEquals(False, ipaddr.IPAddress('::2').is_loopback) + + self.assertEquals(True, ipaddr.IPAddress('0::0').is_unspecified) + self.assertEquals(False, ipaddr.IPAddress('::1').is_unspecified) + def testAddrExclude(self): addr1 = ipaddr.IPNetwork('10.1.1.0/24') addr2 = ipaddr.IPNetwork('10.1.1.0/26') |