]> git.proxmox.com Git - libgit2.git/blob - tests/core/env.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / core / env.c
1 #include "clar_libgit2.h"
2 #include "futils.h"
3 #include "sysdir.h"
4
5 #ifdef GIT_WIN32
6 #define NUM_VARS 5
7 static const char *env_vars[NUM_VARS] = {
8 "HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
9 };
10 #else
11 #define NUM_VARS 1
12 static const char *env_vars[NUM_VARS] = { "HOME" };
13 #endif
14
15 static char *env_save[NUM_VARS];
16
17 static char *home_values[] = {
18 "fake_home",
19 "f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */
20 "f\xc4\x80ke_\xc4\xa4ome", /* latin extended */
21 "f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad", /* having fun with greek */
22 "fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */
23 "f\xe1\x9c\x80ke_\xe1\x9c\x91ome", /* tagalog characters */
24 "\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */
25 "\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
26 NULL
27 };
28
29 void test_core_env__initialize(void)
30 {
31 int i;
32 for (i = 0; i < NUM_VARS; ++i)
33 env_save[i] = cl_getenv(env_vars[i]);
34 }
35
36 static void set_global_search_path_from_env(void)
37 {
38 cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
39 }
40
41 static void set_system_search_path_from_env(void)
42 {
43 cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
44 }
45
46 void test_core_env__cleanup(void)
47 {
48 int i;
49 char **val;
50
51 for (i = 0; i < NUM_VARS; ++i) {
52 cl_setenv(env_vars[i], env_save[i]);
53 git__free(env_save[i]);
54 env_save[i] = NULL;
55 }
56
57 /* these will probably have already been cleaned up, but if a test
58 * fails, then it's probably good to try and clear out these dirs
59 */
60 for (val = home_values; *val != NULL; val++) {
61 if (**val != '\0')
62 (void)p_rmdir(*val);
63 }
64
65 cl_sandbox_set_search_path_defaults();
66 }
67
68 static void setenv_and_check(const char *name, const char *value)
69 {
70 char *check;
71
72 cl_git_pass(cl_setenv(name, value));
73 check = cl_getenv(name);
74
75 if (value)
76 cl_assert_equal_s(value, check);
77 else
78 cl_assert(check == NULL);
79
80 git__free(check);
81 }
82
83 void test_core_env__0(void)
84 {
85 git_str path = GIT_STR_INIT, found = GIT_STR_INIT;
86 char testfile[16], tidx = '0';
87 char **val;
88 const char *testname = "testfile";
89 size_t testlen = strlen(testname);
90
91 strncpy(testfile, testname, sizeof(testfile));
92 cl_assert_equal_s(testname, testfile);
93
94 for (val = home_values; *val != NULL; val++) {
95
96 /* if we can't make the directory, let's just assume
97 * we are on a filesystem that doesn't support the
98 * characters in question and skip this test...
99 */
100 if (p_mkdir(*val, 0777) != 0) {
101 *val = ""; /* mark as not created */
102 continue;
103 }
104
105 cl_git_pass(git_fs_path_prettify(&path, *val, NULL));
106
107 /* vary testfile name in each directory so accidentally leaving
108 * an environment variable set from a previous iteration won't
109 * accidentally make this test pass...
110 */
111 testfile[testlen] = tidx++;
112 cl_git_pass(git_str_joinpath(&path, path.ptr, testfile));
113 cl_git_mkfile(path.ptr, "find me");
114 git_str_rtruncate_at_char(&path, '/');
115
116 cl_assert_equal_i(
117 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
118
119 setenv_and_check("HOME", path.ptr);
120 set_global_search_path_from_env();
121
122 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
123
124 cl_setenv("HOME", env_save[0]);
125 set_global_search_path_from_env();
126
127 cl_assert_equal_i(
128 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
129
130 #ifdef GIT_WIN32
131 setenv_and_check("HOMEDRIVE", NULL);
132 setenv_and_check("HOMEPATH", NULL);
133 setenv_and_check("USERPROFILE", path.ptr);
134 set_global_search_path_from_env();
135
136 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
137
138 {
139 int root = git_fs_path_root(path.ptr);
140 char old;
141
142 if (root >= 0) {
143 setenv_and_check("USERPROFILE", NULL);
144 set_global_search_path_from_env();
145
146 cl_assert_equal_i(
147 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
148
149 old = path.ptr[root];
150 path.ptr[root] = '\0';
151 setenv_and_check("HOMEDRIVE", path.ptr);
152 path.ptr[root] = old;
153 setenv_and_check("HOMEPATH", &path.ptr[root]);
154 set_global_search_path_from_env();
155
156 cl_git_pass(git_sysdir_find_global_file(&found, testfile));
157 }
158 }
159 #endif
160
161 (void)p_rmdir(*val);
162 }
163
164 git_str_dispose(&path);
165 git_str_dispose(&found);
166 }
167
168
169 void test_core_env__1(void)
170 {
171 git_str path = GIT_STR_INIT;
172
173 cl_assert_equal_i(
174 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
175
176 cl_git_pass(cl_setenv("HOME", "doesnotexist"));
177 #ifdef GIT_WIN32
178 cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
179 cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
180 #endif
181 set_global_search_path_from_env();
182
183 cl_assert_equal_i(
184 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
185
186 cl_git_pass(cl_setenv("HOME", NULL));
187 #ifdef GIT_WIN32
188 cl_git_pass(cl_setenv("HOMEPATH", NULL));
189 cl_git_pass(cl_setenv("USERPROFILE", NULL));
190 #endif
191 set_global_search_path_from_env();
192 set_system_search_path_from_env();
193
194 cl_assert_equal_i(
195 GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
196
197 cl_assert_equal_i(
198 GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
199
200 #ifdef GIT_WIN32
201 cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
202 set_system_search_path_from_env();
203
204 cl_assert_equal_i(
205 GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
206 #endif
207
208 git_str_dispose(&path);
209 }
210
211 static void check_global_searchpath(
212 const char *path, int position, const char *file, git_str *temp)
213 {
214 git_str out = GIT_STR_INIT;
215
216 /* build and set new path */
217 if (position < 0)
218 cl_git_pass(git_str_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
219 else if (position > 0)
220 cl_git_pass(git_str_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
221 else
222 cl_git_pass(git_str_sets(temp, path));
223
224 cl_git_pass(git_libgit2_opts(
225 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));
226
227 /* get path and make sure $PATH expansion worked */
228 cl_git_pass(git_libgit2_opts(
229 GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
230
231 if (position < 0)
232 cl_assert(git__prefixcmp(out.ptr, path) == 0);
233 else if (position > 0)
234 cl_assert(git__suffixcmp(out.ptr, path) == 0);
235 else
236 cl_assert_equal_s(out.ptr, path);
237
238 /* find file using new path */
239 cl_git_pass(git_sysdir_find_global_file(temp, file));
240
241 /* reset path and confirm file not found */
242 cl_git_pass(git_libgit2_opts(
243 GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
244 cl_assert_equal_i(
245 GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
246
247 git_str_dispose(&out);
248 }
249
250 void test_core_env__2(void)
251 {
252 git_str path = GIT_STR_INIT, found = GIT_STR_INIT;
253 char testfile[16], tidx = '0';
254 char **val;
255 const char *testname = "alternate";
256 size_t testlen = strlen(testname);
257
258 strncpy(testfile, testname, sizeof(testfile));
259 cl_assert_equal_s(testname, testfile);
260
261 for (val = home_values; *val != NULL; val++) {
262
263 /* if we can't make the directory, let's just assume
264 * we are on a filesystem that doesn't support the
265 * characters in question and skip this test...
266 */
267 if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
268 *val = ""; /* mark as not created */
269 continue;
270 }
271
272 cl_git_pass(git_fs_path_prettify(&path, *val, NULL));
273
274 /* vary testfile name so any sloppiness is resetting variables or
275 * deleting files won't accidentally make a test pass.
276 */
277 testfile[testlen] = tidx++;
278 cl_git_pass(git_str_joinpath(&path, path.ptr, testfile));
279 cl_git_mkfile(path.ptr, "find me");
280 git_str_rtruncate_at_char(&path, '/');
281
282 /* default should be NOTFOUND */
283 cl_assert_equal_i(
284 GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
285
286 /* try plain, append $PATH, and prepend $PATH */
287 check_global_searchpath(path.ptr, 0, testfile, &found);
288 check_global_searchpath(path.ptr, -1, testfile, &found);
289 check_global_searchpath(path.ptr, 1, testfile, &found);
290
291 /* cleanup */
292 cl_git_pass(git_str_joinpath(&path, path.ptr, testfile));
293 (void)p_unlink(path.ptr);
294 (void)p_rmdir(*val);
295 }
296
297 git_str_dispose(&path);
298 git_str_dispose(&found);
299 }
300
301 void test_core_env__substitution(void)
302 {
303 git_str buf = GIT_STR_INIT, expected = GIT_STR_INIT;
304
305 /* Set it to something non-default so we have controllable values */
306 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "/tmp/a"));
307 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));
308 cl_assert_equal_s("/tmp/a", buf.ptr);
309
310 git_str_clear(&buf);
311 cl_git_pass(git_str_join(&buf, GIT_PATH_LIST_SEPARATOR, "$PATH", "/tmp/b"));
312 cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, buf.ptr));
313 cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));
314
315 cl_git_pass(git_str_join(&expected, GIT_PATH_LIST_SEPARATOR, "/tmp/a", "/tmp/b"));
316 cl_assert_equal_s(expected.ptr, buf.ptr);
317
318 git_str_dispose(&expected);
319 git_str_dispose(&buf);
320 }