|
8 | 8 | import ipaddress |
9 | 9 |
|
10 | 10 |
|
11 | | -# Compatibility function to cast str to bytes objects |
12 | | -_cb = lambda bytestr: bytes(bytestr, 'charmap') |
13 | | - |
14 | | - |
15 | 11 | class IpaddrUnitTest(unittest.TestCase): |
16 | 12 |
|
17 | 13 | def setUp(self): |
@@ -267,25 +263,36 @@ def testIpFromInt(self): |
267 | 263 | 6) |
268 | 264 |
|
269 | 265 | def testIpFromPacked(self): |
270 | | - ip = ipaddress.ip_network |
271 | | - |
| 266 | + address = ipaddress.ip_address |
272 | 267 | self.assertEqual(self.ipv4_interface._ip, |
273 | | - ipaddress.ip_interface(_cb('\x01\x02\x03\x04'))._ip) |
274 | | - self.assertEqual(ip('255.254.253.252'), |
275 | | - ip(_cb('\xff\xfe\xfd\xfc'))) |
276 | | - self.assertRaises(ValueError, ipaddress.ip_network, _cb('\x00' * 3)) |
277 | | - self.assertRaises(ValueError, ipaddress.ip_network, _cb('\x00' * 5)) |
| 268 | + ipaddress.ip_interface(b'\x01\x02\x03\x04')._ip) |
| 269 | + self.assertEqual(address('255.254.253.252'), |
| 270 | + address(b'\xff\xfe\xfd\xfc')) |
278 | 271 | self.assertEqual(self.ipv6_interface.ip, |
279 | 272 | ipaddress.ip_interface( |
280 | | - _cb('\x20\x01\x06\x58\x02\x2a\xca\xfe' |
281 | | - '\x02\x00\x00\x00\x00\x00\x00\x01')).ip) |
282 | | - self.assertEqual(ip('ffff:2:3:4:ffff::'), |
283 | | - ip(_cb('\xff\xff\x00\x02\x00\x03\x00\x04' + |
284 | | - '\xff\xff' + '\x00' * 6))) |
285 | | - self.assertEqual(ip('::'), |
286 | | - ip(_cb('\x00' * 16))) |
287 | | - self.assertRaises(ValueError, ip, _cb('\x00' * 15)) |
288 | | - self.assertRaises(ValueError, ip, _cb('\x00' * 17)) |
| 273 | + b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
| 274 | + b'\x02\x00\x00\x00\x00\x00\x00\x01').ip) |
| 275 | + self.assertEqual(address('ffff:2:3:4:ffff::'), |
| 276 | + address(b'\xff\xff\x00\x02\x00\x03\x00\x04' + |
| 277 | + b'\xff\xff' + b'\x00' * 6)) |
| 278 | + self.assertEqual(address('::'), |
| 279 | + address(b'\x00' * 16)) |
| 280 | + |
| 281 | + def testIpFromPackedErrors(self): |
| 282 | + def assertInvalidPackedAddress(f, length): |
| 283 | + self.assertRaises(ValueError, f, b'\x00' * length) |
| 284 | + assertInvalidPackedAddress(ipaddress.ip_address, 3) |
| 285 | + assertInvalidPackedAddress(ipaddress.ip_address, 5) |
| 286 | + assertInvalidPackedAddress(ipaddress.ip_address, 15) |
| 287 | + assertInvalidPackedAddress(ipaddress.ip_address, 17) |
| 288 | + assertInvalidPackedAddress(ipaddress.ip_interface, 3) |
| 289 | + assertInvalidPackedAddress(ipaddress.ip_interface, 5) |
| 290 | + assertInvalidPackedAddress(ipaddress.ip_interface, 15) |
| 291 | + assertInvalidPackedAddress(ipaddress.ip_interface, 17) |
| 292 | + assertInvalidPackedAddress(ipaddress.ip_network, 3) |
| 293 | + assertInvalidPackedAddress(ipaddress.ip_network, 5) |
| 294 | + assertInvalidPackedAddress(ipaddress.ip_network, 15) |
| 295 | + assertInvalidPackedAddress(ipaddress.ip_network, 17) |
289 | 296 |
|
290 | 297 | def testGetIp(self): |
291 | 298 | self.assertEqual(int(self.ipv4_interface.ip), 16909060) |
@@ -893,17 +900,17 @@ def testMaxPrefixLength(self): |
893 | 900 |
|
894 | 901 | def testPacked(self): |
895 | 902 | self.assertEqual(self.ipv4_address.packed, |
896 | | - _cb('\x01\x02\x03\x04')) |
| 903 | + b'\x01\x02\x03\x04') |
897 | 904 | self.assertEqual(ipaddress.IPv4Interface('255.254.253.252').packed, |
898 | | - _cb('\xff\xfe\xfd\xfc')) |
| 905 | + b'\xff\xfe\xfd\xfc') |
899 | 906 | self.assertEqual(self.ipv6_address.packed, |
900 | | - _cb('\x20\x01\x06\x58\x02\x2a\xca\xfe' |
901 | | - '\x02\x00\x00\x00\x00\x00\x00\x01')) |
| 907 | + b'\x20\x01\x06\x58\x02\x2a\xca\xfe' |
| 908 | + b'\x02\x00\x00\x00\x00\x00\x00\x01') |
902 | 909 | self.assertEqual(ipaddress.IPv6Interface('ffff:2:3:4:ffff::').packed, |
903 | | - _cb('\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff' |
904 | | - + '\x00' * 6)) |
| 910 | + b'\xff\xff\x00\x02\x00\x03\x00\x04\xff\xff' |
| 911 | + + b'\x00' * 6) |
905 | 912 | self.assertEqual(ipaddress.IPv6Interface('::1:0:0:0:0').packed, |
906 | | - _cb('\x00' * 6 + '\x00\x01' + '\x00' * 8)) |
| 913 | + b'\x00' * 6 + b'\x00\x01' + b'\x00' * 8) |
907 | 914 |
|
908 | 915 | def testIpStrFromPrefixlen(self): |
909 | 916 | ipv4 = ipaddress.IPv4Interface('1.2.3.4/24') |
|
0 commit comments