env.c 7.85 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 43 44
#else
		env_save[i] = original ? git__strdup(original) : NULL;
#endif
	}
}

static void reset_global_search_path(void)
{
45
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
46 47 48 49
}

static void reset_system_search_path(void)
{
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

	/* reset search paths to default */
73 74
	reset_global_search_path();
	reset_system_search_path();
75 76
}

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

81
	cl_git_pass(cl_setenv(name, value));
82

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

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

98 99
	strncpy(testfile, testname, sizeof(testfile));
	cl_assert_equal_s(testname, testfile);
100 101 102

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

103 104 105 106
		/* 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...
		 */
107 108
		if (p_mkdir(*val, 0777) != 0) {
			*val = ""; /* mark as not created */
109
			continue;
110
		}
111 112 113 114 115 116 117

		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...
		 */
118
		testfile[testlen] = tidx++;
119 120 121 122
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
		cl_git_mkfile(path.ptr, "find me");
		git_buf_rtruncate_at_char(&path, '/');

123
		cl_assert_equal_i(
124
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
125 126

		setenv_and_check("HOME", path.ptr);
127 128
		reset_global_search_path();

129
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
130 131

		cl_setenv("HOME", env_save[0]);
132 133
		reset_global_search_path();

134
		cl_assert_equal_i(
135
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
136 137

#ifdef GIT_WIN32
138 139 140
		setenv_and_check("HOMEDRIVE", NULL);
		setenv_and_check("HOMEPATH", NULL);
		setenv_and_check("USERPROFILE", path.ptr);
141
		reset_global_search_path();
142

143
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
144

145 146 147
		{
			int root = git_path_root(path.ptr);
			char old;
148

149 150
			if (root >= 0) {
				setenv_and_check("USERPROFILE", NULL);
151
				reset_global_search_path();
152

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

				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]);
161
				reset_global_search_path();
162

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

		(void)p_rmdir(*val);
169 170 171 172 173
	}

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

175

176 177 178 179
void test_core_env__1(void)
{
	git_buf path = GIT_BUF_INIT;

180
	cl_assert_equal_i(
181
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
182

183
	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
184
#ifdef GIT_WIN32
185
	cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
186 187
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
#endif
188
	reset_global_search_path();
189

190
	cl_assert_equal_i(
191
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
192

193
	cl_git_pass(cl_setenv("HOME", NULL));
194
#ifdef GIT_WIN32
195
	cl_git_pass(cl_setenv("HOMEPATH", NULL));
196 197
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
#endif
198 199
	reset_global_search_path();
	reset_system_search_path();
200

201
	cl_assert_equal_i(
202
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
203

204
	cl_assert_equal_i(
205
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
206 207 208

#ifdef GIT_WIN32
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
209 210
	reset_system_search_path();

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

	git_buf_free(&path);
216
}
217

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

	/* 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(
236
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
Russell Belfer committed
237 238

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

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

	/* 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(
252
		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
253 254

	git_buf_free(&out);
Russell Belfer committed
255 256
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
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...
		 */
274
		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
275 276 277 278 279 280
			*val = ""; /* mark as not created */
			continue;
		}

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

Russell Belfer committed
281 282
		/* vary testfile name so any sloppiness is resetting variables or
		 * deleting files won't accidentally make a test pass.
283 284 285 286 287 288
		 */
		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, '/');

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

Russell Belfer committed
293 294 295 296
		/* 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);
297

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

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