#!/usr/bin/env python3 # SPDX-License-Identifier: LicenseRef-Starship-1.0 # Copyright (c) 2023-2026 Robert A. James """test_transcripts.py -- the demo's transcripts (#192, ruled 2026-11-13 and 2026-11-21): console.py writes each session's console into DEMO_TRANSCRIPTS, one dated file a session, readable only by its writer; and transcripts-tidy.sh hands each to the directory's owner and deletes those older than 7 days. No QEMU: a fake QMP socket that says the machine runs, and a fake console that echoes. Prints PASS:/FAIL: and RESULT:.""" import json, os, socket, subprocess, sys, tempfile, threading, time, glob HERE = os.path.dirname(os.path.abspath(__file__)) ok = True def check(what, cond): global ok print(("PASS: " if cond else "FAIL: ") + what) ok = ok and bool(cond) def qmp_server(path): s = socket.socket(socket.AF_UNIX); s.bind(path); s.listen(1) def run(): c, _ = s.accept(); f = c.makefile("rw") f.write(json.dumps({"QMP": {}}) + "\n"); f.flush() for line in f: f.write(json.dumps({"return": {"status": "running"}} if "query-status" in line else {"return": {}}) + "\n"); f.flush() threading.Thread(target=run, daemon=True).start() def console_server(path): s = socket.socket(socket.AF_UNIX); s.bind(path); s.listen(1) def run(): c, _ = s.accept() c.sendall(b"[zuse@Hera] ok> ") while True: d = c.recv(1024) if not d: break c.sendall(d) # the machine echoes if b"\r" in d: c.sendall(b"7 ok\r\n[zuse@Hera] ok> ") threading.Thread(target=run, daemon=True).start() with tempfile.TemporaryDirectory() as t: tr = os.path.join(t, "transcripts"); os.mkdir(tr, 0o730) qmp_server(os.path.join(t, "qmp.sock")); console_server(os.path.join(t, "con.sock")) env = dict(os.environ, DEMO_TRANSCRIPTS=tr) p = subprocess.Popen([sys.executable, "-I", os.path.join(HERE, "console.py"), os.path.join(t, "qmp.sock"), os.path.join(t, "con.sock")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env) time.sleep(1.0); p.stdin.write(b"3 4 + .\r"); p.stdin.flush(); time.sleep(1.0); p.stdin.close() out = p.stdout.read(); p.wait(timeout=10) files = glob.glob(os.path.join(tr, "*.log")) check("the visitor still sees the session: %r" % out[-40:], b"7 ok" in out) check("one transcript for the session (%d)" % len(files), len(files) == 1) if files: f = files[0]; text = open(f, "rb").read() check("named by its date, UTC: %s" % os.path.basename(f), os.path.basename(f)[:8].isdigit()) check("it holds what was typed, as the machine echoed it, and what it printed", b"3 4 + ." in text and b"7 ok" in text) check("readable by its writer only (mode %o)" % (os.stat(f).st_mode & 0o777), os.stat(f).st_mode & 0o777 == 0o600) # a directory it cannot write: the session goes on, without a transcript ro = os.path.join(t, "nowhere") env2 = dict(os.environ, DEMO_TRANSCRIPTS=ro) os.unlink(os.path.join(t, "qmp.sock")); os.unlink(os.path.join(t, "con.sock")) qmp_server(os.path.join(t, "qmp.sock")); console_server(os.path.join(t, "con.sock")) p = subprocess.Popen([sys.executable, "-I", os.path.join(HERE, "console.py"), os.path.join(t, "qmp.sock"), os.path.join(t, "con.sock")], stdin=subprocess.PIPE, stdout=subprocess.PIPE, env=env2) time.sleep(1.0); p.stdin.write(b"3 4 + .\r"); p.stdin.flush(); time.sleep(1.0); p.stdin.close() out = p.stdout.read(); p.wait(timeout=10) check("with no directory to write in, the session still works", b"7 ok" in out) # transcripts-tidy.sh: handed to the owner, the old deleted box = os.path.join(t, "box"); os.mkdir(box) new, old = os.path.join(box, "20261121-000000-1.log"), os.path.join(box, "20261101-000000-2.log") for f in (new, old): open(f, "w").write("x"); os.chmod(f, 0o600) eight_days = time.time() - 8 * 86400; os.utime(old, (eight_days, eight_days)) r = subprocess.run(["bash", os.path.join(HERE, "transcripts-tidy.sh"), box, "nobody"], capture_output=True, text=True) check("the tidy succeeds: %s" % (r.stderr.strip() or "ok"), r.returncode == 0) check("a transcript older than 7 days is deleted", not os.path.exists(old)) check("a newer one is kept", os.path.exists(new)) if os.path.exists(new): import pwd st = os.stat(new) check("and handed to the directory's owner (%s), mode 600" % pwd.getpwuid(st.st_uid).pw_name, pwd.getpwuid(st.st_uid).pw_name == "nobody" and st.st_mode & 0o777 == 0o600) print("RESULT: " + ("PASS" if ok else "FAIL")) sys.exit(0 if ok else 1)