interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / tools / doi / tests / run.sh
| 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] |
| 4 | set -eu |
| 5 | set +x |
| 6 | export LC_ALL=C |
| 7 | D=$(cd "$(dirname "$0")/.." && pwd) |
| 8 | T=$(mktemp -d) |
| 9 | trap 'rc=$?; if [ "$rc" = 0 ]; then rm -rf -- "$T"; else echo "DOI failure fixtures: $T" >&2; fi' EXIT |
| 10 | trap 'exit 1' HUP INT TERM |
| 11 | unset ZENODO_TOKEN |
| 12 | export ZENODO_TOKEN=fixture-sentinel-not-a-real-token |
| 13 | OMC=${1:-$HOME/work/flux/omega-mv/seed/out/omc} |
| 14 | bash "$D/build.sh" "$OMC" "$T/out" |
| 15 | export DOI_ENGINE=$T/out/doi |
| 16 | PASS=0 |
| 17 | ok() { PASS=$((PASS+1)); printf 'PASS %s\n' "$*"; } |
| 18 | fail() { printf 'FAIL %s\n' "$*" >&2; exit 1; } |
| 19 | newcase() { |
| 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. |
| 27 | plain() { |
| 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 | } |
| 32 | pty() { |
| 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 | } |
| 40 | no_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 | } |
| 45 | no_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. |
| 52 | compile_fixture() { |
| 53 | "$OMC" < "$1.om" > "$1" |
| 54 | chmod 755 "$1" |
| 55 | } |
| 56 | for 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" |
| 59 | done |
| 60 | # A deliberately broken supervisor swallows nonzero keeper completion and never closes output. |
| 61 | sed '/^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" |
| 63 | compile_fixture "$T/ignore-death" |
| 64 | for 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 |
| 107 | done |
| 108 | |
| 109 | newcase happy |
| 110 | pty 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. |
| 114 | grep -q 'including untracked PDFs' "$T/happy/stdout" || fail 'missing pre-upload selection warning' |
| 115 | ok 'all matching PDFs including untracked explicitly disclosed' |
| 116 | |
| 117 | cat > "$C/expected.log" <<'EOF' |
| 118 | POST https://sandbox.zenodo.org/api/deposit/depositions auth=ok |
| 119 | PUT https://sandbox.zenodo.org/api/files/abc-123/paper.pdf auth=ok |
| 120 | POST https://sandbox.zenodo.org/api/deposit/depositions/123/actions/publish auth=ok |
| 121 | EOF |
| 122 | cmp "$C/expected.log" "$C/api/requests.log" || fail 'sandbox default/request order' |
| 123 | ok 'sandbox default and exact request order' |
| 124 | cmp "$C/repo/paper.pdf" "$C/api/uploaded-paper.pdf" || fail 'uploaded PDF bytes' |
| 125 | ok '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" |
| 128 | cmp "$C/body.expected" "$C/api/request-create.json" || fail 'request metadata' |
| 129 | ok '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" |
| 131 | cmp "$C/meta.expected" "$C/repo/.zenodo.json" || fail 'rewrite metadata' |
| 132 | ok 'DOI insertion preserves all metadata bytes' |
| 133 | grep -q 'Reserved DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'reserved DOI output' |
| 134 | grep -q 'Published DOI: 10.5072/zenodo.123' "$C/stdout" || fail 'published DOI output' |
| 135 | ok 'reserved/published DOI output' |
| 136 | # Rerun cannot reserve another deposit now that the local paper has a DOI. |
| 137 | cp "$C/api/requests.log" "$C/before.log" |
| 138 | plain |
| 139 | [ "$rc" -ne 0 ] || fail 'existing DOI accepted' |
| 140 | cmp "$C/before.log" "$C/api/requests.log" || fail 'existing DOI created duplicate' |
| 141 | ok 'existing DOI prevents duplicate draft' |
| 142 | newcase production |
| 143 | sed -i 's/sandbox\.zenodo/zenodo/g;s/10\.5072/10.5281/g' "$C/api/"{create,publish}.json |
| 144 | pty yes --production |
| 145 | [ "$rc" = 0 ] || fail 'production fixture status' |
| 146 | grep -q '^POST https://zenodo.org/api/deposit/depositions auth=ok$' "$C/api/requests.log" || fail 'production base' |
| 147 | grep -q 'Published DOI: 10.5281/zenodo.123' "$C/stdout" || fail 'production DOI' |
| 148 | ok 'production needs explicit flag' |
| 149 | grep -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' |
| 150 | ok 'C8 launcher RLIMIT_CORE zero before children; system collectors explicitly excluded' |
| 151 | for answer in no Yes 'yes '; do |
| 152 | newcase "decline-$PASS" |
| 153 | pty "$answer" |
| 154 | [ "$rc" = 0 ] || fail 'decline status' |
| 155 | no_publish "declined '$answer'" |
| 156 | done |
| 157 | newcase no-tty |
| 158 | plain |
| 159 | [ "$rc" = 0 ] || fail 'no tty status' |
| 160 | no_publish 'stdin cannot consent without tty' |
| 161 | grep -q 'no terminal' "$C/stderr" || fail 'no tty explanation' |
| 162 | # Every required field, creator name and conditional publication type are checked locally. |
| 163 | for 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" |
| 169 | done |
| 170 | newcase bad-creators |
| 171 | sed -i 's/"name": "Researcher, A."/"name": ""/' "$C/repo/.zenodo.json" |
| 172 | plain; no_requests 'empty creator name' |
| 173 | newcase no-license |
| 174 | rm "$C/repo/LICENSE" |
| 175 | plain; no_requests 'missing LICENSE' |
| 176 | newcase empty-license |
| 177 | : > "$C/repo/LICENSE" |
| 178 | plain; no_requests 'empty LICENSE' |
| 179 | newcase duplicate-file |
| 180 | plain --source "$C/repo/paper.pdf" |
| 181 | no_requests 'duplicate upload basename' |
| 182 | newcase no-pdf |
| 183 | rm "$C/repo/paper.pdf" |
| 184 | plain; no_requests 'missing PDF' |
| 185 | newcase invalid-json |
| 186 | printf '{"title":' > "$C/repo/.zenodo.json" |
| 187 | plain; no_requests 'malformed JSON' |
| 188 | newcase escaped-key |
| 189 | sed -i 's/"title"/"ti\\u0074le"/' "$C/repo/.zenodo.json" |
| 190 | plain; no_requests 'escaped metadata key' |
| 191 | newcase missing-token |
| 192 | unset ZENODO_TOKEN |
| 193 | plain; no_requests 'missing token' |
| 194 | export ZENODO_TOKEN=fixture-sentinel-not-a-real-token |
| 195 | newcase bad-token |
| 196 | ZENODO_TOKEN='bad"token'; export ZENODO_TOKEN |
| 197 | plain; no_requests 'config injection token' |
| 198 | export ZENODO_TOKEN=fixture-sentinel-not-a-real-token |
| 199 | newcase source |
| 200 | printf 'source bytes\n' > "$C/repo/source.tar.gz" |
| 201 | printf '{"key":"source.tar.gz","size":13}\n' > "$C/api/upload-source.tar.gz.json" |
| 202 | pty yes "--source '$C/repo/source.tar.gz'" |
| 203 | [ "$rc" = 0 ] || fail 'source fixture' |
| 204 | cmp "$C/repo/source.tar.gz" "$C/api/uploaded-source.tar.gz" || fail 'source bytes' |
| 205 | ok 'optional source upload' |
| 206 | # Each URL must match the selected HTTPS origin. Every link is checked before any PUT. |
| 207 | for 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" |
| 223 | done |
| 224 | newcase upload-error |
| 225 | printf '500\n' > "$C/api/upload.code" |
| 226 | cp "$D/tests/fixtures/error.json" "$C/api/upload.json" |
| 227 | plain |
| 228 | [ "$rc" -ne 0 ] || fail 'HTTP error accepted' |
| 229 | [ "$(wc -l < "$C/api/requests.log")" = 2 ] || fail 'upload failure retried/published' |
| 230 | grep -q 'Missing title' "$C/stderr" || fail 'API field error lost' |
| 231 | ok 'HTTP error reported without retry/publish' |
| 232 | newcase acknowledgement |
| 233 | printf '{"key":"wrong.pdf","size":10}\n' > "$C/api/upload.json" |
| 234 | plain |
| 235 | [ "$rc" -ne 0 ] || fail 'wrong upload acknowledgement accepted' |
| 236 | [ "$(wc -l < "$C/api/requests.log")" = 2 ] || fail 'bad upload published' |
| 237 | ok 'wrong upload acknowledgement refused' |
| 238 | # An untrusted checkout must not exfiltrate a different local file through an input symlink. |
| 239 | for 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" |
| 250 | done |
| 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. |
| 253 | newcase replace-after-snapshot |
| 254 | printf 'unrelated local secret\n' > "$C/outside" |
| 255 | cat > "$C/api/create.hook" <<EOF |
| 256 | rm -- "$C/repo/paper.pdf" |
| 257 | ln -s "$C/outside" "$C/repo/paper.pdf" |
| 258 | EOF |
| 259 | plain |
| 260 | [ "$rc" = 0 ] || fail 'replacement after snapshot status' |
| 261 | [ -L "$C/repo/paper.pdf" ] || fail 'replacement control never ran' |
| 262 | printf 'paper pdf\n' > "$C/expected.pdf" |
| 263 | cmp "$C/expected.pdf" "$C/api/uploaded-paper.pdf" || fail 'curl reopened replaced original' |
| 264 | ok 'curl uses private descriptor snapshot after original path replacement' |
| 265 | newcase stale-temp |
| 266 | printf 'do not overwrite\n' > "$C/repo/.zenodo.json.doi-new" |
| 267 | plain |
| 268 | [ "$rc" -ne 0 ] || fail 'existing temp overwritten' |
| 269 | [ "$(cat "$C/repo/.zenodo.json.doi-new")" = 'do not overwrite' ] || fail 'temp content changed' |
| 270 | no_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. |
| 273 | newcase source-ancestor |
| 274 | mkdir "$C/private" |
| 275 | printf 'private source\n' > "$C/private/source.tar.gz" |
| 276 | ln -s "$C/private" "$C/src" |
| 277 | plain --source "$C/src/source.tar.gz" |
| 278 | no_requests 'source ancestor symlink' |
| 279 | grep -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. |
| 282 | newcase source-ancestor-swap |
| 283 | mkdir "$C/src" "$C/private" |
| 284 | printf 'public source\n' > "$C/src/source.tar.gz" |
| 285 | printf 'private source\n' > "$C/private/source.tar.gz" |
| 286 | export DOI_REAL_ENGINE=$DOI_ENGINE RACE_SRC=$C/src RACE_PRIVATE=$C/private |
| 287 | cat > "$C/engine-wrapper" <<'EOF' |
| 288 | #!/bin/sh |
| 289 | if [ "$1" = serve ]; then |
| 290 | mv "$RACE_SRC" "$RACE_SRC.old" |
| 291 | ln -s "$RACE_PRIVATE" "$RACE_SRC" |
| 292 | fi |
| 293 | exec "$DOI_REAL_ENGINE" "$@" |
| 294 | EOF |
| 295 | chmod 755 "$C/engine-wrapper" |
| 296 | export DOI_ENGINE=$C/engine-wrapper |
| 297 | plain --source "$C/src/source.tar.gz" |
| 298 | export DOI_ENGINE=$DOI_REAL_ENGINE |
| 299 | unset DOI_REAL_ENGINE RACE_SRC RACE_PRIVATE |
| 300 | no_requests 'source ancestor swapped before snapshot' |
| 301 | grep -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 | |
| 303 | newcase repository-ancestor-symlink |
| 304 | mv "$C/repo" "$C/original" |
| 305 | ln -s "$C/original" "$C/repo" |
| 306 | plain |
| 307 | no_requests 'repository root symlink' |
| 308 | grep -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. |
| 311 | newcase refusal-oracle-unrelated |
| 312 | mkdir "$C/private" |
| 313 | printf 'source bytes\n' > "$C/private/source.tar.gz" |
| 314 | ln -s "$C/private" "$C/src" |
| 315 | ZENODO_TOKEN='bad"token' plain --source "$C/src/source.tar.gz" |
| 316 | no_requests 'unrelated token refusal control' |
| 317 | grep -Fq 'doi: ZENODO_TOKEN holds a byte' "$C/stderr" || fail 'unrelated refusal control did not reach token check' |
| 318 | if 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' |
| 320 | fi |
| 321 | ok 'source refusal diagnosis oracle rejects unrelated token failure' |
| 322 | |
| 323 | # Repository pathname replacement must not redirect write-back to a different repository. |
| 324 | newcase root-swap |
| 325 | mkdir "$C/replacement" |
| 326 | cp "$C/repo/.zenodo.json" "$C/replacement/.zenodo.json" |
| 327 | cat > "$C/api/create.hook" <<EOF |
| 328 | mv "$C/repo" "$C/original" |
| 329 | mv "$C/replacement" "$C/repo" |
| 330 | EOF |
| 331 | plain |
| 332 | [ "$rc" = 0 ] || fail 'anchored root swap failed' |
| 333 | cmp "$D/tests/fixtures/metadata.json" "$C/repo/.zenodo.json" || fail 'replacement repo overwritten' |
| 334 | grep -q '"doi": "10.5072/zenodo.123"' "$C/original/.zenodo.json" || fail 'retained repo not updated' |
| 335 | cmp "$C/original/paper.pdf" "$C/api/uploaded-paper.pdf" || fail 'root swap changed upload' |
| 336 | ok 'repo directory identity retained through write-back' |
| 337 | |
| 338 | for 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" |
| 353 | done |
| 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. |
| 357 | for 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" |
| 407 | done |
| 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. |
| 411 | sync_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 | } |
| 419 | saved_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 | } |
| 425 | newcase directory-sync-failure |
| 426 | sync_trace |
| 427 | [ "$rc" != 0 ] || fail 'directory fsync failure ignored' |
| 428 | [ "$(grep -c 'INJECTED' "$C/sync.trace")" = 1 ] || fail 'expected exactly one injected fsync' |
| 429 | grep 'INJECTED' "$C/sync.trace" | grep -Fq "<$C/repo>" || fail 'injected fsync was not the retained directory' |
| 430 | if grep 'INJECTED' "$C/sync.trace" | grep -Fq '.zenodo.json'; then fail 'injected fsync hit the candidate'; fi |
| 431 | grep -Eq 'write\(1<pipe:.*READY' "$C/sync.trace" || fail 'protocol trace did not observe READY' |
| 432 | saved_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' |
| 434 | ok '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). |
| 437 | sed '/^r = fsync int get gfd 0$/{n; /^if lt r 0$/a say tsaved |
| 438 | }' "$D/doi.om" > "$T/false-saved.om" |
| 439 | compile_fixture "$T/false-saved" |
| 440 | newcase false-saved-nonzero |
| 441 | real_engine=$DOI_ENGINE |
| 442 | export DOI_ENGINE=$T/false-saved |
| 443 | sync_trace |
| 444 | export 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' |
| 447 | grep 'INJECTED' "$C/sync.trace" | grep -Fq "<$C/repo>" || fail 'false-SAVED injection not directory' |
| 448 | if saved_absent "$C/sync.trace" 2>"$C/oracle.err"; then fail 'C9 no-SAVED oracle missed erroneous SAVED'; fi |
| 449 | grep -Fxq 'no-SAVED oracle: keeper protocol emitted SAVED' "$C/oracle.err" || fail 'C9 wrong oracle failure' |
| 450 | ok 'C9 false-SAVED plus nonzero exit caught by protocol-write oracle' |
| 451 | |
| 452 | newcase concurrent-writer |
| 453 | mkdir "$C/api2" |
| 454 | cp "$D/tests/fixtures/"{create,upload,publish}.json "$C/api2/" |
| 455 | cat > "$C/api/create.hook" <<EOF |
| 456 | rc=0 |
| 457 | sh "$D/doi.sh" "$C/repo" --fixture "$C/api2" >"$C/writer2.out" 2>"$C/writer2.err" || rc=\$? |
| 458 | [ "\$rc" != 0 ] |
| 459 | EOF |
| 460 | plain |
| 461 | [ "$rc" = 0 ] || fail 'serialized writer control failed' |
| 462 | [ ! -e "$C/api2/requests.log" ] || fail 'concurrent writer sent request' |
| 463 | grep -q 'another DOI writer' "$C/writer2.err" || fail 'writer lock not exercised' |
| 464 | ok 'exclusive directory lock serializes tool writers' |
| 465 | |
| 466 | for 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" |
| 483 | done |
| 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. |
| 487 | if grep -rFl -- "$ZENODO_TOKEN" "$T" > /dev/null; then fail 'token on disk or argv'; fi |
| 488 | ok 'token absent from disk, logs and actual argv' |
| 489 | printf 'DOI FIXTURE PASS checks=%s (no network or deposits)\n' "$PASS" |