import sys

import pytest

from netaddr import INET_PTON, AddrFormatError
from netaddr.strategy import ipv4


def test_strategy_ipv4():
    b = '11000000.00000000.00000010.00000001'
    i = 3221225985
    t = (192, 0, 2, 1)
    s = '192.0.2.1'
    bin_val = '0b11000000000000000000001000000001'

    assert ipv4.bits_to_int(b) == i
    assert ipv4.int_to_bits(i) == b
    assert ipv4.int_to_str(i) == s
    assert ipv4.int_to_words(i) == t
    assert ipv4.int_to_bin(i) == bin_val
    assert ipv4.int_to_bin(i) == bin_val
    assert ipv4.bin_to_int(bin_val) == i
    assert ipv4.words_to_int(t) == i
    assert ipv4.words_to_int(list(t)) == i
    assert ipv4.valid_bin(bin_val)


@pytest.mark.skipif(sys.version_info > (3,), reason="requires python 2.x")
def test_strategy_ipv4_py2():
    i = 3221225985
    p = '\xc0\x00\x02\x01'
    assert ipv4.int_to_packed(i) == p
    assert ipv4.packed_to_int(p) == i


@pytest.mark.skipif(sys.version_info < (3,), reason="requires python 3.x")
def test_strategy_ipv4_py3():
    i = 3221225985
    p = b'\xc0\x00\x02\x01'
    assert ipv4.int_to_packed(i) == p
    assert ipv4.packed_to_int(p) == i


def test_strategy_inet_aton_behaviour():
    # inet_aton() is a very old system call and is very permissive with
    # regard to what is assume is a valid IPv4 address. Unfortunately, it
    # is also the most widely used by system software used in software today,
    # so netaddr supports this behaviour by default.

    assert ipv4.str_to_int('127') == 127
    assert ipv4.str_to_int('0x7f') == 127
    assert ipv4.str_to_int('0177') == 127
    assert ipv4.str_to_int('127.1') == 2130706433
    assert ipv4.str_to_int('0x7f.1') == 2130706433
    assert ipv4.str_to_int('0177.1') == 2130706433
    assert ipv4.str_to_int('127.0.0.1') == 2130706433


def test_strategy_inet_pton_behaviour():
    # inet_pton() is a newer system call that supports both IPv4 and IPv6.
    # It is a lot more strict about what it deems to be a valid IPv4 address
    # and doesn't support many of the features found in inet_aton() such as
    # support for non- decimal octets, partial numbers of octets, etc.

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('127', flags=INET_PTON)

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('0x7f', flags=INET_PTON)

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('0177', flags=INET_PTON)

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('127.1', flags=INET_PTON)

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('0x7f.1', flags=INET_PTON)

    with pytest.raises(AddrFormatError):
        ipv4.str_to_int('0177.1', flags=INET_PTON)

    assert ipv4.str_to_int('127.0.0.1', flags=INET_PTON) == 2130706433
