|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Octets after the last whole SCTP chunk parse and survive a rebuild. |
| 3 | +
|
| 4 | +GitHub issue #1468: SCTP has no packet length field, so octets left after the |
| 5 | +last whole chunk -- fewer than a chunk header, or a chunk whose declared length |
| 6 | +runs past the data -- cannot be told apart from a chunk cut short. They made |
| 7 | +:class:`~pcapkit.protocols.transport.sctp.SCTP` raise ``ProtocolError: Field |
| 8 | +chunks has an option that runs past the end of the data``, so the packet had no |
| 9 | +round trip at all. |
| 10 | +
|
| 11 | +Per the #1458 ruling they are now recorded rather than rejected: the chunk list |
| 12 | +stops at the last whole chunk and the rest is kept as ``trailer``, as IPv4, |
| 13 | +IPv6 (#1209), L2TPv2 and OSPF (#1455) do. :meth:`make` writes a ``trailer`` |
| 14 | +after the chunks. |
| 15 | +
|
| 16 | +Every case builds its own octets in memory and reads no capture. Classes are |
| 17 | +imported inside each test, after :func:`~tests._support.reimport_once_per_class`, |
| 18 | +c.f. ``tests/protocols/link/test_ethernet_mac_roundtrip_unit.py``. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +import struct |
| 23 | +import unittest |
| 24 | +import warnings |
| 25 | + |
| 26 | +from tests._support import reimport_once_per_class |
| 27 | + |
| 28 | +#: SCTP common header. |
| 29 | +COMMON = struct.pack('!HHII', 1, 2, 0, 0) |
| 30 | +#: COOKIE ACK chunk. |
| 31 | +COOKIE_ACK = struct.pack('!BBH', 11, 0, 4) |
| 32 | +#: DATA chunk carrying ``b'hi'`` under PPID 0, padded to eight octets. |
| 33 | +DATA = struct.pack('!BBHIHHI', 0, 3, 18, 1, 0, 0, 0) + b'hi' + bytes(2) |
| 34 | +#: INIT chunk declaring 32 octets, 28 present. |
| 35 | +INIT_CUT = bytes.fromhex('01000020 00000001 00010000 00010001 00000001 000c0010 00050000') |
| 36 | + |
| 37 | +#: Packets with octets after the last whole chunk: (octets, chunk count, trailer). |
| 38 | +STRAY = { |
| 39 | + '1 octet': (COMMON + COOKIE_ACK + b'x', 1, b'x'), |
| 40 | + '3 octets': (COMMON + COOKIE_ACK + b'xyz', 1, b'xyz'), |
| 41 | + '4 octets': (COMMON + COOKIE_ACK + b'wxyz', 1, b'wxyz'), |
| 42 | + '4 zero octets': (COMMON + COOKIE_ACK + bytes(4), 1, bytes(4)), |
| 43 | + '5 octets': (COMMON + COOKIE_ACK + b'vwxyz', 1, b'vwxyz'), |
| 44 | + 'after DATA': (COMMON + DATA + b'x', 1, b'x'), |
| 45 | + 'chunk length past the data': (COMMON + COOKIE_ACK + struct.pack('!BBH', 11, 0, 40) + b'abcd', 1, |
| 46 | + struct.pack('!BBH', 11, 0, 40) + b'abcd'), |
| 47 | + 'first chunk past the data': (COMMON + DATA[:14], 0, DATA[:14]), |
| 48 | + 'chunk length below its header': (COMMON + COOKIE_ACK + struct.pack('!BBH', 11, 0, 2), 1, |
| 49 | + struct.pack('!BBH', 11, 0, 2)), |
| 50 | + 'under a chunk header': (COMMON + b'ab', 0, b'ab'), |
| 51 | + # formerly rejected by tests/protocols/test_declared_length_overrun_unit.py |
| 52 | + 'INIT past the data': (COMMON + INIT_CUT, 0, INIT_CUT), |
| 53 | +} |
| 54 | + |
| 55 | +#: Packets that end on a chunk boundary. |
| 56 | +NO_STRAY = { |
| 57 | + 'header only': COMMON, |
| 58 | + 'COOKIE ACK': COMMON + COOKIE_ACK, |
| 59 | + 'DATA then COOKIE ACK': COMMON + DATA + COOKIE_ACK, |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +class TestSCTPStrayOctets(unittest.TestCase): |
| 64 | + """Pin the octets after the last whole SCTP chunk through ``from_data``.""" |
| 65 | + |
| 66 | + def setUp(self) -> None: |
| 67 | + reimport_once_per_class(self) |
| 68 | + |
| 69 | + def test_packet_parses_and_rebuilds_byte_for_byte(self) -> None: |
| 70 | + from pcapkit.protocols.transport.sctp import SCTP |
| 71 | + |
| 72 | + for name, (raw, count, trailer) in STRAY.items(): |
| 73 | + with self.subTest(packet=name): |
| 74 | + parsed = SCTP(raw, len(raw)) |
| 75 | + self.assertEqual(len(parsed.info.chunks), count) |
| 76 | + self.assertEqual(parsed.info.trailer, trailer) |
| 77 | + self.assertEqual(SCTP.from_data(parsed.info).data, raw) |
| 78 | + self.assertEqual(SCTP.from_data(parsed.info.to_dict()).data, raw) |
| 79 | + |
| 80 | + def test_schema_keeps_the_trailer(self) -> None: |
| 81 | + from pcapkit.protocols.schema.transport.sctp import SCTP as Schema_SCTP |
| 82 | + |
| 83 | + for name, (raw, count, trailer) in STRAY.items(): |
| 84 | + with self.subTest(packet=name): |
| 85 | + schema = Schema_SCTP.unpack(raw) |
| 86 | + self.assertEqual(len(schema.chunks), count) |
| 87 | + self.assertEqual(schema.trailer, trailer) |
| 88 | + self.assertEqual(schema.pack(), raw) |
| 89 | + |
| 90 | + def test_no_trailer_key_on_a_chunk_boundary(self) -> None: |
| 91 | + from pcapkit.protocols.transport.sctp import SCTP |
| 92 | + |
| 93 | + for name, raw in NO_STRAY.items(): |
| 94 | + with self.subTest(packet=name): |
| 95 | + parsed = SCTP(raw, len(raw)) |
| 96 | + self.assertNotIn('trailer', parsed.info) |
| 97 | + self.assertEqual(SCTP.from_data(parsed.info).data, raw) |
| 98 | + |
| 99 | + def test_data_chunk_still_dispatches(self) -> None: |
| 100 | + from pcapkit.protocols.transport.sctp import SCTP |
| 101 | + |
| 102 | + raw = COMMON + DATA + b'x' |
| 103 | + parsed = SCTP(raw, len(raw)) |
| 104 | + self.assertEqual(parsed.info.trailer, b'x') |
| 105 | + self.assertEqual(bytes(parsed.payload.data), b'hi') |
| 106 | + |
| 107 | + def test_parent_rebuilds_byte_for_byte(self) -> None: |
| 108 | + from pcapkit.protocols.internet.ipv4 import IPv4 |
| 109 | + from pcapkit.protocols.transport.sctp import SCTP |
| 110 | + |
| 111 | + for name, (raw, _, trailer) in STRAY.items(): |
| 112 | + with self.subTest(packet=name): |
| 113 | + frame = (struct.pack('!BBHHHBBH4s4s', 0x45, 0, 20 + len(raw), 1, 0, 64, 132, 0, |
| 114 | + bytes(4), bytes(4)) + raw) |
| 115 | + with warnings.catch_warnings(): |
| 116 | + warnings.simplefilter('ignore') |
| 117 | + parsed = IPv4(frame, len(frame)) |
| 118 | + self.assertIsInstance(parsed.payload, SCTP) |
| 119 | + self.assertEqual(parsed.payload.info.trailer, trailer) |
| 120 | + self.assertEqual(IPv4.from_data(parsed.info).data, frame) |
| 121 | + |
| 122 | + def test_raw_chunk_that_is_not_whole_is_rejected(self) -> None: |
| 123 | + from pcapkit.protocols.transport.sctp import SCTP |
| 124 | + from pcapkit.utilities.exceptions import ProtocolError |
| 125 | + |
| 126 | + with warnings.catch_warnings(): |
| 127 | + warnings.simplefilter('ignore') |
| 128 | + with self.assertRaisesRegex(ProtocolError, r"raw chunk b'\\x01' runs past the end of the data"): |
| 129 | + SCTP(chunks=[b'\x01']) |
| 130 | + |
| 131 | + def test_make_writes_the_trailer_under_the_checksum(self) -> None: |
| 132 | + from pcapkit.const.sctp.chunk import Chunk as Enum_Chunk |
| 133 | + from pcapkit.protocols.transport.sctp import SCTP |
| 134 | + |
| 135 | + with warnings.catch_warnings(): |
| 136 | + warnings.simplefilter('ignore') |
| 137 | + raw = SCTP(srcport=1, dstport=2, chunks=[(Enum_Chunk.Cookie_Acknowledgement, {})], |
| 138 | + trailer=b'xyz').data |
| 139 | + self.assertEqual(raw[12:], COOKIE_ACK + b'xyz') |
| 140 | + parsed = SCTP(raw, len(raw)) |
| 141 | + self.assertEqual(parsed.info.trailer, b'xyz') |
| 142 | + self.assertTrue(parsed.checksum_valid) |
| 143 | + |
| 144 | + |
| 145 | +if __name__ == '__main__': |
| 146 | + unittest.main() |
0 commit comments