Skip to content

Commit 790e630

Browse files
authored
fix(ipv6_ext): give the version gate's message the house form (#2397)
IPv6_Ext's version gate raised "only valid for IPv6, got version=4" -- a bespoke tail unlike the <prefix>: <lowercase description>: must be X, not {value!r} form #2232/#2257 established, and it never named the version that was wanted. The same method's docstring quoted the message verbatim for the AH/ESP case, so it was stale by the same edit. Message is now "invalid version: must be 6, not {version!r}". Test: test_version_gate_message_is_in_the_house_form pins the message whole on both routes into the gate -- a bare-bytes call and the BytesIO/alias=/extension=True stream form.
1 parent a660bf3 commit 790e630

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎pcapkit/protocols/internet/ipv6_ext.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
584584
:class:`~pcapkit.protocols.internet.ah.AH` and
585585
:class:`~pcapkit.protocols.internet.esp.ESP` both default to
586586
``version=4`` and are valid under IPv4, so an unconditional gate
587-
would reject them (``ProtocolError: ESP: only valid for IPv6, got
588-
version=4``).
587+
would reject them (``ProtocolError: ESP: invalid version: must be
588+
6, not 4``).
589589
590590
The gate exists because this class is registered into
591591
:attr:`Internet.__proto__
@@ -608,7 +608,7 @@ def __post_init__(self, file: 'Optional[IO[bytes] | bytes]' = None, length: 'Opt
608608
"""
609609
if self.__data__ is Data_IPv6_Ext and version != 6:
610610
raise ProtocolError(
611-
f'{self.__class__.__name__}: only valid for IPv6, got version={version}')
611+
f'{self.__class__.__name__}: invalid version: must be 6, not {version!r}')
612612

613613
#: bool: If the protocol is used as an IPv6 extension header.
614614
self._extf = extension

‎tests/protocols/internet/test_ipv6_ext_unit.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,31 @@ def test_version_gate_rejects_non_ipv6_construction(self) -> None:
312312
IPv6_Ext(io.BytesIO(raw), len(raw), version=4, extension=True,
313313
alias=int(ExtensionHeader.HOPOPT))
314314

315+
def test_version_gate_message_is_in_the_house_form(self) -> None:
316+
"""GitHub issue #2302: the gate said ``only valid for IPv6, got
317+
version=4``, which carries the prefix but not the
318+
``<prefix>: <lowercase description>: ... <value>`` tail #2232 and
319+
#2257 gave the rest of the library, and never named the version it
320+
wanted. Both the issue's own bare-bytes call and the stream form are
321+
pinned whole, since the two reach the gate by different routes.
322+
"""
323+
from pcapkit.const.ipv6.extension_header import ExtensionHeader
324+
from pcapkit.const.reg.transtype import TransType
325+
from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext
326+
from pcapkit.utilities.exceptions import ProtocolError
327+
328+
expected = 'IPv6_Ext: invalid version: must be 6, not 4'
329+
330+
with self.assertRaises(ProtocolError) as context:
331+
IPv6_Ext(b'\x06\x00\x00\x00\x00\x00\x00\x00', version=4)
332+
self.assertEqual(str(context.exception), expected)
333+
334+
raw = bytes([int(TransType.UDP), 1]) + b'\x00' * 14
335+
with self.assertRaises(ProtocolError) as context:
336+
IPv6_Ext(io.BytesIO(raw), len(raw), version=4, extension=True,
337+
alias=int(ExtensionHeader.HOPOPT))
338+
self.assertEqual(str(context.exception), expected)
339+
315340
def test_shim6_registration_does_not_leak_into_ipv4_parsing(self) -> None:
316341
"""GitHub issue #891: registering ``IPv6_Ext``
317342
into the shared ``Internet.__proto__`` for Shim6 must not make an

0 commit comments

Comments
 (0)