|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Regression tests for #1165: ``format='pcap'`` keeps ``thiszone``, ``sigfigs`` |
| 3 | +and ``snaplen``. |
| 4 | +
|
| 5 | +The PCAP engine passes all three from the input's global header to |
| 6 | +:meth:`~pcapkit.foundation.extraction.Extractor._open_output`, so |
| 7 | +:class:`~pcapkit.dumpkit.pcap.PCAPIO` writes them back instead of the |
| 8 | +:class:`~pcapkit.protocols.misc.pcap.header.Header` defaults. |
| 9 | +
|
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import os |
| 14 | +import struct |
| 15 | +import tempfile |
| 16 | +import unittest |
| 17 | +import warnings |
| 18 | + |
| 19 | +from pcapkit.interface import extract |
| 20 | +from tests._support import sample_path |
| 21 | + |
| 22 | +#: Values that differ from every :class:`~pcapkit.protocols.misc.pcap.header.Header` |
| 23 | +#: default (``0``, ``0`` and ``262144``). |
| 24 | +THISZONE, SIGFIGS, SNAPLEN = -3600, 7, 1514 |
| 25 | + |
| 26 | + |
| 27 | +def _extract(fin: str, fout: str, **kwargs): |
| 28 | + with warnings.catch_warnings(): |
| 29 | + warnings.simplefilter('ignore') |
| 30 | + return extract(fin=fin, fout=fout, nofile=False, format='pcap', **kwargs) |
| 31 | + |
| 32 | + |
| 33 | +def _read(path: str) -> bytes: |
| 34 | + with open(path, 'rb') as file: |
| 35 | + return file.read() |
| 36 | + |
| 37 | + |
| 38 | +def _with_header_fields(data: bytes) -> bytes: |
| 39 | + """Rewrite bytes 8--20 of a PCAP file in the byte order of its magic number.""" |
| 40 | + endian = '<' if data[:4] in (b'\xd4\xc3\xb2\xa1', b'\x4d\x3c\xb2\xa1') else '>' |
| 41 | + return data[:8] + struct.pack(f'{endian}iII', THISZONE, SIGFIGS, SNAPLEN) + data[20:] |
| 42 | + |
| 43 | + |
| 44 | +class PCAPOutputHeaderFieldsTests(unittest.TestCase): |
| 45 | + |
| 46 | + def setUp(self) -> None: |
| 47 | + self.tmp = tempfile.TemporaryDirectory() # pylint: disable=consider-using-with |
| 48 | + self.addCleanup(self.tmp.cleanup) |
| 49 | + self.input = os.path.join(self.tmp.name, 'in.pcap') |
| 50 | + with open(self.input, 'wb') as file: |
| 51 | + file.write(_with_header_fields(_read(sample_path('in.pcap')))) |
| 52 | + |
| 53 | + def test_whole_capture_round_trips_byte_identically(self) -> None: |
| 54 | + extractor = _extract(self.input, os.path.join(self.tmp.name, 'out')) |
| 55 | + self.assertEqual(_read(extractor.output), _read(self.input)) |
| 56 | + |
| 57 | + def test_split_mode_headers_carry_the_fields(self) -> None: |
| 58 | + extractor = _extract(self.input, os.path.join(self.tmp.name, 'split'), files=True) |
| 59 | + header = _read(self.input)[:24] |
| 60 | + self.assertEqual(_read(os.path.join(extractor.output, 'Global Header.pcap')), header) |
| 61 | + self.assertGreater(extractor.length, 0) |
| 62 | + for num in range(1, extractor.length + 1): |
| 63 | + self.assertEqual(_read(os.path.join(extractor.output, f'Frame {num}.pcap'))[:24], header) |
| 64 | + |
| 65 | + def test_zero_frame_capture(self) -> None: |
| 66 | + empty = os.path.join(self.tmp.name, 'empty.pcap') |
| 67 | + with open(empty, 'wb') as file: |
| 68 | + file.write(_read(self.input)[:24]) |
| 69 | + extractor = _extract(empty, os.path.join(self.tmp.name, 'empty-out')) |
| 70 | + self.assertEqual(_read(extractor.output), _read(empty)) |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == '__main__': |
| 74 | + unittest.main() |
0 commit comments