| 1 | #!/bin/sh |
| 2 | # tests/run.sh - offline checks for plugin_sysmon across all 3 modes. |
| 3 | # |
| 4 | # Runs against ./plugin_sysmon (native build) on the live machine, so the |
| 5 | # metrics assertions use this node's real /proc (or mach / Win32) counters. |
| 6 | # If a musl static build or a Windows PE (plugin_sysmon.exe) is present they are |
| 7 | # checked too. The whole git history is finally audited for secrets and LAN IPs. |
| 8 | # |
| 9 | # Exit code 0 = everything passed. |
| 10 | |
| 11 | set -u |
| 12 | |
| 13 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 14 | cd "$ROOT" || exit 1 |
| 15 | |
| 16 | BIN=./plugin_sysmon |
| 17 | PASS=0 |
| 18 | FAIL=0 |
| 19 | |
| 20 | if [ -t 1 ]; then |
| 21 | C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') |
| 22 | else |
| 23 | C_OK=; C_BAD=; C_B=; C_0= |
| 24 | fi |
| 25 | pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } |
| 26 | fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } |
| 27 | |
| 28 | check() { # check <desc> <expected-substring> <actual> |
| 29 | if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else |
| 30 | fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)" |
| 31 | fi |
| 32 | } |
| 33 | |
| 34 | check_not() { # check_not <desc> <forbidden-substring> <actual> |
| 35 | if printf '%s' "$3" | grep -F -q -- "$2"; then |
| 36 | fail "$1 (must NOT contain: $2)"; else pass "$1" |
| 37 | fi |
| 38 | } |
| 39 | |
| 40 | section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } |
| 41 | |
| 42 | body() { printf '%s' "$1" | sed '1,/^\r$/d'; } |
| 43 | |
| 44 | # ---------------------------------------------------------------- build |
| 45 | section "build" |
| 46 | if [ ! -x "$BIN" ]; then |
| 47 | echo " building native binary..." |
| 48 | cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_sysmon.c || { echo " build failed"; exit 1; } |
| 49 | fi |
| 50 | [ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" |
| 51 | |
| 52 | # ---------------------------------------------------------------- meta mode |
| 53 | section "mode 1 --meta (must work without auth - discovery)" |
| 54 | META=$("$BIN" --meta) |
| 55 | check "meta emits name sysmon" '"name":"sysmon"' "$META" |
| 56 | check "meta emits desc CPU & memory" '"desc":"CPU & memory"' "$META" |
| 57 | METAENV=$(SX_PLUGIN_META=1 "$BIN") |
| 58 | check "SX_PLUGIN_META=1 yields same meta" '"name":"sysmon"' "$METAENV" |
| 59 | LEN=$(printf '%s' "$META" | wc -c | tr -d ' ') |
| 60 | [ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)" |
| 61 | printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 62 | && pass "meta is valid JSON" || fail "meta is not valid JSON" |
| 63 | DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') |
| 64 | [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" |
| 65 | NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') |
| 66 | [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" |
| 67 | check "--version prints version" '1.0.2' "$("$BIN" --version)" |
| 68 | check "--help is available" 'usage:' "$("$BIN" --help)" |
| 69 | META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) |
| 70 | check "meta still works when BOT_PASSWORD is set" '"name":"sysmon"' "$META_AUTH" |
| 71 | |
| 72 | # ---------------------------------------------------------------- auth |
| 73 | section "node auth" |
| 74 | AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") |
| 75 | check "no credentials -> 401" 'Status: 401' "$AUTHNONE" |
| 76 | check "401 body is json" 'invalid node password' "$AUTHNONE" |
| 77 | AUTHH=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") |
| 78 | check "X-Bot-Password accepted" '"ok":true' "$(body "$AUTHH")" |
| 79 | AUTHB=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=metrics' "$BIN") |
| 80 | check "Authorization Bearer accepted" '"ok":true' "$(body "$AUTHB")" |
| 81 | AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=testpw' "$BIN") |
| 82 | check "?pwd= accepted (plugin iframe)" '"ok":true' "$(body "$AUTHQ")" |
| 83 | AUTHBAD=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=wrong' "$BIN") |
| 84 | check "wrong ?pwd= rejected" 'Status: 401' "$AUTHBAD" |
| 85 | AUTHHTML=$(BOT_PASSWORD=testpw QUERY_STRING='' "$BIN") |
| 86 | check "html also requires auth" 'Status: 401' "$AUTHHTML" |
| 87 | |
| 88 | # ---------------------------------------------------------------- metrics |
| 89 | section "mode 2 action=metrics (live counters)" |
| 90 | RAW=$(QUERY_STRING='action=metrics' "$BIN") |
| 91 | MB=$(body "$RAW") |
| 92 | check_not "metrics has no Content-Length" 'Content-Length' "$RAW" |
| 93 | check "metrics content-type json" 'Content-Type: application/json' "$RAW" |
| 94 | check "metrics ok:true" '"ok":true' "$MB" |
| 95 | check "metrics action echoed" '"action":"metrics"' "$MB" |
| 96 | check "metrics has cpu_pct" '"cpu_pct":' "$MB" |
| 97 | check "metrics has ncores" '"ncores":' "$MB" |
| 98 | check "metrics has mem block" '"mem":{"total_kb":' "$MB" |
| 99 | check "metrics has swap block" '"swap":{"total_kb":' "$MB" |
| 100 | check "metrics has uptime" '"uptime_sec":' "$MB" |
| 101 | check "metrics has load block" '"load":{"available":' "$MB" |
| 102 | check "metrics has processes" '"processes":[' "$MB" |
| 103 | check "metrics has host" '"host":' "$MB" |
| 104 | check "metrics has cpu_model" '"cpu_model":' "$MB" |
| 105 | check "metrics has utc" '"utc":"20' "$MB" |
| 106 | check "metrics top defaults to min(25, proc_count)" '"top":' "$MB" |
| 107 | printf '%s' "$MB" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 108 | && pass "metrics body is valid JSON" || fail "metrics body JSON invalid" |
| 109 | |
| 110 | # real, non-zero assertions - these are what prove the sampler works |
| 111 | MJ=/tmp/sysmon_metrics_$$.json |
| 112 | printf '%s' "$MB" > "$MJ" |
| 113 | python3 - "$MJ" <<'PY' && pass "live counters are non-zero and in range" || fail "live counters wrong" |
| 114 | import json,sys |
| 115 | d=json.load(open(sys.argv[1])) |
| 116 | assert d["mem"]["total_kb"] > 0, "mem_total must be > 0" |
| 117 | assert d["mem"]["used_kb"] > 0, "mem_used must be > 0" |
| 118 | assert 0 <= d["mem"]["pct"] <= 100 |
| 119 | assert 0 <= d["cpu_pct"] <= 100 |
| 120 | assert d["ncores"] >= 1 |
| 121 | assert len(d["cores"]) == d["ncores"] |
| 122 | for c in d["cores"]: |
| 123 | assert 0 <= c["pct"] <= 100 |
| 124 | assert d["uptime_sec"] > 0, "uptime must be > 0" |
| 125 | assert d["proc_count"] > 0, "proc_count must be > 0" |
| 126 | assert len(d["processes"]) == d["top"] |
| 127 | assert d["top"] <= d["proc_count"] |
| 128 | assert d["swap"]["total_kb"] >= 0 |
| 129 | PY |
| 130 | |
| 131 | python3 - "$MJ" <<'PY' && pass "process rows carry pid, name, cpu, rss" || fail "process rows malformed" |
| 132 | import json,sys |
| 133 | d=json.load(open(sys.argv[1])) |
| 134 | assert len(d["processes"]) > 0 |
| 135 | for p in d["processes"]: |
| 136 | assert isinstance(p["pid"], int) and p["pid"] > 0 |
| 137 | assert isinstance(p["name"], str) and p["name"] |
| 138 | assert p["cpu"] >= 0 |
| 139 | assert isinstance(p["rss_kb"], int) and p["rss_kb"] >= 0 |
| 140 | PY |
| 141 | |
| 142 | python3 - "$MJ" <<'PY' && pass "cpu percentages are sorted descending" || fail "cpu sort wrong" |
| 143 | import json,sys |
| 144 | d=json.load(open(sys.argv[1])) |
| 145 | vals=[p["cpu"] for p in d["processes"]] |
| 146 | assert vals == sorted(vals, reverse=True), vals |
| 147 | PY |
| 148 | rm -f "$MJ" |
| 149 | |
| 150 | section "mode 2 sorting, limits and unknown actions" |
| 151 | MEMF=/tmp/sysmon_mem_$$.json |
| 152 | MEM=$(body "$(QUERY_STRING='action=metrics&sort=mem&top=5' "$BIN")") |
| 153 | printf '%s' "$MEM" > "$MEMF" |
| 154 | check "sort=mem echoed" '"sort":"mem"' "$MEM" |
| 155 | check "top=5 honoured" '"top":5' "$MEM" |
| 156 | python3 - "$MEMF" <<'PY' && pass "memory sort is descending" || fail "mem sort wrong" |
| 157 | import json,sys |
| 158 | d=json.load(open(sys.argv[1])) |
| 159 | v=[p["rss_kb"] for p in d["processes"]] |
| 160 | assert v == sorted(v, reverse=True), v |
| 161 | assert len(v) <= 5 |
| 162 | PY |
| 163 | rm -f "$MEMF" |
| 164 | TOP1=$(body "$(QUERY_STRING='action=metrics&top=1' "$BIN")") |
| 165 | check "top=1 honoured" '"top":1' "$TOP1" |
| 166 | UNK=$(QUERY_STRING='action=bogus' "$BIN") |
| 167 | check "unknown action rejected" 'unknown_action' "$(body "$UNK")" |
| 168 | check "unknown action lists supported" '"supported":["metrics","info"]' "$(body "$UNK")" |
| 169 | check "unknown action echoes request" '"requested":"bogus"' "$(body "$UNK")" |
| 170 | BIGTOP=$(body "$(QUERY_STRING='action=metrics&top=99999' "$BIN")") |
| 171 | check "absurd top still returns a valid snapshot" '"ok":true' "$BIGTOP" |
| 172 | check "absurd top is capped" '"top":' "$BIGTOP" |
| 173 | |
| 174 | section "mode 2 action=info" |
| 175 | INFO=$(QUERY_STRING='action=info' "$BIN") |
| 176 | IBODY=$(body "$INFO") |
| 177 | check "info ok:true" '"ok":true' "$IBODY" |
| 178 | check "info action info" '"action":"info"' "$IBODY" |
| 179 | check "info pid present" '"pid":' "$IBODY" |
| 180 | check "info utc present" '"utc":"20' "$IBODY" |
| 181 | check "info zero_network declared" '"zero_network":true' "$IBODY" |
| 182 | check "info sample window declared" '"samples_cpu_delta_ms":80' "$IBODY" |
| 183 | check "info node_auth false when unset" '"node_auth":false' "$IBODY" |
| 184 | INFOAUTH=$(body "$(BOT_PASSWORD=pw QUERY_STRING='action=info&pwd=pw' "$BIN")") |
| 185 | check "info node_auth true when set" '"node_auth":true' "$INFOAUTH" |
| 186 | printf '%s' "$IBODY" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 187 | && pass "info body is valid JSON" || fail "info body JSON invalid" |
| 188 | check "format=json works as alias" '"action":"metrics"' "$(body "$(QUERY_STRING='format=json&top=2' "$BIN")")" |
| 189 | |
| 190 | # ---------------------------------------------------------------- html |
| 191 | section "mode 3 HTML" |
| 192 | HTML=$(QUERY_STRING='' "$BIN") |
| 193 | check "html content-type" 'Content-Type: text/html; charset=utf-8' "$HTML" |
| 194 | check_not "html has no Content-Length" 'Content-Length' "$HTML" |
| 195 | check "html has viewport meta" 'width=device-width' "$HTML" |
| 196 | check "html title sysmon" '<title>sysmon</title>' "$HTML" |
| 197 | check "html back-link present" 'Back to Bot' "$HTML" |
| 198 | check "html theme switch hook" 'bot_theme' "$HTML" |
| 199 | |
| 200 | # ---- theme handoff (bot_theme_plugin.h) ---- |
| 201 | # the bot owns the theme: ?theme= before first paint, sx-theme after it |
| 202 | check "html reads ?theme= before paint" 'theme=(light|dark)' "$HTML" |
| 203 | check "html listens for sx-theme" "d.type==='sx-theme'" "$HTML" |
| 204 | check "html asks the host for the theme" "sx-theme-request" "$HTML" |
| 205 | check "html uses bot token --bg-card" "--bg-card" "$HTML" |
| 206 | check "html uses bot token --surface-2" "--surface-2" "$HTML" |
| 207 | check "html uses bot token --text-2" "--text-2" "$HTML" |
| 208 | check "html hides its toggle when embedded" "embed-hide" "$HTML" |
| 209 | check_not "html no retired brand casing" 'Scorpio'"X Bot" "$HTML" |
| 210 | check_not "html no private palette 0b0b0e" '0b0b0e' "$HTML" |
| 211 | check_not "html no private palette 141419" '141419' "$HTML" |
| 212 | check_not "html no private palette f2f2f6" 'f2f2f6' "$HTML" |
| 213 | THEMEQ=$(QUERY_STRING='theme=light' "$BIN") |
| 214 | check "?theme=light still renders the UI" 'prefers-color-scheme' "$THEMEQ" |
| 215 | # ---- end theme handoff ---- |
| 216 | |
| 217 | check "html per-core host" "id='cores'" "$HTML" |
| 218 | check "html history sparkline" "id='spark'" "$HTML" |
| 219 | check "html memory gauge" "memFill" "$HTML" |
| 220 | check "html swap gauge" "swapFill" "$HTML" |
| 221 | check "html process table host" "id='rows'" "$HTML" |
| 222 | check "html process filter input" "id='q'" "$HTML" |
| 223 | check "html rate segmented control" 'data-ms=' "$HTML" |
| 224 | check "html sort segmented control" 'data-sort=' "$HTML" |
| 225 | check "html calls the metrics action" 'action=metrics' "$HTML" |
| 226 | check "html uses /plugins?plugin= route" "/plugins?plugin=" "$HTML" |
| 227 | check "html falls back to /plugin_sysmon" "/plugin_sysmon" "$HTML" |
| 228 | check "html closes document" '</html>' "$HTML" |
| 229 | check "html declares zero network" 'zero outbound network' "$HTML" |
| 230 | EMB=$(QUERY_STRING='embed=1' "$BIN") |
| 231 | check_not "embed=1 strips the back-link" 'Back to Bot' "$EMB" |
| 232 | check "embed=1 still renders the UI" "id='cores'" "$EMB" |
| 233 | EMBT=$(QUERY_STRING='embed=true' "$BIN") |
| 234 | check_not "embed=true also strips back-link" 'Back to Bot' "$EMBT" |
| 235 | |
| 236 | ZEROEMOJI=$(printf '%s' "$HTML" | python3 -c ' |
| 237 | import sys |
| 238 | s = sys.stdin.read() |
| 239 | bad = [c for c in s if ord(c) > 0x2600 and ord(c) not in (0x2014, 0x2013, 0x2026)] |
| 240 | print(len(bad)) |
| 241 | ') |
| 242 | [ "$ZEROEMOJI" = "0" ] && pass "HTML contains no emoji glyphs" || fail "HTML has $ZEROEMOJI emoji glyph(s)" |
| 243 | PK="pico""bot" |
| 244 | check_not "html has no internal fleet paths" "$PK" "$HTML" |
| 245 | check_not "html has no private IPs" "192.168." "$HTML" |
| 246 | check_not "html has no ssh/sshpass" "sshpass" "$HTML" |
| 247 | check_not "html has no popen of shell" "child_process" "$HTML" |
| 248 | |
| 249 | section "responsive markup" |
| 250 | check "html has a 640px breakpoint" '@media(max-width:640px)' "$HTML" |
| 251 | check "html hides columns on mobile" 'hide-sm' "$HTML" |
| 252 | check "html grids use auto-fill" 'auto-fill,minmax' "$HTML" |
| 253 | check "html sparkline scales" "preserveAspectRatio='none'" "$HTML" |
| 254 | |
| 255 | section "no shell, no sockets" |
| 256 | SRCCAT=$(cat src/plugin_sysmon.c) |
| 257 | check_not "linux/mac code has no popen of user input" 'popen(\"ps' "$SRCCAT" |
| 258 | check_not "source opens no sockets" 'socket(' "$SRCCAT" |
| 259 | check_not "source has no curl or wget" 'wget' "$SRCCAT" |
| 260 | check_not "source has no ssh invocation" 'ssh ' "$SRCCAT" |
| 261 | check_not "source has no sshpass" 'sshpass' "$SRCCAT" |
| 262 | |
| 263 | # ---------------------------------------------------------------- optional targets |
| 264 | section "optional cross builds" |
| 265 | if [ -f plugin_sysmon.musl ]; then |
| 266 | M=$(./plugin_sysmon.musl --meta) |
| 267 | check "musl build --meta" '"name":"sysmon"' "$M" |
| 268 | case $(head -c 4 plugin_sysmon.musl | od -An -tx1 | tr -d ' \n') in |
| 269 | 7f454c46) pass "musl artifact is ELF" ;; |
| 270 | *) fail "musl artifact is not ELF" ;; |
| 271 | esac |
| 272 | else |
| 273 | echo " skip musl (not built)" |
| 274 | fi |
| 275 | if [ -f plugin_sysmon.exe ]; then |
| 276 | MAGIC=$(head -c 2 plugin_sysmon.exe) |
| 277 | [ "$MAGIC" = "MZ" ] && pass "windows artifact has MZ header" || fail "windows artifact missing MZ" |
| 278 | SZ=$(wc -c < plugin_sysmon.exe | tr -d ' ') |
| 279 | [ "$SZ" -gt 20000 ] && pass "windows artifact is not an error page ($SZ bytes)" || fail "windows artifact suspiciously small ($SZ bytes)" |
| 280 | else |
| 281 | echo " skip windows PE (not built)" |
| 282 | fi |
| 283 | if [ -f plugin_sysmon.macos-arm64 ]; then |
| 284 | MF=$(head -c 4 plugin_sysmon.macos-arm64 | od -An -tx1 | tr -d ' \n') |
| 285 | case "$MF" in |
| 286 | cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-arm64 artifact is Mach-O" ;; |
| 287 | *) fail "macos-arm64 artifact is not Mach-O ($MF)" ;; |
| 288 | esac |
| 289 | else |
| 290 | echo " skip macos-arm64 (not built)" |
| 291 | fi |
| 292 | if [ -f plugin_sysmon.macos-x86_64 ]; then |
| 293 | MF=$(head -c 4 plugin_sysmon.macos-x86_64 | od -An -tx1 | tr -d ' \n') |
| 294 | case "$MF" in |
| 295 | cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-x86_64 artifact is Mach-O" ;; |
| 296 | *) fail "macos-x86_64 artifact is not Mach-O ($MF)" ;; |
| 297 | esac |
| 298 | else |
| 299 | echo " skip macos-x86_64 (not built)" |
| 300 | fi |
| 301 | |
| 302 | # ---------------------------------------------------------------- history audit |
| 303 | section "git history secret & LAN audit" |
| 304 | if git rev-parse --git-dir >/dev/null 2>&1; then |
| 305 | ALL=$(mktemp 2>/dev/null || echo /tmp/sysmon_hist_$$) |
| 306 | # Bookkeeping lines of this audit are excluded so the detector cannot flag itself. |
| 307 | git log -p --all 2>/dev/null \ |
| 308 | | grep -v '^Author: ' \ |
| 309 | | grep -vE 'audit "|HITS=|grep -E|scan "' \ |
| 310 | > "$ALL" || : > "$ALL" |
| 311 | |
| 312 | audit() { # audit <label> <regex> |
| 313 | H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true) |
| 314 | if [ -n "$H" ]; then |
| 315 | fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /' |
| 316 | else |
| 317 | pass "history clean: $1" |
| 318 | fi |
| 319 | } |
| 320 | audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+' |
| 321 | audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' |
| 322 | audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' |
| 323 | audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' |
| 324 | audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' |
| 325 | audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}' |
| 326 | # the literal is split so this file never contains the string it hunts |
| 327 | audit "bare host password" 'Xbo''xone0909|xbo''xone' |
| 328 | audit "hard-coded ssh password" 'sshpass -p [^"]$' |
| 329 | PK="pico""bot"; WT=".work""trees" |
| 330 | audit "fleet path" "/codebases/${PK}|${WT}/" |
| 331 | rm -f "$ALL" |
| 332 | else |
| 333 | echo " skip history audit (not a git checkout)" |
| 334 | fi |
| 335 | |
| 336 | # ---------------------------------------------------------------- summary |
| 337 | printf '\n%s== summary ==%s\n' "$C_B" "$C_0" |
| 338 | printf ' passed: %d\n' "$PASS" |
| 339 | printf ' failed: %d\n' "$FAIL" |
| 340 | if [ "$FAIL" -ne 0 ]; then |
| 341 | printf ' %sRESULT: FAILED%s\n' "$C_BAD" "$C_0" |
| 342 | exit 1 |
| 343 | fi |
| 344 | printf ' %sRESULT: ALL PASSED%s\n' "$C_OK" "$C_0" |
| 345 | exit 0 |