Skip to content

Commit fa9af52

Browse files
committed
test(link): build each 802.11 field at its widest value (#2575)
A +-1 sweep over the `bits=` widths in ieee802_11.py left six narrowing mutants alive, since no test built a field at its maximum. Build each multi-bit field make() writes as given inside a radiotap header: at its widest value, pinning the octets and the parse-back, and one past it, requiring a ProtocolError that names the field.
1 parent 5def5cf commit fa9af52

1 file changed

Lines changed: 89 additions & 2 deletions

File tree

‎tests/protocols/link/test_ieee802_11_unit.py‎

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
DS/From DS combination, management frames, the header length of every
88
control subtype, Control Frame Extension and Extension frames, the QoS and HT
99
Control fields, a refused protocol version, truncated input and ``make()``,
10-
each with byte-exact round trips both ways. The Radiotap side of the dispatch
11-
is covered in ``test_radiotap_unit.py``.
10+
each with byte-exact round trips both ways, and each multi-bit field
11+
``make()`` writes as given, at its widest value and one past it, inside a
12+
radiotap header. The Radiotap side of the dispatch is covered in
13+
``test_radiotap_unit.py``.
1214
1315
Each test imports :mod:`pcapkit` itself, after
1416
:func:`~tests._support.reimport_once_per_class`.
@@ -37,6 +39,53 @@ def _mac(n: int) -> str:
3739
#: Flag bits of the second Frame Control octet.
3840
TO_DS, FROM_DS, MORE_FRAG, RETRY, PWR_MGT, MORE_DATA, PROTECTED, ORDER = (1 << i for i in range(8))
3941

42+
#: The radiotap header ``Radiotap()`` writes by default: an empty present
43+
#: bitmask and no fields.
44+
RADIOTAP = bytes.fromhex('0000080000000000')
45+
46+
#: QoS Control with every subfield zero.
47+
QOS_ZERO = {'tid': 0, 'eosp': False, 'ack_policy': 0, 'amsdu_present': False, 'bits_8_15': 0}
48+
49+
50+
def _seq(**fields: int) -> dict:
51+
"""A Data frame whose Sequence Control is zero but for ``fields``."""
52+
return {'type': 2, 'subtype': 0, 'seq_ctrl': {'fragment_number': 0, 'sequence_number': 0, **fields}}
53+
54+
55+
def _qos(**fields: int) -> dict:
56+
"""A QoS Data frame whose QoS Control is zero but for ``fields``."""
57+
return {'type': 2, 'subtype': 8, 'qos_control': {**QOS_ZERO, **fields}}
58+
59+
60+
#: Each multi-bit field ``make()`` writes as given (Protocol Version builds
61+
#: only ``0``): its width in bits (IEEE Std 802.11-2020 §9.2.4), the
62+
#: arguments of a frame that carries it, where a parse reads it back, and
63+
#: that frame's octets with the field at its widest value and the rest zero.
64+
WIDEST = (
65+
('type', 2, lambda v: {'type': v, 'subtype': 0}, lambda i: i.frame_control.type,
66+
bytes.fromhex('0c00') + bytes(8)),
67+
('subtype', 4, lambda v: {'type': 1, 'subtype': v}, lambda i: i.frame_control.subtype,
68+
bytes.fromhex('f400') + bytes(14)),
69+
('duration_id', 16, lambda v: {'type': 2, 'subtype': 0, 'duration_id': v}, lambda i: i.duration_id,
70+
bytes.fromhex('0800ffff') + bytes(20)),
71+
('control_frame_extension', 4, lambda v: {'type': 1, 'subtype': 6, 'control_frame_extension': v},
72+
lambda i: i.frame_control.control_frame_extension, bytes.fromhex('640f') + bytes(14)),
73+
('s1g_beacon_flags', 8, lambda v: {'type': 3, 'subtype': 1, 's1g_beacon_flags': v},
74+
lambda i: i.frame_control.s1g_beacon_flags, bytes.fromhex('1cff') + bytes(8)),
75+
('fragment_number', 4, lambda v: _seq(fragment_number=v), lambda i: i.seq_ctrl.fragment_number,
76+
bytes.fromhex('0800') + bytes(20) + b'\x0f\x00'),
77+
('sequence_number', 12, lambda v: _seq(sequence_number=v), lambda i: i.seq_ctrl.sequence_number,
78+
bytes.fromhex('0800') + bytes(20) + b'\xf0\xff'),
79+
('tid', 4, lambda v: _qos(tid=v), lambda i: i.qos_control.tid,
80+
bytes.fromhex('8800') + bytes(22) + b'\x0f\x00'),
81+
('ack_policy', 2, lambda v: _qos(ack_policy=v), lambda i: i.qos_control.ack_policy,
82+
bytes.fromhex('8800') + bytes(22) + b'\x60\x00'),
83+
('bits_8_15', 8, lambda v: _qos(bits_8_15=v), lambda i: i.qos_control.bits_8_15,
84+
bytes.fromhex('8800') + bytes(22) + b'\x00\xff'),
85+
('ht_control', 32, lambda v: {'type': 2, 'subtype': 8, 'order': True, 'ht_control': v},
86+
lambda i: i.ht_control.value, bytes.fromhex('8880') + bytes(24) + b'\xff' * 4),
87+
)
88+
4089

4190
class TestIEEE802_11(unittest.TestCase):
4291
"""Pin the IEEE 802.11 MAC header layer, alone."""
@@ -433,6 +482,44 @@ def test_id_index_and_names(self) -> None:
433482
self.assertEqual(made.__length_hint__(), 36)
434483

435484

485+
class TestIEEE802_11WidestValues(unittest.TestCase):
486+
"""Each multi-bit field ``make()`` writes as given, built inside a
487+
radiotap header at the widest value its width allows and at one past
488+
it."""
489+
490+
def setUp(self) -> None:
491+
reimport_once_per_class(self)
492+
493+
def test_widest_value_builds_and_parses_back(self) -> None:
494+
"""All ones lands on the field's own bits and reads back whole, and
495+
both layers rebuild the octets from their info."""
496+
from pcapkit.protocols.link.ieee802_11 import IEEE802_11
497+
from pcapkit.protocols.link.radiotap import Radiotap
498+
499+
for name, bits, kwargs, read, octets in WIDEST:
500+
with self.subTest(field=name):
501+
widest = (1 << bits) - 1
502+
raw = Radiotap(payload=IEEE802_11(**kwargs(widest))).data
503+
self.assertEqual(raw.hex(), (RADIOTAP + octets).hex())
504+
505+
parsed = Radiotap(raw, len(raw))
506+
self.assertEqual(parsed.protochain.chain, 'Radiotap:802.11')
507+
frame = parsed.payload
508+
self.assertEqual(int(read(frame.info)), widest)
509+
self.assertEqual(IEEE802_11.from_data(frame.info).data.hex(), octets.hex())
510+
self.assertEqual(Radiotap.from_data(parsed.info).data.hex(), raw.hex())
511+
512+
def test_one_past_the_widest_value_is_refused(self) -> None:
513+
from pcapkit.protocols.link.ieee802_11 import IEEE802_11
514+
from pcapkit.protocols.link.radiotap import Radiotap
515+
from pcapkit.utilities.exceptions import ProtocolError
516+
517+
for name, bits, kwargs, _, _ in WIDEST:
518+
pattern = rf'^802\.11: invalid {name}: {1 << bits}$'
519+
with self.subTest(field=name), self.assertRaisesRegex(ProtocolError, pattern):
520+
Radiotap(payload=IEEE802_11(**kwargs(1 << bits)))
521+
522+
436523
class TestIEEE802_11Registries(unittest.TestCase):
437524
"""The Table 9-1 registries resolve a reserved value under its bare label."""
438525

0 commit comments

Comments
 (0)