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