scorpiox-bot-plugin-github
←run.shbash
234 lines · 10 KBHEADBlameDownload
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
31set -u
32
33ROOT=$(cd "$(dirname "$0")/.." && pwd)
34cd "$ROOT" || exit 1
35
36BIN=${BIN:-./plugin_github}
37WORK=$(mktemp -d "${TMPDIR:-/tmp}/gh-suite.XXXXXX")
38PORTFILE="$WORK/port"
39LOG="$WORK/req.jsonl"
40CFGDIR="$WORK/cfg"
41CGIDIR="$WORK/cgi"
42MOCK_PID=""
43
44cleanup() {
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}
49trap cleanup EXIT INT TERM
50
51PASS=0
52FAIL=0
53
54section() { printf '\n== %s ==\n' "$1"; }
55pass() { PASS=$((PASS + 1)); printf ' ok %s\n' "$1"; }
56fail() { FAIL=$((FAIL + 1)); printf ' FAIL %s\n' "$1"; }
57
58check() { # 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}
65check_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}
72json_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
84mkdir -p "$CFGDIR" "$CGIDIR"
85rm -f "$LOG"
86
87if [ ! -x "$BIN" ]; then
88 echo "plugin_github is not built - run make first" >&2
89 exit 1
90fi
91
92# ------------------------------------------------------------------ mock
93python3 tests/mockapi.py --portfile "$PORTFILE" --log "$LOG" 2>"$WORK/mock.err" &
94MOCK_PID=$!
95i=0
96while [ ! -s "$PORTFILE" ] && [ $i -lt 100 ]; do
97 i=$((i + 1)); sleep 0.05
98done
99if [ ! -s "$PORTFILE" ]; then
100 echo "the mock server did not start: $(cat "$WORK/mock.err")" >&2
101 exit 1
102fi
103MOCK="http://127.0.0.1:$(cat "$PORTFILE")"
104
105# ---------------------------------------------------------------- --meta
106section "--meta / discovery"
107META=$("$BIN" --meta)
108json_ok "--meta is one line of valid JSON" "$META"
109json_ok "name is the short chip label github" "$META" 'd["name"]=="github"'
110json_ok "desc is <= 15 chars" "$META" 'len(d["desc"])<=15'
111json_ok "desc is GitHub tools" "$META" 'd["desc"]=="GitHub tools"'
112json_ok "kind is mcp, mcp true, menus false" \
113 "$META" 'd["kind"]=="mcp" and d["mcp"] is True and d["menus"] is False'
114json_ok "daemon is false (this connector has no always-on bridge)" \
115 "$META" 'd["daemon"] is False'
116json_ok "verified is mock (an honest tier: the suite drives a mock, not a live account)" \
117 "$META" 'd["verified"]=="mock"'
118check "SX_PLUGIN_META=1 agrees" '"name":"github"' "$(SX_PLUGIN_META=1 "$BIN")"
119json_ok "--meta version agrees with plugin.json" "$META" \
120 'd["version"]==__import__("json").load(open("plugin.json"))["version"]'
121
122# ---------------------------------------------------------------- --schema
123section "--schema"
124SCHEMA=$("$BIN" --schema)
125json_ok "--schema is valid JSON" "$SCHEMA"
126json_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"])'
128json_ok "every tool has a description and an inputSchema" "$SCHEMA" \
129 'all(t.get("description") and t.get("inputSchema") for t in d["tools"])'
130json_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"}'
132json_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)
136section "--mcp + request shapes (tests/mcp.py)"
137if BIN="$BIN" PORT="$(cat "$PORTFILE")" LOG="$LOG" CFGDIR="$CFGDIR" \
138 python3 tests/mcp.py; then
139 pass "tests/mcp.py exited 0"
140else
141 fail "tests/mcp.py reported failures"
142fi
143
144# ----------------------------------------------------------------- cgi
145section "CGI control API (tests/cgi.py)"
146if BIN="$BIN" PORT="$(cat "$PORTFILE")" CFGDIR="$CGIDIR" \
147 python3 tests/cgi.py; then
148 pass "tests/cgi.py exited 0"
149else
150 fail "tests/cgi.py reported failures"
151fi
152
153# ------------------------------------------------------- config from disk
154section "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).
158STOREDIR="$WORK/stored"
159mkdir -p "$STOREDIR"
160printf "{\"api_base\":\"%s\",\"token\":\"STORED-TOKEN-AAA\"}\n" "$MOCK" > "$STOREDIR/config.json"
161chmod 600 "$STOREDIR/config.json"
162printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github_create_issue","arguments":{"owner":"o","repo":"r","title":"t"}}}\n' > "$WORK/c1.jsonl"
163OUT=$(SX_CONNECTOR_DIR="$STOREDIR" "$BIN" --mcp < "$WORK/c1.jsonl")
164check "the stored token is used when no env token is set" "#4221" "$OUT"
165printf '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"github_search_code","arguments":{"query":"x"}}}\n' > "$WORK/c2.jsonl"
166OUT=$(SX_CONNECTOR_DIR="$STOREDIR" GITHUB_TOKEN="GHPAT-MOCK-TOKEN-1" "$BIN" --mcp < "$WORK/c2.jsonl")
167check "GITHUB_TOKEN overrides the stored token" "total_count: 137" "$OUT"
168OUT=$(SX_CONNECTOR_DIR="$STOREDIR" GITHUB_TOKEN="GHPAT-MOCK-TOKEN-1" \
169 GITHUB_API_BASE="$MOCK" "$BIN" --mcp < "$WORK/c2.jsonl")
170check "GITHUB_API_BASE overrides the stored base" "total_count: 137" "$OUT"
171check_not "the stored token text never appears in a tool reply" "STORED-TOKEN-AAA" "$OUT"
172
173# ------------------------------------------------------------------ asan
174section "ASan + UBSan + _FORTIFY_SOURCE=2"
175ASAN=./plugin_github-asan
176if 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
195else
196 printf ' SKIP no ASan toolchain here (make asan failed)\n'
197fi
198
199# ---------------------------------------------------------------- source
200section "source hygiene"
201check_not "no popen() anywhere" "popen(" "src/plugin_github.c src/gh_api.c src/gh_config.c"
202FOUND=$(grep -l -E "\bpopen\(|\bsystem\(|\bexecve?\(|\bfork\(" src/*.c 2>/dev/null | grep -v sx_ | tr '\n' ' ')
203if [ -z "$FOUND" ]; then
204 pass "the connector's own sources shell out to nothing (no popen/system/exec/fork)"
205else
206 fail "shell execution found in: $FOUND"
207fi
208FOUND=$(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)
210if [ -z "$FOUND" ]; then pass "no credential-shaped literal in the tree"; else
211 fail "credential-shaped literal: $FOUND"; fi
212FOUND=$(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)
214if [ -z "$FOUND" ]; then pass "no private LAN address literal anywhere"; else
215 fail "LAN address literal: $FOUND"; fi
216PAGE=$(SX_CONNECTOR_DIR="$CGIDIR" REQUEST_METHOD=GET QUERY_STRING="" "$BIN")
217check_not "the page loads no external script" "<script src=" "$PAGE"
218check_not "the page references no CDN" "cdnjs" "$PAGE"
219check_not "no token text leaks into the page" "STORED-TOKEN-AAA" "$PAGE"
220if grep -q '"build_cmd_posix": *"make"' plugin.json; then
221 pass "plugin.json declares its own build command (make)"
222else
223 fail "plugin.json does not declare build_cmd_posix: make"
224fi
225if grep -q '^win64:' Makefile; then
226 pass "make win64 is a real Makefile target"
227else
228 fail "the Makefile has no win64 target while plugin.json advertises make win64"
229fi
230
231# ------------------------------------------------------------------ done
232printf '\n== summary ==\n%s passed, %s failed\n' "$PASS" "$FAIL"
233[ "$FAIL" -eq 0 ] || exit 1
234exit 0