Commits

buge committed 6ccd136

Initial checkin.

  • Participants

Comments (0)

Files changed (2)

+# Copyright (C) 2011  Philipp Bunge
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import operator
+
+
+__all__ = [
+    'Address',
+    'EUIAddress', 'EUI48Address', 'EUI64Address',
+    'IPAddress', 'IPv4Address', 'IPv6Address'
+]
+
+
+class Address(object):
+
+    @classmethod
+    def value_of(cls, value):
+        return None
+#       tokens = []
+#       ipv6 = False
+
+#       while i < len(value):
+#           if value[i:i+2] == "::":
+#               tokens += [("GLUE", 0), ("", 16)]
+#               ipv6 = True
+#               i += 2
+#           elif value[i] == ":":
+#               tokens[-1] = (tokens[-1][0], 16)
+#               tokens += [("", 16)]
+#               ipv6 = True
+#               i += 1
+#           elif value[i] == ".":
+#               tokens[-1] = (tokens[-1][0], 0)
+#               tokens += [("", 0)]
+#               i += 1
+#           else:
+#               tokens[-1] = (tokens[-1][0] + value[i], tokens[-1][1])
+#               i += 1
+
+#       address = 0
+#       for token in tokens:
+#           if token[1] == 16:
+#               address <<= 16
+#           else:
+#               address <<= 8
+#           if token[0]:
+#               address |= int(*token)
+
+#       if ipv6 or (address >> 128) != 0:
+#           return IPv6Address(address)
+#       return IPv4Address(address)
+
+
+    def __init__(self, address):
+        a = int(address)
+        bitsize = self.__class__.BIT_SIZE
+        if a < 0 or (a >> bitsize) != 0:
+            raise ValueError("address must be between 0 and 2**%d-1" % bitsize)
+        self.__address = a
+
+    def get_address(self):
+        return self.__address
+
+    def __has_prefix(self, prefix, length):
+        bitsize = self.__class__.BIT_SIZE
+        return prefix == self.get_address() >> (bitsize - length)
+
+    def __repr__(self):
+        return "%s(%r)" % (self.__class__.__name__, self.get_address())
+
+    def __str__(self):
+        raise NotImplementedError(
+            "subclasses must override __str__ to return a canonical " +
+            "representation of themselves.")
+
+    def __lshift__(self, other):
+        return self.__address_op(operator.lshift, other)
+
+    def __rshift__(self, other):
+        return self.__address_op(operator.rshift, other)
+
+    def __and__(self, other):
+        return self.__address_op(operator.and_, other)
+
+    def __xor__(self, other):
+        return self.__address_op(operator.xor, other)
+
+    def __or__(self, other):
+        return self.__address_op(operator.or_, other)
+
+    def __rand__(self, other):
+        return other & self.get_address()
+
+    def __rxor__(self, other):
+        return other ^ self.get_address()
+
+    def __ror__(self, other):
+        return other | self.get_address()
+
+    def __invert__(self):
+        c = 2 ** self.__class__.BIT_SIZE - 1
+        return self.__address_op(operator.xor, c)
+
+    def __address_op(self, f, other):
+        address = other
+        if isinstance(address, Address):
+            address = address.get_address()
+
+        max_address = 2 ** self.__class__.BIT_SIZE - 1
+        new_address = f(self.get_address(), address) & max_address
+
+        return self.__class__(new_address)
+
+    def __complex__(self):
+        return complex(self.get_address())
+
+    def __int__(self):
+        return int(self.get_address())
+
+    def __long__(self):
+        return long(self.get_address())
+
+    def __float__(self):
+        return long(self.get_address())
+
+    def __oct__(self):
+        # Deprecated with Python 3.0, use __index__ instead
+        return oct(self.get_address())
+
+    def __hex__(self):
+        # Deprecated with Python 3.0, use __index__ instead
+        return hex(self.get_address())
+
+    def __index__(self):
+        return int(self.get_address())
+
+
+class AddressGroup(object):
+
+    pass
+
+
+class _AddressFormatter(object):
+
+    def __init__(self, address_size, group_size, group_format, separator):
+        self.__address_size = address_size
+        self.__group_size = group_size
+        self.__group_format = group_format
+        self.__separator = separator
+
+    def get_address_size(self):
+        return self.__address_size
+
+    def get_group_size(self):
+        return self.__group_size
+
+    def get_group_format(self):
+        return self.__group_format
+
+    def get_separator(self):
+        return self.__separator
+
+    def format(self, address):
+        a = address
+        if isinstance(a, Address):
+            a = a.get_address()
+
+        address_size = self.get_address_size()
+        group_size = self.get_group_size()
+        group_format = self.get_group_format()
+        mask = 2 ** group_size - 1
+
+        fields = []
+        for i in xrange(0, address_size / group_size):
+            fields.insert(0, group_format % (a & mask))
+            a >>= group_size
+        return self.get_separator().join(fields)
+
+
+class EUIAddress(Address):
+
+    @classmethod
+    def get_hardware_address(cls):
+        from uuid import getnode
+        return EUI48Address(getnode())
+
+    def as_eui48(self):
+        return NotImplementedError("abstract method")
+
+    def as_eui64(self):
+        return NotImplementedError("abstract method")
+
+    def get_ipv6_link_local_address(self):
+        return IPv6Address.get_link_local_address(self)
+
+
+class EUI48Address(EUIAddress):
+
+    BIT_SIZE = 48
+
+    def as_eui48(self):
+        return self
+
+    def as_eui64(self):
+        # Conversion from MAC-48 is deprecated
+        # (see IEEE "Guidelines for use of a 64-bit Extended Unique Identifier")
+        return self.as_eui64_from_eui48()
+ 
+    def as_eui64_from_eui48(self):
+        a = self.get_address()
+        eui64_a = (a & 0xffffff << 24) << 16 | 0xfffe << 24 | (a & 0xffffff)
+        return EUI64Address(eui64_a)
+
+    def as_eui64_from_mac48(self):
+        a = self.get_address()
+        eui64_a = (a & 0xffffff << 24) << 16 | 0xffff << 24 | (a & 0xffffff)
+        return EUI64Address(eui64_a)
+
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = EUI48Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%02X",
+            separator = '-'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, EUI48Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+
+class EUI64Address(EUIAddress):
+
+    BIT_SIZE = 64
+
+    def as_eui48(self):
+        a = self.get_address()
+        if a >> 24 & 0xfffe != 0xfffe:
+            raise ValueError(
+                "%s is not an EUI-48 encapsulated EUI-64 value" % self)
+        eui48_a = a >> 16 & 0xffffff << 24 | a & 0xffffff
+        return EUI48Address(eui48_a)
+
+    def as_eui64(self):
+        return self
+ 
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = EUI64Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%02X",
+            separator = '-'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, EUI64Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+
+class IPAddress(Address):
+
+    pass
+
+
+class IPv4Address(IPAddress):
+
+    BIT_SIZE = 32
+
+    def __str__(self):
+        return _AddressFormatter(
+            address_size = IPv4Address.BIT_SIZE,
+            group_size = 8,
+            group_format = "%d",
+            separator = '.'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, IPv4Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+IPv4Address.UNSPECIFIED_ADDRESS = IPv4Address(0)
+IPv4Address.LOCALHOST_ADDRESS = IPv4Address(2130706433)
+
+
+class IPv6Address(IPAddress):
+
+    BIT_SIZE = 128
+
+    # Descriptions as by RFC 4291
+    TYPE_UNSPECIFIED = 'Unspecified'
+    TYPE_LOOPBACK = 'Loopback'
+    TYPE_MULTICAST = 'Multicast'
+    TYPE_LINK_LOCAL_UNICAST = 'Link-Local unicast'
+    TYPE_GLOBAL_UNICAST = 'Global Unicast'
+
+    @classmethod
+    def get_link_local_address(cls, interface_address=None):
+        eui = interface_address
+        if eui is None:
+            eui = EUIAddress.get_hardware_address()
+        elif not isinstance(eui, EUIAddress):
+            eui = EUI64Address(interface_address)
+        eui = eui.as_eui64()
+        return IPv6Address(0xfe80 << 112 | eui ^ 1 << 57)
+
+    def get_address_type(self):
+        """Returns the address type according to :rfc:`4291`.
+
+        The returned string corresponds to the `TYPE_*` constants defined in
+        this class.
+
+        """
+        if self.get_address() == 0:
+            return IPv6Address.TYPE_UNSPECIFIED
+        if self.get_address() == 1:
+            return IPv6Address.TYPE_LOOPBACK
+        if self.__has_prefix(255, 8):
+            return IPv6Address.TYPE_MULTICAST
+        if self.__has_prefix(1018, 10):
+            return IPv6Address.TYPE_LINK_LOCAL_UNICAST
+        return IPv6Address.TYPE_GLOBAL_UNICAST
+
+    def is_type_unspecified(self):
+        """Returns whether the address is of type "Unspecified",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_UNSPECIFIED
+
+    def is_type_loopback(self):
+        """Returns whether the address is of type "Loopback",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_LOOPBACK
+
+    def is_type_multicast(self):
+        """Returns whether the address is of type "Multicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_MULTICAST
+
+    def is_type_linklocal_unicast(self):
+        """Returns whether the address is of type "Link-Local Unicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_LINK_LOCAL_UNICAST
+
+    def is_type_global_unicast(self):
+        """Returns whether the address is of type "Global Unicast",
+        as defined by :rfc:`4291`.
+
+        """
+        return self.get_address_type() == IPv6Address.TYPE_GLOBAL_UNICAST
+
+    def __str__(self):
+        # FIXME implement canonical form correctly
+        return _AddressFormatter(
+            address_size = IPv6Address.BIT_SIZE,
+            group_size = 16,
+            group_format = "%x",
+            separator = ':'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, IPv6Address):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+IPv6Address.UNSPECIFIED_ADDRESS = IPv6Address(0)
+IPv6Address.LOOPBACK_ADDRESS = IPv6Address(1)
+
+
+class IPNetwork(AddressGroup):
+
+    def __init__(self, address, mask):
+        if not isinstance(address, IPAddress):
+            raise TypeError("FIXME") # FIXME
+        bitsize = address.__class__.BIT_SIZE
+        m = int(mask)
+        if m > bitsize:
+            raise ValueError("FIXME") # FIXME
+        a = address & (2 ** m - 1) << (bitsize - m)
+        self.__address = a
+        self.__mask = m
+
+    def get_address(self):
+        return self.__address
+
+    def get_mask(self):
+        return self.__mask
+
+

File tests/addrinfo_testcase.py

+#!/usr/bin/env python
+
+# Copyright (C) 2011  Philipp Bunge
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import unittest
+
+from addrinfo import *
+from addrinfo import _AddressFormatter
+
+
+class TestAddress(Address):
+    """Short address for Address unit tests."""
+
+    BIT_SIZE = 16
+
+    def __str__(self):
+        # FIXME implement correctly
+        return _AddressFormatter(
+            address_size = TestAddress.BIT_SIZE,
+            group_size = 8,
+            group_format = "%02X",
+            separator = '-'
+        ).format(self)
+
+    def __eq__(self, other):
+        if not isinstance(other, TestAddress):
+            return False
+        return self.get_address() == other.get_address()
+
+    def __hash__(self):
+        return hash(self.get_address())
+
+
+class AddressTestCase(unittest.TestCase):
+
+#   def test__value_of__mac_hypens(self):
+#       expected = EUI48Address(1250999896491)
+#       actual = Address.value_of("01-23-45-67-89-ab")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__mac_colons(self):
+#       expected = EUI48Address(1250999896491)
+#       actual = Address.value_of("01:23:45:67:89:ab")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_dot_decimal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("192.0.2.235")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_dot_hexadecimal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("0xC0.0x00.0x02.0xEB")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_dot_octal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("0300.0000.0002.0353")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_dot_mixed(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("192.0x00.0002.235")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_hexadecimal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("0xC00002EB")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_decimal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("3221226219")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv4_octal(self):
+#       expected = IPv4Address(3221226219)
+#       actual = Address.value_of("030000001353")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_full(self):
+#       expected = IPv6Address(42540766452641154071740215577757643572L)
+#       actual = Address.value_of("2001:0db8:85a3:0000:0000:8a2e:0370:7334")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_no_leading_zeros(self):
+#       expected = IPv6Address(42540766452641154071740215577757643572L)
+#       actual = Address.value_of("2001:db8:85a3:0:0:8a2e:370:7334")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_grouped(self):
+#       expected = IPv6Address(42540766452641154071740215577757643572L)
+#       actual = Address.value_of("2001:db8:85a3::8a2e:370:7334")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_loopback(self):
+#       expected = IPv6Address(1)
+#       actual = Address.value_of("::1")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_unspecified(self):
+#       expected = IPv6Address(0)
+#       actual = Address.value_of("::")
+#       self.assertEqual(expected, actual)
+
+#   def test__value_of__ipv6_dotted_quad(self):
+#       expected = IPv6Address(3221226112)
+#       actual = Address.value_of("::ffff:192.0.2.128")
+#       self.assertEqual(expected, actual)
+
+    def test__repr(self):
+        self.assertEquals("TestAddress(42)", repr(TestAddress(42)))
+
+    def test__lshift_1(self):
+        expected = TestAddress(0x1ffe)
+        actual = TestAddress(0x0fff) << 1
+        self.assertEquals(expected, actual)
+
+    def test__lshift_3(self):
+        expected = TestAddress(0x7ff8)
+        actual = TestAddress(0x0fff) << 3
+        self.assertEquals(expected, actual)
+
+    def test__lshift_overflow(self):
+        expected = TestAddress(0xffe0)
+        actual = TestAddress(0x0fff) << 5
+        self.assertEquals(expected, actual)
+
+    def test__rshift_1(self):
+        expected = TestAddress(0x7ff8)
+        actual = TestAddress(0xfff0) >> 1
+        self.assertEquals(expected, actual)
+
+    def test__rshift_3(self):
+        expected = TestAddress(0x1ffe)
+        actual = TestAddress(0xfff0) >> 3
+        self.assertEquals(expected, actual)
+
+    def test__rshift_underflow(self):
+        expected = TestAddress(0x07ff)
+        actual = TestAddress(0xfff0) >> 5
+        self.assertEquals(expected, actual)
+
+    def test__and__numeric(self):
+        expected = TestAddress(0x0204)
+        actual = TestAddress(0x1234) & 0xabcd
+        self.assertEquals(expected, actual)
+
+    def test__and__address(self):
+        expected = TestAddress(0x0204)
+        actual = TestAddress(0x1234) & TestAddress(0xabcd)
+        self.assertEquals(expected, actual)
+
+    def test__or__numeric(self):
+        expected = TestAddress(0xbbfd)
+        actual = TestAddress(0x1234) | 0xabcd
+        self.assertEquals(expected, actual)
+
+    def test__or__address(self):
+        expected = TestAddress(0xbbfd)
+        actual = TestAddress(0x1234) | TestAddress(0xabcd)
+        self.assertEquals(expected, actual)
+
+    def test__xor__numeric(self):
+        expected = TestAddress(0xb9f9)
+        actual = TestAddress(0x1234) ^ 0xabcd
+        self.assertEquals(expected, actual)
+
+    def test__xor__address(self):
+        expected = TestAddress(0xb9f9)
+        actual = TestAddress(0x1234) ^ TestAddress(0xabcd)
+        self.assertEquals(expected, actual)
+
+    def test__rand(self):
+        expected = 0x0204
+        actual = 0xabcd & TestAddress(0x1234)
+        self.assertEquals(expected, actual)
+
+    def test__ror(self):
+        expected = 0xbbfd
+        actual = 0xabcd | TestAddress(0x1234)
+        self.assertEquals(expected, actual)
+
+    def test__rxor(self):
+        expected = 0xb9f9
+        actual = 0xabcd ^ TestAddress(0x1234)
+        self.assertEquals(expected, actual)
+
+    def test__invert(self):
+        expected = TestAddress(0xedcb)
+        actual = ~TestAddress(0x1234)
+        self.assertEquals(expected, actual)
+
+    def test__complex(self):
+        expected = complex(0x0123, 0)
+        actual = complex(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__int(self):
+        expected = 0x0123
+        actual = int(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__long(self):
+        expected = 0x0123L
+        actual = long(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__long(self):
+        expected = 0x0123L
+        actual = long(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__oct(self):
+        expected = "0443"
+        actual = oct(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__hex(self):
+        expected = "0x123"
+        actual = hex(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+    def test__bin(self):
+        expected = "0b100100011"
+        actual = bin(TestAddress(0x0123))
+        self.assertEquals(expected, actual)
+
+
+class EUI48AddressTestCase(unittest.TestCase):
+
+    def test__bit_size(self):
+        self.assertEquals(48, EUI48Address.BIT_SIZE)
+
+    def test__init__address(self):
+        address = EUI48Address(42)
+        self.assertEquals(42, address.get_address())
+
+    def test__init__address_min(self):
+        address = EUI48Address(0)
+        self.assertEquals(0, address.get_address())
+
+    def test__init__address_max(self):
+        address = EUI48Address(2 ** 48 - 1)
+        self.assertEquals(2 ** 48 - 1, address.get_address())
+
+    def test__init__address_under_min(self):
+        try:
+            EUI48Address(-1)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**48-1", str(e))
+
+    def test__init__address_over_max(self):
+        try:
+            EUI48Address(2**48)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**48-1", str(e))
+
+    def test__as_eui48(self):
+        expected = EUI48Address(1250999896491)
+        actual = expected.as_eui48()
+        self.assertEquals(expected, actual)
+
+    def test__as_eui64(self):
+        # 01-23-45-67-89-AB -> 01-23-45-[FF-FE]-67-89-AB
+        # Same as eui64_from_eui48()
+        expected = EUI64Address(81986184009976235)
+        actual = EUI48Address(1250999896491).as_eui64()
+        self.assertEquals(expected, actual)
+
+    def test__as_eui64_from_eui48(self):
+        # 01-23-45-67-89-AB -> 01-23-45-[FF-FE]-67-89-AB
+        expected = EUI64Address(81986184009976235)
+        actual = EUI48Address(1250999896491).as_eui64_from_eui48()
+        self.assertEquals(expected, actual)
+
+    def test__as_eui64_from_mac48(self):
+        # 01-23-45-67-89-AB -> 01-23-45-[FF-FF]-67-89-AB
+        expected = EUI64Address(81986184026753451)
+        actual = EUI48Address(1250999896491).as_eui64_from_mac48()
+        self.assertEquals(expected, actual)
+
+    def test__str__address(self):
+        expected = "01-23-45-67-89-AB"
+        actual = str(EUI48Address(1250999896491))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_min(self):
+        expected = "00-00-00-00-00-00"
+        actual = str(EUI48Address(0))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_max(self):
+        expected = "FF-FF-FF-FF-FF-FF"
+        actual = str(EUI48Address(2 ** 48 - 1))
+        self.assertEquals(expected, actual)
+
+
+class EUI64AddressTestCase(unittest.TestCase):
+
+    def test__bit_size(self):
+        self.assertEquals(64, EUI64Address.BIT_SIZE)
+
+    def test__init__address(self):
+        address = EUI64Address(42)
+        self.assertEquals(42, address.get_address())
+
+    def test__init__address_min(self):
+        address = EUI64Address(0)
+        self.assertEquals(0, address.get_address())
+
+    def test__init__address_max(self):
+        address = EUI64Address(2 ** 64 - 1)
+        self.assertEquals(2 ** 64 - 1, address.get_address())
+
+    def test__init__address_under_min(self):
+        try:
+            EUI64Address(-1)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**64-1", str(e))
+
+    def test__init__address_over_max(self):
+        try:
+            EUI64Address(2**64)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**64-1", str(e))
+
+    def test__as_eui48__from_eui48_encapsulated(self):
+        # 01-23-45-[FF-FE]-67-89-AB -> 01-23-45-67-89-AB
+        expected = EUI48Address(1250999896491)
+        actual = EUI64Address(81986184009976235).as_eui48()
+        self.assertEquals(expected, actual)
+
+    def test__as_eui48__from_mac48_encapsulated(self):
+        # 01-23-45-[FF-FF]-67-89-AB -> 01-23-45-67-89-AB
+        expected = EUI48Address(1250999896491)
+        actual = EUI64Address(81986184026753451).as_eui48()
+        self.assertEquals(expected, actual)
+
+    def test__as_eui48__from_incompatible(self):
+        try:
+            EUI64Address(81985529216486895).as_eui48()
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals(
+                "01-23-45-67-89-AB-CD-EF is not an EUI-48 encapsulated " +
+                "EUI-64 value", str(e))
+
+    def test__as_eui64(self):
+        expected = EUI64Address(81986184009976235)
+        actual = expected.as_eui64()
+        self.assertEquals(expected, actual)
+
+    def test__str__address(self):
+        expected = "01-23-45-67-89-AB-CD-EF"
+        actual = str(EUI64Address(81985529216486895))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_min(self):
+        expected = "00-00-00-00-00-00-00-00"
+        actual = str(EUI64Address(0))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_max(self):
+        expected = "FF-FF-FF-FF-FF-FF-FF-FF"
+        actual = str(EUI64Address(2 ** 64 - 1))
+        self.assertEquals(expected, actual)
+
+
+class IPv4AddressTestCase(unittest.TestCase):
+
+    def test__init__address(self):
+        address = IPv4Address(42)
+        self.assertEquals(42, address.get_address())
+
+    def test__init__address_min(self):
+        address = IPv4Address(0)
+        self.assertEquals(0, address.get_address())
+
+    def test__init__address_max(self):
+        address = IPv4Address(2 ** 32 - 1)
+        self.assertEquals(2 ** 32 - 1, address.get_address())
+
+    def test__init__address_under_min(self):
+        try:
+            IPv4Address(-1)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**32-1", str(e))
+
+    def test__init__address_over_max(self):
+        try:
+            IPv4Address(2**32)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**32-1", str(e))
+
+    def test__str__address(self):
+        expected = "192.0.2.235"
+        actual = str(IPv4Address(3221226219))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_min(self):
+        expected = "0.0.0.0"
+        actual = str(IPv4Address(0))
+        self.assertEquals(expected, actual)
+
+    def test__str__address_max(self):
+        expected = "255.255.255.255"
+        actual = str(IPv4Address(2 ** 32 - 1))
+        self.assertEquals(expected, actual)
+
+
+class IPv6AddressTestCase(unittest.TestCase):
+
+    def test__get_link_local_address(self):
+        eui = EUI64Address(0x0123456789abcdef)
+        expected = IPv6Address(0xfe800000000000000323456789abcdef)
+        actual = IPv6Address.get_link_local_address(eui)
+        self.assertEquals(expected, actual)
+
+    def test__get_link_local_address__inverts_u_bit(self):
+        """Tests that the EUI-64 u-bit is inverted rather than set to 1."""
+        eui = EUI64Address(0xffffffffffffffff)
+        expected = IPv6Address(0xfe80000000000000fdffffffffffffff)
+        actual = IPv6Address.get_link_local_address(eui)
+        self.assertEquals(expected, actual)
+
+    def test__get_link_local_address__from_number(self):
+        expected = IPv6Address(0xfe800000000000000323456789abcdef)
+        actual = IPv6Address.get_link_local_address(0x0123456789abcdef)
+        self.assertEquals(expected, actual)
+
+    def test__get_link_local_address__from_None(self):
+        # Mock the get_hardware_address method
+        org_f = EUIAddress.get_hardware_address
+        eui = EUI48Address(0x00236c802131)
+        EUIAddress.get_hardware_address = classmethod(lambda x: eui)
+        try:
+            expected = IPv6Address(0xfe8000000000000002236cfffe802131)
+            actual = IPv6Address.get_link_local_address()
+            self.assertEquals(expected, actual)
+        finally:
+            EUIAddress.get_hardware_address = org_f
+
+    def test__init__address(self):
+        address = IPv6Address(42)
+        self.assertEquals(42, address.get_address())
+
+    def test__init__address_min(self):
+        address = IPv6Address(0)
+        self.assertEquals(0, address.get_address())
+
+    def test__init__address_max(self):
+        address = IPv6Address(2 ** 128 - 1)
+        self.assertEquals(2 ** 128 - 1, address.get_address())
+
+    def test__init__address_under_min(self):
+        try:
+            IPv6Address(-1)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**128-1", str(e))
+
+    def test__init__address_over_max(self):
+        try:
+            IPv6Address(2**128)
+            self.fail("failed to raise ValueError")
+        except ValueError, e:
+            self.assertEquals("address must be between 0 and 2**128-1", str(e))
+
+
+if __name__ == '__main__':
+    unittest.main()
+