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