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