feat(micromate): one opcode table and a send-side gate -- writes refused unless allowed, 0xA2 always
micromate/opcodes.py holds WRITE_SUBS and the cross-series collision 0xA2.
MicromateProtocol._send reads the SUB of the bytes about to go out
(framing.request_sub) and refuses any write SUB not in allowed_writes, and
0xA2 unconditionally. Docstrings now say the reads-only policy is enforced.
request_sub also refuses any frame not in canonical form
(stuff(unstuff(body)) == body): an interior unescaped 02/03/04, a 10 XX
outside {02,03,04,10}, or a trailing lone DLE. What the gate checks is then
exactly what the unit parses.
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
151ba1546f
commit
a3e4e85dfd
6 files changed
+178
-9
No files matched your search
@@ -468,6 +468,23 @@ def test_request_sub_refuses_anything_that_is_not_a_request_frame(junk):
|
||||
request_sub(junk)
|
||||
|
||||
|
||||
def _noncanonical_frames():
|
||||
good = build_request(0x96, 0xFFFF)
|
||||
return {
|
||||
"interior raw 03": good[:5] + b"\x03" + good[5:],
|
||||
"10 05 pair": good[:5] + b"\x10\x05" + good[5:],
|
||||
"lone DLE before ETX": good[:-1] + b"\x10" + good[-1:],
|
||||
"double 41 02 prefix": good[:2] + good,
|
||||
"first byte not 0x10": good[:2] + bytes([0x11]) + good[3:],
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", list(_noncanonical_frames()))
|
||||
def test_request_sub_refuses_a_frame_that_is_not_in_canonical_form(name):
|
||||
with pytest.raises(ValueError):
|
||||
request_sub(_noncanonical_frames()[name])
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not _CAPTURES.is_dir(),
|
||||
reason="capture directory is gitignored; present only on a dev box",
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
"""The send-side gate: no write opcode reaches the wire unless allowed, and 0xA2 never."""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import micromate.protocol as mm_proto # noqa: E402
|
||||
from micromate.framing import build_request # noqa: E402
|
||||
from micromate.opcodes import ( # noqa: E402
|
||||
CROSS_SERIES_COLLISIONS, WRITE_SUBS, OpcodeRefused, check_transmit,
|
||||
)
|
||||
from test_micromate_protocol import frame, proto # noqa: E402
|
||||
|
||||
# THOR's captured end-call-home frame -- byte-identical to Series III's CONFIRM ERASE.
|
||||
A2_WIRE = bytes.fromhex("4102101000a200ffff00000000000000000000b003")
|
||||
|
||||
|
||||
def test_no_read_command_is_classed_as_a_write():
|
||||
reads = {v for k, v in vars(mm_proto).items() if k.startswith("SUB_")}
|
||||
assert reads and not (reads & set(WRITE_SUBS))
|
||||
|
||||
|
||||
def test_0xa2_is_a_collision_and_a_write():
|
||||
assert 0xA2 in CROSS_SERIES_COLLISIONS and 0xA2 in WRITE_SUBS
|
||||
|
||||
|
||||
def test_a_write_is_refused_unless_allowed():
|
||||
with pytest.raises(OpcodeRefused, match="0x96"):
|
||||
check_transmit(0x96)
|
||||
check_transmit(0x96, frozenset({0x96}))
|
||||
check_transmit(0x5B) # a read needs no permission
|
||||
|
||||
|
||||
def test_0xa2_is_refused_even_when_allowed():
|
||||
with pytest.raises(OpcodeRefused, match="ERASE"):
|
||||
check_transmit(0xA2, frozenset({0xA2}))
|
||||
|
||||
|
||||
def test_the_protocol_refuses_a_write_and_writes_nothing():
|
||||
p, t = proto([])
|
||||
with pytest.raises(OpcodeRefused):
|
||||
p._exchange(0x96, offset=0xFFFF)
|
||||
with pytest.raises(OpcodeRefused):
|
||||
p._send(build_request(0x97, 0xFFFF)) # a direct _send is gated too
|
||||
assert t.written == []
|
||||
|
||||
|
||||
def test_the_protocol_refuses_the_captured_0xa2_frame_byte_for_byte():
|
||||
p, t = proto([], allowed_writes=frozenset({0xA2}))
|
||||
with pytest.raises(OpcodeRefused):
|
||||
p._send(A2_WIRE)
|
||||
assert t.written == []
|
||||
|
||||
|
||||
def test_an_explicitly_allowed_write_goes_out_on_a_scripted_transport():
|
||||
p, t = proto([frame(0x69, bytes(11))], allowed_writes=frozenset({0x96}))
|
||||
p._exchange(0x96, offset=0xFFFF)
|
||||
assert t.written == [build_request(0x96, 0xFFFF)]
|
||||
|
||||
|
||||
def test_reads_are_unaffected_by_the_gate():
|
||||
p, t = proto([frame(0xA4, bytes(11))])
|
||||
p.poll()
|
||||
assert len(t.written) == 1
|
||||
|
||||
|
||||
def test_a_frame_that_is_not_a_request_is_refused_not_sent():
|
||||
p, t = proto([])
|
||||
with pytest.raises(ValueError):
|
||||
p._send(b"\x41\x03")
|
||||
assert t.written == []
|
||||
Reference in new issue
Block a user