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