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