|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""An IPX payload is dissected by IPX Packet Type, not as an IP protocol number. |
| 3 | +
|
| 4 | +GitHub issue #2169: :meth:`IPX.read <pcapkit.protocols.internet.ipx.IPX.read>` |
| 5 | +handed the IPX Packet Type to the next-layer lookup it inherits from |
| 6 | +:class:`~pcapkit.protocols.internet.internet.Internet`, whose registry is keyed |
| 7 | +by IP protocol number. So Packet Type 17 (NCP) parsed as UDP, 4 (PEP) as IPv4 |
| 8 | +and 6 as TCP; and Packet Type 0 counted as an IPv6 Hop-by-Hop Options header |
| 9 | +towards :data:`~pcapkit.utilities.config.EXTENSION_HEADER_LIMIT`. |
| 10 | +
|
| 11 | +:class:`~pcapkit.protocols.internet.ipx.IPX` keeps a registry of its own, keyed |
| 12 | +by Packet Type, and its payload is Raw unless an IPX-native protocol is |
| 13 | +registered there. |
| 14 | +
|
| 15 | +Every case builds its own octets in memory and reads no capture. Classes are |
| 16 | +imported inside each test, after :func:`~tests._support.reimport_once_per_class`. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import io |
| 21 | +import unittest |
| 22 | +import warnings |
| 23 | +from unittest import mock |
| 24 | + |
| 25 | +from tests._support import reimport_once_per_class |
| 26 | + |
| 27 | +#: Destination and source IPX addresses. |
| 28 | +ADDRS = bytes(range(1, 25)) |
| 29 | + |
| 30 | +#: An IPv4 header carrying nothing, which parses as IPv4 when looked up by IP protocol number 4. |
| 31 | +IPV4 = bytes.fromhex('4500' '0014' '0000' '0000' '4011' '0000' '0a000001' '0a000002') |
| 32 | +#: A TCP header carrying nothing, which parses as TCP when looked up by IP protocol number 6. |
| 33 | +TCP = bytes.fromhex('0050' '1f90' '00000001' '00000000' '5002' 'ffff' '0000' '0000') |
| 34 | +#: An 8-octet payload, which parses as UDP when looked up by IP protocol number 17. |
| 35 | +UDP = bytes.fromhex('0035' '0035' '0008' '0000') |
| 36 | + |
| 37 | + |
| 38 | +def _ipx(packet_type: int, payload: bytes) -> bytes: |
| 39 | + """Return an IPX packet of ``packet_type`` carrying ``payload``.""" |
| 40 | + length = 30 + len(payload) |
| 41 | + return b'\xff\xff' + length.to_bytes(2, 'big') + b'\x00' + bytes([packet_type]) + ADDRS + payload |
| 42 | + |
| 43 | + |
| 44 | +#: Packet Types that share a number with a dissected IP protocol, and a payload |
| 45 | +#: that protocol would parse. |
| 46 | +PACKETS = { |
| 47 | + 'NCP-17': _ipx(17, UDP), |
| 48 | + 'PEP-4': _ipx(4, IPV4), |
| 49 | + 'type-6': _ipx(6, TCP), |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +class TestIPXNextLayer(unittest.TestCase): |
| 54 | + """Pin the IPX next-layer dispatch to its own Packet Type registry.""" |
| 55 | + |
| 56 | + def setUp(self) -> None: |
| 57 | + reimport_once_per_class(self) |
| 58 | + |
| 59 | + def test_payload_is_raw_and_rebuilds_byte_for_byte(self) -> None: |
| 60 | + from pcapkit.protocols.internet.ipx import IPX |
| 61 | + from pcapkit.protocols.misc.raw import Raw |
| 62 | + |
| 63 | + for name, packet in PACKETS.items(): |
| 64 | + with self.subTest(packet=name), warnings.catch_warnings(): |
| 65 | + warnings.simplefilter('ignore') |
| 66 | + parsed = IPX(io.BytesIO(packet), len(packet)) |
| 67 | + self.assertIs(type(parsed.payload), Raw) |
| 68 | + self.assertEqual(IPX.from_data(parsed.info).data, packet) |
| 69 | + |
| 70 | + def test_registered_ipx_protocol_is_dissected(self) -> None: |
| 71 | + from pcapkit.const.ipx.packet import Packet |
| 72 | + from pcapkit.protocols.internet.internet import Internet |
| 73 | + from pcapkit.protocols.internet.ipx import IPX |
| 74 | + from pcapkit.protocols.transport.udp import UDP as Proto_UDP |
| 75 | + |
| 76 | + self.assertIsNot(IPX.__proto__, Internet.__proto__) |
| 77 | + packet = PACKETS['NCP-17'] |
| 78 | + with mock.patch.dict(IPX.__proto__, {Packet.NCP: Proto_UDP}), warnings.catch_warnings(): |
| 79 | + warnings.simplefilter('ignore') |
| 80 | + parsed = IPX(io.BytesIO(packet), len(packet)) |
| 81 | + self.assertIs(type(parsed.payload), Proto_UDP) |
| 82 | + |
| 83 | + def test_packet_type_zero_is_no_extension_header(self) -> None: |
| 84 | + from pcapkit.protocols.internet.ipx import IPX |
| 85 | + from pcapkit.protocols.misc.raw import Raw |
| 86 | + from pcapkit.utilities import config |
| 87 | + from pcapkit.utilities.warnings import ProtocolWarning |
| 88 | + |
| 89 | + packet = _ipx(0, b'abcd') |
| 90 | + with mock.patch.object(config, 'EXTENSION_HEADER_LIMIT', 0), warnings.catch_warnings(record=True) as caught: |
| 91 | + warnings.simplefilter('always') |
| 92 | + parsed = IPX(io.BytesIO(packet), len(packet)) |
| 93 | + self.assertIs(type(parsed.payload), Raw) |
| 94 | + self.assertIsNone(parsed.payload.info.error) |
| 95 | + self.assertEqual([w for w in caught if issubclass(w.category, ProtocolWarning)], []) |
| 96 | + |
| 97 | + def test_protocol_is_the_packet_type(self) -> None: |
| 98 | + from pcapkit.const.ipx.packet import Packet |
| 99 | + from pcapkit.protocols.internet.ipx import IPX |
| 100 | + |
| 101 | + packet = PACKETS['NCP-17'] |
| 102 | + with warnings.catch_warnings(): |
| 103 | + warnings.simplefilter('ignore') |
| 104 | + parsed = IPX(io.BytesIO(packet), len(packet)) |
| 105 | + self.assertIs(parsed.protocol, Packet.NCP) |
| 106 | + self.assertEqual(IPX.protocol.fget.__annotations__['return'], 'Enum_Packet') |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments