]> git.saurik.com Git - apple/security.git/blob - regressions/test/testenv.c
Security-55471.14.tar.gz
[apple/security.git] / regressions / test / testenv.c
1 /*
2 * Copyright (c) 2005-2007,2009-2011 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_verbose = 0;
45
46 #ifdef NO_SERVER
47 #include <securityd/spi.h>
48
49 static int current_dir = -1;
50 static char scratch_dir[50];
51 static char *home_var;
52 static bool keep_scratch_dir = false;
53
54 static int
55 rmdir_recursive(const char *path)
56 {
57 char command_buf[256];
58 if (strlen(path) + 10 > sizeof(command_buf) || strchr(path, '\''))
59 {
60 fprintf(stderr, "# rmdir_recursive: invalid path: %s", path);
61 return -1;
62 }
63
64 sprintf(command_buf, "/bin/rm -rf '%s'", path);
65 return system(command_buf);
66 }
67 #endif
68
69 static int tests_init(void) {
70 int ok = 0;
71 #ifdef NO_SERVER
72 char preferences_dir[80];
73 char library_dir[70];
74
75 setup("tests_init");
76
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);
81 }
82
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
91 dir. */
92 ok_unix(mkdir(library_dir, 0755), "mkdir") &&
93 ok_unix(mkdir(preferences_dir, 0755), "mkdir") &&
94 ok(home_var = getenv("HOME"), "getenv"));
95
96 if (ok > 0)
97 securityd_init(scratch_dir);
98 #endif
99
100 return ok;
101 }
102
103 static int
104 tests_end(void)
105 {
106 #ifdef NO_SERVER
107 setup("tests_end");
108 /* Restore previous cwd and remove scratch dir. */
109 int ok = ok_unix(fchdir(current_dir), "fchdir");
110 if (ok)
111 ok = ok_unix(close(current_dir), "close");
112 if (ok) {
113 if (!keep_scratch_dir) {
114 ok = ok_unix(rmdir_recursive(scratch_dir), "rmdir_recursive");
115 }
116 }
117
118 return ok;
119 #else
120 return 0;
121 #endif
122 }
123
124 static void usage(const char *progname)
125 {
126 fprintf(stderr, "usage: %s [-k][-w][testname [testargs] ...]\n", progname);
127 exit(1);
128 }
129
130 static int tests_run_index(int i, int argc, char * const *argv)
131 {
132 int ch;
133
134 while ((ch = getopt(argc, argv, "v")) != -1)
135 {
136 switch (ch)
137 {
138 case 'v':
139 test_verbose++;
140 break;
141 default:
142 usage(argv[0]);
143 }
144 }
145
146 fprintf(stderr, "TEST: Test Case '%s' started.\n", testlist[i].name);
147
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);
151 } else {
152 fprintf(stderr, "PASS: Test Case '%s' passed. (%lu ms)\n", testlist[i].name, testlist[i].duration);
153 }
154 return testlist[i].failed_tests;
155 }
156
157 static int strcmp_under_is_dash(const char *s, const char *t) {
158 for (;;) {
159 char a = *s++, b = *t++;
160 if (a != b) {
161 if (a != '_' || b != '-')
162 return a - b;
163 } else if (a == 0) {
164 return 0;
165 }
166 }
167 }
168
169 static int tests_named_index(const char *testcase)
170 {
171 int i;
172
173 for (i = 0; testlist[i].name; ++i) {
174 if (strcmp_under_is_dash(testlist[i].name, testcase) == 0) {
175 return i;
176 }
177 }
178
179 return -1;
180 }
181
182 static int tests_run_all(int argc, char * const *argv)
183 {
184 int curroptind = optind;
185 int i;
186 int failcount=0;
187
188 for (i = 0; testlist[i].name; ++i) {
189 if(!testlist[i].off) {
190 failcount+=tests_run_index(i, argc, argv);
191 optind = curroptind;
192 }
193 }
194
195 return failcount;
196 }
197
198 int
199 tests_begin(int argc, char * const *argv)
200 {
201 const char *testcase = NULL;
202 bool initialized = false;
203 bool print_security_logs = false;
204 int testix = -1;
205 int failcount = 0;
206 int ch;
207 int loop = 0;
208
209 for (;;) {
210 while (!testcase && (ch = getopt(argc, argv, "klvws")) != -1)
211 {
212 switch (ch)
213 {
214 #ifdef NO_SERVER
215 case 'k':
216 keep_scratch_dir = true;
217 break;
218 #endif
219 case 's':
220 print_security_logs = true;
221 break;
222
223 case 'v':
224 test_verbose++;
225 break;
226
227 case 'w':
228 sleep(100);
229 break;
230 case 'l':
231 loop=1;
232 break;
233 case '?':
234 default:
235 printf("invalid option %c\n",ch);
236 usage(argv[0]);
237 }
238 }
239
240 if (optind < argc) {
241 testix = tests_named_index(argv[optind]);
242 if(testix<0) {
243 printf("invalid test %s\n",argv[optind]);
244 usage(argv[0]);
245 }
246 }
247
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);
252 date[19] = '\0';
253 CFStringRef logStr = CFStringCreateWithFormat(kCFAllocatorDefault, NULL,
254 CFSTR("%s %@ %s %@\n"), date + 4,
255 scope ? scope : CFSTR(""), function, message);
256 CFShow(logStr);
257 CFReleaseSafe(logStr);
258 });
259 }
260
261 if (testix < 0) {
262 if (!initialized) {
263 initialized = true;
264 tests_init();
265 failcount+=tests_run_all(argc, argv);
266 }
267 break;
268 } else {
269 if (!initialized) {
270 tests_init();
271 initialized = true;
272 }
273 optind++;
274 failcount+=tests_run_index(testix, argc, argv);
275 testix = -1;
276 }
277 }
278
279 printf("Total failcount = %d\n", failcount);
280
281 /* Cleanups */
282 tests_end();
283
284 if(loop) {
285 printf("Looping until key press 'q'. You can run leaks now.\n");
286 while(getchar()!='q');
287 }
288
289 return failcount;
290 }
291