about summary refs log tree commit diff
path: root/test/test.c
blob: be05b12722e13baf2d81f5a1c88745f6089cb045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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;
}