]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/record_test.c
Merge branch 'rs/opt-parse-long-fixups'
[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;
20 uint8_t typ;
21
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));
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 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;
131 }
132 in.u.ref.refname = xstrdup("refs/heads/master");
133
134 test_copy(&in);
135
136 EXPECT(reftable_record_val_type(&in) == i);
137
138 reftable_record_key(&in, &key);
139 n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
140 EXPECT(n > 0);
141
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);
145
146 EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
147 GIT_SHA1_RAWSZ));
148 reftable_record_release(&in);
149
150 strbuf_release(&key);
151 reftable_record_release(&out);
152 }
153 }
154
155 static void test_reftable_log_record_equal(void)
156 {
157 struct reftable_log_record in[2] = {
158 {
159 .refname = xstrdup("refs/heads/master"),
160 .update_index = 42,
161 },
162 {
163 .refname = xstrdup("refs/heads/master"),
164 .update_index = 22,
165 }
166 };
167
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]);
173 }
174
175 static void test_reftable_log_record_roundtrip(void)
176 {
177 int i;
178
179 struct reftable_log_record in[] = {
180 {
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,
193 },
194 }
195 },
196 {
197 .refname = xstrdup("refs/heads/master"),
198 .update_index = 22,
199 .value_type = REFTABLE_LOG_DELETION,
200 },
201 {
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. */
210 },
211 },
212 }
213 };
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),
225 };
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"),
239 },
240 },
241 },
242 };
243 int n, m, valtype;
244
245 rec.u.log = in[i];
246
247 test_copy(&rec);
248
249 reftable_record_key(&rec, &key);
250
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);
257
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);
263 }
264 }
265
266 static void test_u24_roundtrip(void)
267 {
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);
274 }
275
276 static void test_key_roundtrip(void)
277 {
278 uint8_t buffer[1024] = { 0 };
279 struct string_view dest = {
280 .buf = buffer,
281 .len = sizeof(buffer),
282 };
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;
290
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);
297
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);
303
304 strbuf_release(&last_key);
305 strbuf_release(&key);
306 strbuf_release(&roundtrip);
307 }
308
309 static void test_reftable_obj_record_roundtrip(void)
310 {
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,
318 },
319 {
320 .hash_prefix = testHash1,
321 .hash_prefix_len = 5,
322 .offsets = till9,
323 .offset_len = 9,
324 },
325 {
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),
335 };
336 struct reftable_record in = {
337 .type = BLOCK_TYPE_OBJ,
338 .u = {
339 .obj = recs[i],
340 },
341 };
342 struct strbuf key = STRBUF_INIT;
343 struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
344 int n, m;
345 uint8_t extra;
346
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);
355
356 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
357 strbuf_release(&key);
358 reftable_record_release(&out);
359 }
360 }
361
362 static void test_reftable_index_record_roundtrip(void)
363 {
364 struct reftable_record in = {
365 .type = BLOCK_TYPE_INDEX,
366 .u.idx = {
367 .offset = 42,
368 .last_key = STRBUF_INIT,
369 },
370 };
371 uint8_t buffer[1024] = { 0 };
372 struct string_view dest = {
373 .buf = buffer,
374 .len = sizeof(buffer),
375 };
376 struct strbuf key = STRBUF_INIT;
377 struct reftable_record out = {
378 .type = BLOCK_TYPE_INDEX,
379 .u.idx = { .last_key = STRBUF_INIT },
380 };
381 int n, m;
382 uint8_t extra;
383
384 strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
385 reftable_record_key(&in, &key);
386 test_copy(&in);
387
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);
391
392 extra = reftable_record_val_type(&in);
393 m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ);
394 EXPECT(m == n);
395
396 EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
397
398 reftable_record_release(&out);
399 strbuf_release(&key);
400 strbuf_release(&in.u.idx.last_key);
401 }
402
403 int record_test_main(int argc, const char *argv[])
404 {
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;
415 }