gh-87799: Improve the textual representation of IPv4-mapped IPv6 addresses (#29345)

Represent IPv4-mapped IPv6 address as xxxd.d.d.d,
where the 'x's are the hexadecimal values
of the six high-order 16-bit pieces of the address,
and the 'd's are the decimal values
of the four low-order 8-bit pieces of the address
(standard IPv4 representation).

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
This commit is contained in:
opavliuk 2023-07-31 16:33:26 +02:00 committed by GitHub
parent 520efecfc3
commit f22bf8e3cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 1 deletions

View file

@ -1321,6 +1321,17 @@ class IpaddrUnitTest(unittest.TestCase):
self.assertEqual(str(self.ipv6_scoped_interface.ip),
'2001:658:22a:cafe:200::1')
def testIPv6IPv4MappedStringRepresentation(self):
long_prefix = '0000:0000:0000:0000:0000:ffff:'
short_prefix = '::ffff:'
ipv4 = '1.2.3.4'
ipv6_ipv4_str = short_prefix + ipv4
ipv6_ipv4_addr = ipaddress.IPv6Address(ipv6_ipv4_str)
ipv6_ipv4_iface = ipaddress.IPv6Interface(ipv6_ipv4_str)
self.assertEqual(str(ipv6_ipv4_addr), ipv6_ipv4_str)
self.assertEqual(ipv6_ipv4_addr.exploded, long_prefix + ipv4)
self.assertEqual(str(ipv6_ipv4_iface.ip), ipv6_ipv4_str)
def testGetScopeId(self):
self.assertEqual(self.ipv6_address.scope_id,
None)