readings-window
aien-dev/readings-window
Brought by drakestapleton
readings-window / scripts / test-exposure-baseline.sh
| 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. |
| 5 | set -u |
| 6 | here=$(cd "$(dirname "$0")" && pwd); W="$here/exposure-watch.sh" |
| 7 | t=$(mktemp -d); trap 'rm -rf "$t"' EXIT |
| 8 | fail=0 |
| 9 | check() { # 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 | } |
| 16 | full=$'expected_tcp=443 \noptional_tcp=53 80 \nexpected_udp=' |
| 17 | check compact '{"expected_tcp":[443],"optional_tcp":[53,80],"expected_udp":[],"note":"x"}' "$full" 0 |
| 18 | check spaced '{"expected_tcp": [443], "optional_tcp": [53, 80], "expected_udp": [], "note": "x"}' "$full" 0 |
| 19 | check pretty $'{\n "expected_tcp" : [ 443 ],\n "optional_tcp" : [\n 80,\n 53\n ],\n "expected_udp" : [ ]\n}\n' "$full" 0 |
| 20 | check two_udp '{"expected_tcp":[443,9443],"optional_tcp":[],"expected_udp":[41641,3478]}' $'expected_tcp=443 9443 \noptional_tcp=\nexpected_udp=3478 41641 ' 0 |
| 21 | check missing_key '{"expected_tcp":[443],"optional_tcp":[53,80]}' $'expected_tcp=443 \noptional_tcp=53 80 \nexpected_udp=MISSING' 1 |
| 22 | check 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. |
| 25 | m="$t/mutant.sh"; sed 's/flat=$(tr -d [^<]*< "$BASE")/flat=$(cat "$BASE")/' "$W" > "$m" |
| 26 | if cmp -s "$W" "$m"; then echo "FAIL mutant did not apply"; fail=1 |
| 27 | else |
| 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 |
| 31 | fi |
| 32 | [ $fail = 0 ] && echo "ALL PASS" || echo "SOME FAILED" |
| 33 | exit $fail |