#!/usr/bin/env python3 """Host integration test for idle-blank -> screenlock daemon wiring. Runs the real shell helpers against stubbed sxmo_wm.sh / sxmo_state.sh and a temporary home, so the lock arming, cancellation and wake paths are verified without an X session or a phone. """ import os import pathlib import subprocess import sys import tempfile import time BASE = pathlib.Path(__file__).resolve().parent.parent BIN = BASE / "overlay/rootfs/usr/bin" LOCK = BIN / "bananarchy-idle-lock.sh" BLANK = BIN / "bananarchy-idle-blank.sh" WAKE = BIN / "bananarchy-screen-wake.sh" UNLOCK_HOOK = BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_unlock.sh" WM_STUB = '''#!/bin/sh if [ "$1" = "display" ]; then case "$2" in off) printf 'off\\n' > "$FAKE_ROOT/display_state" ;; on) printf 'on\\n' > "$FAKE_ROOT/display_state" ;; "") cat "$FAKE_ROOT/display_state" ;; esac fi exit 0 ''' STATE_STUB = '''#!/bin/sh printf '%s\\n' "$*" >> "$FAKE_ROOT/state.log" exit 0 ''' def failure(message): sys.stderr.write("FAIL: %s\n" % message) return 1 def main(): for path in (LOCK, BLANK, WAKE, UNLOCK_HOOK): if not path.is_file(): return failure("missing %s" % path) if subprocess.run(["sh", "-n", str(LOCK)]).returncode or \ subprocess.run(["sh", "-n", str(BLANK)]).returncode or \ subprocess.run(["sh", "-n", str(WAKE)]).returncode: return failure("shell syntax") with tempfile.TemporaryDirectory(prefix="bananarchy-idle-lock-") as tmp: root = pathlib.Path(tmp) bindir = root / "bin" home = root / "home" (home / ".config/sxmo").mkdir(parents=True) bindir.mkdir() for name, body in (("sxmo_wm.sh", WM_STUB), ("sxmo_state.sh", STATE_STUB)): target = bindir / name target.write_text(body, encoding="utf-8") target.chmod(0o755) display = root / "display_state" state_log = root / "state.log" lock_flag = root / "locked" cfg = home / ".config/sxmo/idle_lock_delay" env = dict(os.environ) env.update({ "HOME": str(home), "XDG_CONFIG_HOME": str(home / ".config"), "FAKE_ROOT": str(root), "BANANARCHY_WM_CMD": str(bindir / "sxmo_wm.sh"), "BANANARCHY_STATE_CMD": str(bindir / "sxmo_state.sh"), "BANANARCHY_IDLE_LOCK_HELPER": str(LOCK), "BANANARCHY_IDLE_LOCK_PIDFILE": str(root / "lock.pid"), "BANANARCHY_LOCK_FLAG": str(lock_flag), "BANANARCHY_WAKE_TS": str(root / "wake.ts"), "BANANARCHY_IDLE_BLANK_GRACE": "0", }) def run(script, *args): cmd = ["sh", str(script), *args] if os.environ.get("BANANARCHY_TEST_DEBUG"): cmd.insert(1, "-x") res = subprocess.run(cmd, env=env, capture_output=True, text=True) if os.environ.get("BANANARCHY_TEST_DEBUG"): sys.stderr.write("%s %s: %s\n" % (script.name, args, res.stderr[-1200:])) return res def log(): return state_log.read_text() if state_log.exists() else "" def clear(): if state_log.exists(): state_log.unlink() if lock_flag.exists(): lock_flag.unlink() def dark(value=True): display.write_text("off\n" if value else "on\n", encoding="utf-8") # Defaults and user configuration. dark() if run(LOCK, "get").stdout.strip() != "30": return failure("default lock delay is not 30 s") if run(LOCK, "label").stdout.strip() != "30s": return failure("default lock delay label") if run(LOCK, "set", "off").stdout.strip() != "off" or \ run(LOCK, "label").stdout.strip() != "Off": return failure("lock delay cannot be disabled") if run(LOCK, "set", "bogus").returncode == 0: return failure("invalid lock delay accepted") if run(LOCK, "set", "0").stdout.strip() != "0": return failure("immediate lock delay cannot be configured") print("[ok] config: default, off, immediate and invalid values") # Immediate arm must call the trusted screenoff transition. clear() run(LOCK, "arm") if "set screenoff" not in log(): return failure("immediate arm did not trigger the screenlock daemon") print("[ok] arm: immediate screenoff transition") # Delayed arm must fire while the panel is still dark. clear() cfg.write_text("1\n", encoding="utf-8") run(LOCK, "arm") if "set screenoff" in log(): return failure("delayed arm locked before the deadline") time.sleep(1.4) if "set screenoff" not in log(): return failure("delayed arm did not trigger the screenlock daemon") print("[ok] arm: delayed screenoff transition") # Cancel before the deadline must prevent the lock. clear() run(LOCK, "arm") run(LOCK, "cancel") time.sleep(1.4) if log(): return failure("cancel did not stop the pending lock") print("[ok] cancel: pending lock stopped") # Waking the panel before the deadline must prevent the lock. clear() dark() run(LOCK, "arm") time.sleep(0.2) dark(False) time.sleep(1.3) if log(): return failure("lock fired after the panel was already awake") print("[ok] wake: no lock after the panel is on") # The blank handler itself must arm (and its inverse must cancel) the lock. clear() cfg.write_text("0\n", encoding="utf-8") dark(False) run(BLANK, "off") if display.read_text().strip() != "off" or "set screenoff" not in log(): return failure("bananarchy-idle-blank.sh off did not arm the lock: " "display=%r log=%r" % (display.read_text().strip(), log())) print("[ok] blank: off arms the screenlock daemon") clear() cfg.write_text("1\n", encoding="utf-8") dark(False) run(BLANK, "off") run(BLANK, "on") time.sleep(1.4) if log(): return failure("bananarchy-idle-blank.sh on did not cancel the lock") print("[ok] blank: on cancels the pending lock") # An incoming call / SMS wake must cancel the pending lock too. clear() dark(False) run(BLANK, "off") run(WAKE) time.sleep(1.4) if log(): return failure("bananarchy-screen-wake.sh did not cancel the lock") if "bananarchy-idle-lock.sh cancel" not in UNLOCK_HOOK.read_text(): return failure("sxmo_hook_unlock.sh does not cancel the pending lock") print("[ok] wake: screen-wake and unlock hook cancel the pending lock") run(LOCK, "cancel") print("idle blank screenlock wiring: all checks passed") return 0 if __name__ == "__main__": sys.exit(main())