Debian Codegraph @

tests/test_zone.py

raw
1 # -*- coding: utf-8
2 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3
4 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
5 #
6 # Permission to use, copy, modify, and distribute this software and its
7 # documentation for any purpose with or without fee is hereby granted,
8 # provided that the above copyright notice and this permission notice
9 # appear in all copies.
10 #
11 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19 import difflib
20 import os
21 import sys
22 import unittest
23 from io import BytesIO, StringIO
24 from typing import cast
25
26 import dns.btreezone
27 import dns.exception
28 import dns.message
29 import dns.name
30 import dns.node
31 import dns.rdata
32 import dns.rdataclass
33 import dns.rdataset
34 import dns.rdatatype
35 import dns.rrset
36 import dns.versioned
37 import dns.zone
38 from tests.util import here
39
40 example_text = """$TTL 3600
41 $ORIGIN example.
42 @ soa foo bar 1 2 3 4 5
43 @ ns ns1
44 @ ns ns2
45 ns1 a 10.0.0.1
46 ns2 a 10.0.0.2
47 $TTL 300
48 $ORIGIN foo.example.
49 bar mx 0 blaz
50 """
51
52 example_text_output = """@ 3600 IN SOA foo bar 1 2 3 4 5
53 @ 3600 IN NS ns1
54 @ 3600 IN NS ns2
55 bar.foo 300 IN MX 0 blaz.foo
56 ns1 3600 IN A 10.0.0.1
57 ns2 3600 IN A 10.0.0.2
58 """
59
60 example_text_output_class_before_ttl = """@ IN 3600 SOA foo bar 1 2 3 4 5
61 @ 3600 NS ns1 ; no class
62 @ NS ns2 ; no class or TTL, TTL inferred from prior record
63 bar.foo IN 300 MX 0 blaz.foo
64 ns1 IN 3600 A 10.0.0.1
65 ns2 IN 3600 A 10.0.0.2
66 """
67
68 example_generate = """@ 3600 IN SOA foo bar 1 2 3 4 5
69 @ 3600 IN NS ns
70 $GENERATE 9-12 a.$ A 10.0.0.$
71 $GENERATE 80-254/173 b.${0,5,d} A 10.0.1.$
72 $GENERATE 80-254/173 c.${0,5,o} A 10.0.2.$
73 $GENERATE 80-254/173 d.${0,5,x} A 10.0.3.$
74 $GENERATE 80-254/173 e.${0,5,X} A 10.0.4.$
75 $GENERATE 80-254/173 f.${0,5,n} A 10.0.5.$
76 $GENERATE 80-254/173 g.${0,5,N} A 10.0.6.$
77 $GENERATE 218-218/1 h.${0,4,N} A 10.0.7.$
78 $GENERATE 218-218/1 i.${0,4,N}j A 10.0.8.$
79 $GENERATE 23-24 k.${1,2,d} A 10.0.9.${-1,2,d}
80 """
81
82 example_generate_output = """@ 3600 IN SOA foo bar 1 2 3 4 5
83 @ 3600 IN NS ns
84 a.9 5 IN A 10.0.0.9
85 a.10 5 IN A 10.0.0.10
86 a.11 5 IN A 10.0.0.11
87 a.12 5 IN A 10.0.0.12
88 b.00080 5 IN A 10.0.1.80
89 b.00253 5 IN A 10.0.1.253
90 c.00120 5 IN A 10.0.2.80
91 c.00375 5 IN A 10.0.2.253
92 d.00050 5 IN A 10.0.3.80
93 d.000fd 5 IN A 10.0.3.253
94 e.00050 5 IN A 10.0.4.80
95 e.000FD 5 IN A 10.0.4.253
96 f.0.5.0 5 IN A 10.0.5.80
97 f.d.f.0 5 IN A 10.0.5.253
98 g.0.5.0 5 IN A 10.0.6.80
99 g.D.F.0 5 IN A 10.0.6.253
100 i.A.D.j 5 IN A 10.0.8.218
101 k.24 5 IN A 10.0.9.22
102 k.25 5 IN A 10.0.9.23
103 """
104
105 something_quite_similar = """@ 3600 IN SOA foo bar 1 2 3 4 5
106 @ 3600 IN NS ns1
107 @ 3600 IN NS ns2
108 bar.foo 300 IN MX 0 blaz.foo
109 ns1 3600 IN A 10.0.0.1
110 ns2 3600 IN A 10.0.0.3
111 """
112
113 something_different = """@ 3600 IN SOA fooa bar 1 2 3 4 5
114 @ 3600 IN NS ns11
115 @ 3600 IN NS ns21
116 bar.fooa 300 IN MX 0 blaz.fooa
117 ns11 3600 IN A 10.0.0.11
118 ns21 3600 IN A 10.0.0.21
119 """
120
121 ttl_example_text = """$TTL 1h
122 $ORIGIN example.
123 @ soa foo bar 1 2 3 4 5
124 @ ns ns1
125 @ ns ns2
126 ns1 1d1s a 10.0.0.1
127 ns2 1w1D1h1m1S a 10.0.0.2
128 """
129
130 # No $TTL so default TTL for RRs should be inherited from SOA minimum TTL (
131 # not from the last explicit RR TTL).
132 ttl_from_soa_text = """$ORIGIN example.
133 @ 1h soa foo bar 1 2 3 4 5
134 @ 1h ns ns1
135 @ 1h ns ns2
136 ns1 1w1D1h1m1S a 10.0.0.2
137 ns2 a 10.0.0.1
138 """
139
140 # No $TTL and no SOA, so default TTL for RRs should be inherited from last
141 # explicit RR TTL.
142 ttl_from_last_text = """$ORIGIN example.
143 @ 1h ns ns1
144 @ 1h ns ns2
145 ns1 a 10.0.0.1
146 ns2 1w1D1h1m1S a 10.0.0.2
147 """
148
149 # No $TTL and no SOA should raise SyntaxError as no TTL can be determined.
150 no_ttl_text = """$ORIGIN example.
151 @ ns ns1
152 @ ns ns2
153 ns1 a 10.0.0.1
154 ns2 a 10.0.0.2
155 """
156
157 no_soa_text = """$TTL 1h
158 $ORIGIN example.
159 @ ns ns1
160 @ ns ns2
161 ns1 1d1s a 10.0.0.1
162 ns2 1w1D1h1m1S a 10.0.0.2
163 """
164
165 no_ns_text = """$TTL 1h
166 $ORIGIN example.
167 @ soa foo bar 1 2 3 4 5
168 """
169
170 include_text = """$INCLUDE "%s"
171 """ % here(
172 "example"
173 )
174
175 bad_directive_text = """$FOO bar
176 $ORIGIN example.
177 @ soa foo bar 1 2 3 4 5
178 @ ns ns1
179 @ ns ns2
180 ns1 1d1s a 10.0.0.1
181 ns2 1w1D1h1m1S a 10.0.0.2
182 """
183
184 codec_text = """
185 @ soa foo bar 1 2 3 4 5
186 @ ns ns1
187 @ ns ns2
188 Königsgäßchen 300 NS Königsgäßchen
189 """
190
191 misc_cases_input = """
192 $ORIGIN example.
193 $TTL 300
194
195 @ soa foo bar 1 2 3 4 5
196 @ ns ns1
197 @ ns ns2
198 out-of-zone. in a 10.0.0.1
199 """
200
201 misc_cases_expected = """
202 $ORIGIN example.
203 $TTL 300
204 @ soa foo bar 1 2 3 4 5
205 @ ns ns1
206 @ ns ns2
207 """
208
209 last_ttl_input = """
210 $ORIGIN example.
211 @ 300 ns ns1
212 @ 300 ns ns2
213 foo a 10.0.0.1
214 @ soa foo bar 1 2 3 4 5
215 """
216
217 origin_sets_input = """
218 $ORIGIN example.
219 @ soa foo bar 1 2 3 4 5
220 @ 300 ns ns1
221 @ 300 ns ns2
222 """
223
224 example_comments_text = """$TTL 3600
225 $ORIGIN example.
226 @ soa foo bar (1 ; not kept
227 2 3 4 5) ; kept
228 @ ns ns1
229 @ ns ns2
230 ns1 a 10.0.0.1 ; comment1
231 ns2 a 10.0.0.2 ; comment2
232 """
233
234 example_comments_text_output = """@ 3600 IN SOA foo bar 1 2 3 4 5 ; kept
235 @ 3600 IN NS ns1
236 @ 3600 IN NS ns2
237 ns1 3600 IN A 10.0.0.1 ; comment1
238 ns2 3600 IN A 10.0.0.2 ; comment2
239 """
240
241
242 example_cname = """$TTL 3600
243 $ORIGIN example.
244 @ soa foo bar (1 2 3 4 5)
245 @ ns ns1
246 @ ns ns2
247 ns1 a 10.0.0.1
248 ns2 a 10.0.0.2
249 www a 10.0.0.3
250 web cname www
251 nsec @ CNAME RRSIG
252 rrsig NSEC 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
253 rrsig CNAME 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
254 web2 cname www
255 nsec3 1 1 12 aabbccdd 2t7b4g4vsa5smi47k61mv5bv1a22bojr CNAME RRSIG
256 rrsig NSEC3 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
257 rrsig CNAME 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
258 """
259
260
261 example_other_data = """$TTL 3600
262 $ORIGIN example.
263 @ soa foo bar (1 2 3 4 5)
264 @ ns ns1
265 @ ns ns2
266 ns1 a 10.0.0.1
267 ns2 a 10.0.0.2
268 www a 10.0.0.3
269 web a 10.0.0.4
270 nsec @ A RRSIG
271 rrsig A 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
272 rrsig NSEC 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
273 """
274
275 example_cname_and_other_data = """$TTL 3600
276 $ORIGIN example.
277 @ soa foo bar (1 2 3 4 5)
278 @ ns ns1
279 @ ns ns2
280 ns1 a 10.0.0.1
281 ns2 a 10.0.0.2
282 www a 10.0.0.3
283 web a 10.0.0.4
284 cname www
285 nsec @ A RRSIG
286 rrsig A 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
287 rrsig NSEC 1 3 3600 20200101000000 20030101000000 2143 foo MxFcby9k/yvedMfQgKzhH5er0Mu/vILz 45IkskceFGgiWCn/GxHhai6VAuHAoNUz 4YoU1tVfSCSqQYn6//11U6Nld80jEeC8 aTrO+KKmCaY=
288 """
289
290 _keep_output = True
291
292
293 def _rdata_sort(a):
294 return (a[0], a[2].rdclass, a[2].to_text())
295
296
297 def add_rdataset(msg, name, rds):
298 rrset = msg.get_rrset(
299 msg.answer, name, rds.rdclass, rds.rdtype, create=True, force_unique=True
300 )
301 for rd in rds:
302 rrset.add(rd, ttl=rds.ttl)
303
304
305 def make_xfr(zone):
306 q = dns.message.make_query(zone.origin, "AXFR")
307 msg = dns.message.make_response(q)
308 if zone.relativize:
309 msg.origin = zone.origin
310 soa_name = dns.name.empty
311 else:
312 soa_name = zone.origin
313 soa = zone.find_rdataset(soa_name, "SOA")
314 add_rdataset(msg, soa_name, soa)
315 for name, rds in zone.iterate_rdatasets():
316 if rds.rdtype == dns.rdatatype.SOA:
317 continue
318 add_rdataset(msg, name, rds)
319 add_rdataset(msg, soa_name, soa)
320 return [msg]
321
322
323 def compare_files(test_name, a_name, b_name):
324 with open(a_name, encoding="utf-8") as a:
325 with open(b_name, encoding="utf-8") as b:
326 differences = list(difflib.unified_diff(a.readlines(), b.readlines()))
327 if len(differences) == 0:
328 return True
329 else:
330 print(f"{test_name} differences:")
331 sys.stdout.writelines(differences)
332 return False
333
334
335 class ZoneTestCase(unittest.TestCase):
336 def testFromFile1(self):
337 z = dns.zone.from_file(here("example"), "example")
338 ok = False
339 try:
340 z.to_file(here("example1.out"), nl=b"\x0a")
341 ok = compare_files(
342 "testFromFile1", here("example1.out"), here("example1.good")
343 )
344 finally:
345 if not _keep_output:
346 os.unlink(here("example1.out"))
347 self.assertTrue(ok)
348
349 def testFromFile2(self):
350 z = dns.zone.from_file(here("example"), "example", relativize=False)
351 ok = False
352 try:
353 z.to_file(here("example2.out"), relativize=False, nl=b"\x0a")
354 ok = compare_files(
355 "testFromFile2", here("example2.out"), here("example2.good")
356 )
357 finally:
358 if not _keep_output:
359 os.unlink(here("example2.out"))
360 self.assertTrue(ok)
361
362 def testToFileTextualStream(self):
363 z = dns.zone.from_text(example_text, "example.", relativize=True)
364 f = StringIO()
365 z.to_file(f)
366 out = f.getvalue()
367 f.close()
368 self.assertEqual(out, example_text_output)
369
370 def testToFileBinaryStream(self):
371 z = dns.zone.from_text(example_text, "example.", relativize=True)
372 f = BytesIO()
373 z.to_file(f, nl=b"\n")
374 out = f.getvalue()
375 f.close()
376 self.assertEqual(out, example_text_output.encode())
377
378 def testToFileTextual(self):
379 z = dns.zone.from_file(here("example"), "example")
380 try:
381 f = open(here("example3-textual.out"), "wt", encoding="utf-8")
382 z.to_file(f)
383 f.close()
384 ok = compare_files(
385 "testToFileTextual", here("example3-textual.out"), here("example3.good")
386 )
387 finally:
388 if not _keep_output:
389 os.unlink(here("example3-textual.out"))
390 self.assertTrue(ok)
391
392 def testToFileBinary(self):
393 z = dns.zone.from_file(here("example"), "example")
394 try:
395 f = open(here("example3-binary.out"), "wb")
396 z.to_file(f)
397 f.close()
398 ok = compare_files(
399 "testToFileBinary", here("example3-binary.out"), here("example3.good")
400 )
401 finally:
402 if not _keep_output:
403 os.unlink(here("example3-binary.out"))
404 self.assertTrue(ok)
405
406 def testToFileFilename(self):
407 z = dns.zone.from_file(here("example"), "example")
408 try:
409 z.to_file(here("example3-filename.out"))
410 ok = compare_files(
411 "testToFileFilename",
412 here("example3-filename.out"),
413 here("example3.good"),
414 )
415 finally:
416 if not _keep_output:
417 os.unlink(here("example3-filename.out"))
418 self.assertTrue(ok)
419
420 def testToText(self):
421 z = dns.zone.from_file(here("example"), "example")
422 ok = False
423 try:
424 text_zone = z.to_text(nl="\x0a")
425 f = open(here("example3.out"), "wt", encoding="utf-8")
426 f.write(text_zone)
427 f.close()
428 ok = compare_files(
429 "testToText", here("example3.out"), here("example3.good")
430 )
431 finally:
432 if not _keep_output:
433 os.unlink(here("example3.out"))
434 self.assertTrue(ok)
435
436 def testToFileTextualWithOrigin(self):
437 z = dns.zone.from_file(here("example"), "example")
438 try:
439 f = open(here("example4-textual.out"), "wt", encoding="utf-8")
440 z.to_file(f, want_origin=True)
441 f.close()
442 ok = compare_files(
443 "testToFileTextualWithOrigin",
444 here("example4-textual.out"),
445 here("example4.good"),
446 )
447 finally:
448 if not _keep_output:
449 os.unlink(here("example4-textual.out"))
450 self.assertTrue(ok)
451
452 def testToFileBinaryWithOrigin(self):
453 z = dns.zone.from_file(here("example"), "example")
454 try:
455 f = open(here("example4-binary.out"), "wb")
456 z.to_file(f, want_origin=True)
457 f.close()
458 ok = compare_files(
459 "testToFileBinaryWithOrigin",
460 here("example4-binary.out"),
461 here("example4.good"),
462 )
463 finally:
464 if not _keep_output:
465 os.unlink(here("example4-binary.out"))
466 self.assertTrue(ok)
467
468 def testFromText(self):
469 z = dns.zone.from_text(example_text, "example.", relativize=True)
470 f = StringIO()
471 names = list(z.nodes.keys())
472 names.sort()
473 for n in names:
474 f.write(z[n].to_text(n))
475 f.write("\n")
476 self.assertEqual(f.getvalue(), example_text_output)
477
478 def testGenerate(self):
479 z = dns.zone.from_text(example_generate, "example.", relativize=True)
480 f = StringIO()
481 expected = dns.zone.from_text(
482 example_generate_output, "example.", relativize=True
483 )
484 self.assertEqual(z, expected)
485
486 def testTorture1(self):
487 #
488 # Read a zone containing all our supported RR types, and
489 # for each RR in the zone, convert the rdata into wire format
490 # and then back out, and see if we get equal rdatas.
491 #
492 f = BytesIO()
493 o = dns.name.from_text("example.")
494 z = dns.zone.from_file(here("example"), o)
495 for node in z.values():
496 for rds in node:
497 for rd in rds:
498 f.seek(0)
499 f.truncate()
500 rd.to_wire(f, origin=o)
501 wire = f.getvalue()
502 rd2 = dns.rdata.from_wire(
503 rds.rdclass, rds.rdtype, wire, 0, len(wire), origin=o
504 )
505 self.assertEqual(rd, rd2)
506
507 def testEqual(self):
508 z1 = dns.zone.from_text(example_text, "example.", relativize=True)
509 z2 = dns.zone.from_text(example_text_output, "example.", relativize=True)
510 self.assertEqual(z1, z2)
511
512 def testEqualClassBeforeTTL(self):
513 z1 = dns.zone.from_text(
514 example_text_output_class_before_ttl, "example.", relativize=True
515 )
516 z2 = dns.zone.from_text(example_text_output, "example.", relativize=True)
517 self.assertEqual(z1, z2)
518
519 def testNotEqual1(self):
520 z1 = dns.zone.from_text(example_text, "example.", relativize=True)
521 z2 = dns.zone.from_text(something_quite_similar, "example.", relativize=True)
522 self.assertNotEqual(z1, z2)
523
524 def testNotEqual2(self):
525 z1 = dns.zone.from_text(example_text, "example.", relativize=True)
526 z2 = dns.zone.from_text(something_different, "example.", relativize=True)
527 self.assertNotEqual(z1, z2)
528
529 def testNotEqual3(self):
530 z1 = dns.zone.from_text(example_text, "example.", relativize=True)
531 z2 = dns.zone.from_text(something_different, "example2.", relativize=True)
532 self.assertNotEqual(z1, z2)
533
534 def testFindRdataset1(self):
535 z = dns.zone.from_text(example_text, "example.", relativize=True)
536 rds = z.find_rdataset("@", "soa")
537 exrds = dns.rdataset.from_text("IN", "SOA", 300, "foo bar 1 2 3 4 5")
538 self.assertEqual(rds, exrds)
539
540 def testFindRdataset2(self):
541 def bad():
542 z = dns.zone.from_text(example_text, "example.", relativize=True)
543 z.find_rdataset("@", "loc")
544
545 self.assertRaises(KeyError, bad)
546
547 def testFindRRset1(self):
548 z = dns.zone.from_text(example_text, "example.", relativize=True)
549 rrs = z.find_rrset("@", "soa")
550 exrrs = dns.rrset.from_text("@", 300, "IN", "SOA", "foo bar 1 2 3 4 5")
551 self.assertEqual(rrs, exrrs)
552
553 def testFindRRset2(self):
554 def bad():
555 z = dns.zone.from_text(example_text, "example.", relativize=True)
556 z.find_rrset("@", "loc")
557
558 self.assertRaises(KeyError, bad)
559
560 def testGetRdataset1(self):
561 z = dns.zone.from_text(example_text, "example.", relativize=True)
562 rds = z.get_rdataset("@", "soa")
563 exrds = dns.rdataset.from_text("IN", "SOA", 300, "foo bar 1 2 3 4 5")
564 self.assertEqual(rds, exrds)
565
566 def testGetRdataset2(self):
567 z = dns.zone.from_text(example_text, "example.", relativize=True)
568 rds = z.get_rdataset("@", "loc")
569 self.assertTrue(rds is None)
570
571 def testGetRdatasetWithRelativeNameFromAbsoluteZone(self):
572 z = dns.zone.from_text(example_text, "example.", relativize=False)
573 rds = z.get_rdataset(dns.name.empty, "soa")
574 self.assertIsNotNone(rds)
575 exrds = dns.rdataset.from_text(
576 "IN", "SOA", 300, "foo.example. bar.example. 1 2 3 4 5"
577 )
578 self.assertEqual(rds, exrds)
579
580 def testGetRRset1(self):
581 z = dns.zone.from_text(example_text, "example.", relativize=True)
582 rrs = z.get_rrset("@", "soa")
583 exrrs = dns.rrset.from_text("@", 300, "IN", "SOA", "foo bar 1 2 3 4 5")
584 self.assertEqual(rrs, exrrs)
585
586 def testGetRRset2(self):
587 z = dns.zone.from_text(example_text, "example.", relativize=True)
588 rrs = z.get_rrset("@", "loc")
589 self.assertTrue(rrs is None)
590
591 def testReplaceRdataset1(self):
592 z = dns.zone.from_text(example_text, "example.", relativize=True)
593 rdataset = dns.rdataset.from_text("in", "ns", 300, "ns3", "ns4")
594 z.replace_rdataset("@", rdataset)
595 rds = z.get_rdataset("@", "ns")
596 self.assertTrue(rds is rdataset)
597
598 def testReplaceRdataset2(self):
599 z = dns.zone.from_text(example_text, "example.", relativize=True)
600 rdataset = dns.rdataset.from_text("in", "txt", 300, '"foo"')
601 z.replace_rdataset("@", rdataset)
602 rds = z.get_rdataset("@", "txt")
603 self.assertTrue(rds is rdataset)
604
605 def testDeleteRdataset1(self):
606 z = dns.zone.from_text(example_text, "example.", relativize=True)
607 z.delete_rdataset("@", "ns")
608 rds = z.get_rdataset("@", "ns")
609 self.assertTrue(rds is None)
610
611 def testDeleteRdataset2(self):
612 z = dns.zone.from_text(example_text, "example.", relativize=True)
613 z.delete_rdataset("ns1", "a")
614 node = z.get_node("ns1")
615 self.assertTrue(node is None)
616
617 def testNodeFindRdataset1(self):
618 z = dns.zone.from_text(example_text, "example.", relativize=True)
619 node = z["@"]
620 rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
621 exrds = dns.rdataset.from_text("IN", "SOA", 300, "foo bar 1 2 3 4 5")
622 self.assertEqual(rds, exrds)
623
624 def testNodeFindRdataset2(self):
625 def bad():
626 z = dns.zone.from_text(example_text, "example.", relativize=True)
627 node = z["@"]
628 node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC)
629
630 self.assertRaises(KeyError, bad)
631
632 def testNodeFindRdataset3(self):
633 z = dns.zone.from_text(example_text, "example.", relativize=True)
634 node = z["@"]
635 rds = node.find_rdataset(
636 dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.A, create=True
637 )
638 self.assertEqual(rds.rdclass, dns.rdataclass.IN)
639 self.assertEqual(rds.rdtype, dns.rdatatype.RRSIG)
640 self.assertEqual(rds.covers, dns.rdatatype.A)
641
642 def testNodeGetRdataset1(self):
643 z = dns.zone.from_text(example_text, "example.", relativize=True)
644 node = z["@"]
645 rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
646 exrds = dns.rdataset.from_text("IN", "SOA", 300, "foo bar 1 2 3 4 5")
647 self.assertEqual(rds, exrds)
648
649 def testNodeGetRdataset2(self):
650 z = dns.zone.from_text(example_text, "example.", relativize=True)
651 node = z["@"]
652 rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC)
653 self.assertTrue(rds is None)
654
655 def testNodeDeleteRdataset1(self):
656 z = dns.zone.from_text(example_text, "example.", relativize=True)
657 node = z["@"]
658 node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
659 rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
660 self.assertTrue(rds is None)
661
662 def testNodeDeleteRdataset2(self):
663 z = dns.zone.from_text(example_text, "example.", relativize=True)
664 node = z["@"]
665 node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC)
666 rds = node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.LOC)
667 self.assertTrue(rds is None)
668
669 def testIterateNodes(self):
670 z = dns.zone.from_text(example_text, "example.", relativize=True)
671 count = 0
672 for n in z:
673 count += 1
674 self.assertEqual(count, 4)
675
676 def testIterateRdatasets(self):
677 z = dns.zone.from_text(example_text, "example.", relativize=True)
678 ns = [n for n, r in z.iterate_rdatasets("A")]
679 ns.sort()
680 self.assertEqual(
681 ns, [dns.name.from_text("ns1", None), dns.name.from_text("ns2", None)]
682 )
683
684 def testIterateAllRdatasets(self):
685 z = dns.zone.from_text(example_text, "example.", relativize=True)
686 ns = [n for n, r in z.iterate_rdatasets()]
687 ns.sort()
688 self.assertEqual(
689 ns,
690 [
691 dns.name.from_text("@", None),
692 dns.name.from_text("@", None),
693 dns.name.from_text("bar.foo", None),
694 dns.name.from_text("ns1", None),
695 dns.name.from_text("ns2", None),
696 ],
697 )
698
699 def testIterateRdatas(self):
700 z = dns.zone.from_text(example_text, "example.", relativize=True)
701 l = list(z.iterate_rdatas("A"))
702 l.sort()
703 exl = [
704 (
705 dns.name.from_text("ns1", None),
706 3600,
707 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.1"),
708 ),
709 (
710 dns.name.from_text("ns2", None),
711 3600,
712 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.2"),
713 ),
714 ]
715 self.assertEqual(l, exl)
716
717 def testIterateAllRdatas(self):
718 z = dns.zone.from_text(example_text, "example.", relativize=True)
719 l = list(z.iterate_rdatas())
720 l.sort(key=_rdata_sort)
721 exl = [
722 (
723 dns.name.from_text("@", None),
724 3600,
725 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, "ns1"),
726 ),
727 (
728 dns.name.from_text("@", None),
729 3600,
730 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.NS, "ns2"),
731 ),
732 (
733 dns.name.from_text("@", None),
734 3600,
735 dns.rdata.from_text(
736 dns.rdataclass.IN, dns.rdatatype.SOA, "foo bar 1 2 3 4 5"
737 ),
738 ),
739 (
740 dns.name.from_text("bar.foo", None),
741 300,
742 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.MX, "0 blaz.foo"),
743 ),
744 (
745 dns.name.from_text("ns1", None),
746 3600,
747 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.1"),
748 ),
749 (
750 dns.name.from_text("ns2", None),
751 3600,
752 dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.A, "10.0.0.2"),
753 ),
754 ]
755 exl.sort(key=_rdata_sort)
756 self.assertEqual(l, exl)
757
758 def testNodeGetSetDel(self):
759 z = dns.zone.from_text(example_text, "example.", relativize=True)
760 n = z.node_factory()
761 rds = dns.rdataset.from_text("IN", "A", 300, "10.0.0.1")
762 n.replace_rdataset(rds)
763 z["foo"] = n
764 self.assertTrue(z.find_rdataset("foo", "A") is rds)
765 self.assertEqual(z["foo"], n)
766 self.assertEqual(z.get("foo"), n)
767 del z["foo"]
768 self.assertEqual(z.get("foo"), None)
769 with self.assertRaises(KeyError):
770 z[123] = n
771 with self.assertRaises(KeyError):
772 z["foo."] = n
773 with self.assertRaises(KeyError):
774 bn = z.find_node("bar")
775 bn = z.find_node("bar", True)
776 self.assertTrue(isinstance(bn, dns.node.Node))
777 # The next two tests pass by not raising KeyError
778 z.delete_node("foo")
779 z.delete_node("bar")
780
781 def testBadReplacement(self):
782 z = dns.zone.from_text(example_text, "example.", relativize=True)
783 rds = dns.rdataset.from_text("CH", "TXT", 300, "hi")
784
785 def bad():
786 z.replace_rdataset("foo", rds)
787
788 self.assertRaises(ValueError, bad)
789
790 def testTTLs(self):
791 z = dns.zone.from_text(ttl_example_text, "example.", relativize=True)
792 n = z["@"] # type: dns.node.Node
793 rds = cast(
794 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
795 )
796 self.assertEqual(rds.ttl, 3600)
797 n = z["ns1"]
798 rds = cast(
799 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
800 )
801 self.assertEqual(rds.ttl, 86401)
802 n = z["ns2"]
803 rds = cast(
804 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
805 )
806 self.assertEqual(rds.ttl, 694861)
807
808 def testTTLFromSOA(self):
809 z = dns.zone.from_text(ttl_from_soa_text, "example.", relativize=True)
810 n = z["@"]
811 rds = cast(
812 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
813 )
814 self.assertEqual(rds.ttl, 3600)
815 soa_rd = rds[0]
816 n = z["ns1"]
817 rds = cast(
818 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
819 )
820 self.assertEqual(rds.ttl, 694861)
821 n = z["ns2"]
822 rds = cast(
823 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
824 )
825 self.assertEqual(rds.ttl, soa_rd.minimum)
826
827 def testTTLFromLast(self):
828 z = dns.zone.from_text(ttl_from_last_text, "example.", check_origin=False)
829 n = z["@"]
830 rds = cast(
831 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NS)
832 )
833 self.assertEqual(rds.ttl, 3600)
834 n = z["ns1"]
835 rds = cast(
836 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
837 )
838 self.assertEqual(rds.ttl, 3600)
839 n = z["ns2"]
840 rds = cast(
841 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
842 )
843 self.assertEqual(rds.ttl, 694861)
844
845 def testNoTTL(self):
846 def bad():
847 dns.zone.from_text(no_ttl_text, "example.", check_origin=False)
848
849 self.assertRaises(dns.exception.SyntaxError, bad)
850
851 def testNoSOA(self):
852 def bad():
853 dns.zone.from_text(no_soa_text, "example.", relativize=True)
854
855 self.assertRaises(dns.zone.NoSOA, bad)
856
857 def testNoNS(self):
858 def bad():
859 dns.zone.from_text(no_ns_text, "example.", relativize=True)
860
861 self.assertRaises(dns.zone.NoNS, bad)
862
863 def testInclude(self):
864 z1 = dns.zone.from_text(
865 include_text, "example.", relativize=True, allow_include=True
866 )
867 z2 = dns.zone.from_file(here("example"), "example.", relativize=True)
868 self.assertEqual(z1, z2)
869
870 def testNoInclude(self):
871 def bad():
872 dns.zone.from_text(
873 include_text, "example.", relativize=True, allow_include=False
874 )
875
876 self.assertRaises(dns.exception.SyntaxError, bad)
877
878 def testExplicitInclude(self):
879 z1 = dns.zone.from_text(
880 include_text,
881 "example.",
882 relativize=True,
883 allow_directives={"$INCLUDE", "$ORIGIN", "$TTL"},
884 )
885 z2 = dns.zone.from_file(here("example"), "example.", relativize=True)
886 self.assertEqual(z1, z2)
887
888 def testExplicitIncludeNotUpperNoDollar(self):
889 z1 = dns.zone.from_text(
890 include_text,
891 "example.",
892 relativize=True,
893 allow_directives={"InClUdE", "origin", "TTL"},
894 )
895 z2 = dns.zone.from_file(here("example"), "example.", relativize=True)
896 self.assertEqual(z1, z2)
897
898 def testExplicitLowerCase(self):
899 z1 = dns.zone.from_text(
900 include_text,
901 "example.",
902 relativize=True,
903 allow_directives={"$include", "$origin", "$ttl"},
904 )
905 z2 = dns.zone.from_file(here("example"), "example.", relativize=True)
906 self.assertEqual(z1, z2)
907
908 def testExplicitWithoutInclude1(self):
909 def bad():
910 dns.zone.from_text(
911 include_text,
912 "example.",
913 relativize=True,
914 allow_include=False,
915 allow_directives={"$ORIGIN", "$TTL"},
916 )
917
918 self.assertRaises(dns.exception.SyntaxError, bad)
919
920 def testExplicitWithoutInclude2(self):
921 def bad():
922 dns.zone.from_text(
923 include_text,
924 "example.",
925 relativize=True,
926 allow_include=True,
927 allow_directives={"$ORIGIN", "$TTL"},
928 )
929
930 self.assertRaises(dns.exception.SyntaxError, bad)
931
932 def testBadDirective(self):
933 def bad():
934 dns.zone.from_text(bad_directive_text, "example.", relativize=True)
935
936 self.assertRaises(dns.exception.SyntaxError, bad)
937
938 def testAllowedButNotImplementedDirective(self):
939 def bad():
940 dns.zone.from_text(
941 bad_directive_text,
942 "example.",
943 relativize=True,
944 allow_directives={"$FOO", "$ORIGIN"},
945 )
946
947 self.assertRaises(dns.exception.SyntaxError, bad)
948
949 def testFirstRRStartsWithWhitespace(self):
950 # no name is specified, so default to the initial origin
951 z = dns.zone.from_text(
952 " 300 IN A 10.0.0.1", origin="example.", check_origin=False
953 )
954 n = z["@"]
955 rds = cast(
956 dns.rdataset.Rdataset, n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
957 )
958 self.assertEqual(rds.ttl, 300)
959
960 def testZoneOrigin(self):
961 z = dns.zone.Zone("example.")
962 self.assertEqual(z.origin, dns.name.from_text("example."))
963
964 def bad1():
965 o = dns.name.from_text("example", None)
966 dns.zone.Zone(o)
967
968 self.assertRaises(ValueError, bad1)
969
970 def bad2():
971 dns.zone.Zone(cast(str, 1.0))
972
973 self.assertRaises(ValueError, bad2)
974
975 def testZoneOriginNone(self):
976 dns.zone.Zone(cast(str, None))
977
978 def testZoneFromXFR(self):
979 z1_abs = dns.zone.from_text(example_text, "example.", relativize=False)
980 z2_abs = dns.zone.from_xfr(make_xfr(z1_abs), relativize=False)
981 self.assertEqual(z1_abs, z2_abs)
982
983 z1_rel = dns.zone.from_text(example_text, "example.", relativize=True)
984 z2_rel = dns.zone.from_xfr(make_xfr(z1_rel), relativize=True)
985 self.assertEqual(z1_rel, z2_rel)
986
987 def testCodec2003(self):
988 z = dns.zone.from_text(codec_text, "example.", relativize=True)
989 n2003 = dns.name.from_text("xn--knigsgsschen-lcb0w", None)
990 n2008 = dns.name.from_text("xn--knigsgchen-b4a3dun", None)
991 self.assertTrue(n2003 in z)
992 self.assertFalse(n2008 in z)
993 rrs = z.find_rrset(n2003, "NS")
994 self.assertEqual(rrs[0].target, n2003)
995
996 @unittest.skipUnless(
997 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
998 )
999 def testCodec2008(self):
1000 z = dns.zone.from_text(
1001 codec_text, "example.", relativize=True, idna_codec=dns.name.IDNA_2008
1002 )
1003 n2003 = dns.name.from_text("xn--knigsgsschen-lcb0w", None)
1004 n2008 = dns.name.from_text("xn--knigsgchen-b4a3dun", None)
1005 self.assertFalse(n2003 in z)
1006 self.assertTrue(n2008 in z)
1007 rrs = z.find_rrset(n2008, "NS")
1008 self.assertEqual(rrs[0].target, n2008)
1009
1010 def testZoneMiscCases(self):
1011 # test that leading whitespace followed by EOL is treated like
1012 # a blank line, and that out-of-zone names are dropped.
1013 z1 = dns.zone.from_text(misc_cases_input, "example.")
1014 z2 = dns.zone.from_text(misc_cases_expected, "example.")
1015 self.assertEqual(z1, z2)
1016
1017 def testUnknownOrigin(self):
1018 def bad():
1019 dns.zone.from_text("foo 300 in a 10.0.0.1")
1020
1021 self.assertRaises(dns.zone.UnknownOrigin, bad)
1022
1023 def testBadClass(self):
1024 def bad():
1025 dns.zone.from_text("foo 300 ch txt hi", "example.")
1026
1027 self.assertRaises(dns.exception.SyntaxError, bad)
1028
1029 def testUnknownRdatatype(self):
1030 def bad():
1031 dns.zone.from_text("foo 300 BOGUSTYPE hi", "example.")
1032
1033 self.assertRaises(dns.exception.SyntaxError, bad)
1034
1035 def testDangling(self):
1036 def bad1():
1037 dns.zone.from_text("foo", "example.")
1038
1039 self.assertRaises(dns.exception.SyntaxError, bad1)
1040
1041 def bad2():
1042 dns.zone.from_text("foo 300", "example.")
1043
1044 self.assertRaises(dns.exception.SyntaxError, bad2)
1045
1046 def bad3():
1047 dns.zone.from_text("foo 300 in", "example.")
1048
1049 self.assertRaises(dns.exception.SyntaxError, bad3)
1050
1051 def bad4():
1052 dns.zone.from_text("foo 300 in a", "example.")
1053
1054 self.assertRaises(dns.exception.SyntaxError, bad4)
1055
1056 def bad5():
1057 dns.zone.from_text("$TTL", "example.")
1058
1059 self.assertRaises(dns.exception.SyntaxError, bad5)
1060
1061 def bad6():
1062 dns.zone.from_text("$ORIGIN", "example.")
1063
1064 self.assertRaises(dns.exception.SyntaxError, bad6)
1065
1066 def testUseLastTTL(self):
1067 z = dns.zone.from_text(last_ttl_input, "example.")
1068 rds = z.find_rdataset("foo", "A")
1069 self.assertEqual(rds.ttl, 300)
1070
1071 def testDollarOriginSetsZoneOriginIfUnknown(self):
1072 z = dns.zone.from_text(origin_sets_input)
1073 self.assertEqual(z.origin, dns.name.from_text("example"))
1074
1075 def testValidateNameRelativizesNameInZone(self):
1076 z = dns.zone.from_text(example_text, "example.", relativize=True)
1077 self.assertEqual(
1078 z._validate_name("foo.bar.example."), dns.name.from_text("foo.bar", None)
1079 )
1080
1081 def testComments(self):
1082 z = dns.zone.from_text(example_comments_text, "example.", relativize=True)
1083 f = StringIO()
1084 z.to_file(f, want_comments=True)
1085 out = f.getvalue()
1086 f.close()
1087 self.assertEqual(out, example_comments_text_output)
1088
1089 def testUncomparable(self):
1090 z = dns.zone.from_text(example_comments_text, "example.", relativize=True)
1091 self.assertFalse(z == "a")
1092
1093 def testUnsorted(self):
1094 z = dns.zone.from_text(example_text, "example.", relativize=True)
1095 f = StringIO()
1096 z.to_file(f, sorted=False)
1097 out = f.getvalue()
1098 f.close()
1099 z2 = dns.zone.from_text(out, "example.", relativize=True)
1100 self.assertEqual(z, z2)
1101
1102 def testNodeReplaceRdatasetConvertsRRsets(self):
1103 node = dns.node.Node()
1104 rrs = dns.rrset.from_text("foo", 300, "in", "a", "10.0.0.1")
1105 node.replace_rdataset(rrs)
1106 rds = node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.A)
1107 self.assertEqual(rds, rrs)
1108 self.assertTrue(rds is not rrs)
1109 self.assertFalse(isinstance(rds, dns.rrset.RRset))
1110
1111 def testCnameAndOtherDataAddOther(self):
1112 z = dns.zone.from_text(example_cname, "example.", relativize=True)
1113 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1")
1114 z.replace_rdataset("web", rds)
1115 z.replace_rdataset("web2", rds.copy())
1116 n = z.find_node("web")
1117 self.assertEqual(len(n.rdatasets), 3)
1118 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.A), rds)
1119 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC))
1120 self.assertIsNotNone(
1121 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC)
1122 )
1123 n = z.find_node("web2")
1124 self.assertEqual(len(n.rdatasets), 3)
1125 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.A), rds)
1126 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC3))
1127 self.assertIsNotNone(
1128 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC3)
1129 )
1130
1131 def testCnameAndOtherDataAddCname(self):
1132 z = dns.zone.from_text(example_other_data, "example.", relativize=True)
1133 rds = dns.rdataset.from_text("in", "cname", 300, "www")
1134 z.replace_rdataset("web", rds)
1135 n = z.find_node("web")
1136 self.assertEqual(len(n.rdatasets), 3)
1137 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.CNAME), rds)
1138 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC))
1139 self.assertIsNotNone(
1140 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC)
1141 )
1142
1143 def testCnameAndOtherDataInZonefile(self):
1144 with self.assertRaises(dns.zonefile.CNAMEAndOtherData):
1145 dns.zone.from_text(
1146 example_cname_and_other_data, "example.", relativize=True
1147 )
1148
1149 def testNameInZoneWithStr(self):
1150 z = dns.zone.from_text(example_text, "example.", relativize=False)
1151 self.assertTrue("ns1.example." in z)
1152 self.assertTrue("bar.foo.example." in z)
1153
1154 def testNameInZoneWhereNameIsNotValid(self):
1155 z = dns.zone.from_text(example_text, "example.", relativize=False)
1156 with self.assertRaises(KeyError):
1157 self.assertTrue(1 in z)
1158
1159 def testRelativeNameLengthChecks(self):
1160 z = dns.zone.from_text(example_cname, "example.", relativize=True)
1161 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1")
1162 # This name is 246 bytes long, which along with the 8 bytes for label "example"
1163 # and 1 byte for the root name is 246 + 8 + 1 = 255 bytes long, the maximum
1164 # legal wire format name length.
1165 ok_long_relative = dns.name.Name(["a" * 63, "a" * 63, "a" * 63, "a" * 53])
1166 z.replace_rdataset(ok_long_relative, rds)
1167 self.assertEqual(z.find_rdataset(ok_long_relative, "A"), rds)
1168 # This is the longest possible relative name and won't work in any zone
1169 # as there is no space left for any origin labels.
1170 too_long_relative = dns.name.Name(["a" * 63, "a" * 63, "a" * 63, "a" * 62])
1171 with self.assertRaises(KeyError):
1172 z.replace_rdataset(too_long_relative, rds)
1173
1174
1175 class VersionedZoneTestCase(unittest.TestCase):
1176 zone_factory = dns.versioned.Zone
1177
1178 def testUseTransaction(self):
1179 z = dns.zone.from_text(
1180 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1181 )
1182 with self.assertRaises(dns.versioned.UseTransaction):
1183 z.find_node("not_there", True)
1184 with self.assertRaises(dns.versioned.UseTransaction):
1185 z.delete_node("not_there")
1186 with self.assertRaises(dns.versioned.UseTransaction):
1187 z.find_rdataset("not_there", "a", create=True)
1188 with self.assertRaises(dns.versioned.UseTransaction):
1189 z.get_rdataset("not_there", "a", create=True)
1190 with self.assertRaises(dns.versioned.UseTransaction):
1191 z.delete_rdataset("not_there", "a")
1192 with self.assertRaises(dns.versioned.UseTransaction):
1193 z.replace_rdataset("not_there", None)
1194
1195 def testImmutableNodes(self):
1196 z = dns.zone.from_text(
1197 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1198 )
1199 node = z.find_node("@")
1200 with self.assertRaises(TypeError):
1201 node.find_rdataset(dns.rdataclass.IN, dns.rdatatype.RP, create=True)
1202 with self.assertRaises(TypeError):
1203 node.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RP, create=True)
1204 with self.assertRaises(TypeError):
1205 node.delete_rdataset(dns.rdataclass.IN, dns.rdatatype.SOA)
1206 with self.assertRaises(TypeError):
1207 node.replace_rdataset(None)
1208
1209 def testSelectDefaultPruningPolicy(self):
1210 z = dns.zone.from_text(
1211 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1212 )
1213 z.set_pruning_policy(None)
1214 self.assertEqual(z._pruning_policy, z._default_pruning_policy)
1215
1216 def testSetAlternatePruningPolicyInConstructor(self):
1217 def never_prune(version):
1218 return False
1219
1220 z = dns.versioned.Zone("example", pruning_policy=never_prune)
1221 self.assertEqual(z._pruning_policy, never_prune)
1222
1223 def testCannotSpecifyBothSerialAndVersionIdToReader(self):
1224 z = dns.zone.from_text(
1225 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1226 )
1227 with self.assertRaises(ValueError):
1228 z.reader(1, 1)
1229
1230 def testUnknownVersion(self):
1231 z = dns.zone.from_text(
1232 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1233 )
1234 with self.assertRaises(KeyError):
1235 z.reader(99999)
1236
1237 def testUnknownSerial(self):
1238 z = dns.zone.from_text(
1239 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1240 )
1241 with self.assertRaises(KeyError):
1242 z.reader(serial=99999)
1243
1244 def testNoRelativizeReader(self):
1245 z = dns.zone.from_text(
1246 example_text, "example.", relativize=False, zone_factory=self.zone_factory
1247 )
1248 with z.reader(serial=1) as txn:
1249 rds = txn.get("example.", "soa")
1250 self.assertEqual(rds[0].serial, 1)
1251
1252 def testNoRelativizeReaderOriginInText(self):
1253 z = dns.zone.from_text(
1254 example_text, relativize=False, zone_factory=self.zone_factory
1255 )
1256 with z.reader(serial=1) as txn:
1257 rds = txn.get("example.", "soa")
1258 self.assertEqual(rds[0].serial, 1)
1259
1260 def testNoRelativizeReaderAbsoluteGet(self):
1261 z = dns.zone.from_text(
1262 example_text, "example.", relativize=False, zone_factory=self.zone_factory
1263 )
1264 with z.reader(serial=1) as txn:
1265 rds = txn.get(dns.name.empty, "soa")
1266 self.assertEqual(rds[0].serial, 1)
1267
1268 def testCnameAndOtherDataAddOther(self):
1269 z = dns.zone.from_text(
1270 example_cname, "example.", relativize=True, zone_factory=self.zone_factory
1271 )
1272 rds = dns.rdataset.from_text("in", "a", 300, "10.0.0.1")
1273 with z.writer() as txn:
1274 txn.replace("web", rds)
1275 txn.replace("web2", rds.copy())
1276 n = z.find_node("web")
1277 self.assertEqual(len(n.rdatasets), 3)
1278 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.A), rds)
1279 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC))
1280 self.assertIsNotNone(
1281 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC)
1282 )
1283 n = z.find_node("web2")
1284 self.assertEqual(len(n.rdatasets), 3)
1285 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.A), rds)
1286 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC3))
1287 self.assertIsNotNone(
1288 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC3)
1289 )
1290
1291 def testCnameAndOtherDataAddCname(self):
1292 z = dns.zone.from_text(
1293 example_other_data,
1294 "example.",
1295 relativize=True,
1296 zone_factory=self.zone_factory,
1297 )
1298 rds = dns.rdataset.from_text("in", "cname", 300, "www")
1299 with z.writer() as txn:
1300 txn.replace("web", rds)
1301 n = z.find_node("web")
1302 self.assertEqual(len(n.rdatasets), 3)
1303 self.assertEqual(n.find_rdataset(dns.rdataclass.IN, dns.rdatatype.CNAME), rds)
1304 self.assertIsNotNone(n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.NSEC))
1305 self.assertIsNotNone(
1306 n.get_rdataset(dns.rdataclass.IN, dns.rdatatype.RRSIG, dns.rdatatype.NSEC)
1307 )
1308
1309 def testGetSoa(self):
1310 z = dns.zone.from_text(
1311 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1312 )
1313 soa = z.get_soa()
1314 self.assertTrue(soa.rdtype, dns.rdatatype.SOA)
1315 self.assertEqual(soa.serial, 1)
1316
1317 def testGetSoaTxn(self):
1318 z = dns.zone.from_text(
1319 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1320 )
1321 with z.reader(serial=1) as txn:
1322 soa = z.get_soa(txn)
1323 self.assertTrue(soa.rdtype, dns.rdatatype.SOA)
1324 self.assertEqual(soa.serial, 1)
1325
1326 def testGetSoaEmptyZone(self):
1327 z = dns.zone.Zone("example.")
1328 with self.assertRaises(dns.zone.NoSOA):
1329 soa = z.get_soa()
1330
1331 def testGetRdataset1(self):
1332 z = dns.zone.from_text(
1333 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1334 )
1335 rds = z.get_rdataset("@", "soa")
1336 exrds = dns.rdataset.from_text("IN", "SOA", 300, "foo bar 1 2 3 4 5")
1337 self.assertEqual(rds, exrds)
1338
1339 def testGetRdataset2(self):
1340 z = dns.zone.from_text(
1341 example_text, "example.", relativize=True, zone_factory=self.zone_factory
1342 )
1343 rds = z.get_rdataset("@", "loc")
1344 self.assertTrue(rds is None)
1345
1346
1347 class BTreeZoneTestCase(VersionedZoneTestCase):
1348 zone_factory = dns.btreezone.Zone
1349
1350
1351 if __name__ == "__main__":
1352 unittest.main()