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