Merge branch 'mh/doc-credential-helpers'
[git/debian.git] / reftable / block_test.c
blobcb88af4a5639258945f569ede758e31809b62ead
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 "block.h"
11 #include "system.h"
12 #include "blocksource.h"
13 #include "basics.h"
14 #include "constants.h"
15 #include "record.h"
16 #include "test_framework.h"
17 #include "reftable-tests.h"
19 static void test_block_read_write(void)
21 const int header_off = 21; /* random */
22 char *names[30];
23 const int N = ARRAY_SIZE(names);
24 const int block_size = 1024;
25 struct reftable_block block = { NULL };
26 struct block_writer bw = {
27 .last_key = STRBUF_INIT,
29 struct reftable_record rec = {
30 .type = BLOCK_TYPE_REF,
32 int i = 0;
33 int n;
34 struct block_reader br = { 0 };
35 struct block_iter it = { .last_key = STRBUF_INIT };
36 int j = 0;
37 struct strbuf want = STRBUF_INIT;
39 block.data = reftable_calloc(block_size);
40 block.len = block_size;
41 block.source = malloc_block_source();
42 block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
43 header_off, hash_size(GIT_SHA1_FORMAT_ID));
45 rec.u.ref.refname = "";
46 rec.u.ref.value_type = REFTABLE_REF_DELETION;
47 n = block_writer_add(&bw, &rec);
48 EXPECT(n == REFTABLE_API_ERROR);
50 for (i = 0; i < N; i++) {
51 char name[100];
52 uint8_t hash[GIT_SHA1_RAWSZ];
53 snprintf(name, sizeof(name), "branch%02d", i);
54 memset(hash, i, sizeof(hash));
56 rec.u.ref.refname = name;
57 rec.u.ref.value_type = REFTABLE_REF_VAL1;
58 rec.u.ref.value.val1 = hash;
60 names[i] = xstrdup(name);
61 n = block_writer_add(&bw, &rec);
62 rec.u.ref.refname = NULL;
63 rec.u.ref.value_type = REFTABLE_REF_DELETION;
64 EXPECT(n == 0);
67 n = block_writer_finish(&bw);
68 EXPECT(n > 0);
70 block_writer_release(&bw);
72 block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
74 block_reader_start(&br, &it);
76 while (1) {
77 int r = block_iter_next(&it, &rec);
78 EXPECT(r >= 0);
79 if (r > 0) {
80 break;
82 EXPECT_STREQ(names[j], rec.u.ref.refname);
83 j++;
86 reftable_record_release(&rec);
87 block_iter_close(&it);
89 for (i = 0; i < N; i++) {
90 struct block_iter it = { .last_key = STRBUF_INIT };
91 strbuf_reset(&want);
92 strbuf_addstr(&want, names[i]);
94 n = block_reader_seek(&br, &it, &want);
95 EXPECT(n == 0);
97 n = block_iter_next(&it, &rec);
98 EXPECT(n == 0);
100 EXPECT_STREQ(names[i], rec.u.ref.refname);
102 want.len--;
103 n = block_reader_seek(&br, &it, &want);
104 EXPECT(n == 0);
106 n = block_iter_next(&it, &rec);
107 EXPECT(n == 0);
108 EXPECT_STREQ(names[10 * (i / 10)], rec.u.ref.refname);
110 block_iter_close(&it);
113 reftable_record_release(&rec);
114 reftable_block_done(&br.block);
115 strbuf_release(&want);
116 for (i = 0; i < N; i++) {
117 reftable_free(names[i]);
121 int block_test_main(int argc, const char *argv[])
123 RUN_TEST(test_block_read_write);
124 return 0;