interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / quiet / tests / check.sh

421 lines · 25103 bytes Raw Blame History
1#!/bin/bash
2# ORACLE: drives a test forged and the Omega tool. git, rad, curl and ssh-keygen are oracles.
3# The throwaway node has no route out. It does not touch port 18480, the live node, or the live data dir.
4# QUIET_MUTANT=<sed expression> is applied to the test forged and to the tool (quiet/tests/mutants.sh).
5# The test ends its own radicle-node: the launcher is sudo -> unshare -> runuser -> rad, and the node is
6# rad's child, so killing the launcher alone left one node behind per run.
7# untr FILE: forged adds translation hints while it sends an HTML page (server/tr.om); the checks here
8# read pages without them (server/tests/translate.sh gates the hints). Other bodies are left as sent.
9untr() { case "$(head -c 15 "$1" 2>/dev/null)" in '<!doctype html'*) sed -i 's/<span translate="no">\([^<]*\)<\/span>/\1/g; s/ translate="no"//g' "$1" ;; esac; }
10set -u
11cd "$(dirname "$0")/../.."
12REALHOME=/home/drakestapleton
13OMV=$REALHOME/work/flux/omega-mv
14OMC=$OMV/seed/out/omc
15PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
16PORT=${QUIET_PORT:-18521}
17CORES=${FORGED_CPUS:-10,11}
18# the queen pool lock that owns CORES (pool-10-11 or pool-12-14 with FORGED_CPUS=12,13,14)
19LOCK=${QUIET_LOCK:-$REALHOME/handoffs/hive/queen/pool-10-11.lock}
20T=$(mktemp -d /tmp/forge-quiet.XXXXXX)
21PASS=0
22FAIL=0
23PID=
24NPID=
25FAKE=$T/home
26export HOME="$FAKE"
27export RAD_HOME="$FAKE/.radicle"
28export RAD_PASSPHRASE=simplify-test-pass
29export PATH="$FAKE/.local/bin:/usr/bin:/bin"
30ok() { PASS=$((PASS+1)); echo "PASS $*"; }
31bad() { FAIL=$((FAIL+1)); echo "FAIL $*"; }
32# mynodes: pids of radicle-node processes whose RAD_HOME is this test's (never another run's)
33mynodes() {
34 for p in $(pgrep -u "$(id -u)" -x radicle-node); do
35 tr '\0' '\n' < "/proc/$p/environ" 2>/dev/null | grep -q -x "RAD_HOME=$RAD_HOME" && echo "$p"
36 done
37 return 0
38}
39stopnode() {
40 rad node stop > /dev/null 2>&1 || true
41 if [ -n "$NPID" ]; then sudo -n kill "$NPID" 2>/dev/null || true; wait "$NPID" 2>/dev/null || true; NPID=; fi
42 for p in $(mynodes); do kill "$p" 2>/dev/null || true; done
43 for _ in 1 2 3 4 5 6 7 8 9 10; do [ -z "$(mynodes)" ] && break; sleep 0.2; done
44 for p in $(mynodes); do kill -9 "$p" 2>/dev/null || true; done
45}
46trap 'stopnode; [ -n "$PID" ] && kill "$PID" 2>/dev/null; [ -n "${KEEP:-}" ] || rm -rf "$T"' EXIT
47got=$(sha256sum "$OMC" | cut -d' ' -f1)
48[ "$got" = "$PIN" ] || { echo "omc pin mismatch"; exit 1; }
49mkdir -p "$FAKE/.local/bin"
50ln -s "$REALHOME/.radicle/bin/rad" "$FAKE/.local/bin/rad"
51ln -s "$REALHOME/.radicle/bin/radicle-node" "$FAKE/.local/bin/radicle-node"
52ln -s "$REALHOME/.radicle/bin/git-remote-rad" "$FAKE/.local/bin/git-remote-rad"
53printf '%s\n' "$RAD_PASSPHRASE" | rad auth --alias simplify-test --stdin > "$T/auth.out" 2>&1 \
54 || { echo "rad auth failed"; cat "$T/auth.out"; exit 1; }
55SRC=$T/src
56mkdir -p "$SRC"
57git -C "$SRC" init -q -b main
58git -C "$SRC" config user.email forge@localhost
59git -C "$SRC" config user.name forge-d
60printf 'throwaway\n' > "$SRC/README.md"
61git -C "$SRC" add README.md
62git -C "$SRC" -c commit.gpgsign=false commit -qm init
63rad init --public --no-seed --name forge-d-l1008 --description "throwaway for L1008 D" --default-branch main --no-confirm "$SRC" > "$T/init.out" 2>&1 \
64 || { echo "rad init failed"; cat "$T/init.out"; exit 1; }
65RID=$(grep -o 'rad:z[1-9A-HJ-NP-Za-km-z]*' "$T/init.out" | head -1)
66RID=${RID#rad:}
67[ -n "$RID" ] || { echo "no RID"; cat "$T/init.out"; exit 1; }
68echo "RID $RID"
69mkdir -p "$T/storage" "$T/data"
70ln -s "$RAD_HOME/storage/$RID" "$T/storage/$RID"
71cat > "$T/RIDS.md" <<EOF
72# test table
73
74| repo | RID | default branch | head sha | source dir | UTC |
75|---|---|---|---|---|---|
76| forge-d-l1008 | rad:$RID | main | - | - | - |
77EOF
78sudo -n unshare --net runuser -u drakestapleton -- env \
79 HOME="$FAKE" RAD_HOME="$RAD_HOME" RAD_PASSPHRASE="$RAD_PASSPHRASE" PATH="$PATH" \
80 rad node start --foreground -- --listen 127.0.0.1:18779 > "$T/node.log" 2>&1 &
81NPID=$!
82up=0
83for i in $(seq 1 20); do
84 if rad node status 2>/dev/null | head -1 | grep -q 'Node ID'; then up=1; break; fi
85 sleep 1
86done
87[ "$up" = 1 ] || { echo "isolated node did not start"; cat "$T/node.log"; exit 1; }
88
89U=$(HOME=$REALHOME FORGE_TRY=quiet ./run.sh units | tr '\n' ' ')
90{ printf 'om2\n'; cat $U; } > "$T/all.om"
91sed -i \
92 -e "s|haddr4 a 2130706433 18480|haddr4 a 2130706433 $PORT|" \
93 -e "s|/home/drakestapleton/handoffs/radicle/RIDS.md|$T/RIDS.md|" \
94 -e "s|/home/drakestapleton/.radicle/storage/|$T/storage/|g" \
95 -e "s|/home/drakestapleton/forge-data/|$T/data/|g" \
96 "$T/all.om"
97grep -q "$T/data/" "$T/all.om" || { echo "data dir not redirected"; exit 1; }
98if [ -n "${QUIET_MUTANT:-}" ]; then
99 cp "$T/all.om" "$T/all.orig.om"
100 sed -i -e "$QUIET_MUTANT" "$T/all.om"
101 cmp -s "$T/all.om" "$T/all.orig.om" && { echo "MUTANT changed nothing in the server source"; exit 1; }
102 echo "MUTANT $QUIET_MUTANT"
103fi
104flock "$LOCK" taskset -c "$CORES" nice -n 10 "$OMC" < "$T/all.om" > "$T/forged" 2> "$T/omc.err" \
105 || { echo "BUILD FAILED"; cat "$T/omc.err"; exit 1; }
106chmod +x "$T/forged"
107if [ -n "${QUIET_MUTANT:-}" ]; then
108 # the units of contrib/build.sh, with the same mutant
109 { printf 'om2\n'; cat "$OMV/om/tls/sha2.om" server/util.om quiet/qn2bind.om quiet/qn1dig.om contrib/forge-contribute.om; } \
110 | sed -e "$QUIET_MUTANT" | flock "$LOCK" taskset -c "$CORES" nice -n 10 "$OMC" > "$T/forge-contribute" 2> "$T/omc-tool.err" \
111 || { echo "TOOL BUILD FAILED"; cat "$T/omc-tool.err"; exit 1; }
112 chmod +x "$T/forge-contribute"
113else
114 flock "$LOCK" taskset -c "$CORES" nice -n 10 env HOME="$REALHOME" FORGED_CPUS="$CORES" sh contrib/build.sh "$T/forge-contribute" \
115 || { echo "TOOL BUILD FAILED"; exit 1; }
116fi
117
118B="http://127.0.0.1:$PORT"
119srv() {
120 FORGE_SOCIAL_DIR="$T/data/" taskset -c "$CORES" nice -n 10 "$T/forged" >> "$T/forged.out" 2>> "$T/forged.err" &
121 PID=$!
122 for i in $(seq 50); do curl -sf -o /dev/null "$B/" && return 0; sleep 0.1; done
123 return 1
124}
125# alive NAME: the server process is still there and answers. When it is gone its exit code is
126# reported (104 is trap 4, a loop past its bound) and it is started again, so the next case is judged alone.
127# A process that is ending can still be there for a moment after its last request failed: when the
128# liveness request fails, the process is watched for 5 s more, so that its exit is recorded under
129# this case and not under the next one. Alive but silent after that is its own failure.
130alive() {
131 if kill -0 "$PID" 2>/dev/null; then
132 if [ "$(curl -s -o /dev/null -m 5 -w '%{http_code}' "$B/")" = 200 ]; then ok "$1: server still answers"; return 0; fi
133 for _ in $(seq 25); do kill -0 "$PID" 2>/dev/null || break; sleep 0.2; done
134 if kill -0 "$PID" 2>/dev/null; then bad "$1: server runs but does not answer"; return 1; fi
135 fi
136 wait "$PID" 2>/dev/null; rc=$?
137 bad "$1: server exited rc=$rc"
138 srv || { echo "server did not start again"; exit 1; }
139 return 1
140}
141srv || { echo "server did not start"; cat "$T/forged.err"; exit 1; }
142
143code() { # method path [curl args...]
144 m=$1; p=$2; shift 2
145 c=$(curl -s -o "$T/body" -D "$T/hdr" -X "$m" "$@" "$B$p" -w '%{http_code}'); untr "$T/body"; printf %s "$c"
146}
147hdr() { grep -i "^$1:" "$T/hdr" | head -1 | cut -d' ' -f2- | tr -d '\r'; }
148field() { grep -o "name=\"$1\" value=\"[^\"]*\"" "$T/body" | head -1 | sed 's/.*value="//; s/"$//'; }
149
150ssh-keygen -q -t ed25519 -N '' -C t1 -f "$T/k1"
151D1=$(python3 - "$T/k1.pub" <<'PY'
152import sys, base64
153blob = base64.b64decode(open(sys.argv[1]).read().split()[1])
154raw = b'\xed\x01' + blob[-32:]
155A = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
156n = int.from_bytes(raw, 'big'); s = ''
157while n:
158 n, r = divmod(n, 58); s = A[r] + s
159print('did:key:z' + '1' * (len(raw) - len(raw.lstrip(b'\0'))) + s)
160PY
161)
162c=$(code GET /login)
163[ "$c" = 200 ] && ok "GET /login" || bad "GET /login $c"
164PREN=$(hdr set-cookie | sed -n 's/^\([A-Za-z_-]*forge_pre\)=.*/\1/p')
165PRE=$(hdr set-cookie | sed -n 's/^[A-Za-z_-]*forge_pre=\([0-9a-f]*\);.*/\1/p')
166CSRF=$(field csrf); NONCE=$(field nonce); TS=$(field ts)
167printf '%s' "interplane-forge-login:$NONCE:$TS" | ssh-keygen -Y sign -f "$T/k1" -n interplane-forge > "$T/sig" 2>/dev/null
168c=$(code POST /login -H "Cookie: ${PREN}=$PRE" --data-urlencode "csrf=$CSRF" --data-urlencode "nonce=$NONCE" \
169 --data-urlencode "ts=$TS" --data-urlencode "did=$D1" --data-urlencode "sig@$T/sig")
170[ "$c" = 303 ] && ok "sign in" || bad "sign in $c"
171SESSN=$(hdr set-cookie | sed -n 's/^\([A-Za-z_-]*forge_session\)=.*/\1/p')
172SESS=$(hdr set-cookie | sed -n 's/^[A-Za-z_-]*forge_session=\([^;]*\);.*/\1/p')
173CK="Cookie: ${SESSN}=$SESS"
174c=$(code GET /settings -H "$CK")
175[ "$c" = 200 ] && ok "settings" || bad "settings $c"
176CSRF=$(field csrf)
177c=$(code POST /settings/name -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "name=tester-one")
178[ "$c" = 303 ] && ok "display name tester-one" || bad "display name $c"
179
180# quiet by default: a read does not create a digest file
181for base in / /forge/; do
182 c=$(code GET /notifications -H "$CK" -H "X-Forge-Base: $base")
183 grep -q 'No digest file' "$T/body" && grep -q 'Quiet by default' "$T/body" && [ "$c" = 200 ] \
184 && ok "[$base] quiet page" || bad "[$base] quiet page $c"
185 c=$(code GET /notifications.json -H "$CK" -H "X-Forge-Base: $base")
186 grep -q '"quiet":true' "$T/body" && [ "$c" = 200 ] && ok "[$base] quiet JSON" || bad "[$base] quiet JSON $c"
187done
188[ ! -e "$T/data/quiet/out/tester-one" ] && ok "NEG read did not create a digest file" || bad "digest file created on read"
189c=$(code GET /notifications.json)
190[ "$c" = 401 ] && ok "NEG signed-out JSON is 401" || bad "signed-out JSON $c"
191
192# subscription is an event: it writes the digest
193c=$(code GET /notifications -H "$CK")
194CSRF=$(field csrf)
195c=$(code POST /notifications -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "agent=bay24" --data-urlencode "repo=forge-d-l1008")
196[ "$c" = 303 ] && ok "save filter" || bad "save filter $c"
197grep -q 'count=0' "$T/data/quiet/out/tester-one" && ok "filter event wrote an empty digest" || bad "empty digest missing"
198
199# one command
200# more than 64 KiB of older, non-matching events first: the new event must still reach the digest
201for i in $(seq 800); do printf 'patch other-repo other %040d %040d\n' "$i" "$i"; done >> "$T/data/quiet/events"
202[ "$(stat -c %s "$T/data/quiet/events")" -gt 65536 ] && ok "events file holds more than 64 KiB before the tool" || bad "events prefill too small"
203printf 'hello from the tool\n' > "$T/body.txt"
204printf '#!/bin/sh\npwd > %s/cmd.pwd\nprintf "ran ok\\n"\nexit 0\n' "$T" > "$T/cmd.sh"
205chmod +x "$T/cmd.sh"
206CLONE=$T/clone
207"$T/forge-contribute" "$RID" "$CLONE" "feat-d" "Add a note..." "$T/cmd.sh" tester-one bay24 forge-d-l1008 note.txt "$T/body.txt" "$T/data/" "$HOME" > "$T/tool.out" 2> "$T/tool.err"
208rc=$?
209[ "$rc" = 0 ] && ok "forge-contribute exit 0 (title with ..)" || bad "forge-contribute exit $rc $(cat "$T/tool.err" | tail -20)"
210[ "$(cat "$T/cmd.pwd" 2>/dev/null)" = "$CLONE" ] && ok "CMD ran in the clone" || bad "CMD cwd: $(cat "$T/cmd.pwd" 2>/dev/null)"
211PAT=$(awk '/^patch /{print $2}' "$T/tool.out")
212CMT=$(awk '/^commit /{print $2}' "$T/tool.out")
213[ ${#PAT} = 40 ] && [ ${#CMT} = 40 ] && ok "tool printed patch and commit" || bad "tool output $(cat "$T/tool.out")"
214# an empty id would make every later grep match: replace it with a word no page contains
215[ -n "$PAT" ] || PAT=no-patch-id-printed
216[ -n "$CMT" ] || CMT=no-commit-printed
217grep -q "$PAT" "$T/data/quiet/out/tester-one" && grep -q 'count=1' "$T/data/quiet/out/tester-one" \
218 && ok "event rewrote the digest" || bad "digest after contribute: $(cat "$T/data/quiet/out/tester-one" 2>/dev/null)"
219BEFORE=$(sha256sum "$T/data/quiet/out/tester-one" | cut -d' ' -f1)
220for base in / /forge/; do
221 P=${base%/}
222 c=$(code GET /notifications -H "$CK" -H "X-Forge-Base: $base")
223 grep -q "$PAT" "$T/body" && grep -q 'bay24' "$T/body" && [ "$c" = 200 ] && ok "[$base] digest page shows the patch" || bad "[$base] digest page"
224 c=$(code GET /notifications.json -H "$CK" -H "X-Forge-Base: $base")
225 grep -q "$PAT" "$T/body" && grep -q '"quiet":false' "$T/body" && [ "$c" = 200 ] && ok "[$base] digest JSON" || bad "[$base] digest JSON"
226 python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$T/body" 2>/dev/null && ok "[$base] digest JSON parses" || bad "[$base] digest JSON does not parse"
227 c=$(code GET "/drakestapleton/forge-d-l1008" -H "X-Forge-Base: $base")
228 grep -q "Get this repository" "$T/body" && grep -q "forge-contribute $RID DIR BRANCH" "$T/body" && [ "$c" = 200 ] \
229 && ok "[$base] clone panel names the tool with this RID" || bad "[$base] clone panel $c"
230 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PAT" -H "X-Forge-Base: $base")
231 grep -q 'Evidence receipt' "$T/body" && grep -q '>PASS<' "$T/body" && grep -q "$CMT" "$T/body" \
232 && grep -q ".receipt" "$T/body" && [ "$c" = 200 ] && ok "[$base] patch page PASS" || bad "[$base] patch page $c"
233 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PAT.receipt" -H "X-Forge-Base: $base")
234 grep -q '"verify":"PASS"' "$T/body" && grep -q "\"commit\":\"$CMT\"" "$T/body" && [ "$c" = 200 ] \
235 && ok "[$base] machine receipt PASS" || bad "[$base] machine receipt $c $(head -c 200 "$T/body")"
236 python3 -c 'import json,sys; json.load(open(sys.argv[1]))' "$T/body" 2>/dev/null && ok "[$base] receipt JSON parses" || bad "[$base] receipt JSON does not parse"
237done
238AFTER=$(sha256sum "$T/data/quiet/out/tester-one" | cut -d' ' -f1)
239[ "$BEFORE" = "$AFTER" ] && ok "NEG page read did not rewrite the digest" || bad "digest changed on read"
240rm -f "$T/data/quiet/out/tester-one"
241code GET /notifications -H "$CK" >/dev/null
242[ ! -e "$T/data/quiet/out/tester-one" ] && ok "NEG deleted digest was not rebuilt on read" || bad "read rebuilt the digest"
243
244# agent filter negative: a different agent does not keep the event
245c=$(code GET /notifications -H "$CK"); CSRF=$(field csrf)
246c=$(code POST /notifications -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "agent=other" --data-urlencode "repo=forge-d-l1008")
247grep -q "$PAT" "$T/data/quiet/out/tester-one" && bad "NEG other-agent filter still lists the patch" || ok "NEG other-agent filter excludes the patch"
248grep -q 'count=0' "$T/data/quiet/out/tester-one" && ok "NEG filtered digest count 0" || bad "filtered count"
249
250# tampered receipt: flip rc, keep the bind
251code GET "/drakestapleton/forge-d-l1008/patches/$PAT.receipt" >/dev/null
252python3 - "$T/body" "$T/bad-bind.txt" "$T/bad-commit.txt" <<'PY'
253import json, sys, hashlib
254raw = open(sys.argv[1]).read()
255j = json.loads(raw)
256lines = [
257 f"commit={j['commit']}",
258 f"tree={j['tree']}",
259 f"cmd={j['cmd']}",
260 f"rc={j['rc']+1}",
261 f"out={j['out']}",
262 f"agent={j['agent']}",
263]
264canon = "".join(x+"\n" for x in lines)
265# stale bind from the original
266stale = j["bind"] if "bind" in j else ""
267open(sys.argv[2],"w").write("FORGE-EVIDENCE v1\n"+canon+f"bind={stale}\n")
268lines[0] = "commit=" + "0"*40
269canon = "".join(x+"\n" for x in lines)
270bind = hashlib.sha256(canon.encode()).hexdigest()
271open(sys.argv[3],"w").write("FORGE-EVIDENCE v1\n"+canon+f"bind={bind}\n")
272print(stale[:12], bind[:12])
273PY
274# the JSON may not include bind. If not, compute the original bind from the good lines.
275if ! grep -q '"bind"' "$T/body"; then
276 python3 - "$T/body" "$T/bad-bind.txt" <<'PY'
277import json, sys, hashlib
278j = json.loads(open(sys.argv[1]).read())
279lines = [f"commit={j['commit']}", f"tree={j['tree']}", f"cmd={j['cmd']}", f"rc={j['rc']}", f"out={j['out']}", f"agent={j['agent']}"]
280canon = "".join(x+"\n" for x in lines)
281bind = hashlib.sha256(canon.encode()).hexdigest()
282lines[3] = f"rc={j['rc']+1}"
283open(sys.argv[2],"w").write("FORGE-EVIDENCE v1\n"+"".join(x+"\n" for x in lines)+f"bind={bind}\n")
284PY
285fi
286
287# second and third patches, planted by the oracle, not by the tool
288git -C "$CLONE" checkout -q -b feat-bad
289printf 'bad\n' > "$CLONE/bad.txt"
290git -C "$CLONE" add bad.txt
291git -C "$CLONE" -c commit.gpgsign=false -c user.name=forge-d -c user.email=forge@localhost commit -qm bad
292git -C "$CLONE" push -o patch.message=Bad rad HEAD:refs/patches > "$T/push2.out" 2>&1 || { echo "push2 failed"; cat "$T/push2.out"; bad "push tampered patch"; }
293PAT2=$(grep -o 'Patch [0-9a-f]\{40\}' "$T/push2.out" | awk '{print $2}')
294rad patch edit "$PAT2" --repo "rad:$RID" --no-announce --message Bad --message "$(cat "$T/bad-bind.txt")" > "$T/edit2.out" 2>&1 \
295 && ok "planted stale-bind receipt" || bad "rad patch edit bind $(cat "$T/edit2.out")"
296c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PAT2.receipt")
297grep -q '"verify":"REFUSED"' "$T/body" && grep -q '"reason":"bind"' "$T/body" && [ "$c" = 200 ] \
298 && ok "NEG tampered receipt refused (bind)" || bad "NEG bind $(head -c 240 "$T/body")"
299
300git -C "$CLONE" checkout -q -b feat-commit
301printf 'worse\n' > "$CLONE/worse.txt"
302git -C "$CLONE" add worse.txt
303git -C "$CLONE" -c commit.gpgsign=false -c user.name=forge-d -c user.email=forge@localhost commit -qm worse
304git -C "$CLONE" push -o patch.message=Worse rad HEAD:refs/patches > "$T/push3.out" 2>&1 || bad "push commit-mismatch"
305PAT3=$(grep -o 'Patch [0-9a-f]\{40\}' "$T/push3.out" | awk '{print $2}')
306rad patch edit "$PAT3" --repo "rad:$RID" --no-announce --message Worse --message "$(cat "$T/bad-commit.txt")" > "$T/edit3.out" 2>&1 \
307 && ok "planted wrong-commit receipt" || bad "rad patch edit commit"
308c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PAT3.receipt")
309grep -q '"verify":"REFUSED"' "$T/body" && grep -q '"reason":"commit"' "$T/body" && [ "$c" = 200 ] \
310 && ok "NEG wrong commit refused" || bad "NEG commit $(head -c 240 "$T/body")"
311
312# tool refuses a bad argument
313"$T/forge-contribute" "not-a-rid" "$T/nope" br title "$T/cmd.sh" tester-one bay24 forge-d-l1008 note.txt "$T/body.txt" "$T/data/" "$HOME" > "$T/neg.out" 2>"$T/neg.err"
314[ $? = 2 ] && ok "NEG bad rid refused" || bad "NEG bad rid exit $?"
315"$T/forge-contribute" "$RID" "$T/x/../dots" br title "$T/cmd.sh" tester-one bay24 forge-d-l1008 note.txt "$T/body.txt" "$T/data/" "$HOME" > "$T/neg.out" 2>"$T/neg.err"
316rc=$?
317[ "$rc" = 2 ] && [ ! -e "$T/dots" ] && ok "NEG .. segment in DIR refused" || bad "NEG .. segment exit $rc"
318mkdir "$T/empty"
319"$T/forge-contribute" "$RID" "$T/empty" br title "$T/cmd.sh" tester-one bay24 forge-d-l1008 note.txt "$T/body.txt" "$T/data/" "$HOME" > "$T/neg.out" 2>"$T/neg.err"
320rc=$?
321[ "$rc" = 2 ] && [ -z "$(ls -A "$T/empty")" ] && ok "NEG existing DIR refused" || bad "NEG existing DIR exit $rc"
322# ---- command lengths at and past the old 64-pass bound of qncanh (review of 486f1cd8, finding F2).
323# A receipt may carry 200 bytes of cmd (qncopy); the binder must take all of them, in the tool and
324# on the patch page and the .receipt route.
325mkcmd() { # mkcmd LEN: an executable whose absolute path is exactly LEN bytes; prints the path
326 local pad=$(( $1 - ${#T} - 5 )) p
327 p="$T/c$(printf '%*s' "$pad" '' | tr ' ' c).sh"
328 printf '#!/bin/sh\nprintf "ran ok\\n"\nexit 0\n' > "$p"; chmod +x "$p"; printf '%s' "$p"
329}
330for L in 64 65 200; do
331 CMDP=$(mkcmd $L)
332 [ ${#CMDP} = $L ] && ok "fixture: CMD path of $L bytes" || bad "fixture: CMD path is ${#CMDP} bytes, want $L"
333 "$T/forge-contribute" "$RID" "$T/clone$L" "feat-l$L" "Cmd of $L bytes" "$CMDP" tester-one bay24 forge-d-l1008 "note$L.txt" "$T/body.txt" "$T/data/" "$HOME" > "$T/tool$L.out" 2> "$T/tool$L.err"
334 rc=$?
335 [ "$rc" = 0 ] && ok "forge-contribute exit 0 with a $L-byte CMD" || bad "forge-contribute with a $L-byte CMD: exit $rc"
336 PL=$(awk '/^patch /{print $2}' "$T/tool$L.out"); [ -n "$PL" ] || PL=no-patch-id-printed
337 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PL")
338 grep -q '>PASS<' "$T/body" && [ "$c" = 200 ] && ok "patch page PASS with a $L-byte cmd" || bad "patch page with a $L-byte cmd: $c"
339 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PL.receipt")
340 n=$(python3 -c 'import json,sys; j=json.load(open(sys.argv[1])); print(len(j["cmd"]) if j["verify"]=="PASS" else -1)' "$T/body" 2>/dev/null)
341 [ "$c" = 200 ] && [ "$n" = "$L" ] && ok "machine receipt PASS, cmd is $L bytes" || bad "machine receipt with a $L-byte cmd: $c cmd=$n"
342 alive "after the pages of the $L-byte CMD"
343done
344# The same lengths in a receipt the oracle wrote (python sha256, not the tool), with a commit of
345# zeros: the server's binder must accept the bind, so the refusal names the commit. 201 bytes is over
346# the format: a parse refusal.
347for L in 65 200 201; do
348 git -C "$CLONE" checkout -q -b "plant$L"
349 printf 'p%s\n' "$L" > "$CLONE/plant$L.txt"
350 git -C "$CLONE" add "plant$L.txt"
351 git -C "$CLONE" -c commit.gpgsign=false -c user.name=forge-d -c user.email=forge@localhost commit -qm "plant $L"
352 git -C "$CLONE" push -o "patch.message=Planted$L" rad HEAD:refs/patches > "$T/pushp$L.out" 2>&1 || bad "push planted $L"
353 PP=$(grep -o 'Patch [0-9a-f]\{40\}' "$T/pushp$L.out" | awk '{print $2}'); [ -n "$PP" ] || PP=no-patch-id-printed
354 python3 - "$L" > "$T/plant$L.rcpt" <<'PY'
355import sys, hashlib
356n = int(sys.argv[1])
357canon = "commit=%s\ntree=%s\ncmd=%s\nrc=0\nout=%s\nagent=bay24\n" % ("0" * 40, "0" * 40, "x" * n, "0" * 64)
358print("FORGE-EVIDENCE v1\n" + canon + "bind=" + hashlib.sha256(canon.encode()).hexdigest())
359PY
360 rad patch edit "$PP" --repo "rad:$RID" --no-announce --message "Planted$L" --message "$(cat "$T/plant$L.rcpt")" > "$T/editp$L.out" 2>&1 \
361 && ok "planted a receipt with a $L-byte cmd (bind by python sha256)" || bad "rad patch edit planted $L $(cat "$T/editp$L.out")"
362 want=commit; [ "$L" = 201 ] && want=parse
363 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PP.receipt")
364 grep -q '"verify":"REFUSED"' "$T/body" && grep -q "\"reason\":\"$want\"" "$T/body" && [ "$c" = 200 ] \
365 && ok "planted $L-byte cmd: .receipt refused for $want" || bad "planted $L-byte cmd .receipt: $c $(head -c 200 "$T/body")"
366 alive "after the .receipt of the planted $L-byte cmd"
367 c=$(code GET "/drakestapleton/forge-d-l1008/patches/$PP")
368 grep -q 'Evidence receipt' "$T/body" && grep -q 'REFUSED' "$T/body" && [ "$c" = 200 ] \
369 && ok "planted $L-byte cmd: patch page shows the refusal" || bad "planted $L-byte cmd patch page: $c"
370 alive "after the patch page of the planted $L-byte cmd"
371done
372
373# ---- filter field lengths at the edge of the 40-byte buffers qnba and qnbr (finding F1): 39 bytes
374# are saved, 40 and 41 are refused before anything is copied, an absent field is refused, and the
375# server still answers after each
376savef() { # savef AGENT REPO: POST the filter with a fresh CSRF token; prints the status
377 code GET /notifications -H "$CK" > /dev/null; CSRF=$(field csrf)
378 code POST /notifications -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "agent=$1" --data-urlencode "repo=$2"
379}
380saved() { [ "$c" = 303 ] && hdr location | grep -q 'notifications$'; }
381for f in agent repo; do
382 for L in 39 40 41; do
383 v=$(printf '%*s' "$L" '' | tr ' ' a)
384 if [ "$f" = agent ]; then c=$(savef "$v" forge-d-l1008); line="tester-one $v forge-d-l1008"; else c=$(savef bay24 "$v"); line="tester-one bay24 $v"; fi
385 if [ "$L" = 39 ]; then
386 saved && grep -q -x "$line" "$T/data/quiet/subs" && ok "$f of 39 bytes is saved (303)" || bad "$f of 39 bytes: $c"
387 else
388 [ "$c" = 400 ] && ok "NEG $f of $L bytes refused (400)" || bad "NEG $f of $L bytes: $c, want 400"
389 grep -q "$v" "$T/data/quiet/subs" && bad "NEG $f of $L bytes reached quiet/subs" || ok "NEG $f of $L bytes is not in quiet/subs"
390 fi
391 alive "after $f of $L bytes"
392 done
393done
394code GET /notifications -H "$CK" > /dev/null; CSRF=$(field csrf)
395c=$(code POST /notifications -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "repo=forge-d-l1008")
396[ "$c" = 400 ] && ok "NEG absent agent field refused (400)" || bad "NEG absent agent: $c"
397code GET /notifications -H "$CK" > /dev/null; CSRF=$(field csrf)
398c=$(code POST /notifications -H "$CK" --data-urlencode "csrf=$CSRF" --data-urlencode "agent=bay24")
399[ "$c" = 400 ] && ok "NEG absent repo field refused (400)" || bad "NEG absent repo: $c"
400alive "after the absent fields"
401
402# ---- a subscription file this server did not write (hand-edited or damaged). forged and the tool
403# write lines of at most 119 bytes with words of at most 39; a longer line is kept as it is and a
404# longer filter word is refused, and neither ends the server. Not reachable from a request.
405LONG=$(printf '%*s' 300 '' | tr ' ' b)
406printf '%s\n' "$LONG" >> "$T/data/quiet/subs"
407c=$(savef bay24 forge-d-l1008)
408saved && ok "filter saved with a 300-byte line in quiet/subs" || bad "save with a 300-byte line in quiet/subs: $c"
409[ "$(grep -c -x "$LONG" "$T/data/quiet/subs")" = 1 ] && ok "the 300-byte line is kept whole" || bad "the 300-byte line was cut or dropped"
410alive "after the 300-byte subscription line"
411printf 'zz-user %s forge-d-l1008\n' "$(printf '%*s' 100 '' | tr ' ' a)" >> "$T/data/quiet/subs"
412c=$(savef bay24 forge-d-l1008)
413[ "$c" = 500 ] && ok "NEG a 100-byte filter word in quiet/subs is refused (500), as other malformed lines are" || bad "NEG 100-byte filter word: $c, want 500"
414alive "after the 100-byte filter word"
415
416grep -q 'Connected to' "$T/node.log" && bad "node dialed out" || ok "NEG node made no outbound connection"
417stopnode
418[ -z "$(mynodes)" ] && ok "no radicle-node of this test is left running" || bad "radicle-node of this test still runs: $(mynodes | tr '\n' ' ')"
419echo "quiet: $PASS passed, $FAIL failed"
420echo "RID rad:$RID"
421[ "$FAIL" = 0 ]