1 #include "clar_libgit2.h"
2 #include "repository.h"
3 #include "backend_helpers.h"
5 #define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
7 static git_repository
*_repo
;
9 static git_odb_object
*_obj
;
12 static void setup_backend(const fake_object
*objs
)
14 git_odb_backend
*backend
;
16 cl_git_pass(build_fake_backend(&backend
, objs
));
18 cl_git_pass(git_repository_odb__weakptr(&_odb
, _repo
));
19 cl_git_pass(git_odb_add_backend(_odb
, backend
, 10));
22 static void assert_object_contains(git_odb_object
*obj
, const char *expected
)
24 const char *actual
= (const char *) git_odb_object_data(obj
);
26 cl_assert_equal_s(actual
, expected
);
29 void test_odb_backend_simple__initialize(void)
31 _repo
= cl_git_sandbox_init("testrepo.git");
36 void test_odb_backend_simple__cleanup(void)
38 git_odb_object_free(_obj
);
39 cl_git_sandbox_cleanup();
40 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 1));
43 void test_odb_backend_simple__read_of_object_succeeds(void)
45 const fake_object objs
[] = {
46 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
52 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
53 cl_git_pass(git_odb_read(&_obj
, _odb
, &_oid
));
55 assert_object_contains(_obj
, objs
[0].content
);
58 void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
60 const fake_object objs
[] = {
61 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
67 cl_git_pass(git_oid_fromstr(&_oid
, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
68 cl_git_fail_with(GIT_ENOTFOUND
, git_odb_read(&_obj
, _odb
, &_oid
));
71 void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
73 const fake_object objs
[] = {
74 { "1234567890123456789012345678901234567890", "nonmatching content" },
80 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
81 cl_git_fail_with(GIT_EMISMATCH
, git_odb_read(&_obj
, _odb
, &_oid
));
84 void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
86 const fake_object objs
[] = {
87 { "1234567890123456789012345678901234567890", "nonmatching content" },
92 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
94 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 0));
95 cl_git_pass(git_odb_read(&_obj
, _odb
, &_oid
));
97 assert_object_contains(_obj
, objs
[0].content
);
100 void test_odb_backend_simple__read_prefix_succeeds(void)
102 const fake_object objs
[] = {
103 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
109 cl_git_pass(git_oid_fromstr(&_oid
, "f6ea0495187600e7b2288c8ac19c5886383a4632"));
110 cl_git_pass(git_odb_read(&_obj
, _odb
, &_oid
));
112 assert_object_contains(_obj
, objs
[0].content
);
115 void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
117 const fake_object objs
[] = {
118 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
121 char *hash
= "f6ea0495187600e8";
125 cl_git_pass(git_oid_fromstrn(&_oid
, hash
, strlen(hash
)));
126 cl_git_fail_with(GIT_ENOTFOUND
, git_odb_read(&_obj
, _odb
, &_oid
));
129 void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
131 const fake_object objs
[] = {
132 { "1234567890111111111111111111111111111111", "first content" },
133 { "1234567890222222222222222222222222222222", "second content" },
139 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
140 cl_git_fail_with(GIT_EAMBIGUOUS
, git_odb_read_prefix(&_obj
, _odb
, &_oid
, 7));
143 void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
145 const fake_object objs
[] = {
146 { "1234567890111111111111111111111111111111", "first content" },
147 { "1234567890111111111111111111111111111112", "second content" },
153 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
154 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 0));
155 cl_git_fail_with(GIT_EAMBIGUOUS
, git_odb_read_prefix(&_obj
, _odb
, &_oid
, 39));
156 cl_git_pass(git_odb_read_prefix(&_obj
, _odb
, &_oid
, 40));
157 assert_object_contains(_obj
, objs
[0].content
);
160 void test_odb_backend_simple__exists_succeeds(void)
162 const fake_object objs
[] = {
163 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
169 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
170 cl_assert(git_odb_exists(_odb
, &_oid
));
173 void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
175 const fake_object objs
[] = {
176 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
182 cl_git_pass(git_oid_fromstr(&_oid
, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
183 cl_assert(git_odb_exists(_odb
, &_oid
) == 0);
186 void test_odb_backend_simple__exists_prefix_succeeds(void)
188 const fake_object objs
[] = {
189 { "1234567890111111111111111111111111111111", "first content" },
190 { "1234567890222222222222222222222222222222", "second content" },
197 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
198 cl_git_pass(git_odb_exists_prefix(&found
, _odb
, &_oid
, 12));
199 cl_assert(git_oid_equal(&found
, &_oid
));
202 void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
204 const fake_object objs
[] = {
205 { "1234567890111111111111111111111111111111", "first content" },
206 { "1234567890222222222222222222222222222222", "second content" },
212 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
213 cl_git_fail_with(GIT_EAMBIGUOUS
, git_odb_exists_prefix(NULL
, _odb
, &_oid
, 7));
216 void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
218 const fake_object objs
[] = {
219 { "1234567890111111111111111111111111111111", "first content" },
220 { "1234567890111111111111111111111111111112", "second content" },
227 cl_git_pass(git_oid_fromstr(&_oid
, objs
[0].oid
));
228 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 0));
229 cl_git_fail_with(GIT_EAMBIGUOUS
, git_odb_exists_prefix(&found
, _odb
, &_oid
, 39));
230 cl_git_pass(git_odb_exists_prefix(&found
, _odb
, &_oid
, 40));
231 cl_assert(git_oid_equal(&found
, &_oid
));
234 void test_odb_backend_simple__null_oid_is_ignored(void)
236 const fake_object objs
[] = {
237 { "0000000000000000000000000000000000000000", "null oid content" },
240 git_oid null_oid
= {{0}};
245 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION
, 0));
246 cl_assert(!git_odb_exists(_odb
, &null_oid
));
248 cl_git_fail_with(GIT_ENOTFOUND
, git_odb_read(&obj
, _odb
, &null_oid
));
249 cl_assert(git_error_last() && strstr(git_error_last()->message
, "null OID"));