summaryrefslogtreecommitdiffstats
path: root/test/run.sh
diff options
context:
space:
mode:
Diffstat (limited to 'test/run.sh')
-rwxr-xr-xtest/run.sh71
1 files changed, 70 insertions, 1 deletions
diff --git a/test/run.sh b/test/run.sh
index ab1de2d..39f26b7 100755
--- a/test/run.sh
+++ b/test/run.sh
@@ -144,7 +144,7 @@ erin secrets admin
EOF
# A repository directory, and the hook name this binary is reached under.
-mkdir -p "$TMP/root/team.git" "$TMP/bin"
+mkdir -p "$TMP/root/team.git" "$TMP/bin" "$TMP/empty"
ln -sf "$AUTHZ_ABS" "$TMP/bin/pre-receive"
if [ ! -x "$TMP/bin/pre-receive" ]; then
cp "$AUTHZ" "$TMP/bin/pre-receive"
@@ -427,6 +427,75 @@ out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
GIT_REPO_ROOT=/var/lib/git "$AUTHZ_ABS" --policy "$P" authorize --no-git --explain 2>&1 )); rc=$?
expect "and the last path component works when it is not the root" 0 "team" "$rc" "$out"
+# ------------------------------------------- the two facts that belong to git
+#
+# Everything else the hook knows it works out of the line git gave it. Two
+# facts do not come that way: whether HEAD is a branch, and whether old is an
+# ancestor of new. Those are asked of git, and asking them is a code path the
+# rest of this suite cannot reach, because there is no git in it.
+#
+# So: a stand-in named git, and the real one kept out of PATH. A fake that
+# answers whatever it is told is worth more here than the real thing, which
+# would answer a question about the host instead of the question the hook asked.
+
+mkdir -p "$TMP/fakegit"
+cat >"$TMP/fakegit/git" <<'EOF'
+#!/bin/sh
+case $1 in
+symbolic-ref) printf '%s\n' "${FAKE_HEAD:-refs/heads/main}"; exit 0 ;;
+merge-base) exit "${FAKE_FF:-1}" ;;
+esac
+exit 125
+EOF
+chmod 755 "$TMP/fakegit/git"
+
+# hookgit <user> <dir> [PATH=...] - the hook, with the fake git in front.
+hookgit() {
+ user=$1
+ dir=$2
+ shift 2
+ ( cd "$dir" && env "$@" GIT_HOST_USER="$user" GIT_HOST_AUTHZ="$POLICY" \
+ "$TMP/bin/pre-receive" "$dir" 2>&1 )
+}
+
+section "what the hook asks git"
+
+POLICY="$TMP/authz.conf"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
+expect "a deletion is force, not admin, when HEAD is somewhere else" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main |
+ hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
+expect "but the branch HEAD points at may not go" 1 "needs admin" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/main |
+ hookgit dave "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
+expect "which is what an admin is for" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ hookgit carol "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/feature/x FAKE_FF=1); rc=$?
+expect "and it is HEAD that decides, not the name of the branch" 1 "needs admin" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=0); rc=$?
+expect "an update git calls a fast-forward costs write" 0 "-" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ hookgit bob "$TMP/root/team.git" PATH="$TMP/fakegit" FAKE_HEAD=refs/heads/main FAKE_FF=1); rc=$?
+expect "and one it does not, force" 1 "needs force" "$rc" "$out"
+
+# No git at all: the hook cannot ask, so it must not guess in the caller's
+# favour. A deletion it cannot place is charged the most, not the least.
+out=$(printf '%s %s %s\n' "$A" "$ZERO" refs/heads/feature/x |
+ hookgit carol "$TMP/root/team.git" PATH="$TMP/empty"); rc=$?
+expect "with no git to ask, a deletion is the expensive kind" 1 "needs admin" "$rc" "$out"
+
+out=$(printf '%s %s %s\n' "$A" "$B" refs/heads/feature/x |
+ hookgit bob "$TMP/root/team.git" PATH="$TMP/empty"); rc=$?
+expect "and an update cannot be shown to be a fast-forward" 1 "needs force" "$rc" "$out"
+
# -------------------------------------------------------------- fail closed
section "fail closed"