summaryrefslogtreecommitdiffstats
path: root/test/parse.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 00:06:35 +0000
committerdev <dev@example>2026-11-14 00:06:35 +0000
commitbc7f4da406fbd802b08985ba583e52f040bdba85 (patch)
treefa5020a3781dd80dcba654a3417d22d02df4c2da /test/parse.c
parent50f296cf7609ce77d41b9fb3df51d6aeec8330d6 (diff)
downloadaccess-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/parse.c')
-rw-r--r--test/parse.c133
1 files changed, 133 insertions, 0 deletions
diff --git a/test/parse.c b/test/parse.c
new file mode 100644
index 0000000..fb137c5
--- /dev/null
+++ b/test/parse.c
@@ -0,0 +1,133 @@
+/* parse.c - Reading the file: what parses, what is refused, and who "@all" turns out to
+ * be. One bad line poisons the whole file, so the error cases matter as much
+ * as the good ones.
+ *
+ * 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_parse(void)
+{
+ policy_t p;
+ int rc;
+
+ rc = load_text(&p,
+ "# a comment\n"
+ "\n"
+ " \n"
+ "alice web admin # trailing comment\n"
+ "bob web write refs/heads/feature/* refs/heads/dev\n"
+ "@all team write main\n");
+ CHECK_INT(rc, 0);
+ CHECK_INT(p.errors, 0);
+ CHECK_INT(p.nrules, 3);
+ CHECK_STR(p.rules[0].user, "alice");
+ CHECK_STR(p.rules[0].repo, "web");
+ CHECK_INT(p.rules[0].level, LV_ADMIN);
+ CHECK_INT(p.rules[0].nrefs, 0);
+ CHECK_INT(p.rules[0].line, 4);
+ CHECK_STR(p.rules[1].refs[0], "refs/heads/feature/*");
+ CHECK_INT(p.rules[1].nrefs, 2);
+ CHECK_STR(p.rules[2].refs[0], "refs/heads/main"); /* normalised at parse time */
+ policy_free(&p);
+
+ /* Errors: each of these must stop the file being used at all. The complaints
+ * are asserted as counts, and the parser writes them to stderr as it goes, so
+ * stderr is set aside here - a wall of expected errors would hide the failures.
+ * The descriptor is redirected rather than freopen'd, because freopen would
+ * quietly make stderr block-buffered and hand the whole wall back at exit. */
+ {
+ int keep = dup(2);
+ int devnull = open("/dev/null", O_WRONLY);
+
+ if (devnull >= 0) {
+ dup2(devnull, 2);
+ close(devnull);
+ }
+
+ rc = load_text(&p, "alice web everything\n");
+ CHECK_INT(rc, -1);
+ CHECK_INT(p.errors, 1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web/extra write\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web write !refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "set other-thing refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ /* Removed settings are refused by name, not ignored: a policy that
+ * still carries one would otherwise mean "everyone with write can fast
+ * -forward main" while looking like it said the opposite. */
+ rc = load_text(&p, "set protected-refs refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "set protected-refs.web refs/heads/main\n");
+ CHECK_INT(rc, -1);
+ policy_free(&p);
+
+ rc = load_text(&p, "alice web write refs/heads/main\nbob web nope refs/heads/dev\n");
+ CHECK_INT(rc, -1); /* one bad line poisons the file: nothing is used */
+ policy_free(&p);
+
+ fflush(stderr);
+ dup2(keep, 2);
+ close(keep);
+ }
+
+ /* There is no settings list any more: a rule says what it says. */
+ rc = load_text(&p, "alice * admin\n");
+ CHECK_INT(rc, 0);
+ CHECK_INT(p.nrules, 1);
+ policy_free(&p);
+}
+
+static void test_known_users(void)
+{
+ policy_t p;
+ int rc;
+
+ rc = load_text(&p,
+ "alice web admin\n"
+ "bob web write\n"
+ "@all web write refs/heads/*\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_user_known(&p, "alice"));
+ CHECK(authz_user_known(&p, "bob"));
+ CHECK(!authz_user_known(&p, "carol")); /* '@all' never invents a name */
+ CHECK(!authz_user_known(&p, ""));
+ policy_free(&p);
+
+ rc = load_text(&p, "@all web write\n");
+ CHECK_INT(rc, 0);
+ CHECK(!authz_user_known(&p, "alice")); /* nobody is known yet */
+ policy_free(&p);
+
+ rc = load_text(&p, "ali* web write\n");
+ CHECK_INT(rc, 0);
+ CHECK(authz_user_known(&p, "alice")); /* a pattern is still a name */
+ CHECK(!authz_user_known(&p, "bob"));
+ policy_free(&p);
+}
+
+int main(void)
+{
+ test_init("");
+
+ test_parse();
+ test_known_users();
+
+ return test_report("parse");
+}