Merge branch 'ob/t3501-retitle'
[alt-git.git] / reftable / readwrite_test.c
blob469ab79a5adf3dfaba0160e523bdcece5b9875e0
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 "constants.h"
15 #include "reader.h"
16 #include "record.h"
17 #include "test_framework.h"
18 #include "reftable-tests.h"
19 #include "reftable-writer.h"
21 static const int update_index = 5;
23 static void test_buffer(void)
25 struct strbuf buf = STRBUF_INIT;
26 struct reftable_block_source source = { NULL };
27 struct reftable_block out = { NULL };
28 int n;
29 uint8_t in[] = "hello";
30 strbuf_add(&buf, in, sizeof(in));
31 block_source_from_strbuf(&source, &buf);
32 EXPECT(block_source_size(&source) == 6);
33 n = block_source_read_block(&source, &out, 0, sizeof(in));
34 EXPECT(n == sizeof(in));
35 EXPECT(!memcmp(in, out.data, n));
36 reftable_block_done(&out);
38 n = block_source_read_block(&source, &out, 1, 2);
39 EXPECT(n == 2);
40 EXPECT(!memcmp(out.data, "el", 2));
42 reftable_block_done(&out);
43 block_source_close(&source);
44 strbuf_release(&buf);
47 static void write_table(char ***names, struct strbuf *buf, int N,
48 int block_size, uint32_t hash_id)
50 struct reftable_write_options opts = {
51 .block_size = block_size,
52 .hash_id = hash_id,
54 struct reftable_writer *w =
55 reftable_new_writer(&strbuf_add_void, buf, &opts);
56 struct reftable_ref_record ref = { NULL };
57 int i = 0, n;
58 struct reftable_log_record log = { NULL };
59 const struct reftable_stats *stats = NULL;
60 *names = reftable_calloc(sizeof(char *) * (N + 1));
61 reftable_writer_set_limits(w, update_index, update_index);
62 for (i = 0; i < N; i++) {
63 uint8_t hash[GIT_SHA256_RAWSZ] = { 0 };
64 char name[100];
65 int n;
67 set_test_hash(hash, i);
69 snprintf(name, sizeof(name), "refs/heads/branch%02d", i);
71 ref.refname = name;
72 ref.update_index = update_index;
73 ref.value_type = REFTABLE_REF_VAL1;
74 ref.value.val1 = hash;
75 (*names)[i] = xstrdup(name);
77 n = reftable_writer_add_ref(w, &ref);
78 EXPECT(n == 0);
81 for (i = 0; i < N; i++) {
82 uint8_t hash[GIT_SHA256_RAWSZ] = { 0 };
83 char name[100];
84 int n;
86 set_test_hash(hash, i);
88 snprintf(name, sizeof(name), "refs/heads/branch%02d", i);
90 log.refname = name;
91 log.update_index = update_index;
92 log.value_type = REFTABLE_LOG_UPDATE;
93 log.value.update.new_hash = hash;
94 log.value.update.message = "message";
96 n = reftable_writer_add_log(w, &log);
97 EXPECT(n == 0);
100 n = reftable_writer_close(w);
101 EXPECT(n == 0);
103 stats = reftable_writer_stats(w);
104 for (i = 0; i < stats->ref_stats.blocks; i++) {
105 int off = i * opts.block_size;
106 if (off == 0) {
107 off = header_size(
108 (hash_id == GIT_SHA256_FORMAT_ID) ? 2 : 1);
110 EXPECT(buf->buf[off] == 'r');
113 EXPECT(stats->log_stats.blocks > 0);
114 reftable_writer_free(w);
117 static void test_log_buffer_size(void)
119 struct strbuf buf = STRBUF_INIT;
120 struct reftable_write_options opts = {
121 .block_size = 4096,
123 int err;
124 int i;
125 struct reftable_log_record
126 log = { .refname = "refs/heads/master",
127 .update_index = 0xa,
128 .value_type = REFTABLE_LOG_UPDATE,
129 .value = { .update = {
130 .name = "Han-Wen Nienhuys",
131 .email = "hanwen@google.com",
132 .tz_offset = 100,
133 .time = 0x5e430672,
134 .message = "commit: 9\n",
135 } } };
136 struct reftable_writer *w =
137 reftable_new_writer(&strbuf_add_void, &buf, &opts);
139 /* This tests buffer extension for log compression. Must use a random
140 hash, to ensure that the compressed part is larger than the original.
142 uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ];
143 for (i = 0; i < GIT_SHA1_RAWSZ; i++) {
144 hash1[i] = (uint8_t)(rand() % 256);
145 hash2[i] = (uint8_t)(rand() % 256);
147 log.value.update.old_hash = hash1;
148 log.value.update.new_hash = hash2;
149 reftable_writer_set_limits(w, update_index, update_index);
150 err = reftable_writer_add_log(w, &log);
151 EXPECT_ERR(err);
152 err = reftable_writer_close(w);
153 EXPECT_ERR(err);
154 reftable_writer_free(w);
155 strbuf_release(&buf);
158 static void test_log_overflow(void)
160 struct strbuf buf = STRBUF_INIT;
161 char msg[256] = { 0 };
162 struct reftable_write_options opts = {
163 .block_size = ARRAY_SIZE(msg),
165 int err;
166 struct reftable_log_record
167 log = { .refname = "refs/heads/master",
168 .update_index = 0xa,
169 .value_type = REFTABLE_LOG_UPDATE,
170 .value = { .update = {
171 .name = "Han-Wen Nienhuys",
172 .email = "hanwen@google.com",
173 .tz_offset = 100,
174 .time = 0x5e430672,
175 .message = msg,
176 } } };
177 struct reftable_writer *w =
178 reftable_new_writer(&strbuf_add_void, &buf, &opts);
180 uint8_t hash1[GIT_SHA1_RAWSZ] = {1}, hash2[GIT_SHA1_RAWSZ] = { 2 };
182 memset(msg, 'x', sizeof(msg) - 1);
183 log.value.update.old_hash = hash1;
184 log.value.update.new_hash = hash2;
185 reftable_writer_set_limits(w, update_index, update_index);
186 err = reftable_writer_add_log(w, &log);
187 EXPECT(err == REFTABLE_ENTRY_TOO_BIG_ERROR);
188 reftable_writer_free(w);
189 strbuf_release(&buf);
192 static void test_log_write_read(void)
194 int N = 2;
195 char **names = reftable_calloc(sizeof(char *) * (N + 1));
196 int err;
197 struct reftable_write_options opts = {
198 .block_size = 256,
200 struct reftable_ref_record ref = { NULL };
201 int i = 0;
202 struct reftable_log_record log = { NULL };
203 int n;
204 struct reftable_iterator it = { NULL };
205 struct reftable_reader rd = { NULL };
206 struct reftable_block_source source = { NULL };
207 struct strbuf buf = STRBUF_INIT;
208 struct reftable_writer *w =
209 reftable_new_writer(&strbuf_add_void, &buf, &opts);
210 const struct reftable_stats *stats = NULL;
211 reftable_writer_set_limits(w, 0, N);
212 for (i = 0; i < N; i++) {
213 char name[256];
214 struct reftable_ref_record ref = { NULL };
215 snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7);
216 names[i] = xstrdup(name);
217 ref.refname = name;
218 ref.update_index = i;
220 err = reftable_writer_add_ref(w, &ref);
221 EXPECT_ERR(err);
223 for (i = 0; i < N; i++) {
224 uint8_t hash1[GIT_SHA1_RAWSZ], hash2[GIT_SHA1_RAWSZ];
225 struct reftable_log_record log = { NULL };
226 set_test_hash(hash1, i);
227 set_test_hash(hash2, i + 1);
229 log.refname = names[i];
230 log.update_index = i;
231 log.value_type = REFTABLE_LOG_UPDATE;
232 log.value.update.old_hash = hash1;
233 log.value.update.new_hash = hash2;
235 err = reftable_writer_add_log(w, &log);
236 EXPECT_ERR(err);
239 n = reftable_writer_close(w);
240 EXPECT(n == 0);
242 stats = reftable_writer_stats(w);
243 EXPECT(stats->log_stats.blocks > 0);
244 reftable_writer_free(w);
245 w = NULL;
247 block_source_from_strbuf(&source, &buf);
249 err = init_reader(&rd, &source, "file.log");
250 EXPECT_ERR(err);
252 err = reftable_reader_seek_ref(&rd, &it, names[N - 1]);
253 EXPECT_ERR(err);
255 err = reftable_iterator_next_ref(&it, &ref);
256 EXPECT_ERR(err);
258 /* end of iteration. */
259 err = reftable_iterator_next_ref(&it, &ref);
260 EXPECT(0 < err);
262 reftable_iterator_destroy(&it);
263 reftable_ref_record_release(&ref);
265 err = reftable_reader_seek_log(&rd, &it, "");
266 EXPECT_ERR(err);
268 i = 0;
269 while (1) {
270 int err = reftable_iterator_next_log(&it, &log);
271 if (err > 0) {
272 break;
275 EXPECT_ERR(err);
276 EXPECT_STREQ(names[i], log.refname);
277 EXPECT(i == log.update_index);
278 i++;
279 reftable_log_record_release(&log);
282 EXPECT(i == N);
283 reftable_iterator_destroy(&it);
285 /* cleanup. */
286 strbuf_release(&buf);
287 free_names(names);
288 reader_close(&rd);
291 static void test_log_zlib_corruption(void)
293 struct reftable_write_options opts = {
294 .block_size = 256,
296 struct reftable_iterator it = { 0 };
297 struct reftable_reader rd = { 0 };
298 struct reftable_block_source source = { 0 };
299 struct strbuf buf = STRBUF_INIT;
300 struct reftable_writer *w =
301 reftable_new_writer(&strbuf_add_void, &buf, &opts);
302 const struct reftable_stats *stats = NULL;
303 uint8_t hash1[GIT_SHA1_RAWSZ] = { 1 };
304 uint8_t hash2[GIT_SHA1_RAWSZ] = { 2 };
305 char message[100] = { 0 };
306 int err, i, n;
308 struct reftable_log_record log = {
309 .refname = "refname",
310 .value_type = REFTABLE_LOG_UPDATE,
311 .value = {
312 .update = {
313 .new_hash = hash1,
314 .old_hash = hash2,
315 .name = "My Name",
316 .email = "myname@invalid",
317 .message = message,
322 for (i = 0; i < sizeof(message) - 1; i++)
323 message[i] = (uint8_t)(rand() % 64 + ' ');
325 reftable_writer_set_limits(w, 1, 1);
327 err = reftable_writer_add_log(w, &log);
328 EXPECT_ERR(err);
330 n = reftable_writer_close(w);
331 EXPECT(n == 0);
333 stats = reftable_writer_stats(w);
334 EXPECT(stats->log_stats.blocks > 0);
335 reftable_writer_free(w);
336 w = NULL;
338 /* corrupt the data. */
339 buf.buf[50] ^= 0x99;
341 block_source_from_strbuf(&source, &buf);
343 err = init_reader(&rd, &source, "file.log");
344 EXPECT_ERR(err);
346 err = reftable_reader_seek_log(&rd, &it, "refname");
347 EXPECT(err == REFTABLE_ZLIB_ERROR);
349 reftable_iterator_destroy(&it);
351 /* cleanup. */
352 strbuf_release(&buf);
353 reader_close(&rd);
356 static void test_table_read_write_sequential(void)
358 char **names;
359 struct strbuf buf = STRBUF_INIT;
360 int N = 50;
361 struct reftable_iterator it = { NULL };
362 struct reftable_block_source source = { NULL };
363 struct reftable_reader rd = { NULL };
364 int err = 0;
365 int j = 0;
367 write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID);
369 block_source_from_strbuf(&source, &buf);
371 err = init_reader(&rd, &source, "file.ref");
372 EXPECT_ERR(err);
374 err = reftable_reader_seek_ref(&rd, &it, "");
375 EXPECT_ERR(err);
377 while (1) {
378 struct reftable_ref_record ref = { NULL };
379 int r = reftable_iterator_next_ref(&it, &ref);
380 EXPECT(r >= 0);
381 if (r > 0) {
382 break;
384 EXPECT(0 == strcmp(names[j], ref.refname));
385 EXPECT(update_index == ref.update_index);
387 j++;
388 reftable_ref_record_release(&ref);
390 EXPECT(j == N);
391 reftable_iterator_destroy(&it);
392 strbuf_release(&buf);
393 free_names(names);
395 reader_close(&rd);
398 static void test_table_write_small_table(void)
400 char **names;
401 struct strbuf buf = STRBUF_INIT;
402 int N = 1;
403 write_table(&names, &buf, N, 4096, GIT_SHA1_FORMAT_ID);
404 EXPECT(buf.len < 200);
405 strbuf_release(&buf);
406 free_names(names);
409 static void test_table_read_api(void)
411 char **names;
412 struct strbuf buf = STRBUF_INIT;
413 int N = 50;
414 struct reftable_reader rd = { NULL };
415 struct reftable_block_source source = { NULL };
416 int err;
417 int i;
418 struct reftable_log_record log = { NULL };
419 struct reftable_iterator it = { NULL };
421 write_table(&names, &buf, N, 256, GIT_SHA1_FORMAT_ID);
423 block_source_from_strbuf(&source, &buf);
425 err = init_reader(&rd, &source, "file.ref");
426 EXPECT_ERR(err);
428 err = reftable_reader_seek_ref(&rd, &it, names[0]);
429 EXPECT_ERR(err);
431 err = reftable_iterator_next_log(&it, &log);
432 EXPECT(err == REFTABLE_API_ERROR);
434 strbuf_release(&buf);
435 for (i = 0; i < N; i++) {
436 reftable_free(names[i]);
438 reftable_iterator_destroy(&it);
439 reftable_free(names);
440 reader_close(&rd);
441 strbuf_release(&buf);
444 static void test_table_read_write_seek(int index, int hash_id)
446 char **names;
447 struct strbuf buf = STRBUF_INIT;
448 int N = 50;
449 struct reftable_reader rd = { NULL };
450 struct reftable_block_source source = { NULL };
451 int err;
452 int i = 0;
454 struct reftable_iterator it = { NULL };
455 struct strbuf pastLast = STRBUF_INIT;
456 struct reftable_ref_record ref = { NULL };
458 write_table(&names, &buf, N, 256, hash_id);
460 block_source_from_strbuf(&source, &buf);
462 err = init_reader(&rd, &source, "file.ref");
463 EXPECT_ERR(err);
464 EXPECT(hash_id == reftable_reader_hash_id(&rd));
466 if (!index) {
467 rd.ref_offsets.index_offset = 0;
468 } else {
469 EXPECT(rd.ref_offsets.index_offset > 0);
472 for (i = 1; i < N; i++) {
473 int err = reftable_reader_seek_ref(&rd, &it, names[i]);
474 EXPECT_ERR(err);
475 err = reftable_iterator_next_ref(&it, &ref);
476 EXPECT_ERR(err);
477 EXPECT(0 == strcmp(names[i], ref.refname));
478 EXPECT(REFTABLE_REF_VAL1 == ref.value_type);
479 EXPECT(i == ref.value.val1[0]);
481 reftable_ref_record_release(&ref);
482 reftable_iterator_destroy(&it);
485 strbuf_addstr(&pastLast, names[N - 1]);
486 strbuf_addstr(&pastLast, "/");
488 err = reftable_reader_seek_ref(&rd, &it, pastLast.buf);
489 if (err == 0) {
490 struct reftable_ref_record ref = { NULL };
491 int err = reftable_iterator_next_ref(&it, &ref);
492 EXPECT(err > 0);
493 } else {
494 EXPECT(err > 0);
497 strbuf_release(&pastLast);
498 reftable_iterator_destroy(&it);
500 strbuf_release(&buf);
501 for (i = 0; i < N; i++) {
502 reftable_free(names[i]);
504 reftable_free(names);
505 reader_close(&rd);
508 static void test_table_read_write_seek_linear(void)
510 test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID);
513 static void test_table_read_write_seek_linear_sha256(void)
515 test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID);
518 static void test_table_read_write_seek_index(void)
520 test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID);
523 static void test_table_refs_for(int indexed)
525 int N = 50;
526 char **want_names = reftable_calloc(sizeof(char *) * (N + 1));
527 int want_names_len = 0;
528 uint8_t want_hash[GIT_SHA1_RAWSZ];
530 struct reftable_write_options opts = {
531 .block_size = 256,
533 struct reftable_ref_record ref = { NULL };
534 int i = 0;
535 int n;
536 int err;
537 struct reftable_reader rd;
538 struct reftable_block_source source = { NULL };
540 struct strbuf buf = STRBUF_INIT;
541 struct reftable_writer *w =
542 reftable_new_writer(&strbuf_add_void, &buf, &opts);
544 struct reftable_iterator it = { NULL };
545 int j;
547 set_test_hash(want_hash, 4);
549 for (i = 0; i < N; i++) {
550 uint8_t hash[GIT_SHA1_RAWSZ];
551 char fill[51] = { 0 };
552 char name[100];
553 uint8_t hash1[GIT_SHA1_RAWSZ];
554 uint8_t hash2[GIT_SHA1_RAWSZ];
555 struct reftable_ref_record ref = { NULL };
557 memset(hash, i, sizeof(hash));
558 memset(fill, 'x', 50);
559 /* Put the variable part in the start */
560 snprintf(name, sizeof(name), "br%02d%s", i, fill);
561 name[40] = 0;
562 ref.refname = name;
564 set_test_hash(hash1, i / 4);
565 set_test_hash(hash2, 3 + i / 4);
566 ref.value_type = REFTABLE_REF_VAL2;
567 ref.value.val2.value = hash1;
568 ref.value.val2.target_value = hash2;
570 /* 80 bytes / entry, so 3 entries per block. Yields 17
572 /* blocks. */
573 n = reftable_writer_add_ref(w, &ref);
574 EXPECT(n == 0);
576 if (!memcmp(hash1, want_hash, GIT_SHA1_RAWSZ) ||
577 !memcmp(hash2, want_hash, GIT_SHA1_RAWSZ)) {
578 want_names[want_names_len++] = xstrdup(name);
582 n = reftable_writer_close(w);
583 EXPECT(n == 0);
585 reftable_writer_free(w);
586 w = NULL;
588 block_source_from_strbuf(&source, &buf);
590 err = init_reader(&rd, &source, "file.ref");
591 EXPECT_ERR(err);
592 if (!indexed) {
593 rd.obj_offsets.is_present = 0;
596 err = reftable_reader_seek_ref(&rd, &it, "");
597 EXPECT_ERR(err);
598 reftable_iterator_destroy(&it);
600 err = reftable_reader_refs_for(&rd, &it, want_hash);
601 EXPECT_ERR(err);
603 j = 0;
604 while (1) {
605 int err = reftable_iterator_next_ref(&it, &ref);
606 EXPECT(err >= 0);
607 if (err > 0) {
608 break;
611 EXPECT(j < want_names_len);
612 EXPECT(0 == strcmp(ref.refname, want_names[j]));
613 j++;
614 reftable_ref_record_release(&ref);
616 EXPECT(j == want_names_len);
618 strbuf_release(&buf);
619 free_names(want_names);
620 reftable_iterator_destroy(&it);
621 reader_close(&rd);
624 static void test_table_refs_for_no_index(void)
626 test_table_refs_for(0);
629 static void test_table_refs_for_obj_index(void)
631 test_table_refs_for(1);
634 static void test_write_empty_table(void)
636 struct reftable_write_options opts = { 0 };
637 struct strbuf buf = STRBUF_INIT;
638 struct reftable_writer *w =
639 reftable_new_writer(&strbuf_add_void, &buf, &opts);
640 struct reftable_block_source source = { NULL };
641 struct reftable_reader *rd = NULL;
642 struct reftable_ref_record rec = { NULL };
643 struct reftable_iterator it = { NULL };
644 int err;
646 reftable_writer_set_limits(w, 1, 1);
648 err = reftable_writer_close(w);
649 EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
650 reftable_writer_free(w);
652 EXPECT(buf.len == header_size(1) + footer_size(1));
654 block_source_from_strbuf(&source, &buf);
656 err = reftable_new_reader(&rd, &source, "filename");
657 EXPECT_ERR(err);
659 err = reftable_reader_seek_ref(rd, &it, "");
660 EXPECT_ERR(err);
662 err = reftable_iterator_next_ref(&it, &rec);
663 EXPECT(err > 0);
665 reftable_iterator_destroy(&it);
666 reftable_reader_free(rd);
667 strbuf_release(&buf);
670 static void test_write_object_id_min_length(void)
672 struct reftable_write_options opts = {
673 .block_size = 75,
675 struct strbuf buf = STRBUF_INIT;
676 struct reftable_writer *w =
677 reftable_new_writer(&strbuf_add_void, &buf, &opts);
678 uint8_t hash[GIT_SHA1_RAWSZ] = {42};
679 struct reftable_ref_record ref = {
680 .update_index = 1,
681 .value_type = REFTABLE_REF_VAL1,
682 .value.val1 = hash,
684 int err;
685 int i;
687 reftable_writer_set_limits(w, 1, 1);
689 /* Write the same hash in many refs. If there is only 1 hash, the
690 * disambiguating prefix is length 0 */
691 for (i = 0; i < 256; i++) {
692 char name[256];
693 snprintf(name, sizeof(name), "ref%05d", i);
694 ref.refname = name;
695 err = reftable_writer_add_ref(w, &ref);
696 EXPECT_ERR(err);
699 err = reftable_writer_close(w);
700 EXPECT_ERR(err);
701 EXPECT(reftable_writer_stats(w)->object_id_len == 2);
702 reftable_writer_free(w);
703 strbuf_release(&buf);
706 static void test_write_object_id_length(void)
708 struct reftable_write_options opts = {
709 .block_size = 75,
711 struct strbuf buf = STRBUF_INIT;
712 struct reftable_writer *w =
713 reftable_new_writer(&strbuf_add_void, &buf, &opts);
714 uint8_t hash[GIT_SHA1_RAWSZ] = {42};
715 struct reftable_ref_record ref = {
716 .update_index = 1,
717 .value_type = REFTABLE_REF_VAL1,
718 .value.val1 = hash,
720 int err;
721 int i;
723 reftable_writer_set_limits(w, 1, 1);
725 /* Write the same hash in many refs. If there is only 1 hash, the
726 * disambiguating prefix is length 0 */
727 for (i = 0; i < 256; i++) {
728 char name[256];
729 snprintf(name, sizeof(name), "ref%05d", i);
730 ref.refname = name;
731 ref.value.val1[15] = i;
732 err = reftable_writer_add_ref(w, &ref);
733 EXPECT_ERR(err);
736 err = reftable_writer_close(w);
737 EXPECT_ERR(err);
738 EXPECT(reftable_writer_stats(w)->object_id_len == 16);
739 reftable_writer_free(w);
740 strbuf_release(&buf);
743 static void test_write_empty_key(void)
745 struct reftable_write_options opts = { 0 };
746 struct strbuf buf = STRBUF_INIT;
747 struct reftable_writer *w =
748 reftable_new_writer(&strbuf_add_void, &buf, &opts);
749 struct reftable_ref_record ref = {
750 .refname = "",
751 .update_index = 1,
752 .value_type = REFTABLE_REF_DELETION,
754 int err;
756 reftable_writer_set_limits(w, 1, 1);
757 err = reftable_writer_add_ref(w, &ref);
758 EXPECT(err == REFTABLE_API_ERROR);
760 err = reftable_writer_close(w);
761 EXPECT(err == REFTABLE_EMPTY_TABLE_ERROR);
762 reftable_writer_free(w);
763 strbuf_release(&buf);
766 static void test_write_key_order(void)
768 struct reftable_write_options opts = { 0 };
769 struct strbuf buf = STRBUF_INIT;
770 struct reftable_writer *w =
771 reftable_new_writer(&strbuf_add_void, &buf, &opts);
772 struct reftable_ref_record refs[2] = {
774 .refname = "b",
775 .update_index = 1,
776 .value_type = REFTABLE_REF_SYMREF,
777 .value = {
778 .symref = "target",
780 }, {
781 .refname = "a",
782 .update_index = 1,
783 .value_type = REFTABLE_REF_SYMREF,
784 .value = {
785 .symref = "target",
789 int err;
791 reftable_writer_set_limits(w, 1, 1);
792 err = reftable_writer_add_ref(w, &refs[0]);
793 EXPECT_ERR(err);
794 err = reftable_writer_add_ref(w, &refs[1]);
795 EXPECT(err == REFTABLE_API_ERROR);
796 reftable_writer_close(w);
797 reftable_writer_free(w);
798 strbuf_release(&buf);
801 static void test_corrupt_table_empty(void)
803 struct strbuf buf = STRBUF_INIT;
804 struct reftable_block_source source = { NULL };
805 struct reftable_reader rd = { NULL };
806 int err;
808 block_source_from_strbuf(&source, &buf);
809 err = init_reader(&rd, &source, "file.log");
810 EXPECT(err == REFTABLE_FORMAT_ERROR);
813 static void test_corrupt_table(void)
815 uint8_t zeros[1024] = { 0 };
816 struct strbuf buf = STRBUF_INIT;
817 struct reftable_block_source source = { NULL };
818 struct reftable_reader rd = { NULL };
819 int err;
820 strbuf_add(&buf, zeros, sizeof(zeros));
822 block_source_from_strbuf(&source, &buf);
823 err = init_reader(&rd, &source, "file.log");
824 EXPECT(err == REFTABLE_FORMAT_ERROR);
825 strbuf_release(&buf);
828 int readwrite_test_main(int argc, const char *argv[])
830 RUN_TEST(test_log_zlib_corruption);
831 RUN_TEST(test_corrupt_table);
832 RUN_TEST(test_corrupt_table_empty);
833 RUN_TEST(test_log_write_read);
834 RUN_TEST(test_write_key_order);
835 RUN_TEST(test_table_read_write_seek_linear_sha256);
836 RUN_TEST(test_log_buffer_size);
837 RUN_TEST(test_table_write_small_table);
838 RUN_TEST(test_buffer);
839 RUN_TEST(test_table_read_api);
840 RUN_TEST(test_table_read_write_sequential);
841 RUN_TEST(test_table_read_write_seek_linear);
842 RUN_TEST(test_table_read_write_seek_index);
843 RUN_TEST(test_table_refs_for_no_index);
844 RUN_TEST(test_table_refs_for_obj_index);
845 RUN_TEST(test_write_empty_key);
846 RUN_TEST(test_write_empty_table);
847 RUN_TEST(test_log_overflow);
848 RUN_TEST(test_write_object_id_length);
849 RUN_TEST(test_write_object_id_min_length);
850 return 0;