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