summaryrefslogtreecommitdiffstats
path: root/test/test.h
blob: c4f63d0af50108e2dfd5cff1b88d76116159b52a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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 */