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