diff --git a/CLAUDE.md b/CLAUDE.md index d9011859..03fa042b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -472,6 +472,9 @@ stdout. Also the only completeness oracle: `tools/http_body_checks.py` (exit 78 = inconclusive, #210) + rig_trust_store.py / rig_trust_policy.py — TRUST.A/B on `//c64https-test` + via harness `storage_*`; no + writable volume = exit 2 rig_https_bad_finished.py — forged server Finished must abort (`FINISHED_MODE=good` control first) bench_ecdsa_u64e.py — verify wall-clock sweeps @@ -531,8 +534,9 @@ engineering-notes. 'Q'. Probe DHCP with a fast-boot (onchip) image, never comb. - **writemem exhaustion wedge** (fw 3.14d, GideonZ/1541ultimate#686): each REST `writemem` leaks a `/Temp` file; after ~15 loads REST and the - UCI bridge wedge together. `tools/uci/_temp_gc.py` FTP-deletes them; - wired into the live/wiki rigs. + UCI bridge wedge together. c64-test-harness handles it: any default + `Ultimate64Client` on a leak-prone device sweeps `/Temp` while this + process holds the DeviceLock (our `_temp_gc.py` is retired). - The U64E is a shared device: trust `DeviceLock`, never kill other sessions' processes or force-reboot it. diff --git a/docs/engineering-notes.md b/docs/engineering-notes.md index c986dda0..a9dbafc0 100644 --- a/docs/engineering-notes.md +++ b/docs/engineering-notes.md @@ -1138,9 +1138,9 @@ The scripts: (`HTTPS_TARGET=github.com`, DMA-fed to the PRG so any build works). Pass criteria are C64-side only (sentinel + `http_status=200`). - Runs the `/Temp` GC (`_temp_gc.py`) after - enable_uci — see the writemem-wedge note in - "End-to-end HTTPS status". Comb + `/Temp` hygiene is the harness's (its + `Ultimate64Client`, under the DeviceLock; + the old `_temp_gc.py` is retired). Comb `C64_INIT_WAIT=75` is auto-detected from labels.txt. Keep `DEBUG_CAPTURE=0` here. - `rig_https_local.py` — HTTPS e2e scaffolding against a local TLS 1.3 @@ -1312,7 +1312,8 @@ and then REST *and* the UCI bridge wedge together — ping alive, REST refuses instantly, C64 parks mid-transfer. Power cycle fixes; upstream GideonZ/1541ultimate#686 (merged, unreleased). Mitigated by `tools/uci/_temp_gc.py` (FTP-deletes managed `temp` files, keep -youngest 2; wired into the live/wiki rigs). Budget PRG loads per session; +youngest 2; wired into the live/wiki rigs; since retired — the harness's +`Ultimate64Client` sweeps `/Temp` under the DeviceLock). Budget PRG loads per session; `readmem` is not implicated. diff --git a/pytest.ini b/pytest.ini index c78f6ba6..5684b6b7 100644 --- a/pytest.ini +++ b/pytest.ini @@ -73,6 +73,7 @@ testpaths = tools/test_runner_coverage.py tools/test_skip_policy.py tools/test_trust_bundle_unit.py + tools/test_trust_rig_storage.py tools/test_trust_store_format.py tools/test_uci_abort_recovery.py tools/test_uci_data_acc.py diff --git a/tools/_skip_policy.py b/tools/_skip_policy.py index ea2236f9..e1a8054c 100644 --- a/tools/_skip_policy.py +++ b/tools/_skip_policy.py @@ -156,7 +156,7 @@ def _opted_out(opt_out_env: Optional[str]) -> bool: Bare truthiness would make ``C64_ALLOW_SKIP=0`` and ``C64_ALLOW_SKIP=false`` ENABLE the opt-out -- someone setting 0 to shut the escape hatch would silently disable the whole policy. Every other - gate in this repo (``C64_SKIP_BUILD``, ``C64_SKIP_TEMP_GC``, + gate in this repo (``C64_SKIP_BUILD``, ``C64_NET_TESTS_OPTIONAL``, ``VICE_HTTPS_OK_TO_RUN``) compares to the literal "1"; this one does too. Surrounding whitespace is tolerated because a shell export can carry it; nothing else is. diff --git a/tools/test_rig_scratch.py b/tools/test_rig_scratch.py index 1c14dab9..0f7fa950 100644 --- a/tools/test_rig_scratch.py +++ b/tools/test_rig_scratch.py @@ -176,9 +176,9 @@ def at_lock(lock, **k): patch("ACCEPT_TIMEOUT", 0.2) # rig_https_wiki: trampoline mode is the one that allocates. patch("MENU_MODE", False) - # rig_trust_store: the fake PRG carries no store path to read. - if hasattr(mod, "TEST_DIR"): - patch("image_store_path", lambda prg, L: mod.TEST_DIR + "/TRUST.") + # the trust rigs: the fake PRG carries no store path to read. + if hasattr(mod, "image_store_path"): + patch("image_store_path", lambda prg, L: "/USB1/c64https-test/TRUST.") if orig_bind is not None: patch("_try_bind", lambda ip, port: orig_bind("127.0.0.1", 0)) try: diff --git a/tools/test_trust_rig_storage.py b/tools/test_trust_rig_storage.py new file mode 100644 index 00000000..a41a6d32 --- /dev/null +++ b/tools/test_trust_rig_storage.py @@ -0,0 +1,826 @@ +#!/usr/bin/env python3 +"""Guard the trust rigs' use of the harness storage API (#155; harness #526, #527). + +tools/uci/rig_trust_store.py and rig_trust_policy.py keep TRUST.A / TRUST.B +on the device's removable volume. They used to do it over a private +``ftplib`` session, and swept ``/Temp`` with a private GC +(``tools/uci/_temp_gc.py``). Both are now c64-test-harness's job: the +storage helpers (``storage_*``, which demand the DeviceLock and never write +``/Temp`` or ``/Flash``), and the automatic ``/Temp`` hygiene of any +``Ultimate64Client`` on a leak-prone device while the lock is held. + +Nothing here touches hardware, VICE or a build: the storage calls, the +DeviceLock and the ``Ultimate64Client`` are faked. The fake storage calls +behave like the real ones on the one rule a fake can drop silently: they +refuse (``Ultimate64StorageError``) and record a violation unless the fake +DeviceLock is held. Pinned: + +1. **Volume selection** (``_trust_storage.select_store_dir``): only a + volume in ``storage_writable_volumes``; an explicit ``TRUST_STORE_DIR`` + must match the image; only ``//c64https-test``. +2. **Exit codes.** No writable volume (a C64U with no medium) or an + ``Ultimate64StorageVolumeError`` is COULD NOT RUN: exit 2, the volumes + named, no traceback, no ``Ultimate64Client`` built. A directory holding + anything but TRUST.A/B (listed, or TRUST.P probed by name) is exit 2 + too, and is left untouched. Any other storage error (FTP unreachable, + lock not held) is a failure, exit 1, never "unavailable". +3. **Every storage call runs under the lock**, cleanup included; an error + from a read is never swallowed as "file absent". +4. **No zero-byte trust file is ever written.** +5. **Cleanup removes the directory.** Leftovers found at SETUP predate the + run (exit 2); found at phase 14 or cleanup they were written during it, + by the program under test (setup proved the directory empty, the lock + excludes other writers): exit 1, named, never forced. forced. +6. **No private FTP, no private /Temp GC**: neither trust rig imports + ``ftplib`` or ``_temp_gc``, nor does ``rig_https_m3_demo.py`` import + ``ftplib``; no rig imports ``_temp_gc``, and the module is gone. + +Runs under pytest, and standalone:: + + python3 tools/test_trust_rig_storage.py +""" +from __future__ import annotations + +import ast +import contextlib +import importlib.util +import io +import os +import sys +import tempfile +from pathlib import Path + +REPO = Path(__file__).resolve().parent.parent +UCI = REPO / "tools" / "uci" +RIGS = ("rig_trust_store.py", "rig_trust_policy.py") +for p in (str(UCI), str(REPO / "tools")): + if p not in sys.path: + sys.path.insert(0, p) + +from c64_test_harness import ( # noqa: E402 + StorageEntry, + Ultimate64StorageError, + Ultimate64StorageNotEmptyError, + Ultimate64StorageVolumeError, +) + +C64U_VOLUMES = ["Flash", "Temp"] +U64E_VOLUMES = ["Flash", "Temp", "USB1"] +IMAGE_PATH = "/USB1/c64https-test/TRUST." +DIR = "/USB1/c64https-test" +_LOCK = {"held": False} + + +def _storage(): + import _trust_storage # noqa: PLC0415 - tools/uci is not a package + return _trust_storage + + +@contextlib.contextmanager +def _patched(obj, **attrs): + old = {k: getattr(obj, k) for k in attrs if hasattr(obj, k)} + for k, v in attrs.items(): + setattr(obj, k, v) + try: + yield + finally: + for k in attrs: + if k in old: + setattr(obj, k, old[k]) + else: + delattr(obj, k) + + +@contextlib.contextmanager +def _held(): + _LOCK["held"] = True + try: + yield + finally: + _LOCK["held"] = False + + +_FAKES = ("storage_volumes", "storage_writable_volumes", "storage_list_dir", + "storage_mkdir", "storage_put_file", "storage_get_file", + "storage_delete_file", "storage_rmdir") + + +class _Calls: + """Fake storage_* functions. Each refuses unless the fake lock is held.""" + + def __init__(self, volumes, *, listing=(), files=None, raises=None, + rmdir_raises=None, nth=None): + self.volumes = list(volumes) + self.listing = list(listing) # visible names in DIR + self.files = dict(files or {}) # path -> bytes, for gets + self.raises = dict(raises or {}) # op -> exception + self.rmdir_raises = rmdir_raises + self.nth = dict(nth or {}) # op -> (n, exception): the nth call raises + self.counts = {} + self.log, self.violations = [], [] + + def _op(self, op, *args): + self.log.append((op, *args)) + if not _LOCK["held"]: + self.violations.append(op) + raise Ultimate64StorageError(f"refusing {op}: this process does not hold " + "the device's DeviceLock (fake)") + if op in self.raises: + raise self.raises[op] + self.counts[op] = self.counts.get(op, 0) + 1 + if op in self.nth and self.nth[op][0] == self.counts[op]: + raise self.nth[op][1] + + def storage_volumes(self, host, **kw): + self._op("volumes") + return list(self.volumes) + + def storage_writable_volumes(self, host, **kw): + self._op("writable") + return [v for v in self.volumes if v.casefold() not in ("temp", "flash")] + + def storage_list_dir(self, host, path, **kw): + self._op("list", path) + return [StorageEntry(name=n, kind="file", size=1) for n in sorted(self.listing)] + + def storage_mkdir(self, host, path, **kw): + self._op("mkdir", path) + return True + + def storage_put_file(self, host, path, data, **kw): + self._op("put", path, bytes(data), kw) + + def storage_get_file(self, host, path, **kw): + self._op("get", path) + if path in self.files: + return self.files[path] + raise FileNotFoundError(path) + + def storage_delete_file(self, host, path, **kw): + self._op("delete", path, kw) + return False + + def storage_rmdir(self, host, path, **kw): + self._op("rmdir", path) + if self.rmdir_raises is not None: + raise self.rmdir_raises + return True + + def ops(self, name): + return [c for c in self.log if c[0] == name] + + def install(self): + return _patched(_storage(), **{n: getattr(self, n) for n in _FAKES}) + + +@contextlib.contextmanager +def _env(**kv): + old = {k: os.environ.get(k) for k in kv} + for k, v in kv.items(): + if v is None: + os.environ.pop(k, None) + else: + os.environ[k] = v + try: + yield + finally: + for k, v in old.items(): + if v is None: + os.environ.pop(k, None) + else: + os.environ[k] = v + + +# --- 1. volume selection ---------------------------------------------------------- + +def _select(volumes, image=IMAGE_PATH, explicit=None): + calls = _Calls(volumes) + with calls.install(), _held(): + return _storage().select_store_dir("dev", image, explicit), calls + + +def _unavailable(volumes, image=IMAGE_PATH, explicit=None): + m = _storage() + try: + got, _ = _select(volumes, image, explicit) + except m.StoreUnavailable as exc: + return exc + raise AssertionError(f"expected StoreUnavailable, got {got!r}") + + +def test_selects_the_images_writable_volume() -> None: + got, calls = _select(U64E_VOLUMES) + assert got == DIR, got + assert calls.ops("writable"), "storage_writable_volumes was not asked" + + +def test_uses_the_devices_spelling_of_the_volume() -> None: + got, _ = _select(U64E_VOLUMES, image="/usb1/c64https-test/TRUST.") + assert got == DIR, got + + +def test_writable_set_comes_from_the_harness() -> None: + """A volume storage_writable_volumes leaves out is never chosen, even + when storage_volumes lists it.""" + m = _storage() + calls = _Calls(U64E_VOLUMES) + calls.storage_writable_volumes = lambda host, **kw: (calls._op("writable"), [])[1] + with calls.install(), _held(): + try: + m.select_store_dir("dev", IMAGE_PATH) + except m.StoreUnavailable: + return + raise AssertionError("chose a volume storage_writable_volumes did not offer") + + +def test_no_writable_volume_names_what_exists() -> None: + exc = _unavailable(C64U_VOLUMES) + assert exc.available == C64U_VOLUMES + assert "/Flash" in str(exc) and "/Temp" in str(exc), str(exc) + + +def test_image_volume_absent_suggests_a_writable_one() -> None: + exc = _unavailable(["Flash", "SD", "Temp"]) + assert "TRUST_STORE_DIR=/SD/c64https-test" in str(exc), str(exc) + + +def test_explicit_dir_is_preferred_and_validated() -> None: + got, _ = _select(U64E_VOLUMES, explicit=DIR) + assert got == DIR + exc = _unavailable(["Flash", "Temp", "USB1", "USB2"], explicit="/USB2/c64https-test") + assert "rebuild with TRUST_STORE_DIR=/USB2/c64https-test" in str(exc), str(exc) + _unavailable(U64E_VOLUMES, explicit="/USB1/real-store") + + +def test_refused_volume_never_lists() -> None: + m = _storage() + calls = _Calls(U64E_VOLUMES) + with calls.install(), _held(): + for image in ("/Temp/c64https-test/TRUST.", "/flash/c64https-test/TRUST."): + try: + m.select_store_dir("dev", image) + except m.StoreUnavailable: + pass + else: + raise AssertionError(f"{image} accepted") + assert calls.log == [], calls.log + + +def test_only_the_dedicated_test_directory() -> None: + for image in ("/USB1/TRUST.", "/USB1/store/TRUST.", "/USB1/a/c64https-test/TRUST.", + "/USB1/c64https-test/TRUST.P"): + _unavailable(U64E_VOLUMES, image=image) + + +# --- 3/4/5. TrustFiles ---------------------------------------------------------------- + +def test_zero_byte_trust_file_is_refused() -> None: + m = _storage() + calls = _Calls(U64E_VOLUMES) + files = m.TrustFiles("dev", DIR) + with calls.install(), _held(): + for empty in (b"", bytearray()): + try: + files.put(files.a, empty) + except ValueError as exc: + assert "zero-byte" in str(exc), str(exc) + else: + raise AssertionError("a zero-byte trust file was written") + files.put(files.b, b"C6TS\x01") + puts = calls.ops("put") + assert len(puts) == 1 and puts[0][1] == files.b and puts[0][3].get("overwrite") is True, puts + + +def test_delete_is_limited_to_our_two_names() -> None: + m = _storage() + calls = _Calls(U64E_VOLUMES) + files = m.TrustFiles("dev", DIR) + with calls.install(), _held(): + files.delete_ours() + try: + files.delete("TRUST.P") + except ValueError: + pass + else: + raise AssertionError("deleted a file that is not the rig's") + assert [c[1] for c in calls.log] == [f"{DIR}/TRUST.A", f"{DIR}/TRUST.B"], calls.log + assert all(c[2].get("missing_ok") for c in calls.log) + + +def test_a_read_error_is_not_file_absent() -> None: + """Only FileNotFoundError means "absent": a lock refusal or an FTP + failure from a read must propagate, not read as an empty slot.""" + m = _storage() + files = m.TrustFiles("dev", DIR) + calls = _Calls(U64E_VOLUMES) + with calls.install(): + try: # lock NOT held + got = files.get(files.a) + except Ultimate64StorageError: + pass + else: + raise AssertionError(f"a lock refusal read as {got!r}") + calls = _Calls(U64E_VOLUMES, raises={"get": Ultimate64StorageError("FTP dropped")}) + with calls.install(), _held(): + try: + got = files.get(files.a) + except Ultimate64StorageError: + pass + else: + raise AssertionError(f"an FTP failure read as {got!r}") + + +def test_foreign_entries_are_refused() -> None: + m = _storage() + files = m.TrustFiles("dev", DIR) + for calls, what in ((_Calls(U64E_VOLUMES, listing=["TRUST.A", "notes.txt"]), "notes.txt"), + (_Calls(U64E_VOLUMES, files={f"{DIR}/TRUST.P": b"x"}), "TRUST.P")): + with calls.install(), _held(): + try: + files.check_only_ours() + except m.StoreUnavailable as exc: + assert what in str(exc), str(exc) + else: + raise AssertionError(f"{what} in the directory was accepted") + calls = _Calls(U64E_VOLUMES, listing=["TRUST.A", "TRUST.B"]) + with calls.install(), _held(): + files.check_only_ours() + assert (("get", f"{DIR}/TRUST.P")) in calls.log, "TRUST.P was not probed by name" + + +def test_hidden_leftovers_are_reported_not_forced() -> None: + m = _storage() + files = m.TrustFiles("dev", DIR) + calls = _Calls(U64E_VOLUMES, rmdir_raises=Ultimate64StorageNotEmptyError("550", [])) + with calls.install(), _held(): + try: + files.remove_dir() + except m.StoreLeftovers as exc: + assert "hidden" in str(exc), str(exc) + else: + raise AssertionError("hidden leftovers did not raise StoreLeftovers") + assert len(calls.ops("rmdir")) == 1 and not calls.ops("delete"), calls.log + with _Calls(U64E_VOLUMES).install(), _held(): + assert files.remove_dir() is True + + +def test_final_exit_folds_cleanup() -> None: + m = _storage() + with contextlib.redirect_stdout(io.StringIO()): + assert m.final_exit(0, (0, "")) == 0 + assert m.final_exit(0, (1, "x")) == 1 + assert m.final_exit(1, (1, "x")) == 1 + assert m.final_exit(2, (1, "x")) == 1 + + +def test_setup_leftovers_say_they_predate_the_run() -> None: + m = _storage() + files = m.TrustFiles("dev", DIR) + calls = _Calls(U64E_VOLUMES, rmdir_raises=Ultimate64StorageNotEmptyError("550", [])) + with calls.install(), _held(): + try: + files.prepare() + except m.StoreLeftovers as exc: + assert "before this run" in str(exc), str(exc) + else: + raise AssertionError("prepare() accepted a directory it cannot remove") + assert not calls.ops("mkdir"), calls.log + + +# --- phase 14 (rig_trust_store) and the CLOSED waits (rig_trust_policy), offline ---- + +class _NopathRig: + """Just enough of rig_trust_store.Rig for phase_nopath.""" + + def __init__(self, store_rig, ftp, calls, reason): + self.ftp, self.calls, self.reason = ftp, calls, reason + self.passed, self.failed, self.notes = 0, [], {} + self.check = store_rig.Rig.check.__get__(self) + self.require = store_rig.Rig.require.__get__(self) + + def load(self, host): + self.calls.log.append(("c64-load", host)) + return 1, 3, {"reason": self.reason} + + +def _phase14(calls, reason=None): + store_rig = _load_rig("rig_trust_store.py") + import trust_store as ts # noqa: PLC0415 + files = _storage().TrustFiles("dev", DIR) + r = _NopathRig(store_rig, files, calls, ts.R_NOPATH if reason is None else reason) + exc = None + with calls.install(), _held(), contextlib.redirect_stdout(io.StringIO()): + try: + store_rig.phase_nopath(r) + except Exception as e: # noqa: BLE001 + exc = e + return r, exc + + +def test_phase14_removes_loads_then_restores() -> None: + calls = _Calls(U64E_VOLUMES) + r, exc = _phase14(calls) + assert exc is None and not r.failed and r.passed >= 2, (exc, r.failed) + seq = [c[0] for c in calls.log] + i_rm, i_load = seq.index("rmdir"), seq.index("c64-load") + assert i_rm < i_load, seq + assert "mkdir" in seq[i_load:], f"the directory was not put back after the load: {seq}" + + +def test_phase14_stops_when_the_directory_was_not_removed() -> None: + calls = _Calls(U64E_VOLUMES) + calls.storage_rmdir = lambda host, path, **kw: (calls._op("rmdir", path), False)[1] + r, exc = _phase14(calls) + assert exc is not None and r.failed, "phase 14 went on after rmdir removed nothing" + assert "c64-load" not in [c[0] for c in calls.log], calls.log + + +def test_phase14_checks_the_reason() -> None: + r, exc = _phase14(_Calls(U64E_VOLUMES), reason=0) + assert r.failed and any("NOPATH" in f for f in r.failed), r.failed + + +def test_closed_wait_is_a_check() -> None: + pol = _load_rig("rig_trust_policy.py") + + class R: + def __init__(self, screen): + self.screen, self.passed, self.failed = screen, 0, [] + + def wait(self, marker, budget): + return self.screen if marker == pol.CLOSED else None + + def check(self, cond, what): + if cond: + self.passed += 1 + else: + self.failed.append(what) + + r = R(None) + pol.await_closed(r, "match") + assert r.failed == [f"match: {pol.CLOSED}"], r.failed + r = R("... CONNECTION CLOSED") + pol.await_closed(r, "match") + assert not r.failed and r.passed == 1 + assert pol.CLOSED == "CONNECTION CLOSED" + + +def test_phases_use_the_checked_closed_wait() -> None: + tree = ast.parse((UCI / "rig_trust_policy.py").read_text()) + fn = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == "phases_local") + calls = [n for n in ast.walk(fn) if isinstance(n, ast.Call)] + awaited = [c for c in calls if getattr(c.func, "id", None) == "await_closed"] + assert len(awaited) >= 2, f"phases_local calls await_closed {len(awaited)} time(s), want 2" + bare = [c for c in calls if getattr(c.func, "attr", None) == "wait" and c.args + and isinstance(c.args[0], ast.Name) and c.args[0].id == "CLOSED"] + assert not bare, "an unchecked run.wait(CLOSED) is back in phases_local" + + +# --- 2. the rigs' exit codes ----------------------------------------------------------- + +class _FakeLock: + instances: list = [] + + def __init__(self, host, *a, **kw): + self.host, self.released = host, False + _FakeLock.instances.append(self) + + def release(self): + self.released = True + _LOCK["held"] = False + + +def _acquire(lock): + _LOCK["held"] = True + + +class _ClientBuilt(AssertionError): + pass + + +def _no_client(*a, **kw): + raise _ClientBuilt("an Ultimate64Client was built: the device would be touched") + + +def _no_network(*a, **kw): + raise OSError("test: real FTP attempted") + + +class _Arbiter: + def __init__(self): + self.next = 0x0334 + + def alloc(self, n, name=""): + a, self.next = self.next, self.next + n + return a + + +def _load_rig(name): + spec = importlib.util.spec_from_file_location(f"_ut_{name[:-3]}", UCI / name) + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +def _labels(rig): + names = set(getattr(rig, "NEEDED", ())) + names |= {"trust_pre", "trust_post", "tp_mode", "net_tcp_state", "tls_state", + "tls_reached_connected", "ts_name", "http_host_target", "ts_letter"} + labels = {n: 0x6000 + i for i, n in enumerate(sorted(names))} + labels["http_host_target"] = 0x0801 + return labels + + +class _FakeClient: + def __init__(self, calls): + self.calls = calls + + def reset(self): + self.calls.log.append(("reset",)) + + def get_info(self): + return {} + + def send_text(self, *a, **kw): + pass + + def read_mem(self, addr, n): + return bytes(n) + + def write_mem(self, *a, **kw): + pass + + +class _FakeTransport: + def __init__(self, **kw): + self.memory_policy = None + + +class _FakeRun: + def __init__(self, client, L): + self.passed, self.failed, self.notes = 0, [], {} + + def wait(self, *a, **kw): + return "screen" + + def check(self, cond, what): + if cond: + self.passed += 1 + else: + self.failed.append(what) + + def settled(self, *a): + return True + + def r8(self, name): + return 0 + + +def _full_run_patches(rig, calls): + """Let main() run to the end with no device: every device step faked, + the phases reduced to one passing check.""" + noop = lambda *a, **kw: None # noqa: E731 + p = dict(Ultimate64Client=lambda **kw: _FakeClient(calls), enable_uci=noop, + disable_uci=noop, prepare_device=noop, preflight_reu=noop, + load_verified_and_run=noop) + if hasattr(rig, "Ultimate64Transport"): + p["Ultimate64Transport"] = _FakeTransport + if hasattr(rig, "run_phases"): + p["run_phases"] = lambda r: r.check(True, "fake phase") + if hasattr(rig, "phases_local"): + p.update(Run=_FakeRun, guard_socket_teardown=noop, + phases_local=lambda run, *a: run.check(True, "fake phase")) + return p + + +def _run_main(rig_name, calls, *, full=False, extra=None): + rig = _load_rig(rig_name) + tmp = Path(tempfile.mkdtemp(prefix="trust_rig_ut_")) + prg = tmp / "c64-https.prg" + prg.write_bytes(b"\x01\x08dev.example\x00" + b"\x00" * 64) + labels_path = tmp / "labels.txt" + labels_path.write_text("") + _FakeLock.instances = [] + _LOCK["held"] = False + patches = dict( + PRG_PATH=prg, LABELS_PATH=labels_path, load_labels=lambda: _labels(rig), + image_store_path=lambda prg, L: IMAGE_PATH, DeviceLock=_FakeLock, + acquire_device_lock=_acquire, Ultimate64Client=_no_client, + build_policy_and_low_ram_arbiter=lambda *a: (None, _Arbiter()), + REPO=tmp, + ) + # Master's rigs: keep a regression from reaching a real device. + for legacy in ("FTP", "gc_temp"): + if hasattr(rig, legacy): + patches[legacy] = _no_network + peers = [sys.modules[n] for n in ("rig_trust_store",) if n in sys.modules] + out, err = io.StringIO(), io.StringIO() + rc = exc = None + if full: + patches.update(_full_run_patches(rig, calls)) + patches.update(extra or {}) + with contextlib.ExitStack() as st: + st.enter_context(_patched(rig, **patches)) + if full and hasattr(rig, "Rig"): + st.enter_context(_patched(rig.Rig, boot=lambda self: None)) + for peer in peers: + if hasattr(peer, "FTP"): + st.enter_context(_patched(peer, FTP=_no_network)) + st.enter_context(calls.install()) + st.enter_context(_env(TRUST_STORE_DIR=None, RIG_MODE=None)) + st.enter_context(contextlib.redirect_stdout(out)) + st.enter_context(contextlib.redirect_stderr(err)) + try: + rc = rig.main() + except BaseException as e: # noqa: BLE001 - a raise is the failure + exc = e + _LOCK["held"] = False + return rc, exc, out.getvalue() + err.getvalue() + + +def _check_lock(rig_name, calls, text): + assert not calls.violations, \ + f"{rig_name}: storage called without the DeviceLock: {calls.violations}\n{text}" + assert _FakeLock.instances and all(lk.released for lk in _FakeLock.instances), \ + f"{rig_name}: the DeviceLock was not released" + + +def _assert_exit(rig_name, calls, want, *, names=(), absent=()): + rc, exc, text = _run_main(rig_name, calls) + assert exc is None, f"{rig_name}: main() raised {type(exc).__name__}: {exc}\n{text}" + assert rc == want, f"{rig_name}: exit {rc}, want {want}\n{text}" + assert "Traceback" not in text, text + for n in names: + assert n in text, f"{rig_name}: output does not name {n}\n{text}" + for n in absent: + assert n not in text, f"{rig_name}: output says {n!r}\n{text}" + _check_lock(rig_name, calls, text) + return text + + +def _volume_error(): + return Ultimate64StorageVolumeError( + "/USB1 is listed but cannot be entered on dev; usable volumes: /Flash, /Temp", + ["Flash", "Temp"]) + + +def _both(fn): + for rig_name in RIGS: + fn(rig_name) + + +def test_rigs_exit_2_with_no_writable_volume() -> None: + _both(lambda r: _assert_exit(r, _Calls(C64U_VOLUMES), 2, names=("/Flash", "/Temp"))) + + +def test_rigs_exit_2_on_volume_error() -> None: + _both(lambda r: _assert_exit(r, _Calls(U64E_VOLUMES, raises={"mkdir": _volume_error()}), + 2, names=("/Flash", "/Temp"))) + + +def test_rigs_exit_2_on_foreign_files_and_touch_nothing() -> None: + def one(r): + calls = _Calls(U64E_VOLUMES, listing=["TRUST.A", "keep.me"]) + _assert_exit(r, calls, 2, names=("keep.me",)) + touched = [c for c in calls.log if c[0] in ("delete", "rmdir", "put", "mkdir")] + assert not touched, f"{r}: touched a foreign directory: {touched}" + _both(one) + + +def test_rigs_fail_not_skip_on_other_storage_errors() -> None: + """FTP unreachable is a failure (exit 1), not 'no usable volume'.""" + _both(lambda r: _assert_exit( + r, _Calls(U64E_VOLUMES, raises={"writable": Ultimate64StorageError("cannot reach FTP")}), + 1, names=("cannot reach FTP",), absent=("COULD NOT RUN", "unavailable"))) + + +def test_rigs_exit_2_on_hidden_leftovers_before_touching_the_device() -> None: + _both(lambda r: _assert_exit( + r, _Calls(U64E_VOLUMES, rmdir_raises=Ultimate64StorageNotEmptyError("550", [])), + 2, names=("hidden",))) + + +def _full(rig_name, calls, **extra): + rc, exc, text = _run_main(rig_name, calls, full=True, extra=extra) + assert exc is None, f"{rig_name}: main() raised {type(exc).__name__}: {exc}\n{text}" + assert "Traceback" not in text, text + _check_lock(rig_name, calls, text) + return rc, text + + +def test_rigs_full_offline_run_passes_and_removes_the_directory() -> None: + def one(r): + calls = _Calls(U64E_VOLUMES) + rc, text = _full(r, calls) + assert rc == 0, f"{r}: exit {rc}\n{text}" + assert len(calls.ops("rmdir")) == 2, f"{r}: setup + cleanup rmdir: {calls.log}" + _both(one) + + +def test_rigs_cleanup_failure_is_not_exit_0() -> None: + def one(r): + calls = _Calls(U64E_VOLUMES, nth={"rmdir": (2, Ultimate64StorageError("FTP dropped"))}) + rc, text = _full(r, calls) + assert rc == 1, f"{r}: a failed cleanup rmdir exited {rc}\n{text}" + calls = _Calls(U64E_VOLUMES, nth={"delete": (3, Ultimate64StorageError("FTP dropped"))}) + rc, text = _full(r, calls) + assert rc == 1, f"{r}: a failed cleanup delete exited {rc}\n{text}" + _both(one) + + +def test_rigs_cleanup_leftovers_are_a_failure() -> None: + """A stray TRUST.C (visible) or hidden entry at cleanup was written + during the run: exit 1, named -- not COULD NOT RUN.""" + def one(r): + for entries, name in ((["TRUST.C"], "TRUST.C"), ([], "hidden")): + err = Ultimate64StorageNotEmptyError("not empty", entries) + calls = _Calls(U64E_VOLUMES, nth={"rmdir": (2, err)}) + rc, text = _full(r, calls) + assert rc == 1 and name in text and "DURING the run" in text, \ + f"{r}: leftovers {entries} exited {rc}\n{text}" + assert "COULD NOT RUN" not in text, text + _both(one) + + +def test_store_rig_phase14_leftovers_are_a_failure() -> None: + calls = _Calls(U64E_VOLUMES, + rmdir_raises=Ultimate64StorageNotEmptyError("not empty", ["TRUST.C"])) + r, exc = _phase14(calls) + assert exc is not None and any("TRUST.C" in f and "DURING the run" in f for f in r.failed), \ + (exc, r.failed) + assert "c64-load" not in [c[0] for c in calls.log], calls.log + + +def test_store_rig_resets_before_cleanup() -> None: + calls = _Calls(U64E_VOLUMES) + rc, text = _full("rig_trust_store.py", calls) + seq = [c[0] for c in calls.log] + resets = [i for i, op in enumerate(seq) if op == "reset"] + first_cleanup = seq.index("delete", seq.index("mkdir")) + assert resets and resets[-1] < first_cleanup, f"cleanup ran before the C64 was reset: {seq}" + + +def test_rigs_clean_up_under_the_lock() -> None: + """Past setup, any end of the run deletes TRUST.A/B and removes the + directory, and every one of those calls happens before the lock is + released.""" + def one(r): + calls = _Calls(U64E_VOLUMES) + rc, exc, text = _run_main(r, calls) + assert isinstance(exc, _ClientBuilt), f"{r}: {rc} {exc!r}\n{text}" + _check_lock(r, calls, text) + assert calls.ops("rmdir"), f"{r}: the test directory was not removed\n{text}" + assert len(calls.ops("delete")) >= 4, f"{r}: cleanup deletes missing: {calls.log}" + _both(one) + + +# --- 6. no private FTP, no private /Temp GC ------------------------------------------ + +def _imports(path: Path) -> set[str]: + out = set() + for node in ast.walk(ast.parse(path.read_text(), str(path))): + if isinstance(node, ast.Import): + out |= {a.name.split(".")[0] for a in node.names} + elif isinstance(node, ast.ImportFrom) and node.module: + out.add(node.module.split(".")[0]) + return out + + +def test_rigs_import_no_ftplib_and_no_temp_gc() -> None: + for name in RIGS + ("_trust_storage.py", "rig_https_m3_demo.py"): + bad = _imports(UCI / name) & {"ftplib", "_temp_gc"} + assert not bad, f"{name} imports {sorted(bad)}" + + +def test_no_rig_imports_the_retired_temp_gc() -> None: + assert not (UCI / "_temp_gc.py").exists(), "tools/uci/_temp_gc.py still exists" + offenders = [str(p.relative_to(REPO)) for d in (UCI, REPO / "tools", REPO / "tests") + for p in sorted(d.glob("*.py")) if "_temp_gc" in _imports(p)] + assert not offenders, f"still import _temp_gc: {offenders}" + + +def _main(tests=None) -> int: + """Run every case without pytest; any exception is that case's failure.""" + if tests is None: + tests = {n: f for n, f in globals().items() + if n.startswith("test_") and callable(f)} + failures = 0 + for name, fn in sorted(tests.items()): + try: + fn() + except AssertionError as exc: + failures += 1 + print(f"FAIL {name}: {str(exc).splitlines()[0] if str(exc) else ''}") + except Exception as exc: # noqa: BLE001 - a raise is a result + failures += 1 + print(f"ERROR {name}: {type(exc).__name__}: {exc}") + else: + print(f"ok {name}") + print("FAILED" if failures else "PASS") + from _skip_policy import verdict + return verdict(len(tests) - failures, failures, + certifies="the trust rigs' storage-API use") + + +if __name__ == "__main__": + sys.exit(_main()) diff --git a/tools/uci/README.md b/tools/uci/README.md index ccd1b2ec..8b6e920b 100644 --- a/tools/uci/README.md +++ b/tools/uci/README.md @@ -22,12 +22,14 @@ deliberate — see "Why not pytest" below. | `rig_https_print_body.py` | issue #28 — the decrypted body renders correctly on screen | | `rig_https_refetch.py` | several menu-driven HTTPS fetches in ONE boot — per-connection state a second `tls_connect` would inherit | | `rig_https_local_p384.py` | the P-384 cert profile (blocked: no P-384 PRG builds today) | +| `rig_trust_store.py` | the trust store's UCI DOS I/O against a real volume (#155) | +| `rig_trust_policy.py` | the trust policy end to end through the menu (#155) | | `bench_ecdsa_u64e.py` | ECDSA-P256 verify wall-clock across a clock sweep | Files with a leading underscore are helper modules, not entry points: `_device_lock_helper.py`, `_memory_policy.py`, `_device_prep.py`, `_reu_preflight.py`, `_sni_precondition.py`, `_ecdsa_vectors.py`, -`_analyze_ecdsa_trace.py`. +`_analyze_ecdsa_trace.py`, `_trust_storage.py`. `_sni_precondition.py` is pure logic and carries its own checks: ```sh @@ -92,6 +94,31 @@ ON. Policy and tests: `tools/uci/_device_prep.py`, `tools/test_device_prep.py` (faked client, no hardware). +## `/Temp` hygiene and device storage + +There is no private `/Temp` GC here any more (`_temp_gc.py` is retired). +c64-test-harness sweeps the firmware's leaked `/Temp` attachments (the +writemem exhaustion wedge, GideonZ/1541ultimate#686) for any +`Ultimate64Client` on a leak-prone device while this process holds the +DeviceLock, so a rig only has to build its client the default way (no +`write_mem_query_threshold`, no `temp_hygiene=False`) inside the lock. + +Files on the device's volumes go through the harness's `storage_*` API, +never a private FTP session. The two trust rigs keep TRUST.A / TRUST.B in +`//c64https-test` (the image's `TRUST_STORE_DIR`) via +`_trust_storage.py`: the volume must be in `storage_writable_volumes()`, +and an explicit `TRUST_STORE_DIR` must match the image. A device with no +such volume — a C64U with no medium lists only `Flash` and `Temp` — a +directory holding anything but TRUST.A/B (listed, or TRUST.P probed by +name, since listings omit hidden names), or one that cannot be removed +because of hidden entries (`.DS_Store`, `._*`; setup proves it removable) +is COULD NOT RUN, exit 2, before anything on it is touched. Any other +storage error (lock, FTP) is exit 1. The directory is removed at the end. +Leftovers met at phase 14 or at cleanup were written during the run (setup +proved the directory empty; the lock excludes other writers), so they are +a failure, exit 1, reported and never forced; so is any cleanup failure. Tests: `tools/test_trust_rig_storage.py` (faked storage that +refuses without the fake lock, no hardware). + ## Listener and socket lifecycle (#246, #234) `_rig_lifecycle.py`, pinned by `tools/test_rig_lifecycle.py` (no hardware): diff --git a/tools/uci/_device_prep.py b/tools/uci/_device_prep.py index f11a7f66..0e22f495 100644 --- a/tools/uci/_device_prep.py +++ b/tools/uci/_device_prep.py @@ -258,7 +258,7 @@ def _turbo_failure_message(state: dict[str, str | None], mhz: int) -> str: "refusing\n" " instantly while ping still answers is the writemem exhaustion " "wedge\n" - " (GideonZ/1541ultimate#686) — tools/uci/_temp_gc.py. Do NOT jump " + " (GideonZ/1541ultimate#686); the harness sweeps /Temp. Do NOT jump " "to\n" " 'firmware corruption'; that verdict has been reached wrongly " "and\n" @@ -323,7 +323,7 @@ def _reu_write_failure_message(exc: Exception, reu_size: str, "refusing\n" " instantly while ping still answers is the writemem exhaustion " "wedge\n" - " (GideonZ/1541ultimate#686) — tools/uci/_temp_gc.py. Do NOT jump " + " (GideonZ/1541ultimate#686); the harness sweeps /Temp. Do NOT jump " "to\n" " 'firmware corruption'; that verdict has been reached wrongly " "and\n" diff --git a/tools/uci/_reu_preflight.py b/tools/uci/_reu_preflight.py index b942a096..b3e0ebfc 100644 --- a/tools/uci/_reu_preflight.py +++ b/tools/uci/_reu_preflight.py @@ -436,7 +436,7 @@ def _unreadable_message(detail: str, reason: str) -> str: "refuses\n" " instantly while ping still answers, that is the writemem " "exhaustion\n" - " wedge (GideonZ/1541ultimate#686) — tools/uci/_temp_gc.py. Run " + " wedge (GideonZ/1541ultimate#686); the harness sweeps /Temp. Run " "the\n" " diagnostic ladder; do NOT jump to 'firmware corruption', which " "is a\n" diff --git a/tools/uci/_temp_gc.py b/tools/uci/_temp_gc.py deleted file mode 100644 index c85a2d0f..00000000 --- a/tools/uci/_temp_gc.py +++ /dev/null @@ -1,79 +0,0 @@ -"""Userspace GC for the U64E's /Temp attachment leak (fw <= 3.14d). - -Every REST call that carries a body — run_prg's PRG upload, the keystroke -trigger POST, machine:writemem — lands as a file in /Temp on the device, -and released firmware never deletes them: the "writemem exhaustion wedge". -When /Temp fills (~15 rig runs at ~63 KB PRG + 1 small attachment per -run), the REST API *and* the C64-facing UCI bridge wedge together and -only a power cycle recovers. Upstream fixed it in -GideonZ/1541ultimate#686 (auto-cleanup keeping the youngest 10 managed -files), but no released firmware carries it yet. - -This helper mirrors that policy from the host side over FTP: it deletes -/Temp files matching ``temp`` — the firmware's managed-attachment -naming — oldest-first, keeping the youngest ``keep``. It never touches -any other /Temp entry (user files, mounted images). - -Call ``gc_temp(host)`` at rig start (rigs run it right after enable_uci, -under the DeviceLock) or run standalone: - - python3 tools/uci/_temp_gc.py [host] - -Environment: ``U64_HOST`` (default 192.168.1.81), ``C64_SKIP_TEMP_GC=1`` -to bypass, ``TEMP_GC_KEEP`` to override the keep count (default 2). -""" - -from __future__ import annotations - -import os -import re -import sys -from ftplib import FTP, error_perm - -# The firmware's attachment counter is HEX: temp0009 is followed by -# temp000A (observed live). A \d+ pattern silently skips lettered names. -_MANAGED = re.compile(r"^temp[0-9a-fA-F]+$") - - -def gc_temp(host: str, keep: int | None = None, timeout: float = 10.0) -> int: - """Delete managed /Temp attachments, keeping the youngest *keep*. - - Returns the number of files deleted. Any FTP failure is reported and - swallowed — GC is best-effort hygiene and must never fail a rig run. - """ - if os.environ.get("C64_SKIP_TEMP_GC") == "1": - return 0 - if keep is None: - keep = int(os.environ.get("TEMP_GC_KEEP", "2")) - deleted = 0 - try: - with FTP(host, timeout=timeout) as ftp: - ftp.login() - ftp.cwd("/Temp") - # NLST order is the firmware's directory order, which is - # creation order for the monotonically numbered tempNNNN - # files; the numeric sort below makes oldest-first explicit - # (and survives a counter that skips). - names = [n for n in ftp.nlst() if _MANAGED.match(n)] - names.sort(key=lambda n: int(n[4:], 16)) - victims = names[: max(0, len(names) - keep)] if keep else names - for name in victims: - try: - ftp.delete(name) - deleted += 1 - except error_perm as exc: - # e.g. still referenced; skip — the next GC gets it - print(f" temp-gc: skip {name}: {exc}", file=sys.stderr) - except OSError as exc: - print(f" temp-gc: FTP unavailable on {host}: {exc}", file=sys.stderr) - return deleted - if deleted: - print(f" temp-gc: deleted {deleted} /Temp attachment(s) " - f"(kept youngest {keep}) — writemem-wedge budget restored") - return deleted - - -if __name__ == "__main__": - target = sys.argv[1] if len(sys.argv) > 1 else \ - os.environ.get("U64_HOST", "192.168.1.81") - gc_temp(target) diff --git a/tools/uci/_trust_storage.py b/tools/uci/_trust_storage.py new file mode 100644 index 00000000..c318647c --- /dev/null +++ b/tools/uci/_trust_storage.py @@ -0,0 +1,248 @@ +"""The trust rigs' TRUST.A / TRUST.B files, through c64-test-harness storage (#155). + +rig_trust_store.py and rig_trust_policy.py read, write and delete the store +files on the device's removable volume. They do it only through the +harness's storage API (``storage_*``), never over a private FTP session, so +the harness's rules hold for every call: the DeviceLock must be held by +this process, ``/Temp`` and ``/Flash`` are never written, paths are +validated, and a failed new-file upload is removed. + +The store directory is baked into the image (``TRUST_STORE_DIR`` at build +time), so the rig cannot move it at run time; it can only check that the +image's directory is somewhere the device can write. ``select_store_dir`` +does that: + +* the directory must be ``//c64https-test`` -- the rigs delete + TRUST.A / TRUST.B there and remove the directory, so they refuse any + directory a real store could live in; +* an explicit ``TRUST_STORE_DIR`` in the environment wins, and must be the + image's directory (otherwise the C64 writes one place and the rig reads + another); +* the volume must be in ``storage_writable_volumes(host)``. + +Exit-code contract for a rig's setup: ``StoreUnavailable`` and +``Ultimate64StorageVolumeError`` mean "this device cannot host the test +store" -- COULD NOT RUN, exit 2. A C64U with no medium (``['Flash', +'Temp']``) lands there by design. Every other storage error (lock not +held, FTP unreachable, a transfer failure) is a real failure, never +reported as a missing volume. + +``check_only_ours`` refuses a directory holding anything but TRUST.A / +TRUST.B. Listings omit hidden names (a leading ``.``, or the hidden +attribute), so TRUST.P -- which a TRUST_BUNDLE build reads -- is also +probed by name; a hidden file is caught only when ``storage_rmdir`` +refuses the directory, and is reported, never forced. +""" +from __future__ import annotations + +from c64_test_harness import ( + WRITE_REFUSED_VOLUMES, + Ultimate64StorageNotEmptyError, + Ultimate64StorageVolumeError, + storage_delete_file, + storage_get_file, + storage_list_dir, + storage_mkdir, + storage_put_file, + storage_rmdir, + storage_volumes, + storage_writable_volumes, +) + +TEST_DIR_NAME = "c64https-test" +OURS = ("TRUST.A", "TRUST.B") +#: Read by the image when present (TRUST_BUNDLE=1), so it must not be. +PROBED = ("TRUST.P",) +SLOT_SUFFIX = "/TRUST." + + +class StoreUnavailable(Exception): + """The store directory cannot be used on this device or with this image.""" + + def __init__(self, message: str, available: list[str] | None = None) -> None: + super().__init__(message) + self.available = list(available or []) + + +class StoreLeftovers(StoreUnavailable): + """The emptied test directory cannot be removed: it still holds entries + -- hidden ones the listing omits (.DS_Store, ._*) when none are named. + + Who put them there decides the exit code. Met at SETUP they predate the + run: device state, COULD NOT RUN (2). Met at phase 14 or at cleanup they + were written DURING the run -- setup proved the directory removable and + the DeviceLock excludes other FTP writers -- so the program under test + wrote them: a failure (1).""" + + def __init__(self, directory: str, what: str) -> None: + super().__init__(f"{directory} cannot be removed: it still holds {what}") + self.what = what + + +#: Setup errors that mean COULD NOT RUN (exit 2). Nothing else does. +CANNOT_RUN_ERRORS = (StoreUnavailable, Ultimate64StorageVolumeError) + + +def _vols(names) -> str: + return ", ".join("/" + n for n in names) or "(none)" + + +def image_dir(store_path: str) -> str: + """The directory of an image's slot path ``//c64https-test/TRUST.``. + + Raises StoreUnavailable for any other shape (no volume, not the + dedicated test directory).""" + if not store_path.endswith(SLOT_SUFFIX): + raise StoreUnavailable(f"the image's store path {store_path!r} does not end in " + f"{SLOT_SUFFIX!r}") + d = store_path[: -len(SLOT_SUFFIX)] + parts = d.split("/") + if len(parts) != 3 or parts[0] != "" or not parts[1] or parts[2] != TEST_DIR_NAME: + raise StoreUnavailable( + f"the image stores at {store_path!r}; the trust rigs only run against " + f"TRUST_STORE_DIR=//{TEST_DIR_NAME} (they delete TRUST.A/TRUST.B there)") + return d + + +def select_store_dir(host: str, store_path: str, explicit: str | None = None) -> str: + """The test directory to use, in the device's spelling of its volume. + + *store_path* is the slot path baked into the image; *explicit* is + ``$TRUST_STORE_DIR`` (or None). Needs the DeviceLock (it lists volumes). + """ + want = image_dir(store_path) + if explicit: + explicit = explicit.rstrip("/") + image_dir(explicit + SLOT_SUFFIX) # same shape rules + if explicit.casefold() != want.casefold(): + raise StoreUnavailable( + f"TRUST_STORE_DIR={explicit} but the image was built for {want}; " + f"rebuild with TRUST_STORE_DIR={explicit}") + want = explicit + vol = want.split("/")[1] + if vol.casefold() in WRITE_REFUSED_VOLUMES: + raise StoreUnavailable(f"{want} is on /{vol}, which the harness never writes") + writable = storage_writable_volumes(host) + for v in writable: + if v.casefold() == vol.casefold(): + return f"/{v}/{TEST_DIR_NAME}" + volumes = storage_volumes(host) # only to name them + if not writable: + raise StoreUnavailable( + f"{host} has no writable storage volume (available: {_vols(volumes)}; " + f"/Temp and /Flash are never written). Insert a USB stick or SD card.", + volumes) + raise StoreUnavailable( + f"/{vol} is not a writable volume on {host} (available: {_vols(volumes)}); " + f"rebuild with TRUST_STORE_DIR=/{writable[0]}/{TEST_DIR_NAME}", volumes) + + +class TrustFiles: + """TRUST.A / TRUST.B in one test directory, via the harness storage API.""" + + def __init__(self, host: str, directory: str) -> None: + self.host, self.dir = host, directory + self.a, self.b = f"{directory}/TRUST.A", f"{directory}/TRUST.B" + + def get(self, path: str) -> bytes | None: + """The file's bytes, or None when it does not exist. Every other + error (lock not held, FTP failure) propagates.""" + try: + return storage_get_file(self.host, path) + except FileNotFoundError: + return None + + def put(self, path: str, data: bytes) -> None: + """Write a store image. Zero bytes is refused here, by name: the + store format never produces one (header + checksum is 16 B), the + harness refuses it, and the U64E would create no file for it, so a + rig that wants "no file" must call delete().""" + if not data: + raise ValueError(f"refusing a zero-byte trust file at {path}: " + "use TrustFiles.delete() for an absent slot") + if path not in (self.a, self.b): + raise ValueError(f"{path} is not one of this rig's files ({self.a}, {self.b})") + storage_put_file(self.host, path, bytes(data), overwrite=True) + + def delete(self, name: str) -> None: + if name not in OURS: + raise ValueError(f"{name} is not one of this rig's files {OURS}") + storage_delete_file(self.host, f"{self.dir}/{name}", missing_ok=True) + + def delete_ours(self) -> None: + for n in OURS: + self.delete(n) + + def check_only_ours(self) -> None: + """StoreUnavailable if the directory exists and holds anything but + TRUST.A / TRUST.B (visible entries, or TRUST.P by name).""" + try: + names = [e.name for e in storage_list_dir(self.host, self.dir)] + except FileNotFoundError: + return + foreign = sorted(set(names) - set(OURS)) + for n in PROBED: + if n not in foreign and self.get(f"{self.dir}/{n}") is not None: + foreign.append(n) + if foreign: + raise StoreUnavailable( + f"{self.dir} holds files that are not the rig's: {foreign}; " + "move them away (the rig deletes TRUST.A/TRUST.B and removes the directory)") + + def ensure_dir(self) -> bool: + """Create the test directory if missing; True if it was created.""" + return storage_mkdir(self.host, self.dir, exist_ok=True) + + def remove_dir(self) -> bool: + """Remove the (emptied) test directory: True if removed, False if it + was absent. Leftovers raise StoreLeftovers -- hidden entries are + reported, never forced.""" + try: + return storage_rmdir(self.host, self.dir, missing_ok=True) + except Ultimate64StorageNotEmptyError as exc: + what = (f"visible entries {exc.entries}" if exc.entries else + "hidden entries the listing omits (e.g. .DS_Store, ._*)") + raise StoreLeftovers(self.dir, what) from exc + + def prepare(self) -> None: + """Setup, in order: refuse a foreign directory; prove the directory + can be removed (hidden leftovers are COULD NOT RUN here, before the + device is touched, not a phase-14 failure later); recreate it.""" + self.check_only_ours() + self.delete_ours() + try: + self.remove_dir() + except StoreLeftovers as exc: + raise StoreLeftovers(self.dir, exc.what + " from before this run; " + "remove them by hand") from exc + self.ensure_dir() + + +def clean_up(store: "TrustFiles | None") -> tuple[int, str]: + """End of run: delete TRUST.A/B and remove the directory, under the lock. + + Returns ``(0, msg)`` when done and ``(1, msg)`` otherwise: a cleanup + failure leaves TRUST.A/B behind, and leftovers here were written during + the run (see StoreLeftovers) -- neither may exit 0. Leftovers are + reported, never forced.""" + if store is None: + return 0, "cleanup: nothing to clean" + try: + store.delete_ours() + store.remove_dir() + except StoreLeftovers as exc: + return 1, (f"cleanup: {', '.join(OURS)} deleted, but {exc}; " + "written DURING the run (setup proved the directory removable and the DeviceLock excludes other writers), so the program under test left it") + except Exception as exc: # noqa: BLE001 - reported, and turns the exit non-zero + return 1, f"cleanup of {store.dir} FAILED: {type(exc).__name__}: {exc}" + return 0, f"cleanup: {', '.join(OURS)} deleted; {store.dir} removed" + + +def final_exit(rc: int, cleanup: tuple[int, str]) -> int: + """Fold the cleanup result into the run's exit code: a failed cleanup + turns any other code into 1.""" + crc, msg = cleanup + if crc == 0 or rc == 1: + return rc + print(f"[fatal] {msg}") + return 1 diff --git a/tools/uci/rig_close_retry.py b/tools/uci/rig_close_retry.py index 9a44ed17..9db34514 100755 --- a/tools/uci/rig_close_retry.py +++ b/tools/uci/rig_close_retry.py @@ -78,7 +78,6 @@ ) from _device_prep import DevicePrepError, prepare_device # noqa: E402 from _reu_preflight import ReuPreflightError, preflight_reu # noqa: E402 -from _temp_gc import gc_temp # noqa: E402 sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from _skip_policy import verdict # noqa: E402 from rig_https_local import ( # noqa: E402 @@ -423,7 +422,6 @@ def main() -> int: tr.memory_policy = policy enable_uci(client) uci_enabled = True - gc_temp(HOST) try: prepare_device(client, LABELS_PATH, turbo_mhz=TURBO_MHZ, artifact_dir=run_dir) diff --git a/tools/uci/rig_https_banner.py b/tools/uci/rig_https_banner.py index 1dea838f..54209d65 100755 --- a/tools/uci/rig_https_banner.py +++ b/tools/uci/rig_https_banner.py @@ -254,18 +254,6 @@ def main() -> int: enable_uci(client) uci_on = True - # fw <= 3.14d leaks one /Temp file per REST body; run_prg is a big - # writemem, and a session's worth of them wedges REST and the UCI - # bridge together (CLAUDE.md "writemem exhaustion wedge"). Cheap - # insurance, same as the live/wiki rigs. - if os.environ.get("C64_SKIP_TEMP_GC") != "1": - try: - from _temp_gc import gc_temp - removed = gc_temp(HOST) - print(f"/Temp GC: removed {removed} stale file(s)") - except Exception as exc: - print(f"WARNING: /Temp GC skipped: {exc}") - # --- Device prep: reset-then-configure (#197, #187, #212) ------- # This rig used to probe `CPU Speed` / `Turbo Control` inline and, # when the read failed, perform the write regardless — the #187 diff --git a/tools/uci/rig_https_live.py b/tools/uci/rig_https_live.py index b9d35b62..bf5d4c1f 100644 --- a/tools/uci/rig_https_live.py +++ b/tools/uci/rig_https_live.py @@ -101,7 +101,6 @@ from _rig_lifecycle import guard_socket_teardown, tls_evidence_addrs from _device_prep import DevicePrepError, prepare_device from _reu_preflight import ReuPreflightError, preflight_reu -from _temp_gc import gc_temp # Pure helpers shared with the local rig (all side-effect free; importing # the module only evaluates env defaults, it drives no hardware). @@ -443,12 +442,6 @@ def main() -> int: enable_uci(client) uci_enabled = True - # Userspace /Temp GC (fw <= 3.14d leaks one attachment per REST - # body; ~15 runs wedge REST + the UCI bridge together). Mirrors - # the GideonZ/1541ultimate#686 cleanup until a released firmware - # carries it. Best-effort; C64_SKIP_TEMP_GC=1 bypasses. - gc_temp(HOST) - # Wedged-runner pre-detect (see rig_https_local for the rationale). try: runner_health_check(client) diff --git a/tools/uci/rig_https_m3_demo.py b/tools/uci/rig_https_m3_demo.py index 222f09cc..1e182271 100644 --- a/tools/uci/rig_https_m3_demo.py +++ b/tools/uci/rig_https_m3_demo.py @@ -22,8 +22,9 @@ Drive A's state is read first and restored at the end: our image removed, the old image re-mounted by path when it had one, the drive switched back off when it was off, and the result compared with what was read. The -upload (/Temp/cache/upload/image[_N].d64: the U64E numbers a repeat rather -than overwrite it) is deleted over FTP once unmounted. +upload (/Temp/cache/upload/image[_N].d64) is left alone: that is the +U64E firmware's own managed upload directory, and deleting from it behind +the firmware's back risks its tracking (c64-test-harness's note). Device prep, the REU preflight, the DeviceLock, the init wait and the refusal verdict are rig_https_m3.py's. @@ -40,8 +41,6 @@ import sys import tempfile import time -import ftplib -from ftplib import FTP from pathlib import Path from c64_test_harness.backends.device_lock import DeviceLock, DeviceLockTimeout @@ -104,29 +103,9 @@ def drive_a(client) -> dict: return {} -def delete_upload(ours: dict, before: dict) -> None: - """FTP-delete the /Temp file our mount created (never one found mounted). - Never raises: it is the last step of the restore, and best-effort.""" - name = ours.get("image_file", "") - if not name: - print("WARNING: our mount reported no image_file; its /Temp upload " - "(if any) is not deleted") - return - if not name.startswith("/Temp/") or name == before.get("image_file"): - return - try: - with FTP(HOST, timeout=10.0) as ftp: - ftp.login() - ftp.delete(name) - print(f" deleted {name}") - except ftplib.all_errors as exc: - print(f"WARNING: {name} not deleted: {exc}") - - def restore_drive_a(client, before: dict, ours: dict, mounted: bool) -> None: """Put drive A back. `mounted` is False when our mount itself failed: - whatever the drive held is still there, so only the power is undone. - The upload is deleted last, so nothing it does can skip the rest.""" + whatever the drive held is still there, so only the power is undone.""" # fw 3.15 reports the whole path in image_file and image_path empty. path, file = before.get("image_path"), before.get("image_file") if mounted: @@ -146,8 +125,6 @@ def restore_drive_a(client, before: dict, ours: dict, mounted: bool) -> None: print(f"WARNING: drive A not as found (before, after): {diff}") else: print(f"drive A restored: {after}") - if mounted: - delete_upload(ours, before) def main() -> int: @@ -203,12 +180,6 @@ def main() -> int: except ReuPreflightError as exc: print(str(exc), file=sys.stderr) return 4 - try: - from _temp_gc import gc_temp - print(f"/Temp GC: removed {gc_temp(HOST)} stale file(s)") - except Exception as exc: # noqa: BLE001 - print(f"WARNING: /Temp GC skipped: {exc}") - before = drive_a(client) print(f"drive A before: {before}") touched = True # from here drive A needs restoring diff --git a/tools/uci/rig_https_refetch.py b/tools/uci/rig_https_refetch.py index 573c3260..97be7eac 100644 --- a/tools/uci/rig_https_refetch.py +++ b/tools/uci/rig_https_refetch.py @@ -176,12 +176,6 @@ def main() -> int: print(f"device info: {json.dumps(info)}") enable_uci(client) uci_on = True - if os.environ.get("C64_SKIP_TEMP_GC") != "1": - try: - from _temp_gc import gc_temp - print(f"/Temp GC: removed {gc_temp(HOST)} stale file(s)") - except Exception as exc: # noqa: BLE001 - print(f"WARNING: /Temp GC skipped: {exc}") prep_dir = _create_run_dir(DEBUG_BASE_DIR) _prune_old_run_dirs(DEBUG_BASE_DIR, keep=5) diff --git a/tools/uci/rig_https_wiki.py b/tools/uci/rig_https_wiki.py index d8b5acac..70744e3a 100644 --- a/tools/uci/rig_https_wiki.py +++ b/tools/uci/rig_https_wiki.py @@ -56,7 +56,7 @@ PHASE_TIMING — screen-scrape phase table (default 0 here: the run is long and body progress is the signal) UCI_DEBUG_DIR — artifact dir (default /tmp/uci_https_wiki_debug) - C64_SKIP_TEMP_GC / C64_SKIP_REU_PREFLIGHT / C64_SKIP_DEVICE_PREP — + C64_SKIP_REU_PREFLIGHT / C64_SKIP_DEVICE_PREP — bypass those steps C64_FORCE_TURBO_WRITE — write turbo blind when its state cannot be read (accepts the $88 risk; see _device_prep.py) @@ -76,7 +76,8 @@ checks presence/enabled against the build profile, not size. * turbo set BEFORE reset with the read-skip-write pattern — and an unreadable turbo read now aborts rather than writing blind (#187); - * /Temp GC after enable_uci (fw <= 3.14d writemem leak); + * /Temp hygiene is the harness's (its Ultimate64Client sweeps a + leak-prone device's /Temp while the DeviceLock is held); * every DMA address from build_policy_and_low_ram_arbiter() (pages 2 and 3, outside the link) — nothing hardcoded; * DEBUG_CAPTURE (6510 bus stream) is NOT wired here at all — OFF; @@ -124,7 +125,6 @@ guard_socket_teardown, teardown_warning, tls_evidence_addrs, ) from _reu_preflight import ReuPreflightError, preflight_reu -from _temp_gc import gc_temp # Pure helpers shared with the local/live rigs (side-effect free imports). from rig_https_local import ( @@ -628,9 +628,6 @@ def main() -> int: enable_uci(client) uci_enabled = True - # Userspace /Temp GC (fw <= 3.14d writemem attachment leak). - gc_temp(HOST) - try: runner_health_check(client) except Ultimate64RunnerStuckError as exc: diff --git a/tools/uci/rig_trust_policy.py b/tools/uci/rig_trust_policy.py index 1573cbf2..c2fde410 100644 --- a/tools/uci/rig_trust_policy.py +++ b/tools/uci/rig_trust_policy.py @@ -3,8 +3,9 @@ tools/test_trust_policy_6502.py runs the shipped policy against models. This walks the MENU on the U64E, the way a person does, with the store on -the firmware's real /USB1 and a real TLS 1.3 server, and reads the result -back over FTP. Two modes: +the firmware's real removable volume (/USB1 on the U64E) and a real TLS 1.3 +server, and reads the result back through c64-test-harness's storage API +(tools/uci/_trust_storage.py, under the DeviceLock). Two modes: LOCAL (default). A listener on this Mac serves a self-signed P-256 leaf whose SAN dNSName is this Mac's LAN address, which is also what the rig @@ -44,9 +45,15 @@ bytes are the ones shown on screen. The key this Mac sees for the host is printed beside it (informational: edges may differ). -Both modes work in /USB1/c64https-test (shared with rig_trust_store.py), -which the rig creates, requires to hold nothing but TRUST.A / TRUST.B, and -removes at the end. Every fetch is allowed to finish (or to refuse and +Both modes work in //c64https-test (the image's TRUST_STORE_DIR, +shared with rig_trust_store.py), which the rig creates if missing and in +which it only ever writes and deletes TRUST.A / TRUST.B, refuses if it +holds anything else (listing, plus TRUST.P by name), and removes at the +end. The volume must be in storage_writable_volumes(), and an explicit +TRUST_STORE_DIR in the environment must match the image; a device without +one (a C64U with no medium lists only Flash and Temp) is COULD NOT RUN, +exit 2, before the device is touched. Any other storage error (lock, FTP) +is a failure, exit 1. Every fetch is allowed to finish (or to refuse and close) before the next key; the C64 is reset only at boot, before any socket, and at the end after the close is confirmed. @@ -86,9 +93,11 @@ from _reu_preflight import ReuPreflightError, preflight_reu # noqa: E402 from _rig_lifecycle import guard_socket_teardown # noqa: E402 from _skip_policy import cannot_run, verdict # noqa: E402 -from _temp_gc import gc_temp # noqa: E402 from boot_check import decode_screen, screen_text # noqa: E402 -from rig_trust_store import OURS, TEST_DIR, Ftp, image_store_path, load_labels # noqa: E402 +from _trust_storage import (CANNOT_RUN_ERRORS, StoreUnavailable, TrustFiles, # noqa: E402 + clean_up, final_exit, image_dir, select_store_dir) +from c64_test_harness import Ultimate64StorageError # noqa: E402 +from rig_trust_store import image_store_path, load_labels # noqa: E402 import trust_store as ts # noqa: E402 HOST = os.environ.get("U64_HOST", "192.168.1.81") @@ -102,9 +111,14 @@ "FETCH_TIMEOUT", str((900 if MODE == "live" else 240) * SCALE))) PRG_PATH = REPO / "build" / "c64-https.prg" LABELS_PATH = REPO / "build" / "labels.txt" -A_PATH, B_PATH = f"{TEST_DIR}/TRUST.A", f"{TEST_DIR}/TRUST.B" CERTIFIES = "the trust policy on the U64E (#155 phase 2, L3)" NET_CONNECTED = 1 +# done_msg (src/boot.s): printed whenever do_https_get closes the socket -- +# after a completed fetch, but also after a send failure (boot.s, the +# send_fail_msg branch) and after a truncated body. Seeing it proves the +# fetch ENDED, not that it succeeded; the listener and store checks beside +# each await_closed() say whether it did. +CLOSED = "CONNECTION CLOSED" RESPONSE = (b"HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n" b"Content-Length: 19\r\nConnection: close\r\n\r\nHELLO TRUST POLICY\n") @@ -241,8 +255,16 @@ def clear_screen(self): self.seen = "" +def await_closed(run, what) -> str | None: + """Wait for done_msg and record the wait as a check: a fetch that never + closes is a failure, not a silent 240 s timeout.""" + s = run.wait(CLOSED, FETCH_TIMEOUT) + run.check(s is not None, f"{what}: {CLOSED}") + return s + + def record_of(ftp, host): - return ts.lookup(ts.select(ftp.get(A_PATH), ftp.get(B_PATH)), host) + return ts.lookup(ts.select(ftp.get(ftp.a), ftp.get(ftp.b)), host) def phases_local(run, ftp, ip, port, out, listener_box, fetch_in_flight_box): @@ -276,23 +298,23 @@ def press_g(target_host): run.check(run.settled(), "first use: settled") rec = record_of(ftp, ip) run.check(rec is not None and rec.spki == h1 and rec.mode == ts.MODE_TOFU, - f"first use: the store on /USB1 holds TOFU/K1 ({rec})") + f"first use: the store on the device holds TOFU/K1 ({rec})") want = ts.save(None, None, ip, ts.Record.for_host(ip, h1)) - run.check(want[0] == 0 and ftp.get(A_PATH) == want[1], + run.check(want[0] == 0 and ftp.get(ftp.a) == want[1], "first use: TRUST.A is byte-identical to the host mirror's") run.check(lst.conns and lst.conns[-1]["handshake"] and b"GET " in lst.conns[-1]["request"], "first use: the listener saw client Finished and the GET") - snap = (ftp.get(A_PATH), ftp.get(B_PATH)) + snap = (ftp.get(ftp.a), ftp.get(ftp.b)) # 2 -- match print("\n[2] same key") n0 = len(lst.conns) press_g(ip) - s = run.wait("DONE", FETCH_TIMEOUT) + s = await_closed(run, "match") run.check(f"TRUST: KNOWN KEY {hx(h1)}" in run.seen, "match: KNOWN KEY K1") run.check(run.settled(), "match: settled") run.check(len(lst.conns) == n0 + 1 and lst.conns[-1]["handshake"], "match: the GET completed") - run.check((ftp.get(A_PATH), ftp.get(B_PATH)) == snap, "match: the store was not written") + run.check((ftp.get(ftp.a), ftp.get(ftp.b)) == snap, "match: the store was not written") # 3 -- restarted with a NEW key: refused print("\n[3] listener restarted with K2; RETURN, N") @@ -309,7 +331,7 @@ def press_g(target_host): run.check(run.settled(), "changed: settled") run.check(all(not c["handshake"] for c in lst.conns) and len(lst.conns) == 1, f"changed: no client Finished reached the listener ({lst.conns})") - run.check((ftp.get(A_PATH), ftp.get(B_PATH)) == snap, "changed: the store was not written") + run.check((ftp.get(ftp.a), ftp.get(ftp.b)) == snap, "changed: the store was not written") # 4 -- arm the accept print("\n[4] K2 again; A") @@ -319,7 +341,7 @@ def press_g(target_host): s = run.wait("ACCEPT ARMED: G, SAME HOST", 30) run.check(s is not None, "accept: ACCEPT ARMED") run.check(run.settled(), "accept: settled") - run.check((ftp.get(A_PATH), ftp.get(B_PATH)) == snap, "accept: arming wrote nothing") + run.check((ftp.get(ftp.a), ftp.get(ftp.b)) == snap, "accept: arming wrote nothing") # 5 -- retry: recorded print("\n[5] K2, retry") @@ -333,9 +355,9 @@ def press_g(target_host): rec = record_of(ftp, ip) run.check(rec is not None and rec.spki == h2 and rec.mode == ts.MODE_ACCEPTED, f"retry: the store holds ACCEPTED/K2 ({rec})") - b = ftp.get(B_PATH) + b = ftp.get(ftp.b) run.check(b is not None and ts.decode(b)[0] == 2, "retry: written to TRUST.B, generation 2") - snap = (ftp.get(A_PATH), ftp.get(B_PATH)) + snap = (ftp.get(ftp.a), ftp.get(ftp.b)) # 6 -- override print("\n[6] listener restarted with K1; RETURN, Y") @@ -345,13 +367,13 @@ def press_g(target_host): run.keys("\r") run.wait("CONTINUE UNPINNED? Y/N", 30) run.keys("y") - s = run.wait("DONE", FETCH_TIMEOUT) + s = await_closed(run, "override") run.check(run.seen.count("** UNPINNED: KEY NOT CHECKED **") >= 1, "override: UNPINNED banner") run.check(run.settled(), "override: settled") run.check(len(lst.conns) == 2 and not lst.conns[0]["handshake"] and lst.conns[1]["handshake"] and b"GET " in lst.conns[1]["request"], f"override: refused once, then one unpinned fetch ({lst.conns})") - run.check((ftp.get(A_PATH), ftp.get(B_PATH)) == snap, "override: nothing recorded") + run.check((ftp.get(ftp.a), ftp.get(ftp.b)) == snap, "override: nothing recorded") rec = record_of(ftp, ip) run.check(rec is not None and rec.spki == h2, "override: the store still holds K2") @@ -406,9 +428,11 @@ def main() -> int: certifies=CERTIFIES) prg = PRG_PATH.read_bytes() path = image_store_path(prg, L) - if path != TEST_DIR + "/TRUST.": - return cannot_run(f"the image stores at {path!r}; this rig only runs against " - f"TRUST_STORE_DIR={TEST_DIR}", certifies=CERTIFIES) + explicit = os.environ.get("TRUST_STORE_DIR") + try: + image_dir(path) + except StoreUnavailable as exc: + return cannot_run(str(exc), certifies=CERTIFIES) load = prg[0] | prg[1] << 8 off = L["http_host_target"] - load + 2 build_host = prg[off:prg.index(b"\0", off)].decode("ascii") @@ -423,6 +447,8 @@ def main() -> int: try: acquire_device_lock(lock) except LockTimeoutConfigError as exc: + # exit 2 for a malformed C64_DEVICE_LOCK_TIMEOUT predates the + # storage port (_device_lock_helper.py), unchanged here print(f"[fatal] {exc}", file=sys.stderr) return 2 except DeviceLockTimeout as exc: @@ -430,27 +456,39 @@ def main() -> int: return 3 out = REPO / "build" / "rig_trust_policy" / time.strftime("%Y%m%d-%H%M%S") out.mkdir(parents=True, exist_ok=True) - client = run = None + client = run = store = None uci_on = False fetch_in_flight = False box = [False] listener_box = [None] try: + # The store's volume first: a device with nowhere to write it is + # COULD NOT RUN before anything on it is touched. + try: + store = TrustFiles(HOST, select_store_dir(HOST, path, explicit)) + store.prepare() + except CANNOT_RUN_ERRORS as exc: + # No writable volume, a directory that is not ours, or one that + # cannot be removed (hidden entries): the device cannot host the + # test store. Each names what it saw. + store = None + return cannot_run(f"trust store unavailable on {HOST}: {exc}", + certifies=CERTIFIES) + except Ultimate64StorageError as exc: + # Lock not held, FTP unreachable, a failed transfer: a real + # failure, never "no volume". + store = None + print(f"[fatal] storage on {HOST}: {exc}", file=sys.stderr) + return 1 + ftp = store + print(f"store {store.dir} on {HOST}") + # The harness sweeps /Temp itself for this client while the lock + # is held (leak-prone firmware only): no private GC. client = Ultimate64Client(host=HOST, timeout=20.0) info = client.get_info() print(f"device /v1/info: {json.dumps(info)}") - ftp = Ftp() - listing = ftp.listdir() - if listing is None: - ftp.mkdir() - elif set(listing) - set(OURS): - return cannot_run(f"{TEST_DIR} holds files that are not the rig's: {listing}", - certifies=CERTIFIES) - for n in OURS: - ftp.delete(n) enable_uci(client) uci_on = True - gc_temp(HOST) try: prepare_device(client, LABELS_PATH, turbo_mhz=TURBO_MHZ, artifact_dir=out) preflight_reu(client, LABELS_PATH) @@ -504,30 +542,17 @@ def main() -> int: {"passed": run.passed, "failed": run.failed, "notes": run.notes}, indent=1, default=str)) print(f"\nartifacts: {out}") - try: - ftp = Ftp() - for n in OURS: - ftp.delete(n) - if ftp.listdir() == []: - ftp.rmdir() - print(f"cleanup: {TEST_DIR} -> {ftp.listdir()}") - except Exception as exc: # noqa: BLE001 - print(f"WARNING: cleanup of {TEST_DIR} failed: {exc}") + cleanup = clean_up(store) + print(cleanup[1]) if client is not None: try: if uci_on: disable_uci(client) except Exception as exc: # noqa: BLE001 print(f"WARNING: teardown: {exc}") - try: - gc_temp(HOST) - except Exception as exc: # noqa: BLE001 - print(f"WARNING: gc_temp: {exc}") lock.release() - if run is None: - return 2 print(f"\n{run.passed} passed, {len(run.failed)} failed") - return verdict(run.passed, len(run.failed), certifies=CERTIFIES) + return final_exit(verdict(run.passed, len(run.failed), certifies=CERTIFIES), cleanup) if __name__ == "__main__": diff --git a/tools/uci/rig_trust_store.py b/tools/uci/rig_trust_store.py index 3e56169e..d267fd4b 100644 --- a/tools/uci/rig_trust_store.py +++ b/tools/uci/rig_trust_store.py @@ -2,12 +2,12 @@ """rig_trust_store.py -- the trust store's disk I/O on a real Ultimate (#155). VICE has no UCI, so the DOS-target path of src/net/uci/trust_store.s is -proven here, on the U64E, against the firmware's real filesystem on /USB1. +proven here, on the U64E, against the firmware's real filesystem (a USB stick or SD card). tools/test_trust_store_6502.py covers the same logic against a model; this rig checks the model's assumptions and the bytes that land on the stick. -Build first (the rig refuses any other store path, so it can never touch a -real TRUST.A / TRUST.B): +Build first (the rig refuses any store path but //c64https-test, so +it can never touch a real TRUST.A / TRUST.B): make clean && make BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1 \\ TRUST_STORE_DIR=/USB1/c64https-test @@ -18,12 +18,22 @@ cert_buf. Boot then includes the ~45 s precompute at 48 MHz, so set C64_INIT_WAIT (e.g. 90), the seconds the boot may take to reach the menu. -Everything happens in /USB1/c64https-test/, which the rig creates, requires -to hold nothing but TRUST.A / TRUST.B, and removes at the end. No socket is -ever opened (the program's boot DHCP is the only network traffic), so the -one reset the rig issues cannot poison a lease. - -Phases (each checked against tools/trust_store.py, and over FTP): +Everything happens in //c64https-test/ (the image's +TRUST_STORE_DIR), which the rig creates if missing and in which it only +ever writes and deletes TRUST.A / TRUST.B. The files go through +c64-test-harness's storage API (tools/uci/_trust_storage.py), under the +DeviceLock. The rig refuses a directory holding anything else (listing, +plus TRUST.P by name: listings omit hidden names) and removes the +directory at the end. The volume must be in storage_writable_volumes(); a +device without one (a C64U with no medium lists only Flash and Temp) is +COULD NOT RUN, exit 2, before the device is touched. Any other storage +error (lock, FTP) is a failure, exit 1. +An explicit TRUST_STORE_DIR in the environment must match the image. No +socket is ever opened (the program's boot DHCP is the only network +traffic), so the one reset the rig issues cannot poison a lease. + +Phases (each checked against tools/trust_store.py, and read back from the +device's storage): 1 load, empty directory -> EMPTY 2 save github.com -> TRUST.A, generation 1 3 load GitHub.COM -> VALID, record found @@ -39,25 +49,23 @@ 11 version 2 file -> FAIL VERSION 12 32 records (2,064 B, multi-part read); replace one -> 9 write chunks 13 (TURBO_MHZ=1 only) reset DURING trust_store_save's WRITE_DATA - 14 directory removed -> FAIL NOPATH + 14 directory removed -> FAIL NOPATH; then put back The torn write in phase 5 is produced with the store's own DOS layer (dos_open FA_WRITE|FA_CREATE_ALWAYS + one dos_write, no close), so it is deterministic at any clock. Phase 13 interrupts the real save; only at 1 MHz is its write window (~1 s) wide enough for a REST poll to land in. -Exit: 0 pass, 1 a check failed, 2 could not run, 3 lock timeout, 4 device +Exit: 0 pass, 1 a check failed (or a storage call failed), 2 could not run, 3 lock timeout, 4 device prep/preflight/load refused. """ from __future__ import annotations import hashlib -import io import json import os import sys import time -from ftplib import FTP, error_perm from pathlib import Path HERE = Path(__file__).resolve().parent @@ -77,7 +85,10 @@ from _prg_load import PrgLoadError, load_verified_and_run # noqa: E402 from _reu_preflight import ReuPreflightError, preflight_reu # noqa: E402 from _skip_policy import cannot_run, verdict # noqa: E402 -from _temp_gc import gc_temp # noqa: E402 +from _trust_storage import (CANNOT_RUN_ERRORS, StoreUnavailable, TrustFiles, # noqa: E402 + StoreLeftovers, clean_up, final_exit, image_dir, + select_store_dir) +from c64_test_harness import Ultimate64StorageError # noqa: E402 import trust_store as ts # noqa: E402 HOST = os.environ.get("U64_HOST", "192.168.1.81") @@ -85,9 +96,6 @@ SCALE = max(1.0, 48.0 / TURBO_MHZ) PRG_PATH = REPO / "build" / "c64-https.prg" LABELS_PATH = REPO / "build" / "labels.txt" -TEST_DIR = "/USB1/c64https-test" -OURS = ("TRUST.A", "TRUST.B") -A_PATH, B_PATH = f"{TEST_DIR}/TRUST.A", f"{TEST_DIR}/TRUST.B" CERTIFIES = "the trust store's UCI DOS I/O on the U64E (#155 phase 2)" TS_BUF = 0xC000 TEAR_LEN = 100 @@ -175,51 +183,8 @@ def jsr(x): return bytes(c) -class Ftp: - def _do(self, fn): - with FTP(HOST, timeout=15) as f: - f.login() - return fn(f) - - def listdir(self): - """None if TEST_DIR does not exist. NLST alone cannot say: on the - Ultimate's server it answers [] for a missing directory; CWD fails.""" - def go(f): - try: - f.cwd(TEST_DIR) - except error_perm: - return None - return sorted(n.rsplit("/", 1)[-1] for n in f.nlst(TEST_DIR)) - return self._do(go) - - def get(self, path): - buf = bytearray() - try: - self._do(lambda f: f.retrbinary(f"RETR {path}", buf.extend)) - except error_perm: - return None - return bytes(buf) - - def put(self, path, data): - self._do(lambda f: f.storbinary(f"STOR {path}", io.BytesIO(data))) - - def delete(self, name): - def go(f): - try: - f.delete(f"{TEST_DIR}/{name}") - except error_perm: - pass - self._do(go) - - def mkdir(self): - self._do(lambda f: f.mkd(TEST_DIR)) - - def rmdir(self): - self._do(lambda f: f.rmd(TEST_DIR)) - - class Rig: - def __init__(self, tr, client, L, prg, alloc): + def __init__(self, tr, client, L, prg, alloc, store): self.tr, self.client, self.L, self.prg = tr, client, L, prg (self.a, self.rec, self.host, self.op, self.sent, self.ra, self.rp) = alloc @@ -228,7 +193,7 @@ def __init__(self, tr, client, L, prg, alloc): self.passed = 0 self.failed = [] self.notes = {} - self.ftp = Ftp() + self.ftp = store def require(self, cond, what): """A check every later phase depends on: stop the run if it fails.""" @@ -325,7 +290,7 @@ def saved(self, host, rec, slot, gen): == (ts.ST_VALID, slot, gen) and st2["found"] == 1) def files(self): - return self.ftp.get(A_PATH), self.ftp.get(B_PATH) + return self.ftp.get(self.ftp.a), self.ftp.get(self.ftp.b) def rec(host, spki=SPKI_1): @@ -376,25 +341,25 @@ def run_phases(r: Rig): f"marker=${mark:02X})") rp, ra, took = r.sys(OP_TEAR) ra &= 1 # the write's carry - torn_open = ftp.get(A_PATH) + torn_open = ftp.get(ftp.a) r.notes["torn_open_c"], r.notes["torn_write_c"] = rp, ra r.notes["torn_A_size_before_reset"] = None if torn_open is None else len(torn_open) - print(f" dos_open C={rp} dos_write C={ra}; TRUST.A over FTP before the reset: " + print(f" dos_open C={rp} dos_write C={ra}; TRUST.A on the device before the reset: " f"{r.notes['torn_A_size_before_reset']} B") r.check(rp == 0 and ra == 0, "the torn open + write were accepted") r.client.reset() time.sleep(3.0) - torn = ftp.get(A_PATH) + torn = ftp.get(ftp.a) r.notes["torn_A_size_after_reset"] = None if torn is None else len(torn) print(f" TRUST.A after the reset: {r.notes['torn_A_size_after_reset']} B") - r.check(ftp.get(B_PATH) == pre_b, "TRUST.B (the loaded slot) is byte-identical") + r.check(ftp.get(ftp.b) == pre_b, "TRUST.B (the loaded slot) is byte-identical") r.check(torn is not None and ts.slot_result(torn) != ts.SLOT_OK, "TRUST.A is damaged, as DECISIONS #6 predicts") print("\n[6] reload the PRG; load after the torn write") r.boot() c, a, st = r.load("lwn.net") - torn_after = ftp.get(A_PATH) + torn_after = ftp.get(ftp.a) r.notes["torn_A_size_after_load"] = None if torn_after is None else len(torn_after) print(f" TRUST.A after the load's close-first: {r.notes['torn_A_size_after_load']} B") r.check(c == 0 and (st["state"], st["slot"], st["gen"]) == (ts.ST_VALID, 1, 2) @@ -414,7 +379,7 @@ def run_phases(r: Rig): print("\n[8] corrupt TRUST.B") bad_b = bytearray(fb) bad_b[20] ^= 0x01 - ftp.put(B_PATH, bytes(bad_b)) + ftp.put(ftp.b, bytes(bad_b)) c, a, st = r.load("example.com") r.check(c == 0 and (st["state"], st["slot"], st["gen"]) == (ts.ST_VALID, 0, 3) and st["slot_st"][1] == ts.R_CHECKSUM, "VALID from A; B reported CHECKSUM") @@ -422,7 +387,7 @@ def run_phases(r: Rig): print("\n[9] corrupt both; then one corrupt beside an absent one") bad_a = bytearray(fa) bad_a[-1] ^= 0x80 - ftp.put(A_PATH, bytes(bad_a)) + ftp.put(ftp.a, bytes(bad_a)) c, a, st = r.load("example.com") r.check(c == 1 and a == ts.ST_FAIL and st["reason"] == ts.R_CHECKSUM, "both corrupt: FAIL CHECKSUM") @@ -439,14 +404,14 @@ def run_phases(r: Rig): print("\n[11] unknown version") v2 = bytearray(ts.encode(1, [rec("example.com")])) v2[4] = 2 - ftp.put(A_PATH, bytes(v2)) + ftp.put(ftp.a, bytes(v2)) c, a, st = r.load("example.com") r.check(c == 1 and st["reason"] == ts.R_VERSION, "FAIL VERSION") ftp.delete("TRUST.A") print("\n[12] 32 records: a 2,064 B image") full = ts.encode(40, [rec(f"h{i}.example") for i in range(32)]) - ftp.put(A_PATH, full) + ftp.put(ftp.a, full) c, a, st = r.load("h31.example") r.check(c == 0 and st["found"] == 1 and st["rec"] == rec("h31.example").pack(), "record 32 of 32 found (five 512 B read parts)") @@ -464,9 +429,28 @@ def run_phases(r: Rig): if TURBO_MHZ == 1: phase_midsave(r) + phase_nopath(r) + + +def phase_nopath(r: Rig): + """[14] FAIL NOPATH: the test directory itself removed, then put back.""" print("\n[14] no directory") - ftp.rmdir() - c, a, st = r.load("example.com") + ftp = r.ftp + ftp.delete_ours() + try: + gone = ftp.remove_dir() + except StoreLeftovers as exc: + r.notes["nopath_rmdir"] = str(exc) + # Setup proved the directory removable and the DeviceLock excludes + # other writers: whatever is left was written by the program under + # test, during the run. A failure, not device state. + r.require(False, f"phase 14: {exc}; written DURING the run") + r.notes["nopath_rmdir"] = gone + r.require(gone is True, f"{ftp.dir} removed (rmdir returned {gone!r})") + try: + c, a, st = r.load("example.com") + finally: + ftp.ensure_dir() r.check(c == 1 and st["reason"] == ts.R_NOPATH, "FAIL NOPATH") @@ -475,7 +459,7 @@ def phase_midsave(r: Rig): if TURBO_MHZ == 1: print("\n[13] reset DURING trust_store_save's write (1 MHz)") base = ts.encode(5, [rec("a.example"), rec("b.example"), rec("c.example")]) - ftp.put(A_PATH, base) + ftp.put(ftp.a, base) c, a, st = r.load("d.example") r.check(c == 0 and st["slot"] == 0, "loaded generation 5 from A") # ts_target is store state in CRYPTO_OVERLAY: a deliberate, one-byte @@ -526,10 +510,11 @@ def main() -> int: executed=0, total=None, certifies=CERTIFIES) prg = PRG_PATH.read_bytes() path = image_store_path(prg, L) - if path != TEST_DIR + "/TRUST.": - return cannot_run(f"the image stores at {path!r}; this rig only runs against " - f"TRUST_STORE_DIR={TEST_DIR}", executed=0, total=None, - certifies=CERTIFIES) + explicit = os.environ.get("TRUST_STORE_DIR") + try: + image_dir(path) + except StoreUnavailable as exc: + return cannot_run(str(exc), executed=0, total=None, certifies=CERTIFIES) policy, arbiter = build_policy_and_low_ram_arbiter(LABELS_PATH, PRG_PATH) code_len = len(build_routine(L, 0x0400, 0, 0, 0, 0, 0, 0)) alloc = (arbiter.alloc(code_len, name="routine"), arbiter.alloc(64, name="record"), @@ -545,6 +530,8 @@ def main() -> int: try: acquire_device_lock(lock) except LockTimeoutConfigError as exc: + # exit 2 for a malformed C64_DEVICE_LOCK_TIMEOUT predates the + # storage port (_device_lock_helper.py), unchanged here print(f"[fatal] {exc}", file=sys.stderr) return 2 except DeviceLockTimeout as exc: @@ -552,31 +539,42 @@ def main() -> int: return 3 run_dir = REPO / "build" / "rig_trust_store" / time.strftime("%Y%m%d-%H%M%S") run_dir.mkdir(parents=True, exist_ok=True) - client = tr = rig = None + client = tr = rig = store = None uci_enabled = False try: + # The store's volume first: a device with nowhere to write it is + # COULD NOT RUN before anything on it is touched. + try: + store = TrustFiles(HOST, select_store_dir(HOST, path, explicit)) + store.prepare() + except CANNOT_RUN_ERRORS as exc: + # No writable volume, a directory that is not ours, or one that + # cannot be removed (hidden entries): the device cannot host the + # test store. Each names what it saw. + store = None + return cannot_run(f"trust store unavailable on {HOST}: {exc}", + executed=0, total=None, certifies=CERTIFIES) + except Ultimate64StorageError as exc: + # Lock not held, FTP unreachable, a failed transfer: a real + # failure, never "no volume". + store = None + print(f"[fatal] storage on {HOST}: {exc}", file=sys.stderr) + return 1 + print(f"store {store.dir} on {HOST}") + # The harness sweeps /Temp itself for this client while the lock + # is held (leak-prone firmware only): no private GC. client = Ultimate64Client(host=HOST, timeout=15.0) tr = Ultimate64Transport(host=HOST, timeout=15.0, client=client) tr.memory_policy = policy - ftp = Ftp() - listing = ftp.listdir() - if listing is None: - ftp.mkdir() - elif set(listing) - set(OURS): - return cannot_run(f"{TEST_DIR} holds files that are not the rig's: {listing}", - executed=0, total=None, certifies=CERTIFIES) - for n in OURS: - ftp.delete(n) enable_uci(client) uci_enabled = True - gc_temp(HOST) try: prepare_device(client, LABELS_PATH, turbo_mhz=TURBO_MHZ, artifact_dir=run_dir) preflight_reu(client, LABELS_PATH) except (DevicePrepError, ReuPreflightError) as exc: print(str(exc), file=sys.stderr) return 4 - rig = Rig(tr, client, L, prg, alloc) + rig = Rig(tr, client, L, prg, alloc, store) try: rig.boot() except PrgLoadError as exc: @@ -593,15 +591,8 @@ def main() -> int: "passed": rig.passed, "failed": rig.failed, "notes": rig.notes} (run_dir / "result.json").write_text(json.dumps(summary, indent=1)) print(f"\nnotes: {json.dumps(rig.notes)}\nartifacts: {run_dir}") - try: - ftp = Ftp() - for n in OURS: - ftp.delete(n) - if ftp.listdir() == []: - ftp.rmdir() - print(f"cleanup: {TEST_DIR} -> {ftp.listdir()}") - except Exception as exc: # noqa: BLE001 - print(f"WARNING: cleanup of {TEST_DIR} failed: {exc}") + # Reset first: the 6502 must not be mid-DOS-write while the rig + # deletes the files and removes the directory under it. if client is not None: try: if uci_enabled: @@ -609,12 +600,10 @@ def main() -> int: client.reset() except Exception as exc: # noqa: BLE001 print(f"WARNING: teardown: {exc}") - try: - gc_temp(HOST) - except Exception as exc: # noqa: BLE001 - print(f"WARNING: gc_temp: {exc}") + cleanup = clean_up(store) + print(cleanup[1]) lock.release() - return verdict(rig.passed, len(rig.failed), certifies=CERTIFIES) + return final_exit(verdict(rig.passed, len(rig.failed), certifies=CERTIFIES), cleanup) if __name__ == "__main__":