File: test_network.py

package info (click to toggle)
pyartnet 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 668 kB
  • sloc: python: 1,845; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,635 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
52
53
54
55
import pytest

from pyartnet.base.network import resolve_hostname, validate_ip_address, validate_port


async def test_hostname() -> None:
    with pytest.raises(ValueError) as e:  # noqa: PT011
        await resolve_hostname('does_not_exist', 0)

    assert str(e.value) in (
        'Cannot resolve hostname "does_not_exist"! 11001: getaddrinfo failed'
        'Cannot resolve hostname "does_not_exist"! -3: Temporary failure in name resolution'
        'Cannot resolve hostname "does_not_exist"! -2: Name or service not known'
    )


def test_validate_port() -> None:
    with pytest.raises(ValueError) as e:
        validate_port(0)
    assert str(e.value) == 'port must be between 1 and 65535'

    validate_port(1)
    validate_port(0, allow_0=True)

    with pytest.raises(ValueError) as e:
        validate_port(65536)
    assert str(e.value) == 'port must be between 1 and 65535'

    validate_port(65535)


async def test_get_ip() -> None:
    # ip address v4
    address = '127.0.0.1'
    obj = validate_ip_address(address)
    assert str(obj) == address
    assert obj.version == 4

    # ip address v6
    address = '::1'
    obj = validate_ip_address(address)
    assert str(obj) == address
    assert obj.version == 6

    # hostname gets resolved
    objs = await resolve_hostname('localhost', 0, mode='v4')
    assert str(objs[0]) == '127.0.0.1'
    assert objs[0].version == 4

    objs = await resolve_hostname('localhost', 0, mode='v6')
    assert str(objs[0]) == '::1'
    assert objs[0].version == 6

    objs = await resolve_hostname('localhost', 0, mode='auto')
    assert str(objs[0]) in ('::1', '127.0.0.1')