xref: /xnu-11417.140.69/tests/mach_port_insert_right.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions #include <stdio.h>
2*43a90889SApple OSS Distributions #include <mach/mach.h>
3*43a90889SApple OSS Distributions #include <mach/message.h>
4*43a90889SApple OSS Distributions #include <darwintest.h>
5*43a90889SApple OSS Distributions 
6*43a90889SApple OSS Distributions T_GLOBAL_META(
7*43a90889SApple OSS Distributions 	T_META_NAMESPACE("xnu.ipc"),
8*43a90889SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(TRUE),
9*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
10*43a90889SApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("IPC"),
11*43a90889SApple OSS Distributions 	T_META_TAG_VM_PREFERRED);
12*43a90889SApple OSS Distributions 
13*43a90889SApple OSS Distributions static inline mach_port_type_t
get_port_type(mach_port_t mp)14*43a90889SApple OSS Distributions get_port_type(mach_port_t mp)
15*43a90889SApple OSS Distributions {
16*43a90889SApple OSS Distributions 	mach_port_type_t type;
17*43a90889SApple OSS Distributions 	T_QUIET;
18*43a90889SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(mach_port_type(mach_task_self(), mp, &type),
19*43a90889SApple OSS Distributions 	    "mach_port_type(mP)");
20*43a90889SApple OSS Distributions 	return type;
21*43a90889SApple OSS Distributions }
22*43a90889SApple OSS Distributions 
23*43a90889SApple OSS Distributions T_DECL(mach_port_insert_right, "insert send right for an existing right", T_META_CHECK_LEAKS(false))
24*43a90889SApple OSS Distributions {
25*43a90889SApple OSS Distributions 	mach_port_t port = MACH_PORT_NULL;
26*43a90889SApple OSS Distributions 	mach_port_t port2 = MACH_PORT_NULL;
27*43a90889SApple OSS Distributions 	kern_return_t retval;
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions 	mach_port_t task = mach_task_self();
30*43a90889SApple OSS Distributions 
31*43a90889SApple OSS Distributions 	retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port);
32*43a90889SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port);
33*43a90889SApple OSS Distributions 
34*43a90889SApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE,
35*43a90889SApple OSS Distributions 	    "0x%x should be a receive right", port);
36*43a90889SApple OSS Distributions 
37*43a90889SApple OSS Distributions 	retval = mach_port_insert_right(task, port, port, MACH_MSG_TYPE_MAKE_SEND);
38*43a90889SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "insert a send right for port=[%d] with name=[%d]", port, port);
39*43a90889SApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE | MACH_PORT_TYPE_SEND,
40*43a90889SApple OSS Distributions 	    "0x%x should be a send-receive right", port);
41*43a90889SApple OSS Distributions 
42*43a90889SApple OSS Distributions 	mach_port_name_t name = 123;
43*43a90889SApple OSS Distributions 	/* 0x7B, lower two bits are 0b11 which is valid gen number, but index is invalid (0) */
44*43a90889SApple OSS Distributions 	retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
45*43a90889SApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_FAILURE, "insert a send right for port=[%d] with name=[%d]", port, name);
46*43a90889SApple OSS Distributions 
47*43a90889SApple OSS Distributions 	name = 122;
48*43a90889SApple OSS Distributions 	/* invalid gen number, should return KERN_INVALID_VALUE */
49*43a90889SApple OSS Distributions 	retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
50*43a90889SApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_INVALID_VALUE, "insert a send right for port=[%d] with name=[%d]", port, name);
51*43a90889SApple OSS Distributions 
52*43a90889SApple OSS Distributions 	retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port2);
53*43a90889SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port2);
54*43a90889SApple OSS Distributions 
55*43a90889SApple OSS Distributions 	T_ASSERT_EQ(get_port_type(port2), MACH_PORT_TYPE_RECEIVE,
56*43a90889SApple OSS Distributions 	    "0x%x should be a receive right", port2);
57*43a90889SApple OSS Distributions 
58*43a90889SApple OSS Distributions 	name = port;
59*43a90889SApple OSS Distributions 	retval = mach_port_insert_right(task, name, port2, MACH_MSG_TYPE_MAKE_SEND);
60*43a90889SApple OSS Distributions 	T_ASSERT_MACH_ERROR(retval, KERN_RIGHT_EXISTS, "insert a send right for port=[%d] with name=[%d]", port2, name);
61*43a90889SApple OSS Distributions }
62