Merge branch '2.44-uk-update' of github.com:arkid15r/git-ukrainian-l10n
[alt-git.git] / reftable / record_test.c
bloba86cff552661ba0f00be32200bf8b0c800a9fa87
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 m = reftable_decode_key(&roundtrip, &rt_extra, last_key, dest);
299 EXPECT(n == m);
300 EXPECT(0 == strbuf_cmp(&key, &roundtrip));
301 EXPECT(rt_extra == extra);
303 strbuf_release(&last_key);
304 strbuf_release(&key);
305 strbuf_release(&roundtrip);
308 static void test_reftable_obj_record_roundtrip(void)
310 uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
311 uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
312 struct reftable_obj_record recs[3] = { {
313 .hash_prefix = testHash1,
314 .hash_prefix_len = 5,
315 .offsets = till9,
316 .offset_len = 3,
319 .hash_prefix = testHash1,
320 .hash_prefix_len = 5,
321 .offsets = till9,
322 .offset_len = 9,
325 .hash_prefix = testHash1,
326 .hash_prefix_len = 5,
327 } };
328 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);
353 EXPECT(n == m);
355 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
356 strbuf_release(&key);
357 reftable_record_release(&out);
361 static void test_reftable_index_record_roundtrip(void)
363 struct reftable_record in = {
364 .type = BLOCK_TYPE_INDEX,
365 .u.idx = {
366 .offset = 42,
367 .last_key = STRBUF_INIT,
370 uint8_t buffer[1024] = { 0 };
371 struct string_view dest = {
372 .buf = buffer,
373 .len = sizeof(buffer),
375 struct strbuf key = STRBUF_INIT;
376 struct reftable_record out = {
377 .type = BLOCK_TYPE_INDEX,
378 .u.idx = { .last_key = STRBUF_INIT },
380 int n, m;
381 uint8_t extra;
383 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
384 reftable_record_key(&in, &key);
385 test_copy(&in);
387 EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
388 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
389 EXPECT(n > 0);
391 extra = reftable_record_val_type(&in);
392 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ);
393 EXPECT(m == n);
395 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
397 reftable_record_release(&out);
398 strbuf_release(&key);
399 strbuf_release(&in.u.idx.last_key);
402 int record_test_main(int argc, const char *argv[])
404 RUN_TEST(test_reftable_log_record_equal);
405 RUN_TEST(test_reftable_log_record_roundtrip);
406 RUN_TEST(test_reftable_ref_record_roundtrip);
407 RUN_TEST(test_varint_roundtrip);
408 RUN_TEST(test_key_roundtrip);
409 RUN_TEST(test_common_prefix);
410 RUN_TEST(test_reftable_obj_record_roundtrip);
411 RUN_TEST(test_reftable_index_record_roundtrip);
412 RUN_TEST(test_u24_roundtrip);
413 return 0;