|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""ARP, InARP, RARP and DRARP rebuild every header they parse byte for byte. |
| 3 | +
|
| 4 | +GitHub issues: |
| 5 | +
|
| 6 | +* #1204: :class:`~pcapkit.protocols.link.arp.InARP` and |
| 7 | + :class:`~pcapkit.protocols.application.rarp.DRARP` did not pass |
| 8 | + ``schema=``/``data=``, so they fell back to the ``Raw`` schema and could not |
| 9 | + parse any input. |
| 10 | +* #1205: an Ethernet hardware address whose ``hlen`` is not 6 was read as |
| 11 | + colon-joined hex, but :meth:`ARP._make_addr_resolve |
| 12 | + <pcapkit.protocols.link.arp.ARP._make_addr_resolve>` accepted only a |
| 13 | + 6-octet MAC. |
| 14 | +* #1206: the protocol address was decoded as IPv4 or IPv6 by its length alone, |
| 15 | + ignoring ``ptype``/``plen``, so a mismatched pair either decoded as the other |
| 16 | + version or raised a bare :exc:`ValueError`. |
| 17 | +
|
| 18 | +Every case builds its own octets in memory and reads no capture. |
| 19 | +
|
| 20 | +The classes are imported inside each test, after |
| 21 | +:func:`~tests._support.reimport_once_per_class`, not at module load (c.f. |
| 22 | +``tests/protocols/link/test_ethernet_mac_roundtrip_unit.py``). |
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +import importlib |
| 27 | +import unittest |
| 28 | + |
| 29 | +from tests._support import reimport_once_per_class |
| 30 | + |
| 31 | +#: ``(module, class)`` for every ARP-family class. |
| 32 | +CLASSES = ( |
| 33 | + ('pcapkit.protocols.link.arp', 'ARP'), |
| 34 | + ('pcapkit.protocols.link.arp', 'InARP'), |
| 35 | + ('pcapkit.protocols.application.rarp', 'RARP'), |
| 36 | + ('pcapkit.protocols.application.rarp', 'DRARP'), |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def _header(ptype: str, hlen: int, plen: int, oper: str) -> bytes: |
| 41 | + """Ethernet-htype header with distinct, non-zero addresses.""" |
| 42 | + return bytes.fromhex('0001' + ptype + f'{hlen:02x}{plen:02x}' + oper |
| 43 | + + '01' * hlen + 'a1' * plen + '02' * hlen + 'b2' * plen) |
| 44 | + |
| 45 | + |
| 46 | +class TestARPRoundTrip(unittest.TestCase): |
| 47 | + """Parse, then ``from_data``, reproduces the original octets.""" |
| 48 | + |
| 49 | + def setUp(self) -> None: |
| 50 | + reimport_once_per_class(self) |
| 51 | + |
| 52 | + def _cls(self, module: str, name: str) -> type: |
| 53 | + return getattr(importlib.import_module(module), name) |
| 54 | + |
| 55 | + def assertRoundTrips(self, cls: type, data: bytes) -> None: |
| 56 | + parsed = cls(data) |
| 57 | + self.assertEqual(cls.from_data(parsed.info).data, data) |
| 58 | + |
| 59 | + def test_subclasses_use_arp_schema(self) -> None: |
| 60 | + """#1204: InARP and DRARP inherit ARP's schema and data classes.""" |
| 61 | + from pcapkit.protocols.data.link.arp import ARP as Data_ARP |
| 62 | + from pcapkit.protocols.schema.link.arp import ARP as Schema_ARP |
| 63 | + |
| 64 | + for module, name in CLASSES: |
| 65 | + with self.subTest(cls=name): |
| 66 | + cls = self._cls(module, name) |
| 67 | + self.assertIs(cls.__schema__, Schema_ARP) |
| 68 | + self.assertIs(cls.__data__, Data_ARP) |
| 69 | + |
| 70 | + def test_subclasses_round_trip(self) -> None: |
| 71 | + """#1204: the issue's repro parses and rebuilds for InARP and DRARP.""" |
| 72 | + for module, name, oper in (('pcapkit.protocols.link.arp', 'InARP', '0008'), |
| 73 | + ('pcapkit.protocols.application.rarp', 'DRARP', '0005')): |
| 74 | + with self.subTest(cls=name): |
| 75 | + cls = self._cls(module, name) |
| 76 | + data = bytes.fromhex('000108000604' + oper + '001122334455' '0a000001' |
| 77 | + '66778899aabb' '0a000002') |
| 78 | + parsed = cls(data) |
| 79 | + self.assertEqual(str(parsed.info.spa), '10.0.0.1') |
| 80 | + self.assertRoundTrips(cls, data) |
| 81 | + |
| 82 | + def test_ethernet_hlen_other_than_six(self) -> None: |
| 83 | + """#1205: any Ethernet ``hlen`` that parses also rebuilds.""" |
| 84 | + for module, name in CLASSES: |
| 85 | + for hlen in (0, 1, 8, 20): |
| 86 | + with self.subTest(cls=name, hlen=hlen): |
| 87 | + cls = self._cls(module, name) |
| 88 | + self.assertRoundTrips(cls, _header('0800', hlen, 4, '0001')) |
| 89 | + |
| 90 | + def test_ethernet_issue_repro_round_trips(self) -> None: |
| 91 | + """#1205: the issue's parsed hlen-8 and hlen-0 headers rebuild.""" |
| 92 | + from pcapkit.protocols.link.arp import ARP |
| 93 | + |
| 94 | + for hlen, data in ( |
| 95 | + (8, bytes.fromhex('0001080008040001' + '01' * 8 + '0a000001' + '02' * 8 + '0a000002')), |
| 96 | + (0, bytes.fromhex('0001080000040001' '0a000001' '0a000002')), |
| 97 | + ): |
| 98 | + with self.subTest(hlen=hlen): |
| 99 | + parsed = ARP(data) |
| 100 | + self.assertEqual(parsed.info.hlen, hlen) |
| 101 | + self.assertRoundTrips(ARP, data) |
| 102 | + |
| 103 | + def test_ethernet_make_without_hlen_requires_six_octets(self) -> None: |
| 104 | + """#1205: with ``htype`` Ethernet and no ``hlen``, a MAC is 6 octets.""" |
| 105 | + from pcapkit.utilities.exceptions import ProtocolError |
| 106 | + |
| 107 | + for module, name in CLASSES: |
| 108 | + cls = self._cls(module, name) |
| 109 | + for addr in ('00:11:22:33:44', '', '00:11:22:33:44:55:66'): |
| 110 | + with self.subTest(cls=name, addr=addr): |
| 111 | + with self.assertRaises(ProtocolError): |
| 112 | + cls(htype=1, sha=addr, tha=addr, spa='10.0.0.1', tpa='10.0.0.2') |
| 113 | + with self.subTest(cls=name, addr='six octets'): |
| 114 | + proto = cls(htype=1, sha='00:11:22:33:44:55', tha='66:77:88:99:aa:bb', |
| 115 | + spa='10.0.0.1', tpa='10.0.0.2') |
| 116 | + self.assertEqual(proto.info.hlen, 6) |
| 117 | + |
| 118 | + def test_make_accepts_any_octets_with_explicit_hlen_or_other_htype(self) -> None: |
| 119 | + """#1205: an explicit ``hlen``, or a non-Ethernet ``htype``, takes any count.""" |
| 120 | + from pcapkit.const.arp.hardware import Hardware |
| 121 | + |
| 122 | + for module, name in CLASSES: |
| 123 | + cls = self._cls(module, name) |
| 124 | + for hlen, addr in ((5, '00:11:22:33:44'), (0, ''), (8, '01:01:01:01:01:01:01:01')): |
| 125 | + with self.subTest(cls=name, hlen=hlen): |
| 126 | + proto = cls(htype=1, hlen=hlen, sha=addr, tha=addr, |
| 127 | + spa='10.0.0.1', tpa='10.0.0.2') |
| 128 | + self.assertEqual(proto.info.hlen, hlen) |
| 129 | + self.assertEqual(proto.info.sha, addr) |
| 130 | + with self.subTest(cls=name, htype='Fibre_Channel'): |
| 131 | + proto = cls(htype=Hardware.Fibre_Channel, sha='aabbcc', tha='ddeeff', |
| 132 | + spa='10.0.0.1', tpa='10.0.0.2') |
| 133 | + self.assertEqual(proto.info.hlen, 3) |
| 134 | + |
| 135 | + def test_ethernet_mac_still_validated(self) -> None: |
| 136 | + """#1205: a malformed MAC string is still rejected.""" |
| 137 | + from pcapkit.const.arp.hardware import Hardware |
| 138 | + from pcapkit.protocols.link.arp import ARP |
| 139 | + from pcapkit.utilities.exceptions import ProtocolError |
| 140 | + |
| 141 | + arp = object.__new__(ARP) |
| 142 | + self.assertEqual(arp._make_addr_resolve('01:02:03:04:05:06:07:08', Hardware.Ethernet), |
| 143 | + bytes.fromhex('0102030405060708')) |
| 144 | + self.assertEqual(arp._make_addr_resolve('', Hardware.Ethernet), b'') |
| 145 | + for addr in ('not-a-mac', '0102', '01:2', '01:02:', ':01'): |
| 146 | + with self.subTest(addr=addr): |
| 147 | + with self.assertRaises(ProtocolError): |
| 148 | + arp._make_addr_resolve(addr, Hardware.Ethernet) |
| 149 | + |
| 150 | + def test_protocol_address_follows_ptype_and_plen(self) -> None: |
| 151 | + """#1206: IP decoding requires ``ptype`` and ``plen`` to agree.""" |
| 152 | + import ipaddress |
| 153 | + |
| 154 | + cases = ( |
| 155 | + ('0800', 4, ipaddress.IPv4Address), |
| 156 | + ('86dd', 16, ipaddress.IPv6Address), |
| 157 | + ('0800', 16, str), |
| 158 | + ('0800', 5, str), |
| 159 | + ('0800', 0, str), |
| 160 | + ('86dd', 4, str), |
| 161 | + ) |
| 162 | + for module, name in CLASSES: |
| 163 | + for ptype, plen, kind in cases: |
| 164 | + with self.subTest(cls=name, ptype=ptype, plen=plen): |
| 165 | + cls = self._cls(module, name) |
| 166 | + data = _header(ptype, 6, plen, '0001') |
| 167 | + parsed = cls(data) |
| 168 | + self.assertIsInstance(parsed.info.spa, kind) |
| 169 | + self.assertIsInstance(parsed.info.tpa, kind) |
| 170 | + self.assertRoundTrips(cls, data) |
| 171 | + |
| 172 | + |
| 173 | +if __name__ == '__main__': |
| 174 | + unittest.main() |
0 commit comments