summaryrefslogtreecommitdiffstats
path: root/test/parse.c
blob: 49fdb55b8b24c98be6fa6d20b0edade5000a9faf (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// What parses, what is refused, and who "@all" turns out to be.  One bad line
// poisons the whole file.
#include "test.h"

#include <errno.h>
#include <sys/stat.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);
}

// A field count of exactly MAX_FIELDS is not "too many": the array has room for
// it.  Only the 65th field on a line is the one that does not fit.
static void test_split_field_limit(void)
{
    char line[MAX_FIELDS * 3];
    char *f[MAX_FIELDS];
    int i;
    int n;

    line[0] = '\0';
    for (i = 0; i < MAX_FIELDS; i++)
        strcat(line, i ? " x" : "x");
    n = authz_split_ws(line, f, MAX_FIELDS);
    CHECK_INT(n, MAX_FIELDS);

    // splitting writes over the text, so the line is built again
    line[0] = '\0';
    for (i = 0; i <= MAX_FIELDS; i++)
        strcat(line, i ? " x" : "x");
    n = authz_split_ws(line, f, MAX_FIELDS);
    CHECK(n > MAX_FIELDS);              // one field too many, said so
}

// A rule line with exactly MAX_FIELDS fields is a rule like any other: 61 ref
// patterns on a user and repository.  It used to be refused as "too many".
static void test_rule_at_the_field_limit(void)
{
    policy_t p;
    char text[MAX_FIELDS * 20];
    int i;
    int rc;

    strcpy(text, "bob team write");
    for (i = 0; i < MAX_FIELDS - 3; i++)
        strcat(text, " refs/heads/r");
    strcat(text, "\n");

    rc = load_text(&p, text);
    CHECK_INT(rc, 0);
    CHECK_INT(p.nrules, 1);
    if (p.nrules == 1)               // else the CHECK above already said so
        CHECK_INT(p.rules[0].nrefs, MAX_FIELDS - 3);
    policy_free(&p);
}

// A directory is not a policy that happens to be empty.  Nothing can be read out
// of it, which is the same broken host as a missing file, and has to answer 3
// rather than 1 so a monitor can tell the two apart.
static void test_policy_that_is_a_directory(void)
{
    policy_t p;
    int rc;

    if (mkdir(tmproot, 0755) != 0 && errno != EEXIST) {
        note("cannot make %s; skipped", tmproot);
        return;
    }

    policy_init(&p, tmproot);
    rc = policy_load(&p);
    CHECK_INT(rc, -2);
    CHECK(p.unreadable);
    policy_free(&p);
}

int main(void)
{
    test_init("");

    test_parse();
    test_known_users();
    test_split_field_limit();
    test_rule_at_the_field_limit();
    test_policy_that_is_a_directory();

    return test_report("parse");
}