#!/usr/bin/env bash # Real CivetWeb HTTP smoke test. Run after make native. Never touches production config. set -euo pipefail cd "$(dirname "$0")/.." command -v curl >/dev/null; command -v jq >/dev/null [[ -x build/mars_vme ]] d=$(mktemp -d); pid=''; trap 'if [[ -n $pid ]]; then kill "$pid" 2>/dev/null || true; wait "$pid" 2>/dev/null || true; fi; rm -rf "$d"' EXIT port=${TEST_PORT:-18787}; base="http://127.0.0.1:$port"; cfg="$d/nwserv.conf" # Fixture need not pass validation to test auth and preview. Real Ethernet name selected below. cat > "$cfg" <<'CONF' 2 OLD 3 0x12345678 1 4 0x101 placeholder 802.3 1 1 SYS /tmp 5 0 12 SUPERVISOR root DemoPass123 200 0 99 PRESERVE CONF chmod 600 "$cfg" start(){ ./build/mars_vme --config "$cfg" --www "$PWD/websetup" --bind 127.0.0.1 --port "$port" --no-services >"$d/log" 2>&1 & pid=$!; for i in $(seq 1 70); do if curl -fsS "$base/login" >/dev/null 2>&1 && kill -0 "$pid"; then return; fi; sleep .1; done; cat "$d/log" >&2; return 1; } get(){ curl -fsS -b "$d/cookie" "$base/$1"; } post(){ curl -fsS -b "$d/cookie" -c "$d/cookie" -H "Origin: $base" -H 'Content-Type: application/json' -H "X-Mars-CSRF: ${csrf:-}" -d "$2" "$base/$1"; } status(){ curl -sS -o "$d/response" -w '%{http_code}' "$@"; } start [[ $(status "$base/api/state") == 401 ]] [[ $(status -H 'Host: evil.example' "$base/login") == 403 ]] [[ $(status -H 'Content-Type: application/json' -d '{"user":"admin","password":"admin"}' "$base/api/login") == 403 ]] post api/login '{"user":"admin","password":"admin"}' | jq -e .ok >/dev/null s=$(get api/state); jq -e '.defaultPassword and (.text=="")' <<<"$s" >/dev/null csrf=$(jq -r .csrf <<<"$s"); cp "$d/cookie" "$d/old-cookie" [[ $(status -b "$d/cookie" -H "Origin: $base" -H 'Content-Type: application/json' -d '{}' "$base/api/logout") == 403 ]] post api/password '{"current":"admin","new":"NativeTestPassword123","repeat":"NativeTestPassword123"}' | jq -e .ok >/dev/null [[ $(status -b "$d/old-cookie" "$base/api/state") == 401 ]] post api/login '{"user":"admin","password":"NativeTestPassword123"}' | jq -e .ok >/dev/null s=$(get api/state); jq -e '(.defaultPassword|not) and (.text|contains("99 PRESERVE"))' <<<"$s" >/dev/null csrf=$(jq -r .csrf <<<"$s"); rev=$(jq -r .revision <<<"$s"); dev=$(jq -r '.devices[0].name // empty' <<<"$s") if [[ -n $dev ]]; then request=$(jq -nc --arg rev "$rev" --arg dev "$dev" '{revision:$rev,mode:"guided",groups:{"2":["NEW"],"4":[("0x101 "+$dev+" 802.3 1")]}}') result=$(post api/preview "$request"); jq -e '.errors|length==0' <<<"$result" >/dev/null hash=$(jq -r .candidateHash <<<"$result"); save=$(jq -c --arg hash "$hash" '.+{confirm:true,candidateHash:$hash}' <<<"$request") post api/save "$save" | jq -e '.backup|length>0' >/dev/null; grep -q '99 PRESERVE' "$cfg"; grep -q '2 NEW' "$cfg" [[ $(status -b "$d/cookie" -H "Origin: $base" -H 'Content-Type: application/json' -H "X-Mars-CSRF: $csrf" -d "$save" "$base/api/save") == 409 ]] s=$(get api/state); rev=$(jq -r .revision <<<"$s"); bn=$(jq -r '.backups[0]' <<<"$s"); request=$(jq -nc --arg rev "$rev" --arg bn "$bn" '{revision:$rev,mode:"backup",name:$bn}'); post api/preview "$request" | jq -e '.candidate|contains("2 OLD")' >/dev/null else echo 'SKIP save via HTTP: no Ethernet interface in test environment'; fi get api/download | grep -q '99 PRESERVE' post api/logout '{}' | jq -e .ok >/dev/null [[ $(status -b "$d/cookie" "$base/api/state") == 401 ]] kill "$pid"; wait "$pid"; pid=''; start post api/login '{"user":"admin","password":"NativeTestPassword123"}' | jq -e .ok >/dev/null printf 'PASS: real CivetWeb login, origin/host/CSRF, default-password guard, persistence, logout, preview/save/backup (when Ethernet exists).\n'