blob: f2947b9c8abd2f6600ab5dc5c11a2151a98c0cd6 [file] [log] [blame]
Nick Coghlandc9b2552012-05-20 21:01:57 +10001# Copyright 2007 Google Inc.
2# Licensed to PSF under a Contributor Agreement.
Nick Coghlandc9b2552012-05-20 21:01:57 +10003
Nick Coghlanaff73f92012-05-27 00:57:25 +10004"""Unittest for ipaddress module."""
Nick Coghlandc9b2552012-05-20 21:01:57 +10005
6
7import unittest
Nick Coghlan36f8dcd2012-07-07 19:23:53 +10008import re
9import contextlib
Nick Coghlane0c3f5e2012-08-05 18:20:17 +100010import operator
Nick Coghlandc9b2552012-05-20 21:01:57 +100011import ipaddress
12
Nick Coghlan07c4e332012-07-08 23:06:45 +100013class BaseTestCase(unittest.TestCase):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100014 # One big change in ipaddress over the original ipaddr module is
15 # error reporting that tries to assume users *don't know the rules*
16 # for what constitutes an RFC compliant IP address
17
Nick Coghlan07c4e332012-07-08 23:06:45 +100018 # Ensuring these errors are emitted correctly in all relevant cases
19 # meant moving to a more systematic test structure that allows the
20 # test structure to map more directly to the module structure
21
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100022 # Note that if the constructors are refactored so that addresses with
23 # multiple problems get classified differently, that's OK - just
24 # move the affected examples to the newly appropriate test case.
25
Nick Coghlan07c4e332012-07-08 23:06:45 +100026 # There is some duplication between the original relatively ad hoc
27 # test suite and the new systematic tests. While some redundancy in
28 # testing is considered preferable to accidentally deleting a valid
29 # test, the original test suite will likely be reduced over time as
30 # redundant tests are identified.
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100031
Nick Coghlan297b1432012-07-08 17:11:04 +100032 @property
33 def factory(self):
34 raise NotImplementedError
35
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100036 @contextlib.contextmanager
37 def assertCleanError(self, exc_type, details, *args):
38 """
39 Ensure exception does not display a context by default
40
41 Wraps unittest.TestCase.assertRaisesRegex
42 """
43 if args:
44 details = details % args
45 cm = self.assertRaisesRegex(exc_type, details)
46 with cm as exc:
47 yield exc
48 # Ensure we produce clean tracebacks on failure
49 if exc.exception.__context__ is not None:
50 self.assertTrue(exc.exception.__suppress_context__)
51
52 def assertAddressError(self, details, *args):
53 """Ensure a clean AddressValueError"""
54 return self.assertCleanError(ipaddress.AddressValueError,
55 details, *args)
56
57 def assertNetmaskError(self, details, *args):
58 """Ensure a clean NetmaskValueError"""
59 return self.assertCleanError(ipaddress.NetmaskValueError,
60 details, *args)
61
Nick Coghlan07c4e332012-07-08 23:06:45 +100062 def assertInstancesEqual(self, lhs, rhs):
63 """Check constructor arguments produce equivalent instances"""
64 self.assertEqual(self.factory(lhs), self.factory(rhs))
65
66class CommonTestMixin:
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100067
68 def test_empty_address(self):
69 with self.assertAddressError("Address cannot be empty"):
Nick Coghlan297b1432012-07-08 17:11:04 +100070 self.factory("")
71
72 def test_floats_rejected(self):
73 with self.assertAddressError(re.escape(repr("1.0"))):
74 self.factory(1.0)
75
Nick Coghlane0c3f5e2012-08-05 18:20:17 +100076 def test_not_an_index_issue15559(self):
77 # Implementing __index__ makes for a very nasty interaction with the
78 # bytes constructor. Thus, we disallow implicit use as an integer
79 self.assertRaises(TypeError, operator.index, self.factory(1))
80 self.assertRaises(TypeError, hex, self.factory(1))
81 self.assertRaises(TypeError, bytes, self.factory(1))
82
83
Nick Coghlan07c4e332012-07-08 23:06:45 +100084class CommonTestMixin_v4(CommonTestMixin):
85
86 def test_leading_zeros(self):
87 self.assertInstancesEqual("000.000.000.000", "0.0.0.0")
88 self.assertInstancesEqual("192.168.000.001", "192.168.0.1")
89
90 def test_int(self):
91 self.assertInstancesEqual(0, "0.0.0.0")
92 self.assertInstancesEqual(3232235521, "192.168.0.1")
93
94 def test_packed(self):
95 self.assertInstancesEqual(bytes.fromhex("00000000"), "0.0.0.0")
96 self.assertInstancesEqual(bytes.fromhex("c0a80001"), "192.168.0.1")
Nick Coghlan297b1432012-07-08 17:11:04 +100097
98 def test_negative_ints_rejected(self):
99 msg = "-1 (< 0) is not permitted as an IPv4 address"
100 with self.assertAddressError(re.escape(msg)):
101 self.factory(-1)
102
103 def test_large_ints_rejected(self):
104 msg = "%d (>= 2**32) is not permitted as an IPv4 address"
105 with self.assertAddressError(re.escape(msg % 2**32)):
106 self.factory(2**32)
107
108 def test_bad_packed_length(self):
109 def assertBadLength(length):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000110 addr = bytes(length)
Nick Coghlan297b1432012-07-08 17:11:04 +1000111 msg = "%r (len %d != 4) is not permitted as an IPv4 address"
112 with self.assertAddressError(re.escape(msg % (addr, length))):
113 self.factory(addr)
114
115 assertBadLength(3)
116 assertBadLength(5)
117
Nick Coghlan07c4e332012-07-08 23:06:45 +1000118class CommonTestMixin_v6(CommonTestMixin):
119
120 def test_leading_zeros(self):
121 self.assertInstancesEqual("0000::0000", "::")
122 self.assertInstancesEqual("000::c0a8:0001", "::c0a8:1")
123
124 def test_int(self):
125 self.assertInstancesEqual(0, "::")
126 self.assertInstancesEqual(3232235521, "::c0a8:1")
127
128 def test_packed(self):
129 addr = bytes(12) + bytes.fromhex("00000000")
130 self.assertInstancesEqual(addr, "::")
131 addr = bytes(12) + bytes.fromhex("c0a80001")
132 self.assertInstancesEqual(addr, "::c0a8:1")
133 addr = bytes.fromhex("c0a80001") + bytes(12)
134 self.assertInstancesEqual(addr, "c0a8:1::")
Nick Coghlan297b1432012-07-08 17:11:04 +1000135
136 def test_negative_ints_rejected(self):
137 msg = "-1 (< 0) is not permitted as an IPv6 address"
138 with self.assertAddressError(re.escape(msg)):
139 self.factory(-1)
140
141 def test_large_ints_rejected(self):
142 msg = "%d (>= 2**128) is not permitted as an IPv6 address"
143 with self.assertAddressError(re.escape(msg % 2**128)):
144 self.factory(2**128)
145
146 def test_bad_packed_length(self):
147 def assertBadLength(length):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000148 addr = bytes(length)
Nick Coghlan297b1432012-07-08 17:11:04 +1000149 msg = "%r (len %d != 16) is not permitted as an IPv6 address"
150 with self.assertAddressError(re.escape(msg % (addr, length))):
151 self.factory(addr)
152 self.factory(addr)
153
154 assertBadLength(15)
155 assertBadLength(17)
156
157
Nick Coghlan07c4e332012-07-08 23:06:45 +1000158class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
Nick Coghlan297b1432012-07-08 17:11:04 +1000159 factory = ipaddress.IPv4Address
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000160
161 def test_network_passed_as_address(self):
162 addr = "127.0.0.1/24"
163 with self.assertAddressError("Unexpected '/' in %r", addr):
164 ipaddress.IPv4Address(addr)
165
166 def test_bad_address_split(self):
167 def assertBadSplit(addr):
168 with self.assertAddressError("Expected 4 octets in %r", addr):
169 ipaddress.IPv4Address(addr)
170
171 assertBadSplit("127.0.1")
172 assertBadSplit("42.42.42.42.42")
173 assertBadSplit("42.42.42")
174 assertBadSplit("42.42")
175 assertBadSplit("42")
176 assertBadSplit("42..42.42.42")
177 assertBadSplit("42.42.42.42.")
178 assertBadSplit("42.42.42.42...")
179 assertBadSplit(".42.42.42.42")
180 assertBadSplit("...42.42.42.42")
181 assertBadSplit("016.016.016")
182 assertBadSplit("016.016")
183 assertBadSplit("016")
184 assertBadSplit("000")
185 assertBadSplit("0x0a.0x0a.0x0a")
186 assertBadSplit("0x0a.0x0a")
187 assertBadSplit("0x0a")
188 assertBadSplit(".")
189 assertBadSplit("bogus")
190 assertBadSplit("bogus.com")
191 assertBadSplit("1000")
192 assertBadSplit("1000000000000000")
193 assertBadSplit("192.168.0.1.com")
194
195 def test_empty_octet(self):
196 def assertBadOctet(addr):
197 with self.assertAddressError("Empty octet not permitted in %r",
198 addr):
199 ipaddress.IPv4Address(addr)
200
201 assertBadOctet("42..42.42")
202 assertBadOctet("...")
203
204 def test_invalid_characters(self):
205 def assertBadOctet(addr, octet):
206 msg = "Only decimal digits permitted in %r in %r" % (octet, addr)
207 with self.assertAddressError(re.escape(msg)):
208 ipaddress.IPv4Address(addr)
209
210 assertBadOctet("0x0a.0x0a.0x0a.0x0a", "0x0a")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000211 assertBadOctet("0xa.0x0a.0x0a.0x0a", "0xa")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000212 assertBadOctet("42.42.42.-0", "-0")
213 assertBadOctet("42.42.42.+0", "+0")
214 assertBadOctet("42.42.42.-42", "-42")
215 assertBadOctet("+1.+2.+3.4", "+1")
216 assertBadOctet("1.2.3.4e0", "4e0")
217 assertBadOctet("1.2.3.4::", "4::")
218 assertBadOctet("1.a.2.3", "a")
219
Nick Coghlan07c4e332012-07-08 23:06:45 +1000220 def test_octal_decimal_ambiguity(self):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000221 def assertBadOctet(addr, octet):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000222 msg = "Ambiguous (octal/decimal) value in %r not permitted in %r"
223 with self.assertAddressError(re.escape(msg % (octet, addr))):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000224 ipaddress.IPv4Address(addr)
225
226 assertBadOctet("016.016.016.016", "016")
227 assertBadOctet("001.000.008.016", "008")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000228
229 def test_octet_length(self):
230 def assertBadOctet(addr, octet):
231 msg = "At most 3 characters permitted in %r in %r"
232 with self.assertAddressError(re.escape(msg % (octet, addr))):
233 ipaddress.IPv4Address(addr)
234
235 assertBadOctet("0000.000.000.000", "0000")
236 assertBadOctet("12345.67899.-54321.-98765", "12345")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000237
238 def test_octet_limit(self):
239 def assertBadOctet(addr, octet):
Nick Coghlanb582ecc2012-07-07 22:15:22 +1000240 msg = "Octet %d (> 255) not permitted in %r" % (octet, addr)
241 with self.assertAddressError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000242 ipaddress.IPv4Address(addr)
243
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000244 assertBadOctet("257.0.0.0", 257)
Nick Coghlan07c4e332012-07-08 23:06:45 +1000245 assertBadOctet("192.168.0.999", 999)
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000246
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000247
Nick Coghlan07c4e332012-07-08 23:06:45 +1000248class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
Nick Coghlan297b1432012-07-08 17:11:04 +1000249 factory = ipaddress.IPv6Address
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000250
251 def test_network_passed_as_address(self):
252 addr = "::1/24"
253 with self.assertAddressError("Unexpected '/' in %r", addr):
254 ipaddress.IPv6Address(addr)
255
256 def test_bad_address_split_v6_not_enough_parts(self):
257 def assertBadSplit(addr):
258 msg = "At least 3 parts expected in %r"
259 with self.assertAddressError(msg, addr):
260 ipaddress.IPv6Address(addr)
261
262 assertBadSplit(":")
263 assertBadSplit(":1")
264 assertBadSplit("FEDC:9878")
265
266 def test_bad_address_split_v6_too_many_colons(self):
267 def assertBadSplit(addr):
268 msg = "At most 8 colons permitted in %r"
269 with self.assertAddressError(msg, addr):
270 ipaddress.IPv6Address(addr)
271
272 assertBadSplit("9:8:7:6:5:4:3::2:1")
273 assertBadSplit("10:9:8:7:6:5:4:3:2:1")
274 assertBadSplit("::8:7:6:5:4:3:2:1")
275 assertBadSplit("8:7:6:5:4:3:2:1::")
276 # A trailing IPv4 address is two parts
277 assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42")
278
279 def test_bad_address_split_v6_too_many_parts(self):
280 def assertBadSplit(addr):
281 msg = "Exactly 8 parts expected without '::' in %r"
282 with self.assertAddressError(msg, addr):
283 ipaddress.IPv6Address(addr)
284
285 assertBadSplit("3ffe:0:0:0:0:0:0:0:1")
286 assertBadSplit("9:8:7:6:5:4:3:2:1")
287 assertBadSplit("7:6:5:4:3:2:1")
288 # A trailing IPv4 address is two parts
289 assertBadSplit("9:8:7:6:5:4:3:42.42.42.42")
290 assertBadSplit("7:6:5:4:3:42.42.42.42")
291
292 def test_bad_address_split_v6_too_many_parts_with_double_colon(self):
293 def assertBadSplit(addr):
294 msg = "Expected at most 7 other parts with '::' in %r"
295 with self.assertAddressError(msg, addr):
296 ipaddress.IPv6Address(addr)
297
298 assertBadSplit("1:2:3:4::5:6:7:8")
299
300 def test_bad_address_split_v6_repeated_double_colon(self):
301 def assertBadSplit(addr):
302 msg = "At most one '::' permitted in %r"
303 with self.assertAddressError(msg, addr):
304 ipaddress.IPv6Address(addr)
305
306 assertBadSplit("3ffe::1::1")
307 assertBadSplit("1::2::3::4:5")
308 assertBadSplit("2001::db:::1")
309 assertBadSplit("3ffe::1::")
310 assertBadSplit("::3ffe::1")
311 assertBadSplit(":3ffe::1::1")
312 assertBadSplit("3ffe::1::1:")
313 assertBadSplit(":3ffe::1::1:")
314 assertBadSplit(":::")
315 assertBadSplit('2001:db8:::1')
316
317 def test_bad_address_split_v6_leading_colon(self):
318 def assertBadSplit(addr):
319 msg = "Leading ':' only permitted as part of '::' in %r"
320 with self.assertAddressError(msg, addr):
321 ipaddress.IPv6Address(addr)
322
323 assertBadSplit(":2001:db8::1")
324 assertBadSplit(":1:2:3:4:5:6:7")
325 assertBadSplit(":1:2:3:4:5:6:")
326 assertBadSplit(":6:5:4:3:2:1::")
327
328 def test_bad_address_split_v6_trailing_colon(self):
329 def assertBadSplit(addr):
330 msg = "Trailing ':' only permitted as part of '::' in %r"
331 with self.assertAddressError(msg, addr):
332 ipaddress.IPv6Address(addr)
333
334 assertBadSplit("2001:db8::1:")
335 assertBadSplit("1:2:3:4:5:6:7:")
336 assertBadSplit("::1.2.3.4:")
337 assertBadSplit("::7:6:5:4:3:2:")
338
339 def test_bad_v4_part_in(self):
340 def assertBadAddressPart(addr, v4_error):
341 with self.assertAddressError("%s in %r", v4_error, addr):
342 ipaddress.IPv6Address(addr)
343
344 assertBadAddressPart("3ffe::1.net", "Expected 4 octets in '1.net'")
345 assertBadAddressPart("3ffe::127.0.1",
346 "Expected 4 octets in '127.0.1'")
347 assertBadAddressPart("::1.2.3",
348 "Expected 4 octets in '1.2.3'")
349 assertBadAddressPart("::1.2.3.4.5",
350 "Expected 4 octets in '1.2.3.4.5'")
351 assertBadAddressPart("3ffe::1.1.1.net",
352 "Only decimal digits permitted in 'net' "
353 "in '1.1.1.net'")
354
355 def test_invalid_characters(self):
356 def assertBadPart(addr, part):
357 msg = "Only hex digits permitted in %r in %r" % (part, addr)
358 with self.assertAddressError(re.escape(msg)):
359 ipaddress.IPv6Address(addr)
360
361 assertBadPart("3ffe::goog", "goog")
362 assertBadPart("3ffe::-0", "-0")
363 assertBadPart("3ffe::+0", "+0")
364 assertBadPart("3ffe::-1", "-1")
365 assertBadPart("1.2.3.4::", "1.2.3.4")
366 assertBadPart('1234:axy::b', "axy")
367
368 def test_part_length(self):
369 def assertBadPart(addr, part):
370 msg = "At most 4 characters permitted in %r in %r"
371 with self.assertAddressError(msg, part, addr):
372 ipaddress.IPv6Address(addr)
373
Nick Coghlan07c4e332012-07-08 23:06:45 +1000374 assertBadPart("::00000", "00000")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000375 assertBadPart("3ffe::10000", "10000")
376 assertBadPart("02001:db8::", "02001")
377 assertBadPart('2001:888888::1', "888888")
378
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000379
Nick Coghlan07c4e332012-07-08 23:06:45 +1000380class NetmaskTestMixin_v4(CommonTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000381 """Input validation on interfaces and networks is very similar"""
382
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000383 def test_split_netmask(self):
384 addr = "1.2.3.4/32/24"
385 with self.assertAddressError("Only one '/' permitted in %r" % addr):
386 self.factory(addr)
387
388 def test_address_errors(self):
389 def assertBadAddress(addr, details):
390 with self.assertAddressError(details):
391 self.factory(addr)
392
Nick Coghlan297b1432012-07-08 17:11:04 +1000393 assertBadAddress("/", "Address cannot be empty")
394 assertBadAddress("/8", "Address cannot be empty")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000395 assertBadAddress("bogus", "Expected 4 octets")
396 assertBadAddress("google.com", "Expected 4 octets")
397 assertBadAddress("10/8", "Expected 4 octets")
398 assertBadAddress("::1.2.3.4", "Only decimal digits")
Nick Coghlanb582ecc2012-07-07 22:15:22 +1000399 assertBadAddress("1.2.3.256", re.escape("256 (> 255)"))
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000400
Nick Coghlan932346f2014-02-08 23:17:36 +1000401 def test_valid_netmask(self):
402 self.assertEqual(str(self.factory('192.0.2.0/255.255.255.0')),
403 '192.0.2.0/24')
404 for i in range(0, 33):
405 # Generate and re-parse the CIDR format (trivial).
406 net_str = '0.0.0.0/%d' % i
407 net = self.factory(net_str)
408 self.assertEqual(str(net), net_str)
409 # Generate and re-parse the expanded netmask.
410 self.assertEqual(
411 str(self.factory('0.0.0.0/%s' % net.netmask)), net_str)
412 # Zero prefix is treated as decimal.
413 self.assertEqual(str(self.factory('0.0.0.0/0%d' % i)), net_str)
414 # Generate and re-parse the expanded hostmask. The ambiguous
415 # cases (/0 and /32) are treated as netmasks.
416 if i in (32, 0):
417 net_str = '0.0.0.0/%d' % (32 - i)
418 self.assertEqual(
419 str(self.factory('0.0.0.0/%s' % net.hostmask)), net_str)
420
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000421 def test_netmask_errors(self):
422 def assertBadNetmask(addr, netmask):
Nick Coghlan932346f2014-02-08 23:17:36 +1000423 msg = "%r is not a valid netmask" % netmask
424 with self.assertNetmaskError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000425 self.factory("%s/%s" % (addr, netmask))
426
427 assertBadNetmask("1.2.3.4", "")
Nick Coghlan932346f2014-02-08 23:17:36 +1000428 assertBadNetmask("1.2.3.4", "-1")
429 assertBadNetmask("1.2.3.4", "+1")
430 assertBadNetmask("1.2.3.4", " 1 ")
431 assertBadNetmask("1.2.3.4", "0x1")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000432 assertBadNetmask("1.2.3.4", "33")
433 assertBadNetmask("1.2.3.4", "254.254.255.256")
Nick Coghlan932346f2014-02-08 23:17:36 +1000434 assertBadNetmask("1.2.3.4", "1.a.2.3")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000435 assertBadNetmask("1.1.1.1", "254.xyz.2.3")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000436 assertBadNetmask("1.1.1.1", "240.255.0.0")
Nick Coghlan932346f2014-02-08 23:17:36 +1000437 assertBadNetmask("1.1.1.1", "255.254.128.0")
438 assertBadNetmask("1.1.1.1", "0.1.127.255")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000439 assertBadNetmask("1.1.1.1", "pudding")
Nick Coghlan932346f2014-02-08 23:17:36 +1000440 assertBadNetmask("1.1.1.1", "::")
441
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000442
Nick Coghlan07c4e332012-07-08 23:06:45 +1000443class InterfaceTestCase_v4(BaseTestCase, NetmaskTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000444 factory = ipaddress.IPv4Interface
445
Nick Coghlan07c4e332012-07-08 23:06:45 +1000446class NetworkTestCase_v4(BaseTestCase, NetmaskTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000447 factory = ipaddress.IPv4Network
448
449
Nick Coghlan07c4e332012-07-08 23:06:45 +1000450class NetmaskTestMixin_v6(CommonTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000451 """Input validation on interfaces and networks is very similar"""
452
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000453 def test_split_netmask(self):
454 addr = "cafe:cafe::/128/190"
455 with self.assertAddressError("Only one '/' permitted in %r" % addr):
456 self.factory(addr)
457
458 def test_address_errors(self):
459 def assertBadAddress(addr, details):
460 with self.assertAddressError(details):
461 self.factory(addr)
462
Nick Coghlan297b1432012-07-08 17:11:04 +1000463 assertBadAddress("/", "Address cannot be empty")
464 assertBadAddress("/8", "Address cannot be empty")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000465 assertBadAddress("google.com", "At least 3 parts")
466 assertBadAddress("1.2.3.4", "At least 3 parts")
467 assertBadAddress("10/8", "At least 3 parts")
468 assertBadAddress("1234:axy::b", "Only hex digits")
469
Nick Coghlan932346f2014-02-08 23:17:36 +1000470 def test_valid_netmask(self):
471 # We only support CIDR for IPv6, because expanded netmasks are not
472 # standard notation.
473 self.assertEqual(str(self.factory('2001:db8::/32')), '2001:db8::/32')
474 for i in range(0, 129):
475 # Generate and re-parse the CIDR format (trivial).
476 net_str = '::/%d' % i
477 self.assertEqual(str(self.factory(net_str)), net_str)
478 # Zero prefix is treated as decimal.
479 self.assertEqual(str(self.factory('::/0%d' % i)), net_str)
480
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000481 def test_netmask_errors(self):
482 def assertBadNetmask(addr, netmask):
Nick Coghlan932346f2014-02-08 23:17:36 +1000483 msg = "%r is not a valid netmask" % netmask
484 with self.assertNetmaskError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000485 self.factory("%s/%s" % (addr, netmask))
486
487 assertBadNetmask("::1", "")
488 assertBadNetmask("::1", "::1")
489 assertBadNetmask("::1", "1::")
Nick Coghlan932346f2014-02-08 23:17:36 +1000490 assertBadNetmask("::1", "-1")
491 assertBadNetmask("::1", "+1")
492 assertBadNetmask("::1", " 1 ")
493 assertBadNetmask("::1", "0x1")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000494 assertBadNetmask("::1", "129")
Nick Coghlan932346f2014-02-08 23:17:36 +1000495 assertBadNetmask("::1", "1.2.3.4")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000496 assertBadNetmask("::1", "pudding")
Nick Coghlan932346f2014-02-08 23:17:36 +1000497 assertBadNetmask("::", "::")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000498
Nick Coghlan07c4e332012-07-08 23:06:45 +1000499class InterfaceTestCase_v6(BaseTestCase, NetmaskTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000500 factory = ipaddress.IPv6Interface
501
Nick Coghlan07c4e332012-07-08 23:06:45 +1000502class NetworkTestCase_v6(BaseTestCase, NetmaskTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000503 factory = ipaddress.IPv6Network
504
505
Nick Coghlan07c4e332012-07-08 23:06:45 +1000506class FactoryFunctionErrors(BaseTestCase):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000507
508 def assertFactoryError(self, factory, kind):
509 """Ensure a clean ValueError with the expected message"""
510 addr = "camelot"
511 msg = '%r does not appear to be an IPv4 or IPv6 %s'
512 with self.assertCleanError(ValueError, msg, addr, kind):
513 factory(addr)
514
515 def test_ip_address(self):
516 self.assertFactoryError(ipaddress.ip_address, "address")
517
518 def test_ip_interface(self):
519 self.assertFactoryError(ipaddress.ip_interface, "interface")
520
521 def test_ip_network(self):
522 self.assertFactoryError(ipaddress.ip_network, "network")
523
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200524
Nick Coghlan3008ec02012-07-08 00:45:33 +1000525class ComparisonTests(unittest.TestCase):
526
527 v4addr = ipaddress.IPv4Address(1)
528 v4net = ipaddress.IPv4Network(1)
529 v4intf = ipaddress.IPv4Interface(1)
530 v6addr = ipaddress.IPv6Address(1)
531 v6net = ipaddress.IPv6Network(1)
532 v6intf = ipaddress.IPv6Interface(1)
533
534 v4_addresses = [v4addr, v4intf]
535 v4_objects = v4_addresses + [v4net]
536 v6_addresses = [v6addr, v6intf]
537 v6_objects = v6_addresses + [v6net]
538 objects = v4_objects + v6_objects
539
540 def test_foreign_type_equality(self):
541 # __eq__ should never raise TypeError directly
542 other = object()
543 for obj in self.objects:
544 self.assertNotEqual(obj, other)
545 self.assertFalse(obj == other)
546 self.assertEqual(obj.__eq__(other), NotImplemented)
547 self.assertEqual(obj.__ne__(other), NotImplemented)
548
549 def test_mixed_type_equality(self):
550 # Ensure none of the internal objects accidentally
551 # expose the right set of attributes to become "equal"
552 for lhs in self.objects:
553 for rhs in self.objects:
554 if lhs is rhs:
555 continue
556 self.assertNotEqual(lhs, rhs)
557
558 def test_containment(self):
559 for obj in self.v4_addresses:
560 self.assertIn(obj, self.v4net)
561 for obj in self.v6_addresses:
562 self.assertIn(obj, self.v6net)
563 for obj in self.v4_objects + [self.v6net]:
564 self.assertNotIn(obj, self.v6net)
565 for obj in self.v6_objects + [self.v4net]:
566 self.assertNotIn(obj, self.v4net)
567
568 def test_mixed_type_ordering(self):
569 for lhs in self.objects:
570 for rhs in self.objects:
571 if isinstance(lhs, type(rhs)) or isinstance(rhs, type(lhs)):
572 continue
573 self.assertRaises(TypeError, lambda: lhs < rhs)
574 self.assertRaises(TypeError, lambda: lhs > rhs)
575 self.assertRaises(TypeError, lambda: lhs <= rhs)
576 self.assertRaises(TypeError, lambda: lhs >= rhs)
577
578 def test_mixed_type_key(self):
579 # with get_mixed_type_key, you can sort addresses and network.
580 v4_ordered = [self.v4addr, self.v4net, self.v4intf]
581 v6_ordered = [self.v6addr, self.v6net, self.v6intf]
582 self.assertEqual(v4_ordered,
583 sorted(self.v4_objects,
584 key=ipaddress.get_mixed_type_key))
585 self.assertEqual(v6_ordered,
586 sorted(self.v6_objects,
587 key=ipaddress.get_mixed_type_key))
588 self.assertEqual(v4_ordered + v6_ordered,
589 sorted(self.objects,
590 key=ipaddress.get_mixed_type_key))
591 self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object))
592
593 def test_incompatible_versions(self):
594 # These should always raise TypeError
595 v4addr = ipaddress.ip_address('1.1.1.1')
596 v4net = ipaddress.ip_network('1.1.1.1')
597 v6addr = ipaddress.ip_address('::1')
598 v6net = ipaddress.ip_address('::1')
599
600 self.assertRaises(TypeError, v4addr.__lt__, v6addr)
601 self.assertRaises(TypeError, v4addr.__gt__, v6addr)
602 self.assertRaises(TypeError, v4net.__lt__, v6net)
603 self.assertRaises(TypeError, v4net.__gt__, v6net)
604
605 self.assertRaises(TypeError, v6addr.__lt__, v4addr)
606 self.assertRaises(TypeError, v6addr.__gt__, v4addr)
607 self.assertRaises(TypeError, v6net.__lt__, v4net)
608 self.assertRaises(TypeError, v6net.__gt__, v4net)
609
610
611
Nick Coghlandc9b2552012-05-20 21:01:57 +1000612class IpaddrUnitTest(unittest.TestCase):
613
614 def setUp(self):
615 self.ipv4_address = ipaddress.IPv4Address('1.2.3.4')
616 self.ipv4_interface = ipaddress.IPv4Interface('1.2.3.4/24')
617 self.ipv4_network = ipaddress.IPv4Network('1.2.3.0/24')
618 #self.ipv4_hostmask = ipaddress.IPv4Interface('10.0.0.1/0.255.255.255')
619 self.ipv6_address = ipaddress.IPv6Interface(
620 '2001:658:22a:cafe:200:0:0:1')
621 self.ipv6_interface = ipaddress.IPv6Interface(
622 '2001:658:22a:cafe:200:0:0:1/64')
623 self.ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/64')
624
625 def testRepr(self):
626 self.assertEqual("IPv4Interface('1.2.3.4/32')",
627 repr(ipaddress.IPv4Interface('1.2.3.4')))
628 self.assertEqual("IPv6Interface('::1/128')",
629 repr(ipaddress.IPv6Interface('::1')))
630
631 # issue57
632 def testAddressIntMath(self):
633 self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255,
634 ipaddress.IPv4Address('1.1.2.0'))
635 self.assertEqual(ipaddress.IPv4Address('1.1.1.1') - 256,
636 ipaddress.IPv4Address('1.1.0.1'))
637 self.assertEqual(ipaddress.IPv6Address('::1') + (2**16 - 2),
638 ipaddress.IPv6Address('::ffff'))
639 self.assertEqual(ipaddress.IPv6Address('::ffff') - (2**16 - 2),
640 ipaddress.IPv6Address('::1'))
641
Nick Coghlan3c2570c2012-07-07 01:13:55 +1000642 def testInvalidIntToBytes(self):
643 self.assertRaises(ValueError, ipaddress.v4_int_to_packed, -1)
644 self.assertRaises(ValueError, ipaddress.v4_int_to_packed,
645 2 ** ipaddress.IPV4LENGTH)
646 self.assertRaises(ValueError, ipaddress.v6_int_to_packed, -1)
647 self.assertRaises(ValueError, ipaddress.v6_int_to_packed,
648 2 ** ipaddress.IPV6LENGTH)
649
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200650 def testInternals(self):
651 first, last = ipaddress._find_address_range([
652 ipaddress.IPv4Address('10.10.10.10'),
653 ipaddress.IPv4Address('10.10.10.12')])
654 self.assertEqual(first, last)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200655 self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200656 self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000657
Nick Coghland9722652012-06-17 16:33:00 +1000658 def testMissingAddressVersion(self):
659 class Broken(ipaddress._BaseAddress):
660 pass
661 broken = Broken('127.0.0.1')
662 with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
663 broken.version
664
665 def testMissingNetworkVersion(self):
666 class Broken(ipaddress._BaseNetwork):
667 pass
668 broken = Broken('127.0.0.1')
669 with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
670 broken.version
671
672 def testMissingAddressClass(self):
673 class Broken(ipaddress._BaseNetwork):
674 pass
675 broken = Broken('127.0.0.1')
676 with self.assertRaisesRegex(NotImplementedError, "Broken.*address"):
677 broken._address_class
678
Nick Coghlandc9b2552012-05-20 21:01:57 +1000679 def testGetNetwork(self):
680 self.assertEqual(int(self.ipv4_network.network_address), 16909056)
681 self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0')
682
683 self.assertEqual(int(self.ipv6_network.network_address),
684 42540616829182469433403647294022090752)
685 self.assertEqual(str(self.ipv6_network.network_address),
686 '2001:658:22a:cafe::')
687 self.assertEqual(str(self.ipv6_network.hostmask),
688 '::ffff:ffff:ffff:ffff')
689
Nick Coghlandc9b2552012-05-20 21:01:57 +1000690 def testIpFromInt(self):
691 self.assertEqual(self.ipv4_interface._ip,
692 ipaddress.IPv4Interface(16909060)._ip)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000693
694 ipv4 = ipaddress.ip_network('1.2.3.4')
695 ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000696 self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4.network_address)))
697 self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6.network_address)))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000698
699 v6_int = 42540616829182469433547762482097946625
700 self.assertEqual(self.ipv6_interface._ip,
701 ipaddress.IPv6Interface(v6_int)._ip)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000702
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200703 self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version,
704 4)
705 self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version,
706 6)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000707
708 def testIpFromPacked(self):
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000709 address = ipaddress.ip_address
Nick Coghlandc9b2552012-05-20 21:01:57 +1000710 self.assertEqual(self.ipv4_interface._ip,
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000711 ipaddress.ip_interface(b'\x01\x02\x03\x04')._ip)
712 self.assertEqual(address('255.254.253.252'),
713 address(b'\xff\xfe\xfd\xfc'))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000714 self.assertEqual(self.ipv6_interface.ip,
715 ipaddress.ip_interface(
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000716 b'\x20\x01\x06\x58\x02\x2a\xca\xfe'
717 b'\x02\x00\x00\x00\x00\x00\x00\x01').ip)
718 self.assertEqual(address('ffff:2:3:4:ffff::'),
719 address(b'\xff\xff\x00\x02\x00\x03\x00\x04' +
720 b'\xff\xff' + b'\x00' * 6))
721 self.assertEqual(address('::'),
722 address(b'\x00' * 16))
723
Nick Coghlandc9b2552012-05-20 21:01:57 +1000724 def testGetIp(self):
725 self.assertEqual(int(self.ipv4_interface.ip), 16909060)
726 self.assertEqual(str(self.ipv4_interface.ip), '1.2.3.4')
727
728 self.assertEqual(int(self.ipv6_interface.ip),
729 42540616829182469433547762482097946625)
730 self.assertEqual(str(self.ipv6_interface.ip),
731 '2001:658:22a:cafe:200::1')
732
733 def testGetNetmask(self):
734 self.assertEqual(int(self.ipv4_network.netmask), 4294967040)
735 self.assertEqual(str(self.ipv4_network.netmask), '255.255.255.0')
736 self.assertEqual(int(self.ipv6_network.netmask),
737 340282366920938463444927863358058659840)
738 self.assertEqual(self.ipv6_network.prefixlen, 64)
739
740 def testZeroNetmask(self):
741 ipv4_zero_netmask = ipaddress.IPv4Interface('1.2.3.4/0')
742 self.assertEqual(int(ipv4_zero_netmask.network.netmask), 0)
Nick Coghlan932346f2014-02-08 23:17:36 +1000743 self.assertEqual(ipv4_zero_netmask._prefix_from_prefix_string('0'), 0)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200744 self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0'))
745 self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0.0.0.0'))
746 self.assertFalse(ipv4_zero_netmask._is_valid_netmask('invalid'))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000747
748 ipv6_zero_netmask = ipaddress.IPv6Interface('::1/0')
749 self.assertEqual(int(ipv6_zero_netmask.network.netmask), 0)
Nick Coghlan932346f2014-02-08 23:17:36 +1000750 self.assertEqual(ipv6_zero_netmask._prefix_from_prefix_string('0'), 0)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000751
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200752 def testIPv4NetAndHostmasks(self):
753 net = self.ipv4_network
754 self.assertFalse(net._is_valid_netmask('invalid'))
755 self.assertTrue(net._is_valid_netmask('128.128.128.128'))
756 self.assertFalse(net._is_valid_netmask('128.128.128.127'))
757 self.assertFalse(net._is_valid_netmask('128.128.128.255'))
758 self.assertTrue(net._is_valid_netmask('255.128.128.128'))
759
760 self.assertFalse(net._is_hostmask('invalid'))
761 self.assertTrue(net._is_hostmask('128.255.255.255'))
762 self.assertFalse(net._is_hostmask('255.255.255.255'))
763 self.assertFalse(net._is_hostmask('1.2.3.4'))
764
765 net = ipaddress.IPv4Network('127.0.0.0/0.0.0.255')
Nick Coghlan932346f2014-02-08 23:17:36 +1000766 self.assertEqual(net.prefixlen, 24)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200767
Nick Coghlandc9b2552012-05-20 21:01:57 +1000768 def testGetBroadcast(self):
769 self.assertEqual(int(self.ipv4_network.broadcast_address), 16909311)
770 self.assertEqual(str(self.ipv4_network.broadcast_address), '1.2.3.255')
771
772 self.assertEqual(int(self.ipv6_network.broadcast_address),
773 42540616829182469451850391367731642367)
774 self.assertEqual(str(self.ipv6_network.broadcast_address),
775 '2001:658:22a:cafe:ffff:ffff:ffff:ffff')
776
777 def testGetPrefixlen(self):
Nick Coghlane3ded952012-08-05 22:45:22 +1000778 self.assertEqual(self.ipv4_interface.network.prefixlen, 24)
779 self.assertEqual(self.ipv6_interface.network.prefixlen, 64)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000780
781 def testGetSupernet(self):
782 self.assertEqual(self.ipv4_network.supernet().prefixlen, 23)
783 self.assertEqual(str(self.ipv4_network.supernet().network_address),
784 '1.2.2.0')
785 self.assertEqual(
786 ipaddress.IPv4Interface('0.0.0.0/0').network.supernet(),
787 ipaddress.IPv4Network('0.0.0.0/0'))
788
789 self.assertEqual(self.ipv6_network.supernet().prefixlen, 63)
790 self.assertEqual(str(self.ipv6_network.supernet().network_address),
791 '2001:658:22a:cafe::')
792 self.assertEqual(ipaddress.IPv6Interface('::0/0').network.supernet(),
793 ipaddress.IPv6Network('::0/0'))
794
795 def testGetSupernet3(self):
796 self.assertEqual(self.ipv4_network.supernet(3).prefixlen, 21)
797 self.assertEqual(str(self.ipv4_network.supernet(3).network_address),
798 '1.2.0.0')
799
800 self.assertEqual(self.ipv6_network.supernet(3).prefixlen, 61)
801 self.assertEqual(str(self.ipv6_network.supernet(3).network_address),
802 '2001:658:22a:caf8::')
803
804 def testGetSupernet4(self):
805 self.assertRaises(ValueError, self.ipv4_network.supernet,
806 prefixlen_diff=2, new_prefix=1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200807 self.assertRaises(ValueError, self.ipv4_network.supernet,
808 new_prefix=25)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000809 self.assertEqual(self.ipv4_network.supernet(prefixlen_diff=2),
810 self.ipv4_network.supernet(new_prefix=22))
811
812 self.assertRaises(ValueError, self.ipv6_network.supernet,
813 prefixlen_diff=2, new_prefix=1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200814 self.assertRaises(ValueError, self.ipv6_network.supernet,
815 new_prefix=65)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000816 self.assertEqual(self.ipv6_network.supernet(prefixlen_diff=2),
817 self.ipv6_network.supernet(new_prefix=62))
818
819 def testHosts(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200820 hosts = list(self.ipv4_network.hosts())
821 self.assertEqual(254, len(hosts))
822 self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0])
823 self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1])
824
825 # special case where only 1 bit is left for address
Nick Coghlandc9b2552012-05-20 21:01:57 +1000826 self.assertEqual([ipaddress.IPv4Address('2.0.0.0'),
827 ipaddress.IPv4Address('2.0.0.1')],
828 list(ipaddress.ip_network('2.0.0.0/31').hosts()))
829
830 def testFancySubnetting(self):
831 self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)),
832 sorted(self.ipv4_network.subnets(new_prefix=27)))
833 self.assertRaises(ValueError, list,
834 self.ipv4_network.subnets(new_prefix=23))
835 self.assertRaises(ValueError, list,
836 self.ipv4_network.subnets(prefixlen_diff=3,
837 new_prefix=27))
838 self.assertEqual(sorted(self.ipv6_network.subnets(prefixlen_diff=4)),
839 sorted(self.ipv6_network.subnets(new_prefix=68)))
840 self.assertRaises(ValueError, list,
841 self.ipv6_network.subnets(new_prefix=63))
842 self.assertRaises(ValueError, list,
843 self.ipv6_network.subnets(prefixlen_diff=4,
844 new_prefix=68))
845
846 def testGetSubnets(self):
847 self.assertEqual(list(self.ipv4_network.subnets())[0].prefixlen, 25)
848 self.assertEqual(str(list(
849 self.ipv4_network.subnets())[0].network_address),
850 '1.2.3.0')
851 self.assertEqual(str(list(
852 self.ipv4_network.subnets())[1].network_address),
853 '1.2.3.128')
854
855 self.assertEqual(list(self.ipv6_network.subnets())[0].prefixlen, 65)
856
857 def testGetSubnetForSingle32(self):
858 ip = ipaddress.IPv4Network('1.2.3.4/32')
859 subnets1 = [str(x) for x in ip.subnets()]
860 subnets2 = [str(x) for x in ip.subnets(2)]
861 self.assertEqual(subnets1, ['1.2.3.4/32'])
862 self.assertEqual(subnets1, subnets2)
863
864 def testGetSubnetForSingle128(self):
865 ip = ipaddress.IPv6Network('::1/128')
866 subnets1 = [str(x) for x in ip.subnets()]
867 subnets2 = [str(x) for x in ip.subnets(2)]
868 self.assertEqual(subnets1, ['::1/128'])
869 self.assertEqual(subnets1, subnets2)
870
871 def testSubnet2(self):
872 ips = [str(x) for x in self.ipv4_network.subnets(2)]
873 self.assertEqual(
874 ips,
875 ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26'])
876
877 ipsv6 = [str(x) for x in self.ipv6_network.subnets(2)]
878 self.assertEqual(
879 ipsv6,
880 ['2001:658:22a:cafe::/66',
881 '2001:658:22a:cafe:4000::/66',
882 '2001:658:22a:cafe:8000::/66',
883 '2001:658:22a:cafe:c000::/66'])
884
885 def testSubnetFailsForLargeCidrDiff(self):
886 self.assertRaises(ValueError, list,
887 self.ipv4_interface.network.subnets(9))
888 self.assertRaises(ValueError, list,
889 self.ipv4_network.subnets(9))
890 self.assertRaises(ValueError, list,
891 self.ipv6_interface.network.subnets(65))
892 self.assertRaises(ValueError, list,
893 self.ipv6_network.subnets(65))
894
895 def testSupernetFailsForLargeCidrDiff(self):
896 self.assertRaises(ValueError,
897 self.ipv4_interface.network.supernet, 25)
898 self.assertRaises(ValueError,
899 self.ipv6_interface.network.supernet, 65)
900
901 def testSubnetFailsForNegativeCidrDiff(self):
902 self.assertRaises(ValueError, list,
903 self.ipv4_interface.network.subnets(-1))
904 self.assertRaises(ValueError, list,
Nick Coghlan2c589102012-05-27 01:03:25 +1000905 self.ipv4_network.subnets(-1))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000906 self.assertRaises(ValueError, list,
907 self.ipv6_interface.network.subnets(-1))
908 self.assertRaises(ValueError, list,
909 self.ipv6_network.subnets(-1))
910
911 def testGetNum_Addresses(self):
912 self.assertEqual(self.ipv4_network.num_addresses, 256)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200913 self.assertEqual(list(self.ipv4_network.subnets())[0].num_addresses,
914 128)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000915 self.assertEqual(self.ipv4_network.supernet().num_addresses, 512)
916
917 self.assertEqual(self.ipv6_network.num_addresses, 18446744073709551616)
918 self.assertEqual(list(self.ipv6_network.subnets())[0].num_addresses,
919 9223372036854775808)
920 self.assertEqual(self.ipv6_network.supernet().num_addresses,
921 36893488147419103232)
922
923 def testContains(self):
Serhiy Storchaka7c389e22014-02-08 16:38:35 +0200924 self.assertIn(ipaddress.IPv4Interface('1.2.3.128/25'),
925 self.ipv4_network)
926 self.assertNotIn(ipaddress.IPv4Interface('1.2.4.1/24'),
Nick Coghlandc9b2552012-05-20 21:01:57 +1000927 self.ipv4_network)
928 # We can test addresses and string as well.
929 addr1 = ipaddress.IPv4Address('1.2.3.37')
Serhiy Storchaka7c389e22014-02-08 16:38:35 +0200930 self.assertIn(addr1, self.ipv4_network)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000931 # issue 61, bad network comparison on like-ip'd network objects
932 # with identical broadcast addresses.
933 self.assertFalse(ipaddress.IPv4Network('1.1.0.0/16').__contains__(
934 ipaddress.IPv4Network('1.0.0.0/15')))
935
Nick Coghlandc9b2552012-05-20 21:01:57 +1000936 def testNth(self):
937 self.assertEqual(str(self.ipv4_network[5]), '1.2.3.5')
938 self.assertRaises(IndexError, self.ipv4_network.__getitem__, 256)
939
940 self.assertEqual(str(self.ipv6_network[5]),
941 '2001:658:22a:cafe::5')
942
943 def testGetitem(self):
944 # http://code.google.com/p/ipaddr-py/issues/detail?id=15
945 addr = ipaddress.IPv4Network('172.31.255.128/255.255.255.240')
946 self.assertEqual(28, addr.prefixlen)
947 addr_list = list(addr)
948 self.assertEqual('172.31.255.128', str(addr_list[0]))
949 self.assertEqual('172.31.255.128', str(addr[0]))
950 self.assertEqual('172.31.255.143', str(addr_list[-1]))
951 self.assertEqual('172.31.255.143', str(addr[-1]))
952 self.assertEqual(addr_list[-1], addr[-1])
953
954 def testEqual(self):
955 self.assertTrue(self.ipv4_interface ==
956 ipaddress.IPv4Interface('1.2.3.4/24'))
957 self.assertFalse(self.ipv4_interface ==
958 ipaddress.IPv4Interface('1.2.3.4/23'))
959 self.assertFalse(self.ipv4_interface ==
960 ipaddress.IPv6Interface('::1.2.3.4/24'))
961 self.assertFalse(self.ipv4_interface == '')
962 self.assertFalse(self.ipv4_interface == [])
963 self.assertFalse(self.ipv4_interface == 2)
964
965 self.assertTrue(self.ipv6_interface ==
966 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64'))
967 self.assertFalse(self.ipv6_interface ==
968 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63'))
969 self.assertFalse(self.ipv6_interface ==
970 ipaddress.IPv4Interface('1.2.3.4/23'))
971 self.assertFalse(self.ipv6_interface == '')
972 self.assertFalse(self.ipv6_interface == [])
973 self.assertFalse(self.ipv6_interface == 2)
974
975 def testNotEqual(self):
976 self.assertFalse(self.ipv4_interface !=
977 ipaddress.IPv4Interface('1.2.3.4/24'))
978 self.assertTrue(self.ipv4_interface !=
979 ipaddress.IPv4Interface('1.2.3.4/23'))
980 self.assertTrue(self.ipv4_interface !=
981 ipaddress.IPv6Interface('::1.2.3.4/24'))
982 self.assertTrue(self.ipv4_interface != '')
983 self.assertTrue(self.ipv4_interface != [])
984 self.assertTrue(self.ipv4_interface != 2)
985
986 self.assertTrue(self.ipv4_address !=
987 ipaddress.IPv4Address('1.2.3.5'))
988 self.assertTrue(self.ipv4_address != '')
989 self.assertTrue(self.ipv4_address != [])
990 self.assertTrue(self.ipv4_address != 2)
991
992 self.assertFalse(self.ipv6_interface !=
993 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64'))
994 self.assertTrue(self.ipv6_interface !=
995 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63'))
996 self.assertTrue(self.ipv6_interface !=
997 ipaddress.IPv4Interface('1.2.3.4/23'))
998 self.assertTrue(self.ipv6_interface != '')
999 self.assertTrue(self.ipv6_interface != [])
1000 self.assertTrue(self.ipv6_interface != 2)
1001
1002 self.assertTrue(self.ipv6_address !=
1003 ipaddress.IPv4Address('1.2.3.4'))
1004 self.assertTrue(self.ipv6_address != '')
1005 self.assertTrue(self.ipv6_address != [])
1006 self.assertTrue(self.ipv6_address != 2)
1007
1008 def testSlash32Constructor(self):
1009 self.assertEqual(str(ipaddress.IPv4Interface(
1010 '1.2.3.4/255.255.255.255')), '1.2.3.4/32')
1011
1012 def testSlash128Constructor(self):
1013 self.assertEqual(str(ipaddress.IPv6Interface('::1/128')),
1014 '::1/128')
1015
1016 def testSlash0Constructor(self):
1017 self.assertEqual(str(ipaddress.IPv4Interface('1.2.3.4/0.0.0.0')),
1018 '1.2.3.4/0')
1019
1020 def testCollapsing(self):
1021 # test only IP addresses including some duplicates
1022 ip1 = ipaddress.IPv4Address('1.1.1.0')
1023 ip2 = ipaddress.IPv4Address('1.1.1.1')
1024 ip3 = ipaddress.IPv4Address('1.1.1.2')
1025 ip4 = ipaddress.IPv4Address('1.1.1.3')
1026 ip5 = ipaddress.IPv4Address('1.1.1.4')
1027 ip6 = ipaddress.IPv4Address('1.1.1.0')
1028 # check that addreses are subsumed properly.
1029 collapsed = ipaddress.collapse_addresses(
1030 [ip1, ip2, ip3, ip4, ip5, ip6])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001031 self.assertEqual(list(collapsed),
1032 [ipaddress.IPv4Network('1.1.1.0/30'),
1033 ipaddress.IPv4Network('1.1.1.4/32')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001034
1035 # test a mix of IP addresses and networks including some duplicates
1036 ip1 = ipaddress.IPv4Address('1.1.1.0')
1037 ip2 = ipaddress.IPv4Address('1.1.1.1')
1038 ip3 = ipaddress.IPv4Address('1.1.1.2')
1039 ip4 = ipaddress.IPv4Address('1.1.1.3')
1040 #ip5 = ipaddress.IPv4Interface('1.1.1.4/30')
1041 #ip6 = ipaddress.IPv4Interface('1.1.1.4/30')
1042 # check that addreses are subsumed properly.
1043 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001044 self.assertEqual(list(collapsed),
1045 [ipaddress.IPv4Network('1.1.1.0/30')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001046
1047 # test only IP networks
1048 ip1 = ipaddress.IPv4Network('1.1.0.0/24')
1049 ip2 = ipaddress.IPv4Network('1.1.1.0/24')
1050 ip3 = ipaddress.IPv4Network('1.1.2.0/24')
1051 ip4 = ipaddress.IPv4Network('1.1.3.0/24')
1052 ip5 = ipaddress.IPv4Network('1.1.4.0/24')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001053 # stored in no particular order b/c we want CollapseAddr to call
1054 # [].sort
Nick Coghlandc9b2552012-05-20 21:01:57 +10001055 ip6 = ipaddress.IPv4Network('1.1.0.0/22')
1056 # check that addreses are subsumed properly.
1057 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4, ip5,
1058 ip6])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001059 self.assertEqual(list(collapsed),
1060 [ipaddress.IPv4Network('1.1.0.0/22'),
1061 ipaddress.IPv4Network('1.1.4.0/24')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001062
1063 # test that two addresses are supernet'ed properly
1064 collapsed = ipaddress.collapse_addresses([ip1, ip2])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001065 self.assertEqual(list(collapsed),
1066 [ipaddress.IPv4Network('1.1.0.0/23')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001067
1068 # test same IP networks
1069 ip_same1 = ip_same2 = ipaddress.IPv4Network('1.1.1.1/32')
1070 self.assertEqual(list(ipaddress.collapse_addresses(
1071 [ip_same1, ip_same2])),
1072 [ip_same1])
1073
1074 # test same IP addresses
1075 ip_same1 = ip_same2 = ipaddress.IPv4Address('1.1.1.1')
1076 self.assertEqual(list(ipaddress.collapse_addresses(
1077 [ip_same1, ip_same2])),
1078 [ipaddress.ip_network('1.1.1.1/32')])
1079 ip1 = ipaddress.IPv6Network('2001::/100')
1080 ip2 = ipaddress.IPv6Network('2001::/120')
1081 ip3 = ipaddress.IPv6Network('2001::/96')
1082 # test that ipv6 addresses are subsumed properly.
1083 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3])
1084 self.assertEqual(list(collapsed), [ip3])
1085
1086 # the toejam test
Hynek Schlawack35db5132012-06-01 20:12:17 +02001087 addr_tuples = [
1088 (ipaddress.ip_address('1.1.1.1'),
1089 ipaddress.ip_address('::1')),
1090 (ipaddress.IPv4Network('1.1.0.0/24'),
1091 ipaddress.IPv6Network('2001::/120')),
1092 (ipaddress.IPv4Network('1.1.0.0/32'),
1093 ipaddress.IPv6Network('2001::/128')),
1094 ]
1095 for ip1, ip2 in addr_tuples:
1096 self.assertRaises(TypeError, ipaddress.collapse_addresses,
1097 [ip1, ip2])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001098
1099 def testSummarizing(self):
1100 #ip = ipaddress.ip_address
1101 #ipnet = ipaddress.ip_network
1102 summarize = ipaddress.summarize_address_range
1103 ip1 = ipaddress.ip_address('1.1.1.0')
1104 ip2 = ipaddress.ip_address('1.1.1.255')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001105
1106 # summarize works only for IPv4 & IPv6
1107 class IPv7Address(ipaddress.IPv6Address):
1108 @property
1109 def version(self):
1110 return 7
1111 ip_invalid1 = IPv7Address('::1')
1112 ip_invalid2 = IPv7Address('::1')
1113 self.assertRaises(ValueError, list,
1114 summarize(ip_invalid1, ip_invalid2))
1115 # test that a summary over ip4 & ip6 fails
1116 self.assertRaises(TypeError, list,
1117 summarize(ip1, ipaddress.IPv6Address('::1')))
1118 # test a /24 is summarized properly
Nick Coghlandc9b2552012-05-20 21:01:57 +10001119 self.assertEqual(list(summarize(ip1, ip2))[0],
1120 ipaddress.ip_network('1.1.1.0/24'))
1121 # test an IPv4 range that isn't on a network byte boundary
1122 ip2 = ipaddress.ip_address('1.1.1.8')
1123 self.assertEqual(list(summarize(ip1, ip2)),
1124 [ipaddress.ip_network('1.1.1.0/29'),
1125 ipaddress.ip_network('1.1.1.8')])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001126 # all!
1127 ip1 = ipaddress.IPv4Address(0)
1128 ip2 = ipaddress.IPv4Address(ipaddress.IPv4Address._ALL_ONES)
1129 self.assertEqual([ipaddress.IPv4Network('0.0.0.0/0')],
1130 list(summarize(ip1, ip2)))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001131
1132 ip1 = ipaddress.ip_address('1::')
1133 ip2 = ipaddress.ip_address('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
1134 # test a IPv6 is sumamrized properly
1135 self.assertEqual(list(summarize(ip1, ip2))[0],
1136 ipaddress.ip_network('1::/16'))
1137 # test an IPv6 range that isn't on a network byte boundary
1138 ip2 = ipaddress.ip_address('2::')
1139 self.assertEqual(list(summarize(ip1, ip2)),
1140 [ipaddress.ip_network('1::/16'),
1141 ipaddress.ip_network('2::/128')])
1142
1143 # test exception raised when first is greater than last
1144 self.assertRaises(ValueError, list,
1145 summarize(ipaddress.ip_address('1.1.1.0'),
1146 ipaddress.ip_address('1.1.0.0')))
1147 # test exception raised when first and last aren't IP addresses
1148 self.assertRaises(TypeError, list,
1149 summarize(ipaddress.ip_network('1.1.1.0'),
1150 ipaddress.ip_network('1.1.0.0')))
1151 self.assertRaises(TypeError, list,
1152 summarize(ipaddress.ip_network('1.1.1.0'),
1153 ipaddress.ip_network('1.1.0.0')))
1154 # test exception raised when first and last are not same version
1155 self.assertRaises(TypeError, list,
1156 summarize(ipaddress.ip_address('::'),
1157 ipaddress.ip_network('1.1.0.0')))
1158
1159 def testAddressComparison(self):
1160 self.assertTrue(ipaddress.ip_address('1.1.1.1') <=
1161 ipaddress.ip_address('1.1.1.1'))
1162 self.assertTrue(ipaddress.ip_address('1.1.1.1') <=
1163 ipaddress.ip_address('1.1.1.2'))
1164 self.assertTrue(ipaddress.ip_address('::1') <=
1165 ipaddress.ip_address('::1'))
1166 self.assertTrue(ipaddress.ip_address('::1') <=
1167 ipaddress.ip_address('::2'))
1168
Nick Coghlan3008ec02012-07-08 00:45:33 +10001169 def testInterfaceComparison(self):
1170 self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1171 ipaddress.ip_interface('1.1.1.1'))
1172 self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1173 ipaddress.ip_interface('1.1.1.2'))
1174 self.assertTrue(ipaddress.ip_interface('::1') <=
1175 ipaddress.ip_interface('::1'))
1176 self.assertTrue(ipaddress.ip_interface('::1') <=
1177 ipaddress.ip_interface('::2'))
1178
Nick Coghlandc9b2552012-05-20 21:01:57 +10001179 def testNetworkComparison(self):
1180 # ip1 and ip2 have the same network address
1181 ip1 = ipaddress.IPv4Network('1.1.1.0/24')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001182 ip2 = ipaddress.IPv4Network('1.1.1.0/32')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001183 ip3 = ipaddress.IPv4Network('1.1.2.0/24')
1184
1185 self.assertTrue(ip1 < ip3)
1186 self.assertTrue(ip3 > ip2)
1187
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001188 self.assertEqual(ip1.compare_networks(ip1), 0)
1189
1190 # if addresses are the same, sort by netmask
1191 self.assertEqual(ip1.compare_networks(ip2), -1)
1192 self.assertEqual(ip2.compare_networks(ip1), 1)
1193
Nick Coghlandc9b2552012-05-20 21:01:57 +10001194 self.assertEqual(ip1.compare_networks(ip3), -1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001195 self.assertEqual(ip3.compare_networks(ip1), 1)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001196 self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key())
1197
1198 ip1 = ipaddress.IPv6Network('2001:2000::/96')
1199 ip2 = ipaddress.IPv6Network('2001:2001::/96')
1200 ip3 = ipaddress.IPv6Network('2001:ffff:2000::/96')
1201
1202 self.assertTrue(ip1 < ip3)
1203 self.assertTrue(ip3 > ip2)
1204 self.assertEqual(ip1.compare_networks(ip3), -1)
1205 self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key())
1206
1207 # Test comparing different protocols.
1208 # Should always raise a TypeError.
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001209 self.assertRaises(TypeError,
1210 self.ipv4_network.compare_networks,
1211 self.ipv6_network)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001212 ipv6 = ipaddress.IPv6Interface('::/0')
1213 ipv4 = ipaddress.IPv4Interface('0.0.0.0/0')
1214 self.assertRaises(TypeError, ipv4.__lt__, ipv6)
1215 self.assertRaises(TypeError, ipv4.__gt__, ipv6)
1216 self.assertRaises(TypeError, ipv6.__lt__, ipv4)
1217 self.assertRaises(TypeError, ipv6.__gt__, ipv4)
1218
1219 # Regression test for issue 19.
1220 ip1 = ipaddress.ip_network('10.1.2.128/25')
1221 self.assertFalse(ip1 < ip1)
1222 self.assertFalse(ip1 > ip1)
1223 ip2 = ipaddress.ip_network('10.1.3.0/24')
1224 self.assertTrue(ip1 < ip2)
1225 self.assertFalse(ip2 < ip1)
1226 self.assertFalse(ip1 > ip2)
1227 self.assertTrue(ip2 > ip1)
1228 ip3 = ipaddress.ip_network('10.1.3.0/25')
1229 self.assertTrue(ip2 < ip3)
1230 self.assertFalse(ip3 < ip2)
1231 self.assertFalse(ip2 > ip3)
1232 self.assertTrue(ip3 > ip2)
1233
1234 # Regression test for issue 28.
1235 ip1 = ipaddress.ip_network('10.10.10.0/31')
1236 ip2 = ipaddress.ip_network('10.10.10.0')
1237 ip3 = ipaddress.ip_network('10.10.10.2/31')
1238 ip4 = ipaddress.ip_network('10.10.10.2')
1239 sorted = [ip1, ip2, ip3, ip4]
1240 unsorted = [ip2, ip4, ip1, ip3]
1241 unsorted.sort()
1242 self.assertEqual(sorted, unsorted)
1243 unsorted = [ip4, ip1, ip3, ip2]
1244 unsorted.sort()
1245 self.assertEqual(sorted, unsorted)
1246 self.assertRaises(TypeError, ip1.__lt__,
1247 ipaddress.ip_address('10.10.10.0'))
1248 self.assertRaises(TypeError, ip2.__lt__,
1249 ipaddress.ip_address('10.10.10.0'))
1250
1251 # <=, >=
1252 self.assertTrue(ipaddress.ip_network('1.1.1.1') <=
1253 ipaddress.ip_network('1.1.1.1'))
1254 self.assertTrue(ipaddress.ip_network('1.1.1.1') <=
1255 ipaddress.ip_network('1.1.1.2'))
1256 self.assertFalse(ipaddress.ip_network('1.1.1.2') <=
1257 ipaddress.ip_network('1.1.1.1'))
1258 self.assertTrue(ipaddress.ip_network('::1') <=
1259 ipaddress.ip_network('::1'))
1260 self.assertTrue(ipaddress.ip_network('::1') <=
1261 ipaddress.ip_network('::2'))
1262 self.assertFalse(ipaddress.ip_network('::2') <=
1263 ipaddress.ip_network('::1'))
1264
1265 def testStrictNetworks(self):
1266 self.assertRaises(ValueError, ipaddress.ip_network, '192.168.1.1/24')
1267 self.assertRaises(ValueError, ipaddress.ip_network, '::1/120')
1268
1269 def testOverlaps(self):
1270 other = ipaddress.IPv4Network('1.2.3.0/30')
1271 other2 = ipaddress.IPv4Network('1.2.2.0/24')
1272 other3 = ipaddress.IPv4Network('1.2.2.64/26')
1273 self.assertTrue(self.ipv4_network.overlaps(other))
1274 self.assertFalse(self.ipv4_network.overlaps(other2))
1275 self.assertTrue(other2.overlaps(other3))
1276
1277 def testEmbeddedIpv4(self):
1278 ipv4_string = '192.168.0.1'
1279 ipv4 = ipaddress.IPv4Interface(ipv4_string)
1280 v4compat_ipv6 = ipaddress.IPv6Interface('::%s' % ipv4_string)
1281 self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip))
1282 v4mapped_ipv6 = ipaddress.IPv6Interface('::ffff:%s' % ipv4_string)
1283 self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip)
1284 self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface,
1285 '2001:1.1.1.1:1.1.1.1')
1286
1287 # Issue 67: IPv6 with embedded IPv4 address not recognized.
1288 def testIPv6AddressTooLarge(self):
1289 # RFC4291 2.5.5.2
1290 self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1'),
1291 ipaddress.ip_address('::FFFF:c000:201'))
1292 # RFC4291 2.2 (part 3) x::d.d.d.d
1293 self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
1294 ipaddress.ip_address('FFFF::c000:201'))
1295
1296 def testIPVersion(self):
1297 self.assertEqual(self.ipv4_address.version, 4)
1298 self.assertEqual(self.ipv6_address.version, 6)
1299
1300 def testMaxPrefixLength(self):
1301 self.assertEqual(self.ipv4_interface.max_prefixlen, 32)
1302 self.assertEqual(self.ipv6_interface.max_prefixlen, 128)
1303
1304 def testPacked(self):
1305 self.assertEqual(self.ipv4_address.packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001306 b'\x01\x02\x03\x04')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001307 self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001308 b'\xff\xfe\xfd\xfc')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001309 self.assertEqual(self.ipv6_address.packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001310 b'\x20\x01\x06\x58\x02\x2a\xca\xfe'
1311 b'\x02\x00\x00\x00\x00\x00\x00\x01')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001312 self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001313 b'\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff'
1314 + b'\x00' * 6)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001315 self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001316 b'\x00' * 6 + b'\x00\x01' + b'\x00' * 8)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001317
Nick Coghlandc9b2552012-05-20 21:01:57 +10001318 def testIpType(self):
1319 ipv4net = ipaddress.ip_network('1.2.3.4')
1320 ipv4addr = ipaddress.ip_address('1.2.3.4')
1321 ipv6net = ipaddress.ip_network('::1.2.3.4')
1322 ipv6addr = ipaddress.ip_address('::1.2.3.4')
1323 self.assertEqual(ipaddress.IPv4Network, type(ipv4net))
1324 self.assertEqual(ipaddress.IPv4Address, type(ipv4addr))
1325 self.assertEqual(ipaddress.IPv6Network, type(ipv6net))
1326 self.assertEqual(ipaddress.IPv6Address, type(ipv6addr))
1327
1328 def testReservedIpv4(self):
1329 # test networks
1330 self.assertEqual(True, ipaddress.ip_interface(
1331 '224.1.1.1/31').is_multicast)
1332 self.assertEqual(False, ipaddress.ip_network('240.0.0.0').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001333 self.assertEqual(True, ipaddress.ip_network('240.0.0.0').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001334
1335 self.assertEqual(True, ipaddress.ip_interface(
1336 '192.168.1.1/17').is_private)
1337 self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private)
1338 self.assertEqual(True, ipaddress.ip_network(
1339 '10.255.255.255').is_private)
1340 self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_private)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001341 self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001342 self.assertEqual(True, ipaddress.ip_network(
1343 '172.31.255.255').is_private)
1344 self.assertEqual(False, ipaddress.ip_network('172.32.0.0').is_private)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001345 self.assertEqual(True,
1346 ipaddress.ip_network('169.254.1.0/24').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001347
1348 self.assertEqual(True,
1349 ipaddress.ip_interface(
1350 '169.254.100.200/24').is_link_local)
1351 self.assertEqual(False,
1352 ipaddress.ip_interface(
1353 '169.255.100.200/24').is_link_local)
1354
1355 self.assertEqual(True,
1356 ipaddress.ip_network(
1357 '127.100.200.254/32').is_loopback)
1358 self.assertEqual(True, ipaddress.ip_network(
1359 '127.42.0.0/16').is_loopback)
1360 self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback)
Peter Moodye5019d52013-10-24 09:47:10 -07001361 self.assertEqual(False,
1362 ipaddress.ip_network('100.64.0.0/10').is_private)
Peter Moodybe9c1b12013-10-22 12:36:21 -07001363 self.assertEqual(False, ipaddress.ip_network('100.64.0.0/10').is_global)
Peter Moodye5019d52013-10-24 09:47:10 -07001364
Peter Moody22c31762013-10-21 13:58:06 -07001365 self.assertEqual(True,
1366 ipaddress.ip_network('192.0.2.128/25').is_private)
1367 self.assertEqual(True,
1368 ipaddress.ip_network('192.0.3.0/24').is_global)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001369
1370 # test addresses
Hynek Schlawackbcd30442012-06-04 14:19:39 +02001371 self.assertEqual(True, ipaddress.ip_address('0.0.0.0').is_unspecified)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001372 self.assertEqual(True, ipaddress.ip_address('224.1.1.1').is_multicast)
1373 self.assertEqual(False, ipaddress.ip_address('240.0.0.0').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001374 self.assertEqual(True, ipaddress.ip_address('240.0.0.1').is_reserved)
1375 self.assertEqual(False,
1376 ipaddress.ip_address('239.255.255.255').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001377
1378 self.assertEqual(True, ipaddress.ip_address('192.168.1.1').is_private)
1379 self.assertEqual(False, ipaddress.ip_address('192.169.0.0').is_private)
1380 self.assertEqual(True, ipaddress.ip_address(
1381 '10.255.255.255').is_private)
1382 self.assertEqual(False, ipaddress.ip_address('11.0.0.0').is_private)
1383 self.assertEqual(True, ipaddress.ip_address(
1384 '172.31.255.255').is_private)
1385 self.assertEqual(False, ipaddress.ip_address('172.32.0.0').is_private)
1386
1387 self.assertEqual(True,
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001388 ipaddress.ip_address('169.254.100.200').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001389 self.assertEqual(False,
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001390 ipaddress.ip_address('169.255.100.200').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001391
1392 self.assertEqual(True,
1393 ipaddress.ip_address('127.100.200.254').is_loopback)
1394 self.assertEqual(True, ipaddress.ip_address('127.42.0.0').is_loopback)
1395 self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback)
1396 self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified)
1397
1398 def testReservedIpv6(self):
1399
1400 self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001401 self.assertEqual(True, ipaddress.ip_network(2**128 - 1).is_multicast)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001402 self.assertEqual(True, ipaddress.ip_network('ff00::').is_multicast)
1403 self.assertEqual(False, ipaddress.ip_network('fdff::').is_multicast)
1404
1405 self.assertEqual(True, ipaddress.ip_network('fecf::').is_site_local)
1406 self.assertEqual(True, ipaddress.ip_network(
1407 'feff:ffff:ffff:ffff::').is_site_local)
1408 self.assertEqual(False, ipaddress.ip_network(
1409 'fbf:ffff::').is_site_local)
1410 self.assertEqual(False, ipaddress.ip_network('ff00::').is_site_local)
1411
1412 self.assertEqual(True, ipaddress.ip_network('fc00::').is_private)
1413 self.assertEqual(True, ipaddress.ip_network(
1414 'fc00:ffff:ffff:ffff::').is_private)
1415 self.assertEqual(False, ipaddress.ip_network('fbff:ffff::').is_private)
1416 self.assertEqual(False, ipaddress.ip_network('fe00::').is_private)
1417
1418 self.assertEqual(True, ipaddress.ip_network('fea0::').is_link_local)
1419 self.assertEqual(True, ipaddress.ip_network(
1420 'febf:ffff::').is_link_local)
1421 self.assertEqual(False, ipaddress.ip_network(
1422 'fe7f:ffff::').is_link_local)
1423 self.assertEqual(False, ipaddress.ip_network('fec0::').is_link_local)
1424
1425 self.assertEqual(True, ipaddress.ip_interface('0:0::0:01').is_loopback)
1426 self.assertEqual(False, ipaddress.ip_interface('::1/127').is_loopback)
1427 self.assertEqual(False, ipaddress.ip_network('::').is_loopback)
1428 self.assertEqual(False, ipaddress.ip_network('::2').is_loopback)
1429
1430 self.assertEqual(True, ipaddress.ip_network('0::0').is_unspecified)
1431 self.assertEqual(False, ipaddress.ip_network('::1').is_unspecified)
1432 self.assertEqual(False, ipaddress.ip_network('::/127').is_unspecified)
1433
Peter Moody22c31762013-10-21 13:58:06 -07001434 self.assertEqual(True,
1435 ipaddress.ip_network('2001::1/128').is_private)
1436 self.assertEqual(True,
1437 ipaddress.ip_network('200::1/128').is_global)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001438 # test addresses
1439 self.assertEqual(True, ipaddress.ip_address('ffff::').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001440 self.assertEqual(True, ipaddress.ip_address(2**128 - 1).is_multicast)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001441 self.assertEqual(True, ipaddress.ip_address('ff00::').is_multicast)
1442 self.assertEqual(False, ipaddress.ip_address('fdff::').is_multicast)
1443
1444 self.assertEqual(True, ipaddress.ip_address('fecf::').is_site_local)
1445 self.assertEqual(True, ipaddress.ip_address(
1446 'feff:ffff:ffff:ffff::').is_site_local)
1447 self.assertEqual(False, ipaddress.ip_address(
1448 'fbf:ffff::').is_site_local)
1449 self.assertEqual(False, ipaddress.ip_address('ff00::').is_site_local)
1450
1451 self.assertEqual(True, ipaddress.ip_address('fc00::').is_private)
1452 self.assertEqual(True, ipaddress.ip_address(
1453 'fc00:ffff:ffff:ffff::').is_private)
1454 self.assertEqual(False, ipaddress.ip_address('fbff:ffff::').is_private)
1455 self.assertEqual(False, ipaddress.ip_address('fe00::').is_private)
1456
1457 self.assertEqual(True, ipaddress.ip_address('fea0::').is_link_local)
1458 self.assertEqual(True, ipaddress.ip_address(
1459 'febf:ffff::').is_link_local)
1460 self.assertEqual(False, ipaddress.ip_address(
1461 'fe7f:ffff::').is_link_local)
1462 self.assertEqual(False, ipaddress.ip_address('fec0::').is_link_local)
1463
1464 self.assertEqual(True, ipaddress.ip_address('0:0::0:01').is_loopback)
1465 self.assertEqual(True, ipaddress.ip_address('::1').is_loopback)
1466 self.assertEqual(False, ipaddress.ip_address('::2').is_loopback)
1467
1468 self.assertEqual(True, ipaddress.ip_address('0::0').is_unspecified)
1469 self.assertEqual(False, ipaddress.ip_address('::1').is_unspecified)
1470
1471 # some generic IETF reserved addresses
1472 self.assertEqual(True, ipaddress.ip_address('100::').is_reserved)
1473 self.assertEqual(True, ipaddress.ip_network('4000::1/128').is_reserved)
1474
1475 def testIpv4Mapped(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001476 self.assertEqual(
1477 ipaddress.ip_address('::ffff:192.168.1.1').ipv4_mapped,
1478 ipaddress.ip_address('192.168.1.1'))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001479 self.assertEqual(ipaddress.ip_address('::c0a8:101').ipv4_mapped, None)
1480 self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
1481 ipaddress.ip_address('192.168.1.1'))
1482
1483 def testAddrExclude(self):
1484 addr1 = ipaddress.ip_network('10.1.1.0/24')
1485 addr2 = ipaddress.ip_network('10.1.1.0/26')
1486 addr3 = ipaddress.ip_network('10.2.1.0/24')
1487 addr4 = ipaddress.ip_address('10.1.1.0')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001488 addr5 = ipaddress.ip_network('2001:db8::0/32')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001489 self.assertEqual(sorted(list(addr1.address_exclude(addr2))),
1490 [ipaddress.ip_network('10.1.1.64/26'),
1491 ipaddress.ip_network('10.1.1.128/25')])
1492 self.assertRaises(ValueError, list, addr1.address_exclude(addr3))
1493 self.assertRaises(TypeError, list, addr1.address_exclude(addr4))
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001494 self.assertRaises(TypeError, list, addr1.address_exclude(addr5))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001495 self.assertEqual(list(addr1.address_exclude(addr1)), [])
1496
1497 def testHash(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001498 self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')),
1499 hash(ipaddress.ip_interface('10.1.1.0/24')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001500 self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001501 hash(ipaddress.ip_network('10.1.1.0/24')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001502 self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001503 hash(ipaddress.ip_address('10.1.1.0')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001504 # i70
1505 self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001506 hash(ipaddress.ip_address(
Nick Coghlandc9b2552012-05-20 21:01:57 +10001507 int(ipaddress.ip_address('1.2.3.4')._ip))))
1508 ip1 = ipaddress.ip_address('10.1.1.0')
1509 ip2 = ipaddress.ip_address('1::')
1510 dummy = {}
1511 dummy[self.ipv4_address] = None
1512 dummy[self.ipv6_address] = None
1513 dummy[ip1] = None
1514 dummy[ip2] = None
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001515 self.assertIn(self.ipv4_address, dummy)
1516 self.assertIn(ip2, dummy)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001517
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001518 def testIPBases(self):
1519 net = self.ipv4_network
1520 self.assertEqual('1.2.3.0/24', net.compressed)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001521 net = self.ipv6_network
1522 self.assertRaises(ValueError, net._string_from_ip_int, 2**128 + 1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001523
Hynek Schlawack454a74d2012-06-04 18:14:02 +02001524 def testIPv6NetworkHelpers(self):
1525 net = self.ipv6_network
1526 self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen)
1527 self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::',
1528 net.with_netmask)
1529 self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff',
1530 net.with_hostmask)
1531 self.assertEqual('2001:658:22a:cafe::/64', str(net))
1532
1533 def testIPv4NetworkHelpers(self):
1534 net = self.ipv4_network
1535 self.assertEqual('1.2.3.0/24', net.with_prefixlen)
1536 self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask)
1537 self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask)
1538 self.assertEqual('1.2.3.0/24', str(net))
1539
Nick Coghlandc9b2552012-05-20 21:01:57 +10001540 def testCopyConstructor(self):
1541 addr1 = ipaddress.ip_network('10.1.1.0/24')
1542 addr2 = ipaddress.ip_network(addr1)
1543 addr3 = ipaddress.ip_interface('2001:658:22a:cafe:200::1/64')
1544 addr4 = ipaddress.ip_interface(addr3)
1545 addr5 = ipaddress.IPv4Address('1.1.1.1')
1546 addr6 = ipaddress.IPv6Address('2001:658:22a:cafe:200::1')
1547
1548 self.assertEqual(addr1, addr2)
1549 self.assertEqual(addr3, addr4)
1550 self.assertEqual(addr5, ipaddress.IPv4Address(addr5))
1551 self.assertEqual(addr6, ipaddress.IPv6Address(addr6))
1552
1553 def testCompressIPv6Address(self):
1554 test_addresses = {
1555 '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128',
1556 '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128',
1557 '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128',
1558 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
1559 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
1560 '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128',
1561 '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128',
1562 '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128',
1563 '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128',
1564 '0:0:0:0:0:0:0:0': '::/128',
1565 '0:0:0:0:0:0:0:0/0': '::/0',
1566 '0:0:0:0:0:0:0:1': '::1/128',
1567 '2001:0658:022a:cafe:0000:0000:0000:0000/66':
1568 '2001:658:22a:cafe::/66',
1569 '::1.2.3.4': '::102:304/128',
1570 '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128',
1571 '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128',
1572 '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
1573 '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
1574 '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
1575 }
1576 for uncompressed, compressed in list(test_addresses.items()):
1577 self.assertEqual(compressed, str(ipaddress.IPv6Interface(
1578 uncompressed)))
1579
1580 def testExplodeShortHandIpStr(self):
1581 addr1 = ipaddress.IPv6Interface('2001::1')
1582 addr2 = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
1583 addr3 = ipaddress.IPv6Network('2001::/96')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001584 addr4 = ipaddress.IPv4Address('192.168.178.1')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001585 self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128',
1586 addr1.exploded)
1587 self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128',
1588 ipaddress.IPv6Interface('::1/128').exploded)
1589 # issue 77
1590 self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
1591 addr2.exploded)
1592 self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0000/96',
1593 addr3.exploded)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001594 self.assertEqual('192.168.178.1', addr4.exploded)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001595
1596 def testIntRepresentation(self):
1597 self.assertEqual(16909060, int(self.ipv4_address))
1598 self.assertEqual(42540616829182469433547762482097946625,
1599 int(self.ipv6_address))
1600
Nick Coghlandc9b2552012-05-20 21:01:57 +10001601 def testForceVersion(self):
1602 self.assertEqual(ipaddress.ip_network(1).version, 4)
Nick Coghlan51c30672012-05-27 00:25:58 +10001603 self.assertEqual(ipaddress.IPv6Network(1).version, 6)
Sandro Tosi876ecad2012-05-23 22:26:55 +02001604
Nick Coghlandc9b2552012-05-20 21:01:57 +10001605 def testWithStar(self):
Nick Coghlana8517ad2012-08-20 10:04:26 +10001606 self.assertEqual(self.ipv4_interface.with_prefixlen, "1.2.3.4/24")
1607 self.assertEqual(self.ipv4_interface.with_netmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001608 "1.2.3.4/255.255.255.0")
Nick Coghlana8517ad2012-08-20 10:04:26 +10001609 self.assertEqual(self.ipv4_interface.with_hostmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001610 "1.2.3.4/0.0.0.255")
1611
Nick Coghlana8517ad2012-08-20 10:04:26 +10001612 self.assertEqual(self.ipv6_interface.with_prefixlen,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001613 '2001:658:22a:cafe:200::1/64')
Nick Coghlana8517ad2012-08-20 10:04:26 +10001614 self.assertEqual(self.ipv6_interface.with_netmask,
1615 '2001:658:22a:cafe:200::1/ffff:ffff:ffff:ffff::')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001616 # this probably don't make much sense, but it's included for
1617 # compatibility with ipv4
Nick Coghlana8517ad2012-08-20 10:04:26 +10001618 self.assertEqual(self.ipv6_interface.with_hostmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001619 '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff')
1620
1621 def testNetworkElementCaching(self):
1622 # V4 - make sure we're empty
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001623 self.assertNotIn('network_address', self.ipv4_network._cache)
1624 self.assertNotIn('broadcast_address', self.ipv4_network._cache)
1625 self.assertNotIn('hostmask', self.ipv4_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001626
1627 # V4 - populate and test
1628 self.assertEqual(self.ipv4_network.network_address,
1629 ipaddress.IPv4Address('1.2.3.0'))
1630 self.assertEqual(self.ipv4_network.broadcast_address,
1631 ipaddress.IPv4Address('1.2.3.255'))
1632 self.assertEqual(self.ipv4_network.hostmask,
1633 ipaddress.IPv4Address('0.0.0.255'))
1634
1635 # V4 - check we're cached
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001636 self.assertIn('broadcast_address', self.ipv4_network._cache)
1637 self.assertIn('hostmask', self.ipv4_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001638
1639 # V6 - make sure we're empty
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001640 self.assertNotIn('broadcast_address', self.ipv6_network._cache)
1641 self.assertNotIn('hostmask', self.ipv6_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001642
1643 # V6 - populate and test
1644 self.assertEqual(self.ipv6_network.network_address,
1645 ipaddress.IPv6Address('2001:658:22a:cafe::'))
1646 self.assertEqual(self.ipv6_interface.network.network_address,
1647 ipaddress.IPv6Address('2001:658:22a:cafe::'))
1648
1649 self.assertEqual(
1650 self.ipv6_network.broadcast_address,
1651 ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff'))
1652 self.assertEqual(self.ipv6_network.hostmask,
1653 ipaddress.IPv6Address('::ffff:ffff:ffff:ffff'))
1654 self.assertEqual(
1655 self.ipv6_interface.network.broadcast_address,
1656 ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff'))
1657 self.assertEqual(self.ipv6_interface.network.hostmask,
1658 ipaddress.IPv6Address('::ffff:ffff:ffff:ffff'))
1659
1660 # V6 - check we're cached
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001661 self.assertIn('broadcast_address', self.ipv6_network._cache)
1662 self.assertIn('hostmask', self.ipv6_network._cache)
1663 self.assertIn('broadcast_address', self.ipv6_interface.network._cache)
1664 self.assertIn('hostmask', self.ipv6_interface.network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001665
1666 def testTeredo(self):
1667 # stolen from wikipedia
1668 server = ipaddress.IPv4Address('65.54.227.120')
1669 client = ipaddress.IPv4Address('192.0.2.45')
1670 teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2'
1671 self.assertEqual((server, client),
1672 ipaddress.ip_address(teredo_addr).teredo)
1673 bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2'
1674 self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
1675 bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2'
1676 self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
1677
1678 # i77
1679 teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
1680 self.assertEqual((ipaddress.IPv4Address('94.245.121.253'),
1681 ipaddress.IPv4Address('95.26.244.94')),
1682 teredo_addr.teredo)
1683
Nick Coghlandc9b2552012-05-20 21:01:57 +10001684 def testsixtofour(self):
1685 sixtofouraddr = ipaddress.ip_address('2002:ac1d:2d64::1')
1686 bad_addr = ipaddress.ip_address('2000:ac1d:2d64::1')
1687 self.assertEqual(ipaddress.IPv4Address('172.29.45.100'),
1688 sixtofouraddr.sixtofour)
1689 self.assertFalse(bad_addr.sixtofour)
1690
Nick Coghlandc9b2552012-05-20 21:01:57 +10001691
1692if __name__ == '__main__':
1693 unittest.main()