| 1 |
import pytest |
| 2 |
|
| 3 |
import dns.rrset |
| 4 |
from dns.zonefile import read_rrsets |
| 5 |
|
| 6 |
expected_mx_1 = dns.rrset.from_text("name.", 300, "in", "mx", "10 a.", "20 b.") |
| 7 |
expected_mx_2 = dns.rrset.from_text("name.", 10, "in", "mx", "10 a.", "20 b.") |
| 8 |
expected_mx_3 = dns.rrset.from_text("foo.", 10, "in", "mx", "10 a.") |
| 9 |
expected_mx_4 = dns.rrset.from_text("bar.", 10, "in", "mx", "20 b.") |
| 10 |
expected_mx_5 = dns.rrset.from_text("foo.example.", 10, "in", "mx", "10 a.example.") |
| 11 |
expected_mx_6 = dns.rrset.from_text("bar.example.", 10, "in", "mx", "20 b.") |
| 12 |
expected_mx_7 = dns.rrset.from_text("foo", 10, "in", "mx", "10 a") |
| 13 |
expected_mx_8 = dns.rrset.from_text("bar", 10, "in", "mx", "20 b.") |
| 14 |
expected_ns_1 = dns.rrset.from_text("name.", 300, "in", "ns", "hi.") |
| 15 |
expected_ns_2 = dns.rrset.from_text("name.", 300, "ch", "ns", "hi.") |
| 16 |
|
| 17 |
|
| 18 |
def equal_rrsets(a, b): |
| 19 |
# return True iff. a and b have the same rrsets regardless of order |
| 20 |
if len(a) != len(b): |
| 21 |
return False |
| 22 |
for rrset in a: |
| 23 |
if not rrset in b: |
| 24 |
return False |
| 25 |
return True |
| 26 |
|
| 27 |
|
| 28 |
def test_name_ttl_rdclass_forced(): |
| 29 |
input = """; |
| 30 |
mx 10 a |
| 31 |
mx 20 b. |
| 32 |
ns hi""" |
| 33 |
rrsets = read_rrsets(input, name="name", ttl=300) |
| 34 |
assert equal_rrsets(rrsets, [expected_mx_1, expected_ns_1]) |
| 35 |
assert rrsets[0].ttl == 300 |
| 36 |
assert rrsets[1].ttl == 300 |
| 37 |
|
| 38 |
|
| 39 |
def test_name_ttl_rdclass_forced_rdata_split(): |
| 40 |
input = """; |
| 41 |
mx 10 a |
| 42 |
ns hi |
| 43 |
mx 20 b.""" |
| 44 |
rrsets = read_rrsets(input, name="name", ttl=300) |
| 45 |
assert equal_rrsets(rrsets, [expected_mx_1, expected_ns_1]) |
| 46 |
|
| 47 |
|
| 48 |
def test_name_ttl_rdclass_rdtype_forced(): |
| 49 |
input = """; |
| 50 |
10 a |
| 51 |
20 b.""" |
| 52 |
rrsets = read_rrsets(input, name="name", ttl=300, rdtype="mx") |
| 53 |
assert equal_rrsets(rrsets, [expected_mx_1]) |
| 54 |
|
| 55 |
|
| 56 |
def test_name_rdclass_forced(): |
| 57 |
input = """30 mx 10 a |
| 58 |
10 mx 20 b. |
| 59 |
""" |
| 60 |
rrsets = read_rrsets(input, name="name") |
| 61 |
assert equal_rrsets(rrsets, [expected_mx_2]) |
| 62 |
assert rrsets[0].ttl == 10 |
| 63 |
|
| 64 |
|
| 65 |
def test_rdclass_forced(): |
| 66 |
input = """; |
| 67 |
foo 20 mx 10 a |
| 68 |
bar 30 mx 20 b. |
| 69 |
""" |
| 70 |
rrsets = read_rrsets(input) |
| 71 |
assert equal_rrsets(rrsets, [expected_mx_3, expected_mx_4]) |
| 72 |
|
| 73 |
|
| 74 |
def test_rdclass_forced_with_origin(): |
| 75 |
input = """; |
| 76 |
foo 20 mx 10 a |
| 77 |
bar.example. 30 mx 20 b. |
| 78 |
""" |
| 79 |
rrsets = read_rrsets(input, origin="example") |
| 80 |
assert equal_rrsets(rrsets, [expected_mx_5, expected_mx_6]) |
| 81 |
|
| 82 |
|
| 83 |
def test_rdclass_forced_with_origin_relativized(): |
| 84 |
input = """; |
| 85 |
foo 20 mx 10 a.example. |
| 86 |
bar.example. 30 mx 20 b. |
| 87 |
""" |
| 88 |
rrsets = read_rrsets(input, origin="example", relativize=True) |
| 89 |
assert equal_rrsets(rrsets, [expected_mx_7, expected_mx_8]) |
| 90 |
|
| 91 |
|
| 92 |
def test_rdclass_matching_default_tolerated(): |
| 93 |
input = """; |
| 94 |
foo 20 mx 10 a.example. |
| 95 |
bar.example. 30 in mx 20 b. |
| 96 |
""" |
| 97 |
rrsets = read_rrsets(input, origin="example", relativize=True, rdclass=None) |
| 98 |
assert equal_rrsets(rrsets, [expected_mx_7, expected_mx_8]) |
| 99 |
|
| 100 |
|
| 101 |
def test_rdclass_not_matching_default_rejected(): |
| 102 |
input = """; |
| 103 |
foo 20 mx 10 a.example. |
| 104 |
bar.example. 30 ch mx 20 b. |
| 105 |
""" |
| 106 |
with pytest.raises(dns.exception.SyntaxError): |
| 107 |
rrsets = read_rrsets(input, origin="example", relativize=True, rdclass=None) |
| 108 |
|
| 109 |
|
| 110 |
def test_default_rdclass_is_none(): |
| 111 |
input = "" |
| 112 |
with pytest.raises(TypeError): |
| 113 |
rrsets = read_rrsets( |
| 114 |
input, default_rdclass=None, origin="example", relativize=True |
| 115 |
) |
| 116 |
|
| 117 |
|
| 118 |
def test_name_rdclass_rdtype_force(): |
| 119 |
# No real-world usage should do this, but it can be specified so we test it. |
| 120 |
input = """; |
| 121 |
30 10 a |
| 122 |
10 20 b. |
| 123 |
""" |
| 124 |
rrsets = read_rrsets(input, name="name", rdtype="mx") |
| 125 |
assert equal_rrsets(rrsets, [expected_mx_1]) |
| 126 |
assert rrsets[0].ttl == 10 |
| 127 |
|
| 128 |
|
| 129 |
def test_rdclass_rdtype_force(): |
| 130 |
# No real-world usage should do this, but it can be specified so we test it. |
| 131 |
input = """; |
| 132 |
foo 30 10 a |
| 133 |
bar 30 20 b. |
| 134 |
""" |
| 135 |
rrsets = read_rrsets(input, rdtype="mx") |
| 136 |
assert equal_rrsets(rrsets, [expected_mx_3, expected_mx_4]) |
| 137 |
|
| 138 |
|
| 139 |
# also weird but legal |
| 140 |
# input5 = '''foo 30 10 a |
| 141 |
# bar 10 20 foo. |
| 142 |
#''' |