#!/usr/bin/env bash # Phase 2 Wayland compositor proof — automated runtime validation harness. # # Modes: # --guest Run inside a Red Bear OS guest # --qemu [CONFIG] Boot CONFIG in QEMU and run the same checks automatically # # Exit codes: # 0 — all checks passed # 1 — one or more checks failed # 2 — QEMU boot or login failure set -euo pipefail find_uefi_firmware() { local candidates=( "/usr/share/ovmf/x64/OVMF.4m.fd" "/usr/share/OVMF/x64/OVMF.4m.fd" "/usr/share/ovmf/x64/OVMF_CODE.4m.fd" "/usr/share/OVMF/x64/OVMF_CODE.4m.fd" "/usr/share/qemu/edk2-x86_64-code.fd" ) local path for path in "${candidates[@]}"; do if [[ -f "$path" ]]; then printf '%s\n' "$path" return 0 fi done return 1 } run_guest_checks() { echo "=== Red Bear OS Phase 2 Wayland Runtime Validation ===" echo local failures=0 local expected_bins=( "redbear-phase2-wayland-check" ) local bin for bin in "${expected_bins[@]}"; do if ! command -v "$bin" >/dev/null 2>&1; then echo " FAIL $bin: required Phase 2 check binary is not installed" failures=$((failures + 1)) fi done if [[ "$failures" -eq 0 ]]; then echo " Running redbear-phase2-wayland-check..." if redbear-phase2-wayland-check --json >/dev/null 2>&1; then echo " PASS redbear-phase2-wayland-check: Wayland compositor proof checks passed" else echo " FAIL redbear-phase2-wayland-check: Wayland compositor proof checks failed" failures=$((failures + 1)) fi fi echo echo "=== Phase 2 Wayland Runtime Validation Complete ===" if [[ "$failures" -gt 0 ]]; then echo " $failures check(s) FAILED" return 1 fi echo " All checks PASSED" return 0 } run_qemu_checks() { local config="${1:-redbear-full}" local firmware firmware="$(find_uefi_firmware)" || { echo "ERROR: no usable x86_64 UEFI firmware found" >&2 exit 2 } local arch image iso extra arch="${ARCH:-$(uname -m)}" image="build/$arch/$config/harddrive.img" iso="build/$arch/$config.iso" extra="build/$arch/$config/extra.img" boot_media="" if [[ -f "$iso" ]]; then boot_media="iso" elif [[ -f "$image" ]]; then boot_media="harddrive" else echo "ERROR: no bootable image found ($iso or $image)" >&2 echo "Build it first with: ./local/scripts/build-redbear.sh $config" >&2 exit 2 fi if [[ "$boot_media" == "iso" ]]; then boot_args=(-cdrom "$iso") else boot_args=(-drive file="$image",format=raw,if=none,id=drv0,snapshot=on -device nvme,drive=drv0,serial=NVME_SERIAL) fi if [[ ! -f "$extra" ]]; then truncate -s 1g "$extra" fi attempt=1 while true; do rc=0 python3 local/scripts/qemu-login-expect.py \ --timeout 300 \ --log "build/$arch/$config/phase2-check.log" \ --step "expect:login:" \ --step "send:root" \ --step "expect:assword:" \ --step "send:password" \ --step "expect:Type 'help' for available commands." \ --step "send:echo __READY__" \ --step "expect:__READY__" \ --step "send:command -v redbear-phase2-wayland-check >/dev/null 2>&1 && echo __PHASE2_BIN_OK__ || echo __PHASE2_BIN_FAIL__" \ --step "expect:__PHASE2_BIN_OK__" \ --step "send:redbear-phase2-wayland-check --json >/dev/null 2>&1 && echo __PHASE2_OK__ || echo __PHASE2_FAIL__" \ --step "expect:__PHASE2_OK__" \ --step "send:echo __PHASE2_RUNTIME_DONE__" \ --step "expect:__PHASE2_RUNTIME_DONE__" \ --step "send:shutdown" \ --pass_marker "__PHASE2_BIN_OK__" \ --pass_marker "__PHASE2_OK__" \ --pass_marker "__PHASE2_RUNTIME_DONE__" \ --fail_marker "__PHASE2_BIN_FAIL__" \ --fail_marker "__PHASE2_FAIL__" \ -- \ qemu-system-x86_64 -name "Red Bear OS x86_64" -device qemu-xhci -smp 4 -m 2048 -bios "$firmware" -chardev stdio,id=debug,signal=off,mux=on -serial chardev:debug -mon chardev:debug -machine q35 -device ich9-intel-hda -device hda-output -device virtio-net,netdev=net0 -netdev user,id=net0 -display none -vga none "${boot_args[@]}" -drive file="$extra",format=raw,if=none,id=drv1,snapshot=on -device nvme,drive=drv1,serial=NVME_EXTRA -enable-kvm -cpu host || rc=$? if [[ $rc -eq 0 ]]; then break fi if [[ ( $rc -eq 3 || $rc -eq 137 ) && $attempt -lt 5 ]]; then attempt=$((attempt + 1)) echo "QEMU exited before completing the check; retrying (attempt $attempt/5)" sleep 2 continue fi exit "$rc" done pkill -f "qemu-system-x86_64.*$config" 2>/dev/null || true echo "Phase 2 Wayland runtime validation completed via guest runtime check" exit 0 } usage() { cat <<'USAGE' Usage: ./local/scripts/test-phase2-runtime.sh --guest ./local/scripts/test-phase2-runtime.sh --qemu [redbear-full] This script validates the Phase 2 Wayland compositor proof by running the canonical Phase 2 check binary and treating its exit code as authoritative. Required binary (must be in PATH inside the guest): redbear-phase2-wayland-check — Wayland compositor proof validation USAGE } case "${1:-}" in --guest) run_guest_checks ;; --qemu) run_qemu_checks "${2:-redbear-full}" ;; *) usage exit 1 ;; esac