]>
git.saurik.com Git - apple/security.git/blob - Security/sectests/test/testenv.c
2 * Copyright (c) 2005-2007,2009-2013 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>
41 #include <securityd/spi.h>
43 static int current_dir
= -1;
44 static char scratch_dir
[50];
45 static char *home_var
;
46 static bool keep_scratch_dir
= false;
49 rmdir_recursive(const char *path
)
51 char command_buf
[256];
52 if (strlen(path
) + 10 > sizeof(command_buf
) || strchr(path
, '\''))
54 fprintf(stderr
, "# rmdir_recursive: invalid path: %s", path
);
58 sprintf(command_buf
, "rm -rf '%s'", path
);
59 return system(command_buf
);
63 static int tests_init(void) {
65 char preferences_dir
[80];
68 char *path
= getenv("TESTHOME");
70 setenv("TESTHOME", path
, 1);
75 /* Create scratch dir for tests to run in. */
76 sprintf(scratch_dir
, "/tmp/tst-%d", getpid());
77 if (keep_scratch_dir
) {
78 printf("running tests with HOME=%s\n", scratch_dir
);
80 sprintf(library_dir
, "%s/Library", scratch_dir
);
81 sprintf(preferences_dir
, "%s/Preferences", library_dir
);
82 return (ok_unix(mkdir(scratch_dir
, 0755), "mkdir") &&
83 ok_unix(current_dir
= open(".", O_RDONLY
), "open") &&
84 ok_unix(chdir(scratch_dir
), "chdir") &&
85 ok_unix(setenv("HOME", scratch_dir
, 1), "setenv") &&
86 /* @@@ Work around a bug that the prefs code in
87 libsecurity_keychain never creates the Library/Preferences
89 ok_unix(mkdir(library_dir
, 0755), "mkdir") &&
90 ok_unix(mkdir(preferences_dir
, 0755), "mkdir") &&
91 ok(home_var
= getenv("HOME"), "getenv"));
103 /* Restore previous cwd and remove scratch dir. */
104 int ok
= ok_unix(fchdir(current_dir
), "fchdir");
106 ok
= ok_unix(close(current_dir
), "close");
108 if (!keep_scratch_dir
) {
109 ok
= ok_unix(rmdir_recursive(scratch_dir
), "rmdir_recursive");
119 static void usage(const char *progname
)
121 fprintf(stderr
, "usage: %s [-k][-w][testname [testargs] ...]\n", progname
);
125 static int tests_run_index(int i
, int argc
, char * const *argv
)
129 while ((ch
= getopt(argc
, argv
, "v")) != -1)
141 fprintf(stderr
, "TEST: Test Case '%s' started.\n", testlist
[i
].name
);
143 run_one_test(&testlist
[i
], argc
, argv
);
144 if(testlist
[i
].failed_tests
) {
145 fprintf(stderr
, "FAIL: Test Case '%s' failed.\n", testlist
[i
].name
);
147 fprintf(stderr
, "PASS: Test Case '%s' passed. (%lu ms)\n", testlist
[i
].name
, testlist
[i
].duration
);
149 return testlist
[i
].failed_tests
;
152 static int strcmp_under_is_dash(const char *s
, const char *t
) {
154 char a
= *s
++, b
= *t
++;
156 if (a
!= '_' || b
!= '-')
164 static int tests_named_index(const char *testcase
)
168 for (i
= 0; testlist
[i
].name
; ++i
) {
169 if (strcmp_under_is_dash(testlist
[i
].name
, testcase
) == 0) {
177 static int tests_run_all(int argc
, char * const *argv
)
179 int curroptind
= optind
;
183 for (i
= 0; testlist
[i
].name
; ++i
) {
184 if(!testlist
[i
].off
) {
185 failcount
+=tests_run_index(i
, argc
, argv
);
194 tests_begin(int argc
, char * const *argv
)
196 const char *testcase
= NULL
;
197 bool initialized
= false;
204 while (!testcase
&& (ch
= getopt(argc
, argv
, "klw")) != -1)
210 keep_scratch_dir
= true;
221 printf("invalid option %c\n",ch
);
227 testix
= tests_named_index(argv
[optind
]);
229 printf("invalid test %s\n",argv
[optind
]);
238 failcount
+=tests_run_all(argc
, argv
);
247 failcount
+=tests_run_index(testix
, argc
, argv
);
252 printf("Total failcount = %d\n", failcount
);
258 printf("Looping until key press 'q'. You can run leaks now.\n");
259 while(getchar()!='q');