Blame tests/object/lookup.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo;
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	g_repo = cl_git_sandbox_init("testrepo.git");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstr(&oid, commit));
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstr(&oid, unknown));
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_ANY));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *commit = "e90810b";
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstrn(&oid, commit, strlen(commit)));
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJ_TAG));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstr(&oid, commit));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(
Packit Service 20376f
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_TAG));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_corrupt_object_returns_error(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
Packit Service 20376f
	      *file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
Packit Service 20376f
	git_buf path = GIT_BUF_INIT, contents = GIT_BUF_INIT;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
	size_t i;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstr(&oid, commit));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&path, git_repository_path(g_repo), file));
Packit Service 20376f
	cl_git_pass(git_futils_readbuffer(&contents, path.ptr));
Packit Service 20376f
Packit Service 20376f
	/* Corrupt and try to read the object */
Packit Service 20376f
	for (i = 0; i < contents.size; i++) {
Packit Service 20376f
		contents.ptr[i] ^= 0x1;
Packit Service 20376f
		cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
Packit Service 20376f
		cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
Packit Service 20376f
		contents.ptr[i] ^= 0x1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	/* Restore original content and assert we can read the object */
Packit Service 20376f
	cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
	git_buf_free(&path);
Packit Service 20376f
	git_buf_free(&contents);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
Packit Service 20376f
{
Packit Service 20376f
	const char *oldloose = "objects/8e/73b769e97678d684b809b163bebdae2911720f",
Packit Service 20376f
	      *newloose = "objects/8e/73b769e97678d684b809b163bebdae2911720e",
Packit Service 20376f
	      *commit = "8e73b769e97678d684b809b163bebdae2911720e";
Packit Service 20376f
	git_buf oldpath = GIT_BUF_INIT, newpath = GIT_BUF_INIT;
Packit Service 20376f
	git_object *object;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_oid_fromstr(&oid, commit));
Packit Service 20376f
Packit Service 20376f
	/* Copy object to another location with wrong hash */
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
Packit Service 20376f
	cl_git_pass(git_buf_joinpath(&newpath, git_repository_path(g_repo), newloose));
Packit Service 20376f
	cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));
Packit Service 20376f
Packit Service 20376f
	/* Verify that lookup fails due to a hashsum mismatch */
Packit Service 20376f
	cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
Packit Service 20376f
Packit Service 20376f
	/* Disable verification and try again */
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
Packit Service 20376f
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJ_COMMIT));
Packit Service 20376f
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
Packit Service 20376f
Packit Service 20376f
	git_object_free(object);
Packit Service 20376f
	git_buf_free(&oldpath);
Packit Service 20376f
	git_buf_free(&newpath);
Packit Service 20376f
}