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"
14 #include "constants.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
};
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);
40 EXPECT(!memcmp(out
.data
, "el", 2));
42 reftable_block_done(&out
);
43 block_source_close(&source
);
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
,
54 struct reftable_writer
*w
=
55 reftable_new_writer(&strbuf_add_void
, buf
, &opts
);
56 struct reftable_ref_record ref
= { NULL
};
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 };
67 set_test_hash(hash
, i
);
69 snprintf(name
, sizeof(name
), "refs/heads/branch%02d", i
);
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
);
81 for (i
= 0; i
< N
; i
++) {
82 uint8_t hash
[GIT_SHA256_RAWSZ
] = { 0 };
86 set_test_hash(hash
, i
);
88 snprintf(name
, sizeof(name
), "refs/heads/branch%02d", i
);
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
);
100 n
= reftable_writer_close(w
);
103 stats
= reftable_writer_stats(w
);
104 for (i
= 0; i
< stats
->ref_stats
.blocks
; i
++) {
105 int off
= i
* opts
.block_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
= {
125 struct reftable_log_record
126 log
= { .refname
= "refs/heads/master",
128 .value_type
= REFTABLE_LOG_UPDATE
,
129 .value
= { .update
= {
130 .name
= "Han-Wen Nienhuys",
131 .email
= "hanwen@google.com",
134 .message
= "commit: 9\n",
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
);
152 err
= reftable_writer_close(w
);
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
),
166 struct reftable_log_record
167 log
= { .refname
= "refs/heads/master",
169 .value_type
= REFTABLE_LOG_UPDATE
,
170 .value
= { .update
= {
171 .name
= "Han-Wen Nienhuys",
172 .email
= "hanwen@google.com",
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)
195 char **names
= reftable_calloc(sizeof(char *) * (N
+ 1));
197 struct reftable_write_options opts
= {
200 struct reftable_ref_record ref
= { NULL
};
202 struct reftable_log_record log
= { NULL
};
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
++) {
214 struct reftable_ref_record ref
= { NULL
};
215 snprintf(name
, sizeof(name
), "b%02d%0*d", i
, 130, 7);
216 names
[i
] = xstrdup(name
);
218 ref
.update_index
= i
;
220 err
= reftable_writer_add_ref(w
, &ref
);
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
);
239 n
= reftable_writer_close(w
);
242 stats
= reftable_writer_stats(w
);
243 EXPECT(stats
->log_stats
.blocks
> 0);
244 reftable_writer_free(w
);
247 block_source_from_strbuf(&source
, &buf
);
249 err
= init_reader(&rd
, &source
, "file.log");
252 err
= reftable_reader_seek_ref(&rd
, &it
, names
[N
- 1]);
255 err
= reftable_iterator_next_ref(&it
, &ref
);
258 /* end of iteration. */
259 err
= reftable_iterator_next_ref(&it
, &ref
);
262 reftable_iterator_destroy(&it
);
263 reftable_ref_record_release(&ref
);
265 err
= reftable_reader_seek_log(&rd
, &it
, "");
270 int err
= reftable_iterator_next_log(&it
, &log
);
276 EXPECT_STREQ(names
[i
], log
.refname
);
277 EXPECT(i
== log
.update_index
);
279 reftable_log_record_release(&log
);
283 reftable_iterator_destroy(&it
);
286 strbuf_release(&buf
);
291 static void test_log_zlib_corruption(void)
293 struct reftable_write_options opts
= {
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 };
308 struct reftable_log_record log
= {
309 .refname
= "refname",
310 .value_type
= REFTABLE_LOG_UPDATE
,
316 .email
= "myname@invalid",
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
);
330 n
= reftable_writer_close(w
);
333 stats
= reftable_writer_stats(w
);
334 EXPECT(stats
->log_stats
.blocks
> 0);
335 reftable_writer_free(w
);
338 /* corrupt the data. */
341 block_source_from_strbuf(&source
, &buf
);
343 err
= init_reader(&rd
, &source
, "file.log");
346 err
= reftable_reader_seek_log(&rd
, &it
, "refname");
347 EXPECT(err
== REFTABLE_ZLIB_ERROR
);
349 reftable_iterator_destroy(&it
);
352 strbuf_release(&buf
);
356 static void test_table_read_write_sequential(void)
359 struct strbuf buf
= STRBUF_INIT
;
361 struct reftable_iterator it
= { NULL
};
362 struct reftable_block_source source
= { NULL
};
363 struct reftable_reader rd
= { NULL
};
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");
374 err
= reftable_reader_seek_ref(&rd
, &it
, "");
378 struct reftable_ref_record ref
= { NULL
};
379 int r
= reftable_iterator_next_ref(&it
, &ref
);
384 EXPECT(0 == strcmp(names
[j
], ref
.refname
));
385 EXPECT(update_index
== ref
.update_index
);
388 reftable_ref_record_release(&ref
);
391 reftable_iterator_destroy(&it
);
392 strbuf_release(&buf
);
398 static void test_table_write_small_table(void)
401 struct strbuf buf
= STRBUF_INIT
;
403 write_table(&names
, &buf
, N
, 4096, GIT_SHA1_FORMAT_ID
);
404 EXPECT(buf
.len
< 200);
405 strbuf_release(&buf
);
409 static void test_table_read_api(void)
412 struct strbuf buf
= STRBUF_INIT
;
414 struct reftable_reader rd
= { NULL
};
415 struct reftable_block_source source
= { NULL
};
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");
428 err
= reftable_reader_seek_ref(&rd
, &it
, names
[0]);
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
);
441 strbuf_release(&buf
);
444 static void test_table_read_write_seek(int index
, int hash_id
)
447 struct strbuf buf
= STRBUF_INIT
;
449 struct reftable_reader rd
= { NULL
};
450 struct reftable_block_source source
= { NULL
};
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");
464 EXPECT(hash_id
== reftable_reader_hash_id(&rd
));
467 rd
.ref_offsets
.index_offset
= 0;
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
]);
475 err
= reftable_iterator_next_ref(&it
, &ref
);
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
);
490 struct reftable_ref_record ref
= { NULL
};
491 int err
= reftable_iterator_next_ref(&it
, &ref
);
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
);
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
)
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
= {
533 struct reftable_ref_record ref
= { NULL
};
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
};
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 };
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
);
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
573 n
= reftable_writer_add_ref(w
, &ref
);
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
);
585 reftable_writer_free(w
);
588 block_source_from_strbuf(&source
, &buf
);
590 err
= init_reader(&rd
, &source
, "file.ref");
593 rd
.obj_offsets
.is_present
= 0;
596 err
= reftable_reader_seek_ref(&rd
, &it
, "");
598 reftable_iterator_destroy(&it
);
600 err
= reftable_reader_refs_for(&rd
, &it
, want_hash
);
605 int err
= reftable_iterator_next_ref(&it
, &ref
);
611 EXPECT(j
< want_names_len
);
612 EXPECT(0 == strcmp(ref
.refname
, want_names
[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
);
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
};
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");
659 err
= reftable_reader_seek_ref(rd
, &it
, "");
662 err
= reftable_iterator_next_ref(&it
, &rec
);
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
= {
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
= {
681 .value_type
= REFTABLE_REF_VAL1
,
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
++) {
693 snprintf(name
, sizeof(name
), "ref%05d", i
);
695 err
= reftable_writer_add_ref(w
, &ref
);
699 err
= reftable_writer_close(w
);
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
= {
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
= {
717 .value_type
= REFTABLE_REF_VAL1
,
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
++) {
729 snprintf(name
, sizeof(name
), "ref%05d", i
);
731 ref
.value
.val1
[15] = i
;
732 err
= reftable_writer_add_ref(w
, &ref
);
736 err
= reftable_writer_close(w
);
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
= {
752 .value_type
= REFTABLE_REF_DELETION
,
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] = {
776 .value_type
= REFTABLE_REF_SYMREF
,
783 .value_type
= REFTABLE_REF_SYMREF
,
791 reftable_writer_set_limits(w
, 1, 1);
792 err
= reftable_writer_add_ref(w
, &refs
[0]);
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
};
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
};
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
);