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