]> git.saurik.com Git - apple/xnu.git/blob - tests/mach_port_insert_right.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / mach_port_insert_right.c
1 #include <stdio.h>
2 #include <mach/mach.h>
3 #include <mach/message.h>
4 #include <darwintest.h>
5
6 T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
7
8 static inline mach_port_type_t
9 get_port_type(mach_port_t mp)
10 {
11 mach_port_type_t type;
12 T_QUIET;
13 T_ASSERT_MACH_SUCCESS(mach_port_type(mach_task_self(), mp, &type),
14 "mach_port_type(mP)");
15 return type;
16 }
17
18 T_DECL(mach_port_insert_right, "insert send right for an existing right", T_META_CHECK_LEAKS(false))
19 {
20 mach_port_t port = MACH_PORT_NULL;
21 mach_port_t port2 = MACH_PORT_NULL;
22 kern_return_t retval;
23
24 mach_port_t task = mach_task_self();
25
26 retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port);
27 T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port);
28
29 T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE,
30 "0x%x should be a receive right", port);
31
32 retval = mach_port_insert_right(task, port, port, MACH_MSG_TYPE_MAKE_SEND);
33 T_ASSERT_MACH_SUCCESS(retval, "insert a send right for port=[%d] with name=[%d]", port, port);
34 T_ASSERT_EQ(get_port_type(port), MACH_PORT_TYPE_RECEIVE | MACH_PORT_TYPE_SEND,
35 "0x%x should be a send-receive right", port);
36
37 mach_port_name_t name = 123;
38
39 retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
40 T_ASSERT_MACH_ERROR(retval, KERN_FAILURE, "insert a send right for port=[%d] with name=[%d]", port, name);
41
42 name = port + 1;
43 retval = mach_port_insert_right(task, name, port, MACH_MSG_TYPE_MAKE_SEND);
44 T_ASSERT_MACH_ERROR(retval, KERN_FAILURE, "insert a send right for port=[%d] with name=[%d]", port, name);
45
46 retval = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &port2);
47 T_ASSERT_MACH_SUCCESS(retval, "allocate a port=[%d]", port2);
48
49 T_ASSERT_EQ(get_port_type(port2), MACH_PORT_TYPE_RECEIVE,
50 "0x%x should be a receive right", port2);
51
52 name = port;
53 retval = mach_port_insert_right(task, name, port2, MACH_MSG_TYPE_MAKE_SEND);
54 T_ASSERT_MACH_ERROR(retval, KERN_RIGHT_EXISTS, "insert a send right for port=[%d] with name=[%d]", port2, name);
55 }