interplane.Get Omega
Read in your language

readings-window

aien-dev/readings-window

Brought by drakestapleton

branch main Branches Tags History

readings-window / scripts / test-exposure-baseline.sh

33 lines · 2422 bytes Raw Blame History
1#!/bin/bash
2# Checks exposure-watch.sh baseline parsing (no scan, no writes): run with `bash scripts/test-exposure-baseline.sh`.
3# Regression: from 2026-11-07 the live baseline was written with a space after each colon, the old grep read
4# every list as empty, and the watch reported "changed" on every run for a week. Exit 0 only if every case passes.
5set -u
6here=$(cd "$(dirname "$0")" && pwd); W="$here/exposure-watch.sh"
7t=$(mktemp -d); trap 'rm -rf "$t"' EXIT
8fail=0
9check() { # name, file content (or - for no file), expected output, expected rc
10 local name=$1 body=$2 want=$3 wrc=$4 f="$t/$1.json" out rc
11 [ "$body" = - ] || printf '%s' "$body" > "$f"
12 out=$(bash "$W" --parse-baseline "$f" 2>&1); rc=$?
13 if [ "$out" = "$want" ] && [ "$rc" = "$wrc" ]; then echo "PASS $name"
14 else echo "FAIL $name rc=$rc want_rc=$wrc"; echo " got: ${out//$'\n'/ | }"; echo " want: ${want//$'\n'/ | }"; fail=1; fi
15}
16full=$'expected_tcp=443 \noptional_tcp=53 80 \nexpected_udp='
17check compact '{"expected_tcp":[443],"optional_tcp":[53,80],"expected_udp":[],"note":"x"}' "$full" 0
18check spaced '{"expected_tcp": [443], "optional_tcp": [53, 80], "expected_udp": [], "note": "x"}' "$full" 0
19check pretty $'{\n "expected_tcp" : [ 443 ],\n "optional_tcp" : [\n 80,\n 53\n ],\n "expected_udp" : [ ]\n}\n' "$full" 0
20check two_udp '{"expected_tcp":[443,9443],"optional_tcp":[],"expected_udp":[41641,3478]}' $'expected_tcp=443 9443 \noptional_tcp=\nexpected_udp=3478 41641 ' 0
21check missing_key '{"expected_tcp":[443],"optional_tcp":[53,80]}' $'expected_tcp=443 \noptional_tcp=53 80 \nexpected_udp=MISSING' 1
22check no_file - $'expected_tcp=MISSING\noptional_tcp=MISSING\nexpected_udp=MISSING' 1
23# Mutant check: a copy of exposure-watch.sh with its whitespace removal taken out must fail the
24# "spaced" case, so this test is shown to catch the regression it guards against.
25m="$t/mutant.sh"; sed 's/flat=$(tr -d [^<]*< "$BASE")/flat=$(cat "$BASE")/' "$W" > "$m"
26if cmp -s "$W" "$m"; then echo "FAIL mutant did not apply"; fail=1
27else
28 printf '%s' '{"expected_tcp": [443], "optional_tcp": [53, 80], "expected_udp": [], "note": "x"}' > "$t/m.json"
29 if [ "$(bash "$m" --parse-baseline "$t/m.json" 2>&1)" = "$full" ]; then echo "FAIL mutant (not caught)"; fail=1
30 else echo "PASS mutant (whitespace removal taken out: spaced baseline misread, caught)"; fi
31fi
32[ $fail = 0 ] && echo "ALL PASS" || echo "SOME FAILED"
33exit $fail