]>
git.saurik.com Git - apple/security.git/blob - regressions/test/testenv.c
2 * Copyright (c) 2005-2007,2009-2011 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@
31 #include <sys/types.h>
39 #include <utilities/debugging.h>
40 #include <utilities/SecCFRelease.h>
41 #include <utilities/SecFileLocations.h>
47 #include <securityd/spi.h>
49 static int current_dir
= -1;
50 static char scratch_dir
[50];
51 static char *home_var
;
52 static bool keep_scratch_dir
= false;
55 rmdir_recursive(const char *path
)
57 char command_buf
[256];
58 if (strlen(path
) + 10 > sizeof(command_buf
) || strchr(path
, '\''))
60 fprintf(stderr
, "# rmdir_recursive: invalid path: %s", path
);
64 sprintf(command_buf
, "/bin/rm -rf '%s'", path
);
65 return system(command_buf
);
69 static int tests_init(void) {
72 char preferences_dir
[80];
77 /* Create scratch dir for tests to run in. */
78 sprintf(scratch_dir
, "/tmp/tst-%d", getpid());
79 if (keep_scratch_dir
) {
80 printf("running tests with HOME=%s\n", scratch_dir
);
83 sprintf(library_dir
, "%s/Library", scratch_dir
);
84 sprintf(preferences_dir
, "%s/Preferences", library_dir
);
85 ok
= (ok_unix(mkdir(scratch_dir
, 0755), "mkdir") &&
86 ok_unix(current_dir
= open(".", O_RDONLY
), "open") &&
87 ok_unix(chdir(scratch_dir
), "chdir") &&
88 ok_unix(setenv("HOME", scratch_dir
, 1), "setenv") &&
89 /* @@@ Work around a bug that the prefs code in
90 libsecurity_keychain never creates the Library/Preferences
92 ok_unix(mkdir(library_dir
, 0755), "mkdir") &&
93 ok_unix(mkdir(preferences_dir
, 0755), "mkdir") &&
94 ok(home_var
= getenv("HOME"), "getenv"));
97 securityd_init(scratch_dir
);
108 /* Restore previous cwd and remove scratch dir. */
109 int ok
= ok_unix(fchdir(current_dir
), "fchdir");
111 ok
= ok_unix(close(current_dir
), "close");
113 if (!keep_scratch_dir
) {
114 ok
= ok_unix(rmdir_recursive(scratch_dir
), "rmdir_recursive");
124 static void usage(const char *progname
)
126 fprintf(stderr
, "usage: %s [-k][-w][testname [testargs] ...]\n", progname
);
130 static int tests_run_index(int i
, int argc
, char * const *argv
)
134 while ((ch
= getopt(argc
, argv
, "v")) != -1)
146 fprintf(stderr
, "TEST: Test Case '%s' started.\n", testlist
[i
].name
);
148 run_one_test(&testlist
[i
], argc
, argv
);
149 if(testlist
[i
].failed_tests
) {
150 fprintf(stderr
, "FAIL: Test Case '%s' failed.\n", testlist
[i
].name
);
152 fprintf(stderr
, "PASS: Test Case '%s' passed. (%lu ms)\n", testlist
[i
].name
, testlist
[i
].duration
);
154 return testlist
[i
].failed_tests
;
157 static int strcmp_under_is_dash(const char *s
, const char *t
) {
159 char a
= *s
++, b
= *t
++;
161 if (a
!= '_' || b
!= '-')
169 static int tests_named_index(const char *testcase
)
173 for (i
= 0; testlist
[i
].name
; ++i
) {
174 if (strcmp_under_is_dash(testlist
[i
].name
, testcase
) == 0) {
182 static int tests_run_all(int argc
, char * const *argv
)
184 int curroptind
= optind
;
188 for (i
= 0; testlist
[i
].name
; ++i
) {
189 if(!testlist
[i
].off
) {
190 failcount
+=tests_run_index(i
, argc
, argv
);
199 tests_begin(int argc
, char * const *argv
)
201 const char *testcase
= NULL
;
202 bool initialized
= false;
203 bool print_security_logs
= false;
210 while (!testcase
&& (ch
= getopt(argc
, argv
, "klvws")) != -1)
216 keep_scratch_dir
= true;
220 print_security_logs
= true;
235 printf("invalid option %c\n",ch
);
241 testix
= tests_named_index(argv
[optind
]);
243 printf("invalid test %s\n",argv
[optind
]);
248 if (print_security_logs
) {
249 add_security_log_hanlder(^(const char *level
, CFStringRef scope
, const char *function
, const char *file
, int line
, CFStringRef message
) {
250 time_t now
= time(NULL
);
251 char *date
= ctime(&now
);
253 CFStringRef logStr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
254 CFSTR("%s %@ %s %@\n"), date
+ 4,
255 scope
? scope
: CFSTR(""), function
, message
);
257 CFReleaseSafe(logStr
);
265 failcount
+=tests_run_all(argc
, argv
);
274 failcount
+=tests_run_index(testix
, argc
, argv
);
279 printf("Total failcount = %d\n", failcount
);
285 printf("Looping until key press 'q'. You can run leaks now.\n");
286 while(getchar()!='q');