Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pcapkit/protocols/misc/pcap/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def _read_protos(self, size: int) -> 'Enum_LinkType':
Link layer protocol enumeration.

"""
_byte = self._read_unpack(4, lilendian=True)
_byte = self._read_unpack(size, lilendian=True)
_prot = Enum_LinkType.get(_byte)
return _prot

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

if byteorder not in ('little', 'big'):
raise EndianError(f"unknown byte order: {byteorder!r}")
Expand Down
2 changes: 1 addition & 1 deletion tests/protocols/misc/pcap/test_header_frame_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class NamedBytesIO(io.BytesIO):
self.assertEqual(header._make_magic(lilendian=False, bigendian=True),
(b'\xa1\xb2\xc3\xd4', False))
self.assertEqual(header._make_magic(lilendian=True, bigendian=False),
(b'\x4d\x3c\xb2\xa1', True))
(b'\xd4\xc3\xb2\xa1', True))
self.assertEqual(header._make_magic(byteorder='big', lilendian='', bigendian=False), # type: ignore[arg-type]
(b'\xa1\xb2\xc3\xd4', False))
with self.assertRaises(EndianError):
Expand Down
61 changes: 61 additions & 0 deletions tests/protocols/misc/pcap/test_header_magic_protos_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from __future__ import annotations

import importlib.util
import io
import unittest

from tests._support import reimport_once_per_class

RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper')
HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS)


@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed')
class PCAPHeaderMagicProtosUnitTests(unittest.TestCase):
"""``Header._make_magic`` and ``Header._read_protos`` (GH-1096)."""

def setUp(self) -> None:
reimport_once_per_class(self)

def test_make_magic_endian_flags_honour_nanosecond(self) -> None:
from pcapkit.protocols.misc.pcap.header import Header

header = object.__new__(Header)
cases = [
((True, False, False), (b'\xd4\xc3\xb2\xa1', True)),
((True, False, True), (b'\x4d\x3c\xb2\xa1', True)),
((False, True, False), (b'\xa1\xb2\xc3\xd4', False)),
((False, True, True), (b'\xa1\xb2\x3c\x4d', False)),
]
for (lilendian, bigendian, nanosecond), expected in cases:
with self.subTest(lilendian=lilendian, bigendian=bigendian, nanosecond=nanosecond):
self.assertEqual(header._make_magic(lilendian=lilendian, bigendian=bigendian,
nanosecond=nanosecond), expected)

def test_make_magic_byteorder_matches_endian_flags(self) -> None:
from pcapkit.protocols.misc.pcap.header import Header

header = object.__new__(Header)
for byteorder in ('little', 'big'):
for nanosecond in (False, True):
with self.subTest(byteorder=byteorder, nanosecond=nanosecond):
self.assertEqual(
header._make_magic(byteorder, nanosecond=nanosecond),
header._make_magic(lilendian=byteorder == 'little',
bigendian=byteorder == 'big', nanosecond=nanosecond),
)

def test_read_protos_reads_size_bytes(self) -> None:
from pcapkit.const.reg.linktype import LinkType
from pcapkit.protocols.misc.pcap.header import Header

header = object.__new__(Header)
for size in (1, 2, 4):
with self.subTest(size=size):
header._file = io.BytesIO(int(LinkType.ETHERNET).to_bytes(size, 'little') + b'\xff' * 4)
self.assertEqual(header._read_protos(size), LinkType.ETHERNET)
self.assertEqual(header._file.tell(), size)


if __name__ == '__main__':
unittest.main()
Loading