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;
62 #include <securityd/spi.h>
64 static int current_dir = -1;
65 static char scratch_dir[50];
66 static char *home_var;
67 static bool keep_scratch_dir = false;
70 rmdir_recursive(const char *path)
72 #if (!TARGET_IPHONE_SIMULATOR)
73 char command_buf[256];
74 if (strlen(path) + 10 > sizeof(command_buf) || strchr(path, '\''))
76 fprintf(stdout, "# rmdir_recursive: invalid path: %s", path);
80 sprintf(command_buf, "/bin/rm -rf '%s'", path);
81 return system(command_buf);
83 fprintf(stdout, "# rmdir_recursive: simulator can't rmdir, leaving path: %s\n", path);
89 static int tests_init(void) {
92 char preferences_dir[80];
97 /* Create scratch dir for tests to run in. */
98 sprintf(scratch_dir, "/tmp/tst-%d", getpid());
99 if (keep_scratch_dir) {
100 printf("running tests with HOME=%s\n", scratch_dir);
103 sprintf(library_dir, "%s/Library", scratch_dir);
104 sprintf(preferences_dir, "%s/Preferences", library_dir);
105 ok = (ok_unix(mkdir(scratch_dir, 0755), "mkdir") &&
106 ok_unix(current_dir = open(".", O_RDONLY), "open") &&
107 ok_unix(chdir(scratch_dir), "chdir") &&
108 ok_unix(setenv("HOME", scratch_dir, 1), "setenv") &&
109 /* @@@ Work around a bug that the prefs code in
110 libsecurity_keychain never creates the Library/Preferences
112 ok_unix(mkdir(library_dir, 0755), "mkdir") &&
113 ok_unix(mkdir(preferences_dir, 0755), "mkdir") &&
114 ok(home_var = getenv("HOME"), "getenv"));
117 securityd_init(scratch_dir);
128 /* Restore previous cwd and remove scratch dir. */
129 int ok = ok_unix(fchdir(current_dir), "fchdir");
131 ok = ok_unix(close(current_dir), "close");
133 if (!keep_scratch_dir) {
134 ok = ok_unix(rmdir_recursive(scratch_dir), "rmdir_recursive");
144 static void usage(const char *progname)
146 fprintf(stderr, "usage: %s [-k][-w][testname [testargs] ...]\n", progname);
150 /* run one test, described by test, return info in test struct */
151 static int tests_run_test(struct one_test_s *test, int argc, char * const *argv)
155 while ((ch = getopt(argc, argv, "v")) != -1)
167 if (test_onebatstest)
168 fprintf(stdout, "[TEST] %s\n", test->name);
170 fprintf(stdout, "[BEGIN] %s\n", test->name);
172 const char *token = "PASS";
173 if (test->entry == NULL) {
174 fprintf(stdout, "%s:%d: error, no entry\n", __FILE__, __LINE__);
175 test->failed_tests = 1;
177 struct timeval start, stop;
178 gettimeofday(&start, NULL);
179 test->entry(argc, argv);
180 gettimeofday(&stop, NULL);
181 /* this may overflow... */
182 test->duration = (stop.tv_sec-start.tv_sec) * 1000 + (stop.tv_usec / 1000) - (start.tv_usec / 1000);
183 if (test_plan_ok()) {
188 test_plan_final(&test->failed_tests, &test->todo_pass_tests, &test->todo_tests,
189 &test->actual_tests, &test->planned_tests,
190 &test->plan_file, &test->plan_line);
192 // TODO Use ccperf timing and printing routines that use proper si scaling
193 fprintf(stdout, "%s took %lu ms\n", test->name, test->duration);
195 if (test->failed_tests) {
198 fprintf(stdout, "[%s] %s\n", token, test->name);
200 return test->failed_tests;
203 static int strcmp_under_is_dash(const char *s, const char *t) {
205 char a = *s++, b = *t++;
207 if (a != '_' || b != '-')
215 static int tests_named_index(const char *testcase)
219 for (i = 0; testlist[i].name; ++i) {
220 if (strcmp_under_is_dash(testlist[i].name, testcase) == 0) {
228 static int tests_run_all(int argc, char * const *argv)
230 int curroptind = optind;
234 for (i = 0; testlist[i].name; ++i) {
235 if(!testlist[i].off) {
237 failcount+=tests_run_test(&testlist[i], argc, argv);
247 tests_summary(const char *progname) {
248 int failed_tests = 0;
249 int todo_pass_tests = 0;
251 int actual_tests = 0;
252 int planned_tests = 0;
254 // First compute the totals to help decide if we need to print headers or not.
255 for (int i = 0; testlist[i].name; ++i) {
256 if (!testlist[i].off) {
257 failed_tests += testlist[i].failed_tests;
258 todo_pass_tests += testlist[i].todo_pass_tests;
259 todo_tests += testlist[i].todo_tests;
260 actual_tests += testlist[i].actual_tests;
261 planned_tests += testlist[i].planned_tests;
265 if (!test_onebatstest) {
266 fprintf(stdout, "[%s] %s\n", failed_tests ? "FAIL" : (actual_tests == planned_tests ? "PASS" : "WARN"), progname);
269 fprintf(stdout, "[SUMMARY]\n");
271 // -v -v makes the summary verbose as well.
272 fprintf(stdout, "Test name failed !failed todo total plan\n");
273 if (test_verbose > 1 || failed_tests || todo_pass_tests || actual_tests != planned_tests || (test_verbose && todo_tests)) {
274 fprintf(stdout, "============================================================================================\n");
276 for (int i = 0; testlist[i].name; ++i) {
277 if (!testlist[i].off) {
278 const char *token = NULL;
279 if (testlist[i].failed_tests) {
281 } else if (testlist[i].actual_tests != testlist[i].planned_tests) {
283 } else if (testlist[i].todo_pass_tests) {
285 } else if (test_verbose > 1 || (test_verbose && testlist[i].todo_tests)) {
289 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);
293 if (test_verbose > 1 || failed_tests || todo_pass_tests || actual_tests != planned_tests || (test_verbose && todo_tests)) {
294 fprintf(stdout, "============================================================================================\n");
296 fprintf(stdout, "Totals %6d %6d %6d %6d %6d\n", failed_tests, todo_pass_tests, todo_tests, actual_tests, planned_tests);
300 #define ASYNC_LOGGING 0
301 #define DATE_LOGGING 0
304 tests_begin(int argc, char * const *argv) {
305 const char *testcase = NULL;
306 bool initialized = false;
307 __block bool print_security_logs = false;
315 dispatch_queue_t show_queue = dispatch_queue_create("sec log queue", DISPATCH_QUEUE_SERIAL);
318 security_log_handler handle_logs = ^(int level, CFStringRef scope, const char *function, const char *file, int line, CFStringRef message) {
319 time_t now = time(NULL);
321 char *date = ctime(&now);
323 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
324 CFSTR("%s %@{} %s %@\n"), date + 4,
325 scope ? scope : CFSTR(""), function, message);
327 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
328 CFSTR("%lu %@{} %s %@\n"), now,
329 scope ? scope : CFSTR(""), function, message);
332 dispatch_async(show_queue, ^{ CFShow(logStr); CFReleaseSafe(logStr); });
335 CFReleaseSafe(logStr);
340 while (!testcase && (ch = getopt(argc, argv, "bklL1vwqs")) != -1)
346 keep_scratch_dir = true;
350 if (!print_security_logs) {
351 add_security_log_handler(handle_logs);
352 print_security_logs = true;
357 test_strict_bats = 0;
378 printf("invalid option %c\n",ch);
384 testix = tests_named_index(argv[optind]);
386 printf("invalid test %s\n",argv[optind]);
391 if (!list && !initialized && !test_onebatstest)
392 fprintf(stdout, "[TEST] %s\n", getprogname());
399 failcount+=tests_run_all(argc, argv);
406 for (int i = 0; testlist[i].name; ++i) {
411 testlist[testix].off = 0;
414 failcount+=tests_run_test(&testlist[testix], argc, argv);
421 for (int i = 0; testlist[i].name; ++i) {
422 if (!testlist[i].off) {
423 fprintf(stdout, "%s\n", testlist[i].name);
427 tests_summary(getprogname());
430 remove_security_log_handler(handle_logs);
438 printf("Looping until key press 'q'. You can run leaks now.\n");
439 while(getchar()!='q');