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