|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""HIP writes Header Length 4 when it carries no parameters. |
| 3 | +
|
| 4 | +GitHub issue #1189: :meth:`HIP.make <pcapkit.protocols.internet.hip.HIP.make>` |
| 5 | +wrote Header Length ``0`` for ``parameters=None``. RFC 7401 §5.1 counts the |
| 6 | +field in 8-octet units excluding the first 8, so the two 16-octet HITs alone |
| 7 | +make 4 the minimum. The header's own re-parse then computed a parameter length |
| 8 | +of ``-32`` and raised ``ProtocolError``. :meth:`HIP.read |
| 9 | +<pcapkit.protocols.internet.hip.HIP.read>` now rejects a wire Header Length |
| 10 | +below 4 up front. |
| 11 | +
|
| 12 | +Every case builds its own octets in memory and reads no capture. |
| 13 | +
|
| 14 | +:class:`HIP` is imported inside each test, after |
| 15 | +:func:`~tests._support.reimport_once_per_class`, not at module load, so it |
| 16 | +always belongs to the live :mod:`pcapkit` import. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import unittest |
| 21 | + |
| 22 | +from tests._support import reimport_once_per_class |
| 23 | + |
| 24 | +#: Keyword arguments for a parameterless HIP header with no payload. |
| 25 | +KWARGS = dict(next=59, packet=1, checksum=b'\x00\x00', controls_anonymous=False, |
| 26 | + shit=0, rhit=0, payload=b'') |
| 27 | + |
| 28 | + |
| 29 | +class TestHIPParameterlessLength(unittest.TestCase): |
| 30 | + """Pin Header Length for a HIP header without parameters.""" |
| 31 | + |
| 32 | + def setUp(self) -> None: |
| 33 | + reimport_once_per_class(self) |
| 34 | + |
| 35 | + def test_make_without_parameters_writes_length_4(self) -> None: |
| 36 | + from pcapkit.protocols.internet.hip import HIP |
| 37 | + |
| 38 | + for version in (1, 2): |
| 39 | + with self.subTest(version=version): |
| 40 | + proto = HIP(parameters=None, version=version, **KWARGS) |
| 41 | + self.assertEqual(len(proto.data), 40) |
| 42 | + self.assertEqual(proto.data[1], 4) |
| 43 | + self.assertEqual(proto.info.length, 40) |
| 44 | + self.assertNotIn('parameters', proto.info) |
| 45 | + |
| 46 | + def test_parameterless_header_parses_back(self) -> None: |
| 47 | + from pcapkit.protocols.internet.hip import HIP |
| 48 | + |
| 49 | + for version in (1, 2): |
| 50 | + with self.subTest(version=version): |
| 51 | + data = HIP(parameters=None, version=version, **KWARGS).data |
| 52 | + parsed = HIP(data) |
| 53 | + self.assertEqual(parsed.info.length, 40) |
| 54 | + self.assertEqual(parsed.info.version, version) |
| 55 | + self.assertEqual(parsed.data, data) |
| 56 | + |
| 57 | + def test_read_rejects_header_length_below_4(self) -> None: |
| 58 | + from pcapkit.protocols.internet.hip import HIP |
| 59 | + from pcapkit.utilities.exceptions import ProtocolError |
| 60 | + |
| 61 | + for value in (0, 3): |
| 62 | + with self.subTest(len=value): |
| 63 | + # Next Header, Header Length, Packet Type 1, Version 2, then |
| 64 | + # checksum, controls and the two HITs, all zero. |
| 65 | + data = bytes([59, value, 0x01, 0x21]) + bytes(36) |
| 66 | + with self.assertRaisesRegex(ProtocolError, 'invalid header length'): |
| 67 | + HIP(data) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == '__main__': |
| 71 | + unittest.main() |
0 commit comments