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