blob: bfb569950f0d6e0fa6d45c53c4956d6cb2d5e4ac [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
Serhiy Storchakaf186e122015-01-26 10:11:16 +020010import functools
Nick Coghlane0c3f5e2012-08-05 18:20:17 +100011import operator
Nick Coghlandc9b2552012-05-20 21:01:57 +100012import ipaddress
13
R David Murray75678652014-10-12 15:17:22 -040014
Nick Coghlan07c4e332012-07-08 23:06:45 +100015class BaseTestCase(unittest.TestCase):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100016 # One big change in ipaddress over the original ipaddr module is
17 # error reporting that tries to assume users *don't know the rules*
18 # for what constitutes an RFC compliant IP address
19
Nick Coghlan07c4e332012-07-08 23:06:45 +100020 # Ensuring these errors are emitted correctly in all relevant cases
21 # meant moving to a more systematic test structure that allows the
22 # test structure to map more directly to the module structure
23
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100024 # Note that if the constructors are refactored so that addresses with
25 # multiple problems get classified differently, that's OK - just
26 # move the affected examples to the newly appropriate test case.
27
Nick Coghlan07c4e332012-07-08 23:06:45 +100028 # There is some duplication between the original relatively ad hoc
29 # test suite and the new systematic tests. While some redundancy in
30 # testing is considered preferable to accidentally deleting a valid
31 # test, the original test suite will likely be reduced over time as
32 # redundant tests are identified.
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100033
Nick Coghlan297b1432012-07-08 17:11:04 +100034 @property
35 def factory(self):
36 raise NotImplementedError
37
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100038 @contextlib.contextmanager
39 def assertCleanError(self, exc_type, details, *args):
40 """
41 Ensure exception does not display a context by default
42
43 Wraps unittest.TestCase.assertRaisesRegex
44 """
45 if args:
46 details = details % args
47 cm = self.assertRaisesRegex(exc_type, details)
48 with cm as exc:
49 yield exc
50 # Ensure we produce clean tracebacks on failure
51 if exc.exception.__context__ is not None:
52 self.assertTrue(exc.exception.__suppress_context__)
53
54 def assertAddressError(self, details, *args):
55 """Ensure a clean AddressValueError"""
56 return self.assertCleanError(ipaddress.AddressValueError,
R David Murray75678652014-10-12 15:17:22 -040057 details, *args)
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100058
59 def assertNetmaskError(self, details, *args):
60 """Ensure a clean NetmaskValueError"""
61 return self.assertCleanError(ipaddress.NetmaskValueError,
R David Murray75678652014-10-12 15:17:22 -040062 details, *args)
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100063
Nick Coghlan07c4e332012-07-08 23:06:45 +100064 def assertInstancesEqual(self, lhs, rhs):
65 """Check constructor arguments produce equivalent instances"""
66 self.assertEqual(self.factory(lhs), self.factory(rhs))
67
R David Murray75678652014-10-12 15:17:22 -040068
Nick Coghlan07c4e332012-07-08 23:06:45 +100069class CommonTestMixin:
Nick Coghlan36f8dcd2012-07-07 19:23:53 +100070
71 def test_empty_address(self):
72 with self.assertAddressError("Address cannot be empty"):
Nick Coghlan297b1432012-07-08 17:11:04 +100073 self.factory("")
74
75 def test_floats_rejected(self):
76 with self.assertAddressError(re.escape(repr("1.0"))):
77 self.factory(1.0)
78
Nick Coghlane0c3f5e2012-08-05 18:20:17 +100079 def test_not_an_index_issue15559(self):
80 # Implementing __index__ makes for a very nasty interaction with the
81 # bytes constructor. Thus, we disallow implicit use as an integer
82 self.assertRaises(TypeError, operator.index, self.factory(1))
83 self.assertRaises(TypeError, hex, self.factory(1))
84 self.assertRaises(TypeError, bytes, self.factory(1))
85
86
Nick Coghlan07c4e332012-07-08 23:06:45 +100087class CommonTestMixin_v4(CommonTestMixin):
88
89 def test_leading_zeros(self):
90 self.assertInstancesEqual("000.000.000.000", "0.0.0.0")
91 self.assertInstancesEqual("192.168.000.001", "192.168.0.1")
92
93 def test_int(self):
94 self.assertInstancesEqual(0, "0.0.0.0")
95 self.assertInstancesEqual(3232235521, "192.168.0.1")
96
97 def test_packed(self):
98 self.assertInstancesEqual(bytes.fromhex("00000000"), "0.0.0.0")
99 self.assertInstancesEqual(bytes.fromhex("c0a80001"), "192.168.0.1")
Nick Coghlan297b1432012-07-08 17:11:04 +1000100
101 def test_negative_ints_rejected(self):
102 msg = "-1 (< 0) is not permitted as an IPv4 address"
103 with self.assertAddressError(re.escape(msg)):
104 self.factory(-1)
105
106 def test_large_ints_rejected(self):
107 msg = "%d (>= 2**32) is not permitted as an IPv4 address"
108 with self.assertAddressError(re.escape(msg % 2**32)):
109 self.factory(2**32)
110
111 def test_bad_packed_length(self):
112 def assertBadLength(length):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000113 addr = bytes(length)
Nick Coghlan297b1432012-07-08 17:11:04 +1000114 msg = "%r (len %d != 4) is not permitted as an IPv4 address"
115 with self.assertAddressError(re.escape(msg % (addr, length))):
116 self.factory(addr)
117
118 assertBadLength(3)
119 assertBadLength(5)
120
R David Murray75678652014-10-12 15:17:22 -0400121
Nick Coghlan07c4e332012-07-08 23:06:45 +1000122class CommonTestMixin_v6(CommonTestMixin):
123
124 def test_leading_zeros(self):
125 self.assertInstancesEqual("0000::0000", "::")
126 self.assertInstancesEqual("000::c0a8:0001", "::c0a8:1")
127
128 def test_int(self):
129 self.assertInstancesEqual(0, "::")
130 self.assertInstancesEqual(3232235521, "::c0a8:1")
131
132 def test_packed(self):
133 addr = bytes(12) + bytes.fromhex("00000000")
134 self.assertInstancesEqual(addr, "::")
135 addr = bytes(12) + bytes.fromhex("c0a80001")
136 self.assertInstancesEqual(addr, "::c0a8:1")
137 addr = bytes.fromhex("c0a80001") + bytes(12)
138 self.assertInstancesEqual(addr, "c0a8:1::")
Nick Coghlan297b1432012-07-08 17:11:04 +1000139
140 def test_negative_ints_rejected(self):
141 msg = "-1 (< 0) is not permitted as an IPv6 address"
142 with self.assertAddressError(re.escape(msg)):
143 self.factory(-1)
144
145 def test_large_ints_rejected(self):
146 msg = "%d (>= 2**128) is not permitted as an IPv6 address"
147 with self.assertAddressError(re.escape(msg % 2**128)):
148 self.factory(2**128)
149
150 def test_bad_packed_length(self):
151 def assertBadLength(length):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000152 addr = bytes(length)
Nick Coghlan297b1432012-07-08 17:11:04 +1000153 msg = "%r (len %d != 16) is not permitted as an IPv6 address"
154 with self.assertAddressError(re.escape(msg % (addr, length))):
155 self.factory(addr)
156 self.factory(addr)
157
158 assertBadLength(15)
159 assertBadLength(17)
160
161
Nick Coghlan07c4e332012-07-08 23:06:45 +1000162class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4):
Nick Coghlan297b1432012-07-08 17:11:04 +1000163 factory = ipaddress.IPv4Address
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000164
165 def test_network_passed_as_address(self):
166 addr = "127.0.0.1/24"
167 with self.assertAddressError("Unexpected '/' in %r", addr):
168 ipaddress.IPv4Address(addr)
169
170 def test_bad_address_split(self):
171 def assertBadSplit(addr):
172 with self.assertAddressError("Expected 4 octets in %r", addr):
173 ipaddress.IPv4Address(addr)
174
175 assertBadSplit("127.0.1")
176 assertBadSplit("42.42.42.42.42")
177 assertBadSplit("42.42.42")
178 assertBadSplit("42.42")
179 assertBadSplit("42")
180 assertBadSplit("42..42.42.42")
181 assertBadSplit("42.42.42.42.")
182 assertBadSplit("42.42.42.42...")
183 assertBadSplit(".42.42.42.42")
184 assertBadSplit("...42.42.42.42")
185 assertBadSplit("016.016.016")
186 assertBadSplit("016.016")
187 assertBadSplit("016")
188 assertBadSplit("000")
189 assertBadSplit("0x0a.0x0a.0x0a")
190 assertBadSplit("0x0a.0x0a")
191 assertBadSplit("0x0a")
192 assertBadSplit(".")
193 assertBadSplit("bogus")
194 assertBadSplit("bogus.com")
195 assertBadSplit("1000")
196 assertBadSplit("1000000000000000")
197 assertBadSplit("192.168.0.1.com")
198
199 def test_empty_octet(self):
200 def assertBadOctet(addr):
201 with self.assertAddressError("Empty octet not permitted in %r",
R David Murray75678652014-10-12 15:17:22 -0400202 addr):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000203 ipaddress.IPv4Address(addr)
204
205 assertBadOctet("42..42.42")
206 assertBadOctet("...")
207
208 def test_invalid_characters(self):
209 def assertBadOctet(addr, octet):
210 msg = "Only decimal digits permitted in %r in %r" % (octet, addr)
211 with self.assertAddressError(re.escape(msg)):
212 ipaddress.IPv4Address(addr)
213
214 assertBadOctet("0x0a.0x0a.0x0a.0x0a", "0x0a")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000215 assertBadOctet("0xa.0x0a.0x0a.0x0a", "0xa")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000216 assertBadOctet("42.42.42.-0", "-0")
217 assertBadOctet("42.42.42.+0", "+0")
218 assertBadOctet("42.42.42.-42", "-42")
219 assertBadOctet("+1.+2.+3.4", "+1")
220 assertBadOctet("1.2.3.4e0", "4e0")
221 assertBadOctet("1.2.3.4::", "4::")
222 assertBadOctet("1.a.2.3", "a")
223
Nick Coghlan07c4e332012-07-08 23:06:45 +1000224 def test_octal_decimal_ambiguity(self):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000225 def assertBadOctet(addr, octet):
Nick Coghlan07c4e332012-07-08 23:06:45 +1000226 msg = "Ambiguous (octal/decimal) value in %r not permitted in %r"
227 with self.assertAddressError(re.escape(msg % (octet, addr))):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000228 ipaddress.IPv4Address(addr)
229
230 assertBadOctet("016.016.016.016", "016")
231 assertBadOctet("001.000.008.016", "008")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000232
233 def test_octet_length(self):
234 def assertBadOctet(addr, octet):
235 msg = "At most 3 characters permitted in %r in %r"
236 with self.assertAddressError(re.escape(msg % (octet, addr))):
237 ipaddress.IPv4Address(addr)
238
239 assertBadOctet("0000.000.000.000", "0000")
240 assertBadOctet("12345.67899.-54321.-98765", "12345")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000241
242 def test_octet_limit(self):
243 def assertBadOctet(addr, octet):
Nick Coghlanb582ecc2012-07-07 22:15:22 +1000244 msg = "Octet %d (> 255) not permitted in %r" % (octet, addr)
245 with self.assertAddressError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000246 ipaddress.IPv4Address(addr)
247
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000248 assertBadOctet("257.0.0.0", 257)
Nick Coghlan07c4e332012-07-08 23:06:45 +1000249 assertBadOctet("192.168.0.999", 999)
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000250
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000251
Nick Coghlan07c4e332012-07-08 23:06:45 +1000252class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6):
Nick Coghlan297b1432012-07-08 17:11:04 +1000253 factory = ipaddress.IPv6Address
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000254
255 def test_network_passed_as_address(self):
256 addr = "::1/24"
257 with self.assertAddressError("Unexpected '/' in %r", addr):
258 ipaddress.IPv6Address(addr)
259
260 def test_bad_address_split_v6_not_enough_parts(self):
261 def assertBadSplit(addr):
262 msg = "At least 3 parts expected in %r"
263 with self.assertAddressError(msg, addr):
264 ipaddress.IPv6Address(addr)
265
266 assertBadSplit(":")
267 assertBadSplit(":1")
268 assertBadSplit("FEDC:9878")
269
270 def test_bad_address_split_v6_too_many_colons(self):
271 def assertBadSplit(addr):
272 msg = "At most 8 colons permitted in %r"
273 with self.assertAddressError(msg, addr):
274 ipaddress.IPv6Address(addr)
275
276 assertBadSplit("9:8:7:6:5:4:3::2:1")
277 assertBadSplit("10:9:8:7:6:5:4:3:2:1")
278 assertBadSplit("::8:7:6:5:4:3:2:1")
279 assertBadSplit("8:7:6:5:4:3:2:1::")
280 # A trailing IPv4 address is two parts
281 assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42")
282
283 def test_bad_address_split_v6_too_many_parts(self):
284 def assertBadSplit(addr):
285 msg = "Exactly 8 parts expected without '::' in %r"
286 with self.assertAddressError(msg, addr):
287 ipaddress.IPv6Address(addr)
288
289 assertBadSplit("3ffe:0:0:0:0:0:0:0:1")
290 assertBadSplit("9:8:7:6:5:4:3:2:1")
291 assertBadSplit("7:6:5:4:3:2:1")
292 # A trailing IPv4 address is two parts
293 assertBadSplit("9:8:7:6:5:4:3:42.42.42.42")
294 assertBadSplit("7:6:5:4:3:42.42.42.42")
295
296 def test_bad_address_split_v6_too_many_parts_with_double_colon(self):
297 def assertBadSplit(addr):
298 msg = "Expected at most 7 other parts with '::' in %r"
299 with self.assertAddressError(msg, addr):
300 ipaddress.IPv6Address(addr)
301
302 assertBadSplit("1:2:3:4::5:6:7:8")
303
304 def test_bad_address_split_v6_repeated_double_colon(self):
305 def assertBadSplit(addr):
306 msg = "At most one '::' permitted in %r"
307 with self.assertAddressError(msg, addr):
308 ipaddress.IPv6Address(addr)
309
310 assertBadSplit("3ffe::1::1")
311 assertBadSplit("1::2::3::4:5")
312 assertBadSplit("2001::db:::1")
313 assertBadSplit("3ffe::1::")
314 assertBadSplit("::3ffe::1")
315 assertBadSplit(":3ffe::1::1")
316 assertBadSplit("3ffe::1::1:")
317 assertBadSplit(":3ffe::1::1:")
318 assertBadSplit(":::")
319 assertBadSplit('2001:db8:::1')
320
321 def test_bad_address_split_v6_leading_colon(self):
322 def assertBadSplit(addr):
323 msg = "Leading ':' only permitted as part of '::' in %r"
324 with self.assertAddressError(msg, addr):
325 ipaddress.IPv6Address(addr)
326
327 assertBadSplit(":2001:db8::1")
328 assertBadSplit(":1:2:3:4:5:6:7")
329 assertBadSplit(":1:2:3:4:5:6:")
330 assertBadSplit(":6:5:4:3:2:1::")
331
332 def test_bad_address_split_v6_trailing_colon(self):
333 def assertBadSplit(addr):
334 msg = "Trailing ':' only permitted as part of '::' in %r"
335 with self.assertAddressError(msg, addr):
336 ipaddress.IPv6Address(addr)
337
338 assertBadSplit("2001:db8::1:")
339 assertBadSplit("1:2:3:4:5:6:7:")
340 assertBadSplit("::1.2.3.4:")
341 assertBadSplit("::7:6:5:4:3:2:")
342
343 def test_bad_v4_part_in(self):
344 def assertBadAddressPart(addr, v4_error):
345 with self.assertAddressError("%s in %r", v4_error, addr):
346 ipaddress.IPv6Address(addr)
347
348 assertBadAddressPart("3ffe::1.net", "Expected 4 octets in '1.net'")
349 assertBadAddressPart("3ffe::127.0.1",
350 "Expected 4 octets in '127.0.1'")
351 assertBadAddressPart("::1.2.3",
352 "Expected 4 octets in '1.2.3'")
353 assertBadAddressPart("::1.2.3.4.5",
354 "Expected 4 octets in '1.2.3.4.5'")
355 assertBadAddressPart("3ffe::1.1.1.net",
356 "Only decimal digits permitted in 'net' "
357 "in '1.1.1.net'")
358
359 def test_invalid_characters(self):
360 def assertBadPart(addr, part):
361 msg = "Only hex digits permitted in %r in %r" % (part, addr)
362 with self.assertAddressError(re.escape(msg)):
363 ipaddress.IPv6Address(addr)
364
365 assertBadPart("3ffe::goog", "goog")
366 assertBadPart("3ffe::-0", "-0")
367 assertBadPart("3ffe::+0", "+0")
368 assertBadPart("3ffe::-1", "-1")
369 assertBadPart("1.2.3.4::", "1.2.3.4")
370 assertBadPart('1234:axy::b', "axy")
371
372 def test_part_length(self):
373 def assertBadPart(addr, part):
374 msg = "At most 4 characters permitted in %r in %r"
375 with self.assertAddressError(msg, part, addr):
376 ipaddress.IPv6Address(addr)
377
Nick Coghlan07c4e332012-07-08 23:06:45 +1000378 assertBadPart("::00000", "00000")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000379 assertBadPart("3ffe::10000", "10000")
380 assertBadPart("02001:db8::", "02001")
381 assertBadPart('2001:888888::1', "888888")
382
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000383
Nick Coghlan07c4e332012-07-08 23:06:45 +1000384class NetmaskTestMixin_v4(CommonTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000385 """Input validation on interfaces and networks is very similar"""
386
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000387 def test_split_netmask(self):
388 addr = "1.2.3.4/32/24"
389 with self.assertAddressError("Only one '/' permitted in %r" % addr):
390 self.factory(addr)
391
392 def test_address_errors(self):
393 def assertBadAddress(addr, details):
394 with self.assertAddressError(details):
395 self.factory(addr)
396
Nick Coghlan297b1432012-07-08 17:11:04 +1000397 assertBadAddress("/", "Address cannot be empty")
398 assertBadAddress("/8", "Address cannot be empty")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000399 assertBadAddress("bogus", "Expected 4 octets")
400 assertBadAddress("google.com", "Expected 4 octets")
401 assertBadAddress("10/8", "Expected 4 octets")
402 assertBadAddress("::1.2.3.4", "Only decimal digits")
Nick Coghlanb582ecc2012-07-07 22:15:22 +1000403 assertBadAddress("1.2.3.256", re.escape("256 (> 255)"))
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000404
Nick Coghlan932346f2014-02-08 23:17:36 +1000405 def test_valid_netmask(self):
406 self.assertEqual(str(self.factory('192.0.2.0/255.255.255.0')),
407 '192.0.2.0/24')
408 for i in range(0, 33):
409 # Generate and re-parse the CIDR format (trivial).
410 net_str = '0.0.0.0/%d' % i
411 net = self.factory(net_str)
412 self.assertEqual(str(net), net_str)
413 # Generate and re-parse the expanded netmask.
414 self.assertEqual(
415 str(self.factory('0.0.0.0/%s' % net.netmask)), net_str)
416 # Zero prefix is treated as decimal.
417 self.assertEqual(str(self.factory('0.0.0.0/0%d' % i)), net_str)
418 # Generate and re-parse the expanded hostmask. The ambiguous
419 # cases (/0 and /32) are treated as netmasks.
420 if i in (32, 0):
421 net_str = '0.0.0.0/%d' % (32 - i)
422 self.assertEqual(
423 str(self.factory('0.0.0.0/%s' % net.hostmask)), net_str)
424
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000425 def test_netmask_errors(self):
426 def assertBadNetmask(addr, netmask):
Nick Coghlan932346f2014-02-08 23:17:36 +1000427 msg = "%r is not a valid netmask" % netmask
428 with self.assertNetmaskError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000429 self.factory("%s/%s" % (addr, netmask))
430
431 assertBadNetmask("1.2.3.4", "")
Nick Coghlan932346f2014-02-08 23:17:36 +1000432 assertBadNetmask("1.2.3.4", "-1")
433 assertBadNetmask("1.2.3.4", "+1")
434 assertBadNetmask("1.2.3.4", " 1 ")
435 assertBadNetmask("1.2.3.4", "0x1")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000436 assertBadNetmask("1.2.3.4", "33")
437 assertBadNetmask("1.2.3.4", "254.254.255.256")
Nick Coghlan932346f2014-02-08 23:17:36 +1000438 assertBadNetmask("1.2.3.4", "1.a.2.3")
Nick Coghlan07c4e332012-07-08 23:06:45 +1000439 assertBadNetmask("1.1.1.1", "254.xyz.2.3")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000440 assertBadNetmask("1.1.1.1", "240.255.0.0")
Nick Coghlan932346f2014-02-08 23:17:36 +1000441 assertBadNetmask("1.1.1.1", "255.254.128.0")
442 assertBadNetmask("1.1.1.1", "0.1.127.255")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000443 assertBadNetmask("1.1.1.1", "pudding")
Nick Coghlan932346f2014-02-08 23:17:36 +1000444 assertBadNetmask("1.1.1.1", "::")
445
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000446
Nick Coghlan07c4e332012-07-08 23:06:45 +1000447class InterfaceTestCase_v4(BaseTestCase, NetmaskTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000448 factory = ipaddress.IPv4Interface
449
R David Murray75678652014-10-12 15:17:22 -0400450
Nick Coghlan07c4e332012-07-08 23:06:45 +1000451class NetworkTestCase_v4(BaseTestCase, NetmaskTestMixin_v4):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000452 factory = ipaddress.IPv4Network
453
454
Nick Coghlan07c4e332012-07-08 23:06:45 +1000455class NetmaskTestMixin_v6(CommonTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000456 """Input validation on interfaces and networks is very similar"""
457
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000458 def test_split_netmask(self):
459 addr = "cafe:cafe::/128/190"
460 with self.assertAddressError("Only one '/' permitted in %r" % addr):
461 self.factory(addr)
462
463 def test_address_errors(self):
464 def assertBadAddress(addr, details):
465 with self.assertAddressError(details):
466 self.factory(addr)
467
Nick Coghlan297b1432012-07-08 17:11:04 +1000468 assertBadAddress("/", "Address cannot be empty")
469 assertBadAddress("/8", "Address cannot be empty")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000470 assertBadAddress("google.com", "At least 3 parts")
471 assertBadAddress("1.2.3.4", "At least 3 parts")
472 assertBadAddress("10/8", "At least 3 parts")
473 assertBadAddress("1234:axy::b", "Only hex digits")
474
Nick Coghlan932346f2014-02-08 23:17:36 +1000475 def test_valid_netmask(self):
476 # We only support CIDR for IPv6, because expanded netmasks are not
477 # standard notation.
478 self.assertEqual(str(self.factory('2001:db8::/32')), '2001:db8::/32')
479 for i in range(0, 129):
480 # Generate and re-parse the CIDR format (trivial).
481 net_str = '::/%d' % i
482 self.assertEqual(str(self.factory(net_str)), net_str)
483 # Zero prefix is treated as decimal.
484 self.assertEqual(str(self.factory('::/0%d' % i)), net_str)
485
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000486 def test_netmask_errors(self):
487 def assertBadNetmask(addr, netmask):
Nick Coghlan932346f2014-02-08 23:17:36 +1000488 msg = "%r is not a valid netmask" % netmask
489 with self.assertNetmaskError(re.escape(msg)):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000490 self.factory("%s/%s" % (addr, netmask))
491
492 assertBadNetmask("::1", "")
493 assertBadNetmask("::1", "::1")
494 assertBadNetmask("::1", "1::")
Nick Coghlan932346f2014-02-08 23:17:36 +1000495 assertBadNetmask("::1", "-1")
496 assertBadNetmask("::1", "+1")
497 assertBadNetmask("::1", " 1 ")
498 assertBadNetmask("::1", "0x1")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000499 assertBadNetmask("::1", "129")
Nick Coghlan932346f2014-02-08 23:17:36 +1000500 assertBadNetmask("::1", "1.2.3.4")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000501 assertBadNetmask("::1", "pudding")
Nick Coghlan932346f2014-02-08 23:17:36 +1000502 assertBadNetmask("::", "::")
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000503
R David Murray75678652014-10-12 15:17:22 -0400504
Nick Coghlan07c4e332012-07-08 23:06:45 +1000505class InterfaceTestCase_v6(BaseTestCase, NetmaskTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000506 factory = ipaddress.IPv6Interface
507
R David Murray75678652014-10-12 15:17:22 -0400508
Nick Coghlan07c4e332012-07-08 23:06:45 +1000509class NetworkTestCase_v6(BaseTestCase, NetmaskTestMixin_v6):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000510 factory = ipaddress.IPv6Network
511
512
Nick Coghlan07c4e332012-07-08 23:06:45 +1000513class FactoryFunctionErrors(BaseTestCase):
Nick Coghlan36f8dcd2012-07-07 19:23:53 +1000514
515 def assertFactoryError(self, factory, kind):
516 """Ensure a clean ValueError with the expected message"""
517 addr = "camelot"
518 msg = '%r does not appear to be an IPv4 or IPv6 %s'
519 with self.assertCleanError(ValueError, msg, addr, kind):
520 factory(addr)
521
522 def test_ip_address(self):
523 self.assertFactoryError(ipaddress.ip_address, "address")
524
525 def test_ip_interface(self):
526 self.assertFactoryError(ipaddress.ip_interface, "interface")
527
528 def test_ip_network(self):
529 self.assertFactoryError(ipaddress.ip_network, "network")
530
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200531
Serhiy Storchakaf186e122015-01-26 10:11:16 +0200532@functools.total_ordering
533class LargestObject:
534 def __eq__(self, other):
535 return isinstance(other, LargestObject)
536 def __lt__(self, other):
537 return False
538
539@functools.total_ordering
540class SmallestObject:
541 def __eq__(self, other):
542 return isinstance(other, SmallestObject)
543 def __gt__(self, other):
544 return False
545
Nick Coghlan3008ec02012-07-08 00:45:33 +1000546class ComparisonTests(unittest.TestCase):
547
548 v4addr = ipaddress.IPv4Address(1)
549 v4net = ipaddress.IPv4Network(1)
550 v4intf = ipaddress.IPv4Interface(1)
551 v6addr = ipaddress.IPv6Address(1)
552 v6net = ipaddress.IPv6Network(1)
553 v6intf = ipaddress.IPv6Interface(1)
554
555 v4_addresses = [v4addr, v4intf]
556 v4_objects = v4_addresses + [v4net]
557 v6_addresses = [v6addr, v6intf]
558 v6_objects = v6_addresses + [v6net]
559 objects = v4_objects + v6_objects
560
561 def test_foreign_type_equality(self):
562 # __eq__ should never raise TypeError directly
563 other = object()
564 for obj in self.objects:
565 self.assertNotEqual(obj, other)
566 self.assertFalse(obj == other)
567 self.assertEqual(obj.__eq__(other), NotImplemented)
568 self.assertEqual(obj.__ne__(other), NotImplemented)
569
570 def test_mixed_type_equality(self):
571 # Ensure none of the internal objects accidentally
572 # expose the right set of attributes to become "equal"
573 for lhs in self.objects:
574 for rhs in self.objects:
575 if lhs is rhs:
576 continue
577 self.assertNotEqual(lhs, rhs)
578
579 def test_containment(self):
580 for obj in self.v4_addresses:
581 self.assertIn(obj, self.v4net)
582 for obj in self.v6_addresses:
583 self.assertIn(obj, self.v6net)
584 for obj in self.v4_objects + [self.v6net]:
585 self.assertNotIn(obj, self.v6net)
586 for obj in self.v6_objects + [self.v4net]:
587 self.assertNotIn(obj, self.v4net)
588
589 def test_mixed_type_ordering(self):
590 for lhs in self.objects:
591 for rhs in self.objects:
592 if isinstance(lhs, type(rhs)) or isinstance(rhs, type(lhs)):
593 continue
594 self.assertRaises(TypeError, lambda: lhs < rhs)
595 self.assertRaises(TypeError, lambda: lhs > rhs)
596 self.assertRaises(TypeError, lambda: lhs <= rhs)
597 self.assertRaises(TypeError, lambda: lhs >= rhs)
598
Serhiy Storchakaf186e122015-01-26 10:11:16 +0200599 def test_foreign_type_ordering(self):
600 other = object()
601 smallest = SmallestObject()
602 largest = LargestObject()
603 for obj in self.objects:
604 with self.assertRaises(TypeError):
605 obj < other
606 with self.assertRaises(TypeError):
607 obj > other
608 with self.assertRaises(TypeError):
609 obj <= other
610 with self.assertRaises(TypeError):
611 obj >= other
612 self.assertTrue(obj < largest)
613 self.assertFalse(obj > largest)
614 self.assertTrue(obj <= largest)
615 self.assertFalse(obj >= largest)
616 self.assertFalse(obj < smallest)
617 self.assertTrue(obj > smallest)
618 self.assertFalse(obj <= smallest)
619 self.assertTrue(obj >= smallest)
620
Nick Coghlan3008ec02012-07-08 00:45:33 +1000621 def test_mixed_type_key(self):
622 # with get_mixed_type_key, you can sort addresses and network.
623 v4_ordered = [self.v4addr, self.v4net, self.v4intf]
624 v6_ordered = [self.v6addr, self.v6net, self.v6intf]
625 self.assertEqual(v4_ordered,
626 sorted(self.v4_objects,
627 key=ipaddress.get_mixed_type_key))
628 self.assertEqual(v6_ordered,
629 sorted(self.v6_objects,
630 key=ipaddress.get_mixed_type_key))
631 self.assertEqual(v4_ordered + v6_ordered,
632 sorted(self.objects,
633 key=ipaddress.get_mixed_type_key))
634 self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object))
635
636 def test_incompatible_versions(self):
637 # These should always raise TypeError
638 v4addr = ipaddress.ip_address('1.1.1.1')
639 v4net = ipaddress.ip_network('1.1.1.1')
640 v6addr = ipaddress.ip_address('::1')
Serhiy Storchakaf186e122015-01-26 10:11:16 +0200641 v6net = ipaddress.ip_network('::1')
Nick Coghlan3008ec02012-07-08 00:45:33 +1000642
643 self.assertRaises(TypeError, v4addr.__lt__, v6addr)
644 self.assertRaises(TypeError, v4addr.__gt__, v6addr)
645 self.assertRaises(TypeError, v4net.__lt__, v6net)
646 self.assertRaises(TypeError, v4net.__gt__, v6net)
647
648 self.assertRaises(TypeError, v6addr.__lt__, v4addr)
649 self.assertRaises(TypeError, v6addr.__gt__, v4addr)
650 self.assertRaises(TypeError, v6net.__lt__, v4net)
651 self.assertRaises(TypeError, v6net.__gt__, v4net)
652
653
Nick Coghlandc9b2552012-05-20 21:01:57 +1000654class IpaddrUnitTest(unittest.TestCase):
655
656 def setUp(self):
657 self.ipv4_address = ipaddress.IPv4Address('1.2.3.4')
658 self.ipv4_interface = ipaddress.IPv4Interface('1.2.3.4/24')
659 self.ipv4_network = ipaddress.IPv4Network('1.2.3.0/24')
660 #self.ipv4_hostmask = ipaddress.IPv4Interface('10.0.0.1/0.255.255.255')
661 self.ipv6_address = ipaddress.IPv6Interface(
662 '2001:658:22a:cafe:200:0:0:1')
663 self.ipv6_interface = ipaddress.IPv6Interface(
664 '2001:658:22a:cafe:200:0:0:1/64')
665 self.ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/64')
666
667 def testRepr(self):
668 self.assertEqual("IPv4Interface('1.2.3.4/32')",
669 repr(ipaddress.IPv4Interface('1.2.3.4')))
670 self.assertEqual("IPv6Interface('::1/128')",
671 repr(ipaddress.IPv6Interface('::1')))
672
673 # issue57
674 def testAddressIntMath(self):
675 self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255,
676 ipaddress.IPv4Address('1.1.2.0'))
677 self.assertEqual(ipaddress.IPv4Address('1.1.1.1') - 256,
678 ipaddress.IPv4Address('1.1.0.1'))
679 self.assertEqual(ipaddress.IPv6Address('::1') + (2**16 - 2),
680 ipaddress.IPv6Address('::ffff'))
681 self.assertEqual(ipaddress.IPv6Address('::ffff') - (2**16 - 2),
682 ipaddress.IPv6Address('::1'))
683
Nick Coghlan3c2570c2012-07-07 01:13:55 +1000684 def testInvalidIntToBytes(self):
685 self.assertRaises(ValueError, ipaddress.v4_int_to_packed, -1)
686 self.assertRaises(ValueError, ipaddress.v4_int_to_packed,
687 2 ** ipaddress.IPV4LENGTH)
688 self.assertRaises(ValueError, ipaddress.v6_int_to_packed, -1)
689 self.assertRaises(ValueError, ipaddress.v6_int_to_packed,
690 2 ** ipaddress.IPV6LENGTH)
691
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200692 def testInternals(self):
693 first, last = ipaddress._find_address_range([
694 ipaddress.IPv4Address('10.10.10.10'),
695 ipaddress.IPv4Address('10.10.10.12')])
696 self.assertEqual(first, last)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200697 self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128))
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200698 self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000699
Nick Coghland9722652012-06-17 16:33:00 +1000700 def testMissingAddressVersion(self):
701 class Broken(ipaddress._BaseAddress):
702 pass
703 broken = Broken('127.0.0.1')
704 with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
705 broken.version
706
707 def testMissingNetworkVersion(self):
708 class Broken(ipaddress._BaseNetwork):
709 pass
710 broken = Broken('127.0.0.1')
711 with self.assertRaisesRegex(NotImplementedError, "Broken.*version"):
712 broken.version
713
714 def testMissingAddressClass(self):
715 class Broken(ipaddress._BaseNetwork):
716 pass
717 broken = Broken('127.0.0.1')
718 with self.assertRaisesRegex(NotImplementedError, "Broken.*address"):
719 broken._address_class
720
Nick Coghlandc9b2552012-05-20 21:01:57 +1000721 def testGetNetwork(self):
722 self.assertEqual(int(self.ipv4_network.network_address), 16909056)
723 self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0')
724
725 self.assertEqual(int(self.ipv6_network.network_address),
726 42540616829182469433403647294022090752)
727 self.assertEqual(str(self.ipv6_network.network_address),
728 '2001:658:22a:cafe::')
729 self.assertEqual(str(self.ipv6_network.hostmask),
730 '::ffff:ffff:ffff:ffff')
731
Nick Coghlandc9b2552012-05-20 21:01:57 +1000732 def testIpFromInt(self):
733 self.assertEqual(self.ipv4_interface._ip,
734 ipaddress.IPv4Interface(16909060)._ip)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000735
736 ipv4 = ipaddress.ip_network('1.2.3.4')
737 ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1')
Nick Coghlan730f67f2012-08-05 22:02:18 +1000738 self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4.network_address)))
739 self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6.network_address)))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000740
741 v6_int = 42540616829182469433547762482097946625
742 self.assertEqual(self.ipv6_interface._ip,
743 ipaddress.IPv6Interface(v6_int)._ip)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000744
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200745 self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version,
746 4)
747 self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version,
748 6)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000749
750 def testIpFromPacked(self):
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000751 address = ipaddress.ip_address
Nick Coghlandc9b2552012-05-20 21:01:57 +1000752 self.assertEqual(self.ipv4_interface._ip,
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000753 ipaddress.ip_interface(b'\x01\x02\x03\x04')._ip)
754 self.assertEqual(address('255.254.253.252'),
755 address(b'\xff\xfe\xfd\xfc'))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000756 self.assertEqual(self.ipv6_interface.ip,
757 ipaddress.ip_interface(
Nick Coghlan5cf896f2012-07-07 01:43:31 +1000758 b'\x20\x01\x06\x58\x02\x2a\xca\xfe'
759 b'\x02\x00\x00\x00\x00\x00\x00\x01').ip)
760 self.assertEqual(address('ffff:2:3:4:ffff::'),
761 address(b'\xff\xff\x00\x02\x00\x03\x00\x04' +
762 b'\xff\xff' + b'\x00' * 6))
763 self.assertEqual(address('::'),
764 address(b'\x00' * 16))
765
Nick Coghlandc9b2552012-05-20 21:01:57 +1000766 def testGetIp(self):
767 self.assertEqual(int(self.ipv4_interface.ip), 16909060)
768 self.assertEqual(str(self.ipv4_interface.ip), '1.2.3.4')
769
770 self.assertEqual(int(self.ipv6_interface.ip),
771 42540616829182469433547762482097946625)
772 self.assertEqual(str(self.ipv6_interface.ip),
773 '2001:658:22a:cafe:200::1')
774
775 def testGetNetmask(self):
776 self.assertEqual(int(self.ipv4_network.netmask), 4294967040)
777 self.assertEqual(str(self.ipv4_network.netmask), '255.255.255.0')
778 self.assertEqual(int(self.ipv6_network.netmask),
779 340282366920938463444927863358058659840)
780 self.assertEqual(self.ipv6_network.prefixlen, 64)
781
782 def testZeroNetmask(self):
783 ipv4_zero_netmask = ipaddress.IPv4Interface('1.2.3.4/0')
784 self.assertEqual(int(ipv4_zero_netmask.network.netmask), 0)
Nick Coghlan932346f2014-02-08 23:17:36 +1000785 self.assertEqual(ipv4_zero_netmask._prefix_from_prefix_string('0'), 0)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200786 self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0'))
787 self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0.0.0.0'))
788 self.assertFalse(ipv4_zero_netmask._is_valid_netmask('invalid'))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000789
790 ipv6_zero_netmask = ipaddress.IPv6Interface('::1/0')
791 self.assertEqual(int(ipv6_zero_netmask.network.netmask), 0)
Nick Coghlan932346f2014-02-08 23:17:36 +1000792 self.assertEqual(ipv6_zero_netmask._prefix_from_prefix_string('0'), 0)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000793
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200794 def testIPv4NetAndHostmasks(self):
795 net = self.ipv4_network
796 self.assertFalse(net._is_valid_netmask('invalid'))
797 self.assertTrue(net._is_valid_netmask('128.128.128.128'))
798 self.assertFalse(net._is_valid_netmask('128.128.128.127'))
799 self.assertFalse(net._is_valid_netmask('128.128.128.255'))
800 self.assertTrue(net._is_valid_netmask('255.128.128.128'))
801
802 self.assertFalse(net._is_hostmask('invalid'))
803 self.assertTrue(net._is_hostmask('128.255.255.255'))
804 self.assertFalse(net._is_hostmask('255.255.255.255'))
805 self.assertFalse(net._is_hostmask('1.2.3.4'))
806
807 net = ipaddress.IPv4Network('127.0.0.0/0.0.0.255')
Nick Coghlan932346f2014-02-08 23:17:36 +1000808 self.assertEqual(net.prefixlen, 24)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200809
Nick Coghlandc9b2552012-05-20 21:01:57 +1000810 def testGetBroadcast(self):
811 self.assertEqual(int(self.ipv4_network.broadcast_address), 16909311)
812 self.assertEqual(str(self.ipv4_network.broadcast_address), '1.2.3.255')
813
814 self.assertEqual(int(self.ipv6_network.broadcast_address),
815 42540616829182469451850391367731642367)
816 self.assertEqual(str(self.ipv6_network.broadcast_address),
817 '2001:658:22a:cafe:ffff:ffff:ffff:ffff')
818
819 def testGetPrefixlen(self):
Nick Coghlane3ded952012-08-05 22:45:22 +1000820 self.assertEqual(self.ipv4_interface.network.prefixlen, 24)
821 self.assertEqual(self.ipv6_interface.network.prefixlen, 64)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000822
823 def testGetSupernet(self):
824 self.assertEqual(self.ipv4_network.supernet().prefixlen, 23)
825 self.assertEqual(str(self.ipv4_network.supernet().network_address),
826 '1.2.2.0')
827 self.assertEqual(
828 ipaddress.IPv4Interface('0.0.0.0/0').network.supernet(),
829 ipaddress.IPv4Network('0.0.0.0/0'))
830
831 self.assertEqual(self.ipv6_network.supernet().prefixlen, 63)
832 self.assertEqual(str(self.ipv6_network.supernet().network_address),
833 '2001:658:22a:cafe::')
834 self.assertEqual(ipaddress.IPv6Interface('::0/0').network.supernet(),
835 ipaddress.IPv6Network('::0/0'))
836
837 def testGetSupernet3(self):
838 self.assertEqual(self.ipv4_network.supernet(3).prefixlen, 21)
839 self.assertEqual(str(self.ipv4_network.supernet(3).network_address),
840 '1.2.0.0')
841
842 self.assertEqual(self.ipv6_network.supernet(3).prefixlen, 61)
843 self.assertEqual(str(self.ipv6_network.supernet(3).network_address),
844 '2001:658:22a:caf8::')
845
846 def testGetSupernet4(self):
847 self.assertRaises(ValueError, self.ipv4_network.supernet,
848 prefixlen_diff=2, new_prefix=1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200849 self.assertRaises(ValueError, self.ipv4_network.supernet,
850 new_prefix=25)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000851 self.assertEqual(self.ipv4_network.supernet(prefixlen_diff=2),
852 self.ipv4_network.supernet(new_prefix=22))
853
854 self.assertRaises(ValueError, self.ipv6_network.supernet,
855 prefixlen_diff=2, new_prefix=1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200856 self.assertRaises(ValueError, self.ipv6_network.supernet,
857 new_prefix=65)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000858 self.assertEqual(self.ipv6_network.supernet(prefixlen_diff=2),
859 self.ipv6_network.supernet(new_prefix=62))
860
861 def testHosts(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200862 hosts = list(self.ipv4_network.hosts())
863 self.assertEqual(254, len(hosts))
864 self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0])
865 self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1])
866
867 # special case where only 1 bit is left for address
Nick Coghlandc9b2552012-05-20 21:01:57 +1000868 self.assertEqual([ipaddress.IPv4Address('2.0.0.0'),
869 ipaddress.IPv4Address('2.0.0.1')],
870 list(ipaddress.ip_network('2.0.0.0/31').hosts()))
871
872 def testFancySubnetting(self):
873 self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)),
874 sorted(self.ipv4_network.subnets(new_prefix=27)))
875 self.assertRaises(ValueError, list,
876 self.ipv4_network.subnets(new_prefix=23))
877 self.assertRaises(ValueError, list,
878 self.ipv4_network.subnets(prefixlen_diff=3,
879 new_prefix=27))
880 self.assertEqual(sorted(self.ipv6_network.subnets(prefixlen_diff=4)),
881 sorted(self.ipv6_network.subnets(new_prefix=68)))
882 self.assertRaises(ValueError, list,
883 self.ipv6_network.subnets(new_prefix=63))
884 self.assertRaises(ValueError, list,
885 self.ipv6_network.subnets(prefixlen_diff=4,
886 new_prefix=68))
887
888 def testGetSubnets(self):
889 self.assertEqual(list(self.ipv4_network.subnets())[0].prefixlen, 25)
890 self.assertEqual(str(list(
891 self.ipv4_network.subnets())[0].network_address),
892 '1.2.3.0')
893 self.assertEqual(str(list(
894 self.ipv4_network.subnets())[1].network_address),
895 '1.2.3.128')
896
897 self.assertEqual(list(self.ipv6_network.subnets())[0].prefixlen, 65)
898
899 def testGetSubnetForSingle32(self):
900 ip = ipaddress.IPv4Network('1.2.3.4/32')
901 subnets1 = [str(x) for x in ip.subnets()]
902 subnets2 = [str(x) for x in ip.subnets(2)]
903 self.assertEqual(subnets1, ['1.2.3.4/32'])
904 self.assertEqual(subnets1, subnets2)
905
906 def testGetSubnetForSingle128(self):
907 ip = ipaddress.IPv6Network('::1/128')
908 subnets1 = [str(x) for x in ip.subnets()]
909 subnets2 = [str(x) for x in ip.subnets(2)]
910 self.assertEqual(subnets1, ['::1/128'])
911 self.assertEqual(subnets1, subnets2)
912
913 def testSubnet2(self):
914 ips = [str(x) for x in self.ipv4_network.subnets(2)]
915 self.assertEqual(
916 ips,
917 ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26'])
918
919 ipsv6 = [str(x) for x in self.ipv6_network.subnets(2)]
920 self.assertEqual(
921 ipsv6,
922 ['2001:658:22a:cafe::/66',
923 '2001:658:22a:cafe:4000::/66',
924 '2001:658:22a:cafe:8000::/66',
925 '2001:658:22a:cafe:c000::/66'])
926
927 def testSubnetFailsForLargeCidrDiff(self):
928 self.assertRaises(ValueError, list,
929 self.ipv4_interface.network.subnets(9))
930 self.assertRaises(ValueError, list,
931 self.ipv4_network.subnets(9))
932 self.assertRaises(ValueError, list,
933 self.ipv6_interface.network.subnets(65))
934 self.assertRaises(ValueError, list,
935 self.ipv6_network.subnets(65))
936
937 def testSupernetFailsForLargeCidrDiff(self):
938 self.assertRaises(ValueError,
939 self.ipv4_interface.network.supernet, 25)
940 self.assertRaises(ValueError,
941 self.ipv6_interface.network.supernet, 65)
942
943 def testSubnetFailsForNegativeCidrDiff(self):
944 self.assertRaises(ValueError, list,
945 self.ipv4_interface.network.subnets(-1))
946 self.assertRaises(ValueError, list,
Nick Coghlan2c589102012-05-27 01:03:25 +1000947 self.ipv4_network.subnets(-1))
Nick Coghlandc9b2552012-05-20 21:01:57 +1000948 self.assertRaises(ValueError, list,
949 self.ipv6_interface.network.subnets(-1))
950 self.assertRaises(ValueError, list,
951 self.ipv6_network.subnets(-1))
952
953 def testGetNum_Addresses(self):
954 self.assertEqual(self.ipv4_network.num_addresses, 256)
Hynek Schlawack91c5a342012-06-05 11:55:58 +0200955 self.assertEqual(list(self.ipv4_network.subnets())[0].num_addresses,
956 128)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000957 self.assertEqual(self.ipv4_network.supernet().num_addresses, 512)
958
959 self.assertEqual(self.ipv6_network.num_addresses, 18446744073709551616)
960 self.assertEqual(list(self.ipv6_network.subnets())[0].num_addresses,
961 9223372036854775808)
962 self.assertEqual(self.ipv6_network.supernet().num_addresses,
963 36893488147419103232)
964
965 def testContains(self):
Serhiy Storchaka7c389e22014-02-08 16:38:35 +0200966 self.assertIn(ipaddress.IPv4Interface('1.2.3.128/25'),
967 self.ipv4_network)
968 self.assertNotIn(ipaddress.IPv4Interface('1.2.4.1/24'),
Nick Coghlandc9b2552012-05-20 21:01:57 +1000969 self.ipv4_network)
970 # We can test addresses and string as well.
971 addr1 = ipaddress.IPv4Address('1.2.3.37')
Serhiy Storchaka7c389e22014-02-08 16:38:35 +0200972 self.assertIn(addr1, self.ipv4_network)
Nick Coghlandc9b2552012-05-20 21:01:57 +1000973 # issue 61, bad network comparison on like-ip'd network objects
974 # with identical broadcast addresses.
975 self.assertFalse(ipaddress.IPv4Network('1.1.0.0/16').__contains__(
976 ipaddress.IPv4Network('1.0.0.0/15')))
977
Nick Coghlandc9b2552012-05-20 21:01:57 +1000978 def testNth(self):
979 self.assertEqual(str(self.ipv4_network[5]), '1.2.3.5')
980 self.assertRaises(IndexError, self.ipv4_network.__getitem__, 256)
981
982 self.assertEqual(str(self.ipv6_network[5]),
983 '2001:658:22a:cafe::5')
984
985 def testGetitem(self):
986 # http://code.google.com/p/ipaddr-py/issues/detail?id=15
987 addr = ipaddress.IPv4Network('172.31.255.128/255.255.255.240')
988 self.assertEqual(28, addr.prefixlen)
989 addr_list = list(addr)
990 self.assertEqual('172.31.255.128', str(addr_list[0]))
991 self.assertEqual('172.31.255.128', str(addr[0]))
992 self.assertEqual('172.31.255.143', str(addr_list[-1]))
993 self.assertEqual('172.31.255.143', str(addr[-1]))
994 self.assertEqual(addr_list[-1], addr[-1])
995
996 def testEqual(self):
997 self.assertTrue(self.ipv4_interface ==
998 ipaddress.IPv4Interface('1.2.3.4/24'))
999 self.assertFalse(self.ipv4_interface ==
1000 ipaddress.IPv4Interface('1.2.3.4/23'))
1001 self.assertFalse(self.ipv4_interface ==
1002 ipaddress.IPv6Interface('::1.2.3.4/24'))
1003 self.assertFalse(self.ipv4_interface == '')
1004 self.assertFalse(self.ipv4_interface == [])
1005 self.assertFalse(self.ipv4_interface == 2)
1006
1007 self.assertTrue(self.ipv6_interface ==
1008 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64'))
1009 self.assertFalse(self.ipv6_interface ==
1010 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63'))
1011 self.assertFalse(self.ipv6_interface ==
1012 ipaddress.IPv4Interface('1.2.3.4/23'))
1013 self.assertFalse(self.ipv6_interface == '')
1014 self.assertFalse(self.ipv6_interface == [])
1015 self.assertFalse(self.ipv6_interface == 2)
1016
1017 def testNotEqual(self):
1018 self.assertFalse(self.ipv4_interface !=
1019 ipaddress.IPv4Interface('1.2.3.4/24'))
1020 self.assertTrue(self.ipv4_interface !=
1021 ipaddress.IPv4Interface('1.2.3.4/23'))
1022 self.assertTrue(self.ipv4_interface !=
1023 ipaddress.IPv6Interface('::1.2.3.4/24'))
1024 self.assertTrue(self.ipv4_interface != '')
1025 self.assertTrue(self.ipv4_interface != [])
1026 self.assertTrue(self.ipv4_interface != 2)
1027
1028 self.assertTrue(self.ipv4_address !=
1029 ipaddress.IPv4Address('1.2.3.5'))
1030 self.assertTrue(self.ipv4_address != '')
1031 self.assertTrue(self.ipv4_address != [])
1032 self.assertTrue(self.ipv4_address != 2)
1033
1034 self.assertFalse(self.ipv6_interface !=
1035 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64'))
1036 self.assertTrue(self.ipv6_interface !=
1037 ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63'))
1038 self.assertTrue(self.ipv6_interface !=
1039 ipaddress.IPv4Interface('1.2.3.4/23'))
1040 self.assertTrue(self.ipv6_interface != '')
1041 self.assertTrue(self.ipv6_interface != [])
1042 self.assertTrue(self.ipv6_interface != 2)
1043
1044 self.assertTrue(self.ipv6_address !=
1045 ipaddress.IPv4Address('1.2.3.4'))
1046 self.assertTrue(self.ipv6_address != '')
1047 self.assertTrue(self.ipv6_address != [])
1048 self.assertTrue(self.ipv6_address != 2)
1049
1050 def testSlash32Constructor(self):
1051 self.assertEqual(str(ipaddress.IPv4Interface(
1052 '1.2.3.4/255.255.255.255')), '1.2.3.4/32')
1053
1054 def testSlash128Constructor(self):
1055 self.assertEqual(str(ipaddress.IPv6Interface('::1/128')),
1056 '::1/128')
1057
1058 def testSlash0Constructor(self):
1059 self.assertEqual(str(ipaddress.IPv4Interface('1.2.3.4/0.0.0.0')),
1060 '1.2.3.4/0')
1061
1062 def testCollapsing(self):
1063 # test only IP addresses including some duplicates
1064 ip1 = ipaddress.IPv4Address('1.1.1.0')
1065 ip2 = ipaddress.IPv4Address('1.1.1.1')
1066 ip3 = ipaddress.IPv4Address('1.1.1.2')
1067 ip4 = ipaddress.IPv4Address('1.1.1.3')
1068 ip5 = ipaddress.IPv4Address('1.1.1.4')
1069 ip6 = ipaddress.IPv4Address('1.1.1.0')
1070 # check that addreses are subsumed properly.
1071 collapsed = ipaddress.collapse_addresses(
1072 [ip1, ip2, ip3, ip4, ip5, ip6])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001073 self.assertEqual(list(collapsed),
1074 [ipaddress.IPv4Network('1.1.1.0/30'),
1075 ipaddress.IPv4Network('1.1.1.4/32')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001076
1077 # test a mix of IP addresses and networks including some duplicates
1078 ip1 = ipaddress.IPv4Address('1.1.1.0')
1079 ip2 = ipaddress.IPv4Address('1.1.1.1')
1080 ip3 = ipaddress.IPv4Address('1.1.1.2')
1081 ip4 = ipaddress.IPv4Address('1.1.1.3')
1082 #ip5 = ipaddress.IPv4Interface('1.1.1.4/30')
1083 #ip6 = ipaddress.IPv4Interface('1.1.1.4/30')
1084 # check that addreses are subsumed properly.
1085 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001086 self.assertEqual(list(collapsed),
1087 [ipaddress.IPv4Network('1.1.1.0/30')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001088
1089 # test only IP networks
1090 ip1 = ipaddress.IPv4Network('1.1.0.0/24')
1091 ip2 = ipaddress.IPv4Network('1.1.1.0/24')
1092 ip3 = ipaddress.IPv4Network('1.1.2.0/24')
1093 ip4 = ipaddress.IPv4Network('1.1.3.0/24')
1094 ip5 = ipaddress.IPv4Network('1.1.4.0/24')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001095 # stored in no particular order b/c we want CollapseAddr to call
1096 # [].sort
Nick Coghlandc9b2552012-05-20 21:01:57 +10001097 ip6 = ipaddress.IPv4Network('1.1.0.0/22')
1098 # check that addreses are subsumed properly.
1099 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4, ip5,
1100 ip6])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001101 self.assertEqual(list(collapsed),
1102 [ipaddress.IPv4Network('1.1.0.0/22'),
1103 ipaddress.IPv4Network('1.1.4.0/24')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001104
1105 # test that two addresses are supernet'ed properly
1106 collapsed = ipaddress.collapse_addresses([ip1, ip2])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001107 self.assertEqual(list(collapsed),
1108 [ipaddress.IPv4Network('1.1.0.0/23')])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001109
1110 # test same IP networks
1111 ip_same1 = ip_same2 = ipaddress.IPv4Network('1.1.1.1/32')
1112 self.assertEqual(list(ipaddress.collapse_addresses(
1113 [ip_same1, ip_same2])),
1114 [ip_same1])
1115
1116 # test same IP addresses
1117 ip_same1 = ip_same2 = ipaddress.IPv4Address('1.1.1.1')
1118 self.assertEqual(list(ipaddress.collapse_addresses(
1119 [ip_same1, ip_same2])),
1120 [ipaddress.ip_network('1.1.1.1/32')])
1121 ip1 = ipaddress.IPv6Network('2001::/100')
1122 ip2 = ipaddress.IPv6Network('2001::/120')
1123 ip3 = ipaddress.IPv6Network('2001::/96')
1124 # test that ipv6 addresses are subsumed properly.
1125 collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3])
1126 self.assertEqual(list(collapsed), [ip3])
1127
1128 # the toejam test
Hynek Schlawack35db5132012-06-01 20:12:17 +02001129 addr_tuples = [
1130 (ipaddress.ip_address('1.1.1.1'),
1131 ipaddress.ip_address('::1')),
1132 (ipaddress.IPv4Network('1.1.0.0/24'),
1133 ipaddress.IPv6Network('2001::/120')),
1134 (ipaddress.IPv4Network('1.1.0.0/32'),
1135 ipaddress.IPv6Network('2001::/128')),
1136 ]
1137 for ip1, ip2 in addr_tuples:
1138 self.assertRaises(TypeError, ipaddress.collapse_addresses,
1139 [ip1, ip2])
Nick Coghlandc9b2552012-05-20 21:01:57 +10001140
1141 def testSummarizing(self):
1142 #ip = ipaddress.ip_address
1143 #ipnet = ipaddress.ip_network
1144 summarize = ipaddress.summarize_address_range
1145 ip1 = ipaddress.ip_address('1.1.1.0')
1146 ip2 = ipaddress.ip_address('1.1.1.255')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001147
1148 # summarize works only for IPv4 & IPv6
1149 class IPv7Address(ipaddress.IPv6Address):
1150 @property
1151 def version(self):
1152 return 7
1153 ip_invalid1 = IPv7Address('::1')
1154 ip_invalid2 = IPv7Address('::1')
1155 self.assertRaises(ValueError, list,
1156 summarize(ip_invalid1, ip_invalid2))
1157 # test that a summary over ip4 & ip6 fails
1158 self.assertRaises(TypeError, list,
1159 summarize(ip1, ipaddress.IPv6Address('::1')))
1160 # test a /24 is summarized properly
Nick Coghlandc9b2552012-05-20 21:01:57 +10001161 self.assertEqual(list(summarize(ip1, ip2))[0],
1162 ipaddress.ip_network('1.1.1.0/24'))
1163 # test an IPv4 range that isn't on a network byte boundary
1164 ip2 = ipaddress.ip_address('1.1.1.8')
1165 self.assertEqual(list(summarize(ip1, ip2)),
1166 [ipaddress.ip_network('1.1.1.0/29'),
1167 ipaddress.ip_network('1.1.1.8')])
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001168 # all!
1169 ip1 = ipaddress.IPv4Address(0)
1170 ip2 = ipaddress.IPv4Address(ipaddress.IPv4Address._ALL_ONES)
1171 self.assertEqual([ipaddress.IPv4Network('0.0.0.0/0')],
1172 list(summarize(ip1, ip2)))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001173
1174 ip1 = ipaddress.ip_address('1::')
1175 ip2 = ipaddress.ip_address('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff')
1176 # test a IPv6 is sumamrized properly
1177 self.assertEqual(list(summarize(ip1, ip2))[0],
1178 ipaddress.ip_network('1::/16'))
1179 # test an IPv6 range that isn't on a network byte boundary
1180 ip2 = ipaddress.ip_address('2::')
1181 self.assertEqual(list(summarize(ip1, ip2)),
1182 [ipaddress.ip_network('1::/16'),
1183 ipaddress.ip_network('2::/128')])
1184
1185 # test exception raised when first is greater than last
1186 self.assertRaises(ValueError, list,
1187 summarize(ipaddress.ip_address('1.1.1.0'),
1188 ipaddress.ip_address('1.1.0.0')))
1189 # test exception raised when first and last aren't IP addresses
1190 self.assertRaises(TypeError, list,
1191 summarize(ipaddress.ip_network('1.1.1.0'),
1192 ipaddress.ip_network('1.1.0.0')))
1193 self.assertRaises(TypeError, list,
1194 summarize(ipaddress.ip_network('1.1.1.0'),
1195 ipaddress.ip_network('1.1.0.0')))
1196 # test exception raised when first and last are not same version
1197 self.assertRaises(TypeError, list,
1198 summarize(ipaddress.ip_address('::'),
1199 ipaddress.ip_network('1.1.0.0')))
1200
1201 def testAddressComparison(self):
1202 self.assertTrue(ipaddress.ip_address('1.1.1.1') <=
1203 ipaddress.ip_address('1.1.1.1'))
1204 self.assertTrue(ipaddress.ip_address('1.1.1.1') <=
1205 ipaddress.ip_address('1.1.1.2'))
1206 self.assertTrue(ipaddress.ip_address('::1') <=
1207 ipaddress.ip_address('::1'))
1208 self.assertTrue(ipaddress.ip_address('::1') <=
1209 ipaddress.ip_address('::2'))
1210
Nick Coghlan3008ec02012-07-08 00:45:33 +10001211 def testInterfaceComparison(self):
1212 self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1213 ipaddress.ip_interface('1.1.1.1'))
1214 self.assertTrue(ipaddress.ip_interface('1.1.1.1') <=
1215 ipaddress.ip_interface('1.1.1.2'))
1216 self.assertTrue(ipaddress.ip_interface('::1') <=
1217 ipaddress.ip_interface('::1'))
1218 self.assertTrue(ipaddress.ip_interface('::1') <=
1219 ipaddress.ip_interface('::2'))
1220
Nick Coghlandc9b2552012-05-20 21:01:57 +10001221 def testNetworkComparison(self):
1222 # ip1 and ip2 have the same network address
1223 ip1 = ipaddress.IPv4Network('1.1.1.0/24')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001224 ip2 = ipaddress.IPv4Network('1.1.1.0/32')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001225 ip3 = ipaddress.IPv4Network('1.1.2.0/24')
1226
1227 self.assertTrue(ip1 < ip3)
1228 self.assertTrue(ip3 > ip2)
1229
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001230 self.assertEqual(ip1.compare_networks(ip1), 0)
1231
1232 # if addresses are the same, sort by netmask
1233 self.assertEqual(ip1.compare_networks(ip2), -1)
1234 self.assertEqual(ip2.compare_networks(ip1), 1)
1235
Nick Coghlandc9b2552012-05-20 21:01:57 +10001236 self.assertEqual(ip1.compare_networks(ip3), -1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001237 self.assertEqual(ip3.compare_networks(ip1), 1)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001238 self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key())
1239
1240 ip1 = ipaddress.IPv6Network('2001:2000::/96')
1241 ip2 = ipaddress.IPv6Network('2001:2001::/96')
1242 ip3 = ipaddress.IPv6Network('2001:ffff:2000::/96')
1243
1244 self.assertTrue(ip1 < ip3)
1245 self.assertTrue(ip3 > ip2)
1246 self.assertEqual(ip1.compare_networks(ip3), -1)
1247 self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key())
1248
1249 # Test comparing different protocols.
1250 # Should always raise a TypeError.
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001251 self.assertRaises(TypeError,
1252 self.ipv4_network.compare_networks,
1253 self.ipv6_network)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001254 ipv6 = ipaddress.IPv6Interface('::/0')
1255 ipv4 = ipaddress.IPv4Interface('0.0.0.0/0')
1256 self.assertRaises(TypeError, ipv4.__lt__, ipv6)
1257 self.assertRaises(TypeError, ipv4.__gt__, ipv6)
1258 self.assertRaises(TypeError, ipv6.__lt__, ipv4)
1259 self.assertRaises(TypeError, ipv6.__gt__, ipv4)
1260
1261 # Regression test for issue 19.
1262 ip1 = ipaddress.ip_network('10.1.2.128/25')
1263 self.assertFalse(ip1 < ip1)
1264 self.assertFalse(ip1 > ip1)
1265 ip2 = ipaddress.ip_network('10.1.3.0/24')
1266 self.assertTrue(ip1 < ip2)
1267 self.assertFalse(ip2 < ip1)
1268 self.assertFalse(ip1 > ip2)
1269 self.assertTrue(ip2 > ip1)
1270 ip3 = ipaddress.ip_network('10.1.3.0/25')
1271 self.assertTrue(ip2 < ip3)
1272 self.assertFalse(ip3 < ip2)
1273 self.assertFalse(ip2 > ip3)
1274 self.assertTrue(ip3 > ip2)
1275
1276 # Regression test for issue 28.
1277 ip1 = ipaddress.ip_network('10.10.10.0/31')
1278 ip2 = ipaddress.ip_network('10.10.10.0')
1279 ip3 = ipaddress.ip_network('10.10.10.2/31')
1280 ip4 = ipaddress.ip_network('10.10.10.2')
1281 sorted = [ip1, ip2, ip3, ip4]
1282 unsorted = [ip2, ip4, ip1, ip3]
1283 unsorted.sort()
1284 self.assertEqual(sorted, unsorted)
1285 unsorted = [ip4, ip1, ip3, ip2]
1286 unsorted.sort()
1287 self.assertEqual(sorted, unsorted)
Serhiy Storchakaf186e122015-01-26 10:11:16 +02001288 self.assertIs(ip1.__lt__(ipaddress.ip_address('10.10.10.0')),
1289 NotImplemented)
1290 self.assertIs(ip2.__lt__(ipaddress.ip_address('10.10.10.0')),
1291 NotImplemented)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001292
1293 # <=, >=
1294 self.assertTrue(ipaddress.ip_network('1.1.1.1') <=
1295 ipaddress.ip_network('1.1.1.1'))
1296 self.assertTrue(ipaddress.ip_network('1.1.1.1') <=
1297 ipaddress.ip_network('1.1.1.2'))
1298 self.assertFalse(ipaddress.ip_network('1.1.1.2') <=
1299 ipaddress.ip_network('1.1.1.1'))
1300 self.assertTrue(ipaddress.ip_network('::1') <=
1301 ipaddress.ip_network('::1'))
1302 self.assertTrue(ipaddress.ip_network('::1') <=
1303 ipaddress.ip_network('::2'))
1304 self.assertFalse(ipaddress.ip_network('::2') <=
1305 ipaddress.ip_network('::1'))
1306
1307 def testStrictNetworks(self):
1308 self.assertRaises(ValueError, ipaddress.ip_network, '192.168.1.1/24')
1309 self.assertRaises(ValueError, ipaddress.ip_network, '::1/120')
1310
1311 def testOverlaps(self):
1312 other = ipaddress.IPv4Network('1.2.3.0/30')
1313 other2 = ipaddress.IPv4Network('1.2.2.0/24')
1314 other3 = ipaddress.IPv4Network('1.2.2.64/26')
1315 self.assertTrue(self.ipv4_network.overlaps(other))
1316 self.assertFalse(self.ipv4_network.overlaps(other2))
1317 self.assertTrue(other2.overlaps(other3))
1318
1319 def testEmbeddedIpv4(self):
1320 ipv4_string = '192.168.0.1'
1321 ipv4 = ipaddress.IPv4Interface(ipv4_string)
1322 v4compat_ipv6 = ipaddress.IPv6Interface('::%s' % ipv4_string)
1323 self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip))
1324 v4mapped_ipv6 = ipaddress.IPv6Interface('::ffff:%s' % ipv4_string)
1325 self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip)
1326 self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface,
1327 '2001:1.1.1.1:1.1.1.1')
1328
1329 # Issue 67: IPv6 with embedded IPv4 address not recognized.
1330 def testIPv6AddressTooLarge(self):
1331 # RFC4291 2.5.5.2
1332 self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1'),
1333 ipaddress.ip_address('::FFFF:c000:201'))
1334 # RFC4291 2.2 (part 3) x::d.d.d.d
1335 self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'),
1336 ipaddress.ip_address('FFFF::c000:201'))
1337
1338 def testIPVersion(self):
1339 self.assertEqual(self.ipv4_address.version, 4)
1340 self.assertEqual(self.ipv6_address.version, 6)
1341
1342 def testMaxPrefixLength(self):
1343 self.assertEqual(self.ipv4_interface.max_prefixlen, 32)
1344 self.assertEqual(self.ipv6_interface.max_prefixlen, 128)
1345
1346 def testPacked(self):
1347 self.assertEqual(self.ipv4_address.packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001348 b'\x01\x02\x03\x04')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001349 self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001350 b'\xff\xfe\xfd\xfc')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001351 self.assertEqual(self.ipv6_address.packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001352 b'\x20\x01\x06\x58\x02\x2a\xca\xfe'
1353 b'\x02\x00\x00\x00\x00\x00\x00\x01')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001354 self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001355 b'\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff'
1356 + b'\x00' * 6)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001357 self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed,
Nick Coghlan5cf896f2012-07-07 01:43:31 +10001358 b'\x00' * 6 + b'\x00\x01' + b'\x00' * 8)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001359
Nick Coghlandc9b2552012-05-20 21:01:57 +10001360 def testIpType(self):
1361 ipv4net = ipaddress.ip_network('1.2.3.4')
1362 ipv4addr = ipaddress.ip_address('1.2.3.4')
1363 ipv6net = ipaddress.ip_network('::1.2.3.4')
1364 ipv6addr = ipaddress.ip_address('::1.2.3.4')
1365 self.assertEqual(ipaddress.IPv4Network, type(ipv4net))
1366 self.assertEqual(ipaddress.IPv4Address, type(ipv4addr))
1367 self.assertEqual(ipaddress.IPv6Network, type(ipv6net))
1368 self.assertEqual(ipaddress.IPv6Address, type(ipv6addr))
1369
1370 def testReservedIpv4(self):
1371 # test networks
1372 self.assertEqual(True, ipaddress.ip_interface(
1373 '224.1.1.1/31').is_multicast)
1374 self.assertEqual(False, ipaddress.ip_network('240.0.0.0').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001375 self.assertEqual(True, ipaddress.ip_network('240.0.0.0').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001376
1377 self.assertEqual(True, ipaddress.ip_interface(
1378 '192.168.1.1/17').is_private)
1379 self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private)
1380 self.assertEqual(True, ipaddress.ip_network(
1381 '10.255.255.255').is_private)
1382 self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_private)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001383 self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001384 self.assertEqual(True, ipaddress.ip_network(
1385 '172.31.255.255').is_private)
1386 self.assertEqual(False, ipaddress.ip_network('172.32.0.0').is_private)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001387 self.assertEqual(True,
1388 ipaddress.ip_network('169.254.1.0/24').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001389
1390 self.assertEqual(True,
1391 ipaddress.ip_interface(
1392 '169.254.100.200/24').is_link_local)
1393 self.assertEqual(False,
1394 ipaddress.ip_interface(
1395 '169.255.100.200/24').is_link_local)
1396
1397 self.assertEqual(True,
1398 ipaddress.ip_network(
1399 '127.100.200.254/32').is_loopback)
1400 self.assertEqual(True, ipaddress.ip_network(
1401 '127.42.0.0/16').is_loopback)
1402 self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback)
Peter Moodye5019d52013-10-24 09:47:10 -07001403 self.assertEqual(False,
1404 ipaddress.ip_network('100.64.0.0/10').is_private)
Peter Moodybe9c1b12013-10-22 12:36:21 -07001405 self.assertEqual(False, ipaddress.ip_network('100.64.0.0/10').is_global)
Peter Moodye5019d52013-10-24 09:47:10 -07001406
Peter Moody22c31762013-10-21 13:58:06 -07001407 self.assertEqual(True,
1408 ipaddress.ip_network('192.0.2.128/25').is_private)
1409 self.assertEqual(True,
1410 ipaddress.ip_network('192.0.3.0/24').is_global)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001411
1412 # test addresses
Hynek Schlawackbcd30442012-06-04 14:19:39 +02001413 self.assertEqual(True, ipaddress.ip_address('0.0.0.0').is_unspecified)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001414 self.assertEqual(True, ipaddress.ip_address('224.1.1.1').is_multicast)
1415 self.assertEqual(False, ipaddress.ip_address('240.0.0.0').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001416 self.assertEqual(True, ipaddress.ip_address('240.0.0.1').is_reserved)
1417 self.assertEqual(False,
1418 ipaddress.ip_address('239.255.255.255').is_reserved)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001419
1420 self.assertEqual(True, ipaddress.ip_address('192.168.1.1').is_private)
1421 self.assertEqual(False, ipaddress.ip_address('192.169.0.0').is_private)
1422 self.assertEqual(True, ipaddress.ip_address(
1423 '10.255.255.255').is_private)
1424 self.assertEqual(False, ipaddress.ip_address('11.0.0.0').is_private)
1425 self.assertEqual(True, ipaddress.ip_address(
1426 '172.31.255.255').is_private)
1427 self.assertEqual(False, ipaddress.ip_address('172.32.0.0').is_private)
1428
1429 self.assertEqual(True,
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001430 ipaddress.ip_address('169.254.100.200').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001431 self.assertEqual(False,
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001432 ipaddress.ip_address('169.255.100.200').is_link_local)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001433
1434 self.assertEqual(True,
1435 ipaddress.ip_address('127.100.200.254').is_loopback)
1436 self.assertEqual(True, ipaddress.ip_address('127.42.0.0').is_loopback)
1437 self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback)
1438 self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified)
1439
1440 def testReservedIpv6(self):
1441
1442 self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001443 self.assertEqual(True, ipaddress.ip_network(2**128 - 1).is_multicast)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001444 self.assertEqual(True, ipaddress.ip_network('ff00::').is_multicast)
1445 self.assertEqual(False, ipaddress.ip_network('fdff::').is_multicast)
1446
1447 self.assertEqual(True, ipaddress.ip_network('fecf::').is_site_local)
1448 self.assertEqual(True, ipaddress.ip_network(
1449 'feff:ffff:ffff:ffff::').is_site_local)
1450 self.assertEqual(False, ipaddress.ip_network(
1451 'fbf:ffff::').is_site_local)
1452 self.assertEqual(False, ipaddress.ip_network('ff00::').is_site_local)
1453
1454 self.assertEqual(True, ipaddress.ip_network('fc00::').is_private)
1455 self.assertEqual(True, ipaddress.ip_network(
1456 'fc00:ffff:ffff:ffff::').is_private)
1457 self.assertEqual(False, ipaddress.ip_network('fbff:ffff::').is_private)
1458 self.assertEqual(False, ipaddress.ip_network('fe00::').is_private)
1459
1460 self.assertEqual(True, ipaddress.ip_network('fea0::').is_link_local)
1461 self.assertEqual(True, ipaddress.ip_network(
1462 'febf:ffff::').is_link_local)
1463 self.assertEqual(False, ipaddress.ip_network(
1464 'fe7f:ffff::').is_link_local)
1465 self.assertEqual(False, ipaddress.ip_network('fec0::').is_link_local)
1466
1467 self.assertEqual(True, ipaddress.ip_interface('0:0::0:01').is_loopback)
1468 self.assertEqual(False, ipaddress.ip_interface('::1/127').is_loopback)
1469 self.assertEqual(False, ipaddress.ip_network('::').is_loopback)
1470 self.assertEqual(False, ipaddress.ip_network('::2').is_loopback)
1471
1472 self.assertEqual(True, ipaddress.ip_network('0::0').is_unspecified)
1473 self.assertEqual(False, ipaddress.ip_network('::1').is_unspecified)
1474 self.assertEqual(False, ipaddress.ip_network('::/127').is_unspecified)
1475
Peter Moody22c31762013-10-21 13:58:06 -07001476 self.assertEqual(True,
1477 ipaddress.ip_network('2001::1/128').is_private)
1478 self.assertEqual(True,
1479 ipaddress.ip_network('200::1/128').is_global)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001480 # test addresses
1481 self.assertEqual(True, ipaddress.ip_address('ffff::').is_multicast)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001482 self.assertEqual(True, ipaddress.ip_address(2**128 - 1).is_multicast)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001483 self.assertEqual(True, ipaddress.ip_address('ff00::').is_multicast)
1484 self.assertEqual(False, ipaddress.ip_address('fdff::').is_multicast)
1485
1486 self.assertEqual(True, ipaddress.ip_address('fecf::').is_site_local)
1487 self.assertEqual(True, ipaddress.ip_address(
1488 'feff:ffff:ffff:ffff::').is_site_local)
1489 self.assertEqual(False, ipaddress.ip_address(
1490 'fbf:ffff::').is_site_local)
1491 self.assertEqual(False, ipaddress.ip_address('ff00::').is_site_local)
1492
1493 self.assertEqual(True, ipaddress.ip_address('fc00::').is_private)
1494 self.assertEqual(True, ipaddress.ip_address(
1495 'fc00:ffff:ffff:ffff::').is_private)
1496 self.assertEqual(False, ipaddress.ip_address('fbff:ffff::').is_private)
1497 self.assertEqual(False, ipaddress.ip_address('fe00::').is_private)
1498
1499 self.assertEqual(True, ipaddress.ip_address('fea0::').is_link_local)
1500 self.assertEqual(True, ipaddress.ip_address(
1501 'febf:ffff::').is_link_local)
1502 self.assertEqual(False, ipaddress.ip_address(
1503 'fe7f:ffff::').is_link_local)
1504 self.assertEqual(False, ipaddress.ip_address('fec0::').is_link_local)
1505
1506 self.assertEqual(True, ipaddress.ip_address('0:0::0:01').is_loopback)
1507 self.assertEqual(True, ipaddress.ip_address('::1').is_loopback)
1508 self.assertEqual(False, ipaddress.ip_address('::2').is_loopback)
1509
1510 self.assertEqual(True, ipaddress.ip_address('0::0').is_unspecified)
1511 self.assertEqual(False, ipaddress.ip_address('::1').is_unspecified)
1512
1513 # some generic IETF reserved addresses
1514 self.assertEqual(True, ipaddress.ip_address('100::').is_reserved)
1515 self.assertEqual(True, ipaddress.ip_network('4000::1/128').is_reserved)
1516
1517 def testIpv4Mapped(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001518 self.assertEqual(
1519 ipaddress.ip_address('::ffff:192.168.1.1').ipv4_mapped,
1520 ipaddress.ip_address('192.168.1.1'))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001521 self.assertEqual(ipaddress.ip_address('::c0a8:101').ipv4_mapped, None)
1522 self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped,
1523 ipaddress.ip_address('192.168.1.1'))
1524
1525 def testAddrExclude(self):
1526 addr1 = ipaddress.ip_network('10.1.1.0/24')
1527 addr2 = ipaddress.ip_network('10.1.1.0/26')
1528 addr3 = ipaddress.ip_network('10.2.1.0/24')
1529 addr4 = ipaddress.ip_address('10.1.1.0')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001530 addr5 = ipaddress.ip_network('2001:db8::0/32')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001531 self.assertEqual(sorted(list(addr1.address_exclude(addr2))),
1532 [ipaddress.ip_network('10.1.1.64/26'),
1533 ipaddress.ip_network('10.1.1.128/25')])
1534 self.assertRaises(ValueError, list, addr1.address_exclude(addr3))
1535 self.assertRaises(TypeError, list, addr1.address_exclude(addr4))
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001536 self.assertRaises(TypeError, list, addr1.address_exclude(addr5))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001537 self.assertEqual(list(addr1.address_exclude(addr1)), [])
1538
1539 def testHash(self):
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001540 self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')),
1541 hash(ipaddress.ip_interface('10.1.1.0/24')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001542 self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001543 hash(ipaddress.ip_network('10.1.1.0/24')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001544 self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001545 hash(ipaddress.ip_address('10.1.1.0')))
Nick Coghlandc9b2552012-05-20 21:01:57 +10001546 # i70
1547 self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')),
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001548 hash(ipaddress.ip_address(
Nick Coghlandc9b2552012-05-20 21:01:57 +10001549 int(ipaddress.ip_address('1.2.3.4')._ip))))
1550 ip1 = ipaddress.ip_address('10.1.1.0')
1551 ip2 = ipaddress.ip_address('1::')
1552 dummy = {}
1553 dummy[self.ipv4_address] = None
1554 dummy[self.ipv6_address] = None
1555 dummy[ip1] = None
1556 dummy[ip2] = None
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001557 self.assertIn(self.ipv4_address, dummy)
1558 self.assertIn(ip2, dummy)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001559
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001560 def testIPBases(self):
1561 net = self.ipv4_network
1562 self.assertEqual('1.2.3.0/24', net.compressed)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001563 net = self.ipv6_network
1564 self.assertRaises(ValueError, net._string_from_ip_int, 2**128 + 1)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001565
Hynek Schlawack454a74d2012-06-04 18:14:02 +02001566 def testIPv6NetworkHelpers(self):
1567 net = self.ipv6_network
1568 self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen)
1569 self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::',
1570 net.with_netmask)
1571 self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff',
1572 net.with_hostmask)
1573 self.assertEqual('2001:658:22a:cafe::/64', str(net))
1574
1575 def testIPv4NetworkHelpers(self):
1576 net = self.ipv4_network
1577 self.assertEqual('1.2.3.0/24', net.with_prefixlen)
1578 self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask)
1579 self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask)
1580 self.assertEqual('1.2.3.0/24', str(net))
1581
Nick Coghlandc9b2552012-05-20 21:01:57 +10001582 def testCopyConstructor(self):
1583 addr1 = ipaddress.ip_network('10.1.1.0/24')
1584 addr2 = ipaddress.ip_network(addr1)
1585 addr3 = ipaddress.ip_interface('2001:658:22a:cafe:200::1/64')
1586 addr4 = ipaddress.ip_interface(addr3)
1587 addr5 = ipaddress.IPv4Address('1.1.1.1')
1588 addr6 = ipaddress.IPv6Address('2001:658:22a:cafe:200::1')
1589
1590 self.assertEqual(addr1, addr2)
1591 self.assertEqual(addr3, addr4)
1592 self.assertEqual(addr5, ipaddress.IPv4Address(addr5))
1593 self.assertEqual(addr6, ipaddress.IPv6Address(addr6))
1594
1595 def testCompressIPv6Address(self):
1596 test_addresses = {
1597 '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128',
1598 '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128',
1599 '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128',
1600 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
1601 '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128',
1602 '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128',
1603 '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128',
1604 '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128',
1605 '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128',
1606 '0:0:0:0:0:0:0:0': '::/128',
1607 '0:0:0:0:0:0:0:0/0': '::/0',
1608 '0:0:0:0:0:0:0:1': '::1/128',
1609 '2001:0658:022a:cafe:0000:0000:0000:0000/66':
1610 '2001:658:22a:cafe::/66',
1611 '::1.2.3.4': '::102:304/128',
1612 '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128',
1613 '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128',
1614 '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128',
1615 '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128',
1616 '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128',
1617 }
1618 for uncompressed, compressed in list(test_addresses.items()):
1619 self.assertEqual(compressed, str(ipaddress.IPv6Interface(
1620 uncompressed)))
1621
1622 def testExplodeShortHandIpStr(self):
1623 addr1 = ipaddress.IPv6Interface('2001::1')
1624 addr2 = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
1625 addr3 = ipaddress.IPv6Network('2001::/96')
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001626 addr4 = ipaddress.IPv4Address('192.168.178.1')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001627 self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128',
1628 addr1.exploded)
1629 self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128',
1630 ipaddress.IPv6Interface('::1/128').exploded)
1631 # issue 77
1632 self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1',
1633 addr2.exploded)
1634 self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0000/96',
1635 addr3.exploded)
Hynek Schlawack91c5a342012-06-05 11:55:58 +02001636 self.assertEqual('192.168.178.1', addr4.exploded)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001637
1638 def testIntRepresentation(self):
1639 self.assertEqual(16909060, int(self.ipv4_address))
1640 self.assertEqual(42540616829182469433547762482097946625,
1641 int(self.ipv6_address))
1642
Nick Coghlandc9b2552012-05-20 21:01:57 +10001643 def testForceVersion(self):
1644 self.assertEqual(ipaddress.ip_network(1).version, 4)
Nick Coghlan51c30672012-05-27 00:25:58 +10001645 self.assertEqual(ipaddress.IPv6Network(1).version, 6)
Sandro Tosi876ecad2012-05-23 22:26:55 +02001646
Nick Coghlandc9b2552012-05-20 21:01:57 +10001647 def testWithStar(self):
Nick Coghlana8517ad2012-08-20 10:04:26 +10001648 self.assertEqual(self.ipv4_interface.with_prefixlen, "1.2.3.4/24")
1649 self.assertEqual(self.ipv4_interface.with_netmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001650 "1.2.3.4/255.255.255.0")
Nick Coghlana8517ad2012-08-20 10:04:26 +10001651 self.assertEqual(self.ipv4_interface.with_hostmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001652 "1.2.3.4/0.0.0.255")
1653
Nick Coghlana8517ad2012-08-20 10:04:26 +10001654 self.assertEqual(self.ipv6_interface.with_prefixlen,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001655 '2001:658:22a:cafe:200::1/64')
Nick Coghlana8517ad2012-08-20 10:04:26 +10001656 self.assertEqual(self.ipv6_interface.with_netmask,
1657 '2001:658:22a:cafe:200::1/ffff:ffff:ffff:ffff::')
Nick Coghlandc9b2552012-05-20 21:01:57 +10001658 # this probably don't make much sense, but it's included for
1659 # compatibility with ipv4
Nick Coghlana8517ad2012-08-20 10:04:26 +10001660 self.assertEqual(self.ipv6_interface.with_hostmask,
Nick Coghlandc9b2552012-05-20 21:01:57 +10001661 '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff')
1662
1663 def testNetworkElementCaching(self):
1664 # V4 - make sure we're empty
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001665 self.assertNotIn('network_address', self.ipv4_network._cache)
1666 self.assertNotIn('broadcast_address', self.ipv4_network._cache)
1667 self.assertNotIn('hostmask', self.ipv4_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001668
1669 # V4 - populate and test
1670 self.assertEqual(self.ipv4_network.network_address,
1671 ipaddress.IPv4Address('1.2.3.0'))
1672 self.assertEqual(self.ipv4_network.broadcast_address,
1673 ipaddress.IPv4Address('1.2.3.255'))
1674 self.assertEqual(self.ipv4_network.hostmask,
1675 ipaddress.IPv4Address('0.0.0.255'))
1676
1677 # V4 - check we're cached
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001678 self.assertIn('broadcast_address', self.ipv4_network._cache)
1679 self.assertIn('hostmask', self.ipv4_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001680
1681 # V6 - make sure we're empty
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001682 self.assertNotIn('broadcast_address', self.ipv6_network._cache)
1683 self.assertNotIn('hostmask', self.ipv6_network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001684
1685 # V6 - populate and test
1686 self.assertEqual(self.ipv6_network.network_address,
1687 ipaddress.IPv6Address('2001:658:22a:cafe::'))
1688 self.assertEqual(self.ipv6_interface.network.network_address,
1689 ipaddress.IPv6Address('2001:658:22a:cafe::'))
1690
1691 self.assertEqual(
1692 self.ipv6_network.broadcast_address,
1693 ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff'))
1694 self.assertEqual(self.ipv6_network.hostmask,
1695 ipaddress.IPv6Address('::ffff:ffff:ffff:ffff'))
1696 self.assertEqual(
1697 self.ipv6_interface.network.broadcast_address,
1698 ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff'))
1699 self.assertEqual(self.ipv6_interface.network.hostmask,
1700 ipaddress.IPv6Address('::ffff:ffff:ffff:ffff'))
1701
1702 # V6 - check we're cached
Serhiy Storchaka7c389e22014-02-08 16:38:35 +02001703 self.assertIn('broadcast_address', self.ipv6_network._cache)
1704 self.assertIn('hostmask', self.ipv6_network._cache)
1705 self.assertIn('broadcast_address', self.ipv6_interface.network._cache)
1706 self.assertIn('hostmask', self.ipv6_interface.network._cache)
Nick Coghlandc9b2552012-05-20 21:01:57 +10001707
1708 def testTeredo(self):
1709 # stolen from wikipedia
1710 server = ipaddress.IPv4Address('65.54.227.120')
1711 client = ipaddress.IPv4Address('192.0.2.45')
1712 teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2'
1713 self.assertEqual((server, client),
1714 ipaddress.ip_address(teredo_addr).teredo)
1715 bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2'
1716 self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
1717 bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2'
1718 self.assertFalse(ipaddress.ip_address(bad_addr).teredo)
1719
1720 # i77
1721 teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1')
1722 self.assertEqual((ipaddress.IPv4Address('94.245.121.253'),
1723 ipaddress.IPv4Address('95.26.244.94')),
1724 teredo_addr.teredo)
1725
Nick Coghlandc9b2552012-05-20 21:01:57 +10001726 def testsixtofour(self):
1727 sixtofouraddr = ipaddress.ip_address('2002:ac1d:2d64::1')
1728 bad_addr = ipaddress.ip_address('2000:ac1d:2d64::1')
1729 self.assertEqual(ipaddress.IPv4Address('172.29.45.100'),
1730 sixtofouraddr.sixtofour)
1731 self.assertFalse(bad_addr.sixtofour)
1732
Nick Coghlandc9b2552012-05-20 21:01:57 +10001733
1734if __name__ == '__main__':
1735 unittest.main()