// No test framework: a suite with a dependency is a suite that does not run at // 3am. Each program in test/ links the library and reports its own count. #ifndef TEST_H #define TEST_H #include "authz.h" #include #include #include #include #include #include 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 */