1 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
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.
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.
23 aton4
= dns
.ipv4
.inet_aton
24 ntoa4
= dns
.ipv4
.inet_ntoa
25 aton6
= dns
.ipv6
.inet_aton
26 ntoa6
= dns
.ipv6
.inet_ntoa
28 v4_bad_addrs
= ['256.1.1.1', '1.1.1', '1.1.1.1.1', '01.1.1.1',
29 '+1.1.1.1', '1.1.1.1+', '1..2.3.4', '.1.2.3.4',
32 class NtoAAtoNTestCase(unittest
.TestCase
):
36 self
.failUnless(a
== '\x00' * 16)
40 self
.failUnless(a
== '\x00' * 15 + '\x01')
43 a
= aton6('::10.0.0.1')
44 self
.failUnless(a
== '\x00' * 12 + '\x0a\x00\x00\x01')
47 a
= aton6('abcd::dcba')
48 self
.failUnless(a
== '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
51 a
= aton6('1:2:3:4:5:6:7:8')
52 self
.failUnless(a
== \
53 '00010002000300040005000600070008'.decode('hex_codec'))
55 def test_bad_aton1(self
):
57 a
= aton6('abcd:dcba')
58 self
.failUnlessRaises(dns
.exception
.SyntaxError, bad
)
60 def test_bad_aton2(self
):
62 a
= aton6('abcd::dcba::1')
63 self
.failUnlessRaises(dns
.exception
.SyntaxError, bad
)
65 def test_bad_aton3(self
):
67 a
= aton6('1:2:3:4:5:6:7:8:9')
68 self
.failUnlessRaises(dns
.exception
.SyntaxError, bad
)
72 self
.failUnless(a
== '\x00' * 16)
76 self
.failUnless(a
== '\x00' * 15 + '\x01')
79 a
= aton6('::10.0.0.1')
80 self
.failUnless(a
== '\x00' * 12 + '\x0a\x00\x00\x01')
83 a
= aton6('abcd::dcba')
84 self
.failUnless(a
== '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
87 b
= '00010002000300040005000600070008'.decode('hex_codec')
89 self
.failUnless(t
== '1:2:3:4:5:6:7:8')
94 self
.failUnless(t
== '::')
97 b
= '\x00' * 15 + '\x01'
99 self
.failUnless(t
== '::1')
101 def test_ntoa4(self
):
102 b
= '\x80' + '\x00' * 15
104 self
.failUnless(t
== '8000::')
106 def test_ntoa5(self
):
107 b
= '\x01\xcd' + '\x00' * 12 + '\x03\xef'
109 self
.failUnless(t
== '1cd::3ef')
111 def test_ntoa6(self
):
112 b
= 'ffff00000000ffff000000000000ffff'.decode('hex_codec')
114 self
.failUnless(t
== 'ffff:0:0:ffff::ffff')
116 def test_ntoa7(self
):
117 b
= '00000000ffff000000000000ffffffff'.decode('hex_codec')
119 self
.failUnless(t
== '0:0:ffff::ffff:ffff')
121 def test_ntoa8(self
):
122 b
= 'ffff0000ffff00000000ffff00000000'.decode('hex_codec')
124 self
.failUnless(t
== 'ffff:0:ffff::ffff:0:0')
126 def test_ntoa9(self
):
127 b
= '0000000000000000000000000a000001'.decode('hex_codec')
129 self
.failUnless(t
== '::10.0.0.1')
131 def test_ntoa10(self
):
132 b
= '0000000000000000000000010a000001'.decode('hex_codec')
134 self
.failUnless(t
== '::1:a00:1')
136 def test_ntoa11(self
):
137 b
= '00000000000000000000ffff0a000001'.decode('hex_codec')
139 self
.failUnless(t
== '::ffff:10.0.0.1')
141 def test_ntoa12(self
):
142 b
= '000000000000000000000000ffffffff'.decode('hex_codec')
144 self
.failUnless(t
== '::255.255.255.255')
146 def test_ntoa13(self
):
147 b
= '00000000000000000000ffffffffffff'.decode('hex_codec')
149 self
.failUnless(t
== '::ffff:255.255.255.255')
151 def test_ntoa14(self
):
152 b
= '0000000000000000000000000001ffff'.decode('hex_codec')
154 self
.failUnless(t
== '::0.1.255.255')
156 def test_bad_ntoa1(self
):
159 self
.failUnlessRaises(ValueError, bad
)
161 def test_bad_ntoa2(self
):
163 a
= ntoa6('\x00' * 17)
164 self
.failUnlessRaises(ValueError, bad
)
166 def test_good_v4_aton(self
):
167 pairs
= [('1.2.3.4', '\x01\x02\x03\x04'),
168 ('255.255.255.255', '\xff\xff\xff\xff'),
169 ('0.0.0.0', '\x00\x00\x00\x00')]
173 self
.failUnless(b1
== b
)
174 self
.failUnless(t1
== t
)
176 def test_bad_v4_aton(self
):
181 for addr
in v4_bad_addrs
:
182 self
.failUnlessRaises(dns
.exception
.SyntaxError, make_bad(addr
))
184 def test_bad_v6_aton(self
):
185 addrs
= ['+::0', '0::0::', '::0::', '1:2:3:4:5:6:7:8:9',
187 embedded
= ['::' + x
for x
in v4_bad_addrs
]
188 addrs
.extend(embedded
)
194 self
.failUnlessRaises(dns
.exception
.SyntaxError, make_bad(addr
))
196 if __name__
== '__main__':