]>
git.saurik.com Git - apple/security.git/blob - Security/regressions/test/testenv.c
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@
31 #include <sys/types.h>
39 #include <utilities/debugging.h>
40 #include <utilities/SecCFRelease.h>
41 #include <utilities/SecFileLocations.h>
44 int test_strict_bats
= 1;
46 int test_onebatstest
= 0;
49 #include <securityd/spi.h>
51 static int current_dir
= -1;
52 static char scratch_dir
[50];
53 static char *home_var
;
54 static bool keep_scratch_dir
= false;
57 rmdir_recursive(const char *path
)
59 char command_buf
[256];
60 if (strlen(path
) + 10 > sizeof(command_buf
) || strchr(path
, '\''))
62 fprintf(stdout
, "# rmdir_recursive: invalid path: %s", path
);
66 sprintf(command_buf
, "/bin/rm -rf '%s'", path
);
67 return system(command_buf
);
71 static int tests_init(void) {
74 char preferences_dir
[80];
79 /* Create scratch dir for tests to run in. */
80 sprintf(scratch_dir
, "/tmp/tst-%d", getpid());
81 if (keep_scratch_dir
) {
82 printf("running tests with HOME=%s\n", scratch_dir
);
85 sprintf(library_dir
, "%s/Library", scratch_dir
);
86 sprintf(preferences_dir
, "%s/Preferences", library_dir
);
87 ok
= (ok_unix(mkdir(scratch_dir
, 0755), "mkdir") &&
88 ok_unix(current_dir
= open(".", O_RDONLY
), "open") &&
89 ok_unix(chdir(scratch_dir
), "chdir") &&
90 ok_unix(setenv("HOME", scratch_dir
, 1), "setenv") &&
91 /* @@@ Work around a bug that the prefs code in
92 libsecurity_keychain never creates the Library/Preferences
94 ok_unix(mkdir(library_dir
, 0755), "mkdir") &&
95 ok_unix(mkdir(preferences_dir
, 0755), "mkdir") &&
96 ok(home_var
= getenv("HOME"), "getenv"));
99 securityd_init(scratch_dir
);
110 /* Restore previous cwd and remove scratch dir. */
111 int ok
= ok_unix(fchdir(current_dir
), "fchdir");
113 ok
= ok_unix(close(current_dir
), "close");
115 if (!keep_scratch_dir
) {
116 ok
= ok_unix(rmdir_recursive(scratch_dir
), "rmdir_recursive");
126 static void usage(const char *progname
)
128 fprintf(stderr
, "usage: %s [-k][-w][testname [testargs] ...]\n", progname
);
132 static int tests_run_index(int i
, int argc
, char * const *argv
)
136 while ((ch
= getopt(argc
, argv
, "v")) != -1)
148 if (test_onebatstest
)
149 fprintf(stdout
, "[TEST] %s\n", testlist
[i
].name
);
151 fprintf(stdout
, "[BEGIN] %s\n", testlist
[i
].name
);
152 run_one_test(&testlist
[i
], argc
, argv
);
153 if(testlist
[i
].failed_tests
) {
154 fprintf(stdout
, "[FAIL] %s\n", testlist
[i
].name
);
156 fprintf(stdout
, "[PASS] %s\n", testlist
[i
].name
);
158 fprintf(stdout
, "(%lu ms)\n", testlist
[i
].duration
);
160 return testlist
[i
].failed_tests
;
163 static int strcmp_under_is_dash(const char *s
, const char *t
) {
165 char a
= *s
++, b
= *t
++;
167 if (a
!= '_' || b
!= '-')
175 static int tests_named_index(const char *testcase
)
179 for (i
= 0; testlist
[i
].name
; ++i
) {
180 if (strcmp_under_is_dash(testlist
[i
].name
, testcase
) == 0) {
188 static int tests_run_all(int argc
, char * const *argv
)
190 int curroptind
= optind
;
194 for (i
= 0; testlist
[i
].name
; ++i
) {
195 if(!testlist
[i
].off
) {
196 failcount
+=tests_run_index(i
, argc
, argv
);
205 tests_begin(int argc
, char * const *argv
)
207 const char *testcase
= NULL
;
208 bool initialized
= false;
209 bool print_security_logs
= false;
216 // TODO Currently our callers do this, but we can move this here together with the build date info.
217 const char *progname
= strrchr(argv
[0], '/');
218 progname
= progname
? progname
+ 1 : argv
[0];
221 while (!testcase
&& (ch
= getopt(argc
, argv
, "bklL1vwqs")) != -1)
227 keep_scratch_dir
= true;
231 print_security_logs
= true;
235 test_strict_bats
= 0;
256 printf("invalid option %c\n",ch
);
262 testix
= tests_named_index(argv
[optind
]);
264 printf("invalid test %s\n",argv
[optind
]);
269 if (print_security_logs
) {
270 add_security_log_handler(^(int level
, CFStringRef scope
, const char *function
, const char *file
, int line
, CFStringRef message
) {
271 time_t now
= time(NULL
);
272 char *date
= ctime(&now
);
274 CFStringRef logStr
= CFStringCreateWithFormat(kCFAllocatorDefault
, NULL
,
275 CFSTR("%s %@ %s %@\n"), date
+ 4,
276 scope
? scope
: CFSTR(""), function
, message
);
278 CFReleaseSafe(logStr
);
282 if (!list
&& !initialized
&& !test_onebatstest
)
283 fprintf(stdout
, "[TEST] %s\n", progname
);
290 failcount
+=tests_run_all(argc
, argv
);
297 for (int i
= 0; testlist
[i
].name
; ++i
) {
302 testlist
[testix
].off
= 0;
304 failcount
+=tests_run_index(testix
, argc
, argv
);
309 if (!test_onebatstest
) {
310 fprintf(stdout
, "[%s] %s\n", failcount
? "FAIL" : "PASS", progname
);
312 fprintf(stdout
, "[SUMMARY]\n");
316 fprintf(stdout
, "Test name failed total\n");
317 fprintf(stdout
, "================================================================\n");
319 for (int i
= 0; testlist
[i
].name
; ++i
) {
320 if (!testlist
[i
].off
) {
321 sub_tests
+= testlist
[i
].sub_tests
;
322 if (testlist
[i
].failed_tests
) {
323 fprintf(stdout
, "%-50s %6d %6d\n", testlist
[i
].name
, testlist
[i
].failed_tests
, testlist
[i
].sub_tests
);
325 fprintf(stdout
, "%s\n", testlist
[i
].name
);
330 fprintf(stdout
, "================================================================\n");
332 fprintf(stdout
, "Total failcount: %6d %6d\n", failcount
, sub_tests
);
339 printf("Looping until key press 'q'. You can run leaks now.\n");
340 while(getchar()!='q');