Skip to content

Commit 19c4677

Browse files
committed
[red] expose invalid IDs in valid host PUT transfers
Signed-off-by: Viwat Vchirawongkwin <viwat.v@chula.ac.th>
1 parent 2a3b537 commit 19c4677

1 file changed

Lines changed: 215 additions & 0 deletions

File tree

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: MIT
3+
# Part of PyBLE (https://pyble.dev) — see /LICENSE.
4+
"""Actual host senders plus unchanged compiled native wire admission; no HIL.
5+
6+
Synthetic ACKs test sender orchestration only. Every captured valid CMD is
7+
also admitted by the production C decision, so a permissive fake cannot hide
8+
zero-ID frames. The raw codec remains available for deliberate negative tests.
9+
"""
10+
import ast
11+
from pathlib import Path
12+
import sys
13+
from types import SimpleNamespace
14+
import unittest
15+
from unittest import mock
16+
17+
HERE = Path(__file__).resolve().parent
18+
HIL = HERE.parent / "hil"
19+
sys.path.insert(0, str(HERE))
20+
sys.path.insert(0, str(HIL))
21+
import test_v061_pble_wire_native as native_fixture
22+
import _pble_bench as shared
23+
import _pble_wire as wire
24+
import f11_reliability_bench as reliability
25+
import file_roundtrip_bench as roundtrip
26+
import rp2_run_stop as rp2
27+
import target_run_stop as target
28+
29+
30+
class Clock:
31+
def __init__(self):
32+
self.value = 1.0
33+
34+
def now(self):
35+
return self.value
36+
37+
def ns(self):
38+
self.value += 0.001
39+
return int(self.value * 1_000_000_000)
40+
41+
async def sleep(self, seconds):
42+
self.value += max(0.05, seconds)
43+
44+
45+
class StrictCentral:
46+
def __init__(self, test, data, *, drop_first=False):
47+
self.test, self.data = test, data
48+
self.drop_first = drop_first
49+
self.dropped = False
50+
self.last_ack = 0
51+
self.calls, self.frames, self.offsets = [], [], []
52+
self.events = []
53+
self.chunk = 229
54+
55+
def capture(self, opcode, request_id, payload, response):
56+
self.test.assertIs(type(request_id), int, "caller must allocate an integer CMD ID")
57+
self.test.assertTrue(1 <= request_id <= 255, "valid CMD ID must be 1..255 before encoding")
58+
raw = wire.encode(wire.CMD, opcode, request_id, payload)
59+
self.test.assertEqual(raw[3], request_id)
60+
self.calls.append((opcode, request_id, bytes(payload), response))
61+
self.frames.append(raw)
62+
63+
async def send_cmd(self, opcode, request_id, payload=b"", **kwargs):
64+
self.test.assertNotEqual(opcode, wire.OP_FILE_PUT_DATA, "DATA must never wait for a RSP")
65+
self.capture(opcode, request_id, payload, True)
66+
result = b"\x00"
67+
if opcode == wire.OP_FILE_PUT_BEGIN:
68+
self.test.assertEqual(int.from_bytes(payload[:4], "little"), len(self.data))
69+
self.test.assertEqual(int.from_bytes(payload[4:8], "little"), wire.crc32(self.data))
70+
self.last_ack = 0
71+
result += b"\0" * 4
72+
elif opcode == wire.OP_FILE_PUT_END:
73+
self.test.assertEqual(payload, wire.crc32(self.data).to_bytes(4, "little"))
74+
self.test.assertEqual(self.last_ack, len(self.data))
75+
elif opcode == wire.OP_FILE_STAT:
76+
result += len(self.data).to_bytes(4, "little") + wire.crc32(self.data).to_bytes(4, "little")
77+
elif opcode == wire.OP_FILE_GET_BEGIN:
78+
result += len(self.data).to_bytes(4, "little")
79+
self.events.extend([
80+
wire.Frame(wire.EVT, wire.OP_FILE_GET_DATA, 0, b"\0" * 4 + self.data),
81+
wire.Frame(wire.EVT, wire.OP_FILE_GET_END, 0, wire.crc32(self.data).to_bytes(4, "little")),
82+
])
83+
return wire.Frame(wire.RSP, opcode, request_id, result)
84+
85+
async def send_cmd_no_rsp(self, opcode, request_id, payload=b""):
86+
self.test.assertEqual(opcode, wire.OP_FILE_PUT_DATA)
87+
self.capture(opcode, request_id, payload, False)
88+
offset = int.from_bytes(payload[:4], "little")
89+
piece = payload[4:]
90+
self.test.assertGreater(len(piece), 0)
91+
self.test.assertLessEqual(len(piece), self.chunk)
92+
self.test.assertEqual(piece, self.data[offset:offset + len(piece)])
93+
self.offsets.append(offset)
94+
if self.drop_first and not self.dropped:
95+
self.dropped = True
96+
return
97+
if offset == self.last_ack:
98+
self.last_ack += len(piece)
99+
100+
def begin_ack_scope(self, watermark):
101+
self.test.assertEqual(watermark, 0)
102+
return SimpleNamespace(poll=lambda **kwargs: self.last_ack)
103+
104+
105+
class ValidTransferCommandIDs(unittest.IsolatedAsyncioTestCase):
106+
@classmethod
107+
def setUpClass(cls):
108+
cls.native = native_fixture.CompiledNativeWireTest()
109+
cls.native.setUp()
110+
cls.native.build_harness()
111+
112+
@classmethod
113+
def tearDownClass(cls):
114+
cls.native.doCleanups()
115+
116+
def assert_admitted(self, central):
117+
ids = [call[1] for call in central.calls]
118+
self.assertEqual(ids, [(index % 255) + 1 for index in range(len(ids))])
119+
hello = wire.encode(wire.CMD, wire.OP_HELLO, 255, native_fixture.CANONICAL_HELLO)
120+
output = self.native.run_harness([
121+
"RESET", "FRAME 1 " + hello.hex(),
122+
*["FRAME 1 " + raw.hex() for raw in central.frames],
123+
])
124+
self.assertEqual(len(output), len(central.frames) + 2)
125+
for raw, decision in zip(central.frames, output[2:]):
126+
self.assertEqual(decision[1], "dispatch", decision)
127+
self.assertEqual(int(decision[3]), raw[3])
128+
self.assertEqual(decision[5], "0", "valid upload must not consume violation budget")
129+
130+
def test_native_admission_rejects_zero_even_for_no_response_command(self):
131+
hello = wire.encode(wire.CMD, wire.OP_HELLO, 1, native_fixture.CANONICAL_HELLO)
132+
for request_id, action, violation in ((0, "drop", "1"), (1, "dispatch", "0"), (255, "dispatch", "0")):
133+
with self.subTest(request_id=request_id):
134+
raw = wire.encode(wire.CMD, wire.OP_FILE_PUT_DATA, request_id, b"\0" * 4 + b"x")
135+
result = self.native.run_harness(["RESET", "FRAME 1 " + hello.hex(), "FRAME 1 " + raw.hex()])[-1]
136+
self.assertEqual(result[1], action)
137+
self.assertEqual(result[5], violation)
138+
139+
def test_allocator_wraps_through_three_complete_cycles_without_zero(self):
140+
ids = shared.CommandIds()
141+
self.assertEqual([ids.next() for _ in range(770)], [(i % 255) + 1 for i in range(770)])
142+
143+
def test_valid_senders_have_no_literal_zero_cmd_ids(self):
144+
for module in (shared, reliability, roundtrip, target, rp2):
145+
with self.subTest(module=module.__name__):
146+
tree = ast.parse(Path(module.__file__).read_text())
147+
for node in ast.walk(tree):
148+
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
149+
if node.func.attr in {"send_cmd", "send_cmd_no_rsp"} and len(node.args) > 1:
150+
self.assertFalse(isinstance(node.args[1], ast.Constant) and node.args[1].value == 0,
151+
"%s:%d sends a valid CMD with ID zero" % (module.__name__, node.lineno))
152+
153+
async def test_oi1_all_chunks_and_rewinds_share_wrapping_control_allocator(self):
154+
data = bytes(range(229)) * 260 + b"tail"
155+
for drop_first in (False, True):
156+
with self.subTest(drop_first=drop_first):
157+
central = StrictCentral(self, data, drop_first=drop_first)
158+
ids, clock = shared.CommandIds(), Clock()
159+
with mock.patch.object(shared, "time", SimpleNamespace(monotonic=clock.now)):
160+
result = await shared.put_file(
161+
central, "/host-only.bin", data, window=8, chunk=229,
162+
next_id=ids.next, ack_timeout_s=0.1, operation_timeout_s=60,
163+
clock_ns=clock.ns, sleep=clock.sleep)
164+
self.assertEqual(result.unique_committed_bytes, len(data))
165+
self.assertEqual(bool(result.retransmitted_chunks), drop_first)
166+
self.assertEqual(bool(result.rewinds), drop_first)
167+
self.assertGreater(len(central.offsets), 255)
168+
self.assert_admitted(central)
169+
170+
async def test_f11_all_chunks_and_rewinds_share_wrapping_control_allocator(self):
171+
data = bytes(range(229)) * 260 + b"tail"
172+
for drop_first in (False, True):
173+
with self.subTest(drop_first=drop_first):
174+
central = StrictCentral(self, data, drop_first=drop_first)
175+
ids, clock = shared.CommandIds(), Clock()
176+
fake_asyncio = SimpleNamespace(get_event_loop=lambda: SimpleNamespace(time=clock.now), sleep=clock.sleep)
177+
with mock.patch.object(reliability, "asyncio", fake_asyncio):
178+
result = await reliability._upload_one(central, "/host-only.bin", data, 8, 229, ids.next, 0.1, 1)
179+
self.assertTrue(result.ok)
180+
self.assertTrue(result.stat_verified)
181+
self.assertEqual(bool(result.retransmits), drop_first)
182+
self.assertGreater(len(central.offsets), 255)
183+
self.assert_admitted(central)
184+
185+
async def test_roundtrip_put_get_and_rewinds_share_wrapping_allocator(self):
186+
data = bytes(range(229)) * 260 + b"tail"
187+
for drop_first in (False, True):
188+
with self.subTest(drop_first=drop_first):
189+
central = StrictCentral(self, data, drop_first=drop_first)
190+
ids, clock = shared.CommandIds(), Clock()
191+
with mock.patch.object(roundtrip, "time", SimpleNamespace(monotonic=clock.now)), \
192+
mock.patch.object(roundtrip, "asyncio", SimpleNamespace(sleep=clock.sleep)):
193+
await roundtrip._put(central, data, 229, 8, ids.next)
194+
got, checksum, _duration = await roundtrip._get(central, data, ids.next)
195+
self.assertEqual(got, data)
196+
self.assertEqual(checksum, wire.crc32(data))
197+
self.assertEqual(len(central.offsets) != len(set(central.offsets)), drop_first)
198+
self.assertGreater(len(central.offsets), 255)
199+
self.assert_admitted(central)
200+
201+
async def test_small_file_helpers_share_caller_allocator_across_wrap(self):
202+
data = b"print('host-only')\n"
203+
for module in (target, rp2):
204+
with self.subTest(module=module.__name__):
205+
ids = shared.CommandIds()
206+
central = StrictCentral(self, data)
207+
for _ in range(90):
208+
await module.put_small_file(central, "/host-only.py", data, ids.next)
209+
self.assertEqual(len(central.frames), 270)
210+
self.assertEqual(len(central.offsets), 90)
211+
self.assert_admitted(central)
212+
213+
214+
if __name__ == "__main__":
215+
unittest.main()

0 commit comments

Comments
 (0)