]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/record_test.c
submodule-config.c: strengthen URL fsck check
[thirdparty/git.git] / reftable / record_test.c
1 /*
2 Copyright 2020 Google LLC
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
8
9 #include "record.h"
10
11 #include "system.h"
12 #include "basics.h"
13 #include "constants.h"
14 #include "test_framework.h"
15 #include "reftable-tests.h"
16
17 static void test_copy(struct reftable_record *rec)
18 {
19 struct reftable_record copy = { 0 };
20 uint8_t typ;
21
22 typ = reftable_record_type(rec);
23 copy = reftable_new_record(typ);
24 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
25 /* do it twice to catch memory leaks */
26 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
27 EXPECT(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));
28
29 puts("testing print coverage:\n");
30 reftable_record_print(&copy, GIT_SHA1_RAWSZ);
31
32 reftable_record_release(&copy);
33 }
34
35 static void test_varint_roundtrip(void)
36 {
37 uint64_t inputs[] = { 0,
38 1,
39 27,
40 127,
41 128,
42 257,
43 4096,
44 ((uint64_t)1 << 63),
45 ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
46 int i = 0;
47 for (i = 0; i < ARRAY_SIZE(inputs); i++) {
48 uint8_t dest[10];
49
50 struct string_view out = {
51 .buf = dest,
52 .len = sizeof(dest),
53 };
54 uint64_t in = inputs[i];
55 int n = put_var_int(&out, in);
56 uint64_t got = 0;
57
58 EXPECT(n > 0);
59 out.len = n;
60 n = get_var_int(&got, &out);
61 EXPECT(n > 0);
62
63 EXPECT(got == in);
64 }
65 }
66
67 static void test_common_prefix(void)
68 {
69 struct {
70 const char *a, *b;
71 int want;
72 } cases[] = {
73 { "abc", "ab", 2 },
74 { "", "abc", 0 },
75 { "abc", "abd", 2 },
76 { "abc", "pqr", 0 },
77 };
78
79 int i = 0;
80 for (i = 0; i < ARRAY_SIZE(cases); i++) {
81 struct strbuf a = STRBUF_INIT;
82 struct strbuf b = STRBUF_INIT;
83 strbuf_addstr(&a, cases[i].a);
84 strbuf_addstr(&b, cases[i].b);
85 EXPECT(common_prefix_size(&a, &b) == cases[i].want);
86
87 strbuf_release(&a);
88 strbuf_release(&b);
89 }
90 }
91
92 static void set_hash(uint8_t *h, int j)
93 {
94 int i = 0;
95 for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
96 h[i] = (j >> i) & 0xff;
97 }
98 }
99
100 static void test_reftable_ref_record_roundtrip(void)
101 {
102 int i = 0;
103
104 for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
105 struct reftable_record in = {
106 .type = BLOCK_TYPE_REF,
107 };
108 struct reftable_record out = { .type = BLOCK_TYPE_REF };
109 struct strbuf key = STRBUF_INIT;
110 uint8_t buffer[1024] = { 0 };
111 struct string_view dest = {
112 .buf = buffer,
113 .len = sizeof(buffer),
114 };
115 int n, m;
116
117 in.u.ref.value_type = i;
118 switch (i) {
119 case REFTABLE_REF_DELETION:
120 break;
121 case REFTABLE_REF_VAL1:
122 in.u.ref.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ);
123 set_hash(in.u.ref.value.val1, 1);
124 break;
125 case REFTABLE_REF_VAL2:
126 in.u.ref.value.val2.value =
127 reftable_malloc(GIT_SHA1_RAWSZ);
128 set_hash(in.u.ref.value.val2.value, 1);
129 in.u.ref.value.val2.target_value =
130 reftable_malloc(GIT_SHA1_RAWSZ);
131 set_hash(in.u.ref.value.val2.target_value, 2);
132 break;
133 case REFTABLE_REF_SYMREF:
134 in.u.ref.value.symref = xstrdup("target");
135 break;
136 }
137 in.u.ref.refname = xstrdup("refs/heads/master");
138
139 test_copy(&in);
140
141 EXPECT(reftable_record_val_type(&in) == i);
142
143 reftable_record_key(&in, &key);
144 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
145 EXPECT(n > 0);
146
147 /* decode into a non-zero reftable_record to test for leaks. */
148 m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ);
149 EXPECT(n == m);
150
151 EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
152 GIT_SHA1_RAWSZ));
153 reftable_record_release(&in);
154
155 strbuf_release(&key);
156 reftable_record_release(&out);
157 }
158 }
159
160 static void test_reftable_log_record_equal(void)
161 {
162 struct reftable_log_record in[2] = {
163 {
164 .refname = xstrdup("refs/heads/master"),
165 .update_index = 42,
166 },
167 {
168 .refname = xstrdup("refs/heads/master"),
169 .update_index = 22,
170 }
171 };
172
173 EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
174 in[1].update_index = in[0].update_index;
175 EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
176 reftable_log_record_release(&in[0]);
177 reftable_log_record_release(&in[1]);
178 }
179
180 static void test_reftable_log_record_roundtrip(void)
181 {
182 int i;
183
184 struct reftable_log_record in[] = {
185 {
186 .refname = xstrdup("refs/heads/master"),
187 .update_index = 42,
188 .value_type = REFTABLE_LOG_UPDATE,
189 .value = {
190 .update = {
191 .old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
192 .new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
193 .name = xstrdup("han-wen"),
194 .email = xstrdup("hanwen@google.com"),
195 .message = xstrdup("test"),
196 .time = 1577123507,
197 .tz_offset = 100,
198 },
199 }
200 },
201 {
202 .refname = xstrdup("refs/heads/master"),
203 .update_index = 22,
204 .value_type = REFTABLE_LOG_DELETION,
205 },
206 {
207 .refname = xstrdup("branch"),
208 .update_index = 33,
209 .value_type = REFTABLE_LOG_UPDATE,
210 .value = {
211 .update = {
212 .old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
213 .new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
214 /* rest of fields left empty. */
215 },
216 },
217 }
218 };
219 set_test_hash(in[0].value.update.new_hash, 1);
220 set_test_hash(in[0].value.update.old_hash, 2);
221 set_test_hash(in[2].value.update.new_hash, 3);
222 set_test_hash(in[2].value.update.old_hash, 4);
223 for (i = 0; i < ARRAY_SIZE(in); i++) {
224 struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
225 struct strbuf key = STRBUF_INIT;
226 uint8_t buffer[1024] = { 0 };
227 struct string_view dest = {
228 .buf = buffer,
229 .len = sizeof(buffer),
230 };
231 /* populate out, to check for leaks. */
232 struct reftable_record out = {
233 .type = BLOCK_TYPE_LOG,
234 .u.log = {
235 .refname = xstrdup("old name"),
236 .value_type = REFTABLE_LOG_UPDATE,
237 .value = {
238 .update = {
239 .new_hash = reftable_calloc(GIT_SHA1_RAWSZ),
240 .old_hash = reftable_calloc(GIT_SHA1_RAWSZ),
241 .name = xstrdup("old name"),
242 .email = xstrdup("old@email"),
243 .message = xstrdup("old message"),
244 },
245 },
246 },
247 };
248 int n, m, valtype;
249
250 rec.u.log = in[i];
251
252 test_copy(&rec);
253
254 reftable_record_key(&rec, &key);
255
256 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
257 EXPECT(n >= 0);
258 valtype = reftable_record_val_type(&rec);
259 m = reftable_record_decode(&out, key, valtype, dest,
260 GIT_SHA1_RAWSZ);
261 EXPECT(n == m);
262
263 EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
264 GIT_SHA1_RAWSZ));
265 reftable_log_record_release(&in[i]);
266 strbuf_release(&key);
267 reftable_record_release(&out);
268 }
269 }
270
271 static void test_u24_roundtrip(void)
272 {
273 uint32_t in = 0x112233;
274 uint8_t dest[3];
275 uint32_t out;
276 put_be24(dest, in);
277 out = get_be24(dest);
278 EXPECT(in == out);
279 }
280
281 static void test_key_roundtrip(void)
282 {
283 uint8_t buffer[1024] = { 0 };
284 struct string_view dest = {
285 .buf = buffer,
286 .len = sizeof(buffer),
287 };
288 struct strbuf last_key = STRBUF_INIT;
289 struct strbuf key = STRBUF_INIT;
290 struct strbuf roundtrip = STRBUF_INIT;
291 int restart;
292 uint8_t extra;
293 int n, m;
294 uint8_t rt_extra;
295
296 strbuf_addstr(&last_key, "refs/heads/master");
297 strbuf_addstr(&key, "refs/tags/bla");
298 extra = 6;
299 n = reftable_encode_key(&restart, dest, last_key, key, extra);
300 EXPECT(!restart);
301 EXPECT(n > 0);
302
303 m = reftable_decode_key(&roundtrip, &rt_extra, last_key, dest);
304 EXPECT(n == m);
305 EXPECT(0 == strbuf_cmp(&key, &roundtrip));
306 EXPECT(rt_extra == extra);
307
308 strbuf_release(&last_key);
309 strbuf_release(&key);
310 strbuf_release(&roundtrip);
311 }
312
313 static void test_reftable_obj_record_roundtrip(void)
314 {
315 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
316 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
317 struct reftable_obj_record recs[3] = { {
318 .hash_prefix = testHash1,
319 .hash_prefix_len = 5,
320 .offsets = till9,
321 .offset_len = 3,
322 },
323 {
324 .hash_prefix = testHash1,
325 .hash_prefix_len = 5,
326 .offsets = till9,
327 .offset_len = 9,
328 },
329 {
330 .hash_prefix = testHash1,
331 .hash_prefix_len = 5,
332 } };
333 int i = 0;
334 for (i = 0; i < ARRAY_SIZE(recs); i++) {
335 uint8_t buffer[1024] = { 0 };
336 struct string_view dest = {
337 .buf = buffer,
338 .len = sizeof(buffer),
339 };
340 struct reftable_record in = {
341 .type = BLOCK_TYPE_OBJ,
342 .u = {
343 .obj = recs[i],
344 },
345 };
346 struct strbuf key = STRBUF_INIT;
347 struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
348 int n, m;
349 uint8_t extra;
350
351 test_copy(&in);
352 reftable_record_key(&in, &key);
353 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
354 EXPECT(n > 0);
355 extra = reftable_record_val_type(&in);
356 m = reftable_record_decode(&out, key, extra, dest,
357 GIT_SHA1_RAWSZ);
358 EXPECT(n == m);
359
360 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
361 strbuf_release(&key);
362 reftable_record_release(&out);
363 }
364 }
365
366 static void test_reftable_index_record_roundtrip(void)
367 {
368 struct reftable_record in = {
369 .type = BLOCK_TYPE_INDEX,
370 .u.idx = {
371 .offset = 42,
372 .last_key = STRBUF_INIT,
373 },
374 };
375 uint8_t buffer[1024] = { 0 };
376 struct string_view dest = {
377 .buf = buffer,
378 .len = sizeof(buffer),
379 };
380 struct strbuf key = STRBUF_INIT;
381 struct reftable_record out = {
382 .type = BLOCK_TYPE_INDEX,
383 .u.idx = { .last_key = STRBUF_INIT },
384 };
385 int n, m;
386 uint8_t extra;
387
388 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
389 reftable_record_key(&in, &key);
390 test_copy(&in);
391
392 EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
393 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
394 EXPECT(n > 0);
395
396 extra = reftable_record_val_type(&in);
397 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ);
398 EXPECT(m == n);
399
400 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
401
402 reftable_record_release(&out);
403 strbuf_release(&key);
404 strbuf_release(&in.u.idx.last_key);
405 }
406
407 int record_test_main(int argc, const char *argv[])
408 {
409 RUN_TEST(test_reftable_log_record_equal);
410 RUN_TEST(test_reftable_log_record_roundtrip);
411 RUN_TEST(test_reftable_ref_record_roundtrip);
412 RUN_TEST(test_varint_roundtrip);
413 RUN_TEST(test_key_roundtrip);
414 RUN_TEST(test_common_prefix);
415 RUN_TEST(test_reftable_obj_record_roundtrip);
416 RUN_TEST(test_reftable_index_record_roundtrip);
417 RUN_TEST(test_u24_roundtrip);
418 return 0;
419 }