|
| 1 | +"""#1098 -- the TCP NS flag round-trips through read, ``make()`` and ``from_data``. |
| 2 | +
|
| 3 | +:meth:`TCP.make <pcapkit.protocols.transport.tcp.TCP.make>` accepts ``ns`` and |
| 4 | +packs it into bit 103 (the low bit of byte 12), and |
| 5 | +:class:`~pcapkit.protocols.schema.transport.tcp.OffsetFlag` declares the bit. The |
| 6 | +reader and :meth:`TCP._make_data <pcapkit.protocols.transport.tcp.TCP._make_data>` |
| 7 | +must carry it too, so a parsed segment exposes ``flags.ns`` and a |
| 8 | +:meth:`~pcapkit.protocols.protocol.ProtocolBase.from_data` rebuild keeps the bit |
| 9 | +on the wire rather than clearing it silently. |
| 10 | +
|
| 11 | +""" |
| 12 | +from __future__ import annotations |
| 13 | + |
| 14 | +import importlib.util |
| 15 | +import unittest |
| 16 | + |
| 17 | +from tests._support import reimport_once_per_class |
| 18 | + |
| 19 | +RUNTIME_DEPS = ('tbtrim', 'aenum', 'chardet', 'dictdumper') |
| 20 | +HAS_RUNTIME = all(importlib.util.find_spec(name) is not None for name in RUNTIME_DEPS) |
| 21 | + |
| 22 | + |
| 23 | +@unittest.skipUnless(HAS_RUNTIME, 'runtime dependencies not installed') |
| 24 | +class TCPNSFlagRoundTripUnitTests(unittest.TestCase): |
| 25 | + def setUp(self) -> None: |
| 26 | + reimport_once_per_class(self) |
| 27 | + |
| 28 | + def test_make_sets_ns_on_the_wire(self) -> None: |
| 29 | + """``make(ns=True)`` sets the low bit of byte 12, next to a data offset of 5.""" |
| 30 | + from pcapkit.protocols.transport.tcp import TCP |
| 31 | + |
| 32 | + self.assertEqual(TCP(srcport=1, dstport=2, ns=True).data[12], 0x51) |
| 33 | + self.assertEqual(TCP(srcport=1, dstport=2, ns=False).data[12], 0x50) |
| 34 | + |
| 35 | + def test_read_exposes_ns(self) -> None: |
| 36 | + """A parsed segment reports ``flags.ns`` as read from bit 103.""" |
| 37 | + from pcapkit.protocols.transport.tcp import TCP |
| 38 | + |
| 39 | + for ns in (True, False): |
| 40 | + with self.subTest(ns=ns): |
| 41 | + raw = TCP(srcport=1, dstport=2, ns=ns).data |
| 42 | + self.assertIs(TCP(raw).info.flags.ns, ns) |
| 43 | + |
| 44 | + def test_from_data_keeps_ns(self) -> None: |
| 45 | + """A ``from_data`` rebuild of a parsed segment reproduces its bytes, NS included.""" |
| 46 | + from pcapkit.protocols.transport.tcp import TCP |
| 47 | + |
| 48 | + for ns in (True, False): |
| 49 | + with self.subTest(ns=ns): |
| 50 | + raw = TCP(srcport=1, dstport=2, ns=ns, ack=True).data |
| 51 | + rebuilt = TCP.from_data(TCP(raw).info) |
| 52 | + self.assertEqual(rebuilt.data, raw) |
| 53 | + self.assertIs(rebuilt.info.flags.ns, ns) |
| 54 | + |
| 55 | + def test_flags_field_order(self) -> None: |
| 56 | + """``ns`` leads the flags, in wire order, ahead of ``cwr``.""" |
| 57 | + from pcapkit.protocols.transport.tcp import TCP |
| 58 | + |
| 59 | + flags = TCP(TCP(srcport=1, dstport=2).data).info.flags |
| 60 | + self.assertEqual(list(flags.keys()), |
| 61 | + ['ns', 'cwr', 'ece', 'urg', 'ack', 'psh', 'rst', 'syn', 'fin']) |
| 62 | + |
| 63 | + |
| 64 | +if __name__ == '__main__': |
| 65 | + unittest.main() |
0 commit comments