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