Skip to content

Commit fd9d47d

Browse files
committed
test(generators): enumerate FRAD's NLPID map and ICMP's type registries (#2560)
- dispatch.py: a `frad` family over `FRAD.__proto__`, probing each NLPID through a LINUX_SLL capture with `arphrd_type` FRAD, and `PINNED_TARGETS` entries for both codes; `test_cases_cover_every_table_named_in_the_issue` counts them. - options.py: `icmpv4-type` and `icmpv6-type` families over `ICMPv4.__type__` and `ICMPv6.__type__`, with non-degenerate echo, quoted-packet, pointer and MTU arguments; neither writes a capture. - test_option_generator_icmp_2560_unit: pins each generated message's data model and body against RFC 792 and RFC 4443, which catches a retarget between two types of the same layout that the round trip cannot see.
1 parent 5145a10 commit fd9d47d

4 files changed

Lines changed: 322 additions & 11 deletions

File tree

‎examples/generators/dispatch.py‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,36 @@ def _loopback_build(family: 'int') -> 'Any':
612612
return _extract_first(_pcap_bytes((_loopback(family, payload),), linktype=0), '.pcap')
613613

614614

615+
###############################################################################
616+
# FRAD -- NLPID.
617+
###############################################################################
618+
619+
620+
def _frad_registry() -> 'Any':
621+
from pcapkit.protocols.link.frad import FRAD
622+
return FRAD.__proto__
623+
624+
625+
def _frad_payload(nlpid: 'int') -> 'bytes':
626+
"""The registered target's own wire bytes for ``nlpid``."""
627+
if nlpid == 0xCC:
628+
return _ip4(6, _tcp(9999))
629+
if nlpid == 0x8E:
630+
return _ip6(6, _tcp(9999))
631+
raise LookupError(f'frad: no payload builder for NLPID {nlpid!r}') # pragma: no cover
632+
633+
634+
def _frad_build(nlpid: 'int') -> 'Any':
635+
"""A direct-NLPID FRAD frame, reached as LinuxSLL dispatches it in a capture."""
636+
from pcapkit.const.linux.arphrd import ARPHRD
637+
from pcapkit.protocols.link.frad import FRAD
638+
from pcapkit.protocols.link.linux_sll1 import LinuxSLL1
639+
640+
frad = bytes(FRAD(nlpid=int(nlpid), payload=_frad_payload(nlpid)))
641+
record = bytes(LinuxSLL1(arphrd_type=ARPHRD.FRAD, protocol_type=0, payload=frad))
642+
return _extract_first(_pcap_bytes((record,), linktype=113), '.pcap')
643+
644+
615645
###############################################################################
616646
# The families, the cases, and the probe.
617647
###############################################################################
@@ -639,7 +669,8 @@ def _link_enum() -> 'Any':
639669

640670

641671
#: Every family this module exercises, one per ``__proto__`` table named in
642-
#: GitHub issue #496, and the loopback table #1574 added.
672+
#: GitHub issue #496, the loopback table #1574 added, and FRAD's NLPID table
673+
#: (#2560).
643674
FAMILIES = (
644675
Family('link', _link_registry, _link_build, _link_enum),
645676
Family('internet', _internet_registry, _internet_build),
@@ -649,6 +680,7 @@ def _link_enum() -> 'Any':
649680
Family('pcap-frame', _pcap_frame_registry, _pcap_frame_build),
650681
Family('pcapng-frame', _pcapng_registry, _pcapng_build),
651682
Family('loopback', _loopback_registry, _loopback_build),
683+
Family('frad', _frad_registry, _frad_build),
652684
)
653685

654686
#: The families keyed by label, for a caller -- :func:`probe` -- that wants
@@ -758,6 +790,10 @@ def _link_enum() -> 'Any':
758790
'loopback/24': ('pcapkit.protocols.internet.ipv6', 'IPv6'),
759791
'loopback/28': ('pcapkit.protocols.internet.ipv6', 'IPv6'),
760792
'loopback/30': ('pcapkit.protocols.internet.ipv6', 'IPv6'),
793+
794+
# -- FRAD.__proto__ (NLPID, keyed in decimal: 0x8E, 0xCC) ------------------
795+
'frad/142': ('pcapkit.protocols.internet.ipv6', 'IPv6'),
796+
'frad/204': ('pcapkit.protocols.internet.ipv4', 'IPv4'),
761797
}
762798

763799

‎examples/generators/options.py‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,103 @@ def _hip_rebuild(parameters: 'Any') -> 'Any':
11031103
return HIP(parameters=parameters, extension=True, **base)
11041104

11051105

1106+
###############################################################################
1107+
# ICMP and ICMPv6 -- message types
1108+
###############################################################################
1109+
1110+
#: The invoking packet an ICMP error message quotes: an IPv4 header and the
1111+
#: first eight octets of its UDP datagram [:rfc:`792`].
1112+
ICMPV4_QUOTED = (bytes.fromhex('4500001c00010000401100000a0000010a000002')
1113+
+ bytes.fromhex('3039003500080000'))
1114+
1115+
#: The invoking packet an ICMPv6 error message quotes: an IPv6 header and its
1116+
#: UDP header [:rfc:`4443#section-3`].
1117+
ICMPV6_QUOTED = (bytes.fromhex('6000000000081140') + bytes(15) + b'\x01' + bytes(15) + b'\x02'
1118+
+ bytes.fromhex('3039003500080000'))
1119+
1120+
#: Echo arguments shared by both versions. The defaults are all zero and empty,
1121+
#: which cannot tell a swapped ``id`` and ``seq``, or a dropped ``data``, apart.
1122+
ICMP_ECHO = {'id': 0x1234, 'seq': 0x5678, 'data': b'ping'}
1123+
1124+
1125+
def _icmpv4_registry() -> 'Any':
1126+
from pcapkit.protocols.internet.icmpv4 import ICMPv4
1127+
return ICMPv4.__type__
1128+
1129+
1130+
def _icmpv4_overrides() -> 'dict[Any, dict[str, Any]]':
1131+
from pcapkit.const.icmp.type import Type as Enum_Type
1132+
return {
1133+
Enum_Type.Echo_Reply: dict(ICMP_ECHO),
1134+
Enum_Type.Echo: dict(ICMP_ECHO),
1135+
Enum_Type.Destination_Unreachable: {'data': ICMPV4_QUOTED},
1136+
Enum_Type.Time_Exceeded: {'data': ICMPV4_QUOTED},
1137+
# Octet 8 of the quoted header is its TTL.
1138+
Enum_Type.Parameter_Problem: {'pointer': 8, 'data': ICMPV4_QUOTED},
1139+
}
1140+
1141+
1142+
def _icmpv4_build(code: 'Any', kwargs: 'dict[str, Any]') -> 'Any':
1143+
from pcapkit.protocols.internet.icmpv4 import ICMPv4
1144+
1145+
# ``body`` has to be a mapping for ``make`` to dispatch on ``__type__``;
1146+
# a ``bytes`` body is parsed against the schema registry instead.
1147+
return ICMPv4(type=code, code=0, body=dict(kwargs))
1148+
1149+
1150+
def _icmpv4_parse(octets: 'bytes') -> 'Any':
1151+
from pcapkit.protocols.internet.icmpv4 import ICMPv4
1152+
return ICMPv4(octets, len(octets))
1153+
1154+
1155+
def _icmp_extract(parsed: 'Any') -> 'Any':
1156+
return parsed.info
1157+
1158+
1159+
def _icmpv4_rebuild(message: 'Any') -> 'Any':
1160+
from pcapkit.protocols.internet.icmpv4 import ICMPv4
1161+
return ICMPv4(type=message.type, code=message.code, checksum=message.checksum,
1162+
body=message)
1163+
1164+
1165+
def _icmpv6_registry() -> 'Any':
1166+
from pcapkit.protocols.internet.icmpv6 import ICMPv6
1167+
return ICMPv6.__type__
1168+
1169+
1170+
def _icmpv6_overrides() -> 'dict[Any, dict[str, Any]]':
1171+
from pcapkit.const.icmpv6.type import Type as Enum_Type
1172+
return {
1173+
Enum_Type.Destination_Unreachable: {'data': ICMPV6_QUOTED},
1174+
# The IPv6 minimum link MTU [RFC 8200 section 5].
1175+
Enum_Type.Packet_Too_Big: {'mtu': 1280, 'data': ICMPV6_QUOTED},
1176+
Enum_Type.Time_Exceeded: {'data': ICMPV6_QUOTED},
1177+
# Octet 6 of the quoted header is its Next Header.
1178+
Enum_Type.Parameter_Problem: {'pointer': 6, 'data': ICMPV6_QUOTED},
1179+
Enum_Type.Echo_Request: dict(ICMP_ECHO),
1180+
Enum_Type.Echo_Reply: dict(ICMP_ECHO),
1181+
}
1182+
1183+
1184+
def _icmpv6_build(code: 'Any', kwargs: 'dict[str, Any]') -> 'Any':
1185+
from pcapkit.protocols.internet.icmpv6 import ICMPv6
1186+
1187+
# With no enclosing IPv6 layer to supply the pseudo-header, the checksum is
1188+
# left zero, and the rebuild passes the parsed zero back.
1189+
return ICMPv6(type=code, code=0, body=dict(kwargs))
1190+
1191+
1192+
def _icmpv6_parse(octets: 'bytes') -> 'Any':
1193+
from pcapkit.protocols.internet.icmpv6 import ICMPv6
1194+
return ICMPv6(octets, len(octets))
1195+
1196+
1197+
def _icmpv6_rebuild(message: 'Any') -> 'Any':
1198+
from pcapkit.protocols.internet.icmpv6 import ICMPv6
1199+
return ICMPv6(type=message.type, code=message.code, checksum=message.checksum,
1200+
body=message)
1201+
1202+
11061203
###############################################################################
11071204
# HTTP/2 -- frames
11081205
###############################################################################
@@ -1595,6 +1692,14 @@ def _pcapng_secrets_build(code: 'Any', kwargs: 'dict[str, Any]') -> 'Any':
15951692
Family('hip-parameter', _hip_registry, _hip_overrides,
15961693
_hip_build, _hip_parse, _hip_extract, _hip_rebuild,
15971694
capture='options-internet.pcap', envelope='ipv6', proto=139),
1695+
# NOTE: The ICMP families write no capture; their wire form is pinned against
1696+
# RFC 792 and RFC 4443 by test_option_generator_icmp_2560_unit instead.
1697+
Family('icmpv4-type', _icmpv4_registry, _icmpv4_overrides,
1698+
_icmpv4_build, _icmpv4_parse, _icmp_extract, _icmpv4_rebuild,
1699+
capture=None, envelope=None),
1700+
Family('icmpv6-type', _icmpv6_registry, _icmpv6_overrides,
1701+
_icmpv6_build, _icmpv6_parse, _icmp_extract, _icmpv6_rebuild,
1702+
capture=None, envelope=None),
15981703
# The PCAP-NG families write no capture -- see the section comment
15991704
# above ``PCAPNG_TIME`` for why the construction API's defaults do not
16001705
# make a reproducible one, and what already covers that space instead.

‎tests/protocols/test_dispatch_registry_unit.py‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
GitHub issue #496: the ``__proto__`` dispatch registries decide which
55
:class:`~pcapkit.protocols.protocol.Protocol` subclass parses the next layer.
66
#496 names the tables, joined by #904's
7-
``Internet.__proto__[TransType.Shim6]`` and #1574's loopback table.
7+
``Internet.__proto__[TransType.Shim6]``, #1574's loopback table and #2560's
8+
FRAD table.
89
:file:`test_registry_runtime.py` checks that a table entry *resolves* to the
910
right class object, and
1011
:file:`test_dispatch_bindings_unit.py` checks that a hand-picked set of codes
@@ -215,7 +216,7 @@ def test_registry_matches_pinned_target(self) -> None:
215216
)
216217

217218
def test_cases_cover_every_table_named_in_the_issue(self) -> None:
218-
"""The enumeration finds every entry, across the tables of #496 and #1574's loopback table.
219+
"""The enumeration finds every entry, across the tables of #496, #1574 and #2560.
219220
220221
A guard on the shape of the result rather than on any one case: if the
221222
registries grow or shrink without this module noticing, the per-family
@@ -228,20 +229,22 @@ def test_cases_cover_every_table_named_in_the_issue(self) -> None:
228229
# LinkType.NULL and LinkType.LOOP in both link-type tables, the four
229230
# address families of Loopback.__proto__, #2438's
230231
# Frame.__proto__[LinkType.LINUX_SLL]/[LinkType.LINUX_SLL2] in both the
231-
# pcap and pcapng frame tables, #2439's IGMP, and #2436's
232-
# Internet.__proto__[TransType.ICMP] and [TransType.IPv6_ICMP].
233-
self.assertEqual(len(cases), 56,
234-
'expected exactly 56 entries across the seven __proto__ '
235-
'tables named in GitHub issue #496 and the loopback table '
236-
'of #1574; a different count means a registry changed '
237-
'shape and this module was not updated to match')
232+
# pcap and pcapng frame tables, #2439's IGMP, #2436's
233+
# Internet.__proto__[TransType.ICMP] and [TransType.IPv6_ICMP], and
234+
# #2560's two NLPIDs of FRAD.__proto__.
235+
self.assertEqual(len(cases), 58,
236+
'expected exactly 58 entries across the seven __proto__ '
237+
'tables named in GitHub issue #496, the loopback table '
238+
'of #1574 and the FRAD table of #2560; a different count '
239+
'means a registry changed shape and this module was not '
240+
'updated to match')
238241

239242
by_family = {} # type: dict[str, int]
240243
for case in cases:
241244
by_family[case.family] = by_family.get(case.family, 0) + 1
242245
self.assertEqual(by_family, {
243246
'link': 7, 'internet': 20, 'tcp': 5, 'udp': 4, 'sctp': 2,
244-
'pcap-frame': 7, 'pcapng-frame': 7, 'loopback': 4,
247+
'pcap-frame': 7, 'pcapng-frame': 7, 'loopback': 4, 'frad': 2,
245248
})
246249

247250
def test_dispatch_reaches_target_or_is_a_recorded_degrade(self) -> None:

0 commit comments

Comments
 (0)