|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""A TCP or UDP schema built directly rejects a port outside ``0``-``65535``. |
| 3 | +
|
| 4 | +GitHub issue #2314. :meth:`~pcapkit.corekit.fields.numbers.NumberField.pre_process` |
| 5 | +masks a value to the field's width, which is the documented schema contract |
| 6 | +(#1488), so ``Schema_UDP(srcport=70000, ...)`` packed ``0x1170`` with no error. |
| 7 | +The owner ruled that the masking stays and that the ports get their range check |
| 8 | +from :class:`~pcapkit.corekit.fields.numbers.PortEnumField`, the field the |
| 9 | +schemas carry them on. These tests pin that an out-of-range port is rejected on |
| 10 | +both ports of both schemas, and that the boundary ports still close byte-exact. |
| 11 | +
|
| 12 | +""" |
| 13 | +import unittest |
| 14 | +from typing import TYPE_CHECKING |
| 15 | + |
| 16 | +from tests._support import reimport_once_per_class |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from typing import Any |
| 20 | + |
| 21 | +#: Test tier (#1958): a hand-picked round trip over the ``int`` boundaries of a |
| 22 | +#: two-octet field. |
| 23 | +__tier__ = 'ALL' |
| 24 | + |
| 25 | +#: Ports the issue measured masking, one past each end and well past the top. |
| 26 | +OUT_OF_RANGE = (-1, 65536, 70000) |
| 27 | + |
| 28 | +#: The two ends of a two-octet port. |
| 29 | +BOUNDARIES = (0, 65535) |
| 30 | + |
| 31 | + |
| 32 | +class SchemaPortRangeTests(unittest.TestCase): |
| 33 | + """Out-of-range ports, and the boundary round trip, on the TCP and UDP schemas.""" |
| 34 | + |
| 35 | + def setUp(self) -> None: |
| 36 | + reimport_once_per_class(self) |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def _udp(**ports: 'int') -> 'Any': |
| 40 | + from pcapkit.protocols.schema.transport.udp import UDP |
| 41 | + |
| 42 | + values = {'srcport': 1024, 'dstport': 53, 'len': 8, 'checksum': b'\x00\x00', 'payload': b''} |
| 43 | + values.update(ports) |
| 44 | + return UDP(**values) |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def _tcp(**ports: 'int') -> 'Any': |
| 48 | + from pcapkit.protocols.schema.transport.tcp import TCP |
| 49 | + |
| 50 | + flags = dict.fromkeys(('cwr', 'ece', 'urg', 'ack', 'psh', 'rst', 'syn', 'fin'), 0) |
| 51 | + values = {'srcport': 1024, 'dstport': 80, 'seq': 0, 'ack': 0, |
| 52 | + 'offset': {'offset': 5, 'reserved': 0, 'ns': 0}, 'flags': flags, |
| 53 | + 'window': 0, 'checksum': b'\x00\x00', 'urgent': 0, 'options': [], 'payload': b''} |
| 54 | + values.update(ports) |
| 55 | + return TCP(**values) |
| 56 | + |
| 57 | + def test_an_out_of_range_port_is_rejected_on_packing(self) -> None: |
| 58 | + from pcapkit.utilities.exceptions import FieldValueError |
| 59 | + |
| 60 | + for name, build in (('UDP', self._udp), ('TCP', self._tcp)): |
| 61 | + for field in ('srcport', 'dstport'): |
| 62 | + for port in OUT_OF_RANGE: |
| 63 | + with self.subTest(schema=name, field=field, port=port): |
| 64 | + with self.assertRaisesRegex(FieldValueError, rf'port {port} is outside 0-65535'): |
| 65 | + build(**{field: port}).pack() |
| 66 | + |
| 67 | + def test_the_boundary_ports_round_trip_byte_exact(self) -> None: |
| 68 | + from pcapkit.protocols.schema.transport.tcp import TCP |
| 69 | + from pcapkit.protocols.schema.transport.udp import UDP |
| 70 | + |
| 71 | + for name, schema, build in (('UDP', UDP, self._udp), ('TCP', TCP, self._tcp)): |
| 72 | + for srcport in BOUNDARIES: |
| 73 | + for dstport in BOUNDARIES: |
| 74 | + with self.subTest(schema=name, srcport=srcport, dstport=dstport): |
| 75 | + octets = build(srcport=srcport, dstport=dstport).pack() |
| 76 | + self.assertEqual(octets[:4], srcport.to_bytes(2, 'big') + dstport.to_bytes(2, 'big')) |
| 77 | + parsed = schema.unpack(octets) |
| 78 | + self.assertEqual((parsed.srcport.port, parsed.dstport.port), (srcport, dstport)) |
| 79 | + self.assertEqual(parsed.pack(), octets) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + unittest.main() |
0 commit comments