"""Test cases for jersey.inet"""

import socket

from twisted.python import log
from twisted.trial.unittest import TestCase

from jersey import inet


class AddressTestCase(TestCase):

    v4Addr = "10.0.1.20"
    v6v4Addr = "::ffff:" + v4Addr
    v6Addr = "2001:470:1f06:2b8::2"
    zeroPaddedV6Addr = "2001:0470:1f06:02b8:0000:0000:0000:0002"
    otherV6Addr = v6Addr[:-1] + "2222"
    hostName = "panix.olix0r.net"
    hexVal = 0x01010101

    def setUp(self):
        self.v4Bytes = socket.inet_pton(socket.AF_INET, self.v4Addr)
        self.v6v4Bytes = socket.inet_pton(socket.AF_INET6, self.v6v4Addr)
        self.v6Bytes = socket.inet_pton(socket.AF_INET6, self.v6Addr)
        self.otherV6Bytes = socket.inet_pton(socket.AF_INET6, self.otherV6Addr)


    def test_IP_v4(self):
        ip = inet.IP(self.v4Addr)
        self.assertIsInstance(ip, inet.V4Address)

    def test_IP_v6(self):
        ip = inet.IP(self.v6Addr)
        self.assertIsInstance(ip, inet.V6Address)

    def test_IP_hostname_error(self):
        self.assertRaises(ValueError, inet.IP, self.hostName)

    def test_IP_hex_error(self):
        self.assertRaises(ValueError, inet.IP, self.hexVal)
        self.assertRaises(ValueError, inet.IP, hex(self.hexVal))


    def test_IP_object_error(self):
        self.assertRaises(ValueError, inet.IP, object())


    def test_nToIP_v4(self):
        self.assertRaises(ValueError, inet.IP, self.v4Bytes)

        ip = inet.nToIP(self.v4Bytes)
        self.assertEquals(ip, inet.IP(self.v4Addr))

    def test_nToIP_v6(self):
        self.assertRaises(ValueError, inet.IP, self.v6Bytes)

        ip = inet.nToIP(self.v6Bytes)
        self.assertEquals(ip, inet.IP(self.v6Addr))


    #def test_nToIP_hostname_error(self):
    #    self.assertRaises(ValueError, inet.nToIP, self.hostName)
    #
    #test_nToIP_hostname_error.skip = "'panix.olix0r.net' is a valid IPv6 " \
    #            "bytestream representing the address: " \
    #            "7061:6e69:782e:6f6c:6978:3072:2e6e:6574."


    def test_nToIP_hex_error(self):
        self.assertRaises(ValueError, inet.nToIP, self.hexVal)
        self.assertRaises(ValueError, inet.nToIP, hex(self.hexVal))

    def test_nToIP_object_error(self):