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