interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / visits / tests / owner.sh

79 lines · 4351 bytes Raw Blame History
1#!/usr/bin/env bash
2# visits/tests/owner.sh: gate for owner.om (the owner's pages inside the private network).
3# OMV=/abs/omega-mv OWNER_TEST_PORT=18492 bash visits/tests/owner.sh
4# Starts the server on 127.0.0.1 in a temp dir and checks the three files byte for byte, the
5# headers, 404 and 405, and that it refuses every address that is not loopback or tailnet.
6# Negative control: a mutant without the address rule does start on 0.0.0.0.
7set -u
8HERE=$(cd "$(dirname "$0")" && pwd)
9V=$HERE/..
10OMV=${OMV:-$HOME/canon/omega-mv}
11OMC=${OMC:-$OMV/seed/out/omc}
12PIN=34d611ca0415eab1728d3234911d90e74af17ba9ab0c581d559f48274d7ca6d3
13PORT=${OWNER_TEST_PORT:-18492}
14T=$(mktemp -d) || exit 2
15pid=
16pass=0; fail=0
17ok() { pass=$((pass + 1)); echo "PASS $1"; }
18no() { fail=$((fail + 1)); echo "FAIL $1"; }
19eq() { if [ "$2" = "$3" ]; then ok "$1"; else no "$1: got '$2' want '$3'"; fi; }
20cleanup() { [ -n "$pid" ] && kill "$pid" 2>/dev/null; wait 2>/dev/null; rm -rf "$T"; }
21trap cleanup EXIT
22got=$(sha256sum "$OMC" 2>/dev/null | cut -d' ' -f1)
23if [ "$got" != "$PIN" ]; then echo "compiler pin mismatch: $got" >&2; exit 2; fi
24if ss -ltn | grep -q ":$PORT "; then echo "NOT_RUN: port $PORT busy"; exit 3; fi
25build() { { printf 'om2\n'; cat "$OMV/seed/net/http.om" "$OMV/seed/net/notls.om" "$1"; } | "$OMC" >"$2" 2>"$2.err" && chmod +x "$2"; }
26build "$V/owner.om" "$T/owner"
27eq "compile exit" "$?" 0
28
29mkdir "$T/w"
30printf '<!doctype html><title>t</title><p>owner page &amp; more</p>\n' >"$T/w/visits.html"
31cp "$V/visits.css" "$T/w/visits.css"
32head -c 300000 /dev/urandom | base64 >"$T/w/tokens.css"
33printf 'secret\n' >"$T/w/other.txt"
34(cd "$T/w" && exec "$T/owner" 127 0 0 1 "$PORT") &
35pid=$!
36for _ in $(seq 50); do curl -s -o /dev/null "http://127.0.0.1:$PORT/" && break; sleep 0.1; done
37get() { curl -s -o "$T/b" -D "$T/h" -w '%{http_code}' "$@"; }
38hdr() { grep -i "^$1:" "$T/h" | tr -d '\r' | cut -d' ' -f2-; }
39
40eq "GET /" "$(get "http://127.0.0.1:$PORT/")" 200
41if cmp -s "$T/b" "$T/w/visits.html"; then ok "page body, byte for byte"; else no "page body, byte for byte"; fi
42eq "page content type" "$(hdr content-type)" "text/html; charset=utf-8"
43eq "never cached" "$(hdr cache-control)" "no-store"
44eq "never indexed" "$(hdr x-robots-tag)" "noindex"
45case "$(hdr content-security-policy)" in "default-src 'none'"*) ok "policy allows no script" ;; *) no "policy: $(hdr content-security-policy)" ;; esac
46eq "GET /visits.css" "$(get "http://127.0.0.1:$PORT/visits.css")" 200
47if cmp -s "$T/b" "$T/w/visits.css"; then ok "stylesheet body"; else no "stylesheet body"; fi
48eq "stylesheet content type" "$(hdr content-type)" "text/css; charset=utf-8"
49eq "GET /tokens.css (405,000 bytes)" "$(get "http://127.0.0.1:$PORT/tokens.css")" 200
50if cmp -s "$T/b" "$T/w/tokens.css"; then ok "large file body, byte for byte"; else no "large file body, byte for byte"; fi
51eq "a file that is not one of the three" "$(get "http://127.0.0.1:$PORT/other.txt")" 404
52eq "a path that climbs out" "$(get --path-as-is "http://127.0.0.1:$PORT/../w/other.txt")" 404
53eq "POST refused" "$(get -X POST --data x=1 "http://127.0.0.1:$PORT/")" 405
54eq "query string still serves the page" "$(get "http://127.0.0.1:$PORT/?x=1")" 200
55rm "$T/w/visits.html"
56eq "page file missing" "$(get "http://127.0.0.1:$PORT/")" 404
57kill "$pid" 2>/dev/null; wait "$pid" 2>/dev/null; pid=
58
59# addresses: only loopback and tailnet may be used
60for a in "0 0 0 0" "192 168 1 108" "8 8 8 8" "100 63 0 2" "100 128 0 2" "10 0 0 1"; do
61 (cd "$T/w" && timeout 3 "$T/owner" $a "$PORT") >/dev/null 2>&1
62 eq "refuses to start on ${a// /.}" "$?" 1
63done
64(cd "$T/w" && timeout 3 "$T/owner" 127 0 0 1) >/dev/null 2>&1; eq "missing port refused" "$?" 1
65(cd "$T/w" && timeout 3 "$T/owner" 127 0 0 1 70000) >/dev/null 2>&1; eq "port out of range refused" "$?" 1
66(cd "$T/w" && timeout 3 "$T/owner" 127 0 0 x "$PORT") >/dev/null 2>&1; eq "non-number refused" "$?" 1
67
68# negative control: without the address rule the server starts on 0.0.0.0 (timeout ends it: 124)
69sed 's/^if not oaddrok a b$/if false/' "$V/owner.om" >"$T/mutant.om"
70if cmp -s "$V/owner.om" "$T/mutant.om"; then
71 no "mutant differs from source"
72else
73 build "$T/mutant.om" "$T/mutant"
74 (cd "$T/w" && timeout 2 "$T/mutant" 0 0 0 0 "$PORT") >/dev/null 2>&1
75 eq "mutant without the address rule starts on 0.0.0.0" "$?" 124
76fi
77
78echo "RESULT pass=$pass fail=$fail"
79[ "$fail" -eq 0 ]