interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / server / tests / overflow.sh

158 lines · 10855 bytes Raw Blame History
1#!/usr/bin/env bash
2# ORACLE: host test driver (bash + curl + git + python3), not product code.
3# server/tests/overflow.sh: a response that does not fit its buffer is never sent cut (review of
4# 3edf9201, finding N2). fputc drops a byte that does not fit fob and sets fobtr; fsend
5# (server/main.om) reads the flag before the first header and sends a whole 500 instead.
6# collab/tests/bigstore.sh shows this with 9 MB and 12 MB pages from stored data. This gate shows the
7# parts that data cannot reach easily, on trial builds whose fob is made small with sed (the port,
8# allow-list, storage, data dir and count sources are private, as in server/tests/title.sh):
9# L fob = the size of the explore page less 100 bytes: the fragment fits, the layout appended
10# after it does not. The answer must be a whole 500 page, not 200 cut 100 bytes short.
11# J fob = the size of /explore.json less 50 bytes: a .json name with no Accept header must get a
12# whole forge/error/1 document, and so must the HTML URL asked for with Accept: application/json.
13# R the full-size build: a redirect whose Location would pass floc (4096 bytes) is a 500 with no
14# Location; one that fits is the 302 it was.
15# Negative control for each: the same build with the check taken out of fsend gives the cut answer.
16# FORGE_OVERFLOW_PORT (default 18559), FORGED_CPUS (default 4,5), KEEP_TEST_TMP=1 keeps the temp dir
17set -u
18cd "$(dirname "$0")/../.."
19OMC=$HOME/work/flux/omega-mv/seed/out/omc
20PIN=3ad9f10f64309e7acf8a725082b28e23a2a4aeb07e98322fb4dc0fcd8fc93194
21PORT=${FORGE_OVERFLOW_PORT:-18559}
22[ "$(sha256sum "$OMC" | cut -d' ' -f1)" = "$PIN" ] || { echo 'FAIL compiler pin'; exit 1; }
23if ss -ltnH "sport = :$PORT" | grep -q .; then echo "NOT_RUN: private port $PORT busy"; exit 3; fi
24T=$(mktemp -d)
25pid=; pass=0; fail=0
26ok() { pass=$((pass+1)); echo "PASS $*"; }
27bad() { fail=$((fail+1)); echo "FAIL $*"; }
28want() { [ "$2" = "$3" ] && ok "$1 = $2" || bad "$1 = $2 (want $3)"; }
29cleanup() {
30 [ -n "$pid" ] && { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; }
31 if [ "${KEEP_TEST_TMP:-0}" = 1 ]; then echo "Evidence: $T"; else rm -rf "$T"; fi
32}
33trap cleanup EXIT
34run() { taskset -c "${FORGED_CPUS:-4,5}" nice -n 10 "$@"; }
35# five repositories, so the explore fragment is well over the size of the notice
36mkdir -p "$T/storage" "$T/data/logs" "$T/ng" "$T/bin" "$T/w"
37cp server/tests/fake/rad server/tests/fake/getent "$T/bin/"; chmod 755 "$T/bin/rad" "$T/bin/getent"
38: > "$T/ng/dl.log"; : > "$T/ng/access.log"
39git init -q -b main "$T/w" && echo fixture > "$T/w/a.txt" && git -C "$T/w" add a.txt \
40 && git -C "$T/w" -c user.name=Fixture -c user.email=fixture@example.invalid commit -qm fixture || { echo 'FAIL fixture repo'; exit 1; }
41printf '| repo | RID | default branch |\n|---|---|---|\n' > "$T/RIDS.md"
42# STORE_VARIANT=1 (with IPL_DIR): the same gates on the store-reading build; the fixture becomes a
43# store (store/tests/fixstore.sh) and its ids are longer (the store's old-id rule wants 20..60 letters)
44RIDP=zOverflowFixture
45[ "${STORE_VARIANT:-0}" = 1 ] && RIDP=zOverflowFixtureStore
46for n in a b c d e; do
47 git clone -q --bare "$T/w" "$T/storage/$RIDP$n"
48 printf '| overflow-fixture-%s | rad:%s%s | main |\n' "$n" "$RIDP" "$n" >> "$T/RIDS.md"
49done
50if [ "${STORE_VARIANT:-0}" = 1 ]; then
51 bash store/tests/fixstore.sh "$T/RIDS.md" "$T/storage" "$T/store" || { echo 'FAIL fixture store'; exit 1; }
52 export FORGE_STORE=$T/store
53 echo "STORE_VARIANT: store-reading build, store $T/store"
54fi
55{ printf 'om2\n'; for u in $(./run.sh units); do cat "$u"; done; } > "$T/base.om"
56sed -e "s|2130706433 18480|2130706433 $PORT|" \
57 -e "s|/home/drakestapleton/handoffs/radicle/RIDS.md|$T/RIDS.md|g" \
58 -e "s|/home/drakestapleton/.radicle/storage/|$T/storage/|g" \
59 -e "s|/home/drakestapleton/forge-data/|$T/data/|g" \
60 -e "s|^text tfzp0 \".*\"|text tfzp0 \"$T/ng/dl.log\"|" \
61 -e "s|^text tfzp1 \".*\"|text tfzp1 \"$T/ng/access.log\"|" \
62 -e "s|^text tfzwal \".*\"|text tfzwal \"$T/ng/wal\"|" \
63 -e "s|^text tfzrad \".*\"|text tfzrad \"$T/bin/rad\"|" \
64 -e "s|^text tfzspth \".*\"|text tfzspth \"$T/counts.snapshot\"|" \
65 -e "s|^text tfzgent \".*\"|text tfzgent \"$T/bin/getent\"|" "$T/base.om" > "$T/a.om"
66if grep -qE '2130706433 18480|/home/drakestapleton/(forge-data|\.radicle|handoffs/radicle)' "$T/a.om"; then echo 'FAIL live target remains'; exit 1; fi
67[ "$(grep -c '^global fob uchar 8388608$' "$T/a.om")" = 1 ] || { echo 'FAIL fob is not declared as expected'; exit 1; }
68[ "$(grep -c '^if fovfany$' "$T/a.om")" = 1 ] || { echo 'FAIL the check line is not unique'; exit 1; }
69start() { # start NAME [sed expression...]: build $T/a.om with the expressions as $T/NAME and start it
70 local n=$1; shift
71 if [ $# -gt 0 ]; then local e=(); for x in "$@"; do e+=(-e "$x"); done; sed "${e[@]}" "$T/a.om" > "$T/$n.om"; fi
72 run "$OMC" < "$T/$n.om" > "$T/$n.bin" 2> "$T/$n.err" || { bad "build $n: $(cat "$T/$n.err")"; return 1; }
73 chmod 755 "$T/$n.bin"
74 FORGE_SOCIAL_DIR="$T/data/" taskset -c "${FORGED_CPUS:-4,5}" nice -n 10 "$T/$n.bin" > "$T/$n.log" 2>&1 &
75 pid=$!
76 for _ in $(seq 50); do curl --max-time 1 -s -o /dev/null "http://127.0.0.1:$PORT/login" && return 0; kill -0 "$pid" 2>/dev/null || break; sleep 0.1; done
77 bad "trial $n did not listen"; return 1
78}
79stop() { [ -n "$pid" ] && { kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; }; pid=; for _ in $(seq 50); do ss -ltnH "sport = :$PORT" | grep -q . || return 0; sleep 0.1; done; }
80get() { # get PATH [curl args]: body in $T/b, headers in $T/h; prints the status
81 local p=$1; shift
82 curl --max-time 20 -s -o "$T/b" -D "$T/h" -w '%{http_code}' -H 'X-Forge-Base: /' "$@" "http://127.0.0.1:$PORT$p"
83}
84hdr() { grep -i "^$1:" "$T/h" | head -1 | tr -d '\r' | cut -d' ' -f2-; }
85size() { stat -c %s "$T/b"; }
86# fobsize: the bytes the page held in fob. forged adds translation hints while it sends a page
87# (server/tr.om: translate="no" attributes and <span translate="no"> around hashes and ids), so the
88# body is longer than fob by exactly those hints; server/tests/translate_check.py strip removes them.
89fobsize() { python3 server/tests/translate_check.py strip "$T/b" "$T/b.fob" && stat -c %s "$T/b.fob"; }
90whole() { tail -c 16 "$T/b" | grep -q '</html>' && echo whole || echo cut; }
91errdoc() { python3 -c 'import json,sys; d=json.load(open(sys.argv[1],"rb")); print("yes" if d.get("schema")=="forge/error/1" and d.get("status")==500 and "too large" in d.get("message","") else "no")' "$T/b" 2> /dev/null || echo no; }
92lenok() { [ "$(hdr content-length)" = "$(size)" ] && echo yes || echo "no (header $(hdr content-length), body $(size))"; }
93
94# ---- the full-size build: sizes, and the redirect
95start a || exit 1
96want "GET /explore" "$(get /explore)" 200; TE=$(fobsize); want "explore page is whole" "$(whole)" whole
97want "GET /explore.json" "$(get /explore.json)" 200; JE=$(size)
98want "GET /no-such-page" "$(get /no-such-page)" 404; E4=$(fobsize)
99echo "sizes: explore page $TE, explore.json $JE, 404 page $E4"
100[ "$TE" -gt $((E4 + 700)) ] && [ "$JE" -gt 600 ] && ok "fixture: the pages are larger than the notice" || bad "fixture: explore $TE, json $JE, 404 $E4"
101RID=${RIDP}a
102short=/rad:$RID/tree/main
103want "GET $short" "$(get "$short")" 302
104want "its Location" "$(hdr location)" /drakestapleton/overflow-fixture-a/tree/main
105# the Location swaps /rad:<17 bytes> (22 bytes) for /drakestapleton/overflow-fixture-a (34 bytes): 12 longer
106long=/rad:$RID/tree/$(printf '%*s' $((4095 - 11 - ${#RID})) '' | tr ' ' a)
107want "fixture: the long path is 4095 bytes" "${#long}" 4095
108want "GET the 4095-byte /rad: path (its Location would be 4107 bytes, floc holds 4096)" "$(get "$long")" 500
109want "no Location on it" "$(grep -i -c '^location:' "$T/h")" 0
110want "a whole notice page" "$(whole)" whole
111want "Content-Length equals the body" "$(lenok)" yes
112want "server still answers" "$(get /explore)" 200
113stop
114start am 's/^if fovfany$/if false/' || exit 1
115c=$(get "$long"); l=$(hdr location)
116[ "$c" = 302 ] && [ "${#l}" = 4096 ] && ok "NEG without the check: 302 with a Location cut at 4096 bytes" || bad "NEG redirect without the check: $c, Location ${#l} bytes"
117stop
118# ---- L: the layout does not fit
119SL=$((TE - 100))
120start l "s/^global fob uchar 8388608\$/global fob uchar $SL/" || exit 1
121want "[fob $SL] GET /explore, layout 100 bytes over" "$(get /explore)" 500
122want "[fob $SL] a whole notice page" "$(whole)" whole
123grep -q 'too large' "$T/b" && ok "[fob $SL] the page says why" || bad "[fob $SL] the page does not say why"
124want "[fob $SL] Content-Length equals the body" "$(lenok)" yes
125want "[fob $SL] no ETag" "$(grep -i -c '^etag:' "$T/h")" 0
126want "[fob $SL] Cache-Control" "$(hdr cache-control)" no-store
127want "[fob $SL] a page that fits is served" "$(get /no-such-page)" 404
128want "[fob $SL] that page is whole" "$(whole)" whole
129sleep 0.3
130want "[fob $SL] the access log says 500" "$(grep -a -c ' /explore 500 ' "$T/l.log")" 1
131stop
132start lm "s/^global fob uchar 8388608\$/global fob uchar $SL/" 's/^if fovfany$/if false/' || exit 1
133c=$(get /explore)
134[ "$c" = 200 ] && [ "$(fobsize)" = "$SL" ] && [ "$(whole)" = cut ] && ok "NEG without the check: 200, $SL bytes of fob, cut" || bad "NEG layout without the check: $c, $(fobsize) bytes of fob, $(whole)"
135stop
136# ---- J: a JSON document does not fit
137SJ=$((JE - 50))
138start j "s/^global fob uchar 8388608\$/global fob uchar $SJ/" || exit 1
139want "[fob $SJ] GET /explore.json with no Accept header" "$(get /explore.json)" 500
140want "[fob $SJ] a whole forge/error/1 document" "$(errdoc)" yes
141case "$(hdr content-type)" in application/json*) ok "[fob $SJ] content type is JSON" ;; *) bad "[fob $SJ] content type $(hdr content-type)" ;; esac
142want "[fob $SJ] Content-Length equals the body" "$(lenok)" yes
143want "[fob $SJ] GET /explore with Accept: application/json" "$(get /explore -H 'Accept: application/json')" 500
144want "[fob $SJ] a whole forge/error/1 document again" "$(errdoc)" yes
145c=$(get /explore)
146[ "$c" = 500 ] && [ "$(lenok)" = yes ] && { [ "$(size)" = 0 ] || [ "$(whole)" = whole ]; } \
147 && ok "[fob $SJ] the HTML page: 500, and the body is whole or empty ($(size) bytes), never cut" || bad "[fob $SJ] the HTML page: $c, $(size) bytes, $(whole)"
148# The 404 HTML page (site header and footer, about 7 KB) no longer fits this small fob; the small
149# forge/error/1 document does, so the JSON 404 shows the server still answers.
150want "[fob $SJ] server still answers" "$(get /no-such-page -H 'Accept: application/json')" 404
151stop
152start jm "s/^global fob uchar 8388608\$/global fob uchar $SJ/" 's/^if fovfany$/if false/' || exit 1
153c=$(get /explore.json)
154[ "$c" = 200 ] && [ "$(size)" = "$SJ" ] && ! python3 -c 'import json,sys; json.load(open(sys.argv[1],"rb"))' "$T/b" 2> /dev/null \
155 && ok "NEG without the check: 200, $SJ bytes, not JSON" || bad "NEG JSON without the check: $c, $(size) bytes"
156stop
157echo "server/tests/overflow.sh: $pass passed, $fail failed"
158[ "$fail" = 0 ]