xref: /xnu-12377.41.6/tests/ipc/port_api.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828) !
1*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
2*bbb1b6f9SApple OSS Distributions #include <darwintest_utils.h>
3*bbb1b6f9SApple OSS Distributions 
4*bbb1b6f9SApple OSS Distributions #include <mach/mach.h>
5*bbb1b6f9SApple OSS Distributions #include <mach/mach_types.h>
6*bbb1b6f9SApple OSS Distributions #include <mach/mach_vm.h>
7*bbb1b6f9SApple OSS Distributions #include <mach/message.h>
8*bbb1b6f9SApple OSS Distributions #include <mach/mach_error.h>
9*bbb1b6f9SApple OSS Distributions #include <mach/task.h>
10*bbb1b6f9SApple OSS Distributions 
11*bbb1b6f9SApple OSS Distributions #include <pthread.h>
12*bbb1b6f9SApple OSS Distributions #include <pthread/workqueue_private.h>
13*bbb1b6f9SApple OSS Distributions 
14*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
15*bbb1b6f9SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
16*bbb1b6f9SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
17*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
18*bbb1b6f9SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"));
19*bbb1b6f9SApple OSS Distributions 
20*bbb1b6f9SApple OSS Distributions T_DECL(mach_port_insert_right_123724977, "regression test for 123724977")
21*bbb1b6f9SApple OSS Distributions {
22*bbb1b6f9SApple OSS Distributions 	mach_port_name_t pset;
23*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
24*bbb1b6f9SApple OSS Distributions 
25*bbb1b6f9SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_PORT_SET, &pset);
26*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "creating port set");
27*bbb1b6f9SApple OSS Distributions 
28*bbb1b6f9SApple OSS Distributions 	kr = mach_port_insert_right(mach_task_self(), pset, pset,
29*bbb1b6f9SApple OSS Distributions 	    MACH_MSG_TYPE_MAKE_SEND);
30*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_RIGHT, "insert right fails");
31*bbb1b6f9SApple OSS Distributions }
32*bbb1b6f9SApple OSS Distributions 
33*bbb1b6f9SApple OSS Distributions T_DECL(mach_port_name_rules, "make sure port names work correctly")
34*bbb1b6f9SApple OSS Distributions {
35*bbb1b6f9SApple OSS Distributions 	mach_port_type_t ty;
36*bbb1b6f9SApple OSS Distributions 	kern_return_t kr;
37*bbb1b6f9SApple OSS Distributions 	mach_port_t mp, mp2;
38*bbb1b6f9SApple OSS Distributions 
39*bbb1b6f9SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp);
40*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "creating port");
41*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(mp & 0x3u, 0x3, "low bits are 0x3");
42*bbb1b6f9SApple OSS Distributions 
43*bbb1b6f9SApple OSS Distributions 	kr = mach_port_type(mach_task_self(), mp, &ty);
44*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_type");
45*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(ty & MACH_PORT_TYPE_RECEIVE, "mp is a receive right");
46*bbb1b6f9SApple OSS Distributions 
47*bbb1b6f9SApple OSS Distributions 	kr = mach_port_type(mach_task_self(), mp & ~0x3u, &ty);
48*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_NAME,
49*bbb1b6f9SApple OSS Distributions 	    "lookup is sensitive to the low bits");
50*bbb1b6f9SApple OSS Distributions 
51*bbb1b6f9SApple OSS Distributions 	kr = mach_port_destruct(mach_task_self(), mp, 0, 0);
52*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "destroying port");
53*bbb1b6f9SApple OSS Distributions 
54*bbb1b6f9SApple OSS Distributions 	kr = mach_port_type(mach_task_self(), mp, &ty);
55*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_NAME, "port is destroyed");
56*bbb1b6f9SApple OSS Distributions 
57*bbb1b6f9SApple OSS Distributions 	kr = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &mp2);
58*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "creating port");
59*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(mp2 & 0x3, 0x3, "low bits are 0x3");
60*bbb1b6f9SApple OSS Distributions 	T_ASSERT_NE(mp, mp2, "port name will change");
61*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(mp & ~0xffu, mp2 & ~0xffu,
62*bbb1b6f9SApple OSS Distributions 	    "the index was reused with a generation delta of %d",
63*bbb1b6f9SApple OSS Distributions 	    (mp2 - mp) >> 2);
64*bbb1b6f9SApple OSS Distributions 
65*bbb1b6f9SApple OSS Distributions 	kr = mach_port_destruct(mach_task_self(), mp2, 0, 0);
66*bbb1b6f9SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "destroying port");
67*bbb1b6f9SApple OSS Distributions }
68