about summary refs log tree commit diff
path: root/test
diff options
context:
space:
mode:
authorPjotr Prins <pjotr.public12@thebird.nl>2026-09-26 13:09:25 +0000
committerPjotr Prins <pjotr.public12@thebird.nl>2026-09-26 13:09:25 +0000
commit2ff6ab0645463504fd101e73d3e4e2ab75a08c94 (patch)
tree5294b1b455b52fb194e1c9b8c25d80c0b85bfc23 /test
downloadrun-guix-container-2ff6ab0645463504fd101e73d3e4e2ab75a08c94.tar.gz
run-guix-container: new tool
A C daemon (run as root) that starts Guix system containers on
request over a line-based network protocol, plus an unprivileged
Ruby client.

* START/KILL/LOG/FINGERPRINT protocol; one request per connection
* strict validation of store items before exec (HASH-run-container
  name, guile shebang, container markers, immutable store file)
* --fingerprint WORD identifies the daemon instance (random hash
  when unset); verified by clients and passed to containers in
  GUIX_CONTAINER_FINGERPRINT
* on SIGINT/SIGTERM: SIGTERM all started containers, 10s timeout,
  then SIGKILL
* container boot output appended to /var/log/guix-containers/
* unit tests for launcher and fingerprint logic
Diffstat (limited to 'test')
-rwxr-xr-xtest/run.sh20
-rw-r--r--test/test.c115
2 files changed, 135 insertions, 0 deletions
diff --git a/test/run.sh b/test/run.sh
new file mode 100755
index 0000000..1316bae
--- /dev/null
+++ b/test/run.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+# run.sh — build and run the run-guix-container unit tests.
+#
+# Usage: ./run.sh
+
+set -e
+
+here=$(cd "$(dirname "$0")" && pwd)
+tmp=$(mktemp -d)
+trap 'rm -rf "$tmp"' EXIT
+
+# Find a real generated container script in the store, if any.
+container=$(ls /gnu/store | grep -E '^[0-9a-z]{32}-run-container$' | head -1)
+if [ -n "$container" ]; then
+    container="/gnu/store/$container"
+fi
+
+cc -O2 -Wall -o "$tmp/test" "$here/test.c"
+
+"$tmp/test" "$container" "$tmp"
diff --git a/test/test.c b/test/test.c
new file mode 100644
index 0000000..18f003f
--- /dev/null
+++ b/test/test.c
@@ -0,0 +1,115 @@
+/* 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);
+}
+
+/* ------------------------------------------------------------------ */
+
+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_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;
+}