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 |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 10 | import ipaddress |
| 11 | |
Nick Coghlan | 36f8dcd | 2012-07-07 19:23:53 +1000 | [diff] [blame^] | 12 | class ErrorReporting(unittest.TestCase): |
| 13 | # One big change in ipaddress over the original ipaddr module is |
| 14 | # error reporting that tries to assume users *don't know the rules* |
| 15 | # for what constitutes an RFC compliant IP address |
| 16 | |
| 17 | # Note that if the constructors are refactored so that addresses with |
| 18 | # multiple problems get classified differently, that's OK - just |
| 19 | # move the affected examples to the newly appropriate test case. |
| 20 | |
| 21 | # The error reporting tests also cover a few corner cases that |
| 22 | # specifically *aren't* errors (such as leading zeroes in v6 |
| 23 | # address parts) that don't have an obvious home in the main test |
| 24 | # suite |
| 25 | |
| 26 | @contextlib.contextmanager |
| 27 | def assertCleanError(self, exc_type, details, *args): |
| 28 | """ |
| 29 | Ensure exception does not display a context by default |
| 30 | |
| 31 | Wraps unittest.TestCase.assertRaisesRegex |
| 32 | """ |
| 33 | if args: |
| 34 | details = details % args |
| 35 | cm = self.assertRaisesRegex(exc_type, details) |
| 36 | with cm as exc: |
| 37 | yield exc |
| 38 | # Ensure we produce clean tracebacks on failure |
| 39 | if exc.exception.__context__ is not None: |
| 40 | self.assertTrue(exc.exception.__suppress_context__) |
| 41 | |
| 42 | def assertAddressError(self, details, *args): |
| 43 | """Ensure a clean AddressValueError""" |
| 44 | return self.assertCleanError(ipaddress.AddressValueError, |
| 45 | details, *args) |
| 46 | |
| 47 | def assertNetmaskError(self, details, *args): |
| 48 | """Ensure a clean NetmaskValueError""" |
| 49 | return self.assertCleanError(ipaddress.NetmaskValueError, |
| 50 | details, *args) |
| 51 | |
| 52 | class AddressErrors_v4(ErrorReporting): |
| 53 | |
| 54 | def test_empty_address(self): |
| 55 | with self.assertAddressError("Address cannot be empty"): |
| 56 | ipaddress.IPv4Address("") |
| 57 | |
| 58 | def test_network_passed_as_address(self): |
| 59 | addr = "127.0.0.1/24" |
| 60 | with self.assertAddressError("Unexpected '/' in %r", addr): |
| 61 | ipaddress.IPv4Address(addr) |
| 62 | |
| 63 | def test_bad_address_split(self): |
| 64 | def assertBadSplit(addr): |
| 65 | with self.assertAddressError("Expected 4 octets in %r", addr): |
| 66 | ipaddress.IPv4Address(addr) |
| 67 | |
| 68 | assertBadSplit("127.0.1") |
| 69 | assertBadSplit("42.42.42.42.42") |
| 70 | assertBadSplit("42.42.42") |
| 71 | assertBadSplit("42.42") |
| 72 | assertBadSplit("42") |
| 73 | assertBadSplit("42..42.42.42") |
| 74 | assertBadSplit("42.42.42.42.") |
| 75 | assertBadSplit("42.42.42.42...") |
| 76 | assertBadSplit(".42.42.42.42") |
| 77 | assertBadSplit("...42.42.42.42") |
| 78 | assertBadSplit("016.016.016") |
| 79 | assertBadSplit("016.016") |
| 80 | assertBadSplit("016") |
| 81 | assertBadSplit("000") |
| 82 | assertBadSplit("0x0a.0x0a.0x0a") |
| 83 | assertBadSplit("0x0a.0x0a") |
| 84 | assertBadSplit("0x0a") |
| 85 | assertBadSplit(".") |
| 86 | assertBadSplit("bogus") |
| 87 | assertBadSplit("bogus.com") |
| 88 | assertBadSplit("1000") |
| 89 | assertBadSplit("1000000000000000") |
| 90 | assertBadSplit("192.168.0.1.com") |
| 91 | |
| 92 | def test_empty_octet(self): |
| 93 | def assertBadOctet(addr): |
| 94 | with self.assertAddressError("Empty octet not permitted in %r", |
| 95 | addr): |
| 96 | ipaddress.IPv4Address(addr) |
| 97 | |
| 98 | assertBadOctet("42..42.42") |
| 99 | assertBadOctet("...") |
| 100 | |
| 101 | def test_invalid_characters(self): |
| 102 | def assertBadOctet(addr, octet): |
| 103 | msg = "Only decimal digits permitted in %r in %r" % (octet, addr) |
| 104 | with self.assertAddressError(re.escape(msg)): |
| 105 | ipaddress.IPv4Address(addr) |
| 106 | |
| 107 | assertBadOctet("0x0a.0x0a.0x0a.0x0a", "0x0a") |
| 108 | assertBadOctet("42.42.42.-0", "-0") |
| 109 | assertBadOctet("42.42.42.+0", "+0") |
| 110 | assertBadOctet("42.42.42.-42", "-42") |
| 111 | assertBadOctet("+1.+2.+3.4", "+1") |
| 112 | assertBadOctet("1.2.3.4e0", "4e0") |
| 113 | assertBadOctet("1.2.3.4::", "4::") |
| 114 | assertBadOctet("1.a.2.3", "a") |
| 115 | |
| 116 | def test_leading_zeros(self): |
| 117 | def assertBadOctet(addr, octet): |
| 118 | msg = "Ambiguous leading zero in %r not permitted in %r" |
| 119 | with self.assertAddressError(msg, octet, addr): |
| 120 | ipaddress.IPv4Address(addr) |
| 121 | |
| 122 | assertBadOctet("016.016.016.016", "016") |
| 123 | assertBadOctet("001.000.008.016", "008") |
| 124 | self.assertEqual(ipaddress.IPv4Address("192.168.000.001"), |
| 125 | ipaddress.IPv4Address("192.168.0.1")) |
| 126 | |
| 127 | def test_octet_limit(self): |
| 128 | def assertBadOctet(addr, octet): |
| 129 | msg = "Octet %d > 255 not permitted in %r" |
| 130 | with self.assertAddressError(msg, octet, addr): |
| 131 | ipaddress.IPv4Address(addr) |
| 132 | |
| 133 | assertBadOctet("12345.67899.-54321.-98765", 12345) |
| 134 | assertBadOctet("257.0.0.0", 257) |
| 135 | |
| 136 | def test_bad_packed_length(self): |
| 137 | def assertBadLength(length): |
| 138 | addr = b'\x00' * length |
| 139 | msg = "Packed address %r must be exactly 4 bytes" % addr |
| 140 | with self.assertAddressError(re.escape(msg)): |
| 141 | ipaddress.IPv4Address(addr) |
| 142 | |
| 143 | assertBadLength(3) |
| 144 | assertBadLength(5) |
| 145 | |
| 146 | |
| 147 | class AddressErrors_v6(ErrorReporting): |
| 148 | |
| 149 | def test_empty_address(self): |
| 150 | with self.assertAddressError("Address cannot be empty"): |
| 151 | ipaddress.IPv6Address("") |
| 152 | |
| 153 | def test_network_passed_as_address(self): |
| 154 | addr = "::1/24" |
| 155 | with self.assertAddressError("Unexpected '/' in %r", addr): |
| 156 | ipaddress.IPv6Address(addr) |
| 157 | |
| 158 | def test_bad_address_split_v6_not_enough_parts(self): |
| 159 | def assertBadSplit(addr): |
| 160 | msg = "At least 3 parts expected in %r" |
| 161 | with self.assertAddressError(msg, addr): |
| 162 | ipaddress.IPv6Address(addr) |
| 163 | |
| 164 | assertBadSplit(":") |
| 165 | assertBadSplit(":1") |
| 166 | assertBadSplit("FEDC:9878") |
| 167 | |
| 168 | def test_bad_address_split_v6_too_many_colons(self): |
| 169 | def assertBadSplit(addr): |
| 170 | msg = "At most 8 colons permitted in %r" |
| 171 | with self.assertAddressError(msg, addr): |
| 172 | ipaddress.IPv6Address(addr) |
| 173 | |
| 174 | assertBadSplit("9:8:7:6:5:4:3::2:1") |
| 175 | assertBadSplit("10:9:8:7:6:5:4:3:2:1") |
| 176 | assertBadSplit("::8:7:6:5:4:3:2:1") |
| 177 | assertBadSplit("8:7:6:5:4:3:2:1::") |
| 178 | # A trailing IPv4 address is two parts |
| 179 | assertBadSplit("10:9:8:7:6:5:4:3:42.42.42.42") |
| 180 | |
| 181 | def test_bad_address_split_v6_too_many_parts(self): |
| 182 | def assertBadSplit(addr): |
| 183 | msg = "Exactly 8 parts expected without '::' in %r" |
| 184 | with self.assertAddressError(msg, addr): |
| 185 | ipaddress.IPv6Address(addr) |
| 186 | |
| 187 | assertBadSplit("3ffe:0:0:0:0:0:0:0:1") |
| 188 | assertBadSplit("9:8:7:6:5:4:3:2:1") |
| 189 | assertBadSplit("7:6:5:4:3:2:1") |
| 190 | # A trailing IPv4 address is two parts |
| 191 | assertBadSplit("9:8:7:6:5:4:3:42.42.42.42") |
| 192 | assertBadSplit("7:6:5:4:3:42.42.42.42") |
| 193 | |
| 194 | def test_bad_address_split_v6_too_many_parts_with_double_colon(self): |
| 195 | def assertBadSplit(addr): |
| 196 | msg = "Expected at most 7 other parts with '::' in %r" |
| 197 | with self.assertAddressError(msg, addr): |
| 198 | ipaddress.IPv6Address(addr) |
| 199 | |
| 200 | assertBadSplit("1:2:3:4::5:6:7:8") |
| 201 | |
| 202 | def test_bad_address_split_v6_repeated_double_colon(self): |
| 203 | def assertBadSplit(addr): |
| 204 | msg = "At most one '::' permitted in %r" |
| 205 | with self.assertAddressError(msg, addr): |
| 206 | ipaddress.IPv6Address(addr) |
| 207 | |
| 208 | assertBadSplit("3ffe::1::1") |
| 209 | assertBadSplit("1::2::3::4:5") |
| 210 | assertBadSplit("2001::db:::1") |
| 211 | assertBadSplit("3ffe::1::") |
| 212 | assertBadSplit("::3ffe::1") |
| 213 | assertBadSplit(":3ffe::1::1") |
| 214 | assertBadSplit("3ffe::1::1:") |
| 215 | assertBadSplit(":3ffe::1::1:") |
| 216 | assertBadSplit(":::") |
| 217 | assertBadSplit('2001:db8:::1') |
| 218 | |
| 219 | def test_bad_address_split_v6_leading_colon(self): |
| 220 | def assertBadSplit(addr): |
| 221 | msg = "Leading ':' only permitted as part of '::' in %r" |
| 222 | with self.assertAddressError(msg, addr): |
| 223 | ipaddress.IPv6Address(addr) |
| 224 | |
| 225 | assertBadSplit(":2001:db8::1") |
| 226 | assertBadSplit(":1:2:3:4:5:6:7") |
| 227 | assertBadSplit(":1:2:3:4:5:6:") |
| 228 | assertBadSplit(":6:5:4:3:2:1::") |
| 229 | |
| 230 | def test_bad_address_split_v6_trailing_colon(self): |
| 231 | def assertBadSplit(addr): |
| 232 | msg = "Trailing ':' only permitted as part of '::' in %r" |
| 233 | with self.assertAddressError(msg, addr): |
| 234 | ipaddress.IPv6Address(addr) |
| 235 | |
| 236 | assertBadSplit("2001:db8::1:") |
| 237 | assertBadSplit("1:2:3:4:5:6:7:") |
| 238 | assertBadSplit("::1.2.3.4:") |
| 239 | assertBadSplit("::7:6:5:4:3:2:") |
| 240 | |
| 241 | def test_bad_v4_part_in(self): |
| 242 | def assertBadAddressPart(addr, v4_error): |
| 243 | with self.assertAddressError("%s in %r", v4_error, addr): |
| 244 | ipaddress.IPv6Address(addr) |
| 245 | |
| 246 | assertBadAddressPart("3ffe::1.net", "Expected 4 octets in '1.net'") |
| 247 | assertBadAddressPart("3ffe::127.0.1", |
| 248 | "Expected 4 octets in '127.0.1'") |
| 249 | assertBadAddressPart("::1.2.3", |
| 250 | "Expected 4 octets in '1.2.3'") |
| 251 | assertBadAddressPart("::1.2.3.4.5", |
| 252 | "Expected 4 octets in '1.2.3.4.5'") |
| 253 | assertBadAddressPart("3ffe::1.1.1.net", |
| 254 | "Only decimal digits permitted in 'net' " |
| 255 | "in '1.1.1.net'") |
| 256 | |
| 257 | def test_invalid_characters(self): |
| 258 | def assertBadPart(addr, part): |
| 259 | msg = "Only hex digits permitted in %r in %r" % (part, addr) |
| 260 | with self.assertAddressError(re.escape(msg)): |
| 261 | ipaddress.IPv6Address(addr) |
| 262 | |
| 263 | assertBadPart("3ffe::goog", "goog") |
| 264 | assertBadPart("3ffe::-0", "-0") |
| 265 | assertBadPart("3ffe::+0", "+0") |
| 266 | assertBadPart("3ffe::-1", "-1") |
| 267 | assertBadPart("1.2.3.4::", "1.2.3.4") |
| 268 | assertBadPart('1234:axy::b', "axy") |
| 269 | |
| 270 | def test_part_length(self): |
| 271 | def assertBadPart(addr, part): |
| 272 | msg = "At most 4 characters permitted in %r in %r" |
| 273 | with self.assertAddressError(msg, part, addr): |
| 274 | ipaddress.IPv6Address(addr) |
| 275 | |
| 276 | assertBadPart("3ffe::10000", "10000") |
| 277 | assertBadPart("02001:db8::", "02001") |
| 278 | assertBadPart('2001:888888::1', "888888") |
| 279 | |
| 280 | def test_bad_packed_length(self): |
| 281 | def assertBadLength(length): |
| 282 | addr = b'\x00' * length |
| 283 | msg = "Packed address %r must be exactly 16 bytes" % addr |
| 284 | with self.assertAddressError(re.escape(msg)): |
| 285 | ipaddress.IPv6Address(addr) |
| 286 | |
| 287 | assertBadLength(15) |
| 288 | assertBadLength(17) |
| 289 | |
| 290 | |
| 291 | class NetmaskErrorsMixin_v4: |
| 292 | """Input validation on interfaces and networks is very similar""" |
| 293 | |
| 294 | @property |
| 295 | def factory(self): |
| 296 | raise NotImplementedError |
| 297 | |
| 298 | def test_split_netmask(self): |
| 299 | addr = "1.2.3.4/32/24" |
| 300 | with self.assertAddressError("Only one '/' permitted in %r" % addr): |
| 301 | self.factory(addr) |
| 302 | |
| 303 | def test_address_errors(self): |
| 304 | def assertBadAddress(addr, details): |
| 305 | with self.assertAddressError(details): |
| 306 | self.factory(addr) |
| 307 | |
| 308 | assertBadAddress("", "Address cannot be empty") |
| 309 | assertBadAddress("bogus", "Expected 4 octets") |
| 310 | assertBadAddress("google.com", "Expected 4 octets") |
| 311 | assertBadAddress("10/8", "Expected 4 octets") |
| 312 | assertBadAddress("::1.2.3.4", "Only decimal digits") |
| 313 | assertBadAddress("1.2.3.256", "256 > 255") |
| 314 | |
| 315 | def test_netmask_errors(self): |
| 316 | def assertBadNetmask(addr, netmask): |
| 317 | msg = "%r is not a valid netmask" |
| 318 | with self.assertNetmaskError(msg % netmask): |
| 319 | self.factory("%s/%s" % (addr, netmask)) |
| 320 | |
| 321 | assertBadNetmask("1.2.3.4", "") |
| 322 | assertBadNetmask("1.2.3.4", "33") |
| 323 | assertBadNetmask("1.2.3.4", "254.254.255.256") |
| 324 | assertBadNetmask("1.1.1.1", "240.255.0.0") |
| 325 | assertBadNetmask("1.1.1.1", "1.a.2.3") |
| 326 | assertBadNetmask("1.1.1.1", "pudding") |
| 327 | |
| 328 | def test_bad_packed_length(self): |
| 329 | def assertBadLength(length): |
| 330 | addr = b'\x00' * length |
| 331 | msg = "Packed address %r must be exactly 4 bytes" % addr |
| 332 | with self.assertAddressError(re.escape(msg)): |
| 333 | self.factory(addr) |
| 334 | |
| 335 | assertBadLength(3) |
| 336 | assertBadLength(5) |
| 337 | |
| 338 | |
| 339 | class InterfaceErrors_v4(ErrorReporting, NetmaskErrorsMixin_v4): |
| 340 | factory = ipaddress.IPv4Interface |
| 341 | |
| 342 | class NetworkErrors_v4(ErrorReporting, NetmaskErrorsMixin_v4): |
| 343 | factory = ipaddress.IPv4Network |
| 344 | |
| 345 | |
| 346 | class NetmaskErrorsMixin_v6: |
| 347 | """Input validation on interfaces and networks is very similar""" |
| 348 | |
| 349 | @property |
| 350 | def factory(self): |
| 351 | raise NotImplementedError |
| 352 | |
| 353 | def test_split_netmask(self): |
| 354 | addr = "cafe:cafe::/128/190" |
| 355 | with self.assertAddressError("Only one '/' permitted in %r" % addr): |
| 356 | self.factory(addr) |
| 357 | |
| 358 | def test_address_errors(self): |
| 359 | def assertBadAddress(addr, details): |
| 360 | with self.assertAddressError(details): |
| 361 | self.factory(addr) |
| 362 | |
| 363 | assertBadAddress("", "Address cannot be empty") |
| 364 | assertBadAddress("google.com", "At least 3 parts") |
| 365 | assertBadAddress("1.2.3.4", "At least 3 parts") |
| 366 | assertBadAddress("10/8", "At least 3 parts") |
| 367 | assertBadAddress("1234:axy::b", "Only hex digits") |
| 368 | |
| 369 | def test_netmask_errors(self): |
| 370 | def assertBadNetmask(addr, netmask): |
| 371 | msg = "%r is not a valid netmask" |
| 372 | with self.assertNetmaskError(msg % netmask): |
| 373 | self.factory("%s/%s" % (addr, netmask)) |
| 374 | |
| 375 | assertBadNetmask("::1", "") |
| 376 | assertBadNetmask("::1", "::1") |
| 377 | assertBadNetmask("::1", "1::") |
| 378 | assertBadNetmask("::1", "129") |
| 379 | assertBadNetmask("::1", "pudding") |
| 380 | |
| 381 | def test_bad_packed_length(self): |
| 382 | def assertBadLength(length): |
| 383 | addr = b'\x00' * length |
| 384 | msg = "Packed address %r must be exactly 16 bytes" % addr |
| 385 | with self.assertAddressError(re.escape(msg)): |
| 386 | self.factory(addr) |
| 387 | |
| 388 | assertBadLength(15) |
| 389 | assertBadLength(17) |
| 390 | |
| 391 | |
| 392 | class InterfaceErrors_v6(ErrorReporting, NetmaskErrorsMixin_v6): |
| 393 | factory = ipaddress.IPv6Interface |
| 394 | |
| 395 | class NetworkErrors_v6(ErrorReporting, NetmaskErrorsMixin_v6): |
| 396 | factory = ipaddress.IPv6Network |
| 397 | |
| 398 | |
| 399 | class FactoryFunctionErrors(ErrorReporting): |
| 400 | |
| 401 | def assertFactoryError(self, factory, kind): |
| 402 | """Ensure a clean ValueError with the expected message""" |
| 403 | addr = "camelot" |
| 404 | msg = '%r does not appear to be an IPv4 or IPv6 %s' |
| 405 | with self.assertCleanError(ValueError, msg, addr, kind): |
| 406 | factory(addr) |
| 407 | |
| 408 | def test_ip_address(self): |
| 409 | self.assertFactoryError(ipaddress.ip_address, "address") |
| 410 | |
| 411 | def test_ip_interface(self): |
| 412 | self.assertFactoryError(ipaddress.ip_interface, "interface") |
| 413 | |
| 414 | def test_ip_network(self): |
| 415 | self.assertFactoryError(ipaddress.ip_network, "network") |
| 416 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 417 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 418 | class IpaddrUnitTest(unittest.TestCase): |
| 419 | |
| 420 | def setUp(self): |
| 421 | self.ipv4_address = ipaddress.IPv4Address('1.2.3.4') |
| 422 | self.ipv4_interface = ipaddress.IPv4Interface('1.2.3.4/24') |
| 423 | self.ipv4_network = ipaddress.IPv4Network('1.2.3.0/24') |
| 424 | #self.ipv4_hostmask = ipaddress.IPv4Interface('10.0.0.1/0.255.255.255') |
| 425 | self.ipv6_address = ipaddress.IPv6Interface( |
| 426 | '2001:658:22a:cafe:200:0:0:1') |
| 427 | self.ipv6_interface = ipaddress.IPv6Interface( |
| 428 | '2001:658:22a:cafe:200:0:0:1/64') |
| 429 | self.ipv6_network = ipaddress.IPv6Network('2001:658:22a:cafe::/64') |
| 430 | |
| 431 | def testRepr(self): |
| 432 | self.assertEqual("IPv4Interface('1.2.3.4/32')", |
| 433 | repr(ipaddress.IPv4Interface('1.2.3.4'))) |
| 434 | self.assertEqual("IPv6Interface('::1/128')", |
| 435 | repr(ipaddress.IPv6Interface('::1'))) |
| 436 | |
| 437 | # issue57 |
| 438 | def testAddressIntMath(self): |
| 439 | self.assertEqual(ipaddress.IPv4Address('1.1.1.1') + 255, |
| 440 | ipaddress.IPv4Address('1.1.2.0')) |
| 441 | self.assertEqual(ipaddress.IPv4Address('1.1.1.1') - 256, |
| 442 | ipaddress.IPv4Address('1.1.0.1')) |
| 443 | self.assertEqual(ipaddress.IPv6Address('::1') + (2**16 - 2), |
| 444 | ipaddress.IPv6Address('::ffff')) |
| 445 | self.assertEqual(ipaddress.IPv6Address('::ffff') - (2**16 - 2), |
| 446 | ipaddress.IPv6Address('::1')) |
| 447 | |
Nick Coghlan | 3c2570c | 2012-07-07 01:13:55 +1000 | [diff] [blame] | 448 | def testInvalidIntToBytes(self): |
| 449 | self.assertRaises(ValueError, ipaddress.v4_int_to_packed, -1) |
| 450 | self.assertRaises(ValueError, ipaddress.v4_int_to_packed, |
| 451 | 2 ** ipaddress.IPV4LENGTH) |
| 452 | self.assertRaises(ValueError, ipaddress.v6_int_to_packed, -1) |
| 453 | self.assertRaises(ValueError, ipaddress.v6_int_to_packed, |
| 454 | 2 ** ipaddress.IPV6LENGTH) |
| 455 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 456 | def testInternals(self): |
| 457 | first, last = ipaddress._find_address_range([ |
| 458 | ipaddress.IPv4Address('10.10.10.10'), |
| 459 | ipaddress.IPv4Address('10.10.10.12')]) |
| 460 | self.assertEqual(first, last) |
| 461 | self.assertEqual(0, ipaddress._get_prefix_length(2**32, 0, 32)) |
| 462 | self.assertEqual(128, ipaddress._count_righthand_zero_bits(0, 128)) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 463 | self.assertEqual("IPv4Network('1.2.3.0/24')", repr(self.ipv4_network)) |
| 464 | self.assertEqual('0x1020318', hex(self.ipv4_network)) |
| 465 | self.assertRaises(TypeError, self.ipv4_network.__eq__, object()) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 466 | |
Nick Coghlan | d972265 | 2012-06-17 16:33:00 +1000 | [diff] [blame] | 467 | def testMissingAddressVersion(self): |
| 468 | class Broken(ipaddress._BaseAddress): |
| 469 | pass |
| 470 | broken = Broken('127.0.0.1') |
| 471 | with self.assertRaisesRegex(NotImplementedError, "Broken.*version"): |
| 472 | broken.version |
| 473 | |
| 474 | def testMissingNetworkVersion(self): |
| 475 | class Broken(ipaddress._BaseNetwork): |
| 476 | pass |
| 477 | broken = Broken('127.0.0.1') |
| 478 | with self.assertRaisesRegex(NotImplementedError, "Broken.*version"): |
| 479 | broken.version |
| 480 | |
| 481 | def testMissingAddressClass(self): |
| 482 | class Broken(ipaddress._BaseNetwork): |
| 483 | pass |
| 484 | broken = Broken('127.0.0.1') |
| 485 | with self.assertRaisesRegex(NotImplementedError, "Broken.*address"): |
| 486 | broken._address_class |
| 487 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 488 | def testGetNetwork(self): |
| 489 | self.assertEqual(int(self.ipv4_network.network_address), 16909056) |
| 490 | self.assertEqual(str(self.ipv4_network.network_address), '1.2.3.0') |
| 491 | |
| 492 | self.assertEqual(int(self.ipv6_network.network_address), |
| 493 | 42540616829182469433403647294022090752) |
| 494 | self.assertEqual(str(self.ipv6_network.network_address), |
| 495 | '2001:658:22a:cafe::') |
| 496 | self.assertEqual(str(self.ipv6_network.hostmask), |
| 497 | '::ffff:ffff:ffff:ffff') |
| 498 | |
| 499 | def testBadVersionComparison(self): |
| 500 | # These should always raise TypeError |
| 501 | v4addr = ipaddress.ip_address('1.1.1.1') |
| 502 | v4net = ipaddress.ip_network('1.1.1.1') |
| 503 | v6addr = ipaddress.ip_address('::1') |
| 504 | v6net = ipaddress.ip_address('::1') |
| 505 | |
| 506 | self.assertRaises(TypeError, v4addr.__lt__, v6addr) |
| 507 | self.assertRaises(TypeError, v4addr.__gt__, v6addr) |
| 508 | self.assertRaises(TypeError, v4net.__lt__, v6net) |
| 509 | self.assertRaises(TypeError, v4net.__gt__, v6net) |
| 510 | |
| 511 | self.assertRaises(TypeError, v6addr.__lt__, v4addr) |
| 512 | self.assertRaises(TypeError, v6addr.__gt__, v4addr) |
| 513 | self.assertRaises(TypeError, v6net.__lt__, v4net) |
| 514 | self.assertRaises(TypeError, v6net.__gt__, v4net) |
| 515 | |
| 516 | def testMixedTypeComparison(self): |
| 517 | v4addr = ipaddress.ip_address('1.1.1.1') |
| 518 | v4net = ipaddress.ip_network('1.1.1.1/32') |
| 519 | v6addr = ipaddress.ip_address('::1') |
| 520 | v6net = ipaddress.ip_network('::1/128') |
| 521 | |
| 522 | self.assertFalse(v4net.__contains__(v6net)) |
| 523 | self.assertFalse(v6net.__contains__(v4net)) |
| 524 | |
| 525 | self.assertRaises(TypeError, lambda: v4addr < v4net) |
| 526 | self.assertRaises(TypeError, lambda: v4addr > v4net) |
| 527 | self.assertRaises(TypeError, lambda: v4net < v4addr) |
| 528 | self.assertRaises(TypeError, lambda: v4net > v4addr) |
| 529 | |
| 530 | self.assertRaises(TypeError, lambda: v6addr < v6net) |
| 531 | self.assertRaises(TypeError, lambda: v6addr > v6net) |
| 532 | self.assertRaises(TypeError, lambda: v6net < v6addr) |
| 533 | self.assertRaises(TypeError, lambda: v6net > v6addr) |
| 534 | |
| 535 | # with get_mixed_type_key, you can sort addresses and network. |
| 536 | self.assertEqual([v4addr, v4net], |
| 537 | sorted([v4net, v4addr], |
| 538 | key=ipaddress.get_mixed_type_key)) |
| 539 | self.assertEqual([v6addr, v6net], |
| 540 | sorted([v6net, v6addr], |
| 541 | key=ipaddress.get_mixed_type_key)) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 542 | self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object)) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 543 | |
| 544 | def testIpFromInt(self): |
| 545 | self.assertEqual(self.ipv4_interface._ip, |
| 546 | ipaddress.IPv4Interface(16909060)._ip) |
| 547 | self.assertRaises(ipaddress.AddressValueError, |
| 548 | ipaddress.IPv4Interface, 2**32) |
| 549 | self.assertRaises(ipaddress.AddressValueError, |
| 550 | ipaddress.IPv4Interface, -1) |
| 551 | |
| 552 | ipv4 = ipaddress.ip_network('1.2.3.4') |
| 553 | ipv6 = ipaddress.ip_network('2001:658:22a:cafe:200:0:0:1') |
| 554 | self.assertEqual(ipv4, ipaddress.ip_network(int(ipv4))) |
| 555 | self.assertEqual(ipv6, ipaddress.ip_network(int(ipv6))) |
| 556 | |
| 557 | v6_int = 42540616829182469433547762482097946625 |
| 558 | self.assertEqual(self.ipv6_interface._ip, |
| 559 | ipaddress.IPv6Interface(v6_int)._ip) |
| 560 | self.assertRaises(ipaddress.AddressValueError, |
| 561 | ipaddress.IPv6Interface, 2**128) |
| 562 | self.assertRaises(ipaddress.AddressValueError, |
| 563 | ipaddress.IPv6Interface, -1) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 564 | self.assertRaises(ipaddress.AddressValueError, |
| 565 | ipaddress.IPv6Network, 2**128) |
| 566 | self.assertRaises(ipaddress.AddressValueError, |
| 567 | ipaddress.IPv6Network, -1) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 568 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 569 | self.assertEqual(ipaddress.ip_network(self.ipv4_address._ip).version, |
| 570 | 4) |
| 571 | self.assertEqual(ipaddress.ip_network(self.ipv6_address._ip).version, |
| 572 | 6) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 573 | |
| 574 | def testIpFromPacked(self): |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 575 | address = ipaddress.ip_address |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 576 | self.assertEqual(self.ipv4_interface._ip, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 577 | ipaddress.ip_interface(b'\x01\x02\x03\x04')._ip) |
| 578 | self.assertEqual(address('255.254.253.252'), |
| 579 | address(b'\xff\xfe\xfd\xfc')) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 580 | self.assertEqual(self.ipv6_interface.ip, |
| 581 | ipaddress.ip_interface( |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 582 | b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
| 583 | b'\x02\x00\x00\x00\x00\x00\x00\x01').ip) |
| 584 | self.assertEqual(address('ffff:2:3:4:ffff::'), |
| 585 | address(b'\xff\xff\x00\x02\x00\x03\x00\x04' + |
| 586 | b'\xff\xff' + b'\x00' * 6)) |
| 587 | self.assertEqual(address('::'), |
| 588 | address(b'\x00' * 16)) |
| 589 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 590 | def testGetIp(self): |
| 591 | self.assertEqual(int(self.ipv4_interface.ip), 16909060) |
| 592 | self.assertEqual(str(self.ipv4_interface.ip), '1.2.3.4') |
| 593 | |
| 594 | self.assertEqual(int(self.ipv6_interface.ip), |
| 595 | 42540616829182469433547762482097946625) |
| 596 | self.assertEqual(str(self.ipv6_interface.ip), |
| 597 | '2001:658:22a:cafe:200::1') |
| 598 | |
| 599 | def testGetNetmask(self): |
| 600 | self.assertEqual(int(self.ipv4_network.netmask), 4294967040) |
| 601 | self.assertEqual(str(self.ipv4_network.netmask), '255.255.255.0') |
| 602 | self.assertEqual(int(self.ipv6_network.netmask), |
| 603 | 340282366920938463444927863358058659840) |
| 604 | self.assertEqual(self.ipv6_network.prefixlen, 64) |
| 605 | |
| 606 | def testZeroNetmask(self): |
| 607 | ipv4_zero_netmask = ipaddress.IPv4Interface('1.2.3.4/0') |
| 608 | self.assertEqual(int(ipv4_zero_netmask.network.netmask), 0) |
| 609 | self.assertTrue(ipv4_zero_netmask.network._is_valid_netmask( |
| 610 | str(0))) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 611 | self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0')) |
| 612 | self.assertTrue(ipv4_zero_netmask._is_valid_netmask('0.0.0.0')) |
| 613 | self.assertFalse(ipv4_zero_netmask._is_valid_netmask('invalid')) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 614 | |
| 615 | ipv6_zero_netmask = ipaddress.IPv6Interface('::1/0') |
| 616 | self.assertEqual(int(ipv6_zero_netmask.network.netmask), 0) |
| 617 | self.assertTrue(ipv6_zero_netmask.network._is_valid_netmask( |
| 618 | str(0))) |
| 619 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 620 | def testIPv4NetAndHostmasks(self): |
| 621 | net = self.ipv4_network |
| 622 | self.assertFalse(net._is_valid_netmask('invalid')) |
| 623 | self.assertTrue(net._is_valid_netmask('128.128.128.128')) |
| 624 | self.assertFalse(net._is_valid_netmask('128.128.128.127')) |
| 625 | self.assertFalse(net._is_valid_netmask('128.128.128.255')) |
| 626 | self.assertTrue(net._is_valid_netmask('255.128.128.128')) |
| 627 | |
| 628 | self.assertFalse(net._is_hostmask('invalid')) |
| 629 | self.assertTrue(net._is_hostmask('128.255.255.255')) |
| 630 | self.assertFalse(net._is_hostmask('255.255.255.255')) |
| 631 | self.assertFalse(net._is_hostmask('1.2.3.4')) |
| 632 | |
| 633 | net = ipaddress.IPv4Network('127.0.0.0/0.0.0.255') |
| 634 | self.assertEqual(24, net.prefixlen) |
| 635 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 636 | def testGetBroadcast(self): |
| 637 | self.assertEqual(int(self.ipv4_network.broadcast_address), 16909311) |
| 638 | self.assertEqual(str(self.ipv4_network.broadcast_address), '1.2.3.255') |
| 639 | |
| 640 | self.assertEqual(int(self.ipv6_network.broadcast_address), |
| 641 | 42540616829182469451850391367731642367) |
| 642 | self.assertEqual(str(self.ipv6_network.broadcast_address), |
| 643 | '2001:658:22a:cafe:ffff:ffff:ffff:ffff') |
| 644 | |
| 645 | def testGetPrefixlen(self): |
| 646 | self.assertEqual(self.ipv4_interface.prefixlen, 24) |
| 647 | self.assertEqual(self.ipv6_interface.prefixlen, 64) |
| 648 | |
| 649 | def testGetSupernet(self): |
| 650 | self.assertEqual(self.ipv4_network.supernet().prefixlen, 23) |
| 651 | self.assertEqual(str(self.ipv4_network.supernet().network_address), |
| 652 | '1.2.2.0') |
| 653 | self.assertEqual( |
| 654 | ipaddress.IPv4Interface('0.0.0.0/0').network.supernet(), |
| 655 | ipaddress.IPv4Network('0.0.0.0/0')) |
| 656 | |
| 657 | self.assertEqual(self.ipv6_network.supernet().prefixlen, 63) |
| 658 | self.assertEqual(str(self.ipv6_network.supernet().network_address), |
| 659 | '2001:658:22a:cafe::') |
| 660 | self.assertEqual(ipaddress.IPv6Interface('::0/0').network.supernet(), |
| 661 | ipaddress.IPv6Network('::0/0')) |
| 662 | |
| 663 | def testGetSupernet3(self): |
| 664 | self.assertEqual(self.ipv4_network.supernet(3).prefixlen, 21) |
| 665 | self.assertEqual(str(self.ipv4_network.supernet(3).network_address), |
| 666 | '1.2.0.0') |
| 667 | |
| 668 | self.assertEqual(self.ipv6_network.supernet(3).prefixlen, 61) |
| 669 | self.assertEqual(str(self.ipv6_network.supernet(3).network_address), |
| 670 | '2001:658:22a:caf8::') |
| 671 | |
| 672 | def testGetSupernet4(self): |
| 673 | self.assertRaises(ValueError, self.ipv4_network.supernet, |
| 674 | prefixlen_diff=2, new_prefix=1) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 675 | self.assertRaises(ValueError, self.ipv4_network.supernet, |
| 676 | new_prefix=25) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 677 | self.assertEqual(self.ipv4_network.supernet(prefixlen_diff=2), |
| 678 | self.ipv4_network.supernet(new_prefix=22)) |
| 679 | |
| 680 | self.assertRaises(ValueError, self.ipv6_network.supernet, |
| 681 | prefixlen_diff=2, new_prefix=1) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 682 | self.assertRaises(ValueError, self.ipv6_network.supernet, |
| 683 | new_prefix=65) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 684 | self.assertEqual(self.ipv6_network.supernet(prefixlen_diff=2), |
| 685 | self.ipv6_network.supernet(new_prefix=62)) |
| 686 | |
| 687 | def testHosts(self): |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 688 | hosts = list(self.ipv4_network.hosts()) |
| 689 | self.assertEqual(254, len(hosts)) |
| 690 | self.assertEqual(ipaddress.IPv4Address('1.2.3.1'), hosts[0]) |
| 691 | self.assertEqual(ipaddress.IPv4Address('1.2.3.254'), hosts[-1]) |
| 692 | |
| 693 | # special case where only 1 bit is left for address |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 694 | self.assertEqual([ipaddress.IPv4Address('2.0.0.0'), |
| 695 | ipaddress.IPv4Address('2.0.0.1')], |
| 696 | list(ipaddress.ip_network('2.0.0.0/31').hosts())) |
| 697 | |
| 698 | def testFancySubnetting(self): |
| 699 | self.assertEqual(sorted(self.ipv4_network.subnets(prefixlen_diff=3)), |
| 700 | sorted(self.ipv4_network.subnets(new_prefix=27))) |
| 701 | self.assertRaises(ValueError, list, |
| 702 | self.ipv4_network.subnets(new_prefix=23)) |
| 703 | self.assertRaises(ValueError, list, |
| 704 | self.ipv4_network.subnets(prefixlen_diff=3, |
| 705 | new_prefix=27)) |
| 706 | self.assertEqual(sorted(self.ipv6_network.subnets(prefixlen_diff=4)), |
| 707 | sorted(self.ipv6_network.subnets(new_prefix=68))) |
| 708 | self.assertRaises(ValueError, list, |
| 709 | self.ipv6_network.subnets(new_prefix=63)) |
| 710 | self.assertRaises(ValueError, list, |
| 711 | self.ipv6_network.subnets(prefixlen_diff=4, |
| 712 | new_prefix=68)) |
| 713 | |
| 714 | def testGetSubnets(self): |
| 715 | self.assertEqual(list(self.ipv4_network.subnets())[0].prefixlen, 25) |
| 716 | self.assertEqual(str(list( |
| 717 | self.ipv4_network.subnets())[0].network_address), |
| 718 | '1.2.3.0') |
| 719 | self.assertEqual(str(list( |
| 720 | self.ipv4_network.subnets())[1].network_address), |
| 721 | '1.2.3.128') |
| 722 | |
| 723 | self.assertEqual(list(self.ipv6_network.subnets())[0].prefixlen, 65) |
| 724 | |
| 725 | def testGetSubnetForSingle32(self): |
| 726 | ip = ipaddress.IPv4Network('1.2.3.4/32') |
| 727 | subnets1 = [str(x) for x in ip.subnets()] |
| 728 | subnets2 = [str(x) for x in ip.subnets(2)] |
| 729 | self.assertEqual(subnets1, ['1.2.3.4/32']) |
| 730 | self.assertEqual(subnets1, subnets2) |
| 731 | |
| 732 | def testGetSubnetForSingle128(self): |
| 733 | ip = ipaddress.IPv6Network('::1/128') |
| 734 | subnets1 = [str(x) for x in ip.subnets()] |
| 735 | subnets2 = [str(x) for x in ip.subnets(2)] |
| 736 | self.assertEqual(subnets1, ['::1/128']) |
| 737 | self.assertEqual(subnets1, subnets2) |
| 738 | |
| 739 | def testSubnet2(self): |
| 740 | ips = [str(x) for x in self.ipv4_network.subnets(2)] |
| 741 | self.assertEqual( |
| 742 | ips, |
| 743 | ['1.2.3.0/26', '1.2.3.64/26', '1.2.3.128/26', '1.2.3.192/26']) |
| 744 | |
| 745 | ipsv6 = [str(x) for x in self.ipv6_network.subnets(2)] |
| 746 | self.assertEqual( |
| 747 | ipsv6, |
| 748 | ['2001:658:22a:cafe::/66', |
| 749 | '2001:658:22a:cafe:4000::/66', |
| 750 | '2001:658:22a:cafe:8000::/66', |
| 751 | '2001:658:22a:cafe:c000::/66']) |
| 752 | |
| 753 | def testSubnetFailsForLargeCidrDiff(self): |
| 754 | self.assertRaises(ValueError, list, |
| 755 | self.ipv4_interface.network.subnets(9)) |
| 756 | self.assertRaises(ValueError, list, |
| 757 | self.ipv4_network.subnets(9)) |
| 758 | self.assertRaises(ValueError, list, |
| 759 | self.ipv6_interface.network.subnets(65)) |
| 760 | self.assertRaises(ValueError, list, |
| 761 | self.ipv6_network.subnets(65)) |
| 762 | |
| 763 | def testSupernetFailsForLargeCidrDiff(self): |
| 764 | self.assertRaises(ValueError, |
| 765 | self.ipv4_interface.network.supernet, 25) |
| 766 | self.assertRaises(ValueError, |
| 767 | self.ipv6_interface.network.supernet, 65) |
| 768 | |
| 769 | def testSubnetFailsForNegativeCidrDiff(self): |
| 770 | self.assertRaises(ValueError, list, |
| 771 | self.ipv4_interface.network.subnets(-1)) |
| 772 | self.assertRaises(ValueError, list, |
Nick Coghlan | 2c58910 | 2012-05-27 01:03:25 +1000 | [diff] [blame] | 773 | self.ipv4_network.subnets(-1)) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 774 | self.assertRaises(ValueError, list, |
| 775 | self.ipv6_interface.network.subnets(-1)) |
| 776 | self.assertRaises(ValueError, list, |
| 777 | self.ipv6_network.subnets(-1)) |
| 778 | |
| 779 | def testGetNum_Addresses(self): |
| 780 | self.assertEqual(self.ipv4_network.num_addresses, 256) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 781 | self.assertEqual(list(self.ipv4_network.subnets())[0].num_addresses, |
| 782 | 128) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 783 | self.assertEqual(self.ipv4_network.supernet().num_addresses, 512) |
| 784 | |
| 785 | self.assertEqual(self.ipv6_network.num_addresses, 18446744073709551616) |
| 786 | self.assertEqual(list(self.ipv6_network.subnets())[0].num_addresses, |
| 787 | 9223372036854775808) |
| 788 | self.assertEqual(self.ipv6_network.supernet().num_addresses, |
| 789 | 36893488147419103232) |
| 790 | |
| 791 | def testContains(self): |
| 792 | self.assertTrue(ipaddress.IPv4Interface('1.2.3.128/25') in |
| 793 | self.ipv4_network) |
| 794 | self.assertFalse(ipaddress.IPv4Interface('1.2.4.1/24') in |
| 795 | self.ipv4_network) |
| 796 | # We can test addresses and string as well. |
| 797 | addr1 = ipaddress.IPv4Address('1.2.3.37') |
| 798 | self.assertTrue(addr1 in self.ipv4_network) |
| 799 | # issue 61, bad network comparison on like-ip'd network objects |
| 800 | # with identical broadcast addresses. |
| 801 | self.assertFalse(ipaddress.IPv4Network('1.1.0.0/16').__contains__( |
| 802 | ipaddress.IPv4Network('1.0.0.0/15'))) |
| 803 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 804 | def testNth(self): |
| 805 | self.assertEqual(str(self.ipv4_network[5]), '1.2.3.5') |
| 806 | self.assertRaises(IndexError, self.ipv4_network.__getitem__, 256) |
| 807 | |
| 808 | self.assertEqual(str(self.ipv6_network[5]), |
| 809 | '2001:658:22a:cafe::5') |
| 810 | |
| 811 | def testGetitem(self): |
| 812 | # http://code.google.com/p/ipaddr-py/issues/detail?id=15 |
| 813 | addr = ipaddress.IPv4Network('172.31.255.128/255.255.255.240') |
| 814 | self.assertEqual(28, addr.prefixlen) |
| 815 | addr_list = list(addr) |
| 816 | self.assertEqual('172.31.255.128', str(addr_list[0])) |
| 817 | self.assertEqual('172.31.255.128', str(addr[0])) |
| 818 | self.assertEqual('172.31.255.143', str(addr_list[-1])) |
| 819 | self.assertEqual('172.31.255.143', str(addr[-1])) |
| 820 | self.assertEqual(addr_list[-1], addr[-1]) |
| 821 | |
| 822 | def testEqual(self): |
| 823 | self.assertTrue(self.ipv4_interface == |
| 824 | ipaddress.IPv4Interface('1.2.3.4/24')) |
| 825 | self.assertFalse(self.ipv4_interface == |
| 826 | ipaddress.IPv4Interface('1.2.3.4/23')) |
| 827 | self.assertFalse(self.ipv4_interface == |
| 828 | ipaddress.IPv6Interface('::1.2.3.4/24')) |
| 829 | self.assertFalse(self.ipv4_interface == '') |
| 830 | self.assertFalse(self.ipv4_interface == []) |
| 831 | self.assertFalse(self.ipv4_interface == 2) |
| 832 | |
| 833 | self.assertTrue(self.ipv6_interface == |
| 834 | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) |
| 835 | self.assertFalse(self.ipv6_interface == |
| 836 | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) |
| 837 | self.assertFalse(self.ipv6_interface == |
| 838 | ipaddress.IPv4Interface('1.2.3.4/23')) |
| 839 | self.assertFalse(self.ipv6_interface == '') |
| 840 | self.assertFalse(self.ipv6_interface == []) |
| 841 | self.assertFalse(self.ipv6_interface == 2) |
| 842 | |
| 843 | def testNotEqual(self): |
| 844 | self.assertFalse(self.ipv4_interface != |
| 845 | ipaddress.IPv4Interface('1.2.3.4/24')) |
| 846 | self.assertTrue(self.ipv4_interface != |
| 847 | ipaddress.IPv4Interface('1.2.3.4/23')) |
| 848 | self.assertTrue(self.ipv4_interface != |
| 849 | ipaddress.IPv6Interface('::1.2.3.4/24')) |
| 850 | self.assertTrue(self.ipv4_interface != '') |
| 851 | self.assertTrue(self.ipv4_interface != []) |
| 852 | self.assertTrue(self.ipv4_interface != 2) |
| 853 | |
| 854 | self.assertTrue(self.ipv4_address != |
| 855 | ipaddress.IPv4Address('1.2.3.5')) |
| 856 | self.assertTrue(self.ipv4_address != '') |
| 857 | self.assertTrue(self.ipv4_address != []) |
| 858 | self.assertTrue(self.ipv4_address != 2) |
| 859 | |
| 860 | self.assertFalse(self.ipv6_interface != |
| 861 | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/64')) |
| 862 | self.assertTrue(self.ipv6_interface != |
| 863 | ipaddress.IPv6Interface('2001:658:22a:cafe:200::1/63')) |
| 864 | self.assertTrue(self.ipv6_interface != |
| 865 | ipaddress.IPv4Interface('1.2.3.4/23')) |
| 866 | self.assertTrue(self.ipv6_interface != '') |
| 867 | self.assertTrue(self.ipv6_interface != []) |
| 868 | self.assertTrue(self.ipv6_interface != 2) |
| 869 | |
| 870 | self.assertTrue(self.ipv6_address != |
| 871 | ipaddress.IPv4Address('1.2.3.4')) |
| 872 | self.assertTrue(self.ipv6_address != '') |
| 873 | self.assertTrue(self.ipv6_address != []) |
| 874 | self.assertTrue(self.ipv6_address != 2) |
| 875 | |
| 876 | def testSlash32Constructor(self): |
| 877 | self.assertEqual(str(ipaddress.IPv4Interface( |
| 878 | '1.2.3.4/255.255.255.255')), '1.2.3.4/32') |
| 879 | |
| 880 | def testSlash128Constructor(self): |
| 881 | self.assertEqual(str(ipaddress.IPv6Interface('::1/128')), |
| 882 | '::1/128') |
| 883 | |
| 884 | def testSlash0Constructor(self): |
| 885 | self.assertEqual(str(ipaddress.IPv4Interface('1.2.3.4/0.0.0.0')), |
| 886 | '1.2.3.4/0') |
| 887 | |
| 888 | def testCollapsing(self): |
| 889 | # test only IP addresses including some duplicates |
| 890 | ip1 = ipaddress.IPv4Address('1.1.1.0') |
| 891 | ip2 = ipaddress.IPv4Address('1.1.1.1') |
| 892 | ip3 = ipaddress.IPv4Address('1.1.1.2') |
| 893 | ip4 = ipaddress.IPv4Address('1.1.1.3') |
| 894 | ip5 = ipaddress.IPv4Address('1.1.1.4') |
| 895 | ip6 = ipaddress.IPv4Address('1.1.1.0') |
| 896 | # check that addreses are subsumed properly. |
| 897 | collapsed = ipaddress.collapse_addresses( |
| 898 | [ip1, ip2, ip3, ip4, ip5, ip6]) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 899 | self.assertEqual(list(collapsed), |
| 900 | [ipaddress.IPv4Network('1.1.1.0/30'), |
| 901 | ipaddress.IPv4Network('1.1.1.4/32')]) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 902 | |
| 903 | # test a mix of IP addresses and networks including some duplicates |
| 904 | ip1 = ipaddress.IPv4Address('1.1.1.0') |
| 905 | ip2 = ipaddress.IPv4Address('1.1.1.1') |
| 906 | ip3 = ipaddress.IPv4Address('1.1.1.2') |
| 907 | ip4 = ipaddress.IPv4Address('1.1.1.3') |
| 908 | #ip5 = ipaddress.IPv4Interface('1.1.1.4/30') |
| 909 | #ip6 = ipaddress.IPv4Interface('1.1.1.4/30') |
| 910 | # check that addreses are subsumed properly. |
| 911 | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4]) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 912 | self.assertEqual(list(collapsed), |
| 913 | [ipaddress.IPv4Network('1.1.1.0/30')]) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 914 | |
| 915 | # test only IP networks |
| 916 | ip1 = ipaddress.IPv4Network('1.1.0.0/24') |
| 917 | ip2 = ipaddress.IPv4Network('1.1.1.0/24') |
| 918 | ip3 = ipaddress.IPv4Network('1.1.2.0/24') |
| 919 | ip4 = ipaddress.IPv4Network('1.1.3.0/24') |
| 920 | ip5 = ipaddress.IPv4Network('1.1.4.0/24') |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 921 | # stored in no particular order b/c we want CollapseAddr to call |
| 922 | # [].sort |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 923 | ip6 = ipaddress.IPv4Network('1.1.0.0/22') |
| 924 | # check that addreses are subsumed properly. |
| 925 | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3, ip4, ip5, |
| 926 | ip6]) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 927 | self.assertEqual(list(collapsed), |
| 928 | [ipaddress.IPv4Network('1.1.0.0/22'), |
| 929 | ipaddress.IPv4Network('1.1.4.0/24')]) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 930 | |
| 931 | # test that two addresses are supernet'ed properly |
| 932 | collapsed = ipaddress.collapse_addresses([ip1, ip2]) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 933 | self.assertEqual(list(collapsed), |
| 934 | [ipaddress.IPv4Network('1.1.0.0/23')]) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 935 | |
| 936 | # test same IP networks |
| 937 | ip_same1 = ip_same2 = ipaddress.IPv4Network('1.1.1.1/32') |
| 938 | self.assertEqual(list(ipaddress.collapse_addresses( |
| 939 | [ip_same1, ip_same2])), |
| 940 | [ip_same1]) |
| 941 | |
| 942 | # test same IP addresses |
| 943 | ip_same1 = ip_same2 = ipaddress.IPv4Address('1.1.1.1') |
| 944 | self.assertEqual(list(ipaddress.collapse_addresses( |
| 945 | [ip_same1, ip_same2])), |
| 946 | [ipaddress.ip_network('1.1.1.1/32')]) |
| 947 | ip1 = ipaddress.IPv6Network('2001::/100') |
| 948 | ip2 = ipaddress.IPv6Network('2001::/120') |
| 949 | ip3 = ipaddress.IPv6Network('2001::/96') |
| 950 | # test that ipv6 addresses are subsumed properly. |
| 951 | collapsed = ipaddress.collapse_addresses([ip1, ip2, ip3]) |
| 952 | self.assertEqual(list(collapsed), [ip3]) |
| 953 | |
| 954 | # the toejam test |
Hynek Schlawack | 35db513 | 2012-06-01 20:12:17 +0200 | [diff] [blame] | 955 | addr_tuples = [ |
| 956 | (ipaddress.ip_address('1.1.1.1'), |
| 957 | ipaddress.ip_address('::1')), |
| 958 | (ipaddress.IPv4Network('1.1.0.0/24'), |
| 959 | ipaddress.IPv6Network('2001::/120')), |
| 960 | (ipaddress.IPv4Network('1.1.0.0/32'), |
| 961 | ipaddress.IPv6Network('2001::/128')), |
| 962 | ] |
| 963 | for ip1, ip2 in addr_tuples: |
| 964 | self.assertRaises(TypeError, ipaddress.collapse_addresses, |
| 965 | [ip1, ip2]) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 966 | |
| 967 | def testSummarizing(self): |
| 968 | #ip = ipaddress.ip_address |
| 969 | #ipnet = ipaddress.ip_network |
| 970 | summarize = ipaddress.summarize_address_range |
| 971 | ip1 = ipaddress.ip_address('1.1.1.0') |
| 972 | ip2 = ipaddress.ip_address('1.1.1.255') |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 973 | |
| 974 | # summarize works only for IPv4 & IPv6 |
| 975 | class IPv7Address(ipaddress.IPv6Address): |
| 976 | @property |
| 977 | def version(self): |
| 978 | return 7 |
| 979 | ip_invalid1 = IPv7Address('::1') |
| 980 | ip_invalid2 = IPv7Address('::1') |
| 981 | self.assertRaises(ValueError, list, |
| 982 | summarize(ip_invalid1, ip_invalid2)) |
| 983 | # test that a summary over ip4 & ip6 fails |
| 984 | self.assertRaises(TypeError, list, |
| 985 | summarize(ip1, ipaddress.IPv6Address('::1'))) |
| 986 | # test a /24 is summarized properly |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 987 | self.assertEqual(list(summarize(ip1, ip2))[0], |
| 988 | ipaddress.ip_network('1.1.1.0/24')) |
| 989 | # test an IPv4 range that isn't on a network byte boundary |
| 990 | ip2 = ipaddress.ip_address('1.1.1.8') |
| 991 | self.assertEqual(list(summarize(ip1, ip2)), |
| 992 | [ipaddress.ip_network('1.1.1.0/29'), |
| 993 | ipaddress.ip_network('1.1.1.8')]) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 994 | # all! |
| 995 | ip1 = ipaddress.IPv4Address(0) |
| 996 | ip2 = ipaddress.IPv4Address(ipaddress.IPv4Address._ALL_ONES) |
| 997 | self.assertEqual([ipaddress.IPv4Network('0.0.0.0/0')], |
| 998 | list(summarize(ip1, ip2))) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 999 | |
| 1000 | ip1 = ipaddress.ip_address('1::') |
| 1001 | ip2 = ipaddress.ip_address('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff') |
| 1002 | # test a IPv6 is sumamrized properly |
| 1003 | self.assertEqual(list(summarize(ip1, ip2))[0], |
| 1004 | ipaddress.ip_network('1::/16')) |
| 1005 | # test an IPv6 range that isn't on a network byte boundary |
| 1006 | ip2 = ipaddress.ip_address('2::') |
| 1007 | self.assertEqual(list(summarize(ip1, ip2)), |
| 1008 | [ipaddress.ip_network('1::/16'), |
| 1009 | ipaddress.ip_network('2::/128')]) |
| 1010 | |
| 1011 | # test exception raised when first is greater than last |
| 1012 | self.assertRaises(ValueError, list, |
| 1013 | summarize(ipaddress.ip_address('1.1.1.0'), |
| 1014 | ipaddress.ip_address('1.1.0.0'))) |
| 1015 | # test exception raised when first and last aren't IP addresses |
| 1016 | self.assertRaises(TypeError, list, |
| 1017 | summarize(ipaddress.ip_network('1.1.1.0'), |
| 1018 | ipaddress.ip_network('1.1.0.0'))) |
| 1019 | self.assertRaises(TypeError, list, |
| 1020 | summarize(ipaddress.ip_network('1.1.1.0'), |
| 1021 | ipaddress.ip_network('1.1.0.0'))) |
| 1022 | # test exception raised when first and last are not same version |
| 1023 | self.assertRaises(TypeError, list, |
| 1024 | summarize(ipaddress.ip_address('::'), |
| 1025 | ipaddress.ip_network('1.1.0.0'))) |
| 1026 | |
| 1027 | def testAddressComparison(self): |
| 1028 | self.assertTrue(ipaddress.ip_address('1.1.1.1') <= |
| 1029 | ipaddress.ip_address('1.1.1.1')) |
| 1030 | self.assertTrue(ipaddress.ip_address('1.1.1.1') <= |
| 1031 | ipaddress.ip_address('1.1.1.2')) |
| 1032 | self.assertTrue(ipaddress.ip_address('::1') <= |
| 1033 | ipaddress.ip_address('::1')) |
| 1034 | self.assertTrue(ipaddress.ip_address('::1') <= |
| 1035 | ipaddress.ip_address('::2')) |
| 1036 | |
| 1037 | def testNetworkComparison(self): |
| 1038 | # ip1 and ip2 have the same network address |
| 1039 | ip1 = ipaddress.IPv4Network('1.1.1.0/24') |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1040 | ip2 = ipaddress.IPv4Network('1.1.1.0/32') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1041 | ip3 = ipaddress.IPv4Network('1.1.2.0/24') |
| 1042 | |
| 1043 | self.assertTrue(ip1 < ip3) |
| 1044 | self.assertTrue(ip3 > ip2) |
| 1045 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1046 | self.assertEqual(ip1.compare_networks(ip1), 0) |
| 1047 | |
| 1048 | # if addresses are the same, sort by netmask |
| 1049 | self.assertEqual(ip1.compare_networks(ip2), -1) |
| 1050 | self.assertEqual(ip2.compare_networks(ip1), 1) |
| 1051 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1052 | self.assertEqual(ip1.compare_networks(ip3), -1) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1053 | self.assertEqual(ip3.compare_networks(ip1), 1) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1054 | self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) |
| 1055 | |
| 1056 | ip1 = ipaddress.IPv6Network('2001:2000::/96') |
| 1057 | ip2 = ipaddress.IPv6Network('2001:2001::/96') |
| 1058 | ip3 = ipaddress.IPv6Network('2001:ffff:2000::/96') |
| 1059 | |
| 1060 | self.assertTrue(ip1 < ip3) |
| 1061 | self.assertTrue(ip3 > ip2) |
| 1062 | self.assertEqual(ip1.compare_networks(ip3), -1) |
| 1063 | self.assertTrue(ip1._get_networks_key() < ip3._get_networks_key()) |
| 1064 | |
| 1065 | # Test comparing different protocols. |
| 1066 | # Should always raise a TypeError. |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1067 | self.assertRaises(TypeError, |
| 1068 | self.ipv4_network.compare_networks, |
| 1069 | self.ipv6_network) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1070 | ipv6 = ipaddress.IPv6Interface('::/0') |
| 1071 | ipv4 = ipaddress.IPv4Interface('0.0.0.0/0') |
| 1072 | self.assertRaises(TypeError, ipv4.__lt__, ipv6) |
| 1073 | self.assertRaises(TypeError, ipv4.__gt__, ipv6) |
| 1074 | self.assertRaises(TypeError, ipv6.__lt__, ipv4) |
| 1075 | self.assertRaises(TypeError, ipv6.__gt__, ipv4) |
| 1076 | |
| 1077 | # Regression test for issue 19. |
| 1078 | ip1 = ipaddress.ip_network('10.1.2.128/25') |
| 1079 | self.assertFalse(ip1 < ip1) |
| 1080 | self.assertFalse(ip1 > ip1) |
| 1081 | ip2 = ipaddress.ip_network('10.1.3.0/24') |
| 1082 | self.assertTrue(ip1 < ip2) |
| 1083 | self.assertFalse(ip2 < ip1) |
| 1084 | self.assertFalse(ip1 > ip2) |
| 1085 | self.assertTrue(ip2 > ip1) |
| 1086 | ip3 = ipaddress.ip_network('10.1.3.0/25') |
| 1087 | self.assertTrue(ip2 < ip3) |
| 1088 | self.assertFalse(ip3 < ip2) |
| 1089 | self.assertFalse(ip2 > ip3) |
| 1090 | self.assertTrue(ip3 > ip2) |
| 1091 | |
| 1092 | # Regression test for issue 28. |
| 1093 | ip1 = ipaddress.ip_network('10.10.10.0/31') |
| 1094 | ip2 = ipaddress.ip_network('10.10.10.0') |
| 1095 | ip3 = ipaddress.ip_network('10.10.10.2/31') |
| 1096 | ip4 = ipaddress.ip_network('10.10.10.2') |
| 1097 | sorted = [ip1, ip2, ip3, ip4] |
| 1098 | unsorted = [ip2, ip4, ip1, ip3] |
| 1099 | unsorted.sort() |
| 1100 | self.assertEqual(sorted, unsorted) |
| 1101 | unsorted = [ip4, ip1, ip3, ip2] |
| 1102 | unsorted.sort() |
| 1103 | self.assertEqual(sorted, unsorted) |
| 1104 | self.assertRaises(TypeError, ip1.__lt__, |
| 1105 | ipaddress.ip_address('10.10.10.0')) |
| 1106 | self.assertRaises(TypeError, ip2.__lt__, |
| 1107 | ipaddress.ip_address('10.10.10.0')) |
| 1108 | |
| 1109 | # <=, >= |
| 1110 | self.assertTrue(ipaddress.ip_network('1.1.1.1') <= |
| 1111 | ipaddress.ip_network('1.1.1.1')) |
| 1112 | self.assertTrue(ipaddress.ip_network('1.1.1.1') <= |
| 1113 | ipaddress.ip_network('1.1.1.2')) |
| 1114 | self.assertFalse(ipaddress.ip_network('1.1.1.2') <= |
| 1115 | ipaddress.ip_network('1.1.1.1')) |
| 1116 | self.assertTrue(ipaddress.ip_network('::1') <= |
| 1117 | ipaddress.ip_network('::1')) |
| 1118 | self.assertTrue(ipaddress.ip_network('::1') <= |
| 1119 | ipaddress.ip_network('::2')) |
| 1120 | self.assertFalse(ipaddress.ip_network('::2') <= |
| 1121 | ipaddress.ip_network('::1')) |
| 1122 | |
| 1123 | def testStrictNetworks(self): |
| 1124 | self.assertRaises(ValueError, ipaddress.ip_network, '192.168.1.1/24') |
| 1125 | self.assertRaises(ValueError, ipaddress.ip_network, '::1/120') |
| 1126 | |
| 1127 | def testOverlaps(self): |
| 1128 | other = ipaddress.IPv4Network('1.2.3.0/30') |
| 1129 | other2 = ipaddress.IPv4Network('1.2.2.0/24') |
| 1130 | other3 = ipaddress.IPv4Network('1.2.2.64/26') |
| 1131 | self.assertTrue(self.ipv4_network.overlaps(other)) |
| 1132 | self.assertFalse(self.ipv4_network.overlaps(other2)) |
| 1133 | self.assertTrue(other2.overlaps(other3)) |
| 1134 | |
| 1135 | def testEmbeddedIpv4(self): |
| 1136 | ipv4_string = '192.168.0.1' |
| 1137 | ipv4 = ipaddress.IPv4Interface(ipv4_string) |
| 1138 | v4compat_ipv6 = ipaddress.IPv6Interface('::%s' % ipv4_string) |
| 1139 | self.assertEqual(int(v4compat_ipv6.ip), int(ipv4.ip)) |
| 1140 | v4mapped_ipv6 = ipaddress.IPv6Interface('::ffff:%s' % ipv4_string) |
| 1141 | self.assertNotEqual(v4mapped_ipv6.ip, ipv4.ip) |
| 1142 | self.assertRaises(ipaddress.AddressValueError, ipaddress.IPv6Interface, |
| 1143 | '2001:1.1.1.1:1.1.1.1') |
| 1144 | |
| 1145 | # Issue 67: IPv6 with embedded IPv4 address not recognized. |
| 1146 | def testIPv6AddressTooLarge(self): |
| 1147 | # RFC4291 2.5.5.2 |
| 1148 | self.assertEqual(ipaddress.ip_address('::FFFF:192.0.2.1'), |
| 1149 | ipaddress.ip_address('::FFFF:c000:201')) |
| 1150 | # RFC4291 2.2 (part 3) x::d.d.d.d |
| 1151 | self.assertEqual(ipaddress.ip_address('FFFF::192.0.2.1'), |
| 1152 | ipaddress.ip_address('FFFF::c000:201')) |
| 1153 | |
| 1154 | def testIPVersion(self): |
| 1155 | self.assertEqual(self.ipv4_address.version, 4) |
| 1156 | self.assertEqual(self.ipv6_address.version, 6) |
| 1157 | |
| 1158 | def testMaxPrefixLength(self): |
| 1159 | self.assertEqual(self.ipv4_interface.max_prefixlen, 32) |
| 1160 | self.assertEqual(self.ipv6_interface.max_prefixlen, 128) |
| 1161 | |
| 1162 | def testPacked(self): |
| 1163 | self.assertEqual(self.ipv4_address.packed, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 1164 | b'\x01\x02\x03\x04') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1165 | self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 1166 | b'\xff\xfe\xfd\xfc') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1167 | self.assertEqual(self.ipv6_address.packed, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 1168 | b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
| 1169 | b'\x02\x00\x00\x00\x00\x00\x00\x01') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1170 | self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 1171 | b'\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff' |
| 1172 | + b'\x00' * 6) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1173 | self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed, |
Nick Coghlan | 5cf896f | 2012-07-07 01:43:31 +1000 | [diff] [blame] | 1174 | b'\x00' * 6 + b'\x00\x01' + b'\x00' * 8) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1175 | |
| 1176 | def testIpStrFromPrefixlen(self): |
| 1177 | ipv4 = ipaddress.IPv4Interface('1.2.3.4/24') |
| 1178 | self.assertEqual(ipv4._ip_string_from_prefix(), '255.255.255.0') |
| 1179 | self.assertEqual(ipv4._ip_string_from_prefix(28), '255.255.255.240') |
| 1180 | |
| 1181 | def testIpType(self): |
| 1182 | ipv4net = ipaddress.ip_network('1.2.3.4') |
| 1183 | ipv4addr = ipaddress.ip_address('1.2.3.4') |
| 1184 | ipv6net = ipaddress.ip_network('::1.2.3.4') |
| 1185 | ipv6addr = ipaddress.ip_address('::1.2.3.4') |
| 1186 | self.assertEqual(ipaddress.IPv4Network, type(ipv4net)) |
| 1187 | self.assertEqual(ipaddress.IPv4Address, type(ipv4addr)) |
| 1188 | self.assertEqual(ipaddress.IPv6Network, type(ipv6net)) |
| 1189 | self.assertEqual(ipaddress.IPv6Address, type(ipv6addr)) |
| 1190 | |
| 1191 | def testReservedIpv4(self): |
| 1192 | # test networks |
| 1193 | self.assertEqual(True, ipaddress.ip_interface( |
| 1194 | '224.1.1.1/31').is_multicast) |
| 1195 | self.assertEqual(False, ipaddress.ip_network('240.0.0.0').is_multicast) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1196 | self.assertEqual(True, ipaddress.ip_network('240.0.0.0').is_reserved) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1197 | |
| 1198 | self.assertEqual(True, ipaddress.ip_interface( |
| 1199 | '192.168.1.1/17').is_private) |
| 1200 | self.assertEqual(False, ipaddress.ip_network('192.169.0.0').is_private) |
| 1201 | self.assertEqual(True, ipaddress.ip_network( |
| 1202 | '10.255.255.255').is_private) |
| 1203 | self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_private) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1204 | self.assertEqual(False, ipaddress.ip_network('11.0.0.0').is_reserved) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1205 | self.assertEqual(True, ipaddress.ip_network( |
| 1206 | '172.31.255.255').is_private) |
| 1207 | self.assertEqual(False, ipaddress.ip_network('172.32.0.0').is_private) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1208 | self.assertEqual(True, |
| 1209 | ipaddress.ip_network('169.254.1.0/24').is_link_local) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1210 | |
| 1211 | self.assertEqual(True, |
| 1212 | ipaddress.ip_interface( |
| 1213 | '169.254.100.200/24').is_link_local) |
| 1214 | self.assertEqual(False, |
| 1215 | ipaddress.ip_interface( |
| 1216 | '169.255.100.200/24').is_link_local) |
| 1217 | |
| 1218 | self.assertEqual(True, |
| 1219 | ipaddress.ip_network( |
| 1220 | '127.100.200.254/32').is_loopback) |
| 1221 | self.assertEqual(True, ipaddress.ip_network( |
| 1222 | '127.42.0.0/16').is_loopback) |
| 1223 | self.assertEqual(False, ipaddress.ip_network('128.0.0.0').is_loopback) |
| 1224 | |
| 1225 | # test addresses |
Hynek Schlawack | bcd3044 | 2012-06-04 14:19:39 +0200 | [diff] [blame] | 1226 | self.assertEqual(True, ipaddress.ip_address('0.0.0.0').is_unspecified) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1227 | self.assertEqual(True, ipaddress.ip_address('224.1.1.1').is_multicast) |
| 1228 | self.assertEqual(False, ipaddress.ip_address('240.0.0.0').is_multicast) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1229 | self.assertEqual(True, ipaddress.ip_address('240.0.0.1').is_reserved) |
| 1230 | self.assertEqual(False, |
| 1231 | ipaddress.ip_address('239.255.255.255').is_reserved) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1232 | |
| 1233 | self.assertEqual(True, ipaddress.ip_address('192.168.1.1').is_private) |
| 1234 | self.assertEqual(False, ipaddress.ip_address('192.169.0.0').is_private) |
| 1235 | self.assertEqual(True, ipaddress.ip_address( |
| 1236 | '10.255.255.255').is_private) |
| 1237 | self.assertEqual(False, ipaddress.ip_address('11.0.0.0').is_private) |
| 1238 | self.assertEqual(True, ipaddress.ip_address( |
| 1239 | '172.31.255.255').is_private) |
| 1240 | self.assertEqual(False, ipaddress.ip_address('172.32.0.0').is_private) |
| 1241 | |
| 1242 | self.assertEqual(True, |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1243 | ipaddress.ip_address('169.254.100.200').is_link_local) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1244 | self.assertEqual(False, |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1245 | ipaddress.ip_address('169.255.100.200').is_link_local) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1246 | |
| 1247 | self.assertEqual(True, |
| 1248 | ipaddress.ip_address('127.100.200.254').is_loopback) |
| 1249 | self.assertEqual(True, ipaddress.ip_address('127.42.0.0').is_loopback) |
| 1250 | self.assertEqual(False, ipaddress.ip_address('128.0.0.0').is_loopback) |
| 1251 | self.assertEqual(True, ipaddress.ip_network('0.0.0.0').is_unspecified) |
| 1252 | |
| 1253 | def testReservedIpv6(self): |
| 1254 | |
| 1255 | self.assertEqual(True, ipaddress.ip_network('ffff::').is_multicast) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1256 | self.assertEqual(True, ipaddress.ip_network(2**128 - 1).is_multicast) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1257 | self.assertEqual(True, ipaddress.ip_network('ff00::').is_multicast) |
| 1258 | self.assertEqual(False, ipaddress.ip_network('fdff::').is_multicast) |
| 1259 | |
| 1260 | self.assertEqual(True, ipaddress.ip_network('fecf::').is_site_local) |
| 1261 | self.assertEqual(True, ipaddress.ip_network( |
| 1262 | 'feff:ffff:ffff:ffff::').is_site_local) |
| 1263 | self.assertEqual(False, ipaddress.ip_network( |
| 1264 | 'fbf:ffff::').is_site_local) |
| 1265 | self.assertEqual(False, ipaddress.ip_network('ff00::').is_site_local) |
| 1266 | |
| 1267 | self.assertEqual(True, ipaddress.ip_network('fc00::').is_private) |
| 1268 | self.assertEqual(True, ipaddress.ip_network( |
| 1269 | 'fc00:ffff:ffff:ffff::').is_private) |
| 1270 | self.assertEqual(False, ipaddress.ip_network('fbff:ffff::').is_private) |
| 1271 | self.assertEqual(False, ipaddress.ip_network('fe00::').is_private) |
| 1272 | |
| 1273 | self.assertEqual(True, ipaddress.ip_network('fea0::').is_link_local) |
| 1274 | self.assertEqual(True, ipaddress.ip_network( |
| 1275 | 'febf:ffff::').is_link_local) |
| 1276 | self.assertEqual(False, ipaddress.ip_network( |
| 1277 | 'fe7f:ffff::').is_link_local) |
| 1278 | self.assertEqual(False, ipaddress.ip_network('fec0::').is_link_local) |
| 1279 | |
| 1280 | self.assertEqual(True, ipaddress.ip_interface('0:0::0:01').is_loopback) |
| 1281 | self.assertEqual(False, ipaddress.ip_interface('::1/127').is_loopback) |
| 1282 | self.assertEqual(False, ipaddress.ip_network('::').is_loopback) |
| 1283 | self.assertEqual(False, ipaddress.ip_network('::2').is_loopback) |
| 1284 | |
| 1285 | self.assertEqual(True, ipaddress.ip_network('0::0').is_unspecified) |
| 1286 | self.assertEqual(False, ipaddress.ip_network('::1').is_unspecified) |
| 1287 | self.assertEqual(False, ipaddress.ip_network('::/127').is_unspecified) |
| 1288 | |
| 1289 | # test addresses |
| 1290 | self.assertEqual(True, ipaddress.ip_address('ffff::').is_multicast) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1291 | self.assertEqual(True, ipaddress.ip_address(2**128 - 1).is_multicast) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1292 | self.assertEqual(True, ipaddress.ip_address('ff00::').is_multicast) |
| 1293 | self.assertEqual(False, ipaddress.ip_address('fdff::').is_multicast) |
| 1294 | |
| 1295 | self.assertEqual(True, ipaddress.ip_address('fecf::').is_site_local) |
| 1296 | self.assertEqual(True, ipaddress.ip_address( |
| 1297 | 'feff:ffff:ffff:ffff::').is_site_local) |
| 1298 | self.assertEqual(False, ipaddress.ip_address( |
| 1299 | 'fbf:ffff::').is_site_local) |
| 1300 | self.assertEqual(False, ipaddress.ip_address('ff00::').is_site_local) |
| 1301 | |
| 1302 | self.assertEqual(True, ipaddress.ip_address('fc00::').is_private) |
| 1303 | self.assertEqual(True, ipaddress.ip_address( |
| 1304 | 'fc00:ffff:ffff:ffff::').is_private) |
| 1305 | self.assertEqual(False, ipaddress.ip_address('fbff:ffff::').is_private) |
| 1306 | self.assertEqual(False, ipaddress.ip_address('fe00::').is_private) |
| 1307 | |
| 1308 | self.assertEqual(True, ipaddress.ip_address('fea0::').is_link_local) |
| 1309 | self.assertEqual(True, ipaddress.ip_address( |
| 1310 | 'febf:ffff::').is_link_local) |
| 1311 | self.assertEqual(False, ipaddress.ip_address( |
| 1312 | 'fe7f:ffff::').is_link_local) |
| 1313 | self.assertEqual(False, ipaddress.ip_address('fec0::').is_link_local) |
| 1314 | |
| 1315 | self.assertEqual(True, ipaddress.ip_address('0:0::0:01').is_loopback) |
| 1316 | self.assertEqual(True, ipaddress.ip_address('::1').is_loopback) |
| 1317 | self.assertEqual(False, ipaddress.ip_address('::2').is_loopback) |
| 1318 | |
| 1319 | self.assertEqual(True, ipaddress.ip_address('0::0').is_unspecified) |
| 1320 | self.assertEqual(False, ipaddress.ip_address('::1').is_unspecified) |
| 1321 | |
| 1322 | # some generic IETF reserved addresses |
| 1323 | self.assertEqual(True, ipaddress.ip_address('100::').is_reserved) |
| 1324 | self.assertEqual(True, ipaddress.ip_network('4000::1/128').is_reserved) |
| 1325 | |
| 1326 | def testIpv4Mapped(self): |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1327 | self.assertEqual( |
| 1328 | ipaddress.ip_address('::ffff:192.168.1.1').ipv4_mapped, |
| 1329 | ipaddress.ip_address('192.168.1.1')) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1330 | self.assertEqual(ipaddress.ip_address('::c0a8:101').ipv4_mapped, None) |
| 1331 | self.assertEqual(ipaddress.ip_address('::ffff:c0a8:101').ipv4_mapped, |
| 1332 | ipaddress.ip_address('192.168.1.1')) |
| 1333 | |
| 1334 | def testAddrExclude(self): |
| 1335 | addr1 = ipaddress.ip_network('10.1.1.0/24') |
| 1336 | addr2 = ipaddress.ip_network('10.1.1.0/26') |
| 1337 | addr3 = ipaddress.ip_network('10.2.1.0/24') |
| 1338 | addr4 = ipaddress.ip_address('10.1.1.0') |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1339 | addr5 = ipaddress.ip_network('2001:db8::0/32') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1340 | self.assertEqual(sorted(list(addr1.address_exclude(addr2))), |
| 1341 | [ipaddress.ip_network('10.1.1.64/26'), |
| 1342 | ipaddress.ip_network('10.1.1.128/25')]) |
| 1343 | self.assertRaises(ValueError, list, addr1.address_exclude(addr3)) |
| 1344 | self.assertRaises(TypeError, list, addr1.address_exclude(addr4)) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1345 | self.assertRaises(TypeError, list, addr1.address_exclude(addr5)) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1346 | self.assertEqual(list(addr1.address_exclude(addr1)), []) |
| 1347 | |
| 1348 | def testHash(self): |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1349 | self.assertEqual(hash(ipaddress.ip_interface('10.1.1.0/24')), |
| 1350 | hash(ipaddress.ip_interface('10.1.1.0/24'))) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1351 | self.assertEqual(hash(ipaddress.ip_network('10.1.1.0/24')), |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1352 | hash(ipaddress.ip_network('10.1.1.0/24'))) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1353 | self.assertEqual(hash(ipaddress.ip_address('10.1.1.0')), |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1354 | hash(ipaddress.ip_address('10.1.1.0'))) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1355 | # i70 |
| 1356 | self.assertEqual(hash(ipaddress.ip_address('1.2.3.4')), |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1357 | hash(ipaddress.ip_address( |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1358 | int(ipaddress.ip_address('1.2.3.4')._ip)))) |
| 1359 | ip1 = ipaddress.ip_address('10.1.1.0') |
| 1360 | ip2 = ipaddress.ip_address('1::') |
| 1361 | dummy = {} |
| 1362 | dummy[self.ipv4_address] = None |
| 1363 | dummy[self.ipv6_address] = None |
| 1364 | dummy[ip1] = None |
| 1365 | dummy[ip2] = None |
| 1366 | self.assertTrue(self.ipv4_address in dummy) |
| 1367 | self.assertTrue(ip2 in dummy) |
| 1368 | |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1369 | def testIPBases(self): |
| 1370 | net = self.ipv4_network |
| 1371 | self.assertEqual('1.2.3.0/24', net.compressed) |
| 1372 | self.assertEqual( |
| 1373 | net._ip_int_from_prefix(24), |
| 1374 | net._ip_int_from_prefix(None)) |
| 1375 | net = self.ipv6_network |
| 1376 | self.assertRaises(ValueError, net._string_from_ip_int, 2**128 + 1) |
| 1377 | self.assertEqual( |
| 1378 | self.ipv6_address._string_from_ip_int(self.ipv6_address._ip), |
| 1379 | self.ipv6_address._string_from_ip_int(None)) |
| 1380 | |
Hynek Schlawack | 454a74d | 2012-06-04 18:14:02 +0200 | [diff] [blame] | 1381 | def testIPv6NetworkHelpers(self): |
| 1382 | net = self.ipv6_network |
| 1383 | self.assertEqual('2001:658:22a:cafe::/64', net.with_prefixlen) |
| 1384 | self.assertEqual('2001:658:22a:cafe::/ffff:ffff:ffff:ffff::', |
| 1385 | net.with_netmask) |
| 1386 | self.assertEqual('2001:658:22a:cafe::/::ffff:ffff:ffff:ffff', |
| 1387 | net.with_hostmask) |
| 1388 | self.assertEqual('2001:658:22a:cafe::/64', str(net)) |
| 1389 | |
| 1390 | def testIPv4NetworkHelpers(self): |
| 1391 | net = self.ipv4_network |
| 1392 | self.assertEqual('1.2.3.0/24', net.with_prefixlen) |
| 1393 | self.assertEqual('1.2.3.0/255.255.255.0', net.with_netmask) |
| 1394 | self.assertEqual('1.2.3.0/0.0.0.255', net.with_hostmask) |
| 1395 | self.assertEqual('1.2.3.0/24', str(net)) |
| 1396 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1397 | def testCopyConstructor(self): |
| 1398 | addr1 = ipaddress.ip_network('10.1.1.0/24') |
| 1399 | addr2 = ipaddress.ip_network(addr1) |
| 1400 | addr3 = ipaddress.ip_interface('2001:658:22a:cafe:200::1/64') |
| 1401 | addr4 = ipaddress.ip_interface(addr3) |
| 1402 | addr5 = ipaddress.IPv4Address('1.1.1.1') |
| 1403 | addr6 = ipaddress.IPv6Address('2001:658:22a:cafe:200::1') |
| 1404 | |
| 1405 | self.assertEqual(addr1, addr2) |
| 1406 | self.assertEqual(addr3, addr4) |
| 1407 | self.assertEqual(addr5, ipaddress.IPv4Address(addr5)) |
| 1408 | self.assertEqual(addr6, ipaddress.IPv6Address(addr6)) |
| 1409 | |
| 1410 | def testCompressIPv6Address(self): |
| 1411 | test_addresses = { |
| 1412 | '1:2:3:4:5:6:7:8': '1:2:3:4:5:6:7:8/128', |
| 1413 | '2001:0:0:4:0:0:0:8': '2001:0:0:4::8/128', |
| 1414 | '2001:0:0:4:5:6:7:8': '2001::4:5:6:7:8/128', |
| 1415 | '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128', |
| 1416 | '2001:0:3:4:5:6:7:8': '2001:0:3:4:5:6:7:8/128', |
| 1417 | '0:0:3:0:0:0:0:ffff': '0:0:3::ffff/128', |
| 1418 | '0:0:0:4:0:0:0:ffff': '::4:0:0:0:ffff/128', |
| 1419 | '0:0:0:0:5:0:0:ffff': '::5:0:0:ffff/128', |
| 1420 | '1:0:0:4:0:0:7:8': '1::4:0:0:7:8/128', |
| 1421 | '0:0:0:0:0:0:0:0': '::/128', |
| 1422 | '0:0:0:0:0:0:0:0/0': '::/0', |
| 1423 | '0:0:0:0:0:0:0:1': '::1/128', |
| 1424 | '2001:0658:022a:cafe:0000:0000:0000:0000/66': |
| 1425 | '2001:658:22a:cafe::/66', |
| 1426 | '::1.2.3.4': '::102:304/128', |
| 1427 | '1:2:3:4:5:ffff:1.2.3.4': '1:2:3:4:5:ffff:102:304/128', |
| 1428 | '::7:6:5:4:3:2:1': '0:7:6:5:4:3:2:1/128', |
| 1429 | '::7:6:5:4:3:2:0': '0:7:6:5:4:3:2:0/128', |
| 1430 | '7:6:5:4:3:2:1::': '7:6:5:4:3:2:1:0/128', |
| 1431 | '0:6:5:4:3:2:1::': '0:6:5:4:3:2:1:0/128', |
| 1432 | } |
| 1433 | for uncompressed, compressed in list(test_addresses.items()): |
| 1434 | self.assertEqual(compressed, str(ipaddress.IPv6Interface( |
| 1435 | uncompressed))) |
| 1436 | |
| 1437 | def testExplodeShortHandIpStr(self): |
| 1438 | addr1 = ipaddress.IPv6Interface('2001::1') |
| 1439 | addr2 = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') |
| 1440 | addr3 = ipaddress.IPv6Network('2001::/96') |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1441 | addr4 = ipaddress.IPv4Address('192.168.178.1') |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1442 | self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0001/128', |
| 1443 | addr1.exploded) |
| 1444 | self.assertEqual('0000:0000:0000:0000:0000:0000:0000:0001/128', |
| 1445 | ipaddress.IPv6Interface('::1/128').exploded) |
| 1446 | # issue 77 |
| 1447 | self.assertEqual('2001:0000:5ef5:79fd:0000:059d:a0e5:0ba1', |
| 1448 | addr2.exploded) |
| 1449 | self.assertEqual('2001:0000:0000:0000:0000:0000:0000:0000/96', |
| 1450 | addr3.exploded) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1451 | self.assertEqual('192.168.178.1', addr4.exploded) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1452 | |
| 1453 | def testIntRepresentation(self): |
| 1454 | self.assertEqual(16909060, int(self.ipv4_address)) |
| 1455 | self.assertEqual(42540616829182469433547762482097946625, |
| 1456 | int(self.ipv6_address)) |
| 1457 | |
| 1458 | def testHexRepresentation(self): |
| 1459 | self.assertEqual(hex(0x1020304), |
| 1460 | hex(self.ipv4_address)) |
| 1461 | |
| 1462 | self.assertEqual(hex(0x20010658022ACAFE0200000000000001), |
| 1463 | hex(self.ipv6_address)) |
| 1464 | |
| 1465 | def testForceVersion(self): |
| 1466 | self.assertEqual(ipaddress.ip_network(1).version, 4) |
Nick Coghlan | 51c3067 | 2012-05-27 00:25:58 +1000 | [diff] [blame] | 1467 | self.assertEqual(ipaddress.IPv6Network(1).version, 6) |
Sandro Tosi | 876ecad | 2012-05-23 22:26:55 +0200 | [diff] [blame] | 1468 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1469 | def testWithStar(self): |
| 1470 | self.assertEqual(str(self.ipv4_interface.with_prefixlen), "1.2.3.4/24") |
| 1471 | self.assertEqual(str(self.ipv4_interface.with_netmask), |
| 1472 | "1.2.3.4/255.255.255.0") |
| 1473 | self.assertEqual(str(self.ipv4_interface.with_hostmask), |
| 1474 | "1.2.3.4/0.0.0.255") |
| 1475 | |
| 1476 | self.assertEqual(str(self.ipv6_interface.with_prefixlen), |
| 1477 | '2001:658:22a:cafe:200::1/64') |
| 1478 | # rfc3513 sec 2.3 says that ipv6 only uses cidr notation for |
| 1479 | # subnets |
| 1480 | self.assertEqual(str(self.ipv6_interface.with_netmask), |
| 1481 | '2001:658:22a:cafe:200::1/64') |
| 1482 | # this probably don't make much sense, but it's included for |
| 1483 | # compatibility with ipv4 |
| 1484 | self.assertEqual(str(self.ipv6_interface.with_hostmask), |
| 1485 | '2001:658:22a:cafe:200::1/::ffff:ffff:ffff:ffff') |
| 1486 | |
| 1487 | def testNetworkElementCaching(self): |
| 1488 | # V4 - make sure we're empty |
| 1489 | self.assertFalse('network_address' in self.ipv4_network._cache) |
| 1490 | self.assertFalse('broadcast_address' in self.ipv4_network._cache) |
| 1491 | self.assertFalse('hostmask' in self.ipv4_network._cache) |
| 1492 | |
| 1493 | # V4 - populate and test |
| 1494 | self.assertEqual(self.ipv4_network.network_address, |
| 1495 | ipaddress.IPv4Address('1.2.3.0')) |
| 1496 | self.assertEqual(self.ipv4_network.broadcast_address, |
| 1497 | ipaddress.IPv4Address('1.2.3.255')) |
| 1498 | self.assertEqual(self.ipv4_network.hostmask, |
| 1499 | ipaddress.IPv4Address('0.0.0.255')) |
| 1500 | |
| 1501 | # V4 - check we're cached |
| 1502 | self.assertTrue('broadcast_address' in self.ipv4_network._cache) |
| 1503 | self.assertTrue('hostmask' in self.ipv4_network._cache) |
| 1504 | |
| 1505 | # V6 - make sure we're empty |
| 1506 | self.assertFalse('broadcast_address' in self.ipv6_network._cache) |
| 1507 | self.assertFalse('hostmask' in self.ipv6_network._cache) |
| 1508 | |
| 1509 | # V6 - populate and test |
| 1510 | self.assertEqual(self.ipv6_network.network_address, |
| 1511 | ipaddress.IPv6Address('2001:658:22a:cafe::')) |
| 1512 | self.assertEqual(self.ipv6_interface.network.network_address, |
| 1513 | ipaddress.IPv6Address('2001:658:22a:cafe::')) |
| 1514 | |
| 1515 | self.assertEqual( |
| 1516 | self.ipv6_network.broadcast_address, |
| 1517 | ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) |
| 1518 | self.assertEqual(self.ipv6_network.hostmask, |
| 1519 | ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) |
| 1520 | self.assertEqual( |
| 1521 | self.ipv6_interface.network.broadcast_address, |
| 1522 | ipaddress.IPv6Address('2001:658:22a:cafe:ffff:ffff:ffff:ffff')) |
| 1523 | self.assertEqual(self.ipv6_interface.network.hostmask, |
| 1524 | ipaddress.IPv6Address('::ffff:ffff:ffff:ffff')) |
| 1525 | |
| 1526 | # V6 - check we're cached |
| 1527 | self.assertTrue('broadcast_address' in self.ipv6_network._cache) |
| 1528 | self.assertTrue('hostmask' in self.ipv6_network._cache) |
Hynek Schlawack | 91c5a34 | 2012-06-05 11:55:58 +0200 | [diff] [blame] | 1529 | self.assertTrue( |
| 1530 | 'broadcast_address' in self.ipv6_interface.network._cache) |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1531 | self.assertTrue('hostmask' in self.ipv6_interface.network._cache) |
| 1532 | |
| 1533 | def testTeredo(self): |
| 1534 | # stolen from wikipedia |
| 1535 | server = ipaddress.IPv4Address('65.54.227.120') |
| 1536 | client = ipaddress.IPv4Address('192.0.2.45') |
| 1537 | teredo_addr = '2001:0000:4136:e378:8000:63bf:3fff:fdd2' |
| 1538 | self.assertEqual((server, client), |
| 1539 | ipaddress.ip_address(teredo_addr).teredo) |
| 1540 | bad_addr = '2000::4136:e378:8000:63bf:3fff:fdd2' |
| 1541 | self.assertFalse(ipaddress.ip_address(bad_addr).teredo) |
| 1542 | bad_addr = '2001:0001:4136:e378:8000:63bf:3fff:fdd2' |
| 1543 | self.assertFalse(ipaddress.ip_address(bad_addr).teredo) |
| 1544 | |
| 1545 | # i77 |
| 1546 | teredo_addr = ipaddress.IPv6Address('2001:0:5ef5:79fd:0:59d:a0e5:ba1') |
| 1547 | self.assertEqual((ipaddress.IPv4Address('94.245.121.253'), |
| 1548 | ipaddress.IPv4Address('95.26.244.94')), |
| 1549 | teredo_addr.teredo) |
| 1550 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1551 | def testsixtofour(self): |
| 1552 | sixtofouraddr = ipaddress.ip_address('2002:ac1d:2d64::1') |
| 1553 | bad_addr = ipaddress.ip_address('2000:ac1d:2d64::1') |
| 1554 | self.assertEqual(ipaddress.IPv4Address('172.29.45.100'), |
| 1555 | sixtofouraddr.sixtofour) |
| 1556 | self.assertFalse(bad_addr.sixtofour) |
| 1557 | |
Nick Coghlan | dc9b255 | 2012-05-20 21:01:57 +1000 | [diff] [blame] | 1558 | |
| 1559 | if __name__ == '__main__': |
| 1560 | unittest.main() |