fix(sfm): legacy Series-3 writes and erase refuse a unit that does not answer as Series 3
0xA2 is CONFIRM ERASE on a MiniMate and ends a call-home on a Micromate, and the legacy host/port routes had no identity check: only the MiniMate parser's inability to read a Micromate's replies stood between Diagnostics' Erase and a Micromate (phase 1c safeguard, agreed with Brian 2026-10-28). _require_series3(unit_serial, expected) refuses (409, 'nothing sent') unless the unit's own serial is Series 3 and, when ?serial= names one, that one: - POST /device/config (+ /config/project) and /device/events/erase: the serial connect() already reads -- no extra request; - /device/rescue (when it writes call-home or erases) and POST /device/call_home: one SUB 0x15 read (new MiniMateClient.read_serial). Exempt, with the reason in a comment: legacy monitor start/stop, blind stop, slow drip, spam -- 0x96/0x97 mean the same on both series (measured), and the rescue tools must reach a unit too wedged to answer. Tests: each guarded route refuses a unit answering UM20147 and a different BE unit with no write call, proceeds for the named unit (normalised), reuses connect()'s serial where it has one; the blind stop builds no client; read_serial decodes BE9441's recorded 0x15 reply. 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
c3746a6a01
commit
1cb11abcef
3 files changed
+232
-4
No files matched your search
@@ -0,0 +1,181 @@
|
||||
"""Legacy Series-3 writes and erase refuse a unit that does not answer as Series 3.
|
||||
|
||||
0xA2 is CONFIRM ERASE on a MiniMate and ends a call-home on a Micromate; the
|
||||
legacy host/port routes had no identity check, so nothing but a MiniMate parser
|
||||
that cannot read a Micromate's replies stood between Diagnostics' Erase and a
|
||||
Micromate. Each guarded route now reads -- or reuses -- the unit's own serial and
|
||||
refuses (409) before its first write unless that serial is Series 3 and, when one
|
||||
was named, the named one. The stop-only tools are exempt: 0x96/0x97 mean the
|
||||
same on both series, and they must reach a unit too wedged to answer.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from fastapi import HTTPException # noqa: E402
|
||||
|
||||
import sfm.server as srv # noqa: E402
|
||||
|
||||
WRITES = {"delete_all_events", "apply_config", "set_call_home_config", "stop_monitoring"}
|
||||
ADDR = dict(port=None, baud=38400, host="10.0.0.9", tcp_port=9034)
|
||||
|
||||
|
||||
class Unit:
|
||||
"""A unit that answers as `serial`; records every client call."""
|
||||
|
||||
def __init__(self, serial):
|
||||
self.serial, self.calls = serial, []
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
|
||||
def __exit__(self, *a):
|
||||
return False
|
||||
|
||||
def _rec(self, name):
|
||||
self.calls.append(name)
|
||||
|
||||
def connect(self):
|
||||
self._rec("connect")
|
||||
return SimpleNamespace(serial=self.serial)
|
||||
|
||||
def poll(self):
|
||||
self._rec("poll")
|
||||
|
||||
def read_serial(self):
|
||||
self._rec("read_serial")
|
||||
return self.serial
|
||||
|
||||
def delete_all_events(self):
|
||||
self._rec("delete_all_events")
|
||||
|
||||
def apply_config(self, **kw):
|
||||
self._rec("apply_config")
|
||||
|
||||
def set_call_home_config(self, **kw):
|
||||
self._rec("set_call_home_config")
|
||||
|
||||
|
||||
class Cache:
|
||||
def clear_device(self, key):
|
||||
return 0
|
||||
|
||||
def mark_config_dirty(self, key):
|
||||
pass
|
||||
|
||||
|
||||
def _call(route, serial):
|
||||
if route == "config":
|
||||
return srv.device_config(body=srv.DeviceConfigBody(), serial=serial, **ADDR)
|
||||
if route == "erase":
|
||||
return srv.device_events_erase(serial=serial, **ADDR)
|
||||
if route == "rescue":
|
||||
return srv.device_rescue(connect_timeout=5.0, recv_timeout=5.0, disable_ach=True,
|
||||
erase=True, serial=serial, **ADDR)
|
||||
if route == "call_home":
|
||||
return srv.device_call_home_set(body=srv.CallHomeConfigBody(auto_call_home_enabled=False),
|
||||
serial=serial, **ADDR)
|
||||
raise AssertionError(route)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def unit(monkeypatch):
|
||||
holder = {}
|
||||
|
||||
def make(serial):
|
||||
u = Unit(serial)
|
||||
holder["u"] = u
|
||||
monkeypatch.setattr(srv, "_build_client", lambda *a, **kw: u)
|
||||
monkeypatch.setattr(srv, "get_cache", lambda: Cache())
|
||||
return u
|
||||
return make
|
||||
|
||||
|
||||
ROUTES = ["config", "erase", "rescue", "call_home"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", ROUTES)
|
||||
def test_guarded_route_refuses_a_unit_that_answers_as_series4(unit, route):
|
||||
u = unit("UM20147")
|
||||
with pytest.raises(HTTPException) as e:
|
||||
_call(route, None)
|
||||
assert e.value.status_code == 409
|
||||
assert "not a Series-3 unit" in str(e.value.detail)
|
||||
assert not WRITES & set(u.calls), u.calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", ROUTES)
|
||||
def test_guarded_route_refuses_a_different_series3_unit(unit, route):
|
||||
u = unit("BE9441")
|
||||
with pytest.raises(HTTPException) as e:
|
||||
_call(route, "BE12599")
|
||||
assert e.value.status_code == 409
|
||||
assert "not BE12599" in str(e.value.detail)
|
||||
assert not WRITES & set(u.calls), u.calls
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", ROUTES)
|
||||
def test_guarded_route_proceeds_for_the_named_series3_unit(unit, route):
|
||||
u = unit("BE9441")
|
||||
_call(route, "be9441 ")
|
||||
assert len(WRITES & set(u.calls)) >= 1
|
||||
assert u.calls.index(next(c for c in u.calls if c in WRITES)) > \
|
||||
max(i for i, c in enumerate(u.calls) if c in ("connect", "read_serial"))
|
||||
|
||||
|
||||
def test_the_identity_check_reuses_connects_serial_where_there_is_one(unit):
|
||||
# speed: config and erase already read the serial in connect(); no extra read
|
||||
for route in ("config", "erase"):
|
||||
u = unit("BE9441")
|
||||
_call(route, None)
|
||||
assert "read_serial" not in u.calls
|
||||
|
||||
|
||||
def test_unguarded_stop_routes_send_without_an_identity_read(monkeypatch):
|
||||
sent = []
|
||||
|
||||
class Transport:
|
||||
def __init__(self, *a, **kw):
|
||||
pass
|
||||
|
||||
def connect(self):
|
||||
pass
|
||||
|
||||
def write(self, data):
|
||||
sent.append(bytes(data))
|
||||
|
||||
def disconnect(self):
|
||||
pass
|
||||
|
||||
def no_client(*a, **kw):
|
||||
raise AssertionError("the blind stop must not build a client")
|
||||
monkeypatch.setattr(srv, "TcpTransport", Transport)
|
||||
monkeypatch.setattr(srv, "_build_client", no_client)
|
||||
monkeypatch.setattr(srv.time, "sleep", lambda s: None)
|
||||
srv.device_stop_monitoring_blind(host="10.0.0.9", tcp_port=9034, connect_timeout=5.0, repeat=1)
|
||||
assert sent and all(b"\x97" in s for s in sent)
|
||||
|
||||
|
||||
def test_read_serial_decodes_the_units_own_reply():
|
||||
# BE9441's SUB 0x15 reply, recorded 2026-10-26 through the bridge
|
||||
from pathlib import Path
|
||||
from minimateplus.client import MiniMateClient
|
||||
from minimateplus.framing import S3FrameParser
|
||||
raw = (Path(__file__).parent / "fixtures" / "minimate-bench-10-26-26-histogram"
|
||||
/ "unit_replies.bin").read_bytes()
|
||||
reply = next(f for f in S3FrameParser().feed(raw) if f.sub == 0xEA and len(f.data) > 12)
|
||||
|
||||
class Proto:
|
||||
def read(self, sub):
|
||||
assert sub == 0x15
|
||||
return reply.data
|
||||
c = MiniMateClient(transport=object())
|
||||
c._proto = Proto()
|
||||
assert c.read_serial() == "BE9441"
|
||||
Reference in new issue
Block a user