summaryrefslogtreecommitdiffstats
path: root/tests/manager/datamodel/test_network_schema.py
blob: 1451ac20ebfc4658b0a55450b3ab9b4c0c16c481 (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
84
85
86
87
88
89
90
91
92
93
from typing import Any, Dict, Optional

import pytest
from pytest import raises

from knot_resolver.constants import WATCHDOG_LIB
from knot_resolver.datamodel.network_schema import ListenSchema, NetworkSchema, TLSSchema
from knot_resolver.datamodel.types import InterfaceOptionalPort, PortNumber
from knot_resolver.utils.modeling.exceptions import DataValidationError


def test_listen_defaults():
    o = NetworkSchema()

    assert len(o.listen) == 2
    # {"ip-address": "127.0.0.1"}
    assert o.listen[0].interface.to_std() == [InterfaceOptionalPort("127.0.0.1")]
    assert o.listen[0].port == PortNumber(53)
    assert o.listen[0].kind == "dns"
    assert o.listen[0].freebind == False
    # {"ip-address": "::1", "freebind": True}
    assert o.listen[1].interface.to_std() == [InterfaceOptionalPort("::1")]
    assert o.listen[1].port == PortNumber(53)
    assert o.listen[1].kind == "dns"
    assert o.listen[1].freebind == True


@pytest.mark.parametrize(
    "listen,port",
    [
        ({"unix-socket": ["/tmp/kresd-socket"]}, None),
        ({"interface": ["::1"]}, 53),
        ({"interface": ["::1"], "kind": "dot"}, 853),
        ({"interface": ["::1"], "kind": "doh-legacy"}, 443),
        ({"interface": ["::1"], "kind": "doh2"}, 443),
    ],
)
def test_listen_port_defaults(listen: Dict[str, Any], port: Optional[int]):
    assert ListenSchema(listen).port == (PortNumber(port) if port else None)


@pytest.mark.parametrize(
    "listen",
    [
        {"unix-socket": "/tmp/kresd-socket"},
        {"unix-socket": ["/tmp/kresd-socket", "/tmp/kresd-socket2"]},
        {"interface": "::1"},
        {"interface": "::1@5353"},
        {"interface": "::1", "port": 5353},
        {"interface": ["127.0.0.1", "::1"]},
        {"interface": ["127.0.0.1@5353", "::1@5353"]},
        {"interface": ["127.0.0.1", "::1"], "port": 5353},
        {"interface": "lo"},
        {"interface": "lo@5353"},
        {"interface": "lo", "port": 5353},
        {"interface": ["lo", "eth0"]},
        {"interface": ["lo@5353", "eth0@5353"]},
        {"interface": ["lo", "eth0"], "port": 5353},
    ],
)
def test_listen_valid(listen: Dict[str, Any]):
    assert ListenSchema(listen)


@pytest.mark.parametrize(
    "listen",
    [
        {"unix-socket": "/tmp/kresd-socket", "port": "53"},
        {"interface": "::1", "unix-socket": "/tmp/kresd-socket"},
        {"interface": "::1@5353", "port": 5353},
        {"interface": ["127.0.0.1", "::1@5353"]},
        {"interface": ["127.0.0.1@5353", "::1@5353"], "port": 5353},
        {"interface": "lo@5353", "port": 5353},
        {"interface": ["lo", "eth0@5353"]},
        {"interface": ["lo@5353", "eth0@5353"], "port": 5353},
    ],
)
def test_listen_invalid(listen: Dict[str, Any]):
    with raises(DataValidationError):
        ListenSchema(listen)


@pytest.mark.parametrize(
    "tls",
    [
        {"files-watchdog": "auto"},
        {"files-watchdog": True},
        {"files-watchdog": False},
    ],
)
def test_tls_files_watchdog(tls: Dict[str, Any]):
    expected: bool = WATCHDOG_LIB if tls["files-watchdog"] == "auto" else tls["files-watchdog"]
    assert TLSSchema(tls).files_watchdog == expected