summaryrefslogtreecommitdiffstats
path: root/test/run.sh
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 01:05:08 +0000
committerdev <dev@example>2026-11-14 01:05:08 +0000
commit312ddd4936f09ae27a180ca495cacf2c06faf33e (patch)
tree2fa46ab568ad55f7e8fb844c3447b1d296b2e340 /test/run.sh
parente737f3d560de2b8172e6dd83901bc1359283303c (diff)
downloadaccess-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.tar.gz
access-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.zip
Comments: cut to what the code cannot say
The comments had grown into essays. A file header explaining why the file exists, then a paragraph above each function restating what the next eight lines do, then a paragraph above that one explaining why that matters. The reader gets no further than the first paragraph, and the second is the one that goes stale: it describes code that has since changed, and nothing fails when it does. Cut by about half. What is left is the three things a comment can do that code cannot: say why the constraint exists (no regex because a hook must not depend on what might be missing at 3am), say what is not obvious from the shape (a bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the shell), and say where it hurts (the NULL in an execvp array, the mode and owner that have to survive an atomic rewrite, the child that must not keep the hook's stdin). Block comments became // lines, which is also what the other C trees here use, and the file headers went to two or three lines. Section banners stayed: they are navigation, not prose. Behaviour is unchanged and the suites prove it: 171 direct checks, 162 behavioural, clean under -Werror and under ASan/UBSan.
Diffstat (limited to 'test/run.sh')
-rwxr-xr-xtest/run.sh71
1 files changed, 29 insertions, 42 deletions
diff --git a/test/run.sh b/test/run.sh
index 39f26b7..bc1a9e4 100755
--- a/test/run.sh
+++ b/test/run.sh
@@ -1,16 +1,11 @@
#!/bin/sh
-# run.sh - drive git-authz the way git does, without git.
+# Drive git-authz the way git does, without git: identity in the environment,
+# "old new ref" on stdin, the repository as cwd. The two facts only git knows are
+# supplied with --ff and --head, so no repository or commits are needed.
#
-# Everything a receive hook knows, this feeds it: an identity in the
-# environment, "old new ref" lines on stdin, the repository as the working
-# directory. What only the object store knows - whether old is an ancestor of
-# new, and which branch HEAD points at - is supplied explicitly (--ff, --head),
-# so the suite is deterministic and needs no repository, no commits, and no
-# git. hook.c asks git those questions in production; the answers it gets are
-# what the tests below pin the consequences of.
-#
-# POSIX sh: this runs under dash, busybox ash and bash alike, because the
-# container it has to survive is Alpine.
+# POSIX sh: runs under dash, busybox ash and bash, because the container it has to
+# survive is Alpine.
+
AUTHZ=${AUTHZ:-./git-authz}
if [ ! -x "$AUTHZ" ]; then
@@ -91,9 +86,9 @@ do_check() {
"$AUTHZ" --policy "$POLICY" check "$@" 2>&1
}
-# Which policy the two drivers below read. Overridden per test with
-# "out=$(POLICY=something push ...)", which is a subshell and therefore does not
-# leak into the next test.
+# Overridden per test with "out=$(POLICY=something push ...)", a subshell, so it
+# does not leak into the next test.
+
POLICY=$P
# push <user|none> <repo> [extra authorize args] (ref lines on stdin)
@@ -110,12 +105,10 @@ push() {
fi
}
-# hook <user|none> <repodir> (invoked through the hook name, as git does, with
-# the repository as the working directory and git's own argument in hand)
-# Reached the way core.hooksPath reaches it: by name, with the repository as the
-# working directory, git's own argument in hand (which is not ours, and must not
-# be read as one), and no --policy anywhere - the hook gets its policy from
-# GIT_HOST_AUTHZ or the default path, exactly as a deployed one does.
+# hook <user|none> <repodir>
+# The way core.hooksPath reaches it: by name, repo as cwd, git's own argument in
+# hand (not ours, and must not be read as one), no --policy anywhere.
+
hook() {
user=$1
dir=$2
@@ -154,8 +147,8 @@ if [ ! -L "$TMP/bin/pre-receive" ]; then
skip_it "hook reached by its own name" "cannot make a symlink here"
fi
-# A policy with no '@all' line, for the tests that need a repository and a
-# stranger rather than a policy that reaches everywhere.
+# No '@all' line: for tests that need a stranger and a specific repository.
+
cat >"$TMP/plain.conf" <<'EOF'
alice * admin
bob team write refs/heads/feature/*
@@ -429,14 +422,10 @@ expect "and the last path component works when it is not the root" 0 "team" "$rc
# ------------------------------------------- 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.
+# The two questions the rest of this suite cannot reach, because there is no git
+# in it. A stand-in named git answers to order, with the real one kept out of
+# PATH: the host's git would answer a different question than the one asked.
+
mkdir -p "$TMP/fakegit"
cat >"$TMP/fakegit/git" <<'EOF'
@@ -486,8 +475,8 @@ 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.
+# No git to ask: the hook must not guess in the caller's favour.
+
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"
@@ -746,10 +735,9 @@ else
bad "--stdout lost the comments or the settings"
fi
-# A file whose *directory* is read-only cannot be replaced, which is what a
-# read-only ConfigMap mount means in practice. A 0444 file in a writable
-# directory is a different thing and worth pinning too: the grant succeeds and
-# the mode survives, because it is the directory that decides.
+# It is the directory that decides: a read-only mount is a read-only directory,
+# while a 0444 file in a writable directory is still replaceable.
+
cp "$TMP/edit.conf" "$TMP/ro.conf"
chmod 444 "$TMP/ro.conf"
out=$("$AUTHZ" --policy "$TMP/ro.conf" grant bob team admin 2>&1); rc=$?
@@ -808,9 +796,9 @@ else
out=$(make install DESTDIR="$S" 2>&1); rc=$?
expect "make install into a DESTDIR" 0 "pre-receive" "$rc" "$out"
- # The hook has to be a relative link: staged, unpacked, or shipped in an
- # image, it must point at the binary that came with it rather than at
- # whatever occupies that absolute path on the machine reading it.
+ # A relative link: an absolute one points at whatever occupies that path on
+ # whatever machine reads it.
+
link=$(ls -l "$S/usr/local/share/git-host/hooks/pre-receive")
case $link in
*'= /'*) bad "the installed hook is an absolute symlink: $link" ;;
@@ -818,9 +806,8 @@ else
*) bad "the installed hook is not a relative symlink: $link" ;;
esac
- # End to end through the installed name, with the installed policy, in a
- # repository under an installed root: nothing but the paths from the
- # installer here.
+ # Nothing here but the paths the installer chose.
+
mkdir -p "$S/var/lib/git/team.git"
out=$(printf '%s %s %s\n' "$ZERO" "$A" refs/heads/feature/x |
( cd "$S/var/lib/git/team.git" && env GIT_HOST_USER=bob \