|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Two octets of option padding are one ``PadN``, not two ``Pad1``. |
| 3 | +
|
| 4 | +GitHub issue #1793: HOPOPT, IPv6-Opts and MH each filled a two-octet shortfall |
| 5 | +with two ``Pad1`` options. :rfc:`8200#section-4.2`, and :rfc:`6275#section-6.2.2` |
| 6 | +for mobility options, say a ``PadN`` should be used rather than multiple |
| 7 | +``Pad1``, so two octets are now one ``PadN`` of length ``0``: the octets |
| 8 | +``01 00``. One octet is still a ``Pad1``, and parsed padding is still rebuilt as |
| 9 | +it was read -- only freshly built padding changed. |
| 10 | +
|
| 11 | +Every case builds its own octets in memory and reads no capture. Classes are |
| 12 | +imported inside each test, after :func:`~tests._support.reimport_once_per_class`. |
| 13 | +
|
| 14 | +""" |
| 15 | + |
| 16 | +import importlib |
| 17 | +import io |
| 18 | +import unittest |
| 19 | +import warnings |
| 20 | + |
| 21 | +from tests._support import reimport_once_per_class |
| 22 | + |
| 23 | +#: Protocol module, class name and option enum module of each padding builder. |
| 24 | +PROTOCOLS = ( |
| 25 | + ('pcapkit.protocols.internet.hopopt', 'HOPOPT', 'pcapkit.const.ipv6.option'), |
| 26 | + ('pcapkit.protocols.internet.ipv6_opts', 'IPv6_Opts', 'pcapkit.const.ipv6.option'), |
| 27 | + ('pcapkit.protocols.internet.mh', 'MH', 'pcapkit.const.mh.option'), |
| 28 | +) |
| 29 | + |
| 30 | +#: Parsed headers whose padding is two ``Pad1`` options, which have to rebuild |
| 31 | +#: unchanged: a Router Alert in an options header, and a Binding Refresh Request |
| 32 | +#: carrying a 6-octet ``PadN``. |
| 33 | +TWO_PAD1 = { |
| 34 | + 'HOPOPT': '3b00' '05020000' '00' '00', |
| 35 | + 'IPv6_Opts': '3b00' '05020000' '00' '00', |
| 36 | + 'MH': '3b01000000000000' '010400000000' '00' '00', |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +class TestTwoOctetPadding(unittest.TestCase): |
| 41 | + """Pin the wire form of two octets of HOPOPT, IPv6-Opts and MH padding.""" |
| 42 | + |
| 43 | + def setUp(self) -> None: |
| 44 | + reimport_once_per_class(self) |
| 45 | + |
| 46 | + def _protocols(self) -> 'list[tuple[type, type]]': |
| 47 | + return [(getattr(importlib.import_module(mod), name), |
| 48 | + importlib.import_module(enum).Option) |
| 49 | + for mod, name, enum in PROTOCOLS] |
| 50 | + |
| 51 | + def test_two_octets_are_one_zero_length_padn(self) -> None: |
| 52 | + for cls, option in self._protocols(): |
| 53 | + proto = object.__new__(cls) |
| 54 | + # the offset counts the fixed header too, so 6 and 14 are two short |
| 55 | + for offset in (6, 14): |
| 56 | + with self.subTest(protocol=cls.__name__, offset=offset): |
| 57 | + with warnings.catch_warnings(): |
| 58 | + warnings.simplefilter('error') |
| 59 | + pads, pad_len = proto._make_pad_options(offset) |
| 60 | + self.assertEqual(pad_len, 2) |
| 61 | + self.assertEqual([pad.type for pad in pads], [option.PadN]) |
| 62 | + self.assertEqual(pads[0].pack(), b'\x01\x00') |
| 63 | + |
| 64 | + def test_one_and_three_octets_keep_their_form(self) -> None: |
| 65 | + for cls, option in self._protocols(): |
| 66 | + proto = object.__new__(cls) |
| 67 | + with self.subTest(protocol=cls.__name__, octets=1): |
| 68 | + pads, pad_len = proto._make_pad_options(7) |
| 69 | + self.assertEqual(pad_len, 1) |
| 70 | + self.assertEqual([(pad.type, pad.pack()) for pad in pads], |
| 71 | + [(option.Pad1, b'\x00')]) |
| 72 | + with self.subTest(protocol=cls.__name__, octets=3): |
| 73 | + pads, pad_len = proto._make_pad_options(5) |
| 74 | + self.assertEqual(pad_len, 3) |
| 75 | + self.assertEqual([(pad.type, pad.pack()) for pad in pads], |
| 76 | + [(option.PadN, b'\x01\x01\x00')]) |
| 77 | + |
| 78 | + def test_constructed_header_ends_in_one_zero_length_padn(self) -> None: |
| 79 | + from pcapkit.const.ipv6.option import Option as IPv6_Option |
| 80 | + from pcapkit.const.mh.option import Option as MH_Option |
| 81 | + from pcapkit.const.mh.packet import Packet |
| 82 | + from pcapkit.const.reg.transtype import TransType |
| 83 | + |
| 84 | + protocols = {cls.__name__: cls for cls, _ in self._protocols()} |
| 85 | + built = { |
| 86 | + # 2 + a 4-octet Router Alert = 6, two short of 8 |
| 87 | + 'HOPOPT': (protocols['HOPOPT'](next=TransType.IPv6_NoNxt, |
| 88 | + options=[(IPv6_Option.Router_Alert, {'alert': 0})]), |
| 89 | + '3b00' '05020000' '0100', |
| 90 | + [(IPv6_Option.Router_Alert, 4), (IPv6_Option.PadN, 2)]), |
| 91 | + 'IPv6_Opts': (protocols['IPv6_Opts'](next=TransType.IPv6_NoNxt, |
| 92 | + options=[(IPv6_Option.Router_Alert, {'alert': 0})]), |
| 93 | + '3b00' '05020000' '0100', |
| 94 | + [(IPv6_Option.Router_Alert, 4), (IPv6_Option.PadN, 2)]), |
| 95 | + # 6 + 2 octets of reserved + a 6-octet PadN = 14, two short of 16 |
| 96 | + 'MH': (protocols['MH'](next=TransType.IPv6_NoNxt, type=Packet.Binding_Refresh_Request, |
| 97 | + chksum=b'\x00\x00', data={'options': [(MH_Option.PadN, {'length': 4})]}), |
| 98 | + '3b01000000000000' '010400000000' '0100', |
| 99 | + [(MH_Option.PadN, 6), (MH_Option.PadN, 2)]), |
| 100 | + } |
| 101 | + for name, (proto, hexstr, expected) in built.items(): |
| 102 | + with self.subTest(protocol=name): |
| 103 | + raw = bytes(proto) |
| 104 | + self.assertEqual(raw.hex(), hexstr) |
| 105 | + parsed = type(proto)(io.BytesIO(raw), len(raw), extension=True) |
| 106 | + self.assertEqual([(code, opt.length) |
| 107 | + for code, opt in parsed.info.options.items(multi=True)], |
| 108 | + expected) |
| 109 | + |
| 110 | + def test_parsed_two_pad1_rebuild_byte_for_byte(self) -> None: |
| 111 | + for cls, option in self._protocols(): |
| 112 | + octets = bytes.fromhex(TWO_PAD1[cls.__name__]) |
| 113 | + with self.subTest(protocol=cls.__name__): |
| 114 | + parsed = cls(io.BytesIO(octets), len(octets), extension=True) |
| 115 | + pads = list(parsed.info.options.items(multi=True))[-2:] |
| 116 | + self.assertEqual([code for code, _ in pads], [option.Pad1, option.Pad1]) |
| 117 | + self.assertEqual(bytes(cls.from_data(parsed.info)).hex(), octets.hex()) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + unittest.main() |
0 commit comments