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