|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""A tail shorter than 8 octets is not an ``IPv6_Ext`` header. |
| 3 | +
|
| 4 | +GitHub issue #1178. :meth:`~pcapkit.protocols.protocol.ProtocolBase._parse_next_layer` |
| 5 | +keeps a next layer as :class:`~pcapkit.protocols.misc.raw.Raw` when fewer octets |
| 6 | +were captured than its ``__length_hint__`` (:issue:`1170`). |
| 7 | +:class:`~pcapkit.protocols.internet.ipv6_ext.IPv6_Ext` hinted 2, though |
| 8 | +:rfc:`6564#section-4` and :rfc:`8200#section-4` make every extension header at |
| 9 | +least 8 octets, so a 2--7 octet tail of a longer declared payload was parsed as |
| 10 | +an 8-octet header built partly from octets that were never captured. |
| 11 | +
|
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import importlib.util |
| 16 | +import unittest |
| 17 | + |
| 18 | +from tests._support import reimport_once_per_class |
| 19 | + |
| 20 | +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') |
| 21 | +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) |
| 22 | + |
| 23 | +#: ``alias`` values that reach the generic fallback: an absent alias, a number |
| 24 | +#: IANA has not registered, ``Shim6`` (registered to ``IPv6_Ext`` directly) and |
| 25 | +#: ``HOPOPT`` (the code ``IPv6`` substitutes the fallback for). |
| 26 | +ALIASES = {'absent': {}, 'unregistered': {'alias': 200}, 'Shim6': {'alias': 140}, 'HOPOPT': {'alias': 0}} |
| 27 | + |
| 28 | +#: The IPv6 payload length the caller declares, longer than any tail below. |
| 29 | +DECLARED = 300 |
| 30 | + |
| 31 | + |
| 32 | +def tail(size: int) -> bytes: |
| 33 | + """``size`` octets opening with No Next Header and Hdr Ext Len 0 or 1.""" |
| 34 | + return bytes([59, 1 if size >= 16 else 0]) + bytes(range(1, size - 1)) |
| 35 | + |
| 36 | + |
| 37 | +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') |
| 38 | +class IPv6ExtLengthHintTests(unittest.TestCase): |
| 39 | + """The fallback's hint is the 8-octet minimum header.""" |
| 40 | + |
| 41 | + def setUp(self) -> None: |
| 42 | + reimport_once_per_class(self) |
| 43 | + |
| 44 | + def parse(self, data: bytes, **kwargs: int) -> object: |
| 45 | + from pcapkit.protocols.internet.ipv6 import IPv6 |
| 46 | + from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext |
| 47 | + |
| 48 | + outer = bytes([0x60]) + bytes(5) + bytes([59, 64]) + bytes(32) |
| 49 | + return IPv6(outer, len(outer))._parse_next_layer( |
| 50 | + IPv6_Ext, data, DECLARED, version=6, extension=True, **kwargs) |
| 51 | + |
| 52 | + def test_short_tail_is_raw_and_rebuilds_exactly(self) -> None: |
| 53 | + from pcapkit.protocols.misc.raw import Raw |
| 54 | + |
| 55 | + for label, kwargs in ALIASES.items(): |
| 56 | + for size in range(2, 8): |
| 57 | + with self.subTest(alias=label, size=size): |
| 58 | + parsed = self.parse(tail(size), **kwargs) |
| 59 | + self.assertIsInstance(parsed, Raw) |
| 60 | + self.assertEqual(bytes(Raw.from_data(parsed.info)), tail(size)) |
| 61 | + |
| 62 | + def test_complete_header_still_parses(self) -> None: |
| 63 | + from pcapkit.protocols.internet.ipv6_ext import IPv6_Ext |
| 64 | + |
| 65 | + for label, kwargs in ALIASES.items(): |
| 66 | + for size in (8, 16): |
| 67 | + with self.subTest(alias=label, size=size): |
| 68 | + parsed = self.parse(tail(size), **kwargs) |
| 69 | + self.assertIsInstance(parsed, IPv6_Ext) |
| 70 | + self.assertEqual(parsed.length, size) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + unittest.main() |
0 commit comments