xref: /xnu-11215/tests/subsystem_root_path.c (revision bb611c8f)
1 #include <spawn_private.h>
2 #include "subsystem_root_path.h"
3 
4 #include <darwintest.h>
5 #include <darwintest_utils.h>
6 
7 #define UNENTITLED_EXECUTABLE_PATH "./subsystem_root_path_helper"
8 #define ENTITLED_EXECUTABLE_PATH "./subsystem_root_path_helper_entitled"
9 
10 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
11 
12 T_DECL(subsystem_root_path,
13     "Test the support for setting subsystem_root_path",
14     T_META_CHECK_LEAKS(false))
15 {
16 	char * args[] = { ENTITLED_EXECUTABLE_PATH, HELPER_BEHAVIOR_NOT_SET, "/main_root/", NULL};
17 	int pid = 0;
18 	posix_spawnattr_t attr = NULL;
19 
20 	T_ASSERT_EQ_INT(_spawn_and_wait(args, NULL), 0, "posix_spawn without attributes");
21 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_init(&attr), "posix_spawnattr_init");
22 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "posix_spawn with attributes");
23 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, args[2]), "Set subsystem root path");
24 
25 	args[1] = HELPER_BEHAVIOR_SET;
26 
27 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "posix_spawn with subsystem root path");
28 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, NULL), "Clear subsystem root path attribute");
29 
30 	args[1] = HELPER_BEHAVIOR_NOT_SET;
31 
32 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Spawn without subsystem root path");
33 
34 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_set_subsystem_root_path_np(&attr, args[2]), "Set subsystem root path (again)");
35 
36 	args[1] = HELPER_BEHAVIOR_FORK_EXEC;
37 
38 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Subsystem root path inheritence across fork/exec");
39 
40 	args[1] = HELPER_BEHAVIOR_SPAWN;
41 
42 	T_ASSERT_EQ_INT(_spawn_and_wait(args, &attr), 0, "Subsystem root path override through posix_spawn");
43 
44 	args[0] = UNENTITLED_EXECUTABLE_PATH;
45 
46 	T_ASSERT_NE_INT(_spawn_and_wait(args, &attr), 0, "Entitlement check");
47 	T_ASSERT_POSIX_SUCCESS(posix_spawnattr_destroy(&attr), "posix_spawnattr_destroy");
48 }
49