summaryrefslogtreecommitdiffstats
path: root/test/test.h
diff options
context:
space:
mode:
Diffstat (limited to 'test/test.h')
-rw-r--r--test/test.h141
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 */