1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
#include "unit-test.h"
#include "lib-reftable.h"
#include "reftable/basics.h"
#include "reftable/block.h"
#include "reftable/blocksource.h"
#include "reftable/constants.h"
#include "reftable/iter.h"
#include "reftable/reftable-error.h"
#include "reftable/table.h"
#include "strbuf.h"
void test_reftable_table__seek_once(void)
{
struct reftable_ref_record records[] = {
{
.refname = (char *) "refs/heads/main",
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 42 },
},
};
struct reftable_block_source source = { 0 };
struct reftable_ref_record ref = { 0 };
struct reftable_iterator it = { 0 };
struct reftable_table *table;
struct reftable_buf buf = REFTABLE_BUF_INIT;
int ret;
cl_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0,
REFTABLE_HASH_SHA1, NULL);
block_source_from_buf(&source, &buf);
ret = reftable_table_new(&table, &source, "name");
cl_assert(!ret);
ret = reftable_table_init_ref_iterator(table, &it);
cl_assert_equal_i(ret, 0);
ret = reftable_iterator_seek_ref(&it, "");
cl_assert(!ret);
ret = reftable_iterator_next_ref(&it, &ref);
cl_assert(!ret);
ret = reftable_ref_record_equal(&ref, &records[0],
REFTABLE_HASH_SIZE_SHA1);
cl_assert_equal_i(ret, 1);
ret = reftable_iterator_next_ref(&it, &ref);
cl_assert_equal_i(ret, 1);
reftable_ref_record_release(&ref);
reftable_iterator_destroy(&it);
reftable_table_decref(table);
reftable_buf_release(&buf);
}
void test_reftable_table__reseek(void)
{
struct reftable_ref_record records[] = {
{
.refname = (char *) "refs/heads/main",
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 42 },
},
};
struct reftable_block_source source = { 0 };
struct reftable_ref_record ref = { 0 };
struct reftable_iterator it = { 0 };
struct reftable_table *table;
struct reftable_buf buf = REFTABLE_BUF_INIT;
int ret;
cl_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records),
NULL, 0, REFTABLE_HASH_SHA1, NULL);
block_source_from_buf(&source, &buf);
ret = reftable_table_new(&table, &source, "name");
cl_assert(!ret);
ret = reftable_table_init_ref_iterator(table, &it);
cl_assert_equal_i(ret, 0);
for (size_t i = 0; i < 5; i++) {
ret = reftable_iterator_seek_ref(&it, "");
cl_assert(!ret);
ret = reftable_iterator_next_ref(&it, &ref);
cl_assert(!ret);
ret = reftable_ref_record_equal(&ref, &records[0], REFTABLE_HASH_SIZE_SHA1);
cl_assert_equal_i(ret, 1);
ret = reftable_iterator_next_ref(&it, &ref);
cl_assert_equal_i(ret, 1);
}
reftable_ref_record_release(&ref);
reftable_iterator_destroy(&it);
reftable_table_decref(table);
reftable_buf_release(&buf);
}
void test_reftable_table__block_iterator(void)
{
struct reftable_block_source source = { 0 };
struct reftable_table_iterator it = { 0 };
struct reftable_ref_record *records;
const struct reftable_block *block;
struct reftable_table *table;
struct reftable_buf buf = REFTABLE_BUF_INIT;
struct {
uint8_t block_type;
uint16_t header_off;
uint16_t restart_count;
uint16_t record_count;
} expected_blocks[] = {
{
.block_type = REFTABLE_BLOCK_TYPE_REF,
.header_off = 24,
.restart_count = 10,
.record_count = 158,
},
{
.block_type = REFTABLE_BLOCK_TYPE_REF,
.restart_count = 10,
.record_count = 159,
},
{
.block_type = REFTABLE_BLOCK_TYPE_REF,
.restart_count = 10,
.record_count = 159,
},
{
.block_type = REFTABLE_BLOCK_TYPE_REF,
.restart_count = 2,
.record_count = 24,
},
{
.block_type = REFTABLE_BLOCK_TYPE_INDEX,
.restart_count = 1,
.record_count = 4,
},
{
.block_type = REFTABLE_BLOCK_TYPE_OBJ,
.restart_count = 1,
.record_count = 1,
},
};
const size_t nrecords = 500;
int ret;
REFTABLE_CALLOC_ARRAY(records, nrecords);
for (size_t i = 0; i < nrecords; i++) {
records[i].value_type = REFTABLE_REF_VAL1;
records[i].refname = xstrfmt("refs/heads/branch-%03"PRIuMAX,
(uintmax_t) i);
}
cl_reftable_write_to_buf(&buf, records, nrecords, NULL, 0,
REFTABLE_HASH_SHA1, NULL);
block_source_from_buf(&source, &buf);
ret = reftable_table_new(&table, &source, "name");
cl_assert(!ret);
ret = reftable_table_iterator_init(&it, table);
cl_assert(!ret);
for (size_t i = 0; i < ARRAY_SIZE(expected_blocks); i++) {
struct reftable_iterator record_it = { 0 };
struct reftable_record record = {
.type = expected_blocks[i].block_type,
};
ret = reftable_table_iterator_next(&it, &block);
cl_assert(!ret);
cl_assert_equal_i(block->block_type,
expected_blocks[i].block_type);
cl_assert_equal_i(block->header_off,
expected_blocks[i].header_off);
cl_assert_equal_i(block->restart_count,
expected_blocks[i].restart_count);
ret = reftable_block_init_iterator(block, &record_it);
cl_assert(!ret);
for (size_t j = 0; ; j++) {
ret = iterator_next(&record_it, &record);
if (ret > 0) {
cl_assert_equal_i(j,
expected_blocks[i].record_count);
break;
}
cl_assert(!ret);
}
reftable_iterator_destroy(&record_it);
reftable_record_release(&record);
}
ret = reftable_table_iterator_next(&it, &block);
cl_assert_equal_i(ret, 1);
for (size_t i = 0; i < nrecords; i++)
reftable_free(records[i].refname);
reftable_table_iterator_release(&it);
reftable_table_decref(table);
reftable_buf_release(&buf);
reftable_free(records);
}
void test_reftable_table__seek_invalid_log_offset(void)
{
struct reftable_ref_record refs[] = {
{
.refname = (char *) "refs/heads/main",
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 42 },
},
};
struct reftable_log_record logs[] = {
{
.refname = (char *) "refs/heads/main",
.update_index = 1,
.value_type = REFTABLE_LOG_UPDATE,
.value.update = {
.name = (char *) "user",
.email = (char *) "user@example.com",
.message = (char *) "message\n",
},
},
};
struct reftable_block_source source = { 0 };
struct reftable_log_record log = { 0 };
struct reftable_iterator it = { 0 };
struct reftable_table *table;
struct reftable_buf buf = REFTABLE_BUF_INIT;
size_t fsize = footer_size(1);
uint8_t *footer;
cl_reftable_write_to_buf(&buf, refs, ARRAY_SIZE(refs),
logs, ARRAY_SIZE(logs), REFTABLE_HASH_SHA1, NULL);
/*
* Corrupt the log section offset stored in the footer so that it points
* past the end of the table. The footer is checksummed, so we also have
* to recompute and rewrite the CRC.
*/
footer = (uint8_t *) buf.buf + buf.len - fsize;
reftable_put_be64(footer + header_size(1) + 24, UINT64_MAX);
reftable_put_be32(footer + fsize - 4, crc32(0, footer, fsize - 4));
block_source_from_buf(&source, &buf);
cl_must_pass(reftable_table_new(&table, &source, "name"));
/*
* Seeking the log iterator must not crash even though the log section
* offset is bogus. As the offset points past the end of the table we
* know that the table is corrupt, so the seek must report a format
* error instead of pretending that the section is empty.
*/
reftable_table_init_log_iterator(table, &it);
cl_assert_equal_i(reftable_iterator_seek_log(&it, ""),
REFTABLE_FORMAT_ERROR);
reftable_log_record_release(&log);
reftable_iterator_destroy(&it);
reftable_table_decref(table);
reftable_buf_release(&buf);
}
void test_reftable_table__new_with_truncated_table(void)
{
struct reftable_ref_record refs[] = {
{
.refname = (char *) "refs/heads/main",
.value_type = REFTABLE_REF_VAL1,
.value.val1 = { 42 },
},
};
struct reftable_block_source source = { 0 };
struct reftable_table *table;
struct reftable_buf buf = REFTABLE_BUF_INIT;
cl_reftable_write_to_buf(&buf, refs, ARRAY_SIZE(refs), NULL, 0,
REFTABLE_HASH_SHA1, NULL);
/*
* Truncate the table so that it is large enough to read the header, but
* too small to also contain the footer.
*/
buf.len = footer_size(1) - 1;
block_source_from_buf(&source, &buf);
cl_assert_equal_i(reftable_table_new(&table, &source, "name"),
REFTABLE_FORMAT_ERROR);
reftable_buf_release(&buf);
}
|