|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""GitHub issue #2526: pypcapfile's header check warns nothing for a header it rejects. |
| 3 | +
|
| 4 | +:func:`~pcapkit.toolkit.pypcapfile._default_accepts` asks pcapkit's IPv4 or TCP |
| 5 | +parser about a header, and answers :data:`False` when that raises. The #2308 |
| 6 | +rule applies: the trial's warnings describe a header nothing keeps, so they go |
| 7 | +with it. Held by nothing, a TCP header whose Timestamps option runs past its end |
| 8 | +warns ``packet length < 0`` twice before the answer. A header the parser accepts |
| 9 | +warns from the caller's line. |
| 10 | +
|
| 11 | +The parser reads its own header rather than a next layer, so setting |
| 12 | +``_beholding`` around it holds nothing, and the trial holds its own warnings. |
| 13 | +
|
| 14 | +The answers are cached by octets, so each test clears the cache before it asks. |
| 15 | +:mod:`pcapkit` is imported inside each test, after |
| 16 | +:func:`~tests._support.reimport_once_per_class`. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import inspect |
| 21 | +import struct |
| 22 | +import unittest |
| 23 | +import warnings |
| 24 | + |
| 25 | +from tests._support import reimport_once_per_class |
| 26 | +from tests.utilities._harness import capture |
| 27 | + |
| 28 | +#: Test tier (#1958): exercises the warning machinery, which free-threaded |
| 29 | +#: builds make context-aware. |
| 30 | +__tier__ = 'ALL' |
| 31 | + |
| 32 | + |
| 33 | +def _ipv4(options: 'bytes') -> 'bytes': |
| 34 | + """A bare IPv4 header carrying ``options``.""" |
| 35 | + return struct.pack('!BBHHHBBH4s4s', 0x45 + len(options) // 4, 0, 20 + len(options), |
| 36 | + 0, 0, 64, 6, 0, bytes([127, 0, 0, 1]), bytes([127, 0, 0, 2])) + options |
| 37 | + |
| 38 | + |
| 39 | +#: A 24-octet TCP header whose Timestamps option claims 10 octets of the 4 left. |
| 40 | +TCP_TIMESTAMPS_CUT = (struct.pack('!HHIIBBHHH', 12345, 80, 1, 0, 6 << 4, 0x02, 4095, 0, 0) |
| 41 | + + bytes.fromhex('080a0000')) |
| 42 | +#: A 24-octet IPv4 header whose Record Route option claims 40 octets of the 4 left. |
| 43 | +IPV4_RECORD_ROUTE_CUT = _ipv4(bytes.fromhex('07280400')) |
| 44 | +#: A 28-octet IPv4 header whose Security option runs on past its last octet. |
| 45 | +IPV4_SECURITY = _ipv4(bytes.fromhex('8205ab0000') + bytes(3)) |
| 46 | +#: The warning :data:`IPV4_SECURITY`'s option reports. |
| 47 | +SECURITY_WARNING = 'IPv4: [OptNo 130] invalid format: remaining data' |
| 48 | + |
| 49 | + |
| 50 | +class TestAcceptsTrial(unittest.TestCase): |
| 51 | + """Pin that a rejected header reports nothing, and an accepted one does.""" |
| 52 | + |
| 53 | + def setUp(self) -> None: |
| 54 | + reimport_once_per_class(self) |
| 55 | + |
| 56 | + def _ask(self, name: 'str', header: 'bytes', |
| 57 | + action: 'str' = 'always') -> 'tuple[bool, list[warnings.WarningMessage]]': |
| 58 | + """Ask the uncached check about ``header``, with the warnings it reports.""" |
| 59 | + from pcapkit.toolkit.pypcapfile import _default_accepts |
| 60 | + |
| 61 | + _default_accepts.cache_clear() |
| 62 | + with warnings.catch_warnings(record=True) as caught: |
| 63 | + warnings.simplefilter(action) |
| 64 | + answer = _default_accepts(name, header) |
| 65 | + return answer, caught |
| 66 | + |
| 67 | + def test_rejected_header_warns_nothing(self) -> None: |
| 68 | + """A TCP or IPv4 header whose option runs past its end is rejected, reporting nothing on either channel.""" |
| 69 | + from pcapkit.utilities.logging import logger |
| 70 | + |
| 71 | + for name, header in (('TCP', TCP_TIMESTAMPS_CUT), ('IPv4', IPV4_RECORD_ROUTE_CUT)): |
| 72 | + with self.subTest(name=name): |
| 73 | + with capture(logger) as recorder: |
| 74 | + answer, caught = self._ask(name, header) |
| 75 | + self.assertFalse(answer) |
| 76 | + self.assertEqual([str(item.message) for item in caught], []) |
| 77 | + self.assertEqual([record.getMessage() for record in recorder.records |
| 78 | + if record.getMessage().startswith('packet length')], []) |
| 79 | + |
| 80 | + def test_error_filter_rejects_a_warning_header(self) -> None: |
| 81 | + """Control: under ``error``, a header that warns is rejected, and nothing escapes.""" |
| 82 | + for name, header in (('TCP', TCP_TIMESTAMPS_CUT), ('IPv4', IPV4_SECURITY)): |
| 83 | + with self.subTest(name=name): |
| 84 | + answer, caught = self._ask(name, header, 'error') |
| 85 | + self.assertFalse(answer) |
| 86 | + self.assertEqual(caught, []) |
| 87 | + |
| 88 | + def test_accepted_header_warns_from_the_callers_line(self) -> None: |
| 89 | + """Control: an IPv4 header the parser accepts reports its option's warning.""" |
| 90 | + from pcapkit.toolkit.pypcapfile import _default_accepts |
| 91 | + from pcapkit.utilities.warnings import ProtocolWarning |
| 92 | + |
| 93 | + _default_accepts.cache_clear() |
| 94 | + with warnings.catch_warnings(record=True) as caught: |
| 95 | + warnings.simplefilter('always') |
| 96 | + line = inspect.currentframe().f_lineno + 1 # type: ignore[union-attr] |
| 97 | + answer = _default_accepts('IPv4', IPV4_SECURITY) |
| 98 | + self.assertTrue(answer) |
| 99 | + self.assertEqual([(item.category, str(item.message), item.filename, item.lineno) for item in caught], |
| 100 | + [(ProtocolWarning, SECURITY_WARNING, __file__, line)]) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + unittest.main() |
0 commit comments