builtin/gc: forward git-gc(1)'s `--auto` flag when packing refs
[alt-git.git] / reftable / readwrite_test.c
blob363fe0f998b91f72002f819c396893b8086dc992
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 "system.h"
11 #include "basics.h"
12 #include "block.h"
13 #include "blocksource.h"
14 #include "reader.h"
15 #include "record.h"
16 #include "test_framework.h"
17 #include "reftable-tests.h"
18 #include "reftable-writer.h"
20 static const int update_index = 5;
22 static void test_buffer(void)
24 struct strbuf buf = STRBUF_INIT;
25 struct reftable_block_source source = { NULL };
26 struct reftable_block out = { NULL };
27 int n;
28 uint8_t in[] = "hello";
29 strbuf_add(&buf, in, sizeof(in));
30 block_source_from_strbuf(&source, &buf);
31 EXPECT(block_source_size(&source) == 6);
32 n = block_source_read_block(&source, &out, 0, sizeof(in));
33 EXPECT(n == sizeof(in));
34 EXPECT(!memcmp(in, out.data, n));
35 reftable_block_done(&out);
37 n = block_source_read_block(&source, &out, 1, 2);
38 EXPECT(n == 2);
39 EXPECT(!memcmp(out.data, "el", 2));
41 reftable_block_done(&out);
42 block_source_close(&source);
43 strbuf_release(&buf);
46 static void write_table(char ***names, struct strbuf *buf, int N,
47 int block_size, uint32_t hash_id)
49 struct reftable_write_options opts = {
50 .block_size = block_size,
51 .hash_id = hash_id,
53 struct reftable_writer *w =
54 reftable_new_writer(&strbuf_add_void, &noop_flush, buf, &opts);
55 struct reftable_ref_record ref = { NULL };
56 int i = 0, n;
57 struct reftable_log_record log = { NULL };
58 const struct reftable_stats *stats = NULL;
60 REFTABLE_CALLOC_ARRAY(*names, N + 1);
62 reftable_writer_set_limits(w, update_index, update_index);
63 for (i = 0; i < N; i++) {
64 char name[100];
65 int n;
67 snprintf(name, sizeof(name), "refs/heads/branch%02d", i);
69 ref.refname = name;
70 ref.update_index = update_index;
71 ref.value_type = REFTABLE_REF_VAL1;
72 set_test_hash(ref.value.val1, i);
73 (*names)[i] = xstrdup(name);
75 n = reftable_writer_add_ref(w, &ref);
76 EXPECT(n == 0);
79 for (i = 0; i < N; i++) {
80 uint8_t hash[GIT_SHA256_RAWSZ] = { 0 };
81 char name[100];
82 int n;
84 set_test_hash(hash, i);
86 snprintf(name, sizeof(name), "refs/heads/branch%02d", i);
88 log.refname = name;
89 log.update_index = update_index;
90 log.value_type = REFTABLE_LOG_UPDATE;
91 log.value.update.new_hash = hash;
92 log.value.update.message = "message";
94 n = reftable_writer_add_log(w, &log);
95 EXPECT(n == 0);
98 n = reftable_writer_close(w);
99 EXPECT(n == 0);
101 stats = reftable_writer_stats(w);
102 for (i = 0; i < stats->ref_stats.blocks; i++) {
103 int off = i * opts.block_size;
104 if (off == 0) {
105 off = header_size(
106 (hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1);
108 EXPECT(buf->buf[off] == 'r');
111 EXPECT(stats->log_stats.blocks > 0);
112 reftable_writer_free(w);
115 static void test_log_buffer_size(void)
117 struct strbuf buf = STRBUF_INIT;
118 struct reftable_write_options opts = {
119 .block_size = 4096,
121 int err;
122 int i;
123 struct reftable_log_record
124 log = { .refname = "refs/heads/master",
125 .update_index = 0xa,
126 .value_type = REFTABLE_LOG_UPDATE,
127 .value = { .update = {
128 .name = "Han-Wen Nienhuys",
129 .email = "hanwen@google.com",
130 .tz_offset = 100,
131 .time = 0x5e430672,
132 .message = "commit: 9\n",
133 } } };
134 struct reftable_writer *w =
135 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
137 /* This tests buffer extension for log compression. Must use a random
138 hash, to ensure that the compressed part is larger than the original.
140 uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ];
141 for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
142 hash1[i] = (uint8_t)(git_rand() % 256);
143 hash2[i] = (uint8_t)(git_rand() % 256);
145 log.value.update.old_hash = hash1;
146 log.value.update.new_hash = hash2;
147 reftable_writer_set_limits(w, update_index, update_index);
148 err = reftable_writer_add_log(w, &log);
149 EXPECT_ERR(err);
150 err = reftable_writer_close(w);
151 EXPECT_ERR(err);
152 reftable_writer_free(w);
153 strbuf_release(&buf);
156 static void test_log_overflow(void)
158 struct strbuf buf = STRBUF_INIT;
159 char msg[256] = { 0 };
160 struct reftable_write_options opts = {
161 .block_size = ARRAY_SIZE(msg),
163 int err;
164 struct reftable_log_record
165 log = { .refname = "refs/heads/master",
166 .update_index = 0xa,
167 .value_type = REFTABLE_LOG_UPDATE,
168 .value = { .update = {
169 .name = "Han-Wen Nienhuys",
170 .email = "hanwen@google.com",
171 .tz_offset = 100,
172 .time = 0x5e430672,
173 .message = msg,
174 } } };
175 struct reftable_writer *w =
176 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
178 uint8_t hash1[GIT_SHA1_RAWSZ] = {1}, hash2[GIT_SHA1_RAWSZ] = { 2 };
180 memset(msg, 'x', sizeof(msg) - 1);
181 log.value.update.old_hash = hash1;
182 log.value.update.new_hash = hash2;
183 reftable_writer_set_limits(w, update_index, update_index);
184 err = reftable_writer_add_log(w, &log);
185 EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR);
186 reftable_writer_free(w);
187 strbuf_release(&buf);
190 static void test_log_write_read(void)
192 int N = 2;
193 char **names = reftable_calloc(N + 1, sizeof(*names));
194 int err;
195 struct reftable_write_options opts = {
196 .block_size = 256,
198 struct reftable_ref_record ref = { NULL };
199 int i = 0;
200 struct reftable_log_record log = { NULL };
201 int n;
202 struct reftable_iterator it = { NULL };
203 struct reftable_reader rd = { NULL };
204 struct reftable_block_source source = { NULL };
205 struct strbuf buf = STRBUF_INIT;
206 struct reftable_writer *w =
207 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
208 const struct reftable_stats *stats = NULL;
209 reftable_writer_set_limits(w, 0, N);
210 for (i = 0; i < N; i++) {
211 char name[256];
212 struct reftable_ref_record ref = { NULL };
213 snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7);
214 names[i] = xstrdup(name);
215 ref.refname = name;
216 ref.update_index = i;
218 err = reftable_writer_add_ref(w, &ref);
219 EXPECT_ERR(err);
221 for (i = 0; i < N; i++) {
222 uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ];
223 struct reftable_log_record log = { NULL };
224 set_test_hash(hash1, i);
225 set_test_hash(hash2, i + 1);
227 log.refname = names[i];
228 log.update_index = i;
229 log.value_type = REFTABLE_LOG_UPDATE;
230 log.value.update.old_hash = hash1;
231 log.value.update.new_hash = hash2;
233 err = reftable_writer_add_log(w, &log);
234 EXPECT_ERR(err);
237 n = reftable_writer_close(w);
238 EXPECT(n == 0);
240 stats = reftable_writer_stats(w);
241 EXPECT(stats->log_stats.blocks > 0);
242 reftable_writer_free(w);
243 w = NULL;
245 block_source_from_strbuf(&source, &buf);
247 err = init_reader(&rd, &source, "file.log");
248 EXPECT_ERR(err);
250 err = reftable_reader_seek_ref(&rd, &it, names[N - 1]);
251 EXPECT_ERR(err);
253 err = reftable_iterator_next_ref(&it, &ref);
254 EXPECT_ERR(err);
256 /* end of iteration. */
257 err = reftable_iterator_next_ref(&it, &ref);
258 EXPECT(0 < err);
260 reftable_iterator_destroy(&it);
261 reftable_ref_record_release(&ref);
263 err = reftable_reader_seek_log(&rd, &it, "");
264 EXPECT_ERR(err);
266 i = 0;
267 while (1) {
268 int err = reftable_iterator_next_log(&it, &log);
269 if (err > 0) {
270 break;
273 EXPECT_ERR(err);
274 EXPECT_STREQ(names[i], log.refname);
275 EXPECT(i == log.update_index);
276 i++;
277 reftable_log_record_release(&log);
280 EXPECT(i == N);
281 reftable_iterator_destroy(&it);
283 /* cleanup. */
284 strbuf_release(&buf);
285 free_names(names);
286 reader_close(&rd);
289 static void test_log_zlib_corruption(void)
291 struct reftable_write_options opts = {
292 .block_size = 256,
294 struct reftable_iterator it = { 0 };
295 struct reftable_reader rd = { 0 };
296 struct reftable_block_source source = { 0 };
297 struct strbuf buf = STRBUF_INIT;
298 struct reftable_writer *w =
299 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
300 const struct reftable_stats *stats = NULL;
301 uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 };
302 uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 };
303 char message[100] = { 0 };
304 int err, i, n;
306 struct reftable_log_record log = {
307 .refname = "refname",
308 .value_type = REFTABLE_LOG_UPDATE,
309 .value = {
310 .update = {
311 .new_hash = hash1,
312 .old_hash = hash2,
313 .name = "My Name",
314 .email = "myname@invalid",
315 .message = message,
320 for (i = 0; i < sizeof(message) - 1; i++)
321 message[i] = (uint8_t)(git_rand() % 64 + ' ');
323 reftable_writer_set_limits(w, 1, 1);
325 err = reftable_writer_add_log(w, &log);
326 EXPECT_ERR(err);
328 n = reftable_writer_close(w);
329 EXPECT(n == 0);
331 stats = reftable_writer_stats(w);
332 EXPECT(stats->log_stats.blocks > 0);
333 reftable_writer_free(w);
334 w = NULL;
336 /* corrupt the data. */
337 buf.buf[50] ^= 0x99;
339 block_source_from_strbuf(&source, &buf);
341 err = init_reader(&rd, &source, "file.log");
342 EXPECT_ERR(err);
344 err = reftable_reader_seek_log(&rd, &it, "refname");
345 EXPECT(err == REFTABLE_ZLIB_ERROR);
347 reftable_iterator_destroy(&it);
349 /* cleanup. */
350 strbuf_release(&buf);
351 reader_close(&rd);
354 static void test_table_read_write_sequential(void)
356 char **names;
357 struct strbuf buf = STRBUF_INIT;
358 int N = 50;
359 struct reftable_iterator it = { NULL };
360 struct reftable_block_source source = { NULL };
361 struct reftable_reader rd = { NULL };
362 int err = 0;
363 int j = 0;
365 write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID);
367 block_source_from_strbuf(&source, &buf);
369 err = init_reader(&rd, &source, "file.ref");
370 EXPECT_ERR(err);
372 err = reftable_reader_seek_ref(&rd, &it, "");
373 EXPECT_ERR(err);
375 while (1) {
376 struct reftable_ref_record ref = { NULL };
377 int r = reftable_iterator_next_ref(&it, &ref);
378 EXPECT(r >= 0);
379 if (r > 0) {
380 break;
382 EXPECT(0 == strcmp(names[j], ref.refname));
383 EXPECT(update_index == ref.update_index);
385 j++;
386 reftable_ref_record_release(&ref);
388 EXPECT(j == N);
389 reftable_iterator_destroy(&it);
390 strbuf_release(&buf);
391 free_names(names);
393 reader_close(&rd);
396 static void test_table_write_small_table(void)
398 char **names;
399 struct strbuf buf = STRBUF_INIT;
400 int N = 1;
401 write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID);
402 EXPECT(buf.len < 200);
403 strbuf_release(&buf);
404 free_names(names);
407 static void test_table_read_api(void)
409 char **names;
410 struct strbuf buf = STRBUF_INIT;
411 int N = 50;
412 struct reftable_reader rd = { NULL };
413 struct reftable_block_source source = { NULL };
414 int err;
415 int i;
416 struct reftable_log_record log = { NULL };
417 struct reftable_iterator it = { NULL };
419 write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID);
421 block_source_from_strbuf(&source, &buf);
423 err = init_reader(&rd, &source, "file.ref");
424 EXPECT_ERR(err);
426 err = reftable_reader_seek_ref(&rd, &it, names[0]);
427 EXPECT_ERR(err);
429 err = reftable_iterator_next_log(&it, &log);
430 EXPECT(err == REFTABLE_API_ERROR);
432 strbuf_release(&buf);
433 for (i = 0; i < N; i++) {
434 reftable_free(names[i]);
436 reftable_iterator_destroy(&it);
437 reftable_free(names);
438 reader_close(&rd);
439 strbuf_release(&buf);
442 static void test_table_read_write_seek(int index, int hash_id)
444 char **names;
445 struct strbuf buf = STRBUF_INIT;
446 int N = 50;
447 struct reftable_reader rd = { NULL };
448 struct reftable_block_source source = { NULL };
449 int err;
450 int i = 0;
452 struct reftable_iterator it = { NULL };
453 struct strbuf pastLast = STRBUF_INIT;
454 struct reftable_ref_record ref = { NULL };
456 write_table(&names, &buf, N, 256, hash_id);
458 block_source_from_strbuf(&source, &buf);
460 err = init_reader(&rd, &source, "file.ref");
461 EXPECT_ERR(err);
462 EXPECT(hash_id == reftable_reader_hash_id(&rd));
464 if (!index) {
465 rd.ref_offsets.index_offset = 0;
466 } else {
467 EXPECT(rd.ref_offsets.index_offset > 0);
470 for (i = 1; i < N; i++) {
471 int err = reftable_reader_seek_ref(&rd, &it, names[i]);
472 EXPECT_ERR(err);
473 err = reftable_iterator_next_ref(&it, &ref);
474 EXPECT_ERR(err);
475 EXPECT(0 == strcmp(names[i], ref.refname));
476 EXPECT(REFTABLE_REF_VAL1 == ref.value_type);
477 EXPECT(i == ref.value.val1[0]);
479 reftable_ref_record_release(&ref);
480 reftable_iterator_destroy(&it);
483 strbuf_addstr(&pastLast, names[N - 1]);
484 strbuf_addstr(&pastLast, "/");
486 err = reftable_reader_seek_ref(&rd, &it, pastLast.buf);
487 if (err == 0) {
488 struct reftable_ref_record ref = { NULL };
489 int err = reftable_iterator_next_ref(&it, &ref);
490 EXPECT(err > 0);
491 } else {
492 EXPECT(err > 0);
495 strbuf_release(&pastLast);
496 reftable_iterator_destroy(&it);
498 strbuf_release(&buf);
499 for (i = 0; i < N; i++) {
500 reftable_free(names[i]);
502 reftable_free(names);
503 reader_close(&rd);
506 static void test_table_read_write_seek_linear(void)
508 test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID);
511 static void test_table_read_write_seek_linear_sha256(void)
513 test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID);
516 static void test_table_read_write_seek_index(void)
518 test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID);
521 static void test_table_refs_for(int indexed)
523 int N = 50;
524 char **want_names = reftable_calloc(N + 1, sizeof(*want_names));
525 int want_names_len = 0;
526 uint8_t want_hash[GIT_SHA1_RAWSZ];
528 struct reftable_write_options opts = {
529 .block_size = 256,
531 struct reftable_ref_record ref = { NULL };
532 int i = 0;
533 int n;
534 int err;
535 struct reftable_reader rd;
536 struct reftable_block_source source = { NULL };
538 struct strbuf buf = STRBUF_INIT;
539 struct reftable_writer *w =
540 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
542 struct reftable_iterator it = { NULL };
543 int j;
545 set_test_hash(want_hash, 4);
547 for (i = 0; i < N; i++) {
548 uint8_t hash[GIT_SHA1_RAWSZ];
549 char fill[51] = { 0 };
550 char name[100];
551 struct reftable_ref_record ref = { NULL };
553 memset(hash, i, sizeof(hash));
554 memset(fill, 'x', 50);
555 /* Put the variable part in the start */
556 snprintf(name, sizeof(name), "br%02d%s", i, fill);
557 name[40] = 0;
558 ref.refname = name;
560 ref.value_type = REFTABLE_REF_VAL2;
561 set_test_hash(ref.value.val2.value, i / 4);
562 set_test_hash(ref.value.val2.target_value, 3 + i / 4);
564 /* 80 bytes / entry, so 3 entries per block. Yields 17
566 /* blocks. */
567 n = reftable_writer_add_ref(w, &ref);
568 EXPECT(n == 0);
570 if (!memcmp(ref.value.val2.value, want_hash, GIT_SHA1_RAWSZ) ||
571 !memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) {
572 want_names[want_names_len++] = xstrdup(name);
576 n = reftable_writer_close(w);
577 EXPECT(n == 0);
579 reftable_writer_free(w);
580 w = NULL;
582 block_source_from_strbuf(&source, &buf);
584 err = init_reader(&rd, &source, "file.ref");
585 EXPECT_ERR(err);
586 if (!indexed) {
587 rd.obj_offsets.is_present = 0;
590 err = reftable_reader_seek_ref(&rd, &it, "");
591 EXPECT_ERR(err);
592 reftable_iterator_destroy(&it);
594 err = reftable_reader_refs_for(&rd, &it, want_hash);
595 EXPECT_ERR(err);
597 j = 0;
598 while (1) {
599 int err = reftable_iterator_next_ref(&it, &ref);
600 EXPECT(err >= 0);
601 if (err > 0) {
602 break;
605 EXPECT(j < want_names_len);
606 EXPECT(0 == strcmp(ref.refname, want_names[j]));
607 j++;
608 reftable_ref_record_release(&ref);
610 EXPECT(j == want_names_len);
612 strbuf_release(&buf);
613 free_names(want_names);
614 reftable_iterator_destroy(&it);
615 reader_close(&rd);
618 static void test_table_refs_for_no_index(void)
620 test_table_refs_for(0);
623 static void test_table_refs_for_obj_index(void)
625 test_table_refs_for(1);
628 static void test_write_empty_table(void)
630 struct reftable_write_options opts = { 0 };
631 struct strbuf buf = STRBUF_INIT;
632 struct reftable_writer *w =
633 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
634 struct reftable_block_source source = { NULL };
635 struct reftable_reader *rd = NULL;
636 struct reftable_ref_record rec = { NULL };
637 struct reftable_iterator it = { NULL };
638 int err;
640 reftable_writer_set_limits(w, 1, 1);
642 err = reftable_writer_close(w);
643 EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
644 reftable_writer_free(w);
646 EXPECT(buf.len == header_size(1) + footer_size(1));
648 block_source_from_strbuf(&source, &buf);
650 err = reftable_new_reader(&rd, &source, "filename");
651 EXPECT_ERR(err);
653 err = reftable_reader_seek_ref(rd, &it, "");
654 EXPECT_ERR(err);
656 err = reftable_iterator_next_ref(&it, &rec);
657 EXPECT(err > 0);
659 reftable_iterator_destroy(&it);
660 reftable_reader_free(rd);
661 strbuf_release(&buf);
664 static void test_write_object_id_min_length(void)
666 struct reftable_write_options opts = {
667 .block_size = 75,
669 struct strbuf buf = STRBUF_INIT;
670 struct reftable_writer *w =
671 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
672 struct reftable_ref_record ref = {
673 .update_index = 1,
674 .value_type = REFTABLE_REF_VAL1,
675 .value.val1 = {42},
677 int err;
678 int i;
680 reftable_writer_set_limits(w, 1, 1);
682 /* Write the same hash in many refs. If there is only 1 hash, the
683 * disambiguating prefix is length 0 */
684 for (i = 0; i < 256; i++) {
685 char name[256];
686 snprintf(name, sizeof(name), "ref%05d", i);
687 ref.refname = name;
688 err = reftable_writer_add_ref(w, &ref);
689 EXPECT_ERR(err);
692 err = reftable_writer_close(w);
693 EXPECT_ERR(err);
694 EXPECT(reftable_writer_stats(w)->object_id_len == 2);
695 reftable_writer_free(w);
696 strbuf_release(&buf);
699 static void test_write_object_id_length(void)
701 struct reftable_write_options opts = {
702 .block_size = 75,
704 struct strbuf buf = STRBUF_INIT;
705 struct reftable_writer *w =
706 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
707 struct reftable_ref_record ref = {
708 .update_index = 1,
709 .value_type = REFTABLE_REF_VAL1,
710 .value.val1 = {42},
712 int err;
713 int i;
715 reftable_writer_set_limits(w, 1, 1);
717 /* Write the same hash in many refs. If there is only 1 hash, the
718 * disambiguating prefix is length 0 */
719 for (i = 0; i < 256; i++) {
720 char name[256];
721 snprintf(name, sizeof(name), "ref%05d", i);
722 ref.refname = name;
723 ref.value.val1[15] = i;
724 err = reftable_writer_add_ref(w, &ref);
725 EXPECT_ERR(err);
728 err = reftable_writer_close(w);
729 EXPECT_ERR(err);
730 EXPECT(reftable_writer_stats(w)->object_id_len == 16);
731 reftable_writer_free(w);
732 strbuf_release(&buf);
735 static void test_write_empty_key(void)
737 struct reftable_write_options opts = { 0 };
738 struct strbuf buf = STRBUF_INIT;
739 struct reftable_writer *w =
740 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
741 struct reftable_ref_record ref = {
742 .refname = "",
743 .update_index = 1,
744 .value_type = REFTABLE_REF_DELETION,
746 int err;
748 reftable_writer_set_limits(w, 1, 1);
749 err = reftable_writer_add_ref(w, &ref);
750 EXPECT(err == REFTABLE_API_ERROR);
752 err = reftable_writer_close(w);
753 EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
754 reftable_writer_free(w);
755 strbuf_release(&buf);
758 static void test_write_key_order(void)
760 struct reftable_write_options opts = { 0 };
761 struct strbuf buf = STRBUF_INIT;
762 struct reftable_writer *w =
763 reftable_new_writer(&strbuf_add_void, &noop_flush, &buf, &opts);
764 struct reftable_ref_record refs[2] = {
766 .refname = "b",
767 .update_index = 1,
768 .value_type = REFTABLE_REF_SYMREF,
769 .value = {
770 .symref = "target",
772 }, {
773 .refname = "a",
774 .update_index = 1,
775 .value_type = REFTABLE_REF_SYMREF,
776 .value = {
777 .symref = "target",
781 int err;
783 reftable_writer_set_limits(w, 1, 1);
784 err = reftable_writer_add_ref(w, &refs[0]);
785 EXPECT_ERR(err);
786 err = reftable_writer_add_ref(w, &refs[1]);
787 EXPECT(err == REFTABLE_API_ERROR);
788 reftable_writer_close(w);
789 reftable_writer_free(w);
790 strbuf_release(&buf);
793 static void test_write_multiple_indices(void)
795 struct reftable_write_options opts = {
796 .block_size = 100,
798 struct strbuf writer_buf = STRBUF_INIT, buf = STRBUF_INIT;
799 struct reftable_block_source source = { 0 };
800 struct reftable_iterator it = { 0 };
801 const struct reftable_stats *stats;
802 struct reftable_writer *writer;
803 struct reftable_reader *reader;
804 int err, i;
806 writer = reftable_new_writer(&strbuf_add_void, &noop_flush, &writer_buf, &opts);
807 reftable_writer_set_limits(writer, 1, 1);
808 for (i = 0; i < 100; i++) {
809 struct reftable_ref_record ref = {
810 .update_index = 1,
811 .value_type = REFTABLE_REF_VAL1,
812 .value.val1 = {i},
815 strbuf_reset(&buf);
816 strbuf_addf(&buf, "refs/heads/%04d", i);
817 ref.refname = buf.buf,
819 err = reftable_writer_add_ref(writer, &ref);
820 EXPECT_ERR(err);
823 for (i = 0; i < 100; i++) {
824 unsigned char hash[GIT_SHA1_RAWSZ] = {i};
825 struct reftable_log_record log = {
826 .update_index = 1,
827 .value_type = REFTABLE_LOG_UPDATE,
828 .value.update = {
829 .old_hash = hash,
830 .new_hash = hash,
834 strbuf_reset(&buf);
835 strbuf_addf(&buf, "refs/heads/%04d", i);
836 log.refname = buf.buf,
838 err = reftable_writer_add_log(writer, &log);
839 EXPECT_ERR(err);
842 reftable_writer_close(writer);
845 * The written data should be sufficiently large to result in indices
846 * for each of the block types.
848 stats = reftable_writer_stats(writer);
849 EXPECT(stats->ref_stats.index_offset > 0);
850 EXPECT(stats->obj_stats.index_offset > 0);
851 EXPECT(stats->log_stats.index_offset > 0);
853 block_source_from_strbuf(&source, &writer_buf);
854 err = reftable_new_reader(&reader, &source, "filename");
855 EXPECT_ERR(err);
858 * Seeking the log uses the log index now. In case there is any
859 * confusion regarding indices we would notice here.
861 err = reftable_reader_seek_log(reader, &it, "");
862 EXPECT_ERR(err);
864 reftable_iterator_destroy(&it);
865 reftable_writer_free(writer);
866 reftable_reader_free(reader);
867 strbuf_release(&writer_buf);
868 strbuf_release(&buf);
871 static void test_write_multi_level_index(void)
873 struct reftable_write_options opts = {
874 .block_size = 100,
876 struct strbuf writer_buf = STRBUF_INIT, buf = STRBUF_INIT;
877 struct reftable_block_source source = { 0 };
878 struct reftable_iterator it = { 0 };
879 const struct reftable_stats *stats;
880 struct reftable_writer *writer;
881 struct reftable_reader *reader;
882 int err;
884 writer = reftable_new_writer(&strbuf_add_void, &noop_flush, &writer_buf, &opts);
885 reftable_writer_set_limits(writer, 1, 1);
886 for (size_t i = 0; i < 200; i++) {
887 struct reftable_ref_record ref = {
888 .update_index = 1,
889 .value_type = REFTABLE_REF_VAL1,
890 .value.val1 = {i},
893 strbuf_reset(&buf);
894 strbuf_addf(&buf, "refs/heads/%03" PRIuMAX, (uintmax_t)i);
895 ref.refname = buf.buf,
897 err = reftable_writer_add_ref(writer, &ref);
898 EXPECT_ERR(err);
900 reftable_writer_close(writer);
903 * The written refs should be sufficiently large to result in a
904 * multi-level index.
906 stats = reftable_writer_stats(writer);
907 EXPECT(stats->ref_stats.max_index_level == 2);
909 block_source_from_strbuf(&source, &writer_buf);
910 err = reftable_new_reader(&reader, &source, "filename");
911 EXPECT_ERR(err);
914 * Seeking the last ref should work as expected.
916 err = reftable_reader_seek_ref(reader, &it, "refs/heads/199");
917 EXPECT_ERR(err);
919 reftable_iterator_destroy(&it);
920 reftable_writer_free(writer);
921 reftable_reader_free(reader);
922 strbuf_release(&writer_buf);
923 strbuf_release(&buf);
926 static void test_corrupt_table_empty(void)
928 struct strbuf buf = STRBUF_INIT;
929 struct reftable_block_source source = { NULL };
930 struct reftable_reader rd = { NULL };
931 int err;
933 block_source_from_strbuf(&source, &buf);
934 err = init_reader(&rd, &source, "file.log");
935 EXPECT(err == REFTABLE_FORMAT_ERROR);
938 static void test_corrupt_table(void)
940 uint8_t zeros[1024] = { 0 };
941 struct strbuf buf = STRBUF_INIT;
942 struct reftable_block_source source = { NULL };
943 struct reftable_reader rd = { NULL };
944 int err;
945 strbuf_add(&buf, zeros, sizeof(zeros));
947 block_source_from_strbuf(&source, &buf);
948 err = init_reader(&rd, &source, "file.log");
949 EXPECT(err == REFTABLE_FORMAT_ERROR);
950 strbuf_release(&buf);
953 int readwrite_test_main(int argc, const char *argv[])
955 RUN_TEST(test_log_zlib_corruption);
956 RUN_TEST(test_corrupt_table);
957 RUN_TEST(test_corrupt_table_empty);
958 RUN_TEST(test_log_write_read);
959 RUN_TEST(test_write_key_order);
960 RUN_TEST(test_table_read_write_seek_linear_sha256);
961 RUN_TEST(test_log_buffer_size);
962 RUN_TEST(test_table_write_small_table);
963 RUN_TEST(test_buffer);
964 RUN_TEST(test_table_read_api);
965 RUN_TEST(test_table_read_write_sequential);
966 RUN_TEST(test_table_read_write_seek_linear);
967 RUN_TEST(test_table_read_write_seek_index);
968 RUN_TEST(test_table_refs_for_no_index);
969 RUN_TEST(test_table_refs_for_obj_index);
970 RUN_TEST(test_write_empty_key);
971 RUN_TEST(test_write_empty_table);
972 RUN_TEST(test_log_overflow);
973 RUN_TEST(test_write_object_id_length);
974 RUN_TEST(test_write_object_id_min_length);
975 RUN_TEST(test_write_multiple_indices);
976 RUN_TEST(test_write_multi_level_index);
977 return 0;