1*4d495c6eSApple OSS Distributions /*
2*4d495c6eSApple OSS Distributions * Copyright (c) 2018 Apple Computer, Inc. All rights reserved.
3*4d495c6eSApple OSS Distributions *
4*4d495c6eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*4d495c6eSApple OSS Distributions *
6*4d495c6eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*4d495c6eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*4d495c6eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*4d495c6eSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*4d495c6eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*4d495c6eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*4d495c6eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*4d495c6eSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*4d495c6eSApple OSS Distributions *
15*4d495c6eSApple OSS Distributions * Please obtain a copy of the License at
16*4d495c6eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*4d495c6eSApple OSS Distributions *
18*4d495c6eSApple OSS Distributions * The Original Code and all software distributed under the License are
19*4d495c6eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*4d495c6eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*4d495c6eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*4d495c6eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*4d495c6eSApple OSS Distributions * Please see the License for the specific language governing rights and
24*4d495c6eSApple OSS Distributions * limitations under the License.
25*4d495c6eSApple OSS Distributions *
26*4d495c6eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*4d495c6eSApple OSS Distributions */
28*4d495c6eSApple OSS Distributions #include <darwintest.h>
29*4d495c6eSApple OSS Distributions #include <mach/port_descriptions.h>
30*4d495c6eSApple OSS Distributions
31*4d495c6eSApple OSS Distributions T_GLOBAL_META(
32*4d495c6eSApple OSS Distributions T_META_NAMESPACE("xnu.ipc"),
33*4d495c6eSApple OSS Distributions T_META_RUN_CONCURRENTLY(TRUE),
34*4d495c6eSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
35*4d495c6eSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("IPC"));
36*4d495c6eSApple OSS Distributions
37*4d495c6eSApple OSS Distributions static void
expect_special_port_description(const char * (* fn)(int),mach_port_t port,const char * namestr)38*4d495c6eSApple OSS Distributions expect_special_port_description(const char *(*fn)(int),
39*4d495c6eSApple OSS Distributions mach_port_t port, const char *namestr)
40*4d495c6eSApple OSS Distributions {
41*4d495c6eSApple OSS Distributions const char *desc = fn((int)port);
42*4d495c6eSApple OSS Distributions T_EXPECT_NOTNULL(desc, "%s is %s", namestr, desc);
43*4d495c6eSApple OSS Distributions if (desc) {
44*4d495c6eSApple OSS Distributions T_QUIET; T_EXPECT_GT(strlen(desc), strlen(""),
45*4d495c6eSApple OSS Distributions "%s's description string is not empty", namestr);
46*4d495c6eSApple OSS Distributions }
47*4d495c6eSApple OSS Distributions }
48*4d495c6eSApple OSS Distributions
49*4d495c6eSApple OSS Distributions T_DECL(host_special_port_descriptions,
50*4d495c6eSApple OSS Distributions "verify that host special ports can be described", T_META_TAG_VM_PREFERRED)
51*4d495c6eSApple OSS Distributions {
52*4d495c6eSApple OSS Distributions #define TEST_HSP(portdef) \
53*4d495c6eSApple OSS Distributions expect_special_port_description(mach_host_special_port_description, \
54*4d495c6eSApple OSS Distributions portdef, #portdef)
55*4d495c6eSApple OSS Distributions
56*4d495c6eSApple OSS Distributions TEST_HSP(HOST_PORT);
57*4d495c6eSApple OSS Distributions TEST_HSP(HOST_PRIV_PORT);
58*4d495c6eSApple OSS Distributions TEST_HSP(HOST_IO_MAIN_PORT);
59*4d495c6eSApple OSS Distributions TEST_HSP(HOST_DYNAMIC_PAGER_PORT);
60*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AUDIT_CONTROL_PORT);
61*4d495c6eSApple OSS Distributions TEST_HSP(HOST_USER_NOTIFICATION_PORT);
62*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AUTOMOUNTD_PORT);
63*4d495c6eSApple OSS Distributions TEST_HSP(HOST_LOCKD_PORT);
64*4d495c6eSApple OSS Distributions TEST_HSP(HOST_KTRACE_BACKGROUND_PORT);
65*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SEATBELT_PORT);
66*4d495c6eSApple OSS Distributions TEST_HSP(HOST_KEXTD_PORT);
67*4d495c6eSApple OSS Distributions TEST_HSP(HOST_LAUNCHCTL_PORT);
68*4d495c6eSApple OSS Distributions TEST_HSP(HOST_UNFREED_PORT);
69*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AMFID_PORT);
70*4d495c6eSApple OSS Distributions TEST_HSP(HOST_GSSD_PORT);
71*4d495c6eSApple OSS Distributions TEST_HSP(HOST_TELEMETRY_PORT);
72*4d495c6eSApple OSS Distributions TEST_HSP(HOST_ATM_NOTIFICATION_PORT);
73*4d495c6eSApple OSS Distributions TEST_HSP(HOST_COALITION_PORT);
74*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SYSDIAGNOSE_PORT);
75*4d495c6eSApple OSS Distributions TEST_HSP(HOST_XPC_EXCEPTION_PORT);
76*4d495c6eSApple OSS Distributions TEST_HSP(HOST_CONTAINERD_PORT);
77*4d495c6eSApple OSS Distributions TEST_HSP(HOST_NODE_PORT);
78*4d495c6eSApple OSS Distributions TEST_HSP(HOST_RESOURCE_NOTIFY_PORT);
79*4d495c6eSApple OSS Distributions TEST_HSP(HOST_CLOSURED_PORT);
80*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SYSPOLICYD_PORT);
81*4d495c6eSApple OSS Distributions TEST_HSP(HOST_FILECOORDINATIOND_PORT);
82*4d495c6eSApple OSS Distributions TEST_HSP(HOST_FAIRPLAYD_PORT);
83*4d495c6eSApple OSS Distributions TEST_HSP(HOST_IOCOMPRESSIONSTATS_PORT);
84*4d495c6eSApple OSS Distributions TEST_HSP(HOST_MEMORY_ERROR_PORT);
85*4d495c6eSApple OSS Distributions TEST_HSP(HOST_MANAGEDAPPDISTD_PORT);
86*4d495c6eSApple OSS Distributions TEST_HSP(HOST_DOUBLEAGENTD_PORT);
87*4d495c6eSApple OSS Distributions
88*4d495c6eSApple OSS Distributions #undef TEST_HSP
89*4d495c6eSApple OSS Distributions
90*4d495c6eSApple OSS Distributions T_EXPECT_EQ(HOST_DOUBLEAGENTD_PORT, HOST_MAX_SPECIAL_PORT,
91*4d495c6eSApple OSS Distributions "checked all of the ports");
92*4d495c6eSApple OSS Distributions
93*4d495c6eSApple OSS Distributions const char *invalid_hsp =
94*4d495c6eSApple OSS Distributions mach_host_special_port_description(HOST_MAX_SPECIAL_PORT + 1);
95*4d495c6eSApple OSS Distributions T_EXPECT_NULL(invalid_hsp,
96*4d495c6eSApple OSS Distributions "invalid host special port description should be NULL");
97*4d495c6eSApple OSS Distributions }
98*4d495c6eSApple OSS Distributions
99*4d495c6eSApple OSS Distributions T_DECL(task_special_port_descriptions,
100*4d495c6eSApple OSS Distributions "verify that task special ports can be described", T_META_TAG_VM_PREFERRED)
101*4d495c6eSApple OSS Distributions {
102*4d495c6eSApple OSS Distributions #define TEST_TSP(portdef) \
103*4d495c6eSApple OSS Distributions expect_special_port_description(mach_task_special_port_description, \
104*4d495c6eSApple OSS Distributions portdef, #portdef)
105*4d495c6eSApple OSS Distributions
106*4d495c6eSApple OSS Distributions TEST_TSP(TASK_KERNEL_PORT);
107*4d495c6eSApple OSS Distributions TEST_TSP(TASK_READ_PORT);
108*4d495c6eSApple OSS Distributions TEST_TSP(TASK_INSPECT_PORT);
109*4d495c6eSApple OSS Distributions TEST_TSP(TASK_HOST_PORT);
110*4d495c6eSApple OSS Distributions TEST_TSP(TASK_NAME_PORT);
111*4d495c6eSApple OSS Distributions TEST_TSP(TASK_BOOTSTRAP_PORT);
112*4d495c6eSApple OSS Distributions TEST_TSP(TASK_ACCESS_PORT);
113*4d495c6eSApple OSS Distributions TEST_TSP(TASK_DEBUG_CONTROL_PORT);
114*4d495c6eSApple OSS Distributions TEST_TSP(TASK_RESOURCE_NOTIFY_PORT);
115*4d495c6eSApple OSS Distributions
116*4d495c6eSApple OSS Distributions #undef TEST_TSP
117*4d495c6eSApple OSS Distributions
118*4d495c6eSApple OSS Distributions T_EXPECT_EQ(TASK_RESOURCE_NOTIFY_PORT, TASK_MAX_SPECIAL_PORT,
119*4d495c6eSApple OSS Distributions "checked all of the ports");
120*4d495c6eSApple OSS Distributions
121*4d495c6eSApple OSS Distributions const char *invalid_tsp =
122*4d495c6eSApple OSS Distributions mach_task_special_port_description(TASK_MAX_SPECIAL_PORT + 1);
123*4d495c6eSApple OSS Distributions T_EXPECT_NULL(invalid_tsp,
124*4d495c6eSApple OSS Distributions "invalid task special port description should be NULL");
125*4d495c6eSApple OSS Distributions }
126*4d495c6eSApple OSS Distributions
127*4d495c6eSApple OSS Distributions T_DECL(thread_special_port_descriptions,
128*4d495c6eSApple OSS Distributions "verify that thread special ports can be described", T_META_TAG_VM_PREFERRED)
129*4d495c6eSApple OSS Distributions {
130*4d495c6eSApple OSS Distributions #define TEST_TSP(portdef) \
131*4d495c6eSApple OSS Distributions expect_special_port_description(mach_thread_special_port_description, \
132*4d495c6eSApple OSS Distributions portdef, #portdef)
133*4d495c6eSApple OSS Distributions
134*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_KERNEL_PORT);
135*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_READ_PORT);
136*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_INSPECT_PORT);
137*4d495c6eSApple OSS Distributions
138*4d495c6eSApple OSS Distributions #undef TEST_TSP
139*4d495c6eSApple OSS Distributions
140*4d495c6eSApple OSS Distributions T_EXPECT_EQ(THREAD_READ_PORT, THREAD_MAX_SPECIAL_PORT,
141*4d495c6eSApple OSS Distributions "checked all of the ports");
142*4d495c6eSApple OSS Distributions
143*4d495c6eSApple OSS Distributions const char *invalid_tsp =
144*4d495c6eSApple OSS Distributions mach_thread_special_port_description(THREAD_MAX_SPECIAL_PORT + 1);
145*4d495c6eSApple OSS Distributions T_EXPECT_NULL(invalid_tsp,
146*4d495c6eSApple OSS Distributions "invalid thread special port description should be NULL");
147*4d495c6eSApple OSS Distributions }
148*4d495c6eSApple OSS Distributions
149*4d495c6eSApple OSS Distributions static void
expect_special_port_id(int (* fn)(const char * id),int port,const char * portid)150*4d495c6eSApple OSS Distributions expect_special_port_id(int (*fn)(const char *id), int port, const char *portid)
151*4d495c6eSApple OSS Distributions {
152*4d495c6eSApple OSS Distributions int observed_port = fn(portid);
153*4d495c6eSApple OSS Distributions T_WITH_ERRNO;
154*4d495c6eSApple OSS Distributions T_EXPECT_EQ(observed_port, port, "%s is %d", portid, observed_port);
155*4d495c6eSApple OSS Distributions }
156*4d495c6eSApple OSS Distributions
157*4d495c6eSApple OSS Distributions T_DECL(host_special_port_mapping,
158*4d495c6eSApple OSS Distributions "verify that task special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
159*4d495c6eSApple OSS Distributions {
160*4d495c6eSApple OSS Distributions #define TEST_HSP(portdef) \
161*4d495c6eSApple OSS Distributions expect_special_port_id(mach_host_special_port_for_id, \
162*4d495c6eSApple OSS Distributions portdef, #portdef)
163*4d495c6eSApple OSS Distributions
164*4d495c6eSApple OSS Distributions TEST_HSP(HOST_PORT);
165*4d495c6eSApple OSS Distributions TEST_HSP(HOST_PRIV_PORT);
166*4d495c6eSApple OSS Distributions TEST_HSP(HOST_IO_MAIN_PORT);
167*4d495c6eSApple OSS Distributions TEST_HSP(HOST_DYNAMIC_PAGER_PORT);
168*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AUDIT_CONTROL_PORT);
169*4d495c6eSApple OSS Distributions TEST_HSP(HOST_USER_NOTIFICATION_PORT);
170*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AUTOMOUNTD_PORT);
171*4d495c6eSApple OSS Distributions TEST_HSP(HOST_LOCKD_PORT);
172*4d495c6eSApple OSS Distributions TEST_HSP(HOST_KTRACE_BACKGROUND_PORT);
173*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SEATBELT_PORT);
174*4d495c6eSApple OSS Distributions TEST_HSP(HOST_KEXTD_PORT);
175*4d495c6eSApple OSS Distributions TEST_HSP(HOST_LAUNCHCTL_PORT);
176*4d495c6eSApple OSS Distributions TEST_HSP(HOST_UNFREED_PORT);
177*4d495c6eSApple OSS Distributions TEST_HSP(HOST_AMFID_PORT);
178*4d495c6eSApple OSS Distributions TEST_HSP(HOST_GSSD_PORT);
179*4d495c6eSApple OSS Distributions TEST_HSP(HOST_TELEMETRY_PORT);
180*4d495c6eSApple OSS Distributions TEST_HSP(HOST_ATM_NOTIFICATION_PORT);
181*4d495c6eSApple OSS Distributions TEST_HSP(HOST_COALITION_PORT);
182*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SYSDIAGNOSE_PORT);
183*4d495c6eSApple OSS Distributions TEST_HSP(HOST_XPC_EXCEPTION_PORT);
184*4d495c6eSApple OSS Distributions TEST_HSP(HOST_CONTAINERD_PORT);
185*4d495c6eSApple OSS Distributions TEST_HSP(HOST_NODE_PORT);
186*4d495c6eSApple OSS Distributions TEST_HSP(HOST_RESOURCE_NOTIFY_PORT);
187*4d495c6eSApple OSS Distributions TEST_HSP(HOST_CLOSURED_PORT);
188*4d495c6eSApple OSS Distributions TEST_HSP(HOST_SYSPOLICYD_PORT);
189*4d495c6eSApple OSS Distributions TEST_HSP(HOST_FILECOORDINATIOND_PORT);
190*4d495c6eSApple OSS Distributions
191*4d495c6eSApple OSS Distributions #undef TEST_HSP
192*4d495c6eSApple OSS Distributions
193*4d495c6eSApple OSS Distributions int invalid_tsp = mach_host_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
194*4d495c6eSApple OSS Distributions T_EXPECT_EQ(invalid_tsp, -1,
195*4d495c6eSApple OSS Distributions "invalid host special port IDs should return -1");
196*4d495c6eSApple OSS Distributions }
197*4d495c6eSApple OSS Distributions
198*4d495c6eSApple OSS Distributions T_DECL(task_special_port_mapping,
199*4d495c6eSApple OSS Distributions "verify that task special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
200*4d495c6eSApple OSS Distributions {
201*4d495c6eSApple OSS Distributions #define TEST_TSP(portdef) \
202*4d495c6eSApple OSS Distributions expect_special_port_id(mach_task_special_port_for_id, \
203*4d495c6eSApple OSS Distributions portdef, #portdef)
204*4d495c6eSApple OSS Distributions
205*4d495c6eSApple OSS Distributions TEST_TSP(TASK_KERNEL_PORT);
206*4d495c6eSApple OSS Distributions TEST_TSP(TASK_READ_PORT);
207*4d495c6eSApple OSS Distributions TEST_TSP(TASK_INSPECT_PORT);
208*4d495c6eSApple OSS Distributions TEST_TSP(TASK_HOST_PORT);
209*4d495c6eSApple OSS Distributions TEST_TSP(TASK_NAME_PORT);
210*4d495c6eSApple OSS Distributions TEST_TSP(TASK_BOOTSTRAP_PORT);
211*4d495c6eSApple OSS Distributions TEST_TSP(TASK_ACCESS_PORT);
212*4d495c6eSApple OSS Distributions TEST_TSP(TASK_DEBUG_CONTROL_PORT);
213*4d495c6eSApple OSS Distributions TEST_TSP(TASK_RESOURCE_NOTIFY_PORT);
214*4d495c6eSApple OSS Distributions
215*4d495c6eSApple OSS Distributions #undef TEST_TSP
216*4d495c6eSApple OSS Distributions
217*4d495c6eSApple OSS Distributions int invalid_tsp = mach_task_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
218*4d495c6eSApple OSS Distributions T_EXPECT_EQ(invalid_tsp, -1,
219*4d495c6eSApple OSS Distributions "invalid task special port IDs should return -1");
220*4d495c6eSApple OSS Distributions }
221*4d495c6eSApple OSS Distributions
222*4d495c6eSApple OSS Distributions T_DECL(thread_special_port_mapping,
223*4d495c6eSApple OSS Distributions "verify that thread special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
224*4d495c6eSApple OSS Distributions {
225*4d495c6eSApple OSS Distributions #define TEST_TSP(portdef) \
226*4d495c6eSApple OSS Distributions expect_special_port_id(mach_thread_special_port_for_id, \
227*4d495c6eSApple OSS Distributions portdef, #portdef)
228*4d495c6eSApple OSS Distributions
229*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_KERNEL_PORT);
230*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_READ_PORT);
231*4d495c6eSApple OSS Distributions TEST_TSP(THREAD_INSPECT_PORT);
232*4d495c6eSApple OSS Distributions
233*4d495c6eSApple OSS Distributions #undef TEST_TSP
234*4d495c6eSApple OSS Distributions
235*4d495c6eSApple OSS Distributions int invalid_tsp = mach_thread_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
236*4d495c6eSApple OSS Distributions T_EXPECT_EQ(invalid_tsp, -1,
237*4d495c6eSApple OSS Distributions "invalid thread special port IDs should return -1");
238*4d495c6eSApple OSS Distributions }
239