Nacre/tools/smoke.sh
astar76 9ad7660855 S025: cookies encrypted at rest with a key in the OS keyring (R1-19)
servo-net hands its public cookie jar to Nacre instead of writing plain
cookie_jar.json; store::cookies seals it in cookies.bin (XChaCha20-Poly1305)
with a keyring-held key, saves on change and at exit, migrates the old plain
jar. No keyring: cookies for the run only, Settings says so (D-019).
Smokes with a throwaway GNOME Keyring (SMOKE_KEYRING=1).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 19:30:09 -06:00

152 lines
7.8 KiB
Shell
Executable file

#!/usr/bin/env bash
# Smoke run: start Nacre on a hidden X display (Xvfb) with a throwaway profile, play a NACRE_SCRIPT, collect results.
#
# Usage: tools/smoke.sh <script.txt> [outdir]
# outdir defaults to target/smoke/<script name>-<time>. It gets:
# nacre.log stdout + stderr of the browser (RUST_BACKTRACE=1)
# selftest.log every script line run, plus js results and warnings
# <name>.png `shot <name>` screenshots; <name>.json `status <name>` files
# home/ the throwaway HOME / XDG_* folders (profile, history, session)
# Exit 0 only if the browser exited by itself with status 0, no "panicked" is in nacre.log and the script
# wrote `status done` (so every script must end with `status done` then `quit`) and no `assert` failed.
#
# Environment:
# SMOKE_HOME=dir reuse this throwaway home (e.g. a previous run's outdir/home) to test session restore
# SMOKE_TIMEOUT=s kill the browser after s seconds (default 180)
# SMOKE_SIZE=WxH screen size (default 1280x800)
# SMOKE_BIN=path browser binary (default target/release/nacre)
# SMOKE_ARGS="..." extra command-line arguments for the browser (e.g. --incognito)
# NACRE_NO_BRIDGE=1 passed through: no network bridge
# SMOKE_FAKE_DISCORD=1 start tests/smoke/net/fake-discord.py on $XDG_RUNTIME_DIR/discord-ipc-0; it logs every frame
# it gets to outdir/discord.log (one JSON line each)
# SMOKE_KEYRING=1 give the run its own session bus with a throwaway GNOME Keyring (password "smoke", unlocked)
# kept in home/, so the OS keyring path is used (cookie key, S025); SMOKE_HOME reruns find the
# same key. Without it the run has no session bus, hence no keyring.
# SMOKE_VISIBLE=1 show the window on the user's screen (their Wayland display, niri monitor
# SMOKE_MONITOR, default DP-1) with the real GPU instead of hidden Xvfb + software GL;
# only the Wayland socket is shared, profile/runtime dir/bus stay throwaway
#
# Script commands (src/selftest.rs has the full list): wait <s>, waitload [s], open <url>, newtab <url>,
# page <nacre://...>, shot <name>, status <name>, set <json pointer> <json>, flyout <name>, do <action>,
# cmd <name> [arg], js <script>, assert <js>, click|rclick <x> <y>, pagetype <text>, log <text>, quit. Lines starting with # are notes.
# In the script, @PAGES@ is replaced by the file:// URL of tests/smoke/pages, @PAGESDIR@ by its path, @HOME@ by the
# throwaway home.
#
# Never uses the user's profile, display, Discord socket or session bus; never sets a proxy.
set -u
root=$(cd "$(dirname "$0")/.." && pwd)
script=${1:?usage: tools/smoke.sh <script.txt> [outdir]}
[ -f "$script" ] || { echo "smoke: no such script $script" >&2; exit 2; }
out=${2:-$root/target/smoke/$(basename "$script" .txt)-$(date +%Y%m%d-%H%M%S)}
bin=${SMOKE_BIN:-$root/target/release/nacre}
[ -x "$bin" ] || { echo "smoke: no binary at $bin (build first)" >&2; exit 2; }
visible=${SMOKE_VISIBLE:-}
if [ -n "$visible" ]; then
[ -n "${WAYLAND_DISPLAY:-}" ] && [ -S "${XDG_RUNTIME_DIR:-}/$WAYLAND_DISPLAY" ] \
|| { echo "smoke: SMOKE_VISIBLE needs the user's Wayland session" >&2; exit 2; }
else
command -v Xvfb >/dev/null || { echo "smoke: Xvfb is missing (pacman -S xorg-server-xvfb)" >&2; exit 2; }
fi
mkdir -p "$out"
out=$(cd "$out" && pwd)
home=${SMOKE_HOME:-$out/home}
mkdir -p "$home"
home=$(cd "$home" && pwd)
rm -f "$out"/*.png "$out"/*.json "$out/selftest.log" "$out/nacre.log"
sed -e "s|@PAGES@|file://$root/tests/smoke/pages|g" -e "s|@PAGESDIR@|$root/tests/smoke/pages|g" -e "s|@HOME@|$home|g" "$script" >"$out/script.txt"
runtime=$(mktemp -d /tmp/nacre-smoke-XXXXXX)
chmod 700 "$runtime"
xvfb_pid=
cleanup() {
[ -n "$xvfb_pid" ] && kill "$xvfb_pid" 2>/dev/null
[ -n "${discord_pid:-}" ] && kill "$discord_pid" 2>/dev/null
[ -n "${keyring_pid:-}" ] && kill "$keyring_pid" 2>/dev/null
[ -n "${bus_pid:-}" ] && kill "$bus_pid" 2>/dev/null
rm -rf "$runtime"
}
trap cleanup EXIT
if [ -n "$visible" ]; then
# Share only the compositor socket; open the window on the chosen monitor.
ln -s "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" "$runtime/wayland-0"
command -v niri >/dev/null && niri msg action focus-monitor "${SMOKE_MONITOR:-DP-1}" >/dev/null 2>&1
dispvars=(WAYLAND_DISPLAY=wayland-0 XDG_SESSION_TYPE=wayland)
else
# Xvfb picks a free display number and writes it to fd 3.
exec 3<>"$runtime/display"
Xvfb -displayfd 3 -screen 0 "${SMOKE_SIZE:-1280x800}x24" -nolisten tcp -noreset >"$out/xvfb.log" 2>&1 &
xvfb_pid=$!
for _ in $(seq 50); do
display=$(head -n1 "$runtime/display" 2>/dev/null)
[ -n "$display" ] && break
sleep 0.1
done
[ -n "${display:-}" ] || { echo "smoke: Xvfb did not start (see $out/xvfb.log)" >&2; exit 1; }
dispvars=(DISPLAY=":$display" XDG_SESSION_TYPE=x11 LIBGL_ALWAYS_SOFTWARE=1)
fi
if [ -n "${SMOKE_FAKE_DISCORD:-}" ]; then
: >"$out/discord.log"
python3 "$root/tests/smoke/net/fake-discord.py" "$runtime/discord-ipc-0" "$out/discord.log" &
discord_pid=$!
for _ in $(seq 50); do [ -S "$runtime/discord-ipc-0" ] && break; sleep 0.1; done
fi
busvars=()
if [ -n "${SMOKE_KEYRING:-}" ]; then
mkdir -p "$home/.local/share"
# A bus without service activation: only the keyring on it (no gvfs mounts, portals or the user's services).
cat >"$runtime/bus.conf" <<CONF
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig><type>session</type><listen>unix:path=$runtime/bus</listen>
<policy context="default"><allow send_destination="*" eavesdrop="true"/><allow eavesdrop="true"/><allow own="*"/></policy></busconfig>
CONF
env -i PATH="$PATH" HOME="$home" XDG_RUNTIME_DIR="$runtime" \
dbus-daemon --config-file="$runtime/bus.conf" --nofork --nopidfile >"$out/bus.log" 2>&1 &
bus_pid=$!
for _ in $(seq 50); do [ -S "$runtime/bus" ] && break; sleep 0.1; done
busvars=(DBUS_SESSION_BUS_ADDRESS="unix:path=$runtime/bus")
# The login keyring's password is "smoke" (an empty one leaves no default collection).
printf 'smoke' | env -i PATH="$PATH" HOME="$home" XDG_DATA_HOME="$home/.local/share" XDG_RUNTIME_DIR="$runtime" \
"${busvars[@]}" gnome-keyring-daemon --foreground --unlock --components=secrets >"$out/keyring.log" 2>&1 &
keyring_pid=$!
for _ in $(seq 50); do
env "${busvars[@]}" dbus-send --session --dest=org.freedesktop.DBus --print-reply /org/freedesktop/DBus org.freedesktop.DBus.NameHasOwner \
string:org.freedesktop.secrets 2>/dev/null | grep -q true && break
sleep 0.1
done
fi
env -u WAYLAND_DISPLAY -u DISPLAY -u DBUS_SESSION_BUS_ADDRESS -u NACRE_HOME \
-u http_proxy -u https_proxy -u all_proxy -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY \
"${dispvars[@]}" "${busvars[@]}" \
HOME="$home" XDG_CONFIG_HOME="$home/.config" XDG_DATA_HOME="$home/.local/share" \
XDG_CACHE_HOME="$home/.cache" XDG_STATE_HOME="$home/.local/state" XDG_RUNTIME_DIR="$runtime" \
RUST_BACKTRACE=1 NACRE_SCRIPT="$out/script.txt" \
timeout -k 10 "${SMOKE_TIMEOUT:-180}" "$bin" ${SMOKE_ARGS:-} >"$out/nacre.log" 2>&1
code=$?
fail=0
if [ "$code" -ne 0 ]; then
echo "smoke: browser exited with $code$([ "$code" -eq 124 ] && echo ' (timeout)')"
fail=1
fi
if grep -q panicked "$out/nacre.log"; then
echo "smoke: panic in nacre.log:"
grep -m3 -A2 panicked "$out/nacre.log"
fail=1
fi
if grep -q "ASSERT FAILED" "$out/selftest.log" 2>/dev/null; then
echo "smoke: failed asserts:"
grep "ASSERT FAILED" "$out/selftest.log"
fail=1
fi
if [ ! -f "$out/done.json" ]; then
echo "smoke: script did not reach 'status done'"
fail=1
fi
echo "smoke: $([ $fail -eq 0 ] && echo ok || echo FAILED), results in $out"
exit $fail