#!/bin/bash
# POC runner. Requires the chromium-changes.patch to be applied and chrome
# rebuilt (`autoninja -C out/asan chrome`).
set -e

ARTIFACTS="$(cd "$(dirname "$0")" && pwd)"
CHROME=${CHROME:-/chromium/chromium/src/out/asan/chrome}
PROFILE=$(mktemp -d /tmp/chrome-poc-profile.XXXXXX)
LOG="$ARTIFACTS/poc-output.log"

cleanup() {
  kill $SERVER_PID 2>/dev/null || true
  rm -rf "$PROFILE"
}
trap cleanup EXIT

python3 "$ARTIFACTS/server.py" >/dev/null 2>&1 &
SERVER_PID=$!
sleep 1

# --no-sandbox / --ozone-platform=headless / --disable-gpu are environment
# accommodations (no display server, no setuid sandbox helper) and do not
# affect the bug. --password-store=basic avoids a dbus dependency.
timeout 25 "$CHROME" \
  --ozone-platform=headless \
  --no-sandbox \
  --disable-gpu \
  --no-first-run \
  --no-default-browser-check \
  --password-store=basic \
  --user-data-dir="$PROFILE" \
  http://localhost:8123/index.html \
  >"$LOG" 2>&1 || true

echo "==== POC log lines ===="
grep -E '\[POC-' "$LOG" || true
echo
echo "(full log: $LOG)"
