Blame tests/core/env.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "sysdir.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
#define NUM_VARS 5
Packit Service 20376f
static const char *env_vars[NUM_VARS] = {
Packit Service 20376f
	"HOME", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "PROGRAMFILES"
Packit Service 20376f
};
Packit Service 20376f
#else
Packit Service 20376f
#define NUM_VARS 1
Packit Service 20376f
static const char *env_vars[NUM_VARS] = { "HOME" };
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
static char *env_save[NUM_VARS];
Packit Service 20376f
Packit Service 20376f
static char *home_values[] = {
Packit Service 20376f
	"fake_home",
Packit Service 20376f
	"f\xc3\xa1ke_h\xc3\xb5me", /* all in latin-1 supplement */
Packit Service 20376f
	"f\xc4\x80ke_\xc4\xa4ome", /* latin extended */
Packit Service 20376f
	"f\xce\xb1\xce\xba\xce\xb5_h\xce\xbfm\xce\xad",  /* having fun with greek */
Packit Service 20376f
	"fa\xe0" "\xb8" "\x87" "e_\xe0" "\xb8" "\x99" "ome", /* thai characters */
Packit Service 20376f
	"f\xe1\x9c\x80ke_\xe1\x9c\x91ome", /* tagalog characters */
Packit Service 20376f
	"\xe1\xb8\x9f\xe1\xba\xa2" "ke_ho" "\xe1" "\xb9" "\x81" "e", /* latin extended additional */
Packit Service 20376f
	"\xf0\x9f\x98\x98\xf0\x9f\x98\x82", /* emoticons */
Packit Service 20376f
	NULL
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
void test_core_env__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	for (i = 0; i < NUM_VARS; ++i)
Packit Service 20376f
		env_save[i] = cl_getenv(env_vars[i]);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_global_search_path_from_env(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_GLOBAL, NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void set_system_search_path_from_env(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_env__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	char **val;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < NUM_VARS; ++i) {
Packit Service 20376f
		cl_setenv(env_vars[i], env_save[i]);
Packit Service 20376f
		git__free(env_save[i]);
Packit Service 20376f
		env_save[i] = NULL;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* these will probably have already been cleaned up, but if a test
Packit Service 20376f
	 * fails, then it's probably good to try and clear out these dirs
Packit Service 20376f
	 */
Packit Service 20376f
	for (val = home_values; *val != NULL; val++) {
Packit Service 20376f
		if (**val != '\0')
Packit Service 20376f
			(void)p_rmdir(*val);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_sandbox_set_search_path_defaults();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void setenv_and_check(const char *name, const char *value)
Packit Service 20376f
{
Packit Service 20376f
	char *check;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(cl_setenv(name, value));
Packit Service 20376f
	check = cl_getenv(name);
Packit Service 20376f
Packit Service 20376f
	if (value)
Packit Service 20376f
		cl_assert_equal_s(value, check);
Packit Service 20376f
	else
Packit Service 20376f
		cl_assert(check == NULL);
Packit Service 20376f
Packit Service 20376f
	git__free(check);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_env__0(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
Packit Service 20376f
	char testfile[16], tidx = '0';
Packit Service 20376f
	char **val;
Packit Service 20376f
	const char *testname = "testfile";
Packit Service 20376f
	size_t testlen = strlen(testname);
Packit Service 20376f
Packit Service 20376f
	strncpy(testfile, testname, sizeof(testfile));
Packit Service 20376f
	cl_assert_equal_s(testname, testfile);
Packit Service 20376f
Packit Service 20376f
	for (val = home_values; *val != NULL; val++) {
Packit Service 20376f
Packit Service 20376f
		/* if we can't make the directory, let's just assume
Packit Service 20376f
		 * we are on a filesystem that doesn't support the
Packit Service 20376f
		 * characters in question and skip this test...
Packit Service 20376f
		 */
Packit Service 20376f
		if (p_mkdir(*val, 0777) != 0) {
Packit Service 20376f
			*val = ""; /* mark as not created */
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		cl_git_pass(git_path_prettify(&path, *val, NULL));
Packit Service 20376f
Packit Service 20376f
		/* vary testfile name in each directory so accidentally leaving
Packit Service 20376f
		 * an environment variable set from a previous iteration won't
Packit Service 20376f
		 * accidentally make this test pass...
Packit Service 20376f
		 */
Packit Service 20376f
		testfile[testlen] = tidx++;
Packit Service 20376f
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
Packit Service 20376f
		cl_git_mkfile(path.ptr, "find me");
Packit Service 20376f
		git_buf_rtruncate_at_char(&path, '/');
Packit Service 20376f
Packit Service 20376f
		cl_assert_equal_i(
Packit Service 20376f
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
		setenv_and_check("HOME", path.ptr);
Packit Service 20376f
		set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
		cl_setenv("HOME", env_save[0]);
Packit Service 20376f
		set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
		cl_assert_equal_i(
Packit Service 20376f
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
		setenv_and_check("HOMEDRIVE", NULL);
Packit Service 20376f
		setenv_and_check("HOMEPATH", NULL);
Packit Service 20376f
		setenv_and_check("USERPROFILE", path.ptr);
Packit Service 20376f
		set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
		cl_git_pass(git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
		{
Packit Service 20376f
			int root = git_path_root(path.ptr);
Packit Service 20376f
			char old;
Packit Service 20376f
Packit Service 20376f
			if (root >= 0) {
Packit Service 20376f
				setenv_and_check("USERPROFILE", NULL);
Packit Service 20376f
				set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
				cl_assert_equal_i(
Packit Service 20376f
					GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
				old = path.ptr[root];
Packit Service 20376f
				path.ptr[root] = '\0';
Packit Service 20376f
				setenv_and_check("HOMEDRIVE", path.ptr);
Packit Service 20376f
				path.ptr[root] = old;
Packit Service 20376f
				setenv_and_check("HOMEPATH", &path.ptr[root]);
Packit Service 20376f
				set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
				cl_git_pass(git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
			}
Packit Service 20376f
		}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
		(void)p_rmdir(*val);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&found);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
void test_core_env__1(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(cl_setenv("HOME", "doesnotexist"));
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	cl_git_pass(cl_setenv("HOMEPATH", "doesnotexist"));
Packit Service 20376f
	cl_git_pass(cl_setenv("USERPROFILE", "doesnotexist"));
Packit Service 20376f
#endif
Packit Service 20376f
	set_global_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(cl_setenv("HOME", NULL));
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	cl_git_pass(cl_setenv("HOMEPATH", NULL));
Packit Service 20376f
	cl_git_pass(cl_setenv("USERPROFILE", NULL));
Packit Service 20376f
#endif
Packit Service 20376f
	set_global_search_path_from_env();
Packit Service 20376f
	set_system_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_global_file(&path, "nonexistentfile"));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	cl_git_pass(cl_setenv("PROGRAMFILES", NULL));
Packit Service 20376f
	set_system_search_path_from_env();
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_system_file(&path, "nonexistentfile"));
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void check_global_searchpath(
Packit Service 20376f
	const char *path, int position, const char *file, git_buf *temp)
Packit Service 20376f
{
Packit Service 20376f
	git_buf out = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	/* build and set new path */
Packit Service 20376f
	if (position < 0)
Packit Service 20376f
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, path, "$PATH"));
Packit Service 20376f
	else if (position > 0)
Packit Service 20376f
		cl_git_pass(git_buf_join(temp, GIT_PATH_LIST_SEPARATOR, "$PATH", path));
Packit Service 20376f
	else
Packit Service 20376f
		cl_git_pass(git_buf_sets(temp, path));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, temp->ptr));
Packit Service 20376f
Packit Service 20376f
	/* get path and make sure $PATH expansion worked */
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &out));
Packit Service 20376f
Packit Service 20376f
	if (position < 0)
Packit Service 20376f
		cl_assert(git__prefixcmp(out.ptr, path) == 0);
Packit Service 20376f
	else if (position > 0)
Packit Service 20376f
		cl_assert(git__suffixcmp(out.ptr, path) == 0);
Packit Service 20376f
	else
Packit Service 20376f
		cl_assert_equal_s(out.ptr, path);
Packit Service 20376f
Packit Service 20376f
	/* find file using new path */
Packit Service 20376f
	cl_git_pass(git_sysdir_find_global_file(temp, file));
Packit Service 20376f
Packit Service 20376f
	/* reset path and confirm file not found */
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(
Packit Service 20376f
		GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, NULL));
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_sysdir_find_global_file(temp, file));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&out;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_env__2(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, found = GIT_BUF_INIT;
Packit Service 20376f
	char testfile[16], tidx = '0';
Packit Service 20376f
	char **val;
Packit Service 20376f
	const char *testname = "alternate";
Packit Service 20376f
	size_t testlen = strlen(testname);
Packit Service 20376f
Packit Service 20376f
	strncpy(testfile, testname, sizeof(testfile));
Packit Service 20376f
	cl_assert_equal_s(testname, testfile);
Packit Service 20376f
Packit Service 20376f
	for (val = home_values; *val != NULL; val++) {
Packit Service 20376f
Packit Service 20376f
		/* if we can't make the directory, let's just assume
Packit Service 20376f
		 * we are on a filesystem that doesn't support the
Packit Service 20376f
		 * characters in question and skip this test...
Packit Service 20376f
		 */
Packit Service 20376f
		if (p_mkdir(*val, 0777) != 0 && errno != EEXIST) {
Packit Service 20376f
			*val = ""; /* mark as not created */
Packit Service 20376f
			continue;
Packit Service 20376f
		}
Packit Service 20376f
Packit Service 20376f
		cl_git_pass(git_path_prettify(&path, *val, NULL));
Packit Service 20376f
Packit Service 20376f
		/* vary testfile name so any sloppiness is resetting variables or
Packit Service 20376f
		 * deleting files won't accidentally make a test pass.
Packit Service 20376f
		 */
Packit Service 20376f
		testfile[testlen] = tidx++;
Packit Service 20376f
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
Packit Service 20376f
		cl_git_mkfile(path.ptr, "find me");
Packit Service 20376f
		git_buf_rtruncate_at_char(&path, '/');
Packit Service 20376f
Packit Service 20376f
		/* default should be NOTFOUND */
Packit Service 20376f
		cl_assert_equal_i(
Packit Service 20376f
			GIT_ENOTFOUND, git_sysdir_find_global_file(&found, testfile));
Packit Service 20376f
Packit Service 20376f
		/* try plain, append $PATH, and prepend $PATH */
Packit Service 20376f
		check_global_searchpath(path.ptr,  0, testfile, &found);
Packit Service 20376f
		check_global_searchpath(path.ptr, -1, testfile, &found);
Packit Service 20376f
		check_global_searchpath(path.ptr,  1, testfile, &found);
Packit Service 20376f
Packit Service 20376f
		/* cleanup */
Packit Service 20376f
		cl_git_pass(git_buf_joinpath(&path, path.ptr, testfile));
Packit Service 20376f
		(void)p_unlink(path.ptr);
Packit Service 20376f
		(void)p_rmdir(*val);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&found);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_env__substitution(void)
Packit Service 20376f
{
Packit Service 20376f
  git_buf buf = GIT_BUF_INIT, expected = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
  /* Set it to something non-default so we have controllable values */
Packit Service 20376f
  cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, "/tmp/a"));
Packit Service 20376f
  cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));
Packit Service 20376f
  cl_assert_equal_s("/tmp/a", buf.ptr);
Packit Service 20376f
Packit Service 20376f
  git_buf_clear(&buf;;
Packit Service 20376f
  cl_git_pass(git_buf_join(&buf, GIT_PATH_LIST_SEPARATOR, "$PATH", "/tmp/b"));
Packit Service 20376f
  cl_git_pass(git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, buf.ptr));
Packit Service 20376f
  cl_git_pass(git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL, &buf));
Packit Service 20376f
Packit Service 20376f
  cl_git_pass(git_buf_join(&expected, GIT_PATH_LIST_SEPARATOR, "/tmp/a", "/tmp/b"));
Packit Service 20376f
  cl_assert_equal_s(expected.ptr, buf.ptr);
Packit Service 20376f
Packit Service 20376f
  git_buf_free(&expected);
Packit Service 20376f
  git_buf_free(&buf;;
Packit Service 20376f
}