xref: /xnu-11215.61.5/tests/port_descriptions.c (revision 4f1223e81cd707a65cc109d0b8ad6653699da3c4)
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", T_META_TAG_VM_PREFERRED)
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 	TEST_HSP(HOST_DOUBLEAGENTD_PORT);
87 
88 #undef TEST_HSP
89 
90 	T_EXPECT_EQ(HOST_DOUBLEAGENTD_PORT, HOST_MAX_SPECIAL_PORT,
91 	    "checked all of the ports");
92 
93 	const char *invalid_hsp =
94 	    mach_host_special_port_description(HOST_MAX_SPECIAL_PORT + 1);
95 	T_EXPECT_NULL(invalid_hsp,
96 	    "invalid host special port description should be NULL");
97 }
98 
99 T_DECL(task_special_port_descriptions,
100     "verify that task special ports can be described", T_META_TAG_VM_PREFERRED)
101 {
102 #define TEST_TSP(portdef) \
103 	        expect_special_port_description(mach_task_special_port_description, \
104 	        portdef, #portdef)
105 
106 	TEST_TSP(TASK_KERNEL_PORT);
107 	TEST_TSP(TASK_READ_PORT);
108 	TEST_TSP(TASK_INSPECT_PORT);
109 	TEST_TSP(TASK_HOST_PORT);
110 	TEST_TSP(TASK_NAME_PORT);
111 	TEST_TSP(TASK_BOOTSTRAP_PORT);
112 	TEST_TSP(TASK_ACCESS_PORT);
113 	TEST_TSP(TASK_DEBUG_CONTROL_PORT);
114 	TEST_TSP(TASK_RESOURCE_NOTIFY_PORT);
115 
116 #undef TEST_TSP
117 
118 	T_EXPECT_EQ(TASK_RESOURCE_NOTIFY_PORT, TASK_MAX_SPECIAL_PORT,
119 	    "checked all of the ports");
120 
121 	const char *invalid_tsp =
122 	    mach_task_special_port_description(TASK_MAX_SPECIAL_PORT + 1);
123 	T_EXPECT_NULL(invalid_tsp,
124 	    "invalid task special port description should be NULL");
125 }
126 
127 T_DECL(thread_special_port_descriptions,
128     "verify that thread special ports can be described", T_META_TAG_VM_PREFERRED)
129 {
130 #define TEST_TSP(portdef) \
131 	        expect_special_port_description(mach_thread_special_port_description, \
132 	        portdef, #portdef)
133 
134 	TEST_TSP(THREAD_KERNEL_PORT);
135 	TEST_TSP(THREAD_READ_PORT);
136 	TEST_TSP(THREAD_INSPECT_PORT);
137 
138 #undef TEST_TSP
139 
140 	T_EXPECT_EQ(THREAD_READ_PORT, THREAD_MAX_SPECIAL_PORT,
141 	    "checked all of the ports");
142 
143 	const char *invalid_tsp =
144 	    mach_thread_special_port_description(THREAD_MAX_SPECIAL_PORT + 1);
145 	T_EXPECT_NULL(invalid_tsp,
146 	    "invalid thread special port description should be NULL");
147 }
148 
149 static void
expect_special_port_id(int (* fn)(const char * id),int port,const char * portid)150 expect_special_port_id(int (*fn)(const char *id), int port, const char *portid)
151 {
152 	int observed_port = fn(portid);
153 	T_WITH_ERRNO;
154 	T_EXPECT_EQ(observed_port, port, "%s is %d", portid, observed_port);
155 }
156 
157 T_DECL(host_special_port_mapping,
158     "verify that task special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
159 {
160 #define TEST_HSP(portdef) \
161 	        expect_special_port_id(mach_host_special_port_for_id, \
162 	        portdef, #portdef)
163 
164 	TEST_HSP(HOST_PORT);
165 	TEST_HSP(HOST_PRIV_PORT);
166 	TEST_HSP(HOST_IO_MAIN_PORT);
167 	TEST_HSP(HOST_DYNAMIC_PAGER_PORT);
168 	TEST_HSP(HOST_AUDIT_CONTROL_PORT);
169 	TEST_HSP(HOST_USER_NOTIFICATION_PORT);
170 	TEST_HSP(HOST_AUTOMOUNTD_PORT);
171 	TEST_HSP(HOST_LOCKD_PORT);
172 	TEST_HSP(HOST_KTRACE_BACKGROUND_PORT);
173 	TEST_HSP(HOST_SEATBELT_PORT);
174 	TEST_HSP(HOST_KEXTD_PORT);
175 	TEST_HSP(HOST_LAUNCHCTL_PORT);
176 	TEST_HSP(HOST_UNFREED_PORT);
177 	TEST_HSP(HOST_AMFID_PORT);
178 	TEST_HSP(HOST_GSSD_PORT);
179 	TEST_HSP(HOST_TELEMETRY_PORT);
180 	TEST_HSP(HOST_ATM_NOTIFICATION_PORT);
181 	TEST_HSP(HOST_COALITION_PORT);
182 	TEST_HSP(HOST_SYSDIAGNOSE_PORT);
183 	TEST_HSP(HOST_XPC_EXCEPTION_PORT);
184 	TEST_HSP(HOST_CONTAINERD_PORT);
185 	TEST_HSP(HOST_NODE_PORT);
186 	TEST_HSP(HOST_RESOURCE_NOTIFY_PORT);
187 	TEST_HSP(HOST_CLOSURED_PORT);
188 	TEST_HSP(HOST_SYSPOLICYD_PORT);
189 	TEST_HSP(HOST_FILECOORDINATIOND_PORT);
190 
191 #undef TEST_HSP
192 
193 	int invalid_tsp = mach_host_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
194 	T_EXPECT_EQ(invalid_tsp, -1,
195 	    "invalid host special port IDs should return -1");
196 }
197 
198 T_DECL(task_special_port_mapping,
199     "verify that task special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
200 {
201 #define TEST_TSP(portdef) \
202 	        expect_special_port_id(mach_task_special_port_for_id, \
203 	        portdef, #portdef)
204 
205 	TEST_TSP(TASK_KERNEL_PORT);
206 	TEST_TSP(TASK_READ_PORT);
207 	TEST_TSP(TASK_INSPECT_PORT);
208 	TEST_TSP(TASK_HOST_PORT);
209 	TEST_TSP(TASK_NAME_PORT);
210 	TEST_TSP(TASK_BOOTSTRAP_PORT);
211 	TEST_TSP(TASK_ACCESS_PORT);
212 	TEST_TSP(TASK_DEBUG_CONTROL_PORT);
213 	TEST_TSP(TASK_RESOURCE_NOTIFY_PORT);
214 
215 #undef TEST_TSP
216 
217 	int invalid_tsp = mach_task_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
218 	T_EXPECT_EQ(invalid_tsp, -1,
219 	    "invalid task special port IDs should return -1");
220 }
221 
222 T_DECL(thread_special_port_mapping,
223     "verify that thread special port names can be mapped to numbers", T_META_TAG_VM_PREFERRED)
224 {
225 #define TEST_TSP(portdef) \
226 	        expect_special_port_id(mach_thread_special_port_for_id, \
227 	        portdef, #portdef)
228 
229 	TEST_TSP(THREAD_KERNEL_PORT);
230 	TEST_TSP(THREAD_READ_PORT);
231 	TEST_TSP(THREAD_INSPECT_PORT);
232 
233 #undef TEST_TSP
234 
235 	int invalid_tsp = mach_thread_special_port_for_id("BOGUS_SPECIAL_PORT_NAME");
236 	T_EXPECT_EQ(invalid_tsp, -1,
237 	    "invalid thread special port IDs should return -1");
238 }
239