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