| 1 | #!/bin/sh |
| 2 | # tests/run.sh - offline checks for plugin_gitstat across all 3 modes. |
| 3 | # |
| 4 | # Everything runs against ./plugin_gitstat (native build). The repository trees |
| 5 | # are real git repos built in a temp directory, so the status parser, the |
| 6 | # ahead/behind counter, the stash counter, the detached and unborn states and |
| 7 | # the credential redaction are all exercised against git's own output rather |
| 8 | # than our idea of it. GITSTAT_ONLY_ROOTS=1 pins the allowed roots to that temp |
| 9 | # directory so a developer machine full of checkouts cannot affect the result. |
| 10 | # |
| 11 | # Optional cross-target artifacts (musl, aarch64, PE) are checked when present. |
| 12 | # The last section audits the whole git history for secrets and LAN addresses. |
| 13 | # |
| 14 | # Exit code 0 = everything passed. |
| 15 | |
| 16 | set -u |
| 17 | |
| 18 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 19 | cd "$ROOT" || exit 1 |
| 20 | |
| 21 | BIN=./plugin_gitstat |
| 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 500)" |
| 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() { # strip CGI headers if present, drop CRs, keep the payload |
| 48 | printf '%s\n' "$1" | tr -d '\r' | awk ' |
| 49 | BEGIN { inhdr = 0 } |
| 50 | NR == 1 && ($0 ~ /^(Content-Type|Status|Cache-Control|X-Content-Type-Options|Content-Security-Policy):/) { inhdr = 1; next } |
| 51 | inhdr && $0 == "" { inhdr = 0; next } |
| 52 | inhdr { next } |
| 53 | { print } |
| 54 | ' |
| 55 | } |
| 56 | |
| 57 | hdr() { printf '%s\n' "$1" | tr -d '\r' | sed -n '1p'; } |
| 58 | |
| 59 | json_ok() { # json_ok <desc> <json> [python-expression on d] |
| 60 | expr=${3:-True} |
| 61 | if printf '%s' "$2" | python3 -c "import json,sys; d=json.load(sys.stdin); sys.exit(0 if ($expr) else 1)" 2>/dev/null; then |
| 62 | pass "$1" |
| 63 | else |
| 64 | fail "$1 (invalid JSON or assertion failed)"; printf ' got: %s\n' "$(printf '%s' "$2" | head -c 500)" |
| 65 | fi |
| 66 | } |
| 67 | |
| 68 | run() { # run <query-string> [extra env assignments...] -> raw output |
| 69 | qs="$1"; shift |
| 70 | env QUERY_STRING="$qs" "$@" $BIN 2>&1 |
| 71 | } |
| 72 | |
| 73 | api() { body "$(run "$1")"; } |
| 74 | |
| 75 | # ---------------------------------------------------------------- setup |
| 76 | section "build" |
| 77 | if [ ! -x "$BIN" ]; then |
| 78 | echo " building native binary..." |
| 79 | cc -O2 -std=c99 -Wall -Wextra -pedantic -Wno-overlength-strings -I src \ |
| 80 | -o "$BIN" src/plugin_gitstat.c || { echo " build failed"; exit 1; } |
| 81 | fi |
| 82 | [ -x "$BIN" ] && pass "native binary exists and is executable" || fail "native binary missing" |
| 83 | |
| 84 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/gitstat-tests-XXXXXX") || exit 1 |
| 85 | trap 'rm -rf "$WORK"' EXIT |
| 86 | |
| 87 | GIT_AUTHOR_NAME="Gitstat Test"; export GIT_AUTHOR_NAME |
| 88 | GIT_AUTHOR_EMAIL="gitstat@example.invalid"; export GIT_AUTHOR_EMAIL |
| 89 | GIT_COMMITTER_NAME="Gitstat Test"; export GIT_COMMITTER_NAME |
| 90 | GIT_COMMITTER_EMAIL="gitstat@example.invalid"; export GIT_COMMITTER_EMAIL |
| 91 | GIT_CONFIG_GLOBAL=/dev/null; export GIT_CONFIG_GLOBAL |
| 92 | GIT_CONFIG_NOSYSTEM=1; export GIT_CONFIG_NOSYSTEM |
| 93 | GIT_TERMINAL_PROMPT=0; export GIT_TERMINAL_PROMPT |
| 94 | |
| 95 | ROOTS="$WORK/roots" |
| 96 | mkdir -p "$ROOTS" |
| 97 | |
| 98 | mkrepo() { # mkrepo <dir> <branch> |
| 99 | mkdir -p "$1" |
| 100 | git -C "$1" init -q -b "$2" |
| 101 | git -C "$1" config user.name "Gitstat Test" |
| 102 | git -C "$1" config user.email "gitstat@example.invalid" |
| 103 | } |
| 104 | |
| 105 | # A bare upstream with one commit in it, cloned into <dir>. Using a real clone |
| 106 | # rather than init+remote means the upstream refs actually exist, which is what |
| 107 | # the ahead/behind counters read. |
| 108 | mkclone() { # mkclone <dir> <branch> |
| 109 | up="$WORK/up-$(basename "$1").git" |
| 110 | seed="$WORK/seed-$(basename "$1")" |
| 111 | git init -q --bare "$up" |
| 112 | git -C "$up" symbolic-ref HEAD "refs/heads/$2" |
| 113 | mkrepo "$seed" "$2" |
| 114 | printf 'base\n' > "$seed/base.txt" |
| 115 | git -C "$seed" add base.txt |
| 116 | git -C "$seed" commit -q -m "base commit" |
| 117 | git -C "$seed" remote add origin "$up" |
| 118 | git -C "$seed" push -q -u origin "$2" |
| 119 | git clone -q "$up" "$1" |
| 120 | git -C "$1" config user.name "Gitstat Test" |
| 121 | git -C "$1" config user.email "gitstat@example.invalid" |
| 122 | } |
| 123 | |
| 124 | # 1. clean repo with an upstream, fully in sync |
| 125 | mkclone "$ROOTS/cleanrepo" main |
| 126 | |
| 127 | # 2. dirty repo: one modified file, one untracked file, one staged file |
| 128 | mkrepo "$ROOTS/dirtyrepo" main |
| 129 | printf 'one\n' > "$ROOTS/dirtyrepo/keep.txt" |
| 130 | printf 'two\n' > "$ROOTS/dirtyrepo/mod.txt" |
| 131 | printf 'three\n' > "$ROOTS/dirtyrepo/add.txt" |
| 132 | git -C "$ROOTS/dirtyrepo" add keep.txt mod.txt add.txt |
| 133 | git -C "$ROOTS/dirtyrepo" commit -q -m "seed three files" |
| 134 | printf 'modified\n' >> "$ROOTS/dirtyrepo/mod.txt" |
| 135 | printf 'staged\n' >> "$ROOTS/dirtyrepo/add.txt" |
| 136 | git -C "$ROOTS/dirtyrepo" add add.txt |
| 137 | printf 'untracked\n' > "$ROOTS/dirtyrepo/new.txt" |
| 138 | |
| 139 | # 3. ahead repo: two local commits past its upstream, plus a stash |
| 140 | mkclone "$ROOTS/aheadrepo" main |
| 141 | printf 'a\n' >> "$ROOTS/aheadrepo/base.txt" |
| 142 | git -C "$ROOTS/aheadrepo" commit -q -am "local commit one" |
| 143 | printf 'b\n' >> "$ROOTS/aheadrepo/base.txt" |
| 144 | git -C "$ROOTS/aheadrepo" commit -q -am "local commit two" |
| 145 | printf 'stashed\n' >> "$ROOTS/aheadrepo/base.txt" |
| 146 | git -C "$ROOTS/aheadrepo" stash push -q -m "work in progress" |
| 147 | |
| 148 | # 3b. behind repo: its upstream has moved on |
| 149 | mkclone "$ROOTS/behindrepo" main |
| 150 | other="$WORK/other-behindrepo" |
| 151 | git clone -q "$WORK/up-behindrepo.git" "$other" |
| 152 | git -C "$other" config user.name "Gitstat Test" |
| 153 | git -C "$other" config user.email "gitstat@example.invalid" |
| 154 | printf 'upstream\n' >> "$other/base.txt" |
| 155 | git -C "$other" commit -q -am "upstream moved on" |
| 156 | git -C "$other" push -q origin main |
| 157 | git -C "$ROOTS/behindrepo" fetch -q origin |
| 158 | |
| 159 | # 4. detached repo |
| 160 | mkrepo "$ROOTS/detachedrepo" main |
| 161 | printf 'd\n' > "$ROOTS/detachedrepo/d.txt" |
| 162 | git -C "$ROOTS/detachedrepo" add d.txt |
| 163 | git -C "$ROOTS/detachedrepo" commit -q -m "detach me" |
| 164 | printf 'd2\n' >> "$ROOTS/detachedrepo/d.txt" |
| 165 | git -C "$ROOTS/detachedrepo" commit -q -am "second commit" |
| 166 | git -C "$ROOTS/detachedrepo" checkout -q HEAD~1 |
| 167 | |
| 168 | # 5. unborn repo: initialised, nothing committed |
| 169 | mkrepo "$ROOTS/unbornrepo" main |
| 170 | |
| 171 | # 6. repo whose origin URL carries userinfo (must be redacted) |
| 172 | mkclone "$ROOTS/secretremote" main |
| 173 | git -C "$ROOTS/secretremote" remote set-url origin \ |
| 174 | "https://plantuser:planttoken123@example.invalid/x.git" |
| 175 | |
| 176 | # 7. a nested repo two levels deep, to prove the walk descends |
| 177 | mkdir -p "$ROOTS/group/nested" |
| 178 | mkrepo "$ROOTS/group/nested/deeprepo" main |
| 179 | printf 'n\n' > "$ROOTS/group/nested/deeprepo/n.txt" |
| 180 | git -C "$ROOTS/group/nested/deeprepo" add n.txt |
| 181 | git -C "$ROOTS/group/nested/deeprepo" commit -q -m "nested seed" |
| 182 | |
| 183 | # 8. a directory with no .git at all (must be ignored) |
| 184 | mkdir -p "$ROOTS/notarepo" |
| 185 | |
| 186 | ROOTENV="GITSTAT_ONLY_ROOTS=1" |
| 187 | ROOTVAR="GITSTAT_ROOTS=$ROOTS" |
| 188 | |
| 189 | # ---------------------------------------------------------------- --meta |
| 190 | section "--meta / discovery" |
| 191 | META=$($BIN --meta) |
| 192 | check "--meta prints name git" '"name":"git"' "$META" |
| 193 | check "--meta prints desc Repo status" '"desc":"Repo status"' "$META" |
| 194 | json_ok "--meta is valid JSON with exactly 2 keys" "$META" "sorted(d.keys())==['desc','name']" |
| 195 | SXMETA=$(env SX_PLUGIN_META=1 $BIN) |
| 196 | check "SX_PLUGIN_META=1 returns the metadata" '"name":"git"' "$SXMETA" |
| 197 | METAQ=$(env SX_PLUGIN_META=1 QUERY_STRING="action=repos" $BIN) |
| 198 | check "--meta wins over the query string" '"name":"git"' "$METAQ" |
| 199 | DESCLEN=$(printf '%s' "$META" | python3 -c "import json,sys;print(len(json.load(sys.stdin)['desc']))") |
| 200 | [ "$DESCLEN" -le 15 ] && pass "desc is <= 15 chars ($DESCLEN)" || fail "desc too long ($DESCLEN)" |
| 201 | |
| 202 | # ---------------------------------------------------------------- auth |
| 203 | section "node authentication" |
| 204 | UNAUTH=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos" \ |
| 205 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 206 | check "unauthenticated action=repos is 401" "Status: 401 Unauthorized" "$UNAUTH" |
| 207 | check "401 body carries the error" '"error":"invalid node password"' "$UNAUTH" |
| 208 | |
| 209 | OKPW=$(env BOT_PASSWORD=testpw HTTP_X_BOT_PASSWORD=testpw QUERY_STRING="action=repos" \ |
| 210 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 211 | check "X-Bot-Password is accepted" '"ok":true' "$OKPW" |
| 212 | |
| 213 | OKBR=$(env BOT_PASSWORD=testpw HTTP_AUTHORIZATION="Bearer testpw" QUERY_STRING="action=repos" \ |
| 214 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 215 | check "Authorization: Bearer is accepted" '"ok":true' "$OKBR" |
| 216 | |
| 217 | OKQP=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos&pwd=testpw" \ |
| 218 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 219 | check "?pwd= is accepted" '"ok":true' "$OKQP" |
| 220 | |
| 221 | BADPW=$(env BOT_PASSWORD=testpw QUERY_STRING="action=repos&pwd=nope" \ |
| 222 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 223 | check "wrong ?pwd= is 401" "Status: 401 Unauthorized" "$BADPW" |
| 224 | |
| 225 | UNAUTHH=$(env BOT_PASSWORD=testpw QUERY_STRING="" \ |
| 226 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 227 | check "unauthenticated HTML page is 401 too" "Status: 401 Unauthorized" "$UNAUTHH" |
| 228 | |
| 229 | METAOK=$(env BOT_PASSWORD=testpw SX_PLUGIN_META=1 $BIN) |
| 230 | check "--meta stays unauthenticated so discovery works" '"name":"git"' "$METAOK" |
| 231 | |
| 232 | NOAUTH=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 233 | check "no BOT_PASSWORD means auth is a no-op" '"ok":true' "$NOAUTH" |
| 234 | |
| 235 | # ---------------------------------------------------------------- repos |
| 236 | section "?action=repos" |
| 237 | REPOS=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 238 | REPOSB=$(body "$REPOS") |
| 239 | check "repos answers JSON" "Content-Type: application/json" "$REPOS" |
| 240 | json_ok "repos doc is valid" "$REPOSB" |
| 241 | json_ok "repos reports ok" "$REPOSB" "d['ok'] is True" |
| 242 | json_ok "repos has a summary" "$REPOSB" "'summary' in d and 'dirty' in d['summary']" |
| 243 | json_ok "repos has a roots array with per-root counts" "$REPOSB" \ |
| 244 | "len(d['roots'])==1 and d['roots'][0]['count']==8 and d['roots'][0]['exists']" |
| 245 | json_ok "repos found the 8 fixture repos" "$REPOSB" "d['count']==8" |
| 246 | json_ok "the plain directory is not a repo" "$REPOSB" \ |
| 247 | "not any('notarepo' in r['path'] for r in d['repos'])" |
| 248 | json_ok "the nested repo was found at depth 3" "$REPOSB" \ |
| 249 | "any(r['name']=='deeprepo' for r in d['repos'])" |
| 250 | json_ok "the first repo is a dirty one (dirty-first sort)" "$REPOSB" "d['repos'][0]['dirty']>0" |
| 251 | json_ok "every repo has the documented field set" "$REPOSB" \ |
| 252 | "all(set(r)>=set(['path','name','root','branch','upstream','ahead','behind','dirty','untracked','staged','conflicted','stash','detached','no_upstream','unborn','last_commit']) for r in d['repos'])" |
| 253 | json_ok "last_commit has hash/ct/at/subject" "$REPOSB" \ |
| 254 | "all(set(r['last_commit'])>=set(['hash','short','ct','at','subject']) for r in d['repos'])" |
| 255 | json_ok "checked_at is an ISO-Z stamp" "$REPOSB" "d['checked_at'].endswith('Z')" |
| 256 | |
| 257 | # per-repo assertions |
| 258 | DIRTY=$(printf '%s' "$REPOSB" | python3 -c " |
| 259 | import json,sys |
| 260 | d=json.load(sys.stdin) |
| 261 | print(json.dumps([r for r in d['repos'] if r['name']=='dirtyrepo'][0]))") |
| 262 | json_ok "dirtyrepo counts 3 changed entries" "$DIRTY" "d['dirty']==3" |
| 263 | json_ok "dirtyrepo counts 1 untracked" "$DIRTY" "d['untracked']==1" |
| 264 | json_ok "dirtyrepo is not detached and has a branch" "$DIRTY" \ |
| 265 | "d['detached'] is False and d['branch']=='main'" |
| 266 | |
| 267 | CLEAN=$(printf '%s' "$REPOSB" | python3 -c " |
| 268 | import json,sys |
| 269 | d=json.load(sys.stdin) |
| 270 | print(json.dumps([r for r in d['repos'] if r['name']=='cleanrepo'][0]))") |
| 271 | json_ok "cleanrepo is clean" "$CLEAN" "d['dirty']==0" |
| 272 | json_ok "cleanrepo is in sync with its upstream" "$CLEAN" "d['ahead']==0 and d['behind']==0" |
| 273 | json_ok "cleanrepo has an upstream" "$CLEAN" "d['no_upstream'] is False and d['upstream']" |
| 274 | json_ok "cleanrepo carried its last commit" "$CLEAN" "d['last_commit']['subject']=='base commit'" |
| 275 | |
| 276 | AHEAD=$(printf '%s' "$REPOSB" | python3 -c " |
| 277 | import json,sys |
| 278 | d=json.load(sys.stdin) |
| 279 | print(json.dumps([r for r in d['repos'] if r['name']=='aheadrepo'][0]))") |
| 280 | json_ok "aheadrepo is 2 ahead" "$AHEAD" "d['ahead']==2" |
| 281 | json_ok "aheadrepo is not behind" "$AHEAD" "d['behind']==0" |
| 282 | json_ok "aheadrepo counted its stash" "$AHEAD" "d['stash']==1" |
| 283 | json_ok "aheadrepo last subject is the newest commit" "$AHEAD" \ |
| 284 | "d['last_commit']['subject']=='local commit two'" |
| 285 | |
| 286 | DET=$(printf '%s' "$REPOSB" | python3 -c " |
| 287 | import json,sys |
| 288 | d=json.load(sys.stdin) |
| 289 | print(json.dumps([r for r in d['repos'] if r['name']=='detachedrepo'][0]))") |
| 290 | json_ok "detachedrepo is flagged detached" "$DET" "d['detached'] is True" |
| 291 | json_ok "detachedrepo branch reads detached" "$DET" "d['branch']=='detached'" |
| 292 | |
| 293 | UNB=$(printf '%s' "$REPOSB" | python3 -c " |
| 294 | import json,sys |
| 295 | d=json.load(sys.stdin) |
| 296 | print(json.dumps([r for r in d['repos'] if r['name']=='unbornrepo'][0]))") |
| 297 | json_ok "unbornrepo is flagged unborn" "$UNB" "d['unborn'] is True" |
| 298 | json_ok "unbornrepo has no upstream" "$UNB" "d['no_upstream'] is True" |
| 299 | |
| 300 | SEC=$(printf '%s' "$REPOSB" | python3 -c " |
| 301 | import json,sys |
| 302 | d=json.load(sys.stdin) |
| 303 | print(json.dumps([r for r in d['repos'] if r['name']=='secretremote'][0]))") |
| 304 | check_not "credential in the origin URL is redacted (token)" "planttoken123" "$SEC" |
| 305 | check_not "credential in the origin URL is redacted (user)" "plantuser" "$SEC" |
| 306 | SECUREMOTE=$(body "$(env QUERY_STRING="action=repo&path=$ROOTS/secretremote" \ |
| 307 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)") |
| 308 | check "the board row does not pay for the origin URL" '"remote": ""' "$SEC" |
| 309 | json_ok "the detail reads the origin URL" "$SECUREMOTE" \ |
| 310 | "d['repo']['remote']=='https://example.invalid/x.git'" |
| 311 | check_not "the detail remote carries no userinfo" "plantuser" "$SECUREMOTE" |
| 312 | check_not "the detail remote carries no token" "planttoken123" "$SECUREMOTE" |
| 313 | |
| 314 | check_not "no path outside the root is reported" "$WORK/upstream.git" "$REPOSB" |
| 315 | |
| 316 | # summary counters |
| 317 | json_ok "summary counts the dirty repos" "$REPOSB" "d['summary']['dirty']>=1" |
| 318 | json_ok "summary counts the ahead repo" "$REPOSB" "d['summary']['ahead']>=1" |
| 319 | json_ok "summary counts the behind repo" "$REPOSB" "d['summary']['behind']>=1" |
| 320 | BEHIND=$(printf '%s' "$REPOSB" | python3 -c " |
| 321 | import json,sys |
| 322 | d=json.load(sys.stdin) |
| 323 | print(json.dumps([r for r in d['repos'] if r['name']=='behindrepo'][0]))") |
| 324 | json_ok "behindrepo is 1 behind" "$BEHIND" "d['behind']==1" |
| 325 | json_ok "behindrepo is not ahead" "$BEHIND" "d['ahead']==0" |
| 326 | json_ok "behindrepo still has an upstream" "$BEHIND" "d['no_upstream'] is False" |
| 327 | json_ok "summary counts the stashes" "$REPOSB" "d['summary']['stashes']>=1" |
| 328 | json_ok "summary counts the detached repo" "$REPOSB" "d['summary']['detached']==1" |
| 329 | |
| 330 | # empty root |
| 331 | EMPTY=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$WORK/nowhere" $BIN) |
| 332 | json_ok "a missing root reports exists:false" "$(body "$EMPTY")" \ |
| 333 | "d['roots'][0]['exists'] is False and d['count']==0" |
| 334 | |
| 335 | # per-root budget: two roots are counted independently |
| 336 | mkdir -p "$WORK/r1" "$WORK/r2" |
| 337 | cp -a "$ROOTS/cleanrepo" "$WORK/r1/one" |
| 338 | cp -a "$ROOTS/cleanrepo" "$WORK/r1/two" |
| 339 | cp -a "$ROOTS/cleanrepo" "$WORK/r2/three" |
| 340 | TWOROOT=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 \ |
| 341 | GITSTAT_ROOTS="$WORK/r1:$WORK/r2" $BIN) |
| 342 | json_ok "two roots keep independent counts" "$(body "$TWOROOT")" \ |
| 343 | "len(d['roots'])==2 and d['roots'][0]['count']==2 and d['roots'][1]['count']==1" |
| 344 | json_ok "the global table is that of both roots" "$(body "$TWOROOT")" "d['count']==3" |
| 345 | |
| 346 | # a duplicate root listing is de-duplicated, not double counted |
| 347 | DUPR=$(env QUERY_STRING="action=repos" GITSTAT_ONLY_ROOTS=1 \ |
| 348 | GITSTAT_ROOTS="$WORK/r1:$WORK/r1" $BIN) |
| 349 | json_ok "a repeated root is de-duplicated" "$(body "$DUPR")" \ |
| 350 | "len(d['roots'])==1 and d['count']==2" |
| 351 | |
| 352 | # ---------------------------------------------------------------- repo detail |
| 353 | section "?action=repo&path=" |
| 354 | RP="env QUERY_STRING=action=repo\&path=$ROOTS/dirtyrepo GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS=$ROOTS" |
| 355 | DETAIL=$(env QUERY_STRING="action=repo&path=$ROOTS/dirtyrepo" \ |
| 356 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 357 | DETB=$(body "$DETAIL") |
| 358 | check "repo detail answers JSON" "Content-Type: application/json" "$DETAIL" |
| 359 | json_ok "repo detail is valid" "$DETB" |
| 360 | json_ok "repo detail reports ok" "$DETB" "d['ok'] is True" |
| 361 | json_ok "repo detail has the repo block" "$DETB" "d['repo']['name']=='dirtyrepo'" |
| 362 | json_ok "repo detail lists 3 changed files" "$DETB" "len(d['changed'])==3" |
| 363 | json_ok "repo detail marks the untracked file" "$DETB" \ |
| 364 | "any(c['code']=='??' and c['path']=='new.txt' for c in d['changed'])" |
| 365 | json_ok "repo detail marks the modified file" "$DETB" \ |
| 366 | "any(c['path']=='mod.txt' and c['code'][1]=='M' for c in d['changed'])" |
| 367 | json_ok "repo detail marks the staged file" "$DETB" \ |
| 368 | "any(c['path']=='add.txt' and c['code'][0]=='M' for c in d['changed'])" |
| 369 | json_ok "repo detail has recent commits" "$DETB" "len(d['commits'])>=1" |
| 370 | json_ok "each commit has hash/ct/author/subject" "$DETB" \ |
| 371 | "all(set(c)>=set(['hash','ct','author','subject']) for c in d['commits'])" |
| 372 | json_ok "changed_count is reported" "$DETB" "d['changed_count']==3" |
| 373 | json_ok "a discovered repo keeps its root attribution" "$DETB" "d['repo']['root']!=''" |
| 374 | |
| 375 | # a repo the walk deliberately skipped (it lives inside a node_modules tree) is |
| 376 | # still served when asked for by path, and is attributed to its root |
| 377 | mkdir -p "$ROOTS/node_modules" |
| 378 | cp -a "$ROOTS/cleanrepo" "$ROOTS/node_modules/skipped" |
| 379 | UNSEEN=$(body "$(env QUERY_STRING="action=repo&path=$ROOTS/node_modules/skipped" \ |
| 380 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN)") |
| 381 | json_ok "the walk skips node_modules" "$REPOSB" \ |
| 382 | "not any('node_modules' in r['path'] for r in d['repos'])" |
| 383 | json_ok "an undiscovered repo is read on demand" "$UNSEEN" "d['ok'] is True" |
| 384 | json_ok "a skipped repo is attributed to its root" "$UNSEEN" \ |
| 385 | "d['repo']['root']=='$ROOTS'" |
| 386 | |
| 387 | AHEADD=$(env QUERY_STRING="action=repo&path=$ROOTS/aheadrepo" \ |
| 388 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 389 | json_ok "detail of a repo with commits lists newest first" "$(body "$AHEADD")" \ |
| 390 | "d['commits'][0]['subject']=='local commit two'" |
| 391 | json_ok "detail caps commits at 20" "$(body "$AHEADD")" "len(d['commits'])<=20" |
| 392 | |
| 393 | # path allowlisting |
| 394 | OUTSIDE=$(env QUERY_STRING="action=repo&path=/etc" \ |
| 395 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 396 | check "a path outside every root is 403" "Status: 403 Forbidden" "$OUTSIDE" |
| 397 | check "403 explains why" "not in an allowlisted root" "$OUTSIDE" |
| 398 | |
| 399 | DOTDOT=$(env QUERY_STRING="action=repo&path=$ROOTS/../etc" \ |
| 400 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 401 | check "a .. traversal is refused" "Status: 403 Forbidden" "$DOTDOT" |
| 402 | |
| 403 | NOTREPO=$(env QUERY_STRING="action=repo&path=$ROOTS/notarepo" \ |
| 404 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 405 | check "a directory without .git is refused" "Status: 403 Forbidden" "$NOTREPO" |
| 406 | |
| 407 | EMPTYP=$(env QUERY_STRING="action=repo&path=" \ |
| 408 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 409 | check "a missing path argument is refused" "Status: 403 Forbidden" "$EMPTYP" |
| 410 | |
| 411 | # ---------------------------------------------------------------- fetch |
| 412 | section "?action=fetch" |
| 413 | NOFETCH=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" REQUEST_METHOD=POST \ |
| 414 | GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 415 | check "fetch is disabled by default" '"ok":false' "$NOFETCH" |
| 416 | check "fetch says how to enable it" "GITSTAT_ALLOW_FETCH" "$NOFETCH" |
| 417 | |
| 418 | GETFETCH=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" \ |
| 419 | GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 420 | check "fetch is POST only" "fetch is POST only" "$GETFETCH" |
| 421 | |
| 422 | FETCHOUT=$(env QUERY_STRING="action=fetch&path=$ROOTS/cleanrepo" REQUEST_METHOD=POST \ |
| 423 | GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 424 | FB=$(body "$FETCHOUT") |
| 425 | json_ok "enabled fetch returns a JSON document" "$FB" |
| 426 | json_ok "enabled fetch of a clean repo succeeds" "$FB" "d['ok'] is True and d['exit']==0" |
| 427 | json_ok "fetch echoes the branch and sync state" "$FB" "'branch' in d and 'ahead' in d" |
| 428 | |
| 429 | FETCHBAD=$(env QUERY_STRING="action=fetch&path=/etc" REQUEST_METHOD=POST \ |
| 430 | GITSTAT_ALLOW_FETCH=1 GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 431 | check "fetch of a path outside the roots is 403" "Status: 403 Forbidden" "$FETCHBAD" |
| 432 | |
| 433 | # ---------------------------------------------------------------- other actions |
| 434 | UNKNOWN=$(env QUERY_STRING="action=bogus" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 435 | check "an unknown action answers an error document" '"error":"unknown action"' "$UNKNOWN" |
| 436 | |
| 437 | FMT=$(env QUERY_STRING="format=json" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 438 | check "format=json is served as JSON" "Content-Type: application/json" "$FMT" |
| 439 | json_ok "format=json returns the repos document" "$(body "$FMT")" "d['action']=='repos'" |
| 440 | |
| 441 | # ---------------------------------------------------------------- HTML |
| 442 | section "HTML UI" |
| 443 | HTML=$(env QUERY_STRING="" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 444 | check "GET answers HTML" "Content-Type: text/html" "$HTML" |
| 445 | check "the page has a doctype" "<!DOCTYPE html>" "$HTML" |
| 446 | check "the page names the product" "SCORPIOX BOT" "$HTML" |
| 447 | check "the page sets the token block" "--bg:#000000" "$HTML" |
| 448 | check "the page carries the light token block" "data-theme=light" "$HTML" |
| 449 | check "the page carries the prepaint script" "sx-theme-request" "$HTML" |
| 450 | check "the page carries the handoff listener" "sx-theme" "$HTML" |
| 451 | check_not "no external script is referenced" "<script src=" "$HTML" |
| 452 | check_not "no CDN is referenced" "cdn." "$HTML" |
| 453 | check_not "no http URL is referenced in the page" "http://" "$HTML" |
| 454 | check "the page has the rows table" 'id="rows"' "$HTML" |
| 455 | check "the page has the filter chips" 'class="chip' "$HTML" |
| 456 | check "the page has the detail drawer" 'id="drawer"' "$HTML" |
| 457 | check "the page has inline SVG icons" "<svg viewBox" "$HTML" |
| 458 | check "the page has a CSP" "Content-Security-Policy" "$HTML" |
| 459 | check "the page declares the theme toggle hidden in embed" "embed-hide" "$HTML" |
| 460 | |
| 461 | EMBED=$(env QUERY_STRING="embed=1" GITSTAT_ONLY_ROOTS=1 GITSTAT_ROOTS="$ROOTS" $BIN) |
| 462 | check "embed=1 still serves the page" "<!DOCTYPE html>" "$EMBED" |
| 463 | check "embed=1 marks the document embedded" "embed" "$EMBED" |
| 464 | |
| 465 | # no emoji anywhere in the page |
| 466 | if printf '%s' "$HTML" | python3 -c " |
| 467 | import sys |
| 468 | s=sys.stdin.buffer.read().decode('utf-8','replace') |
| 469 | bad=[c for c in s if ord(c)>0x2100 and not (0x2010<=ord(c)<=0x2027)] |
| 470 | sys.exit(1 if bad else 0)" 2>/dev/null; then |
| 471 | pass "no emoji in the page" |
| 472 | else |
| 473 | fail "the page contains emoji or exotic glyphs" |
| 474 | fi |
| 475 | |
| 476 | # the hub-vs-direct route branch must be present, got wrong is the classic bug |
| 477 | check "the page branches on ?plugin= for the hub route" \ |
| 478 | "HUB ? '/plugins?plugin='" "$HTML" |
| 479 | check "the page falls back to the direct node route" "'/plugin_gitstat'" "$HTML" |
| 480 | |
| 481 | # ---------------------------------------------------------------- cross targets |
| 482 | section "cross-target artifacts" |
| 483 | if [ -f plugin_gitstat.musl ]; then |
| 484 | file plugin_gitstat.musl | grep -q 'ELF' && pass "the musl build is an ELF" || fail "musl build is not ELF" |
| 485 | file plugin_gitstat.musl | grep -qi 'static' && pass "the musl build is statically linked" || fail "musl build is dynamic" |
| 486 | else |
| 487 | echo " (skip) plugin_gitstat.musl not built here" |
| 488 | fi |
| 489 | if [ -f plugin_gitstat.arm64 ]; then |
| 490 | file plugin_gitstat.arm64 | grep -Eq 'aarch64' && pass "the arm64 build is aarch64" || fail "arm64 build is not aarch64" |
| 491 | else |
| 492 | echo " (skip) plugin_gitstat.arm64 not built here" |
| 493 | fi |
| 494 | if [ -f plugin_gitstat.exe ]; then |
| 495 | if head -c 2 plugin_gitstat.exe | od -An -c | grep -q 'M Z'; then |
| 496 | pass "the Windows build starts with an MZ header" |
| 497 | else |
| 498 | fail "the Windows build is not a PE (no MZ header)" |
| 499 | fi |
| 500 | else |
| 501 | echo " (skip) plugin_gitstat.exe not built here" |
| 502 | fi |
| 503 | |
| 504 | # ---------------------------------------------------------------- generated UI |
| 505 | section "generated page" |
| 506 | if command -v python3 >/dev/null 2>&1; then |
| 507 | if python3 tools/gen_ui.py --check >/dev/null 2>&1; then |
| 508 | pass "src/ui_html.h matches src/ui.html" |
| 509 | else |
| 510 | fail "src/ui_html.h is stale - run tools/gen_ui.py" |
| 511 | fi |
| 512 | else |
| 513 | echo " (skip) python3 not available" |
| 514 | fi |
| 515 | |
| 516 | # ---------------------------------------------------------------- history audit |
| 517 | section "git history secret audit" |
| 518 | if git rev-parse --git-dir >/dev/null 2>&1; then |
| 519 | ALL="$WORK/history.txt" |
| 520 | git log -p --all 2>/dev/null \ |
| 521 | | grep -v '^Author: ' \ |
| 522 | | grep -vE 'scan_hist|HITS=|grep -Eo|suspects=|audit "|check_not "|scan "|audit\(\)' \ |
| 523 | > "$ALL" || : > "$ALL" |
| 524 | |
| 525 | audit() { # audit <label> <regex> |
| 526 | H=$(grep -Eo "$2" "$ALL" 2>/dev/null | sort -u | head -3 || true) |
| 527 | if [ -n "$H" ]; then |
| 528 | fail "history: $1"; printf '%s\n' "$H" | sed 's/^/ /' |
| 529 | else |
| 530 | pass "history: $1" |
| 531 | fi |
| 532 | } |
| 533 | audit "no LAN addresses" '192\.168\.[0-9]+\.[0-9]+' |
| 534 | audit "no private 10.x range" '\b10\.[0-9]+\.[0-9]+\.[0-9]+' |
| 535 | audit "no 172.16-31 range" '172\.(1[6-9]|2[0-9]|3[01])\.[0-9]+\.[0-9]+' |
| 536 | audit "no GitHub PAT" 'ghp_[A-Za-z0-9]{36}' |
| 537 | audit "no HF token" 'hf_[A-Za-z0-9]{30,}' |
| 538 | audit "no embedded git creds" 'scorpioxinc:[A-Za-z0-9]{20,}' |
| 539 | if command -v scorpiox-cli-botplugin >/dev/null 2>&1; then |
| 540 | if scorpiox-cli-botplugin audit . >"$WORK/audit.out" 2>&1; then |
| 541 | pass "scorpiox-cli-botplugin audit passes" |
| 542 | else |
| 543 | fail "scorpiox-cli-botplugin audit failed"; sed 's/^/ /' "$WORK/audit.out" | head -6 |
| 544 | fi |
| 545 | else |
| 546 | echo " (skip) scorpiox-cli-botplugin not installed" |
| 547 | fi |
| 548 | else |
| 549 | echo " (skip) not a git checkout" |
| 550 | fi |
| 551 | |
| 552 | # ---------------------------------------------------------------- summary |
| 553 | printf '\n%s=================================%s\n' "$C_B" "$C_0" |
| 554 | printf ' passed %s%d%s failed %s%d%s\n' "$C_OK" "$PASS" "$C_0" \ |
| 555 | "$([ "$FAIL" -eq 0 ] && echo "$C_OK" || echo "$C_BAD")" "$FAIL" "$C_0" |
| 556 | printf '%s=================================%s\n' "$C_B" "$C_0" |
| 557 | |
| 558 | [ "$FAIL" -eq 0 ] || exit 1 |
| 559 | exit 0 |