File: test_dnsutil.py

package info (click to toggle)
freeipa 4.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 367,240 kB
  • sloc: javascript: 562,763; python: 310,289; ansic: 49,809; sh: 7,176; makefile: 2,589; xml: 343; sed: 16
file content (148 lines) | stat: -rw-r--r-- 4,446 bytes parent folder | download | duplicates (3)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#
# Copyright (C) 2018  FreeIPA Contributors.  See COPYING for license
#
import dns.name
import dns.rdataclass
import dns.rdatatype
from dns.rdtypes.IN.SRV import SRV
from dns.rdtypes.ANY.URI import URI

from ipapython import dnsutil

import pytest


def mksrv(priority, weight, port, target):
    return SRV(
        rdclass=dns.rdataclass.IN,
        rdtype=dns.rdatatype.SRV,
        priority=priority,
        weight=weight,
        port=port,
        target=dns.name.from_text(target)
    )


def mkuri(priority, weight, target):
    return URI(
        rdclass=dns.rdataclass.IN,
        rdtype=dns.rdatatype.URI,
        priority=priority,
        weight=weight,
        target=target
    )


class TestSortSRV:
    def test_empty(self):
        assert dnsutil.sort_prio_weight([]) == []

    def test_one(self):
        h1 = mksrv(1, 0, 443, u"host1")
        assert dnsutil.sort_prio_weight([h1]) == [h1]

        h2 = mksrv(10, 5, 443, u"host2")
        assert dnsutil.sort_prio_weight([h2]) == [h2]

    def test_prio(self):
        h1 = mksrv(1, 0, 443, u"host1")
        h2 = mksrv(2, 0, 443, u"host2")
        h3 = mksrv(3, 0, 443, u"host3")
        assert dnsutil.sort_prio_weight([h3, h2, h1]) == [h1, h2, h3]
        assert dnsutil.sort_prio_weight([h3, h3, h3]) == [h3]
        assert dnsutil.sort_prio_weight([h2, h2, h1, h1]) == [h1, h2]

        h380 = mksrv(4, 0, 80, u"host3")
        assert dnsutil.sort_prio_weight([h1, h3, h380]) == [h1, h3, h380]

    def assert_permutations(self, answers, permutations):
        seen = set()
        for _unused in range(1000):
            result = tuple(dnsutil.sort_prio_weight(answers))
            assert result in permutations
            seen.add(result)
            if seen == permutations:
                break
        else:
            pytest.fail("sorting didn't exhaust all permutations.")

    def test_sameprio(self):
        h1 = mksrv(1, 0, 443, u"host1")
        h2 = mksrv(1, 0, 443, u"host2")
        permutations = {
            (h1, h2),
            (h2, h1),
        }
        self.assert_permutations([h1, h2], permutations)

    def test_weight(self):
        h1 = mksrv(1, 0, 443, u"host1")
        h2_w15 = mksrv(2, 15, 443, u"host2")
        h3_w10 = mksrv(2, 10, 443, u"host3")

        permutations = {
            (h1, h2_w15, h3_w10),
            (h1, h3_w10, h2_w15),
        }
        self.assert_permutations([h1, h2_w15, h3_w10], permutations)

    def test_large(self):
        records = tuple(
            mksrv(1, i, 443, "host{}".format(i)) for i in range(1000)
        )
        assert len(dnsutil.sort_prio_weight(records)) == len(records)


class TestSortURI:
    def test_prio(self):
        h1 = mkuri(1, 0, u"https://host1/api")
        h2 = mkuri(2, 0, u"https://host2/api")
        h3 = mkuri(3, 0, u"https://host3/api")
        assert dnsutil.sort_prio_weight([h3, h2, h1]) == [h1, h2, h3]
        assert dnsutil.sort_prio_weight([h3, h3, h3]) == [h3]
        assert dnsutil.sort_prio_weight([h2, h2, h1, h1]) == [h1, h2]


class TestDNSResolver:
    @pytest.fixture(name="res")
    def resolver(self):
        """Resolver that doesn't read /etc/resolv.conf

        /etc/resolv.conf is not mandatory on systems
        """
        return dnsutil.DNSResolver(configure=False)

    def test_nameservers(self, res):
        res.nameservers = ["4.4.4.4", "8.8.8.8"]
        assert res.nameservers == ["4.4.4.4", "8.8.8.8"]

    def test_nameservers_with_ports(self, res):
        res.nameservers = ["4.4.4.4 port 53", "8.8.8.8 port 8053"]
        assert res.nameservers == ["4.4.4.4", "8.8.8.8"]
        assert res.nameserver_ports == {"4.4.4.4": 53, "8.8.8.8": 8053}

        res.nameservers = ["4.4.4.4 port 53", "8.8.8.8  port  8053"]
        assert res.nameservers == ["4.4.4.4", "8.8.8.8"]
        assert res.nameserver_ports == {"4.4.4.4": 53, "8.8.8.8": 8053}

    def test_nameservers_with_bad_ports(self, res):
        try:
            res.nameservers = ["4.4.4.4 port a"]
        except ValueError:
            pass
        else:
            pytest.fail("No fail on bad port a")

        try:
            res.nameservers = ["4.4.4.4 port -1"]
        except ValueError:
            pass
        else:
            pytest.fail("No fail on bad port -1")

        try:
            res.nameservers = ["4.4.4.4 port 65536"]
        except ValueError:
            pass
        else:
            pytest.fail("No fail on bad port 65536")