interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / collab / tests / sigscan.sh

84 lines · 5675 bytes Raw Blame History
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)
15set -u
16H=$(cd "$(dirname "$0")" && pwd)
17export PORT=${SIGSCAN_PORT:-18501} CORES=${CORES:-14}
18T=$(mktemp -d /tmp/cpsig.XXXXXX); W=$T/w
19PASS=0; FAIL=0; PID=
20ok() { PASS=$((PASS+1)); echo "PASS $*"; }
21bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
22check() { if [ "$1" = "$2" ]; then ok "$3 ($1)"; else bad "$3: want $2, got $1"; fi; }
23trap '[ -n "$PID" ] && kill $PID 2>/dev/null; wait 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT
24if ss -ltn | grep -q "127.0.0.1:$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi
25export RAD_HOME=$T/rad RAD_PASSPHRASE=
26rad auth --alias cpsig < /dev/null > /dev/null 2>&1 || { echo "rad auth failed"; exit 1; }
27git 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; }
31RID=$(rad . | sed 's/^rad://'); S=$RAD_HOME/storage/$RID
32rad 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; }
34REF=$(git -C "$S" for-each-ref --format='%(refname)' 'refs/namespaces/*/refs/cobs/xyz.radicle.issue/*' | head -1)
35ISS=${REF##*/}
36[ -n "$ISS" ] || { echo "issue id not found in $S"; exit 1; }
37rad 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; }
39TIP=$(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; }
41git -C "$S" cat-file commit "$TIP" > "$T/raw"
42python3 - "$T/raw" "$T/raw2" <<'PY'
43import sys
44b = open(sys.argv[1], "rb").read()
45i = b.index(b"\nauthor ") + 8
46j = b.index(b" <", i)
47open(sys.argv[2], "wb").write(b[:j] + b" SSH SIGNATURE-----" + b" " * 5000 + b[j:])
48PY
49NEW=$(git -C "$S" hash-object -t commit -w --literally "$T/raw2") || { bad "fixture: git did not store the commit"; exit 1; }
50git -C "$S" update-ref "$REF" "$NEW"
51n=$(git -C "$S" cat-file commit "$NEW" | sed -n 's/^author [^ ]* SSH SIGNATURE-----\( *\) <.*/\1/p' | LC_ALL=C wc -c)
52check "$n" 5001 "fixture: the author line of the change commit holds the marker and 5000 spaces"
53printf '# test allow-list\n\n| repo | RID | default branch |\n|---|---|---|\n| cpsig | rad:%s | main |\n' "$RID" > "$T/rids.md"
54mkdir -p "$T/data" "$T/social"
55start() { # 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}
62stop() { kill $PID 2> /dev/null; wait $PID 2> /dev/null; PID=; sleep 0.3; }
63P=/drakestapleton/cpsig/issues/$ISS
64start "$T/forged" || { bad "test forged did not start"; exit 1; }
65for 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"
69done
70c=$(curl -s -o /dev/null --max-time 10 -w '%{http_code}' -H 'Accept: application/json' "http://127.0.0.1:$PORT$P")
71check "$c" 200 "issue page as JSON"
72c=$(curl -s -o /dev/null --max-time 10 -w '%{http_code}' "http://127.0.0.1:$PORT/drakestapleton/cpsig/issues")
73check "$c" 200 "issue list"
74kill -0 $PID 2> /dev/null && ok "server process still running" || bad "server process is gone"
75stop
76# negative control: the scan end taken out (the scan runs to the end of the raw commit again)
77start "$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"
80curl -s -o /dev/null --max-time 10 -H 'X-Forge-Base: /' "http://127.0.0.1:$PORT$P"
81wait $PID 2> /dev/null; rc=$?; PID=
82check "$rc" 104 "NEG without the scan end the same request exits (trap 4)"
83echo "collab sigscan: $PASS passed, $FAIL failed"
84[ "$FAIL" = 0 ]