summaryrefslogtreecommitdiffstats
path: root/test/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'test/parse.c')
-rw-r--r--test/parse.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/test/parse.c b/test/parse.c
index 5c8cb5f..49fdb55 100644
--- a/test/parse.c
+++ b/test/parse.c
@@ -2,6 +2,9 @@
// poisons the whole file.
#include "test.h"
+#include <errno.h>
+#include <sys/stat.h>
+
static void test_parse(void)
{
policy_t p;
@@ -117,12 +120,80 @@ static void test_known_users(void)
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");
}