1 | # Copyright (C) 2003-2007, 2009, 2010 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 dns.exception
|
---|
19 | import dns.ipv6
|
---|
20 |
|
---|
21 | class NtoAAtoNTestCase(unittest.TestCase):
|
---|
22 |
|
---|
23 | def test_aton1(self):
|
---|
24 | a = dns.ipv6.inet_aton('::')
|
---|
25 | self.failUnless(a == '\x00' * 16)
|
---|
26 |
|
---|
27 | def test_aton2(self):
|
---|
28 | a = dns.ipv6.inet_aton('::1')
|
---|
29 | self.failUnless(a == '\x00' * 15 + '\x01')
|
---|
30 |
|
---|
31 | def test_aton3(self):
|
---|
32 | a = dns.ipv6.inet_aton('::10.0.0.1')
|
---|
33 | self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
|
---|
34 |
|
---|
35 | def test_aton4(self):
|
---|
36 | a = dns.ipv6.inet_aton('abcd::dcba')
|
---|
37 | self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
|
---|
38 |
|
---|
39 | def test_aton5(self):
|
---|
40 | a = dns.ipv6.inet_aton('1:2:3:4:5:6:7:8')
|
---|
41 | self.failUnless(a == \
|
---|
42 | '00010002000300040005000600070008'.decode('hex_codec'))
|
---|
43 |
|
---|
44 | def test_bad_aton1(self):
|
---|
45 | def bad():
|
---|
46 | a = dns.ipv6.inet_aton('abcd:dcba')
|
---|
47 | self.failUnlessRaises(dns.exception.SyntaxError, bad)
|
---|
48 |
|
---|
49 | def test_bad_aton2(self):
|
---|
50 | def bad():
|
---|
51 | a = dns.ipv6.inet_aton('abcd::dcba::1')
|
---|
52 | self.failUnlessRaises(dns.exception.SyntaxError, bad)
|
---|
53 |
|
---|
54 | def test_bad_aton3(self):
|
---|
55 | def bad():
|
---|
56 | a = dns.ipv6.inet_aton('1:2:3:4:5:6:7:8:9')
|
---|
57 | self.failUnlessRaises(dns.exception.SyntaxError, bad)
|
---|
58 |
|
---|
59 | def test_aton1(self):
|
---|
60 | a = dns.ipv6.inet_aton('::')
|
---|
61 | self.failUnless(a == '\x00' * 16)
|
---|
62 |
|
---|
63 | def test_aton2(self):
|
---|
64 | a = dns.ipv6.inet_aton('::1')
|
---|
65 | self.failUnless(a == '\x00' * 15 + '\x01')
|
---|
66 |
|
---|
67 | def test_aton3(self):
|
---|
68 | a = dns.ipv6.inet_aton('::10.0.0.1')
|
---|
69 | self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
|
---|
70 |
|
---|
71 | def test_aton4(self):
|
---|
72 | a = dns.ipv6.inet_aton('abcd::dcba')
|
---|
73 | self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
|
---|
74 |
|
---|
75 | def test_ntoa1(self):
|
---|
76 | b = '00010002000300040005000600070008'.decode('hex_codec')
|
---|
77 | t = dns.ipv6.inet_ntoa(b)
|
---|
78 | self.failUnless(t == '1:2:3:4:5:6:7:8')
|
---|
79 |
|
---|
80 | def test_ntoa2(self):
|
---|
81 | b = '\x00' * 16
|
---|
82 | t = dns.ipv6.inet_ntoa(b)
|
---|
83 | self.failUnless(t == '::')
|
---|
84 |
|
---|
85 | def test_ntoa3(self):
|
---|
86 | b = '\x00' * 15 + '\x01'
|
---|
87 | t = dns.ipv6.inet_ntoa(b)
|
---|
88 | self.failUnless(t == '::1')
|
---|
89 |
|
---|
90 | def test_ntoa4(self):
|
---|
91 | b = '\x80' + '\x00' * 15
|
---|
92 | t = dns.ipv6.inet_ntoa(b)
|
---|
93 | self.failUnless(t == '8000::')
|
---|
94 |
|
---|
95 | def test_ntoa5(self):
|
---|
96 | b = '\x01\xcd' + '\x00' * 12 + '\x03\xef'
|
---|
97 | t = dns.ipv6.inet_ntoa(b)
|
---|
98 | self.failUnless(t == '1cd::3ef')
|
---|
99 |
|
---|
100 | def test_ntoa6(self):
|
---|
101 | b = 'ffff00000000ffff000000000000ffff'.decode('hex_codec')
|
---|
102 | t = dns.ipv6.inet_ntoa(b)
|
---|
103 | self.failUnless(t == 'ffff:0:0:ffff::ffff')
|
---|
104 |
|
---|
105 | def test_ntoa7(self):
|
---|
106 | b = '00000000ffff000000000000ffffffff'.decode('hex_codec')
|
---|
107 | t = dns.ipv6.inet_ntoa(b)
|
---|
108 | self.failUnless(t == '0:0:ffff::ffff:ffff')
|
---|
109 |
|
---|
110 | def test_ntoa8(self):
|
---|
111 | b = 'ffff0000ffff00000000ffff00000000'.decode('hex_codec')
|
---|
112 | t = dns.ipv6.inet_ntoa(b)
|
---|
113 | self.failUnless(t == 'ffff:0:ffff::ffff:0:0')
|
---|
114 |
|
---|
115 | def test_ntoa9(self):
|
---|
116 | b = '0000000000000000000000000a000001'.decode('hex_codec')
|
---|
117 | t = dns.ipv6.inet_ntoa(b)
|
---|
118 | self.failUnless(t == '::10.0.0.1')
|
---|
119 |
|
---|
120 | def test_ntoa10(self):
|
---|
121 | b = '0000000000000000000000010a000001'.decode('hex_codec')
|
---|
122 | t = dns.ipv6.inet_ntoa(b)
|
---|
123 | self.failUnless(t == '::1:a00:1')
|
---|
124 |
|
---|
125 | def test_ntoa11(self):
|
---|
126 | b = '00000000000000000000ffff0a000001'.decode('hex_codec')
|
---|
127 | t = dns.ipv6.inet_ntoa(b)
|
---|
128 | self.failUnless(t == '::ffff:10.0.0.1')
|
---|
129 |
|
---|
130 | def test_ntoa12(self):
|
---|
131 | b = '000000000000000000000000ffffffff'.decode('hex_codec')
|
---|
132 | t = dns.ipv6.inet_ntoa(b)
|
---|
133 | self.failUnless(t == '::255.255.255.255')
|
---|
134 |
|
---|
135 | def test_ntoa13(self):
|
---|
136 | b = '00000000000000000000ffffffffffff'.decode('hex_codec')
|
---|
137 | t = dns.ipv6.inet_ntoa(b)
|
---|
138 | self.failUnless(t == '::ffff:255.255.255.255')
|
---|
139 |
|
---|
140 | def test_ntoa14(self):
|
---|
141 | b = '0000000000000000000000000001ffff'.decode('hex_codec')
|
---|
142 | t = dns.ipv6.inet_ntoa(b)
|
---|
143 | self.failUnless(t == '::0.1.255.255')
|
---|
144 |
|
---|
145 | def test_bad_ntoa1(self):
|
---|
146 | def bad():
|
---|
147 | a = dns.ipv6.inet_ntoa('')
|
---|
148 | self.failUnlessRaises(ValueError, bad)
|
---|
149 |
|
---|
150 | def test_bad_ntoa2(self):
|
---|
151 | def bad():
|
---|
152 | a = dns.ipv6.inet_ntoa('\x00' * 17)
|
---|
153 | self.failUnlessRaises(ValueError, bad)
|
---|
154 |
|
---|
155 | if __name__ == '__main__':
|
---|
156 | unittest.main()
|
---|