Artifact f2d5337e08dca831151e85ff20afcd3d0d98e6b456918c991e63de0b22fbbc2d:
- File
test/unit.c
— part of check-in
[2f274ce15b]
at
2026-11-12 22:28:09
on branch trunk
— Phase 2: the store, a working copy that is never half written
The store is a working copy of a wardrobe, kept through libfossil's C API with suit's own file writer: every file is written beside itself and renamed, so a reader sees the old file or the new one.
suit store open, status and update, suit commit and suit back. An update refuses when there are local changes, runs as a dry run first, and changes nothing if any file would conflict.
Proven here: the store is byte-identical to fossil's own checkout of a 600-file wardrobe at each version; a reader hashing files during a slowed update saw only old or new contents; an update that fails at any call leaves the old bytes and no temporary file. 100% of functions, lines, regions and branches in all ten source files.
Vendored, unedited: libfossil trunk f2983dcd78, SQLite 3.55.0, zlib 1.3.1. The release binary links only the system library.
Speed: with everything linked, suit costs 0.82 ms over an empty program quiet and up to 1.38 ms under load, which is over budget. The timer's entry point is now suit-run, a 35 KB program built from the no-change path alone, at 0.40 ms quiet and about 0.5 ms loaded. It runs suit only when something changed. The budget test is on suit-run.
Known limits, recorded in docs/decisions.md: libfossil's checkout writes symlinks as regular files, so wardrobes may not hold them; suit back undoes an update and not a check-in; and a store left half updated by a double failure has no repair command yet.
Not yet run: Fil-C and the Linux build of this phase. (user: dmestas, size: 20686) [annotate] [blame] [check-ins using]
// 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 "store_fs.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) { int null = open("/dev/null", O_WRONLY); if (null < 0 || dup2(null, STDERR_FILENO) < 0) exit(99); exit(ensure_false()); } int status = 0; waitpid(pid, &status, 0); // ensure() must end the process abnormally. Natively that is SIGABRT; // a memory-safe runtime may end it with a different signal. if (!WIFSIGNALED(status)) { fprintf(stderr, "ensure child: exited=%d code=%d\n", WIFEXITED(status), WIFEXITED(status) ? WEXITSTATUS(status) : -1); } CHECK(WIFSIGNALED(status)); #ifndef __PIZLONATOR_WAS_HERE__ CHECK(WTERMSIG(status) == SIGABRT); #endif } #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); } // ---- the store's file writer ------------------------------------------------ static fsl_cx *wcx; static fsl_ckout_fs wfs; static char wdir[64]; static char wfile[128]; static void writer_setup(void) { snprintf(wdir, sizeof wdir, "/tmp/suit-unit-w.XXXXXX"); CHECK(mkdtemp(wdir) != nullptr); snprintf(wfile, sizeof wfile, "%s/file", wdir); CHECK(fsl_cx_init(&wcx, nullptr) == 0); CHECK(store_fs_install(wcx) == 0); fsl_cx_ckout_fs_get(wcx, &wfs); } static bool has_temp_file(void) { char cmd[256]; snprintf(cmd, sizeof cmd, "ls %s/.suit-tmp.* >/dev/null 2>&1", wdir); return system(cmd) == 0; } static int write_new(void) { return wfs.xWrite(wfs.state, wfile, "abc", 3) == 0 ? 0 : 1; } // A write that is made to fail fails, and leaves no temporary file and the old bytes. static int write_fails(void) { int rc = wfs.xWrite(wfs.state, wfile, "new", 3); char got[8] = {}; FILE *f = fopen(wfile, "r"); size_t n = f != nullptr ? fread(got, 1, sizeof got - 1, f) : 0; if (f != nullptr) fclose(f); return rc != 0 && !has_temp_file() && n == 3 && strcmp(got, "old") == 0 ? 0 : 1; } static void put(const char *path, const char *text, mode_t mode) { FILE *f = fopen(path, "w"); CHECK(f != nullptr); fputs(text, f); fclose(f); chmod(path, mode); } typedef struct { int count; int ours; } Seen; static int collect(void *state, char const *entry) { Seen *seen = state; seen->count++; if (strncmp(entry, STORE_TMP_PREFIX, strlen(STORE_TMP_PREFIX)) == 0) seen->ours++; return 0; } static void test_writer(void) { writer_setup(); CHECK(write_new() == 0); char got[8] = {}; FILE *f = fopen(wfile, "r"); CHECK(f != nullptr && fread(got, 1, 7, f) == 3 && strcmp(got, "abc") == 0); fclose(f); struct stat st; CHECK(stat(wfile, &st) == 0 && (st.st_mode & 0111) == 0); // An executable file stays executable when it is replaced. put(wfile, "old", 0755); CHECK(wfs.xWrite(wfs.state, wfile, "new", 3) == 0); CHECK(stat(wfile, &st) == 0 && (st.st_mode & 0100) != 0 && st.st_size == 3); CHECK(!has_temp_file()); // A call that fails leaves the old bytes and no temporary file. static const char *const specs[] = {"open:1", "write:1", "fsync:1", "close:1", "rename:1"}; for (size_t i = 0; i < sizeof specs / sizeof specs[0]; i++) { put(wfile, "old", 0644); CHECK(in_child(specs[i], write_fails) == 0); } put(wfile, "old", 0644); CHECK(in_child("stat:1", write_new) == 0); // no stat: treated as not executable CHECK(in_child("write:1:short", write_new) == 0); // The listing never shows our temporary names. put(wfile, "x", 0644); char tmp[160]; snprintf(tmp, sizeof tmp, "%s/" STORE_TMP_PREFIX "1.1", wdir); put(tmp, "x", 0644); Seen seen = {0, 0}; CHECK(wfs.xDirList(wfs.state, wdir, collect, &seen) == 0); CHECK(seen.count == 1 && seen.ours == 0); unlink(tmp); unlink(wfile); rmdir(wdir); fsl_cx_finalize(wcx); } // ---- SUIT_FAULT lists and delays ---------------------------------------------- static int stat_twice(void) { struct stat st; return sys_stat("/", &st) == 0 && sys_stat("/", &st) == 0 ? 0 : 1; } static int parse_dies(void) { struct stat st; (void)sys_stat("/", &st); return 0; } static void test_fault_lists(void) { CHECK(in_child("stat:1:delay", stat_twice) == 0); // every call from the first CHECK(in_child("stat:2:delay", stat_twice) == 0); // only from the second CHECK(in_child("read:1,write:1,stat:9", stat_twice) == 0); // other names are left alone CHECK(in_child("open:1,open:2,open:3,open:4,open:5", parse_dies) == 99); char big[300]; memset(big, 'x', sizeof big - 1); big[sizeof big - 1] = '\0'; CHECK(in_child(big, parse_dies) == 99); } 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(); test_writer(); test_fault_lists(); printf("unit: %d checks, %d failed\n", checks, failures); return failures == 0 ? 0 : 1; }