diff options
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"); +} |
