1 | n/a | # Copyright 2007 Google Inc. |
---|
2 | n/a | # Licensed to PSF under a Contributor Agreement. |
---|
3 | n/a | |
---|
4 | n/a | """Unittest for ipaddress module.""" |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | import unittest |
---|
8 | n/a | import re |
---|
9 | n/a | import contextlib |
---|
10 | n/a | import functools |
---|
11 | n/a | import operator |
---|
12 | n/a | import pickle |
---|
13 | n/a | import ipaddress |
---|
14 | n/a | import weakref |
---|
15 | n/a | |
---|
16 | n/a | |
---|
17 | n/a | class BaseTestCase(unittest.TestCase): |
---|
18 | n/a | # One big change in ipaddress over the original ipaddr module is |
---|
19 | n/a | # error reporting that tries to assume users *don't know the rules* |
---|
20 | n/a | # for what constitutes an RFC compliant IP address |
---|
21 | n/a | |
---|
22 | n/a | # Ensuring these errors are emitted correctly in all relevant cases |
---|
23 | n/a | # meant moving to a more systematic test structure that allows the |
---|
24 | n/a | # test structure to map more directly to the module structure |
---|
25 | n/a | |
---|
26 | n/a | # Note that if the constructors are refactored so that addresses with |
---|
27 | n/a | # multiple problems get classified differently, that's OK - just |
---|
28 | n/a | # move the affected examples to the newly appropriate test case. |
---|
29 | n/a | |
---|
30 | n/a | # There is some duplication between the original relatively ad hoc |
---|
31 | n/a | # test suite and the new systematic tests. While some redundancy in |
---|
32 | n/a | # testing is considered preferable to accidentally deleting a valid |
---|
33 | n/a | # test, the original test suite will likely be reduced over time as |
---|
34 | n/a | # redundant tests are identified. |
---|
35 | n/a | |
---|
36 | n/a | @property |
---|
37 | n/a | def factory(self): |
---|
38 | n/a | raise NotImplementedError |
---|
39 | n/a | |
---|
40 | n/a | @contextlib.contextmanager |
---|
41 | n/a | def assertCleanError(self, exc_type, details, *args): |
---|
42 | n/a | """ |
---|
43 | n/a | Ensure exception does not display a context by default |
---|
44 | n/a | |
---|
45 | n/a | Wraps unittest.TestCase.assertRaisesRegex |
---|
46 | n/a | """ |
---|
47 | n/a | if args: |
---|
48 | n/a | details = details % args |
---|
49 | n/a | cm = self.assertRaisesRegex(exc_type, details) |
---|
50 | n/a | with cm as exc: |
---|
51 | n/a | yield exc |
---|
52 | n/a | # Ensure we produce clean tracebacks on failure |
---|
53 | n/a | if exc.exception.__context__ is not None: |
---|
54 | n/a | self.assertTrue(exc.exception.__suppress_context__) |
---|
55 | n/a | |
---|
56 | n/a | def assertAddressError(self, details, *args): |
---|
57 | n/a | """Ensure a clean AddressValueError""" |
---|
58 | n/a | return self.assertCleanError(ipaddress.AddressValueError, |
---|
59 | n/a | details, *args) |
---|
60 | n/a | |
---|
61 | n/a | def assertNetmaskError(self, details, *args): |
---|
62 | n/a | """Ensure a clean NetmaskValueError""" |
---|
63 | n/a | return self.assertCleanError(ipaddress.NetmaskValueError, |
---|
64 | n/a | details, *args) |
---|
65 | n/a | |
---|
66 | n/a | def assertInstancesEqual(self, lhs, rhs): |
---|
67 | n/a | """Check constructor arguments produce equivalent instances""" |
---|
68 | n/a | self.assertEqual(self.factory(lhs), self.factory(rhs)) |
---|
69 | n/a | |
---|
70 | n/a | |
---|
71 | n/a | class CommonTestMixin: |
---|
72 | n/a | |
---|
73 | n/a | def test_empty_address(self): |
---|
74 | n/a | with self.assertAddressError("Address cannot be empty"): |
---|
75 | n/a | self.factory("") |
---|
76 | n/a | |
---|
77 | n/a | def test_floats_rejected(self): |
---|
78 | n/a | with self.assertAddressError(re.escape(repr("1.0"))): |
---|
79 | n/a | self.factory(1.0) |
---|
80 | n/a | |
---|
81 | n/a | def test_not_an_index_issue15559(self): |
---|
82 | n/a | # Implementing __index__ makes for a very nasty interaction with the |
---|
83 | n/a | # bytes constructor. Thus, we disallow implicit use as an integer |
---|
84 | n/a | self.assertRaises(TypeError, operator.index, self.factory(1)) |
---|
85 | n/a | self.assertRaises(TypeError, hex, self.factory(1)) |
---|
86 | n/a | self.assertRaises(TypeError, bytes, self.factory(1)) |
---|
87 | n/a | |
---|
88 | n/a | def pickle_test(self, addr): |
---|
89 | n/a | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
---|
90 | n/a | with self.subTest(proto=proto): |
---|
91 | n/a | x = self.factory(addr) |
---|
92 | n/a | y = pickle.loads(pickle.dumps(x, proto)) |
---|
93 | n/a | self.assertEqual(y, x) |
---|
94 | n/a | |
---|
95 | n/a | |
---|
96 | n/a | class CommonTestMixin_v4(CommonTestMixin): |
---|
97 | n/a | |
---|
98 | n/a | def test_leading_zeros(self): |
---|
99 | n/a | self.assertInstancesEqual("000.000.000.000", "0.0.0.0") |
---|
100 | n/a | self.assertInstancesEqual("192.168.000.001", "192.168.0.1") |
---|
101 | n/a | |
---|
102 | n/a | def test_int(self): |
---|
103 | n/a | self.assertInstancesEqual(0, "0.0.0.0") |
---|
104 | n/a | self.assertInstancesEqual(3232235521, "192.168.0.1") |
---|
105 | n/a | |
---|
106 | n/a | def test_packed(self): |
---|
107 | n/a | self.assertInstancesEqual(bytes.fromhex("00000000"), "0.0.0.0") |
---|
108 | n/a | self.assertInstancesEqual(bytes.fromhex("c0a80001"), "192.168.0.1") |
---|
109 | n/a | |
---|
110 | n/a | def test_negative_ints_rejected(self): |
---|
111 | n/a | msg = "-1 (< 0) is not permitted as an IPv4 address" |
---|
112 | n/a | with self.assertAddressError(re.escape(msg)): |
---|
113 | n/a | self.factory(-1) |
---|
114 | n/a | |
---|
115 | n/a | def test_large_ints_rejected(self): |
---|
116 | n/a | msg = "%d (>= 2**32) is not permitted as an IPv4 address" |
---|
117 | n/a | with self.assertAddressError(re.escape(msg % 2**32)): |
---|
118 | n/a | self.factory(2**32) |
---|
119 | n/a | |
---|
120 | n/a | def test_bad_packed_length(self): |
---|
121 | n/a | def assertBadLength(length): |
---|
122 | n/a | addr = b'\0' * length |
---|
123 | n/a | msg = "%r (len %d != 4) is not permitted as an IPv4 address" |
---|
124 | n/a | with self.assertAddressError(re.escape(msg % (addr, length))): |
---|
125 | n/a | self.factory(addr) |
---|
126 | n/a | |
---|
127 | n/a | assertBadLength(3) |
---|
128 | n/a | assertBadLength(5) |
---|
129 | n/a | |
---|
130 | n/a | |
---|
131 | n/a | class CommonTestMixin_v6(CommonTestMixin): |
---|
132 | n/a | |
---|
133 | n/a | def test_leading_zeros(self): |
---|
134 | n/a | self.assertInstancesEqual("0000::0000", "::") |
---|
135 | n/a | self.assertInstancesEqual("000::c0a8:0001", "::c0a8:1") |
---|
136 | n/a | |
---|
137 | n/a | def test_int(self): |
---|
138 | n/a | self.assertInstancesEqual(0, "::") |
---|
139 | n/a | self.assertInstancesEqual(3232235521, "::c0a8:1") |
---|
140 | n/a | |
---|
141 | n/a | def test_packed(self): |
---|
142 | n/a | addr = b'\0'*12 + bytes.fromhex("00000000") |
---|
143 | n/a | self.assertInstancesEqual(addr, "::") |
---|
144 | n/a | addr = b'\0'*12 + bytes.fromhex("c0a80001") |
---|
145 | n/a | self.assertInstancesEqual(addr, "::c0a8:1") |
---|
146 | n/a | addr = bytes.fromhex("c0a80001") + b'\0'*12 |
---|
147 | n/a | self.assertInstancesEqual(addr, "c0a8:1::") |
---|
148 | n/a | |
---|
149 | n/a | def test_negative_ints_rejected(self): |
---|
150 | n/a | msg = "-1 (< 0) is not permitted as an IPv6 address" |
---|
151 | n/a | with self.assertAddressError(re.escape(msg)): |
---|
152 | n/a | self.factory(-1) |
---|
153 | n/a | |
---|
154 | n/a | def test_large_ints_rejected(self): |
---|
155 | n/a | msg = "%d (>= 2**128) is not permitted as an IPv6 address" |
---|
156 | n/a | with self.assertAddressError(re.escape(msg % 2**128)): |
---|
157 | n/a | self.factory(2**128) |
---|
158 | n/a | |
---|
159 | n/a | def test_bad_packed_length(self): |
---|
160 | n/a | def assertBadLength(length): |
---|
161 | n/a | addr = b'\0' * length |
---|
162 | n/a | msg = "%r (len %d != 16) is not permitted as an IPv6 address" |
---|
163 | n/a | with self.assertAddressError(re.escape(msg % (addr, length))): |
---|
164 | n/a | self.factory(addr) |
---|
165 | n/a | self.factory(addr) |
---|
166 | n/a | |
---|
167 | n/a | assertBadLength(15) |
---|
168 | n/a | assertBadLength(17) |
---|
169 | n/a | |
---|
170 | n/a | |
---|
171 | n/a | class AddressTestCase_v4(BaseTestCase, CommonTestMixin_v4): |
---|
172 | n/a | factory = ipaddress.IPv4Address |
---|
173 | n/a | |
---|
174 | n/a | def test_network_passed_as_address(self): |
---|
175 | n/a | addr = "127.0.0.1/24" |
---|
176 | n/a | with self.assertAddressError("Unexpected '/' in %r", addr): |
---|
177 | n/a | ipaddress.IPv4Address(addr) |
---|
178 | n/a | |
---|
179 | n/a | def test_bad_address_split(self): |
---|
180 | n/a | def assertBadSplit(addr): |
---|
181 | n/a | with self.assertAddressError("Expected 4 octets in %r", addr): |
---|
182 | n/a | ipaddress.IPv4Address(addr) |
---|
183 | n/a | |
---|
184 | n/a | assertBadSplit("127.0.1") |
---|
185 | n/a | assertBadSplit("42.42.42.42.42") |
---|
186 | n/a | assertBadSplit("42.42.42") |
---|
187 | n/a | assertBadSplit("42.42") |
---|
188 | n/a | assertBadSplit("42") |
---|
189 | n/a | assertBadSplit("42..42.42.42") |
---|
190 | n/a | assertBadSplit("42.42.42.42.") |
---|
191 | n/a | assertBadSplit("42.42.42.42...") |
---|
192 | n/a | assertBadSplit(".42.42.42.42") |
---|
193 | n/a | assertBadSplit("...42.42.42.42") |
---|
194 | n/a | assertBadSplit("016.016.016") |
---|
195 | n/a | assertBadSplit("016.016") |
---|
196 | n/a | assertBadSplit("016") |
---|
197 | n/a | assertBadSplit("000") |
---|
198 | n/a | assertBadSplit("0x0a.0x0a.0x0a") |
---|
199 | n/a | assertBadSplit("0x0a.0x0a") |
---|
200 | n/a | assertBadSplit("0x0a") |
---|
201 | n/a | assertBadSplit(".") |
---|
202 | n/a | assertBadSplit("bogus") |
---|
203 | n/a | assertBadSplit("bogus.com") |
---|
204 | n/a | assertBadSplit("1000") |
---|
205 | n/a | assertBadSplit("1000000000000000") |
---|
206 | n/a | assertBadSplit("192.168.0.1.com") |
---|
207 | n/a | |
---|
208 | n/a | def test_empty_octet(self): |
---|
209 | n/a | def assertBadOctet(addr): |
---|
210 | n/a | with self.assertAddressError("Empty octet not permitted in %r", |
---|
211 | n/a | addr): |
---|
212 | n/a | ipaddress.IPv4Address(addr) |
---|
213 | n/a | |
---|
214 | n/a | assertBadOctet("42..42.42") |
---|
215 | n/a | assertBadOctet("...") |
---|
216 | n/a | |
---|
217 | n/a | def test_invalid_characters(self): |
---|
218 | n/a | def assertBadOctet(addr, octet): |
---|
219 | n/a | msg = "Only decimal digits permitted in %r in %r" % (octet, addr) |
---|
220 | n/a | with self.assertAddressError(re.escape(msg)): |
---|
221 | n/a | ipaddress.IPv4Address(addr) |
---|
222 | n/a | |
---|
223 | n/a | assertBadOctet("0x0a.0x0a.0x0a.0x0a", "0x0a") |
---|
224 | n/a | assertBadOctet("0xa.0x0a.0x0a.0x0a", "0xa") |
---|
225 | n/a | assertBadOctet("42.42.42.-0", "-0") |
---|
226 | n/a | assertBadOctet("42.42.42.+0", "+0") |
---|
227 | n/a | assertBadOctet("42.42.42.-42", "-42") |
---|
228 | n/a | assertBadOctet("+1.+2.+3.4", "+1") |
---|
229 | n/a | assertBadOctet("1.2.3.4e0", "4e0") |
---|
230 | n/a | assertBadOctet("1.2.3.4::", "4::") |
---|
231 | n/a | assertBadOctet("1.a.2.3", "a") |
---|
232 | n/a | |
---|
233 | n/a | def test_octal_decimal_ambiguity(self): |
---|
234 | n/a | def assertBadOctet(addr, octet): |
---|
235 | n/a | msg = "Ambiguous (octal/decimal) value in %r not permitted in %r" |
---|
236 | n/a | with self.assertAddressError(re.escape(msg % (octet, addr))): |
---|
237 | n/a | ipaddress.IPv4Address(addr) |
---|
238 | n/a | |
---|
239 | n/a | assertBadOctet("016.016.016.016", "016") |
---|
240 | n/a | assertBadOctet("001.000.008.016", "008") |
---|
241 | n/a | |
---|
242 | n/a | def test_octet_length(self): |
---|
243 | n/a | def assertBadOctet(addr, octet): |
---|
244 | n/a | msg = "At most 3 characters permitted in %r in %r" |
---|
245 | n/a | with self.assertAddressError(re.escape(msg % (octet, addr))): |
---|
246 | n/a | ipaddress.IPv4Address(addr) |
---|
247 | n/a | |
---|
248 | n/a | assertBadOctet("0000.000.000.000", "0000") |
---|
249 | n/a | assertBadOctet("12345.67899.-54321.-98765", "12345") |
---|
250 | n/a | |
---|
251 | n/a | def test_octet_limit(self): |
---|
252 | n/a | def assertBadOctet(addr, octet): |
---|
253 | n/a | msg = "Octet %d (> 255) not permitted in %r" % (octet, addr) |
---|
254 | n/a | with self.assertAddressError(re.escape(msg)): |
---|
255 | n/a | ipaddress.IPv4Address(addr) |
---|
256 | n/a | |
---|
257 | n/a | assertBadOctet("257.0.0.0", 257) |
---|
258 | n/a | assertBadOctet("192.168.0.999", 999) |
---|
259 | n/a | |
---|
260 | n/a | def test_pickle(self): |
---|
261 | n/a | self.pickle_test('192.0.2.1') |
---|
262 | n/a | |
---|
263 | n/a | def test_weakref(self): |
---|
264 | n/a | weakref.ref(self.factory('192.0.2.1')) |
---|
265 | n/a | |
---|
266 | n/a | |
---|
267 | n/a | class AddressTestCase_v6(BaseTestCase, CommonTestMixin_v6): |
---|
268 | n/a | factory = ipaddress.IPv6Address |
---|
269 | n/a | |
---|
270 | n/a | def test_network_passed_as_address(self): |
---|
271 | n/a | addr = "::1/24" |
---|
272 | n/a | with self.assertAddressError("Unexpected '/' in %r", addr): |
---|
273 | n/a | ipaddress.IPv6Address(addr) |
---|
274 | n/a | |
---|
275 | n/a | def test_bad_address_split_v6_not_enough_parts(self): |
---|
276 | n/a | def assertBadSplit(addr): |
---|
277 | n/a | msg = "At least 3 parts expected in %r" |
---|
278 | n/a | with self.assertAddressError(msg, addr): |
---|
279 | n/a | ipaddress.IPv6Address(addr) |
---|
280 | n/a | |
---|
281 | n/a | assertBadSplit(":") |
---|
282 | n/a | assertBadSplit(":1") |
---|
283 | n/a | assertBadSplit("FEDC:9878") |
---|
284 | n/a | |
---|
285 | n/a | def test_bad_address_split_v6_too_many_colons(self): |
---|
286 | n/a | def assertBadSplit(addr): |
---|
287 | n/a | msg = "At most 8 colons permitted in %r" |
---|
288 | n/a | with self.assertAddressError(msg, addr): |
---|
289 | n/a | ipaddress.IPv6Address(addr) |
---|
290 | n/a | |
---|
291 | n/a | assertBadSplit("9:8:7:6:5:4:3::2:1") |
---|
292 | n/a | assertBadSplit("10:9:8:7:6:5:4:3:2:1") |
---|
293 | n/a | assertBadSplit("::8:7:6:5:4:3:2:1") |
---|
294 | n/a | assertBadSplit("8:7:6:5:4:3:2:1::") |
---|
295 | n/a | # A trailing IPv4 address is two parts |
---|
296 | n/a | assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42") |
---|
297 | n/a | |
---|
298 | n/a | def test_bad_address_split_v6_too_many_parts(self): |
---|
299 | n/a | def assertBadSplit(addr): |
---|
300 | n/a | msg = "Exactly 8 parts expected without '::' in %r" |
---|
301 | n/a | with self.assertAddressError(msg, addr): |
---|
302 | n/a | ipaddress.IPv6Address(addr) |
---|
303 | n/a | |
---|
304 | n/a | assertBadSplit("3ffe:0:0:0:0:0:0:0:1") |
---|
305 | n/a | assertBadSplit("9:8:7:6:5:4:3:2:1") |
---|
306 | n/a | assertBadSplit("7:6:5:4:3:2:1") |
---|
307 | n/a | # A trailing IPv4 address is two parts |
---|
308 | n/a | assertBadSplit("9:8:7:6:5:4:3:42.42.42.42") |
---|
309 | n/a | assertBadSplit("7:6:5:4:3:42.42.42.42") |
---|
310 | n/a | |
---|
311 | n/a | def test_bad_address_split_v6_too_many_parts_with_double_colon(self): |
---|
312 | n/a | def assertBadSplit(addr): |
---|
313 | n/a | msg = "Expected at most 7 other parts with '::' in %r" |
---|
314 | n/a | with self.assertAddressError(msg, addr): |
---|
315 | n/a | ipaddress.IPv6Address(addr) |
---|
316 | n/a | |
---|
317 | n/a | assertBadSplit("1:2:3:4::5:6:7:8") |
---|
318 | n/a | |
---|
319 | n/a | def test_bad_address_split_v6_repeated_double_colon(self): |
---|
320 | n/a | def assertBadSplit(addr): |
---|
321 | n/a | msg = "At most one '::' permitted in %r" |
---|
322 | n/a | with self.assertAddressError(msg, addr): |
---|
323 | n/a | ipaddress.IPv6Address(addr) |
---|
324 | n/a | |
---|
325 | n/a | assertBadSplit("3ffe::1::1") |
---|
326 | n/a | assertBadSplit("1::2::3::4:5") |
---|
327 | n/a | assertBadSplit("2001::db:::1") |
---|
328 | n/a | assertBadSplit("3ffe::1::") |
---|
329 | n/a | assertBadSplit("::3ffe::1") |
---|
330 | n/a | assertBadSplit(":3ffe::1::1") |
---|
331 | n/a | assertBadSplit("3ffe::1::1:") |
---|
332 | n/a | assertBadSplit(":3ffe::1::1:") |
---|
333 | n/a | assertBadSplit(":::") |
---|
334 | n/a | assertBadSplit('2001:db8:::1') |
---|
335 | n/a | |
---|
336 | n/a | def test_bad_address_split_v6_leading_colon(self): |
---|
337 | n/a | def assertBadSplit(addr): |
---|
338 | n/a | msg = "Leading ':' only permitted as part of '::' in %r" |
---|
339 | n/a | with self.assertAddressError(msg, addr): |
---|
340 | n/a | ipaddress.IPv6Address(addr) |
---|
341 | n/a | |
---|
342 | n/a | assertBadSplit(":2001:db8::1") |
---|
343 | n/a | assertBadSplit(":1:2:3:4:5:6:7") |
---|
344 | n/a | assertBadSplit(":1:2:3:4:5:6:") |
---|
345 | n/a | assertBadSplit(":6:5:4:3:2:1::") |
---|
346 | n/a | |
---|
347 | n/a | def test_bad_address_split_v6_trailing_colon(self): |
---|
348 | n/a | def assertBadSplit(addr): |
---|
349 | n/a | msg = "Trailing ':' only permitted as part of '::' in %r" |
---|
350 | n/a | with self.assertAddressError(msg, addr): |
---|
351 | n/a | ipaddress.IPv6Address(addr) |
---|
352 | n/a | |
---|
353 | n/a | assertBadSplit("2001:db8::1:") |
---|
354 | n/a | assertBadSplit("1:2:3:4:5:6:7:") |
---|
355 | n/a | assertBadSplit("::1.2.3.4:") |
---|
356 | n/a | assertBadSplit("::7:6:5:4:3:2:") |
---|
357 | n/a | |
---|
358 | n/a | def test_bad_v4_part_in(self): |
---|
359 | n/a | def assertBadAddressPart(addr, v4_error): |
---|
360 | n/a | with self.assertAddressError("%s in %r", v4_error, addr): |
---|
361 | n/a | ipaddress.IPv6Address(addr) |
---|
362 | n/a | |
---|
363 | n/a | assertBadAddressPart("3ffe::1.net", "Expected 4 octets in '1.net'") |
---|
364 | n/a | assertBadAddressPart("3ffe::127.0.1", |
---|
365 | n/a | "Expected 4 octets in '127.0.1'") |
---|
366 | n/a | assertBadAddressPart("::1.2.3", |
---|
367 | n/a | "Expected 4 octets in '1.2.3'") |
---|
368 | n/a | assertBadAddressPart("::1.2.3.4.5", |
---|
369 | n/a | "Expected 4 octets in '1.2.3.4.5'") |
---|
370 | n/a | assertBadAddressPart("3ffe::1.1.1.net", |
---|
371 | n/a | "Only decimal digits permitted in 'net' " |
---|
372 | n/a | "in '1.1.1.net'") |
---|
373 | n/a | |
---|
374 | n/a | def test_invalid_characters(self): |
---|
375 | n/a | def assertBadPart(addr, part): |
---|
376 | n/a | msg = "Only hex digits permitted in %r in %r" % (part, addr) |
---|
377 | n/a | with self.assertAddressError(re.escape(msg)): |
---|
378 | n/a | ipaddress.IPv6Address(addr) |
---|
379 | n/a | |
---|
380 | n/a | assertBadPart("3ffe::goog", "goog") |
---|
381 | n/a | assertBadPart("3ffe::-0", "-0") |
---|
382 | n/a | assertBadPart("3ffe::+0", "+0") |
---|
383 | n/a | assertBadPart("3ffe::-1", "-1") |
---|
384 | n/a | assertBadPart("1.2.3.4::", "1.2.3.4") |
---|
385 | n/a | assertBadPart('1234:axy::b', "axy") |
---|
386 | n/a | |
---|
387 | n/a | def test_part_length(self): |
---|
388 | n/a | def assertBadPart(addr, part): |
---|
389 | n/a | msg = "At most 4 characters permitted in %r in %r" |
---|
390 | n/a | with self.assertAddressError(msg, part, addr): |
---|
391 | n/a | ipaddress.IPv6Address(addr) |
---|
392 | n/a | |
---|
393 | n/a | assertBadPart("::00000", "00000") |
---|
394 | n/a | assertBadPart("3ffe::10000", "10000") |
---|
395 | n/a | assertBadPart("02001:db8::", "02001") |
---|
396 | n/a | assertBadPart('2001:888888::1', "888888") |
---|
397 | n/a | |
---|
398 | n/a | def test_pickle(self): |
---|
399 | n/a | self.pickle_test('2001:db8::') |
---|
400 | n/a | |
---|
401 | n/a | def test_weakref(self): |
---|
402 | n/a | weakref.ref(self.factory('2001:db8::')) |
---|
403 | n/a | |
---|
404 | n/a | |
---|
405 | n/a | class NetmaskTestMixin_v4(CommonTestMixin_v4): |
---|
406 | n/a | """Input validation on interfaces and networks is very similar""" |
---|
407 | n/a | |
---|
408 | n/a | def test_split_netmask(self): |
---|
409 | n/a | addr = "1.2.3.4/32/24" |
---|
410 | n/a | with self.assertAddressError("Only one '/' permitted in %r" % addr): |
---|
411 | n/a | self.factory(addr) |
---|
412 | n/a | |
---|
413 | n/a | def test_address_errors(self): |
---|
414 | n/a | def assertBadAddress(addr, details): |
---|
415 | n/a | with self.assertAddressError(details): |
---|
416 | n/a | self.factory(addr) |
---|
417 | n/a | |
---|
418 | n/a | assertBadAddress("/", "Address cannot be empty") |
---|
419 | n/a | assertBadAddress("/8", "Address cannot be empty") |
---|
420 | n/a | assertBadAddress("bogus", "Expected 4 octets") |
---|
421 | n/a | assertBadAddress("google.com", "Expected 4 octets") |
---|
422 | n/a | assertBadAddress("10/8", "Expected 4 octets") |
---|
423 | n/a | assertBadAddress("::1.2.3.4", "Only decimal digits") |
---|
424 | n/a | assertBadAddress("1.2.3.256", re.escape("256 (> 255)")) |
---|
425 | n/a | |
---|
426 | n/a | def test_valid_netmask(self): |
---|
427 | n/a | self.assertEqual(str(self.factory('192.0.2.0/255.255.255.0')), |
---|
428 | n/a | '192.0.2.0/24') |
---|
429 | n/a | for i in range(0, 33): |
---|
430 | n/a | # Generate and re-parse the CIDR format (trivial). |
---|
431 | n/a | net_str = '0.0.0.0/%d' % i |
---|
432 | n/a | net = self.factory(net_str) |
---|
433 | n/a | self.assertEqual(str(net), net_str) |
---|
434 | n/a | # Generate and re-parse the expanded netmask. |
---|
435 | n/a | self.assertEqual( |
---|
436 | n/a | str(self.factory('0.0.0.0/%s' % net.netmask)), net_str) |
---|
437 | n/a | # Zero prefix is treated as decimal. |
---|
438 | n/a | self.assertEqual(str(self.factory('0.0.0.0/0%d' % i)), net_str) |
---|
439 | n/a | # Generate and re-parse the expanded hostmask. The ambiguous |
---|
440 | n/a | # cases (/0 and /32) are treated as netmasks. |
---|
441 | n/a | if i in (32, 0): |
---|
442 | n/a | net_str = '0.0.0.0/%d' % (32 - i) |
---|
443 | n/a | self.assertEqual( |
---|
444 | n/a | str(self.factory('0.0.0.0/%s' % net.hostmask)), net_str) |
---|
445 | n/a | |
---|
446 | n/a | def test_netmask_errors(self): |
---|
447 | n/a | def assertBadNetmask(addr, netmask): |
---|
448 | n/a | msg = "%r is not a valid netmask" % netmask |
---|
449 | n/a | with self.assertNetmaskError(re.escape(msg)): |
---|
450 | n/a | self.factory("%s/%s" % (addr, netmask)) |
---|
451 | n/a | |
---|
452 | n/a | assertBadNetmask("1.2.3.4", "") |
---|
453 | n/a | assertBadNetmask("1.2.3.4", "-1") |
---|
454 | n/a | assertBadNetmask("1.2.3.4", "+1") |
---|
455 | n/a | assertBadNetmask("1.2.3.4", " 1 ") |
---|
456 | n/a | assertBadNetmask("1.2.3.4", "0x1") |
---|
457 | n/a | assertBadNetmask("1.2.3.4", "33") |
---|
458 | n/a | assertBadNetmask("1.2.3.4", "254.254.255.256") |
---|
459 | n/a | assertBadNetmask("1.2.3.4", "1.a.2.3") |
---|
460 | n/a | assertBadNetmask("1.1.1.1", "254.xyz.2.3") |
---|
461 | n/a | assertBadNetmask("1.1.1.1", "240.255.0.0") |
---|
462 | n/a | assertBadNetmask("1.1.1.1", "255.254.128.0") |
---|
463 | n/a | assertBadNetmask("1.1.1.1", "0.1.127.255") |
---|
464 | n/a | assertBadNetmask("1.1.1.1", "pudding") |
---|
465 | n/a | assertBadNetmask("1.1.1.1", "::") |
---|
466 | n/a | |
---|
467 | n/a | def test_pickle(self): |
---|
468 | n/a | self.pickle_test('192.0.2.0/27') |
---|
469 | n/a | self.pickle_test('192.0.2.0/31') # IPV4LENGTH - 1 |
---|
470 | n/a | self.pickle_test('192.0.2.0') # IPV4LENGTH |
---|
471 | n/a | |
---|
472 | n/a | |
---|
473 | n/a | class InterfaceTestCase_v4(BaseTestCase, NetmaskTestMixin_v4): |
---|
474 | n/a | factory = ipaddress.IPv4Interface |
---|
475 | n/a | |
---|
476 | n/a | |
---|
477 | n/a | class NetworkTestCase_v4(BaseTestCase, NetmaskTestMixin_v4): |
---|
478 | n/a | factory = ipaddress.IPv4Network |
---|
479 | n/a | |
---|
480 | n/a | |
---|
481 | n/a | class NetmaskTestMixin_v6(CommonTestMixin_v6): |
---|
482 | n/a | """Input validation on interfaces and networks is very similar""" |
---|
483 | n/a | |
---|
484 | n/a | def test_split_netmask(self): |
---|
485 | n/a | addr = "cafe:cafe::/128/190" |
---|
486 | n/a | with self.assertAddressError("Only one '/' permitted in %r" % addr): |
---|
487 | n/a | self.factory(addr) |
---|
488 | n/a | |
---|
489 | n/a | def test_address_errors(self): |
---|
490 | n/a | def assertBadAddress(addr, details): |
---|
491 | n/a | with self.assertAddressError(details): |
---|
492 | n/a | self.factory(addr) |
---|
493 | n/a | |
---|
494 | n/a | assertBadAddress("/", "Address cannot be empty") |
---|
495 | n/a | assertBadAddress("/8", "Address cannot be empty") |
---|
496 | n/a | assertBadAddress("google.com", "At least 3 parts") |
---|
497 | n/a | assertBadAddress("1.2.3.4", "At least 3 parts") |
---|
498 | n/a | assertBadAddress("10/8", "At least 3 parts") |
---|
499 | n/a | assertBadAddress("1234:axy::b", "Only hex digits") |
---|
500 | n/a | |
---|
501 | n/a | def test_valid_netmask(self): |
---|
502 | n/a | # We only support CIDR for IPv6, because expanded netmasks are not |
---|
503 | n/a | # standard notation. |
---|
504 | n/a | self.assertEqual(str(self.factory('2001:db8::/32')), '2001:db8::/32') |
---|
505 | n/a | for i in range(0, 129): |
---|
506 | n/a | # Generate and re-parse the CIDR format (trivial). |
---|
507 | n/a | net_str = '::/%d' % i |
---|
508 | n/a | self.assertEqual(str(self.factory(net_str)), net_str) |
---|
509 | n/a | # Zero prefix is treated as decimal. |
---|
510 | n/a | self.assertEqual(str(self.factory('::/0%d' % i)), net_str) |
---|
511 | n/a | |
---|
512 | n/a | def test_netmask_errors(self): |
---|
513 | n/a | def assertBadNetmask(addr, netmask): |
---|
514 | n/a | msg = "%r is not a valid netmask" % netmask |
---|
515 | n/a | with self.assertNetmaskError(re.escape(msg)): |
---|
516 | n/a | self.factory("%s/%s" % (addr, netmask)) |
---|
517 | n/a | |
---|
518 | n/a | assertBadNetmask("::1", "") |
---|
519 | n/a | assertBadNetmask("::1", "::1") |
---|
520 | n/a | assertBadNetmask("::1", "1::") |
---|
521 | n/a | assertBadNetmask("::1", "-1") |
---|
522 | n/a | assertBadNetmask("::1", "+1") |
---|
523 | n/a | assertBadNetmask("::1", " 1 ") |
---|
524 | n/a | assertBadNetmask("::1", "0x1") |
---|
525 | n/a | assertBadNetmask("::1", "129") |
---|
526 | n/a | assertBadNetmask("::1", "1.2.3.4") |
---|
527 | n/a | assertBadNetmask("::1", "pudding") |
---|
528 | n/a | assertBadNetmask("::", "::") |
---|
529 | n/a | |
---|
530 | n/a | def test_pickle(self): |
---|
531 | n/a | self.pickle_test('2001:db8::1000/124') |
---|
532 | n/a | self.pickle_test('2001:db8::1000/127') # IPV6LENGTH - 1 |
---|
533 | n/a | self.pickle_test('2001:db8::1000') # IPV6LENGTH |
---|
534 | n/a | |
---|
535 | n/a | |
---|
536 | n/a | class InterfaceTestCase_v6(BaseTestCase, NetmaskTestMixin_v6): |
---|
537 | n/a | factory = ipaddress.IPv6Interface |
---|
538 | n/a | |
---|
539 | n/a | |
---|
540 | n/a | class NetworkTestCase_v6(BaseTestCase, NetmaskTestMixin_v6): |
---|
541 | n/a | factory = ipaddress.IPv6Network |
---|
542 | n/a | |
---|
543 | n/a | |
---|
544 | n/a | class FactoryFunctionErrors(BaseTestCase): |
---|
545 | n/a | |
---|
546 | n/a | def assertFactoryError(self, factory, kind): |
---|
547 | n/a | """Ensure a clean ValueError with the expected message""" |
---|
548 | n/a | addr = "camelot" |
---|
549 | n/a | msg = '%r does not appear to be an IPv4 or IPv6 %s' |
---|
550 | n/a | with self.assertCleanError(ValueError, msg, addr, kind): |
---|
551 | n/a | factory(addr) |
---|
552 | n/a | |
---|
553 | n/a | def test_ip_address(self): |
---|
554 | n/a | self.assertFactoryError(ipaddress.ip_address, "address") |
---|
555 | n/a | |
---|
556 | n/a | def test_ip_interface(self): |
---|
557 | n/a | self.assertFactoryError(ipaddress.ip_interface, "interface") |
---|
558 | n/a | |
---|
559 | n/a | def test_ip_network(self): |
---|
560 | n/a | self.assertFactoryError(ipaddress.ip_network, "network") |
---|
561 | n/a | |
---|
562 | n/a | |
---|
563 | n/a | @functools.total_ordering |
---|
564 | n/a | class LargestObject: |
---|
565 | n/a | def __eq__(self, other): |
---|
566 | n/a | return isinstance(other, LargestObject) |
---|
567 | n/a | def __lt__(self, other): |
---|
568 | n/a | return False |
---|
569 | n/a | |
---|
570 | n/a | @functools.total_ordering |
---|
571 | n/a | class SmallestObject: |
---|
572 | n/a | def __eq__(self, other): |
---|
573 | n/a | return isinstance(other, SmallestObject) |
---|
574 | n/a | def __gt__(self, other): |
---|
575 | n/a | return False |
---|
576 | n/a | |
---|
577 | n/a | class ComparisonTests(unittest.TestCase): |
---|
578 | n/a | |
---|
579 | n/a | v4addr = ipaddress.IPv4Address(1) |
---|
580 | n/a | v4net = ipaddress.IPv4Network(1) |
---|
581 | n/a | v4intf = ipaddress.IPv4Interface(1) |
---|
582 | n/a | v6addr = ipaddress.IPv6Address(1) |
---|
583 | n/a | v6net = ipaddress.IPv6Network(1) |
---|
584 | n/a | v6intf = ipaddress.IPv6Interface(1) |
---|
585 | n/a | |
---|
586 | n/a | v4_addresses = [v4addr, v4intf] |
---|
587 | n/a | v4_objects = v4_addresses + [v4net] |
---|
588 | n/a | v6_addresses = [v6addr, v6intf] |
---|
589 | n/a | v6_objects = v6_addresses + [v6net] |
---|
590 | n/a | |
---|
591 | n/a | objects = v4_objects + v6_objects |
---|
592 | n/a | |
---|
593 | n/a | v4addr2 = ipaddress.IPv4Address(2) |
---|
594 | n/a | v4net2 = ipaddress.IPv4Network(2) |
---|
595 | n/a | v4intf2 = ipaddress.IPv4Interface(2) |
---|
596 | n/a | v6addr2 = ipaddress.IPv6Address(2) |
---|
597 | n/a | v6net2 = ipaddress.IPv6Network(2) |
---|
598 | n/a | v6intf2 = ipaddress.IPv6Interface(2) |
---|
599 | n/a | |
---|
600 | n/a | def test_foreign_type_equality(self): |
---|
601 | n/a | # __eq__ should never raise TypeError directly |
---|
602 | n/a | other = object() |
---|
603 | n/a | for obj in self.objects: |
---|
604 | n/a | self.assertNotEqual(obj, other) |
---|
605 | n/a | self.assertFalse(obj == other) |
---|
606 | n/a | self.assertEqual(obj.__eq__(other), NotImplemented) |
---|
607 | n/a | self.assertEqual(obj.__ne__(other), NotImplemented) |
---|
608 | n/a | |
---|
609 | n/a | def test_mixed_type_equality(self): |
---|
610 | n/a | # Ensure none of the internal objects accidentally |
---|
611 | n/a | # expose the right set of attributes to become "equal" |
---|
612 | n/a | for lhs in self.objects: |
---|
613 | n/a | for rhs in self.objects: |
---|
614 | n/a | if lhs is rhs: |
---|
615 | n/a | continue |
---|
616 | n/a | self.assertNotEqual(lhs, rhs) |
---|
617 | n/a | |
---|
618 | n/a | def test_same_type_equality(self): |
---|
619 | n/a | for obj in self.objects: |
---|
620 | n/a | self.assertEqual(obj, obj) |
---|
621 | n/a | self.assertLessEqual(obj, obj) |
---|
622 | n/a | self.assertGreaterEqual(obj, obj) |
---|
623 | n/a | |
---|
624 | n/a | def test_same_type_ordering(self): |
---|
625 | n/a | for lhs, rhs in ( |
---|
626 | n/a | (self.v4addr, self.v4addr2), |
---|
627 | n/a | (self.v4net, self.v4net2), |
---|
628 | n/a | (self.v4intf, self.v4intf2), |
---|
629 | n/a | (self.v6addr, self.v6addr2), |
---|
630 | n/a | (self.v6net, self.v6net2), |
---|
631 | n/a | (self.v6intf, self.v6intf2), |
---|
632 | n/a | ): |
---|
633 | n/a | self.assertNotEqual(lhs, rhs) |
---|
634 | n/a | self.assertLess(lhs, rhs) |
---|
635 | n/a | self.assertLessEqual(lhs, rhs) |
---|
636 | n/a | self.assertGreater(rhs, lhs) |
---|
637 | n/a | self.assertGreaterEqual(rhs, lhs) |
---|
638 | n/a | self.assertFalse(lhs > rhs) |
---|
639 | n/a | self.assertFalse(rhs < lhs) |
---|
640 | n/a | self.assertFalse(lhs >= rhs) |
---|
641 | n/a | self.assertFalse(rhs <= lhs) |
---|
642 | n/a | |
---|
643 | n/a | def test_containment(self): |
---|
644 | n/a | for obj in self.v4_addresses: |
---|
645 | n/a | self.assertIn(obj, self.v4net) |
---|
646 | n/a | for obj in self.v6_addresses: |
---|
647 | n/a | self.assertIn(obj, self.v6net) |
---|
648 | n/a | for obj in self.v4_objects + [self.v6net]: |
---|
649 | n/a | self.assertNotIn(obj, self.v6net) |
---|
650 | n/a | for obj in self.v6_objects + [self.v4net]: |
---|
651 | n/a | self.assertNotIn(obj, self.v4net) |
---|
652 | n/a | |
---|
653 | n/a | def test_mixed_type_ordering(self): |
---|
654 | n/a | for lhs in self.objects: |
---|
655 | n/a | for rhs in self.objects: |
---|
656 | n/a | if isinstance(lhs, type(rhs)) or isinstance(rhs, type(lhs)): |
---|
657 | n/a | continue |
---|
658 | n/a | self.assertRaises(TypeError, lambda: lhs < rhs) |
---|
659 | n/a | self.assertRaises(TypeError, lambda: lhs > rhs) |
---|
660 | n/a | self.assertRaises(TypeError, lambda: lhs <= rhs) |
---|
661 | n/a | self.assertRaises(TypeError, lambda: lhs >= rhs) |
---|
662 | n/a | |
---|
663 | n/a | def test_foreign_type_ordering(self): |
---|
664 | n/a | other = object() |
---|
665 | n/a | smallest = SmallestObject() |
---|
666 | n/a | largest = LargestObject() |
---|
667 | n/a | for obj in self.objects: |
---|
668 | n/a | with self.assertRaises(TypeError): |
---|
669 | n/a | obj < other |
---|
670 | n/a | with self.assertRaises(TypeError): |
---|
671 | n/a | obj > other |
---|
672 | n/a | with self.assertRaises(TypeError): |
---|
673 | n/a | obj <= other |
---|
674 | n/a | with self.assertRaises(TypeError): |
---|
675 | n/a | obj >= other |
---|
676 | n/a | self.assertTrue(obj < largest) |
---|
677 | n/a | self.assertFalse(obj > largest) |
---|
678 | n/a | self.assertTrue(obj <= largest) |
---|
679 | n/a | self.assertFalse(obj >= largest) |
---|
680 | n/a | self.assertFalse(obj < smallest) |
---|
681 | n/a | self.assertTrue(obj > smallest) |
---|
682 | n/a | self.assertFalse(obj <= smallest) |
---|
683 | n/a | self.assertTrue(obj >= smallest) |
---|
684 | n/a | |
---|
685 | n/a | def test_mixed_type_key(self): |
---|
686 | n/a | # with get_mixed_type_key, you can sort addresses and network. |
---|
687 | n/a | v4_ordered = [self.v4addr, self.v4net, self.v4intf] |
---|
688 | n/a | v6_ordered = [self.v6addr, self.v6net, self.v6intf] |
---|
689 | n/a | self.assertEqual(v4_ordered, |
---|
690 | n/a | sorted(self.v4_objects, |
---|
691 | n/a | key=ipaddress.get_mixed_type_key)) |
---|
692 | n/a | self.assertEqual(v6_ordered, |
---|
693 | n/a | sorted(self.v6_objects, |
---|
694 | n/a | key=ipaddress.get_mixed_type_key)) |
---|
695 | n/a | self.assertEqual(v4_ordered + v6_ordered, |
---|
696 | n/a | sorted(self.objects, |
---|
697 | n/a | key=ipaddress.get_mixed_type_key)) |
---|
698 | n/a | self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object)) |
---|
699 | n/a | |
---|
700 | n/a | def test_incompatible_versions(self): |
---|
701 | n/a | # These should always raise TypeError |
---|
702 | n/a | v4addr = ipaddress.ip_address('1.1.1.1') |
---|
703 | n/a | v4net = ipaddress.ip_network('1.1.1.1') |
---|
704 | n/a | v6addr = ipaddress.ip_address('::1') |
---|
705 | n/a | v6net = ipaddress.ip_network('::1') |
---|
706 | n/a | |
---|
707 | n/a | self.assertRaises(TypeError, v4addr.__lt__, v6addr) |
---|
708 | n/a | self.assertRaises(TypeError, v4addr.__gt__, v6addr) |
---|
709 | n/a | self.assertRaises(TypeError, v4net.__lt__, v6net) |
---|
710 | n/a | self.assertRaises(TypeError, v4net.__gt__, v6net) |
---|
711 | n/a | |
---|
712 | n/a | self.assertRaises(TypeError, v6addr.__lt__, v4addr) |
---|
713 | n/a | self.assertRaises(TypeError, v6addr.__gt__, v4addr) |
---|
714 | n/a | self.assertRaises(TypeError, v6net.__lt__, v4net) |
---|
715 | n/a | self.assertRaises(TypeError, v6net.__gt__, v4net) |
---|
716 | n/a | |
---|
717 | n/a | |
---|
718 | n/a | class IpaddrUnitTest(unittest.TestCase): |
---|
719 | n/a | |
---|
720 | n/a | def setUp(self): |
---|
721 | n/a | self.ipv4_address = ipaddress.IPv4Address('1.2.3.4') |
---|
722 | n/a | self.ipv4_interface = ipaddress.IPv4Interface('1.2.3.4/24') |
---|
723 | n/a | self.ipv4_network = ipaddress.IPv4Network('1.2.3.0/24') |
---|
724 | n/a | #self.ipv4_hostmask = ipaddress.IPv4Interface('10.0.0.1/0.255.255.255') |
---|
725 | n/a | self.ipv6_address = ipaddress.IPv6Interface( |
---|
726 | n/a | '2001:658:22a:cafe:200:0:0:1') |
---|
727 | n/a | self.ipv6_interface = ipaddress.IPv6Interface( |
---|
728 | n/a | '2001:658:22a:cafe:200:0:0:1/64') |
---|
729 | n/a | self.ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/64') |
---|
730 | n/a | |
---|
731 | n/a | def testRepr(self): |
---|
732 | n/a | self.assertEqual("IPv4Interface('1.2.3.4/32')", |
---|
733 | n/a | repr(ipaddress.IPv4Interface('1.2.3.4'))) |
---|
734 | n/a | self.assertEqual("IPv6Interface('::1/128')", |
---|
735 | n/a | repr(ipaddress.IPv6Interface('::1'))) |
---|
736 | n/a | |
---|
737 | n/a | # issue #16531: constructing IPv4Network from an (address, mask) tuple |
---|
738 | n/a | def testIPv4Tuple(self): |
---|
739 | n/a | # /32 |
---|
740 | n/a | ip = ipaddress.IPv4Address('192.0.2.1') |
---|
741 | n/a | net = ipaddress.IPv4Network('192.0.2.1/32') |
---|
742 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', 32)), net) |
---|
743 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, 32)), net) |
---|
744 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225985, 32)), net) |
---|
745 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', |
---|
746 | n/a | '255.255.255.255')), net) |
---|
747 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, |
---|
748 | n/a | '255.255.255.255')), net) |
---|
749 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225985, |
---|
750 | n/a | '255.255.255.255')), net) |
---|
751 | n/a | # strict=True and host bits set |
---|
752 | n/a | with self.assertRaises(ValueError): |
---|
753 | n/a | ipaddress.IPv4Network(('192.0.2.1', 24)) |
---|
754 | n/a | with self.assertRaises(ValueError): |
---|
755 | n/a | ipaddress.IPv4Network((ip, 24)) |
---|
756 | n/a | with self.assertRaises(ValueError): |
---|
757 | n/a | ipaddress.IPv4Network((3221225985, 24)) |
---|
758 | n/a | with self.assertRaises(ValueError): |
---|
759 | n/a | ipaddress.IPv4Network(('192.0.2.1', '255.255.255.0')) |
---|
760 | n/a | with self.assertRaises(ValueError): |
---|
761 | n/a | ipaddress.IPv4Network((ip, '255.255.255.0')) |
---|
762 | n/a | with self.assertRaises(ValueError): |
---|
763 | n/a | ipaddress.IPv4Network((3221225985, '255.255.255.0')) |
---|
764 | n/a | # strict=False and host bits set |
---|
765 | n/a | net = ipaddress.IPv4Network('192.0.2.0/24') |
---|
766 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', 24), |
---|
767 | n/a | strict=False), net) |
---|
768 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, 24), |
---|
769 | n/a | strict=False), net) |
---|
770 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225985, 24), |
---|
771 | n/a | strict=False), net) |
---|
772 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.1', |
---|
773 | n/a | '255.255.255.0'), |
---|
774 | n/a | strict=False), net) |
---|
775 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, |
---|
776 | n/a | '255.255.255.0'), |
---|
777 | n/a | strict=False), net) |
---|
778 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225985, |
---|
779 | n/a | '255.255.255.0'), |
---|
780 | n/a | strict=False), net) |
---|
781 | n/a | |
---|
782 | n/a | # /24 |
---|
783 | n/a | ip = ipaddress.IPv4Address('192.0.2.0') |
---|
784 | n/a | net = ipaddress.IPv4Network('192.0.2.0/24') |
---|
785 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.0', |
---|
786 | n/a | '255.255.255.0')), net) |
---|
787 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, |
---|
788 | n/a | '255.255.255.0')), net) |
---|
789 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225984, |
---|
790 | n/a | '255.255.255.0')), net) |
---|
791 | n/a | self.assertEqual(ipaddress.IPv4Network(('192.0.2.0', 24)), net) |
---|
792 | n/a | self.assertEqual(ipaddress.IPv4Network((ip, 24)), net) |
---|
793 | n/a | self.assertEqual(ipaddress.IPv4Network((3221225984, 24)), net) |
---|
794 | n/a | |
---|
795 | n/a | self.assertEqual(ipaddress.IPv4Interface(('192.0.2.1', 24)), |
---|
796 | n/a | ipaddress.IPv4Interface('192.0.2.1/24')) |
---|
797 | n/a | self.assertEqual(ipaddress.IPv4Interface((3221225985, 24)), |
---|
798 | n/a | ipaddress.IPv4Interface('192.0.2.1/24')) |
---|
799 | n/a | |
---|
800 | n/a | # issue #16531: constructing IPv6Network from an (address, mask) tuple |
---|
801 | n/a | def testIPv6Tuple(self): |
---|
802 | n/a | # /128 |
---|
803 | n/a | ip = ipaddress.IPv6Address('2001:db8::') |
---|
804 | n/a | net = ipaddress.IPv6Network('2001:db8::/128') |
---|
805 | n/a | self.assertEqual(ipaddress.IPv6Network(('2001:db8::', '128')), |
---|
806 | n/a | net) |
---|
807 | n/a | self.assertEqual(ipaddress.IPv6Network( |
---|
808 | n/a | (42540766411282592856903984951653826560, 128)), |
---|
809 | n/a | net) |
---|
810 | n/a | self.assertEqual(ipaddress.IPv6Network((ip, '128')), |
---|
811 | n/a | net) |
---|
812 | n/a | ip = ipaddress.IPv6Address('2001:db8::') |
---|
813 | n/a | net = ipaddress.IPv6Network('2001:db8::/96') |
---|
814 | n/a | self.assertEqual(ipaddress.IPv6Network(('2001:db8::', '96')), |
---|
815 | n/a | net) |
---|
816 | n/a | self.assertEqual(ipaddress.IPv6Network( |
---|
817 | n/a | (42540766411282592856903984951653826560, 96)), |
---|
818 | n/a | net) |
---|
819 | n/a | self.assertEqual(ipaddress.IPv6Network((ip, '96')), |
---|
820 | n/a | net) |
---|
821 | n/a | |
---|
822 | n/a | # strict=True and host bits set |
---|
823 | n/a | ip = ipaddress.IPv6Address('2001:db8::1') |
---|
824 | n/a | with self.assertRaises(ValueError): |
---|
825 | n/a | ipaddress.IPv6Network(('2001:db8::1', 96)) |
---|
826 | n/a | with self.assertRaises(ValueError): |
---|
827 | n/a | ipaddress.IPv6Network(( |
---|
828 | n/a | 42540766411282592856903984951653826561, 96)) |
---|
829 | n/a | with self.assertRaises(ValueError): |
---|
830 | n/a | ipaddress.IPv6Network((ip, 96)) |
---|
831 | n/a | # strict=False and host bits set |
---|
832 | n/a | net = ipaddress.IPv6Network('2001:db8::/96') |
---|
833 | n/a | self.assertEqual(ipaddress.IPv6Network(('2001:db8::1', 96), |
---|
834 | n/a | strict=False), |
---|
835 | n/a | net) |
---|
836 | n/a | self.assertEqual(ipaddress.IPv6Network( |
---|
837 | n/a | (42540766411282592856903984951653826561, 96), |
---|
838 | n/a | strict=False), |
---|
839 | n/a | net) |
---|
840 | n/a | self.assertEqual(ipaddress.IPv6Network((ip, 96), strict=False), |
---|
841 | n/a | net) |
---|
842 | n/a | |
---|
843 | n/a | # /96 |
---|
844 | n/a | self.assertEqual(ipaddress.IPv6Interface(('2001:db8::1', '96')), |
---|
845 | n/a | ipaddress.IPv6Interface('2001:db8::1/96')) |
---|
846 | n/a | self.assertEqual(ipaddress.IPv6Interface( |
---|
847 | n/a | (42540766411282592856903984951653826561, '96')), |
---|
848 | n/a | ipaddress.IPv6Interface('2001:db8::1/96')) |
---|
849 | n/a | |
---|
850 | n/a | # issue57 |
---|
851 | n/a | def testAddressIntMath(self): |
---|
852 | n/a | self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255, |
---|
853 | n/a | ipaddress.IPv4Address('1.1.2.0')) |
---|
854 | n/a | self.assertEqual(ipaddress.IPv4Address('1.1.1.1') - 256, |
---|
855 | n/a | ipaddress.IPv4Address('1.1.0.1')) |
---|
856 | n/a | self.assertEqual(ipaddress.IPv6Address('::1') + (2**16 - 2), |
---|
857 | n/a | ipaddress.IPv6Address('::ffff')) |
---|
858 | n/a | self.assertEqual(ipaddress.IPv6Address('::ffff') - (2**16 - 2), |
---|
859 | n/a | ipaddress.IPv6Address('::1')) |
---|
860 | n/a | |
---|
861 | n/a | def testInvalidIntToBytes(self): |
---|
862 | n/a | self.assertRaises(ValueError, ipaddress.v4_int_to_packed, -1) |
---|
863 | n/a | self.assertRaises(ValueError, ipaddress.v4_int_to_packed, |
---|
864 | n/a | 2 ** ipaddress.IPV4LENGTH) |
---|
865 | n/a | self.assertRaises(ValueError, ipaddress.v6_int_to_packed, -1) |
---|
866 | n/a | self.assertRaises(ValueError, ipaddress.v6_int_to_packed, |
---|
867 | n/a | 2 ** ipaddress.IPV6LENGTH) |
---|
868 | n/a | |
---|
869 | n/a | def testInternals(self): |
---|
870 | n/a | ip1 = ipaddress.IPv4Address('10.10.10.10') |
---|
871 | n/a | ip2 = ipaddress.IPv4Address('10.10.10.11') |
---|
872 | n/a | ip3 = ipaddress.IPv4Address('10.10.10.12') |
---|
873 | n/a | self.assertEqual(list(ipaddress._find_address_range([ip1])), |
---|
874 | n/a | [(ip1, ip1)]) |
---|
875 | n/a | self.assertEqual(list(ipaddress._find_address_range([ip1, ip3])), |
---|
876 | n/a | [(ip1, ip1), (ip3, ip3)]) |
---|
877 | n/a | self.assertEqual(list(ipaddress._find_address_range([ip1, ip2, ip3])), |
---|
878 | n/a | [(ip1, ip3)]) |
---|
879 | n/a | self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128)) |
---|
880 | n/a | self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network)) |
---|
881 | n/a | |
---|
882 | n/a | def testMissingNetworkVersion(self): |
---|
883 | n/a | class Broken(ipaddress._BaseNetwork): |
---|
884 | n/a | pass |
---|
885 | n/a | broken = Broken('127.0.0.1') |
---|
886 | n/a | with self.assertRaisesRegex(NotImplementedError, "Broken.*version"): |
---|
887 | n/a | broken.version |
---|
888 | n/a | |
---|
889 | n/a | def testMissingAddressClass(self): |
---|
890 | n/a | class Broken(ipaddress._BaseNetwork): |
---|
891 | n/a | pass |
---|
892 | n/a | broken = Broken('127.0.0.1') |
---|
893 | n/a | with self.assertRaisesRegex(NotImplementedError, "Broken.*address"): |
---|
894 | n/a | broken._address_class |
---|
895 | n/a | |
---|
896 | n/a | def testGetNetwork(self): |
---|
897 | n/a | self.assertEqual(int(self.ipv4_network.network_address), 16909056) |
---|
898 | n/a | self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0') |
---|
899 | n/a | |
---|
900 | n/a | self.assertEqual(int(self.ipv6_network.network_address), |
---|
901 | n/a | 42540616829182469433403647294022090752) |
---|
902 | n/a | self.assertEqual(str(self.ipv6_network.network_address), |
---|
903 | n/a | '2001:658:22a:cafe::') |
---|
904 | n/a | self.assertEqual(str(self.ipv6_network.hostmask), |
---|
905 | n/a | '::ffff:ffff:ffff:ffff') |
---|
906 | n/a | |
---|
907 | n/a | def testIpFromInt(self): |
---|
908 | n/a | self.assertEqual(self.ipv4_interface._ip, |
---|
909 | n/a | ipaddress.IPv4Interface(16909060)._ip) |
---|
910 | n/a | |
---|
911 | n/a | ipv4 = ipaddress.ip_network('1.2.3.4') |
---|
912 | n/a | ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1') |
---|
913 | n/a | self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4.network_address))) |
---|
914 | n/a | self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6.network_address))) |
---|
915 | n/a | |
---|
916 | n/a | v6_int = 42540616829182469433547762482097946625 |
---|
917 | n/a | self.assertEqual(self.ipv6_interface._ip, |
---|
918 | n/a | ipaddress.IPv6Interface(v6_int)._ip) |
---|
919 | n/a | |
---|
920 | n/a | self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version, |
---|
921 | n/a | 4) |
---|
922 | n/a | self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version, |
---|
923 | n/a | 6) |
---|
924 | n/a | |
---|
925 | n/a | def testIpFromPacked(self): |
---|
926 | n/a | address = ipaddress.ip_address |
---|
927 | n/a | self.assertEqual(self.ipv4_interface._ip, |
---|
928 | n/a | ipaddress.ip_interface(b'\x01\x02\x03\x04')._ip) |
---|
929 | n/a | self.assertEqual(address('255.254.253.252'), |
---|
930 | n/a | address(b'\xff\xfe\xfd\xfc')) |
---|
931 | n/a | self.assertEqual(self.ipv6_interface.ip, |
---|
932 | n/a | ipaddress.ip_interface( |
---|
933 | n/a | b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
---|
934 | n/a | b'\x02\x00\x00\x00\x00\x00\x00\x01').ip) |
---|
935 | n/a | self.assertEqual(address('ffff:2:3:4:ffff::'), |
---|
936 | n/a | address(b'\xff\xff\x00\x02\x00\x03\x00\x04' + |
---|
937 | n/a | b'\xff\xff' + b'\x00' * 6)) |
---|
938 | n/a | self.assertEqual(address('::'), |
---|
939 | n/a | address(b'\x00' * 16)) |
---|
940 | n/a | |
---|
941 | n/a | def testGetIp(self): |
---|
942 | n/a | self.assertEqual(int(self.ipv4_interface.ip), 16909060) |
---|
943 | n/a | self.assertEqual(str(self.ipv4_interface.ip), '1.2.3.4') |
---|
944 | n/a | |
---|
945 | n/a | self.assertEqual(int(self.ipv6_interface.ip), |
---|
946 | n/a | 42540616829182469433547762482097946625) |
---|
947 | n/a | self.assertEqual(str(self.ipv6_interface.ip), |
---|
948 | n/a | '2001:658:22a:cafe:200::1') |
---|
949 | n/a | |
---|
950 | n/a | def testGetNetmask(self): |
---|
951 | n/a | self.assertEqual(int(self.ipv4_network.netmask), 4294967040) |
---|
952 | n/a | self.assertEqual(str(self.ipv4_network.netmask), '255.255.255.0') |
---|
953 | n/a | self.assertEqual(int(self.ipv6_network.netmask), |
---|
954 | n/a | 340282366920938463444927863358058659840) |
---|
955 | n/a | self.assertEqual(self.ipv6_network.prefixlen, 64) |
---|
956 | n/a | |
---|
957 | n/a | def testZeroNetmask(self): |
---|
958 | n/a | ipv4_zero_netmask = ipaddress.IPv4Interface('1.2.3.4/0') |
---|
959 | n/a | self.assertEqual(int(ipv4_zero_netmask.network.netmask), 0) |
---|
960 | n/a | self.assertEqual(ipv4_zero_netmask._prefix_from_prefix_string('0'), 0) |
---|
961 | n/a | self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0')) |
---|
962 | n/a | self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0.0.0.0')) |
---|
963 | n/a | self.assertFalse(ipv4_zero_netmask._is_valid_netmask('invalid')) |
---|
964 | n/a | |
---|
965 | n/a | ipv6_zero_netmask = ipaddress.IPv6Interface('::1/0') |
---|
966 | n/a | self.assertEqual(int(ipv6_zero_netmask.network.netmask), 0) |
---|
967 | n/a | self.assertEqual(ipv6_zero_netmask._prefix_from_prefix_string('0'), 0) |
---|
968 | n/a | |
---|
969 | n/a | def testIPv4NetAndHostmasks(self): |
---|
970 | n/a | net = self.ipv4_network |
---|
971 | n/a | self.assertFalse(net._is_valid_netmask('invalid')) |
---|
972 | n/a | self.assertTrue(net._is_valid_netmask('128.128.128.128')) |
---|
973 | n/a | self.assertFalse(net._is_valid_netmask('128.128.128.127')) |
---|
974 | n/a | self.assertFalse(net._is_valid_netmask('128.128.128.255')) |
---|
975 | n/a | self.assertTrue(net._is_valid_netmask('255.128.128.128')) |
---|
976 | n/a | |
---|
977 | n/a | self.assertFalse(net._is_hostmask('invalid')) |
---|
978 | n/a | self.assertTrue(net._is_hostmask('128.255.255.255')) |
---|
979 | n/a | self.assertFalse(net._is_hostmask('255.255.255.255')) |
---|
980 | n/a | self.assertFalse(net._is_hostmask('1.2.3.4')) |
---|
981 | n/a | |
---|
982 | n/a | net = ipaddress.IPv4Network('127.0.0.0/0.0.0.255') |
---|
983 | n/a | self.assertEqual(net.prefixlen, 24) |
---|
984 | n/a | |
---|
985 | n/a | def testGetBroadcast(self): |
---|
986 | n/a | self.assertEqual(int(self.ipv4_network.broadcast_address), 16909311) |
---|
987 | n/a | self.assertEqual(str(self.ipv4_network.broadcast_address), '1.2.3.255') |
---|
988 | n/a | |
---|
989 | n/a | self.assertEqual(int(self.ipv6_network.broadcast_address), |
---|
990 | n/a | 42540616829182469451850391367731642367) |
---|
991 | n/a | self.assertEqual(str(self.ipv6_network.broadcast_address), |
---|
992 | n/a | '2001:658:22a:cafe:ffff:ffff:ffff:ffff') |
---|
993 | n/a | |
---|
994 | n/a | def testGetPrefixlen(self): |
---|
995 | n/a | self.assertEqual(self.ipv4_interface.network.prefixlen, 24) |
---|
996 | n/a | self.assertEqual(self.ipv6_interface.network.prefixlen, 64) |
---|
997 | n/a | |
---|
998 | n/a | def testGetSupernet(self): |
---|
999 | n/a | self.assertEqual(self.ipv4_network.supernet().prefixlen, 23) |
---|
1000 | n/a | self.assertEqual(str(self.ipv4_network.supernet().network_address), |
---|
1001 | n/a | '1.2.2.0') |
---|
1002 | n/a | self.assertEqual( |
---|
1003 | n/a | ipaddress.IPv4Interface('0.0.0.0/0').network.supernet(), |
---|
1004 | n/a | ipaddress.IPv4Network('0.0.0.0/0')) |
---|
1005 | n/a | |
---|
1006 | n/a | self.assertEqual(self.ipv6_network.supernet().prefixlen, 63) |
---|
1007 | n/a | self.assertEqual(str(self.ipv6_network.supernet().network_address), |
---|
1008 | n/a | '2001:658:22a:cafe::') |
---|
1009 | n/a | self.assertEqual(ipaddress.IPv6Interface('::0/0').network.supernet(), |
---|
1010 | n/a | ipaddress.IPv6Network('::0/0')) |
---|
1011 | n/a | |
---|
1012 | n/a | def testGetSupernet3(self): |
---|
1013 | n/a | self.assertEqual(self.ipv4_network.supernet(3).prefixlen, 21) |
---|
1014 | n/a | self.assertEqual(str(self.ipv4_network.supernet(3).network_address), |
---|
1015 | n/a | '1.2.0.0') |
---|
1016 | n/a | |
---|
1017 | n/a | self.assertEqual(self.ipv6_network.supernet(3).prefixlen, 61) |
---|
1018 | n/a | self.assertEqual(str(self.ipv6_network.supernet(3).network_address), |
---|
1019 | n/a | '2001:658:22a:caf8::') |
---|
1020 | n/a | |
---|
1021 | n/a | def testGetSupernet4(self): |
---|
1022 | n/a | self.assertRaises(ValueError, self.ipv4_network.supernet, |
---|
1023 | n/a | prefixlen_diff=2, new_prefix=1) |
---|
1024 | n/a | self.assertRaises(ValueError, self.ipv4_network.supernet, |
---|
1025 | n/a | new_prefix=25) |
---|
1026 | n/a | self.assertEqual(self.ipv4_network.supernet(prefixlen_diff=2), |
---|
1027 | n/a | self.ipv4_network.supernet(new_prefix=22)) |
---|
1028 | n/a | |
---|
1029 | n/a | self.assertRaises(ValueError, self.ipv6_network.supernet, |
---|
1030 | n/a | prefixlen_diff=2, new_prefix=1) |
---|
1031 | n/a | self.assertRaises(ValueError, self.ipv6_network.supernet, |
---|
1032 | n/a | new_prefix=65) |
---|
1033 | n/a | self.assertEqual(self.ipv6_network.supernet(prefixlen_diff=2), |
---|
1034 | n/a | self.ipv6_network.supernet(new_prefix=62)) |
---|
1035 | n/a | |
---|
1036 | n/a | def testHosts(self): |
---|
1037 | n/a | hosts = list(self.ipv4_network.hosts()) |
---|
1038 | n/a | self.assertEqual(254, len(hosts)) |
---|
1039 | n/a | self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0]) |
---|
1040 | n/a | self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1]) |
---|
1041 | n/a | |
---|
1042 | n/a | # special case where only 1 bit is left for address |
---|
1043 | n/a | self.assertEqual([ipaddress.IPv4Address('2.0.0.0'), |
---|
1044 | n/a | ipaddress.IPv4Address('2.0.0.1')], |
---|
1045 | n/a | list(ipaddress.ip_network('2.0.0.0/31').hosts())) |
---|
1046 | n/a | |
---|
1047 | n/a | def testFancySubnetting(self): |
---|
1048 | n/a | self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)), |
---|
1049 | n/a | sorted(self.ipv4_network.subnets(new_prefix=27))) |
---|
1050 | n/a | self.assertRaises(ValueError, list, |
---|
1051 | n/a | self.ipv4_network.subnets(new_prefix=23)) |
---|
1052 | n/a | self.assertRaises(ValueError, list, |
---|
1053 | n/a | self.ipv4_network.subnets(prefixlen_diff=3, |
---|
1054 | n/a | new_prefix=27)) |
---|
1055 | n/a | self.assertEqual(sorted(self.ipv6_network.subnets(prefixlen_diff=4)), |
---|
1056 | n/a | sorted(self.ipv6_network.subnets(new_prefix=68))) |
---|
1057 | n/a | self.assertRaises(ValueError, list, |
---|
1058 | n/a | self.ipv6_network.subnets(new_prefix=63)) |
---|
1059 | n/a | self.assertRaises(ValueError, list, |
---|
1060 | n/a | self.ipv6_network.subnets(prefixlen_diff=4, |
---|
1061 | n/a | new_prefix=68)) |
---|
1062 | n/a | |
---|
1063 | n/a | def testGetSubnets(self): |
---|
1064 | n/a | self.assertEqual(list(self.ipv4_network.subnets())[0].prefixlen, 25) |
---|
1065 | n/a | self.assertEqual(str(list( |
---|
1066 | n/a | self.ipv4_network.subnets())[0].network_address), |
---|
1067 | n/a | '1.2.3.0') |
---|
1068 | n/a | self.assertEqual(str(list( |
---|
1069 | n/a | self.ipv4_network.subnets())[1].network_address), |
---|
1070 | n/a | '1.2.3.128') |
---|
1071 | n/a | |
---|
1072 | n/a | self.assertEqual(list(self.ipv6_network.subnets())[0].prefixlen, 65) |
---|
1073 | n/a | |
---|
1074 | n/a | def testGetSubnetForSingle32(self): |
---|
1075 | n/a | ip = ipaddress.IPv4Network('1.2.3.4/32') |
---|
1076 | n/a | subnets1 = [str(x) for x in ip.subnets()] |
---|
1077 | n/a | subnets2 = [str(x) for x in ip.subnets(2)] |
---|
1078 | n/a | self.assertEqual(subnets1, ['1.2.3.4/32']) |
---|
1079 | n/a | self.assertEqual(subnets1, subnets2) |
---|
1080 | n/a | |
---|
1081 | n/a | def testGetSubnetForSingle128(self): |
---|
1082 | n/a | ip = ipaddress.IPv6Network('::1/128') |
---|
1083 | n/a | subnets1 = [str(x) for x in ip.subnets()] |
---|
1084 | n/a | subnets2 = [str(x) for x in ip.subnets(2)] |
---|
1085 | n/a | self.assertEqual(subnets1, ['::1/128']) |
---|
1086 | n/a | self.assertEqual(subnets1, subnets2) |
---|
1087 | n/a | |
---|
1088 | n/a | def testSubnet2(self): |
---|
1089 | n/a | ips = [str(x) for x in self.ipv4_network.subnets(2)] |
---|
1090 | n/a | self.assertEqual( |
---|
1091 | n/a | ips, |
---|
1092 | n/a | ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26']) |
---|
1093 | n/a | |
---|
1094 | n/a | ipsv6 = [str(x) for x in self.ipv6_network.subnets(2)] |
---|
1095 | n/a | self.assertEqual( |
---|
1096 | n/a | ipsv6, |
---|
1097 | n/a | ['2001:658:22a:cafe::/66', |
---|
1098 | n/a | '2001:658:22a:cafe:4000::/66', |
---|
1099 | n/a | '2001:658:22a:cafe:8000::/66', |
---|
1100 | n/a | '2001:658:22a:cafe:c000::/66']) |
---|
1101 | n/a | |
---|
1102 | n/a | def testGetSubnets3(self): |
---|
1103 | n/a | subnets = [str(x) for x in self.ipv4_network.subnets(8)] |
---|
1104 | n/a | self.assertEqual(subnets[:3], |
---|
1105 | n/a | ['1.2.3.0/32', '1.2.3.1/32', '1.2.3.2/32']) |
---|
1106 | n/a | self.assertEqual(subnets[-3:], |
---|
1107 | n/a | ['1.2.3.253/32', '1.2.3.254/32', '1.2.3.255/32']) |
---|
1108 | n/a | self.assertEqual(len(subnets), 256) |
---|
1109 | n/a | |
---|
1110 | n/a | ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/120') |
---|
1111 | n/a | subnets = [str(x) for x in ipv6_network.subnets(8)] |
---|
1112 | n/a | self.assertEqual(subnets[:3], |
---|
1113 | n/a | ['2001:658:22a:cafe::/128', |
---|
1114 | n/a | '2001:658:22a:cafe::1/128', |
---|
1115 | n/a | '2001:658:22a:cafe::2/128']) |
---|
1116 | n/a | self.assertEqual(subnets[-3:], |
---|
1117 | n/a | ['2001:658:22a:cafe::fd/128', |
---|
1118 | n/a | '2001:658:22a:cafe::fe/128', |
---|
1119 | n/a | '2001:658:22a:cafe::ff/128']) |
---|
1120 | n/a | self.assertEqual(len(subnets), 256) |
---|
1121 | n/a | |
---|
1122 | n/a | def testSubnetFailsForLargeCidrDiff(self): |
---|
1123 | n/a | self.assertRaises(ValueError, list, |
---|
1124 | n/a | self.ipv4_interface.network.subnets(9)) |
---|
1125 | n/a | self.assertRaises(ValueError, list, |
---|
1126 | n/a | self.ipv4_network.subnets(9)) |
---|
1127 | n/a | self.assertRaises(ValueError, list, |
---|
1128 | n/a | self.ipv6_interface.network.subnets(65)) |
---|
1129 | n/a | self.assertRaises(ValueError, list, |
---|
1130 | n/a | self.ipv6_network.subnets(65)) |
---|
1131 | n/a | |
---|
1132 | n/a | def testSupernetFailsForLargeCidrDiff(self): |
---|
1133 | n/a | self.assertRaises(ValueError, |
---|
1134 | n/a | self.ipv4_interface.network.supernet, 25) |
---|
1135 | n/a | self.assertRaises(ValueError, |
---|
1136 | n/a | self.ipv6_interface.network.supernet, 65) |
---|
1137 | n/a | |
---|
1138 | n/a | def testSubnetFailsForNegativeCidrDiff(self): |
---|
1139 | n/a | self.assertRaises(ValueError, list, |
---|
1140 | n/a | self.ipv4_interface.network.subnets(-1)) |
---|
1141 | n/a | self.assertRaises(ValueError, list, |
---|
1142 | n/a | self.ipv4_network.subnets(-1)) |
---|
1143 | n/a | self.assertRaises(ValueError, list, |
---|
1144 | n/a | self.ipv6_interface.network.subnets(-1)) |
---|
1145 | n/a | self.assertRaises(ValueError, list, |
---|
1146 | n/a | self.ipv6_network.subnets(-1)) |
---|
1147 | n/a | |
---|
1148 | n/a | def testGetNum_Addresses(self): |
---|
1149 | n/a | self.assertEqual(self.ipv4_network.num_addresses, 256) |
---|
1150 | n/a | self.assertEqual(list(self.ipv4_network.subnets())[0].num_addresses, |
---|
1151 | n/a | 128) |
---|
1152 | n/a | self.assertEqual(self.ipv4_network.supernet().num_addresses, 512) |
---|
1153 | n/a | |
---|
1154 | n/a | self.assertEqual(self.ipv6_network.num_addresses, 18446744073709551616) |
---|
1155 | n/a | self.assertEqual(list(self.ipv6_network.subnets())[0].num_addresses, |
---|
1156 | n/a | 9223372036854775808) |
---|
1157 | n/a | self.assertEqual(self.ipv6_network.supernet().num_addresses, |
---|
1158 | n/a | 36893488147419103232) |
---|
1159 | n/a | |
---|
1160 | n/a | def testContains(self): |
---|
1161 | n/a | self.assertIn(ipaddress.IPv4Interface('1.2.3.128/25'), |
---|
1162 | n/a | self.ipv4_network) |
---|
1163 | n/a | self.assertNotIn(ipaddress.IPv4Interface('1.2.4.1/24'), |
---|
1164 | n/a | self.ipv4_network) |
---|
1165 | n/a | # We can test addresses and string as well. |
---|
1166 | n/a | addr1 = ipaddress.IPv4Address('1.2.3.37') |
---|
1167 | n/a | self.assertIn(addr1, self.ipv4_network) |
---|
1168 | n/a | # issue 61, bad network comparison on like-ip'd network objects |
---|
1169 | n/a | # with identical broadcast addresses. |
---|
1170 | n/a | self.assertFalse(ipaddress.IPv4Network('1.1.0.0/16').__contains__( |
---|
1171 | n/a | ipaddress.IPv4Network('1.0.0.0/15'))) |
---|
1172 | n/a | |
---|
1173 | n/a | def testNth(self): |
---|
1174 | n/a | self.assertEqual(str(self.ipv4_network[5]), '1.2.3.5') |
---|
1175 | n/a | self.assertRaises(IndexError, self.ipv4_network.__getitem__, 256) |
---|
1176 | n/a | |
---|
1177 | n/a | self.assertEqual(str(self.ipv6_network[5]), |
---|
1178 | n/a | '2001:658:22a:cafe::5') |
---|
1179 | n/a | self.assertRaises(IndexError, self.ipv6_network.__getitem__, 1 << 64) |
---|
1180 | n/a | |
---|
1181 | n/a | def testGetitem(self): |
---|
1182 | n/a | # http://code.google.com/p/ipaddr-py/issues/detail?id=15 |
---|
1183 | n/a | addr = ipaddress.IPv4Network('172.31.255.128/255.255.255.240') |
---|
1184 | n/a | self.assertEqual(28, addr.prefixlen) |
---|
1185 | n/a | addr_list = list(addr) |
---|
1186 | n/a | self.assertEqual('172.31.255.128', str(addr_list[0])) |
---|
1187 | n/a | self.assertEqual('172.31.255.128', str(addr[0])) |
---|
1188 | n/a | self.assertEqual('172.31.255.143', str(addr_list[-1])) |
---|
1189 | n/a | self.assertEqual('172.31.255.143', str(addr[-1])) |
---|
1190 | n/a | self.assertEqual(addr_list[-1], addr[-1]) |
---|
1191 | n/a | |
---|
1192 | n/a | def testEqual(self): |
---|
1193 | n/a | self.assertTrue(self.ipv4_interface == |
---|
1194 | n/a | ipaddress.IPv4Interface('1.2.3.4/24')) |
---|
1195 | n/a | self.assertFalse(self.ipv4_interface == |
---|
1196 | n/a | ipaddress.IPv4Interface('1.2.3.4/23')) |
---|
1197 | n/a | self.assertFalse(self.ipv4_interface == |
---|
1198 | n/a | ipaddress.IPv6Interface('::1.2.3.4/24')) |
---|
1199 | n/a | self.assertFalse(self.ipv4_interface == '') |
---|
1200 | n/a | self.assertFalse(self.ipv4_interface == []) |
---|
1201 | n/a | self.assertFalse(self.ipv4_interface == 2) |
---|
1202 | n/a | |
---|
1203 | n/a | self.assertTrue(self.ipv6_interface == |
---|
1204 | n/a | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) |
---|
1205 | n/a | self.assertFalse(self.ipv6_interface == |
---|
1206 | n/a | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) |
---|
1207 | n/a | self.assertFalse(self.ipv6_interface == |
---|
1208 | n/a | ipaddress.IPv4Interface('1.2.3.4/23')) |
---|
1209 | n/a | self.assertFalse(self.ipv6_interface == '') |
---|
1210 | n/a | self.assertFalse(self.ipv6_interface == []) |
---|
1211 | n/a | self.assertFalse(self.ipv6_interface == 2) |
---|
1212 | n/a | |
---|
1213 | n/a | def testNotEqual(self): |
---|
1214 | n/a | self.assertFalse(self.ipv4_interface != |
---|
1215 | n/a | ipaddress.IPv4Interface('1.2.3.4/24')) |
---|
1216 | n/a | self.assertTrue(self.ipv4_interface != |
---|
1217 | n/a | ipaddress.IPv4Interface('1.2.3.4/23')) |
---|
1218 | n/a | self.assertTrue(self.ipv4_interface != |
---|
1219 | n/a | ipaddress.IPv6Interface('::1.2.3.4/24')) |
---|
1220 | n/a | self.assertTrue(self.ipv4_interface != '') |
---|
1221 | n/a | self.assertTrue(self.ipv4_interface != []) |
---|
1222 | n/a | self.assertTrue(self.ipv4_interface != 2) |
---|
1223 | n/a | |
---|
1224 | n/a | self.assertTrue(self.ipv4_address != |
---|
1225 | n/a | ipaddress.IPv4Address('1.2.3.5')) |
---|
1226 | n/a | self.assertTrue(self.ipv4_address != '') |
---|
1227 | n/a | self.assertTrue(self.ipv4_address != []) |
---|
1228 | n/a | self.assertTrue(self.ipv4_address != 2) |
---|
1229 | n/a | |
---|
1230 | n/a | self.assertFalse(self.ipv6_interface != |
---|
1231 | n/a | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) |
---|
1232 | n/a | self.assertTrue(self.ipv6_interface != |
---|
1233 | n/a | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) |
---|
1234 | n/a | self.assertTrue(self.ipv6_interface != |
---|
1235 | n/a | ipaddress.IPv4Interface('1.2.3.4/23')) |
---|
1236 | n/a | self.assertTrue(self.ipv6_interface != '') |
---|
1237 | n/a | self.assertTrue(self.ipv6_interface != []) |
---|
1238 | n/a | self.assertTrue(self.ipv6_interface != 2) |
---|
1239 | n/a | |
---|
1240 | n/a | self.assertTrue(self.ipv6_address != |
---|
1241 | n/a | ipaddress.IPv4Address('1.2.3.4')) |
---|
1242 | n/a | self.assertTrue(self.ipv6_address != '') |
---|
1243 | n/a | self.assertTrue(self.ipv6_address != []) |
---|
1244 | n/a | self.assertTrue(self.ipv6_address != 2) |
---|
1245 | n/a | |
---|
1246 | n/a | def testSlash32Constructor(self): |
---|
1247 | n/a | self.assertEqual(str(ipaddress.IPv4Interface( |
---|
1248 | n/a | '1.2.3.4/255.255.255.255')), '1.2.3.4/32') |
---|
1249 | n/a | |
---|
1250 | n/a | def testSlash128Constructor(self): |
---|
1251 | n/a | self.assertEqual(str(ipaddress.IPv6Interface('::1/128')), |
---|
1252 | n/a | '::1/128') |
---|
1253 | n/a | |
---|
1254 | n/a | def testSlash0Constructor(self): |
---|
1255 | n/a | self.assertEqual(str(ipaddress.IPv4Interface('1.2.3.4/0.0.0.0')), |
---|
1256 | n/a | '1.2.3.4/0') |
---|
1257 | n/a | |
---|
1258 | n/a | def testCollapsing(self): |
---|
1259 | n/a | # test only IP addresses including some duplicates |
---|
1260 | n/a | ip1 = ipaddress.IPv4Address('1.1.1.0') |
---|
1261 | n/a | ip2 = ipaddress.IPv4Address('1.1.1.1') |
---|
1262 | n/a | ip3 = ipaddress.IPv4Address('1.1.1.2') |
---|
1263 | n/a | ip4 = ipaddress.IPv4Address('1.1.1.3') |
---|
1264 | n/a | ip5 = ipaddress.IPv4Address('1.1.1.4') |
---|
1265 | n/a | ip6 = ipaddress.IPv4Address('1.1.1.0') |
---|
1266 | n/a | # check that addresses are subsumed properly. |
---|
1267 | n/a | collapsed = ipaddress.collapse_addresses( |
---|
1268 | n/a | [ip1, ip2, ip3, ip4, ip5, ip6]) |
---|
1269 | n/a | self.assertEqual(list(collapsed), |
---|
1270 | n/a | [ipaddress.IPv4Network('1.1.1.0/30'), |
---|
1271 | n/a | ipaddress.IPv4Network('1.1.1.4/32')]) |
---|
1272 | n/a | |
---|
1273 | n/a | # test a mix of IP addresses and networks including some duplicates |
---|
1274 | n/a | ip1 = ipaddress.IPv4Address('1.1.1.0') |
---|
1275 | n/a | ip2 = ipaddress.IPv4Address('1.1.1.1') |
---|
1276 | n/a | ip3 = ipaddress.IPv4Address('1.1.1.2') |
---|
1277 | n/a | ip4 = ipaddress.IPv4Address('1.1.1.3') |
---|
1278 | n/a | #ip5 = ipaddress.IPv4Interface('1.1.1.4/30') |
---|
1279 | n/a | #ip6 = ipaddress.IPv4Interface('1.1.1.4/30') |
---|
1280 | n/a | # check that addresses are subsumed properly. |
---|
1281 | n/a | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4]) |
---|
1282 | n/a | self.assertEqual(list(collapsed), |
---|
1283 | n/a | [ipaddress.IPv4Network('1.1.1.0/30')]) |
---|
1284 | n/a | |
---|
1285 | n/a | # test only IP networks |
---|
1286 | n/a | ip1 = ipaddress.IPv4Network('1.1.0.0/24') |
---|
1287 | n/a | ip2 = ipaddress.IPv4Network('1.1.1.0/24') |
---|
1288 | n/a | ip3 = ipaddress.IPv4Network('1.1.2.0/24') |
---|
1289 | n/a | ip4 = ipaddress.IPv4Network('1.1.3.0/24') |
---|
1290 | n/a | ip5 = ipaddress.IPv4Network('1.1.4.0/24') |
---|
1291 | n/a | # stored in no particular order b/c we want CollapseAddr to call |
---|
1292 | n/a | # [].sort |
---|
1293 | n/a | ip6 = ipaddress.IPv4Network('1.1.0.0/22') |
---|
1294 | n/a | # check that addresses are subsumed properly. |
---|
1295 | n/a | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4, ip5, |
---|
1296 | n/a | ip6]) |
---|
1297 | n/a | self.assertEqual(list(collapsed), |
---|
1298 | n/a | [ipaddress.IPv4Network('1.1.0.0/22'), |
---|
1299 | n/a | ipaddress.IPv4Network('1.1.4.0/24')]) |
---|
1300 | n/a | |
---|
1301 | n/a | # test that two addresses are supernet'ed properly |
---|
1302 | n/a | collapsed = ipaddress.collapse_addresses([ip1, ip2]) |
---|
1303 | n/a | self.assertEqual(list(collapsed), |
---|
1304 | n/a | [ipaddress.IPv4Network('1.1.0.0/23')]) |
---|
1305 | n/a | |
---|
1306 | n/a | # test same IP networks |
---|
1307 | n/a | ip_same1 = ip_same2 = ipaddress.IPv4Network('1.1.1.1/32') |
---|
1308 | n/a | self.assertEqual(list(ipaddress.collapse_addresses( |
---|
1309 | n/a | [ip_same1, ip_same2])), |
---|
1310 | n/a | [ip_same1]) |
---|
1311 | n/a | |
---|
1312 | n/a | # test same IP addresses |
---|
1313 | n/a | ip_same1 = ip_same2 = ipaddress.IPv4Address('1.1.1.1') |
---|
1314 | n/a | self.assertEqual(list(ipaddress.collapse_addresses( |
---|
1315 | n/a | [ip_same1, ip_same2])), |
---|
1316 | n/a | [ipaddress.ip_network('1.1.1.1/32')]) |
---|
1317 | n/a | ip1 = ipaddress.IPv6Network('2001::/100') |
---|
1318 | n/a | ip2 = ipaddress.IPv6Network('2001::/120') |
---|
1319 | n/a | ip3 = ipaddress.IPv6Network('2001::/96') |
---|
1320 | n/a | # test that ipv6 addresses are subsumed properly. |
---|
1321 | n/a | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3]) |
---|
1322 | n/a | self.assertEqual(list(collapsed), [ip3]) |
---|
1323 | n/a | |
---|
1324 | n/a | # the toejam test |
---|
1325 | n/a | addr_tuples = [ |
---|
1326 | n/a | (ipaddress.ip_address('1.1.1.1'), |
---|
1327 | n/a | ipaddress.ip_address('::1')), |
---|
1328 | n/a | (ipaddress.IPv4Network('1.1.0.0/24'), |
---|
1329 | n/a | ipaddress.IPv6Network('2001::/120')), |
---|
1330 | n/a | (ipaddress.IPv4Network('1.1.0.0/32'), |
---|
1331 | n/a | ipaddress.IPv6Network('2001::/128')), |
---|
1332 | n/a | ] |
---|
1333 | n/a | for ip1, ip2 in addr_tuples: |
---|
1334 | n/a | self.assertRaises(TypeError, ipaddress.collapse_addresses, |
---|
1335 | n/a | [ip1, ip2]) |
---|
1336 | n/a | |
---|
1337 | n/a | def testSummarizing(self): |
---|
1338 | n/a | #ip = ipaddress.ip_address |
---|
1339 | n/a | #ipnet = ipaddress.ip_network |
---|
1340 | n/a | summarize = ipaddress.summarize_address_range |
---|
1341 | n/a | ip1 = ipaddress.ip_address('1.1.1.0') |
---|
1342 | n/a | ip2 = ipaddress.ip_address('1.1.1.255') |
---|
1343 | n/a | |
---|
1344 | n/a | # summarize works only for IPv4 & IPv6 |
---|
1345 | n/a | class IPv7Address(ipaddress.IPv6Address): |
---|
1346 | n/a | @property |
---|
1347 | n/a | def version(self): |
---|
1348 | n/a | return 7 |
---|
1349 | n/a | ip_invalid1 = IPv7Address('::1') |
---|
1350 | n/a | ip_invalid2 = IPv7Address('::1') |
---|
1351 | n/a | self.assertRaises(ValueError, list, |
---|
1352 | n/a | summarize(ip_invalid1, ip_invalid2)) |
---|
1353 | n/a | # test that a summary over ip4 & ip6 fails |
---|
1354 | n/a | self.assertRaises(TypeError, list, |
---|
1355 | n/a | summarize(ip1, ipaddress.IPv6Address('::1'))) |
---|
1356 | n/a | # test a /24 is summarized properly |
---|
1357 | n/a | self.assertEqual(list(summarize(ip1, ip2))[0], |
---|
1358 | n/a | ipaddress.ip_network('1.1.1.0/24')) |
---|
1359 | n/a | # test an IPv4 range that isn't on a network byte boundary |
---|
1360 | n/a | ip2 = ipaddress.ip_address('1.1.1.8') |
---|
1361 | n/a | self.assertEqual(list(summarize(ip1, ip2)), |
---|
1362 | n/a | [ipaddress.ip_network('1.1.1.0/29'), |
---|
1363 | n/a | ipaddress.ip_network('1.1.1.8')]) |
---|
1364 | n/a | # all! |
---|
1365 | n/a | ip1 = ipaddress.IPv4Address(0) |
---|
1366 | n/a | ip2 = ipaddress.IPv4Address(ipaddress.IPv4Address._ALL_ONES) |
---|
1367 | n/a | self.assertEqual([ipaddress.IPv4Network('0.0.0.0/0')], |
---|
1368 | n/a | list(summarize(ip1, ip2))) |
---|
1369 | n/a | |
---|
1370 | n/a | ip1 = ipaddress.ip_address('1::') |
---|
1371 | n/a | ip2 = ipaddress.ip_address('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff') |
---|
1372 | n/a | # test an IPv6 is summarized properly |
---|
1373 | n/a | self.assertEqual(list(summarize(ip1, ip2))[0], |
---|
1374 | n/a | ipaddress.ip_network('1::/16')) |
---|
1375 | n/a | # test an IPv6 range that isn't on a network byte boundary |
---|
1376 | n/a | ip2 = ipaddress.ip_address('2::') |
---|
1377 | n/a | self.assertEqual(list(summarize(ip1, ip2)), |
---|
1378 | n/a | [ipaddress.ip_network('1::/16'), |
---|
1379 | n/a | ipaddress.ip_network('2::/128')]) |
---|
1380 | n/a | |
---|
1381 | n/a | # test exception raised when first is greater than last |
---|
1382 | n/a | self.assertRaises(ValueError, list, |
---|
1383 | n/a | summarize(ipaddress.ip_address('1.1.1.0'), |
---|
1384 | n/a | ipaddress.ip_address('1.1.0.0'))) |
---|
1385 | n/a | # test exception raised when first and last aren't IP addresses |
---|
1386 | n/a | self.assertRaises(TypeError, list, |
---|
1387 | n/a | summarize(ipaddress.ip_network('1.1.1.0'), |
---|
1388 | n/a | ipaddress.ip_network('1.1.0.0'))) |
---|
1389 | n/a | self.assertRaises(TypeError, list, |
---|
1390 | n/a | summarize(ipaddress.ip_network('1.1.1.0'), |
---|
1391 | n/a | ipaddress.ip_network('1.1.0.0'))) |
---|
1392 | n/a | # test exception raised when first and last are not same version |
---|
1393 | n/a | self.assertRaises(TypeError, list, |
---|
1394 | n/a | summarize(ipaddress.ip_address('::'), |
---|
1395 | n/a | ipaddress.ip_network('1.1.0.0'))) |
---|
1396 | n/a | |
---|
1397 | n/a | def testAddressComparison(self): |
---|
1398 | n/a | self.assertTrue(ipaddress.ip_address('1.1.1.1') <= |
---|
1399 | n/a | ipaddress.ip_address('1.1.1.1')) |
---|
1400 | n/a | self.assertTrue(ipaddress.ip_address('1.1.1.1') <= |
---|
1401 | n/a | ipaddress.ip_address('1.1.1.2')) |
---|
1402 | n/a | self.assertTrue(ipaddress.ip_address('::1') <= |
---|
1403 | n/a | ipaddress.ip_address('::1')) |
---|
1404 | n/a | self.assertTrue(ipaddress.ip_address('::1') <= |
---|
1405 | n/a | ipaddress.ip_address('::2')) |
---|
1406 | n/a | |
---|
1407 | n/a | def testInterfaceComparison(self): |
---|
1408 | n/a | self.assertTrue(ipaddress.ip_interface('1.1.1.1') <= |
---|
1409 | n/a | ipaddress.ip_interface('1.1.1.1')) |
---|
1410 | n/a | self.assertTrue(ipaddress.ip_interface('1.1.1.1') <= |
---|
1411 | n/a | ipaddress.ip_interface('1.1.1.2')) |
---|
1412 | n/a | self.assertTrue(ipaddress.ip_interface('::1') <= |
---|
1413 | n/a | ipaddress.ip_interface('::1')) |
---|
1414 | n/a | self.assertTrue(ipaddress.ip_interface('::1') <= |
---|
1415 | n/a | ipaddress.ip_interface('::2')) |
---|
1416 | n/a | |
---|
1417 | n/a | def testNetworkComparison(self): |
---|
1418 | n/a | # ip1 and ip2 have the same network address |
---|
1419 | n/a | ip1 = ipaddress.IPv4Network('1.1.1.0/24') |
---|
1420 | n/a | ip2 = ipaddress.IPv4Network('1.1.1.0/32') |
---|
1421 | n/a | ip3 = ipaddress.IPv4Network('1.1.2.0/24') |
---|
1422 | n/a | |
---|
1423 | n/a | self.assertTrue(ip1 < ip3) |
---|
1424 | n/a | self.assertTrue(ip3 > ip2) |
---|
1425 | n/a | |
---|
1426 | n/a | self.assertEqual(ip1.compare_networks(ip1), 0) |
---|
1427 | n/a | |
---|
1428 | n/a | # if addresses are the same, sort by netmask |
---|
1429 | n/a | self.assertEqual(ip1.compare_networks(ip2), -1) |
---|
1430 | n/a | self.assertEqual(ip2.compare_networks(ip1), 1) |
---|
1431 | n/a | |
---|
1432 | n/a | self.assertEqual(ip1.compare_networks(ip3), -1) |
---|
1433 | n/a | self.assertEqual(ip3.compare_networks(ip1), 1) |
---|
1434 | n/a | self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) |
---|
1435 | n/a | |
---|
1436 | n/a | ip1 = ipaddress.IPv6Network('2001:2000::/96') |
---|
1437 | n/a | ip2 = ipaddress.IPv6Network('2001:2001::/96') |
---|
1438 | n/a | ip3 = ipaddress.IPv6Network('2001:ffff:2000::/96') |
---|
1439 | n/a | |
---|
1440 | n/a | self.assertTrue(ip1 < ip3) |
---|
1441 | n/a | self.assertTrue(ip3 > ip2) |
---|
1442 | n/a | self.assertEqual(ip1.compare_networks(ip3), -1) |
---|
1443 | n/a | self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) |
---|
1444 | n/a | |
---|
1445 | n/a | # Test comparing different protocols. |
---|
1446 | n/a | # Should always raise a TypeError. |
---|
1447 | n/a | self.assertRaises(TypeError, |
---|
1448 | n/a | self.ipv4_network.compare_networks, |
---|
1449 | n/a | self.ipv6_network) |
---|
1450 | n/a | ipv6 = ipaddress.IPv6Interface('::/0') |
---|
1451 | n/a | ipv4 = ipaddress.IPv4Interface('0.0.0.0/0') |
---|
1452 | n/a | self.assertRaises(TypeError, ipv4.__lt__, ipv6) |
---|
1453 | n/a | self.assertRaises(TypeError, ipv4.__gt__, ipv6) |
---|
1454 | n/a | self.assertRaises(TypeError, ipv6.__lt__, ipv4) |
---|
1455 | n/a | self.assertRaises(TypeError, ipv6.__gt__, ipv4) |
---|
1456 | n/a | |
---|
1457 | n/a | # Regression test for issue 19. |
---|
1458 | n/a | ip1 = ipaddress.ip_network('10.1.2.128/25') |
---|
1459 | n/a | self.assertFalse(ip1 < ip1) |
---|
1460 | n/a | self.assertFalse(ip1 > ip1) |
---|
1461 | n/a | ip2 = ipaddress.ip_network('10.1.3.0/24') |
---|
1462 | n/a | self.assertTrue(ip1 < ip2) |
---|
1463 | n/a | self.assertFalse(ip2 < ip1) |
---|
1464 | n/a | self.assertFalse(ip1 > ip2) |
---|
1465 | n/a | self.assertTrue(ip2 > ip1) |
---|
1466 | n/a | ip3 = ipaddress.ip_network('10.1.3.0/25') |
---|
1467 | n/a | self.assertTrue(ip2 < ip3) |
---|
1468 | n/a | self.assertFalse(ip3 < ip2) |
---|
1469 | n/a | self.assertFalse(ip2 > ip3) |
---|
1470 | n/a | self.assertTrue(ip3 > ip2) |
---|
1471 | n/a | |
---|
1472 | n/a | # Regression test for issue 28. |
---|
1473 | n/a | ip1 = ipaddress.ip_network('10.10.10.0/31') |
---|
1474 | n/a | ip2 = ipaddress.ip_network('10.10.10.0') |
---|
1475 | n/a | ip3 = ipaddress.ip_network('10.10.10.2/31') |
---|
1476 | n/a | ip4 = ipaddress.ip_network('10.10.10.2') |
---|
1477 | n/a | sorted = [ip1, ip2, ip3, ip4] |
---|
1478 | n/a | unsorted = [ip2, ip4, ip1, ip3] |
---|
1479 | n/a | unsorted.sort() |
---|
1480 | n/a | self.assertEqual(sorted, unsorted) |
---|
1481 | n/a | unsorted = [ip4, ip1, ip3, ip2] |
---|
1482 | n/a | unsorted.sort() |
---|
1483 | n/a | self.assertEqual(sorted, unsorted) |
---|
1484 | n/a | self.assertIs(ip1.__lt__(ipaddress.ip_address('10.10.10.0')), |
---|
1485 | n/a | NotImplemented) |
---|
1486 | n/a | self.assertIs(ip2.__lt__(ipaddress.ip_address('10.10.10.0')), |
---|
1487 | n/a | NotImplemented) |
---|
1488 | n/a | |
---|
1489 | n/a | # <=, >= |
---|
1490 | n/a | self.assertTrue(ipaddress.ip_network('1.1.1.1') <= |
---|
1491 | n/a | ipaddress.ip_network('1.1.1.1')) |
---|
1492 | n/a | self.assertTrue(ipaddress.ip_network('1.1.1.1') <= |
---|
1493 | n/a | ipaddress.ip_network('1.1.1.2')) |
---|
1494 | n/a | self.assertFalse(ipaddress.ip_network('1.1.1.2') <= |
---|
1495 | n/a | ipaddress.ip_network('1.1.1.1')) |
---|
1496 | n/a | self.assertTrue(ipaddress.ip_network('::1') <= |
---|
1497 | n/a | ipaddress.ip_network('::1')) |
---|
1498 | n/a | self.assertTrue(ipaddress.ip_network('::1') <= |
---|
1499 | n/a | ipaddress.ip_network('::2')) |
---|
1500 | n/a | self.assertFalse(ipaddress.ip_network('::2') <= |
---|
1501 | n/a | ipaddress.ip_network('::1')) |
---|
1502 | n/a | |
---|
1503 | n/a | def testStrictNetworks(self): |
---|
1504 | n/a | self.assertRaises(ValueError, ipaddress.ip_network, '192.168.1.1/24') |
---|
1505 | n/a | self.assertRaises(ValueError, ipaddress.ip_network, '::1/120') |
---|
1506 | n/a | |
---|
1507 | n/a | def testOverlaps(self): |
---|
1508 | n/a | other = ipaddress.IPv4Network('1.2.3.0/30') |
---|
1509 | n/a | other2 = ipaddress.IPv4Network('1.2.2.0/24') |
---|
1510 | n/a | other3 = ipaddress.IPv4Network('1.2.2.64/26') |
---|
1511 | n/a | self.assertTrue(self.ipv4_network.overlaps(other)) |
---|
1512 | n/a | self.assertFalse(self.ipv4_network.overlaps(other2)) |
---|
1513 | n/a | self.assertTrue(other2.overlaps(other3)) |
---|
1514 | n/a | |
---|
1515 | n/a | def testEmbeddedIpv4(self): |
---|
1516 | n/a | ipv4_string = '192.168.0.1' |
---|
1517 | n/a | ipv4 = ipaddress.IPv4Interface(ipv4_string) |
---|
1518 | n/a | v4compat_ipv6 = ipaddress.IPv6Interface('::%s' % ipv4_string) |
---|
1519 | n/a | self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip)) |
---|
1520 | n/a | v4mapped_ipv6 = ipaddress.IPv6Interface('::ffff:%s' % ipv4_string) |
---|
1521 | n/a | self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip) |
---|
1522 | n/a | self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, |
---|
1523 | n/a | '2001:1.1.1.1:1.1.1.1') |
---|
1524 | n/a | |
---|
1525 | n/a | # Issue 67: IPv6 with embedded IPv4 address not recognized. |
---|
1526 | n/a | def testIPv6AddressTooLarge(self): |
---|
1527 | n/a | # RFC4291 2.5.5.2 |
---|
1528 | n/a | self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1'), |
---|
1529 | n/a | ipaddress.ip_address('::FFFF:c000:201')) |
---|
1530 | n/a | # RFC4291 2.2 (part 3) x::d.d.d.d |
---|
1531 | n/a | self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'), |
---|
1532 | n/a | ipaddress.ip_address('FFFF::c000:201')) |
---|
1533 | n/a | |
---|
1534 | n/a | def testIPVersion(self): |
---|
1535 | n/a | self.assertEqual(self.ipv4_address.version, 4) |
---|
1536 | n/a | self.assertEqual(self.ipv6_address.version, 6) |
---|
1537 | n/a | |
---|
1538 | n/a | def testMaxPrefixLength(self): |
---|
1539 | n/a | self.assertEqual(self.ipv4_interface.max_prefixlen, 32) |
---|
1540 | n/a | self.assertEqual(self.ipv6_interface.max_prefixlen, 128) |
---|
1541 | n/a | |
---|
1542 | n/a | def testPacked(self): |
---|
1543 | n/a | self.assertEqual(self.ipv4_address.packed, |
---|
1544 | n/a | b'\x01\x02\x03\x04') |
---|
1545 | n/a | self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed, |
---|
1546 | n/a | b'\xff\xfe\xfd\xfc') |
---|
1547 | n/a | self.assertEqual(self.ipv6_address.packed, |
---|
1548 | n/a | b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
---|
1549 | n/a | b'\x02\x00\x00\x00\x00\x00\x00\x01') |
---|
1550 | n/a | self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed, |
---|
1551 | n/a | b'\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff' |
---|
1552 | n/a | + b'\x00' * 6) |
---|
1553 | n/a | self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed, |
---|
1554 | n/a | b'\x00' * 6 + b'\x00\x01' + b'\x00' * 8) |
---|
1555 | n/a | |
---|
1556 | n/a | def testIpType(self): |
---|
1557 | n/a | ipv4net = ipaddress.ip_network('1.2.3.4') |
---|
1558 | n/a | ipv4addr = ipaddress.ip_address('1.2.3.4') |
---|
1559 | n/a | ipv6net = ipaddress.ip_network('::1.2.3.4') |
---|
1560 | n/a | ipv6addr = ipaddress.ip_address('::1.2.3.4') |
---|
1561 | n/a | self.assertEqual(ipaddress.IPv4Network, type(ipv4net)) |
---|
1562 | n/a | self.assertEqual(ipaddress.IPv4Address, type(ipv4addr)) |
---|
1563 | n/a | self.assertEqual(ipaddress.IPv6Network, type(ipv6net)) |
---|
1564 | n/a | self.assertEqual(ipaddress.IPv6Address, type(ipv6addr)) |
---|
1565 | n/a | |
---|
1566 | n/a | def testReservedIpv4(self): |
---|
1567 | n/a | # test networks |
---|
1568 | n/a | self.assertEqual(True, ipaddress.ip_interface( |
---|
1569 | n/a | '224.1.1.1/31').is_multicast) |
---|
1570 | n/a | self.assertEqual(False, ipaddress.ip_network('240.0.0.0').is_multicast) |
---|
1571 | n/a | self.assertEqual(True, ipaddress.ip_network('240.0.0.0').is_reserved) |
---|
1572 | n/a | |
---|
1573 | n/a | self.assertEqual(True, ipaddress.ip_interface( |
---|
1574 | n/a | '192.168.1.1/17').is_private) |
---|
1575 | n/a | self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private) |
---|
1576 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1577 | n/a | '10.255.255.255').is_private) |
---|
1578 | n/a | self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_private) |
---|
1579 | n/a | self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_reserved) |
---|
1580 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1581 | n/a | '172.31.255.255').is_private) |
---|
1582 | n/a | self.assertEqual(False, ipaddress.ip_network('172.32.0.0').is_private) |
---|
1583 | n/a | self.assertEqual(True, |
---|
1584 | n/a | ipaddress.ip_network('169.254.1.0/24').is_link_local) |
---|
1585 | n/a | |
---|
1586 | n/a | self.assertEqual(True, |
---|
1587 | n/a | ipaddress.ip_interface( |
---|
1588 | n/a | '169.254.100.200/24').is_link_local) |
---|
1589 | n/a | self.assertEqual(False, |
---|
1590 | n/a | ipaddress.ip_interface( |
---|
1591 | n/a | '169.255.100.200/24').is_link_local) |
---|
1592 | n/a | |
---|
1593 | n/a | self.assertEqual(True, |
---|
1594 | n/a | ipaddress.ip_network( |
---|
1595 | n/a | '127.100.200.254/32').is_loopback) |
---|
1596 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1597 | n/a | '127.42.0.0/16').is_loopback) |
---|
1598 | n/a | self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback) |
---|
1599 | n/a | self.assertEqual(False, |
---|
1600 | n/a | ipaddress.ip_network('100.64.0.0/10').is_private) |
---|
1601 | n/a | self.assertEqual(False, ipaddress.ip_network('100.64.0.0/10').is_global) |
---|
1602 | n/a | |
---|
1603 | n/a | self.assertEqual(True, |
---|
1604 | n/a | ipaddress.ip_network('192.0.2.128/25').is_private) |
---|
1605 | n/a | self.assertEqual(True, |
---|
1606 | n/a | ipaddress.ip_network('192.0.3.0/24').is_global) |
---|
1607 | n/a | |
---|
1608 | n/a | # test addresses |
---|
1609 | n/a | self.assertEqual(True, ipaddress.ip_address('0.0.0.0').is_unspecified) |
---|
1610 | n/a | self.assertEqual(True, ipaddress.ip_address('224.1.1.1').is_multicast) |
---|
1611 | n/a | self.assertEqual(False, ipaddress.ip_address('240.0.0.0').is_multicast) |
---|
1612 | n/a | self.assertEqual(True, ipaddress.ip_address('240.0.0.1').is_reserved) |
---|
1613 | n/a | self.assertEqual(False, |
---|
1614 | n/a | ipaddress.ip_address('239.255.255.255').is_reserved) |
---|
1615 | n/a | |
---|
1616 | n/a | self.assertEqual(True, ipaddress.ip_address('192.168.1.1').is_private) |
---|
1617 | n/a | self.assertEqual(False, ipaddress.ip_address('192.169.0.0').is_private) |
---|
1618 | n/a | self.assertEqual(True, ipaddress.ip_address( |
---|
1619 | n/a | '10.255.255.255').is_private) |
---|
1620 | n/a | self.assertEqual(False, ipaddress.ip_address('11.0.0.0').is_private) |
---|
1621 | n/a | self.assertEqual(True, ipaddress.ip_address( |
---|
1622 | n/a | '172.31.255.255').is_private) |
---|
1623 | n/a | self.assertEqual(False, ipaddress.ip_address('172.32.0.0').is_private) |
---|
1624 | n/a | |
---|
1625 | n/a | self.assertEqual(True, |
---|
1626 | n/a | ipaddress.ip_address('169.254.100.200').is_link_local) |
---|
1627 | n/a | self.assertEqual(False, |
---|
1628 | n/a | ipaddress.ip_address('169.255.100.200').is_link_local) |
---|
1629 | n/a | |
---|
1630 | n/a | self.assertTrue(ipaddress.ip_address('192.0.7.1').is_global) |
---|
1631 | n/a | self.assertFalse(ipaddress.ip_address('203.0.113.1').is_global) |
---|
1632 | n/a | |
---|
1633 | n/a | self.assertEqual(True, |
---|
1634 | n/a | ipaddress.ip_address('127.100.200.254').is_loopback) |
---|
1635 | n/a | self.assertEqual(True, ipaddress.ip_address('127.42.0.0').is_loopback) |
---|
1636 | n/a | self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback) |
---|
1637 | n/a | self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified) |
---|
1638 | n/a | |
---|
1639 | n/a | def testReservedIpv6(self): |
---|
1640 | n/a | |
---|
1641 | n/a | self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast) |
---|
1642 | n/a | self.assertEqual(True, ipaddress.ip_network(2**128 - 1).is_multicast) |
---|
1643 | n/a | self.assertEqual(True, ipaddress.ip_network('ff00::').is_multicast) |
---|
1644 | n/a | self.assertEqual(False, ipaddress.ip_network('fdff::').is_multicast) |
---|
1645 | n/a | |
---|
1646 | n/a | self.assertEqual(True, ipaddress.ip_network('fecf::').is_site_local) |
---|
1647 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1648 | n/a | 'feff:ffff:ffff:ffff::').is_site_local) |
---|
1649 | n/a | self.assertEqual(False, ipaddress.ip_network( |
---|
1650 | n/a | 'fbf:ffff::').is_site_local) |
---|
1651 | n/a | self.assertEqual(False, ipaddress.ip_network('ff00::').is_site_local) |
---|
1652 | n/a | |
---|
1653 | n/a | self.assertEqual(True, ipaddress.ip_network('fc00::').is_private) |
---|
1654 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1655 | n/a | 'fc00:ffff:ffff:ffff::').is_private) |
---|
1656 | n/a | self.assertEqual(False, ipaddress.ip_network('fbff:ffff::').is_private) |
---|
1657 | n/a | self.assertEqual(False, ipaddress.ip_network('fe00::').is_private) |
---|
1658 | n/a | |
---|
1659 | n/a | self.assertEqual(True, ipaddress.ip_network('fea0::').is_link_local) |
---|
1660 | n/a | self.assertEqual(True, ipaddress.ip_network( |
---|
1661 | n/a | 'febf:ffff::').is_link_local) |
---|
1662 | n/a | self.assertEqual(False, ipaddress.ip_network( |
---|
1663 | n/a | 'fe7f:ffff::').is_link_local) |
---|
1664 | n/a | self.assertEqual(False, ipaddress.ip_network('fec0::').is_link_local) |
---|
1665 | n/a | |
---|
1666 | n/a | self.assertEqual(True, ipaddress.ip_interface('0:0::0:01').is_loopback) |
---|
1667 | n/a | self.assertEqual(False, ipaddress.ip_interface('::1/127').is_loopback) |
---|
1668 | n/a | self.assertEqual(False, ipaddress.ip_network('::').is_loopback) |
---|
1669 | n/a | self.assertEqual(False, ipaddress.ip_network('::2').is_loopback) |
---|
1670 | n/a | |
---|
1671 | n/a | self.assertEqual(True, ipaddress.ip_network('0::0').is_unspecified) |
---|
1672 | n/a | self.assertEqual(False, ipaddress.ip_network('::1').is_unspecified) |
---|
1673 | n/a | self.assertEqual(False, ipaddress.ip_network('::/127').is_unspecified) |
---|
1674 | n/a | |
---|
1675 | n/a | self.assertEqual(True, |
---|
1676 | n/a | ipaddress.ip_network('2001::1/128').is_private) |
---|
1677 | n/a | self.assertEqual(True, |
---|
1678 | n/a | ipaddress.ip_network('200::1/128').is_global) |
---|
1679 | n/a | # test addresses |
---|
1680 | n/a | self.assertEqual(True, ipaddress.ip_address('ffff::').is_multicast) |
---|
1681 | n/a | self.assertEqual(True, ipaddress.ip_address(2**128 - 1).is_multicast) |
---|
1682 | n/a | self.assertEqual(True, ipaddress.ip_address('ff00::').is_multicast) |
---|
1683 | n/a | self.assertEqual(False, ipaddress.ip_address('fdff::').is_multicast) |
---|
1684 | n/a | |
---|
1685 | n/a | self.assertEqual(True, ipaddress.ip_address('fecf::').is_site_local) |
---|
1686 | n/a | self.assertEqual(True, ipaddress.ip_address( |
---|
1687 | n/a | 'feff:ffff:ffff:ffff::').is_site_local) |
---|
1688 | n/a | self.assertEqual(False, ipaddress.ip_address( |
---|
1689 | n/a | 'fbf:ffff::').is_site_local) |
---|
1690 | n/a | self.assertEqual(False, ipaddress.ip_address('ff00::').is_site_local) |
---|
1691 | n/a | |
---|
1692 | n/a | self.assertEqual(True, ipaddress.ip_address('fc00::').is_private) |
---|
1693 | n/a | self.assertEqual(True, ipaddress.ip_address( |
---|
1694 | n/a | 'fc00:ffff:ffff:ffff::').is_private) |
---|
1695 | n/a | self.assertEqual(False, ipaddress.ip_address('fbff:ffff::').is_private) |
---|
1696 | n/a | self.assertEqual(False, ipaddress.ip_address('fe00::').is_private) |
---|
1697 | n/a | |
---|
1698 | n/a | self.assertEqual(True, ipaddress.ip_address('fea0::').is_link_local) |
---|
1699 | n/a | self.assertEqual(True, ipaddress.ip_address( |
---|
1700 | n/a | 'febf:ffff::').is_link_local) |
---|
1701 | n/a | self.assertEqual(False, ipaddress.ip_address( |
---|
1702 | n/a | 'fe7f:ffff::').is_link_local) |
---|
1703 | n/a | self.assertEqual(False, ipaddress.ip_address('fec0::').is_link_local) |
---|
1704 | n/a | |
---|
1705 | n/a | self.assertEqual(True, ipaddress.ip_address('0:0::0:01').is_loopback) |
---|
1706 | n/a | self.assertEqual(True, ipaddress.ip_address('::1').is_loopback) |
---|
1707 | n/a | self.assertEqual(False, ipaddress.ip_address('::2').is_loopback) |
---|
1708 | n/a | |
---|
1709 | n/a | self.assertEqual(True, ipaddress.ip_address('0::0').is_unspecified) |
---|
1710 | n/a | self.assertEqual(False, ipaddress.ip_address('::1').is_unspecified) |
---|
1711 | n/a | |
---|
1712 | n/a | # some generic IETF reserved addresses |
---|
1713 | n/a | self.assertEqual(True, ipaddress.ip_address('100::').is_reserved) |
---|
1714 | n/a | self.assertEqual(True, ipaddress.ip_network('4000::1/128').is_reserved) |
---|
1715 | n/a | |
---|
1716 | n/a | def testIpv4Mapped(self): |
---|
1717 | n/a | self.assertEqual( |
---|
1718 | n/a | ipaddress.ip_address('::ffff:192.168.1.1').ipv4_mapped, |
---|
1719 | n/a | ipaddress.ip_address('192.168.1.1')) |
---|
1720 | n/a | self.assertEqual(ipaddress.ip_address('::c0a8:101').ipv4_mapped, None) |
---|
1721 | n/a | self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped, |
---|
1722 | n/a | ipaddress.ip_address('192.168.1.1')) |
---|
1723 | n/a | |
---|
1724 | n/a | def testAddrExclude(self): |
---|
1725 | n/a | addr1 = ipaddress.ip_network('10.1.1.0/24') |
---|
1726 | n/a | addr2 = ipaddress.ip_network('10.1.1.0/26') |
---|
1727 | n/a | addr3 = ipaddress.ip_network('10.2.1.0/24') |
---|
1728 | n/a | addr4 = ipaddress.ip_address('10.1.1.0') |
---|
1729 | n/a | addr5 = ipaddress.ip_network('2001:db8::0/32') |
---|
1730 | n/a | addr6 = ipaddress.ip_network('10.1.1.5/32') |
---|
1731 | n/a | self.assertEqual(sorted(list(addr1.address_exclude(addr2))), |
---|
1732 | n/a | [ipaddress.ip_network('10.1.1.64/26'), |
---|
1733 | n/a | ipaddress.ip_network('10.1.1.128/25')]) |
---|
1734 | n/a | self.assertRaises(ValueError, list, addr1.address_exclude(addr3)) |
---|
1735 | n/a | self.assertRaises(TypeError, list, addr1.address_exclude(addr4)) |
---|
1736 | n/a | self.assertRaises(TypeError, list, addr1.address_exclude(addr5)) |
---|
1737 | n/a | self.assertEqual(list(addr1.address_exclude(addr1)), []) |
---|
1738 | n/a | self.assertEqual(sorted(list(addr1.address_exclude(addr6))), |
---|
1739 | n/a | [ipaddress.ip_network('10.1.1.0/30'), |
---|
1740 | n/a | ipaddress.ip_network('10.1.1.4/32'), |
---|
1741 | n/a | ipaddress.ip_network('10.1.1.6/31'), |
---|
1742 | n/a | ipaddress.ip_network('10.1.1.8/29'), |
---|
1743 | n/a | ipaddress.ip_network('10.1.1.16/28'), |
---|
1744 | n/a | ipaddress.ip_network('10.1.1.32/27'), |
---|
1745 | n/a | ipaddress.ip_network('10.1.1.64/26'), |
---|
1746 | n/a | ipaddress.ip_network('10.1.1.128/25')]) |
---|
1747 | n/a | |
---|
1748 | n/a | def testHash(self): |
---|
1749 | n/a | self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')), |
---|
1750 | n/a | hash(ipaddress.ip_interface('10.1.1.0/24'))) |
---|
1751 | n/a | self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')), |
---|
1752 | n/a | hash(ipaddress.ip_network('10.1.1.0/24'))) |
---|
1753 | n/a | self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')), |
---|
1754 | n/a | hash(ipaddress.ip_address('10.1.1.0'))) |
---|
1755 | n/a | # i70 |
---|
1756 | n/a | self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')), |
---|
1757 | n/a | hash(ipaddress.ip_address( |
---|
1758 | n/a | int(ipaddress.ip_address('1.2.3.4')._ip)))) |
---|
1759 | n/a | ip1 = ipaddress.ip_address('10.1.1.0') |
---|
1760 | n/a | ip2 = ipaddress.ip_address('1::') |
---|
1761 | n/a | dummy = {} |
---|
1762 | n/a | dummy[self.ipv4_address] = None |
---|
1763 | n/a | dummy[self.ipv6_address] = None |
---|
1764 | n/a | dummy[ip1] = None |
---|
1765 | n/a | dummy[ip2] = None |
---|
1766 | n/a | self.assertIn(self.ipv4_address, dummy) |
---|
1767 | n/a | self.assertIn(ip2, dummy) |
---|
1768 | n/a | |
---|
1769 | n/a | def testIPBases(self): |
---|
1770 | n/a | net = self.ipv4_network |
---|
1771 | n/a | self.assertEqual('1.2.3.0/24', net.compressed) |
---|
1772 | n/a | net = self.ipv6_network |
---|
1773 | n/a | self.assertRaises(ValueError, net._string_from_ip_int, 2**128 + 1) |
---|
1774 | n/a | |
---|
1775 | n/a | def testIPv6NetworkHelpers(self): |
---|
1776 | n/a | net = self.ipv6_network |
---|
1777 | n/a | self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen) |
---|
1778 | n/a | self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::', |
---|
1779 | n/a | net.with_netmask) |
---|
1780 | n/a | self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff', |
---|
1781 | n/a | net.with_hostmask) |
---|
1782 | n/a | self.assertEqual('2001:658:22a:cafe::/64', str(net)) |
---|
1783 | n/a | |
---|
1784 | n/a | def testIPv4NetworkHelpers(self): |
---|
1785 | n/a | net = self.ipv4_network |
---|
1786 | n/a | self.assertEqual('1.2.3.0/24', net.with_prefixlen) |
---|
1787 | n/a | self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask) |
---|
1788 | n/a | self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask) |
---|
1789 | n/a | self.assertEqual('1.2.3.0/24', str(net)) |
---|
1790 | n/a | |
---|
1791 | n/a | def testCopyConstructor(self): |
---|
1792 | n/a | addr1 = ipaddress.ip_network('10.1.1.0/24') |
---|
1793 | n/a | addr2 = ipaddress.ip_network(addr1) |
---|
1794 | n/a | addr3 = ipaddress.ip_interface('2001:658:22a:cafe:200::1/64') |
---|
1795 | n/a | addr4 = ipaddress.ip_interface(addr3) |
---|
1796 | n/a | addr5 = ipaddress.IPv4Address('1.1.1.1') |
---|
1797 | n/a | addr6 = ipaddress.IPv6Address('2001:658:22a:cafe:200::1') |
---|
1798 | n/a | |
---|
1799 | n/a | self.assertEqual(addr1, addr2) |
---|
1800 | n/a | self.assertEqual(addr3, addr4) |
---|
1801 | n/a | self.assertEqual(addr5, ipaddress.IPv4Address(addr5)) |
---|
1802 | n/a | self.assertEqual(addr6, ipaddress.IPv6Address(addr6)) |
---|
1803 | n/a | |
---|
1804 | n/a | def testCompressIPv6Address(self): |
---|
1805 | n/a | test_addresses = { |
---|
1806 | n/a | '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128', |
---|
1807 | n/a | '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128', |
---|
1808 | n/a | '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128', |
---|
1809 | n/a | '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128', |
---|
1810 | n/a | '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128', |
---|
1811 | n/a | '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128', |
---|
1812 | n/a | '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128', |
---|
1813 | n/a | '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128', |
---|
1814 | n/a | '0:0:0:0:0:0:0:0': '::/128', |
---|
1815 | n/a | '0:0:0:0:0:0:0:0/0': '::/0', |
---|
1816 | n/a | '0:0:0:0:0:0:0:1': '::1/128', |
---|
1817 | n/a | '2001:0658:022a:cafe:0000:0000:0000:0000/66': |
---|
1818 | n/a | '2001:658:22a:cafe::/66', |
---|
1819 | n/a | '::1.2.3.4': '::102:304/128', |
---|
1820 | n/a | '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128', |
---|
1821 | n/a | '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128', |
---|
1822 | n/a | '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128', |
---|
1823 | n/a | '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128', |
---|
1824 | n/a | '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128', |
---|
1825 | n/a | } |
---|
1826 | n/a | for uncompressed, compressed in list(test_addresses.items()): |
---|
1827 | n/a | self.assertEqual(compressed, str(ipaddress.IPv6Interface( |
---|
1828 | n/a | uncompressed))) |
---|
1829 | n/a | |
---|
1830 | n/a | def testExplodeShortHandIpStr(self): |
---|
1831 | n/a | addr1 = ipaddress.IPv6Interface('2001::1') |
---|
1832 | n/a | addr2 = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') |
---|
1833 | n/a | addr3 = ipaddress.IPv6Network('2001::/96') |
---|
1834 | n/a | addr4 = ipaddress.IPv4Address('192.168.178.1') |
---|
1835 | n/a | self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128', |
---|
1836 | n/a | addr1.exploded) |
---|
1837 | n/a | self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128', |
---|
1838 | n/a | ipaddress.IPv6Interface('::1/128').exploded) |
---|
1839 | n/a | # issue 77 |
---|
1840 | n/a | self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1', |
---|
1841 | n/a | addr2.exploded) |
---|
1842 | n/a | self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0000/96', |
---|
1843 | n/a | addr3.exploded) |
---|
1844 | n/a | self.assertEqual('192.168.178.1', addr4.exploded) |
---|
1845 | n/a | |
---|
1846 | n/a | def testReversePointer(self): |
---|
1847 | n/a | addr1 = ipaddress.IPv4Address('127.0.0.1') |
---|
1848 | n/a | addr2 = ipaddress.IPv6Address('2001:db8::1') |
---|
1849 | n/a | self.assertEqual('1.0.0.127.in-addr.arpa', addr1.reverse_pointer) |
---|
1850 | n/a | self.assertEqual('1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.' + |
---|
1851 | n/a | 'b.d.0.1.0.0.2.ip6.arpa', |
---|
1852 | n/a | addr2.reverse_pointer) |
---|
1853 | n/a | |
---|
1854 | n/a | def testIntRepresentation(self): |
---|
1855 | n/a | self.assertEqual(16909060, int(self.ipv4_address)) |
---|
1856 | n/a | self.assertEqual(42540616829182469433547762482097946625, |
---|
1857 | n/a | int(self.ipv6_address)) |
---|
1858 | n/a | |
---|
1859 | n/a | def testForceVersion(self): |
---|
1860 | n/a | self.assertEqual(ipaddress.ip_network(1).version, 4) |
---|
1861 | n/a | self.assertEqual(ipaddress.IPv6Network(1).version, 6) |
---|
1862 | n/a | |
---|
1863 | n/a | def testWithStar(self): |
---|
1864 | n/a | self.assertEqual(self.ipv4_interface.with_prefixlen, "1.2.3.4/24") |
---|
1865 | n/a | self.assertEqual(self.ipv4_interface.with_netmask, |
---|
1866 | n/a | "1.2.3.4/255.255.255.0") |
---|
1867 | n/a | self.assertEqual(self.ipv4_interface.with_hostmask, |
---|
1868 | n/a | "1.2.3.4/0.0.0.255") |
---|
1869 | n/a | |
---|
1870 | n/a | self.assertEqual(self.ipv6_interface.with_prefixlen, |
---|
1871 | n/a | '2001:658:22a:cafe:200::1/64') |
---|
1872 | n/a | self.assertEqual(self.ipv6_interface.with_netmask, |
---|
1873 | n/a | '2001:658:22a:cafe:200::1/ffff:ffff:ffff:ffff::') |
---|
1874 | n/a | # this probably don't make much sense, but it's included for |
---|
1875 | n/a | # compatibility with ipv4 |
---|
1876 | n/a | self.assertEqual(self.ipv6_interface.with_hostmask, |
---|
1877 | n/a | '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff') |
---|
1878 | n/a | |
---|
1879 | n/a | def testNetworkElementCaching(self): |
---|
1880 | n/a | # V4 - make sure we're empty |
---|
1881 | n/a | self.assertNotIn('network_address', self.ipv4_network._cache) |
---|
1882 | n/a | self.assertNotIn('broadcast_address', self.ipv4_network._cache) |
---|
1883 | n/a | self.assertNotIn('hostmask', self.ipv4_network._cache) |
---|
1884 | n/a | |
---|
1885 | n/a | # V4 - populate and test |
---|
1886 | n/a | self.assertEqual(self.ipv4_network.network_address, |
---|
1887 | n/a | ipaddress.IPv4Address('1.2.3.0')) |
---|
1888 | n/a | self.assertEqual(self.ipv4_network.broadcast_address, |
---|
1889 | n/a | ipaddress.IPv4Address('1.2.3.255')) |
---|
1890 | n/a | self.assertEqual(self.ipv4_network.hostmask, |
---|
1891 | n/a | ipaddress.IPv4Address('0.0.0.255')) |
---|
1892 | n/a | |
---|
1893 | n/a | # V4 - check we're cached |
---|
1894 | n/a | self.assertIn('broadcast_address', self.ipv4_network._cache) |
---|
1895 | n/a | self.assertIn('hostmask', self.ipv4_network._cache) |
---|
1896 | n/a | |
---|
1897 | n/a | # V6 - make sure we're empty |
---|
1898 | n/a | self.assertNotIn('broadcast_address', self.ipv6_network._cache) |
---|
1899 | n/a | self.assertNotIn('hostmask', self.ipv6_network._cache) |
---|
1900 | n/a | |
---|
1901 | n/a | # V6 - populate and test |
---|
1902 | n/a | self.assertEqual(self.ipv6_network.network_address, |
---|
1903 | n/a | ipaddress.IPv6Address('2001:658:22a:cafe::')) |
---|
1904 | n/a | self.assertEqual(self.ipv6_interface.network.network_address, |
---|
1905 | n/a | ipaddress.IPv6Address('2001:658:22a:cafe::')) |
---|
1906 | n/a | |
---|
1907 | n/a | self.assertEqual( |
---|
1908 | n/a | self.ipv6_network.broadcast_address, |
---|
1909 | n/a | ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) |
---|
1910 | n/a | self.assertEqual(self.ipv6_network.hostmask, |
---|
1911 | n/a | ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) |
---|
1912 | n/a | self.assertEqual( |
---|
1913 | n/a | self.ipv6_interface.network.broadcast_address, |
---|
1914 | n/a | ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) |
---|
1915 | n/a | self.assertEqual(self.ipv6_interface.network.hostmask, |
---|
1916 | n/a | ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) |
---|
1917 | n/a | |
---|
1918 | n/a | # V6 - check we're cached |
---|
1919 | n/a | self.assertIn('broadcast_address', self.ipv6_network._cache) |
---|
1920 | n/a | self.assertIn('hostmask', self.ipv6_network._cache) |
---|
1921 | n/a | self.assertIn('broadcast_address', self.ipv6_interface.network._cache) |
---|
1922 | n/a | self.assertIn('hostmask', self.ipv6_interface.network._cache) |
---|
1923 | n/a | |
---|
1924 | n/a | def testTeredo(self): |
---|
1925 | n/a | # stolen from wikipedia |
---|
1926 | n/a | server = ipaddress.IPv4Address('65.54.227.120') |
---|
1927 | n/a | client = ipaddress.IPv4Address('192.0.2.45') |
---|
1928 | n/a | teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2' |
---|
1929 | n/a | self.assertEqual((server, client), |
---|
1930 | n/a | ipaddress.ip_address(teredo_addr).teredo) |
---|
1931 | n/a | bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2' |
---|
1932 | n/a | self.assertFalse(ipaddress.ip_address(bad_addr).teredo) |
---|
1933 | n/a | bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2' |
---|
1934 | n/a | self.assertFalse(ipaddress.ip_address(bad_addr).teredo) |
---|
1935 | n/a | |
---|
1936 | n/a | # i77 |
---|
1937 | n/a | teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') |
---|
1938 | n/a | self.assertEqual((ipaddress.IPv4Address('94.245.121.253'), |
---|
1939 | n/a | ipaddress.IPv4Address('95.26.244.94')), |
---|
1940 | n/a | teredo_addr.teredo) |
---|
1941 | n/a | |
---|
1942 | n/a | def testsixtofour(self): |
---|
1943 | n/a | sixtofouraddr = ipaddress.ip_address('2002:ac1d:2d64::1') |
---|
1944 | n/a | bad_addr = ipaddress.ip_address('2000:ac1d:2d64::1') |
---|
1945 | n/a | self.assertEqual(ipaddress.IPv4Address('172.29.45.100'), |
---|
1946 | n/a | sixtofouraddr.sixtofour) |
---|
1947 | n/a | self.assertFalse(bad_addr.sixtofour) |
---|
1948 | n/a | |
---|
1949 | n/a | |
---|
1950 | n/a | if __name__ == '__main__': |
---|
1951 | n/a | unittest.main() |
---|