xref: /xnu-11215/tests/task_ident_test.c (revision 8d741a5d)
1 #include <darwintest.h>
2 #include <darwintest_utils.h>
3 #include <errno.h>
4 #include <mach/mach.h>
5 #include <mach/mach_types.h>
6 #include <mach/task.h>
7 #include <mach/mach_error.h>
8 #include <mach/task_special_ports.h>
9 
10 #include <signal.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 
15 T_GLOBAL_META(
16 	T_META_NAMESPACE("xnu.ipc"),
17 	T_META_RUN_CONCURRENTLY(TRUE),
18 	T_META_RADAR_COMPONENT_NAME("xnu"),
19 	T_META_RADAR_COMPONENT_VERSION("IPC"),
20 	T_META_TAG_VM_PREFERRED);
21 
22 T_DECL(task_ident, "test task identity token")
23 {
24 	kern_return_t kr;
25 	task_id_token_t token;
26 	mach_port_t port1, port2;
27 
28 	kr = task_create_identity_token(mach_task_self(), &token);
29 	T_ASSERT_MACH_SUCCESS(kr, "task_create_identity_token()");
30 
31 	port1 = mach_task_self();
32 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_CONTROL, &port2); /* Immovable control port for self */
33 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - CONTROL");
34 	T_EXPECT_EQ(port1, port2, "Control port does not match!");
35 
36 	mach_port_deallocate(mach_task_self(), port2);
37 
38 	kr = task_get_special_port(mach_task_self(), TASK_READ_PORT, &port1);
39 	T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port() - READ");
40 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_READ, &port2);
41 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - read");
42 	T_EXPECT_EQ(port1, port2, "Read port does not match!");
43 
44 	mach_port_deallocate(mach_task_self(), port1);
45 	mach_port_deallocate(mach_task_self(), port2);
46 
47 	kr = task_get_special_port(mach_task_self(), TASK_INSPECT_PORT, &port1);
48 	T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port() - INSPECT");
49 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_INSPECT, &port2);
50 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - inspect");
51 	T_EXPECT_EQ(port1, port2, "Inspect port does not match!");
52 
53 	mach_port_deallocate(mach_task_self(), port1);
54 	mach_port_deallocate(mach_task_self(), port2);
55 
56 	kr = task_get_special_port(mach_task_self(), TASK_NAME_PORT, &port1);
57 	T_ASSERT_MACH_SUCCESS(kr, "task_get_special_port() - NAME");
58 	kr = task_identity_token_get_task_port(token, TASK_FLAVOR_NAME, &port2);
59 	T_ASSERT_MACH_SUCCESS(kr, "task_identity_token_get_task_port() - name");
60 	T_EXPECT_EQ(port1, port2, "Name port does not match!");
61 
62 	mach_port_deallocate(mach_task_self(), port1);
63 	mach_port_deallocate(mach_task_self(), port2);
64 
65 	kr = task_identity_token_get_task_port(mach_thread_self(), TASK_FLAVOR_NAME, &port2);
66 	T_EXPECT_NE(kr, KERN_SUCCESS, "task_identity_token_get_task_port() should fail on non-token port");
67 
68 	mach_port_deallocate(mach_task_self(), token);
69 }
70