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