ยปCore Development>Code coverage>Lib/test/test_ipaddress.py

Python code coverage for Lib/test/test_ipaddress.py

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