2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
29 #import <sys/boolean.h>
31 #define NELEM(x) (sizeof(x)/sizeof(x[0]))
32 #define LAST_ELEMENT(x) ((x)[NELEM(x)-1])
34 print(const char *format
, ...)
39 vfprintf(stderr
, format
, ap
);
40 fprintf(stderr
, "\n");
44 error(const char *format
, ...)
49 fprintf(stderr
, "ERROR: ");
50 vfprintf(stderr
, format
, ap
);
51 fprintf(stderr
, "\n");
59 port_t bootstrap_port
, port
, myport
;
64 unsigned mach_ports_cnt
;
71 name_array_t service_names
;
72 unsigned service_cnt
, server_cnt
, service_active_cnt
;
73 name_array_t server_names
;
74 boolean_t
*service_actives
;
77 print("test server running");
78 result
= task_get_bootstrap_port(task_self(), &bootstrap_port
);
79 if (result
!= KERN_SUCCESS
) {
80 error("Couldn't get bootstrap port: %d", result
);
83 print("Bootstrap port is %d", bootstrap_port
);
84 if (bootstrap_port
== PORT_NULL
) {
85 error("Invalid bootstrap port");
92 print("Checkin test 1");
93 result
= bootstrap_check_in(bootstrap_port
, "FreeService1", &port
);
94 if (result
!= BOOTSTRAP_SUCCESS
)
95 error("Checkin failed: %d", result
);
97 result
= port_type(task_self(), port
, &ptype
);
98 if (result
!= KERN_SUCCESS
)
99 error("port type failed: %d", result
);
101 print("Checkin returned port type 0x%x", ptype
);
103 * Try a status request
105 result
= bootstrap_status(bootstrap_port
, "FreeService1", &active
);
106 if (result
!= BOOTSTRAP_SUCCESS
)
107 error("Status failed: %d", result
);
108 else if (active
!= TRUE
)
109 error("Service shown inactive");
115 print("lookup test");
116 result
= bootstrap_look_up(bootstrap_port
, "FreeService2", &port
);
117 if (result
!= BOOTSTRAP_SUCCESS
)
118 error("lookup failed: %d", result
);
120 result
= port_type(task_self(), port
, &ptype
);
121 if (result
!= KERN_SUCCESS
)
122 error("port type failed: %d", result
);
124 print("Lookup returned port type 0x%x", ptype
);
126 * Try a status request
128 result
= bootstrap_status(bootstrap_port
, "FreeService2", &active
);
129 if (result
!= BOOTSTRAP_SUCCESS
)
130 error("Status failed: %d", result
);
131 else if (active
!= FALSE
)
132 error("Service shown active");
136 * Test that mach ports are initialized
138 print("mach ports test");
139 result
= mach_ports_lookup(task_self(), &mach_ports
, &mach_ports_cnt
);
140 if (result
!= KERN_SUCCESS
)
141 error("mach_ports_lookup failed: %d", result
);
143 result
= bootstrap_look_up(bootstrap_port
, "NetMsgService", &port
);
144 if (result
!= BOOTSTRAP_SUCCESS
)
145 error("Lookup of NetMsgService failed: %d", result
);
146 else if (port
!= mach_ports
[0])
147 error("mach ports not setup correctly for NetMsgService");
149 result
= bootstrap_look_up(bootstrap_port
, "EnvironService", &port
);
150 if (result
!= BOOTSTRAP_SUCCESS
)
151 error("Lookup of EnvironService failed: %d", result
);
152 else if (port
!= mach_ports
[1])
153 error("mach ports not setup correctly for EnvironService");
155 result
= bootstrap_look_up(bootstrap_port
, "Service", &port
);
156 if (result
!= BOOTSTRAP_SUCCESS
)
157 error("Lookup of Service failed: %d", result
);
158 else if (port
!= mach_ports
[2])
159 error("mach ports not setup correctly for Service");
161 result
= bootstrap_look_up(bootstrap_port
, "WindowService", &port
);
162 if (result
!= BOOTSTRAP_SUCCESS
)
163 error("Lookup of WindowService failed: %d", result
);
164 else if (port
!= mach_ports
[3])
165 error("mach ports not setup correctly for WindowService");
169 * Try doing a checkin with the old service interface
171 result
= service_checkin(mach_ports
[2], mach_ports
[1], &myport
);
172 if (result
!= KERN_SUCCESS
)
173 error("service checkin failed: %d", result
);
175 result
= port_type(task_self(), myport
, &ptype
);
176 if (result
!= KERN_SUCCESS
)
177 error("port type failed: %d", result
);
179 print("Checkin returned port type 0x%x", ptype
);
185 print("register test");
186 print("...Dynamic creation");
187 result
= port_allocate(task_self(), &myport
);
188 if (result
!= KERN_SUCCESS
)
189 error("couldn't allocate port: %d", result
);
191 result
= bootstrap_register(bootstrap_port
, "NewService", myport
);
192 if (result
!= BOOTSTRAP_SUCCESS
)
193 error("Couldn't register port: %d", result
);
197 * Try a lookup on just registered port
199 result
= bootstrap_look_up(bootstrap_port
, "NewService", &port
);
200 if (result
!= BOOTSTRAP_SUCCESS
)
201 error("lookup failed: %d", result
);
203 result
= port_type(task_self(), port
, &ptype
);
204 if (result
!= KERN_SUCCESS
)
205 error("port type failed: %d", result
);
207 print("Lookup returned port type 0x%x", ptype
);
209 error("lookup didn't match register");
214 * Try re-registering service name
216 result
= bootstrap_register(bootstrap_port
, "NewService", myport
);
217 if (result
!= BOOTSTRAP_SERVICE_ACTIVE
)
218 error("Unexpected register response: %d", result
);
221 * Delete the port. This should cause the service to go away
224 port_deallocate(task_self(), myport
);
226 result
= bootstrap_look_up(bootstrap_port
, "NewService", &port
);
227 if (result
!= BOOTSTRAP_UNKNOWN_SERVICE
)
228 error("service active after port deleted");
232 print("...Declared service");
233 result
= port_allocate(task_self(), &myport
);
234 if (result
!= KERN_SUCCESS
)
235 error("couldn't allocate port: %d", result
);
237 result
= bootstrap_register(bootstrap_port
, "FreeService2", myport
);
238 if (result
!= BOOTSTRAP_SUCCESS
)
239 error("Couldn't register port: %d", result
);
243 * Try a lookup on just registered port
245 result
= bootstrap_look_up(bootstrap_port
, "FreeService2", &port
);
246 if (result
!= BOOTSTRAP_SUCCESS
)
247 error("lookup failed: %d", result
);
249 result
= port_type(task_self(), port
, &ptype
);
250 if (result
!= KERN_SUCCESS
)
251 error("port type failed: %d", result
);
253 print("Lookup returned port type 0x%x", ptype
);
255 error("lookup didn't match register");
260 * Delete the port. This should cause service to revert.
262 port_deallocate(task_self(), myport
);
264 result
= bootstrap_status(bootstrap_port
, "FreeService2", &active
);
265 if (result
!= BOOTSTRAP_SUCCESS
)
266 error("Status failed: %d", result
);
267 else if (active
!= FALSE
)
268 error("Service shown active");
273 * Try a checkin on a port bound to Terminal server
275 print("Bound checkin test -- Terminal");
276 result
= bootstrap_check_in(bootstrap_port
, "TerminalService", &port
);
277 if (result
!= BOOTSTRAP_SUCCESS
)
278 error("Checkin of TerminalService failed: %d", result
);
280 result
= port_type(task_self(), port
, &ptype
);
281 if (result
!= KERN_SUCCESS
)
282 error("port type failed: %d", result
);
283 print("Checkin returned port type 0x%x", ptype
);
287 * Try a checkin on a port bound to Sleep server
289 print("Bound checkin test -- Sleep");
290 result
= bootstrap_check_in(bootstrap_port
, "SleepService", &port
);
291 if (result
!= BOOTSTRAP_SUCCESS
)
292 print("Checkin of SleepService failed (as expected): %d",
295 result
= port_type(task_self(), port
, &ptype
);
296 if (result
!= KERN_SUCCESS
)
297 error("port type failed: %d", result
);
298 error("Checkin returned port type 0x%x(didn't fail!)", ptype
);
304 print("Lookup array test");
306 strncpy(&name_array
[0], "NetMsgService", sizeof(name_array
[0]));
307 LAST_ELEMENT(name_array
[0]) = '\0';
308 strncpy(&name_array
[1], "EnvironService", sizeof(name_array
[1]));
309 LAST_ELEMENT(name_array
[1]) = '\0';
310 strncpy(&name_array
[2], "Service", sizeof(name_array
[2]));
311 LAST_ELEMENT(name_array
[2]) = '\0';
312 strncpy(&name_array
[3], "WindowService", sizeof(name_array
[3]));
313 LAST_ELEMENT(name_array
[3]) = '\0';
315 result
= bootstrap_look_up_array(bootstrap_port
, name_array
, 4, &ports
,
316 &port_cnt
, &all_known
);
317 if (result
!= BOOTSTRAP_SUCCESS
)
318 error("Lookup array failed: %d", result
);
320 print("Port count = %d, all known = %d", port_cnt
, all_known
);
321 for (i
= 0; i
< 4; i
++)
322 if (ports
[i
] != mach_ports
[i
])
323 error("port mismatch on port %d", i
);
327 * Get an unprivileged port
329 print("Unprivileged port test");
330 result
= port_allocate(task_self(), &myport
);
331 result
= bootstrap_get_unpriv_port(bootstrap_port
, &unpriv_port
);
332 if (result
!= BOOTSTRAP_SUCCESS
)
333 error("Couldn't get unpriv port: %d", result
);
336 * Try doing an unpriv operation
338 result
= bootstrap_look_up(unpriv_port
, "FreeService2", &port
);
339 if (result
!= BOOTSTRAP_SUCCESS
)
340 error("lookup failed: %d", result
);
342 * Try doing a privileged operation
344 result
= bootstrap_register(unpriv_port
, "ANewService", myport
);
345 if (result
!= BOOTSTRAP_NOT_PRIVILEGED
)
346 error("Unexpected register port response: %d", result
);
349 * Try creating a subset port.
351 result
= bootstrap_subset(unpriv_port
, task_self(),
353 if (result
!= BOOTSTRAP_SUCCESS
)
354 error("Couldn't get subset port from unpriv %d",
361 print("Subset port test");
362 result
= bootstrap_subset(bootstrap_port
, task_self(), &subset_port
);
363 if (result
!= BOOTSTRAP_SUCCESS
)
364 error("Couldn't get subset port: %d", result
);
370 result
= port_allocate(task_self(), &sub_reg_port
);
371 if (result
!= KERN_SUCCESS
)
372 error("port_allocate of sub_reg_port failed %d",
374 result
= bootstrap_register(subset_port
, "SubsetReg",
376 if (result
!= BOOTSTRAP_SUCCESS
)
377 error("register of SubsetReg failed on subset port %d",
380 * Check that port registered only in subset.
382 result
= bootstrap_status(bootstrap_port
, "SubsetReg",
384 if (result
!= BOOTSTRAP_UNKNOWN_SERVICE
)
385 error("status of SubsetReg ok on bootstrap! %d",
387 result
= bootstrap_status(subset_port
, "SubsetReg",
389 if (result
!= BOOTSTRAP_SUCCESS
)
390 error("status of SubsetReg failed on subset port %d",
393 error("SubsetReg isn't active");
397 * Try an info request.
399 print("Subset info request");
400 result
= bootstrap_info(subset_port
, &service_names
,
402 &server_names
, &server_cnt
, &service_actives
,
403 &service_active_cnt
);
404 if (result
!= BOOTSTRAP_SUCCESS
)
405 error("info failed: %d", result
);
407 for (i
= 0; i
< service_cnt
; i
++)
408 print("Name: %s Server: %s Active: %s",
410 server_names
[i
][0] == '\0'
413 service_actives
[i
] ? "Yes" : "No");
418 * Try an info request
421 result
= bootstrap_info(bootstrap_port
, &service_names
, &service_cnt
,
422 &server_names
, &server_cnt
, &service_actives
, &service_active_cnt
);
423 if (result
!= BOOTSTRAP_SUCCESS
)
424 error("info failed: %d", result
);
426 for (i
= 0; i
< service_cnt
; i
++)
427 print("Name: %s Server: %s Active: %s", service_names
[i
],
428 server_names
[i
][0] == '\0' ? "Unknown" : server_names
[i
],
429 service_actives
[i
] ? "Yes" : "No");