| 1 | #!/bin/sh |
| 2 | # tests/run.sh - offline checks for plugin_nvtop across all 3 modes. |
| 3 | # |
| 4 | # Runs against ./plugin_nvtop (native build) on whatever machine it is executed |
| 5 | # on, so the CPU-only assertions use this host's real state. Multi-GPU telemetry |
| 6 | # is exercised deterministically through the NVTOP_MOCK_* fixtures, which is the |
| 7 | # whole point of those hooks: a build box without a GPU must still be able to |
| 8 | # prove the parsers are correct. |
| 9 | # |
| 10 | # If a musl static build, an aarch64 build or a Windows PE are present they are |
| 11 | # checked too. The whole git history is finally audited for secrets and LAN IPs. |
| 12 | # |
| 13 | # Exit code 0 = everything passed. |
| 14 | |
| 15 | set -u |
| 16 | |
| 17 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 18 | cd "$ROOT" || exit 1 |
| 19 | |
| 20 | BIN=./plugin_nvtop |
| 21 | FIX=tests/fixtures |
| 22 | PASS=0 |
| 23 | FAIL=0 |
| 24 | |
| 25 | if [ -t 1 ]; then |
| 26 | C_OK=$(printf '\033[32m'); C_BAD=$(printf '\033[31m'); C_B=$(printf '\033[1m'); C_0=$(printf '\033[0m') |
| 27 | else |
| 28 | C_OK=; C_BAD=; C_B=; C_0= |
| 29 | fi |
| 30 | pass() { PASS=$((PASS + 1)); printf ' %sok%s %s\n' "$C_OK" "$C_0" "$1"; } |
| 31 | fail() { FAIL=$((FAIL + 1)); printf ' %sFAIL%s %s\n' "$C_BAD" "$C_0" "$1"; } |
| 32 | |
| 33 | check() { # check <desc> <expected-substring> <actual> |
| 34 | if printf '%s' "$3" | grep -F -q -- "$2"; then pass "$1"; else |
| 35 | fail "$1 (want: $2)"; printf ' got: %s\n' "$(printf '%s' "$3" | head -c 300)" |
| 36 | fi |
| 37 | } |
| 38 | |
| 39 | check_not() { # check_not <desc> <forbidden-substring> <actual> |
| 40 | if printf '%s' "$3" | grep -F -q -- "$2"; then |
| 41 | fail "$1 (must NOT contain: $2)"; else pass "$1" |
| 42 | fi |
| 43 | } |
| 44 | |
| 45 | section() { printf '\n%s== %s ==%s\n' "$C_B" "$1" "$C_0"; } |
| 46 | |
| 47 | body() { printf '%s' "$1" | sed '1,/^\r$/d'; } |
| 48 | |
| 49 | py() { # py <desc> <python-script> <json-file> [extra args...] |
| 50 | desc=$1; script=$2; file=$3; shift 3 |
| 51 | if python3 -c "$script" "$file" "$@" 2>/dev/null; then pass "$desc"; else fail "$desc"; fi |
| 52 | } |
| 53 | |
| 54 | TMP=$(mktemp -d 2>/dev/null || echo /tmp/nvtop_tests_$$) |
| 55 | mkdir -p "$TMP" 2>/dev/null || true |
| 56 | |
| 57 | # ---------------------------------------------------------------- build |
| 58 | section "build" |
| 59 | if [ ! -x "$BIN" ]; then |
| 60 | echo " building native binary..." |
| 61 | cc -O2 -std=c99 -Wall -Wextra -o "$BIN" src/plugin_nvtop.c || { echo " build failed"; exit 1; } |
| 62 | fi |
| 63 | [ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" |
| 64 | |
| 65 | # ---------------------------------------------------------------- meta mode |
| 66 | section "mode 1 --meta (must work without auth - discovery)" |
| 67 | META=$("$BIN" --meta) |
| 68 | check "meta emits name nvtop" '"name":"nvtop"' "$META" |
| 69 | check "meta emits desc Local GPUs" '"desc":"Local GPUs"' "$META" |
| 70 | METAENV=$(SX_PLUGIN_META=1 "$BIN") |
| 71 | check "SX_PLUGIN_META=1 yields same meta" '"name":"nvtop"' "$METAENV" |
| 72 | LEN=$(printf '%s' "$META" | wc -c | tr -d ' ') |
| 73 | [ "$LEN" -lt 60 ] && pass "meta payload is compact ($LEN bytes)" || fail "meta payload too long ($LEN)" |
| 74 | printf '%s' "$META" | python3 -c 'import json,sys; json.load(sys.stdin)' 2>/dev/null \ |
| 75 | && pass "meta is valid JSON" || fail "meta is not valid JSON" |
| 76 | DESC=$(printf '%s' "$META" | sed 's/.*"desc":"\([^"]*\)".*/\1/') |
| 77 | [ ${#DESC} -le 15 ] && pass "desc length ${#DESC} <= 15 chars" || fail "desc too long (${#DESC})" |
| 78 | NAME=$(printf '%s' "$META" | sed 's/.*"name":"\([^"]*\)".*/\1/') |
| 79 | [ "$(printf '%s' "$NAME" | wc -w | tr -d ' ')" -le 1 ] && pass "name is a single word" || fail "name has spaces" |
| 80 | check "--version prints version" '1.0.3' "$("$BIN" --version)" |
| 81 | check "--help is available" 'usage:' "$("$BIN" --help)" |
| 82 | check "--help documents the mock hook" 'NVTOP_MOCK_CSV' "$("$BIN" --help)" |
| 83 | META_AUTH=$(BOT_PASSWORD=secret "$BIN" --meta) |
| 84 | check "meta still works when BOT_PASSWORD is set" '"name":"nvtop"' "$META_AUTH" |
| 85 | |
| 86 | # ---------------------------------------------------------------- auth |
| 87 | section "node auth" |
| 88 | AUTHNONE=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") |
| 89 | check "no credentials -> 401" 'Status: 401' "$AUTHNONE" |
| 90 | check "401 body is json" 'invalid node password' "$AUTHNONE" |
| 91 | AUTHH=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING='action=metrics' "$BIN") |
| 92 | check "X-Bot-Password accepted" '"ok":true' "$(body "$AUTHH")" |
| 93 | AUTHB=$(BOT_PASSWORD=testpw HTTP_AUTHORIZATION='Bearer testpw' QUERY_STRING='action=metrics' "$BIN") |
| 94 | check "Bearer token accepted" '"ok":true' "$(body "$AUTHB")" |
| 95 | AUTHQ=$(BOT_PASSWORD=testpw QUERY_STRING='action=metrics&pwd=testpw' "$BIN") |
| 96 | check "?pwd= accepted" '"ok":true' "$(body "$AUTHQ")" |
| 97 | AUTHBAD=$(BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=wrong QUERY_STRING='action=metrics' "$BIN") |
| 98 | check "wrong password -> 401" 'Status: 401' "$AUTHBAD" |
| 99 | AUTHPAGE=$(BOT_PASSWORD=testpw QUERY_STRING='embed=1' "$BIN") |
| 100 | check "html mode is protected too" 'Status: 401' "$AUTHPAGE" |
| 101 | |
| 102 | # ---------------------------------------------------------------- json header |
| 103 | section "mode 2 json framing" |
| 104 | J0=$(body "$(QUERY_STRING='action=metrics' "$BIN")") |
| 105 | check "json mode reports an action" '"action":"metrics"' "$J0" |
| 106 | check_not "json mode emits no Content-Length" 'Content-Length' "$J0" |
| 107 | check "json mode is ok" '"ok":true' "$J0" |
| 108 | |
| 109 | # ---------------------------------------------------------------- cpu-only |
| 110 | section "mode 2 CPU-only node (clean empty state)" |
| 111 | CPU="$TMP/cpu.json" |
| 112 | printf '%s' "$J0" > "$CPU" |
| 113 | if printf '%s' "$J0" | grep -F -q '"available":false'; then |
| 114 | pass "empty state reports available:false" |
| 115 | check "empty state counts zero gpus" '"count":0' "$J0" |
| 116 | check "empty state counts zero procs" '"process_count":0' "$J0" |
| 117 | check "empty state explains itself" 'No local NVIDIA/AMD GPU detected' "$J0" |
| 118 | check "empty state source is none" '"source":"none"' "$J0" |
| 119 | check "empty state power is null" '"power_w":null' "$J0" |
| 120 | else |
| 121 | # A machine that really has a GPU (e.g. the .3 inference host) must report it. |
| 122 | pass "this node has a GPU, so available:true (not the empty state)" |
| 123 | python3 - "$CPU" <<'PY' && pass "live GPU rows are well formed" || fail "live GPU rows malformed" |
| 124 | import json, sys |
| 125 | d = json.load(open(sys.argv[1])) |
| 126 | assert d["count"] >= 1 |
| 127 | for g in d["gpus"]: |
| 128 | assert isinstance(g["index"], int) |
| 129 | assert g["mem_total_mb"] > 0 |
| 130 | assert 0 <= g["util_gpu"] <= 100 |
| 131 | PY |
| 132 | fi |
| 133 | py "empty-state payload is valid JSON" 'import json,sys; json.load(open(sys.argv[1]))' "$CPU" |
| 134 | py "empty-state gpus is an empty array" 'import json,sys; d=json.load(open(sys.argv[1])); assert isinstance(d["gpus"], list); assert isinstance(d["processes"], list)' "$CPU" |
| 135 | |
| 136 | # ---------------------------------------------------------------- mock: 4 GPU |
| 137 | section "mode 2 NVTOP_MOCK_CSV (4-GPU fixture, deterministic)" |
| 138 | NVTOP_MOCK_CSV="$FIX/mock-4gpu.csv" QUERY_STRING='action=metrics' "$BIN" > "$TMP/raw-mock.txt" |
| 139 | MOCK="$TMP/mock.json" |
| 140 | body "$(cat "$TMP/raw-mock.txt")" > "$MOCK" |
| 141 | J=$(cat "$MOCK") |
| 142 | check "mock reports available:true" '"available":true' "$J" |
| 143 | check "mock reports 4 gpus" '"count":4' "$J" |
| 144 | check "mock reports 4 processes" '"process_count":4' "$J" |
| 145 | check "mock source is mock" '"source":"mock"' "$J" |
| 146 | check "mock is flagged as mocked" '"mocked":true' "$J" |
| 147 | check "mock carries the driver" '"driver":"550.54.14"' "$J" |
| 148 | check "mock carries the GPU name" 'NVIDIA H100 80GB HBM3' "$J" |
| 149 | check "mock reports power" '"power_w":982.6' "$J" |
| 150 | check "mock reports memory" '"mem_total_mb":327680' "$J" |
| 151 | py "mock payload is valid JSON" 'import json,sys; json.load(open(sys.argv[1]))' "$MOCK" |
| 152 | py "mock has exactly 4 well-formed GPU rows" \ |
| 153 | 'import json,sys; d=json.load(open(sys.argv[1])); g=d["gpus"]; assert len(g)==4; [ (lambda x: (assert_kind(x)))(x) for x in [] ]; |
| 154 | assert [x["index"] for x in g]==[0,1,2,3]; |
| 155 | assert all(isinstance(x["util_gpu"],(int,float)) for x in g); |
| 156 | assert all(x["mem_total_mb"]==81920 for x in g)' "$MOCK" |
| 157 | py "mock per-GPU utilisation parsed" \ |
| 158 | 'import json,sys; d=json.load(open(sys.argv[1])); u=[x["util_gpu"] for x in d["gpus"]]; assert u==[91.0,0.0,55.0,12.0], u' "$MOCK" |
| 159 | py "mock unsupported temp/power become null" \ |
| 160 | 'import json,sys; d=json.load(open(sys.argv[1])); g=d["gpus"][3]; |
| 161 | assert g["temp_c"] is None and g["power_w"] is None and g["power_limit_w"] is None, g' "$MOCK" |
| 162 | py "mock keeps a supported temp as a number" \ |
| 163 | 'import json,sys; d=json.load(open(sys.argv[1])); assert d["gpus"][0]["temp_c"]==74, d["gpus"][0]' "$MOCK" |
| 164 | py "mock averages compute across all GPUs" \ |
| 165 | 'import json,sys; d=json.load(open(sys.argv[1])); assert abs(d["util_gpu_avg"]-39.5)<1e-6, d["util_gpu_avg"]' "$MOCK" |
| 166 | py "mock power sums only the GPUs that report it" \ |
| 167 | 'import json,sys; d=json.load(open(sys.argv[1])); assert abs(d["power_w"]-982.6)<1e-6, d["power_w"]' "$MOCK" |
| 168 | py "mock maps every process back to its GPU index" \ |
| 169 | 'import json,sys; d=json.load(open(sys.argv[1])); idx={p["pid"]:p["gpu_index"] for p in d["processes"]} |
| 170 | assert idx[12001]==0 and idx[12002]==0 and idx[12044]==2 and idx[12077]==2, idx' "$MOCK" |
| 171 | py "mock keeps process VRAM and names" \ |
| 172 | 'import json,sys; d=json.load(open(sys.argv[1])); p=[x for x in d["processes"] if x["pid"]==12001][0] |
| 173 | assert p["name"]=="python3" and p["used_mb"]==68000, p' "$MOCK" |
| 174 | py "mock uuids survive round-trip" \ |
| 175 | 'import json,sys; d=json.load(open(sys.argv[1])); |
| 176 | assert d["gpus"][0]["uuid"]=="GPU-11111111-aaaa-bbbb-cccc-000000000001"' "$MOCK" |
| 177 | py "mock message is empty when a GPU is present" \ |
| 178 | 'import json,sys; d=json.load(open(sys.argv[1])); assert d["message"]=="", d["message"]' "$MOCK" |
| 179 | py "index.json-style keys are all present" \ |
| 180 | 'import json,sys; d=json.load(open(sys.argv[1])); |
| 181 | for k in ["ok","available","driver","count","process_count","gpus","processes","source","utc"]: |
| 182 | assert k in d, k' "$MOCK" |
| 183 | |
| 184 | # ---------------------------------------------------------------- mock: raw nvidia |
| 185 | section "mode 2 raw nvidia-smi csv fixture (real query layout)" |
| 186 | NVTOP_MOCK_NVIDIA="$FIX/nvidia-4gpu.csv" QUERY_STRING='action=metrics' "$BIN" > "$TMP/raw-nv.txt" |
| 187 | NVM="$TMP/nvm.json" |
| 188 | body "$(cat "$TMP/raw-nv.txt")" > "$NVM" |
| 189 | NV=$(cat "$NVM") |
| 190 | check "raw probe reports 4 gpus" '"count":4' "$NV" |
| 191 | check "raw probe reports 4 procs" '"process_count":4' "$NV" |
| 192 | check "raw probe source is nvidia" '"source":"nvidia"' "$NV" |
| 193 | check "raw probe is flagged mocked" '"mocked":true' "$NV" |
| 194 | check "raw probe picks up the driver" '"driver":"550.54.14' "$NV" |
| 195 | py "raw probe parses the same numbers as the fixture" \ |
| 196 | 'import json,sys; d=json.load(open(sys.argv[1])); assert [x["util_gpu"] for x in d["gpus"]]==[91.0,0.0,55.0,12.0]' "$NVM" |
| 197 | py "raw probe tolerates N/A counters" \ |
| 198 | 'import json,sys; d=json.load(open(sys.argv[1])); g=d["gpus"][3]; assert g["temp_c"] is None and g["power_w"] is None' "$NVM" |
| 199 | py "raw probe splits APP lines from GPU lines" \ |
| 200 | 'import json,sys; d=json.load(open(sys.argv[1])); assert d["processes"][0]["pid"]==12001' "$NVM" |
| 201 | |
| 202 | # An empty but successful probe must degrade to the clean empty state, not crash. |
| 203 | NVTOP_MOCK_NVIDIA="$FIX/nvidia-empty.csv" QUERY_STRING='action=metrics' "$BIN" > "$TMP/raw-empty.txt" |
| 204 | E=$(body "$(cat "$TMP/raw-empty.txt")") |
| 205 | check "empty probe falls back to no-GPU state" '"available":false' "$E" |
| 206 | check "empty probe explains itself" 'No local NVIDIA/AMD GPU detected' "$E" |
| 207 | |
| 208 | # ---------------------------------------------------------------- mock: rocm |
| 209 | section "mode 2 rocm-smi text fixture (AMD path)" |
| 210 | NVTOP_MOCK_ROCM="$FIX/rocm-1gpu.txt" QUERY_STRING='action=metrics' "$BIN" > "$TMP/raw-rocm.txt" |
| 211 | RM="$TMP/rocm.json" |
| 212 | body "$(cat "$TMP/raw-rocm.txt")" > "$RM" |
| 213 | R=$(cat "$RM") |
| 214 | check "rocm fixture reports one gpu" '"count":1' "$R" |
| 215 | check "rocm fixture source is amd" '"source":"amd"' "$R" |
| 216 | check "rocm fixture reads the product name" 'AMD Radeon RX 7900 XTX' "$R" |
| 217 | py "rocm fixture converts VRAM bytes to MB" \ |
| 218 | 'import json,sys; d=json.load(open(sys.argv[1])); g=d["gpus"][0] |
| 219 | assert abs(g["mem_total_mb"]-24560)<2, g["mem_total_mb"] |
| 220 | assert abs(g["mem_used_mb"]-2048)<1, g["mem_used_mb"]' "$RM" |
| 221 | py "rocm fixture parses utilisation, temp and power" \ |
| 222 | 'import json,sys; d=json.load(open(sys.argv[1])); g=d["gpus"][0] |
| 223 | assert g["util_gpu"]==44.0 and g["temp_c"]==58 and g["power_w"]==112.0 and g["power_limit_w"]==355.0, g' "$RM" |
| 224 | |
| 225 | # ---------------------------------------------------------------- info |
| 226 | section "mode 2 action=info" |
| 227 | INFO=$(body "$(QUERY_STRING='action=info' "$BIN")") |
| 228 | check "info names the plugin" '"plugin":"plugin_nvtop"' "$INFO" |
| 229 | check "info reports the version" '"version":"1.0.3"' "$INFO" |
| 230 | check "info lists the probes" 'nvidia-smi' "$INFO" |
| 231 | check "info lists the mock hook" 'NVTOP_MOCK_CSV' "$INFO" |
| 232 | check "info advertises zero_network" '"zero_network":true' "$INFO" |
| 233 | check "info reports largest GPU count" '"max_gpus":32' "$INFO" |
| 234 | INFOAUTH=$(BOT_PASSWORD=pw HTTP_X_BOT_PASSWORD=pw QUERY_STRING='action=info' "$BIN") |
| 235 | check "info reflects node auth state" '"node_auth":true' "$INFOAUTH" |
| 236 | |
| 237 | # ---------------------------------------------------------------- unknown/misc |
| 238 | section "mode 2 error handling" |
| 239 | UNK=$(body "$(QUERY_STRING='action=destroy' "$BIN")") |
| 240 | check "unknown action is rejected" '"ok":false' "$UNK" |
| 241 | check "unknown action names itself" '"error":"unknown_action"' "$UNK" |
| 242 | check "unknown action echoes input" '"requested":"destroy"' "$UNK" |
| 243 | check "format=json aliases to metrics" '"action":"metrics"' "$(body "$(QUERY_STRING='format=json' "$BIN")")" |
| 244 | BADAUTH=$(body "$(QUERY_STRING='action=metrics&pwd=x' BOT_PASSWORD=x "$BIN")") |
| 245 | check "matching ?pwd still authorises" '"ok":true' "$BADAUTH" |
| 246 | |
| 247 | # ---------------------------------------------------------------- html |
| 248 | section "mode 3 html dashboard" |
| 249 | HTML=$("$BIN") |
| 250 | check "html content type is set" 'Content-Type: text/html' "$HTML" |
| 251 | check "html title is nvtop" "<title>nvtop</title>" "$HTML" |
| 252 | |
| 253 | # ---- theme handoff (bot_theme_plugin.h) ---- |
| 254 | # the bot owns the theme: ?theme= before first paint, sx-theme after it |
| 255 | check "html reads ?theme= before paint" 'theme=(light|dark)' "$HTML" |
| 256 | check "html listens for sx-theme" "d.type==='sx-theme'" "$HTML" |
| 257 | check "html asks the host for the theme" "sx-theme-request" "$HTML" |
| 258 | check "html uses bot token --bg-card" "--bg-card" "$HTML" |
| 259 | check "html uses bot token --surface-2" "--surface-2" "$HTML" |
| 260 | check "html uses bot token --text-2" "--text-2" "$HTML" |
| 261 | check "html hides its toggle when embedded" "embed-hide" "$HTML" |
| 262 | check_not "html no retired brand casing" 'Scorpio'"X Bot" "$HTML" |
| 263 | check_not "html no private palette 0b0b0e" '0b0b0e' "$HTML" |
| 264 | check_not "html no private palette 141419" '141419' "$HTML" |
| 265 | check_not "html no private palette f2f2f6" 'f2f2f6' "$HTML" |
| 266 | THEMEQ=$(QUERY_STRING='theme=light' "$BIN") |
| 267 | check "?theme=light still renders the UI" 'prefers-color-scheme' "$THEMEQ" |
| 268 | # ---- end theme handoff ---- |
| 269 | |
| 270 | check_not "html emits no Content-Length" 'Content-Length' "$HTML" |
| 271 | check "html has the compute legend" 'compute %' "$HTML" |
| 272 | check "html has the VRAM legend" 'VRAM' "$HTML" |
| 273 | check "html explains the empty state" 'No local NVIDIA/AMD GPU detected' "$HTML" |
| 274 | check "empty state explains remote customization" 'customize' "$HTML" |
| 275 | check "empty state points at the source file" 'src/plugin_nvtop.c' "$HTML" |
| 276 | check "html renders per-GPU cards" "id='gpuGrid'" "$HTML" |
| 277 | check "html renders the process table" "id='procRows'" "$HTML" |
| 278 | check "html wires the refresh button" "id='btnRefresh'" "$HTML" |
| 279 | check "html builds rolling sparklines" 'spark' "$HTML" |
| 280 | check "html polls every 2s" '2000' "$HTML" |
| 281 | check "html routes via the hub proxy" "/plugins?plugin=" "$HTML" |
| 282 | check "html routes directly on the node" '/plugin_nvtop?action=' "$HTML" |
| 283 | check "html carries pwd on fetches" 'X-Bot-Password' "$HTML" |
| 284 | check_not "html has no emoji-ish escapes" '\u26' "$HTML" |
| 285 | HTML_EMBED=$(QUERY_STRING='embed=1' "$BIN") |
| 286 | check "embed mode still renders" "id='gpuGrid'" "$HTML_EMBED" |
| 287 | check_not "embed mode drops the direct-open note" 'Opened directly from the node API' "$HTML_EMBED" |
| 288 | check "non-embed mode keeps the note" 'Opened directly from the node API' "$HTML" |
| 289 | check_not "html has no remote hosts" 'ssh ' "$HTML" |
| 290 | |
| 291 | # ---------------------------------------------------------------- source scan |
| 292 | section "source invariants" |
| 293 | SRCCAT=$(cat src/plugin_nvtop.c) |
| 294 | check "source probes nvidia-smi" 'nvidia-smi --query-gpu' "$SRCCAT" |
| 295 | check "source probes compute apps" '--query-compute-apps' "$SRCCAT" |
| 296 | check "source probes rocm-smi" 'rocm-smi' "$SRCCAT" |
| 297 | check "source redacts stderr per platform" 'DEV_NULL' "$SRCCAT" |
| 298 | check "source refuses Content-Length" 'no Content-Length' "$SRCCAT" |
| 299 | check "source requires node auth" 'require_node_auth' "$SRCCAT" |
| 300 | check "source honours embed=1" 'embed=true' "$SRCCAT" |
| 301 | check "source documents the mock hook" 'NVTOP_MOCK_CSV' "$SRCCAT" |
| 302 | check_not "source has no sockets" 'socket(' "$SRCCAT" |
| 303 | check_not "source has no curl" 'curl ' "$SRCCAT" |
| 304 | check_not "source has no wget" 'wget' "$SRCCAT" |
| 305 | check_not "source has no ssh invocation" 'ssh ' "$SRCCAT" |
| 306 | check_not "source has no sshpass" 'sshpass' "$SRCCAT" |
| 307 | check_not "source has no system()" 'system(' "$SRCCAT" |
| 308 | SNUM=$(grep -cE 'popen\(' src/plugin_nvtop.c) |
| 309 | [ "$SNUM" -ge 1 ] && pass "source uses popen only for fixed probe commands ($SNUM call)" \ |
| 310 | || fail "source does not use popen at all (expected for the probes)" |
| 311 | # Every popen() argument must be a compile-time constant, never a query value. |
| 312 | grep -n 'popen(' src/plugin_nvtop.c | grep -F 'cmd' >/dev/null \ |
| 313 | && pass "popen is called with the const cmd string only" || fail "popen call shape changed - re-audit" |
| 314 | check_not "query string never reaches a shell" 'qs, "r"' "$SRCCAT" |
| 315 | |
| 316 | # ---------------------------------------------------------------- optional targets |
| 317 | section "optional cross builds" |
| 318 | if [ -f plugin_nvtop.musl ]; then |
| 319 | M=$(./plugin_nvtop.musl --meta) |
| 320 | check "musl build --meta" '"name":"nvtop"' "$M" |
| 321 | case $(head -c 4 plugin_nvtop.musl | od -An -tx1 | tr -d ' \n') in |
| 322 | 7f454c46) pass "musl artifact is ELF" ;; |
| 323 | *) fail "musl artifact is not ELF" ;; |
| 324 | esac |
| 325 | else |
| 326 | echo " skip musl (not built)" |
| 327 | fi |
| 328 | if [ -f plugin_nvtop.arm64 ]; then |
| 329 | case $(head -c 4 plugin_nvtop.arm64 | od -An -tx1 | tr -d ' \n') in |
| 330 | 7f454c46) pass "arm64 artifact is ELF" ;; |
| 331 | *) fail "arm64 artifact is not ELF" ;; |
| 332 | esac |
| 333 | EM=$(od -An -tx1 -j 18 -N 2 plugin_nvtop.arm64 | tr -d ' \n') |
| 334 | [ "$EM" = "b700" ] && pass "arm64 artifact declares EM_AARCH64" \ |
| 335 | || fail "arm64 artifact e_machine is $EM, expected b700" |
| 336 | else |
| 337 | echo " skip arm64 (not built)" |
| 338 | fi |
| 339 | if [ -f plugin_nvtop.exe ]; then |
| 340 | MAGIC=$(head -c 2 plugin_nvtop.exe) |
| 341 | [ "$MAGIC" = "MZ" ] && pass "windows artifact has MZ header" || fail "windows artifact missing MZ" |
| 342 | SZ=$(wc -c < plugin_nvtop.exe | tr -d ' ') |
| 343 | [ "$SZ" -gt 20000 ] && pass "windows artifact is not an error page ($SZ bytes)" \ |
| 344 | || fail "windows artifact suspiciously small ($SZ bytes)" |
| 345 | else |
| 346 | echo " skip windows PE (not built)" |
| 347 | fi |
| 348 | for slice in arm64 x86_64; do |
| 349 | f="plugin_nvtop.macos-$slice" |
| 350 | if [ -f "$f" ]; then |
| 351 | MFG=$(head -c 4 "$f" | od -An -tx1 | tr -d ' \n') |
| 352 | case "$MFG" in |
| 353 | cffaedfe|feedfacf|cefaedfe|feedface) pass "macos-$slice artifact is Mach-O" ;; |
| 354 | *) fail "macos-$slice artifact is not Mach-O ($MFG)" ;; |
| 355 | esac |
| 356 | else |
| 357 | echo " skip macos-$slice (not built)" |
| 358 | fi |
| 359 | done |
| 360 | |
| 361 | # ---------------------------------------------------------------- history audit |
| 362 | section "git history secret & LAN audit" |
| 363 | if git rev-parse --git-dir >/dev/null 2>&1; then |
| 364 | ALL="$TMP/history.txt" |
| 365 | # Bookkeeping lines of this audit are excluded so the detector cannot flag itself. |
| 366 | git log -p --all 2>/dev/null \ |
| 367 | | grep -v '^Author: ' \ |
| 368 | | grep -vE 'audit "|HITS=|grep -E|scan "|P_ROOT=|P_WT=' \ |
| 369 | > "$ALL" || : > "$ALL" |
| 370 | |
| 371 | audit() { # audit <label> <regex> |
| 372 | H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true) |
| 373 | if [ -n "$H" ]; then |
| 374 | fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /' |
| 375 | else |
| 376 | pass "history clean: $1" |
| 377 | fi |
| 378 | } |
| 379 | audit "LAN 192.168.x" '192\.168\.[0-9]+\.[0-9]+' |
| 380 | audit "private 10.x" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' |
| 381 | audit "private 172.16-31.x" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' |
| 382 | audit "GitHub PAT" 'ghp_[A-Za-z0-9]{36}' |
| 383 | audit "HuggingFace token" 'hf_[A-Za-z0-9]{30,}' |
| 384 | audit "embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}' |
| 385 | # the literal is split so this file never contains the string it hunts |
| 386 | audit "bare host password" 'Xbo''xone0909|xbo''xone' |
| 387 | PK="pico""bot"; WT=".work""trees" |
| 388 | audit "fleet path" "/codebases/${PK}|${WT}/" |
| 389 | rm -f "$ALL" |
| 390 | else |
| 391 | echo " skip history audit (not a git checkout)" |
| 392 | fi |
| 393 | |
| 394 | rm -rf "$TMP" |
| 395 | |
| 396 | # ---------------------------------------------------------------- summary |
| 397 | printf '\n%s== summary ==%s\n' "$C_B" "$C_0" |
| 398 | printf ' passed: %d\n' "$PASS" |
| 399 | printf ' failed: %d\n' "$FAIL" |
| 400 | if [ "$FAIL" -ne 0 ]; then |
| 401 | printf ' %sRESULT: FAIL%s\n' "$C_BAD" "$C_0" |
| 402 | exit 1 |
| 403 | fi |
| 404 | printf ' %sRESULT: PASS%s\n' "$C_OK" "$C_0" |
| 405 | exit 0 |