s3:autoconf add non_posix_acls to NFS4ACL_OBJ
[Samba.git] / lib / dnspython / tests / ntoaaton.py
blob9d8bedd702d92679601ab7e597b858901bd17cb3
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.
16 import unittest
18 import dns.exception
19 import dns.ipv4
20 import dns.ipv6
22 # for convenience
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',
30 '1.2.3.4.']
32 class NtoAAtoNTestCase(unittest.TestCase):
34 def test_aton1(self):
35 a = aton6('::')
36 self.failUnless(a == '\x00' * 16)
38 def test_aton2(self):
39 a = aton6('::1')
40 self.failUnless(a == '\x00' * 15 + '\x01')
42 def test_aton3(self):
43 a = aton6('::10.0.0.1')
44 self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
46 def test_aton4(self):
47 a = aton6('abcd::dcba')
48 self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
50 def test_aton5(self):
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):
56 def bad():
57 a = aton6('abcd:dcba')
58 self.failUnlessRaises(dns.exception.SyntaxError, bad)
60 def test_bad_aton2(self):
61 def bad():
62 a = aton6('abcd::dcba::1')
63 self.failUnlessRaises(dns.exception.SyntaxError, bad)
65 def test_bad_aton3(self):
66 def bad():
67 a = aton6('1:2:3:4:5:6:7:8:9')
68 self.failUnlessRaises(dns.exception.SyntaxError, bad)
70 def test_aton1(self):
71 a = aton6('::')
72 self.failUnless(a == '\x00' * 16)
74 def test_aton2(self):
75 a = aton6('::1')
76 self.failUnless(a == '\x00' * 15 + '\x01')
78 def test_aton3(self):
79 a = aton6('::10.0.0.1')
80 self.failUnless(a == '\x00' * 12 + '\x0a\x00\x00\x01')
82 def test_aton4(self):
83 a = aton6('abcd::dcba')
84 self.failUnless(a == '\xab\xcd' + '\x00' * 12 + '\xdc\xba')
86 def test_ntoa1(self):
87 b = '00010002000300040005000600070008'.decode('hex_codec')
88 t = ntoa6(b)
89 self.failUnless(t == '1:2:3:4:5:6:7:8')
91 def test_ntoa2(self):
92 b = '\x00' * 16
93 t = ntoa6(b)
94 self.failUnless(t == '::')
96 def test_ntoa3(self):
97 b = '\x00' * 15 + '\x01'
98 t = ntoa6(b)
99 self.failUnless(t == '::1')
101 def test_ntoa4(self):
102 b = '\x80' + '\x00' * 15
103 t = ntoa6(b)
104 self.failUnless(t == '8000::')
106 def test_ntoa5(self):
107 b = '\x01\xcd' + '\x00' * 12 + '\x03\xef'
108 t = ntoa6(b)
109 self.failUnless(t == '1cd::3ef')
111 def test_ntoa6(self):
112 b = 'ffff00000000ffff000000000000ffff'.decode('hex_codec')
113 t = ntoa6(b)
114 self.failUnless(t == 'ffff:0:0:ffff::ffff')
116 def test_ntoa7(self):
117 b = '00000000ffff000000000000ffffffff'.decode('hex_codec')
118 t = ntoa6(b)
119 self.failUnless(t == '0:0:ffff::ffff:ffff')
121 def test_ntoa8(self):
122 b = 'ffff0000ffff00000000ffff00000000'.decode('hex_codec')
123 t = ntoa6(b)
124 self.failUnless(t == 'ffff:0:ffff::ffff:0:0')
126 def test_ntoa9(self):
127 b = '0000000000000000000000000a000001'.decode('hex_codec')
128 t = ntoa6(b)
129 self.failUnless(t == '::10.0.0.1')
131 def test_ntoa10(self):
132 b = '0000000000000000000000010a000001'.decode('hex_codec')
133 t = ntoa6(b)
134 self.failUnless(t == '::1:a00:1')
136 def test_ntoa11(self):
137 b = '00000000000000000000ffff0a000001'.decode('hex_codec')
138 t = ntoa6(b)
139 self.failUnless(t == '::ffff:10.0.0.1')
141 def test_ntoa12(self):
142 b = '000000000000000000000000ffffffff'.decode('hex_codec')
143 t = ntoa6(b)
144 self.failUnless(t == '::255.255.255.255')
146 def test_ntoa13(self):
147 b = '00000000000000000000ffffffffffff'.decode('hex_codec')
148 t = ntoa6(b)
149 self.failUnless(t == '::ffff:255.255.255.255')
151 def test_ntoa14(self):
152 b = '0000000000000000000000000001ffff'.decode('hex_codec')
153 t = ntoa6(b)
154 self.failUnless(t == '::0.1.255.255')
156 def test_bad_ntoa1(self):
157 def bad():
158 a = ntoa6('')
159 self.failUnlessRaises(ValueError, bad)
161 def test_bad_ntoa2(self):
162 def bad():
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')]
170 for (t, b) in pairs:
171 b1 = aton4(t)
172 t1 = ntoa4(b1)
173 self.failUnless(b1 == b)
174 self.failUnless(t1 == t)
176 def test_bad_v4_aton(self):
177 def make_bad(a):
178 def bad():
179 return aton4(a)
180 return bad
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',
186 ':::::::']
187 embedded = ['::' + x for x in v4_bad_addrs]
188 addrs.extend(embedded)
189 def make_bad(a):
190 def bad():
191 x = aton6(a)
192 return bad
193 for addr in addrs:
194 self.failUnlessRaises(dns.exception.SyntaxError, make_bad(addr))
196 if __name__ == '__main__':
197 unittest.main()