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