|
7 | 7 | DS/From DS combination, management frames, the header length of every |
8 | 8 | control subtype, Control Frame Extension and Extension frames, the QoS and HT |
9 | 9 | 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``. |
12 | 14 |
|
13 | 15 | Each test imports :mod:`pcapkit` itself, after |
14 | 16 | :func:`~tests._support.reimport_once_per_class`. |
@@ -37,6 +39,53 @@ def _mac(n: int) -> str: |
37 | 39 | #: Flag bits of the second Frame Control octet. |
38 | 40 | TO_DS, FROM_DS, MORE_FRAG, RETRY, PWR_MGT, MORE_DATA, PROTECTED, ORDER = (1 << i for i in range(8)) |
39 | 41 |
|
| 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 | + |
40 | 89 |
|
41 | 90 | class TestIEEE802_11(unittest.TestCase): |
42 | 91 | """Pin the IEEE 802.11 MAC header layer, alone.""" |
@@ -433,6 +482,44 @@ def test_id_index_and_names(self) -> None: |
433 | 482 | self.assertEqual(made.__length_hint__(), 36) |
434 | 483 |
|
435 | 484 |
|
| 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 | + |
436 | 523 | class TestIEEE802_11Registries(unittest.TestCase): |
437 | 524 | """The Table 9-1 registries resolve a reserved value under its bare label.""" |
438 | 525 |
|
|
0 commit comments