]> git.saurik.com Git - apple/security.git/blob - SecurityTests/regressions/test/testenv.c
Security-57740.1.18.tar.gz
[apple/security.git] / SecurityTests / regressions / test / testenv.c
1 /*
2 * Copyright (c) 2005-2006 Apple Computer, 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
34 #include "testmore.h"
35 #include "testenv.h"
36
37 static int current_dir = -1;
38 static char scratch_dir[50];
39 static char *home_var;
40
41 int
42 rmdir_recursive(const char *path)
43 {
44 char command_buf[256];
45 if (strlen(path) + 10 > sizeof(command_buf) || strchr(path, '\''))
46 {
47 fprintf(stderr, "# rmdir_recursive: invalid path: %s", path);
48 return -1;
49 }
50
51 sprintf(command_buf, "rm -rf '%s'", path);
52 return system(command_buf);
53 }
54
55 int
56 tests_begin(int argc, char * const *argv)
57 {
58 char library_dir[70];
59 char preferences_dir[80];
60
61 setup("tests_begin");
62
63 /* Create scratch dir for tests to run in. */
64 sprintf(scratch_dir, "/tmp/tst-%d", getpid());
65 sprintf(library_dir, "%s/Library", scratch_dir);
66 sprintf(preferences_dir, "%s/Preferences", library_dir);
67 return (ok_unix(mkdir(scratch_dir, 0755), "mkdir") &&
68 ok_unix(current_dir = open(".", O_RDONLY), "open") &&
69 ok_unix(chdir(scratch_dir), "chdir") &&
70 ok_unix(setenv("HOME", scratch_dir, 1), "setenv") &&
71 /* @@@ Work around a bug that the prefs code in
72 libsecurity_keychain never creates the Library/Preferences
73 dir. */
74 ok_unix(mkdir(library_dir, 0755), "mkdir") &&
75 ok_unix(mkdir(preferences_dir, 0755), "mkdir") &&
76 ok_unix(home_var = getenv("HOME"), "getenv"));
77 }
78
79 int
80 tests_end(int result)
81 {
82 setup("tests_end");
83 /* Restore previous cwd and remove scratch dir. */
84 return (ok_unix(fchdir(current_dir), "fchdir") &&
85 ok_unix(close(current_dir), "close") &&
86 ok_unix(rmdir_recursive(scratch_dir), "rmdir_recursive"));
87 }