#!/bin/sh # Tests for gotea-cgi. git is used only to create fixtures. set -eu here=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) cgi=$(dirname "$here") bin="$cgi/gotea-cgi" tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT HUP INT TERM fail() { echo "FAIL: $*" >&2 exit 1 } contains() { printf '%s' "$1" | grep -Fq -- "$2" } expect_contains() { contains "$1" "$2" || fail "$3 (missing: $2)" } expect_no() { if contains "$1" "$2"; then fail "$3 (found: $2)" fi } run() { env -i PATH=/usr/bin:/bin HOME="$tmp" \ GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \ SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD="${2:-GET}" \ PATH_INFO="$1" "$bin" } run_ref() { env -i PATH=/usr/bin:/bin HOME="$tmp" \ GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \ SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD=GET \ QUERY_STRING="$2" PATH_INFO="$1" "$bin" } run_palette() { env -i PATH=/usr/bin:/bin HOME="$tmp" \ GOTEA_REPO_ROOT="$tmp/repos" GOT_BIN="${GOT_BIN:-/usr/bin/got}" \ GOTEA_PALETTE="$2" SCRIPT_NAME=/cgi-bin/gotea \ REQUEST_METHOD=GET PATH_INFO="$1" "$bin" } make -C "$cgi" >/dev/null mkdir -p "$tmp/repos/owner" git init -q -b main "$tmp/src" cd "$tmp/src" printf 'hello gotea\n' > README.md mkdir src printf 'int main(void){return 0;}\n' > src/main.c printf '\000\001\002' > src/logo.bin git add . GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \ GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \ git commit -qm 'initial import' git checkout -q -b dev printf 'dev branch\n' > dev.txt git add dev.txt GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \ GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \ git commit -qm 'dev branch' git checkout -q main cd "$cgi" hash=$(git -C "$tmp/src" rev-parse HEAD) git clone -q --bare "$tmp/src" "$tmp/repos/owner/demo.git" git clone -q --bare "$tmp/src" "$tmp/repos/flat.git" git init -q --bare "$tmp/repos/owner/empty.git" home=$(run /) expect_contains "$home" 'DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' "home: HTML 4.01 doctype" expect_contains "$home" 'GOTEA' "home: brand" expect_contains "$home" 'owner/demo' "home: nested repository" expect_contains "$home" 'flat' "home: flat repository" expect_contains "$home" 'empty repository' "home: empty repository" expect_no "$home" 'dev' "ref: current branch marked" maintree=$(run /repo/owner/demo/tree) expect_no "$maintree" 'dev.txt' "ref: main stays main" devblob=$(run_ref /repo/owner/demo/blob/dev.txt ref=refs/heads/dev) expect_contains "$devblob" 'dev branch' "ref: blob from dev" badref=$(run_ref /repo/owner/demo/tree ref=../x) expect_contains "$badref" 'Status: 400' "ref: traversal rejected" echo "PASS refs" sub=$(run /repo/owner/demo/tree/src) expect_contains "$sub" 'main.c' "subtree: file" expect_contains "$sub" 'logo.bin' "subtree: binary file" echo "PASS subtree" blob=$(run /repo/owner/demo/blob/README.md) expect_contains "$blob" '
hello gotea' "blob: content"
expect_contains "$blob" '/raw/README.md' "blob: raw link"
expect_contains "$blob" '[history]' "blob: history link"
echo "PASS blob"

raw=$(run /repo/owner/demo/raw/README.md)
expect_contains "$raw" 'Content-Type: text/plain; charset=UTF-8' "raw: content type"
expect_contains "$raw" 'hello gotea' "raw: content"
echo "PASS raw"

binblob=$(run /repo/owner/demo/blob/src/logo.bin)
expect_contains "$binblob" 'Binary file' "binary: notice"
echo "PASS binary"

log=$(run /repo/owner/demo/log)
expect_contains "$log" 'commit ' "log: commit line"
expect_contains "$log" 'initial import' "log: message"
expect_contains "$log" "$hash" "log: full hash"
echo "PASS log"

commit=$(run "/repo/owner/demo/commit/$hash")
expect_contains "$commit" 'initial import' "commit: message"
expect_contains "$commit" 'diff ' "commit: patch"
echo "PASS commit"

notfound=$(run /repo/nope/tree)
expect_contains "$notfound" 'Status: 404' "missing repo: 404"
echo "PASS 404"

missingfile=$(run /repo/owner/demo/blob/nope.txt)
expect_contains "$missingfile" 'Status: 404' "missing file: 404"
echo "PASS missing file"

badpath=$(run /repo/../tree)
expect_contains "$badpath" 'Status: 400' "traversal: 400"
echo "PASS traversal"

head=$(run /repo/owner/demo/tree HEAD)
expect_contains "$head" 'Content-Type: text/html; charset=UTF-8' "HEAD: headers"
expect_no "$head" ' "$tmp/pal.palette"
pal=$(run_palette / "$tmp/pal.palette")
expect_contains "$pal" '#123456' "palette: custom base colour"
expect_contains "$pal" 'bgcolor="#123456"' "palette: body fallback"
echo "PASS palette"

pal_sample=$(run_palette / "$cgi/palettes/cde-default.palette")
expect_contains "$pal_sample" 'bgcolor="#a0a0a0"' \
    "palette: key=value sample with comments"
echo "PASS palette sample"

pal_dp=$(run_palette / "$cgi/palettes/cde-default.dp")
expect_contains "$pal_dp" 'bgcolor="#c6b2a8"' "palette: .dp background"
expect_contains "$pal_dp" 'pre{background-color:#686f82;color:#ffffff' \
    "palette: .dp text area contrast"
echo "PASS palette dp"

flat=$(run /repo/flat/tree)
expect_contains "$flat" 'README.md' "flat: tree"
echo "PASS flat"

bb="$cgi/../third_party/bb/bb"
"$bb" init "$tmp/bb-src" >/dev/null
cd "$tmp/bb-src"
rm -rf dist build
git init -q -b main
git add .
GIT_AUTHOR_NAME='Test User' GIT_AUTHOR_EMAIL='test@example.com' \
GIT_COMMITTER_NAME='Test User' GIT_COMMITTER_EMAIL='test@example.com' \
    git commit -qm 'pages site'
git branch pages
git clone -q --bare . "$tmp/repos/owner/site.git"
bb_rev=$(git rev-parse pages)
mkdir -p "$tmp/pages"
printf 'owner/site\n' > "$tmp/allow"
refs_before=$(got ref -l -r "$tmp/repos/owner/site.git" |
    grep -c 'refs/got/worktree' || true)

GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_BB="$bb" \
    "$cgi/pages/publish.sh" "$tmp/repos/owner/site.git" owner/site \
    "$bb_rev" >/dev/null
[ -f "$tmp/pages/owner/site/index.html" ] ||
    fail "publish: no index.html"
expect_contains "$(cat "$tmp/pages/owner/site/index.html")" \
    ' $refs_after)"
echo "PASS publish"

pages_run() {
	env -i PATH=/usr/bin:/bin HOME="$tmp" \
	    GOTEA_PAGES_HOST=pages.test HTTP_HOST="${2:-pages.test}" \
	    GOTEA_PAGES_ROOT="$tmp/pages" GOTEA_PAGES_ALLOW="$tmp/allow" \
	    SCRIPT_NAME=/cgi-bin/gotea REQUEST_METHOD=GET PATH_INFO="$1" "$bin"
}

pages_home=$(pages_run /)
expect_contains "$pages_home" 'GOTEA PAGES' "pages: index banner"
expect_contains "$pages_home" 'owner/site' "pages: allowlisted site"
echo "PASS pages index"

pages_site=$(pages_run /owner/site/)
expect_contains "$pages_site" 'Content-Type: text/html; charset=UTF-8' \
    "pages: html content type"
expect_contains "$pages_site" '/dev/null
hookout=$(printf '%040d %s refs/heads/pages\n' 0 "$bb_rev" |
    env -i PATH=/usr/bin:/bin GIT_DIR="$tmp/repos/owner/site.git" \
    "$tmp/repos/owner/site.git/hooks/post-receive.d/gotea-pages" 2>&1)
expect_contains "$hookout" "published owner/site" "hook: publish output"
refs_hook=$(got ref -l -r "$tmp/repos/owner/site.git" |
    grep -c 'refs/got/worktree' || true)
[ "$refs_before" = "$refs_hook" ] ||
    fail "hook: worktree ref leaked ($refs_before -> $refs_hook)"
echo "PASS post-receive hook"

echo "all gotea-cgi tests passed"