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