File: test_rrset_reader.py

package info (click to toggle)
dnspython 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,448 kB
  • sloc: python: 34,885; sh: 7; makefile: 4
file content (142 lines) | stat: -rw-r--r-- 3,920 bytes parent folder | download | duplicates (2)
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
import pytest

import dns.rrset
from dns.zonefile import read_rrsets

expected_mx_1 = dns.rrset.from_text("name.", 300, "in", "mx", "10 a.", "20 b.")
expected_mx_2 = dns.rrset.from_text("name.", 10, "in", "mx", "10 a.", "20 b.")
expected_mx_3 = dns.rrset.from_text("foo.", 10, "in", "mx", "10 a.")
expected_mx_4 = dns.rrset.from_text("bar.", 10, "in", "mx", "20 b.")
expected_mx_5 = dns.rrset.from_text("foo.example.", 10, "in", "mx", "10 a.example.")
expected_mx_6 = dns.rrset.from_text("bar.example.", 10, "in", "mx", "20 b.")
expected_mx_7 = dns.rrset.from_text("foo", 10, "in", "mx", "10 a")
expected_mx_8 = dns.rrset.from_text("bar", 10, "in", "mx", "20 b.")
expected_ns_1 = dns.rrset.from_text("name.", 300, "in", "ns", "hi.")
expected_ns_2 = dns.rrset.from_text("name.", 300, "ch", "ns", "hi.")


def equal_rrsets(a, b):
    # return True iff. a and b have the same rrsets regardless of order
    if len(a) != len(b):
        return False
    for rrset in a:
        if not rrset in b:
            return False
    return True


def test_name_ttl_rdclass_forced():
    input = """;
mx 10 a
mx 20 b.
ns hi"""
    rrsets = read_rrsets(input, name="name", ttl=300)
    assert equal_rrsets(rrsets, [expected_mx_1, expected_ns_1])
    assert rrsets[0].ttl == 300
    assert rrsets[1].ttl == 300


def test_name_ttl_rdclass_forced_rdata_split():
    input = """;
mx 10 a
ns hi
mx 20 b."""
    rrsets = read_rrsets(input, name="name", ttl=300)
    assert equal_rrsets(rrsets, [expected_mx_1, expected_ns_1])


def test_name_ttl_rdclass_rdtype_forced():
    input = """;
10 a
20 b."""
    rrsets = read_rrsets(input, name="name", ttl=300, rdtype="mx")
    assert equal_rrsets(rrsets, [expected_mx_1])


def test_name_rdclass_forced():
    input = """30 mx 10 a
10 mx 20 b.
"""
    rrsets = read_rrsets(input, name="name")
    assert equal_rrsets(rrsets, [expected_mx_2])
    assert rrsets[0].ttl == 10


def test_rdclass_forced():
    input = """;
foo 20 mx 10 a
bar 30 mx 20 b.
"""
    rrsets = read_rrsets(input)
    assert equal_rrsets(rrsets, [expected_mx_3, expected_mx_4])


def test_rdclass_forced_with_origin():
    input = """;
foo 20 mx 10 a
bar.example. 30 mx 20 b.
"""
    rrsets = read_rrsets(input, origin="example")
    assert equal_rrsets(rrsets, [expected_mx_5, expected_mx_6])


def test_rdclass_forced_with_origin_relativized():
    input = """;
foo 20 mx 10 a.example.
bar.example. 30 mx 20 b.
"""
    rrsets = read_rrsets(input, origin="example", relativize=True)
    assert equal_rrsets(rrsets, [expected_mx_7, expected_mx_8])


def test_rdclass_matching_default_tolerated():
    input = """;
foo 20 mx 10 a.example.
bar.example. 30 in mx 20 b.
"""
    rrsets = read_rrsets(input, origin="example", relativize=True, rdclass=None)
    assert equal_rrsets(rrsets, [expected_mx_7, expected_mx_8])


def test_rdclass_not_matching_default_rejected():
    input = """;
foo 20 mx 10 a.example.
bar.example. 30 ch mx 20 b.
"""
    with pytest.raises(dns.exception.SyntaxError):
        rrsets = read_rrsets(input, origin="example", relativize=True, rdclass=None)


def test_default_rdclass_is_none():
    input = ""
    with pytest.raises(TypeError):
        rrsets = read_rrsets(
            input, default_rdclass=None, origin="example", relativize=True
        )


def test_name_rdclass_rdtype_force():
    # No real-world usage should do this, but it can be specified so we test it.
    input = """;
30 10 a
10 20 b.
"""
    rrsets = read_rrsets(input, name="name", rdtype="mx")
    assert equal_rrsets(rrsets, [expected_mx_1])
    assert rrsets[0].ttl == 10


def test_rdclass_rdtype_force():
    # No real-world usage should do this, but it can be specified so we test it.
    input = """;
foo 30 10 a
bar 30 20 b.
"""
    rrsets = read_rrsets(input, rdtype="mx")
    assert equal_rrsets(rrsets, [expected_mx_3, expected_mx_4])


# also weird but legal
# input5 = '''foo 30 10 a
# bar 10 20 foo.
#'''