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
= 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_write_read(void)
161 char **names
= reftable_calloc(sizeof(char *) * (N
+ 1));
163 struct reftable_write_options opts
= {
166 struct reftable_ref_record ref
= { NULL
};
168 struct reftable_log_record log
= { NULL
};
170 struct reftable_iterator it
= { NULL
};
171 struct reftable_reader rd
= { NULL
};
172 struct reftable_block_source source
= { NULL
};
173 struct strbuf buf
= STRBUF_INIT
;
174 struct reftable_writer
*w
=
175 reftable_new_writer(&strbuf_add_void
, &buf
, &opts
);
176 const struct reftable_stats
*stats
= NULL
;
177 reftable_writer_set_limits(w
, 0, N
);
178 for (i
= 0; i
< N
; i
++) {
180 struct reftable_ref_record ref
= { NULL
};
181 snprintf(name
, sizeof(name
), "b%02d%0*d", i
, 130, 7);
182 names
[i
] = xstrdup(name
);
184 ref
.update_index
= i
;
186 err
= reftable_writer_add_ref(w
, &ref
);
189 for (i
= 0; i
< N
; i
++) {
190 uint8_t hash1
[GIT_SHA1_RAWSZ
], hash2
[GIT_SHA1_RAWSZ
];
191 struct reftable_log_record log
= { NULL
};
192 set_test_hash(hash1
, i
);
193 set_test_hash(hash2
, i
+ 1);
195 log
.refname
= names
[i
];
196 log
.update_index
= i
;
197 log
.value_type
= REFTABLE_LOG_UPDATE
;
198 log
.value
.update
.old_hash
= hash1
;
199 log
.value
.update
.new_hash
= hash2
;
201 err
= reftable_writer_add_log(w
, &log
);
205 n
= reftable_writer_close(w
);
208 stats
= writer_stats(w
);
209 EXPECT(stats
->log_stats
.blocks
> 0);
210 reftable_writer_free(w
);
213 block_source_from_strbuf(&source
, &buf
);
215 err
= init_reader(&rd
, &source
, "file.log");
218 err
= reftable_reader_seek_ref(&rd
, &it
, names
[N
- 1]);
221 err
= reftable_iterator_next_ref(&it
, &ref
);
224 /* end of iteration. */
225 err
= reftable_iterator_next_ref(&it
, &ref
);
228 reftable_iterator_destroy(&it
);
229 reftable_ref_record_release(&ref
);
231 err
= reftable_reader_seek_log(&rd
, &it
, "");
236 int err
= reftable_iterator_next_log(&it
, &log
);
242 EXPECT_STREQ(names
[i
], log
.refname
);
243 EXPECT(i
== log
.update_index
);
245 reftable_log_record_release(&log
);
249 reftable_iterator_destroy(&it
);
252 strbuf_release(&buf
);
257 static void test_table_read_write_sequential(void)
260 struct strbuf buf
= STRBUF_INIT
;
262 struct reftable_iterator it
= { NULL
};
263 struct reftable_block_source source
= { NULL
};
264 struct reftable_reader rd
= { NULL
};
268 write_table(&names
, &buf
, N
, 256, GIT_SHA1_FORMAT_ID
);
270 block_source_from_strbuf(&source
, &buf
);
272 err
= init_reader(&rd
, &source
, "file.ref");
275 err
= reftable_reader_seek_ref(&rd
, &it
, "");
279 struct reftable_ref_record ref
= { NULL
};
280 int r
= reftable_iterator_next_ref(&it
, &ref
);
285 EXPECT(0 == strcmp(names
[j
], ref
.refname
));
286 EXPECT(update_index
== ref
.update_index
);
289 reftable_ref_record_release(&ref
);
292 reftable_iterator_destroy(&it
);
293 strbuf_release(&buf
);
299 static void test_table_write_small_table(void)
302 struct strbuf buf
= STRBUF_INIT
;
304 write_table(&names
, &buf
, N
, 4096, GIT_SHA1_FORMAT_ID
);
305 EXPECT(buf
.len
< 200);
306 strbuf_release(&buf
);
310 static void test_table_read_api(void)
313 struct strbuf buf
= STRBUF_INIT
;
315 struct reftable_reader rd
= { NULL
};
316 struct reftable_block_source source
= { NULL
};
319 struct reftable_log_record log
= { NULL
};
320 struct reftable_iterator it
= { NULL
};
322 write_table(&names
, &buf
, N
, 256, GIT_SHA1_FORMAT_ID
);
324 block_source_from_strbuf(&source
, &buf
);
326 err
= init_reader(&rd
, &source
, "file.ref");
329 err
= reftable_reader_seek_ref(&rd
, &it
, names
[0]);
332 err
= reftable_iterator_next_log(&it
, &log
);
333 EXPECT(err
== REFTABLE_API_ERROR
);
335 strbuf_release(&buf
);
336 for (i
= 0; i
< N
; i
++) {
337 reftable_free(names
[i
]);
339 reftable_iterator_destroy(&it
);
340 reftable_free(names
);
342 strbuf_release(&buf
);
345 static void test_table_read_write_seek(int index
, int hash_id
)
348 struct strbuf buf
= STRBUF_INIT
;
350 struct reftable_reader rd
= { NULL
};
351 struct reftable_block_source source
= { NULL
};
355 struct reftable_iterator it
= { NULL
};
356 struct strbuf pastLast
= STRBUF_INIT
;
357 struct reftable_ref_record ref
= { NULL
};
359 write_table(&names
, &buf
, N
, 256, hash_id
);
361 block_source_from_strbuf(&source
, &buf
);
363 err
= init_reader(&rd
, &source
, "file.ref");
365 EXPECT(hash_id
== reftable_reader_hash_id(&rd
));
368 rd
.ref_offsets
.index_offset
= 0;
370 EXPECT(rd
.ref_offsets
.index_offset
> 0);
373 for (i
= 1; i
< N
; i
++) {
374 int err
= reftable_reader_seek_ref(&rd
, &it
, names
[i
]);
376 err
= reftable_iterator_next_ref(&it
, &ref
);
378 EXPECT(0 == strcmp(names
[i
], ref
.refname
));
379 EXPECT(REFTABLE_REF_VAL1
== ref
.value_type
);
380 EXPECT(i
== ref
.value
.val1
[0]);
382 reftable_ref_record_release(&ref
);
383 reftable_iterator_destroy(&it
);
386 strbuf_addstr(&pastLast
, names
[N
- 1]);
387 strbuf_addstr(&pastLast
, "/");
389 err
= reftable_reader_seek_ref(&rd
, &it
, pastLast
.buf
);
391 struct reftable_ref_record ref
= { NULL
};
392 int err
= reftable_iterator_next_ref(&it
, &ref
);
398 strbuf_release(&pastLast
);
399 reftable_iterator_destroy(&it
);
401 strbuf_release(&buf
);
402 for (i
= 0; i
< N
; i
++) {
403 reftable_free(names
[i
]);
405 reftable_free(names
);
409 static void test_table_read_write_seek_linear(void)
411 test_table_read_write_seek(0, GIT_SHA1_FORMAT_ID
);
414 static void test_table_read_write_seek_linear_sha256(void)
416 test_table_read_write_seek(0, GIT_SHA256_FORMAT_ID
);
419 static void test_table_read_write_seek_index(void)
421 test_table_read_write_seek(1, GIT_SHA1_FORMAT_ID
);
424 static void test_table_refs_for(int indexed
)
427 char **want_names
= reftable_calloc(sizeof(char *) * (N
+ 1));
428 int want_names_len
= 0;
429 uint8_t want_hash
[GIT_SHA1_RAWSZ
];
431 struct reftable_write_options opts
= {
434 struct reftable_ref_record ref
= { NULL
};
438 struct reftable_reader rd
;
439 struct reftable_block_source source
= { NULL
};
441 struct strbuf buf
= STRBUF_INIT
;
442 struct reftable_writer
*w
=
443 reftable_new_writer(&strbuf_add_void
, &buf
, &opts
);
445 struct reftable_iterator it
= { NULL
};
448 set_test_hash(want_hash
, 4);
450 for (i
= 0; i
< N
; i
++) {
451 uint8_t hash
[GIT_SHA1_RAWSZ
];
452 char fill
[51] = { 0 };
454 uint8_t hash1
[GIT_SHA1_RAWSZ
];
455 uint8_t hash2
[GIT_SHA1_RAWSZ
];
456 struct reftable_ref_record ref
= { NULL
};
458 memset(hash
, i
, sizeof(hash
));
459 memset(fill
, 'x', 50);
460 /* Put the variable part in the start */
461 snprintf(name
, sizeof(name
), "br%02d%s", i
, fill
);
465 set_test_hash(hash1
, i
/ 4);
466 set_test_hash(hash2
, 3 + i
/ 4);
467 ref
.value_type
= REFTABLE_REF_VAL2
;
468 ref
.value
.val2
.value
= hash1
;
469 ref
.value
.val2
.target_value
= hash2
;
471 /* 80 bytes / entry, so 3 entries per block. Yields 17
474 n
= reftable_writer_add_ref(w
, &ref
);
477 if (!memcmp(hash1
, want_hash
, GIT_SHA1_RAWSZ
) ||
478 !memcmp(hash2
, want_hash
, GIT_SHA1_RAWSZ
)) {
479 want_names
[want_names_len
++] = xstrdup(name
);
483 n
= reftable_writer_close(w
);
486 reftable_writer_free(w
);
489 block_source_from_strbuf(&source
, &buf
);
491 err
= init_reader(&rd
, &source
, "file.ref");
494 rd
.obj_offsets
.is_present
= 0;
497 err
= reftable_reader_seek_ref(&rd
, &it
, "");
499 reftable_iterator_destroy(&it
);
501 err
= reftable_reader_refs_for(&rd
, &it
, want_hash
);
506 int err
= reftable_iterator_next_ref(&it
, &ref
);
512 EXPECT(j
< want_names_len
);
513 EXPECT(0 == strcmp(ref
.refname
, want_names
[j
]));
515 reftable_ref_record_release(&ref
);
517 EXPECT(j
== want_names_len
);
519 strbuf_release(&buf
);
520 free_names(want_names
);
521 reftable_iterator_destroy(&it
);
525 static void test_table_refs_for_no_index(void)
527 test_table_refs_for(0);
530 static void test_table_refs_for_obj_index(void)
532 test_table_refs_for(1);
535 static void test_write_empty_table(void)
537 struct reftable_write_options opts
= { 0 };
538 struct strbuf buf
= STRBUF_INIT
;
539 struct reftable_writer
*w
=
540 reftable_new_writer(&strbuf_add_void
, &buf
, &opts
);
541 struct reftable_block_source source
= { NULL
};
542 struct reftable_reader
*rd
= NULL
;
543 struct reftable_ref_record rec
= { NULL
};
544 struct reftable_iterator it
= { NULL
};
547 reftable_writer_set_limits(w
, 1, 1);
549 err
= reftable_writer_close(w
);
550 EXPECT(err
== REFTABLE_EMPTY_TABLE_ERROR
);
551 reftable_writer_free(w
);
553 EXPECT(buf
.len
== header_size(1) + footer_size(1));
555 block_source_from_strbuf(&source
, &buf
);
557 err
= reftable_new_reader(&rd
, &source
, "filename");
560 err
= reftable_reader_seek_ref(rd
, &it
, "");
563 err
= reftable_iterator_next_ref(&it
, &rec
);
566 reftable_iterator_destroy(&it
);
567 reftable_reader_free(rd
);
568 strbuf_release(&buf
);
571 static void test_write_key_order(void)
573 struct reftable_write_options opts
= { 0 };
574 struct strbuf buf
= STRBUF_INIT
;
575 struct reftable_writer
*w
=
576 reftable_new_writer(&strbuf_add_void
, &buf
, &opts
);
577 struct reftable_ref_record refs
[2] = {
581 .value_type
= REFTABLE_REF_SYMREF
,
588 .value_type
= REFTABLE_REF_SYMREF
,
596 reftable_writer_set_limits(w
, 1, 1);
597 err
= reftable_writer_add_ref(w
, &refs
[0]);
599 err
= reftable_writer_add_ref(w
, &refs
[1]);
601 EXPECT(err
== REFTABLE_API_ERROR
);
602 reftable_writer_close(w
);
603 reftable_writer_free(w
);
604 strbuf_release(&buf
);
607 static void test_corrupt_table_empty(void)
609 struct strbuf buf
= STRBUF_INIT
;
610 struct reftable_block_source source
= { NULL
};
611 struct reftable_reader rd
= { NULL
};
614 block_source_from_strbuf(&source
, &buf
);
615 err
= init_reader(&rd
, &source
, "file.log");
616 EXPECT(err
== REFTABLE_FORMAT_ERROR
);
619 static void test_corrupt_table(void)
621 uint8_t zeros
[1024] = { 0 };
622 struct strbuf buf
= STRBUF_INIT
;
623 struct reftable_block_source source
= { NULL
};
624 struct reftable_reader rd
= { NULL
};
626 strbuf_add(&buf
, zeros
, sizeof(zeros
));
628 block_source_from_strbuf(&source
, &buf
);
629 err
= init_reader(&rd
, &source
, "file.log");
630 EXPECT(err
== REFTABLE_FORMAT_ERROR
);
631 strbuf_release(&buf
);
634 int readwrite_test_main(int argc
, const char *argv
[])
636 RUN_TEST(test_corrupt_table
);
637 RUN_TEST(test_corrupt_table_empty
);
638 RUN_TEST(test_log_write_read
);
639 RUN_TEST(test_write_key_order
);
640 RUN_TEST(test_table_read_write_seek_linear_sha256
);
641 RUN_TEST(test_log_buffer_size
);
642 RUN_TEST(test_table_write_small_table
);
643 RUN_TEST(test_buffer
);
644 RUN_TEST(test_table_read_api
);
645 RUN_TEST(test_table_read_write_sequential
);
646 RUN_TEST(test_table_read_write_seek_linear
);
647 RUN_TEST(test_table_read_write_seek_index
);
648 RUN_TEST(test_table_refs_for_no_index
);
649 RUN_TEST(test_table_refs_for_obj_index
);
650 RUN_TEST(test_write_empty_table
);