diff --git a/docs/source/pcapkit/utilities/contextvars.rst b/docs/source/pcapkit/utilities/contextvars.rst index 8a34825b05..9f8f82cab4 100644 --- a/docs/source/pcapkit/utilities/contextvars.rst +++ b/docs/source/pcapkit/utilities/contextvars.rst @@ -17,5 +17,8 @@ a module at any layer may import it; see :ref:`utilities-or-corekit`. .. autodata:: pcapkit.utilities.contextvars.frame_depth :no-value: +.. autodata:: pcapkit.utilities.contextvars.frame_length + :no-value: + .. autodata:: pcapkit.utilities.contextvars.rebuilding :no-value: diff --git a/pcapkit/protocols/internet/internet.py b/pcapkit/protocols/internet/internet.py index bff2517bff..2a99e10f0c 100644 --- a/pcapkit/protocols/internet/internet.py +++ b/pcapkit/protocols/internet/internet.py @@ -24,7 +24,7 @@ from pcapkit.protocols.protocol import _PT, _ST, ProtocolBase from pcapkit.protocols.schema.schema import keep_short_read, replay_short_read from pcapkit.utilities import config -from pcapkit.utilities.contextvars import rebuilding +from pcapkit.utilities.contextvars import frame_length, rebuilding from pcapkit.utilities.exceptions import stacklevel from pcapkit.utilities.warnings import ProtocolWarning, RegistryWarning, warn @@ -466,10 +466,21 @@ def _decode_next_layer(self, dict_: '_PT', proto: 'Optional[int]' = None, # pyl :meth:`Info.to_dict ` carries and the dumpers leave out. + The next layer is parsed with + :data:`~pcapkit.utilities.contextvars.frame_length` cleared, since + this layer, not the frame, bounds its payload (:issue:`2653`). + """ - next_ = cast('ProtocolBase', # type: ignore[redundant-cast] - self._import_next_layer(proto, length, packet=packet, version=version, - payload=payload)) # type: ignore[misc,call-arg] + # NOTE: The length this layer hands on bounds the payload, so octets the + # capture cut off the frame past it are none of the next layer's (#2653). + # An IPv4 Total Length of 0, read as the rest of the frame, loses the cut. + token = frame_length.set(None) + try: + next_ = cast('ProtocolBase', # type: ignore[redundant-cast] + self._import_next_layer(proto, length, packet=packet, version=version, + payload=payload)) # type: ignore[misc,call-arg] + finally: + frame_length.reset(token) info, chain = next_.info, next_.protochain # make next layer protocol name diff --git a/pcapkit/protocols/link/gre.py b/pcapkit/protocols/link/gre.py index dccf27c283..ce29692d49 100644 --- a/pcapkit/protocols/link/gre.py +++ b/pcapkit/protocols/link/gre.py @@ -74,6 +74,7 @@ from pcapkit.protocols.link.link import Link from pcapkit.protocols.schema.link.gre import GRE as Schema_GRE from pcapkit.utilities import config +from pcapkit.utilities.contextvars import frame_length from pcapkit.utilities.exceptions import ProtocolError if TYPE_CHECKING: @@ -104,9 +105,11 @@ class GRE(Link[Data_GRE, Schema_GRE], :data:`~pcapkit.utilities.config.CHECKSUM_VERIFY`, and computed by :meth:`make` when ``checksum_present`` asks for one without giving it. Neither this header nor the Linux cooked-capture header before it states - a length, so the octets verified are the ones captured: a frame the - capture cut short reads as - :attr:`~pcapkit.corekit.checksum.internet.ChecksumStatus.INVALID`. + a length, so the message runs to the end of its frame. A frame the + capture cut short, holding fewer octets than its original length, reads + as :attr:`~pcapkit.corekit.checksum.internet.ChecksumStatus.TRUNCATED`, + as :data:`~pcapkit.utilities.contextvars.frame_length` tells it + (:issue:`2653`). """ @@ -217,7 +220,13 @@ def read(self, length: 'Optional[int]' = None, **kwargs: 'Any') -> 'Data_GRE': if _checksum is None: status = ChecksumStatus.NOT_COMPUTED else: - status = verify_message(self.alias, self._data, _checksum.to_bytes(2, 'big'), length=length) + # NOTE: Behind LinuxSLL the message runs to the end of its frame, so + # what the capture cut off is covered too. Behind IP the value is None: + # Internet clears it, as the length IP declares bounds GRE (#2653). + lengths = frame_length.get() + missing = 0 if lengths is None else max(lengths[1] - lengths[0], 0) + status = verify_message(self.alias, self._data, _checksum.to_bytes(2, 'big'), + length=length + missing) gre.__update__([ ('checksum_status', status), ]) diff --git a/pcapkit/protocols/misc/pcap/frame.py b/pcapkit/protocols/misc/pcap/frame.py index 189b745e46..d3d4e58f07 100644 --- a/pcapkit/protocols/misc/pcap/frame.py +++ b/pcapkit/protocols/misc/pcap/frame.py @@ -48,6 +48,7 @@ from pcapkit.protocols.schema.misc.pcap.frame import Frame as Schema_Frame from pcapkit.protocols.schema.misc.pcap.header import RESOLUTION from pcapkit.utilities.compat import localcontext +from pcapkit.utilities.contextvars import frame_length from pcapkit.utilities.datetime import fromtimestamp, totimestamp from pcapkit.utilities.exceptions import StreamEOFError, UnsupportedCall, stacklevel from pcapkit.utilities.warnings import ProtocolWarning, RegistryWarning, warn @@ -416,7 +417,14 @@ def read(self, length: 'Optional[int]' = None, *, _read: 'bool' = True, # (#2308). As the dissection does not tell the two apart, # ``test_truncated_ipv4_header_is_raw_with_its_error`` pins the argument # where it is passed (#2236). - return self._decode_next_layer(frame, self._ghdr.network, frame.cap_len) + # + # NOTE: A layer that states no length cannot see the cut, so the layers + # below are told both lengths (#2653). + token = frame_length.set((_ilen, _olen)) + try: + return self._decode_next_layer(frame, self._ghdr.network, frame.cap_len) + finally: + frame_length.reset(token) def make(self, timestamp: 'Optional[float | Decimal | int | dt_type]' = None, diff --git a/pcapkit/protocols/misc/pcapng.py b/pcapkit/protocols/misc/pcapng.py index 7599176d3d..da4a7caef2 100644 --- a/pcapkit/protocols/misc/pcapng.py +++ b/pcapkit/protocols/misc/pcapng.py @@ -211,6 +211,7 @@ from pcapkit.protocols.schema.misc.pcapng import zigbee_key_held from pcapkit.protocols.schema.schema import Schema from pcapkit.utilities.compat import StrEnum, localcontext +from pcapkit.utilities.contextvars import frame_length from pcapkit.utilities.datetime import fromtimestamp, totimestamp from pcapkit.utilities.exceptions import (BaseError, EnumValueError, FormatError, ProtocolError, StreamEOFError, UnsupportedCall, stacklevel) @@ -2663,8 +2664,13 @@ def _read_block_epb(self, schema: 'Schema_EnhancedPacketBlock', *, warn(f'PCAP-NG: [Block {header.type}] captured length {schema.captured_len} runs past ' f'the block, which holds {len(schema.packet_data)} octet(s); kept as captured', ProtocolWarning, stacklevel=stacklevel()) - return self._decode_next_layer(data, self._get_linktype(schema.interface_id), - schema.captured_len) # type: ignore[return-value] + # NOTE: The layers below are told both lengths, as a PCAP frame tells them (#2653). + token = frame_length.set((data.captured_len, data.original_len)) + try: + return self._decode_next_layer(data, self._get_linktype(schema.interface_id), + schema.captured_len) # type: ignore[return-value] + finally: + frame_length.reset(token) def _read_block_spb(self, schema: 'Schema_SimplePacketBlock', *, header: 'Schema_PCAPNG') -> 'Data_SimplePacketBlock': @@ -2725,7 +2731,12 @@ def _read_block_spb(self, schema: 'Schema_SimplePacketBlock', *, warn(f'PCAP-NG: [Block {header.type}] packet data of {data.captured_len} octet(s) ' f'exceeds the original length of {data.original_len}; kept as captured', ProtocolWarning, stacklevel=stacklevel()) - return self._decode_next_layer(data, self._get_linktype(0), data.captured_len) # type: ignore[return-value] + # NOTE: The layers below are told both lengths, as a PCAP frame tells them (#2653). + token = frame_length.set((data.captured_len, data.original_len)) + try: + return self._decode_next_layer(data, self._get_linktype(0), data.captured_len) # type: ignore[return-value] + finally: + frame_length.reset(token) def _read_block_nrb(self, schema: 'Schema_NameResolutionBlock', *, header: 'Schema_PCAPNG') -> 'Data_NameResolutionBlock': @@ -3062,8 +3073,13 @@ def _read_block_packet(self, schema: 'Schema_PacketBlock', *, warn(f'PCAP-NG: [Block {header.type}] captured length {schema.captured_length} runs past ' f'the block, which holds {len(schema.packet_data)} octet(s); kept as captured', ProtocolWarning, stacklevel=stacklevel()) - return self._decode_next_layer(data, self._get_linktype(schema.interface_id), - schema.captured_length) # type: ignore[return-value] + # NOTE: The layers below are told both lengths, as a PCAP frame tells them (#2653). + token = frame_length.set((data.captured_len, data.original_len)) + try: + return self._decode_next_layer(data, self._get_linktype(schema.interface_id), + schema.captured_length) # type: ignore[return-value] + finally: + frame_length.reset(token) def _read_pcapng_options(self, options_schema: 'list[Schema_Option]') -> 'Option': """Read PCAP-NG options. diff --git a/pcapkit/utilities/contextvars.py b/pcapkit/utilities/contextvars.py index 36bebda4d6..3802fc92ff 100644 --- a/pcapkit/utilities/contextvars.py +++ b/pcapkit/utilities/contextvars.py @@ -14,8 +14,12 @@ """ import contextvars +from typing import TYPE_CHECKING -__all__ = ['beholding', 'frame_depth', 'rebuilding'] +if TYPE_CHECKING: + from typing import Optional + +__all__ = ['beholding', 'frame_depth', 'frame_length', 'rebuilding'] #: contextvars.ContextVar[bool]: Whether a layer that raises is discarded by its #: caller, as :func:`~pcapkit.corekit.decorators.beholder` replaces it and @@ -31,6 +35,18 @@ #: :data:`~pcapkit.utilities.config.FRAME_LAYER_LIMIT`. frame_depth = contextvars.ContextVar('pcapkit.utilities.contextvars.frame_depth', default=0) +#: contextvars.ContextVar[tuple[int, int] | None]: The captured and the original +#: length of the frame being dissected, in octets, as its PCAP record or PCAP-NG +#: packet block states them; :data:`None` outside a frame, and inside the payload +#: of an :class:`~pcapkit.protocols.internet.internet.Internet` layer, which +#: bounds it. A frame the capture cut short holds fewer octets than were on the +#: wire, which a layer stating no length of its own, as +#: :class:`~pcapkit.protocols.link.gre.GRE`, cannot tell from the octets it is +#: handed (:issue:`2653`). +frame_length = contextvars.ContextVar( + 'pcapkit.utilities.contextvars.frame_length', default=None, +) # type: contextvars.ContextVar[Optional[tuple[int, int]]] + #: contextvars.ContextVar[bool]: Whether a payload is being rebuilt by #: :meth:`Protocol._make_payload #: `, so that the layer diff --git a/tests/protocols/link/test_gre_snaplen_cut_2653_unit.py b/tests/protocols/link/test_gre_snaplen_cut_2653_unit.py new file mode 100644 index 0000000000..a92cddc285 --- /dev/null +++ b/tests/protocols/link/test_gre_snaplen_cut_2653_unit.py @@ -0,0 +1,345 @@ +# -*- coding: utf-8 -*- +"""A GRE frame the capture cut short reads ``TRUNCATED``, not ``INVALID``. + +GitHub issue #2653: neither the GRE header nor the Linux cooked-capture header in +front of it states a length, so :meth:`GRE.read ` +cannot tell from its own octets a frame the snapshot length cut short from one whose +checksum is wrong. The record's original length tells them apart, as Wireshark's +captured length against its reported one does: +:class:`~pcapkit.protocols.misc.pcap.frame.Frame` and +:class:`~pcapkit.protocols.misc.pcapng.PCAPNG` set +:data:`~pcapkit.utilities.contextvars.frame_length` around the parse of their +payload, and GRE reads it. A complete frame whose checksum is wrong stays +``INVALID`` and warns. + +Each capture is built here and read through :func:`pcapkit.extract`: PCAP, and +PCAP-NG's Enhanced, Simple and obsolete Packet Blocks, each over ``LINUX_SLL`` and +``LINUX_SLL2``. This module is unit-tier and reads no capture file. + +""" +from __future__ import annotations + +import io +import struct +import unittest +import warnings +from typing import TYPE_CHECKING +from unittest import mock + +from tests._support import reimport_once_per_class +from tests.foundation import _roundtrip as wire + +if TYPE_CHECKING: + from typing import Any, Callable + + #: A writer of a capture: link type, the octets captured and the original length. + Writer = Callable[[int, bytes, int], bytes] + +#: Test tier (#1958): hand-picked parse -> rebuild round trips. +__tier__ = 'ALL' + +#: ``LINKTYPE_LINUX_SLL`` and ``LINKTYPE_LINUX_SLL2``. +LINUX_SLL, LINUX_SLL2 = 113, 276 +#: ``ARPHRD_IPGRE``, which both cooked-capture headers dispatch to GRE. +ARPHRD_IPGRE = 778 +#: The octets the capture cuts off the end of each frame. +CUT = 4 + + +def gre(payload: bytes, *, checksum: 'int | None' = None, protocol: int = 0x0800) -> bytes: + """A GRE header with ``C`` set, carrying ``payload`` of EtherType ``protocol``, IPv4 by default. + + ``checksum`` is the field's value, the right one by default. + + """ + head = struct.pack('>HH', 0x8000, protocol) + if checksum is None: + checksum = wire.checksum(head + bytes(4) + payload) + return head + struct.pack('>HH', checksum, 0) + payload + + +def sll(linktype: int, frame: bytes) -> bytes: + """``frame`` behind a cooked-capture header of ``linktype`` naming ``ARPHRD_IPGRE``.""" + if linktype == LINUX_SLL: + return struct.pack('>HHH', 0, ARPHRD_IPGRE, 0) + bytes(8) + struct.pack('>H', 0x0800) + frame + return struct.pack('>HHIHBB', 0x0800, 0, 1, ARPHRD_IPGRE, 0, 0) + bytes(8) + frame + + +def pcap(linktype: int, packet: bytes, original: int) -> bytes: + """A PCAP savefile holding ``packet``, whose record says ``original`` octets were on the wire.""" + return (struct.pack(' bytes: + """A little-endian PCAP-NG block of ``kind`` around ``body``, padded to 32 bits.""" + body += bytes(-len(body) % 4) + size = 12 + len(body) + return struct.pack(' bytes: + """An Enhanced Packet Block holding ``packet`` of ``original`` octets on the wire.""" + return block(6, struct.pack(' bytes: + """A Simple Packet Block holding ``packet`` of ``original`` octets on the wire.""" + return block(3, struct.pack(' bytes: + """An obsolete Packet Block holding ``packet`` of ``original`` octets on the wire.""" + return block(2, struct.pack(' 'Writer': + """A writer of PCAP-NG files of one interface and one packet block that ``maker`` builds.""" + def write(linktype: int, packet: bytes, original: int) -> bytes: + shb = block(0x0A0D0D0A, struct.pack(' 'tuple[bytes, int]': + """``message`` in an IPv4 or IPv6 datagram in an Ethernet frame, and where the datagram ends. + + The frame is padded to the 60-octet minimum, or given a 4-octet trailer when + it is longer, so that octets follow the datagram for a capture to cut. + + """ + if version == 4: + frame = wire.ethernet(wire.ipv4(message, proto=47), 0x0800) + else: + frame = wire.ethernet(wire.ipv6(message, nxt=47), 0x86DD) + return frame + bytes(max(60 - len(frame), 4)), len(frame) + + +class GRESnaplenCutTests(unittest.TestCase): + """A frame's original length tells GRE a short capture from a wrong checksum.""" + + def setUp(self) -> None: + reimport_once_per_class(self) + import pcapkit + from pcapkit.corekit.checksum.internet import ChecksumStatus + from pcapkit.utilities import config + from pcapkit.utilities.warnings import ChecksumWarning + + self.pcapkit = pcapkit + self.Status = ChecksumStatus + self.config = config + self.ChecksumWarning = ChecksumWarning + + def extract(self, capture: bytes, *, verify: bool = True) -> 'tuple[Any, list[str]]': + """The first frame of ``capture``, and the checksum warnings reading it gave.""" + with mock.patch.object(self.config, 'CHECKSUM_VERIFY', verify), \ + warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always') + frame = self.pcapkit.extract(fin=io.BytesIO(capture), nofile=True).frame[0] + return frame, [str(item.message) for item in caught + if issubclass(item.category, self.ChecksumWarning)] + + def assertCutReadsTruncated(self, write: 'Writer') -> None: # pylint: disable=invalid-name + """Every case over both cooked-capture headers, as ``write`` puts them in a capture.""" + for linktype, link in ((LINUX_SLL, 'LINUX_SLL1'), (LINUX_SLL2, 'LINUX_SLL2')): + good, bad = sll(linktype, GOOD), sll(linktype, BAD) + cases = { + 'cut, right checksum': (good[:-CUT], len(good), self.Status.TRUNCATED, []), + 'cut, wrong checksum': (bad[:-CUT], len(bad), self.Status.TRUNCATED, []), + 'complete, right checksum': (good, len(good), self.Status.VALID, []), + 'complete, wrong checksum': (bad, len(bad), self.Status.INVALID, [WRONG]), + } + for name, (packet, original, status, messages) in cases.items(): + with self.subTest(linktype=linktype, case=name): + frame, caught = self.extract(write(linktype, packet, original)) + # The payload parses whatever the status; UDP port 9 leaves its data as Raw. + self.assertEqual(frame.protochain.chain, f'{link}:GRE:IPv4:UDP:Raw') + self.assertEqual((frame['GRE'].info.checksum_status, caught), (status, messages)) + # Off, nothing is verified, cut or not. + frame, caught = self.extract(write(linktype, packet, original), verify=False) + self.assertEqual((frame['GRE'].info.get('checksum_status'), caught), (None, [])) + + def test_pcap(self) -> None: + self.assertCutReadsTruncated(pcap) + + def test_pcapng_enhanced_packet_block(self) -> None: + self.assertCutReadsTruncated(pcapng(epb)) + + def test_pcapng_simple_packet_block(self) -> None: + self.assertCutReadsTruncated(pcapng(spb)) + + def test_pcapng_obsolete_packet_block(self) -> None: + self.assertCutReadsTruncated(pcapng(packet_block)) + + def test_an_original_length_below_the_captured_one_cuts_nothing(self) -> None: + # An ``orig_len`` smaller than ``incl_len`` is malformed, not a cut. + packet = sll(LINUX_SLL, GOOD) + for name, write in (('pcap', pcap), ('epb', pcapng(epb)), ('pb', pcapng(packet_block))): + with self.subTest(capture=name): + frame, caught = self.extract(write(LINUX_SLL, packet, len(packet) - CUT)) + self.assertEqual((frame['GRE'].info.checksum_status, caught), (self.Status.VALID, [])) + # A cut one reads as cut. + frame, caught = self.extract(pcap(LINUX_SLL, packet[:-CUT], len(packet))) + self.assertEqual((frame['GRE'].info.checksum_status, caught), (self.Status.TRUNCATED, [])) + + def test_the_lengths_reach_the_layers_below_the_frame_only(self) -> None: + from pcapkit.protocols.link.gre import GRE + from pcapkit.utilities.contextvars import frame_length + + seen = [] # type: list[object] + read = GRE.read + + def spy(layer: 'Any', *args: 'Any', **kwargs: 'Any') -> 'Any': + seen.append(frame_length.get()) + return read(layer, *args, **kwargs) + + packet = sll(LINUX_SLL2, BAD) + with mock.patch.object(GRE, 'read', spy): + self.extract(pcap(LINUX_SLL2, packet[:-CUT], len(packet))) + self.extract(pcapng(epb)(LINUX_SLL2, packet[:-CUT], len(packet))) + self.extract(pcapng(spb)(LINUX_SLL2, packet[:-CUT], len(packet))) + self.assertEqual(seen, [(len(packet) - CUT, len(packet))] * 3) + self.assertIsNone(frame_length.get()) + + # Outside a frame, GRE verifies the octets it holds. + with mock.patch.object(self.config, 'CHECKSUM_VERIFY', True), \ + warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always') + layer = GRE(BAD[:-CUT]) + self.assertIs(layer.info.checksum_status, self.Status.INVALID) + self.assertEqual([item.category for item in caught], [self.ChecksumWarning]) + + def test_parse_rebuild_is_exact(self) -> None: + from pcapkit.protocols.misc.pcap.frame import Frame + + def rebuild(frame: 'Any') -> 'tuple[Any, list[type]]': + """``frame`` rebuilt from its data model, and the checksum warnings that gave.""" + if isinstance(frame, Frame): + kwargs = {'num': frame.info.number, 'header': frame.header} # type: dict[str, Any] + else: + kwargs = {'num': frame.info.number, 'sct': frame.info.section_number, + 'ctx': frame._ctx} # pylint: disable=protected-access + with mock.patch.object(self.config, 'CHECKSUM_VERIFY', True), \ + warnings.catch_warnings(record=True) as caught: + warnings.simplefilter('always') + rebuilt = type(frame).from_data(frame.info, **kwargs) + return rebuilt, [item.category for item in caught + if issubclass(item.category, self.ChecksumWarning)] + + for linktype in (LINUX_SLL, LINUX_SLL2): + packet = sll(linktype, BAD) + capture = pcap(linktype, packet[:-CUT], len(packet)) + cases = {'pcap': (capture, capture[24:]), + 'pcapng': (pcapng(epb)(linktype, packet[:-CUT], len(packet)), + epb(packet[:-CUT], len(packet)))} + for name, (capture, record) in cases.items(): + with self.subTest(linktype=linktype, capture=name): + self.assertTrue(capture.endswith(record)) + frame, _ = self.extract(capture) + rebuilt, caught = rebuild(frame) + self.assertEqual(rebuilt.data, record) + # The rebuilt frame hands its lengths down as the parsed one did. + self.assertEqual((rebuilt['GRE'].info.checksum_status, caught), + (self.Status.TRUNCATED, [])) + + +class GREBehindIPTests(GRESnaplenCutTests): + """Behind IP, GRE verifies over the length the IP layer declares, not the frame's. + + :class:`~pcapkit.protocols.internet.internet.Internet` clears + :data:`~pcapkit.utilities.contextvars.frame_length` around its payload's parse, + so octets the capture cut off past the datagram are none of GRE's. No shipped + registry binds IP protocol 47 to GRE, so each test registers it with + :func:`~pcapkit.foundation.registry.protocols.internet.register_transtype` and + puts the registry back afterwards. + + """ + + # NOTE: The inherited tests run here too, with GRE also bound under IP; they + # carry no IP layer, so they read as they do on the class above. + + def setUp(self) -> None: + super().setUp() + from pcapkit.const.reg.transtype import TransType + from pcapkit.foundation.registry import register_transtype + from pcapkit.protocols.internet.internet import Internet + from pcapkit.protocols.link.gre import GRE + + previous = Internet.__proto__.get(TransType.GRE) + with warnings.catch_warnings(): + warnings.simplefilter('ignore') + register_transtype(TransType.GRE, GRE) + + def restore() -> None: + if previous is None: + Internet.__proto__.pop(TransType.GRE, None) + else: + Internet.__proto__[TransType.GRE] = previous + self.addCleanup(restore) + + def assertBehindIPReads(self, write: 'Writer') -> None: # pylint: disable=invalid-name + """Every case behind IPv4 and IPv6, as ``write`` puts them in an Ethernet capture.""" + for version, network in ((4, 'IPv4'), (6, 'IPv6')): + good, end = behind_ip(version, SHORT_GOOD) + bad, _ = behind_ip(version, SHORT_BAD) + cases = { + 'padding cut in part': (good[:end + 2], len(good), self.Status.VALID, []), + 'padding cut whole': (good[:end], len(good), self.Status.VALID, []), + 'cut inside the datagram': (good[:end - 2], len(good), self.Status.TRUNCATED, []), + 'cut inside, wrong checksum': (bad[:end - 2], len(bad), self.Status.TRUNCATED, []), + 'complete': (good, len(good), self.Status.VALID, []), + 'complete, wrong checksum': (bad, len(bad), self.Status.INVALID, [SHORT_WRONG]), + } + for name, (packet, original, status, messages) in cases.items(): + with self.subTest(network=network, case=name): + frame, caught = self.extract(write(1, packet, original)) + # GRE's payload is Raw, named after its EtherType. + self.assertEqual(frame.protochain.chain.split(':')[:3], ['Ethernet', network, 'GRE']) + self.assertEqual((frame['GRE'].info.checksum_status, caught), (status, messages)) + + def test_pcap_behind_ip(self) -> None: + self.assertBehindIPReads(pcap) + + def test_pcapng_behind_ip(self) -> None: + self.assertBehindIPReads(pcapng(epb)) + + def test_the_lengths_are_cleared_behind_ip(self) -> None: + from pcapkit.protocols.link.gre import GRE + from pcapkit.utilities.contextvars import frame_length + + seen = [] # type: list[object] + read = GRE.read + + def spy(layer: 'Any', *args: 'Any', **kwargs: 'Any') -> 'Any': + seen.append(frame_length.get()) + return read(layer, *args, **kwargs) + + packet, end = behind_ip(4, SHORT_GOOD) + with mock.patch.object(GRE, 'read', spy): + self.extract(pcap(1, packet[:end], len(packet))) + self.extract(pcap(LINUX_SLL, sll(LINUX_SLL, GOOD)[:-CUT], len(sll(LINUX_SLL, GOOD)))) + self.assertEqual(seen, [None, (len(sll(LINUX_SLL, GOOD)) - CUT, len(sll(LINUX_SLL, GOOD)))]) + self.assertIsNone(frame_length.get()) + + +if __name__ == '__main__': + unittest.main()