refs/reftable: print errors on compaction failure
[alt-git.git] / reftable / record_test.c
blob89209894d84395ce9ae476ba461fea04100c05a6
1 /*
2 Copyright 2020 Google LLC
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 */
9 #include "record.h"
11 #include "system.h"
12 #include "basics.h"
13 #include "constants.h"
14 #include "test_framework.h"
15 #include "reftable-tests.h"
17 static void test_copy(struct reftable_record *rec)
19 struct reftable_record copy;
20 uint8_t typ;
22 typ = reftable_record_type(rec);
23 reftable_record_init(&copy, 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));
29 puts("testing print coverage:\n");
30 reftable_record_print(&copy, GIT_SHA1_RAWSZ);
32 reftable_record_release(&copy);
35 static void test_varint_roundtrip(void)
37 uint64_t inputs[] = { 0,
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];
50 struct string_view out = {
51 .buf = dest,
52 .len = sizeof(dest),
54 uint64_t in = inputs[i];
55 int n = put_var_int(&out, in);
56 uint64_t got = 0;
58 EXPECT(n > 0);
59 out.len = n;
60 n = get_var_int(&got, &out);
61 EXPECT(n > 0);
63 EXPECT(got == in);
67 static void test_common_prefix(void)
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 },
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);
87 strbuf_release(&a);
88 strbuf_release(&b);
92 static void set_hash(uint8_t *h, int j)
94 int i = 0;
95 for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
96 h[i] = (j >> i) & 0xff;
100 static void test_reftable_ref_record_roundtrip(void)
102 int i = 0;
104 for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
105 struct reftable_record in = {
106 .type = BLOCK_TYPE_REF,
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),
115 int n, m;
117 in.u.ref.value_type = i;
118 switch (i) {
119 case REFTABLE_REF_DELETION:
120 break;
121 case REFTABLE_REF_VAL1:
122 set_hash(in.u.ref.value.val1, 1);
123 break;
124 case REFTABLE_REF_VAL2:
125 set_hash(in.u.ref.value.val2.value, 1);
126 set_hash(in.u.ref.value.val2.target_value, 2);
127 break;
128 case REFTABLE_REF_SYMREF:
129 in.u.ref.value.symref = xstrdup("target");
130 break;
132 in.u.ref.refname = xstrdup("refs/heads/master");
134 test_copy(&in);
136 EXPECT(reftable_record_val_type(&in) == i);
138 reftable_record_key(&in, &key);
139 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
140 EXPECT(n > 0);
142 /* decode into a non-zero reftable_record to test for leaks. */
143 m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ);
144 EXPECT(n == m);
146 EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
147 GIT_SHA1_RAWSZ));
148 reftable_record_release(&in);
150 strbuf_release(&key);
151 reftable_record_release(&out);
155 static void test_reftable_log_record_equal(void)
157 struct reftable_log_record in[2] = {
159 .refname = xstrdup("refs/heads/master"),
160 .update_index = 42,
163 .refname = xstrdup("refs/heads/master"),
164 .update_index = 22,
168 EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
169 in[1].update_index = in[0].update_index;
170 EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
171 reftable_log_record_release(&in[0]);
172 reftable_log_record_release(&in[1]);
175 static void test_reftable_log_record_roundtrip(void)
177 int i;
179 struct reftable_log_record in[] = {
181 .refname = xstrdup("refs/heads/master"),
182 .update_index = 42,
183 .value_type = REFTABLE_LOG_UPDATE,
184 .value = {
185 .update = {
186 .old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
187 .new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
188 .name = xstrdup("han-wen"),
189 .email = xstrdup("hanwen@google.com"),
190 .message = xstrdup("test"),
191 .time = 1577123507,
192 .tz_offset = 100,
197 .refname = xstrdup("refs/heads/master"),
198 .update_index = 22,
199 .value_type = REFTABLE_LOG_DELETION,
202 .refname = xstrdup("branch"),
203 .update_index = 33,
204 .value_type = REFTABLE_LOG_UPDATE,
205 .value = {
206 .update = {
207 .old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
208 .new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
209 /* rest of fields left empty. */
214 set_test_hash(in[0].value.update.new_hash, 1);
215 set_test_hash(in[0].value.update.old_hash, 2);
216 set_test_hash(in[2].value.update.new_hash, 3);
217 set_test_hash(in[2].value.update.old_hash, 4);
218 for (i = 0; i < ARRAY_SIZE(in); i++) {
219 struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
220 struct strbuf key = STRBUF_INIT;
221 uint8_t buffer[1024] = { 0 };
222 struct string_view dest = {
223 .buf = buffer,
224 .len = sizeof(buffer),
226 /* populate out, to check for leaks. */
227 struct reftable_record out = {
228 .type = BLOCK_TYPE_LOG,
229 .u.log = {
230 .refname = xstrdup("old name"),
231 .value_type = REFTABLE_LOG_UPDATE,
232 .value = {
233 .update = {
234 .new_hash = reftable_calloc(GIT_SHA1_RAWSZ, 1),
235 .old_hash = reftable_calloc(GIT_SHA1_RAWSZ, 1),
236 .name = xstrdup("old name"),
237 .email = xstrdup("old@email"),
238 .message = xstrdup("old message"),
243 int n, m, valtype;
245 rec.u.log = in[i];
247 test_copy(&rec);
249 reftable_record_key(&rec, &key);
251 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
252 EXPECT(n >= 0);
253 valtype = reftable_record_val_type(&rec);
254 m = reftable_record_decode(&out, key, valtype, dest,
255 GIT_SHA1_RAWSZ);
256 EXPECT(n == m);
258 EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
259 GIT_SHA1_RAWSZ));
260 reftable_log_record_release(&in[i]);
261 strbuf_release(&key);
262 reftable_record_release(&out);
266 static void test_u24_roundtrip(void)
268 uint32_t in = 0x112233;
269 uint8_t dest[3];
270 uint32_t out;
271 put_be24(dest, in);
272 out = get_be24(dest);
273 EXPECT(in == out);
276 static void test_key_roundtrip(void)
278 uint8_t buffer[1024] = { 0 };
279 struct string_view dest = {
280 .buf = buffer,
281 .len = sizeof(buffer),
283 struct strbuf last_key = STRBUF_INIT;
284 struct strbuf key = STRBUF_INIT;
285 struct strbuf roundtrip = STRBUF_INIT;
286 int restart;
287 uint8_t extra;
288 int n, m;
289 uint8_t rt_extra;
291 strbuf_addstr(&last_key, "refs/heads/master");
292 strbuf_addstr(&key, "refs/tags/bla");
293 extra = 6;
294 n = reftable_encode_key(&restart, dest, last_key, key, extra);
295 EXPECT(!restart);
296 EXPECT(n > 0);
298 strbuf_addstr(&roundtrip, "refs/heads/master");
299 m = reftable_decode_key(&roundtrip, &rt_extra, dest);
300 EXPECT(n == m);
301 EXPECT(0 == strbuf_cmp(&key, &roundtrip));
302 EXPECT(rt_extra == extra);
304 strbuf_release(&last_key);
305 strbuf_release(&key);
306 strbuf_release(&roundtrip);
309 static void test_reftable_obj_record_roundtrip(void)
311 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
312 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
313 struct reftable_obj_record recs[3] = { {
314 .hash_prefix = testHash1,
315 .hash_prefix_len = 5,
316 .offsets = till9,
317 .offset_len = 3,
320 .hash_prefix = testHash1,
321 .hash_prefix_len = 5,
322 .offsets = till9,
323 .offset_len = 9,
326 .hash_prefix = testHash1,
327 .hash_prefix_len = 5,
328 } };
329 int i = 0;
330 for (i = 0; i < ARRAY_SIZE(recs); i++) {
331 uint8_t buffer[1024] = { 0 };
332 struct string_view dest = {
333 .buf = buffer,
334 .len = sizeof(buffer),
336 struct reftable_record in = {
337 .type = BLOCK_TYPE_OBJ,
338 .u = {
339 .obj = recs[i],
342 struct strbuf key = STRBUF_INIT;
343 struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
344 int n, m;
345 uint8_t extra;
347 test_copy(&in);
348 reftable_record_key(&in, &key);
349 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
350 EXPECT(n > 0);
351 extra = reftable_record_val_type(&in);
352 m = reftable_record_decode(&out, key, extra, dest,
353 GIT_SHA1_RAWSZ);
354 EXPECT(n == m);
356 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
357 strbuf_release(&key);
358 reftable_record_release(&out);
362 static void test_reftable_index_record_roundtrip(void)
364 struct reftable_record in = {
365 .type = BLOCK_TYPE_INDEX,
366 .u.idx = {
367 .offset = 42,
368 .last_key = STRBUF_INIT,
371 uint8_t buffer[1024] = { 0 };
372 struct string_view dest = {
373 .buf = buffer,
374 .len = sizeof(buffer),
376 struct strbuf key = STRBUF_INIT;
377 struct reftable_record out = {
378 .type = BLOCK_TYPE_INDEX,
379 .u.idx = { .last_key = STRBUF_INIT },
381 int n, m;
382 uint8_t extra;
384 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
385 reftable_record_key(&in, &key);
386 test_copy(&in);
388 EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
389 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
390 EXPECT(n > 0);
392 extra = reftable_record_val_type(&in);
393 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ);
394 EXPECT(m == n);
396 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
398 reftable_record_release(&out);
399 strbuf_release(&key);
400 strbuf_release(&in.u.idx.last_key);
403 int record_test_main(int argc, const char *argv[])
405 RUN_TEST(test_reftable_log_record_equal);
406 RUN_TEST(test_reftable_log_record_roundtrip);
407 RUN_TEST(test_reftable_ref_record_roundtrip);
408 RUN_TEST(test_varint_roundtrip);
409 RUN_TEST(test_key_roundtrip);
410 RUN_TEST(test_common_prefix);
411 RUN_TEST(test_reftable_obj_record_roundtrip);
412 RUN_TEST(test_reftable_index_record_roundtrip);
413 RUN_TEST(test_u24_roundtrip);
414 return 0;