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