Skip to content

Commit ea5e5df

Browse files
committed
fix(pcap): _make_magic ignores nanosecond with explicit endian flags; _read_protos ignores size
- _make_magic: the lilendian/bigendian branch hard-coded the magic, always nanosecond for little-endian and microsecond for big-endian. It now looks up _MAGIC_NUM with the nanosecond flag, as the byteorder branch does. - Header._read_protos: read `size` bytes instead of a fixed 4, matching Link._read_protos and Internet._read_protos. - test_header_frame_unit pinned the old little-endian nanosecond magic for lilendian=True with nanosecond=False; corrected to d4c3b2a1. Closes #1096
1 parent b10e795 commit ea5e5df

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎pcapkit/protocols/misc/pcap/header.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def _read_protos(self, size: int) -> 'Enum_LinkType':
324324
Link layer protocol enumeration.
325325
326326
"""
327-
_byte = self._read_unpack(4, lilendian=True)
327+
_byte = self._read_unpack(size, lilendian=True)
328328
_prot = Enum_LinkType.get(_byte)
329329
return _prot
330330

@@ -347,9 +347,9 @@ def _make_magic(self, byteorder: 'Literal["big", "little"]' = sys.byteorder,
347347
if lilendian == bigendian:
348348
raise EndianError('unresolved byte order')
349349
if bigendian:
350-
return _MAGIC_NUM[('big', False)], False
350+
return _MAGIC_NUM[('big', nanosecond)], False
351351
if lilendian:
352-
return _MAGIC_NUM[('little', True)], True
352+
return _MAGIC_NUM[('little', nanosecond)], True
353353

354354
if byteorder not in ('little', 'big'):
355355
raise EndianError(f"unknown byte order: {byteorder!r}")

‎tests/protocols/misc/pcap/test_header_frame_unit.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class NamedBytesIO(io.BytesIO):
143143
self.assertEqual(header._make_magic(lilendian=False, bigendian=True),
144144
(b'\xa1\xb2\xc3\xd4', False))
145145
self.assertEqual(header._make_magic(lilendian=True, bigendian=False),
146-
(b'\x4d\x3c\xb2\xa1', True))
146+
(b'\xd4\xc3\xb2\xa1', True))
147147
self.assertEqual(header._make_magic(byteorder='big', lilendian='', bigendian=False), # type: ignore[arg-type]
148148
(b'\xa1\xb2\xc3\xd4', False))
149149
with self.assertRaises(EndianError):
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from __future__ import annotations
2+
3+
import importlib.util
4+
import io
5+
import unittest
6+
7+
from tests._support import reimport_once_per_class
8+
9+
RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper')
10+
HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS)
11+
12+
13+
@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
14+
class PCAPHeaderMagicProtosUnitTests(unittest.TestCase):
15+
"""``Header._make_magic`` and ``Header._read_protos`` (GH-1096)."""
16+
17+
def setUp(self) -> None:
18+
reimport_once_per_class(self)
19+
20+
def test_make_magic_endian_flags_honour_nanosecond(self) -> None:
21+
from pcapkit.protocols.misc.pcap.header import Header
22+
23+
header = object.__new__(Header)
24+
cases = [
25+
((True, False, False), (b'\xd4\xc3\xb2\xa1', True)),
26+
((True, False, True), (b'\x4d\x3c\xb2\xa1', True)),
27+
((False, True, False), (b'\xa1\xb2\xc3\xd4', False)),
28+
((False, True, True), (b'\xa1\xb2\x3c\x4d', False)),
29+
]
30+
for (lilendian, bigendian, nanosecond), expected in cases:
31+
with self.subTest(lilendian=lilendian, bigendian=bigendian, nanosecond=nanosecond):
32+
self.assertEqual(header._make_magic(lilendian=lilendian, bigendian=bigendian,
33+
nanosecond=nanosecond), expected)
34+
35+
def test_make_magic_byteorder_matches_endian_flags(self) -> None:
36+
from pcapkit.protocols.misc.pcap.header import Header
37+
38+
header = object.__new__(Header)
39+
for byteorder in ('little', 'big'):
40+
for nanosecond in (False, True):
41+
with self.subTest(byteorder=byteorder, nanosecond=nanosecond):
42+
self.assertEqual(
43+
header._make_magic(byteorder, nanosecond=nanosecond),
44+
header._make_magic(lilendian=byteorder == 'little',
45+
bigendian=byteorder == 'big', nanosecond=nanosecond),
46+
)
47+
48+
def test_read_protos_reads_size_bytes(self) -> None:
49+
from pcapkit.const.reg.linktype import LinkType
50+
from pcapkit.protocols.misc.pcap.header import Header
51+
52+
header = object.__new__(Header)
53+
for size in (1, 2, 4):
54+
with self.subTest(size=size):
55+
header._file = io.BytesIO(int(LinkType.ETHERNET).to_bytes(size, 'little') + b'\xff' * 4)
56+
self.assertEqual(header._read_protos(size), LinkType.ETHERNET)
57+
self.assertEqual(header._file.tell(), size)
58+
59+
60+
if __name__ == '__main__':
61+
unittest.main()

0 commit comments

Comments
 (0)