interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / research / tests / intake.sh
| 1 | #!/bin/sh |
| 2 | # Full composed forged, private data/port/key. No live service, model or Radicle node. |
| 3 | set -eu |
| 4 | cd "$(dirname "$0")/../.." |
| 5 | OMV=$HOME/work/flux/omega-mv |
| 6 | OMC=$OMV/seed/out/omc |
| 7 | PORT=${RESEARCH_TEST_PORT:-19464} |
| 8 | T=$(mktemp -d) |
| 9 | PID= |
| 10 | cleanup(){ if [ -n "$PID" ]; then kill "$PID" 2>/dev/null || :; wait "$PID" 2>/dev/null || :; fi; rm -rf "$T"; } |
| 11 | trap cleanup EXIT HUP INT TERM |
| 12 | pass=0; fail=0 |
| 13 | ok(){ pass=$((pass+1)); echo "PASS intake $*"; } |
| 14 | bad(){ fail=$((fail+1)); echo "FAIL intake $*"; } |
| 15 | mkdir -p "$T/data/open" "$T/data/research/add" "$T/social" "$T/slwork" |
| 16 | chmod 700 "$T/social" "$T/slwork" |
| 17 | { echo om2; cat $(./run.sh units); } > "$T/composed.om" |
| 18 | python3 - "$T/composed.om" "${RESEARCH_INTAKE_MUTANT:-}" "${RESEARCH_INTAKE_FAULT:-}" <<'PYEDIT' |
| 19 | import sys |
| 20 | p=sys.argv[1];s=open(p).read() |
| 21 | if sys.argv[2]=='A3': |
| 22 | assert s.count('p = rqpre')==1 and s.count('ret rqpartial')==1 |
| 23 | s=s.replace('p = rqpre','p = long 0').replace('ret rqpartial','ret p') |
| 24 | if sys.argv[3]=='writefail': |
| 25 | s=s.replace('fn rqappend kind uchar to long\n','fn rqappend kind uchar to long\nif eq kind 80\nret soerr 503 tomsgq\nend\n') |
| 26 | open(p,'w').write(s) |
| 27 | PYEDIT |
| 28 | sed -e "s|text tfdata \".*\"|text tfdata \"$T/data/\"|" -e "s/18480/$PORT/g" "$T/composed.om" | "$OMC" > "$T/forged" 2> "$T/omc.err" || { cat "$T/omc.err"; exit 1; } |
| 29 | chmod 700 "$T/forged" |
| 30 | FORGE_SOCIAL_DIR="$T/social/" INTERPLANE_SALVAGE_DIR="$T/slwork/" "$T/forged" > "$T/server.out" 2> "$T/server.err" & PID=$! |
| 31 | for i in $(seq 1 60); do curl -s -o /dev/null "http://127.0.0.1:$PORT/" && break; sleep .1; done |
| 32 | B=http://127.0.0.1:$PORT |
| 33 | code(){ m=$1; p=$2; shift 2; c=$(curl -s -o "$T/body" -D "$T/hdr" -X "$m" "$@" "$B$p" -w '%{http_code}'); sed -i -e 's/<span translate="no">//g' -e 's|</span>||g' "$T/body"; printf '%s' "$c"; } |
| 34 | field(){ sed -n "s/.*name=\"$1\" value=\"\([^\"]*\)\".*/\1/p" "$T/body" | head -1; } |
| 35 | hdr(){ grep -i "^$1:" "$T/hdr" | head -1 | cut -d: -f2- | tr -d '\r' | sed 's/^ *//'; } |
| 36 | ssh-keygen -q -t ed25519 -N '' -C research-test -f "$T/key" |
| 37 | D=$(python3 - "$T/key.pub" <<'PY' |
| 38 | import base64,sys |
| 39 | raw=base64.b64decode(open(sys.argv[1]).read().split()[1]);n=int.from_bytes(b'\xed\x01'+raw[-32:],'big');s='';a='123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
| 40 | while n:n,r=divmod(n,58);s=a[r]+s |
| 41 | print('did:key:z'+s) |
| 42 | PY |
| 43 | ) |
| 44 | # Actual generated documents through the composed router, not only files on disk. |
| 45 | sh research/publish.sh "$T/data/research" "$T/corpus" > "$T/publish.log" 2>&1 |
| 46 | generation=$(readlink "$T/data/research/current") |
| 47 | genhash(){ find "$T/data/research/$generation" -type f -print0 | sort -z | xargs -0 sha256sum | sha256sum | cut -d ' ' -f1; } |
| 48 | immutable_before=$(genhash) |
| 49 | [ "$(code GET /research/authors/drakestapleton)" = 200 ] && grep -q 'Drake Stapleton' "$T/body" && ok 'generated author page is served through author route' || bad 'author route' |
| 50 | [ "$(code GET /research/add)" = 200 ] && grep -q '/research/add/' "$T/body" && ok 'add information page is served' || bad 'add information route' |
| 51 | [ "$(code GET /research/doi)" = 200 ] && grep -q 'Zenodo mints' "$T/body" && ok 'DOI help page is served' || bad 'DOI information route' |
| 52 | [ "$(code GET /research/authors/../outside --path-as-is)" = 400 ] && ok 'author traversal refused by request parser' || bad 'author traversal' |
| 53 | # Signed out: no key, no queue. |
| 54 | [ "$(code GET /research/add/)" = 200 ] && grep -q 'Sign in with your key' "$T/body" && ok 'sign-in page' || bad 'sign-in page' |
| 55 | [ "$(code POST /research/add/ --data 'action=sync&orcid=0009-0002-6890-7111&doi=')" = 303 ] && [ ! -e "$T/data/research/add/queue" ] && ok 'signed-out request writes nothing' || bad 'signed-out request' |
| 56 | code GET /login > /dev/null |
| 57 | PREN=$(hdr set-cookie | sed -n 's/^\([A-Za-z_-]*forge_pre\)=.*/\1/p'); PRE=$(hdr set-cookie | sed -n 's/^[^=]*=\([^;]*\).*/\1/p') |
| 58 | CSRF=$(field csrf); NONCE=$(field nonce); TS=$(field ts) |
| 59 | printf '%s' "interplane-forge-login:$NONCE:$TS" | ssh-keygen -Y sign -f "$T/key" -n interplane-forge > "$T/sig" 2>/dev/null |
| 60 | code POST /login -H "Cookie: $PREN=$PRE" --data-urlencode "csrf=$CSRF" --data-urlencode "nonce=$NONCE" --data-urlencode "ts=$TS" --data-urlencode "did=$D" --data-urlencode "sig@$T/sig" > /dev/null |
| 61 | SES=$(hdr set-cookie | sed -n 's/^__Host-forge_session=\([^;]*\).*/\1/p'); CK="Cookie: __Host-forge_session=$SES" |
| 62 | [ "${#SES}" = 64 ] && ok 'real throwaway Radicle key signed in' || bad 'sign-in failed' |
| 63 | code GET /research/add/ -H "$CK" > /dev/null |
| 64 | TOK=$(field csrf) |
| 65 | [ "${#TOK}" = 32 ] && ok 'shared CSRF form token' || bad 'csrf field' |
| 66 | grep -q "Bringing new projects is closed until Interplane's own store accepts them." "$T/body" && ! grep -q 'name="rid"' "$T/body" && ok 'bring form states the flow is closed, no repository ID field' || bad 'bring form closed text' |
| 67 | [ "$(code POST /research/add/ -H "$CK" --data 'action=sync&orcid=0009-0002-6890-7111&doi=')" = 403 ] && ok 'missing CSRF refused' || bad 'CSRF refusal' |
| 68 | post(){ code POST /research/add/ -H "$CK" --data-urlencode "csrf=$TOK" "$@"; } |
| 69 | [ "$(post --data-urlencode action=sync --data-urlencode orcid=bad --data-urlencode doi=)" = 400 ] && ok 'malformed ORCID refused' || bad 'ORCID form' |
| 70 | [ "$(post --data-urlencode action=sync --data-urlencode orcid= --data-urlencode doi=http://example.org)" = 400 ] && ok 'non-Zenodo DOI refused' || bad 'DOI form' |
| 71 | [ "$(post --data-urlencode action=sync --data-urlencode orcid=0009-0002-6890-7111 --data-urlencode doi=)" = 200 ] && ok 'ORCID request queued' || bad 'ORCID submit' |
| 72 | [ "$(cut -f2 "$T/data/research/add/queue")" = "$D" ] && ok 'session key, not an asserted field, is recorded' || bad 'key attribution' |
| 73 | R=z2abcdefghijkmnopqrstuvw111 |
| 74 | c=$(post --data-urlencode action=bring --data-urlencode "rid=rad:$R" --data-urlencode note=paper --data-urlencode consent=yes) |
| 75 | if [ "${RESEARCH_INTAKE_FAULT:-}" = writefail ]; then |
| 76 | [ "$c" = 202 ] && grep -q 'partial acceptance' "$T/body" && grep -q "$R" "$T/body" && ok 'A3 write failure reports truthful recoverable shelf acceptance' || bad 'A3 misleading partial reply' |
| 77 | grep -q "B$(printf '\t')$R$(printf '\t')$D" "$T/data/open/queue" && ! grep -q "P$(printf '\t')$D$(printf '\t')$R" "$T/data/research/add/queue" && ok 'A3 partial result retains shelf RID and key without false indexing claim' || bad 'A3 partial queue evidence' |
| 78 | else |
| 79 | [ "$c" = 200 ] && ok 'repository goes through open shelf POST' || bad 'repository submit' |
| 80 | grep -q "B$(printf '\t')$R$(printf '\t')$D" "$T/data/open/queue" && grep -q "P$(printf '\t')$D$(printf '\t')$R" "$T/data/research/add/queue" && ok 'same RID and signed key in shelf and research queues' || bad 'queue provenance' |
| 81 | fi |
| 82 | [ "$(post --data-urlencode action=chunk --data-urlencode data=raw-file)" = 400 ] && ok 'raw upload action not accepted by research' || bad 'upload refusal' |
| 83 | # A fully consumed prefix must be reclaimed, not make the 4 MiB admission boundary permanent. |
| 84 | python3 - "$T/data/research/add/queue" <<'PY' |
| 85 | import sys |
| 86 | open(sys.argv[1],'wb').write(b'x\n' * 2097152) |
| 87 | PY |
| 88 | cp "$T/data/research/add/queue" "$T/queue-before" |
| 89 | [ "$(post --data-urlencode action=sync --data-urlencode orcid=0009-0002-6890-7111 --data-urlencode doi=)" = 503 ] && ok 'full queue refuses before maintenance' || bad 'queue containment' |
| 90 | before=$(wc -l < "$T/data/open/queue" | tr -d ' ') |
| 91 | c=$(post --data-urlencode action=bring --data-urlencode "rid=rad:$R" --data-urlencode note=paper --data-urlencode consent=yes) |
| 92 | [ "$c" = 503 ] && ok 'A3 full research queue refuses bring before shelf admission' || bad 'A3 full queue response' |
| 93 | [ "$(wc -l < "$T/data/open/queue" | tr -d ' ')" = "$before" ] && ok 'A3 full queue causes no shelf effect' || bad 'A3 SHELF_EFFECT_AFTER_QUEUE_REFUSAL' |
| 94 | sh research/build-tools.sh "$T/bin" |
| 95 | printf '*\n' > "$T/allow"; : > "$T/remove" |
| 96 | RESEARCH_TOOLS=$T/bin sh research/sync.sh --queue "$T/data/research/add/" "$T/sync-state" "$T/allow" "$T/remove" "$T/reg/" "$T/data/open/list.tsv" "$T/storage" > "$T/maintenance.log" 2>&1 |
| 97 | [ "$(post --data-urlencode action=sync --data-urlencode orcid=0009-0002-6890-7111 --data-urlencode doi=)" = 200 ] && ok 'consumed prefix reclaimed: a fresh request is accepted' || bad 'permanent queue exhaustion' |
| 98 | python3 - "$T/queue-before" "$T/data/research/add/queue" <<'PY' |
| 99 | import sys |
| 100 | old=open(sys.argv[1],'rb').read()[65536:];new=open(sys.argv[2],'rb').read() |
| 101 | assert new.startswith(old), 'unread tail changed during compaction' |
| 102 | PY |
| 103 | [ "$?" = 0 ] && ok 'queue reclamation preserves the complete unread tail' || bad 'unread queue bytes lost' |
| 104 | [ "$(cat "$T/sync-state")" = 0 ] && ok 'queue compaction resets cursor under shared lock' || bad 'compacted cursor' |
| 105 | last= |
| 106 | for i in $(seq 1 32); do last=$(post --data-urlencode action=sync --data-urlencode orcid=0009-0002-6890-7111 --data-urlencode doi=); done |
| 107 | [ "$last" = 429 ] && ok 'shared per-key write limit' || bad 'rate limit' |
| 108 | [ "$(genhash)" = "$immutable_before" ] && ok 'mutable ask/intake traffic leaves selected public generation unchanged' || bad 'A5 PUBLIC_GENERATION_MUTATED' |
| 109 | echo "INTAKE_RESULT pass=$pass fail=$fail" |
| 110 | [ "$fail" = 0 ] |