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