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