1 /*
2 * xnu_quick_test - this tool will do a quick test of every (well, to be
3 * honest most) system calls we support in xnu.
4 *
5 * WARNING - this is not meant to be a full regression test of all the
6 * system calls. The intent is to have a quick test of each system call that
7 * can be run very easily and quickly when a new kerenl is built.
8 *
9 * This tool is meant to grow as we find xnu problems that could have be
10 * caught before we submit to a build train. So please add more tests and
11 * make the existing ones better. Some of the original tests are nothing
12 * more than place holders and quite lame. Just keep in mind that the tool
13 * should run as fast as possible. If it gets too slow then most people
14 * will stop running it.
15 *
16 * LP64 testing tip - when adding or modifying tests, keep in mind the
17 * variants in the LP64 world. If xnu gets passed a structure the varies in
18 * size between 32 and 64-bit processes, try to test that a field in the
19 * sructure contains valid data. For example if we know foo structure
20 * looks like:
21 * struct foo {
22 * int an_int;
23 * long a_long;
24 * int another_int;
25 * }
26 * And if we know what another_int should contain then test for the known
27 * value since it's offset will vary depending on whether the calling process
28 * is 32 or 64 bits.
29 *
30 * NOTE - we have several workarounds and test exceptions for some
31 * outstanding bugs in xnu. All the workarounds are marked with "todo" and
32 * some comments noting the radar number of the offending bug. Do a seach
33 * for "todo" in the source files for this project to locate which tests have
34 * known failures. And please tag any new exceptions you find with "todo"
35 * in the comment and the radar number of the bug.
36 */
37
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <time.h>
44 #include <unistd.h>
45 #include <sys/mount.h>
46 #include <sys/param.h>
47 #include <sys/select.h>
48 #include <sys/stat.h>
49 #include <sys/syslimits.h>
50 #include <sys/types.h>
51 #include <sys/ucred.h>
52 #include <sys/uio.h>
53 #include <XILog/XILog.h>
54
55 #include "tests.h"
56
57
58 /* our table of tests to run */
59 struct test_entry g_tests[] =
60 {
61 {1, &syscall_test, NULL, "syscall"},
62 {1, &fork_wait4_exit_test, NULL, "fork, wait4, exit"},
63 {1, &read_write_test, NULL, "fsync, ftruncate, lseek, pread, pwrite, read, readv, truncate, write, writev"},
64 {1, &open_close_test, NULL, "close, fpathconf, fstat, open, pathconf"},
65 {1, &link_stat_unlink_test, NULL, "link, stat, unlink"},
66 {1, &chdir_fchdir_test, NULL, "chdir, fchdir"},
67 {1, &access_chmod_fchmod_test, NULL, "access, chmod, fchmod"},
68 {1, &chown_fchown_lchown_lstat_symlink_test, NULL, "chown, fchown, lchown, lstat, readlink, symlink"},
69 {1, &fs_stat_tests, NULL, "fstatfs, getattrlist, getfsstat, statfs, getfsstat64, statfs64, fstatfs64"},
70 {1, &getpid_getppid_pipe_test, NULL, "getpid, getppid, pipe"},
71 {1, &uid_tests, NULL, "getauid, gettid, getuid, geteuid, issetugid, setauid, seteuid, settid, settid_with_pid, setuid"},
72 {1, &mkdir_rmdir_umask_test, NULL, "mkdir, rmdir, umask"},
73 {1, &mknod_sync_test, NULL, "mknod, sync"},
74 {1, &socket2_tests, NULL, "fsync, getsockopt, poll, select, setsockopt, socketpair"},
75 {1, &socket_tests, NULL, "accept, bind, connect, getpeername, getsockname, listen, socket, recvmsg, sendmsg, sendto"},
76 {1, &chflags_fchflags_test, NULL, "chflags, fchflags"},
77 {1, &execve_kill_vfork_test, NULL, "kill, vfork, execve, posix_spawn"},
78 {1, &groups_test, NULL, "getegid, getgid, getgroups, setegid, setgid, setgroups"},
79 {1, &dup_test, NULL, "dup, dup2, getdtablesize"},
80 {1, &getrusage_profil_test, NULL, "getrusage, profil"},
81 {1, &signals_test, NULL, "getitimer, setitimer, sigaction, sigpending, sigprocmask, sigsuspend, sigwait"},
82 {1, &acct_test, NULL, "acct"},
83 {1, &ioctl_test, NULL, "ioctl"},
84 {1, &chroot_test, NULL, "chroot"},
85 {1, &memory_tests, NULL, "madvise, mincore, minherit, mlock, mlock, mmap, mprotect, msync, munmap"},
86 {1, &process_group_test, NULL, "getpgrp, getpgid, getsid, setpgid, setpgrp, setsid"},
87 {1, &fcntl_test, NULL, "fcntl"},
88 {1, &getlogin_setlogin_test, NULL, "getlogin, setlogin"},
89 {1, &getpriority_setpriority_test, NULL, "getpriority, setpriority"},
90 {1, &time_tests, NULL, "futimes, gettimeofday, settimeofday, utimes"},
91 {1, &rename_test, NULL, "rename, stat"},
92 {1, &locking_test, NULL, "flock"},
93 {1, &mkfifo_test, NULL, "mkfifo, read, write"},
94 {1, "actl_test, NULL, "quotactl"},
95 {1, &limit_tests, NULL, "getrlimit, setrlimit"},
96 {1, &directory_tests, NULL, "getattrlist, getdirentries, getdirentriesattr, setattrlist"},
97 {1, &exchangedata_test, NULL, "exchangedata"},
98 {1, &searchfs_test, NULL, "searchfs"},
99 {1, &sema2_tests, NULL, "sem_close, sem_open, sem_post, sem_trywait, sem_unlink, sem_wait"},
100 {1, &sema_tests, NULL, "semctl, semget, semop"},
101 {1, &bsd_shm_tests, NULL, "shm_open, shm_unlink"},
102 {1, &shm_tests, NULL, "shmat, shmctl, shmdt, shmget"},
103 {1, &xattr_tests, NULL, "fgetxattr, flistxattr, fremovexattr, fsetxattr, getxattr, listxattr, removexattr, setxattr"},
104 {1, &aio_tests, NULL, "aio_cancel, aio_error, aio_read, aio_return, aio_suspend, aio_write, fcntl, lio_listio"},
105 {1, &kqueue_tests, NULL, "kevent, kqueue"},
106 {1, &message_queue_tests, NULL, "msgctl, msgget, msgrcv, msgsnd"},
107 {0, NULL, NULL, "last one"}
108 };
109
110 static void create_target_directory( const char * the_targetp );
111 static void list_all_tests( void );
112 static void mark_tests_to_run( long my_start, long my_end );
113 static int parse_tests_to_run( int argc, const char * argv[], int * indexp );
114 static void usage( void );
115
116 /* globals */
117 long g_max_failures = 0;
118 int g_skip_setuid_tests = 0;
119 int g_xilog_active = 0;
120 const char * g_cmd_namep;
121 char g_target_path[ PATH_MAX ];
122 int g_is_under_rosetta = 0;
123
124 int main( int argc, const char * argv[] )
125 {
126 int my_tests_count, i;
127 int err;
128 int my_failures = 0;
129 int list_the_tests = 0;
130 const char * my_targetp;
131 time_t my_start_time, my_end_time;
132 struct stat my_stat_buf;
133 char my_buffer[64];
134 /* vars for XILog */
135 XILogRef logRef;
136 char *logPath = "";
137 char *config = NULL;
138 int echo = 0;
139 int xml = 0;
140
141 sranddev( ); /* set up seed for our random name generator */
142 g_cmd_namep = argv[0];
143
144 /* NOTE - code in create_target_directory will append '/' if it is necessary */
145 my_targetp = getenv("TMPDIR");
146 if ( my_targetp == NULL )
147 my_targetp = "/tmp";
148
149 /* make sure our executable is owned by root and has set uid bit */
150 err = stat( g_cmd_namep, &my_stat_buf );
151 if ( err != 0 ) {
152 err = errno;
153 printf( "stat call on our executable failed - \"%s\" \n", g_cmd_namep );
154 printf( " failed with error %d - \"%s\" \n", err, strerror( err) );
155 exit( -1 );
156 }
157 if ( my_stat_buf.st_uid != 0 || (my_stat_buf.st_mode & S_ISUID) == 0 ) {
158 printf( "executable file - \"%s\" \n", g_cmd_namep );
159 printf( "does not have correct owner (must be root) or setuid bit is not set \n" );
160 exit( -1 );
161 }
162
163 /* parse input parameters */
164 for ( i = 1; i < argc; i++ ) {
165 if ( strcmp( argv[i], "-u" ) == 0 ) {
166 usage( );
167 }
168 if ( strcmp( argv[i], "-t" ) == 0 ||
169 strcmp( argv[i], "-target" ) == 0 ) {
170 if ( ++i >= argc ) {
171 printf( "invalid target parameter \n" );
172 usage( );
173 }
174 /* verify our target directory exists */
175 my_targetp = argv[i];
176 err = stat( my_targetp, &my_stat_buf );
177 if ( err != 0 || S_ISDIR(my_stat_buf.st_mode) == 0 ) {
178 printf( "invalid target path \n" );
179 if ( err != 0 ) {
180 printf( "stat call failed with error %d - \"%s\" \n", errno, strerror( errno) );
181 }
182 usage( );
183 }
184 continue;
185 }
186 if ( strcmp( argv[i], "-f" ) == 0 ||
187 strcmp( argv[i], "-failures" ) == 0 ) {
188 if ( ++i >= argc ) {
189 printf( "invalid failures parameter \n" );
190 usage( );
191 }
192
193 /* get our max number of failures */
194 g_max_failures = strtol( argv[i], NULL, 10 );
195 continue;
196 }
197 if ( strcmp( argv[i], "-l" ) == 0 ||
198 strcmp( argv[i], "-list" ) == 0 ) {
199 /* list all the tests this tool will do.
200 */
201 list_the_tests = 1;
202 continue;
203 }
204 if ( strcmp( argv[i], "-r" ) == 0 ||
205 strcmp( argv[i], "-run" ) == 0 ) {
206 if ( ++i >= argc ) {
207 printf( "invalid run tests parameter \n" );
208 usage( );
209 }
210
211 /* get which tests to run */
212 if ( parse_tests_to_run( argc, argv, &i ) != 0 ) {
213 printf( "invalid run tests parameter \n" );
214 usage( );
215 }
216 continue;
217 }
218 if ( strcmp( argv[i], "-s" ) == 0 ||
219 strcmp( argv[i], "-skip" ) == 0 ) {
220 /* set that want to skip the setuid related tests - this is useful for debgugging since since I can't
221 * get setuid tests to work while in gdb.
222 */
223 g_skip_setuid_tests = 1;
224 continue;
225 }
226 if ( strcmp( argv[i], "-x" ) == 0 ||
227 strcmp( argv[i], "-xilog" ) == 0 ) {
228 g_xilog_active = 1;
229 continue;
230 }
231
232 printf( "invalid argument \"%s\" \n", argv[i] );
233 usage( );
234 }
235
236 /* done parsing.
237 */
238
239 #ifdef __ppc__
240 /* determine if we are running under Rosetta
241 */
242 {
243 int val = 0;
244 size_t size = sizeof val;
245 if (sysctlbyname("sysctl.proc_native", &val, &size, NULL, 0) == -1)
246 g_is_under_rosetta = 0;
247 else
248 g_is_under_rosetta = val ? 0 : 1;
249 }
250 #endif
251
252 if ( list_the_tests != 0 ) {
253 list_all_tests( );
254 return 0;
255 }
256
257 if (g_xilog_active == 1) {
258 logRef = XILogOpenLogExtended( logPath, "xnu_quick_test", "com.apple.coreos",
259 config, xml, echo, NULL, "ResultOwner",
260 "com.apple.coreos", NULL );
261 if( logRef == NULL ) {
262 fprintf(stderr,"Couldn't create log: %s",logPath);
263 exit(-1);
264 }
265 }
266
267 /* build a test target directory that we use as our path to create any test
268 * files and directories.
269 */
270 create_target_directory( my_targetp );
271 printf( "Will allow %d failures before testing is aborted \n", g_max_failures );
272
273 if (g_is_under_rosetta) {
274 printf("Running under Rosetta.\n");
275 }
276
277 my_start_time = time( NULL );
278 printf( "\nBegin testing - %s \n", ctime_r( &my_start_time, &my_buffer[0] ) );
279
280 /* run each test that is marked to run in our table until we complete all of them or
281 * hit the maximum number of failures.
282 */
283 my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] ));
284 for ( i = 0; i < (my_tests_count - 1); i++ ) {
285 int my_err;
286 test_entryp my_testp;
287
288 my_testp = &g_tests[i];
289 if ( my_testp->test_run_it == 0 || my_testp->test_routine == NULL )
290 continue;
291 if (g_xilog_active == 1) {
292 XILogBeginTestCase( logRef, my_testp->test_infop, my_testp->test_infop );
293 XILogMsg( "test #%d - %s \n", (i + 1), my_testp->test_infop );
294 }
295 printf( "test #%d - %s \n", (i + 1), my_testp->test_infop );
296 my_err = my_testp->test_routine( my_testp->test_input );
297 if ( my_err != 0 ) {
298 printf("\t--> FAILED \n");
299 if (g_xilog_active == 1) {
300 XILogMsg("SysCall %s failed", my_testp->test_infop);
301 XILogErr("Result %d", my_err);
302 }
303 my_failures++;
304 if ( my_failures > g_max_failures ) {
305 if (g_xilog_active == 1) {
306 XILogEndTestCase( logRef, kXILogTestPassOnErrorLevel );
307 }
308 printf( "\n too many failures - test aborted \n" );
309 goto exit_this_routine;
310 }
311 }
312 if (g_xilog_active == 1) {
313 XILogEndTestCase(logRef, kXILogTestPassOnErrorLevel);
314 }
315 }
316
317 exit_this_routine:
318 my_end_time = time( NULL );
319 printf( "\nEnd testing - %s \n", ctime_r( &my_end_time, &my_buffer[0] ) );
320
321 /* clean up our test directory */
322 rmdir( &g_target_path[0] );
323
324 if (g_xilog_active == 1) {
325 XILogCloseLog(logRef);
326 }
327
328 return 0;
329 } /* main */
330
331
332 /*
333 * parse_tests_to_run - parse the -run argument parameters. the run argument tells us which tests the user
334 * wants to run. we accept ranges (example 1 - 44) and runs of tests (example 2, 33, 34, 100) or a mix of
335 * both (example 1, 44 - 100, 200, 250)
336 */
337 static int parse_tests_to_run( int argc, const char * argv[], int * indexp )
338 {
339 int my_tests_count, is_range = 0, i;
340 const char * my_ptr;
341 char * my_temp_ptr;
342 long my_first_test_number, my_last_test_number;
343 char my_buffer[ 128 ];
344
345 /* set tests table to not run any tests then go back and set the specific tests the caller asked for */
346 my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] ));
347 for ( i = 0; i < (my_tests_count - 1); i++ ) {
348 g_tests[ i ].test_run_it = 0;
349 }
350
351 for ( i = *indexp; i < argc; i++ ) {
352 my_ptr = argv[ i ];
353 if ( strlen( my_ptr ) > 1 && *my_ptr == '-' && isalpha( *(my_ptr + 1) ) ) {
354 /* we have hit a new argument - need to make sure caller uses this argument on the next
355 * pass through its parse loop (which will bump the index value so we want to be one less
356 * than the actual index).
357 */
358 *indexp = (i - 1);
359 return 0;
360 }
361
362 if ( strlen( my_ptr ) == 1 && *my_ptr == '-' ) {
363 /* we are dealing with a range of tests, for example: 33 - 44 */
364 is_range = 1;
365 continue;
366 }
367
368 if ( strlen( my_ptr ) > (sizeof( my_buffer ) - 1) ) {
369 printf( "-run argument has too many test parameters (max of %d characters) \n", sizeof( my_buffer ) );
370 return -1;
371 }
372 /* get a local copy of the parameter string to work with - break range into two strings */
373 strcpy( &my_buffer[0], my_ptr );
374
375 my_temp_ptr = strrchr( &my_buffer[0], '-' );
376 if ( my_temp_ptr != NULL ) {
377 /* we are dealing with a range of tests with no white space, for example: 33-44 or 33- 44 */
378 my_temp_ptr = strrchr( &my_buffer[0], '-' );
379 *my_temp_ptr = 0x00;
380 my_first_test_number = strtol( &my_buffer[0], NULL, 10 );
381 if ( *(my_temp_ptr + 1) == 0x00 ) {
382 /* we hit the case where the range indicator is at the end of one string, for example: 33- */
383 is_range = 1;
384 continue;
385 }
386 my_last_test_number = strtol( (my_temp_ptr + 1), NULL, 10 );
387 if ( my_first_test_number < 1 || my_first_test_number > my_last_test_number ) {
388 printf( "-run argument has invalid range parmeters \n" );
389 return -1;
390 }
391 mark_tests_to_run( my_first_test_number, my_last_test_number );
392 is_range = 0;
393 continue;
394 }
395
396 if ( is_range ) {
397 /* should be the second part of the test number range */
398 my_last_test_number = strtol( &my_buffer[0], NULL, 10 );
399 if ( my_first_test_number < 1 || my_first_test_number > my_last_test_number ) {
400 printf( "-run argument has invalid range parmeters \n" );
401 return -1;
402 }
403
404 mark_tests_to_run( my_first_test_number, my_last_test_number );
405 is_range = 0;
406 continue;
407 }
408 else {
409 my_first_test_number = strtol( &my_buffer[0], NULL, 10 );
410 if ( my_first_test_number < 1 ) {
411 printf( "-run argument has invalid test number parameter \n" );
412 return -1;
413 }
414 mark_tests_to_run( my_first_test_number, my_first_test_number );
415 continue;
416 }
417 }
418
419 *indexp = i;
420 return 0;
421
422 } /* parse_tests_to_run */
423
424
425 static void create_target_directory( const char * the_targetp )
426 {
427 int err;
428
429 if ( strlen( the_targetp ) > (sizeof(g_target_path) - 1) ) {
430 printf( "target path too long - \"%s\" \n", the_targetp );
431 exit( 1 );
432 }
433
434 for ( ;; ) {
435 int my_rand;
436 char my_name[64];
437
438 my_rand = rand( );
439 sprintf( &my_name[0], "xnu_quick_test-%d", my_rand );
440 if ( (strlen( &my_name[0] ) + strlen( the_targetp ) + 2) > PATH_MAX ) {
441 printf( "target path plus our test directory name is too long: \n" );
442 printf( "target path - \"%s\" \n", the_targetp );
443 printf( "test directory name - \"%s\" \n", &my_name[0] );
444 exit( 1 );
445 }
446
447 /* append generated directory name onto our path */
448 g_target_path[0] = 0x00;
449 strcat( &g_target_path[0], the_targetp );
450 if ( g_target_path[ (strlen(the_targetp) - 1) ] != '/' ) {
451 strcat( &g_target_path[0], "/" );
452 }
453 strcat( &g_target_path[0], &my_name[0] );
454
455 /* try to create the test directory */
456 err = mkdir( &g_target_path[0], (S_IRWXU | S_IRWXG | S_IROTH) );
457 if ( err == 0 ) {
458 break;
459 }
460 err = errno;
461 if ( EEXIST != err ) {
462 printf( "test directory creation failed - \"%s\" \n", &g_target_path[0] );
463 printf( "mkdir call failed with error %d - \"%s\" \n", errno, strerror( err) );
464 exit( 1 );
465 }
466 }
467 printf( "created test directory at \"%s\" \n", &g_target_path[0] );
468
469 } /* create_target_directory */
470
471
472 static void list_all_tests( void )
473 {
474 int i, my_tests_count;
475
476 my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] ));
477 printf( "\nList of all tests this tool performs... \n" );
478
479 for ( i = 0; i < (my_tests_count - 1); i++ ) {
480 printf( " %d \t %s \n", (i + 1), g_tests[ i ].test_infop );
481 }
482
483 return;
484 } /* list_all_tests */
485
486
487 static void mark_tests_to_run( long my_start, long my_end )
488 {
489 int my_tests_count, i;
490
491 my_tests_count = (sizeof( g_tests ) / sizeof( g_tests[0] ));
492 my_end = (my_end < (my_tests_count - 1)) ? my_end : (my_tests_count - 1);
493 for ( i = (my_start - 1); i < my_end; i++ ) {
494 g_tests[ i ].test_run_it = 1; /* run this test */
495 }
496 return;
497 } /* mark_tests_to_run */
498
499
500 static void usage( void )
501 {
502 char * my_ptr;
503
504 /* skip past full path and just show the tool name */
505 my_ptr = strrchr( g_cmd_namep, '/' );
506 if ( my_ptr != NULL ) {
507 my_ptr++;
508 }
509
510 printf( "\nUSAGE: %s -target TARGET_PATH \n\n", (my_ptr != NULL) ? my_ptr : g_cmd_namep );
511 printf( "\t -f[ailures] MAX_FAILS_ALLOWED # number of test cases that may fail before we give up. defaults to 0 \n" );
512 printf( "\t -l[ist] # list all the tests this tool performs \n" );
513 printf( "\t -r[un] 1, 3, 10 - 19 # run specific tests. enter individual test numbers and/or range of numbers. use -list to list tests. \n" );
514 printf( "\t -s[kip] # skip setuid tests \n" );
515 printf( "\t -t[arget] TARGET_PATH # path to directory where tool will create test files. defaults to \"/tmp/\" \n" );
516 printf( "\t -x[ilog] # use XILog\n");
517 printf( "\nexamples: \n" );
518 printf( "--- Place all test files and directories at the root of volume \"test_vol\" --- \n" );
519 printf( "%s -t /Volumes/test_vol/ \n", (my_ptr != NULL) ? my_ptr : g_cmd_namep );
520 printf( " \n" );
521 printf( "--- Run the tool for tests 10 thru 15, test 18 and test 20 --- \n" );
522 printf( "%s -r 10-15, 18, 20 \n", (my_ptr != NULL) ? my_ptr : g_cmd_namep );
523 printf( " \n" );
524 exit( 1 );
525
526 } /* usage */
527
Cache object: db7de256f7cd028b7375f6c3d333a10d
|