MCPcopy Index your code
hub / github.com/RustPython/RustPython / testIPv6toString

Method testIPv6toString

Lib/test/test_socket.py:1383–1443  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1381 @unittest.skipUnless(hasattr(socket, 'inet_pton'),
1382 'test needs socket.inet_pton()')
1383 def testIPv6toString(self):
1384 try:
1385 from socket import inet_pton, AF_INET6, has_ipv6
1386 if not has_ipv6:
1387 self.skipTest('IPv6 not available')
1388 except ImportError:
1389 self.skipTest('could not import needed symbols from socket')
1390
1391 if sys.platform == "win32":
1392 try:
1393 inet_pton(AF_INET6, '::')
1394 except OSError as e:
1395 if e.winerror == 10022:
1396 self.skipTest('IPv6 might not be supported')
1397
1398 f = lambda a: inet_pton(AF_INET6, a)
1399 assertInvalid = lambda a: self.assertRaises(
1400 (OSError, ValueError), f, a
1401 )
1402
1403 self.assertEqual(b'\x00' * 16, f('::'))
1404 self.assertEqual(b'\x00' * 16, f('0::0'))
1405 self.assertEqual(b'\x00\x01' + b'\x00' * 14, f('1::'))
1406 self.assertEqual(
1407 b'\x45\xef\x76\xcb\x00\x1a\x56\xef\xaf\xeb\x0b\xac\x19\x24\xae\xae',
1408 f('45ef:76cb:1a:56ef:afeb:bac:1924:aeae')
1409 )
1410 self.assertEqual(
1411 b'\xad\x42\x0a\xbc' + b'\x00' * 4 + b'\x01\x27\x00\x00\x02\x54\x00\x02',
1412 f('ad42:abc::127:0:254:2')
1413 )
1414 self.assertEqual(b'\x00\x12\x00\x0a' + b'\x00' * 12, f('12:a::'))
1415 assertInvalid('0x20::')
1416 assertInvalid(':::')
1417 assertInvalid('::0::')
1418 assertInvalid('1::abc::')
1419 assertInvalid('1::abc::def')
1420 assertInvalid('1:2:3:4:5:6')
1421 assertInvalid('1:2:3:4:5:6:')
1422 assertInvalid('1:2:3:4:5:6:7:8:0')
1423 # bpo-29972: inet_pton() doesn't fail on AIX
1424 if not AIX:
1425 assertInvalid('1:2:3:4:5:6:7:8:')
1426
1427 self.assertEqual(b'\x00' * 12 + b'\xfe\x2a\x17\x40',
1428 f('::254.42.23.64')
1429 )
1430 self.assertEqual(
1431 b'\x00\x42' + b'\x00' * 8 + b'\xa2\x9b\xfe\x2a\x17\x40',
1432 f('42::a29b:254.42.23.64')
1433 )
1434 self.assertEqual(
1435 b'\x00\x42\xa8\xb9\x00\x00\x00\x02\xff\xff\xa2\x9b\xfe\x2a\x17\x40',
1436 f('42:a8b9:0:2:ffff:a29b:254.42.23.64')
1437 )
1438 assertInvalid('255.254.253.252')
1439 assertInvalid('1::260.2.3.0')
1440 assertInvalid('1::0.be.e.0')

Callers

nothing calls this directly

Calls 5

inet_ptonFunction · 0.90
skipTestMethod · 0.80
fFunction · 0.70
assertRaisesMethod · 0.45
assertEqualMethod · 0.45

Tested by

no test coverage detected