interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / collab / tests / sigscan.sh
| 1 | #!/bin/bash |
| 2 | # ORACLE: host test driver (bash + curl + rad + git + python3), not product code. |
| 3 | # collab/tests/sigscan.sh: a change commit whose raw text holds "SSH SIGNATURE-----" followed by a long |
| 4 | # run of spaces must not end forged. cob64d (collab/co2cob.om) reads the signer key after that marker |
| 5 | # with a scan bounded at 4096 passes, and the scan used to run to the end of the raw commit: 4097 |
| 6 | # spaces or line breaks after the marker were trap 4 (exit 104). Found in the bound sweep after the |
| 7 | # review of 486f1cd8. |
| 8 | # Everything is private to a temp dir: a throwaway Radicle profile with one private repo, one issue |
| 9 | # and one comment. The comment's change commit is written again with git plumbing so that its author |
| 10 | # name holds the marker and 5000 spaces (its tree, parents, message and signature stay). Whether a |
| 11 | # remote peer can deliver such a commit to a node was not established; the test only shows that |
| 12 | # forged survives one in its storage. A test forged on PORT (default 18501) reads that profile. |
| 13 | # Negative control: with the scan end taken out, the same request exits 104. |
| 14 | # Usage: collab/tests/sigscan.sh (CORES default 14; SIGSCAN_PORT; KEEP=1 keeps the temp dir) |
| 15 | set -u |
| 16 | H=$(cd "$(dirname "$0")" && pwd) |
| 17 | export PORT=${SIGSCAN_PORT:-18501} CORES=${CORES:-14} |
| 18 | T=$(mktemp -d /tmp/cpsig.XXXXXX); W=$T/w |
| 19 | PASS=0; FAIL=0; PID= |
| 20 | ok() { PASS=$((PASS+1)); echo "PASS $*"; } |
| 21 | bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; } |
| 22 | check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want $2, got $1"; fi; } |
| 23 | trap '[ -n "$PID" ] && kill $PID 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT |
| 24 | if ss -ltn | grep -q "127.0.0.1:$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi |
| 25 | export RAD_HOME=$T/rad RAD_PASSPHRASE= |
| 26 | rad auth --alias cpsig < /dev/null > /dev/null 2>&1 || { echo "rad auth failed"; exit 1; } |
| 27 | git init -q -b main "$W" && cd "$W" && echo hi > a.txt && git add a.txt \ |
| 28 | && git -c user.name=t -c user.email=t@t.invalid commit -qm init \ |
| 29 | && rad init --private --name cpsig --description "signature scan test" --default-branch main --no-confirm --no-seed > /dev/null 2>&1 \ |
| 30 | || { echo "rad init failed"; exit 1; } |
| 31 | RID=$(rad . | sed 's/^rad://'); S=$RAD_HOME/storage/$RID |
| 32 | rad issue open --repo "rad:$RID" --title "signature scan" --description body --no-announce > "$T/open.out" 2>&1 \ |
| 33 | || { echo "rad issue open failed: $(cat "$T/open.out")"; exit 1; } |
| 34 | REF=$(git -C "$S" for-each-ref --format='%(refname)' 'refs/namespaces/*/refs/cobs/xyz.radicle.issue/*' | head -1) |
| 35 | ISS=${REF##*/} |
| 36 | [ -n "$ISS" ] || { echo "issue id not found in $S"; exit 1; } |
| 37 | rad issue comment "$ISS" --repo "rad:$RID" --message "a comment" --no-announce > "$T/com.out" 2>&1 \ |
| 38 | || { echo "rad issue comment failed: $(cat "$T/com.out")"; exit 1; } |
| 39 | TIP=$(git -C "$S" rev-parse "$REF") |
| 40 | [ "$TIP" != "$ISS" ] && ok "fixture: the issue has a second change commit" || { bad "fixture: no comment commit"; exit 1; } |
| 41 | git -C "$S" cat-file commit "$TIP" > "$T/raw" |
| 42 | python3 - "$T/raw" "$T/raw2" <<'PY' |
| 43 | import sys |
| 44 | b = open(sys.argv[1], "rb").read() |
| 45 | i = b.index(b"\nauthor ") + 8 |
| 46 | j = b.index(b" <", i) |
| 47 | open(sys.argv[2], "wb").write(b[:j] + b" SSH SIGNATURE-----" + b" " * 5000 + b[j:]) |
| 48 | PY |
| 49 | NEW=$(git -C "$S" hash-object -t commit -w --literally "$T/raw2") || { bad "fixture: git did not store the commit"; exit 1; } |
| 50 | git -C "$S" update-ref "$REF" "$NEW" |
| 51 | n=$(git -C "$S" cat-file commit "$NEW" | sed -n 's/^author [^ ]* SSH SIGNATURE-----\( *\) <.*/\1/p' | LC_ALL=C wc -c) |
| 52 | check "$n" 5001 "fixture: the author line of the change commit holds the marker and 5000 spaces" |
| 53 | printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| cpsig | rad:%s | main |\n' "$RID" > "$T/rids.md" |
| 54 | mkdir -p "$T/data" "$T/social" |
| 55 | start() { # start OUT [SEDX]: build and start a test forged; PID set |
| 56 | DATA=$T/data/ STORE=$RAD_HOME/storage/ RIDS=$T/rids.md SEDX=${2:-} "$H/tbuild.sh" "$1" || return 1 |
| 57 | FORGE_SOCIAL_DIR=$T/social/ taskset -c "$CORES" nice -n 10 "$1" 2>> "$1.log" & |
| 58 | PID=$! |
| 59 | for _ in $(seq 50); do curl -s -o /dev/null "http://127.0.0.1:$PORT/explore" && return 0; sleep 0.1; done |
| 60 | return 1 |
| 61 | } |
| 62 | stop() { kill $PID 2> /dev/null; wait $PID 2> /dev/null; PID=; sleep 0.3; } |
| 63 | P=/drakestapleton/cpsig/issues/$ISS |
| 64 | start "$T/forged" || { bad "test forged did not start"; exit 1; } |
| 65 | for b in / /forge/; do |
| 66 | c=$(curl -s -o "$T/page" --max-time 10 -w '%{http_code}' -H "X-Forge-Base: $b" "http://127.0.0.1:$PORT$P") |
| 67 | check "$c" 200 "[$b] issue page whose comment commit holds the marker and 5000 spaces" |
| 68 | grep -q 'a comment' "$T/page" && ok "[$b] the comment is on the page" || bad "[$b] the comment is not on the page" |
| 69 | done |
| 70 | c=$(curl -s -o /dev/null --max-time 10 -w '%{http_code}' -H 'Accept: application/json' "http://127.0.0.1:$PORT$P") |
| 71 | check "$c" 200 "issue page as JSON" |
| 72 | c=$(curl -s -o /dev/null --max-time 10 -w '%{http_code}' "http://127.0.0.1:$PORT/drakestapleton/cpsig/issues") |
| 73 | check "$c" 200 "issue list" |
| 74 | kill -0 $PID 2> /dev/null && ok "server process still running" || bad "server process is gone" |
| 75 | stop |
| 76 | # negative control: the scan end taken out (the scan runs to the end of the raw commit again) |
| 77 | start "$T/mut" 's/^e = add a 4096$/e = b/' || { bad "mutant did not start"; exit 1; } |
| 78 | [ "$(grep -c '^e = add a 4096$' "$T/mut.om")" = 0 ] && [ "$(grep -c '^e = add a 4096$' "$T/forged.om")" = 1 ] \ |
| 79 | && ok "mutant: the scan end is the end of the commit again" || bad "mutant: the sed did not change exactly one line" |
| 80 | curl -s -o /dev/null --max-time 10 -H 'X-Forge-Base: /' "http://127.0.0.1:$PORT$P" |
| 81 | wait $PID 2> /dev/null; rc=$?; PID= |
| 82 | check "$rc" 104 "NEG without the scan end the same request exits (trap 4)" |
| 83 | echo "collab sigscan: $PASS passed, $FAIL failed" |
| 84 | [ "$FAIL" = 0 ] |