#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-or-later """Run the optional, isolated NitrOS-9 telnet probe against a local TCP service.""" import argparse import socket import subprocess import sys import tempfile from concurrent.futures import ThreadPoolExecutor from pathlib import Path def receive_through(connection, marker): data = bytearray() while marker not in data: chunk = connection.recv(256) if not chunk or len(data) > 4096: raise RuntimeError(f"Guest failed to send {marker!r}: {bytes(data)!r}") data.extend(chunk) def serve(listener, task): # Poll so a failed guest boot can return immediately, even before accept. listener.settimeout(0.25) for session in (b"1", b"2"): while task.poll() is None: try: connection, _ = listener.accept() break except socket.timeout: continue else: raise RuntimeError("Guest exited without establishing both connections") with connection: connection.settimeout(25) receive_through(connection, b"localpayload" + session) connection.sendall(b"\r\nremoteack" + session + b"\r\n") receive_through(connection, b"goodbye" + session) def check(probe, resources, work): for processor in ("6809", "6309"): edit = work / ("edit" + processor) lines = [f"line{n:02d}" for n in range(1, 41)] edit.write_text("\n".join(lines) + "\n", encoding="ascii") with socket.socket() as listener, ThreadPoolExecutor(max_workers=1) as worker: listener.bind(("127.0.0.1", 0)) listener.listen(1) with subprocess.Popen([probe, resources, str(listener.getsockname()[1]), processor, edit]) as child: result = worker.submit(serve, listener, child) try: if child.wait(timeout=160) != 0: raise SystemExit(f"{processor} guest probe failed") result.result() expected = "\n".join(lines[:-3] + ["lLRe38", "lDne39", "typedfromcoco", "final"]) + "\n" if edit.read_bytes() != expected.encode("ascii"): raise RuntimeError(f"{processor}: vi did not save the expected file") finally: if child.poll() is None: child.kill() def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("probe", type=Path) parser.add_argument("resources", type=Path, help="LocalResources containing private ROMs and Media") args = parser.parse_args() prepare = Path(__file__).resolve().parents[2] / "platforms/macos/Tools/prepare-nitros9.py" source = args.resources.resolve() image = Path("Media/NitrOS-9 L2 V3.3.0.vhd") with tempfile.TemporaryDirectory(prefix="vcc-drivewire-guest-") as temporary: resources = Path(temporary) (resources / "ROMs").symlink_to(source / "ROMs", target_is_directory=True) (resources / "Media").mkdir() subprocess.run([sys.executable, prepare, source / image, resources / image], check=True) with tempfile.TemporaryDirectory(prefix="vccvi", dir="/tmp") as scratch: check(args.probe.resolve(), resources, Path(scratch)) if __name__ == "__main__": main()