|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""A raw option or record in a PCAP-NG block builds in the right byte order. |
| 3 | +
|
| 4 | +This guards against GitHub issue #2353. An option handed to a block maker as raw |
| 5 | +:obj:`bytes` has its Option Code read back out of those octets, and a name |
| 6 | +resolution record handed over the same way has its Record Type read the same |
| 7 | +way. Both read it in the block's byte order, which only the section header |
| 8 | +block's maker and the :manpage:`systemd(1)` journal block's maker used to set -- |
| 9 | +so every other block, an Interface Description Block and a Name Resolution |
| 10 | +Block among them, raised ``AttributeError: 'PCAPNG' object has no attribute |
| 11 | +'_byte'`` instead of building. |
| 12 | +
|
| 13 | +The byte order resolved has to be the one the block is written in, not merely |
| 14 | +some byte order: read in the wrong one, a code is misread, which silently loses |
| 15 | +the "only one of this option" guard and attributes the option to a type that is |
| 16 | +not there. So the cases below pin the code actually resolved, in a big-endian |
| 17 | +section as well as a little-endian one, and parse each built block back from its |
| 18 | +octets rather than trusting the schema the maker returned. |
| 19 | +
|
| 20 | +Every case builds its own octets in memory and reads no capture. :mod:`pcapkit` |
| 21 | +is imported inside each test, after |
| 22 | +:func:`~tests._support.reimport_once_per_class`. |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +import unittest |
| 27 | +from typing import TYPE_CHECKING |
| 28 | + |
| 29 | +from tests._support import reimport_once_per_class |
| 30 | + |
| 31 | +if TYPE_CHECKING: |
| 32 | + from typing import Any, Literal |
| 33 | + |
| 34 | +#: Test tier (#1958): a hand-picked round trip, over the host byte order and |
| 35 | +#: both section byte orders. |
| 36 | +__tier__ = 'ALL' |
| 37 | + |
| 38 | + |
| 39 | +def _raw(code: 'int', value: 'bytes', order: 'Literal["little", "big"]') -> 'bytes': |
| 40 | + """A whole option or record of ``code`` carrying ``value``, padded to 32 bits.""" |
| 41 | + return (int(code).to_bytes(2, order) + len(value).to_bytes(2, order) |
| 42 | + + value + bytes(-len(value) % 4)) |
| 43 | + |
| 44 | + |
| 45 | +#: An ``if_name`` option value, and the name it decodes to. |
| 46 | +IF_NAME = b'eth0' |
| 47 | +#: A ``nrb_record_ipv4`` record value: the loopback address, then a |
| 48 | +#: zero-terminated name. |
| 49 | +NRB_RECORD = bytes([127, 0, 0, 1]) + b'host\x00' |
| 50 | + |
| 51 | + |
| 52 | +class TestPCAPNGRawOptionByteOrder(unittest.TestCase): |
| 53 | + """Pin that a raw option or record area resolves the block's byte order.""" |
| 54 | + |
| 55 | + def setUp(self) -> None: |
| 56 | + reimport_once_per_class(self) |
| 57 | + |
| 58 | + def _context(self, order: 'Literal["little", "big"]') -> 'Any': |
| 59 | + """A one-section context of ``order``, with one Ethernet interface.""" |
| 60 | + from pcapkit.const.pcapng.block_type import BlockType |
| 61 | + from pcapkit.const.reg.linktype import LinkType |
| 62 | + from pcapkit.foundation.engines.pcapng import Context |
| 63 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 64 | + |
| 65 | + section = PCAPNG(num=0, sct=1, ctx=None, type=BlockType.Section_Header_Block, |
| 66 | + block={'byteorder': order}) |
| 67 | + self.assertEqual(section.info.byteorder, order) |
| 68 | + |
| 69 | + context = Context(section.info) # type: Any |
| 70 | + interface = PCAPNG(num=1, sct=1, ctx=context, type=BlockType.Interface_Description_Block, |
| 71 | + block={'linktype': LinkType.ETHERNET, 'snaplen': 0x40000}) |
| 72 | + context.interfaces.append(interface.info) |
| 73 | + return context |
| 74 | + |
| 75 | + def _reparse(self, octets: 'bytes', context: 'Any') -> 'Any': |
| 76 | + """Parse ``octets`` as one block, as a reader of the file would.""" |
| 77 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 78 | + |
| 79 | + return PCAPNG(octets, len(octets), num=2, sct=1, ctx=context) |
| 80 | + |
| 81 | + def test_a_raw_option_builds_with_no_byte_order_set(self) -> None: |
| 82 | + """#2353: an IDB takes a raw option without an earlier call setting one.""" |
| 83 | + import sys |
| 84 | + |
| 85 | + from pcapkit.const.pcapng.block_type import BlockType |
| 86 | + from pcapkit.const.pcapng.option_type import OptionType |
| 87 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 88 | + |
| 89 | + option = _raw(OptionType.if_name, IF_NAME, sys.byteorder) |
| 90 | + block = PCAPNG(num=2, sct=1, ctx=None, type=BlockType.Interface_Description_Block, |
| 91 | + block={'linktype': 1, 'snaplen': 0xFFFF, 'options': [option]}) |
| 92 | + self.assertEqual(block.info.options_raw, option) |
| 93 | + |
| 94 | + parsed = self._reparse(block.data, None) |
| 95 | + self.assertEqual(parsed.info.options[OptionType.if_name].name, IF_NAME.decode()) |
| 96 | + self.assertEqual(PCAPNG.from_data(parsed.info, num=2, sct=1, ctx=None).data, block.data) |
| 97 | + |
| 98 | + def test_a_raw_record_builds_with_no_byte_order_set(self) -> None: |
| 99 | + """#2353: an NRB takes a raw record without an earlier call setting one.""" |
| 100 | + import ipaddress |
| 101 | + import sys |
| 102 | + |
| 103 | + from pcapkit.const.pcapng.block_type import BlockType |
| 104 | + from pcapkit.const.pcapng.record_type import RecordType |
| 105 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 106 | + |
| 107 | + record = _raw(RecordType.nrb_record_ipv4, NRB_RECORD, sys.byteorder) |
| 108 | + block = PCAPNG(num=2, sct=1, ctx=None, type=BlockType.Name_Resolution_Block, |
| 109 | + block={'records': [record]}) |
| 110 | + self.assertEqual(block.info.records_raw, record) |
| 111 | + |
| 112 | + parsed = self._reparse(block.data, None) |
| 113 | + self.assertEqual(parsed.info.mapping[ipaddress.ip_address('127.0.0.1')], 'host') |
| 114 | + self.assertEqual(PCAPNG.from_data(parsed.info, num=2, sct=1, ctx=None).data, block.data) |
| 115 | + |
| 116 | + def test_a_raw_option_round_trips_in_either_section_byte_order(self) -> None: |
| 117 | + """#2353: the option area is written, and reads back, in the section's order.""" |
| 118 | + from pcapkit.const.pcapng.block_type import BlockType |
| 119 | + from pcapkit.const.pcapng.option_type import OptionType |
| 120 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 121 | + |
| 122 | + for order in ('little', 'big'): # type: Literal['little', 'big'] |
| 123 | + with self.subTest(order=order): |
| 124 | + context = self._context(order) |
| 125 | + option = _raw(OptionType.if_name, IF_NAME, order) |
| 126 | + block = PCAPNG(num=2, sct=1, ctx=context, |
| 127 | + type=BlockType.Interface_Description_Block, |
| 128 | + block={'linktype': 1, 'snaplen': 0xFFFF, 'options': [option]}) |
| 129 | + self.assertEqual(block.byteorder, order) |
| 130 | + # The Block Type is the block's first field, so the section's |
| 131 | + # byte order is visible in the octets emitted. |
| 132 | + self.assertEqual(block.data[:4], |
| 133 | + int(BlockType.Interface_Description_Block).to_bytes(4, order)) |
| 134 | + self.assertIn(option, block.data) |
| 135 | + |
| 136 | + parsed = self._reparse(block.data, context) |
| 137 | + self.assertEqual(parsed.info.options[OptionType.if_name].name, IF_NAME.decode()) |
| 138 | + self.assertEqual( |
| 139 | + PCAPNG.from_data(parsed.info, num=2, sct=1, ctx=context).data, block.data) |
| 140 | + |
| 141 | + def test_a_raw_record_round_trips_in_either_section_byte_order(self) -> None: |
| 142 | + """#2353: the record area is written, and reads back, in the section's order.""" |
| 143 | + import ipaddress |
| 144 | + |
| 145 | + from pcapkit.const.pcapng.block_type import BlockType |
| 146 | + from pcapkit.const.pcapng.record_type import RecordType |
| 147 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 148 | + |
| 149 | + for order in ('little', 'big'): # type: Literal['little', 'big'] |
| 150 | + with self.subTest(order=order): |
| 151 | + context = self._context(order) |
| 152 | + record = _raw(RecordType.nrb_record_ipv4, NRB_RECORD, order) |
| 153 | + block = PCAPNG(num=2, sct=1, ctx=context, |
| 154 | + type=BlockType.Name_Resolution_Block, |
| 155 | + block={'records': [record]}) |
| 156 | + self.assertEqual(block.byteorder, order) |
| 157 | + self.assertIn(record, block.data) |
| 158 | + |
| 159 | + parsed = self._reparse(block.data, context) |
| 160 | + self.assertEqual(parsed.info.mapping[ipaddress.ip_address('127.0.0.1')], 'host') |
| 161 | + self.assertEqual( |
| 162 | + PCAPNG.from_data(parsed.info, num=2, sct=1, ctx=context).data, block.data) |
| 163 | + |
| 164 | + def test_a_raw_option_code_is_read_in_the_section_byte_order(self) -> None: |
| 165 | + """#2353: the code read is the section's, so the "only one" guard still holds. |
| 166 | +
|
| 167 | + A raw ``if_name`` followed by one built from keywords is two of an option |
| 168 | + there may only be one of. The guard counts the raw one by the code read |
| 169 | + out of its octets, so it only fires when that code was read in the byte |
| 170 | + order the section is written in -- the host's would read ``if_name`` as |
| 171 | + some other type on one of the two sections below. |
| 172 | +
|
| 173 | + """ |
| 174 | + from pcapkit.const.pcapng.block_type import BlockType |
| 175 | + from pcapkit.const.pcapng.option_type import OptionType |
| 176 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 177 | + from pcapkit.utilities.exceptions import ProtocolError |
| 178 | + |
| 179 | + for order in ('little', 'big'): # type: Literal['little', 'big'] |
| 180 | + with self.subTest(order=order): |
| 181 | + context = self._context(order) |
| 182 | + with self.assertRaisesRegex(ProtocolError, |
| 183 | + r'\[if_name\] option must be only one, but 2 found'): |
| 184 | + PCAPNG(num=2, sct=1, ctx=context, |
| 185 | + type=BlockType.Interface_Description_Block, |
| 186 | + block={'linktype': 1, 'snaplen': 0xFFFF, |
| 187 | + 'options': [_raw(OptionType.if_name, IF_NAME, order), |
| 188 | + (OptionType.if_name, {'name': 'eth1'})]}) |
| 189 | + |
| 190 | + def test_a_raw_option_code_is_read_in_the_packet_byte_order(self) -> None: |
| 191 | + """#2353: with no section, the byte order the packet data declares is used. |
| 192 | +
|
| 193 | + :meth:`PCAPNG.pack <pcapkit.protocols.misc.pcapng.PCAPNG.pack>` writes |
| 194 | + the block in the order ``__packet__`` declares when there is no section |
| 195 | + to take one from, so that is the order the code has to be read in too. |
| 196 | +
|
| 197 | + """ |
| 198 | + from pcapkit.const.pcapng.block_type import BlockType |
| 199 | + from pcapkit.const.pcapng.option_type import OptionType |
| 200 | + from pcapkit.protocols.misc.pcapng import PCAPNG |
| 201 | + from pcapkit.utilities.exceptions import ProtocolError |
| 202 | + |
| 203 | + for order in ('little', 'big'): # type: Literal['little', 'big'] |
| 204 | + with self.subTest(order=order): |
| 205 | + with self.assertRaisesRegex(ProtocolError, |
| 206 | + r'\[if_name\] option must be only one, but 2 found'): |
| 207 | + PCAPNG(num=2, sct=1, ctx=None, |
| 208 | + type=BlockType.Interface_Description_Block, |
| 209 | + block={'linktype': 1, 'snaplen': 0xFFFF, |
| 210 | + 'options': [_raw(OptionType.if_name, IF_NAME, order), |
| 211 | + (OptionType.if_name, {'name': 'eth1'})]}, |
| 212 | + __packet__={'byteorder': order}) |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == '__main__': |
| 216 | + unittest.main() |
0 commit comments