lookup.c 3.93 KB
Newer Older
1 2 3 4 5 6 7 8
#include "clar_libgit2.h"

#include "repository.h"

static git_repository *g_repo;

void test_object_lookup__initialize(void)
{
9
	g_repo = cl_git_sandbox_init("testrepo.git");
10 11 12 13
}

void test_object_lookup__cleanup(void)
{
14
	cl_git_sandbox_cleanup();
15 16
}

17
void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
18 19 20 21 22
{
	const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
	git_oid oid;
	git_object *object;

23
	cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
24
	cl_assert_equal_i(
25
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
26 27
}

28
void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
29 30 31 32 33
{
	const char *unknown = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
	git_oid oid;
	git_object *object;

34
	cl_git_pass(git_oid__fromstr(&oid, unknown, GIT_OID_SHA1));
35
	cl_assert_equal_i(
36
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_ANY));
37
}
38 39 40 41 42 43 44

void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void)
{
	const char *commit = "e90810b";
	git_oid oid;
	git_object *object;

45
	cl_git_pass(git_oid__fromstrn(&oid, commit, strlen(commit), GIT_OID_SHA1));
46
	cl_assert_equal_i(
47
		GIT_ENOTFOUND, git_object_lookup_prefix(&object, g_repo, &oid, strlen(commit), GIT_OBJECT_TAG));
48 49 50 51 52 53 54 55
}

void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
{
	const char *commit = "e90810b8df3e80c413d903f631643c716887138d";
	git_oid oid;
	git_object *object;

56
	cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
57

58
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
59 60 61
	git_object_free(object);

	cl_assert_equal_i(
62
		GIT_ENOTFOUND, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_TAG));
63
}
64

65 66 67 68
void test_object_lookup__lookup_corrupt_object_returns_error(void)
{
	const char *commit = "8e73b769e97678d684b809b163bebdae2911720f",
	      *file = "objects/8e/73b769e97678d684b809b163bebdae2911720f";
69
	git_str path = GIT_STR_INIT, contents = GIT_STR_INIT;
70 71 72 73
	git_oid oid;
	git_object *object;
	size_t i;

74
	cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
75
	cl_git_pass(git_str_joinpath(&path, git_repository_path(g_repo), file));
76 77 78 79 80 81
	cl_git_pass(git_futils_readbuffer(&contents, path.ptr));

	/* Corrupt and try to read the object */
	for (i = 0; i < contents.size; i++) {
		contents.ptr[i] ^= 0x1;
		cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
82
		cl_git_fail(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
83 84 85 86 87
		contents.ptr[i] ^= 0x1;
	}

	/* Restore original content and assert we can read the object */
	cl_git_pass(git_futils_writebuffer(&contents, path.ptr, O_RDWR, 0644));
88
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
89 90

	git_object_free(object);
91 92
	git_str_dispose(&path);
	git_str_dispose(&contents);
93 94
}

95 96 97 98 99
void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
{
	const char *oldloose = "objects/8e/73b769e97678d684b809b163bebdae2911720f",
	      *newloose = "objects/8e/73b769e97678d684b809b163bebdae2911720e",
	      *commit = "8e73b769e97678d684b809b163bebdae2911720e";
100
	git_str oldpath = GIT_STR_INIT, newpath = GIT_STR_INIT;
101 102 103
	git_object *object;
	git_oid oid;

104
	cl_git_pass(git_oid__fromstr(&oid, commit, GIT_OID_SHA1));
105 106

	/* Copy object to another location with wrong hash */
107 108
	cl_git_pass(git_str_joinpath(&oldpath, git_repository_path(g_repo), oldloose));
	cl_git_pass(git_str_joinpath(&newpath, git_repository_path(g_repo), newloose));
109 110 111
	cl_git_pass(git_futils_cp(oldpath.ptr, newpath.ptr, 0644));

	/* Verify that lookup fails due to a hashsum mismatch */
112
	cl_git_fail_with(GIT_EMISMATCH, git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
113

114 115
	/* Disable verification and try again */
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
116
	cl_git_pass(git_object_lookup(&object, g_repo, &oid, GIT_OBJECT_COMMIT));
117 118
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));

119
	git_object_free(object);
120 121
	git_str_dispose(&oldpath);
	git_str_dispose(&newpath);
122
}