Skip to content

Commit f4aa577

Browse files
authored
fix(link): honour a stop at LinuxSLL on its ARPHRD registry path (#2532) (#2587)
1 parent 5145a10 commit f4aa577

3 files changed

Lines changed: 86 additions & 20 deletions

File tree

‎pcapkit/protocols/link/linux_sll.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,9 @@ def _import_next_layer(self, proto: 'int', length: 'Optional[int]' = None, *, #
238238
<LinuxSLL.__proto__>` registers for ``arphrd_type`` when it has a key
239239
for it, and otherwise the class :attr:`Link.__proto__
240240
<pcapkit.protocols.link.link.Link.__proto__>` registers for ``proto``
241-
as an EtherType. A :class:`~pcapkit.protocols.misc.raw.Raw` fallback
242-
records ``proto`` either way.
241+
as an EtherType. Where parsing stops at this layer, either path
242+
keeps the payload :class:`~pcapkit.protocols.misc.raw.Raw`, and a
243+
:class:`~pcapkit.protocols.misc.raw.Raw` records ``proto`` either way.
243244
244245
Note:
245246
:attr:`self.__proto__ <LinuxSLL.__proto__>` holds the
@@ -261,13 +262,11 @@ def _import_next_layer(self, proto: 'int', length: 'Optional[int]' = None, *, #
261262
if not min(length, len(file_)):
262263
from pcapkit.protocols.misc.null import \
263264
NoPayload as protocol # isort: skip # pylint: disable=import-outside-toplevel
264-
# NOTE: ahead of ``_sigterm``, so a registered ``arphrd_type`` is parsed
265-
# even where parsing stops at this layer (#2510).
266-
elif arphrd_type in self.__proto__:
267-
protocol = self._lookup_next_layer(self.__proto__, arphrd_type)
268265
elif self._sigterm:
269266
from pcapkit.protocols.misc.raw import \
270267
Raw as protocol # isort: skip # pylint: disable=import-outside-toplevel
268+
elif arphrd_type in self.__proto__:
269+
protocol = self._lookup_next_layer(self.__proto__, arphrd_type)
271270
else:
272271
protocol = self._lookup_next_layer(Link.__proto__, proto)
273272

‎tests/protocols/link/test_linux_sll_arphrd_registry_unit.py‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
Covers :attr:`LinuxSLL.__proto__ <pcapkit.protocols.link.linux_sll.LinuxSLL.__proto__>`
66
as shipped, dispatch through it and through the EtherType path from both versions,
7+
a parse stopping at either version on either path (#2532),
78
the ``__index__`` of the classes it reaches, and the registrars of it and of
89
:class:`~pcapkit.protocols.link.frad.FRAD`'s and
910
:class:`~pcapkit.protocols.link.netlink.Netlink`'s registries.
@@ -138,25 +139,73 @@ def test_an_empty_payload_is_no_payload_on_either_path(self) -> None:
138139
raw = header(0x0800, arphrd)
139140
self.assertIsInstance(version(raw, len(raw)).payload, NoPayload)
140141

141-
def test_a_stop_at_this_layer_still_parses_a_registered_arphrd_type(self) -> None:
142-
"""The order the NOTE in ``LinuxSLL._import_next_layer`` records.
142+
def _stops(self, version): # type: ignore[no-untyped-def]
143+
"""Each spelling of a stop at ``version``: its layer, family and own name or class."""
144+
from pcapkit.protocols.link.linux_sll import LinuxSLL
145+
146+
return ({'layer': 'Link'}, {'layer': 'link'}, {'protocol': 'LinuxSLL'},
147+
{'protocol': version.__name__}, {'protocol': LinuxSLL}, {'protocol': version})
143148

144-
Stopping at the link layer or at the version keeps an EtherType payload
145-
:class:`~pcapkit.protocols.misc.raw.Raw`, while a registered
146-
``arphrd_type`` is still dispatched.
147-
"""
149+
def test_a_stop_at_this_layer_keeps_a_registered_arphrd_type_raw(self) -> None:
150+
"""The registry path stops as the EtherType path does (#2532)."""
148151
from pcapkit.const.linux.arphrd import ARPHRD
152+
from pcapkit.protocols.misc.raw import Raw
153+
154+
for version, header, alias in self._versions():
155+
for name, (payload, _) in CASES.items():
156+
for stop in self._stops(version):
157+
with self.subTest(version=version.__name__, arphrd=name, stop=stop):
158+
raw = header(0x0800, getattr(ARPHRD, name)) + payload
159+
parsed = version(raw, len(raw), **stop)
160+
self.assertEqual(parsed.protochain.chain, f'{alias}:Raw')
161+
self.assertIsInstance(parsed.payload, Raw)
162+
self.assertEqual(parsed.payload.data, payload)
163+
self.assertEqual(parsed.payload.info.protocol, 0x0800)
164+
self.assertEqual(parsed.data, raw)
165+
self.assertEqual(version.from_data(parsed.info).data, raw)
166+
167+
# Without a length, the whole payload is the next layer's.
168+
again = parsed._import_next_layer(parsed.protocol)
169+
self.assertIsInstance(again, Raw)
170+
self.assertEqual(again.data, payload)
171+
172+
def test_a_stop_at_this_layer_keeps_an_ethertype_raw(self) -> None:
173+
"""The Raw is named after the EtherType it would have dispatched on."""
174+
from pcapkit.const.reg.ethertype import EtherType
175+
from pcapkit.protocols.misc.raw import Raw
149176

150177
for version, header, alias in self._versions():
151-
for stop in ({'layer': 'Link'}, {'protocol': version.__name__}):
178+
for stop in self._stops(version):
152179
with self.subTest(version=version.__name__, stop=stop):
153180
raw = header(0x0800, 1) + IPV4
154-
self.assertEqual(version(raw, len(raw), **stop).protochain.chain,
181+
parsed = version(raw, len(raw), **stop)
182+
self.assertEqual(parsed.protochain.chain,
155183
f'{alias}:Internet_Protocol_version_4')
184+
self.assertIsInstance(parsed.payload, Raw)
185+
self.assertEqual(parsed.payload.data, IPV4)
186+
self.assertIs(parsed.payload.info.protocol,
187+
EtherType.Internet_Protocol_version_4)
188+
self.assertEqual(parsed.data, raw)
189+
self.assertEqual(version.from_data(parsed.info).data, raw)
190+
191+
def test_a_stop_at_another_protocol_still_dispatches_here(self) -> None:
192+
"""The other version's name and a protocol further in do not stop this layer."""
193+
from pcapkit.const.linux.arphrd import ARPHRD
194+
195+
frad = CASES['FRAD'][0]
196+
for version, header, alias in self._versions():
197+
other = 'LinuxSLL2' if version.__name__ == 'LinuxSLL1' else 'LinuxSLL1'
198+
for stop, chain in (({'protocol': other}, 'FRAD:IPv4:UDP'),
199+
({'protocol': 'FRAD'}, 'FRAD:Raw')):
200+
with self.subTest(version=version.__name__, stop=stop):
201+
raw = header(0x0800, ARPHRD.FRAD) + frad
202+
parsed = version(raw, len(raw), **stop)
203+
self.assertEqual(parsed.protochain.chain, f'{alias}:{chain}')
204+
self.assertEqual(version.from_data(parsed.info).data, raw)
156205

157-
raw = header(0x0800, ARPHRD.NETLINK) + CASES['NETLINK'][0]
206+
raw = header(0x0800, 1) + IPV4
158207
self.assertEqual(version(raw, len(raw), **stop).protochain.chain,
159-
f'{alias}:Netlink:Raw')
208+
f'{alias}:IPv4:UDP')
160209

161210
def test_index_is_the_arphrd_type_that_dispatches_here(self) -> None:
162211
from pcapkit.const.linux.arphrd import ARPHRD

‎tests/protocols/link/test_linux_sll_selector_unit.py‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,20 @@
2121
+ bytes.fromhex('3039003500080000'))
2222

2323

24-
def _header_v1(protocol_type: int = 0x0800) -> bytes:
24+
#: A FRAD frame, Q.922 address and UI control, carrying :data:`IPV4` by NLPID ``0xCC``.
25+
FRAD = bytes.fromhex('0c2103cc') + IPV4
26+
27+
28+
def _header_v1(protocol_type: int = 0x0800, arphrd_type: int = 1) -> bytes:
2529
"""Pack a minimal ``LINUX_SLL`` (v1) header."""
2630
link_addr = bytes.fromhex('001122334455').ljust(8, b'\x00')
27-
return struct.pack('>HHH', 0, 1, 6) + link_addr + struct.pack('>H', protocol_type)
31+
return struct.pack('>HHH', 0, arphrd_type, 6) + link_addr + struct.pack('>H', protocol_type)
2832

2933

30-
def _header_v2(protocol_type: int = 0x0800) -> bytes:
34+
def _header_v2(protocol_type: int = 0x0800, arphrd_type: int = 1) -> bytes:
3135
"""Pack a minimal ``LINUX_SLL2`` header."""
3236
link_addr = bytes.fromhex('001122334455').ljust(8, b'\x00')
33-
return struct.pack('>HHIHBB', protocol_type, 0, 1, 1, 0, 6) + link_addr
37+
return struct.pack('>HHIHBB', protocol_type, 0, 1, arphrd_type, 0, 6) + link_addr
3438

3539

3640
def _pcap(records: 'list[bytes]', linktype: int) -> bytes:
@@ -156,6 +160,20 @@ def test_extract_version_name_stops_only_that_version(self) -> None:
156160
self.assertEqual(_extract(_header_v1() + IPV4, LinkType.LINUX_SLL, 'LinuxSLL2'),
157161
'LINUX_SLL1:IPv4:UDP')
158162

163+
def test_extract_stops_a_registered_arphrd_type_alike(self) -> None:
164+
"""A FRAD frame stops where an EtherType one does (#2532)."""
165+
from pcapkit.const.linux.arphrd import ARPHRD
166+
from pcapkit.const.reg.linktype import LinkType
167+
168+
for linktype, header, alias, own, other in (
169+
(LinkType.LINUX_SLL, _header_v1, 'LINUX_SLL1', 'LinuxSLL1', 'LinuxSLL2'),
170+
(LinkType.LINUX_SLL2, _header_v2, 'LINUX_SLL2', 'LinuxSLL2', 'LinuxSLL1')):
171+
record = header(arphrd_type=ARPHRD.FRAD) + FRAD
172+
for protocol in ('LinuxSLL', own):
173+
with self.subTest(alias=alias, protocol=protocol):
174+
self.assertEqual(_extract(record, linktype, protocol), f'{alias}:Raw')
175+
self.assertEqual(_extract(record, linktype, other), f'{alias}:FRAD:IPv4:UDP')
176+
159177

160178
if __name__ == '__main__':
161179
unittest.main()

0 commit comments

Comments
 (0)