Debian Codegraph @

tests/test_zonedigest.py

raw
1 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
2
3 import io
4 import textwrap
5 import unittest
6
7 import dns.rdata
8 import dns.rrset
9 import dns.zone
10
11
12 class ZoneDigestTestCase(unittest.TestCase):
13 # Examples from RFC 8976, fixed per errata.
14
15 simple_example = textwrap.dedent(
16 """
17 example. 86400 IN SOA ns1 admin 2018031900 (
18 1800 900 604800 86400 )
19 86400 IN NS ns1
20 86400 IN NS ns2
21 86400 IN ZONEMD 2018031900 1 1 (
22 c68090d90a7aed71
23 6bc459f9340e3d7c
24 1370d4d24b7e2fc3
25 a1ddc0b9a87153b9
26 a9713b3c9ae5cc27
27 777f98b8e730044c )
28 ns1 3600 IN A 203.0.113.63
29 ns2 3600 IN AAAA 2001:db8::63
30 """
31 )
32
33 complex_example = textwrap.dedent(
34 """
35 example. 86400 IN SOA ns1 admin 2018031900 (
36 1800 900 604800 86400 )
37 86400 IN NS ns1
38 86400 IN NS ns2
39 86400 IN ZONEMD 2018031900 1 1 (
40 a3b69bad980a3504
41 e1cffcb0fd6397f9
42 3848071c93151f55
43 2ae2f6b1711d4bd2
44 d8b39808226d7b9d
45 b71e34b72077f8fe )
46 ns1 3600 IN A 203.0.113.63
47 NS2 3600 IN AAAA 2001:db8::63
48 occluded.sub 7200 IN TXT "I'm occluded but must be digested"
49 sub 7200 IN NS ns1
50 duplicate 300 IN TXT "I must be digested just once"
51 duplicate 300 IN TXT "I must be digested just once"
52 foo.test. 555 IN TXT "out-of-zone data must be excluded"
53 UPPERCASE 3600 IN TXT "canonicalize uppercase owner names"
54 * 777 IN PTR dont-forget-about-wildcards
55 mail 3600 IN MX 20 MAIL1
56 mail 3600 IN MX 10 Mail2.Example.
57 sortme 3600 IN AAAA 2001:db8::5:61
58 sortme 3600 IN AAAA 2001:db8::3:62
59 sortme 3600 IN AAAA 2001:db8::4:63
60 sortme 3600 IN AAAA 2001:db8::1:65
61 sortme 3600 IN AAAA 2001:db8::2:64
62 non-apex 900 IN ZONEMD 2018031900 1 1 (
63 616c6c6f77656420
64 6275742069676e6f
65 7265642e20616c6c
66 6f77656420627574
67 2069676e6f726564
68 2e20616c6c6f7765 )
69 """
70 )
71
72 multiple_digests_example = textwrap.dedent(
73 """
74 example. 86400 IN SOA ns1 admin 2018031900 (
75 1800 900 604800 86400 )
76 example. 86400 IN NS ns1.example.
77 example. 86400 IN NS ns2.example.
78 example. 86400 IN ZONEMD 2018031900 1 1 (
79 62e6cf51b02e54b9
80 b5f967d547ce4313
81 6792901f9f88e637
82 493daaf401c92c27
83 9dd10f0edb1c56f8
84 080211f8480ee306 )
85 example. 86400 IN ZONEMD 2018031900 1 2 (
86 08cfa1115c7b948c
87 4163a901270395ea
88 226a930cd2cbcf2f
89 a9a5e6eb85f37c8a
90 4e114d884e66f176
91 eab121cb02db7d65
92 2e0cc4827e7a3204
93 f166b47e5613fd27 )
94 example. 86400 IN ZONEMD 2018031900 1 240 (
95 e2d523f654b9422a
96 96c5a8f44607bbee )
97 example. 86400 IN ZONEMD 2018031900 241 1 (
98 e1846540e33a9e41
99 89792d18d5d131f6
100 05fc283eaaaaaaaa
101 aaaaaaaaaaaaaaaa
102 aaaaaaaaaaaaaaaa
103 aaaaaaaaaaaaaaaa)
104 ns1.example. 3600 IN A 203.0.113.63
105 ns2.example. 86400 IN TXT "This example has multiple digests"
106 NS2.EXAMPLE. 3600 IN AAAA 2001:db8::63
107 """
108 )
109
110 def _get_zonemd(self, zone):
111 return zone.get_rdataset(zone.origin, "ZONEMD")
112
113 def test_zonemd_simple(self):
114 zone = dns.zone.from_text(self.simple_example, origin="example")
115 zone.verify_digest()
116 zonemd = self._get_zonemd(zone)
117 self.assertEqual(zonemd[0], zone.compute_digest(zonemd[0].hash_algorithm))
118
119 def test_zonemd_simple_absolute(self):
120 zone = dns.zone.from_text(
121 self.simple_example, origin="example", relativize=False
122 )
123 zone.verify_digest()
124 zonemd = self._get_zonemd(zone)
125 self.assertEqual(zonemd[0], zone.compute_digest(zonemd[0].hash_algorithm))
126
127 def test_zonemd_complex(self):
128 zone = dns.zone.from_text(self.complex_example, origin="example")
129 zone.verify_digest()
130 zonemd = self._get_zonemd(zone)
131 self.assertEqual(zonemd[0], zone.compute_digest(zonemd[0].hash_algorithm))
132
133 def test_zonemd_multiple_digests(self):
134 zone = dns.zone.from_text(self.multiple_digests_example, origin="example")
135 zone.verify_digest()
136
137 zonemd = self._get_zonemd(zone)
138 for rr in zonemd:
139 if rr.scheme == 1 and rr.hash_algorithm in (1, 2):
140 zone.verify_digest(rr)
141 self.assertEqual(rr, zone.compute_digest(rr.hash_algorithm))
142 else:
143 with self.assertRaises(dns.zone.DigestVerificationFailure):
144 zone.verify_digest(rr)
145
146 def test_zonemd_no_digest(self):
147 zone = dns.zone.from_text(self.simple_example, origin="example")
148 zone.delete_rdataset(dns.name.empty, "ZONEMD")
149 with self.assertRaises(dns.zone.NoDigest):
150 zone.verify_digest()
151
152 sha384_hash = "ab" * 48
153 sha512_hash = "ab" * 64
154
155 def test_zonemd_parse_rdata(self):
156 dns.rdata.from_text("IN", "ZONEMD", "100 1 1 " + self.sha384_hash)
157 dns.rdata.from_text("IN", "ZONEMD", "100 1 2 " + self.sha512_hash)
158 dns.rdata.from_text("IN", "ZONEMD", "100 100 1 " + self.sha384_hash)
159 dns.rdata.from_text("IN", "ZONEMD", "100 1 100 abcd")
160
161 def test_zonemd_unknown_scheme(self):
162 zone = dns.zone.from_text(self.simple_example, origin="example")
163 with self.assertRaises(dns.zone.UnsupportedDigestScheme):
164 zone.compute_digest(dns.zone.DigestHashAlgorithm.SHA384, 2)
165
166 def test_zonemd_unknown_hash_algorithm(self):
167 zone = dns.zone.from_text(self.simple_example, origin="example")
168 with self.assertRaises(dns.zone.UnsupportedDigestHashAlgorithm):
169 zone.compute_digest(5)
170
171 def test_zonemd_invalid_digest_length(self):
172 with self.assertRaises(dns.exception.SyntaxError):
173 dns.rdata.from_text("IN", "ZONEMD", "100 1 2 " + self.sha384_hash)
174 with self.assertRaises(dns.exception.SyntaxError):
175 dns.rdata.from_text("IN", "ZONEMD", "100 2 1 " + self.sha512_hash)
176
177 def test_zonemd_parse_rdata_reserved(self):
178 with self.assertRaises(dns.exception.SyntaxError):
179 dns.rdata.from_text("IN", "ZONEMD", "100 0 1 " + self.sha384_hash)
180 with self.assertRaises(dns.exception.SyntaxError):
181 dns.rdata.from_text("IN", "ZONEMD", "100 1 0 " + self.sha384_hash)
182
183 sorting_zone = textwrap.dedent(
184 """
185 @ 86400 IN SOA ns1 admin 2018031900 (
186 1800 900 604800 86400 )
187 86400 IN NS ns1
188 86400 IN NS ns2
189 86400 IN RP n1.example. a.
190 86400 IN RP n1. b.
191 """
192 )
193
194 def test_relative_zone_sorting(self):
195 z1 = dns.zone.from_text(self.sorting_zone, "example.", relativize=True)
196 z2 = dns.zone.from_text(self.sorting_zone, "example.", relativize=False)
197 zmd1 = z1.compute_digest(dns.zone.DigestHashAlgorithm.SHA384)
198 zmd2 = z2.compute_digest(dns.zone.DigestHashAlgorithm.SHA384)
199 self.assertEqual(zmd1, zmd2)