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
13 #include "blocksource.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
};
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);
39 EXPECT(!memcmp(out
.data
, "el", 2));
41 reftable_block_done(&out
);
42 block_source_close(&source
);
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
,
53 struct reftable_writer
*w
=
54 reftable_new_writer(&strbuf_add_void
, &noop_flush
, buf
, &opts
);
55 struct reftable_ref_record ref
= { NULL
};
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
++) {
67 snprintf(name
, sizeof(name
), "refs/heads/branch%02d", i
);
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
);
79 for (i
= 0; i
< N
; i
++) {
80 uint8_t hash
[GIT_SHA256_RAWSZ
] = { 0 };
84 set_test_hash(hash
, i
);
86 snprintf(name
, sizeof(name
), "refs/heads/branch%02d", i
);
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
);
98 n
= reftable_writer_close(w
);
101 stats
= reftable_writer_stats(w
);
102 for (i
= 0; i
< stats
->ref_stats
.blocks
; i
++) {
103 int off
= i
* opts
.block_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
= {
123 struct reftable_log_record
124 log
= { .refname
= "refs/heads/master",
126 .value_type
= REFTABLE_LOG_UPDATE
,
127 .value
= { .update
= {
128 .name
= "Han-Wen Nienhuys",
129 .email
= "hanwen@google.com",
132 .message
= "commit: 9\n",
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
);
150 err
= reftable_writer_close(w
);
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
),
164 struct reftable_log_record
165 log
= { .refname
= "refs/heads/master",
167 .value_type
= REFTABLE_LOG_UPDATE
,
168 .value
= { .update
= {
169 .name
= "Han-Wen Nienhuys",
170 .email
= "hanwen@google.com",
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)
193 char **names
= reftable_calloc(N
+ 1, sizeof(*names
));
195 struct reftable_write_options opts
= {
198 struct reftable_ref_record ref
= { NULL
};
200 struct reftable_log_record log
= { NULL
};
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
++) {
212 struct reftable_ref_record ref
= { NULL
};
213 snprintf(name
, sizeof(name
), "b%02d%0*d", i
, 130, 7);
214 names
[i
] = xstrdup(name
);
216 ref
.update_index
= i
;
218 err
= reftable_writer_add_ref(w
, &ref
);
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
);
237 n
= reftable_writer_close(w
);
240 stats
= reftable_writer_stats(w
);
241 EXPECT(stats
->log_stats
.blocks
> 0);
242 reftable_writer_free(w
);
245 block_source_from_strbuf(&source
, &buf
);
247 err
= init_reader(&rd
, &source
, "file.log");
250 err
= reftable_reader_seek_ref(&rd
, &it
, names
[N
- 1]);
253 err
= reftable_iterator_next_ref(&it
, &ref
);
256 /* end of iteration. */
257 err
= reftable_iterator_next_ref(&it
, &ref
);
260 reftable_iterator_destroy(&it
);
261 reftable_ref_record_release(&ref
);
263 err
= reftable_reader_seek_log(&rd
, &it
, "");
268 int err
= reftable_iterator_next_log(&it
, &log
);
274 EXPECT_STREQ(names
[i
], log
.refname
);
275 EXPECT(i
== log
.update_index
);
277 reftable_log_record_release(&log
);
281 reftable_iterator_destroy(&it
);
284 strbuf_release(&buf
);
289 static void test_log_zlib_corruption(void)
291 struct reftable_write_options opts
= {
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 };
306 struct reftable_log_record log
= {
307 .refname
= "refname",
308 .value_type
= REFTABLE_LOG_UPDATE
,
314 .email
= "myname@invalid",
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
);
328 n
= reftable_writer_close(w
);
331 stats
= reftable_writer_stats(w
);
332 EXPECT(stats
->log_stats
.blocks
> 0);
333 reftable_writer_free(w
);
336 /* corrupt the data. */
339 block_source_from_strbuf(&source
, &buf
);
341 err
= init_reader(&rd
, &source
, "file.log");
344 err
= reftable_reader_seek_log(&rd
, &it
, "refname");
345 EXPECT(err
== REFTABLE_ZLIB_ERROR
);
347 reftable_iterator_destroy(&it
);
350 strbuf_release(&buf
);
354 static void test_table_read_write_sequential(void)
357 struct strbuf buf
= STRBUF_INIT
;
359 struct reftable_iterator it
= { NULL
};
360 struct reftable_block_source source
= { NULL
};
361 struct reftable_reader rd
= { NULL
};
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");
372 err
= reftable_reader_seek_ref(&rd
, &it
, "");
376 struct reftable_ref_record ref
= { NULL
};
377 int r
= reftable_iterator_next_ref(&it
, &ref
);
382 EXPECT(0 == strcmp(names
[j
], ref
.refname
));
383 EXPECT(update_index
== ref
.update_index
);
386 reftable_ref_record_release(&ref
);
389 reftable_iterator_destroy(&it
);
390 strbuf_release(&buf
);
396 static void test_table_write_small_table(void)
399 struct strbuf buf
= STRBUF_INIT
;
401 write_table(&names
, &buf
, N
, 4096, GIT_SHA1_FORMAT_ID
);
402 EXPECT(buf
.len
< 200);
403 strbuf_release(&buf
);
407 static void test_table_read_api(void)
410 struct strbuf buf
= STRBUF_INIT
;
412 struct reftable_reader rd
= { NULL
};
413 struct reftable_block_source source
= { NULL
};
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");
426 err
= reftable_reader_seek_ref(&rd
, &it
, names
[0]);
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
);
439 strbuf_release(&buf
);
442 static void test_table_read_write_seek(int index
, int hash_id
)
445 struct strbuf buf
= STRBUF_INIT
;
447 struct reftable_reader rd
= { NULL
};
448 struct reftable_block_source source
= { NULL
};
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");
462 EXPECT(hash_id
== reftable_reader_hash_id(&rd
));
465 rd
.ref_offsets
.index_offset
= 0;
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
]);
473 err
= reftable_iterator_next_ref(&it
, &ref
);
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
);
488 struct reftable_ref_record ref
= { NULL
};
489 int err
= reftable_iterator_next_ref(&it
, &ref
);
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
);
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
)
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
= {
531 struct reftable_ref_record ref
= { NULL
};
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
};
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 };
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
);
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
567 n
= reftable_writer_add_ref(w
, &ref
);
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
);
579 reftable_writer_free(w
);
582 block_source_from_strbuf(&source
, &buf
);
584 err
= init_reader(&rd
, &source
, "file.ref");
587 rd
.obj_offsets
.is_present
= 0;
590 err
= reftable_reader_seek_ref(&rd
, &it
, "");
592 reftable_iterator_destroy(&it
);
594 err
= reftable_reader_refs_for(&rd
, &it
, want_hash
);
599 int err
= reftable_iterator_next_ref(&it
, &ref
);
605 EXPECT(j
< want_names_len
);
606 EXPECT(0 == strcmp(ref
.refname
, want_names
[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
);
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
};
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");
653 err
= reftable_reader_seek_ref(rd
, &it
, "");
656 err
= reftable_iterator_next_ref(&it
, &rec
);
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
= {
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
= {
674 .value_type
= REFTABLE_REF_VAL1
,
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
++) {
686 snprintf(name
, sizeof(name
), "ref%05d", i
);
688 err
= reftable_writer_add_ref(w
, &ref
);
692 err
= reftable_writer_close(w
);
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
= {
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
= {
709 .value_type
= REFTABLE_REF_VAL1
,
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
++) {
721 snprintf(name
, sizeof(name
), "ref%05d", i
);
723 ref
.value
.val1
[15] = i
;
724 err
= reftable_writer_add_ref(w
, &ref
);
728 err
= reftable_writer_close(w
);
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
= {
744 .value_type
= REFTABLE_REF_DELETION
,
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] = {
768 .value_type
= REFTABLE_REF_SYMREF
,
775 .value_type
= REFTABLE_REF_SYMREF
,
783 reftable_writer_set_limits(w
, 1, 1);
784 err
= reftable_writer_add_ref(w
, &refs
[0]);
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
= {
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
;
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
= {
811 .value_type
= REFTABLE_REF_VAL1
,
816 strbuf_addf(&buf
, "refs/heads/%04d", i
);
817 ref
.refname
= buf
.buf
,
819 err
= reftable_writer_add_ref(writer
, &ref
);
823 for (i
= 0; i
< 100; i
++) {
824 unsigned char hash
[GIT_SHA1_RAWSZ
] = {i
};
825 struct reftable_log_record log
= {
827 .value_type
= REFTABLE_LOG_UPDATE
,
835 strbuf_addf(&buf
, "refs/heads/%04d", i
);
836 log
.refname
= buf
.buf
,
838 err
= reftable_writer_add_log(writer
, &log
);
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");
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
, "");
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
= {
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
;
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
= {
889 .value_type
= REFTABLE_REF_VAL1
,
894 strbuf_addf(&buf
, "refs/heads/%03" PRIuMAX
, (uintmax_t)i
);
895 ref
.refname
= buf
.buf
,
897 err
= reftable_writer_add_ref(writer
, &ref
);
900 reftable_writer_close(writer
);
903 * The written refs should be sufficiently large to result in a
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");
914 * Seeking the last ref should work as expected.
916 err
= reftable_reader_seek_ref(reader
, &it
, "refs/heads/199");
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
};
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
};
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
);