|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""L2TPv2 keeps its reserved flag bits, and never drops a lone Ns or Nr. |
| 3 | +
|
| 4 | +GitHub issue #1207: the flags :class:`~pcapkit.corekit.fields.strings.BitField` |
| 5 | +declared no reserved subfields, so bits 2, 3, 5 and 8-11 were lost on a |
| 6 | +``from_data`` rebuild: ``200200010002ff03002145000000`` came back as |
| 7 | +``000200010002ff03002145000000``. |
| 8 | +
|
| 9 | +GitHub issue #1208: :meth:`~pcapkit.protocols.link.l2tpv2.L2TPv2.make` set ``S`` |
| 10 | +only when both ``ns`` and ``nr`` were given, so ``L2TPv2(ns=7)`` silently wrote |
| 11 | +neither. :rfc:`2661` §3.1 has ``S`` mark the presence of both fields together, |
| 12 | +so a lone value is now refused with |
| 13 | +:class:`~pcapkit.utilities.exceptions.ProtocolError`. |
| 14 | +
|
| 15 | +Every case builds its own octets in memory and reads no capture. |
| 16 | +
|
| 17 | +:class:`L2TPv2` is imported inside each test, after |
| 18 | +:func:`~tests._support.reimport_once_per_class`, not at module load, for the |
| 19 | +reason :mod:`tests.protocols.link.test_ethernet_mac_roundtrip_unit` gives. |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | +import unittest |
| 24 | + |
| 25 | +from tests._support import reimport_once_per_class |
| 26 | + |
| 27 | +#: A PPP frame header and the first IPv4 octet, as the L2TPv2 payload. |
| 28 | +PAYLOAD = bytes.fromhex('ff03002145') |
| 29 | + |
| 30 | +#: Each reserved bit group on its own, then all of them at once. |
| 31 | +RESERVED_MASKS = (0x2000, 0x1000, 0x0400, 0x00f0, 0x34f0) |
| 32 | + |
| 33 | + |
| 34 | +class TestL2TPv2RoundTrip(unittest.TestCase): |
| 35 | + """Pin the reserved flag bits and the Ns/Nr pairing.""" |
| 36 | + |
| 37 | + def setUp(self) -> None: |
| 38 | + reimport_once_per_class(self) |
| 39 | + |
| 40 | + def test_issue_1207_reproduction(self) -> None: |
| 41 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 42 | + |
| 43 | + raw = bytes.fromhex('200200010002ff03002145000000') |
| 44 | + parsed = L2TPv2(raw) |
| 45 | + self.assertEqual(parsed.info.flags.reserved, 0x2000) |
| 46 | + self.assertEqual(L2TPv2.from_data(parsed.info).data, raw) |
| 47 | + |
| 48 | + def test_each_reserved_mask_round_trips(self) -> None: |
| 49 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 50 | + |
| 51 | + for mask in RESERVED_MASKS: |
| 52 | + raw = (mask | 0x0002).to_bytes(2, 'big') + bytes.fromhex('00010002') + PAYLOAD |
| 53 | + with self.subTest(mask=hex(mask)): |
| 54 | + parsed = L2TPv2(raw) |
| 55 | + self.assertEqual(parsed.info.flags.reserved, mask) |
| 56 | + self.assertEqual(L2TPv2.from_data(parsed.info).data, raw) |
| 57 | + |
| 58 | + def test_reserved_bits_round_trip_inside_udp(self) -> None: |
| 59 | + from pcapkit.protocols.transport.udp import UDP |
| 60 | + |
| 61 | + l2tp = bytes.fromhex('34f200010002') + PAYLOAD |
| 62 | + raw = (1701).to_bytes(2, 'big') * 2 + (8 + len(l2tp)).to_bytes(2, 'big') + b'\x00\x00' + l2tp |
| 63 | + parsed = UDP(raw) |
| 64 | + self.assertEqual(parsed.payload.info.flags.reserved, 0x34f0) |
| 65 | + self.assertEqual(UDP.from_data(parsed.info).data, raw) |
| 66 | + |
| 67 | + def test_make_writes_reserved_and_refuses_other_bits(self) -> None: |
| 68 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 69 | + from pcapkit.utilities.exceptions import ProtocolError |
| 70 | + |
| 71 | + self.assertEqual(L2TPv2(type=0, reserved=0x0400).data[:2], bytes.fromhex('0402')) |
| 72 | + self.assertEqual(L2TPv2(type=0).data[:2], bytes.fromhex('0002')) |
| 73 | + for bad in (0x0001, 0x0800, 0x8000): |
| 74 | + with self.subTest(reserved=hex(bad)): |
| 75 | + with self.assertRaises(ProtocolError): |
| 76 | + L2TPv2(type=0, reserved=bad) |
| 77 | + |
| 78 | + def test_issue_1208_lone_ns_or_nr_is_refused(self) -> None: |
| 79 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 80 | + from pcapkit.utilities.exceptions import ProtocolError |
| 81 | + |
| 82 | + for kwargs in ({'ns': 7}, {'nr': 7}, {'ns': 0}, {'nr': 0}): |
| 83 | + with self.subTest(**kwargs): |
| 84 | + with self.assertRaises(ProtocolError): |
| 85 | + L2TPv2(**kwargs) |
| 86 | + |
| 87 | + def test_ns_and_nr_together_round_trip(self) -> None: |
| 88 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 89 | + |
| 90 | + made = L2TPv2(type=0, ns=7, nr=0) |
| 91 | + self.assertEqual(made.data, bytes.fromhex('08020000000000070000')) |
| 92 | + self.assertEqual((made.info.ns, made.info.nr), (7, 0)) |
| 93 | + self.assertEqual(L2TPv2.from_data(made.info).data, made.data) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + unittest.main() |
0 commit comments