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