env.c 7.87 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "fileops.h"
3
#include "sysdir.h"
4 5 6
#include "path.h"

#ifdef GIT_WIN32
7 8 9 10
#define NUM_VARS 5
static const char *env_vars[NUM_VARS] = {
	"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
};
11
#else
12 13
#define NUM_VARS 1
static const char *env_vars[NUM_VARS] = { "HOME" };
14
#endif
15

16 17
static char *env_save[NUM_VARS];

18 19
static char *home_values[] = {
	"fake_home",
20 21 22 23
	"f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */
	"f\xc4\x80ke_\xc4\xa4ome", /* latin extended */
	"f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad",  /* having fun with greek */
	"fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */
24
	"f\xe1\x9c\x80ke_\xe1\x9c\x91ome", /* tagalog characters */
25
	"\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */
26 27 28 29
	"\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
	NULL
};

30 31
void test_core_env__initialize(void)
{
32
	int i;
33 34 35
	for (i = 0; i < NUM_VARS; ++i) {
		const char *original = cl_getenv(env_vars[i]);
#ifdef GIT_WIN32
Russell Belfer committed
36
		env_save[i] = (char *)original;
37 38 39 40 41 42
#else
		env_save[i] = original ? git__strdup(original) : NULL;
#endif
	}
}

43
static void set_global_search_path_from_env(void)
44
{
45
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
46 47
}

48
static void set_system_search_path_from_env(void)
49
{
50
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
51 52 53 54 55
}

void test_core_env__cleanup(void)
{
	int i;
56 57
	char **val;

58 59 60
	for (i = 0; i < NUM_VARS; ++i) {
		cl_setenv(env_vars[i], env_save[i]);
		git__free(env_save[i]);
61
		env_save[i] = NULL;
62
	}
63

64 65 66
	/* these will probably have already been cleaned up, but if a test
	 * fails, then it's probably good to try and clear out these dirs
	 */
67
	for (val = home_values; *val != NULL; val++) {
68 69
		if (**val != '\0')
			(void)p_rmdir(*val);
70
	}
71

72
	cl_sandbox_set_search_path_defaults();
73 74
}

75
static void setenv_and_check(const char *name, const char *value)
76
{
77
	char *check;
78

79
	cl_git_pass(cl_setenv(name, value));
80

81 82
	check = cl_getenv(name);
	cl_assert_equal_s(value, check);
Russell Belfer committed
83
#ifdef GIT_WIN32
84
	git__free(check);
Russell Belfer committed
85
#endif
86 87
}

88 89 90
void test_core_env__0(void)
{
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
91
	char testfile[16], tidx = '0';
92
	char **val;
93 94
	const char *testname = "testfile";
	size_t testlen = strlen(testname);
95

96 97
	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);
98 99 100

	for (val = home_values; *val != NULL; val++) {

101 102 103 104
		/* if we can't make the directory, let's just assume
		 * we are on a filesystem that doesn't support the
		 * characters in question and skip this test...
		 */
105 106
		if (p_mkdir(*val, 0777) != 0) {
			*val = ""; /* mark as not created */
107
			continue;
108
		}
109 110 111 112 113 114 115

		cl_git_pass(git_path_prettify(&path, *val, NULL));

		/* vary testfile name in each directory so accidentally leaving
		 * an environment variable set from a previous iteration won't
		 * accidentally make this test pass...
		 */
116
		testfile[testlen] = tidx++;
117 118 119 120
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

121
		cl_assert_equal_i(
122
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
123 124

		setenv_and_check("HOME", path.ptr);
125
		set_global_search_path_from_env();
126

127
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
128 129

		cl_setenv("HOME", env_save[0]);
130
		set_global_search_path_from_env();
131

132
		cl_assert_equal_i(
133
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
134 135

#ifdef GIT_WIN32
136 137 138
		setenv_and_check("HOMEDRIVE", NULL);
		setenv_and_check("HOMEPATH", NULL);
		setenv_and_check("USERPROFILE", path.ptr);
139
		set_global_search_path_from_env();
140

141
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
142

143 144 145
		{
			int root = git_path_root(path.ptr);
			char old;
146

147 148
			if (root >= 0) {
				setenv_and_check("USERPROFILE", NULL);
149
				set_global_search_path_from_env();
150

151
				cl_assert_equal_i(
152
					GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
153 154 155 156 157 158

				old = path.ptr[root];
				path.ptr[root] = '\0';
				setenv_and_check("HOMEDRIVE", path.ptr);
				path.ptr[root] = old;
				setenv_and_check("HOMEPATH", &path.ptr[root]);
159
				set_global_search_path_from_env();
160

161
				cl_git_pass(git_sysdir_find_global_file(&found, testfile));
162
			}
163
		}
164
#endif
165 166

		(void)p_rmdir(*val);
167 168 169 170 171
	}

	git_buf_free(&path);
	git_buf_free(&found);
}
172

173

174 175 176 177
void test_core_env__1(void)
{
	git_buf path = GIT_BUF_INIT;

178
	cl_assert_equal_i(
179
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
180

181
	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
182
#ifdef GIT_WIN32
183
	cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
184 185
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
#endif
186
	set_global_search_path_from_env();
187

188
	cl_assert_equal_i(
189
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
190

191
	cl_git_pass(cl_setenv("HOME", NULL));
192
#ifdef GIT_WIN32
193
	cl_git_pass(cl_setenv("HOMEPATH", NULL));
194 195
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
#endif
196 197
	set_global_search_path_from_env();
	set_system_search_path_from_env();
198

199
	cl_assert_equal_i(
200
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
201

202
	cl_assert_equal_i(
203
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
204 205 206

#ifdef GIT_WIN32
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
207
	set_system_search_path_from_env();
208

209
	cl_assert_equal_i(
210
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
211
#endif
Carlos Martín Nieto committed
212 213

	git_buf_free(&path);
214
}
215

Russell Belfer committed
216 217 218
static void check_global_searchpath(
	const char *path, int position, const char *file, git_buf *temp)
{
219
	git_buf out = GIT_BUF_INIT;
Russell Belfer committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233

	/* build and set new path */
	if (position < 0)
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
	else if (position > 0)
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
	else
		cl_git_pass(git_buf_sets(temp, path));

	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));

	/* get path and make sure $PATH expansion worked */
	cl_git_pass(git_libgit2_opts(
234
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
Russell Belfer committed
235 236

	if (position < 0)
237
		cl_assert(git__prefixcmp(out.ptr, path) == 0);
Russell Belfer committed
238
	else if (position > 0)
239
		cl_assert(git__suffixcmp(out.ptr, path) == 0);
Russell Belfer committed
240
	else
241
		cl_assert_equal_s(out.ptr, path);
Russell Belfer committed
242 243

	/* find file using new path */
244
	cl_git_pass(git_sysdir_find_global_file(temp, file));
Russell Belfer committed
245 246 247 248 249

	/* reset path and confirm file not found */
	cl_git_pass(git_libgit2_opts(
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
	cl_assert_equal_i(
250
		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
251 252

	git_buf_free(&out);
Russell Belfer committed
253 254
}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
void test_core_env__2(void)
{
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
	char testfile[16], tidx = '0';
	char **val;
	const char *testname = "alternate";
	size_t testlen = strlen(testname);

	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);

	for (val = home_values; *val != NULL; val++) {

		/* if we can't make the directory, let's just assume
		 * we are on a filesystem that doesn't support the
		 * characters in question and skip this test...
		 */
272
		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
273 274 275 276 277 278
			*val = ""; /* mark as not created */
			continue;
		}

		cl_git_pass(git_path_prettify(&path, *val, NULL));

Russell Belfer committed
279 280
		/* vary testfile name so any sloppiness is resetting variables or
		 * deleting files won't accidentally make a test pass.
281 282 283 284 285 286
		 */
		testfile[testlen] = tidx++;
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

287
		/* default should be NOTFOUND */
288
		cl_assert_equal_i(
289
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
290

Russell Belfer committed
291 292 293 294
		/* try plain, append $PATH, and prepend $PATH */
		check_global_searchpath(path.ptr,  0, testfile, &found);
		check_global_searchpath(path.ptr, -1, testfile, &found);
		check_global_searchpath(path.ptr,  1, testfile, &found);
295

Russell Belfer committed
296
		/* cleanup */
297 298
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		(void)p_unlink(path.ptr);
299 300 301 302 303 304
		(void)p_rmdir(*val);
	}

	git_buf_free(&path);
	git_buf_free(&found);
}