// Unit tests for the config parser's API. The shell tests in run.sh cover the
// command line; these cover the parser directly, including the limits.
#include "conf.h"
#include "platform.h"
#include "stamp.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
static Conf conf;
static int checks;
static int failures;
#define CHECK(cond) \
do { \
checks++; \
if (!(cond)) { \
failures++; \
printf("FAIL unit %s:%d: %s\n", __FILE__, __LINE__, #cond); \
} \
} while (0)
static bool parse(const char *text, ConfError *e)
{
return conf_parse(&conf, text, strlen(text), e);
}
static void expect_error(const char *text, int line, const char *part)
{
ConfError e;
bool ok = parse(text, &e);
CHECK(!ok);
CHECK(e.line == line);
CHECK(strstr(e.msg, part) != nullptr);
if (ok || e.line != line || strstr(e.msg, part) == nullptr)
printf(" wanted line %d with '%s', got line %d '%s'\n", line, part, e.line,
ok ? "(parsed)" : e.msg);
}
static void test_valid(void)
{
ConfError e;
CHECK(parse("", &e));
CHECK(conf.nsections == 0);
CHECK(parse("# only a comment\n\n \n", &e));
CHECK(parse("[harness a]", &e)); // no trailing newline
CHECK(conf.nsections == 1);
CHECK(parse("[harness a]\r\nrecord = x\r\n", &e));
CHECK(strcmp(conf.entries[0].val, "x") == 0);
}
static void test_comments(void)
{
ConfError e;
CHECK(parse("[harness a]\nrecord = x # note\nevent E = a#b\n", &e));
CHECK(strcmp(conf.entries[0].val, "x") == 0);
CHECK(strcmp(conf.entries[1].val, "a#b") == 0);
CHECK(parse(" # indented\n[harness a] # header\n", &e));
}
static void test_lists(void)
{
ConfError e;
CHECK(parse("[outfit a]\nskills = x\t y z* \n", &e));
const char *cur = conf.entries[0].val;
const char *w;
size_t n;
CHECK(conf_list_next(&cur, &w, &n) && n == 1 && w[0] == 'x');
CHECK(conf_list_next(&cur, &w, &n) && n == 1 && w[0] == 'y');
CHECK(conf_list_next(&cur, &w, &n) && n == 2 && memcmp(w, "z*", 2) == 0);
CHECK(!conf_list_next(&cur, &w, &n));
}
static void test_errors(void)
{
expect_error("[bogus x]\n", 1, "unknown section kind");
expect_error("[harness a]\nnope = 1\n", 2, "unknown key");
expect_error("[harness a]\n[harness a]\n", 2, "duplicate section");
expect_error("[outfit a]\nonly = h\nonly = h\n", 3, "duplicate key");
expect_error("record = x\n", 1, "before any section");
expect_error("[harness a\n", 1, "malformed");
expect_error("[outfit a]\ninclude = b\n[outfit b]\ninclude = a\n", 2 + 2,
"include cycle: a -> b -> a");
expect_error("[outfit a]\ninclude = a\n", 2, "include cycle: a -> a");
expect_error("[outfit a]\ninclude = zz\n", 2, "unknown outfit");
expect_error("[wear user]\noutfits = zz\n", 2, "unknown outfit");
expect_error("[outfit a]\nonly = zz\n", 2, "unknown harness");
}
static void test_more_errors(void)
{
expect_error("[harness bad/name]\n", 1, "invalid harness name");
expect_error("[need h bad/name]\n", 1, "invalid need");
expect_error("[harness a b c d e]\n", 1, "too many words");
expect_error("[wear user machine=bad/name]\n", 1, "invalid machine name");
expect_error("[wear user machine=]\n", 1, "invalid machine name");
expect_error("[wear shared bad$target]\n", 1, "invalid target");
expect_error("[wear]\n", 1, "needs a SCOPE");
expect_error("[wear shared a b]\n", 1, "too many arguments");
expect_error("[harness a]\nx\n", 2, "expected KEY = VALUE");
expect_error("[harness a]\nevent E = a b\n", 2, "takes one word");
expect_error("[outfit o]\nslotx = a\n", 2, "unknown key 'slotx'");
expect_error("[outfit o]\nslot = a\n", 2, "unknown key 'slot'");
expect_error("[outfit o]\nslot a$b = f\n", 2, "invalid key");
expect_error("[outfit o]\nslot a b = f\n", 2, "invalid key");
expect_error("[outfit o]\nfile ~/a b = f\n", 2, "invalid key");
expect_error("[harness a]\nbogus.user = x\n", 2, "unknown key 'bogus.user'");
expect_error("[harness a]\nskills.galaxy = x\n", 2, "unknown key");
expect_error("[outfit o]\nskills = x yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy/z\n", 2,
"invalid name 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'");
expect_error("[outfit abc]\n[outfit o]\ninclude = ab\n", 3, "include of unknown outfit 'ab'");
expect_error("[outfit a]\ninclude = b\n[outfit b]\ninclude = c\n[outfit c]\ninclude = b\n", 6,
"include cycle: b -> c -> b");
expect_error("[wear user]\npublish = yes\n", 2, "publish is allowed only on wear shared");
}
static void test_valid_names(void)
{
ConfError e;
CHECK(parse("[harness a_b]\nevent Some_Event.v2-x/y = z\n[outfit Z.9]\nskills = A_b-c.d* x\n", &e));
CHECK(parse("[wear shared g.com/o/n_x-y:z@h~w]\noutfits = \n", &e) == false);
CHECK(parse("[wear shared g.com/o/n_x-y:z@h~w]\n", &e));
CHECK(parse("[outfit o]\nfile ~/a/b = x\nslot hook/Stop = y\n", &e));
}
static void test_long_cycle_message(void)
{
char names[3][121];
for (int i = 0; i < 3; i++) {
memset(names[i], 'a' + i, 120);
names[i][120] = '\0';
}
char text[2048];
int n = snprintf(text, sizeof text,
"[outfit %s]\ninclude = %s\n[outfit %s]\ninclude = %s\n[outfit %s]\ninclude = %s\n",
names[0], names[1], names[1], names[2], names[2], names[0]);
ConfError e;
CHECK(!conf_parse(&conf, text, (size_t)n, &e));
CHECK(e.line == 6);
CHECK(strlen(e.msg) == sizeof e.msg - 1);
CHECK(strncmp(e.msg, "include cycle: aaaa", 19) == 0);
}
static void test_header_names_and_spacing(void)
{
ConfError e;
char text[256];
char name[130];
memset(name, 'n', 129);
name[129] = '\0';
snprintf(text, sizeof text, "[harness %s]\n", name);
CHECK(!conf_parse(&conf, text, strlen(text), &e) && e.line == 1);
name[128] = '\0';
snprintf(text, sizeof text, "[harness %s]\n", name);
CHECK(parse(text, &e));
expect_error("[need bad/h n]\n", 1, "invalid need");
CHECK(parse("[ harness a ]\n", &e));
CHECK(conf.nsections == 1 && strcmp(conf.sections[0].arg[0], "a") == 0);
}
static void test_wear_headers(void)
{
ConfError e;
CHECK(parse("[wear enterprise]\n[wear user]\n[wear user machine=m]\n", &e));
CHECK(parse("[wear shared g/o/n]\n[wear personal g/o/n machine=m]\n", &e));
expect_error("[wear shared]\n", 1, "needs a TARGET");
expect_error("[wear personal]\n", 1, "needs a TARGET");
expect_error("[wear enterprise g/o/n]\n", 1, "takes no TARGET");
expect_error("[wear user g/o/n]\n", 1, "takes no TARGET");
expect_error("[wear enterprise machine=m]\n", 1, "machine=");
expect_error("[wear shared g/o/n machine=m]\n", 1, "machine=");
expect_error("[wear galaxy]\n", 1, "unknown scope");
expect_error("[wear user]\npublish = yes\n", 2, "publish");
}
static char *repeat_lines(const char *pre, const char *post, int n, size_t *len)
{
char *s = malloc((size_t)n * 64 + 1);
size_t off = 0;
for (int i = 1; i <= n; i++)
off += (size_t)snprintf(s + off, 64, "%s%d%s", pre, i, post);
*len = off;
return s;
}
static bool parse_n(const char *pre, const char *post, int n, ConfError *e)
{
size_t len;
char *s = repeat_lines(pre, post, n, &len);
bool ok = conf_parse(&conf, s, len, e);
free(s);
return ok;
}
static void test_limits(void)
{
ConfError e;
CHECK(parse_n("[harness h", "]\n", 64, &e));
CHECK(!parse_n("[harness h", "]\n", 65, &e) && e.line == 65);
CHECK(parse_n("[outfit o", "]\n", 512, &e));
CHECK(!parse_n("[outfit o", "]\n", 513, &e) && e.line == 513);
CHECK(parse_n("[wear personal g/o/n", "]\n", 256, &e));
CHECK(!parse_n("[wear personal g/o/n", "]\n", 257, &e) && e.line == 257);
CHECK(parse_n("[upstream u", "]\n", 128, &e));
CHECK(!parse_n("[upstream u", "]\n", 129, &e) && e.line == 129);
// 256 needs, then 257. Needs name a harness, so lead with one.
size_t len;
char *s = repeat_lines("[need h n", "]\n", 257, &len);
char *text = malloc(len + 32);
int head = snprintf(text, 32, "[harness h]\n");
memcpy(text + head, s, len);
CHECK(!conf_parse(&conf, text, (size_t)head + len, &e) && e.line == 258);
CHECK(strstr(e.msg, "too many need") != nullptr);
free(s);
free(text);
// 8192 entries fit, the 8193rd does not.
char *big = malloc(8200 * 16);
size_t off = (size_t)snprintf(big, 32, "[harness h]\n");
for (int i = 0; i < 8192; i++) off += (size_t)snprintf(big + off, 16, "record = r\n");
CHECK(conf_parse(&conf, big, off, &e));
off += (size_t)snprintf(big + off, 16, "record = r\n");
CHECK(!conf_parse(&conf, big, off, &e) && e.line == 8194);
free(big);
}
static void test_line_and_file(void)
{
ConfError e;
static char line[CONF_FILE_MAX + 64];
memset(line, '#', CONF_LINE_MAX);
line[CONF_LINE_MAX] = '\n';
CHECK(conf_parse(&conf, line, CONF_LINE_MAX + 1, &e));
memset(line, '#', CONF_LINE_MAX + 1);
line[CONF_LINE_MAX + 1] = '\n';
CHECK(!conf_parse(&conf, line, CONF_LINE_MAX + 2, &e) && e.line == 1);
memset(line, '\n', CONF_FILE_MAX);
CHECK(conf_parse(&conf, line, CONF_FILE_MAX, &e));
CHECK(!conf_parse(&conf, line, CONF_FILE_MAX + 1, &e) && e.line == 0);
CHECK(!conf_parse(&conf, "[harness a]\nrecord = x\0y\n", 24, &e) && e.line == 2);
}
static void test_dump_is_stable(void)
{
ConfError e;
CHECK(parse("[outfit b]\n[outfit a]\nskills = z y\n", &e));
char path[] = "/tmp/suit-unit.XXXXXX";
int fd = mkstemp(path);
CHECK(fd >= 0);
CHECK(conf_dump(&conf, fd));
char got[256] = {};
CHECK(pread(fd, got, sizeof got - 1, 0) > 0);
CHECK(strcmp(got, "outfit a\noutfit a skills = z y\noutfit b\n") == 0);
// Entries equal in key and value keep their file order.
CHECK(ftruncate(fd, 0) == 0 && lseek(fd, 0, SEEK_SET) == 0);
CHECK(parse("[harness h]\nrecord = x\nrecord = x\nrecord = a\n", &e));
CHECK(conf_dump(&conf, fd));
memset(got, 0, sizeof got);
CHECK(pread(fd, got, sizeof got - 1, 0) > 0);
CHECK(strcmp(got, "harness h\nharness h record = a\nharness h record = x\nharness h record = x\n") == 0);
close(fd);
unlink(path);
}
// ---- platform -------------------------------------------------------------
// Run FN in a child with SUIT_FAULT=SPEC; returns the child's exit status.
static int in_child(const char *spec, int (*fn)(void))
{
fflush(nullptr);
pid_t pid = fork();
if (pid == 0) {
setenv("SUIT_FAULT", spec, 1);
fault_reset();
int rc = fn();
fflush(nullptr);
exit(rc);
}
int status = 0;
waitpid(pid, &status, 0);
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
}
// Each of these makes one wrapper call and returns 0 if it failed with EIO.
static int fails_eio(bool failed) { return failed && errno == EIO ? 0 : 1; }
static int w_open(void) { return fails_eio(sys_open("/dev/null", O_RDONLY, 0) < 0); }
static int w_read(void) { char b[1]; return fails_eio(sys_read(0, b, 1) < 0); }
static int w_write(void) { return fails_eio(sys_write(1, "", 1) < 0); }
static int w_close(void) { return fails_eio(sys_close(-1) < 0 && errno == EIO); }
static int w_fsync(void) { return fails_eio(sys_fsync(1) < 0); }
static int w_stat(void) { struct stat st; return fails_eio(sys_stat("/", &st) < 0); }
static int w_fstatat(void) { struct stat st; return fails_eio(sys_fstatat(AT_FDCWD, "/", &st) < 0); }
static int w_rename(void) { return fails_eio(sys_rename("/nonexistent-a", "/nonexistent-b") < 0); }
static int w_mkdir(void) { return fails_eio(sys_mkdir("/", 0755) < 0); }
static int w_unlink(void) { return fails_eio(sys_unlink("/nonexistent-a") < 0); }
static int w_getenv(void) { return sys_getenv("PATH") == nullptr ? 0 : 1; }
static int w_spawn(void) { pid_t p; char *argv[] = {(char *)"true", nullptr}; return sys_spawn(&p, "/usr/bin/true", argv) == EIO ? 0 : 1; }
static int w_wait4(void) { int st; return fails_eio(sys_wait4(-1, &st, nullptr) < 0); }
static int w_clock(void) { uint64_t ns; return sys_now_ns(&ns) ? 1 : 0; }
static int w_self(void) { char b[4096]; return platform_self_path(b, sizeof b) ? 1 : 0; }
static void test_wrapper_faults(void)
{
static const struct {
const char *spec;
int (*fn)(void);
} cases[] = {
{"open:1", w_open}, {"read:1", w_read}, {"write:1", w_write},
{"close:1:EIO", w_close}, {"fsync:1", w_fsync}, {"stat:1", w_stat},
{"fstatat:1", w_fstatat}, {"rename:1", w_rename}, {"mkdir:1", w_mkdir},
{"unlink:1", w_unlink}, {"getenv:1", w_getenv}, {"spawn:1", w_spawn},
{"wait4:1", w_wait4}, {"clock_gettime:1", w_clock}, {"self_path:1", w_self},
};
for (size_t i = 0; i < sizeof cases / sizeof cases[0]; i++) {
CHECK(in_child(cases[i].spec, cases[i].fn) == 0);
if (in_child(cases[i].spec, cases[i].fn) != 0) printf(" case %s\n", cases[i].spec);
}
}
static int second_call(void) // the fault fires on call 2 only
{
struct stat st;
return sys_stat("/", &st) == 0 && sys_stat("/", &st) < 0 && sys_stat("/", &st) == 0 ? 0 : 1;
}
static void test_fault_counts_calls(void)
{
CHECK(in_child("stat:2", second_call) == 0);
}
static void test_self_path(void)
{
char big[4096];
char tiny[2];
CHECK(platform_self_path(big, sizeof big));
CHECK(big[0] != '\0');
CHECK(!platform_self_path(tiny, sizeof tiny));
}
static void test_say_and_out(void)
{
char path[] = "/tmp/suit-unit-out.XXXXXX";
int fd = mkstemp(path);
CHECK(fd >= 0);
static char text[SAY_MAX * 2];
memset(text, 'x', sizeof text - 1);
say(fd, "%s", text);
struct stat st;
CHECK(fstat(fd, &st) == 0 && (size_t)st.st_size == SAY_MAX - 1);
char last = 0;
CHECK(pread(fd, &last, 1, (off_t)(SAY_MAX - 2)) == 1 && last == '\n');
// A string bigger than the buffer, then a write error.
CHECK(ftruncate(fd, 0) == 0 && lseek(fd, 0, SEEK_SET) == 0);
Out o;
out_init(&o, fd);
out_str(&o, text);
out_str(&o, "tail");
CHECK(out_finish(&o));
CHECK(fstat(fd, &st) == 0 && (size_t)st.st_size == sizeof text - 1 + 4);
close(fd);
unlink(path);
out_init(&o, -1);
out_str(&o, text);
CHECK(!out_finish(&o) && errno == EBADF);
}
#ifndef SUIT_COVER
static int ensure_false(void)
{
ensure(1 == 2);
return 0;
}
static void test_ensure_aborts(void)
{
fflush(stdout);
pid_t pid = fork();
if (pid == 0) {
freopen("/dev/null", "w", stderr);
int null = open("/dev/null", O_WRONLY);
dup2(null, STDERR_FILENO);
exit(ensure_false());
}
int status = 0;
waitpid(pid, &status, 0);
CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
}
#endif
static void test_stamp_too_large(void)
{
char dir[] = "/tmp/suit-unit-home.XXXXXX";
CHECK(mkdtemp(dir) != nullptr);
setenv("SUIT_HOME", dir, 1);
enum { N = 70000 };
static char *paths[N];
for (int i = 0; i < N; i++) paths[i] = (char *)"/suit-no-such-file-for-the-limit-test";
CHECK(stamp_write(N, paths) == 1);
char file[256];
snprintf(file, sizeof file, "%s/stamp", dir);
CHECK(access(file, F_OK) != 0);
unsetenv("SUIT_HOME");
rmdir(dir);
}
int main(void)
{
test_valid();
test_comments();
test_lists();
test_errors();
test_wear_headers();
test_more_errors();
test_header_names_and_spacing();
test_valid_names();
test_long_cycle_message();
test_limits();
test_line_and_file();
test_dump_is_stable();
test_wrapper_faults();
test_fault_counts_calls();
test_self_path();
test_say_and_out();
#ifndef SUIT_COVER
test_ensure_aborts();
#endif
test_stamp_too_large();
printf("unit: %d checks, %d failed\n", checks, failures);
return failures == 0 ? 0 : 1;
}