#!/bin/bash
# test-audit-fixtures.sh — fixture test for kido-audit-abs-refs.sh.
#
# Drives the audit's parsing/decision core (analyze()) against
# hand-written readelf-output fixtures — no toolchain needed. Real
# pass/fail: the positive fixture must PASS with exact counts
# (total=10 applied=8 carved=2); each negative fixture must FAIL.
# The thin readelf-invocation layer in main() is NOT covered here;
# it is untested by construction (stated in the audit header).
set -u

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1091
source "$SCRIPT_DIR/kido-audit-abs-refs.sh"  # functions only; main() is guarded

FX="$SCRIPT_DIR/fixtures"
fail=0

echo "--- positive fixture (expect PASS 10/8/2) ---"
if analyze "$FX/symtab.txt" "$FX/relocs.txt" "$FX/sections.txt" "$FX/objrelocs.txt"; then
    if [ "$AUDIT_TOTAL" -eq 10 ] && [ "$AUDIT_APPLIED" -eq 8 ] && [ "$AUDIT_CARVED" -eq 2 ]; then
        echo "PASS: positive fixture (total=10 applied=8 carved=2)"
    else
        echo "FAIL: positive fixture passed with wrong counts: total=$AUDIT_TOTAL applied=$AUDIT_APPLIED carved=$AUDIT_CARVED"
        fail=1
    fi
else
    echo "FAIL: positive fixture did not pass"
    fail=1
fi

echo "--- negative-A fixture: rogue .text reloc (expect FAIL) ---"
if analyze "$FX/symtab.txt" "$FX/relocs-rogue-text.txt" "$FX/sections.txt" "$FX/objrelocs.txt" >/dev/null 2>&1; then
    echo "FAIL: negative-A passed — rogue .text reloc not caught"
    fail=1
else
    echo "PASS: negative-A failed the audit as required"
fi

echo "--- negative-B fixture: rogue ABS symbol (expect FAIL) ---"
if analyze "$FX/symtab.txt" "$FX/relocs.txt" "$FX/sections.txt" "$FX/objrelocs-rogue-sym.txt" >/dev/null 2>&1; then
    echo "FAIL: negative-B passed — rogue ABS symbol not caught"
    fail=1
else
    echo "PASS: negative-B failed the audit as required"
fi

if [ "$fail" -eq 0 ]; then echo "== fixture test: ALL PASS =="; else echo "== fixture test: FAILURES =="; fi
exit "$fail"
