]> git.saurik.com Git - apple/security.git/blob - Security/regressions/test/testenv.c
Security-57031.1.35.tar.gz
[apple/security.git] / Security / regressions / test / testenv.c
1 /*
2 * Copyright (c) 2005-2007,2009-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 *
23 * testenv.c
24 */
25
26 #include <fcntl.h>
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdbool.h>
34 #include <time.h>
35
36 #include "testmore.h"
37 #include "testenv.h"
38
39 #include <utilities/debugging.h>
40 #include <utilities/SecCFRelease.h>
41 #include <utilities/SecFileLocations.h>
42
43
44 int test_strict_bats = 1;
45 int test_verbose = 0;
46 int test_onebatstest = 0;
47
48 #ifdef NO_SERVER
49 #include <securityd/spi.h>
50
51 static int current_dir = -1;
52 static char scratch_dir[50];
53 static char *home_var;
54 static bool keep_scratch_dir = false;
55
56 static int
57 rmdir_recursive(const char *path)
58 {
59 char command_buf[256];
60 if (strlen(path) + 10 > sizeof(command_buf) || strchr(path, '\''))
61 {
62 fprintf(stdout, "# rmdir_recursive: invalid path: %s", path);
63 return -1;
64 }
65
66 sprintf(command_buf, "/bin/rm -rf '%s'", path);
67 return system(command_buf);
68 }
69 #endif
70
71 static int tests_init(void) {
72 int ok = 0;
73 #ifdef NO_SERVER
74 char preferences_dir[80];
75 char library_dir[70];
76
77 setup("tests_init");
78
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);
83 }
84
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
93 dir. */
94 ok_unix(mkdir(library_dir, 0755), "mkdir") &&
95 ok_unix(mkdir(preferences_dir, 0755), "mkdir") &&
96 ok(home_var = getenv("HOME"), "getenv"));
97
98 if (ok > 0)
99 securityd_init(scratch_dir);
100 #endif
101
102 return ok;
103 }
104
105 static int
106 tests_end(void)
107 {
108 #ifdef NO_SERVER
109 setup("tests_end");
110 /* Restore previous cwd and remove scratch dir. */
111 int ok = ok_unix(fchdir(current_dir), "fchdir");
112 if (ok)
113 ok = ok_unix(close(current_dir), "close");
114 if (ok) {
115 if (!keep_scratch_dir) {
116 ok = ok_unix(rmdir_recursive(scratch_dir), "rmdir_recursive");
117 }
118 }
119
120 return ok;
121 #else
122 return 0;
123 #endif
124 }
125
126 static void usage(const char *progname)
127 {
128 fprintf(stderr, "usage: %s [-k][-w][testname [testargs] ...]\n", progname);
129 exit(1);
130 }
131
132 static int tests_run_index(int i, int argc, char * const *argv)
133 {
134 int ch;
135
136 while ((ch = getopt(argc, argv, "v")) != -1)
137 {
138 switch (ch)
139 {
140 case 'v':
141 test_verbose++;
142 break;
143 default:
144 usage(argv[0]);
145 }
146 }
147
148 if (test_onebatstest)
149 fprintf(stdout, "[TEST] %s\n", testlist[i].name);
150 else
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);
155 } else {
156 fprintf(stdout, "[PASS] %s\n", testlist[i].name);
157 if (test_verbose)
158 fprintf(stdout, "(%lu ms)\n", testlist[i].duration);
159 }
160 return testlist[i].failed_tests;
161 }
162
163 static int strcmp_under_is_dash(const char *s, const char *t) {
164 for (;;) {
165 char a = *s++, b = *t++;
166 if (a != b) {
167 if (a != '_' || b != '-')
168 return a - b;
169 } else if (a == 0) {
170 return 0;
171 }
172 }
173 }
174
175 static int tests_named_index(const char *testcase)
176 {
177 int i;
178
179 for (i = 0; testlist[i].name; ++i) {
180 if (strcmp_under_is_dash(testlist[i].name, testcase) == 0) {
181 return i;
182 }
183 }
184
185 return -1;
186 }
187
188 static int tests_run_all(int argc, char * const *argv)
189 {
190 int curroptind = optind;
191 int i;
192 int failcount=0;
193
194 for (i = 0; testlist[i].name; ++i) {
195 if(!testlist[i].off) {
196 failcount+=tests_run_index(i, argc, argv);
197 optind = curroptind;
198 }
199 }
200
201 return failcount;
202 }
203
204 int
205 tests_begin(int argc, char * const *argv)
206 {
207 const char *testcase = NULL;
208 bool initialized = false;
209 bool print_security_logs = false;
210 int testix = -1;
211 int failcount = 0;
212 int ch;
213 int loop = 0;
214 int list = 0;
215
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];
219
220 for (;;) {
221 while (!testcase && (ch = getopt(argc, argv, "bklL1vwqs")) != -1)
222 {
223 switch (ch)
224 {
225 #ifdef NO_SERVER
226 case 'k':
227 keep_scratch_dir = true;
228 break;
229 #endif
230 case 's':
231 print_security_logs = true;
232 break;
233
234 case 'b':
235 test_strict_bats = 0;
236 break;
237
238 case 'v':
239 test_verbose++;
240 break;
241
242 case 'w':
243 sleep(100);
244 break;
245 case 'l':
246 loop=1;
247 break;
248 case 'L':
249 list=1;
250 break;
251 case '1':
252 test_onebatstest=1;
253 break;
254 case '?':
255 default:
256 printf("invalid option %c\n",ch);
257 usage(argv[0]);
258 }
259 }
260
261 if (optind < argc) {
262 testix = tests_named_index(argv[optind]);
263 if(testix<0) {
264 printf("invalid test %s\n",argv[optind]);
265 usage(argv[0]);
266 }
267 }
268
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);
273 date[19] = '\0';
274 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
275 CFSTR("%s %@ %s %@\n"), date + 4,
276 scope ? scope : CFSTR(""), function, message);
277 CFShow(logStr);
278 CFReleaseSafe(logStr);
279 });
280 }
281
282 if (!list && !initialized && !test_onebatstest)
283 fprintf(stdout, "[TEST] %s\n", progname);
284 if (testix < 0) {
285 if (!initialized) {
286 tests_init();
287 initialized = true;
288 (void)initialized;
289 if (!list)
290 failcount+=tests_run_all(argc, argv);
291 }
292 break;
293 } else {
294 if (!initialized) {
295 tests_init();
296 initialized = true;
297 for (int i = 0; testlist[i].name; ++i) {
298 testlist[i].off = 1;
299 }
300 }
301 optind++;
302 testlist[testix].off = 0;
303 if (!list)
304 failcount+=tests_run_index(testix, argc, argv);
305 testix = -1;
306 }
307 }
308 if (!list) {
309 if (!test_onebatstest) {
310 fprintf(stdout, "[%s] %s\n", failcount ? "FAIL" : "PASS", progname);
311 }
312 fprintf(stdout, "[SUMMARY]\n");
313 }
314 int sub_tests = 0;
315 if (failcount) {
316 fprintf(stdout, "Test name failed total\n");
317 fprintf(stdout, "================================================================\n");
318 }
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);
324 } else if (list) {
325 fprintf(stdout, "%s\n", testlist[i].name);
326 }
327 }
328 }
329 if (failcount)
330 fprintf(stdout, "================================================================\n");
331 if (!list)
332 fprintf(stdout, "Total failcount: %6d %6d\n", failcount, sub_tests);
333 fflush(stdout);
334
335 /* Cleanups */
336 tests_end();
337
338 if (loop) {
339 printf("Looping until key press 'q'. You can run leaks now.\n");
340 while(getchar()!='q');
341 }
342
343 return failcount;
344 }
345