/* test.c — unit tests for run-guix-system-container.c * * Includes the daemon source with its main() renamed, then exercises * the container-launcher fingerprinting — the security-relevant part * that decides what the daemon will exec. * * Build & run: ./run.sh */ /* Pull in the daemon under test (main renamed away). */ #define main daemon_main #include "../bin/run-guix-system-container.c" #undef main static int failures; static void check_str(const char *name, int got, int want) { printf(" %-58s %s\n", name, got == want ? "ok" : "FAIL"); if (got != want) failures++; } /* ------------------------------------------------------------------ */ /* Container launcher fingerprint */ /* ------------------------------------------------------------------ */ static void test_fingerprint(const char *real_container, const char *tmpdir) { char fake_shell[MAX_LINE], fake_guile[MAX_LINE]; FILE *f; /* Fake 1: correct name, plain shell script mentioning the magic * strings in a comment. */ snprintf(fake_shell, sizeof(fake_shell), "%s/00000000000000000000000000000000-run-container", tmpdir); f = fopen(fake_shell, "w"); fprintf(f, "#!/bin/sh\n" "echo fake; # call-with-container GUIX_NEW_SYSTEM linux-container\n"); fclose(f); /* Fake 2: correct name, real guile shebang, but no * call-with-container call. */ snprintf(fake_guile, sizeof(fake_guile), "%s/11111111111111111111111111111111-run-container", tmpdir); f = fopen(fake_guile, "w"); fprintf(f, "#!/gnu/store/0000000000000000000000000000000-guile-3.0.9/bin/guile --no-auto-compile\n" "(display \"not a container\")\n"); fclose(f); printf("container fingerprint:\n"); if (real_container) { const char *slash = strrchr(real_container, '/'); char base[MAX_LINE]; snprintf(base, sizeof(base), "%s", slash ? slash + 1 : real_container); check_str("real generated container script", is_container_script(real_container, base), 1); } else { printf(" %-58s %s\n", "real generated container script", "SKIP (none in store)"); } check_str("plain shell script with magic strings in a comment", is_container_script(fake_shell, "00000000000000000000000000000000-run-container"), 0); check_str("guile script without call-with-container", is_container_script(fake_guile, "11111111111111111111111111111111-run-container"), 0); } /* ------------------------------------------------------------------ */ /* Daemon fingerprint */ /* ------------------------------------------------------------------ */ static void test_daemon_fingerprint(void) { char first[33]; printf("daemon fingerprint:\n"); check_str("valid: v0.1", valid_fingerprint("v0.1"), 1); check_str("valid: 8 hex chars", valid_fingerprint("abcd1234"), 1); check_str("empty string rejected", valid_fingerprint(""), 0); check_str("too long rejected", valid_fingerprint("0123456789012345678901234567890123"), 0); check_str("whitespace rejected", valid_fingerprint("v0 1"), 0); strcpy(daemon_fingerprint, ""); random_fingerprint(); check_str("random fingerprint is 8 lowercase hex chars", strlen(daemon_fingerprint) == 8 && strspn(daemon_fingerprint, "0123456789abcdef") == 8, 1); strcpy(first, daemon_fingerprint); random_fingerprint(); check_str("two random fingerprints differ", strcmp(first, daemon_fingerprint) != 0, 1); } /* ------------------------------------------------------------------ */ /* Privileges */ /* ------------------------------------------------------------------ */ static void test_privileges(void) { unsigned long long caps = 0; printf("privileges:\n"); check_str("CapEff is readable", read_cap_eff(&caps), 1); if (geteuid() == 0) check_str("root has container privileges", have_container_privileges(), 1); else check_str("non-root is refused", have_container_privileges(), 0); } /* ------------------------------------------------------------------ */ int main(int argc, char **argv) { /* argv[1]: a real *-run-container path in the store (or NULL) * argv[2]: temp dir for the fake scripts */ test_daemon_fingerprint(); test_privileges(); test_fingerprint(argc > 1 ? argv[1] : NULL, argc > 2 ? argv[2] : "/tmp"); printf("\n%s (%d failure%s)\n", failures ? "FAILED" : "ALL TESTS PASSED", failures, failures == 1 ? "" : "s"); return failures != 0; }