Files
seismo-relay/tests/test_s3_call_home.py
T
serversdownandClaude Opus 5.5 5326e99ac2 fix(sfm): a monitoring MiniMate's call-home says 'stop monitoring first', not 'unknown(0x6E)'
Measured at the bench 2026-11-03 (BE9441): while monitoring, a MiniMate answers
the 0x2C call-home read with its DEVICE-INFO block ('Instantel', 'MiniMate Plus',
its serial) -- as it answers 1A -- and idle again, minutes later, the call-home
block.  SFM writes back only a block it just read, so a monitoring MiniMate's
call-home cannot be changed; the change core already refused (byte 5 read as
'unknown(0x6E)', nothing sent), but said so cryptically.

- minimateplus.protocol.is_call_home_block(raw): the block starts with its own
  length 0x7C and is N (or N + 2) bytes unescaped; the device-info block fails it.
- The Series-3 change raises UnitStateUnknown (409) with 'did not return its
  call-home settings -- a MiniMate answers the call-home read with its
  device-info block while monitoring; stop monitoring first ... nothing sent',
  for a dry run too.  call_home_state returns enabled = None with that reason.
- The page's callHomeRows shows the reason in place of 'Unknown (byte 5 = ...)'.

A blind write while monitoring stays untested on purpose.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-03 14:19:19 -05:00

182 lines
7.4 KiB
Python

"""Series 3 call-home: the enable byte from the unescaped 0x2C block, a write that
changes byte 5 only, and the audited change (phase 4). The live send stays
refused until its frame is reproduced from a Blastware capture (task 4)."""
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 sfm.device_access.s3 as s3mod # noqa: E402
from device_access_fakes import FakeMiniMateClient, s3_call_home_raw # noqa: E402
from minimateplus.protocol import ( # noqa: E402
SUB_CALL_HOME_WRITE, call_home_enabled, call_home_image, write_image,
)
from sfm.device_access.errors import NotSupported, UnitStateUnknown # noqa: E402
from sfm.device_access.models import UnitAddress # noqa: E402
from sfm.device_access.s3 import Series3Adapter # noqa: E402
TCP = UnitAddress(host="10.0.0.6", tcp_port=12345, port=None, baud=None)
class Audit:
def __init__(self):
self.rows = {}
def begin(self, **f):
cid = f"c{len(self.rows) + 1}"
self.rows[cid] = dict(f, outcome="pending")
return cid
def finish(self, cid, **f):
self.rows[cid].update(f)
def adapter(fake):
calls = []
def factory(address, **kw):
calls.append(kw)
return fake
a = Series3Adapter(client_factory=factory)
a.factory_calls = calls
return a
def change(a, *, enabled, dry_run=False, even_if_already=False):
return a.change_call_home(TCP, "BE18193", enabled=enabled, dry_run=dry_run,
even_if_already=even_if_already, audit=Audit(), caller="test",
sleep=lambda s: None, verify_timeout_s=3.0, verify_interval_s=1.0)
def test_s3_enable_byte_reads_from_the_unescaped_block():
raw = s3_call_home_raw()
assert call_home_enabled(raw) is True
assert call_home_enabled(s3_call_home_raw(0x00)) is False
# An escaped byte before [5] moves the raw index; the unescaped one does not.
shifted = raw[:2] + b"\x10\x03" + raw[3:]
assert shifted[5] != raw[5] and call_home_enabled(shifted) is True
assert call_home_enabled(s3_call_home_raw(0x07)) is None
def test_s3_image_changes_only_byte_5():
raw = s3_call_home_raw()
img = call_home_image(raw, False)
base = write_image(SUB_CALL_HOME_WRITE, raw)
assert len(img) == 126 == 0x7C + 2
assert [i for i in range(len(img)) if img[i] != base[i]] == [5]
assert img[5] == 0x00 and call_home_image(raw, True)[5] == 0x01
def test_s3_image_refuses_an_unknown_byte_5():
with pytest.raises(ValueError):
call_home_image(s3_call_home_raw(0x07), True)
def test_s3_call_home_state():
fake = FakeMiniMateClient()
st = adapter(fake).call_home_state(TCP, "BE18193")
assert (st.serial, st.series, st.enabled, st.dial_string) == ("BE18193", 3, True, "RADIO RING")
assert st.detail["serial_verified"] is False and st.detail["byte5"] == "0x01"
assert fake.calls.index("wake") < fake.calls.index("read_call_home_raw")
def test_the_gate_is_open_and_names_its_proof():
assert s3mod.S3_CALL_HOME_FRAME_VERIFIED is True
assert "tests/test_s3_call_home_bw_frames.py" in open(s3mod.__file__).read()
def test_s3_live_change_is_refused_while_the_gate_is_shut(monkeypatch):
monkeypatch.setattr(s3mod, "S3_CALL_HOME_FRAME_VERIFIED", False)
a = adapter(FakeMiniMateClient())
with pytest.raises(NotSupported) as e:
change(a, enabled=False)
assert e.value.http_status == 501 and "Blastware" in str(e.value)
assert a.factory_calls == [] # refused before any connection
def test_s3_dry_run_shows_both_frames():
fake = FakeMiniMateClient()
r = change(adapter(fake), enabled=False, dry_run=True)
assert r.outcome == "dry_run"
assert [st["sub"] for st in r.plan["steps"]] == ["0x7E", "0x7F"]
assert fake.writes == []
def test_s3_change_once_verified(monkeypatch):
monkeypatch.setattr(s3mod, "S3_CALL_HOME_FRAME_VERIFIED", True)
fake = FakeMiniMateClient()
r = change(adapter(fake), enabled=False)
assert (r.outcome, r.verified, r.state_after, r.action) == ("sent", "ok", "disabled", "disable call-home")
assert fake.writes == ["call_home"]
assert r.detail["snapshot"]["changed_offsets"] == [5]
def test_s3_unknown_byte_5_sends_nothing(monkeypatch):
monkeypatch.setattr(s3mod, "S3_CALL_HOME_FRAME_VERIFIED", True)
fake = FakeMiniMateClient(call_home_raw=s3_call_home_raw(0x07))
with pytest.raises(UnitStateUnknown):
change(adapter(fake), enabled=False)
assert fake.writes == []
def test_s3_dry_run_with_an_unknown_byte_5_shows_no_frames():
fake = FakeMiniMateClient(call_home_raw=s3_call_home_raw(0x07))
r = change(adapter(fake), enabled=False, dry_run=True)
assert r.plan["steps"] == [] and any("0x07" in w for w in r.plan["warnings"])
def test_a_pre_wire_refusal_is_recorded_as_nothing_sent():
# Final review #2: a refusal raised before the frame went out must not be
# audited as "sent, acknowledgement lost".
from minimateplus.protocol import ImageRefused
audit = Audit()
fake = FakeMiniMateClient(write_error=ImageRefused("write SUB 0x7E: not a whole block"))
from sfm.device_access.errors import DeviceError
with pytest.raises(DeviceError) as e: # the adapter wraps it, as it does OpcodeRefused
adapter(fake).change_call_home(TCP, "BE18193", enabled=False, dry_run=False, even_if_already=False,
audit=audit, caller="test", sleep=lambda s: None,
verify_timeout_s=3.0, verify_interval_s=1.0)
assert isinstance(e.value.__cause__, ImageRefused)
((_, row),) = audit.rows.items()
assert (row["outcome"], row["sent"]) == ("failed", 0)
assert row["error"].startswith("refused before sending")
# Measured 2026-11-03 (BE9441): while monitoring, a MiniMate answers the 0x2C read
# with its DEVICE-INFO block ("Instantel", "MiniMate Plus", its serial), not its
# call-home block -- as it answers 1A. Idle again, it returns the call-home block.
# SFM says so plainly instead of "run state unknown(0x6E)".
def device_info_shaped(serial=b"BE9441"):
"""Shaped like BE9441's reply (its first bytes: 00 00 00 08 'Instantel')."""
block = b"\x00\x00\x00\x08Instantel".ljust(26, b"\x00") + b"MiniMate Plus".ljust(16, b"\x00") + serial
return block.ljust(126, b"\x00")
def test_a_call_home_block_is_told_from_the_device_info_block():
from minimateplus.protocol import is_call_home_block
assert is_call_home_block(s3_call_home_raw()) is True
assert is_call_home_block(device_info_shaped()) is False
@pytest.mark.parametrize("dry_run", [True, False])
def test_a_monitoring_minimate_is_told_to_stop_first_and_nothing_is_sent(dry_run):
fake = FakeMiniMateClient(call_home_raw=device_info_shaped())
with pytest.raises(UnitStateUnknown) as e:
change(adapter(fake), enabled=True, dry_run=dry_run)
msg = str(e.value)
assert "monitoring" in msg and "stop" in msg and "nothing sent" in msg
assert e.value.http_status == 409 and fake.writes == []
def test_the_call_home_state_of_a_monitoring_minimate_says_why():
st = adapter(FakeMiniMateClient(call_home_raw=device_info_shaped())).call_home_state(TCP, "BE18193")
assert st.enabled is None
assert "monitoring" in st.detail["reason"]