File: test_socket_module_fallback.py

package info (click to toggle)
python-netaddr 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,912 kB
  • sloc: xml: 8,264; python: 6,697; makefile: 198; sh: 5
file content (51 lines) | stat: -rw-r--r-- 1,514 bytes parent folder | download
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
import pytest

from netaddr.fbsocket import inet_ntop, inet_pton, inet_ntoa, AF_INET, AF_INET6


@pytest.mark.parametrize(
    ('actual', 'expected'),
    [
        ('0:0:0:0:0:0:0:0', '::'),
        ('0:0:0:0:0:0:0:A', '::a'),
        ('A:0:0:0:0:0:0:0', 'a::'),
        ('A:0:A:0:0:0:0:0', 'a:0:a::'),
        ('A:0:0:0:0:0:0:A', 'a::a'),
        ('0:A:0:0:0:0:0:A', '0:a::a'),
        ('A:0:A:0:0:0:0:A', 'a:0:a::a'),
        ('0:0:0:A:0:0:0:A', '::a:0:0:0:a'),
        ('0:0:0:0:A:0:0:A', '::a:0:0:a'),
        ('A:0:0:0:0:A:0:A', 'a::a:0:a'),
        ('A:0:0:A:0:0:A:0', 'a::a:0:0:a:0'),
        ('A:0:A:0:A:0:A:0', 'a:0:a:0:a:0:a:0'),
        ('0:A:0:A:0:A:0:A', '0:a:0:a:0:a:0:a'),
        ('1080:0:0:0:8:800:200C:417A', '1080::8:800:200c:417a'),
        ('FEDC:BA98:7654:3210:FEDC:BA98:7654:3210', 'fedc:ba98:7654:3210:fedc:ba98:7654:3210'),
    ],
)
def test_inet_ntop_and_inet_pton_ipv6_conversion(actual, expected):
    assert inet_ntop(AF_INET6, inet_pton(AF_INET6, actual)) == expected


def test_inet_ntoa_ipv4_exceptions():
    with pytest.raises(TypeError):
        inet_ntoa(1)

    with pytest.raises(ValueError):
        inet_ntoa('\x00')


def test_inet_pton_ipv4_exceptions():
    with pytest.raises(OSError):
        inet_pton(AF_INET, '::0x07f')

    with pytest.raises(TypeError):
        inet_pton(AF_INET, 1)


def test_inet_pton_ipv6_exceptions():
    with pytest.raises(OSError):
        inet_pton(AF_INET6, '::0x07f')

    with pytest.raises(TypeError):
        inet_pton(AF_INET6, 1)