xref: /xnu-11215.61.5/tests/task_is_self.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
1*4f1223e8SApple OSS Distributions #include <darwintest.h>
2*4f1223e8SApple OSS Distributions #include <mach/mach.h>
3*4f1223e8SApple OSS Distributions #include <mach/task.h>
4*4f1223e8SApple OSS Distributions #include <mach/mach_init.h>
5*4f1223e8SApple OSS Distributions 
6*4f1223e8SApple OSS Distributions T_GLOBAL_META(
7*4f1223e8SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
8*4f1223e8SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
9*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
10*4f1223e8SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
11*4f1223e8SApple OSS Distributions 	T_META_TAG_VM_PREFERRED);
12*4f1223e8SApple OSS Distributions 
13*4f1223e8SApple OSS Distributions T_DECL(mach_task_is_self,
14*4f1223e8SApple OSS Distributions     "test task port comparison check")
15*4f1223e8SApple OSS Distributions {
16*4f1223e8SApple OSS Distributions 	mach_port_t self_insp, self_read, self_name, port;
17*4f1223e8SApple OSS Distributions 
18*4f1223e8SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(task_get_special_port(mach_task_self(), TASK_READ_PORT, &self_read), "task_get_special_port failed");
19*4f1223e8SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(task_get_special_port(mach_task_self(), TASK_INSPECT_PORT, &self_insp), "task_get_special_port failed");
20*4f1223e8SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(task_get_special_port(mach_task_self(), TASK_NAME_PORT, &self_name), "task_get_special_port failed");
21*4f1223e8SApple OSS Distributions 
22*4f1223e8SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port), "mach_port_allocate failed");
23*4f1223e8SApple OSS Distributions 
24*4f1223e8SApple OSS Distributions 	T_EXPECT_NE(self_read, self_insp, "read and inspect port should be different");
25*4f1223e8SApple OSS Distributions 	T_EXPECT_NE(self_read, mach_task_self(), "read and control port should be different");
26*4f1223e8SApple OSS Distributions 
27*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(1, mach_task_is_self(mach_task_self()), "control port should point to self");
28*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(1, mach_task_is_self(self_read), "read port should point to self");
29*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(1, mach_task_is_self(self_insp), "inspect port should point to self");
30*4f1223e8SApple OSS Distributions 	T_EXPECT_EQ(1, mach_task_is_self(self_name), "name port should point to self");
31*4f1223e8SApple OSS Distributions 	T_EXPECT_NE(1, mach_task_is_self(port), "_port_ should not point to self");
32*4f1223e8SApple OSS Distributions }
33