#!/bin/sh # Tests of handset.sh against a fake phone: a directory tree and a script standing in for the phone's # commands. Run in the image's own shell: # docker run --rm -v "$PWD/handset:/t:ro" alpine:3.22 sh /t/test_handset.sh set -u here=$(cd "$(dirname "$0")" && pwd) T=$(mktemp -d) export T HOST="$T/host" PAYLOAD="$T/payload" RUN="$T/run" RELEASE=r2 POLL=1 IDLE_MIN=0 UP_POLL=0 export FAKE_HOST="$T/fake" fails=0 check() { if [ "$2" = "$3" ]; then echo "ok $1"; else echo "FAIL $1: expected '$3', got '$2'"; fails=$((fails + 1)); fi; } has() { if grep -q -- "$2" "$T/calls"; then r=yes; else r=no; fi; check "$1" "$r" "$3"; } cat > "$FAKE_HOST" <<'F' #!/bin/sh # The phone: state in $T/{lock,screen,usr2,alive,bad}; every call in $T/calls. echo "$*" >> "$T/calls" case "$*" in *"chandra-ctl"*" ping") [ "$(cat "$T/alive")" = 1 ] && echo "ok pong" ;; *"chandra-ctl"*" lock status") [ "$(cat "$T/alive")" = 1 ] && echo "ok $(cat "$T/lock")" ;; *"chandra-ctl"*" lock") echo locked > "$T/lock"; echo "ok locked" ;; *"chandra-ctl"*" screen off") echo off > "$T/screen" ;; *"chandra-ctl"*" status") echo "ok 540x1170 rot=0 screen=$(cat "$T/screen") brightness=35" ;; "pgrep -x chandra-host") [ "$(cat "$T/alive")" = 1 ] ;; "pgrep "*chandra-remote) echo 42 ;; "sed "*SigCgt*) if [ "$(cat "$T/usr2")" = 1 ]; then echo 0000000100004803; else echo 0000000100004003; fi ;; "kill -USR2 42") ;; "curl "*) [ "$(cat "$HOST/usr/local/bin/chandra-remote")" != broken ] ;; "ldd "*) [ ! -e "$T/missing" ] || { echo "libfoo.so => not found"; exit 0; } ;; "sh -c echo 1 > /run/chandra-start-locked") touch "$T/marker" ;; "rm -f /run/chandra-start-locked") rm -f "${T:?}/marker" ;; "systemctl restart greetd") echo on > "$T/screen"; echo unlocked > "$T/lock"; echo 1 > "$T/alive" c=$(cat "$HOST/usr/local/share/chandra/compositor.wasm") # A compositor that knows the marker starts locked; a bad one doesn't start. case "$c" in bad) echo 0 > "$T/alive" ;; new) [ -e "$T/marker" ] && echo locked > "$T/lock" ;; esac [ ! -e "$T/marker" ] || echo "restarted with marker" >> "$T/calls" ;; *) echo "fake: unexpected: $*" >> "$T/calls" ;; esac F chmod +x "$FAKE_HOST" put() { mkdir -p "$(dirname "$2")"; echo "$1" > "$2"; } # fresh LIVE SESSION: the phone has "old" everything; the payload has LIVE and SESSION. fresh() { for d in host payload run; do rm -rf "${T:?}/$d"; done rm -f "${T:?}/missing" "${T:?}/marker" put old "$HOST/usr/local/bin/chandra-remote" put old "$HOST/usr/local/share/chandra/remote/index.html" put old "$HOST/usr/local/share/chandra/compositor.wasm" put old "$HOST/usr/local/bin/chandra-host" put old "$HOST/usr/local/lib/chandra/chandra-polkit-agent" put "$1" "$PAYLOAD/live/usr/local/bin/chandra-remote" put "$1" "$PAYLOAD/live/usr/local/share/chandra/remote/index.html" put "$2" "$PAYLOAD/session/usr/local/share/chandra/compositor.wasm" put old "$PAYLOAD/session/usr/local/bin/chandra-host" put old "$PAYLOAD/session/usr/local/lib/chandra/chandra-polkit-agent" put added "$PAYLOAD/session/usr/local/share/chandra/modules/new one.wasm" echo unlocked > "$T/lock"; echo on > "$T/screen"; echo 1 > "$T/usr2"; echo 1 > "$T/alive" : > "$T/calls" } run() { ROUNDS=$1 sh "$here/handset.sh" run > "$T/out" 2>&1; echo $?; } remote() { cat "$HOST/usr/local/bin/chandra-remote"; } comp() { cat "$HOST/usr/local/share/chandra/compositor.wasm"; } module() { [ -e "$HOST/usr/local/share/chandra/modules/new one.wasm" ] && echo yes || echo no; } fresh new new check "in use: exits cleanly" "$(run 2)" 0 check "in use: live installed" "$(remote)" new check "in use: compositor waits" "$(comp)" old has "in use: remote restarted in place" "kill -USR2 42" yes has "in use: session left alone" "restart greetd" no check "remote keeps its mode" "$(stat -c %a "$HOST/usr/local/bin/chandra-remote")" 755 check "ready" "$(sh "$here/handset.sh" ready; echo $?)" 0 fresh new new; echo 0 > "$T/usr2" run 2 > /dev/null check "old remote: live waits" "$(remote)" old has "old remote: not signalled" "kill" no fresh new new; echo locked > "$T/lock" run 2 > /dev/null check "locked, screen on: waits" "$(comp)" old fresh new new; echo off > "$T/screen" run 2 > /dev/null check "unlocked, screen off: waits" "$(comp)" old fresh new new; echo locked > "$T/lock"; echo off > "$T/screen" IDLE_MIN=1 run 2 > /dev/null check "locked and dark, not long enough: waits" "$(comp)" old fresh new new; echo locked > "$T/lock"; echo off > "$T/screen" run 1 > /dev/null check "idle: compositor installed" "$(comp)" new check "idle: new file installed" "$(module)" yes has "idle: marker was there for the restart" "restarted with marker" yes check "idle: locked again" "$(cat "$T/lock")" locked check "idle: screen off again" "$(cat "$T/screen")" off check "idle: marker removed" "$([ -e "$T/marker" ] && echo yes || echo no)" no fresh new knows-no-marker; echo locked > "$T/lock"; echo off > "$T/screen" run 1 > /dev/null check "a compositor without the marker is locked by hand" "$(cat "$T/lock")" locked fresh new bad; echo locked > "$T/lock"; echo off > "$T/screen" check "bad compositor: updater lives on" "$(run 3)" 0 check "bad compositor: rolled back" "$(comp)" old check "bad compositor: added file taken out" "$(module)" no check "bad compositor: locked after rollback" "$(cat "$T/lock")" locked check "bad compositor: tried once" "$(grep -c 'restart greetd' "$T/calls")" 2 check "bad compositor: marked failed" "$(cat "$HOST/var/lib/chandra-handset/failed")" r2 fresh new new (sleep 2; sh "$here/handset.sh" approve > "$T/approve" 2>&1; echo $? > "$T/approve-rc") & run 6 > /dev/null; wait check "approve: exit code" "$(cat "$T/approve-rc")" 0 check "approve: installed while in use" "$(comp)" new check "approve: an unlocked session isn't locked" "$(cat "$T/lock")" unlocked has "approve: no marker" "chandra-start-locked" no fresh broken new check "broken remote: fails" "$(run 1)" 1 check "broken remote: rolled back" "$(remote)" old check "broken remote: page too" "$(cat "$HOST/usr/local/share/chandra/remote/index.html")" old check "broken remote: not ready" "$(sh "$here/handset.sh" ready; echo $?)" 1 fresh new new; touch "$T/missing" check "missing library: fails" "$(run 1)" 1 check "missing library: nothing installed" "$(remote)" old has "only known commands were run" "fake: unexpected" no rm -rf "${T:?}" [ $fails = 0 ] && echo "all passed" || { echo "$fails failed"; exit 1; }