interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / ipl / tests / run.sh

641 lines · 45049 bytes Raw Blame History
1#!/bin/bash
2# tests/run.sh OMC SHA2_OM CHAIN_OM: the step 1 gate. Builds bin/landrec and bin/chain with OMC (its
3# sha256 must be PIN's), then checks the record rules, a full project life and every tamper control.
4# Prints PASS/FAIL per case and ends "GATE PASS cases=N" (exit 0) or "GATE FAIL ..." (exit 1);
5# exit 2 on a pin or build error. Scratch files only; no network, no old store.
6set -u
7cd "$(dirname "$0")/.." || exit 2
8omc=${1:?OMC}; sha2=${2:?SHA2_OM}; chainom=${3:?CHAIN_OM}
9want=$(sed -n 's/^omc-sha256 //p' PIN); got=$(sha256sum "$omc" | cut -d' ' -f1)
10[ "$got" = "$want" ] || { echo "GATE FAIL omc sha256 $got, PIN wants $want" >&2; exit 2; }
11[ "$(sha256sum "$sha2" | cut -d' ' -f1)" = "$(sed -n 's/^sha2-om-sha256 //p' PIN)" ] || { echo "GATE FAIL sha2.om is not PIN's" >&2; exit 2; }
12[ "$(sha256sum "$chainom" | cut -d' ' -f1)" = "$(sed -n 's/^chain-om-sha256 //p' PIN)" ] || { echo "GATE FAIL chain.om is not PIN's" >&2; exit 2; }
13T=$(mktemp -d /tmp/ipl-gate.XXXXXX) || exit 2; trap 'rm -rf "$T"' EXIT
14mkdir "$T/bin"; cp ipl "$T/ipl" || exit 2
15"$omc" < om/landrec.om > "$T/bin/landrec" || { echo "GATE FAIL landrec.om did not compile" >&2; exit 2; }
16{ printf 'om2\n'; cat "$sha2" "$chainom"; } | "$omc" > "$T/bin/chain" || { echo "GATE FAIL chain did not compile" >&2; exit 2; }
17chmod +x "$T/bin/landrec" "$T/bin/chain" "$T/ipl"; cp "$T/bin/chain" "$T/chain"
18L=$T/bin/landrec; pass=0; fail=0; NC=0123456789abcdef0123456789abcdef
19t() { # NAME WANT_EXIT cmd...
20 local name=$1 want=$2 rc; shift 2; "$@" > "$T/out" 2> "$T/err"; rc=$?
21 if [ "$rc" = "$want" ]; then pass=$((pass + 1)); echo "PASS $name"; else fail=$((fail + 1)); echo "FAIL $name: exit $rc, want $want: $(head -c 200 "$T/err")"; fi
22}
23Z=0000000000000000000000000000000000000000; A=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
24H=$(printf x | sha256sum | cut -c1-64); FP="SHA256:bBVPM9xU/+oMCeKoeBrBmyI4XkGmp3YeBKYF/0hvArU"
25# ---- record rules
26t rec-land 0 $L land a main $Z $A $H $H r
27t rec-land-branch 0 $L land AienOS-nv feature/x-1.2 $Z $A $H $H r
28t rec-project 0 $L project interplane.io main private "$FP" - $NC
29t rec-project-alias 0 $L project a main public "$FP" rad:z6do9WDBhuSnvopDkgFdFr1yWA62 $NC
30t no-nonce-short 1 $L project a main public "$FP" - ${NC%?}
31t no-nonce-upper 1 $L project a main public "$FP" - ${NC%f}F
32t usage-project-no-nonce 2 $L project a main public "$FP" -
33printf 'interplane-land 1\nproject a\nbranch main\nold %s\nnew %s\nreceipt %s\nverdict %s\nrequest r\n' $Z $A $H $H > "$T/want"
34$L land a main $Z $A $H $H r > "$T/gotp" 2>/dev/null; t rec-canonical-bytes 0 cmp -s "$T/want" "$T/gotp"
35t no-name-dot 1 $L land .a main $Z $A $H $H r
36t no-name-enddot 1 $L land a. main $Z $A $H $H r
37t no-name-space 1 $L land "a b" main $Z $A $H $H r
38t no-name-newline 1 $L land "$(printf 'a\nb')" main $Z $A $H $H r
39t no-name-empty 1 $L land "" main $Z $A $H $H r
40t no-name-slash 1 $L land a/b main $Z $A $H $H r
41t no-branch-dash 1 $L land a -main $Z $A $H $H r
42t no-branch-dotdot 1 $L land a a..b $Z $A $H $H r
43t no-branch-slashes 1 $L land a a//b $Z $A $H $H r
44t no-branch-dotpart 1 $L land a a/.b $Z $A $H $H r
45t no-branch-trail 1 $L land a main/ $Z $A $H $H r
46t no-old-long 1 $L land a main ${Z}0 $A $H $H r
47t no-new-upper 1 $L land a main $Z ${A%a}A $H $H r
48t no-new-short 1 $L land a main $Z ${A%a} $H $H r
49t no-receipt-short 1 $L land a main $Z $A ${H%?} $H r
50t no-verdict-nonhex 1 $L land a main $Z $A $H ${H%?}g r
51t no-request-space 1 $L land a main $Z $A $H $H "r 1"
52t no-request-tab 1 $L land a main $Z $A $H $H "$(printf 'r\tx')"
53t no-visibility 1 $L project a main secret "$FP" - $NC
54t no-owner-short 1 $L project a main public SHA256:short - $NC
55t no-owner-kind 1 $L project a main public "MD5:bBVPM9xU/+oMCeKoeBrBmyI4XkGmp3YeBKYF/0hvArUab" - $NC
56t no-alias 1 $L project a main public "$FP" rad:x6do9WDBhuSnvopDkgFdFr1yWA62 $NC
57t no-alias-dashes 1 $L project a main public "$FP" -- $NC
58t usage-few 2 $L land a main $Z $A $H $H
59t usage-many 2 $L land a main $Z $A $H $H r extra
60t usage-word 2 $L nothing
61$L land .a main $Z $A $H $H r > "$T/gotp" 2>/dev/null; t refusal-prints-nothing 0 test ! -s "$T/gotp"
62t no-branch-lock 1 $L land a feature.lock $Z $A $H $H r
63t no-branch-lock-part 1 $L land a a.lock/b $Z $A $H $H r
64t rec-branch-lockish 0 $L land a a.locked/blocks $Z $A $H $H r
65N128=$(head -c 128 /dev/zero | tr '\0' a)
66t rec-name-128 0 $L land "$N128" main $Z $A $H $H r
67t rec-name-129 0 $L land "${N128}a" main $Z $A $H $H r
68t rec-branch-129 0 $L land a "${N128}a" $Z $A $H $H r
69# a request id has no size chosen here: the longest argument Linux allows passes and comes back whole
70BIG=$(head -c 131071 /dev/zero | tr '\0' r)
71t rec-request-longest 0 $L land a main $Z $A $H $H "$BIG"
72$L land a main $Z $A $H $H "$BIG" 2>/dev/null | tail -n 1 > "$T/gotp"; t rec-request-longest-whole 0 test "$(wc -c < "$T/gotp")" = 131080
73t no-request-bad-byte-late 1 $L land a main $Z $A $H $H "${BIG%r}!"
74t check-name 0 $L check name interplane.io
75t check-name-traversal 1 $L check name ../demo
76t check-name-slash 1 $L check name a/b
77t check-branch 0 $L check branch feature/x
78t check-request-newline 1 $L check request "$(printf 'x\nrequest req-1')"
79t check-unknown-rule 2 $L check colour red
80PUB68=$(head -c 68 /dev/zero | tr '\0' A)
81FP2=SHA256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
82t rec-successor 0 $L successor a "$FP" "$FP2" "$PUB68" rotate-1
83printf 'interplane-successor 1\nproject a\nold %s\nnew %s\npub %s\nreason rotate-1\n' "$FP" "$FP2" "$PUB68" > "$T/want"
84$L successor a "$FP" "$FP2" "$PUB68" rotate-1 > "$T/gotp" 2>/dev/null; t rec-successor-canonical-bytes 0 cmp -s "$T/want" "$T/gotp"
85t rec-successor-dash-reason 0 $L successor a "$FP" "$FP2" "$PUB68" -
86t no-successor-pub-short 1 $L successor a "$FP" "$FP2" "${PUB68%A}" rotate-1
87t no-successor-reason-space 1 $L successor a "$FP" "$FP2" "$PUB68" "rotate 1"
88t no-successor-old-short 1 $L successor a SHA256:short "$FP2" "$PUB68" rotate-1
89t no-successor-new-kind 1 $L successor a "$FP" "MD5:bBVPM9xU/+oMCeKoeBrBmyI4XkGmp3YeBKYF/0hvArUab" "$PUB68" -
90t check-pub 0 $L check pub "$PUB68"
91t check-pub-short 1 $L check pub "${PUB68%A}"
92t check-owner 0 $L check owner "$FP"
93t check-reason 0 $L check reason rotate-1
94t check-reason-dash 0 $L check reason -
95t check-reason-space 1 $L check reason "a b"
96t usage-successor-few 2 $L successor a "$FP" "$FP2" "$PUB68"
97t usage-successor-many 2 $L successor a "$FP" "$FP2" "$PUB68" - extra
98# ---- one project's life
99export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@example.invalid GIT_COMMITTER_NAME=t GIT_COMMITTER_EMAIL=t@example.invalid
100R=$T/root; P=$R/projects/demo; mkdir -p "$P"
101ssh-keygen -q -t ed25519 -N "" -C gate -f "$T/key"; ssh-keygen -q -t ed25519 -N "" -C other -f "$T/key2"
102FPK=$(ssh-keygen -lf "$T/key.pub" | awk '{print $2}')
103printf '* %s\n' "$FPK" > "$T/owner-pins"
104chmod 600 "$T/owner-pins"
105export IPL_OWNER_PINS="$T/owner-pins"
106git init -q -b main "$T/work"; cd "$T/work" || exit 2
107echo a > f; git add f; git commit -q -m one; C1=$(git rev-parse HEAD)
108echo b >> f; git commit -q -am two; C2=$(git rev-parse HEAD)
109echo c >> f; git commit -q -am three; C4=$(git rev-parse HEAD)
110git checkout -q -b side "$C1"; echo z > g; git add g; git commit -q -m side; C3=$(git rev-parse HEAD)
111cd - >/dev/null || exit 2
112git init -q --bare "$P/repo.git"; git --git-dir="$P/repo.git" fetch -q "$T/work" "$C1"; git --git-dir="$P/repo.git" update-ref refs/heads/main "$C1"
113echo ev > "$T/ev"; echo note > "$T/note"; echo rc > "$T/rc"; echo vd > "$T/vd"
114I=$T/ipl
115t init 0 $I init "$R" demo main public - "$T/key"
116n=$(wc -l < "$P/chain.tsv")
117t init-again-same-terms 0 $I init "$R" demo main public - "$T/key"
118t init-again-adds-nothing 0 test "$(wc -l < "$P/chain.tsv")" = "$n"
119t init-again-other-terms 2 $I init "$R" demo main private - "$T/key"
120t init-again-other-key 2 $I init "$R" demo main public - "$T/key2"
121# a name or branch longer than the chain's subject field goes in as a digest; only the file system says no
122N200=$(head -c 200 /dev/zero | tr '\0' n); mkdir -p "$R/projects/$N200"; git init -q --bare "$R/projects/$N200/repo.git"
123t long-name-init 0 $I init "$R" "$N200" main public - "$T/key"
124t long-name-subject-is-digest 0 grep -q " PROJECT [^ ]* sha256:[0-9a-f]\{64\} " "$R/projects/$N200/chain.tsv"
125t long-name-verify 0 $I verify "$R" "$N200"; rm -rf "$R/projects/$N200"
126t no-name-longer-than-file-names 2 $I verify "$R" "$(head -c 300 /dev/zero | tr '\0' n)"
127mkdir -p "$R/projects/Demo"; git init -q --bare "$R/projects/Demo/repo.git"
128t init-case-collision 2 $I init "$R" Demo main public - "$T/key"; rm -rf "$R/projects/Demo"
129t import 0 $I import "$R" demo main "$T/ev" "$T/note" import-1 "$T/key"
130t import-twice-new-request 4 $I import "$R" demo main "$T/ev" "$T/note" import-2 "$T/key"
131t land 0 $I land "$R" demo main "$C2" "$T/work" "$T/rc" "$T/vd" req-1 "$T/key"
132n=$(wc -l < "$P/chain.tsv")
133t replay-same 0 $I land "$R" demo main "$C2" "$T/work" "$T/rc" "$T/vd" req-1 "$T/key"
134t replay-adds-nothing 0 test "$(wc -l < "$P/chain.tsv")" = "$n"
135t replay-other-head 4 $I land "$R" demo main "$C3" "$T/work" "$T/rc" "$T/vd" req-1 "$T/key"
136t no-non-fast-forward 4 $I land "$R" demo main "$C3" "$T/work" "$T/rc" "$T/vd" req-2 "$T/key"
137t no-same-head 4 $I land "$R" demo main "$C2" - "$T/rc" "$T/vd" req-3 "$T/key"
138t no-unknown-commit 4 $I land "$R" demo main 1111111111111111111111111111111111111111 - "$T/rc" "$T/vd" req-4 "$T/key"
139t no-bad-request 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" "bad req" "$T/key"
140t no-other-key 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-5 "$T/key2"
141t no-missing-verdict 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/absent" req-6 "$T/key"
142t refusals-add-nothing 0 test "$(wc -l < "$P/chain.tsv")" = "$n"
143t verify 0 $I verify "$R" demo
144# ---- crafted arguments: refused before anything is read or written outside the project
145n=$(wc -l < "$P/chain.tsv"); h=$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)
146mkdir -p "$T/outside"; cp -a "$P" "$T/outside/demo"
147t no-traversal-verify 2 $I verify "$R" ../../outside/demo
148t no-traversal-land 2 $I land "$R" ../../outside/demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-t "$T/key"
149ln -s "$T/outside/demo" "$R/projects/linked"; t no-symlink-project 2 $I verify "$R" linked; rm "$R/projects/linked"
150t no-request-newline 2 $I land "$R" demo main "$C2" "$T/work" "$T/rc" "$T/vd" "$(printf 'x\nrequest req-1')" "$T/key"
151t no-source-option 2 $I land "$R" demo main "$C4" "--upload-pack=touch $T/pwned" "$T/rc" "$T/vd" req-o "$T/key"
152t no-source-remote 2 $I land "$R" demo main "$C4" "ssh://example.invalid/x" "$T/rc" "$T/vd" req-o "$T/key"
153t no-source-ext 2 $I land "$R" demo main "$C4" "ext::sh -c touch% $T/pwned" "$T/rc" "$T/vd" req-o "$T/key"
154t source-ran-nothing 0 test ! -e "$T/pwned"
155t no-branch-lock-land 2 $I land "$R" demo x.lock "$C4" "$T/work" "$T/rc" "$T/vd" req-l "$T/key"
156t no-branch-under-existing 4 $I land "$R" demo main/sub "$C4" "$T/work" "$T/rc" "$T/vd" req-d "$T/key"
157ssh-keygen -q -t rsa -b 2048 -N "" -C rsa -f "$T/rsa"
158mkdir -p "$R/projects/rsaproj"; git init -q --bare "$R/projects/rsaproj/repo.git"
159t no-rsa-owner 2 $I init "$R" rsaproj main public - "$T/rsa"
160t rsa-init-wrote-nothing 0 test ! -e "$R/projects/rsaproj/chain.tsv" -a ! -e "$R/projects/rsaproj/owner.pub"
161t no-bad-init-name 2 $I init "$R" "bad name" main public - "$T/key"
162# the owner's public key beside another private key: refused before anything is appended
163cp "$T/key2" "$T/mixed"; cp "$T/key.pub" "$T/mixed.pub"
164t no-mismatched-key-pair 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-m "$T/mixed"
165t crafted-add-nothing 0 test "$(wc -l < "$P/chain.tsv")" = "$n" -a "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$h"
166# the same request with other evidence is not the same request
167echo other > "$T/vd2"; t replay-other-verdict 4 $I land "$R" demo main "$C2" "$T/work" "$T/rc" "$T/vd2" req-1 "$T/key"
168t replay-as-import 4 $I import "$R" demo main "$T/rc" "$T/vd" req-1 "$T/key"
169# a second branch, and landings racing on one request id: one record, both answer the same
170t land-new-branch 0 $I land "$R" demo topic "$C3" "$T/work" "$T/rc" "$T/vd" req-b "$T/key"
171( $I land "$R" demo topic/x "$C3" - "$T/rc" "$T/vd" req-c "$T/key" >/dev/null 2>&1; echo $? > "$T/r1" ) &
172( $I land "$R" demo topic/x "$C3" - "$T/rc" "$T/vd" req-c "$T/key" >/dev/null 2>&1; echo $? > "$T/r2" ) & wait
173t race-topic-child-refused 0 test "$(cat "$T/r1")$(cat "$T/r2")" = 44
174git --git-dir="$P/repo.git" update-ref -d refs/heads/topic; t tamper-second-branch-removed 3 $I verify "$R" demo
175git --git-dir="$P/repo.git" update-ref refs/heads/topic "$C3"; t second-branch-back 0 $I verify "$R" demo
176# ---- tamper controls: each starts from the good copy
177cp -a "$P" "$T/good"; fresh() { rm -rf "$P"; cp -a "$T/good" "$P"; }
178sed -i '3s/main/maim/' "$P/chain.tsv"; t tamper-chain-byte 3 $I verify "$R" demo; fresh
179sed -i '5d' "$P/chain.tsv"; t tamper-record-removed 3 $I verify "$R" demo; fresh
180echo x >> "$P/records/3"; t tamper-payload 3 $I verify "$R" demo; fresh
181rm "$P/records/5"; t tamper-payload-missing 3 $I verify "$R" demo; fresh
182git --git-dir="$P/repo.git" update-ref refs/heads/main "$C1"; t tamper-ref-behind 3 $I verify "$R" demo; fresh
183git --git-dir="$P/repo.git" fetch -q "$T/work" "$C3"; git --git-dir="$P/repo.git" update-ref refs/heads/main "$C3"; t tamper-ref-elsewhere 3 $I verify "$R" demo; fresh
184cp "$T/key2.pub" "$P/owner.pub"; t tamper-owner-key 3 $I verify "$R" demo; fresh
185head -n 5 "$T/good/chain.tsv" > "$P/chain.tsv"; t tamper-signature-dropped 3 $I verify "$R" demo; fresh
186# a valid signature moved to stand for another record must not verify (records 2 and 6 are both SIG)
187cp "$P/records/2" "$T/sig2"; cp "$T/sig2" "$P/records/6"; t tamper-signature-swapped 3 $I verify "$R" demo; fresh
188# a well-formed record 6 whose signature is over other bytes, by another key or for another purpose
189FPK=$(ssh-keygen -lf "$T/key.pub" | awk '{print $2}')
190lmsg() { awk -F'\t' -v n="$1" 'NR==n {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$T/good/chain.tsv"; }
191forge() { head -n 5 "$T/good/chain.tsv" > "$P/chain.tsv"; "$T/chain" add "$P/chain.tsv" SIG "$FPK" 5 < "$1" >/dev/null; cp "$1" "$P/records/6"; }
192printf 'deadbeef\n' > "$T/m1"; ssh-keygen -q -Y sign -n interplane -f "$T/key" "$T/m1" 2>/dev/null
193forge "$T/m1.sig"; t tamper-signature-over-other-bytes 3 $I verify "$R" demo; fresh
194lmsg 3 > "$T/m0"; ssh-keygen -q -Y sign -n interplane -f "$T/key" "$T/m0" 2>/dev/null
195forge "$T/m0.sig"; t tamper-signature-of-another-record 3 $I verify "$R" demo; fresh
196lmsg 5 > "$T/m2"; ssh-keygen -q -Y sign -n interplane -f "$T/key2" "$T/m2" 2>/dev/null
197forge "$T/m2.sig"; t tamper-signature-by-other-key 3 $I verify "$R" demo; fresh
198lmsg 5 > "$T/m3"; ssh-keygen -q -Y sign -n other-purpose -f "$T/key" "$T/m3" 2>/dev/null
199forge "$T/m3.sig"; t tamper-signature-for-other-purpose 3 $I verify "$R" demo; fresh
200lmsg 5 > "$T/m4"; ssh-keygen -q -Y sign -n interplane -f "$T/key" "$T/m4" 2>/dev/null
201forge "$T/m4.sig"; t control-honest-resigned-record-verifies 0 $I verify "$R" demo; fresh
202# the last line is not covered by a later record: its actor is checked on its own
203sed -i "\$s#$FPK#SHA256:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA#" "$P/chain.tsv"; t tamper-last-actor 3 $I verify "$R" demo; fresh
204# an owner-signed record that is not canonical (no new head) must not verify
205printf 'interplane-land 1\nproject demo\nbranch main\nold %s\nreceipt %s\nverdict %s\nrequest bad\n' "$C2" $H $H > "$T/badpay"
206head -n 6 "$T/good/chain.tsv" > "$P/chain.tsv"; "$T/chain" add "$P/chain.tsv" LAND "$FPK" main < "$T/badpay" >/dev/null; cp "$T/badpay" "$P/records/7"
207awk -F'\t' 'NR==7 {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$P/chain.tsv" > "$T/m5"; ssh-keygen -q -Y sign -n interplane -f "$T/key" "$T/m5" 2>/dev/null
208"$T/chain" add "$P/chain.tsv" SIG "$FPK" 7 < "$T/m5.sig" >/dev/null; cp "$T/m5.sig" "$P/records/8"
209t tamper-signed-but-malformed 3 $I verify "$R" demo; fresh
210# a whole project copied under another name is not that project
211cp -a "$T/good" "$R/projects/demo2"; t tamper-copied-under-other-name 3 $I verify "$R" demo2; rm -rf "$R/projects/demo2"
212# ---- interruptions: only the same request, by the owner, finishes one; nothing else gets in
213t land-2 0 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
214n=$(wc -l < "$P/chain.tsv"); cp -a "$P" "$T/done"; redo() { rm -rf "$P"; cp -a "$T/done" "$P"; }
215# (a) signed, branch not moved yet
216git --git-dir="$P/repo.git" update-ref refs/heads/main "$C2"
217t pending-ref-seen 3 $I verify "$R" demo
218t pending-ref-blocks-other 3 $I land "$R" demo main "$C4" - "$T/rc" "$T/vd" req-8 "$T/key"
219t pending-ref-not-for-other-key 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key2"
220t pending-ref-still-behind 0 test "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$C2"
221t pending-ref-finished 0 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
222t finish-adds-nothing 0 test "$(wc -l < "$P/chain.tsv")" = "$n"
223# (b) recorded, not signed yet, branch not moved
224redo; sed -i '$d' "$P/chain.tsv"; rm "$P/records/$n"; git --git-dir="$P/repo.git" update-ref refs/heads/main "$C2"
225t pending-sig-seen 3 $I verify "$R" demo
226t pending-sig-blocks-other 3 $I land "$R" demo main "$C4" - "$T/rc" "$T/vd" req-8 "$T/key"
227t pending-sig-not-for-other-key 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key2"
228t pending-sig-not-for-mixed-pair 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/mixed"
229t pending-sig-untouched 0 test "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$C2" -a "$(wc -l < "$P/chain.tsv")" = "$((n - 1))"
230t pending-sig-finished 0 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
231t pending-sig-result 0 test "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$C4" -a "$(wc -l < "$P/chain.tsv")" = "$n"
232# (c) recorded, the payload file did not reach records/ but is still in staging
233redo; mkdir -p "$P/staging"; mv "$P/records/$((n - 1))" "$P/staging/req-req-7.payload"
234t payload-lost-seen 3 $I verify "$R" demo
235t payload-lost-healed 0 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
236# (d) recorded, the payload is gone for good: refused whole, nothing moves
237redo; rm "$P/records/$((n - 1))"; git --git-dir="$P/repo.git" update-ref refs/heads/main "$C2"
238t payload-gone-refused 3 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
239t payload-gone-untouched 0 test "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$C2"
240# an old request repeated after later landings answers and moves nothing
241redo; t replay-historical 0 $I land "$R" demo main "$C2" "$T/work" "$T/rc" "$T/vd" req-1 "$T/key"
242t replay-historical-moved-nothing 0 test "$(git --git-dir="$P/repo.git" rev-parse refs/heads/main)" = "$C4" -a "$(wc -l < "$P/chain.tsv")" = "$n"
243# init interrupted before its signature is finished by the same init, not by another key
244mkdir -p "$R/projects/half"; git init -q --bare "$R/projects/half/repo.git"
245t half-init 0 $I init "$R" half main private - "$T/key"
246sed -i '$d' "$R/projects/half/chain.tsv"; rm "$R/projects/half/records/2"
247t half-init-seen 3 $I verify "$R" half
248t half-init-not-other-key 2 $I init "$R" half main private - "$T/key2"
249t half-init-not-other-terms 2 $I init "$R" half main public - "$T/key"
250t half-init-finished 0 $I init "$R" half main private - "$T/key"
251# ---- second review round
252ref() { git --git-dir="$P/repo.git" rev-parse -q --verify "refs/heads/$1" 2>/dev/null; }
253lines() { wc -l < "$P/chain.tsv"; }
254# PENDING_REF is not finished by the owner's public key beside another private key
255redo; git --git-dir="$P/repo.git" update-ref refs/heads/main "$C2"
256t pending-ref-not-for-mixed-pair 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/mixed"
257t pending-ref-mixed-untouched 0 test "$(ref main)" = "$C2"
258# an annotated tag is not a commit, even though it leads to one
259redo; TAG=$(cd "$T/work" && git tag -a -m t vt "$C3" && git rev-parse vt); n=$(lines)
260t no-tag-object 4 $I land "$R" demo tagbr "$TAG" "$T/work" "$T/rc" "$T/vd" req-tag "$T/key"
261t tag-added-nothing 0 test "$(lines)" = "$n" -a -z "$(ref tagbr)"
262t tag-commit-control 0 $I land "$R" demo tagbr "$C3" "$T/work" "$T/rc" "$T/vd" req-tag2 "$T/key"
263# a landing stopped at each write boundary: seen, closed to others, finished by the same request
264C5=$(cd "$T/work" && git checkout -q main && echo d >> f && git commit -q -am four && git rev-parse HEAD)
265for s in after-stage mid-record after-record after-sig; do
266 redo; n=$(lines)
267 IPL_TEST_STOP=$s t "stop-$s" 9 $I land "$R" demo main "$C5" "$T/work" "$T/rc" "$T/vd" req-f "$T/key"
268 t "stop-$s-branch-not-moved" 0 test "$(ref main)" = "$C4"
269 if [ $s = after-stage ]; then t "stop-$s-nothing-recorded" 0 $I verify "$R" demo
270 else
271 t "stop-$s-seen" 3 $I verify "$R" demo
272 t "stop-$s-closed-to-others" 3 $I land "$R" demo topic "$C4" "$T/work" "$T/rc" "$T/vd" req-g "$T/key"
273 t "stop-$s-not-for-other-key" 2 $I land "$R" demo main "$C5" "$T/work" "$T/rc" "$T/vd" req-f "$T/key2"
274 fi
275 t "stop-$s-finished" 0 $I land "$R" demo main "$C5" "$T/work" "$T/rc" "$T/vd" req-f "$T/key"
276 t "stop-$s-result" 0 test "$(ref main)" = "$C5" -a "$(lines)" = "$((n + 2))" -a -z "$(ls "$P/staging")"
277 t "stop-$s-catalog-follows" 0 grep -q "^demo [0-9a-f]\{64\} main $C5 public$" "$R/catalog.tsv"
278done
279# stopped after the branch moved, before the catalog: the project verifies, the same request publishes
280redo; n=$(lines); $I catalog "$R" >/dev/null 2>&1
281IPL_TEST_STOP=after-ref t stop-after-ref 9 $I land "$R" demo main "$C5" "$T/work" "$T/rc" "$T/vd" req-f "$T/key"
282t stop-after-ref-verifies 0 $I verify "$R" demo
283t stop-after-ref-catalog-behind 1 grep -q "^demo [0-9a-f]\{64\} main $C5 public$" "$R/catalog.tsv"
284t stop-after-ref-finished 0 $I land "$R" demo main "$C5" "$T/work" "$T/rc" "$T/vd" req-f "$T/key"
285t stop-after-ref-result 0 test "$(lines)" = "$((n + 2))" -a -z "$(ls "$P/staging")"
286t stop-after-ref-catalog-follows 0 grep -q "^demo [0-9a-f]\{64\} main $C5 public$" "$R/catalog.tsv"
287# a longer branch part than the chain's subject field lands; one the file system cannot hold is refused
288redo; B200=$(head -c 200 /dev/zero | tr '\0' b)
289t long-branch-lands 0 $I land "$R" demo "x/$B200" "$C3" "$T/work" "$T/rc" "$T/vd" req-lb "$T/key"
290t long-branch-verifies 0 $I verify "$R" demo
291t no-branch-part-too-long 2 $I land "$R" demo "y/$(head -c 252 /dev/zero | tr '\0' b)" "$C3" "$T/work" "$T/rc" "$T/vd" req-lb2 "$T/key"
292# a folder part may be as long as a file name; only the last part needs room for git's .lock
293t long-branch-folder-part 0 $I land "$R" demo "$(head -c 255 /dev/zero | tr '\0' f)/x" "$C3" "$T/work" "$T/rc" "$T/vd" req-lb3 "$T/key"
294mkdir -p "$R/projects/lb"; git init -q --bare "$R/projects/lb/repo.git"
295t no-default-branch-too-long 2 $I init "$R" lb "$(head -c 252 /dev/zero | tr '\0' b)" public - "$T/key"
296t long-default-refusal-wrote-nothing 0 test ! -e "$R/projects/lb/owner.pub" -a ! -e "$R/projects/lb/chain.tsv"; rm -rf "$R/projects/lb"
297# name, branch and request all as long as an argument can be: the payload holds them whole
298t rec-all-longest 0 $L land "$BIG" "$BIG" $Z $A $H $H "$BIG"
299$L land "$BIG" "$BIG" $Z $A $H $H "$BIG" 2>/dev/null > "$T/gotp"; t rec-all-longest-whole 0 test "$(wc -c < "$T/gotp")" = 393493
300# init stopped at each point: only the key that started it goes on
301for s in after-stage mid-record after-record after-sig; do
302 Q=$R/projects/i-$s; mkdir -p "$Q"; git init -q --bare "$Q/repo.git"
303 IPL_TEST_STOP=$s t "init-stop-$s" 9 $I init "$R" "i-$s" main public - "$T/key"
304 cp "$Q/owner.pub" "$T/owner.before"
305 t "init-stop-$s-not-other-key" 2 $I init "$R" "i-$s" main public - "$T/key2"
306 t "init-stop-$s-owner-kept" 0 cmp -s "$T/owner.before" "$Q/owner.pub"
307 t "init-stop-$s-not-mixed-pair" 2 $I init "$R" "i-$s" main public - "$T/mixed"
308 t "init-stop-$s-owner-still-kept" 0 cmp -s "$T/owner.before" "$Q/owner.pub"
309 t "init-stop-$s-not-other-terms" 2 $I init "$R" "i-$s" main private - "$T/key"
310 t "init-stop-$s-finished" 0 $I init "$R" "i-$s" main public - "$T/key"
311 t "init-stop-$s-two-records" 0 test "$(wc -l < "$Q/chain.tsv")" = 2
312 t "init-stop-$s-in-catalog" 0 grep -q "^i-$s " "$R/catalog.tsv"
313 rm -rf "$Q"
314done
315# a first init with someone's public key beside another private key: refused, and nothing is left behind
316Q=$R/projects/mix1; mkdir -p "$Q"; git init -q --bare "$Q/repo.git"
317t no-fresh-init-with-mixed-pair 2 $I init "$R" mix1 main public - "$T/mixed"
318t mixed-init-left-nothing 0 test ! -e "$Q/owner.pub" -a ! -e "$Q/staging/init.payload" -a ! -e "$Q/chain.tsv"; rm -rf "$Q"
319# a first chain line that was never finished: the owner's same init cuts it and completes, in one call
320Q=$R/projects/torn1; mkdir -p "$Q"; git init -q --bare "$Q/repo.git"
321IPL_TEST_STOP=after-stage t torn-first-setup 9 $I init "$R" torn1 main public - "$T/key"
322printf '1\t2026' > "$Q/chain.tsv"
323t torn-first-line-finished 0 $I init "$R" torn1 main public - "$T/key"
324t torn-first-line-two-records 0 test "$(wc -l < "$Q/chain.tsv")" = 2; rm -rf "$Q"
325# a repair on the owner's call is said out loud, even when the request itself is then refused
326redo; printf '99\tgarbage' >> "$P/chain.tsv"
327t torn-then-altered-request 4 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd2" req-7 "$T/key"
328cp "$T/err" "$T/err.kept"; t repair-was-announced 0 grep -q "repaired: cut a torn last line" "$T/err.kept"
329# a torn last line is never a record: the owner's next call cuts it, nobody else's does
330redo; printf '99\tgarbage' >> "$P/chain.tsv"
331t torn-seen 3 $I verify "$R" demo
332t torn-not-cut-for-other-key 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key2"
333t torn-still-there 0 grep -q garbage "$P/chain.tsv"
334t torn-cut-for-owner 0 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key"
335t torn-gone 0 test "$(lines)" = "$(wc -l < "$T/done/chain.tsv")" -a -z "$(grep garbage "$P/chain.tsv")"
336# an init that created the chain file and wrote nothing starts over
337mkdir -p "$R/projects/e"; git init -q --bare "$R/projects/e/repo.git"; : > "$R/projects/e/chain.tsv"
338t empty-chain-init 0 $I init "$R" e main public - "$T/key"; rm -rf "$R/projects/e"
339# a staged payload is put in place only for the owner
340redo; n=$(lines); mkdir -p "$P/staging"; mv "$P/records/$((n - 1))" "$P/staging/x"
341t staged-not-restored-for-other-key 2 $I land "$R" demo main "$C4" "$T/work" "$T/rc" "$T/vd" req-7 "$T/key2"
342t staged-still-staged 0 test ! -e "$P/records/$((n - 1))"
343# the original import request, repeated after later landings, answers and adds nothing
344redo; n=$(lines)
345t import-replay-after-landings 0 $I import "$R" demo main "$T/ev" "$T/note" import-1 "$T/key"
346t import-replay-added-nothing 0 test "$(lines)" = "$n" -a "$(ref main)" = "$C4"
347# the project id is the digest of the signed record 1 payload
348t id-is-payload-digest 0 test "$($I verify "$R" demo | cut -d' ' -f3)" = "$(sha256sum < "$P/records/1" | cut -c1-64)"
349# git settings in the caller's environment do not reach the store
350GIT_DIR=/nonexistent GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=core.bare GIT_CONFIG_VALUE_0=nonsense GIT_WORK_TREE=/nonexistent t env-not-inherited 0 $I verify "$R" demo
351# a project whose repository is a link is no project, for verify and for the catalog alike
352mkdir -p "$R/projects/lnk"; ln -s "$P/repo.git" "$R/projects/lnk/repo.git"
353t no-linked-repository 2 $I verify "$R" lnk
354$I catalog "$R" > /dev/null 2> "$T/caterr"; t catalog-leaves-out-linked 0 grep -q "lnk is not a project" "$T/caterr"; rm -rf "$R/projects/lnk"
355# a linked project folder is left out, and nothing behind the link is touched
356mkdir -p "$T/elsewhere"; echo keep > "$T/elsewhere/catalog.row"; ln -s "$T/elsewhere" "$R/projects/away"
357$I catalog "$R" > /dev/null 2>&1; t linked-folder-untouched 0 test "$(cat "$T/elsewhere/catalog.row")" = keep; rm "$R/projects/away"
358redo
359# two catalog runs at once: one whole catalog, no leftovers
360( $I catalog "$R" >/dev/null 2>&1 ) & ( $I catalog "$R" >/dev/null 2>&1 ) & wait
361cp "$R/catalog.tsv" "$T/err.cat"; t catalog-race-whole 0 test "$(wc -l < "$R/catalog.tsv")" = 2 -a -z "$(ls "$R" | grep 'catalog.tsv\.')"
362[ "$(wc -l < "$R/catalog.tsv")" = 2 ] || { echo "catalog was:"; cut -f1,4 "$R/catalog.tsv"; ls "$R" "$R/projects"; }
363rm -rf "$R/projects/rsaproj" "$R/projects/half"; t catalog 0 $I catalog "$R"
364t catalog-row 0 grep -q "^demo [0-9a-f]\{64\} main $C4 public$" "$R/catalog.tsv"
365echo x >> "$P/records/1"; t catalog-leaves-out-broken 3 $I catalog "$R"; t catalog-empty 0 test ! -s "$R/catalog.tsv"
366# ---- owner-key handover. Throwaway ed25519 keys in this temp store only; never ~/.ssh, never the live store.
367HR=$T/hroot; HP=$HR/projects/demo; mkdir -p "$HP"
368ssh-keygen -q -t ed25519 -N "" -C ho0 -f "$T/h0"
369ssh-keygen -q -t ed25519 -N "" -C ho1 -f "$T/h1"
370ssh-keygen -q -t ed25519 -N "" -C ho2 -f "$T/h2"
371FP0=$(ssh-keygen -lf "$T/h0.pub" | awk '{print $2}')
372FP1=$(ssh-keygen -lf "$T/h1.pub" | awk '{print $2}')
373FP2=$(ssh-keygen -lf "$T/h2.pub" | awk '{print $2}')
374BLOB0=$(awk '{print $2}' "$T/h0.pub")
375BLOB2=$(awk '{print $2}' "$T/h2.pub")
376write_pin() { printf '* %s\n' "$1" > "$T/owner-pins"; chmod 600 "$T/owner-pins"; }
377write_pin "$FP0"
378git init -q --bare "$HP/repo.git"
379git --git-dir="$HP/repo.git" fetch -q "$T/work" "$C1"
380git --git-dir="$HP/repo.git" update-ref refs/heads/main "$C1"
381t ho-init 0 $I init "$HR" demo main public - "$T/h0"
382t ho-import 0 $I import "$HR" demo main "$T/ev" "$T/note" h-import-1 "$T/h0"
383t ho-land-before 0 $I land "$HR" demo main "$C2" "$T/work" "$T/rc" "$T/vd" h-req-1 "$T/h0"
384n=$(wc -l < "$HP/chain.tsv")
385t ho-pre-history-verifies 0 $I verify "$HR" demo
386t ho-legacy-no-successor 0 test "$(grep -c $'\tSUCCESSOR\t' "$HP/chain.tsv" || true)" = 0
387own0=$(sha256sum < "$HP/owner.pub" | cut -c1-64)
388mkdir -p "$T/legacy-root/projects"
389cp -a "$HP" "$T/legacy-root/projects/demo"
390t ho-legacy-copy-still-verifies 0 $I verify "$T/legacy-root" demo
391t ho-handover 0 $I handover "$HR" demo "$T/h0" "$T/h1.pub" rotate-1
392cp "$T/err" "$T/err.handover"
393t ho-pin-line-printed 0 grep -q "ipl: pin: demo $FP1" "$T/err.handover"
394t ho-pin-file-updated 0 grep -q "$FP1" "$T/owner-pins"
395t ho-adds-two-records 0 test "$(wc -l < "$HP/chain.tsv")" = "$((n + 2))"
396t ho-owner-pub-unchanged 0 test "$(sha256sum < "$HP/owner.pub" | cut -c1-64)" = "$own0"
397t ho-verify-after 0 $I verify "$HR" demo
398$I verify "$HR" demo > "$T/ok"
399t ho-ok-current-owner 0 test "$(awk '{print $7}' "$T/ok")" = "$FP1"
400t ho-ok-chain-seq 0 test "$(awk '{print $8}' "$T/ok")" = "$(wc -l < "$HP/chain.tsv")"
401t ho-ok-chain-head 0 test "$(awk '{print $9}' "$T/ok")" = "$(tail -n 1 "$HP/chain.tsv" | tr -d '\n' | sha256sum | cut -c1-64)"
402t ho-expect-new 0 $I verify "$HR" demo --expect-owner "$FP1"
403t ho-expect-old-fails 3 $I verify --expect-owner "$FP0" "$HR" demo
404printf '* %s\n' "$FP0" > "$T/pin-old"; chmod 600 "$T/pin-old"
405IPL_OWNER_PINS="$T/pin-old" t ho-legacy-with-old-pin 0 $I verify "$T/legacy-root" demo
406nleg=$(wc -l < "$T/legacy-root/projects/demo/chain.tsv")
407t ho-fork-expect-new-fails 3 $I verify "$T/legacy-root" demo --expect-owner "$FP1"
408t ho-fork-land-expect-fails 3 $I land "$T/legacy-root" demo main "$C4" "$T/work" "$T/rc" "$T/vd" h-fork-1 "$T/h0" --expect-owner "$FP1"
409t ho-fork-land-expect-adds-nothing 0 test "$(wc -l < "$T/legacy-root/projects/demo/chain.tsv")" = "$nleg"
410# required: a pin file (successor FP) catches a pre-handover fork and an attacker re-hand, without --expect-owner
411t ho-fork-pin-verify 3 $I verify "$T/legacy-root" demo
412t ho-fork-pin-catalog 3 $I catalog "$T/legacy-root"
413cp -a "$T/legacy-root" "$T/rehand-root"
414printf '* %s\n' "$FP0" > "$T/attacker-pins"; chmod 600 "$T/attacker-pins"
415IPL_OWNER_PINS="$T/attacker-pins" t ho-attacker-rehand 0 $I handover "$T/rehand-root" demo "$T/h0" "$T/h2.pub" attack
416t ho-rehand-fork-pin-verify 3 $I verify "$T/rehand-root" demo
417t ho-rehand-fork-pin-catalog 3 $I catalog "$T/rehand-root"
418# pin file inside ROOT, group-writable, missing, and a disagreeing --expect-owner
419printf '* %s\n' "$FP1" > "$HR/owner-pins"; chmod 600 "$HR/owner-pins"
420IPL_OWNER_PINS="$HR/owner-pins" t ho-pin-inside-root 3 $I verify "$HR" demo
421rm -f "$HR/owner-pins"
422printf '* %s\n' "$FP1" > "$T/pin-gw"; chmod 660 "$T/pin-gw"
423IPL_OWNER_PINS="$T/pin-gw" t ho-pin-group-writable 3 $I verify "$HR" demo
424IPL_OWNER_PINS="$T/no-such-pins" t ho-pin-missing 3 $I verify "$HR" demo
425t ho-pin-disagree 3 $I verify "$HR" demo --expect-owner "$FP0"
426# catalog drops a project whose current owner is not the pin
427write_pin "$FP1"
428CR=$T/catpin
429for spec in "p-ok:$T/h1" "p-bad:$T/h0"; do
430 name=${spec%%:*}; key=${spec#*:}
431 mkdir -p "$CR/projects/$name"
432 git init -q --bare "$CR/projects/$name/repo.git"
433 git --git-dir="$CR/projects/$name/repo.git" fetch -q "$T/work" "$C1"
434 git --git-dir="$CR/projects/$name/repo.git" update-ref refs/heads/main "$C1"
435 $I init "$CR" "$name" main public - "$key" >/dev/null
436done
437t ho-catalog-drops-mismatch 3 $I catalog "$CR"
438cp "$T/err" "$T/err.catpin"
439t ho-catalog-drops-mismatch-reason 0 grep -q "p-bad: current owner is $FP0, not $FP1" "$T/err.catpin"
440t ho-catalog-keeps-pinned 0 grep -q "^p-ok " "$CR/catalog.tsv"
441t ho-catalog-omits-mismatch 0 test "$(grep -c "^p-bad" "$CR/catalog.tsv" || true)" = 0
442mkdir -p "$T/ipltmp"
443TMPDIR="$T/ipltmp" $I verify "$HR" demo >/dev/null
444t ho-verify-cleans-temp 0 test -z "$(ls -A "$T/ipltmp")"
445n=$(wc -l < "$HP/chain.tsv")
446t ho-idempotent 0 $I handover "$HR" demo "$T/h0" "$T/h1.pub" rotate-1
447t ho-idempotent-adds-nothing 0 test "$(wc -l < "$HP/chain.tsv")" = "$n"
448cp -a "$HP" "$T/ho-after1"
449t ho-new-key-land 0 $I land "$HR" demo main "$C4" "$T/work" "$T/rc" "$T/vd" h-req-2 "$T/h1" --expect-owner "$FP1"
450t ho-new-key-land-verifies 0 $I verify "$HR" demo
451n=$(wc -l < "$HP/chain.tsv")
452t ho-land-expect-old-fails 3 $I land "$HR" demo main "$C5" "$T/work" "$T/rc" "$T/vd" h-req-expect "$T/h1" --expect-owner "$FP0"
453t ho-land-expect-old-adds-nothing 0 test "$(wc -l < "$HP/chain.tsv")" = "$n"
454n=$(wc -l < "$HP/chain.tsv")
455t ho-old-key-land-rejected 2 $I land "$HR" demo main "$C4" "$T/work" "$T/rc" "$T/vd" h-req-3 "$T/h0"
456t ho-old-key-land-adds-nothing 0 test "$(wc -l < "$HP/chain.tsv")" = "$n"
457# swapping owner.pub to the successor key must still fail: owner.pub is the key record 1 names
458cp "$T/h1.pub" "$HP/owner.pub"
459t ho-tamper-owner-to-successor 3 $I verify "$HR" demo
460cp "$T/ho-after1/owner.pub" "$HP/owner.pub"
461t ho-owner-restored-after-tamper 0 $I verify "$HR" demo
462# a forged SUCCESSOR not signed by the current key fails verify (actor is the attacker)
463rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
464"$L" successor demo "$FP1" "$FP2" "$BLOB2" forged > "$T/hpay"
465head -n "$(wc -l < "$HP/chain.tsv")" "$HP/chain.tsv" > "$T/hchain.bak"
466"$T/chain" add "$HP/chain.tsv" SUCCESSOR "$FP2" "$FP2" < "$T/hpay" >/dev/null
467hs=$(wc -l < "$HP/chain.tsv")
468cp "$T/hpay" "$HP/records/$hs"
469awk -F'\t' -v n="$hs" 'NR==n {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$HP/chain.tsv" > "$T/hmsg"
470ssh-keygen -q -Y sign -n interplane -f "$T/h2" "$T/hmsg" 2>/dev/null
471"$T/chain" add "$HP/chain.tsv" SIG "$FP2" "$hs" < "$T/hmsg.sig" >/dev/null
472cp "$T/hmsg.sig" "$HP/records/$((hs + 1))"
473t ho-forged-handover-fails 3 $I verify "$HR" demo
474# P1: a SUCCESSOR whose actor is the current key, but whose SIG is by another key, must fail on the signature
475rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
476"$L" successor demo "$FP1" "$FP2" "$BLOB2" forged-sig > "$T/hpay2"
477"$T/chain" add "$HP/chain.tsv" SUCCESSOR "$FP1" "$FP2" < "$T/hpay2" >/dev/null
478hs=$(wc -l < "$HP/chain.tsv")
479cp "$T/hpay2" "$HP/records/$hs"
480awk -F'\t' -v n="$hs" 'NR==n {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$HP/chain.tsv" > "$T/hmsg2"
481ssh-keygen -q -Y sign -n interplane -f "$T/h2" "$T/hmsg2" 2>/dev/null
482"$T/chain" add "$HP/chain.tsv" SIG "$FP1" "$hs" < "$T/hmsg2.sig" >/dev/null
483cp "$T/hmsg2.sig" "$HP/records/$((hs + 1))"
484t ho-forged-successor-bad-sig 3 $I verify "$HR" demo
485cp "$T/err" "$T/err.forgedsig"
486t ho-forged-successor-bad-sig-reason 0 grep -q "does not verify" "$T/err.forgedsig"
487# an old-key LAND after a real handover, even if well-formed and signed by the old key, fails verify
488rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
489rd=$(sha256sum < "$T/rc" | cut -c1-64); vd=$(sha256sum < "$T/vd" | cut -c1-64)
490"$L" land demo main "$C2" "$C4" "$rd" "$vd" forged-land > "$T/lpay"
491"$T/chain" add "$HP/chain.tsv" LAND "$FP0" main < "$T/lpay" >/dev/null
492hs=$(wc -l < "$HP/chain.tsv")
493cp "$T/lpay" "$HP/records/$hs"
494awk -F'\t' -v n="$hs" 'NR==n {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$HP/chain.tsv" > "$T/lmsg"
495ssh-keygen -q -Y sign -n interplane -f "$T/h0" "$T/lmsg" 2>/dev/null
496"$T/chain" add "$HP/chain.tsv" SIG "$FP0" "$hs" < "$T/lmsg.sig" >/dev/null
497cp "$T/lmsg.sig" "$HP/records/$((hs + 1))"
498# the branch also needs to move for a would-be-honest land; either way it must fail
499git --git-dir="$HP/repo.git" fetch -q "$T/work" "$C4"
500git --git-dir="$HP/repo.git" update-ref refs/heads/main "$C4"
501t ho-forged-old-key-land-after-fails 3 $I verify "$HR" demo
502# two sequential handovers
503rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
504t ho-second-handover 0 $I handover "$HR" demo "$T/h1" "$T/h2.pub" rotate-2
505t ho-second-verifies 0 $I verify "$HR" demo
506t ho-third-key-land 0 $I land "$HR" demo main "$C4" "$T/work" "$T/rc" "$T/vd" h-req-4 "$T/h2"
507t ho-third-key-land-verifies 0 $I verify "$HR" demo
508n=$(wc -l < "$HP/chain.tsv")
509t ho-first-key-after-two-rejected 2 $I land "$HR" demo main "$C5" "$T/work" "$T/rc" "$T/vd" h-req-5 "$T/h0"
510t ho-second-key-after-two-rejected 2 $I land "$HR" demo main "$C5" "$T/work" "$T/rc" "$T/vd" h-req-6 "$T/h1"
511t ho-retired-keys-add-nothing 0 test "$(wc -l < "$HP/chain.tsv")" = "$n"
512t ho-third-key-lands-on 0 $I land "$HR" demo main "$C5" "$T/work" "$T/rc" "$T/vd" h-req-5 "$T/h2"
513t ho-after-two-verifies 0 $I verify "$HR" demo
514# --all: two projects, one call
515AR=$T/allroot
516for name in p1 p2; do
517 mkdir -p "$AR/projects/$name"
518 git init -q --bare "$AR/projects/$name/repo.git"
519 git --git-dir="$AR/projects/$name/repo.git" fetch -q "$T/work" "$C1"
520 git --git-dir="$AR/projects/$name/repo.git" update-ref refs/heads/main "$C1"
521 $I init "$AR" "$name" main public - "$T/h0" >/dev/null
522done
523write_pin "$FP0"
524t ho-all 0 $I handover "$AR" --all "$T/h0" "$T/h1.pub" rotate-all
525t ho-all-pin-updated 0 grep -q "$FP1" "$T/owner-pins"
526t ho-all-p1-verifies 0 $I verify "$AR" p1
527t ho-all-p2-verifies 0 $I verify "$AR" p2
528t ho-all-p1-has-successor 0 grep -q $'\tSUCCESSOR\t' "$AR/projects/p1/chain.tsv"
529t ho-all-p2-has-successor 0 grep -q $'\tSUCCESSOR\t' "$AR/projects/p2/chain.tsv"
530t ho-all-idempotent 0 $I handover "$AR" --all "$T/h0" "$T/h1.pub" rotate-all
531# a5: single-project handovers leave NAME lines; --all must rewrite them, not leave them stale
532A5=$T/a5root
533for name in demo other; do
534 mkdir -p "$A5/projects/$name"
535 git init -q --bare "$A5/projects/$name/repo.git"
536 git --git-dir="$A5/projects/$name/repo.git" fetch -q "$T/work" "$C1"
537 git --git-dir="$A5/projects/$name/repo.git" update-ref refs/heads/main "$C1"
538 $I init "$A5" "$name" main public - "$T/h0" >/dev/null
539done
540write_pin "$FP0"
541t ho-a5-single-demo 0 $I handover "$A5" demo "$T/h0" "$T/h1.pub" a5-demo
542t ho-a5-single-other 0 $I handover "$A5" other "$T/h0" "$T/h1.pub" a5-other
543cp -a "$A5" "$T/a5-at-h1"
544t ho-a5-all 0 $I handover "$A5" --all "$T/h1" "$T/h2.pub" a5-all
545t ho-a5-pin-demo 0 grep -q "^demo $FP2$" "$T/owner-pins"
546t ho-a5-pin-other 0 grep -q "^other $FP2$" "$T/owner-pins"
547t ho-a5-pin-no-stale-demo 0 test "$(grep -c "^demo $FP1$" "$T/owner-pins" || true)" = 0
548t ho-a5-pin-no-stale-other 0 test "$(grep -c "^other $FP1$" "$T/owner-pins" || true)" = 0
549t ho-a5-demo-verifies 0 $I verify "$A5" demo
550t ho-a5-other-verifies 0 $I verify "$A5" other
551t ho-a5-catalog 0 $I catalog "$A5"
552t ho-a5-catalog-rows 0 test "$(wc -l < "$A5/catalog.tsv")" = 2
553t ho-a5-frozen-h1-verify 3 $I verify "$T/a5-at-h1" demo
554t ho-a5-frozen-h1-other 3 $I verify "$T/a5-at-h1" other
555t ho-a5-frozen-h1-catalog 3 $I catalog "$T/a5-at-h1"
556# a3: a pre-existing per-name line that matches * must be rewritten by --all, not left at the old key
557A3=$T/a3root
558for name in a1 c3; do
559 mkdir -p "$A3/projects/$name"
560 git init -q --bare "$A3/projects/$name/repo.git"
561 git --git-dir="$A3/projects/$name/repo.git" fetch -q "$T/work" "$C1"
562 git --git-dir="$A3/projects/$name/repo.git" update-ref refs/heads/main "$C1"
563 $I init "$A3" "$name" main public - "$T/h0" >/dev/null
564done
565printf '* %s\n' "$FP0" > "$T/owner-pins"
566printf 'c3 %s\n' "$FP0" >> "$T/owner-pins"
567chmod 600 "$T/owner-pins"
568t ho-a3-all 0 $I handover "$A3" --all "$T/h0" "$T/h1.pub" a3-all
569t ho-a3-c3-rewritten 0 grep -q "^c3 $FP1$" "$T/owner-pins"
570t ho-a3-c3-not-stale 0 test "$(grep -c "^c3 $FP0$" "$T/owner-pins" || true)" = 0
571t ho-a3-star-rewritten 0 grep -q "^\* $FP1$" "$T/owner-pins"
572t ho-a3-c3-verifies 0 $I verify "$A3" c3
573t ho-a3-a1-verifies 0 $I verify "$A3" a1
574t ho-a3-catalog 0 $I catalog "$A3"
575t ho-a3-catalog-rows 0 test "$(wc -l < "$A3/catalog.tsv")" = 2
576# --all preflight is all-or-nothing: a mixed-owner store writes nothing
577MR=$T/mixroot
578for spec in "a1:$T/h0" "b2:$T/h2" "c3:$T/h0"; do
579 name=${spec%%:*}; key=${spec#*:}
580 mkdir -p "$MR/projects/$name"
581 git init -q --bare "$MR/projects/$name/repo.git"
582 git --git-dir="$MR/projects/$name/repo.git" fetch -q "$T/work" "$C1"
583 git --git-dir="$MR/projects/$name/repo.git" update-ref refs/heads/main "$C1"
584 $I init "$MR" "$name" main public - "$key" >/dev/null
585done
586na1=$(wc -l < "$MR/projects/a1/chain.tsv"); nb2=$(wc -l < "$MR/projects/b2/chain.tsv"); nc3=$(wc -l < "$MR/projects/c3/chain.tsv")
587write_pin "$FP0"
588t ho-all-preflight-mixed 2 $I handover "$MR" --all "$T/h0" "$T/h1.pub" rotate-mix
589cp "$T/err" "$T/err.preflight"
590t ho-all-preflight-announced 0 grep -q "preflight failed; nothing was recorded" "$T/err.preflight"
591t ho-all-preflight-names-b2 0 grep -q "b2: current owner is $FP2, not $FP0" "$T/err.preflight"
592t ho-all-preflight-a1-untouched 0 test "$(wc -l < "$MR/projects/a1/chain.tsv")" = "$na1" -a "$(grep -c $'\tSUCCESSOR\t' "$MR/projects/a1/chain.tsv" || true)" = 0
593t ho-all-preflight-b2-untouched 0 test "$(wc -l < "$MR/projects/b2/chain.tsv")" = "$nb2"
594t ho-all-preflight-c3-untouched 0 test "$(wc -l < "$MR/projects/c3/chain.tsv")" = "$nc3" -a "$(grep -c $'\tSUCCESSOR\t' "$MR/projects/c3/chain.tsv" || true)" = 0
595# a retired key cannot become the owner again
596rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
597write_pin "$FP1"
598n=$(wc -l < "$HP/chain.tsv")
599t ho-refuse-prior-owner 2 $I handover "$HR" demo "$T/h1" "$T/h0.pub" rotate-back
600cp "$T/err" "$T/err.prior"
601t ho-refuse-prior-owner-reason 0 grep -q "already an owner" "$T/err.prior"
602t ho-refuse-prior-owner-adds-nothing 0 test "$(wc -l < "$HP/chain.tsv")" = "$n"
603# a crafted h0->h1->h0 chain, even honestly signed by h1, does not verify
604"$L" successor demo "$FP1" "$FP0" "$BLOB0" rotate-back > "$T/hback"
605"$T/chain" add "$HP/chain.tsv" SUCCESSOR "$FP1" "$FP0" < "$T/hback" >/dev/null
606hs=$(wc -l < "$HP/chain.tsv")
607cp "$T/hback" "$HP/records/$hs"
608awk -F'\t' -v n="$hs" 'NR==n {printf "interplane-sig 1\nkind %s\nactor %s\nsubject %s\npayload %s\nprev %s\n",$3,$4,$5,$6,$7}' "$HP/chain.tsv" > "$T/hbackmsg"
609ssh-keygen -q -Y sign -n interplane -f "$T/h1" "$T/hbackmsg" 2>/dev/null
610"$T/chain" add "$HP/chain.tsv" SIG "$FP1" "$hs" < "$T/hbackmsg.sig" >/dev/null
611cp "$T/hbackmsg.sig" "$HP/records/$((hs + 1))"
612t ho-verify-refuses-returned-owner 3 $I verify "$HR" demo
613cp "$T/err" "$T/err.back"
614t ho-verify-refuses-returned-owner-reason 0 grep -q "already an owner" "$T/err.back"
615# interrupted handover at each write point is finished by the same keys; after-sig no-op clears staging
616for s in after-stage mid-record after-record after-sig; do
617 rm -rf "$HP"; cp -a "$T/ho-after1" "$HP"
618 write_pin "$FP1"
619 n=$(wc -l < "$HP/chain.tsv")
620 IPL_TEST_STOP=$s t "ho-stop-$s" 9 $I handover "$HR" demo "$T/h1" "$T/h2.pub" rotate-stop
621 n1=$(wc -l < "$HP/chain.tsv")
622 if [ "$s" = after-stage ]; then
623 t "ho-stop-$s-nothing-recorded" 0 $I verify "$HR" demo
624 t "ho-stop-$s-lines" 0 test "$n1" = "$n"
625 elif [ "$s" = after-sig ]; then
626 t "ho-stop-$s-verifies" 0 $I verify "$HR" demo
627 t "ho-stop-$s-lines" 0 test "$n1" = "$((n + 2))"
628 else
629 t "ho-stop-$s-seen" 3 $I verify "$HR" demo
630 t "ho-stop-$s-lines" 0 test "$n1" = "$((n + 1))"
631 fi
632 if [ "$s" = after-record ]; then
633 t ho-stop-not-other-key 2 $I handover "$HR" demo "$T/h0" "$T/h2.pub" rotate-stop
634 fi
635 t "ho-stop-$s-finished" 0 $I handover "$HR" demo "$T/h1" "$T/h2.pub" rotate-stop
636 t "ho-stop-$s-result" 0 test "$(wc -l < "$HP/chain.tsv")" = "$((n + 2))" -a -z "$(ls "$HP/staging" 2>/dev/null)"
637 t "ho-stop-$s-final-verifies" 0 $I verify "$HR" demo
638done
639
640echo "tools: $(git --version), $(ssh -V 2>&1), $(bash --version | head -1), $(sha256sum --version | head -1), $(flock --version), $(awk -W version 2>&1 | head -1), $(sed --version | head -1), $(cmp --version | head -1), $(getconf --version | head -1)"
641if [ "$fail" = 0 ]; then echo "GATE PASS cases=$pass"; else echo "GATE FAIL pass=$pass fail=$fail"; exit 1; fi