Merge branch 'ak/protect-any-current-branch'
[alt-git.git] / reftable / record_test.c
blobf4ad7cace41bb227564e4d6a17078fdefd36c555
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 reftable_new_record(reftable_record_type(rec));
21 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
22 /* do it twice to catch memory leaks */
23 reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
24 switch (reftable_record_type(&copy)) {
25 case BLOCK_TYPE_REF:
26 EXPECT(reftable_ref_record_equal(reftable_record_as_ref(&copy),
27 reftable_record_as_ref(rec),
28 GIT_SHA1_RAWSZ));
29 break;
30 case BLOCK_TYPE_LOG:
31 EXPECT(reftable_log_record_equal(reftable_record_as_log(&copy),
32 reftable_record_as_log(rec),
33 GIT_SHA1_RAWSZ));
34 break;
36 reftable_record_destroy(&copy);
39 static void test_varint_roundtrip(void)
41 uint64_t inputs[] = { 0,
43 27,
44 127,
45 128,
46 257,
47 4096,
48 ((uint64_t)1 << 63),
49 ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
50 int i = 0;
51 for (i = 0; i < ARRAY_SIZE(inputs); i++) {
52 uint8_t dest[10];
54 struct string_view out = {
55 .buf = dest,
56 .len = sizeof(dest),
58 uint64_t in = inputs[i];
59 int n = put_var_int(&out, in);
60 uint64_t got = 0;
62 EXPECT(n > 0);
63 out.len = n;
64 n = get_var_int(&got, &out);
65 EXPECT(n > 0);
67 EXPECT(got == in);
71 static void test_common_prefix(void)
73 struct {
74 const char *a, *b;
75 int want;
76 } cases[] = {
77 { "abc", "ab", 2 },
78 { "", "abc", 0 },
79 { "abc", "abd", 2 },
80 { "abc", "pqr", 0 },
83 int i = 0;
84 for (i = 0; i < ARRAY_SIZE(cases); i++) {
85 struct strbuf a = STRBUF_INIT;
86 struct strbuf b = STRBUF_INIT;
87 strbuf_addstr(&a, cases[i].a);
88 strbuf_addstr(&b, cases[i].b);
89 EXPECT(common_prefix_size(&a, &b) == cases[i].want);
91 strbuf_release(&a);
92 strbuf_release(&b);
96 static void set_hash(uint8_t *h, int j)
98 int i = 0;
99 for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
100 h[i] = (j >> i) & 0xff;
104 static void test_reftable_ref_record_roundtrip(void)
106 int i = 0;
108 for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
109 struct reftable_ref_record in = { NULL };
110 struct reftable_ref_record out = { NULL };
111 struct reftable_record rec_out = { NULL };
112 struct strbuf key = STRBUF_INIT;
113 struct reftable_record rec = { NULL };
114 uint8_t buffer[1024] = { 0 };
115 struct string_view dest = {
116 .buf = buffer,
117 .len = sizeof(buffer),
120 int n, m;
122 in.value_type = i;
123 switch (i) {
124 case REFTABLE_REF_DELETION:
125 break;
126 case REFTABLE_REF_VAL1:
127 in.value.val1 = reftable_malloc(GIT_SHA1_RAWSZ);
128 set_hash(in.value.val1, 1);
129 break;
130 case REFTABLE_REF_VAL2:
131 in.value.val2.value = reftable_malloc(GIT_SHA1_RAWSZ);
132 set_hash(in.value.val2.value, 1);
133 in.value.val2.target_value =
134 reftable_malloc(GIT_SHA1_RAWSZ);
135 set_hash(in.value.val2.target_value, 2);
136 break;
137 case REFTABLE_REF_SYMREF:
138 in.value.symref = xstrdup("target");
139 break;
141 in.refname = xstrdup("refs/heads/master");
143 reftable_record_from_ref(&rec, &in);
144 test_copy(&rec);
146 EXPECT(reftable_record_val_type(&rec) == i);
148 reftable_record_key(&rec, &key);
149 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
150 EXPECT(n > 0);
152 /* decode into a non-zero reftable_record to test for leaks. */
154 reftable_record_from_ref(&rec_out, &out);
155 m = reftable_record_decode(&rec_out, key, i, dest,
156 GIT_SHA1_RAWSZ);
157 EXPECT(n == m);
159 EXPECT(reftable_ref_record_equal(&in, &out, GIT_SHA1_RAWSZ));
160 reftable_record_release(&rec_out);
162 strbuf_release(&key);
163 reftable_ref_record_release(&in);
167 static void test_reftable_log_record_equal(void)
169 struct reftable_log_record in[2] = {
171 .refname = xstrdup("refs/heads/master"),
172 .update_index = 42,
175 .refname = xstrdup("refs/heads/master"),
176 .update_index = 22,
180 EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
181 in[1].update_index = in[0].update_index;
182 EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
183 reftable_log_record_release(&in[0]);
184 reftable_log_record_release(&in[1]);
187 static void test_reftable_log_record_roundtrip(void)
189 int i;
190 struct reftable_log_record in[2] = {
192 .refname = xstrdup("refs/heads/master"),
193 .update_index = 42,
194 .value_type = REFTABLE_LOG_UPDATE,
195 .value = {
196 .update = {
197 .old_hash = reftable_malloc(GIT_SHA1_RAWSZ),
198 .new_hash = reftable_malloc(GIT_SHA1_RAWSZ),
199 .name = xstrdup("han-wen"),
200 .email = xstrdup("hanwen@google.com"),
201 .message = xstrdup("test"),
202 .time = 1577123507,
203 .tz_offset = 100,
208 .refname = xstrdup("refs/heads/master"),
209 .update_index = 22,
210 .value_type = REFTABLE_LOG_DELETION,
213 set_test_hash(in[0].value.update.new_hash, 1);
214 set_test_hash(in[0].value.update.old_hash, 2);
215 for (i = 0; i < ARRAY_SIZE(in); i++) {
216 struct reftable_record rec = { NULL };
217 struct strbuf key = STRBUF_INIT;
218 uint8_t buffer[1024] = { 0 };
219 struct string_view dest = {
220 .buf = buffer,
221 .len = sizeof(buffer),
223 /* populate out, to check for leaks. */
224 struct reftable_log_record out = {
225 .refname = xstrdup("old name"),
226 .value_type = REFTABLE_LOG_UPDATE,
227 .value = {
228 .update = {
229 .new_hash = reftable_calloc(GIT_SHA1_RAWSZ),
230 .old_hash = reftable_calloc(GIT_SHA1_RAWSZ),
231 .name = xstrdup("old name"),
232 .email = xstrdup("old@email"),
233 .message = xstrdup("old message"),
237 struct reftable_record rec_out = { NULL };
238 int n, m, valtype;
240 reftable_record_from_log(&rec, &in[i]);
242 test_copy(&rec);
244 reftable_record_key(&rec, &key);
246 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
247 EXPECT(n >= 0);
248 reftable_record_from_log(&rec_out, &out);
249 valtype = reftable_record_val_type(&rec);
250 m = reftable_record_decode(&rec_out, key, valtype, dest,
251 GIT_SHA1_RAWSZ);
252 EXPECT(n == m);
254 EXPECT(reftable_log_record_equal(&in[i], &out, GIT_SHA1_RAWSZ));
255 reftable_log_record_release(&in[i]);
256 strbuf_release(&key);
257 reftable_record_release(&rec_out);
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 m = reftable_decode_key(&roundtrip, &rt_extra, last_key, dest);
294 EXPECT(n == m);
295 EXPECT(0 == strbuf_cmp(&key, &roundtrip));
296 EXPECT(rt_extra == extra);
298 strbuf_release(&last_key);
299 strbuf_release(&key);
300 strbuf_release(&roundtrip);
303 static void test_reftable_obj_record_roundtrip(void)
305 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
306 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
307 struct reftable_obj_record recs[3] = { {
308 .hash_prefix = testHash1,
309 .hash_prefix_len = 5,
310 .offsets = till9,
311 .offset_len = 3,
314 .hash_prefix = testHash1,
315 .hash_prefix_len = 5,
316 .offsets = till9,
317 .offset_len = 9,
320 .hash_prefix = testHash1,
321 .hash_prefix_len = 5,
322 } };
323 int i = 0;
324 for (i = 0; i < ARRAY_SIZE(recs); i++) {
325 struct reftable_obj_record in = recs[i];
326 uint8_t buffer[1024] = { 0 };
327 struct string_view dest = {
328 .buf = buffer,
329 .len = sizeof(buffer),
331 struct reftable_record rec = { NULL };
332 struct strbuf key = STRBUF_INIT;
333 struct reftable_obj_record out = { NULL };
334 struct reftable_record rec_out = { NULL };
335 int n, m;
336 uint8_t extra;
338 reftable_record_from_obj(&rec, &in);
339 test_copy(&rec);
340 reftable_record_key(&rec, &key);
341 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
342 EXPECT(n > 0);
343 extra = reftable_record_val_type(&rec);
344 reftable_record_from_obj(&rec_out, &out);
345 m = reftable_record_decode(&rec_out, key, extra, dest,
346 GIT_SHA1_RAWSZ);
347 EXPECT(n == m);
349 EXPECT(in.hash_prefix_len == out.hash_prefix_len);
350 EXPECT(in.offset_len == out.offset_len);
352 EXPECT(!memcmp(in.hash_prefix, out.hash_prefix,
353 in.hash_prefix_len));
354 EXPECT(0 == memcmp(in.offsets, out.offsets,
355 sizeof(uint64_t) * in.offset_len));
356 strbuf_release(&key);
357 reftable_record_release(&rec_out);
361 static void test_reftable_index_record_roundtrip(void)
363 struct reftable_index_record in = {
364 .offset = 42,
365 .last_key = STRBUF_INIT,
367 uint8_t buffer[1024] = { 0 };
368 struct string_view dest = {
369 .buf = buffer,
370 .len = sizeof(buffer),
372 struct strbuf key = STRBUF_INIT;
373 struct reftable_record rec = { NULL };
374 struct reftable_index_record out = { .last_key = STRBUF_INIT };
375 struct reftable_record out_rec = { NULL };
376 int n, m;
377 uint8_t extra;
379 strbuf_addstr(&in.last_key, "refs/heads/master");
380 reftable_record_from_index(&rec, &in);
381 reftable_record_key(&rec, &key);
382 test_copy(&rec);
384 EXPECT(0 == strbuf_cmp(&key, &in.last_key));
385 n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
386 EXPECT(n > 0);
388 extra = reftable_record_val_type(&rec);
389 reftable_record_from_index(&out_rec, &out);
390 m = reftable_record_decode(&out_rec, key, extra, dest, GIT_SHA1_RAWSZ);
391 EXPECT(m == n);
393 EXPECT(in.offset == out.offset);
395 reftable_record_release(&out_rec);
396 strbuf_release(&key);
397 strbuf_release(&in.last_key);
400 int record_test_main(int argc, const char *argv[])
402 RUN_TEST(test_reftable_log_record_equal);
403 RUN_TEST(test_reftable_log_record_roundtrip);
404 RUN_TEST(test_reftable_ref_record_roundtrip);
405 RUN_TEST(test_varint_roundtrip);
406 RUN_TEST(test_key_roundtrip);
407 RUN_TEST(test_common_prefix);
408 RUN_TEST(test_reftable_obj_record_roundtrip);
409 RUN_TEST(test_reftable_index_record_roundtrip);
410 RUN_TEST(test_u24_roundtrip);
411 return 0;