e30e43d3a221eb1fbb6e4b99560deb836037ad5e
[bbaumbach/samba-autobuild/.git] / lib / dnspython / tests / name.py
1 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
2 #
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 import unittest
17
18 import cStringIO
19 import socket
20
21 import dns.name
22 import dns.reversename
23 import dns.e164
24
25 class NameTestCase(unittest.TestCase):
26     def setUp(self):
27         self.origin = dns.name.from_text('example.')
28         
29     def testFromTextRel1(self):
30         n = dns.name.from_text('foo.bar')
31         self.failUnless(n.labels == ('foo', 'bar', ''))
32
33     def testFromTextRel2(self):
34         n = dns.name.from_text('foo.bar', origin=self.origin)
35         self.failUnless(n.labels == ('foo', 'bar', 'example', ''))
36
37     def testFromTextRel3(self):
38         n = dns.name.from_text('foo.bar', origin=None)
39         self.failUnless(n.labels == ('foo', 'bar'))
40
41     def testFromTextRel4(self):
42         n = dns.name.from_text('@', origin=None)
43         self.failUnless(n == dns.name.empty)
44
45     def testFromTextRel5(self):
46         n = dns.name.from_text('@', origin=self.origin)
47         self.failUnless(n == self.origin)
48
49     def testFromTextAbs1(self):
50         n = dns.name.from_text('foo.bar.')
51         self.failUnless(n.labels == ('foo', 'bar', ''))
52
53     def testTortureFromText(self):
54         good = [
55             r'.',
56             r'a',
57             r'a.',
58             r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
59             r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
60             r'\000.\008.\010.\032.\046.\092.\099.\255',
61             r'\\',
62             r'\..\.',
63             r'\\.\\',
64             r'!"#%&/()=+-',
65             r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255',
66             ]
67         bad = [
68             r'..',
69             r'.a',
70             r'\\..',
71             '\\',               # yes, we don't want the 'r' prefix!
72             r'\0',
73             r'\00',
74             r'\00Z',
75             r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
76             r'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
77             r'\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255.\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255\255',
78             ]
79         for t in good:
80             try:
81                 n = dns.name.from_text(t)
82             except:
83                 self.fail("good test '%s' raised an exception" % t)
84         for t in bad:
85             caught = False
86             try:
87                 n = dns.name.from_text(t)
88             except:
89                 caught = True
90             if not caught:
91                 self.fail("bad test '%s' did not raise an exception" % t)
92
93     def testImmutable1(self):
94         def bad():
95             self.origin.labels = ()
96         self.failUnlessRaises(TypeError, bad)
97
98     def testImmutable2(self):
99         def bad():
100             self.origin.labels[0] = 'foo'
101         self.failUnlessRaises(TypeError, bad)
102
103     def testAbs1(self):
104         self.failUnless(dns.name.root.is_absolute())
105
106     def testAbs2(self):
107         self.failUnless(not dns.name.empty.is_absolute())
108
109     def testAbs3(self):
110         self.failUnless(self.origin.is_absolute())
111
112     def testAbs3(self):
113         n = dns.name.from_text('foo', origin=None)
114         self.failUnless(not n.is_absolute())
115
116     def testWild1(self):
117         n = dns.name.from_text('*.foo', origin=None)
118         self.failUnless(n.is_wild())
119
120     def testWild2(self):
121         n = dns.name.from_text('*a.foo', origin=None)
122         self.failUnless(not n.is_wild())
123
124     def testWild3(self):
125         n = dns.name.from_text('a.*.foo', origin=None)
126         self.failUnless(not n.is_wild())
127
128     def testWild4(self):
129         self.failUnless(not dns.name.root.is_wild())
130
131     def testWild5(self):
132         self.failUnless(not dns.name.empty.is_wild())
133
134     def testHash1(self):
135         n1 = dns.name.from_text('fOo.COM')
136         n2 = dns.name.from_text('foo.com')
137         self.failUnless(hash(n1) == hash(n2))
138
139     def testCompare1(self):
140         n1 = dns.name.from_text('a')
141         n2 = dns.name.from_text('b')
142         self.failUnless(n1 < n2)
143         self.failUnless(n2 > n1)
144
145     def testCompare2(self):
146         n1 = dns.name.from_text('')
147         n2 = dns.name.from_text('b')
148         self.failUnless(n1 < n2)
149         self.failUnless(n2 > n1)
150
151     def testCompare3(self):
152         self.failUnless(dns.name.empty < dns.name.root)
153         self.failUnless(dns.name.root > dns.name.empty)
154
155     def testCompare4(self):
156         self.failUnless(dns.name.root != 1)
157
158     def testCompare5(self):
159         self.failUnless(dns.name.root < 1 or dns.name.root > 1)
160
161     def testSubdomain1(self):
162         self.failUnless(not dns.name.empty.is_subdomain(dns.name.root))
163
164     def testSubdomain2(self):
165         self.failUnless(not dns.name.root.is_subdomain(dns.name.empty))
166
167     def testSubdomain3(self):
168         n = dns.name.from_text('foo', origin=self.origin)
169         self.failUnless(n.is_subdomain(self.origin))
170
171     def testSubdomain4(self):
172         n = dns.name.from_text('foo', origin=self.origin)
173         self.failUnless(n.is_subdomain(dns.name.root))
174
175     def testSubdomain5(self):
176         n = dns.name.from_text('foo', origin=self.origin)
177         self.failUnless(n.is_subdomain(n))
178
179     def testSuperdomain1(self):
180         self.failUnless(not dns.name.empty.is_superdomain(dns.name.root))
181
182     def testSuperdomain2(self):
183         self.failUnless(not dns.name.root.is_superdomain(dns.name.empty))
184
185     def testSuperdomain3(self):
186         n = dns.name.from_text('foo', origin=self.origin)
187         self.failUnless(self.origin.is_superdomain(n))
188
189     def testSuperdomain4(self):
190         n = dns.name.from_text('foo', origin=self.origin)
191         self.failUnless(dns.name.root.is_superdomain(n))
192
193     def testSuperdomain5(self):
194         n = dns.name.from_text('foo', origin=self.origin)
195         self.failUnless(n.is_superdomain(n))
196
197     def testCanonicalize1(self):
198         n = dns.name.from_text('FOO.bar', origin=self.origin)
199         c = n.canonicalize()
200         self.failUnless(c.labels == ('foo', 'bar', 'example', ''))
201
202     def testToText1(self):
203         n = dns.name.from_text('FOO.bar', origin=self.origin)
204         t = n.to_text()
205         self.failUnless(t == 'FOO.bar.example.')
206
207     def testToText2(self):
208         n = dns.name.from_text('FOO.bar', origin=self.origin)
209         t = n.to_text(True)
210         self.failUnless(t == 'FOO.bar.example')
211
212     def testToText3(self):
213         n = dns.name.from_text('FOO.bar', origin=None)
214         t = n.to_text()
215         self.failUnless(t == 'FOO.bar')
216
217     def testToText4(self):
218         t = dns.name.empty.to_text()
219         self.failUnless(t == '@')
220
221     def testToText5(self):
222         t = dns.name.root.to_text()
223         self.failUnless(t == '.')
224
225     def testToText6(self):
226         n = dns.name.from_text('FOO bar', origin=None)
227         t = n.to_text()
228         self.failUnless(t == r'FOO\032bar')
229
230     def testToText7(self):
231         n = dns.name.from_text(r'FOO\.bar', origin=None)
232         t = n.to_text()
233         self.failUnless(t == r'FOO\.bar')
234
235     def testToText8(self):
236         n = dns.name.from_text(r'\070OO\.bar', origin=None)
237         t = n.to_text()
238         self.failUnless(t == r'FOO\.bar')
239
240     def testSlice1(self):
241         n = dns.name.from_text(r'a.b.c.', origin=None)
242         s = n[:]
243         self.failUnless(s == ('a', 'b', 'c', ''))
244
245     def testSlice2(self):
246         n = dns.name.from_text(r'a.b.c.', origin=None)
247         s = n[:2]
248         self.failUnless(s == ('a', 'b'))
249
250     def testSlice3(self):
251         n = dns.name.from_text(r'a.b.c.', origin=None)
252         s = n[2:]
253         self.failUnless(s == ('c', ''))
254
255     def testEmptyLabel1(self):
256         def bad():
257             n = dns.name.Name(['a', '', 'b'])
258         self.failUnlessRaises(dns.name.EmptyLabel, bad)
259
260     def testEmptyLabel2(self):
261         def bad():
262             n = dns.name.Name(['', 'b'])
263         self.failUnlessRaises(dns.name.EmptyLabel, bad)
264
265     def testEmptyLabel3(self):
266         n = dns.name.Name(['b', ''])
267         self.failUnless(n)
268
269     def testLongLabel(self):
270         n = dns.name.Name(['a' * 63])
271         self.failUnless(n)
272
273     def testLabelTooLong(self):
274         def bad():
275             n = dns.name.Name(['a' * 64, 'b'])
276         self.failUnlessRaises(dns.name.LabelTooLong, bad)
277
278     def testLongName(self):
279         n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 62])
280         self.failUnless(n)
281
282     def testNameTooLong(self):
283         def bad():
284             n = dns.name.Name(['a' * 63, 'a' * 63, 'a' * 63, 'a' * 63])
285         self.failUnlessRaises(dns.name.NameTooLong, bad)
286
287     def testConcat1(self):
288         n1 = dns.name.Name(['a', 'b'])
289         n2 = dns.name.Name(['c', 'd'])
290         e = dns.name.Name(['a', 'b', 'c', 'd'])
291         r = n1 + n2
292         self.failUnless(r == e)
293
294     def testConcat2(self):
295         n1 = dns.name.Name(['a', 'b'])
296         n2 = dns.name.Name([])
297         e = dns.name.Name(['a', 'b'])
298         r = n1 + n2
299         self.failUnless(r == e)
300
301     def testConcat2(self):
302         n1 = dns.name.Name([])
303         n2 = dns.name.Name(['a', 'b'])
304         e = dns.name.Name(['a', 'b'])
305         r = n1 + n2
306         self.failUnless(r == e)
307
308     def testConcat3(self):
309         n1 = dns.name.Name(['a', 'b', ''])
310         n2 = dns.name.Name([])
311         e = dns.name.Name(['a', 'b', ''])
312         r = n1 + n2
313         self.failUnless(r == e)
314
315     def testConcat4(self):
316         n1 = dns.name.Name(['a', 'b'])
317         n2 = dns.name.Name(['c', ''])
318         e = dns.name.Name(['a', 'b', 'c', ''])
319         r = n1 + n2
320         self.failUnless(r == e)
321
322     def testConcat5(self):
323         def bad():
324             n1 = dns.name.Name(['a', 'b', ''])
325             n2 = dns.name.Name(['c'])
326             r = n1 + n2
327         self.failUnlessRaises(dns.name.AbsoluteConcatenation, bad)
328
329     def testBadEscape(self):
330         def bad():
331             n = dns.name.from_text(r'a.b\0q1.c.')
332             print n
333         self.failUnlessRaises(dns.name.BadEscape, bad)
334
335     def testDigestable1(self):
336         n = dns.name.from_text('FOO.bar')
337         d = n.to_digestable()
338         self.failUnless(d == '\x03foo\x03bar\x00')
339
340     def testDigestable2(self):
341         n1 = dns.name.from_text('FOO.bar')
342         n2 = dns.name.from_text('foo.BAR.')
343         d1 = n1.to_digestable()
344         d2 = n2.to_digestable()
345         self.failUnless(d1 == d2)
346
347     def testDigestable3(self):
348         d = dns.name.root.to_digestable()
349         self.failUnless(d == '\x00')
350
351     def testDigestable4(self):
352         n = dns.name.from_text('FOO.bar', None)
353         d = n.to_digestable(dns.name.root)
354         self.failUnless(d == '\x03foo\x03bar\x00')
355         
356     def testBadDigestable(self):
357         def bad():
358             n = dns.name.from_text('FOO.bar', None)
359             d = n.to_digestable()
360         self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad)
361
362     def testToWire1(self):
363         n = dns.name.from_text('FOO.bar')
364         f = cStringIO.StringIO()
365         compress = {}
366         n.to_wire(f, compress)
367         self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00')
368
369     def testToWire2(self):
370         n = dns.name.from_text('FOO.bar')
371         f = cStringIO.StringIO()
372         compress = {}
373         n.to_wire(f, compress)
374         n.to_wire(f, compress)
375         self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00')
376
377     def testToWire3(self):
378         n1 = dns.name.from_text('FOO.bar')
379         n2 = dns.name.from_text('foo.bar')
380         f = cStringIO.StringIO()
381         compress = {}
382         n1.to_wire(f, compress)
383         n2.to_wire(f, compress)
384         self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\xc0\x00')
385
386     def testToWire4(self):
387         n1 = dns.name.from_text('FOO.bar')
388         n2 = dns.name.from_text('a.foo.bar')
389         f = cStringIO.StringIO()
390         compress = {}
391         n1.to_wire(f, compress)
392         n2.to_wire(f, compress)
393         self.failUnless(f.getvalue() == '\x03FOO\x03bar\x00\x01\x61\xc0\x00')
394
395     def testToWire5(self):
396         n1 = dns.name.from_text('FOO.bar')
397         n2 = dns.name.from_text('a.foo.bar')
398         f = cStringIO.StringIO()
399         compress = {}
400         n1.to_wire(f, compress)
401         n2.to_wire(f, None)
402         self.failUnless(f.getvalue() == \
403                         '\x03FOO\x03bar\x00\x01\x61\x03foo\x03bar\x00')
404
405     def testToWire6(self):
406         n = dns.name.from_text('FOO.bar')
407         v = n.to_wire()
408         self.failUnless(v == '\x03FOO\x03bar\x00')
409
410     def testBadToWire(self):
411         def bad():
412             n = dns.name.from_text('FOO.bar', None)
413             f = cStringIO.StringIO()
414             compress = {}
415             n.to_wire(f, compress)
416         self.failUnlessRaises(dns.name.NeedAbsoluteNameOrOrigin, bad)
417
418     def testSplit1(self):
419         n = dns.name.from_text('foo.bar.')
420         (prefix, suffix) = n.split(2)
421         ep = dns.name.from_text('foo', None)
422         es = dns.name.from_text('bar.', None)
423         self.failUnless(prefix == ep and suffix == es)
424
425     def testSplit2(self):
426         n = dns.name.from_text('foo.bar.')
427         (prefix, suffix) = n.split(1)
428         ep = dns.name.from_text('foo.bar', None)
429         es = dns.name.from_text('.', None)
430         self.failUnless(prefix == ep and suffix == es)
431
432     def testSplit3(self):
433         n = dns.name.from_text('foo.bar.')
434         (prefix, suffix) = n.split(0)
435         ep = dns.name.from_text('foo.bar.', None)
436         es = dns.name.from_text('', None)
437         self.failUnless(prefix == ep and suffix == es)
438
439     def testSplit4(self):
440         n = dns.name.from_text('foo.bar.')
441         (prefix, suffix) = n.split(3)
442         ep = dns.name.from_text('', None)
443         es = dns.name.from_text('foo.bar.', None)
444         self.failUnless(prefix == ep and suffix == es)
445
446     def testBadSplit1(self):
447         def bad():
448             n = dns.name.from_text('foo.bar.')
449             (prefix, suffix) = n.split(-1)
450         self.failUnlessRaises(ValueError, bad)
451
452     def testBadSplit2(self):
453         def bad():
454             n = dns.name.from_text('foo.bar.')
455             (prefix, suffix) = n.split(4)
456         self.failUnlessRaises(ValueError, bad)
457
458     def testRelativize1(self):
459         n = dns.name.from_text('a.foo.bar.', None)
460         o = dns.name.from_text('bar.', None)
461         e = dns.name.from_text('a.foo', None)
462         self.failUnless(n.relativize(o) == e)
463
464     def testRelativize2(self):
465         n = dns.name.from_text('a.foo.bar.', None)
466         o = n
467         e = dns.name.empty
468         self.failUnless(n.relativize(o) == e)
469
470     def testRelativize3(self):
471         n = dns.name.from_text('a.foo.bar.', None)
472         o = dns.name.from_text('blaz.', None)
473         e = n
474         self.failUnless(n.relativize(o) == e)
475
476     def testRelativize4(self):
477         n = dns.name.from_text('a.foo', None)
478         o = dns.name.root
479         e = n
480         self.failUnless(n.relativize(o) == e)
481
482     def testDerelativize1(self):
483         n = dns.name.from_text('a.foo', None)
484         o = dns.name.from_text('bar.', None)
485         e = dns.name.from_text('a.foo.bar.', None)
486         self.failUnless(n.derelativize(o) == e)
487
488     def testDerelativize2(self):
489         n = dns.name.empty
490         o = dns.name.from_text('a.foo.bar.', None)
491         e = o
492         self.failUnless(n.derelativize(o) == e)
493
494     def testDerelativize3(self):
495         n = dns.name.from_text('a.foo.bar.', None)
496         o = dns.name.from_text('blaz.', None)
497         e = n
498         self.failUnless(n.derelativize(o) == e)
499
500     def testChooseRelativity1(self):
501         n = dns.name.from_text('a.foo.bar.', None)
502         o = dns.name.from_text('bar.', None)
503         e = dns.name.from_text('a.foo', None)
504         self.failUnless(n.choose_relativity(o, True) == e)
505
506     def testChooseRelativity2(self):
507         n = dns.name.from_text('a.foo.bar.', None)
508         o = dns.name.from_text('bar.', None)
509         e = n
510         self.failUnless(n.choose_relativity(o, False) == e)
511
512     def testChooseRelativity3(self):
513         n = dns.name.from_text('a.foo', None)
514         o = dns.name.from_text('bar.', None)
515         e = dns.name.from_text('a.foo.bar.', None)
516         self.failUnless(n.choose_relativity(o, False) == e)
517
518     def testChooseRelativity4(self):
519         n = dns.name.from_text('a.foo', None)
520         o = None
521         e = n
522         self.failUnless(n.choose_relativity(o, True) == e)
523
524     def testChooseRelativity5(self):
525         n = dns.name.from_text('a.foo', None)
526         o = None
527         e = n
528         self.failUnless(n.choose_relativity(o, False) == e)
529
530     def testChooseRelativity6(self):
531         n = dns.name.from_text('a.foo.', None)
532         o = None
533         e = n
534         self.failUnless(n.choose_relativity(o, True) == e)
535
536     def testChooseRelativity7(self):
537         n = dns.name.from_text('a.foo.', None)
538         o = None
539         e = n
540         self.failUnless(n.choose_relativity(o, False) == e)
541
542     def testFromWire1(self):
543         w = '\x03foo\x00\xc0\x00'
544         (n1, cused1) = dns.name.from_wire(w, 0)
545         (n2, cused2) = dns.name.from_wire(w, cused1)
546         en1 = dns.name.from_text('foo.')
547         en2 = en1
548         ecused1 = 5
549         ecused2 = 2
550         self.failUnless(n1 == en1 and cused1 == ecused1 and \
551                         n2 == en2 and cused2 == ecused2)
552
553     def testFromWire1(self):
554         w = '\x03foo\x00\x01a\xc0\x00\x01b\xc0\x05'
555         current = 0
556         (n1, cused1) = dns.name.from_wire(w, current)
557         current += cused1
558         (n2, cused2) = dns.name.from_wire(w, current)
559         current += cused2
560         (n3, cused3) = dns.name.from_wire(w, current)
561         en1 = dns.name.from_text('foo.')
562         en2 = dns.name.from_text('a.foo.')
563         en3 = dns.name.from_text('b.a.foo.')
564         ecused1 = 5
565         ecused2 = 4
566         ecused3 = 4
567         self.failUnless(n1 == en1 and cused1 == ecused1 and \
568                         n2 == en2 and cused2 == ecused2 and \
569                         n3 == en3 and cused3 == ecused3)
570
571     def testBadFromWire1(self):
572         def bad():
573             w = '\x03foo\xc0\x04'
574             (n, cused) = dns.name.from_wire(w, 0)
575         self.failUnlessRaises(dns.name.BadPointer, bad)
576
577     def testBadFromWire2(self):
578         def bad():
579             w = '\x03foo\xc0\x05'
580             (n, cused) = dns.name.from_wire(w, 0)
581         self.failUnlessRaises(dns.name.BadPointer, bad)
582
583     def testBadFromWire3(self):
584         def bad():
585             w = '\xbffoo'
586             (n, cused) = dns.name.from_wire(w, 0)
587         self.failUnlessRaises(dns.name.BadLabelType, bad)
588
589     def testBadFromWire4(self):
590         def bad():
591             w = '\x41foo'
592             (n, cused) = dns.name.from_wire(w, 0)
593         self.failUnlessRaises(dns.name.BadLabelType, bad)
594
595     def testParent1(self):
596         n = dns.name.from_text('foo.bar.')
597         self.failUnless(n.parent() == dns.name.from_text('bar.'))
598         self.failUnless(n.parent().parent() == dns.name.root)
599
600     def testParent2(self):
601         n = dns.name.from_text('foo.bar', None)
602         self.failUnless(n.parent() == dns.name.from_text('bar', None))
603         self.failUnless(n.parent().parent() == dns.name.empty)
604
605     def testParent3(self):
606         def bad():
607             n = dns.name.root
608             n.parent()
609         self.failUnlessRaises(dns.name.NoParent, bad)
610
611     def testParent4(self):
612         def bad():
613             n = dns.name.empty
614             n.parent()
615         self.failUnlessRaises(dns.name.NoParent, bad)
616
617     def testFromUnicode1(self):
618         n = dns.name.from_text(u'foo.bar')
619         self.failUnless(n.labels == ('foo', 'bar', ''))
620
621     def testFromUnicode2(self):
622         n = dns.name.from_text(u'foo\u1234bar.bar')
623         self.failUnless(n.labels == ('xn--foobar-r5z', 'bar', ''))
624
625     def testFromUnicodeAlternateDot1(self):
626         n = dns.name.from_text(u'foo\u3002bar')
627         self.failUnless(n.labels == ('foo', 'bar', ''))
628
629     def testFromUnicodeAlternateDot2(self):
630         n = dns.name.from_text(u'foo\uff0ebar')
631         self.failUnless(n.labels == ('foo', 'bar', ''))
632
633     def testFromUnicodeAlternateDot3(self):
634         n = dns.name.from_text(u'foo\uff61bar')
635         self.failUnless(n.labels == ('foo', 'bar', ''))
636
637     def testToUnicode1(self):
638         n = dns.name.from_text(u'foo.bar')
639         s = n.to_unicode()
640         self.failUnless(s == u'foo.bar.')
641
642     def testToUnicode2(self):
643         n = dns.name.from_text(u'foo\u1234bar.bar')
644         s = n.to_unicode()
645         self.failUnless(s == u'foo\u1234bar.bar.')
646
647     def testToUnicode3(self):
648         n = dns.name.from_text('foo.bar')
649         s = n.to_unicode()
650         self.failUnless(s == u'foo.bar.')
651
652     def testReverseIPv4(self):
653         e = dns.name.from_text('1.0.0.127.in-addr.arpa.')
654         n = dns.reversename.from_address('127.0.0.1')
655         self.failUnless(e == n)
656
657     def testReverseIPv6(self):
658         e = dns.name.from_text('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.')
659         n = dns.reversename.from_address('::1')
660         self.failUnless(e == n)
661
662     def testBadReverseIPv4(self):
663         def bad():
664             n = dns.reversename.from_address('127.0.foo.1')
665         self.failUnlessRaises(dns.exception.SyntaxError, bad)
666
667     def testBadReverseIPv6(self):
668         def bad():
669             n = dns.reversename.from_address('::1::1')
670         self.failUnlessRaises(dns.exception.SyntaxError, bad)
671
672     def testForwardIPv4(self):
673         n = dns.name.from_text('1.0.0.127.in-addr.arpa.')
674         e = '127.0.0.1'
675         text = dns.reversename.to_address(n)
676         self.failUnless(text == e)
677
678     def testForwardIPv6(self):
679         n = dns.name.from_text('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.')
680         e = '::1'
681         text = dns.reversename.to_address(n)
682         self.failUnless(text == e)
683
684     def testE164ToEnum(self):
685         text = '+1 650 555 1212'
686         e = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.')
687         n = dns.e164.from_e164(text)
688         self.failUnless(n == e)
689
690     def testEnumToE164(self):
691         n = dns.name.from_text('2.1.2.1.5.5.5.0.5.6.1.e164.arpa.')
692         e = '+16505551212'
693         text = dns.e164.to_e164(n)
694         self.failUnless(text == e)
695
696 if __name__ == '__main__':
697     unittest.main()