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/test.h | |
| 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/test.h')
| -rw-r--r-- | test/test.h | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..c4f63d0 --- /dev/null +++ b/test/test.h @@ -0,0 +1,141 @@ +/* test.h - the little harness the test programs share. + * + * No test framework, because a test suite with a dependency is a test suite + * that does not run at 3am. Each program in test/ is its own: it links the + * library, runs its own cases and reports its own count, so one failing file + * says which area broke rather than taking the whole run down with it. + */ +#ifndef TEST_H +#define TEST_H + +#include "authz.h" + +#include <fcntl.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static int checks; +static int failures; +static char tmproot[128]; + +#define CHECK(cond) \ + do { \ + checks++; \ + if (!(cond)) { \ + failures++; \ + printf("not ok %d: %s [%s:%d]\n", failures, #cond, __FILE__, __LINE__);\ + } \ + } while (0) + +#define CHECK_STR(got, want) \ + do { \ + checks++; \ + if (strcmp((got), (want)) != 0) { \ + failures++; \ + printf("not ok %d: got \"%s\", want \"%s\" [%s:%d]\n", failures, \ + (got), (want), __FILE__, __LINE__); \ + } \ + } while (0) + +#define CHECK_INT(got, want) \ + do { \ + checks++; \ + if ((got) != (want)) { \ + failures++; \ + printf("not ok %d: got %ld, want %ld [%s:%d]\n", failures, \ + (long)(got), (long)(want), __FILE__, __LINE__); \ + } \ + } while (0) + +static inline void note(const char *fmt, ...) +{ + va_list ap; + + fputs(" # ", stdout); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + fputc('\n', stdout); +} + +/* Where the fixtures are written. UNIT_TMPDIR is there for a build that runs + * the tests somewhere the source tree is not writable. */ +static inline void test_init(const char *argv0) +{ + const char *dir = getenv("UNIT_TMPDIR"); + + (void)argv0; + snprintf(tmproot, sizeof(tmproot), "%s", dir != NULL ? dir : "test/tmp"); +} + +static inline int test_report(const char *what) +{ + printf("%s: %d checks, %d failure(s)\n", what, checks, failures); + if (failures != 0) + note("these are the semantics; the behavioural suite assumes them"); + return failures != 0; +} + +/* ------------------------------------------------------------- fixtures */ + +/* A policy from a string. The file is unlinked straight after loading: the + * decisions are made in memory, and a test that left policies behind would + * leave them to be trusted. */ +static inline int load_text(policy_t *p, const char *text) +{ + static int seq; + char path[512]; + FILE *f; + int rc; + + snprintf(path, sizeof(path), "%s/unit-%d", tmproot, ++seq); + f = fopen(path, "w"); + if (f == NULL) { + printf("not ok %d: cannot write %s\n", ++failures, path); + return -1; + } + fputs(text, f); + fclose(f); + + policy_init(p, path); + rc = policy_load(p); + unlink(path); + return rc; +} + +static inline void expect_allow(const policy_t *p, const char *user, const char *repo, + level_t need, const char *ref) +{ + decision_t d; + + authz_check(p, user, repo, need, ref, &d); + checks++; + if (!d.allowed) { + failures++; + printf("not ok %d: expected %s %s %s %s to be allowed, got: %s\n", failures, + user, repo, authz_level_name(need), ref != NULL ? ref : "(no ref)", d.why); + } +} + +static inline void expect_deny(const policy_t *p, const char *user, const char *repo, + level_t need, const char *ref, const char *needle) +{ + decision_t d; + + authz_check(p, user, repo, need, ref, &d); + checks++; + if (d.allowed) { + failures++; + printf("not ok %d: expected %s %s %s %s to be denied\n", failures, user, repo, + authz_level_name(need), ref != NULL ? ref : "(no ref)"); + } else if (needle != NULL && strstr(d.why, needle) == NULL) { + failures++; + printf("not ok %d: denial said \"%s\", expected it to mention \"%s\"\n", + failures, d.why, needle); + } +} + +#endif /* TEST_H */ |
