summaryrefslogtreecommitdiffstats
path: root/netaddr/tests/strategy/test_ipv4_strategy.py
blob: 607d816ff8244005ca89a2dafdd161e55be87272 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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