Debian Codegraph @

tests/test_name.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # -*- coding: utf-8
2 # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
3
4 # Copyright (C) 2003-2017 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 copy
20 import operator
21 import pickle
22 import unittest
23 from io import BytesIO
24 from typing import Dict # pylint: disable=unused-import
25
26 import dns.e164
27 import dns.name
28 import dns.reversename
29
30 # pylint: disable=line-too-long,unsupported-assignment-operation
31
32
33 def expand(text):
34 # This is a helper routine to expand name patterns for RFC 4471 tests.
35 #
36 # Basically it turns <character>{<n>} into <n> instances of the character.
37 # For example:
38 #
39 # r"fo{2}.example." => r"foo.example.".
40 #
41 # Two characters get special treatment:
42 # "-" is mapped to r"\000" and "+" is mapped to r"\255". For example
43 #
44 # r"+{3}-.example." -> r"\255\255\255\000.example."
45 #
46 # We do this just to make parsing simpler, so we don't have to process escapes
47 # ourselves.
48 i = 0
49 l = len(text)
50 previous = ""
51 reading_count = False
52 count = 0
53 expanded = []
54 for c in text:
55 if c == "-":
56 c = r"\000"
57 elif c == "+":
58 c = r"\255"
59 if reading_count:
60 assert len(c) == 1
61 if c >= "0" and c <= "9":
62 count *= 10
63 count += ord(c) - ord("0")
64 elif c == "}":
65 expanded.append(previous * count)
66 previous = ""
67 reading_count = False
68 count = 0
69 elif c == "{":
70 reading_count = True
71 else:
72 expanded.append(previous)
73 previous = c
74 # don't forget the last char (if there is one)
75 expanded.append(previous)
76 x = "".join(expanded)
77 return x
78
79
80 class NameTestCase(unittest.TestCase):
81 def setUp(self):
82 self.origin = dns.name.from_text("example.")
83
84 def testFromTextRel1(self):
85 n = dns.name.from_text("foo.bar")
86 self.assertEqual(n.labels, (b"foo", b"bar", b""))
87
88 def testFromTextRel2(self):
89 n = dns.name.from_text("foo.bar", origin=self.origin)
90 self.assertEqual(n.labels, (b"foo", b"bar", b"example", b""))
91
92 def testFromTextRel3(self):
93 n = dns.name.from_text("foo.bar", origin=None)
94 self.assertEqual(n.labels, (b"foo", b"bar"))
95
96 def testFromTextRel4(self):
97 n = dns.name.from_text("@", origin=None)
98 self.assertEqual(n, dns.name.empty)
99
100 def testFromTextRel5(self):
101 n = dns.name.from_text("@", origin=self.origin)
102 self.assertEqual(n, self.origin)
103
104 def testFromTextAbs1(self):
105 n = dns.name.from_text("foo.bar.")
106 self.assertEqual(n.labels, (b"foo", b"bar", b""))
107
108 def testTortureFromText(self):
109 good = [
110 rb".",
111 rb"a",
112 rb"a.",
113 rb"a" * 63,
114 (rb"a" * 63 + rb".") * 3 + rb"a" * 61,
115 rb"\000.\008.\010.\032.\046.\092.\099.\255",
116 rb"\\",
117 rb"\..\.",
118 rb"\\.\\",
119 rb'!"#%&/()=+-',
120 (rb"\255" * 63 + rb".") * 3 + rb"\255" * 61,
121 ]
122 bad = [
123 rb"..",
124 rb".a",
125 rb"\\..",
126 b"\\", # yes, we don't want the 'r' prefix!
127 rb"\0",
128 rb"\00",
129 rb"\00Z",
130 rb"a" * 64,
131 (rb"a" * 63 + rb".") * 3 + rb"a" * 62,
132 (rb"\255" * 63 + rb".") * 3 + rb"\255" * 62,
133 ]
134 for t in good:
135 try:
136 dns.name.from_text(t)
137 except Exception:
138 self.fail("good test '%r' raised an exception" % t)
139 for t in bad:
140 caught = False
141 try:
142 dns.name.from_text(t)
143 except Exception:
144 caught = True
145 if not caught:
146 self.fail("bad test '%r' did not raise an exception" % t)
147
148 def testImmutable1(self):
149 def bad():
150 self.origin.labels = ()
151
152 self.assertRaises(TypeError, bad)
153
154 def testImmutable2(self):
155 def bad():
156 self.origin.labels[0] = "foo" # type: ignore
157
158 self.assertRaises(TypeError, bad)
159
160 def testAbs1(self):
161 self.assertTrue(dns.name.root.is_absolute())
162
163 def testAbs2(self):
164 self.assertFalse(dns.name.empty.is_absolute())
165
166 def testAbs3(self):
167 self.assertTrue(self.origin.is_absolute())
168
169 def testAbs4(self):
170 n = dns.name.from_text("foo", origin=None)
171 self.assertFalse(n.is_absolute())
172
173 def testWild1(self):
174 n = dns.name.from_text("*.foo", origin=None)
175 self.assertTrue(n.is_wild())
176
177 def testWild2(self):
178 n = dns.name.from_text("*a.foo", origin=None)
179 self.assertFalse(n.is_wild())
180
181 def testWild3(self):
182 n = dns.name.from_text("a.*.foo", origin=None)
183 self.assertFalse(n.is_wild())
184
185 def testWild4(self):
186 self.assertFalse(dns.name.root.is_wild())
187
188 def testWild5(self):
189 self.assertFalse(dns.name.empty.is_wild())
190
191 def testHash1(self):
192 n1 = dns.name.from_text("fOo.COM")
193 n2 = dns.name.from_text("foo.com")
194 self.assertEqual(hash(n1), hash(n2))
195
196 def testCompare1(self):
197 n1 = dns.name.from_text("a")
198 n2 = dns.name.from_text("b")
199 self.assertLess(n1, n2)
200 self.assertLessEqual(n1, n2)
201 self.assertGreater(n2, n1)
202 self.assertGreaterEqual(n2, n1)
203
204 def testCompare2(self):
205 n1 = dns.name.from_text("")
206 n2 = dns.name.from_text("b")
207 self.assertLess(n1, n2)
208 self.assertLessEqual(n1, n2)
209 self.assertGreater(n2, n1)
210 self.assertGreaterEqual(n2, n1)
211
212 def testCompare3(self):
213 self.assertLess(dns.name.empty, dns.name.root)
214 self.assertGreater(dns.name.root, dns.name.empty)
215
216 def testCompare4(self):
217 self.assertNotEqual(dns.name.root, 1)
218
219 def testSubdomain1(self):
220 self.assertFalse(dns.name.empty.is_subdomain(dns.name.root))
221
222 def testSubdomain2(self):
223 self.assertFalse(dns.name.root.is_subdomain(dns.name.empty))
224
225 def testSubdomain3(self):
226 n = dns.name.from_text("foo", origin=self.origin)
227 self.assertTrue(n.is_subdomain(self.origin))
228
229 def testSubdomain4(self):
230 n = dns.name.from_text("foo", origin=self.origin)
231 self.assertTrue(n.is_subdomain(dns.name.root))
232
233 def testSubdomain5(self):
234 n = dns.name.from_text("foo", origin=self.origin)
235 self.assertTrue(n.is_subdomain(n))
236
237 def testSuperdomain1(self):
238 self.assertFalse(dns.name.empty.is_superdomain(dns.name.root))
239
240 def testSuperdomain2(self):
241 self.assertFalse(dns.name.root.is_superdomain(dns.name.empty))
242
243 def testSuperdomain3(self):
244 n = dns.name.from_text("foo", origin=self.origin)
245 self.assertTrue(self.origin.is_superdomain(n))
246
247 def testSuperdomain4(self):
248 n = dns.name.from_text("foo", origin=self.origin)
249 self.assertTrue(dns.name.root.is_superdomain(n))
250
251 def testSuperdomain5(self):
252 n = dns.name.from_text("foo", origin=self.origin)
253 self.assertTrue(n.is_superdomain(n))
254
255 def testCanonicalize1(self):
256 n = dns.name.from_text("FOO.bar", origin=self.origin)
257 c = n.canonicalize()
258 self.assertEqual(c.labels, (b"foo", b"bar", b"example", b""))
259
260 def testToText1(self):
261 n = dns.name.from_text("FOO.bar", origin=self.origin)
262 t = n.to_text()
263 self.assertEqual(t, "FOO.bar.example.")
264
265 def testToText2(self):
266 n = dns.name.from_text("FOO.bar", origin=self.origin)
267 t = n.to_text(True)
268 self.assertEqual(t, "FOO.bar.example")
269
270 def testToText3(self):
271 n = dns.name.from_text("FOO.bar", origin=None)
272 t = n.to_text()
273 self.assertEqual(t, "FOO.bar")
274
275 def testToText4(self):
276 t = dns.name.empty.to_text()
277 self.assertEqual(t, "@")
278
279 def testToText5(self):
280 t = dns.name.root.to_text()
281 self.assertEqual(t, ".")
282
283 def testToText6(self):
284 n = dns.name.from_text("FOO bar", origin=None)
285 t = n.to_text()
286 self.assertEqual(t, r"FOO\032bar")
287
288 def testToText7(self):
289 n = dns.name.from_text(r"FOO\.bar", origin=None)
290 t = n.to_text()
291 self.assertEqual(t, r"FOO\.bar")
292
293 def testToText8(self):
294 n = dns.name.from_text(r"\070OO\.bar", origin=None)
295 t = n.to_text()
296 self.assertEqual(t, r"FOO\.bar")
297
298 def testToText9(self):
299 n = dns.name.from_text("FOO bar", origin=None)
300 t = n.to_unicode()
301 self.assertEqual(t, "FOO\\032bar")
302
303 def testToText10(self):
304 t = dns.name.empty.to_unicode()
305 self.assertEqual(t, "@")
306
307 def testToText11(self):
308 t = dns.name.root.to_unicode()
309 self.assertEqual(t, ".")
310
311 def testToText12(self):
312 n = dns.name.from_text(r"a\.b.c")
313 t = n.to_unicode()
314 self.assertEqual(t, r"a\.b.c.")
315
316 def testToText13(self):
317 n = dns.name.from_text(r"\150\151\152\153\154\155\156\157\158\159.")
318 t = n.to_text()
319 self.assertEqual(t, r"\150\151\152\153\154\155\156\157\158\159.")
320
321 def testToText14(self):
322 # Something that didn't start as unicode should go to escapes and not
323 # raise due to interpreting arbitrary binary DNS labels as UTF-8.
324 n = dns.name.from_text(r"\150\151\152\153\154\155\156\157\158\159.")
325 t = n.to_unicode()
326 self.assertEqual(t, r"\150\151\152\153\154\155\156\157\158\159.")
327
328 def testSlice1(self):
329 n = dns.name.from_text(r"a.b.c.", origin=None)
330 s = n[:]
331 self.assertEqual(s, (b"a", b"b", b"c", b""))
332
333 def testSlice2(self):
334 n = dns.name.from_text(r"a.b.c.", origin=None)
335 s = n[:2]
336 self.assertEqual(s, (b"a", b"b"))
337
338 def testSlice3(self):
339 n = dns.name.from_text(r"a.b.c.", origin=None)
340 s = n[2:]
341 self.assertEqual(s, (b"c", b""))
342
343 def testEmptyLabel1(self):
344 def bad():
345 dns.name.Name(["a", "", "b"])
346
347 self.assertRaises(dns.name.EmptyLabel, bad)
348
349 def testEmptyLabel2(self):
350 def bad():
351 dns.name.Name(["", "b"])
352
353 self.assertRaises(dns.name.EmptyLabel, bad)
354
355 def testEmptyLabel3(self):
356 n = dns.name.Name(["b", ""])
357 self.assertTrue(n)
358
359 def testLongLabel(self):
360 n = dns.name.Name(["a" * 63])
361 self.assertTrue(n)
362
363 def testLabelTooLong(self):
364 def bad():
365 dns.name.Name(["a" * 64, "b"])
366
367 self.assertRaises(dns.name.LabelTooLong, bad)
368
369 def testLongName(self):
370 n = dns.name.Name(["a" * 63, "a" * 63, "a" * 63, "a" * 62])
371 self.assertTrue(n)
372
373 def testNameTooLong(self):
374 def bad():
375 dns.name.Name(["a" * 63, "a" * 63, "a" * 63, "a" * 63])
376
377 self.assertRaises(dns.name.NameTooLong, bad)
378
379 def testConcat1(self):
380 n1 = dns.name.Name(["a", "b"])
381 n2 = dns.name.Name(["c", "d"])
382 e = dns.name.Name(["a", "b", "c", "d"])
383 r = n1 + n2
384 self.assertEqual(r, e)
385
386 def testConcat2(self):
387 n1 = dns.name.Name(["a", "b"])
388 n2 = dns.name.Name([])
389 e = dns.name.Name(["a", "b"])
390 r = n1 + n2
391 self.assertEqual(r, e)
392
393 def testConcat3(self):
394 n1 = dns.name.Name([])
395 n2 = dns.name.Name(["a", "b"])
396 e = dns.name.Name(["a", "b"])
397 r = n1 + n2
398 self.assertEqual(r, e)
399
400 def testConcat4(self):
401 n1 = dns.name.Name(["a", "b", ""])
402 n2 = dns.name.Name([])
403 e = dns.name.Name(["a", "b", ""])
404 r = n1 + n2
405 self.assertEqual(r, e)
406
407 def testConcat5(self):
408 n1 = dns.name.Name(["a", "b"])
409 n2 = dns.name.Name(["c", ""])
410 e = dns.name.Name(["a", "b", "c", ""])
411 r = n1 + n2
412 self.assertEqual(r, e)
413
414 def testConcat6(self):
415 def bad():
416 n1 = dns.name.Name(["a", "b", ""])
417 n2 = dns.name.Name(["c"])
418 return n1 + n2
419
420 self.assertRaises(dns.name.AbsoluteConcatenation, bad)
421
422 def testBadEscape(self):
423 def bad():
424 n = dns.name.from_text(r"a.b\0q1.c.")
425
426 self.assertRaises(dns.name.BadEscape, bad)
427
428 def testDigestable1(self):
429 n = dns.name.from_text("FOO.bar")
430 d = n.to_digestable()
431 self.assertEqual(d, b"\x03foo\x03bar\x00")
432
433 def testDigestable2(self):
434 n1 = dns.name.from_text("FOO.bar")
435 n2 = dns.name.from_text("foo.BAR.")
436 d1 = n1.to_digestable()
437 d2 = n2.to_digestable()
438 self.assertEqual(d1, d2)
439
440 def testDigestable3(self):
441 d = dns.name.root.to_digestable()
442 self.assertEqual(d, b"\x00")
443
444 def testDigestable4(self):
445 n = dns.name.from_text("FOO.bar", None)
446 d = n.to_digestable(dns.name.root)
447 self.assertEqual(d, b"\x03foo\x03bar\x00")
448
449 def testBadDigestable(self):
450 def bad():
451 n = dns.name.from_text("FOO.bar", None)
452 n.to_digestable()
453
454 self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin, bad)
455
456 def testToWire1(self):
457 n = dns.name.from_text("FOO.bar")
458 f = BytesIO()
459 compress = {} # type: Dict[dns.name.Name,int]
460 n.to_wire(f, compress)
461 self.assertEqual(f.getvalue(), b"\x03FOO\x03bar\x00")
462
463 def testToWire2(self):
464 n = dns.name.from_text("FOO.bar")
465 f = BytesIO()
466 compress = {} # type: Dict[dns.name.Name,int]
467 n.to_wire(f, compress)
468 n.to_wire(f, compress)
469 self.assertEqual(f.getvalue(), b"\x03FOO\x03bar\x00\xc0\x00")
470
471 def testToWire3(self):
472 n1 = dns.name.from_text("FOO.bar")
473 n2 = dns.name.from_text("foo.bar")
474 f = BytesIO()
475 compress = {} # type: Dict[dns.name.Name,int]
476 n1.to_wire(f, compress)
477 n2.to_wire(f, compress)
478 self.assertEqual(f.getvalue(), b"\x03FOO\x03bar\x00\xc0\x00")
479
480 def testToWire4(self):
481 n1 = dns.name.from_text("FOO.bar")
482 n2 = dns.name.from_text("a.foo.bar")
483 f = BytesIO()
484 compress = {} # type: Dict[dns.name.Name,int]
485 n1.to_wire(f, compress)
486 n2.to_wire(f, compress)
487 self.assertEqual(f.getvalue(), b"\x03FOO\x03bar\x00\x01\x61\xc0\x00")
488
489 def testToWire5(self):
490 n1 = dns.name.from_text("FOO.bar")
491 n2 = dns.name.from_text("a.foo.bar")
492 f = BytesIO()
493 compress = {} # type: Dict[dns.name.Name,int]
494 n1.to_wire(f, compress)
495 n2.to_wire(f, None)
496 self.assertEqual(f.getvalue(), b"\x03FOO\x03bar\x00\x01\x61\x03foo\x03bar\x00")
497
498 def testToWire6(self):
499 n = dns.name.from_text("FOO.bar")
500 v = n.to_wire()
501 self.assertEqual(v, b"\x03FOO\x03bar\x00")
502
503 def testToWireRelativeNameWithOrigin(self):
504 n = dns.name.from_text("FOO", None)
505 o = dns.name.from_text("bar")
506 v = n.to_wire(origin=o)
507 self.assertEqual(v, b"\x03FOO\x03bar\x00")
508
509 def testToWireRelativeNameWithoutOrigin(self):
510 n = dns.name.from_text("FOO", None)
511
512 def bad():
513 v = n.to_wire()
514
515 self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin, bad)
516
517 def testBadToWire(self):
518 def bad():
519 n = dns.name.from_text("FOO.bar", None)
520 f = BytesIO()
521 compress = {} # type: Dict[dns.name.Name,int]
522 n.to_wire(f, compress)
523
524 self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin, bad)
525
526 def testGiantCompressionTable(self):
527 # Only the first 16KiB of a message can have compression pointers.
528 f = BytesIO()
529 compress = {} # type: Dict[dns.name.Name,int]
530 # exactly 16 bytes encoded
531 n = dns.name.from_text("0000000000.com.")
532 n.to_wire(f, compress)
533 # There are now two entries in the compression table (for the full
534 # name, and for the com. suffix.
535 self.assertEqual(len(compress), 2)
536 for i in range(1023):
537 # exactly 16 bytes encoded with compression
538 n = dns.name.from_text(f"{i:013d}.com")
539 n.to_wire(f, compress)
540 # There are now 1025 entries in the compression table with
541 # the last entry at offset 16368.
542 self.assertEqual(len(compress), 1025)
543 self.assertEqual(compress[n], 16368)
544 # Adding another name should not increase the size of the compression
545 # table, as the pointer would be at offset 16384, which is too big.
546 n = dns.name.from_text("toobig.com.")
547 n.to_wire(f, compress)
548 self.assertEqual(len(compress), 1025)
549
550 def testSplit1(self):
551 n = dns.name.from_text("foo.bar.")
552 (prefix, suffix) = n.split(2)
553 ep = dns.name.from_text("foo", None)
554 es = dns.name.from_text("bar.", None)
555 self.assertEqual(prefix, ep)
556 self.assertEqual(suffix, es)
557
558 def testSplit2(self):
559 n = dns.name.from_text("foo.bar.")
560 (prefix, suffix) = n.split(1)
561 ep = dns.name.from_text("foo.bar", None)
562 es = dns.name.from_text(".", None)
563 self.assertEqual(prefix, ep)
564 self.assertEqual(suffix, es)
565
566 def testSplit3(self):
567 n = dns.name.from_text("foo.bar.")
568 (prefix, suffix) = n.split(0)
569 ep = dns.name.from_text("foo.bar.", None)
570 es = dns.name.from_text("", None)
571 self.assertEqual(prefix, ep)
572 self.assertEqual(suffix, es)
573
574 def testSplit4(self):
575 n = dns.name.from_text("foo.bar.")
576 (prefix, suffix) = n.split(3)
577 ep = dns.name.from_text("", None)
578 es = dns.name.from_text("foo.bar.", None)
579 self.assertEqual(prefix, ep)
580 self.assertEqual(suffix, es)
581
582 def testBadSplit1(self):
583 def bad():
584 n = dns.name.from_text("foo.bar.")
585 n.split(-1)
586
587 self.assertRaises(ValueError, bad)
588
589 def testBadSplit2(self):
590 def bad():
591 n = dns.name.from_text("foo.bar.")
592 n.split(4)
593
594 self.assertRaises(ValueError, bad)
595
596 def testRelativize1(self):
597 n = dns.name.from_text("a.foo.bar.", None)
598 o = dns.name.from_text("bar.", None)
599 e = dns.name.from_text("a.foo", None)
600 self.assertEqual(n.relativize(o), e)
601
602 def testRelativize2(self):
603 n = dns.name.from_text("a.foo.bar.", None)
604 o = n
605 e = dns.name.empty
606 self.assertEqual(n.relativize(o), e)
607
608 def testRelativize3(self):
609 n = dns.name.from_text("a.foo.bar.", None)
610 o = dns.name.from_text("blaz.", None)
611 e = n
612 self.assertEqual(n.relativize(o), e)
613
614 def testRelativize4(self):
615 n = dns.name.from_text("a.foo", None)
616 o = dns.name.root
617 e = n
618 self.assertEqual(n.relativize(o), e)
619
620 def testDerelativize1(self):
621 n = dns.name.from_text("a.foo", None)
622 o = dns.name.from_text("bar.", None)
623 e = dns.name.from_text("a.foo.bar.", None)
624 self.assertEqual(n.derelativize(o), e)
625
626 def testDerelativize2(self):
627 n = dns.name.empty
628 o = dns.name.from_text("a.foo.bar.", None)
629 e = o
630 self.assertEqual(n.derelativize(o), e)
631
632 def testDerelativize3(self):
633 n = dns.name.from_text("a.foo.bar.", None)
634 o = dns.name.from_text("blaz.", None)
635 e = n
636 self.assertEqual(n.derelativize(o), e)
637
638 def testChooseRelativity1(self):
639 n = dns.name.from_text("a.foo.bar.", None)
640 o = dns.name.from_text("bar.", None)
641 e = dns.name.from_text("a.foo", None)
642 self.assertEqual(n.choose_relativity(o, True), e)
643
644 def testChooseRelativity2(self):
645 n = dns.name.from_text("a.foo.bar.", None)
646 o = dns.name.from_text("bar.", None)
647 e = n
648 self.assertEqual(n.choose_relativity(o, False), e)
649
650 def testChooseRelativity3(self):
651 n = dns.name.from_text("a.foo", None)
652 o = dns.name.from_text("bar.", None)
653 e = dns.name.from_text("a.foo.bar.", None)
654 self.assertEqual(n.choose_relativity(o, False), e)
655
656 def testChooseRelativity4(self):
657 n = dns.name.from_text("a.foo", None)
658 o = None
659 e = n
660 self.assertEqual(n.choose_relativity(o, True), e)
661
662 def testChooseRelativity5(self):
663 n = dns.name.from_text("a.foo", None)
664 o = None
665 e = n
666 self.assertEqual(n.choose_relativity(o, False), e)
667
668 def testChooseRelativity6(self):
669 n = dns.name.from_text("a.foo.", None)
670 o = None
671 e = n
672 self.assertEqual(n.choose_relativity(o, True), e)
673
674 def testChooseRelativity7(self):
675 n = dns.name.from_text("a.foo.", None)
676 o = None
677 e = n
678 self.assertEqual(n.choose_relativity(o, False), e)
679
680 def testFromWire1(self):
681 w = b"\x03foo\x00\xc0\x00"
682 (n1, cused1) = dns.name.from_wire(w, 0)
683 (n2, cused2) = dns.name.from_wire(w, cused1)
684 en1 = dns.name.from_text("foo.")
685 en2 = en1
686 ecused1 = 5
687 ecused2 = 2
688 self.assertEqual(n1, en1)
689 self.assertEqual(cused1, ecused1)
690 self.assertEqual(n2, en2)
691 self.assertEqual(cused2, ecused2)
692
693 def testFromWire2(self):
694 w = b"\x03foo\x00\x01a\xc0\x00\x01b\xc0\x05"
695 current = 0
696 (n1, cused1) = dns.name.from_wire(w, current)
697 current += cused1
698 (n2, cused2) = dns.name.from_wire(w, current)
699 current += cused2
700 (n3, cused3) = dns.name.from_wire(w, current)
701 en1 = dns.name.from_text("foo.")
702 en2 = dns.name.from_text("a.foo.")
703 en3 = dns.name.from_text("b.a.foo.")
704 ecused1 = 5
705 ecused2 = 4
706 ecused3 = 4
707 self.assertEqual(n1, en1)
708 self.assertEqual(cused1, ecused1)
709 self.assertEqual(n2, en2)
710 self.assertEqual(cused2, ecused2)
711 self.assertEqual(n3, en3)
712 self.assertEqual(cused3, ecused3)
713
714 def testBadFromWire1(self):
715 def bad():
716 w = b"\x03foo\xc0\x04"
717 dns.name.from_wire(w, 0)
718
719 self.assertRaises(dns.name.BadPointer, bad)
720
721 def testBadFromWire2(self):
722 def bad():
723 w = b"\x03foo\xc0\x05"
724 dns.name.from_wire(w, 0)
725
726 self.assertRaises(dns.name.BadPointer, bad)
727
728 def testBadFromWire3(self):
729 def bad():
730 w = b"\xbffoo"
731 dns.name.from_wire(w, 0)
732
733 self.assertRaises(dns.name.BadLabelType, bad)
734
735 def testBadFromWire4(self):
736 def bad():
737 w = b"\x41foo"
738 dns.name.from_wire(w, 0)
739
740 self.assertRaises(dns.name.BadLabelType, bad)
741
742 def testParent1(self):
743 n = dns.name.from_text("foo.bar.")
744 self.assertEqual(n.parent(), dns.name.from_text("bar."))
745 self.assertEqual(n.parent().parent(), dns.name.root)
746
747 def testParent2(self):
748 n = dns.name.from_text("foo.bar", None)
749 self.assertEqual(n.parent(), dns.name.from_text("bar", None))
750 self.assertEqual(n.parent().parent(), dns.name.empty)
751
752 def testParent3(self):
753 def bad():
754 n = dns.name.root
755 n.parent()
756
757 self.assertRaises(dns.name.NoParent, bad)
758
759 def testParent4(self):
760 def bad():
761 n = dns.name.empty
762 n.parent()
763
764 self.assertRaises(dns.name.NoParent, bad)
765
766 def testFromUnicode1(self):
767 n = dns.name.from_text("foo.bar")
768 self.assertEqual(n.labels, (b"foo", b"bar", b""))
769
770 def testFromUnicode2(self):
771 n = dns.name.from_text("foo\u1234bar.bar")
772 self.assertEqual(n.labels, (b"xn--foobar-r5z", b"bar", b""))
773
774 def testFromUnicodeAlternateDot1(self):
775 n = dns.name.from_text("foo\u3002bar")
776 self.assertEqual(n.labels, (b"foo", b"bar", b""))
777
778 def testFromUnicodeAlternateDot2(self):
779 n = dns.name.from_text("foo\uff0ebar")
780 self.assertEqual(n.labels, (b"foo", b"bar", b""))
781
782 def testFromUnicodeAlternateDot3(self):
783 n = dns.name.from_text("foo\uff61bar")
784 self.assertEqual(n.labels, (b"foo", b"bar", b""))
785
786 def testFromUnicodeRoot(self):
787 n = dns.name.from_text(".")
788 self.assertEqual(n.labels, (b"",))
789
790 def testFromUnicodeAlternateRoot1(self):
791 n = dns.name.from_text("\u3002")
792 self.assertEqual(n.labels, (b"",))
793
794 def testFromUnicodeAlternateRoot2(self):
795 n = dns.name.from_text("\uff0e")
796 self.assertEqual(n.labels, (b"",))
797
798 def testFromUnicodeAlternateRoot3(self):
799 n = dns.name.from_text("\uff61")
800 self.assertEqual(n.labels, (b"",))
801
802 def testFromUnicodeIDNA2003Explicit(self):
803 t = "Königsgäßchen"
804 e = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2003)
805 self.assertEqual(str(e), "xn--knigsgsschen-lcb0w.")
806
807 def testFromUnicodeIDNA2003Default(self):
808 t = "Königsgäßchen"
809 e = dns.name.from_unicode(t)
810 self.assertEqual(str(e), "xn--knigsgsschen-lcb0w.")
811
812 @unittest.skipUnless(
813 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
814 )
815 def testFromUnicodeIDNA2008(self):
816 t = "Königsgäßchen"
817
818 def bad():
819 codec = dns.name.IDNA_2008_Strict
820 return dns.name.from_unicode(t, idna_codec=codec)
821
822 self.assertRaises(dns.name.IDNAException, bad)
823 e1 = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2008)
824 self.assertEqual(str(e1), "xn--knigsgchen-b4a3dun.")
825 c2 = dns.name.IDNA_2008_Transitional
826 e2 = dns.name.from_unicode(t, idna_codec=c2)
827 self.assertEqual(str(e2), "xn--knigsgsschen-lcb0w.")
828
829 @unittest.skipUnless(
830 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
831 )
832 def testFromUnicodeIDNA2008Mixed(self):
833 # the IDN rules for names are very restrictive, disallowing
834 # practical names like '_sip._tcp.Königsgäßchen'. Dnspython
835 # has a "practical" mode which permits labels which are purely
836 # ASCII to go straight through, and thus not invalid useful
837 # things in the real world.
838 t = "_sip._tcp.Königsgäßchen"
839
840 def bad1():
841 codec = dns.name.IDNA_2008_Strict
842 return dns.name.from_unicode(t, idna_codec=codec)
843
844 def bad2():
845 codec = dns.name.IDNA_2008_UTS_46
846 return dns.name.from_unicode(t, idna_codec=codec)
847
848 def bad3():
849 codec = dns.name.IDNA_2008_Transitional
850 return dns.name.from_unicode(t, idna_codec=codec)
851
852 self.assertRaises(dns.name.IDNAException, bad1)
853 self.assertRaises(dns.name.IDNAException, bad2)
854 self.assertRaises(dns.name.IDNAException, bad3)
855 e = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2008_Practical)
856 self.assertEqual(str(e), "_sip._tcp.xn--knigsgchen-b4a3dun.")
857
858 def testFromUnicodeEscapes(self):
859 n = dns.name.from_unicode(r"\097.\098.\099.")
860 t = n.to_unicode()
861 self.assertEqual(t, "a.b.c.")
862
863 def testToUnicode1(self):
864 n = dns.name.from_text("foo.bar")
865 s = n.to_unicode()
866 self.assertEqual(s, "foo.bar.")
867
868 def testToUnicode2(self):
869 n = dns.name.from_text("foo\u1234bar.bar")
870 s = n.to_unicode()
871 self.assertEqual(s, "foo\u1234bar.bar.")
872
873 def testToUnicode3(self):
874 n = dns.name.from_text("foo.bar")
875 s = n.to_unicode()
876 self.assertEqual(s, "foo.bar.")
877
878 @unittest.skipUnless(
879 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
880 )
881 def testToUnicode4(self):
882 n = dns.name.from_text("ドメイン.テスト", idna_codec=dns.name.IDNA_2008)
883 s = n.to_unicode()
884 self.assertEqual(str(n), "xn--eckwd4c7c.xn--zckzah.")
885 self.assertEqual(s, "ドメイン.テスト.")
886
887 @unittest.skipUnless(
888 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
889 )
890 def testToUnicode5(self):
891 # Exercise UTS 46 remapping in decode. This doesn't normally happen
892 # as you can see from us having to instantiate the codec as
893 # transitional with strict decoding, not one of our usual choices.
894 codec = dns.name.IDNA2008Codec(True, True, False, True)
895 n = dns.name.from_text("xn--gro-7ka.com")
896 self.assertEqual(n.to_unicode(idna_codec=codec), "gross.com.")
897
898 @unittest.skipUnless(
899 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
900 )
901 def testToUnicode6(self):
902 # Test strict 2008 decoding without UTS 46
903 n = dns.name.from_text("xn--gro-7ka.com")
904 self.assertEqual(
905 n.to_unicode(idna_codec=dns.name.IDNA_2008_Strict), "groß.com."
906 )
907
908 def testDefaultDecodeIsJustPunycode(self):
909 # groß.com. in IDNA2008 form, pre-encoded.
910 n = dns.name.from_text("xn--gro-7ka.com")
911 # output using default codec which just decodes the punycode and
912 # doesn't test for IDNA2003 or IDNA2008.
913 self.assertEqual(n.to_unicode(), "groß.com.")
914
915 def testStrictINDA2003Decode(self):
916 # groß.com. in IDNA2008 form, pre-encoded.
917 n = dns.name.from_text("xn--gro-7ka.com")
918
919 def bad():
920 # This throws in IDNA2003 because it doesn't "round trip".
921 n.to_unicode(idna_codec=dns.name.IDNA_2003_Strict)
922
923 self.assertRaises(dns.name.IDNAException, bad)
924
925 def testINDA2008Decode(self):
926 # groß.com. in IDNA2008 form, pre-encoded.
927 n = dns.name.from_text("xn--gro-7ka.com")
928 self.assertEqual(n.to_unicode(idna_codec=dns.name.IDNA_2008), "groß.com.")
929
930 def testToUnicodeOmitFinalDot(self):
931 # groß.com. in IDNA2008 form, pre-encoded.
932 n = dns.name.from_text("xn--gro-7ka.com")
933 self.assertEqual(n.to_unicode(True, dns.name.IDNA_2008), "groß.com")
934
935 def testIDNA2003Misc(self):
936 self.assertEqual(dns.name.IDNA_2003.encode(""), b"")
937 self.assertRaises(
938 dns.name.LabelTooLong, lambda: dns.name.IDNA_2003.encode("x" * 64)
939 )
940
941 @unittest.skipUnless(
942 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
943 )
944 def testIDNA2008Misc(self):
945 self.assertEqual(dns.name.IDNA_2008.encode(""), b"")
946 self.assertRaises(
947 dns.name.LabelTooLong, lambda: dns.name.IDNA_2008.encode("x" * 64)
948 )
949 self.assertRaises(
950 dns.name.LabelTooLong, lambda: dns.name.IDNA_2008.encode("groß" + "x" * 60)
951 )
952
953 def testReverseIPv4(self):
954 e = dns.name.from_text("1.0.0.127.in-addr.arpa.")
955 n = dns.reversename.from_address("127.0.0.1")
956 self.assertEqual(e, n)
957
958 def testReverseIPv6(self):
959 e = dns.name.from_text(
960 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa."
961 )
962 n = dns.reversename.from_address("::1")
963 self.assertEqual(e, n)
964
965 def testReverseIPv6MappedIpv4(self):
966 e = dns.name.from_text("1.0.0.127.in-addr.arpa.")
967 n = dns.reversename.from_address("::ffff:127.0.0.1")
968 self.assertEqual(e, n)
969
970 def testBadReverseIPv4(self):
971 def bad():
972 dns.reversename.from_address("127.0.foo.1")
973
974 self.assertRaises(dns.exception.SyntaxError, bad)
975
976 def testBadReverseIPv6(self):
977 def bad():
978 dns.reversename.from_address("::1::1")
979
980 self.assertRaises(dns.exception.SyntaxError, bad)
981
982 def testReverseIPv4AlternateOrigin(self):
983 e = dns.name.from_text("1.0.0.127.foo.bar.")
984 origin = dns.name.from_text("foo.bar")
985 n = dns.reversename.from_address("127.0.0.1", v4_origin=origin)
986 self.assertEqual(e, n)
987
988 def testReverseIPv6AlternateOrigin(self):
989 e = dns.name.from_text(
990 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.foo.bar."
991 )
992 origin = dns.name.from_text("foo.bar")
993 n = dns.reversename.from_address("::1", v6_origin=origin)
994 self.assertEqual(e, n)
995
996 def testForwardIPv4(self):
997 n = dns.name.from_text("1.0.0.127.in-addr.arpa.")
998 e = "127.0.0.1"
999 text = dns.reversename.to_address(n)
1000 self.assertEqual(text, e)
1001
1002 def testForwardIPv6(self):
1003 n = dns.name.from_text(
1004 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa."
1005 )
1006 e = "::1"
1007 text = dns.reversename.to_address(n)
1008 self.assertEqual(text, e)
1009
1010 def testForwardIPv4AlternateOrigin(self):
1011 n = dns.name.from_text("1.0.0.127.foo.bar.")
1012 e = "127.0.0.1"
1013 origin = dns.name.from_text("foo.bar")
1014 text = dns.reversename.to_address(n, v4_origin=origin)
1015 self.assertEqual(text, e)
1016
1017 def testForwardIPv6AlternateOrigin(self):
1018 n = dns.name.from_text(
1019 "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.foo.bar."
1020 )
1021 e = "::1"
1022 origin = dns.name.from_text("foo.bar")
1023 text = dns.reversename.to_address(n, v6_origin=origin)
1024 self.assertEqual(text, e)
1025
1026 def testUnknownReverseOrigin(self):
1027 n = dns.name.from_text("1.2.3.4.unknown.")
1028 with self.assertRaises(dns.exception.SyntaxError):
1029 dns.reversename.to_address(n)
1030
1031 def testE164ToEnum(self):
1032 text = "+1 650 555 1212"
1033 e = dns.name.from_text("2.1.2.1.5.5.5.0.5.6.1.e164.arpa.")
1034 n = dns.e164.from_e164(text)
1035 self.assertEqual(n, e)
1036
1037 def testEnumToE164(self):
1038 n = dns.name.from_text("2.1.2.1.5.5.5.0.5.6.1.e164.arpa.")
1039 e = "+16505551212"
1040 text = dns.e164.to_e164(n)
1041 self.assertEqual(text, e)
1042
1043 def testBadEnumToE164(self):
1044 n = dns.name.from_text("2.1.2.q.5.5.5.0.5.6.1.e164.arpa.")
1045 self.assertRaises(dns.exception.SyntaxError, lambda: dns.e164.to_e164(n))
1046
1047 def test_incompatible_relations(self):
1048 n1 = dns.name.from_text("example")
1049 n2 = "abc"
1050 for oper in [operator.lt, operator.le, operator.ge, operator.gt]:
1051 self.assertRaises(TypeError, lambda: oper(n1, n2))
1052 self.assertFalse(n1 == n2)
1053 self.assertTrue(n1 != n2)
1054
1055 def testFromUnicodeSimpleEscape(self):
1056 n = dns.name.from_unicode(r"a.\b")
1057 e = dns.name.from_unicode(r"a.b")
1058 self.assertEqual(n, e)
1059
1060 def testFromUnicodeBadEscape(self):
1061 def bad1():
1062 n = dns.name.from_unicode(r"a.b\0q1.c.")
1063
1064 self.assertRaises(dns.name.BadEscape, bad1)
1065
1066 def bad2():
1067 n = dns.name.from_unicode(r"a.b\0")
1068
1069 self.assertRaises(dns.name.BadEscape, bad2)
1070
1071 def testFromUnicodeNotString(self):
1072 def bad():
1073 dns.name.from_unicode(b"123") # type: ignore
1074
1075 self.assertRaises(ValueError, bad)
1076
1077 def testFromUnicodeBadOrigin(self):
1078 def bad():
1079 dns.name.from_unicode("example", 123) # type: ignore
1080
1081 self.assertRaises(ValueError, bad)
1082
1083 def testFromUnicodeEmptyLabel(self):
1084 def bad():
1085 dns.name.from_unicode("a..b.example")
1086
1087 self.assertRaises(dns.name.EmptyLabel, bad)
1088
1089 def testFromUnicodeEmptyName(self):
1090 self.assertEqual(dns.name.from_unicode("@", None), dns.name.empty)
1091
1092 def testFromTextNotString(self):
1093 def bad():
1094 dns.name.from_text(123) # type: ignore
1095
1096 self.assertRaises(ValueError, bad)
1097
1098 def testFromTextBadOrigin(self):
1099 def bad():
1100 dns.name.from_text("example", 123) # type: ignore
1101
1102 self.assertRaises(ValueError, bad)
1103
1104 def testFromWireNotBytes(self):
1105 def bad():
1106 dns.name.from_wire(123, 0) # type: ignore
1107
1108 self.assertRaises(ValueError, bad)
1109
1110 def testBadPunycode(self):
1111 c = dns.name.IDNACodec()
1112 with self.assertRaises(dns.name.IDNAException):
1113 c.decode(b"xn--0000h")
1114
1115 def testRootLabel2003StrictDecode(self):
1116 c = dns.name.IDNA_2003_Strict
1117 self.assertEqual(c.decode(b""), "")
1118
1119 @unittest.skipUnless(
1120 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1121 )
1122 def testRootLabel2008StrictDecode(self):
1123 c = dns.name.IDNA_2008_Strict
1124 self.assertEqual(c.decode(b""), "")
1125
1126 @unittest.skipUnless(
1127 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1128 )
1129 def testCodecNotFoundRaises(self):
1130 dns.name.have_idna_2008 = False
1131 with self.assertRaises(dns.name.NoIDNA2008):
1132 c = dns.name.IDNA2008Codec()
1133 c.encode("Königsgäßchen")
1134 with self.assertRaises(dns.name.NoIDNA2008):
1135 c = dns.name.IDNA2008Codec(strict_decode=True)
1136 c.decode(b"xn--eckwd4c7c.xn--zckzah.")
1137 dns.name.have_idna_2008 = True
1138
1139 @unittest.skipUnless(
1140 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1141 )
1142 def testBadPunycodeStrict2008(self):
1143 c = dns.name.IDNA2008Codec(strict_decode=True)
1144 with self.assertRaises(dns.name.IDNAException):
1145 c.decode(b"xn--0000h")
1146
1147 def testRelativizeSubtractionSyntax(self):
1148 n = dns.name.from_text("foo.example.")
1149 o = dns.name.from_text("example.")
1150 e = dns.name.from_text("foo", None)
1151 self.assertEqual(n - o, e)
1152
1153 def testCopy(self):
1154 n1 = dns.name.from_text("foo.example.")
1155 n2 = copy.copy(n1)
1156 self.assertTrue(n1 is not n2)
1157 # the Name constructor always copies labels, so there is no
1158 # difference between copy and deepcopy
1159 self.assertTrue(n1.labels is not n2.labels)
1160 self.assertEqual(len(n1.labels), len(n2.labels))
1161 for i, l in enumerate(n1.labels):
1162 self.assertTrue(l is n2[i])
1163
1164 def testDeepCopy(self):
1165 n1 = dns.name.from_text("foo.example.")
1166 n2 = copy.deepcopy(n1)
1167 self.assertTrue(n1 is not n2)
1168 self.assertTrue(n1.labels is not n2.labels)
1169 self.assertEqual(len(n1.labels), len(n2.labels))
1170 for i, l in enumerate(n1.labels):
1171 self.assertTrue(l is n2[i])
1172
1173 def testNoAttributeDeletion(self):
1174 n = dns.name.from_text("foo.example.")
1175 with self.assertRaises(TypeError):
1176 del n.labels
1177
1178 def testUnicodeEscapify(self):
1179 n = dns.name.from_unicode("Königsgäßchen;\ttext")
1180 self.assertEqual(n.to_unicode(), "königsgässchen\\;\\009text.")
1181
1182 def test_pickle(self):
1183 n1 = dns.name.from_text("foo.example")
1184 p = pickle.dumps(n1)
1185 n2 = pickle.loads(p)
1186 self.assertEqual(n1, n2)
1187
1188 def test_pad_to_max_name(self):
1189 # Test edge cases in our padding helper.
1190 tests = [
1191 ("o{61}.o{63}.o{63}.o{63}.", "o{61}.o{63}.o{63}.o{63}."),
1192 ("o{60}.o{63}.o{63}.o{63}.", "o{60}.o{63}.o{63}.o{63}."),
1193 ("o{59}.o{63}.o{63}.o{63}.", "+.o{59}.o{63}.o{63}.o{63}."),
1194 ("o{63}.o{63}.o{63}.", "+{61}.o{63}.o{63}.o{63}."),
1195 ("o{63}.o{63}.", "+{61}.+{63}.o{63}.o{63}."),
1196 ]
1197 for name_text, expected_text in tests:
1198 name = dns.name.from_text(expand(name_text))
1199 expected = dns.name.from_text(expand(expected_text))
1200 self.assertEqual(dns.name._pad_to_max_name(name), expected)
1201
1202 def test_predecessors_and_successors(self):
1203 # Test RFC 4471 predecessor and successor methods.
1204
1205 # Here we're actually testing the test suite, but expand() is complicated enough
1206 # to deserve a little testing.
1207 self.assertEqual(expand("f+{3}-o.example."), r"f\255\255\255\000o.example.")
1208
1209 # Ok, now test successors!
1210 origin = dns.name.from_text("example.com.")
1211 tests = [
1212 # Examples from the RFC.
1213 ("foo", True, "\\000.foo"),
1214 ("foo", False, "foo\\000"),
1215 # The syntax here is almost the RFC's "alternate syntax" except that to
1216 # make expand simpler, i.e. not have to understand that \000 was one octet,
1217 # I made the convention that "-" means 0 and "+" means 255. We use raw
1218 # string constants where needed to avoid escaping backslash.
1219 (
1220 "fo{47}.o{63}.o{63}.o{63}",
1221 True,
1222 "fo{47}-.o{63}.o{63}.o{63}",
1223 ),
1224 (
1225 "fo{48}.o{63}.o{63}.o{63}",
1226 True,
1227 "fo{47}p.o{63}.o{63}.o{63}",
1228 ),
1229 ("+{49}.o{63}.o{63}.o{63}", True, "o{62}p.o{63}.o{63}"),
1230 (
1231 "fo{40}+{8}.o{63}.o{63}.o{63}",
1232 True,
1233 "fo{39}p.o{63}.o{63}.o{63}",
1234 ),
1235 (
1236 r"fo{47}\@.o{63}.o{63}.o{63}",
1237 True,
1238 r"fo{47}\[.o{63}.o{63}.o{63}",
1239 ),
1240 ("+{49}.+{63}.+{63}.+{63}", True, ""),
1241 # Some more tests not in the RFC
1242 ("+{49}.+{63}.o{63}.o{63}", True, "o{62}p.o{63}"),
1243 (
1244 "+{48}.o{63}.o{63}.o{63}",
1245 True,
1246 "+{48}-.o{63}.o{63}.o{63}",
1247 ),
1248 ]
1249 for test_origin in [origin, None]:
1250 for name_text, prefix_ok, expected_successor_text in tests:
1251 name = dns.name.from_text(expand(name_text), test_origin)
1252 expected_successor = dns.name.from_text(
1253 expand(expected_successor_text), test_origin
1254 )
1255 successor = name.successor(origin, prefix_ok)
1256 self.assertEqual(successor, expected_successor)
1257 self.assertTrue(
1258 successor > name
1259 or successor == origin
1260 or successor == dns.name.empty
1261 )
1262 # Now test the predecessor
1263 predecessor = successor.predecessor(origin, prefix_ok)
1264 self.assertEqual(predecessor, name)
1265
1266 # Finally, test that a maximal length origin is its own predecessor and
1267 # successor.
1268 origin = dns.name.from_text(expand("+{49}.+{63}.o{63}.o{63}.example.com."))
1269 assert origin.successor(origin, True) == origin
1270 assert origin.predecessor(origin, True) == origin
1271
1272 def test_predecessor_and_successor_errors(self):
1273 name = dns.name.from_text("name", None)
1274 origin = dns.name.from_text("origin", None) # note Relative!
1275 with self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin):
1276 name.successor(origin, True)
1277 with self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin):
1278 name.predecessor(origin, True)
1279 name = dns.name.from_text("name.") # Note absolute
1280 origin = dns.name.from_text("origin.") # 'name' is not a subdomain of 'origin'
1281 with self.assertRaises(dns.name.NeedSubdomainOfOrigin):
1282 name.successor(origin, True)
1283 with self.assertRaises(dns.name.NeedSubdomainOfOrigin):
1284 name.predecessor(origin, True)
1285
1286
1287 if __name__ == "__main__":
1288 unittest.main()