|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""``Frame.from_data`` rebuilds a captured frame. |
| 3 | +
|
| 4 | +GitHub issue #1126: :meth:`~pcapkit.protocols.protocol.ProtocolBase.from_data` |
| 5 | +took nothing but the data, so the ``num`` and ``header`` that |
| 6 | +:class:`~pcapkit.protocols.misc.pcap.frame.Frame` requires could not reach it, |
| 7 | +and every call raised :exc:`TypeError`. ``from_data`` now forwards extra |
| 8 | +keywords to the construction, so the caller passes both alongside the data. |
| 9 | +
|
| 10 | +The frames are read from :file:`examples/captures/in.pcap`, so this belongs to |
| 11 | +the runtime tier. |
| 12 | +
|
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import importlib.util |
| 17 | +import os |
| 18 | +import unittest |
| 19 | + |
| 20 | +from tests._support import reimport_once_per_class, sample_path |
| 21 | + |
| 22 | +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') |
| 23 | +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) |
| 24 | + |
| 25 | + |
| 26 | +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') |
| 27 | +class FrameFromDataRuntimeTests(unittest.TestCase): |
| 28 | + def setUp(self) -> None: |
| 29 | + reimport_once_per_class(self) |
| 30 | + |
| 31 | + def test_from_data_round_trips_every_frame_byte_for_byte(self) -> None: |
| 32 | + from pcapkit.protocols.misc.pcap.frame import Frame |
| 33 | + from pcapkit.protocols.misc.pcap.header import Header |
| 34 | + |
| 35 | + path = sample_path('in.pcap') |
| 36 | + size = os.path.getsize(path) |
| 37 | + with open(path, 'rb') as file: |
| 38 | + header = Header(file) |
| 39 | + num = 0 |
| 40 | + while file.tell() < size: |
| 41 | + num += 1 |
| 42 | + start = file.tell() |
| 43 | + frame = Frame(file, num=num, header=header.info) |
| 44 | + end = start + 16 + frame.info.frame_info.incl_len |
| 45 | + file.seek(start) |
| 46 | + octets = file.read(end - start) |
| 47 | + |
| 48 | + with self.subTest(frame=num): |
| 49 | + rebuilt = Frame.from_data(frame.info, num=num, header=header.info) |
| 50 | + self.assertEqual(bytes(rebuilt), octets) |
| 51 | + self.assertEqual(rebuilt.info.number, num) |
| 52 | + |
| 53 | + self.assertEqual(num, 6) |
| 54 | + |
| 55 | + def test_from_data_keyword_overrides_make_data(self) -> None: |
| 56 | + from pcapkit.protocols.misc.pcap.frame import Frame |
| 57 | + from pcapkit.protocols.misc.pcap.header import Header |
| 58 | + |
| 59 | + with open(sample_path('in.pcap'), 'rb') as file: |
| 60 | + header = Header(file) |
| 61 | + frame = Frame(file, num=1, header=header.info) |
| 62 | + |
| 63 | + rebuilt = Frame.from_data(frame.info, num=1, header=header.info, ts_sec=42) |
| 64 | + self.assertEqual(rebuilt.info.frame_info.ts_sec, 42) |
| 65 | + |
| 66 | + def test_from_data_rejects_a_misspelled_keyword(self) -> None: |
| 67 | + from pcapkit.protocols.misc.pcap.frame import Frame |
| 68 | + from pcapkit.protocols.misc.pcap.header import Header |
| 69 | + from pcapkit.utilities.exceptions import UnsupportedCall |
| 70 | + |
| 71 | + with open(sample_path('in.pcap'), 'rb') as file: |
| 72 | + header = Header(file) |
| 73 | + frame = Frame(file, num=1, header=header.info) |
| 74 | + |
| 75 | + with self.assertRaisesRegex(UnsupportedCall, "'heaedr'"): |
| 76 | + Frame.from_data(frame.info, num=1, heaedr=header.info) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + unittest.main() |
0 commit comments