Blame tests/core/path.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_dirname(const char *A, const char *B)
Packit Service 20376f
{
Packit Service 20376f
	git_buf dir = GIT_BUF_INIT;
Packit Service 20376f
	char *dir2;
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_dirname_r(&dir, A) >= 0);
Packit Service 20376f
	cl_assert_equal_s(B, dir.ptr);
Packit Service 20376f
	git_buf_free(&dir;;
Packit Service 20376f
Packit Service 20376f
	cl_assert((dir2 = git_path_dirname(A)) != NULL);
Packit Service 20376f
	cl_assert_equal_s(B, dir2);
Packit Service 20376f
	git__free(dir2);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_basename(const char *A, const char *B)
Packit Service 20376f
{
Packit Service 20376f
	git_buf base = GIT_BUF_INIT;
Packit Service 20376f
	char *base2;
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_basename_r(&base, A) >= 0);
Packit Service 20376f
	cl_assert_equal_s(B, base.ptr);
Packit Service 20376f
	git_buf_free(&base);
Packit Service 20376f
Packit Service 20376f
	cl_assert((base2 = git_path_basename(A)) != NULL);
Packit Service 20376f
	cl_assert_equal_s(B, base2);
Packit Service 20376f
	git__free(base2);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_topdir(const char *A, const char *B)
Packit Service 20376f
{
Packit Service 20376f
	const char *dir;
Packit Service 20376f
Packit Service 20376f
	cl_assert((dir = git_path_topdir(A)) != NULL);
Packit Service 20376f
	cl_assert_equal_s(B, dir);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_joinpath(const char *path_a, const char *path_b, const char *expected_path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf joined_path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&joined_path, path_a, path_b));
Packit Service 20376f
	cl_assert_equal_s(expected_path, joined_path.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&joined_path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_joinpath_n(
Packit Service 20376f
	const char *path_a,
Packit Service 20376f
	const char *path_b,
Packit Service 20376f
	const char *path_c,
Packit Service 20376f
	const char *path_d,
Packit Service 20376f
	const char *expected_path)
Packit Service 20376f
{
Packit Service 20376f
	git_buf joined_path = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_join_n(&joined_path, '/', 4,
Packit Service 20376f
							   path_a, path_b, path_c, path_d));
Packit Service 20376f
	cl_assert_equal_s(expected_path, joined_path.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&joined_path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/* get the dirname of a path */
Packit Service 20376f
void test_core_path__00_dirname(void)
Packit Service 20376f
{
Packit Service 20376f
	check_dirname(NULL, ".");
Packit Service 20376f
	check_dirname("", ".");
Packit Service 20376f
	check_dirname("a", ".");
Packit Service 20376f
	check_dirname("/", "/");
Packit Service 20376f
	check_dirname("/usr", "/");
Packit Service 20376f
	check_dirname("/usr/", "/");
Packit Service 20376f
	check_dirname("/usr/lib", "/usr");
Packit Service 20376f
	check_dirname("/usr/lib/", "/usr");
Packit Service 20376f
	check_dirname("/usr/lib//", "/usr");
Packit Service 20376f
	check_dirname("usr/lib", "usr");
Packit Service 20376f
	check_dirname("usr/lib/", "usr");
Packit Service 20376f
	check_dirname("usr/lib//", "usr");
Packit Service 20376f
	check_dirname(".git/", ".");
Packit Service 20376f
Packit Service 20376f
	check_dirname(REP16("/abc"), REP15("/abc"));
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	check_dirname("C:/", "C:/");
Packit Service 20376f
	check_dirname("C:", "C:/");
Packit Service 20376f
	check_dirname("C:/path/", "C:/");
Packit Service 20376f
	check_dirname("C:/path", "C:/");
Packit Service 20376f
	check_dirname("//computername/", "//computername/");
Packit Service 20376f
	check_dirname("//computername", "//computername/");
Packit Service 20376f
	check_dirname("//computername/path/", "//computername/");
Packit Service 20376f
	check_dirname("//computername/path", "//computername/");
Packit Service 20376f
	check_dirname("//computername/sub/path/", "//computername/sub");
Packit Service 20376f
	check_dirname("//computername/sub/path", "//computername/sub");
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* get the base name of a path */
Packit Service 20376f
void test_core_path__01_basename(void)
Packit Service 20376f
{
Packit Service 20376f
	check_basename(NULL, ".");
Packit Service 20376f
	check_basename("", ".");
Packit Service 20376f
	check_basename("a", "a");
Packit Service 20376f
	check_basename("/", "/");
Packit Service 20376f
	check_basename("/usr", "usr");
Packit Service 20376f
	check_basename("/usr/", "usr");
Packit Service 20376f
	check_basename("/usr/lib", "lib");
Packit Service 20376f
	check_basename("/usr/lib//", "lib");
Packit Service 20376f
	check_basename("usr/lib", "lib");
Packit Service 20376f
Packit Service 20376f
	check_basename(REP16("/abc"), "abc");
Packit Service 20376f
	check_basename(REP1024("/abc"), "abc");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* get the latest component in a path */
Packit Service 20376f
void test_core_path__02_topdir(void)
Packit Service 20376f
{
Packit Service 20376f
	check_topdir(".git/", ".git/");
Packit Service 20376f
	check_topdir("/.git/", ".git/");
Packit Service 20376f
	check_topdir("usr/local/.git/", ".git/");
Packit Service 20376f
	check_topdir("./.git/", ".git/");
Packit Service 20376f
	check_topdir("/usr/.git/", ".git/");
Packit Service 20376f
	check_topdir("/", "/");
Packit Service 20376f
	check_topdir("a/", "a/");
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_topdir("/usr/.git") == NULL);
Packit Service 20376f
	cl_assert(git_path_topdir(".") == NULL);
Packit Service 20376f
	cl_assert(git_path_topdir("") == NULL);
Packit Service 20376f
	cl_assert(git_path_topdir("a") == NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* properly join path components */
Packit Service 20376f
void test_core_path__05_joins(void)
Packit Service 20376f
{
Packit Service 20376f
	check_joinpath("", "", "");
Packit Service 20376f
	check_joinpath("", "a", "a");
Packit Service 20376f
	check_joinpath("", "/a", "/a");
Packit Service 20376f
	check_joinpath("a", "", "a/");
Packit Service 20376f
	check_joinpath("a", "/", "a/");
Packit Service 20376f
	check_joinpath("a", "b", "a/b");
Packit Service 20376f
	check_joinpath("/", "a", "/a");
Packit Service 20376f
	check_joinpath("/", "", "/");
Packit Service 20376f
	check_joinpath("/a", "/b", "/a/b");
Packit Service 20376f
	check_joinpath("/a", "/b/", "/a/b/");
Packit Service 20376f
	check_joinpath("/a/", "b/", "/a/b/");
Packit Service 20376f
	check_joinpath("/a/", "/b/", "/a/b/");
Packit Service 20376f
Packit Service 20376f
	check_joinpath("/abcd", "/defg", "/abcd/defg");
Packit Service 20376f
	check_joinpath("/abcd", "/defg/", "/abcd/defg/");
Packit Service 20376f
	check_joinpath("/abcd/", "defg/", "/abcd/defg/");
Packit Service 20376f
	check_joinpath("/abcd/", "/defg/", "/abcd/defg/");
Packit Service 20376f
Packit Service 20376f
	check_joinpath("/abcdefgh", "/12345678", "/abcdefgh/12345678");
Packit Service 20376f
	check_joinpath("/abcdefgh", "/12345678/", "/abcdefgh/12345678/");
Packit Service 20376f
	check_joinpath("/abcdefgh/", "12345678/", "/abcdefgh/12345678/");
Packit Service 20376f
Packit Service 20376f
	check_joinpath(REP1024("aaaa"), "", REP1024("aaaa") "/");
Packit Service 20376f
	check_joinpath(REP1024("aaaa/"), "", REP1024("aaaa/"));
Packit Service 20376f
	check_joinpath(REP1024("/aaaa"), "", REP1024("/aaaa") "/");
Packit Service 20376f
Packit Service 20376f
	check_joinpath(REP1024("aaaa"), REP1024("bbbb"),
Packit Service 20376f
				   REP1024("aaaa") "/" REP1024("bbbb"));
Packit Service 20376f
	check_joinpath(REP1024("/aaaa"), REP1024("/bbbb"),
Packit Service 20376f
				   REP1024("/aaaa") REP1024("/bbbb"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* properly join path components for more than one path */
Packit Service 20376f
void test_core_path__06_long_joins(void)
Packit Service 20376f
{
Packit Service 20376f
	check_joinpath_n("", "", "", "", "");
Packit Service 20376f
	check_joinpath_n("", "a", "", "", "a/");
Packit Service 20376f
	check_joinpath_n("a", "", "", "", "a/");
Packit Service 20376f
	check_joinpath_n("", "", "", "a", "a");
Packit Service 20376f
	check_joinpath_n("a", "b", "", "/c/d/", "a/b/c/d/");
Packit Service 20376f
	check_joinpath_n("a", "b", "", "/c/d", "a/b/c/d");
Packit Service 20376f
	check_joinpath_n("abcd", "efgh", "ijkl", "mnop", "abcd/efgh/ijkl/mnop");
Packit Service 20376f
	check_joinpath_n("abcd/", "efgh/", "ijkl/", "mnop/", "abcd/efgh/ijkl/mnop/");
Packit Service 20376f
	check_joinpath_n("/abcd/", "/efgh/", "/ijkl/", "/mnop/", "/abcd/efgh/ijkl/mnop/");
Packit Service 20376f
Packit Service 20376f
	check_joinpath_n(REP1024("a"), REP1024("b"), REP1024("c"), REP1024("d"),
Packit Service 20376f
					 REP1024("a") "/" REP1024("b") "/"
Packit Service 20376f
					 REP1024("c") "/" REP1024("d"));
Packit Service 20376f
	check_joinpath_n(REP1024("/a"), REP1024("/b"), REP1024("/c"), REP1024("/d"),
Packit Service 20376f
					 REP1024("/a") REP1024("/b")
Packit Service 20376f
					 REP1024("/c") REP1024("/d"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_path_to_dir(
Packit Service 20376f
	const char* path,
Packit Service 20376f
    const char* expected)
Packit Service 20376f
{
Packit Service 20376f
	git_buf tgt = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	git_buf_sets(&tgt, path);
Packit Service 20376f
	cl_git_pass(git_path_to_dir(&tgt));
Packit Service 20376f
	cl_assert_equal_s(expected, tgt.ptr);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&tgt);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void
Packit Service 20376f
check_string_to_dir(
Packit Service 20376f
	const char* path,
Packit Service 20376f
	size_t      maxlen,
Packit Service 20376f
    const char* expected)
Packit Service 20376f
{
Packit Service 20376f
	size_t len = strlen(path);
Packit Service 20376f
	char *buf = git__malloc(len + 2);
Packit Service 20376f
	cl_assert(buf);
Packit Service 20376f
Packit Service 20376f
	strncpy(buf, path, len + 2);
Packit Service 20376f
Packit Service 20376f
	git_path_string_to_dir(buf, maxlen);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_s(expected, buf);
Packit Service 20376f
Packit Service 20376f
	git__free(buf);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* convert paths to dirs */
Packit Service 20376f
void test_core_path__07_path_to_dir(void)
Packit Service 20376f
{
Packit Service 20376f
	check_path_to_dir("", "");
Packit Service 20376f
	check_path_to_dir(".", "./");
Packit Service 20376f
	check_path_to_dir("./", "./");
Packit Service 20376f
	check_path_to_dir("a/", "a/");
Packit Service 20376f
	check_path_to_dir("ab", "ab/");
Packit Service 20376f
	/* make sure we try just under and just over an expansion that will
Packit Service 20376f
	 * require a realloc
Packit Service 20376f
	 */
Packit Service 20376f
	check_path_to_dir("abcdef", "abcdef/");
Packit Service 20376f
	check_path_to_dir("abcdefg", "abcdefg/");
Packit Service 20376f
	check_path_to_dir("abcdefgh", "abcdefgh/");
Packit Service 20376f
	check_path_to_dir("abcdefghi", "abcdefghi/");
Packit Service 20376f
	check_path_to_dir(REP1024("abcd") "/", REP1024("abcd") "/");
Packit Service 20376f
	check_path_to_dir(REP1024("abcd"), REP1024("abcd") "/");
Packit Service 20376f
Packit Service 20376f
	check_string_to_dir("", 1, "");
Packit Service 20376f
	check_string_to_dir(".", 1, ".");
Packit Service 20376f
	check_string_to_dir(".", 2, "./");
Packit Service 20376f
	check_string_to_dir(".", 3, "./");
Packit Service 20376f
	check_string_to_dir("abcd", 3, "abcd");
Packit Service 20376f
	check_string_to_dir("abcd", 4, "abcd");
Packit Service 20376f
	check_string_to_dir("abcd", 5, "abcd/");
Packit Service 20376f
	check_string_to_dir("abcd", 6, "abcd/");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/* join path to itself */
Packit Service 20376f
void test_core_path__08_self_join(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf path = GIT_BUF_INIT;
Packit Service 20376f
	size_t asize = 0;
Packit Service 20376f
Packit Service 20376f
	asize = path.asize;
Packit Service 20376f
	cl_git_pass(git_buf_sets(&path, "/foo"));
Packit Service 20376f
	cl_assert_equal_s(path.ptr, "/foo");
Packit Service 20376f
	cl_assert(asize < path.asize);
Packit Service 20376f
Packit Service 20376f
	asize = path.asize;
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "this is a new string"));
Packit Service 20376f
	cl_assert_equal_s(path.ptr, "/foo/this is a new string");
Packit Service 20376f
	cl_assert(asize < path.asize);
Packit Service 20376f
Packit Service 20376f
	asize = path.asize;
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr, "/grow the buffer, grow the buffer, grow the buffer"));
Packit Service 20376f
	cl_assert_equal_s(path.ptr, "/foo/this is a new string/grow the buffer, grow the buffer, grow the buffer");
Packit Service 20376f
	cl_assert(asize < path.asize);
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	cl_git_pass(git_buf_sets(&path, "/foo/bar"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "baz"));
Packit Service 20376f
	cl_assert_equal_s(path.ptr, "/bar/baz");
Packit Service 20376f
Packit Service 20376f
	asize = path.asize;
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, path.ptr + 4, "somethinglongenoughtorealloc"));
Packit Service 20376f
	cl_assert_equal_s(path.ptr, "/baz/somethinglongenoughtorealloc");
Packit Service 20376f
	cl_assert(asize < path.asize);
Packit Service 20376f
	
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void check_percent_decoding(const char *expected_result, const char *input)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git__percent_decode(&buf, input));
Packit Service 20376f
	cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__09_percent_decode(void)
Packit Service 20376f
{
Packit Service 20376f
	check_percent_decoding("abcd", "abcd");
Packit Service 20376f
	check_percent_decoding("a2%", "a2%");
Packit Service 20376f
	check_percent_decoding("a2%3", "a2%3");
Packit Service 20376f
	check_percent_decoding("a2%%3", "a2%%3");
Packit Service 20376f
	check_percent_decoding("a2%3z", "a2%3z");
Packit Service 20376f
	check_percent_decoding("a,", "a%2c");
Packit Service 20376f
	check_percent_decoding("a21", "a2%31");
Packit Service 20376f
	check_percent_decoding("a2%1", "a2%%31");
Packit Service 20376f
	check_percent_decoding("a bc ", "a%20bc%20");
Packit Service 20376f
	check_percent_decoding("Vicent Mart" "\355", "Vicent%20Mart%ED");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void check_fromurl(const char *expected_result, const char *input, int should_fail)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert(should_fail || expected_result);
Packit Service 20376f
Packit Service 20376f
	if (!should_fail) {
Packit Service 20376f
		cl_git_pass(git_path_fromurl(&buf, input));
Packit Service 20376f
		cl_assert_equal_s(expected_result, git_buf_cstr(&buf));
Packit Service 20376f
	} else
Packit Service 20376f
		cl_git_fail(git_path_fromurl(&buf, input));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
#define ABS_PATH_MARKER ""
Packit Service 20376f
#else
Packit Service 20376f
#define ABS_PATH_MARKER "/"
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
void test_core_path__10_fromurl(void)
Packit Service 20376f
{
Packit Service 20376f
	/* Failing cases */
Packit Service 20376f
	check_fromurl(NULL, "a", 1);
Packit Service 20376f
	check_fromurl(NULL, "http:///c:/Temp%20folder/note.txt", 1);
Packit Service 20376f
	check_fromurl(NULL, "file://c:/Temp%20folder/note.txt", 1);
Packit Service 20376f
	check_fromurl(NULL, "file:////c:/Temp%20folder/note.txt", 1);
Packit Service 20376f
	check_fromurl(NULL, "file:///", 1);
Packit Service 20376f
	check_fromurl(NULL, "file:////", 1);
Packit Service 20376f
	check_fromurl(NULL, "file://servername/c:/Temp%20folder/note.txt", 1);
Packit Service 20376f
Packit Service 20376f
	/* Passing cases */
Packit Service 20376f
	check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file:///c:/Temp%20folder/note.txt", 0);
Packit Service 20376f
	check_fromurl(ABS_PATH_MARKER "c:/Temp folder/note.txt", "file://localhost/c:/Temp%20folder/note.txt", 0);
Packit Service 20376f
	check_fromurl(ABS_PATH_MARKER "c:/Temp+folder/note.txt", "file:///c:/Temp+folder/note.txt", 0);
Packit Service 20376f
	check_fromurl(ABS_PATH_MARKER "a", "file:///a", 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
typedef struct {
Packit Service 20376f
	int expect_idx;
Packit Service 20376f
	int cancel_after;
Packit Service 20376f
	char **expect;
Packit Service 20376f
} check_walkup_info;
Packit Service 20376f
Packit Service 20376f
#define CANCEL_VALUE 1234
Packit Service 20376f
Packit Service 20376f
static int check_one_walkup_step(void *ref, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	check_walkup_info *info = (check_walkup_info *)ref;
Packit Service 20376f
Packit Service 20376f
	if (!info->cancel_after) {
Packit Service 20376f
		cl_assert_equal_s(info->expect[info->expect_idx], "[CANCEL]");
Packit Service 20376f
		return CANCEL_VALUE;
Packit Service 20376f
	}
Packit Service 20376f
	info->cancel_after--;
Packit Service 20376f
Packit Service 20376f
	cl_assert(info->expect[info->expect_idx] != NULL);
Packit Service 20376f
	cl_assert_equal_s(info->expect[info->expect_idx], path);
Packit Service 20376f
	info->expect_idx++;
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__11_walkup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf p = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	char *expect[] = {
Packit Service 20376f
		/*  1 */ "/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
Packit Service 20376f
		/*  2 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
Packit Service 20376f
		/*  3 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
Packit Service 20376f
		/*  4 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", "/a/", "/", NULL,
Packit Service 20376f
		/*  5 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
Packit Service 20376f
		/*  6 */ "/a/b/c/d/e", "/a/b/c/d/", "/a/b/c/", "/a/b/", NULL,
Packit Service 20376f
		/*  7 */ "this_is_a_path", "", NULL,
Packit Service 20376f
		/*  8 */ "this_is_a_path/", "", NULL,
Packit Service 20376f
		/*  9 */ "///a///b///c///d///e///", "///a///b///c///d///", "///a///b///c///", "///a///b///", "///a///", "///", NULL,
Packit Service 20376f
		/* 10 */ "a/b/c/", "a/b/", "a/", "", NULL,
Packit Service 20376f
		/* 11 */ "a/b/c", "a/b/", "a/", "", NULL,
Packit Service 20376f
		/* 12 */ "a/b/c/", "a/b/", "a/", NULL,
Packit Service 20376f
		/* 13 */ "", NULL,
Packit Service 20376f
		/* 14 */ "/", NULL,
Packit Service 20376f
		/* 15 */ NULL
Packit Service 20376f
	};
Packit Service 20376f
Packit Service 20376f
	char *root[] = {
Packit Service 20376f
		/*  1 */ NULL,
Packit Service 20376f
		/*  2 */ NULL,
Packit Service 20376f
		/*  3 */ "/",
Packit Service 20376f
		/*  4 */ "",
Packit Service 20376f
		/*  5 */ "/a/b",
Packit Service 20376f
		/*  6 */ "/a/b/",
Packit Service 20376f
		/*  7 */ NULL,
Packit Service 20376f
		/*  8 */ NULL,
Packit Service 20376f
		/*  9 */ NULL,
Packit Service 20376f
		/* 10 */ NULL,
Packit Service 20376f
		/* 11 */ NULL,
Packit Service 20376f
		/* 12 */ "a/",
Packit Service 20376f
		/* 13 */ NULL,
Packit Service 20376f
		/* 14 */ NULL,
Packit Service 20376f
	};
Packit Service 20376f
Packit Service 20376f
	int i, j;
Packit Service 20376f
	check_walkup_info info;
Packit Service 20376f
Packit Service 20376f
	info.expect = expect;
Packit Service 20376f
	info.cancel_after = -1;
Packit Service 20376f
Packit Service 20376f
	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
Packit Service 20376f
Packit Service 20376f
		git_buf_sets(&p, expect[i]);
Packit Service 20376f
Packit Service 20376f
		info.expect_idx = i;
Packit Service 20376f
		cl_git_pass(
Packit Service 20376f
			git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
Packit Service 20376f
		);
Packit Service 20376f
Packit Service 20376f
		cl_assert_equal_s(p.ptr, expect[i]);
Packit Service 20376f
		cl_assert(expect[info.expect_idx] == NULL);
Packit Service 20376f
		i = info.expect_idx;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__11a_walkup_cancel(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf p = GIT_BUF_INIT;
Packit Service 20376f
	int cancel[] = { 3, 2, 1, 0 };
Packit Service 20376f
	char *expect[] = {
Packit Service 20376f
		"/a/b/c/d/e/", "/a/b/c/d/", "/a/b/c/", "[CANCEL]", NULL,
Packit Service 20376f
		"/a/b/c/d/e", "/a/b/c/d/", "[CANCEL]", NULL,
Packit Service 20376f
		"/a/b/c/d/e", "[CANCEL]", NULL,
Packit Service 20376f
		"[CANCEL]", NULL,
Packit Service 20376f
		NULL
Packit Service 20376f
	};
Packit Service 20376f
	char *root[] = { NULL, NULL, "/", "", NULL };
Packit Service 20376f
	int i, j;
Packit Service 20376f
	check_walkup_info info;
Packit Service 20376f
Packit Service 20376f
	info.expect = expect;
Packit Service 20376f
Packit Service 20376f
	for (i = 0, j = 0; expect[i] != NULL; i++, j++) {
Packit Service 20376f
Packit Service 20376f
		git_buf_sets(&p, expect[i]);
Packit Service 20376f
Packit Service 20376f
		info.cancel_after = cancel[j];
Packit Service 20376f
		info.expect_idx = i;
Packit Service 20376f
Packit Service 20376f
		cl_assert_equal_i(
Packit Service 20376f
			CANCEL_VALUE,
Packit Service 20376f
			git_path_walk_up(&p, root[j], check_one_walkup_step, &info)
Packit Service 20376f
		);
Packit Service 20376f
Packit Service 20376f
		/* skip to next run of expectations */
Packit Service 20376f
		while (expect[i] != NULL) i++;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__12_offset_to_path_root(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert(git_path_root("non/rooted/path") == -1);
Packit Service 20376f
	cl_assert(git_path_root("/rooted/path") == 0);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	/* Windows specific tests */
Packit Service 20376f
	cl_assert(git_path_root("C:non/rooted/path") == -1);
Packit Service 20376f
	cl_assert(git_path_root("C:/rooted/path") == 2);
Packit Service 20376f
	cl_assert(git_path_root("//computername/sharefolder/resource") == 14);
Packit Service 20376f
	cl_assert(git_path_root("//computername/sharefolder") == 14);
Packit Service 20376f
	cl_assert(git_path_root("//computername") == -1);
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define NON_EXISTING_FILEPATH "i_hope_i_do_not_exist"
Packit Service 20376f
Packit Service 20376f
void test_core_path__13_cannot_prettify_a_non_existing_file(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf p = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_b(git_path_exists(NON_EXISTING_FILEPATH), false);
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH, NULL));
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND, git_path_prettify(&p, NON_EXISTING_FILEPATH "/so-do-i", NULL));
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__14_apply_relative(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf p = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&p, "/this/is/a/base"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../test"));
Packit Service 20376f
	cl_assert_equal_s("/this/is/a/test", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../../the/./end"));
Packit Service 20376f
	cl_assert_equal_s("/this/is/the/end", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "./of/this/../the/string"));
Packit Service 20376f
	cl_assert_equal_s("/this/is/the/end/of/the/string", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../../../../../.."));
Packit Service 20376f
	cl_assert_equal_s("/this/", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../"));
Packit Service 20376f
	cl_assert_equal_s("/", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_path_apply_relative(&p, "../../.."));
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&p, "d:/another/test"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../.."));
Packit Service 20376f
	cl_assert_equal_s("d:/", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "from/here/to/../and/./back/."));
Packit Service 20376f
	cl_assert_equal_s("d:/from/here/and/back/", p.ptr);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&p, "https://my.url.com/test.git"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../another.git"));
Packit Service 20376f
	cl_assert_equal_s("https://my.url.com/another.git", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../full/path/url.patch"));
Packit Service 20376f
	cl_assert_equal_s("https://my.url.com/full/path/url.patch", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, ".."));
Packit Service 20376f
	cl_assert_equal_s("https://my.url.com/full/path/", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../../../"));
Packit Service 20376f
	cl_assert_equal_s("https://", p.ptr);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&p, "../../this/is/relative"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../../preserves/the/prefix"));
Packit Service 20376f
	cl_assert_equal_s("../../this/preserves/the/prefix", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../../../../that"));
Packit Service 20376f
	cl_assert_equal_s("../../that", p.ptr);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_path_apply_relative(&p, "../there"));
Packit Service 20376f
	cl_assert_equal_s("../../there", p.ptr);
Packit Service 20376f
	git_buf_free(&p);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_resolve_relative(
Packit Service 20376f
	git_buf *buf, const char *expected, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_buf_sets(buf, path));
Packit Service 20376f
	cl_git_pass(git_path_resolve_relative(buf, 0));
Packit Service 20376f
	cl_assert_equal_s(expected, buf->ptr);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_path__15_resolve_relative(void)
Packit Service 20376f
{
Packit Service 20376f
	git_buf buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "", "");
Packit Service 20376f
	assert_resolve_relative(&buf, "", ".");
Packit Service 20376f
	assert_resolve_relative(&buf, "", "./");
Packit Service 20376f
	assert_resolve_relative(&buf, "..", "..");
Packit Service 20376f
	assert_resolve_relative(&buf, "../", "../");
Packit Service 20376f
	assert_resolve_relative(&buf, "..", "./..");
Packit Service 20376f
	assert_resolve_relative(&buf, "../", "./../");
Packit Service 20376f
	assert_resolve_relative(&buf, "../", "../.");
Packit Service 20376f
	assert_resolve_relative(&buf, "../", ".././");
Packit Service 20376f
	assert_resolve_relative(&buf, "../..", "../..");
Packit Service 20376f
	assert_resolve_relative(&buf, "../../", "../../");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "/");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "/.");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "", "a/..");
Packit Service 20376f
	assert_resolve_relative(&buf, "", "a/../");
Packit Service 20376f
	assert_resolve_relative(&buf, "", "a/../.");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "/a", "/a");
Packit Service 20376f
	assert_resolve_relative(&buf, "/a/", "/a/.");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "/a/../");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "/a/../.");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "/a/.././");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "a", "a");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/", "a/");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/", "a/.");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/", "a/./");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "a/b", "a//b");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/b/c", "a/b/c");
Packit Service 20376f
	assert_resolve_relative(&buf, "b/c", "./b/c");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/c", "a/./c");
Packit Service 20376f
	assert_resolve_relative(&buf, "a/b/", "a/b/.");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "/a/b/c", "///a/b/c");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "////");
Packit Service 20376f
	assert_resolve_relative(&buf, "/a", "///a");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "///.");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "///a/..");
Packit Service 20376f
Packit Service 20376f
	assert_resolve_relative(&buf, "../../path", "../../test//../././path");
Packit Service 20376f
	assert_resolve_relative(&buf, "../d", "a/b/../../../c/../d");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "/.."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "/./.."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "/.//.."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "/../."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "/../.././../a"));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "////.."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
Packit Service 20376f
	/* things that start with Windows network paths */
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	assert_resolve_relative(&buf, "//a/b/c", "//a/b/c");
Packit Service 20376f
	assert_resolve_relative(&buf, "//a/", "//a/b/..");
Packit Service 20376f
	assert_resolve_relative(&buf, "//a/b/c", "//a/Q/../b/x/y/../../c");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_buf_sets(&buf, "//a/b/../.."));
Packit Service 20376f
	cl_git_fail(git_path_resolve_relative(&buf, 0));
Packit Service 20376f
#else
Packit Service 20376f
	assert_resolve_relative(&buf, "/a/b/c", "//a/b/c");
Packit Service 20376f
	assert_resolve_relative(&buf, "/a/", "//a/b/..");
Packit Service 20376f
	assert_resolve_relative(&buf, "/a/b/c", "//a/Q/../b/x/y/../../c");
Packit Service 20376f
	assert_resolve_relative(&buf, "/", "//a/b/../..");
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	git_buf_free(&buf;;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define assert_common_dirlen(i, p, q) \
Packit Service 20376f
	cl_assert_equal_i((i), git_path_common_dirlen((p), (q)));
Packit Service 20376f
Packit Service 20376f
void test_core_path__16_resolve_relative(void)
Packit Service 20376f
{
Packit Service 20376f
	assert_common_dirlen(0, "", "");
Packit Service 20376f
	assert_common_dirlen(0, "", "bar.txt");
Packit Service 20376f
	assert_common_dirlen(0, "foo.txt", "bar.txt");
Packit Service 20376f
	assert_common_dirlen(0, "foo.txt", "");
Packit Service 20376f
	assert_common_dirlen(0, "foo/bar.txt", "bar/foo.txt");
Packit Service 20376f
	assert_common_dirlen(0, "foo/bar.txt", "../foo.txt");
Packit Service 20376f
Packit Service 20376f
	assert_common_dirlen(1, "/one.txt", "/two.txt");
Packit Service 20376f
	assert_common_dirlen(4, "foo/one.txt", "foo/two.txt");
Packit Service 20376f
	assert_common_dirlen(5, "/foo/one.txt", "/foo/two.txt");
Packit Service 20376f
Packit Service 20376f
	assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt");
Packit Service 20376f
	assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt");
Packit Service 20376f
}