interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / tools / doi / tests / run.sh

489 lines · 24708 bytes Raw Blame History
1#!/usr/bin/env bash
2# Offline only. A pty simulates author typing ONLY against fixture-curl; no network/deposit.
3# Full module: bash tools/doi/tests/run.sh [OMC]
4set -eu
5set +x
6export LC_ALL=C
7D=$(cd "$(dirname "$0")/.." && pwd)
8T=$(mktemp -d)
9trap 'rc=$?; if [ "$rc" = 0 ]; then rm -rf -- "$T"; else echo "DOI failure fixtures: $T" >&2; fi' EXIT
10trap 'exit 1' HUP INT TERM
11unset ZENODO_TOKEN
12export ZENODO_TOKEN=fixture-sentinel-not-a-real-token
13OMC=${1:-$HOME/work/flux/omega-mv/seed/out/omc}
14bash "$D/build.sh" "$OMC" "$T/out"
15export DOI_ENGINE=$T/out/doi
16PASS=0
17ok() { PASS=$((PASS+1)); printf 'PASS %s\n' "$*"; }
18fail() { printf 'FAIL %s\n' "$*" >&2; exit 1; }
19newcase() {
20 C=$T/$1; mkdir -p "$C/repo" "$C/api" "$C/work"
21 cp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json"
22 printf 'CC-BY-4.0\n' > "$C/repo/LICENSE"
23 printf 'paper pdf\n' > "$C/repo/paper.pdf"
24 cp "$D/tests/fixtures/"{create,upload,publish}.json "$C/api/"
25}
26# Capture statuses, keep fixtures/work/repo beneath T so the final scan covers all files.
27plain() {
28 rc=0
29 TMPDIR=$C/work setsid --wait sh "$D/doi.sh" "$C/repo" --fixture "$C/api" "$@" \
30 </dev/null > "$C/stdout" 2> "$C/stderr" || rc=$?
31}
32pty() {
33 answer=$1; shift
34 rc=0
35 # strace records execve argv (not environment contents), checking actual process arguments.
36 printf '%s\n' "$answer" | TMPDIR=$C/work script -qefc \
37 "strace -f -yy -s 65536 -e trace=execve,fsync,renameat2,prlimit64 -o '$C/exec.log' sh '$D/doi.sh' '$C/repo' --fixture '$C/api' $*" \
38 /dev/null > "$C/stdout" 2> "$C/stderr" || rc=$?
39}
40no_requests() {
41 [ "$rc" -ne 0 ] || fail "$1 accepted"
42 [ ! -s "$C/api/requests.log" ] || fail "$1 sent a request"
43 ok "$1 refuses before request"
44}
45no_publish() {
46 if grep -q '/actions/publish auth=ok' "$C/api/requests.log"; then fail "$1 published"; fi
47 grep -q 'Draft kept, not published' "$C/stdout" || fail "$1 missing decline notice"
48 grep -q '"doi": "10.5072/zenodo.123"' "$C/repo/.zenodo.json" || fail "$1 lost reserved DOI"
49 ok "$1 keeps draft"
50}
51# Only fixture source copies acquire gates; production has no fault-injection environment path.
52compile_fixture() {
53 "$OMC" < "$1.om" > "$1"
54 chmod 755 "$1"
55}
56for stage in before-input before-output; do
57 sed "/^note startup gate $stage /a r = killpid int getpid 19" "$D/doi.om" > "$T/gate-$stage.om"
58 compile_fixture "$T/gate-$stage"
59done
60# A deliberately broken supervisor swallows nonzero keeper completion and never closes output.
61sed '/^if ne rd32 st 0 0$/a while true bound 9223372036854775807\nr = waitio fd 1 10000000\nend' \
62 "$T/gate-before-input.om" > "$T/ignore-death.om"
63compile_fixture "$T/ignore-death"
64for stage in before-input before-output ignore-death; do
65 for action in kill continue; do
66 [ "$stage:$action" != ignore-death:continue ] || continue
67 newcase "startup-$stage-$action"
68 engine=$T/gate-$stage
69 [ "$stage" != ignore-death ] || engine=$T/ignore-death
70 DOI_ENGINE=$engine TMPDIR=$C/work timeout -k 2s 8s sh "$D/doi.sh" "$C/repo" --fixture "$C/api" \
71 </dev/null >"$C/stdout" 2>"$C/stderr" &
72 runner=$!
73 child=
74 for ((i=0;i<500;i++)); do
75 for pid in $(pgrep -f "^$engine serve " || :); do
76 state=$(ps -o stat= -p "$pid" || :)
77 case "$state" in T*) child=$pid; break;; esac
78 done
79 [ -z "$child" ] || break
80 sleep .01
81 done
82 [ -n "$child" ] || fail "$stage never reached deterministic stop gate"
83 supervisor=$(ps -o ppid= -p "$child" | tr -d ' ')
84 grep -Eq '^Max core file size +0 +0 +bytes[[:space:]]*$' "/proc/$child/limits" || fail 'keeper RLIMIT_CORE not zero'
85 [ ! -s "$C/api/requests.log" ] || fail "$stage sent request while stopped"
86 if [ "$action" = kill ]; then kill -KILL "$child"; else kill -CONT "$child"; fi
87 rc=0; wait "$runner" || rc=$?
88 if [ "$stage" = ignore-death ]; then
89 [ "$rc" = 124 ] || fail "death-ignore control not caught by timeout oracle: $rc"
90 [ ! -s "$C/api/requests.log" ] || fail 'death-ignore control sent request'
91 ok 'C7 negative control death-ignore caught by bounded-exit oracle'
92 elif [ "$action" = kill ]; then
93 [ "$rc" != 124 ] && [ "$rc" != 137 ] || fail "$stage launcher timed out"
94 no_requests "C7 $stage keeper death"
95 grep -Fxq 'doi: keeper exited unsuccessfully; no upload permitted' "$C/stderr" || fail "$stage death diagnosis"
96 else
97 [ "$rc" = 0 ] || fail "$stage gated control failed: $rc"
98 [ "$(wc -l < "$C/api/requests.log")" = 2 ] || fail "$stage gated control requests"
99 grep -q '"doi": "10.5072/zenodo.123"' "$C/repo/.zenodo.json" || fail "$stage gated control DOI"
100 ok "C7 $stage identical gate continued successfully; C8 keeper core limit zero"
101 fi
102 # Both actual PIDs were captured before release; a zombie also counts as a leftover.
103 for pid in "$child" "$supervisor"; do
104 [ ! -e "/proc/$pid" ] || fail "$stage leftover child/supervisor $pid"
105 done
106 done
107done
108
109newcase happy
110pty yes
111[ "$rc" = 0 ] || fail "happy status $rc"
112# Check before the existing-DOI rerun below overwrites this case stdout.
113# Selection is intentionally independent of git: the normal fixture PDF is untracked.
114grep -q 'including untracked PDFs' "$T/happy/stdout" || fail 'missing pre-upload selection warning'
115ok 'all matching PDFs including untracked explicitly disclosed'
116
117cat > "$C/expected.log" <<'EOF'
118POST https://sandbox.zenodo.org/api/deposit/depositions auth=ok
119PUT https://sandbox.zenodo.org/api/files/abc-123/paper.pdf auth=ok
120POST https://sandbox.zenodo.org/api/deposit/depositions/123/actions/publish auth=ok
121EOF
122cmp "$C/expected.log" "$C/api/requests.log" || fail 'sandbox default/request order'
123ok 'sandbox default and exact request order'
124cmp "$C/repo/paper.pdf" "$C/api/uploaded-paper.pdf" || fail 'uploaded PDF bytes'
125ok 'uploaded PDF bytes'
126# Independent byte oracles: the original metadata body and all original fields remain verbatim.
127{ printf '{"metadata":{"prereserve_doi":true,\n'; sed '1d;$d' "$D/tests/fixtures/metadata.json"; printf '}}\n'; } > "$C/body.expected"
128cmp "$C/body.expected" "$C/api/request-create.json" || fail 'request metadata'
129ok 'request JSON preserves metadata'
130{ head -n 1 "$D/tests/fixtures/metadata.json"; printf ' "doi": "10.5072/zenodo.123",\n'; tail -n +2 "$D/tests/fixtures/metadata.json"; } > "$C/meta.expected"
131cmp "$C/meta.expected" "$C/repo/.zenodo.json" || fail 'rewrite metadata'
132ok 'DOI insertion preserves all metadata bytes'
133grep -q 'Reserved DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'reserved DOI output'
134grep -q 'Published DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'published DOI output'
135ok 'reserved/published DOI output'
136# Rerun cannot reserve another deposit now that the local paper has a DOI.
137cp "$C/api/requests.log" "$C/before.log"
138plain
139[ "$rc" -ne 0 ] || fail 'existing DOI accepted'
140cmp "$C/before.log" "$C/api/requests.log" || fail 'existing DOI created duplicate'
141ok 'existing DOI prevents duplicate draft'
142newcase production
143sed -i 's/sandbox\.zenodo/zenodo/g;s/10\.5072/10.5281/g' "$C/api/"{create,publish}.json
144pty yes --production
145[ "$rc" = 0 ] || fail 'production fixture status'
146grep -q '^POST https://zenodo.org/api/deposit/depositions auth=ok$' "$C/api/requests.log" || fail 'production base'
147grep -q 'Published DOI: 10.5281/zenodo.123' "$C/stdout" || fail 'production DOI'
148ok 'production needs explicit flag'
149grep -Eq 'prlimit64\(0, RLIMIT_CORE, \{rlim_cur=0, rlim_max=0\}' "$T/happy/exec.log" || fail 'launcher did not set both core limits to zero'
150ok 'C8 launcher RLIMIT_CORE zero before children; system collectors explicitly excluded'
151for answer in no Yes 'yes '; do
152 newcase "decline-$PASS"
153 pty "$answer"
154 [ "$rc" = 0 ] || fail 'decline status'
155 no_publish "declined '$answer'"
156done
157newcase no-tty
158plain
159[ "$rc" = 0 ] || fail 'no tty status'
160no_publish 'stdin cannot consent without tty'
161grep -q 'no terminal' "$C/stderr" || fail 'no tty explanation'
162# Every required field, creator name and conditional publication type are checked locally.
163for field in title creators description license upload_type publication_type; do
164 newcase "missing-$field"
165 sed -i "/^ \"$field\":/d" "$C/repo/.zenodo.json"
166 plain
167 no_requests "missing $field"
168 grep -q "$field" "$C/stderr" || fail "missing $field diagnosis"
169done
170newcase bad-creators
171sed -i 's/"name": "Researcher, A."/"name": ""/' "$C/repo/.zenodo.json"
172plain; no_requests 'empty creator name'
173newcase no-license
174rm "$C/repo/LICENSE"
175plain; no_requests 'missing LICENSE'
176newcase empty-license
177: > "$C/repo/LICENSE"
178plain; no_requests 'empty LICENSE'
179newcase duplicate-file
180plain --source "$C/repo/paper.pdf"
181no_requests 'duplicate upload basename'
182newcase no-pdf
183rm "$C/repo/paper.pdf"
184plain; no_requests 'missing PDF'
185newcase invalid-json
186printf '{"title":' > "$C/repo/.zenodo.json"
187plain; no_requests 'malformed JSON'
188newcase escaped-key
189sed -i 's/"title"/"ti\\u0074le"/' "$C/repo/.zenodo.json"
190plain; no_requests 'escaped metadata key'
191newcase missing-token
192unset ZENODO_TOKEN
193plain; no_requests 'missing token'
194export ZENODO_TOKEN=fixture-sentinel-not-a-real-token
195newcase bad-token
196ZENODO_TOKEN='bad"token'; export ZENODO_TOKEN
197plain; no_requests 'config injection token'
198export ZENODO_TOKEN=fixture-sentinel-not-a-real-token
199newcase source
200printf 'source bytes\n' > "$C/repo/source.tar.gz"
201printf '{"key":"source.tar.gz","size":13}\n' > "$C/api/upload-source.tar.gz.json"
202pty yes "--source '$C/repo/source.tar.gz'"
203[ "$rc" = 0 ] || fail 'source fixture'
204cmp "$C/repo/source.tar.gz" "$C/api/uploaded-source.tar.gz" || fail 'source bytes'
205ok 'optional source upload'
206# Each URL must match the selected HTTPS origin. Every link is checked before any PUT.
207for change in other-host http lookalike publish-host html-host prod-mismatch; do
208 newcase "url-$change"
209 case "$change" in
210 other-host) sed -i 's@https://sandbox.zenodo.org/api/files/@https://interplane.io/api/files/@' "$C/api/create.json";;
211 http) sed -i 's@https://sandbox.zenodo.org/api/files/@http://sandbox.zenodo.org/api/files/@' "$C/api/create.json";;
212 lookalike) sed -i 's@https://sandbox.zenodo.org/api/files/@https://sandbox.zenodo.org.evil.example/api/files/@' "$C/api/create.json";;
213 publish-host) sed -i 's@https://sandbox.zenodo.org/api/deposit/@https://interplane.io/api/deposit/@' "$C/api/create.json";;
214 html-host) sed -i 's@https://sandbox.zenodo.org/deposit/@https://interplane.io/deposit/@' "$C/api/create.json";;
215 prod-mismatch) :;;
216 esac
217 if [ "$change" = prod-mismatch ]; then plain --production; else plain; fi
218 [ "$rc" -ne 0 ] || fail "$change URL accepted"
219 [ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail "$change sent upload/publish"
220 cmp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json" || fail "$change modified metadata"
221 grep -Fq "doi: refused: Zenodo's answer points outside " "$C/stderr" || fail "$change URL diagnosis"
222 ok "$change URL refused before upload"
223done
224newcase upload-error
225printf '500\n' > "$C/api/upload.code"
226cp "$D/tests/fixtures/error.json" "$C/api/upload.json"
227plain
228[ "$rc" -ne 0 ] || fail 'HTTP error accepted'
229[ "$(wc -l < "$C/api/requests.log")" = 2 ] || fail 'upload failure retried/published'
230grep -q 'Missing title' "$C/stderr" || fail 'API field error lost'
231ok 'HTTP error reported without retry/publish'
232newcase acknowledgement
233printf '{"key":"wrong.pdf","size":10}\n' > "$C/api/upload.json"
234plain
235[ "$rc" -ne 0 ] || fail 'wrong upload acknowledgement accepted'
236[ "$(wc -l < "$C/api/requests.log")" = 2 ] || fail 'bad upload published'
237ok 'wrong upload acknowledgement refused'
238# An untrusted checkout must not exfiltrate a different local file through an input symlink.
239for input in paper metadata license source dangling; do
240 newcase "symlink-$input"
241 case "$input" in
242 paper) mv "$C/repo/paper.pdf" "$C/outside"; ln -s "$C/outside" "$C/repo/paper.pdf";;
243 metadata) mv "$C/repo/.zenodo.json" "$C/outside"; ln -s "$C/outside" "$C/repo/.zenodo.json";;
244 license) mv "$C/repo/LICENSE" "$C/outside"; ln -s "$C/outside" "$C/repo/LICENSE";;
245 source) printf 'private source\n' > "$C/outside"; ln -s "$C/outside" "$C/repo/source.tar.gz";;
246 dangling) rm "$C/repo/paper.pdf"; ln -s "$C/missing" "$C/repo/paper.pdf";;
247 esac
248 if [ "$input" = source ]; then plain --source "$C/repo/source.tar.gz"; else plain; fi
249 no_requests "symlink $input"
250done
251# Swap an original path to a symlink immediately AFTER Omega copied its descriptor.
252# Curl must upload the frozen private PDF, never reopen that now-hostile path.
253newcase replace-after-snapshot
254printf 'unrelated local secret\n' > "$C/outside"
255cat > "$C/api/create.hook" <<EOF
256rm -- "$C/repo/paper.pdf"
257ln -s "$C/outside" "$C/repo/paper.pdf"
258EOF
259plain
260[ "$rc" = 0 ] || fail 'replacement after snapshot status'
261[ -L "$C/repo/paper.pdf" ] || fail 'replacement control never ran'
262printf 'paper pdf\n' > "$C/expected.pdf"
263cmp "$C/expected.pdf" "$C/api/uploaded-paper.pdf" || fail 'curl reopened replaced original'
264ok 'curl uses private descriptor snapshot after original path replacement'
265newcase stale-temp
266printf 'do not overwrite\n' > "$C/repo/.zenodo.json.doi-new"
267plain
268[ "$rc" -ne 0 ] || fail 'existing temp overwritten'
269[ "$(cat "$C/repo/.zenodo.json.doi-new")" = 'do not overwrite' ] || fail 'temp content changed'
270no_requests 'stale exclusive metadata replacement temp'
271# No fixture hook: the happy path above is the distinct unchanged-metadata/root control.
272# A source ancestor symlink must be rejected, not canonicalized by the shell.
273newcase source-ancestor
274mkdir "$C/private"
275printf 'private source\n' > "$C/private/source.tar.gz"
276ln -s "$C/private" "$C/src"
277plain --source "$C/src/source.tar.gz"
278no_requests 'source ancestor symlink'
279grep -Fxq "doi: not a non-empty regular file, or a symlink (refused): $C/src/source.tar.gz" "$C/stderr" || fail 'source ancestor diagnosis'
280
281# Swap a real ancestor immediately before the Omega supervisor begins resolving it.
282newcase source-ancestor-swap
283mkdir "$C/src" "$C/private"
284printf 'public source\n' > "$C/src/source.tar.gz"
285printf 'private source\n' > "$C/private/source.tar.gz"
286export DOI_REAL_ENGINE=$DOI_ENGINE RACE_SRC=$C/src RACE_PRIVATE=$C/private
287cat > "$C/engine-wrapper" <<'EOF'
288#!/bin/sh
289if [ "$1" = serve ]; then
290 mv "$RACE_SRC" "$RACE_SRC.old"
291 ln -s "$RACE_PRIVATE" "$RACE_SRC"
292fi
293exec "$DOI_REAL_ENGINE" "$@"
294EOF
295chmod 755 "$C/engine-wrapper"
296export DOI_ENGINE=$C/engine-wrapper
297plain --source "$C/src/source.tar.gz"
298export DOI_ENGINE=$DOI_REAL_ENGINE
299unset DOI_REAL_ENGINE RACE_SRC RACE_PRIVATE
300no_requests 'source ancestor swapped before snapshot'
301grep -Fxq "doi: not a non-empty regular file, or a symlink (refused): $C/src/source.tar.gz" "$C/stderr" || fail 'source swapped ancestor diagnosis'
302
303newcase repository-ancestor-symlink
304mv "$C/repo" "$C/original"
305ln -s "$C/original" "$C/repo"
306plain
307no_requests 'repository root symlink'
308grep -Fxq "doi: cannot anchor path without following symlinks: $C/repo" "$C/stderr" || fail 'root symlink diagnosis'
309
310# A refusal for an unrelated reason must not satisfy the path-specific oracle.
311newcase refusal-oracle-unrelated
312mkdir "$C/private"
313printf 'source bytes\n' > "$C/private/source.tar.gz"
314ln -s "$C/private" "$C/src"
315ZENODO_TOKEN='bad"token' plain --source "$C/src/source.tar.gz"
316no_requests 'unrelated token refusal control'
317grep -Fq 'doi: ZENODO_TOKEN holds a byte' "$C/stderr" || fail 'unrelated refusal control did not reach token check'
318if grep -Fxq "doi: not a non-empty regular file, or a symlink (refused): $C/src/source.tar.gz" "$C/stderr"; then
319 fail 'path diagnosis oracle accepted unrelated refusal'
320fi
321ok 'source refusal diagnosis oracle rejects unrelated token failure'
322
323# Repository pathname replacement must not redirect write-back to a different repository.
324newcase root-swap
325mkdir "$C/replacement"
326cp "$C/repo/.zenodo.json" "$C/replacement/.zenodo.json"
327cat > "$C/api/create.hook" <<EOF
328mv "$C/repo" "$C/original"
329mv "$C/replacement" "$C/repo"
330EOF
331plain
332[ "$rc" = 0 ] || fail 'anchored root swap failed'
333cmp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json" || fail 'replacement repo overwritten'
334grep -q '"doi": "10.5072/zenodo.123"' "$C/original/.zenodo.json" || fail 'retained repo not updated'
335cmp "$C/original/paper.pdf" "$C/api/uploaded-paper.pdf" || fail 'root swap changed upload'
336ok 'repo directory identity retained through write-back'
337
338for timing in inplace replace; do
339 newcase "metadata-drift-$timing"
340 sed 's/A paper/A newer paper/' "$C/repo/.zenodo.json" > "$C/newer.json"
341 if [ "$timing" = inplace ]; then
342 printf 'cat "%s" > "%s"\n' "$C/newer.json" "$C/repo/.zenodo.json" > "$C/api/create.hook"
343 else
344 printf 'cp "%s" "%s"; mv "%s" "%s"\n' "$C/newer.json" "$C/repo/edit.json" "$C/repo/edit.json" "$C/repo/.zenodo.json" > "$C/api/create.hook"
345 fi
346 plain
347 [ "$rc" != 0 ] || fail 'metadata drift accepted'
348 cmp "$C/newer.json" "$C/repo/.zenodo.json" || fail 'newer metadata lost'
349 [ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail 'drift uploaded or published'
350 grep -q 'Reserved DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'drift lost DOI'
351 grep -q 'metadata changed since submission' "$C/stderr" || fail 'drift diagnosis'
352 ok "metadata $timing drift preserves edit"
353done
354
355# Delay the first file-fsync return: comparison has finished but exchange has not begun.
356# The displaced inode is checked AFTER exchange; two exchanges prove rollback was exercised.
357for mutation in no replace inplace candidate; do
358 newcase "just-before-rename-$mutation"
359 sed 's/A paper/A latest paper/' "$C/repo/.zenodo.json" > "$C/newer.json"
360 premode=$(stat -c %a "$C/repo/.zenodo.json")
361 rc=0
362 TMPDIR=$C/work setsid -w strace -f -yy -o "$C/race.trace" \
363 -e trace=fsync,renameat2 -e inject=fsync:delay_exit=3s:when=1 \
364 sh "$D/doi.sh" "$C/repo" --fixture "$C/api" </dev/null >"$C/stdout" 2>"$C/stderr" &
365 runner=$!
366 if [ "$mutation" != no ]; then
367 # Bounded test wait, not a runtime timing hook. Fail rather than silently miss the window.
368 seen=no
369 for ((i=0;i<500;i++)); do
370 if grep -q '"doi":' "$C/repo/.zenodo.json.doi-new" 2>/dev/null; then seen=yes; break; fi
371 sleep .01
372 done
373 [ "$seen" = yes ] || fail 'did not reach candidate-fsync window'
374 [ "$(stat -c %a "$C/repo/.zenodo.json.doi-new")" = 600 ] || fail 'candidate exposed before exchange'
375 if [ "$mutation" = candidate ]; then
376 printf '{"title":"attacker"}\n' > "$C/repo/evil.json"
377 mv "$C/repo/evil.json" "$C/repo/.zenodo.json.doi-new"
378 elif [ "$mutation" = inplace ]; then
379 cat "$C/newer.json" > "$C/repo/.zenodo.json"
380 else
381 cp "$C/newer.json" "$C/repo/edit.json"
382 mv "$C/repo/edit.json" "$C/repo/.zenodo.json"
383 fi
384 fi
385 wait "$runner" || rc=$?
386 if [ "$mutation" = candidate ]; then
387 [ "$rc" != 0 ] || fail 'substituted candidate accepted'
388 cmp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json" || fail 'original metadata not restored'
389 [ "$(grep -c RENAME_EXCHANGE "$C/race.trace")" = 2 ] || fail 'candidate rollback not exercised'
390 [ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail 'substituted candidate uploaded'
391 grep -q 'candidate was replaced' "$C/stderr" || fail 'candidate substitution diagnosis'
392 grep -q 'Reserved DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'candidate substitution lost DOI'
393 grep -q attacker "$C/repo/.zenodo.json.doi-new" || fail 'substituted file not left for inspection'
394 elif [ "$mutation" != no ]; then
395 [ "$rc" != 0 ] || fail 'just-before-rename drift accepted'
396 cmp "$C/newer.json" "$C/repo/.zenodo.json" || fail 'just-before-rename edit lost'
397 [ "$(grep -c RENAME_EXCHANGE "$C/race.trace")" = 2 ] || fail 'exchange rollback not exercised'
398 [ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail 'late drift uploaded'
399 grep -q 'metadata changed since submission' "$C/stderr" || fail 'late drift diagnosis'
400 else
401 [ "$rc" = 0 ] || fail 'delayed exchange negative control failed'
402 grep -q '"doi": "10.5072/zenodo.123"' "$C/repo/.zenodo.json" || fail 'control DOI not installed'
403 [ "$(stat -c %a "$C/repo/.zenodo.json")" = "$premode" ] || fail 'original mode not restored'
404 grep -Eq 'fsync\([0-9]+<[^>]+/repo>\).* = 0' "$C/race.trace" || fail 'retained repo directory not fsynced'
405 fi
406 ok "just-before-rename mutation=$mutation"
407done
408
409# Persist only fsync and protocol writes from strace's output pipe. Never persist headers/token.
410# This observes the keeper's actual write(1<pipe:...>, "SAVED\n", 6), not CLI stdout.
411sync_trace() {
412 rc=0
413 TMPDIR=$C/work setsid -w strace -f -yy -s 128 \
414 -o "|grep -E 'fsync\\(|write\\(1<pipe:.*(READY|SAVED)' > '$C/sync.trace'" \
415 -e trace=fsync,write -e inject=fsync:error=EIO:when=3 \
416 sh "$D/doi.sh" "$C/repo" --fixture "$C/api" \
417 </dev/null >"$C/stdout" 2>"$C/stderr" || rc=$?
418}
419saved_absent() {
420 if grep -Eq 'write\(1<pipe:.*SAVED' "$1"; then
421 echo 'no-SAVED oracle: keeper protocol emitted SAVED' >&2
422 return 1
423 fi
424}
425newcase directory-sync-failure
426sync_trace
427[ "$rc" != 0 ] || fail 'directory fsync failure ignored'
428[ "$(grep -c 'INJECTED' "$C/sync.trace")" = 1 ] || fail 'expected exactly one injected fsync'
429grep 'INJECTED' "$C/sync.trace" | grep -Fq "<$C/repo>" || fail 'injected fsync was not the retained directory'
430if grep 'INJECTED' "$C/sync.trace" | grep -Fq '.zenodo.json'; then fail 'injected fsync hit the candidate'; fi
431grep -Eq 'write\(1<pipe:.*READY' "$C/sync.trace" || fail 'protocol trace did not observe READY'
432saved_absent "$C/sync.trace" || fail 'directory fsync failure reported saved'
433[ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail 'directory fsync failure allowed upload/publish'
434ok 'directory fsync failure prevents saved flow (actual keeper write channel)'
435# Erroneous SAVED followed by nonzero exit must be rejected by the no-SAVED oracle itself,
436# independently of the status/no-upload checks (which this mutant still satisfies).
437sed '/^r = fsync int get gfd 0$/{n; /^if lt r 0$/a say tsaved
438}' "$D/doi.om" > "$T/false-saved.om"
439compile_fixture "$T/false-saved"
440newcase false-saved-nonzero
441real_engine=$DOI_ENGINE
442export DOI_ENGINE=$T/false-saved
443sync_trace
444export DOI_ENGINE=$real_engine
445[ "$rc" != 0 ] || fail 'false-SAVED mutant did not fail'
446[ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail 'false-SAVED mutant uploaded'
447grep 'INJECTED' "$C/sync.trace" | grep -Fq "<$C/repo>" || fail 'false-SAVED injection not directory'
448if saved_absent "$C/sync.trace" 2>"$C/oracle.err"; then fail 'C9 no-SAVED oracle missed erroneous SAVED'; fi
449grep -Fxq 'no-SAVED oracle: keeper protocol emitted SAVED' "$C/oracle.err" || fail 'C9 wrong oracle failure'
450ok 'C9 false-SAVED plus nonzero exit caught by protocol-write oracle'
451
452newcase concurrent-writer
453mkdir "$C/api2"
454cp "$D/tests/fixtures/"{create,upload,publish}.json "$C/api2/"
455cat > "$C/api/create.hook" <<EOF
456rc=0
457sh "$D/doi.sh" "$C/repo" --fixture "$C/api2" >"$C/writer2.out" 2>"$C/writer2.err" || rc=\$?
458[ "\$rc" != 0 ]
459EOF
460plain
461[ "$rc" = 0 ] || fail 'serialized writer control failed'
462[ ! -e "$C/api2/requests.log" ] || fail 'concurrent writer sent request'
463grep -q 'another DOI writer' "$C/writer2.err" || fail 'writer lock not exercised'
464ok 'exclusive directory lock serializes tool writers'
465
466for invalid in bucket-dot bucket-nested publish-dot publish-id publish-extra html-dot encoded-dot; do
467 newcase "endpoint-$invalid"
468 case "$invalid" in
469 bucket-dot) sed -i 's@files/abc-123@files/../deposit@' "$C/api/create.json";;
470 bucket-nested) sed -i 's@files/abc-123@files/a/b@' "$C/api/create.json";;
471 publish-dot) sed -i 's@depositions/123/actions@depositions/123/../123/actions@' "$C/api/create.json";;
472 publish-id) sed -i 's@depositions/123/actions@depositions/456/actions@' "$C/api/create.json";;
473 publish-extra) sed -i 's@depositions/123/actions@depositions/123/extra/actions@' "$C/api/create.json";;
474 html-dot) sed -i 's@deposit/123"@deposit/../123"@' "$C/api/create.json";;
475 encoded-dot) sed -i 's@files/abc-123@files/%2e%2e@' "$C/api/create.json";;
476 esac
477 plain
478 [ "$rc" != 0 ] || fail "$invalid accepted"
479 [ "$(wc -l < "$C/api/requests.log")" = 1 ] || fail "$invalid uploaded/published"
480 cmp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json" || fail "$invalid rewrote metadata"
481 grep -Fq "doi: refused: Zenodo's answer points outside " "$C/stderr" || fail "$invalid endpoint diagnosis"
482 ok "$invalid endpoint rejected"
483done
484
485# No real token entered the suite. This sentinel must not occur in any file whatsoever,
486# including response fixtures, shell output, curl argv logs and strace's actual execve argv.
487if grep -rFl -- "$ZENODO_TOKEN" "$T" > /dev/null; then fail 'token on disk or argv'; fi
488ok 'token absent from disk, logs and actual argv'
489printf 'DOI FIXTURE PASS checks=%s (no network or deposits)\n' "$PASS"