3 # Copyright 2007 Google Inc.
4 # Licensed to PSF under a Contributor Agreement.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 """Unittest for ipaddr module."""
25 # Compatibility function to cast str to bytes objects
26 if issubclass(ipaddr
.Bytes
, str):
29 _cb
= lambda bytestr
: bytes(bytestr
, 'charmap')
31 class IpaddrUnitTest(unittest
.TestCase
):
34 self
.ipv4
= ipaddr
.IPv4Network('1.2.3.4/24')
35 self
.ipv4_hostmask
= ipaddr
.IPv4Network('10.0.0.1/0.255.255.255')
36 self
.ipv6
= ipaddr
.IPv6Network('2001:658:22a:cafe:200:0:0:1/64')
40 del(self
.ipv4_hostmask
)
45 self
.assertEqual("IPv4Network('1.2.3.4/32')",
46 repr(ipaddr
.IPv4Network('1.2.3.4')))
47 self
.assertEqual("IPv6Network('::1/128')",
48 repr(ipaddr
.IPv6Network('::1')))
50 def testAutoMasking(self
):
51 addr1
= ipaddr
.IPv4Network('1.1.1.255/24')
52 addr1_masked
= ipaddr
.IPv4Network('1.1.1.0/24')
53 self
.assertEqual(addr1_masked
, addr1
.masked())
55 addr2
= ipaddr
.IPv6Network('2000:cafe::efac:100/96')
56 addr2_masked
= ipaddr
.IPv6Network('2000:cafe::/96')
57 self
.assertEqual(addr2_masked
, addr2
.masked())
60 def testAddressIntMath(self
):
61 self
.assertEqual(ipaddr
.IPv4Address('1.1.1.1') + 255,
62 ipaddr
.IPv4Address('1.1.2.0'))
63 self
.assertEqual(ipaddr
.IPv4Address('1.1.1.1') - 256,
64 ipaddr
.IPv4Address('1.1.0.1'))
65 self
.assertEqual(ipaddr
.IPv6Address('::1') + (2**16 - 2),
66 ipaddr
.IPv6Address('::ffff'))
67 self
.assertEqual(ipaddr
.IPv6Address('::ffff') - (2**16 - 2),
68 ipaddr
.IPv6Address('::1'))
70 def testInvalidStrings(self
):
71 def AssertInvalidIP(ip_str
):
72 self
.assertRaises(ValueError, ipaddr
.IPAddress
, ip_str
)
74 AssertInvalidIP("016.016.016.016")
75 AssertInvalidIP("016.016.016")
76 AssertInvalidIP("016.016")
77 AssertInvalidIP("016")
78 AssertInvalidIP("000.000.000.000")
79 AssertInvalidIP("000")
80 AssertInvalidIP("0x0a.0x0a.0x0a.0x0a")
81 AssertInvalidIP("0x0a.0x0a.0x0a")
82 AssertInvalidIP("0x0a.0x0a")
83 AssertInvalidIP("0x0a")
84 AssertInvalidIP("42.42.42.42.42")
85 AssertInvalidIP("42.42.42")
86 AssertInvalidIP("42.42")
88 AssertInvalidIP("42..42.42")
89 AssertInvalidIP("42..42.42.42")
90 AssertInvalidIP("42.42.42.42.")
91 AssertInvalidIP("42.42.42.42...")
92 AssertInvalidIP(".42.42.42.42")
93 AssertInvalidIP("...42.42.42.42")
94 AssertInvalidIP("42.42.42.-0")
95 AssertInvalidIP("42.42.42.+0")
97 AssertInvalidIP("...")
98 AssertInvalidIP("bogus")
99 AssertInvalidIP("bogus.com")
100 AssertInvalidIP("192.168.0.1.com")
101 AssertInvalidIP("12345.67899.-54321.-98765")
102 AssertInvalidIP("257.0.0.0")
103 AssertInvalidIP("42.42.42.-42")
104 AssertInvalidIP("3ffe::1.net")
105 AssertInvalidIP("3ffe::1::1")
106 AssertInvalidIP("1::2::3::4:5")
107 AssertInvalidIP("::7:6:5:4:3:2:")
108 AssertInvalidIP(":6:5:4:3:2:1::")
109 AssertInvalidIP("2001::db:::1")
110 AssertInvalidIP("FEDC:9878")
111 AssertInvalidIP("+1.+2.+3.4")
112 AssertInvalidIP("1.2.3.4e0")
113 AssertInvalidIP("::7:6:5:4:3:2:1:0")
114 AssertInvalidIP("7:6:5:4:3:2:1:0::")
115 AssertInvalidIP("9:8:7:6:5:4:3::2:1")
116 AssertInvalidIP("0:1:2:3::4:5:6:7")
117 AssertInvalidIP("3ffe:0:0:0:0:0:0:0:1")
118 AssertInvalidIP("3ffe::10000")
119 AssertInvalidIP("3ffe::goog")
120 AssertInvalidIP("3ffe::-0")
121 AssertInvalidIP("3ffe::+0")
122 AssertInvalidIP("3ffe::-1")
124 AssertInvalidIP(":::")
125 AssertInvalidIP("::1.2.3")
126 AssertInvalidIP("::1.2.3.4.5")
127 AssertInvalidIP("::1.2.3.4:")
128 AssertInvalidIP("1.2.3.4::")
129 AssertInvalidIP("2001:db8::1:")
130 AssertInvalidIP(":2001:db8::1")
131 AssertInvalidIP(":1:2:3:4:5:6:7")
132 AssertInvalidIP("1:2:3:4:5:6:7:")
133 AssertInvalidIP(":1:2:3:4:5:6:")
134 AssertInvalidIP("192.0.2.1/32")
135 AssertInvalidIP("2001:db8::1/128")
136 AssertInvalidIP("02001:db8::")
138 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv4Network
, '')
139 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv4Network
,
141 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv4Network
,
143 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
, '')
144 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
146 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
148 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
149 'cafe:cafe::/128/190')
150 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
152 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Address
,
154 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Address
,
156 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Address
,
158 self
.assertRaises(ipaddr
.AddressValueError
,
159 ipaddr
.IPv4Address(1)._ip
_int
_from
_string
,
161 self
.assertEqual(False, ipaddr
.IPv4Network(1)._is
_hostmask
('1.a.2.3'))
163 def testGetNetwork(self
):
164 self
.assertEqual(int(self
.ipv4
.network
), 16909056)
165 self
.assertEqual(str(self
.ipv4
.network
), '1.2.3.0')
166 self
.assertEqual(str(self
.ipv4_hostmask
.network
), '10.0.0.0')
168 self
.assertEqual(int(self
.ipv6
.network
),
169 42540616829182469433403647294022090752)
170 self
.assertEqual(str(self
.ipv6
.network
),
171 '2001:658:22a:cafe::')
172 self
.assertEqual(str(self
.ipv6
.hostmask
),
173 '::ffff:ffff:ffff:ffff')
175 def testBadVersionComparison(self
):
176 # These should always raise TypeError
177 v4addr
= ipaddr
.IPAddress('1.1.1.1')
178 v4net
= ipaddr
.IPNetwork('1.1.1.1')
179 v6addr
= ipaddr
.IPAddress('::1')
180 v6net
= ipaddr
.IPAddress('::1')
182 self
.assertRaises(TypeError, v4addr
.__lt
__, v6addr
)
183 self
.assertRaises(TypeError, v4addr
.__gt
__, v6addr
)
184 self
.assertRaises(TypeError, v4net
.__lt
__, v6net
)
185 self
.assertRaises(TypeError, v4net
.__gt
__, v6net
)
187 self
.assertRaises(TypeError, v6addr
.__lt
__, v4addr
)
188 self
.assertRaises(TypeError, v6addr
.__gt
__, v4addr
)
189 self
.assertRaises(TypeError, v6net
.__lt
__, v4net
)
190 self
.assertRaises(TypeError, v6net
.__gt
__, v4net
)
192 def testMixedTypeComparison(self
):
193 v4addr
= ipaddr
.IPAddress('1.1.1.1')
194 v4net
= ipaddr
.IPNetwork('1.1.1.1/32')
195 v6addr
= ipaddr
.IPAddress('::1')
196 v6net
= ipaddr
.IPNetwork('::1/128')
198 self
.assertFalse(v4net
.__contains
__(v6net
))
199 self
.assertFalse(v6net
.__contains
__(v4net
))
201 self
.assertRaises(TypeError, lambda: v4addr
< v4net
)
202 self
.assertRaises(TypeError, lambda: v4addr
> v4net
)
203 self
.assertRaises(TypeError, lambda: v4net
< v4addr
)
204 self
.assertRaises(TypeError, lambda: v4net
> v4addr
)
206 self
.assertRaises(TypeError, lambda: v6addr
< v6net
)
207 self
.assertRaises(TypeError, lambda: v6addr
> v6net
)
208 self
.assertRaises(TypeError, lambda: v6net
< v6addr
)
209 self
.assertRaises(TypeError, lambda: v6net
> v6addr
)
211 # with get_mixed_type_key, you can sort addresses and network.
212 self
.assertEqual([v4addr
, v4net
], sorted([v4net
, v4addr
],
213 key
=ipaddr
.get_mixed_type_key
))
214 self
.assertEqual([v6addr
, v6net
], sorted([v6net
, v6addr
],
215 key
=ipaddr
.get_mixed_type_key
))
217 def testIpFromInt(self
):
218 self
.assertEqual(self
.ipv4
.ip
, ipaddr
.IPv4Network(16909060).ip
)
219 self
.assertRaises(ipaddr
.AddressValueError
,
220 ipaddr
.IPv4Network
, 2**32)
221 self
.assertRaises(ipaddr
.AddressValueError
,
222 ipaddr
.IPv4Network
, -1)
224 ipv4
= ipaddr
.IPNetwork('1.2.3.4')
225 ipv6
= ipaddr
.IPNetwork('2001:658:22a:cafe:200:0:0:1')
226 self
.assertEqual(ipv4
, ipaddr
.IPNetwork(int(ipv4
)))
227 self
.assertEqual(ipv6
, ipaddr
.IPNetwork(int(ipv6
)))
229 v6_int
= 42540616829182469433547762482097946625
230 self
.assertEqual(self
.ipv6
.ip
, ipaddr
.IPv6Network(v6_int
).ip
)
231 self
.assertRaises(ipaddr
.AddressValueError
,
232 ipaddr
.IPv6Network
, 2**128)
233 self
.assertRaises(ipaddr
.AddressValueError
,
234 ipaddr
.IPv6Network
, -1)
236 self
.assertEqual(ipaddr
.IPNetwork(self
.ipv4
.ip
).version
, 4)
237 self
.assertEqual(ipaddr
.IPNetwork(self
.ipv6
.ip
).version
, 6)
239 def testIpFromPacked(self
):
240 ip
= ipaddr
.IPNetwork
242 self
.assertEqual(self
.ipv4
.ip
,
243 ip(_cb('\x01\x02\x03\x04')).ip
)
244 self
.assertEqual(ip('255.254.253.252'),
245 ip(_cb('\xff\xfe\xfd\xfc')))
246 self
.assertRaises(ValueError, ipaddr
.IPNetwork
, _cb('\x00' * 3))
247 self
.assertRaises(ValueError, ipaddr
.IPNetwork
, _cb('\x00' * 5))
248 self
.assertEqual(self
.ipv6
.ip
,
249 ip(_cb('\x20\x01\x06\x58\x02\x2a\xca\xfe'
250 '\x02\x00\x00\x00\x00\x00\x00\x01')).ip
)
251 self
.assertEqual(ip('ffff:2:3:4:ffff::'),
252 ip(_cb('\xff\xff\x00\x02\x00\x03\x00\x04' +
253 '\xff\xff' + '\x00' * 6)))
254 self
.assertEqual(ip('::'),
255 ip(_cb('\x00' * 16)))
256 self
.assertRaises(ValueError, ip
, _cb('\x00' * 15))
257 self
.assertRaises(ValueError, ip
, _cb('\x00' * 17))
260 self
.assertEqual(int(self
.ipv4
.ip
), 16909060)
261 self
.assertEqual(str(self
.ipv4
.ip
), '1.2.3.4')
262 self
.assertEqual(str(self
.ipv4_hostmask
.ip
), '10.0.0.1')
264 self
.assertEqual(int(self
.ipv6
.ip
),
265 42540616829182469433547762482097946625)
266 self
.assertEqual(str(self
.ipv6
.ip
),
267 '2001:658:22a:cafe:200::1')
269 def testGetNetmask(self
):
270 self
.assertEqual(int(self
.ipv4
.netmask
), 4294967040L)
271 self
.assertEqual(str(self
.ipv4
.netmask
), '255.255.255.0')
272 self
.assertEqual(str(self
.ipv4_hostmask
.netmask
), '255.0.0.0')
273 self
.assertEqual(int(self
.ipv6
.netmask
),
274 340282366920938463444927863358058659840)
275 self
.assertEqual(self
.ipv6
.prefixlen
, 64)
277 def testZeroNetmask(self
):
278 ipv4_zero_netmask
= ipaddr
.IPv4Network('1.2.3.4/0')
279 self
.assertEqual(int(ipv4_zero_netmask
.netmask
), 0)
280 self
.assertTrue(ipv4_zero_netmask
._is
_valid
_netmask
(str(0)))
282 ipv6_zero_netmask
= ipaddr
.IPv6Network('::1/0')
283 self
.assertEqual(int(ipv6_zero_netmask
.netmask
), 0)
284 self
.assertTrue(ipv6_zero_netmask
._is
_valid
_netmask
(str(0)))
286 def testGetBroadcast(self
):
287 self
.assertEqual(int(self
.ipv4
.broadcast
), 16909311L)
288 self
.assertEqual(str(self
.ipv4
.broadcast
), '1.2.3.255')
290 self
.assertEqual(int(self
.ipv6
.broadcast
),
291 42540616829182469451850391367731642367)
292 self
.assertEqual(str(self
.ipv6
.broadcast
),
293 '2001:658:22a:cafe:ffff:ffff:ffff:ffff')
295 def testGetPrefixlen(self
):
296 self
.assertEqual(self
.ipv4
.prefixlen
, 24)
298 self
.assertEqual(self
.ipv6
.prefixlen
, 64)
300 def testGetSupernet(self
):
301 self
.assertEqual(self
.ipv4
.supernet().prefixlen
, 23)
302 self
.assertEqual(str(self
.ipv4
.supernet().network
), '1.2.2.0')
303 self
.assertEqual(ipaddr
.IPv4Network('0.0.0.0/0').supernet(),
304 ipaddr
.IPv4Network('0.0.0.0/0'))
306 self
.assertEqual(self
.ipv6
.supernet().prefixlen
, 63)
307 self
.assertEqual(str(self
.ipv6
.supernet().network
),
308 '2001:658:22a:cafe::')
309 self
.assertEqual(ipaddr
.IPv6Network('::0/0').supernet(),
310 ipaddr
.IPv6Network('::0/0'))
312 def testGetSupernet3(self
):
313 self
.assertEqual(self
.ipv4
.supernet(3).prefixlen
, 21)
314 self
.assertEqual(str(self
.ipv4
.supernet(3).network
), '1.2.0.0')
316 self
.assertEqual(self
.ipv6
.supernet(3).prefixlen
, 61)
317 self
.assertEqual(str(self
.ipv6
.supernet(3).network
),
318 '2001:658:22a:caf8::')
320 def testGetSupernet4(self
):
321 self
.assertRaises(ValueError, self
.ipv4
.supernet
, prefixlen_diff
=2,
323 self
.assertRaises(ValueError, self
.ipv4
.supernet
, new_prefix
=25)
324 self
.assertEqual(self
.ipv4
.supernet(prefixlen_diff
=2),
325 self
.ipv4
.supernet(new_prefix
=22))
327 self
.assertRaises(ValueError, self
.ipv6
.supernet
, prefixlen_diff
=2,
329 self
.assertRaises(ValueError, self
.ipv6
.supernet
, new_prefix
=65)
330 self
.assertEqual(self
.ipv6
.supernet(prefixlen_diff
=2),
331 self
.ipv6
.supernet(new_prefix
=62))
333 def testIterSubnets(self
):
334 self
.assertEqual(self
.ipv4
.subnet(), list(self
.ipv4
.iter_subnets()))
335 self
.assertEqual(self
.ipv6
.subnet(), list(self
.ipv6
.iter_subnets()))
337 def testIterHosts(self
):
338 self
.assertEqual([ipaddr
.IPv4Address('2.0.0.0'),
339 ipaddr
.IPv4Address('2.0.0.1')],
340 list(ipaddr
.IPNetwork('2.0.0.0/31').iterhosts()))
342 def testFancySubnetting(self
):
343 self
.assertEqual(sorted(self
.ipv4
.subnet(prefixlen_diff
=3)),
344 sorted(self
.ipv4
.subnet(new_prefix
=27)))
345 self
.assertRaises(ValueError, self
.ipv4
.subnet
, new_prefix
=23)
346 self
.assertRaises(ValueError, self
.ipv4
.subnet
,
347 prefixlen_diff
=3, new_prefix
=27)
348 self
.assertEqual(sorted(self
.ipv6
.subnet(prefixlen_diff
=4)),
349 sorted(self
.ipv6
.subnet(new_prefix
=68)))
350 self
.assertRaises(ValueError, self
.ipv6
.subnet
, new_prefix
=63)
351 self
.assertRaises(ValueError, self
.ipv6
.subnet
,
352 prefixlen_diff
=4, new_prefix
=68)
354 def testGetSubnet(self
):
355 self
.assertEqual(self
.ipv4
.subnet()[0].prefixlen
, 25)
356 self
.assertEqual(str(self
.ipv4
.subnet()[0].network
), '1.2.3.0')
357 self
.assertEqual(str(self
.ipv4
.subnet()[1].network
), '1.2.3.128')
359 self
.assertEqual(self
.ipv6
.subnet()[0].prefixlen
, 65)
361 def testGetSubnetForSingle32(self
):
362 ip
= ipaddr
.IPv4Network('1.2.3.4/32')
363 subnets1
= [str(x
) for x
in ip
.subnet()]
364 subnets2
= [str(x
) for x
in ip
.subnet(2)]
365 self
.assertEqual(subnets1
, ['1.2.3.4/32'])
366 self
.assertEqual(subnets1
, subnets2
)
368 def testGetSubnetForSingle128(self
):
369 ip
= ipaddr
.IPv6Network('::1/128')
370 subnets1
= [str(x
) for x
in ip
.subnet()]
371 subnets2
= [str(x
) for x
in ip
.subnet(2)]
372 self
.assertEqual(subnets1
, ['::1/128'])
373 self
.assertEqual(subnets1
, subnets2
)
375 def testSubnet2(self
):
376 ips
= [str(x
) for x
in self
.ipv4
.subnet(2)]
379 ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26'])
381 ipsv6
= [str(x
) for x
in self
.ipv6
.subnet(2)]
384 ['2001:658:22a:cafe::/66',
385 '2001:658:22a:cafe:4000::/66',
386 '2001:658:22a:cafe:8000::/66',
387 '2001:658:22a:cafe:c000::/66'])
389 def testSubnetFailsForLargeCidrDiff(self
):
390 self
.assertRaises(ValueError, self
.ipv4
.subnet
, 9)
391 self
.assertRaises(ValueError, self
.ipv6
.subnet
, 65)
393 def testSupernetFailsForLargeCidrDiff(self
):
394 self
.assertRaises(ValueError, self
.ipv4
.supernet
, 25)
395 self
.assertRaises(ValueError, self
.ipv6
.supernet
, 65)
397 def testSubnetFailsForNegativeCidrDiff(self
):
398 self
.assertRaises(ValueError, self
.ipv4
.subnet
, -1)
399 self
.assertRaises(ValueError, self
.ipv6
.subnet
, -1)
401 def testGetNumHosts(self
):
402 self
.assertEqual(self
.ipv4
.numhosts
, 256)
403 self
.assertEqual(self
.ipv4
.subnet()[0].numhosts
, 128)
404 self
.assertEqual(self
.ipv4
.supernet().numhosts
, 512)
406 self
.assertEqual(self
.ipv6
.numhosts
, 18446744073709551616)
407 self
.assertEqual(self
.ipv6
.subnet()[0].numhosts
, 9223372036854775808)
408 self
.assertEqual(self
.ipv6
.supernet().numhosts
, 36893488147419103232)
410 def testContains(self
):
411 self
.assertTrue(ipaddr
.IPv4Network('1.2.3.128/25') in self
.ipv4
)
412 self
.assertFalse(ipaddr
.IPv4Network('1.2.4.1/24') in self
.ipv4
)
413 self
.assertTrue(self
.ipv4
in self
.ipv4
)
414 self
.assertTrue(self
.ipv6
in self
.ipv6
)
415 # We can test addresses and string as well.
416 addr1
= ipaddr
.IPv4Address('1.2.3.37')
417 self
.assertTrue(addr1
in self
.ipv4
)
418 # issue 61, bad network comparison on like-ip'd network objects
419 # with identical broadcast addresses.
420 self
.assertFalse(ipaddr
.IPv4Network('1.1.0.0/16').__contains
__(
421 ipaddr
.IPv4Network('1.0.0.0/15')))
423 def testBadAddress(self
):
424 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv4Network
,
426 self
.assertRaises(ipaddr
.AddressValueError
,
427 ipaddr
.IPv4Network
, '1.2.3.256')
429 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
431 self
.assertRaises(ipaddr
.AddressValueError
,
432 ipaddr
.IPv4Network
, '1.2.3.4/32/24')
433 self
.assertRaises(ipaddr
.AddressValueError
,
434 ipaddr
.IPv4Network
, '10/8')
435 self
.assertRaises(ipaddr
.AddressValueError
,
436 ipaddr
.IPv6Network
, '10/8')
439 def testBadNetMask(self
):
440 self
.assertRaises(ipaddr
.NetmaskValueError
,
441 ipaddr
.IPv4Network
, '1.2.3.4/')
442 self
.assertRaises(ipaddr
.NetmaskValueError
,
443 ipaddr
.IPv4Network
, '1.2.3.4/33')
444 self
.assertRaises(ipaddr
.NetmaskValueError
,
445 ipaddr
.IPv4Network
, '1.2.3.4/254.254.255.256')
446 self
.assertRaises(ipaddr
.NetmaskValueError
,
447 ipaddr
.IPv4Network
, '1.1.1.1/240.255.0.0')
448 self
.assertRaises(ipaddr
.NetmaskValueError
,
449 ipaddr
.IPv6Network
, '::1/')
450 self
.assertRaises(ipaddr
.NetmaskValueError
,
451 ipaddr
.IPv6Network
, '::1/129')
454 self
.assertEqual(str(self
.ipv4
[5]), '1.2.3.5')
455 self
.assertRaises(IndexError, self
.ipv4
.__getitem__
, 256)
457 self
.assertEqual(str(self
.ipv6
[5]),
458 '2001:658:22a:cafe::5')
460 def testGetitem(self
):
461 # http://code.google.com/p/ipaddr-py/issues/detail?id=15
462 addr
= ipaddr
.IPv4Network('172.31.255.128/255.255.255.240')
463 self
.assertEqual(28, addr
.prefixlen
)
464 addr_list
= list(addr
)
465 self
.assertEqual('172.31.255.128', str(addr_list
[0]))
466 self
.assertEqual('172.31.255.128', str(addr
[0]))
467 self
.assertEqual('172.31.255.143', str(addr_list
[-1]))
468 self
.assertEqual('172.31.255.143', str(addr
[-1]))
469 self
.assertEqual(addr_list
[-1], addr
[-1])
472 self
.assertTrue(self
.ipv4
== ipaddr
.IPv4Network('1.2.3.4/24'))
473 self
.assertFalse(self
.ipv4
== ipaddr
.IPv4Network('1.2.3.4/23'))
474 self
.assertFalse(self
.ipv4
== ipaddr
.IPv6Network('::1.2.3.4/24'))
475 self
.assertFalse(self
.ipv4
== '')
476 self
.assertFalse(self
.ipv4
== [])
477 self
.assertFalse(self
.ipv4
== 2)
478 self
.assertTrue(ipaddr
.IPNetwork('1.1.1.1/32') ==
479 ipaddr
.IPAddress('1.1.1.1'))
480 self
.assertTrue(ipaddr
.IPNetwork('1.1.1.1/24') ==
481 ipaddr
.IPAddress('1.1.1.1'))
482 self
.assertFalse(ipaddr
.IPNetwork('1.1.1.0/24') ==
483 ipaddr
.IPAddress('1.1.1.1'))
485 self
.assertTrue(self
.ipv6
==
486 ipaddr
.IPv6Network('2001:658:22a:cafe:200::1/64'))
487 self
.assertTrue(ipaddr
.IPNetwork('::1/128') ==
488 ipaddr
.IPAddress('::1'))
489 self
.assertTrue(ipaddr
.IPNetwork('::1/127') ==
490 ipaddr
.IPAddress('::1'))
491 self
.assertFalse(ipaddr
.IPNetwork('::0/127') ==
492 ipaddr
.IPAddress('::1'))
493 self
.assertFalse(self
.ipv6
==
494 ipaddr
.IPv6Network('2001:658:22a:cafe:200::1/63'))
495 self
.assertFalse(self
.ipv6
== ipaddr
.IPv4Network('1.2.3.4/23'))
496 self
.assertFalse(self
.ipv6
== '')
497 self
.assertFalse(self
.ipv6
== [])
498 self
.assertFalse(self
.ipv6
== 2)
500 def testNotEqual(self
):
501 self
.assertFalse(self
.ipv4
!= ipaddr
.IPv4Network('1.2.3.4/24'))
502 self
.assertTrue(self
.ipv4
!= ipaddr
.IPv4Network('1.2.3.4/23'))
503 self
.assertTrue(self
.ipv4
!= ipaddr
.IPv6Network('::1.2.3.4/24'))
504 self
.assertTrue(self
.ipv4
!= '')
505 self
.assertTrue(self
.ipv4
!= [])
506 self
.assertTrue(self
.ipv4
!= 2)
508 addr2
= ipaddr
.IPAddress('2001:658:22a:cafe:200::1')
509 self
.assertFalse(self
.ipv6
!=
510 ipaddr
.IPv6Network('2001:658:22a:cafe:200::1/64'))
511 self
.assertTrue(self
.ipv6
!=
512 ipaddr
.IPv6Network('2001:658:22a:cafe:200::1/63'))
513 self
.assertTrue(self
.ipv6
!= ipaddr
.IPv4Network('1.2.3.4/23'))
514 self
.assertTrue(self
.ipv6
!= '')
515 self
.assertTrue(self
.ipv6
!= [])
516 self
.assertTrue(self
.ipv6
!= 2)
518 def testSlash32Constructor(self
):
519 self
.assertEqual(str(ipaddr
.IPv4Network('1.2.3.4/255.255.255.255')),
522 def testSlash128Constructor(self
):
523 self
.assertEqual(str(ipaddr
.IPv6Network('::1/128')),
526 def testSlash0Constructor(self
):
527 self
.assertEqual(str(ipaddr
.IPv4Network('1.2.3.4/0.0.0.0')),
530 def testCollapsing(self
):
531 # test only IP addresses including some duplicates
532 ip1
= ipaddr
.IPv4Address('1.1.1.0')
533 ip2
= ipaddr
.IPv4Address('1.1.1.1')
534 ip3
= ipaddr
.IPv4Address('1.1.1.2')
535 ip4
= ipaddr
.IPv4Address('1.1.1.3')
536 ip5
= ipaddr
.IPv4Address('1.1.1.4')
537 ip6
= ipaddr
.IPv4Address('1.1.1.0')
538 # check that addreses are subsumed properly.
539 collapsed
= ipaddr
.collapse_address_list([ip1
, ip2
, ip3
, ip4
, ip5
, ip6
])
540 self
.assertEqual(collapsed
, [ipaddr
.IPv4Network('1.1.1.0/30'),
541 ipaddr
.IPv4Network('1.1.1.4/32')])
543 # test a mix of IP addresses and networks including some duplicates
544 ip1
= ipaddr
.IPv4Address('1.1.1.0')
545 ip2
= ipaddr
.IPv4Address('1.1.1.1')
546 ip3
= ipaddr
.IPv4Address('1.1.1.2')
547 ip4
= ipaddr
.IPv4Address('1.1.1.3')
548 ip5
= ipaddr
.IPv4Network('1.1.1.4/30')
549 ip6
= ipaddr
.IPv4Network('1.1.1.4/30')
550 # check that addreses are subsumed properly.
551 collapsed
= ipaddr
.collapse_address_list([ip5
, ip1
, ip2
, ip3
, ip4
, ip6
])
552 self
.assertEqual(collapsed
, [ipaddr
.IPv4Network('1.1.1.0/29')])
554 # test only IP networks
555 ip1
= ipaddr
.IPv4Network('1.1.0.0/24')
556 ip2
= ipaddr
.IPv4Network('1.1.1.0/24')
557 ip3
= ipaddr
.IPv4Network('1.1.2.0/24')
558 ip4
= ipaddr
.IPv4Network('1.1.3.0/24')
559 ip5
= ipaddr
.IPv4Network('1.1.4.0/24')
560 # stored in no particular order b/c we want CollapseAddr to call [].sort
561 ip6
= ipaddr
.IPv4Network('1.1.0.0/22')
562 # check that addreses are subsumed properly.
563 collapsed
= ipaddr
.collapse_address_list([ip1
, ip2
, ip3
, ip4
, ip5
, ip6
])
564 self
.assertEqual(collapsed
, [ipaddr
.IPv4Network('1.1.0.0/22'),
565 ipaddr
.IPv4Network('1.1.4.0/24')])
567 # test that two addresses are supernet'ed properly
568 collapsed
= ipaddr
.collapse_address_list([ip1
, ip2
])
569 self
.assertEqual(collapsed
, [ipaddr
.IPv4Network('1.1.0.0/23')])
571 # test same IP networks
572 ip_same1
= ip_same2
= ipaddr
.IPv4Network('1.1.1.1/32')
573 self
.assertEqual(ipaddr
.collapse_address_list([ip_same1
, ip_same2
]),
576 # test same IP addresses
577 ip_same1
= ip_same2
= ipaddr
.IPv4Address('1.1.1.1')
578 self
.assertEqual(ipaddr
.collapse_address_list([ip_same1
, ip_same2
]),
579 [ipaddr
.IPNetwork('1.1.1.1/32')])
580 ip1
= ipaddr
.IPv6Network('::2001:1/100')
581 ip2
= ipaddr
.IPv6Network('::2002:1/120')
582 ip3
= ipaddr
.IPv6Network('::2001:1/96')
583 # test that ipv6 addresses are subsumed properly.
584 collapsed
= ipaddr
.collapse_address_list([ip1
, ip2
, ip3
])
585 self
.assertEqual(collapsed
, [ip3
])
588 ip1
= ipaddr
.IPAddress('1.1.1.1')
589 ip2
= ipaddr
.IPAddress('::1')
590 self
.assertRaises(TypeError, ipaddr
.collapse_address_list
,
593 def testSummarizing(self
):
594 #ip = ipaddr.IPAddress
595 #ipnet = ipaddr.IPNetwork
596 summarize
= ipaddr
.summarize_address_range
597 ip1
= ipaddr
.IPAddress('1.1.1.0')
598 ip2
= ipaddr
.IPAddress('1.1.1.255')
599 # test a /24 is sumamrized properly
600 self
.assertEqual(summarize(ip1
, ip2
)[0], ipaddr
.IPNetwork('1.1.1.0/24'))
601 # test an IPv4 range that isn't on a network byte boundary
602 ip2
= ipaddr
.IPAddress('1.1.1.8')
603 self
.assertEqual(summarize(ip1
, ip2
), [ipaddr
.IPNetwork('1.1.1.0/29'),
604 ipaddr
.IPNetwork('1.1.1.8')])
606 ip1
= ipaddr
.IPAddress('1::')
607 ip2
= ipaddr
.IPAddress('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
608 # test a IPv6 is sumamrized properly
609 self
.assertEqual(summarize(ip1
, ip2
)[0], ipaddr
.IPNetwork('1::/16'))
610 # test an IPv6 range that isn't on a network byte boundary
611 ip2
= ipaddr
.IPAddress('2::')
612 self
.assertEqual(summarize(ip1
, ip2
), [ipaddr
.IPNetwork('1::/16'),
613 ipaddr
.IPNetwork('2::/128')])
615 # test exception raised when first is greater than last
616 self
.assertRaises(ValueError, summarize
, ipaddr
.IPAddress('1.1.1.0'),
617 ipaddr
.IPAddress('1.1.0.0'))
618 # test exception raised when first and last aren't IP addresses
619 self
.assertRaises(TypeError, summarize
,
620 ipaddr
.IPNetwork('1.1.1.0'),
621 ipaddr
.IPNetwork('1.1.0.0'))
622 self
.assertRaises(TypeError, summarize
,
623 ipaddr
.IPNetwork('1.1.1.0'), ipaddr
.IPNetwork('1.1.0.0'))
624 # test exception raised when first and last are not same version
625 self
.assertRaises(TypeError, summarize
, ipaddr
.IPAddress('::'),
626 ipaddr
.IPNetwork('1.1.0.0'))
628 def testAddressComparison(self
):
629 self
.assertTrue(ipaddr
.IPAddress('1.1.1.1') <=
630 ipaddr
.IPAddress('1.1.1.1'))
631 self
.assertTrue(ipaddr
.IPAddress('1.1.1.1') <=
632 ipaddr
.IPAddress('1.1.1.2'))
633 self
.assertTrue(ipaddr
.IPAddress('::1') <= ipaddr
.IPAddress('::1'))
634 self
.assertTrue(ipaddr
.IPAddress('::1') <= ipaddr
.IPAddress('::2'))
636 def testNetworkComparison(self
):
637 # ip1 and ip2 have the same network address
638 ip1
= ipaddr
.IPv4Network('1.1.1.0/24')
639 ip2
= ipaddr
.IPv4Network('1.1.1.1/24')
640 ip3
= ipaddr
.IPv4Network('1.1.2.0/24')
642 self
.assertTrue(ip1
< ip3
)
643 self
.assertTrue(ip3
> ip2
)
645 self
.assertEqual(ip1
.compare_networks(ip2
), 0)
646 self
.assertTrue(ip1
._get_networks_key() == ip2
._get_networks_key())
647 self
.assertEqual(ip1
.compare_networks(ip3
), -1)
648 self
.assertTrue(ip1
._get_networks_key() < ip3
._get_networks_key())
650 ip1
= ipaddr
.IPv6Network('2001::2000/96')
651 ip2
= ipaddr
.IPv6Network('2001::2001/96')
652 ip3
= ipaddr
.IPv6Network('2001:ffff::2000/96')
654 self
.assertTrue(ip1
< ip3
)
655 self
.assertTrue(ip3
> ip2
)
656 self
.assertEqual(ip1
.compare_networks(ip2
), 0)
657 self
.assertTrue(ip1
._get_networks_key() == ip2
._get_networks_key())
658 self
.assertEqual(ip1
.compare_networks(ip3
), -1)
659 self
.assertTrue(ip1
._get_networks_key() < ip3
._get_networks_key())
661 # Test comparing different protocols.
662 # Should always raise a TypeError.
663 ipv6
= ipaddr
.IPv6Network('::/0')
664 ipv4
= ipaddr
.IPv4Network('0.0.0.0/0')
665 self
.assertRaises(TypeError, ipv4
.__lt__
, ipv6
)
666 self
.assertRaises(TypeError, ipv4
.__gt__
, ipv6
)
667 self
.assertRaises(TypeError, ipv6
.__lt__
, ipv4
)
668 self
.assertRaises(TypeError, ipv6
.__gt__
, ipv4
)
670 # Regression test for issue 19.
671 ip1
= ipaddr
.IPNetwork('10.1.2.128/25')
672 self
.assertFalse(ip1
< ip1
)
673 self
.assertFalse(ip1
> ip1
)
674 ip2
= ipaddr
.IPNetwork('10.1.3.0/24')
675 self
.assertTrue(ip1
< ip2
)
676 self
.assertFalse(ip2
< ip1
)
677 self
.assertFalse(ip1
> ip2
)
678 self
.assertTrue(ip2
> ip1
)
679 ip3
= ipaddr
.IPNetwork('10.1.3.0/25')
680 self
.assertTrue(ip2
< ip3
)
681 self
.assertFalse(ip3
< ip2
)
682 self
.assertFalse(ip2
> ip3
)
683 self
.assertTrue(ip3
> ip2
)
685 # Regression test for issue 28.
686 ip1
= ipaddr
.IPNetwork('10.10.10.0/31')
687 ip2
= ipaddr
.IPNetwork('10.10.10.0')
688 ip3
= ipaddr
.IPNetwork('10.10.10.2/31')
689 ip4
= ipaddr
.IPNetwork('10.10.10.2')
690 sorted = [ip1
, ip2
, ip3
, ip4
]
691 unsorted
= [ip2
, ip4
, ip1
, ip3
]
693 self
.assertEqual(sorted, unsorted
)
694 unsorted
= [ip4
, ip1
, ip3
, ip2
]
696 self
.assertEqual(sorted, unsorted
)
697 self
.assertRaises(TypeError, ip1
.__lt__
, ipaddr
.IPAddress('10.10.10.0'))
698 self
.assertRaises(TypeError, ip2
.__lt__
, ipaddr
.IPAddress('10.10.10.0'))
701 self
.assertTrue(ipaddr
.IPNetwork('1.1.1.1') <=
702 ipaddr
.IPNetwork('1.1.1.1'))
703 self
.assertTrue(ipaddr
.IPNetwork('1.1.1.1') <=
704 ipaddr
.IPNetwork('1.1.1.2'))
705 self
.assertFalse(ipaddr
.IPNetwork('1.1.1.2') <=
706 ipaddr
.IPNetwork('1.1.1.1'))
707 self
.assertTrue(ipaddr
.IPNetwork('::1') <= ipaddr
.IPNetwork('::1'))
708 self
.assertTrue(ipaddr
.IPNetwork('::1') <= ipaddr
.IPNetwork('::2'))
709 self
.assertFalse(ipaddr
.IPNetwork('::2') <= ipaddr
.IPNetwork('::1'))
711 def testStrictNetworks(self
):
712 self
.assertRaises(ValueError, ipaddr
.IPNetwork
, '192.168.1.1/24',
714 self
.assertRaises(ValueError, ipaddr
.IPNetwork
, '::1/120', strict
=True)
716 def testOverlaps(self
):
717 other
= ipaddr
.IPv4Network('1.2.3.0/30')
718 other2
= ipaddr
.IPv4Network('1.2.2.0/24')
719 other3
= ipaddr
.IPv4Network('1.2.2.64/26')
720 self
.assertTrue(self
.ipv4
.overlaps(other
))
721 self
.assertFalse(self
.ipv4
.overlaps(other2
))
722 self
.assertTrue(other2
.overlaps(other3
))
724 def testEmbeddedIpv4(self
):
725 ipv4_string
= '192.168.0.1'
726 ipv4
= ipaddr
.IPv4Network(ipv4_string
)
727 v4compat_ipv6
= ipaddr
.IPv6Network('::%s' % ipv4_string
)
728 self
.assertEqual(int(v4compat_ipv6
.ip
), int(ipv4
.ip
))
729 v4mapped_ipv6
= ipaddr
.IPv6Network('::ffff:%s' % ipv4_string
)
730 self
.assertNotEqual(v4mapped_ipv6
.ip
, ipv4
.ip
)
731 self
.assertRaises(ipaddr
.AddressValueError
, ipaddr
.IPv6Network
,
732 '2001:1.1.1.1:1.1.1.1')
734 # Issue 67: IPv6 with embedded IPv4 address not recognized.
735 def testIPv6AddressTooLarge(self
):
737 self
.assertEqual(ipaddr
.IPAddress('::FFFF:192.0.2.1'),
738 ipaddr
.IPAddress('::FFFF:c000:201'))
739 # RFC4291 2.2 (part 3) x::d.d.d.d
740 self
.assertEqual(ipaddr
.IPAddress('FFFF::192.0.2.1'),
741 ipaddr
.IPAddress('FFFF::c000:201'))
743 def testIPVersion(self
):
744 self
.assertEqual(self
.ipv4
.version
, 4)
745 self
.assertEqual(self
.ipv6
.version
, 6)
747 def testMaxPrefixLength(self
):
748 self
.assertEqual(self
.ipv4
.max_prefixlen
, 32)
749 self
.assertEqual(self
.ipv6
.max_prefixlen
, 128)
751 def testPacked(self
):
752 self
.assertEqual(self
.ipv4
.packed
,
753 _cb('\x01\x02\x03\x04'))
754 self
.assertEqual(ipaddr
.IPv4Network('255.254.253.252').packed
,
755 _cb('\xff\xfe\xfd\xfc'))
756 self
.assertEqual(self
.ipv6
.packed
,
757 _cb('\x20\x01\x06\x58\x02\x2a\xca\xfe'
758 '\x02\x00\x00\x00\x00\x00\x00\x01'))
759 self
.assertEqual(ipaddr
.IPv6Network('ffff:2:3:4:ffff::').packed
,
760 _cb('\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff'
762 self
.assertEqual(ipaddr
.IPv6Network('::1:0:0:0:0').packed
,
763 _cb('\x00' * 6 + '\x00\x01' + '\x00' * 8))
765 def testIpStrFromPrefixlen(self
):
766 ipv4
= ipaddr
.IPv4Network('1.2.3.4/24')
767 self
.assertEqual(ipv4
._ip_string_from_prefix(), '255.255.255.0')
768 self
.assertEqual(ipv4
._ip_string_from_prefix(28), '255.255.255.240')
770 def testIpType(self
):
771 ipv4net
= ipaddr
.IPNetwork('1.2.3.4')
772 ipv4addr
= ipaddr
.IPAddress('1.2.3.4')
773 ipv6net
= ipaddr
.IPNetwork('::1.2.3.4')
774 ipv6addr
= ipaddr
.IPAddress('::1.2.3.4')
775 self
.assertEqual(ipaddr
.IPv4Network
, type(ipv4net
))
776 self
.assertEqual(ipaddr
.IPv4Address
, type(ipv4addr
))
777 self
.assertEqual(ipaddr
.IPv6Network
, type(ipv6net
))
778 self
.assertEqual(ipaddr
.IPv6Address
, type(ipv6addr
))
780 def testReservedIpv4(self
):
782 self
.assertEqual(True, ipaddr
.IPNetwork('224.1.1.1/31').is_multicast
)
783 self
.assertEqual(False, ipaddr
.IPNetwork('240.0.0.0').is_multicast
)
785 self
.assertEqual(True, ipaddr
.IPNetwork('192.168.1.1/17').is_private
)
786 self
.assertEqual(False, ipaddr
.IPNetwork('192.169.0.0').is_private
)
787 self
.assertEqual(True, ipaddr
.IPNetwork('10.255.255.255').is_private
)
788 self
.assertEqual(False, ipaddr
.IPNetwork('11.0.0.0').is_private
)
789 self
.assertEqual(True, ipaddr
.IPNetwork('172.31.255.255').is_private
)
790 self
.assertEqual(False, ipaddr
.IPNetwork('172.32.0.0').is_private
)
792 self
.assertEqual(True,
793 ipaddr
.IPNetwork('169.254.100.200/24').is_link_local
)
794 self
.assertEqual(False,
795 ipaddr
.IPNetwork('169.255.100.200/24').is_link_local
)
797 self
.assertEqual(True,
798 ipaddr
.IPNetwork('127.100.200.254/32').is_loopback
)
799 self
.assertEqual(True, ipaddr
.IPNetwork('127.42.0.0/16').is_loopback
)
800 self
.assertEqual(False, ipaddr
.IPNetwork('128.0.0.0').is_loopback
)
803 self
.assertEqual(True, ipaddr
.IPAddress('224.1.1.1').is_multicast
)
804 self
.assertEqual(False, ipaddr
.IPAddress('240.0.0.0').is_multicast
)
806 self
.assertEqual(True, ipaddr
.IPAddress('192.168.1.1').is_private
)
807 self
.assertEqual(False, ipaddr
.IPAddress('192.169.0.0').is_private
)
808 self
.assertEqual(True, ipaddr
.IPAddress('10.255.255.255').is_private
)
809 self
.assertEqual(False, ipaddr
.IPAddress('11.0.0.0').is_private
)
810 self
.assertEqual(True, ipaddr
.IPAddress('172.31.255.255').is_private
)
811 self
.assertEqual(False, ipaddr
.IPAddress('172.32.0.0').is_private
)
813 self
.assertEqual(True,
814 ipaddr
.IPAddress('169.254.100.200').is_link_local
)
815 self
.assertEqual(False,
816 ipaddr
.IPAddress('169.255.100.200').is_link_local
)
818 self
.assertEqual(True,
819 ipaddr
.IPAddress('127.100.200.254').is_loopback
)
820 self
.assertEqual(True, ipaddr
.IPAddress('127.42.0.0').is_loopback
)
821 self
.assertEqual(False, ipaddr
.IPAddress('128.0.0.0').is_loopback
)
822 self
.assertEqual(True, ipaddr
.IPNetwork('0.0.0.0').is_unspecified
)
824 def testReservedIpv6(self
):
826 self
.assertEqual(True, ipaddr
.IPNetwork('ffff::').is_multicast
)
827 self
.assertEqual(True, ipaddr
.IPNetwork(2**128-1).is_multicast
)
828 self
.assertEqual(True, ipaddr
.IPNetwork('ff00::').is_multicast
)
829 self
.assertEqual(False, ipaddr
.IPNetwork('fdff::').is_multicast
)
831 self
.assertEqual(True, ipaddr
.IPNetwork('fecf::').is_site_local
)
832 self
.assertEqual(True, ipaddr
.IPNetwork(
833 'feff:ffff:ffff:ffff::').is_site_local
)
834 self
.assertEqual(False, ipaddr
.IPNetwork('fbf:ffff::').is_site_local
)
835 self
.assertEqual(False, ipaddr
.IPNetwork('ff00::').is_site_local
)
837 self
.assertEqual(True, ipaddr
.IPNetwork('fc00::').is_private
)
838 self
.assertEqual(True, ipaddr
.IPNetwork(
839 'fc00:ffff:ffff:ffff::').is_private
)
840 self
.assertEqual(False, ipaddr
.IPNetwork('fbff:ffff::').is_private
)
841 self
.assertEqual(False, ipaddr
.IPNetwork('fe00::').is_private
)
843 self
.assertEqual(True, ipaddr
.IPNetwork('fea0::').is_link_local
)
844 self
.assertEqual(True, ipaddr
.IPNetwork('febf:ffff::').is_link_local
)
845 self
.assertEqual(False, ipaddr
.IPNetwork('fe7f:ffff::').is_link_local
)
846 self
.assertEqual(False, ipaddr
.IPNetwork('fec0::').is_link_local
)
848 self
.assertEqual(True, ipaddr
.IPNetwork('0:0::0:01').is_loopback
)
849 self
.assertEqual(False, ipaddr
.IPNetwork('::1/127').is_loopback
)
850 self
.assertEqual(False, ipaddr
.IPNetwork('::').is_loopback
)
851 self
.assertEqual(False, ipaddr
.IPNetwork('::2').is_loopback
)
853 self
.assertEqual(True, ipaddr
.IPNetwork('0::0').is_unspecified
)
854 self
.assertEqual(False, ipaddr
.IPNetwork('::1').is_unspecified
)
855 self
.assertEqual(False, ipaddr
.IPNetwork('::/127').is_unspecified
)
858 self
.assertEqual(True, ipaddr
.IPAddress('ffff::').is_multicast
)
859 self
.assertEqual(True, ipaddr
.IPAddress(2**128-1).is_multicast
)
860 self
.assertEqual(True, ipaddr
.IPAddress('ff00::').is_multicast
)
861 self
.assertEqual(False, ipaddr
.IPAddress('fdff::').is_multicast
)
863 self
.assertEqual(True, ipaddr
.IPAddress('fecf::').is_site_local
)
864 self
.assertEqual(True, ipaddr
.IPAddress(
865 'feff:ffff:ffff:ffff::').is_site_local
)
866 self
.assertEqual(False, ipaddr
.IPAddress('fbf:ffff::').is_site_local
)
867 self
.assertEqual(False, ipaddr
.IPAddress('ff00::').is_site_local
)
869 self
.assertEqual(True, ipaddr
.IPAddress('fc00::').is_private
)
870 self
.assertEqual(True, ipaddr
.IPAddress(
871 'fc00:ffff:ffff:ffff::').is_private
)
872 self
.assertEqual(False, ipaddr
.IPAddress('fbff:ffff::').is_private
)
873 self
.assertEqual(False, ipaddr
.IPAddress('fe00::').is_private
)
875 self
.assertEqual(True, ipaddr
.IPAddress('fea0::').is_link_local
)
876 self
.assertEqual(True, ipaddr
.IPAddress('febf:ffff::').is_link_local
)
877 self
.assertEqual(False, ipaddr
.IPAddress('fe7f:ffff::').is_link_local
)
878 self
.assertEqual(False, ipaddr
.IPAddress('fec0::').is_link_local
)
880 self
.assertEqual(True, ipaddr
.IPAddress('0:0::0:01').is_loopback
)
881 self
.assertEqual(True, ipaddr
.IPAddress('::1').is_loopback
)
882 self
.assertEqual(False, ipaddr
.IPAddress('::2').is_loopback
)
884 self
.assertEqual(True, ipaddr
.IPAddress('0::0').is_unspecified
)
885 self
.assertEqual(False, ipaddr
.IPAddress('::1').is_unspecified
)
887 # some generic IETF reserved addresses
888 self
.assertEqual(True, ipaddr
.IPAddress('100::').is_reserved
)
889 self
.assertEqual(True, ipaddr
.IPNetwork('4000::1/128').is_reserved
)
891 def testIpv4Mapped(self
):
892 self
.assertEqual(ipaddr
.IPAddress('::ffff:192.168.1.1').ipv4_mapped
,
893 ipaddr
.IPAddress('192.168.1.1'))
894 self
.assertEqual(ipaddr
.IPAddress('::c0a8:101').ipv4_mapped
, None)
895 self
.assertEqual(ipaddr
.IPAddress('::ffff:c0a8:101').ipv4_mapped
,
896 ipaddr
.IPAddress('192.168.1.1'))
898 def testAddrExclude(self
):
899 addr1
= ipaddr
.IPNetwork('10.1.1.0/24')
900 addr2
= ipaddr
.IPNetwork('10.1.1.0/26')
901 addr3
= ipaddr
.IPNetwork('10.2.1.0/24')
902 addr4
= ipaddr
.IPAddress('10.1.1.0')
903 self
.assertEqual(addr1
.address_exclude(addr2
),
904 [ipaddr
.IPNetwork('10.1.1.64/26'),
905 ipaddr
.IPNetwork('10.1.1.128/25')])
906 self
.assertRaises(ValueError, addr1
.address_exclude
, addr3
)
907 self
.assertRaises(TypeError, addr1
.address_exclude
, addr4
)
908 self
.assertEqual(addr1
.address_exclude(addr1
), [])
911 self
.assertEqual(hash(ipaddr
.IPNetwork('10.1.1.0/24')),
912 hash(ipaddr
.IPNetwork('10.1.1.0/24')))
913 self
.assertEqual(hash(ipaddr
.IPAddress('10.1.1.0')),
914 hash(ipaddr
.IPAddress('10.1.1.0')))
916 self
.assertEqual(hash(ipaddr
.IPAddress('1.2.3.4')),
917 hash(ipaddr
.IPAddress(
918 long(ipaddr
.IPAddress('1.2.3.4')._ip
))))
919 ip1
= ipaddr
.IPAddress('10.1.1.0')
920 ip2
= ipaddr
.IPAddress('1::')
922 dummy
[self
.ipv4
] = None
923 dummy
[self
.ipv6
] = None
926 self
.assertTrue(self
.ipv4
in dummy
)
927 self
.assertTrue(ip2
in dummy
)
929 def testCopyConstructor(self
):
930 addr1
= ipaddr
.IPNetwork('10.1.1.0/24')
931 addr2
= ipaddr
.IPNetwork(addr1
)
932 addr3
= ipaddr
.IPNetwork('2001:658:22a:cafe:200::1/64')
933 addr4
= ipaddr
.IPNetwork(addr3
)
934 addr5
= ipaddr
.IPv4Address('1.1.1.1')
935 addr6
= ipaddr
.IPv6Address('2001:658:22a:cafe:200::1')
937 self
.assertEqual(addr1
, addr2
)
938 self
.assertEqual(addr3
, addr4
)
939 self
.assertEqual(addr5
, ipaddr
.IPv4Address(addr5
))
940 self
.assertEqual(addr6
, ipaddr
.IPv6Address(addr6
))
942 def testCompressIPv6Address(self
):
944 '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128',
945 '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128',
946 '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128',
947 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
948 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
949 '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128',
950 '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128',
951 '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128',
952 '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128',
953 '0:0:0:0:0:0:0:0': '::/128',
954 '0:0:0:0:0:0:0:0/0': '::/0',
955 '0:0:0:0:0:0:0:1': '::1/128',
956 '2001:0658:022a:cafe:0000:0000:0000:0000/66':
957 '2001:658:22a:cafe::/66',
958 '::1.2.3.4': '::102:304/128',
959 '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128',
960 '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128',
961 '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
962 '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
963 '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
965 for uncompressed
, compressed
in test_addresses
.items():
966 self
.assertEqual(compressed
, str(ipaddr
.IPv6Network(uncompressed
)))
968 def testExplodeShortHandIpStr(self
):
969 addr1
= ipaddr
.IPv6Network('2001::1')
970 addr2
= ipaddr
.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
971 self
.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128',
973 self
.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128',
974 ipaddr
.IPv6Network('::1/128').exploded
)
976 self
.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
979 def testIntRepresentation(self
):
980 self
.assertEqual(16909060, int(self
.ipv4
))
981 self
.assertEqual(42540616829182469433547762482097946625, int(self
.ipv6
))
983 def testHexRepresentation(self
):
984 self
.assertEqual(hex(0x1020304),
987 self
.assertEqual(hex(0x20010658022ACAFE0200000000000001),
990 # backwards compatibility
991 def testBackwardsCompability(self
):
992 self
.assertEqual(ipaddr
.CollapseAddrList(
993 [ipaddr
.IPNetwork('1.1.0.0/24'), ipaddr
.IPNetwork('1.1.1.0/24')]),
994 [ipaddr
.IPNetwork('1.1.0.0/23')])
996 self
.assertEqual(ipaddr
.IPNetwork('::42:0/112').AddressExclude(
997 ipaddr
.IPNetwork('::42:8000/113')),
998 [ipaddr
.IPNetwork('::42:0/113')])
1000 self
.assertTrue(ipaddr
.IPNetwork('1::/8').CompareNetworks(
1001 ipaddr
.IPNetwork('2::/9')) < 0)
1003 self
.assertEqual(ipaddr
.IPNetwork('1::/16').Contains(
1004 ipaddr
.IPNetwork('2::/16')), False)
1006 self
.assertEqual(ipaddr
.IPNetwork('0.0.0.0/0').Subnet(),
1007 [ipaddr
.IPNetwork('0.0.0.0/1'),
1008 ipaddr
.IPNetwork('128.0.0.0/1')])
1009 self
.assertEqual(ipaddr
.IPNetwork('::/127').Subnet(),
1010 [ipaddr
.IPNetwork('::/128'),
1011 ipaddr
.IPNetwork('::1/128')])
1013 self
.assertEqual(ipaddr
.IPNetwork('1.0.0.0/32').Supernet(),
1014 ipaddr
.IPNetwork('1.0.0.0/31'))
1015 self
.assertEqual(ipaddr
.IPNetwork('::/121').Supernet(),
1016 ipaddr
.IPNetwork('::/120'))
1018 self
.assertEqual(ipaddr
.IPNetwork('10.0.0.2').IsRFC1918(), True)
1019 self
.assertEqual(ipaddr
.IPNetwork('10.0.0.0').IsMulticast(), False)
1020 self
.assertEqual(ipaddr
.IPNetwork('127.255.255.255').IsLoopback(), True)
1021 self
.assertEqual(ipaddr
.IPNetwork('169.255.255.255').IsLinkLocal(),
1024 def testForceVersion(self
):
1025 self
.assertEqual(ipaddr
.IPNetwork(1).version
, 4)
1026 self
.assertEqual(ipaddr
.IPNetwork(1, version
=6).version
, 6)
1028 def testWithStar(self
):
1029 self
.assertEqual(str(self
.ipv4
.with_prefixlen
), "1.2.3.4/24")
1030 self
.assertEqual(str(self
.ipv4
.with_netmask
), "1.2.3.4/255.255.255.0")
1031 self
.assertEqual(str(self
.ipv4
.with_hostmask
), "1.2.3.4/0.0.0.255")
1033 self
.assertEqual(str(self
.ipv6
.with_prefixlen
),
1034 '2001:658:22a:cafe:200::1/64')
1035 # rfc3513 sec 2.3 says that ipv6 only uses cidr notation for
1037 self
.assertEqual(str(self
.ipv6
.with_netmask
),
1038 '2001:658:22a:cafe:200::1/64')
1039 # this probably don't make much sense, but it's included for
1040 # compatibility with ipv4
1041 self
.assertEqual(str(self
.ipv6
.with_hostmask
),
1042 '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff')
1044 def testNetworkElementCaching(self
):
1045 # V4 - make sure we're empty
1046 self
.assertFalse(self
.ipv4
._cache
.has_key('network'))
1047 self
.assertFalse(self
.ipv4
._cache
.has_key('broadcast'))
1048 self
.assertFalse(self
.ipv4
._cache
.has_key('hostmask'))
1050 # V4 - populate and test
1051 self
.assertEqual(self
.ipv4
.network
, ipaddr
.IPv4Address('1.2.3.0'))
1052 self
.assertEqual(self
.ipv4
.broadcast
, ipaddr
.IPv4Address('1.2.3.255'))
1053 self
.assertEqual(self
.ipv4
.hostmask
, ipaddr
.IPv4Address('0.0.0.255'))
1055 # V4 - check we're cached
1056 self
.assertTrue(self
.ipv4
._cache
.has_key('network'))
1057 self
.assertTrue(self
.ipv4
._cache
.has_key('broadcast'))
1058 self
.assertTrue(self
.ipv4
._cache
.has_key('hostmask'))
1060 # V6 - make sure we're empty
1061 self
.assertFalse(self
.ipv6
._cache
.has_key('network'))
1062 self
.assertFalse(self
.ipv6
._cache
.has_key('broadcast'))
1063 self
.assertFalse(self
.ipv6
._cache
.has_key('hostmask'))
1065 # V6 - populate and test
1066 self
.assertEqual(self
.ipv6
.network
,
1067 ipaddr
.IPv6Address('2001:658:22a:cafe::'))
1068 self
.assertEqual(self
.ipv6
.broadcast
, ipaddr
.IPv6Address(
1069 '2001:658:22a:cafe:ffff:ffff:ffff:ffff'))
1070 self
.assertEqual(self
.ipv6
.hostmask
,
1071 ipaddr
.IPv6Address('::ffff:ffff:ffff:ffff'))
1073 # V6 - check we're cached
1074 self
.assertTrue(self
.ipv6
._cache
.has_key('network'))
1075 self
.assertTrue(self
.ipv6
._cache
.has_key('broadcast'))
1076 self
.assertTrue(self
.ipv6
._cache
.has_key('hostmask'))
1078 def testTeredo(self
):
1079 # stolen from wikipedia
1080 server
= ipaddr
.IPv4Address('65.54.227.120')
1081 client
= ipaddr
.IPv4Address('192.0.2.45')
1082 teredo_addr
= '2001:0000:4136:e378:8000:63bf:3fff:fdd2'
1083 self
.assertEqual((server
, client
),
1084 ipaddr
.IPAddress(teredo_addr
).teredo
)
1085 bad_addr
= '2000::4136:e378:8000:63bf:3fff:fdd2'
1086 self
.assertFalse(ipaddr
.IPAddress(bad_addr
).teredo
)
1087 bad_addr
= '2001:0001:4136:e378:8000:63bf:3fff:fdd2'
1088 self
.assertFalse(ipaddr
.IPAddress(bad_addr
).teredo
)
1091 teredo_addr
= ipaddr
.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
1092 self
.assertEqual((ipaddr
.IPv4Address('94.245.121.253'),
1093 ipaddr
.IPv4Address('95.26.244.94')),
1097 def testsixtofour(self
):
1098 sixtofouraddr
= ipaddr
.IPAddress('2002:ac1d:2d64::1')
1099 bad_addr
= ipaddr
.IPAddress('2000:ac1d:2d64::1')
1100 self
.assertEqual(ipaddr
.IPv4Address('172.29.45.100'),
1101 sixtofouraddr
.sixtofour
)
1102 self
.assertFalse(bad_addr
.sixtofour
)
1105 if __name__
== '__main__':