|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""L2TPv2 keeps the ``L`` flag and the Length field through ``from_data``. |
| 3 | +
|
| 4 | +GitHub issue #1172: :meth:`L2TPv2._make_data |
| 5 | +<pcapkit.protocols.link.l2tpv2.L2TPv2._make_data>` returned the parsed Length |
| 6 | +under ``length``, which is the parse length |
| 7 | +:class:`~pcapkit.protocols.protocol.ProtocolBase` takes for itself, so it never |
| 8 | +reached :meth:`~pcapkit.protocols.link.l2tpv2.L2TPv2.make`. The rebuild then |
| 9 | +cleared ``L`` and omitted the two Length octets: ``c802000c0001000000010002`` |
| 10 | +came back as ``88020001000000010002``. |
| 11 | +
|
| 12 | +Every case builds its own octets in memory and reads no capture. |
| 13 | +
|
| 14 | +:class:`L2TPv2` is imported inside each test, after |
| 15 | +:func:`~tests._support.reimport_once_per_class`, not at module load, for the |
| 16 | +reason :mod:`tests.protocols.link.test_ethernet_mac_roundtrip_unit` gives. |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +import io |
| 21 | +import itertools |
| 22 | +import unittest |
| 23 | + |
| 24 | +from tests._support import reimport_once_per_class |
| 25 | + |
| 26 | +#: A PPP frame header, as the L2TPv2 payload. |
| 27 | +PAYLOAD = bytes.fromhex('ff030021') |
| 28 | + |
| 29 | + |
| 30 | +def build(type_: int, len_: int, seq: int, offset: int, pad: int) -> bytes: |
| 31 | + """Build an L2TPv2 datagram with the given optional fields present.""" |
| 32 | + flags = (type_ << 15) | (len_ << 14) | (seq << 11) | (offset << 9) | 2 |
| 33 | + hdr_len = 6 + 2 * (len_ + 2 * seq + offset) + pad |
| 34 | + body = b'' |
| 35 | + if len_: |
| 36 | + body += (hdr_len + len(PAYLOAD)).to_bytes(2, 'big') |
| 37 | + body += (7).to_bytes(2, 'big') + (9).to_bytes(2, 'big') |
| 38 | + if seq: |
| 39 | + body += (3).to_bytes(2, 'big') + (4).to_bytes(2, 'big') |
| 40 | + if offset: |
| 41 | + body += pad.to_bytes(2, 'big') + bytes(pad) |
| 42 | + return flags.to_bytes(2, 'big') + body + PAYLOAD |
| 43 | + |
| 44 | + |
| 45 | +#: Every combination of ``T``, ``L``, ``S`` and ``O``; with ``O``, both an |
| 46 | +#: empty and a three-octet (zero) offset pad. |
| 47 | +COMBINATIONS = [ |
| 48 | + (t, l, s, o, pad) |
| 49 | + for t, l, s, o in itertools.product((0, 1), repeat=4) |
| 50 | + for pad in ((0, 3) if o else (0,)) |
| 51 | +] |
| 52 | + |
| 53 | + |
| 54 | +class TestL2TPv2LengthRoundTrip(unittest.TestCase): |
| 55 | + """Pin that the Length field survives a ``from_data`` rebuild.""" |
| 56 | + |
| 57 | + def setUp(self) -> None: |
| 58 | + reimport_once_per_class(self) |
| 59 | + |
| 60 | + def test_issue_reproduction(self) -> None: |
| 61 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 62 | + |
| 63 | + raw = bytes.fromhex('c802000c0001000000010002') # ZLB ACK: T, L and S set |
| 64 | + parsed = L2TPv2(raw) |
| 65 | + self.assertTrue(parsed.info.flags.len) |
| 66 | + self.assertEqual(parsed.info.length, 12) |
| 67 | + self.assertEqual(L2TPv2.from_data(parsed.info).data, raw) |
| 68 | + |
| 69 | + def test_every_flag_combination_round_trips(self) -> None: |
| 70 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 71 | + |
| 72 | + self.assertEqual(len(COMBINATIONS), 24) |
| 73 | + for t, l, s, o, pad in COMBINATIONS: |
| 74 | + raw = build(t, l, s, o, pad) |
| 75 | + with self.subTest(T=t, L=l, S=s, O=o, pad=pad, raw=raw.hex()): |
| 76 | + parsed = L2TPv2(io.BytesIO(raw), len(raw)) |
| 77 | + self.assertEqual(L2TPv2.from_data(parsed.info).data, raw) |
| 78 | + |
| 79 | + def test_make_data_passes_the_length_as_total_length(self) -> None: |
| 80 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 81 | + |
| 82 | + values = L2TPv2._make_data(L2TPv2(build(1, 1, 1, 0, 0)).info) |
| 83 | + self.assertNotIn('length', values) |
| 84 | + self.assertEqual(values['total_length'], 16) |
| 85 | + |
| 86 | + values = L2TPv2._make_data(L2TPv2(build(0, 0, 0, 0, 0)).info) |
| 87 | + self.assertIsNone(values['total_length']) |
| 88 | + |
| 89 | + def test_make_sets_l_flag_only_with_total_length(self) -> None: |
| 90 | + from pcapkit.protocols.link.l2tpv2 import L2TPv2 |
| 91 | + |
| 92 | + # ``type`` is passed as the raw ``T`` bit so the octets do not depend on |
| 93 | + # how :class:`~pcapkit.const.l2tp.type.Type` names it. |
| 94 | + with_len = L2TPv2(type=0, tunnel_id=7, session_id=9, total_length=12, |
| 95 | + payload=PAYLOAD) |
| 96 | + self.assertEqual(with_len.data, build(0, 1, 0, 0, 0)) |
| 97 | + self.assertTrue(with_len.info.flags.len) |
| 98 | + self.assertEqual(with_len.info.length, 12) |
| 99 | + |
| 100 | + without = L2TPv2(type=0, tunnel_id=7, session_id=9, payload=PAYLOAD) |
| 101 | + self.assertEqual(without.data, build(0, 0, 0, 0, 0)) |
| 102 | + self.assertFalse(without.info.flags.len) |
| 103 | + self.assertIsNone(without.info.length) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + unittest.main() |
0 commit comments