]> git.proxmox.com Git - libgit2.git/blame - tests/odb/mixed.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / odb / mixed.c
CommitLineData
24634c6f
HWN
1#include "clar_libgit2.h"
2#include "odb.h"
24634c6f
HWN
3
4static git_odb *_odb;
5
6void test_odb_mixed__initialize(void)
7{
8 cl_git_pass(git_odb_open(&_odb, cl_fixture("duplicate.git/objects")));
9}
10
11void test_odb_mixed__cleanup(void)
12{
13 git_odb_free(_odb);
9094d30b 14 _odb = NULL;
24634c6f
HWN
15}
16
17void test_odb_mixed__dup_oid(void) {
18 const char hex[] = "ce013625030ba8dba906f756967f9e9ca394464a";
d19bcb33 19 const char short_hex[] = "ce01362";
24634c6f
HWN
20 git_oid oid;
21 git_odb_object *obj;
437224b4 22
24634c6f
HWN
23 cl_git_pass(git_oid_fromstr(&oid, hex));
24 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, GIT_OID_HEXSZ));
e54cfb9b 25 git_odb_object_free(obj);
f5753999
RB
26
27 cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, GIT_OID_HEXSZ));
28
d19bcb33
BR
29 cl_git_pass(git_oid_fromstrn(&oid, short_hex, sizeof(short_hex) - 1));
30 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, sizeof(short_hex) - 1));
24634c6f 31 git_odb_object_free(obj);
f5753999
RB
32
33 cl_git_pass(git_odb_exists_prefix(NULL, _odb, &oid, sizeof(short_hex) - 1));
24634c6f
HWN
34}
35
437224b4
RB
36/* some known sha collisions of file content:
37 * 'aabqhq' and 'aaazvc' with prefix 'dea509d0' (+ '9' and + 'b')
38 * 'aaeufo' and 'aaaohs' with prefix '81b5bff5' (+ 'f' and + 'b')
39 * 'aafewy' and 'aaepta' with prefix '739e3c4c'
40 * 'aahsyn' and 'aadrjg' with prefix '0ddeaded' (+ '9' and + 'e')
41 */
42
43void test_odb_mixed__dup_oid_prefix_0(void) {
44 char hex[10];
f5753999 45 git_oid oid, found;
437224b4
RB
46 git_odb_object *obj;
47
48 /* ambiguous in the same pack file */
49
50 strncpy(hex, "dea509d0", sizeof(hex));
51 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
52 cl_assert_equal_i(
53 GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999
RB
54 cl_assert_equal_i(
55 GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
437224b4
RB
56
57 strncpy(hex, "dea509d09", sizeof(hex));
58 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
59 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999 60 cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
0cee70eb 61 cl_assert_equal_oid(&found, git_odb_object_id(obj));
437224b4
RB
62 git_odb_object_free(obj);
63
64 strncpy(hex, "dea509d0b", sizeof(hex));
65 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
66 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
67 git_odb_object_free(obj);
68
69 /* ambiguous in different pack files */
70
71 strncpy(hex, "81b5bff5", sizeof(hex));
72 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
73 cl_assert_equal_i(
74 GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999
RB
75 cl_assert_equal_i(
76 GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
437224b4
RB
77
78 strncpy(hex, "81b5bff5b", sizeof(hex));
79 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
80 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999 81 cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
0cee70eb 82 cl_assert_equal_oid(&found, git_odb_object_id(obj));
437224b4
RB
83 git_odb_object_free(obj);
84
85 strncpy(hex, "81b5bff5f", sizeof(hex));
86 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
87 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
88 git_odb_object_free(obj);
89
90 /* ambiguous in pack file and loose */
91
92 strncpy(hex, "0ddeaded", sizeof(hex));
93 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
94 cl_assert_equal_i(
95 GIT_EAMBIGUOUS, git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999
RB
96 cl_assert_equal_i(
97 GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
437224b4
RB
98
99 strncpy(hex, "0ddeaded9", sizeof(hex));
100 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
101 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
f5753999 102 cl_git_pass(git_odb_exists_prefix(&found, _odb, &oid, strlen(hex)));
0cee70eb 103 cl_assert_equal_oid(&found, git_odb_object_id(obj));
437224b4
RB
104 git_odb_object_free(obj);
105
106 strncpy(hex, "0ddeadede", sizeof(hex));
107 cl_git_pass(git_oid_fromstrn(&oid, hex, strlen(hex)));
108 cl_git_pass(git_odb_read_prefix(&obj, _odb, &oid, strlen(hex)));
109 git_odb_object_free(obj);
110}
6c04269c 111
4b1f0f79 112struct expand_id_test_data {
6c04269c
ET
113 char *lookup_id;
114 char *expected_id;
ac3d33df 115 git_object_t expected_type;
6c04269c
ET
116};
117
4b1f0f79 118struct expand_id_test_data expand_id_test_data[] = {
6c04269c 119 /* some prefixes and their expected values */
ac3d33df
JK
120 { "dea509d0", NULL, GIT_OBJECT_ANY },
121 { "00000000", NULL, GIT_OBJECT_ANY },
122 { "dea509d0", NULL, GIT_OBJECT_ANY },
123 { "dea509d09", "dea509d097ce692e167dfc6a48a7a280cc5e877e", GIT_OBJECT_BLOB },
124 { "dea509d0b", "dea509d0b3cb8ee0650f6ca210bc83f4678851ba", GIT_OBJECT_BLOB },
125 { "ce0136250", "ce013625030ba8dba906f756967f9e9ca394464a", GIT_OBJECT_BLOB },
126 { "0ddeaded", NULL, GIT_OBJECT_ANY },
127 { "4d5979b", "4d5979b468252190cb572ae758aca36928e8a91e", GIT_OBJECT_TREE },
128 { "0ddeaded", NULL, GIT_OBJECT_ANY },
129 { "0ddeadede", "0ddeadede9e6d6ccddce0ee1e5749eed0485e5ea", GIT_OBJECT_BLOB },
130 { "0ddeaded9", "0ddeaded9502971eefe1e41e34d0e536853ae20f", GIT_OBJECT_BLOB },
131 { "f00b4e", NULL, GIT_OBJECT_ANY },
6c04269c 132
9a786650 133 /* this OID is too short and should be ambiguous! */
ac3d33df 134 { "f00", NULL, GIT_OBJECT_ANY },
9a786650 135
6c04269c 136 /* some full-length object ids */
ac3d33df 137 { "0000000000000000000000000000000000000000", NULL, GIT_OBJECT_ANY },
6c04269c
ET
138 {
139 "dea509d097ce692e167dfc6a48a7a280cc5e877e",
140 "dea509d097ce692e167dfc6a48a7a280cc5e877e",
ac3d33df 141 GIT_OBJECT_BLOB
6c04269c 142 },
ac3d33df 143 { "f00f00f00f00f00f00f00f00f00f00f00f00f00f", NULL, GIT_OBJECT_ANY },
6c04269c
ET
144 {
145 "4d5979b468252190cb572ae758aca36928e8a91e",
146 "4d5979b468252190cb572ae758aca36928e8a91e",
ac3d33df 147 GIT_OBJECT_TREE
6c04269c 148 },
9a786650
VM
149
150 /*
151 * ensure we're not leaking the return error code for the
152 * last lookup if the last object is invalid
153 */
ac3d33df 154 { "0ddeadedfff", NULL, GIT_OBJECT_ANY },
6c04269c
ET
155};
156
157static void setup_prefix_query(
62484f52 158 git_odb_expand_id **out_ids,
6c04269c
ET
159 size_t *out_num)
160{
62484f52
ET
161 git_odb_expand_id *ids;
162 size_t num, i;
6c04269c 163
4b1f0f79 164 num = ARRAY_SIZE(expand_id_test_data);
6c04269c 165
62484f52 166 cl_assert((ids = git__calloc(num, sizeof(git_odb_expand_id))));
6c04269c
ET
167
168 for (i = 0; i < num; i++) {
62484f52
ET
169 git_odb_expand_id *id = &ids[i];
170
171 size_t len = strlen(expand_id_test_data[i].lookup_id);
172
173 git_oid_fromstrn(&id->id, expand_id_test_data[i].lookup_id, len);
174 id->length = (unsigned short)len;
175 id->type = expand_id_test_data[i].expected_type;
6c04269c
ET
176 }
177
178 *out_ids = ids;
6c04269c
ET
179 *out_num = num;
180}
181
62484f52 182static void assert_found_objects(git_odb_expand_id *ids)
6c04269c
ET
183{
184 size_t num, i;
185
4b1f0f79 186 num = ARRAY_SIZE(expand_id_test_data);
6c04269c
ET
187
188 for (i = 0; i < num; i++) {
189 git_oid expected_id = {{0}};
190 size_t expected_len = 0;
ac3d33df 191 git_object_t expected_type = 0;
6c04269c 192
4b1f0f79
ET
193 if (expand_id_test_data[i].expected_id) {
194 git_oid_fromstr(&expected_id, expand_id_test_data[i].expected_id);
6c04269c 195 expected_len = GIT_OID_HEXSZ;
4b1f0f79 196 expected_type = expand_id_test_data[i].expected_type;
6c04269c
ET
197 }
198
62484f52
ET
199 cl_assert_equal_oid(&expected_id, &ids[i].id);
200 cl_assert_equal_i(expected_len, ids[i].length);
201 cl_assert_equal_i(expected_type, ids[i].type);
6c04269c
ET
202 }
203}
204
62484f52 205static void assert_notfound_objects(git_odb_expand_id *ids)
6c04269c
ET
206{
207 git_oid expected_id = {{0}};
208 size_t num, i;
209
4b1f0f79 210 num = ARRAY_SIZE(expand_id_test_data);
6c04269c
ET
211
212 for (i = 0; i < num; i++) {
62484f52
ET
213 cl_assert_equal_oid(&expected_id, &ids[i].id);
214 cl_assert_equal_i(0, ids[i].length);
215 cl_assert_equal_i(0, ids[i].type);
6c04269c
ET
216 }
217}
218
4b1f0f79 219void test_odb_mixed__expand_ids(void)
6c04269c 220{
62484f52
ET
221 git_odb_expand_id *ids;
222 size_t i, num;
6c04269c
ET
223
224 /* test looking for the actual (correct) types */
225
62484f52
ET
226 setup_prefix_query(&ids, &num);
227 cl_git_pass(git_odb_expand_ids(_odb, ids, num));
228 assert_found_objects(ids);
229 git__free(ids);
230
231 /* test looking for an explicit `type == 0` */
6c04269c 232
62484f52
ET
233 setup_prefix_query(&ids, &num);
234
235 for (i = 0; i < num; i++)
236 ids[i].type = 0;
6c04269c 237
62484f52
ET
238 cl_git_pass(git_odb_expand_ids(_odb, ids, num));
239 assert_found_objects(ids);
240 git__free(ids);
6c04269c 241
ac3d33df 242 /* test looking for an explicit GIT_OBJECT_ANY */
6c04269c 243
62484f52 244 setup_prefix_query(&ids, &num);
6c04269c
ET
245
246 for (i = 0; i < num; i++)
ac3d33df 247 ids[i].type = GIT_OBJECT_ANY;
6c04269c 248
62484f52
ET
249 cl_git_pass(git_odb_expand_ids(_odb, ids, num));
250 assert_found_objects(ids);
251 git__free(ids);
6c04269c
ET
252
253 /* test looking for the completely wrong type */
254
62484f52 255 setup_prefix_query(&ids, &num);
6c04269c
ET
256
257 for (i = 0; i < num; i++)
ac3d33df
JK
258 ids[i].type = (ids[i].type == GIT_OBJECT_BLOB) ?
259 GIT_OBJECT_TREE : GIT_OBJECT_BLOB;
6c04269c 260
62484f52
ET
261 cl_git_pass(git_odb_expand_ids(_odb, ids, num));
262 assert_notfound_objects(ids);
263 git__free(ids);
6c04269c
ET
264}
265
6147f643
PP
266void test_odb_mixed__expand_ids_cached(void)
267{
268 git_odb_expand_id *ids;
269 size_t i, num;
270
271 /* test looking for the actual (correct) types after accessing the object */
272
273 setup_prefix_query(&ids, &num);
274
275 for (i = 0; i < num; i++) {
276 git_odb_object *obj;
277 if (ids[i].type == GIT_OBJECT_ANY)
278 continue;
279 cl_git_pass(git_odb_read_prefix(&obj, _odb, &ids[i].id, ids[i].length));
280 git_odb_object_free(obj);
281 }
282
283 cl_git_pass(git_odb_expand_ids(_odb, ids, num));
284 assert_found_objects(ids);
285 git__free(ids);
286}