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