Blame tests/submodule/escape.c

Packit ae9e2a
#include "clar_libgit2.h"
Packit ae9e2a
#include "posix.h"
Packit ae9e2a
#include "path.h"
Packit ae9e2a
#include "submodule_helpers.h"
Packit ae9e2a
#include "fileops.h"
Packit ae9e2a
#include "repository.h"
Packit ae9e2a
Packit ae9e2a
static git_repository *g_repo = NULL;
Packit ae9e2a
Packit ae9e2a
void test_submodule_escape__cleanup(void)
Packit ae9e2a
{
Packit ae9e2a
	cl_git_sandbox_cleanup();
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
#define EVIL_SM_NAME "../../modules/evil"
Packit ae9e2a
#define EVIL_SM_NAME_WINDOWS "..\\\\..\\\\modules\\\\evil"
Packit ae9e2a
#define EVIL_SM_NAME_WINDOWS_UNESC "..\\..\\modules\\evil"
Packit ae9e2a
Packit ae9e2a
static int find_evil(git_submodule *sm, const char *name, void *payload)
Packit ae9e2a
{
Packit ae9e2a
	int *foundit = (int *) payload;
Packit ae9e2a
Packit ae9e2a
	GIT_UNUSED(sm);
Packit ae9e2a
Packit ae9e2a
	if (!git__strcmp(EVIL_SM_NAME, name) ||
Packit ae9e2a
	    !git__strcmp(EVIL_SM_NAME_WINDOWS_UNESC, name))
Packit ae9e2a
		*foundit = true;
Packit ae9e2a
Packit ae9e2a
	return 0;
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_submodule_escape__from_gitdir(void)
Packit ae9e2a
{
Packit ae9e2a
	int foundit;
Packit ae9e2a
	git_submodule *sm;
Packit ae9e2a
	git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
	unsigned int sm_location;
Packit ae9e2a
Packit ae9e2a
	g_repo = setup_fixture_submodule_simple();
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
Packit ae9e2a
	cl_git_rewritefile(buf.ptr,
Packit ae9e2a
			   "[submodule \"" EVIL_SM_NAME "\"]\n"
Packit ae9e2a
			   "    path = testrepo\n"
Packit ae9e2a
			   "    url = ../testrepo.git\n");
Packit ae9e2a
	git_buf_free(&buf;;
Packit ae9e2a
Packit ae9e2a
	/* Find it all the different ways we know about it */
Packit ae9e2a
	foundit = 0;
Packit ae9e2a
	cl_git_pass(git_submodule_foreach(g_repo, find_evil, &foundit));
Packit ae9e2a
	cl_assert_equal_i(0, foundit);
Packit ae9e2a
	cl_git_fail_with(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, EVIL_SM_NAME));
Packit ae9e2a
	/*
Packit ae9e2a
	 * We do know about this as it's in the index and HEAD, but the data is
Packit ae9e2a
	 * incomplete as there is no configured data for it (we pretend it
Packit ae9e2a
	 * doesn't exist). This leaves us with an odd situation but it's
Packit ae9e2a
	 * consistent with what we would do if we did add a submodule with no
Packit ae9e2a
	 * configuration.
Packit ae9e2a
	 */
Packit ae9e2a
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
Packit ae9e2a
	cl_git_pass(git_submodule_location(&sm_location, sm));
Packit ae9e2a
	cl_assert_equal_i(GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_HEAD, sm_location);
Packit ae9e2a
	git_submodule_free(sm);
Packit ae9e2a
}
Packit ae9e2a
Packit ae9e2a
void test_submodule_escape__from_gitdir_windows(void)
Packit ae9e2a
{
Packit ae9e2a
	int foundit;
Packit ae9e2a
	git_submodule *sm;
Packit ae9e2a
	git_buf buf = GIT_BUF_INIT;
Packit ae9e2a
	unsigned int sm_location;
Packit ae9e2a
Packit ae9e2a
	g_repo = setup_fixture_submodule_simple();
Packit ae9e2a
Packit ae9e2a
	cl_git_pass(git_buf_joinpath(&buf, git_repository_workdir(g_repo), ".gitmodules"));
Packit ae9e2a
	cl_git_rewritefile(buf.ptr,
Packit ae9e2a
			   "[submodule \"" EVIL_SM_NAME_WINDOWS "\"]\n"
Packit ae9e2a
			   "    path = testrepo\n"
Packit ae9e2a
			   "    url = ../testrepo.git\n");
Packit ae9e2a
	git_buf_free(&buf;;
Packit ae9e2a
Packit ae9e2a
	/* Find it all the different ways we know about it */
Packit ae9e2a
	foundit = 0;
Packit ae9e2a
	cl_git_pass(git_submodule_foreach(g_repo, find_evil, &foundit));
Packit ae9e2a
	cl_assert_equal_i(0, foundit);
Packit ae9e2a
	cl_git_fail_with(GIT_ENOTFOUND, git_submodule_lookup(&sm, g_repo, EVIL_SM_NAME_WINDOWS_UNESC));
Packit ae9e2a
	/*
Packit ae9e2a
	 * We do know about this as it's in the index and HEAD, but the data is
Packit ae9e2a
	 * incomplete as there is no configured data for it (we pretend it
Packit ae9e2a
	 * doesn't exist). This leaves us with an odd situation but it's
Packit ae9e2a
	 * consistent with what we would do if we did add a submodule with no
Packit ae9e2a
	 * configuration.
Packit ae9e2a
	 */
Packit ae9e2a
	cl_git_pass(git_submodule_lookup(&sm, g_repo, "testrepo"));
Packit ae9e2a
	cl_git_pass(git_submodule_location(&sm_location, sm));
Packit ae9e2a
	cl_assert_equal_i(GIT_SUBMODULE_STATUS_IN_INDEX | GIT_SUBMODULE_STATUS_IN_HEAD, sm_location);
Packit ae9e2a
	git_submodule_free(sm);
Packit ae9e2a
}