1 #include "clar_libgit2.h"
3 #include "repository.h"
5 static git_repository
*g_repo
;
7 void test_object_lookup__initialize(void)
9 g_repo
= cl_git_sandbox_init("testrepo.git");
12 void test_object_lookup__cleanup(void)
14 cl_git_sandbox_cleanup();
17 void test_object_lookup__lookup_wrong_type_returns_enotfound(void)
19 const char *commit
= "e90810b8df3e80c413d903f631643c716887138d";
23 cl_git_pass(git_oid_fromstr(&oid
, commit
));
25 GIT_ENOTFOUND
, git_object_lookup(&object
, g_repo
, &oid
, GIT_OBJECT_TAG
));
28 void test_object_lookup__lookup_nonexisting_returns_enotfound(void)
30 const char *unknown
= "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef";
34 cl_git_pass(git_oid_fromstr(&oid
, unknown
));
36 GIT_ENOTFOUND
, git_object_lookup(&object
, g_repo
, &oid
, GIT_OBJECT_ANY
));
39 void test_object_lookup__lookup_wrong_type_by_abbreviated_id_returns_enotfound(void)
41 const char *commit
= "e90810b";
45 cl_git_pass(git_oid_fromstrn(&oid
, commit
, strlen(commit
)));
47 GIT_ENOTFOUND
, git_object_lookup_prefix(&object
, g_repo
, &oid
, strlen(commit
), GIT_OBJECT_TAG
));
50 void test_object_lookup__lookup_wrong_type_eventually_returns_enotfound(void)
52 const char *commit
= "e90810b8df3e80c413d903f631643c716887138d";
56 cl_git_pass(git_oid_fromstr(&oid
, commit
));
58 cl_git_pass(git_object_lookup(&object
, g_repo
, &oid
, GIT_OBJECT_COMMIT
));
59 git_object_free(object
);
62 GIT_ENOTFOUND
, git_object_lookup(&object
, g_repo
, &oid
, GIT_OBJECT_TAG
));
65 void test_object_lookup__lookup_corrupt_object_returns_error(void)
67 const char *commit
= "8e73b769e97678d684b809b163bebdae2911720f",
68 *file
= "objects/8e/73b769e97678d684b809b163bebdae2911720f";
69 git_str path
= GIT_STR_INIT
, contents
= GIT_STR_INIT
;
74 cl_git_pass(git_oid_fromstr(&oid
, commit
));
75 cl_git_pass(git_str_joinpath(&path
, git_repository_path(g_repo
), file
));
76 cl_git_pass(git_futils_readbuffer(&contents
, path
.ptr
));
78 /* Corrupt and try to read the object */
79 for (i
= 0; i
< contents
.size
; i
++) {
80 contents
.ptr
[i
] ^= 0x1;
81 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 contents
.ptr
[i
] ^= 0x1;
86 /* Restore original content and assert we can read the object */
87 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
));
90 git_object_free(object
);
91 git_str_dispose(&path
);
92 git_str_dispose(&contents
);
95 void test_object_lookup__lookup_object_with_wrong_hash_returns_error(void)
97 const char *oldloose
= "objects/8e/73b769e97678d684b809b163bebdae2911720f",
98 *newloose
= "objects/8e/73b769e97678d684b809b163bebdae2911720e",
99 *commit
= "8e73b769e97678d684b809b163bebdae2911720e";
100 git_str oldpath
= GIT_STR_INIT
, newpath
= GIT_STR_INIT
;
104 cl_git_pass(git_oid_fromstr(&oid
, commit
));
106 /* Copy object to another location with wrong hash */
107 cl_git_pass(git_str_joinpath(&oldpath
, git_repository_path(g_repo
), oldloose
));
108 cl_git_pass(git_str_joinpath(&newpath
, git_repository_path(g_repo
), newloose
));
109 cl_git_pass(git_futils_cp(oldpath
.ptr
, newpath
.ptr
, 0644));
111 /* 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
));
114 /* Disable verification and try again */
115 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 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 1));
119 git_object_free(object
);
120 git_str_dispose(&oldpath
);
121 git_str_dispose(&newpath
);