fix(sfm): RADIO RING detection ignores bytes inside frames
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
This commit is contained in:
1 parent
82b91835fb
commit
52ada0215c
2 files changed
+106
-7
No files matched your search
+26
-7
@@ -29,12 +29,21 @@ class RingDetected(Exception):
|
||||
|
||||
|
||||
class RingWatchTransport(BaseTransport):
|
||||
"""Passes bytes through; raises RingDetected the moment RADIO RING appears,
|
||||
including when it is split across two reads."""
|
||||
"""Passes bytes through; raises RingDetected the moment RADIO RING appears
|
||||
OUTSIDE a frame, including when it is split across two reads.
|
||||
|
||||
Inside a frame the bytes are data: the 0x2C call-home config block's dial
|
||||
string is literally "RADIO RING". A frame runs from STX to an unescaped
|
||||
ETX; inside it DLE escapes the next byte.
|
||||
"""
|
||||
|
||||
_STX, _ETX, _DLE = 0x02, 0x03, 0x10
|
||||
|
||||
def __init__(self, inner: BaseTransport) -> None:
|
||||
self._inner = inner
|
||||
self._tail = b""
|
||||
self._in_frame = False
|
||||
self._escaped = False
|
||||
|
||||
def connect(self) -> None:
|
||||
self._inner.connect()
|
||||
@@ -51,12 +60,22 @@ class RingWatchTransport(BaseTransport):
|
||||
|
||||
def read(self, n: int) -> bytes:
|
||||
data = self._inner.read(n)
|
||||
if data:
|
||||
window = self._tail + data
|
||||
if RING in window:
|
||||
for b in data:
|
||||
if self._in_frame:
|
||||
if self._escaped:
|
||||
self._escaped = False
|
||||
elif b == self._DLE:
|
||||
self._escaped = True
|
||||
elif b == self._ETX:
|
||||
self._in_frame = False
|
||||
elif b == self._STX:
|
||||
self._in_frame = True
|
||||
self._tail = b""
|
||||
raise RingDetected("RADIO RING")
|
||||
self._tail = window[-(len(RING) - 1):]
|
||||
else:
|
||||
self._tail = (self._tail + bytes([b]))[-len(RING):]
|
||||
if self._tail == RING:
|
||||
self._tail = b""
|
||||
raise RingDetected("RADIO RING")
|
||||
return data
|
||||
|
||||
|
||||
|
||||
@@ -79,6 +79,86 @@ def test_a_ring_split_across_reads_still_raises():
|
||||
t.read(4096)
|
||||
|
||||
|
||||
def _real_frame(payload_data: bytes, sub: int = 0xA4) -> bytes:
|
||||
from micromate.framing import ETX, STX, checksum, stuff
|
||||
payload = bytes([0x00, 0xC5, sub, 0x00, 0x00]) + payload_data
|
||||
return bytes([STX]) + stuff(payload + bytes([checksum(payload)])) + bytes([ETX])
|
||||
|
||||
|
||||
def test_a_ring_inside_a_frame_is_data_not_a_call_home():
|
||||
# The 0x2C call-home config block's dial string is literally "RADIO RING".
|
||||
wire = _real_frame(b"\x00" * 6 + b"RADIO RING" + b"\x00" * 4)
|
||||
t = RingWatchTransport(ScriptedInner([wire]))
|
||||
assert t.read(4096) == wire
|
||||
|
||||
|
||||
def test_a_ring_inside_a_frame_split_across_reads_is_still_data():
|
||||
wire = _real_frame(b"xxRADIO RINGyy")
|
||||
cut = wire.index(b"RADIO R") + 7
|
||||
t = RingWatchTransport(ScriptedInner([wire[:cut], wire[cut:]]))
|
||||
assert t.read(4096) == wire[:cut]
|
||||
assert t.read(4096) == wire[cut:]
|
||||
|
||||
|
||||
def test_a_ring_between_frames_still_raises():
|
||||
t = RingWatchTransport(ScriptedInner([_real_frame(b"abc") + b"\r\nRADIO RING\r\n"]))
|
||||
with pytest.raises(RingDetected):
|
||||
t.read(4096)
|
||||
|
||||
|
||||
def test_a_ring_split_across_reads_after_a_frame_still_raises():
|
||||
t = RingWatchTransport(ScriptedInner([_real_frame(b"abc") + b"\r\nRADIO R", b"ING\r\n"]))
|
||||
t.read(4096)
|
||||
with pytest.raises(RingDetected):
|
||||
t.read(4096)
|
||||
|
||||
|
||||
def test_a_ring_arriving_right_after_a_frame_ends_still_raises():
|
||||
t = RingWatchTransport(ScriptedInner([_real_frame(b"abc"), b"RADIO RING"]))
|
||||
t.read(4096)
|
||||
with pytest.raises(RingDetected):
|
||||
t.read(4096)
|
||||
|
||||
|
||||
def test_an_escaped_etx_inside_a_frame_does_not_end_it():
|
||||
# DLE ETX inside the payload is literal data; the ring after it is still inside.
|
||||
wire = b"\x02" + b"ab\x10\x03RADIO RINGcd" + b"\x03"
|
||||
t = RingWatchTransport(ScriptedInner([wire]))
|
||||
assert t.read(4096) == wire
|
||||
|
||||
|
||||
def test_a_ring_that_follows_a_frame_with_an_escaped_etx_still_raises():
|
||||
wire = b"\x02" + b"ab\x10\x03cd" + b"\x03" + b"RADIO RING"
|
||||
t = RingWatchTransport(ScriptedInner([wire]))
|
||||
with pytest.raises(RingDetected):
|
||||
t.read(4096)
|
||||
|
||||
|
||||
def _real_chain_client(chunks):
|
||||
from micromate.client import MicromateClient
|
||||
inner = ScriptedInner(chunks)
|
||||
return MicromateClient(RingWatchTransport(inner), recv_timeout=0.5), inner
|
||||
|
||||
|
||||
def test_real_chain_a_response_carrying_a_ring_in_its_payload_succeeds():
|
||||
client, _ = _real_chain_client([_real_frame(b"\x30" + b"\x00" * 8 + b"RADIO RING")])
|
||||
client.open()
|
||||
frame = client.protocol.poll()
|
||||
assert b"RADIO RING" in frame.data
|
||||
|
||||
|
||||
def test_real_chain_an_unsolicited_ring_before_the_response_hangs_up():
|
||||
from types import SimpleNamespace
|
||||
|
||||
client, inner = _real_chain_client([b"\r\nRADIO RING\r\n", _real_frame(b"\x30" + b"\x00" * 8)])
|
||||
# Only the identity read is stubbed; open/close/protocol/transport are the real chain.
|
||||
client.connect = lambda: SimpleNamespace(serial="UM20147")
|
||||
s = Series4Session(ADDR, "UM20147", lambda address: client, clock=Clock())
|
||||
with pytest.raises(CallHomeInProgress):
|
||||
s.run(lambda c: c.protocol.poll())
|
||||
assert not s.is_open and not inner.is_connected
|
||||
|
||||
|
||||
def test_the_wrapper_keeps_is_connected_a_property():
|
||||
inner = ScriptedInner([])
|
||||
t = RingWatchTransport(inner)
|
||||
|
||||
Reference in new issue
Block a user