Debian Codegraph @

tests/test_name.py

raw ◀ ▶
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
826 @unittest.skipUnless(
827 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
828 )
829 def testFromUnicodeIDNA2008Mixed(self):
830 # the IDN rules for names are very restrictive, disallowing
831 # practical names like '_sip._tcp.Königsgäßchen'. Dnspython
832 # has a "practical" mode which permits labels which are purely
•833 # ASCII to go straight through, and thus not invalidate useful
834 # things in the real world.
835 t = "_sip._tcp.Königsgäßchen"
836
837 def bad1():
838 codec = dns.name.IDNA_2008_Strict
839 return dns.name.from_unicode(t, idna_codec=codec)
840
841 def bad2():
842 codec = dns.name.IDNA_2008_UTS_46
843 return dns.name.from_unicode(t, idna_codec=codec)
844
845 self.assertRaises(dns.name.IDNAException, bad1)
846 self.assertRaises(dns.name.IDNAException, bad2)
847 e = dns.name.from_unicode(t, idna_codec=dns.name.IDNA_2008_Practical)
848 self.assertEqual(str(e), "_sip._tcp.xn--knigsgchen-b4a3dun.")
849
850 def testFromUnicodeEscapes(self):
851 n = dns.name.from_unicode(r"\097.\098.\099.")
852 t = n.to_unicode()
853 self.assertEqual(t, "a.b.c.")
854
855 def testToUnicode1(self):
856 n = dns.name.from_text("foo.bar")
857 s = n.to_unicode()
858 self.assertEqual(s, "foo.bar.")
859
860 def testToUnicode2(self):
861 n = dns.name.from_text("foo\u1234bar.bar")
862 s = n.to_unicode()
863 self.assertEqual(s, "foo\u1234bar.bar.")
864
865 def testToUnicode3(self):
866 n = dns.name.from_text("foo.bar")
867 s = n.to_unicode()
868 self.assertEqual(s, "foo.bar.")
869
870 @unittest.skipUnless(
871 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
872 )
873 def testToUnicode4(self):
874 n = dns.name.from_text("ドメイン.テスト", idna_codec=dns.name.IDNA_2008)
875 s = n.to_unicode()
876 self.assertEqual(str(n), "xn--eckwd4c7c.xn--zckzah.")
877 self.assertEqual(s, "ドメイン.テスト.")
878
879 @unittest.skipUnless(
880 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
881 )
882 def testToUnicode6(self):
883 # Test strict 2008 decoding without UTS 46
884 n = dns.name.from_text("xn--gro-7ka.com")
885 self.assertEqual(
886 n.to_unicode(idna_codec=dns.name.IDNA_2008_Strict), "groß.com."
887 )
888
889 def testDefaultDecodeIsJustPunycode(self):
890 # groß.com. in IDNA2008 form, pre-encoded.
891 n = dns.name.from_text("xn--gro-7ka.com")
892 # output using default codec which just decodes the punycode and
893 # doesn't test for IDNA2003 or IDNA2008.
894 self.assertEqual(n.to_unicode(), "groß.com.")
895
896 def testStrictINDA2003Decode(self):
897 # groß.com. in IDNA2008 form, pre-encoded.
898 n = dns.name.from_text("xn--gro-7ka.com")
899
900 def bad():
901 # This throws in IDNA2003 because it doesn't "round trip".
902 n.to_unicode(idna_codec=dns.name.IDNA_2003_Strict)
903
904 self.assertRaises(dns.name.IDNAException, bad)
905
906 def testINDA2008Decode(self):
907 # groß.com. in IDNA2008 form, pre-encoded.
908 n = dns.name.from_text("xn--gro-7ka.com")
909 self.assertEqual(n.to_unicode(idna_codec=dns.name.IDNA_2008), "groß.com.")
910
911 def testToUnicodeOmitFinalDot(self):
912 # groß.com. in IDNA2008 form, pre-encoded.
913 n = dns.name.from_text("xn--gro-7ka.com")
914 self.assertEqual(n.to_unicode(True, dns.name.IDNA_2008), "groß.com")
915
916 def testIDNA2003Misc(self):
917 self.assertEqual(dns.name.IDNA_2003.encode(""), b"")
918 self.assertRaises(
919 dns.name.LabelTooLong, lambda: dns.name.IDNA_2003.encode("x" * 64)
920 )
921
922 @unittest.skipUnless(
923 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
924 )
925 def testIDNA2008Misc(self):
926 self.assertEqual(dns.name.IDNA_2008.encode(""), b"")
927 self.assertRaises(
928 dns.name.LabelTooLong, lambda: dns.name.IDNA_2008.encode("x" * 64)
929 )
930 self.assertRaises(
931 dns.name.LabelTooLong, lambda: dns.name.IDNA_2008.encode("groß" + "x" * 60)
932 )
933
934 def testReverseIPv4(self):
935 e = dns.name.from_text("1.0.0.127.in-addr.arpa.")
936 n = dns.reversename.from_address("127.0.0.1")
937 self.assertEqual(e, n)
938
939 def testReverseIPv6(self):
940 e = dns.name.from_text(
941 "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."
942 )
943 n = dns.reversename.from_address("::1")
944 self.assertEqual(e, n)
945
946 def testReverseIPv6MappedIpv4(self):
947 e = dns.name.from_text("1.0.0.127.in-addr.arpa.")
948 n = dns.reversename.from_address("::ffff:127.0.0.1")
949 self.assertEqual(e, n)
950
951 def testBadReverseIPv4(self):
952 def bad():
953 dns.reversename.from_address("127.0.foo.1")
954
955 self.assertRaises(dns.exception.SyntaxError, bad)
956
957 def testBadReverseIPv6(self):
958 def bad():
959 dns.reversename.from_address("::1::1")
960
961 self.assertRaises(dns.exception.SyntaxError, bad)
962
963 def testReverseIPv4AlternateOrigin(self):
964 e = dns.name.from_text("1.0.0.127.foo.bar.")
965 origin = dns.name.from_text("foo.bar")
966 n = dns.reversename.from_address("127.0.0.1", v4_origin=origin)
967 self.assertEqual(e, n)
968
969 def testReverseIPv6AlternateOrigin(self):
970 e = dns.name.from_text(
971 "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."
972 )
973 origin = dns.name.from_text("foo.bar")
974 n = dns.reversename.from_address("::1", v6_origin=origin)
975 self.assertEqual(e, n)
976
977 def testForwardIPv4(self):
978 n = dns.name.from_text("1.0.0.127.in-addr.arpa.")
979 e = "127.0.0.1"
980 text = dns.reversename.to_address(n)
981 self.assertEqual(text, e)
982
983 def testForwardIPv6(self):
984 n = dns.name.from_text(
985 "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."
986 )
987 e = "::1"
988 text = dns.reversename.to_address(n)
989 self.assertEqual(text, e)
990
991 def testForwardIPv4AlternateOrigin(self):
992 n = dns.name.from_text("1.0.0.127.foo.bar.")
993 e = "127.0.0.1"
994 origin = dns.name.from_text("foo.bar")
995 text = dns.reversename.to_address(n, v4_origin=origin)
996 self.assertEqual(text, e)
997
998 def testForwardIPv6AlternateOrigin(self):
999 n = dns.name.from_text(
1000 "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."
1001 )
1002 e = "::1"
1003 origin = dns.name.from_text("foo.bar")
1004 text = dns.reversename.to_address(n, v6_origin=origin)
1005 self.assertEqual(text, e)
1006
1007 def testUnknownReverseOrigin(self):
1008 n = dns.name.from_text("1.2.3.4.unknown.")
1009 with self.assertRaises(dns.exception.SyntaxError):
1010 dns.reversename.to_address(n)
1011
1012 def testE164ToEnum(self):
1013 text = "+1 650 555 1212"
1014 e = dns.name.from_text("2.1.2.1.5.5.5.0.5.6.1.e164.arpa.")
1015 n = dns.e164.from_e164(text)
1016 self.assertEqual(n, e)
1017
1018 def testEnumToE164(self):
1019 n = dns.name.from_text("2.1.2.1.5.5.5.0.5.6.1.e164.arpa.")
1020 e = "+16505551212"
1021 text = dns.e164.to_e164(n)
1022 self.assertEqual(text, e)
1023
1024 def testBadEnumToE164(self):
1025 n = dns.name.from_text("2.1.2.q.5.5.5.0.5.6.1.e164.arpa.")
1026 self.assertRaises(dns.exception.SyntaxError, lambda: dns.e164.to_e164(n))
1027
1028 def test_incompatible_relations(self):
1029 n1 = dns.name.from_text("example")
1030 n2 = "abc"
1031 for oper in [operator.lt, operator.le, operator.ge, operator.gt]:
1032 self.assertRaises(TypeError, lambda: oper(n1, n2))
1033 self.assertFalse(n1 == n2)
1034 self.assertTrue(n1 != n2)
1035
1036 def testFromUnicodeSimpleEscape(self):
1037 n = dns.name.from_unicode(r"a.\b")
1038 e = dns.name.from_unicode(r"a.b")
1039 self.assertEqual(n, e)
1040
1041 def testFromUnicodeBadEscape(self):
1042 def bad1():
1043 n = dns.name.from_unicode(r"a.b\0q1.c.")
1044
1045 self.assertRaises(dns.name.BadEscape, bad1)
1046
1047 def bad2():
1048 n = dns.name.from_unicode(r"a.b\0")
1049
1050 self.assertRaises(dns.name.BadEscape, bad2)
1051
1052 def testFromUnicodeNotString(self):
1053 def bad():
1054 dns.name.from_unicode(b"123") # type: ignore
1055
1056 self.assertRaises(ValueError, bad)
1057
1058 def testFromUnicodeBadOrigin(self):
1059 def bad():
1060 dns.name.from_unicode("example", 123) # type: ignore
1061
1062 self.assertRaises(ValueError, bad)
1063
1064 def testFromUnicodeEmptyLabel(self):
1065 def bad():
1066 dns.name.from_unicode("a..b.example")
1067
1068 self.assertRaises(dns.name.EmptyLabel, bad)
1069
1070 def testFromUnicodeEmptyName(self):
1071 self.assertEqual(dns.name.from_unicode("@", None), dns.name.empty)
1072
1073 def testFromTextNotString(self):
1074 def bad():
1075 dns.name.from_text(123) # type: ignore
1076
1077 self.assertRaises(ValueError, bad)
1078
1079 def testFromTextBadOrigin(self):
1080 def bad():
1081 dns.name.from_text("example", 123) # type: ignore
1082
1083 self.assertRaises(ValueError, bad)
1084
1085 def testFromWireNotBytes(self):
1086 def bad():
1087 dns.name.from_wire(123, 0) # type: ignore
1088
1089 self.assertRaises(ValueError, bad)
1090
1091 def testBadPunycode(self):
1092 c = dns.name.IDNACodec()
1093 with self.assertRaises(dns.name.IDNAException):
1094 c.decode(b"xn--0000h")
1095
1096 def testRootLabel2003StrictDecode(self):
1097 c = dns.name.IDNA_2003_Strict
1098 self.assertEqual(c.decode(b""), "")
1099
1100 @unittest.skipUnless(
1101 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1102 )
1103 def testRootLabel2008StrictDecode(self):
1104 c = dns.name.IDNA_2008_Strict
1105 self.assertEqual(c.decode(b""), "")
1106
1107 @unittest.skipUnless(
1108 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1109 )
1110 def testCodecNotFoundRaises(self):
1111 dns.name.have_idna_2008 = False
1112 with self.assertRaises(dns.name.NoIDNA2008):
1113 c = dns.name.IDNA2008Codec()
1114 c.encode("Königsgäßchen")
1115 with self.assertRaises(dns.name.NoIDNA2008):
1116 c = dns.name.IDNA2008Codec(strict_decode=True)
1117 c.decode(b"xn--eckwd4c7c.xn--zckzah.")
1118 dns.name.have_idna_2008 = True
1119
1120 @unittest.skipUnless(
1121 dns.name.have_idna_2008, "Python idna cannot be imported; no IDNA2008"
1122 )
1123 def testBadPunycodeStrict2008(self):
1124 c = dns.name.IDNA2008Codec(strict_decode=True)
1125 with self.assertRaises(dns.name.IDNAException):
1126 c.decode(b"xn--0000h")
1127
1128 def testRelativizeSubtractionSyntax(self):
1129 n = dns.name.from_text("foo.example.")
1130 o = dns.name.from_text("example.")
1131 e = dns.name.from_text("foo", None)
1132 self.assertEqual(n - o, e)
1133
1134 def testCopy(self):
1135 n1 = dns.name.from_text("foo.example.")
1136 n2 = copy.copy(n1)
1137 self.assertTrue(n1 is not n2)
1138 # the Name constructor always copies labels, so there is no
1139 # difference between copy and deepcopy
1140 self.assertTrue(n1.labels is not n2.labels)
1141 self.assertEqual(len(n1.labels), len(n2.labels))
1142 for i, l in enumerate(n1.labels):
1143 self.assertTrue(l is n2[i])
1144
1145 def testDeepCopy(self):
1146 n1 = dns.name.from_text("foo.example.")
1147 n2 = copy.deepcopy(n1)
1148 self.assertTrue(n1 is not n2)
1149 self.assertTrue(n1.labels is not n2.labels)
1150 self.assertEqual(len(n1.labels), len(n2.labels))
1151 for i, l in enumerate(n1.labels):
1152 self.assertTrue(l is n2[i])
1153
1154 def testNoAttributeDeletion(self):
1155 n = dns.name.from_text("foo.example.")
1156 with self.assertRaises(TypeError):
1157 del n.labels
1158
1159 def testUnicodeEscapify(self):
1160 n = dns.name.from_unicode("Königsgäßchen;\ttext")
1161 self.assertEqual(n.to_unicode(), "königsgässchen\\;\\009text.")
1162
1163 def test_pickle(self):
1164 n1 = dns.name.from_text("foo.example")
1165 p = pickle.dumps(n1)
1166 n2 = pickle.loads(p)
1167 self.assertEqual(n1, n2)
1168
1169 def test_pad_to_max_name(self):
1170 # Test edge cases in our padding helper.
1171 tests = [
1172 ("o{61}.o{63}.o{63}.o{63}.", "o{61}.o{63}.o{63}.o{63}."),
1173 ("o{60}.o{63}.o{63}.o{63}.", "o{60}.o{63}.o{63}.o{63}."),
1174 ("o{59}.o{63}.o{63}.o{63}.", "+.o{59}.o{63}.o{63}.o{63}."),
1175 ("o{63}.o{63}.o{63}.", "+{61}.o{63}.o{63}.o{63}."),
1176 ("o{63}.o{63}.", "+{61}.+{63}.o{63}.o{63}."),
1177 ]
1178 for name_text, expected_text in tests:
1179 name = dns.name.from_text(expand(name_text))
1180 expected = dns.name.from_text(expand(expected_text))
1181 self.assertEqual(dns.name._pad_to_max_name(name), expected)
1182
1183 def test_predecessors_and_successors(self):
1184 # Test RFC 4471 predecessor and successor methods.
1185
1186 # Here we're actually testing the test suite, but expand() is complicated enough
1187 # to deserve a little testing.
1188 self.assertEqual(expand("f+{3}-o.example."), r"f\255\255\255\000o.example.")
1189
1190 # Ok, now test successors!
1191 origin = dns.name.from_text("example.com.")
1192 tests = [
1193 # Examples from the RFC.
1194 ("foo", True, "\\000.foo"),
1195 ("foo", False, "foo\\000"),
1196 # The syntax here is almost the RFC's "alternate syntax" except that to
1197 # make expand simpler, i.e. not have to understand that \000 was one octet,
1198 # I made the convention that "-" means 0 and "+" means 255. We use raw
1199 # string constants where needed to avoid escaping backslash.
1200 (
1201 "fo{47}.o{63}.o{63}.o{63}",
1202 True,
1203 "fo{47}-.o{63}.o{63}.o{63}",
1204 ),
1205 (
1206 "fo{48}.o{63}.o{63}.o{63}",
1207 True,
1208 "fo{47}p.o{63}.o{63}.o{63}",
1209 ),
1210 ("+{49}.o{63}.o{63}.o{63}", True, "o{62}p.o{63}.o{63}"),
1211 (
1212 "fo{40}+{8}.o{63}.o{63}.o{63}",
1213 True,
1214 "fo{39}p.o{63}.o{63}.o{63}",
1215 ),
1216 (
1217 r"fo{47}\@.o{63}.o{63}.o{63}",
1218 True,
1219 r"fo{47}\[.o{63}.o{63}.o{63}",
1220 ),
1221 ("+{49}.+{63}.+{63}.+{63}", True, ""),
1222 # Some more tests not in the RFC
1223 ("+{49}.+{63}.o{63}.o{63}", True, "o{62}p.o{63}"),
1224 (
1225 "+{48}.o{63}.o{63}.o{63}",
1226 True,
1227 "+{48}-.o{63}.o{63}.o{63}",
1228 ),
1229 ]
1230 for test_origin in [origin, None]:
1231 for name_text, prefix_ok, expected_successor_text in tests:
1232 name = dns.name.from_text(expand(name_text), test_origin)
1233 expected_successor = dns.name.from_text(
1234 expand(expected_successor_text), test_origin
1235 )
1236 successor = name.successor(origin, prefix_ok)
1237 self.assertEqual(successor, expected_successor)
1238 self.assertTrue(
1239 successor > name
1240 or successor == origin
1241 or successor == dns.name.empty
1242 )
1243 # Now test the predecessor
1244 predecessor = successor.predecessor(origin, prefix_ok)
1245 self.assertEqual(predecessor, name)
1246
1247 # Finally, test that a maximal length origin is its own predecessor and
1248 # successor.
1249 origin = dns.name.from_text(expand("+{49}.+{63}.o{63}.o{63}.example.com."))
1250 assert origin.successor(origin, True) == origin
1251 assert origin.predecessor(origin, True) == origin
1252
1253 def test_predecessor_and_successor_errors(self):
1254 name = dns.name.from_text("name", None)
1255 origin = dns.name.from_text("origin", None) # note Relative!
1256 with self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin):
1257 name.successor(origin, True)
1258 with self.assertRaises(dns.name.NeedAbsoluteNameOrOrigin):
1259 name.predecessor(origin, True)
1260 name = dns.name.from_text("name.") # Note absolute
1261 origin = dns.name.from_text("origin.") # 'name' is not a subdomain of 'origin'
1262 with self.assertRaises(dns.name.NeedSubdomainOfOrigin):
1263 name.successor(origin, True)
1264 with self.assertRaises(dns.name.NeedSubdomainOfOrigin):
1265 name.predecessor(origin, True)
1266
1267
1268 if __name__ == "__main__":
1269 unittest.main()