2 * Copyright (c) 2005-2007,2009-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * This is to fool os services to not provide the Keychain manager
28 * interface tht doens't work since we don't have unified headers
29 * between iOS and OS X. rdar://23405418/
31 #define __KEYCHAINCORE__ 1
34 #import <Foundation/Foundation.h>
40 #include <sys/types.h>
49 #include <utilities/debugging.h>
50 #include <utilities/SecCFRelease.h>
51 #include <utilities/SecCFWrappers.h>
52 #include <utilities/SecFileLocations.h>
54 #include <dispatch/dispatch.h>
57 int test_strict_bats = 1;
59 int test_onebatstest = 0;
60 int test_check_leaks = 0;
61 char **test_skip_leaks_test = NULL;
64 #include <securityd/spi.h>
66 static int current_dir = -1;
67 static char scratch_dir[50];
68 static char *home_var;
69 static bool keep_scratch_dir = false;
72 rmdir_recursive(const char *path)
74 #if (!TARGET_IPHONE_SIMULATOR)
75 char command_buf[256];
76 if (strlen(path) + 10 > sizeof(command_buf) || strchr(path, '\''))
78 fprintf(stdout, "# rmdir_recursive: invalid path: %s", path);
82 sprintf(command_buf, "/bin/rm -rf '%s'", path);
83 return system(command_buf);
85 fprintf(stdout, "# rmdir_recursive: simulator can't rmdir, leaving path: %s\n", path);
91 static int tests_init(void) {
94 char preferences_dir[80];
99 /* Create scratch dir for tests to run in. */
100 sprintf(scratch_dir, "/tmp/tst-%d", getpid());
101 if (keep_scratch_dir) {
102 printf("running tests with HOME=%s\n", scratch_dir);
105 sprintf(library_dir, "%s/Library", scratch_dir);
106 sprintf(preferences_dir, "%s/Preferences", library_dir);
107 ok = (ok_unix(mkdir(scratch_dir, 0755), "mkdir") &&
108 ok_unix(current_dir = open(".", O_RDONLY), "open") &&
109 ok_unix(chdir(scratch_dir), "chdir") &&
110 ok_unix(setenv("HOME", scratch_dir, 1), "setenv") &&
111 /* @@@ Work around a bug that the prefs code in
112 libsecurity_keychain never creates the Library/Preferences
114 ok_unix(mkdir(library_dir, 0755), "mkdir") &&
115 ok_unix(mkdir(preferences_dir, 0755), "mkdir") &&
116 ok(home_var = getenv("HOME"), "getenv"));
119 securityd_init(scratch_dir);
130 /* Restore previous cwd and remove scratch dir. */
131 int ok = ok_unix(fchdir(current_dir), "fchdir");
133 ok = ok_unix(close(current_dir), "close");
135 if (!keep_scratch_dir) {
136 ok = ok_unix(rmdir_recursive(scratch_dir), "rmdir_recursive");
146 static void usage(const char *progname)
148 fprintf(stderr, "usage: %s [-k][-w][testname [testargs] ...]\n", progname);
152 /* run one test, described by test, return info in test struct */
153 static int tests_run_test(struct one_test_s *test, int argc, char * const *argv)
157 while ((ch = getopt(argc, argv, "v")) != -1)
169 if (test_onebatstest)
170 fprintf(stdout, "[TEST] %s\n", test->name);
172 fprintf(stdout, "[BEGIN] %s\n", test->name);
174 const char *token = "PASS";
175 if (test->entry == NULL) {
176 fprintf(stdout, "%s:%d: error, no entry\n", __FILE__, __LINE__);
177 test->failed_tests = 1;
179 struct timeval start, stop;
180 gettimeofday(&start, NULL);
182 test->entry(argc, argv);
184 gettimeofday(&stop, NULL);
185 /* this may overflow... */
186 test->duration = (stop.tv_sec-start.tv_sec) * 1000 + (stop.tv_usec / 1000) - (start.tv_usec / 1000);
187 if (test_plan_ok()) {
193 * Check if we have any leaks, allow skipping test
196 if (test_check_leaks) {
200 asprintf(&cmd, "leaks %d >/dev/null", getpid());
207 fprintf(stdout, "leaks found in test %s\n", test->name);
209 if (test_skip_leaks_test) {
210 while (test_skip_leaks_test[n]) {
211 if (strcmp(test_skip_leaks_test[n], test->name) == 0) {
212 fprintf(stdout, "test %s known to be leaky, skipping\n", test->name);
222 if (test_skip_leaks_test) {
225 while (test_skip_leaks_test[n]) {
226 if (strcmp(test_skip_leaks_test[n], test->name) == 0) {
227 fprintf(stdout, "leaks didn't find leak in test %s, yet it was ignore\n", test->name);
237 test_plan_final(&test->failed_tests, &test->todo_pass_tests, &test->todo_tests,
238 &test->actual_tests, &test->planned_tests,
239 &test->plan_file, &test->plan_line);
241 // TODO Use ccperf timing and printing routines that use proper si scaling
242 fprintf(stdout, "%s took %lu ms\n", test->name, test->duration);
244 if (test->failed_tests) {
247 fprintf(stdout, "[%s] %s\n", token, test->name);
249 return test->failed_tests;
252 static int strcmp_under_is_dash(const char *s, const char *t) {
254 char a = *s++, b = *t++;
256 if (a != '_' || b != '-')
264 static int tests_named_index(const char *testcase)
268 for (i = 0; testlist[i].name; ++i) {
269 if (strcmp_under_is_dash(testlist[i].name, testcase) == 0) {
277 static int tests_run_all(int argc, char * const *argv)
279 int curroptind = optind;
283 for (i = 0; testlist[i].name; ++i) {
284 if(!testlist[i].off) {
286 failcount+=tests_run_test(&testlist[i], argc, argv);
296 tests_summary(const char *progname) {
297 int failed_tests = 0;
298 int todo_pass_tests = 0;
300 int actual_tests = 0;
301 int planned_tests = 0;
303 // First compute the totals to help decide if we need to print headers or not.
304 for (int i = 0; testlist[i].name; ++i) {
305 if (!testlist[i].off) {
306 failed_tests += testlist[i].failed_tests;
307 todo_pass_tests += testlist[i].todo_pass_tests;
308 todo_tests += testlist[i].todo_tests;
309 actual_tests += testlist[i].actual_tests;
310 planned_tests += testlist[i].planned_tests;
314 if (!test_onebatstest) {
315 fprintf(stdout, "[%s] %s\n", failed_tests ? "FAIL" : (actual_tests == planned_tests ? "PASS" : "WARN"), progname);
318 fprintf(stdout, "[SUMMARY]\n");
320 // -v -v makes the summary verbose as well.
321 fprintf(stdout, "Test name failed !failed todo total plan\n");
322 if (test_verbose > 1 || failed_tests || todo_pass_tests || actual_tests != planned_tests || (test_verbose && todo_tests)) {
323 fprintf(stdout, "============================================================================================\n");
325 for (int i = 0; testlist[i].name; ++i) {
326 if (!testlist[i].off) {
327 const char *token = NULL;
328 if (testlist[i].failed_tests) {
330 } else if (testlist[i].actual_tests != testlist[i].planned_tests) {
332 } else if (testlist[i].todo_pass_tests) {
334 } else if (test_verbose > 1 || (test_verbose && testlist[i].todo_tests)) {
338 fprintf(stdout, "[%s] %-50s %6d %6d %6d %6d %6d\n", token, testlist[i].name, testlist[i].failed_tests, testlist[i].todo_pass_tests, testlist[i].todo_tests, testlist[i].actual_tests, testlist[i].planned_tests);
342 if (test_verbose > 1 || failed_tests || todo_pass_tests || actual_tests != planned_tests || (test_verbose && todo_tests)) {
343 fprintf(stdout, "============================================================================================\n");
345 fprintf(stdout, "Totals %6d %6d %6d %6d %6d\n", failed_tests, todo_pass_tests, todo_tests, actual_tests, planned_tests);
349 #define ASYNC_LOGGING 0
350 #define DATE_LOGGING 0
353 tests_begin(int argc, char * const *argv) {
354 const char *testcase = NULL;
355 bool initialized = false;
356 __block bool print_security_logs = false;
364 dispatch_queue_t show_queue = dispatch_queue_create("sec log queue", DISPATCH_QUEUE_SERIAL);
369 security_log_handler handle_logs = ^(int level, CFStringRef scope, const char *function, const char *file, int line, CFStringRef message) {
370 time_t now = time(NULL);
373 char *date = ctime(&now);
375 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
376 CFSTR("%s %@{} %s %@\n"), date + 4,
377 scope ? scope : CFSTR(""), function, message);
379 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
380 CFSTR("%lu %@{} %s %@\n"), now,
381 scope ? scope : CFSTR(""), function, message);
384 dispatch_async(show_queue, ^{ CFShow(logStr); CFReleaseSafe(logStr); });
387 CFReleaseSafe(logStr);
394 while (!testcase && (ch = getopt(argc, argv, "bklL1vwqs")) != -1)
400 keep_scratch_dir = true;
404 if (!print_security_logs) {
405 //add_security_log_handler(handle_logs);
406 print_security_logs = true;
411 test_strict_bats = 0;
432 printf("invalid option %c\n",ch);
438 testix = tests_named_index(argv[optind]);
440 printf("invalid test %s\n",argv[optind]);
445 if(!print_security_logs) secLogDisable();
448 if (!list && !initialized && !test_onebatstest)
449 fprintf(stdout, "[TEST] %s\n", getprogname());
456 failcount+=tests_run_all(argc, argv);
463 for (int i = 0; testlist[i].name; ++i) {
468 testlist[testix].off = 0;
471 failcount+=tests_run_test(&testlist[testix], argc, argv);
478 for (int i = 0; testlist[i].name; ++i) {
479 if (!testlist[i].off) {
480 fprintf(stdout, "%s\n", testlist[i].name);
484 tests_summary(getprogname());
487 // remove_security_log_handler(handle_logs);
495 printf("Looping until key press 'q'. You can run leaks now.\n");
496 while(getchar()!='q');