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