diff options
| author | dev <dev@example> | 2026-11-14 00:06:35 +0000 |
|---|---|---|
| committer | dev <dev@example> | 2026-11-14 00:06:35 +0000 |
| commit | bc7f4da406fbd802b08985ba583e52f040bdba85 (patch) | |
| tree | fa5020a3781dd80dcba654a3417d22d02df4c2da /test/check.c | |
| parent | 50f296cf7609ce77d41b9fb3df51d6aeec8330d6 (diff) | |
| download | access-control-bc7f4da406fbd802b08985ba583e52f040bdba85.tar.gz access-control-bc7f4da406fbd802b08985ba583e52f040bdba85.zip | |
Layout: cmd/, src/, test/, everything built into obj/
The tree had grown sideways: main.c living among the sources it dispatches to,
one test binary covering everything, object files left in the source directory
because the Makefile put them there and, worse, tracked them.
Now the command line is cmd/main.c, the engine is src/ and nothing in it knows
about the command line, and each area of the engine has its own program under
test/ - glob, norm, parse, need, check - each linking the library and reporting
its own count. A failure says which area broke instead of taking the whole run
down with a single "unit: 171" line, which is the only thing a per-file split
buys and it is worth the small duplication the shared test.h absorbs.
Objects go to obj/, test runners to obj/test_runner/, so a build leaves the
tree as it found it. The stale src/*.o and tests/unit that were committed
earlier are gone; they were build output in a source tree and the .gitignore
that should have stopped them arrived after they did.
The Makefile follows the same shape as the other C trees here: directories in
variables, wildcards for the sources, one pattern rule that makes its own
directory, test runners built from the same objects as the binary so they
cannot test a different build than ships, and `test` running every runner with
a foreach so the first failure stops the line. `strict` is `all` with -Werror,
which is what CI now calls instead of passing CFLAGS in from outside; the
sanitizer and install targets are unchanged in what they do.
Behaviour is unchanged: 171 direct checks and 154 behavioural ones, same as
before the move, and make install still stages a working hook symlink.
Diffstat (limited to 'test/check.c')
| -rw-r--r-- | test/check.c | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/test/check.c b/test/check.c new file mode 100644 index 0000000..3df2ade --- /dev/null +++ b/test/check.c @@ -0,0 +1,171 @@ +/* check.c - The decision, and the sentence that goes with it. A refusal has to name + * the level held, the ref and the level required, because a person who is + * refused should be able to fix it without opening the policy first. + * + * Part of the direct suite: this links the library, not the command + * line. test/run.sh is the other half, and drives the program the way git does. + */ +#include "test.h" + +static void test_check(void) +{ + policy_t p; + int rc; + + rc = load_text(&p, + "alice * admin\n" + "@all * write refs/heads/feature/*\n" + "frank team write refs/heads/main refs/tags/*\n" + "bob team write refs/heads/feature/*\n" + "carol team force refs/heads/feature/*\n" + "dave team admin\n" + "erin secrets admin\n"); + CHECK_INT(rc, 0); + + /* the ladder */ + expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/feature/x"); + expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x", "needs force"); + expect_deny(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", "needs admin"); + expect_allow(&p, "carol", "team", LV_FORCE, "refs/heads/feature/x"); + expect_allow(&p, "dave", "team", LV_ADMIN, "refs/heads/main"); + + /* strongest match wins, additively */ + expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/feature/x"); + expect_allow(&p, "alice", "anything", LV_ADMIN, "refs/heads/main"); + + /* '@all' reaches the named, and nobody else. Note that the '@all *' rule + * below reaches every repository, so a named user is not confined to the + * repository their own rule names - that is what the rule says. */ + expect_allow(&p, "carol", "web", LV_WRITE, "refs/heads/feature/any"); + expect_allow(&p, "erin", "web", LV_WRITE, "refs/heads/feature/any"); + expect_deny(&p, "nobody", "web", LV_WRITE, "refs/heads/any", "no rule"); + expect_deny(&p, "", "web", LV_WRITE, "refs/heads/any", "no identity"); + expect_deny(&p, NULL, "web", LV_WRITE, "refs/heads/any", "no identity"); + + /* main is not special: it is refused here because no rule of bob's or + * carol's names it, and allowed for dave because his names everything. */ + expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/main", + "but not on refs/heads/main"); + expect_deny(&p, "carol", "team", LV_FORCE, "refs/heads/main", + "but not on refs/heads/main"); + expect_allow(&p, "dave", "team", LV_WRITE, "refs/heads/main"); + /* and the person whose rule does name main, and nothing else, gets main */ + expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/main"); + /* frank reaches the '@all' rule like anyone else the policy names, so what + * he lacks is a branch nobody's rule names, and a rewrite of his own */ + expect_allow(&p, "frank", "team", LV_WRITE, "refs/heads/feature/x"); + expect_deny(&p, "frank", "team", LV_WRITE, "refs/heads/dev", + "but not on refs/heads/dev"); + expect_deny(&p, "frank", "team", LV_FORCE, "refs/heads/main", "needs force"); + + /* refs that are not branches or tags: only an admin, at any request level */ + expect_allow(&p, "dave", "team", LV_WRITE, "refs/pull/1/head"); + expect_allow(&p, "dave", "team", LV_ADMIN, "refs/pull/1/head"); + expect_deny(&p, "bob", "team", LV_ADMIN, "refs/notes/x", "needs admin"); + + /* the same branch, three spellings */ + expect_deny(&p, "bob", "team", LV_WRITE, "main", "but not on refs/heads/main"); + expect_deny(&p, "bob", "team", LV_WRITE, "heads/main", + "but not on refs/heads/main"); + + /* repository matching: .git is the same repository */ + expect_allow(&p, "bob", "team.git", LV_WRITE, "refs/heads/feature/x"); + expect_allow(&p, "erin", "secrets.git", LV_ADMIN, "refs/heads/main"); + + /* the question without a ref */ + expect_allow(&p, "dave", "team", LV_FORCE, NULL); + expect_allow(&p, "alice", "team", LV_ADMIN, NULL); + expect_allow(&p, "bob", "team", LV_WRITE, NULL); + expect_deny(&p, "bob", "team", LV_FORCE, NULL, "no force"); + expect_allow(&p, "erin", "team", LV_WRITE, NULL); /* via '@all *' */ + expect_deny(&p, "nobody", "team", LV_WRITE, NULL, "no write"); + + /* what a denial names: the level held, and the level needed */ + { + decision_t d; + + authz_check(&p, "bob", "team", LV_ADMIN, "refs/heads/feature/x", &d); + CHECK(!d.allowed); + CHECK_INT(d.need, LV_ADMIN); + CHECK(strstr(d.why, "bob has write") != NULL); + CHECK(strstr(d.why, "needs admin") != NULL); + + authz_check(&p, "nobody", "team", LV_WRITE, "refs/heads/dev", &d); + CHECK(!d.allowed); + CHECK(strstr(d.why, "nobody has no rule for team") != NULL); + } + policy_free(&p); +} + +static void test_scope_edges(void) +{ + policy_t p; + int rc; + + /* a rule with no refs is the whole repository: every branch, main included, + * which is exactly why naming the refs is the honest way to write one */ + rc = load_text(&p, "bob team write\n"); + CHECK_INT(rc, 0); + expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/anything"); + expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/main"); + /* write on everything still is not force */ + expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/main", "needs force"); + policy_free(&p); + + /* a bare name is a branch, and only a branch */ + rc = load_text(&p, "bob web write refs/heads/*\n"); + CHECK_INT(rc, 0); + expect_allow(&p, "bob", "web", LV_WRITE, "v1"); /* refs/heads/v1 */ + expect_deny(&p, "bob", "web", LV_WRITE, "refs/tags/v1", "needs write"); + expect_deny(&p, "bob", "web", LV_WRITE, "tags/v1", "needs write"); + policy_free(&p); + + /* a user may hold several rules for one repository; all of them count */ + rc = load_text(&p, + "bob team write refs/heads/dev\n" + "bob team force refs/heads/feature/*\n"); + CHECK_INT(rc, 0); + expect_allow(&p, "bob", "team", LV_WRITE, "refs/heads/dev"); + expect_allow(&p, "bob", "team", LV_FORCE, "refs/heads/feature/x"); + /* A refusal names the level held on the ref in question, not the strongest + * level held somewhere in the repository: "you have force here" next to a + * refusal is a sentence nobody can act on. */ + expect_deny(&p, "bob", "team", LV_FORCE, "refs/heads/dev", + "bob has write on team refs/heads/dev; it needs force"); + expect_deny(&p, "bob", "team", LV_WRITE, "refs/heads/other", + "but not on refs/heads/other"); + policy_free(&p); + + /* naming refs in a rule is the whole of the mechanism: what is not named is + * not granted, and the refusal says so rather than blaming a list */ + rc = load_text(&p, "bob web write refs/heads/feature/* refs/heads/dev\n"); + CHECK_INT(rc, 0); + expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/dev"); + expect_allow(&p, "bob", "web", LV_WRITE, "refs/heads/feature/x"); + expect_deny(&p, "bob", "web", LV_WRITE, "refs/heads/main", "but not on"); + expect_deny(&p, "bob", "api", LV_WRITE, "refs/heads/dev", "no rule"); + policy_free(&p); + + /* reachable-by-nobody detection, which validate reports */ + rc = load_text(&p, "bob team write\n"); + CHECK_INT(rc, 0); + CHECK(authz_repo_reachable(&p, "team")); + CHECK(authz_repo_reachable(&p, "team.git")); + CHECK(!authz_repo_reachable(&p, "other")); + policy_free(&p); + + rc = load_text(&p, "bob * write\n"); + CHECK_INT(rc, 0); + CHECK(authz_repo_reachable(&p, "anything")); + policy_free(&p); +} + +int main(void) +{ + test_init(""); + + test_check(); + test_scope_edges(); + + return test_report("check"); +} |
