]> git.proxmox.com Git - libgit2.git/blame - tests/odb/backend/simple.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / odb / backend / simple.c
CommitLineData
a180e7d9
PS
1#include "clar_libgit2.h"
2#include "repository.h"
3#include "backend_helpers.h"
4
5#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
6
7static git_repository *_repo;
8static git_odb *_odb;
9static git_odb_object *_obj;
10static git_oid _oid;
11
12static void setup_backend(const fake_object *objs)
13{
14 git_odb_backend *backend;
15
e579e0f7 16 cl_git_pass(build_fake_backend(&backend, objs, false));
a180e7d9
PS
17
18 cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
19 cl_git_pass(git_odb_add_backend(_odb, backend, 10));
20}
21
22static void assert_object_contains(git_odb_object *obj, const char *expected)
23{
24 const char *actual = (const char *) git_odb_object_data(obj);
25
26 cl_assert_equal_s(actual, expected);
27}
28
29void test_odb_backend_simple__initialize(void)
30{
31 _repo = cl_git_sandbox_init("testrepo.git");
32 _odb = NULL;
33 _obj = NULL;
34}
35
36void test_odb_backend_simple__cleanup(void)
37{
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));
41}
42
43void test_odb_backend_simple__read_of_object_succeeds(void)
44{
45 const fake_object objs[] = {
46 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
47 { NULL, NULL }
48 };
49
50 setup_backend(objs);
51
52 cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
53 cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
54
55 assert_object_contains(_obj, objs[0].content);
56}
57
58void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
59{
60 const fake_object objs[] = {
61 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
62 { NULL, NULL }
63 };
64
65 setup_backend(objs);
66
67 cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
68 cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
69}
70
71void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
72{
73 const fake_object objs[] = {
74 { "1234567890123456789012345678901234567890", "nonmatching content" },
75 { NULL, NULL }
76 };
77
78 setup_backend(objs);
79
80 cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
81 cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
82}
83
84void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
85{
86 const fake_object objs[] = {
87 { "1234567890123456789012345678901234567890", "nonmatching content" },
88 { NULL, NULL }
89 };
90
91 setup_backend(objs);
92 cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
93
94 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
95 cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
96
97 assert_object_contains(_obj, objs[0].content);
98}
99
100void test_odb_backend_simple__read_prefix_succeeds(void)
101{
102 const fake_object objs[] = {
103 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
104 { NULL, NULL }
105 };
106
107 setup_backend(objs);
108
109 cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632"));
110 cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
111
112 assert_object_contains(_obj, objs[0].content);
113}
114
115void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
116{
117 const fake_object objs[] = {
118 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
119 { NULL, NULL }
120 };
121 char *hash = "f6ea0495187600e8";
122
123 setup_backend(objs);
124
125 cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash)));
126 cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
127}
128
129void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
130{
131 const fake_object objs[] = {
132 { "1234567890111111111111111111111111111111", "first content" },
133 { "1234567890222222222222222222222222222222", "second content" },
134 { NULL, NULL }
135 };
136
137 setup_backend(objs);
138
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));
141}
142
143void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
144{
145 const fake_object objs[] = {
146 { "1234567890111111111111111111111111111111", "first content" },
147 { "1234567890111111111111111111111111111112", "second content" },
148 { NULL, NULL }
149 };
150
151 setup_backend(objs);
152
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);
158}
159
160void test_odb_backend_simple__exists_succeeds(void)
161{
162 const fake_object objs[] = {
163 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
164 { NULL, NULL }
165 };
166
167 setup_backend(objs);
168
169 cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
170 cl_assert(git_odb_exists(_odb, &_oid));
171}
172
173void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
174{
175 const fake_object objs[] = {
176 { "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
177 { NULL, NULL }
178 };
179
180 setup_backend(objs);
181
182 cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
183 cl_assert(git_odb_exists(_odb, &_oid) == 0);
184}
185
186void test_odb_backend_simple__exists_prefix_succeeds(void)
187{
188 const fake_object objs[] = {
189 { "1234567890111111111111111111111111111111", "first content" },
190 { "1234567890222222222222222222222222222222", "second content" },
191 { NULL, NULL }
192 };
193 git_oid found;
194
195 setup_backend(objs);
196
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));
200}
201
202void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
203{
204 const fake_object objs[] = {
205 { "1234567890111111111111111111111111111111", "first content" },
206 { "1234567890222222222222222222222222222222", "second content" },
207 { NULL, NULL }
208 };
209
210 setup_backend(objs);
211
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));
214}
215
216void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
217{
218 const fake_object objs[] = {
219 { "1234567890111111111111111111111111111111", "first content" },
220 { "1234567890111111111111111111111111111112", "second content" },
221 { NULL, NULL }
222 };
223 git_oid found;
224
225 setup_backend(objs);
226
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));
232}
eae0bfdc
PP
233
234void test_odb_backend_simple__null_oid_is_ignored(void)
235{
236 const fake_object objs[] = {
237 { "0000000000000000000000000000000000000000", "null oid content" },
238 { NULL, NULL }
239 };
240 git_oid null_oid = {{0}};
241 git_odb_object *obj;
242
243 setup_backend(objs);
244
245 cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
246 cl_assert(!git_odb_exists(_odb, &null_oid));
247
248 cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&obj, _odb, &null_oid));
ac3d33df 249 cl_assert(git_error_last() && strstr(git_error_last()->message, "null OID"));
eae0bfdc 250}