#!/usr/bin/env python3 """Host integration test for the GPS-only datetime source. Runs the real shell helpers with stubbed mmcli/date/chronyd, verifying that a GNSS fix sets the clock without ever consulting NTP, and that the network time helper stays inert while the GPS-only source is selected. """ import os import pathlib import subprocess import sys import tempfile BASE = pathlib.Path(__file__).resolve().parent.parent BIN = BASE / "overlay/rootfs/usr/bin" GPS_TIME = BIN / "bananarchy-gps-time-sync.sh" GPS_WRAPPER = BIN / "bananarchy-sync-gps-time.sh" NET_TIME = BIN / "bananarchy-time-sync.sh" GPS_MODEM = BIN / "bananarchy-gps-modem.sh" GPS_CLI = BIN / "bananarchy-gps" STUBS = { "date": '''#!/bin/sh if [ "$1" = "-u" ] && [ "$2" = "-s" ]; then printf '%s\\n' "$3" >> "$FAKE_ROOT/date.log" exit 0 fi if [ "$1" = "+%s" ]; then n=0 [ -r "$FAKE_ROOT/epoch" ] && read -r n < "$FAKE_ROOT/epoch" n=$((n + 1)) printf '%s\\n' "$n" > "$FAKE_ROOT/epoch" printf '%s\\n' "$n" exit 0 fi exit 0 ''', "mmcli": '''#!/bin/sh case "$*" in *--location-get*) cat "$FAKE_ROOT/location" ;; *-L*) printf ' /org/freedesktop/ModemManager1/Modem/0 [QUALCOMM]\\n' ;; *--location-status*) printf ' capabilities: 3gpp-lac-ci, gps-raw, gps-nmea\\n' ;; esac exit 0 ''', "bananarchy-gps-modem.sh": '''#!/bin/sh case "${1:-path}" in path|enable|disable) printf '/org/freedesktop/ModemManager1/Modem/0\\n' ;; status) printf 'modem=/org/freedesktop/ModemManager1/Modem/0\\n' ;; esac exit 0 ''', "rc-service": '''#!/bin/sh printf '%s\\n' "$*" >> "$FAKE_ROOT/rc.log" exit 0 ''', "chronyc": '''#!/bin/sh printf 'chronyc %s\\n' "$*" >> "$FAKE_ROOT/chronyc.log" exit 0 ''', "bananarchy-net-sync-stub": '''#!/bin/sh printf 'net-sync %s\\n' "$*" >> "$FAKE_ROOT/net.log" exit 0 ''', "hwclock": '#!/bin/sh\nexit 0\n', "sleep": '#!/bin/sh\nexit 0\n', "su": '#!/bin/sh\nexit 0\n', "notify-send": '''#!/bin/sh printf '%s\\n' "$*" >> "$FAKE_ROOT/notify.log" exit 0 ''', } ISO_SAMPLE = ( "modem.location.gps.utc : '2026-11-10T12:34:56Z'\n" "modem.location.gps.latitude : 50.4501\n" "modem.location.gps.longitude : 30.5234\n" ) RMC_SAMPLE = ( "modem.location.gps.nmea.value : " "$GNRMC,123456.00,A,5000.0000,N,03000.0000,E,0.0,0.0,101126,,,A\n" ) def fail(message): sys.stderr.write("FAIL: %s\n" % message) return 1 def main(): for path in (GPS_TIME, GPS_WRAPPER, NET_TIME, GPS_MODEM, GPS_CLI): if not path.is_file(): return fail("missing %s" % path) if path.suffix == ".sh" or path.name in ("bananarchy-gps", "bananarchy-time-sync.sh"): if subprocess.run(["sh", "-n", str(path)]).returncode: return fail("shell syntax: %s" % path.name) with tempfile.TemporaryDirectory(prefix="bananarchy-gps-time-") as tmp: root = pathlib.Path(tmp) bindir = root / "bin" home = root / "home" (home / ".config/sxmo").mkdir(parents=True) bindir.mkdir() for name, body in STUBS.items(): target = bindir / name target.write_text(body, encoding="utf-8") target.chmod(0o755) env = dict(os.environ) env.update({ "PATH": str(bindir) + os.pathsep + os.environ["PATH"], "FAKE_ROOT": str(root), "HOME": str(home), "BANANARCHY_TIME_SOURCE_FILE": str(home / ".config/sxmo/time_source"), "BANANARCHY_GPS_TIME_RUNDIR": str(root / "run"), "BANANARCHY_GPS_TIME_NO_ESCALATION": "1", "BANANARCHY_TIME_SYNC_NO_ESCALATION": "1", "BANANARCHY_GPS_TIME_TIMEOUT": "5", "BANANARCHY_STATUS_REFRESH": "true", "BANANARCHY_GPS_MODEM": str(bindir / "bananarchy-gps-modem.sh"), "BANANARCHY_MMCLI": str(bindir / "mmcli"), "BANANARCHY_NET_SYNC": str(bindir / "bananarchy-net-sync-stub"), "BANANARCHY_GPS_TIME_HELPER": str(GPS_TIME), }) def run(script, *args): return subprocess.run(["sh", str(script), *args], env=env, capture_output=True, text=True) def text(name): p = root / name return p.read_text() if p.exists() else "" (root / "location").write_text(ISO_SAMPLE, encoding="utf-8") if run(GPS_TIME, "get").stdout.strip() != "network": return fail("default time source is not network") res = run(GPS_TIME, "source", "gps") if "Time source: gps" not in res.stdout or \ (home / ".config/sxmo/time_source").read_text().strip() != "gps": return fail("cannot select the GPS-only time source") if "chronyd stop" not in text("rc.log"): return fail("GPS-only source did not stop chronyd") print("[ok] source: gps disables chronyd") res = run(GPS_TIME, "sync") if "GPS time synced: 2026-11-10 12:34:56 UTC" not in res.stdout: return fail("ISO GPS UTC did not set the clock: %r %r" % (res.stdout, res.stderr)) if "2026-11-10 12:34:56" not in text("date.log"): return fail("date -u -s was not called with the GNSS timestamp") print("[ok] sync: ModemManager GPS UTC sets the clock") (root / "location").write_text(RMC_SAMPLE, encoding="utf-8") res = run(GPS_TIME, "sync") if "GPS time synced: 2026-11-10 12:34:56 UTC" not in res.stdout: return fail("RMC fix did not set the clock: %r %r" % (res.stdout, res.stderr)) print("[ok] sync: RMC sentence fallback sets the clock") # NTP must stay completely unused while the GPS source is selected. for action in ("boot", "network-up", "force", "sync"): run(NET_TIME, action) if text("chronyc.log"): return fail("network time helper consulted chronyd in GPS-only mode") print("[ok] network time helper stays inert in GPS-only mode") res = run(GPS_WRAPPER) if res.returncode or "GPS time synced" not in text("notify.log"): return fail("Config -> Sync GPS time did not notify the user") print("[ok] user wrapper reports the GPS sync result") res = run(GPS_TIME, "source", "network") if "Time source: network" not in res.stdout or \ "chronyd start" not in text("rc.log") or \ "net-sync force" not in text("net.log"): return fail("switching back to network time did not restore NTP") print("[ok] source: network restores chronyd and NTP") # The GPS CLI resolves the modem through the shared helper. res = run(GPS_CLI, "status") if "Modem : /org/freedesktop/ModemManager1/Modem/0" not in res.stdout: return fail("bananarchy-gps did not use the resolved modem path") print("[ok] CLI: GPS status uses the resolved modem") print("GPS-only time source: all checks passed") return 0 if __name__ == "__main__": sys.exit(main())