interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / galaxy / tests / route.sh

148 lines · 8562 bytes Raw Blame History
1#!/usr/bin/env bash
2# galaxy/tests/route.sh: gate for the galaxy routes in server/route.om.
3# bash galaxy/tests/route.sh (spark; FORGED_CPUS default 4,5; FORGE_TEST_PORT default 18489)
4# A trial forged with its port, RIDS.md, storage, data dir and count sources redirected into a temp dir (the
5# server/tests/check.sh pattern). Checks: on base "/" /galaxy streams the page file byte for byte
6# with the site's no-script policy, the root stays the explore page whether or not the page file
7# exists, /assets/galaxy.css and /assets/tokens.css stream their files, POST is refused, a missing
8# page file is 404 at /galaxy, and the /forge/ base never serves the galaxy. Negative controls: the
9# route.om of the base commit must not serve the page at /galaxy, and a route.om whose root serves
10# the galaxy file must be seen by the root check.
11# Read-only on real storage; never touches 18480, ~/forge-data or port 18091.
12set -u
13cd "$(dirname "$0")/../.."
14CORES=${FORGED_CPUS:-4,5}
15PORT=${FORGE_TEST_PORT:-18489}
16OMC=$HOME/work/flux/omega-mv/seed/out/omc
17STORE=$HOME/.radicle/storage
18OK_RID=z631r2JJiVT8it4tST7RK24hpowi # cuda-crack
19BASE=${GALAXY_BASE:-edge/spark-front-door}
20T=$(mktemp -d)
21pid=
22pass=0; fail=0
23ok() { pass=$((pass+1)); echo "PASS $*"; }
24bad() { fail=$((fail+1)); echo "FAIL $*"; }
25cleanup() { [ -n "$pid" ] && kill "$pid" 2>/dev/null; wait 2>/dev/null; rm -rf "$T"; }
26trap cleanup EXIT
27if ss -ltn | grep -q "127.0.0.1:$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi
28run() { taskset -c "$CORES" nice -n 10 "$@"; }
29want() { [ "$2" = "$3" ] && ok "$1 = $2" || bad "$1 = $2 (want $3)"; }
30
31compose() { # compose OUT [route.om]
32 local route=${2:-server/route.om}
33 { printf 'om2\n'; for u in $(./run.sh units); do
34 [ "$u" = server/route.om ] && cat "$route" || cat "$u"; done; } > "$1"
35}
36redirect() {
37 sed -i -e "s|haddr4 a 2130706433 18480|haddr4 a 2130706433 $PORT|" \
38 -e "s|/home/drakestapleton/handoffs/radicle/RIDS.md|$T/RIDS.md|" \
39 -e "s|/home/drakestapleton/.radicle/storage/|$T/storage/|g" \
40 -e "s|/home/drakestapleton/forge-data/|$T/data/|g" \
41 -e "s|^text tfzp0 \".*\"|text tfzp0 \"$T/ng/dl.log\"|" \
42 -e "s|^text tfzp1 \".*\"|text tfzp1 \"$T/ng/access.log\"|" \
43 -e "s|^text tfzwal \".*\"|text tfzwal \"$T/ng/wal\"|" \
44 -e "s|^text tfzrad \".*\"|text tfzrad \"$T/bin/rad\"|" \
45 -e "s|^text tfzspth \".*\"|text tfzspth \"$T/counts.snapshot\"|" \
46 -e "s|^text tfzgent \".*\"|text tfzgent \"$T/bin/getent\"|" "$1"
47 if grep -q '2130706433 18480\|/home/drakestapleton/forge-data\|/home/drakestapleton/.radicle\|/home/drakestapleton/handoffs/radicle' "$1"; then bad "redirect: a live path or port remains"; exit 1; fi
48}
49start() {
50 run "$OMC" < "$1" > "$1.bin" 2> "$T/build.err" || { bad "build $1: $(cat "$T/build.err")"; exit 1; }
51 chmod 755 "$1.bin"
52 taskset -c "$CORES" nice -n 10 "$1.bin" > "$1.log" 2>&1 &
53 pid=$!
54 for _ in $(seq 50); do curl -s -o /dev/null "http://127.0.0.1:$PORT/explore" && return; sleep 0.2; done
55 bad "trial forged did not listen on $PORT"; exit 1
56}
57stop() {
58 kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=
59 for _ in $(seq 50); do ss -ltn | grep -q "127.0.0.1:$PORT " || return; sleep 0.2; done
60 bad "trial forged still listening on $PORT"; exit 1
61}
62get() { # get PATH [BASE]
63 curl -s -o "$T/b" -D "$T/h" -w '%{http_code}' -H "X-Forge-Base: ${2:-/}" -H 'X-Real-IP: 10.9.9.9' "http://127.0.0.1:$PORT/$1"
64}
65hdr() { grep -i "^$1:" "$T/h" | tr -d '\r' | cut -d' ' -f2-; }
66
67mkdir -p "$T/storage" "$T/data/logs" "$T/data/galaxy" "$T/ng" "$T/bin"
68# the count sources of server/counts.om (json-counts) are moved too, as server/tests/check.sh does
69cp server/tests/fake/rad server/tests/fake/getent "$T/bin/"
70chmod 755 "$T/bin/rad" "$T/bin/getent"
71: > "$T/ng/dl.log"; : > "$T/ng/access.log"
72ln -s "$STORE/$OK_RID" "$T/storage/$OK_RID"
73printf '# test table\n\n| repo | RID | default branch | head sha | source dir | UTC |\n|---|---|---|---|---|---|\n| cuda-crack | rad:%s | main | - | - | - |\n' "$OK_RID" > "$T/RIDS.md"
74
75# the page files, made by the filter from the fixture
76{ cat galaxy/page.om galaxy/cli.om; } | run "$OMC" > "$T/cli" 2> "$T/cli.err" || { bad "filter build: $(cat "$T/cli.err")"; exit 1; }
77chmod 755 "$T/cli"
78"$T/cli" < galaxy/tests/feed.tsv > "$T/data/galaxy/index.html"
79printf 'css\n' | "$T/cli" > "$T/data/galaxy/galaxy.css"
80cp galaxy/tokens.css "$T/data/galaxy/tokens.css"
81
82compose "$T/f.om"; redirect "$T/f.om"
83start "$T/f.om"
84want "GET / on base /" "$(get '')" 200
85grep -q 'Explore repositories' "$T/b" && ok "root on base / stays the explore page with the galaxy file present" || bad "root on base / stays the explore page with the galaxy file present"
86cmp -s "$T/b" "$T/data/galaxy/index.html" && bad "root body is not the galaxy file" || ok "root body is not the galaxy file"
87want "GET /galaxy" "$(get galaxy)" 200
88cmp -s "$T/b" "$T/data/galaxy/index.html" && ok "/galaxy body is the page file, byte for byte" || bad "/galaxy body is the page file, byte for byte"
89want "/galaxy content type" "$(hdr content-type)" "text/html; charset=utf-8"
90case "$(hdr content-security-policy)" in *"script-src 'none'"*"style-src 'self'"*|*"style-src 'self'"*"script-src 'none'"*) ok "/galaxy policy: no scripts, own stylesheets only" ;; *) bad "/galaxy policy: $(hdr content-security-policy)" ;; esac
91want "GET /galaxy/ (trailing slash)" "$(get galaxy/)" 200
92cmp -s "$T/b" "$T/data/galaxy/index.html" && ok "/galaxy/ body is the page file" || bad "/galaxy/ body is the page file"
93want "GET /assets/galaxy.css" "$(get assets/galaxy.css)" 200
94cmp -s "$T/b" "$T/data/galaxy/galaxy.css" && ok "stylesheet body" || bad "stylesheet body"
95want "stylesheet content type" "$(hdr content-type)" "text/css; charset=utf-8"
96want "GET /assets/tokens.css" "$(get assets/tokens.css)" 200
97cmp -s "$T/b" "$T/data/galaxy/tokens.css" && ok "tokens body" || bad "tokens body"
98want "GET /assets/forge.css still served" "$(get assets/forge.css)" 200
99want "POST / refused" "$(curl -s -o /dev/null -w '%{http_code}' -X POST -H 'X-Forge-Base: /' -H 'Origin: https://interplane.io' --data x=1 "http://127.0.0.1:$PORT/")" 405
100want "GET / on base /forge/" "$(get '' /forge/)" 200
101grep -q 'Explore repositories' "$T/b" && ok "/forge/ base root stays the explore page" || bad "/forge/ base root stays the explore page"
102want "GET /assets/galaxy.css on base /forge/" "$(get assets/galaxy.css /forge/)" 404
103want "GET /explore still served" "$(get explore)" 200
104want "GET drakestapleton/cuda-crack still served" "$(get drakestapleton/cuda-crack)" 200
105mv "$T/data/galaxy/index.html" "$T/data/galaxy/index.html.off"
106want "GET /galaxy without the page file" "$(get galaxy)" 404
107want "GET / without the page file" "$(get '')" 200
108grep -q 'Explore repositories' "$T/b" && ok "without the page file the root is the explore page" || bad "without the page file the root is the explore page"
109want "GET /assets/galaxy.css without tokens touched" "$(get assets/galaxy.css)" 200
110mv "$T/data/galaxy/galaxy.css" "$T/data/galaxy/galaxy.css.off"
111want "GET a missing stylesheet" "$(get assets/galaxy.css)" 404
112mv "$T/data/galaxy/index.html.off" "$T/data/galaxy/index.html"
113stop
114
115# negative control 1: the base commit's route.om has no galaxy; /galaxy must not be the page there
116git show "$BASE:server/route.om" > "$T/oldroute.om"
117if grep -q 'tfgxi' "$T/oldroute.om"; then
118 bad "negative control: base route.om already has the galaxy routes"
119else
120 compose "$T/n.om" "$T/oldroute.om"; redirect "$T/n.om"
121 start "$T/n.om"
122 code=$(get galaxy)
123 if [ "$code" != 200 ] || ! cmp -s "$T/b" "$T/data/galaxy/index.html"; then
124 ok "negative control: base route.om does not serve the page at /galaxy ($code)"
125 else
126 bad "negative control: base route.om served the page at /galaxy"
127 fi
128 stop
129fi
130# negative control 2: a route.om whose root serves the galaxy file (forge/galaxy-live 8f81e8fc did)
131# must fail the "root stays the explore page" check
132sed 's|^note the root stays the explore page; the galaxy is its own page at /galaxy (fpgone), the map$|if fgxhas\nret fgxsend tfgxi tfgxhtm\nend|' server/route.om > "$T/rootgx.om"
133if cmp -s server/route.om "$T/rootgx.om"; then
134 bad "negative control 2: the mutant equals route.om"
135else
136 compose "$T/m.om" "$T/rootgx.om"; redirect "$T/m.om"
137 start "$T/m.om"
138 code=$(get '')
139 if [ "$code" = 200 ] && cmp -s "$T/b" "$T/data/galaxy/index.html"; then
140 ok "negative control 2: a root that serves the galaxy is seen (the root check would fail)"
141 else
142 bad "negative control 2: mutant root gave $code and not the galaxy file"
143 fi
144 stop
145fi
146
147echo "RESULT pass=$pass fail=$fail"
148[ "$fail" -eq 0 ]