|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""GitHub issue #1559: a next-layer hit is resolved, not memoised. |
| 3 | +
|
| 4 | +:meth:`ProtocolBase._lookup_next_layer |
| 5 | +<pcapkit.protocols.protocol.ProtocolBase._lookup_next_layer>` used to write the |
| 6 | +class a registered :class:`~pcapkit.corekit.module.ModuleDescriptor` resolved to |
| 7 | +back over that descriptor. Three consequences, one test each: |
| 8 | +
|
| 9 | +* after :func:`importlib.reload` of the protocol's module, dispatch kept reaching |
| 10 | + the pre-reload class, so a parsed layer failed :func:`isinstance` against the |
| 11 | + live one -- what the method's own docstring said a memo would do; |
| 12 | +* parsing mutated registries shared by the whole process, so what a registry |
| 13 | + held depended on what had been parsed before; |
| 14 | +* so whether re-registering a shipped code warned depended on it too. |
| 15 | +
|
| 16 | +Every lookup now resolves through :attr:`ModuleDescriptor.klass |
| 17 | +<pcapkit.corekit.module.ModuleDescriptor.klass>`, which reads |
| 18 | +:data:`sys.modules`. Each test reloads or parses on a private import, under |
| 19 | +:func:`~tests._support.isolate_modules`, so neither a reloaded module nor a |
| 20 | +mutated registry outlives it. |
| 21 | +
|
| 22 | +""" |
| 23 | + |
| 24 | +import importlib |
| 25 | +import struct |
| 26 | +import sys |
| 27 | +import unittest |
| 28 | +import warnings |
| 29 | +from unittest import mock |
| 30 | + |
| 31 | +from tests._support import isolate_modules |
| 32 | + |
| 33 | +#: Module of the class the reload tests dispatch to. |
| 34 | +UDP_MODULE = 'pcapkit.protocols.transport.udp' |
| 35 | + |
| 36 | +#: An HTTP request, so that a TCP segment to port 80 dispatches once more. |
| 37 | +HTTP_REQUEST = b'GET / HTTP/1.1\r\nHost: x\r\n\r\n' |
| 38 | + |
| 39 | + |
| 40 | +def _ipv4(proto: 'int', body: 'bytes') -> 'bytes': |
| 41 | + """An IPv4 packet carrying ``body`` as next layer ``proto``.""" |
| 42 | + return struct.pack('!BBHHHBBH4s4s', 0x45, 0, 20 + len(body), 0, 0x4000, 64, proto, 0, |
| 43 | + bytes([10, 0, 0, 1]), bytes([10, 0, 0, 2])) + body |
| 44 | + |
| 45 | + |
| 46 | +def _udp(body: 'bytes') -> 'bytes': |
| 47 | + """A UDP datagram between two ports no registry dispatches on.""" |
| 48 | + return struct.pack('!HHHH', 12345, 9999, 8 + len(body), 0) + body |
| 49 | + |
| 50 | + |
| 51 | +def _tcp(dstport: 'int', body: 'bytes') -> 'bytes': |
| 52 | + """A TCP segment, PSH/ACK, to ``dstport``.""" |
| 53 | + return struct.pack('!HHIIBBHHH', 12345, dstport, 1, 0, 5 << 4, 0x18, 65535, 0, 0) + body |
| 54 | + |
| 55 | + |
| 56 | +class NextLayerHitNotMemoisedTests(unittest.TestCase): |
| 57 | + def setUp(self) -> None: |
| 58 | + isolate_modules(self) |
| 59 | + |
| 60 | + def _parse_ipv4_udp(self) -> 'object': |
| 61 | + """Parse an IPv4/UDP packet and return its UDP layer.""" |
| 62 | + from pcapkit.protocols.internet.ipv4 import IPv4 |
| 63 | + |
| 64 | + packet = _ipv4(17, _udp(b'hello')) |
| 65 | + return IPv4(packet, len(packet)).payload |
| 66 | + |
| 67 | + def test_a_reload_after_a_hit_dispatches_to_the_reloaded_class(self) -> None: |
| 68 | + """The issue's reproduction, and the same through a real parse.""" |
| 69 | + from pcapkit.const.reg.transtype import TransType |
| 70 | + from pcapkit.protocols.internet.internet import Internet |
| 71 | + from pcapkit.protocols.protocol import ProtocolBase |
| 72 | + |
| 73 | + registry = Internet.__proto__ |
| 74 | + module = importlib.import_module(UDP_MODULE) |
| 75 | + before = ProtocolBase._lookup_next_layer(registry, TransType.UDP) |
| 76 | + self.assertIs(before, module.UDP) |
| 77 | + self.assertIs(type(self._parse_ipv4_udp()), module.UDP) |
| 78 | + |
| 79 | + with warnings.catch_warnings(): |
| 80 | + warnings.simplefilter('ignore') |
| 81 | + importlib.reload(module) |
| 82 | + self.assertIsNot(module.UDP, before) # the reload really minted a class |
| 83 | + |
| 84 | + self.assertIs(ProtocolBase._lookup_next_layer(registry, TransType.UDP), module.UDP) |
| 85 | + self.assertIsInstance(self._parse_ipv4_udp(), module.UDP) |
| 86 | + |
| 87 | + # and popping the module, which replaces the module object outright |
| 88 | + reloaded = module.UDP |
| 89 | + sys.modules.pop(UDP_MODULE) |
| 90 | + fresh = importlib.import_module(UDP_MODULE) |
| 91 | + self.assertIsNot(fresh.UDP, reloaded) |
| 92 | + self.assertIs(ProtocolBase._lookup_next_layer(registry, TransType.UDP), fresh.UDP) |
| 93 | + |
| 94 | + def test_a_parse_leaves_the_shared_registries_as_declared(self) -> None: |
| 95 | + """Ethernet/IPv4/TCP/HTTP hits each registry once; none of them changes.""" |
| 96 | + from pcapkit.const.reg.ethertype import EtherType |
| 97 | + from pcapkit.const.reg.transtype import TransType |
| 98 | + from pcapkit.corekit.module import ModuleDescriptor |
| 99 | + from pcapkit.protocols.application.http import HTTP |
| 100 | + from pcapkit.protocols.internet.internet import Internet |
| 101 | + from pcapkit.protocols.link.ethernet import Ethernet |
| 102 | + from pcapkit.protocols.link.link import Link |
| 103 | + from pcapkit.protocols.transport.tcp import TCP |
| 104 | + |
| 105 | + hits = ( |
| 106 | + ('Link', Link.__proto__, EtherType.Internet_Protocol_version_4), |
| 107 | + ('Internet', Internet.__proto__, TransType.TCP), |
| 108 | + ('TCP', TCP.__proto__, 80), |
| 109 | + ) |
| 110 | + before = {name: dict(registry) for name, registry, _ in hits} |
| 111 | + for name, registry, code in hits: |
| 112 | + with self.subTest(registry=name, when='before'): |
| 113 | + self.assertIsInstance(registry[code], ModuleDescriptor) |
| 114 | + |
| 115 | + frame = bytes(6) + bytes([2, 0, 0, 0, 0, 1]) + b'\x08\x00' + _ipv4(6, _tcp(80, HTTP_REQUEST)) |
| 116 | + parsed = Ethernet(frame, len(frame)) |
| 117 | + self.assertIsInstance(parsed.payload.payload.payload, HTTP) # every hit really dispatched |
| 118 | + |
| 119 | + for name, registry, code in hits: |
| 120 | + with self.subTest(registry=name, when='after'): |
| 121 | + self.assertIsInstance(registry[code], ModuleDescriptor) |
| 122 | + self.assertEqual(dict(registry), before[name]) |
| 123 | + |
| 124 | + def test_registering_a_dispatched_code_warns_alike_before_and_after_a_parse(self) -> None: |
| 125 | + """Whether ``register`` warns no longer depends on what was parsed. |
| 126 | +
|
| 127 | + ``register`` warns when it displaces an incumbent other than the class |
| 128 | + it is handed, and a shipped descriptor counts as different from the |
| 129 | + class it names. While a hit wrote the class back, re-registering UDP |
| 130 | + warned before any parse and was silent after one had reached UDP. |
| 131 | +
|
| 132 | + """ |
| 133 | + from pcapkit.const.reg.transtype import TransType |
| 134 | + from pcapkit.protocols.internet.internet import Internet |
| 135 | + from pcapkit.protocols.transport.udp import UDP |
| 136 | + |
| 137 | + def overwrites() -> 'list[str]': |
| 138 | + with mock.patch.dict(Internet.__proto__), warnings.catch_warnings(record=True) as caught: |
| 139 | + warnings.simplefilter('always') |
| 140 | + Internet.register(TransType.UDP, UDP) |
| 141 | + return [str(record.message) for record in caught |
| 142 | + if 'already registered' in str(record.message)] |
| 143 | + |
| 144 | + before = overwrites() |
| 145 | + self.assertEqual(len(before), 1) |
| 146 | + self.assertIs(type(self._parse_ipv4_udp()), UDP) |
| 147 | + self.assertEqual(overwrites(), before) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == '__main__': |
| 151 | + unittest.main() |
0 commit comments