reftable/record: use scratch buffer when decoding records
[alt-git.git] / reftable / record_test.c
blobc158ee79ffe36a0bdf13259f83c4eef119f36981
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 struct strbuf scratch = STRBUF_INIT;
103 int i = 0;
105 for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
106 struct reftable_record in = {
107 .type = BLOCK_TYPE_REF,
109 struct reftable_record out = { .type = BLOCK_TYPE_REF };
110 struct strbuf key = STRBUF_INIT;
111 uint8_t buffer[1024] = { 0 };
112 struct string_view dest = {
113 .buf = buffer,
114 .len = sizeof(buffer),
116 int n, m;
118 in.u.ref.value_type = i;
119 switch (i) {
120 case REFTABLE_REF_DELETION:
121 break;
122 case REFTABLE_REF_VAL1:
123 set_hash(in.u.ref.value.val1, 1);
124 break;
125 case REFTABLE_REF_VAL2:
126 set_hash(in.u.ref.value.val2.value, 1);
127 set_hash(in.u.ref.value.val2.target_value, 2);
128 break;
129 case REFTABLE_REF_SYMREF:
130 in.u.ref.value.symref = xstrdup("target");
131 break;
133 in.u.ref.refname = xstrdup("refs/heads/master");
135 test_copy(&in);
137 EXPECT(reftable_record_val_type(&in) == i);
139 reftable_record_key(&in, &key);
140 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
141 EXPECT(n > 0);
143 /* decode into a non-zero reftable_record to test for leaks. */
144 m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
145 EXPECT(n == m);
147 EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
148 GIT_SHA1_RAWSZ));
149 reftable_record_release(&in);
151 strbuf_release(&key);
152 reftable_record_release(&out);
155 strbuf_release(&scratch);
158 static void test_reftable_log_record_equal(void)
160 struct reftable_log_record in[2] = {
162 .refname = xstrdup("refs/heads/master"),
163 .update_index = 42,
166 .refname = xstrdup("refs/heads/master"),
167 .update_index = 22,
171 EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
172 in[1].update_index = in[0].update_index;
173 EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
174 reftable_log_record_release(&in[0]);
175 reftable_log_record_release(&in[1]);
178 static void test_reftable_log_record_roundtrip(void)
180 int i;
181 struct reftable_log_record in[] = {
183 .refname = xstrdup("refs/heads/master"),
184 .update_index = 42,
185 .value_type = REFTABLE_LOG_UPDATE,
186 .value = {
187 .update = {
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,
207 struct strbuf scratch = STRBUF_INIT;
209 set_test_hash(in[0].value.update.new_hash, 1);
210 set_test_hash(in[0].value.update.old_hash, 2);
211 set_test_hash(in[2].value.update.new_hash, 3);
212 set_test_hash(in[2].value.update.old_hash, 4);
213 for (i = 0; i < ARRAY_SIZE(in); i++) {
214 struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
215 struct strbuf key = STRBUF_INIT;
216 uint8_t buffer[1024] = { 0 };
217 struct string_view dest = {
218 .buf = buffer,
219 .len = sizeof(buffer),
221 /* populate out, to check for leaks. */
222 struct reftable_record out = {
223 .type = BLOCK_TYPE_LOG,
224 .u.log = {
225 .refname = xstrdup("old name"),
226 .value_type = REFTABLE_LOG_UPDATE,
227 .value = {
228 .update = {
229 .name = xstrdup("old name"),
230 .email = xstrdup("old@email"),
231 .message = xstrdup("old message"),
236 int n, m, valtype;
238 rec.u.log = in[i];
240 test_copy(&rec);
242 reftable_record_key(&rec, &key);
244 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
245 EXPECT(n >= 0);
246 valtype = reftable_record_val_type(&rec);
247 m = reftable_record_decode(&out, key, valtype, dest,
248 GIT_SHA1_RAWSZ, &scratch);
249 EXPECT(n == m);
251 EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
252 GIT_SHA1_RAWSZ));
253 reftable_log_record_release(&in[i]);
254 strbuf_release(&key);
255 reftable_record_release(&out);
258 strbuf_release(&scratch);
261 static void test_u24_roundtrip(void)
263 uint32_t in = 0x112233;
264 uint8_t dest[3];
265 uint32_t out;
266 put_be24(dest, in);
267 out = get_be24(dest);
268 EXPECT(in == out);
271 static void test_key_roundtrip(void)
273 uint8_t buffer[1024] = { 0 };
274 struct string_view dest = {
275 .buf = buffer,
276 .len = sizeof(buffer),
278 struct strbuf last_key = STRBUF_INIT;
279 struct strbuf key = STRBUF_INIT;
280 struct strbuf roundtrip = STRBUF_INIT;
281 int restart;
282 uint8_t extra;
283 int n, m;
284 uint8_t rt_extra;
286 strbuf_addstr(&last_key, "refs/heads/master");
287 strbuf_addstr(&key, "refs/tags/bla");
288 extra = 6;
289 n = reftable_encode_key(&restart, dest, last_key, key, extra);
290 EXPECT(!restart);
291 EXPECT(n > 0);
293 strbuf_addstr(&roundtrip, "refs/heads/master");
294 m = reftable_decode_key(&roundtrip, &rt_extra, dest);
295 EXPECT(n == m);
296 EXPECT(0 == strbuf_cmp(&key, &roundtrip));
297 EXPECT(rt_extra == extra);
299 strbuf_release(&last_key);
300 strbuf_release(&key);
301 strbuf_release(&roundtrip);
304 static void test_reftable_obj_record_roundtrip(void)
306 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
307 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
308 struct reftable_obj_record recs[3] = {
310 .hash_prefix = testHash1,
311 .hash_prefix_len = 5,
312 .offsets = till9,
313 .offset_len = 3,
316 .hash_prefix = testHash1,
317 .hash_prefix_len = 5,
318 .offsets = till9,
319 .offset_len = 9,
322 .hash_prefix = testHash1,
323 .hash_prefix_len = 5,
326 struct strbuf scratch = STRBUF_INIT;
327 int i = 0;
329 for (i = 0; i < ARRAY_SIZE(recs); i++) {
330 uint8_t buffer[1024] = { 0 };
331 struct string_view dest = {
332 .buf = buffer,
333 .len = sizeof(buffer),
335 struct reftable_record in = {
336 .type = BLOCK_TYPE_OBJ,
337 .u = {
338 .obj = recs[i],
341 struct strbuf key = STRBUF_INIT;
342 struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
343 int n, m;
344 uint8_t extra;
346 test_copy(&in);
347 reftable_record_key(&in, &key);
348 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
349 EXPECT(n > 0);
350 extra = reftable_record_val_type(&in);
351 m = reftable_record_decode(&out, key, extra, dest,
352 GIT_SHA1_RAWSZ, &scratch);
353 EXPECT(n == m);
355 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
356 strbuf_release(&key);
357 reftable_record_release(&out);
360 strbuf_release(&scratch);
363 static void test_reftable_index_record_roundtrip(void)
365 struct reftable_record in = {
366 .type = BLOCK_TYPE_INDEX,
367 .u.idx = {
368 .offset = 42,
369 .last_key = STRBUF_INIT,
372 uint8_t buffer[1024] = { 0 };
373 struct string_view dest = {
374 .buf = buffer,
375 .len = sizeof(buffer),
377 struct strbuf scratch = STRBUF_INIT;
378 struct strbuf key = STRBUF_INIT;
379 struct reftable_record out = {
380 .type = BLOCK_TYPE_INDEX,
381 .u.idx = { .last_key = STRBUF_INIT },
383 int n, m;
384 uint8_t extra;
386 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
387 reftable_record_key(&in, &key);
388 test_copy(&in);
390 EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
391 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
392 EXPECT(n > 0);
394 extra = reftable_record_val_type(&in);
395 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
396 &scratch);
397 EXPECT(m == n);
399 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
401 reftable_record_release(&out);
402 strbuf_release(&key);
403 strbuf_release(&scratch);
404 strbuf_release(&in.u.idx.last_key);
407 int record_test_main(int argc, const char *argv[])
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;