| 1 | #!/bin/sh |
| 2 | # tests/run.sh - the offline proof for plugin_github. |
| 3 | # |
| 4 | # Everything runs against ./plugin_github with the connector state directory |
| 5 | # pointed at a scratch dir and the API base pointed at tests/mockapi.py on |
| 6 | # loopback, so the suite never touches GitHub, never needs a real credential, |
| 7 | # and never depends on the internet. |
| 8 | # |
| 9 | # Sections: |
| 10 | # --meta the node's chip contract (1-2 word name, <= 15 char desc, |
| 11 | # kind mcp / mcp true / daemon false / menus false) and |
| 12 | # SX_PLUGIN_META=1 agreeing, version matching plugin.json |
| 13 | # --schema the tool array parses and carries the five spec tools |
| 14 | # mcp.py initialize/ping/tools/list + all five tools over stdio, each |
| 15 | # request compared against what the mock actually RECEIVED |
| 16 | # (method, path, query, Bearer header, JSON body), the PR filter, |
| 17 | # error relays, the single 503/Retry-After retry |
| 18 | # cgi.py status/save/test/tools/clear as real CGI: the 401 gate, the |
| 19 | # 0600 config.json, save validation, and that NO response ever |
| 20 | # contains the token text |
| 21 | # asan the whole mcp.py + cgi.py pass again against the ASan/UBSan + |
| 22 | # _FORTIFY_SOURCE=2 binary (if this host can run ASan) |
| 23 | # source no popen/system/exec, no credential literal, no LAN address, |
| 24 | # and the page loads no external script |
| 25 | # |
| 26 | # `scorpiox-cli-botplugin audit` runs as a separate gate after the first |
| 27 | # commit (it walks git history) - see the README / release checklist. |
| 28 | # |
| 29 | # Exit code 0 = everything passed. |
| 30 | |
| 31 | set -u |
| 32 | |
| 33 | ROOT=$(cd "$(dirname "$0")/.." && pwd) |
| 34 | cd "$ROOT" || exit 1 |
| 35 | |
| 36 | BIN=${BIN:-./plugin_github} |
| 37 | WORK=$(mktemp -d "${TMPDIR:-/tmp}/gh-suite.XXXXXX") |
| 38 | PORTFILE="$WORK/port" |
| 39 | LOG="$WORK/req.jsonl" |
| 40 | CFGDIR="$WORK/cfg" |
| 41 | CGIDIR="$WORK/cgi" |
| 42 | MOCK_PID="" |
| 43 | |
| 44 | cleanup() { |
| 45 | [ -n "$MOCK_PID" ] && kill "$MOCK_PID" 2>/dev/null |
| 46 | [ "${KEEP_WORK:-0}" = "1" ] || rm -rf "$WORK" |
| 47 | [ "${KEEP_WORK:-0}" = "1" ] && echo "work dir kept: $WORK" |
| 48 | } |
| 49 | trap cleanup EXIT INT TERM |
| 50 | |
| 51 | PASS=0 |
| 52 | FAIL=0 |
| 53 | |
| 54 | section() { printf '\n== %s ==\n' "$1"; } |
| 55 | pass() { PASS=$((PASS + 1)); printf ' ok %s\n' "$1"; } |
| 56 | fail() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; } |
| 57 | |
| 58 | check() { # check <label> <expected-substring> <text|file> |
| 59 | if [ -f "$3" ]; then set -- "$1" "$2" "$(cat "$3")"; fi |
| 60 | if printf '%s' "${3:-}" | grep -F -q -- "$2"; then pass "$1"; else |
| 61 | fail "$1 (want: $2)" |
| 62 | printf ' got: %s\n' "$(printf '%s' "${3:-}" | head -c 300)" |
| 63 | fi |
| 64 | } |
| 65 | check_not() { # check_not <label> <forbidden-substring> <text|file> |
| 66 | if [ -f "$3" ]; then set -- "$1" "$2" "$(cat "$3")"; fi |
| 67 | if printf '%s' "${3:-}" | grep -F -q -- "$2"; then |
| 68 | fail "$1 (must NOT contain: $2)" |
| 69 | printf ' got: %s\n' "$(printf '%s' "${3:-}" | head -c 300)" |
| 70 | else pass "$1"; fi |
| 71 | } |
| 72 | json_ok() { # json_ok <label> <json> [python expr over `d`] |
| 73 | expr=${3:-True} |
| 74 | printf '%s' "${2:-}" > "$WORK/j.json" |
| 75 | if python3 -c "import json,sys; d=json.load(open(sys.argv[1])); sys.exit(0 if ($expr) else 1)" \ |
| 76 | "$WORK/j.json" 2>/dev/null; then |
| 77 | pass "$1" |
| 78 | else |
| 79 | fail "$1 (invalid JSON or failed assertion)" |
| 80 | printf ' got: %s\n' "$(printf '%s' "${2:-}" | head -c 300)" |
| 81 | fi |
| 82 | } |
| 83 | |
| 84 | mkdir -p "$CFGDIR" "$CGIDIR" |
| 85 | rm -f "$LOG" |
| 86 | |
| 87 | if [ ! -x "$BIN" ]; then |
| 88 | echo "plugin_github is not built - run make first" >&2 |
| 89 | exit 1 |
| 90 | fi |
| 91 | |
| 92 | # ------------------------------------------------------------------ mock |
| 93 | python3 tests/mockapi.py --portfile "$PORTFILE" --log "$LOG" 2>"$WORK/mock.err" & |
| 94 | MOCK_PID=$! |
| 95 | i=0 |
| 96 | while [ ! -s "$PORTFILE" ] && [ $i -lt 100 ]; do |
| 97 | i=$((i + 1)); sleep 0.05 |
| 98 | done |
| 99 | if [ ! -s "$PORTFILE" ]; then |
| 100 | echo "the mock server did not start: $(cat "$WORK/mock.err")" >&2 |
| 101 | exit 1 |
| 102 | fi |
| 103 | MOCK="http://127.0.0.1:$(cat "$PORTFILE")" |
| 104 | |
| 105 | # ---------------------------------------------------------------- --meta |
| 106 | section "--meta / discovery" |
| 107 | META=$("$BIN" --meta) |
| 108 | json_ok "--meta is one line of valid JSON" "$META" |
| 109 | json_ok "name is the short chip label github" "$META" 'd["name"]=="github"' |
| 110 | json_ok "desc is <= 15 chars" "$META" 'len(d["desc"])<=15' |
| 111 | json_ok "desc is GitHub tools" "$META" 'd["desc"]=="GitHub tools"' |
| 112 | json_ok "kind is mcp, mcp true, menus false" \ |
| 113 | "$META" 'd["kind"]=="mcp" and d["mcp"] is True and d["menus"] is False' |
| 114 | json_ok "daemon is false (this connector has no always-on bridge)" \ |
| 115 | "$META" 'd["daemon"] is False' |
| 116 | json_ok "verified is mock (an honest tier: the suite drives a mock, not a live account)" \ |
| 117 | "$META" 'd["verified"]=="mock"' |
| 118 | check "SX_PLUGIN_META=1 agrees" '"name":"github"' "$(SX_PLUGIN_META=1 "$BIN")" |
| 119 | json_ok "--meta version agrees with plugin.json" "$META" \ |
| 120 | 'd["version"]==__import__("json").load(open("plugin.json"))["version"]' |
| 121 | |
| 122 | # ---------------------------------------------------------------- --schema |
| 123 | section "--schema" |
| 124 | SCHEMA=$("$BIN" --schema) |
| 125 | json_ok "--schema is valid JSON" "$SCHEMA" |
| 126 | json_ok "--schema carries exactly the five spec tools" "$SCHEMA" \ |
| 127 | 'sorted(t["name"] for t in d["tools"])==sorted(["github_get_repo","github_list_issues","github_create_issue","github_get_pull_request","github_search_code"])' |
| 128 | json_ok "every tool has a description and an inputSchema" "$SCHEMA" \ |
| 129 | 'all(t.get("description") and t.get("inputSchema") for t in d["tools"])' |
| 130 | json_ok "create_issue requires owner,repo,title" "$SCHEMA" \ |
| 131 | 'set(next(t for t in d["tools"] if t["name"]=="github_create_issue")["inputSchema"]["required"])=={"owner","repo","title"}' |
| 132 | json_ok "search_code requires query" "$SCHEMA" \ |
| 133 | 'next(t for t in d["tools"] if t["name"]=="github_search_code")["inputSchema"]["required"]==["query"]' |
| 134 | |
| 135 | # ------------------------------------------------------------ mcp (stdio) |
| 136 | section "--mcp + request shapes (tests/mcp.py)" |
| 137 | if BIN="$BIN" PORT="$(cat "$PORTFILE")" LOG="$LOG" CFGDIR="$CFGDIR" \ |
| 138 | python3 tests/mcp.py; then |
| 139 | pass "tests/mcp.py exited 0" |
| 140 | else |
| 141 | fail "tests/mcp.py reported failures" |
| 142 | fi |
| 143 | |
| 144 | # ----------------------------------------------------------------- cgi |
| 145 | section "CGI control API (tests/cgi.py)" |
| 146 | if BIN="$BIN" PORT="$(cat "$PORTFILE")" CFGDIR="$CGIDIR" \ |
| 147 | python3 tests/cgi.py; then |
| 148 | pass "tests/cgi.py exited 0" |
| 149 | else |
| 150 | fail "tests/cgi.py reported failures" |
| 151 | fi |
| 152 | |
| 153 | # ------------------------------------------------------- config from disk |
| 154 | section "config resolution" |
| 155 | # an env token beats the stored one; a stored base beats the default. |
| 156 | # NOTE: the config must be written to the SAME dir the binary is pointed at - |
| 157 | # a mismatch here is a test bug that reads as a plugin bug (it did once). |
| 158 | STOREDIR="$WORK/stored" |
| 159 | mkdir -p "$STOREDIR" |
| 160 | printf "{\"api_base\":\"%s\",\"token\":\"STORED-TOKEN-AAA\"}\n" "$MOCK" > "$STOREDIR/config.json" |
| 161 | chmod 600 "$STOREDIR/config.json" |
| 162 | printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github_create_issue","arguments":{"owner":"o","repo":"r","title":"t"}}}\n' > "$WORK/c1.jsonl" |
| 163 | OUT=$(SX_CONNECTOR_DIR="$STOREDIR" "$BIN" --mcp < "$WORK/c1.jsonl") |
| 164 | check "the stored token is used when no env token is set" "#4221" "$OUT" |
| 165 | printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github_search_code","arguments":{"query":"x"}}}\n' > "$WORK/c2.jsonl" |
| 166 | OUT=$(SX_CONNECTOR_DIR="$STOREDIR" GITHUB_TOKEN="GHPAT-MOCK-TOKEN-1" "$BIN" --mcp < "$WORK/c2.jsonl") |
| 167 | check "GITHUB_TOKEN overrides the stored token" "total_count: 137" "$OUT" |
| 168 | OUT=$(SX_CONNECTOR_DIR="$STOREDIR" GITHUB_TOKEN="GHPAT-MOCK-TOKEN-1" \ |
| 169 | GITHUB_API_BASE="$MOCK" "$BIN" --mcp < "$WORK/c2.jsonl") |
| 170 | check "GITHUB_API_BASE overrides the stored base" "total_count: 137" "$OUT" |
| 171 | check_not "the stored token text never appears in a tool reply" "STORED-TOKEN-AAA" "$OUT" |
| 172 | |
| 173 | # ------------------------------------------------------------------ asan |
| 174 | section "ASan + UBSan + _FORTIFY_SOURCE=2" |
| 175 | ASAN=./plugin_github-asan |
| 176 | if make asan >/dev/null 2>&1 && [ -x "$ASAN" ]; then |
| 177 | # prove the host can actually run ASan before trusting a "pass" |
| 178 | if ASAN_OPTIONS=detect_leaks=0 "$ASAN" --meta >/dev/null 2>&1; then |
| 179 | if BIN="$ASAN" PORT="$(cat "$PORTFILE")" LOG="$LOG" CFGDIR="$CFGDIR" \ |
| 180 | python3 tests/mcp.py >"$WORK/asan-mcp.log" 2>&1; then |
| 181 | pass "the whole MCP pass runs clean under ASan/UBSan" |
| 182 | else |
| 183 | fail "the MCP pass fails (or reports leaks) under ASan" |
| 184 | tail -6 "$WORK/asan-mcp.log" 2>/dev/null | sed 's/^/ /' |
| 185 | fi |
| 186 | if BIN="$ASAN" PORT="$(cat "$PORTFILE")" CFGDIR="$WORK/cgi-asan" \ |
| 187 | python3 tests/cgi.py >/dev/null; then |
| 188 | pass "the whole CGI pass runs clean under ASan/UBSan" |
| 189 | else |
| 190 | fail "the CGI pass fails under ASan" |
| 191 | fi |
| 192 | else |
| 193 | printf ' SKIP ASan cannot execute on this host (dyld/ASan incompatibility)\n' |
| 194 | fi |
| 195 | else |
| 196 | printf ' SKIP no ASan toolchain here (make asan failed)\n' |
| 197 | fi |
| 198 | |
| 199 | # ---------------------------------------------------------------- source |
| 200 | section "source hygiene" |
| 201 | check_not "no popen() anywhere" "popen(" "src/plugin_github.c src/gh_api.c src/gh_config.c" |
| 202 | FOUND=$(grep -l -E "\bpopen\(|\bsystem\(|\bexecve?\(|\bfork\(" src/*.c 2>/dev/null | grep -v sx_ | tr '\n' ' ') |
| 203 | if [ -z "$FOUND" ]; then |
| 204 | pass "the connector's own sources shell out to nothing (no popen/system/exec/fork)" |
| 205 | else |
| 206 | fail "shell execution found in: $FOUND" |
| 207 | fi |
| 208 | FOUND=$(grep -rn -E "ghp_[A-Za-z0-9]{20,}|xox[baprs]-|AKIA[0-9A-Z]{16}" \ |
| 209 | src/ tests/ Makefile plugin.json README.md 2>/dev/null | head -3) |
| 210 | if [ -z "$FOUND" ]; then pass "no credential-shaped literal in the tree"; else |
| 211 | fail "credential-shaped literal: $FOUND"; fi |
| 212 | FOUND=$(grep -rn -E "192\.168\.|\b10\.[0-9]+\.[0-9]+\.[0-9]+|\b172\.(1[6-9]|2[0-9]|3[01])\." \ |
| 213 | src/ tests/ Makefile plugin.json README.md docs/*.md 2>/dev/null | head -3) |
| 214 | if [ -z "$FOUND" ]; then pass "no private LAN address literal anywhere"; else |
| 215 | fail "LAN address literal: $FOUND"; fi |
| 216 | PAGE=$(SX_CONNECTOR_DIR="$CGIDIR" REQUEST_METHOD=GET QUERY_STRING="" "$BIN") |
| 217 | check_not "the page loads no external script" "<script src=" "$PAGE" |
| 218 | check_not "the page references no CDN" "cdnjs" "$PAGE" |
| 219 | check_not "no token text leaks into the page" "STORED-TOKEN-AAA" "$PAGE" |
| 220 | if grep -q '"build_cmd_posix": *"make"' plugin.json; then |
| 221 | pass "plugin.json declares its own build command (make)" |
| 222 | else |
| 223 | fail "plugin.json does not declare build_cmd_posix: make" |
| 224 | fi |
| 225 | if grep -q '^win64:' Makefile; then |
| 226 | pass "make win64 is a real Makefile target" |
| 227 | else |
| 228 | fail "the Makefile has no win64 target while plugin.json advertises make win64" |
| 229 | fi |
| 230 | |
| 231 | # ------------------------------------------------------------------ done |
| 232 | printf '\n== summary ==\n%s passed, %s failed\n' "$PASS" "$FAIL" |
| 233 | [ "$FAIL" -eq 0 ] || exit 1 |
| 234 | exit 0 |