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 "constants.h"
14 #include "test_framework.h"
15 #include "reftable-tests.h"
17 static void test_copy(struct reftable_record
*rec
)
19 struct reftable_record copy
;
22 typ
= reftable_record_type(rec
);
23 reftable_record_init(©
, typ
);
24 reftable_record_copy_from(©
, rec
, GIT_SHA1_RAWSZ
);
25 /* do it twice to catch memory leaks */
26 reftable_record_copy_from(©
, rec
, GIT_SHA1_RAWSZ
);
27 EXPECT(reftable_record_equal(rec
, ©
, GIT_SHA1_RAWSZ
));
29 puts("testing print coverage:\n");
30 reftable_record_print(©
, GIT_SHA1_RAWSZ
);
32 reftable_record_release(©
);
35 static void test_varint_roundtrip(void)
37 uint64_t inputs
[] = { 0,
45 ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
47 for (i
= 0; i
< ARRAY_SIZE(inputs
); i
++) {
50 struct string_view out
= {
54 uint64_t in
= inputs
[i
];
55 int n
= put_var_int(&out
, in
);
60 n
= get_var_int(&got
, &out
);
67 static void test_common_prefix(void)
80 for (i
= 0; i
< ARRAY_SIZE(cases
); i
++) {
81 struct strbuf a
= STRBUF_INIT
;
82 struct strbuf b
= STRBUF_INIT
;
83 strbuf_addstr(&a
, cases
[i
].a
);
84 strbuf_addstr(&b
, cases
[i
].b
);
85 EXPECT(common_prefix_size(&a
, &b
) == cases
[i
].want
);
92 static void set_hash(uint8_t *h
, int j
)
95 for (i
= 0; i
< hash_size(GIT_SHA1_FORMAT_ID
); i
++) {
96 h
[i
] = (j
>> i
) & 0xff;
100 static void test_reftable_ref_record_roundtrip(void)
102 struct strbuf scratch
= STRBUF_INIT
;
105 for (i
= REFTABLE_REF_DELETION
; i
< REFTABLE_NR_REF_VALUETYPES
; i
++) {
106 struct reftable_record in
= {
107 .type
= BLOCK_TYPE_REF
,
109 struct reftable_record out
= { .type
= BLOCK_TYPE_REF
};
110 struct strbuf key
= STRBUF_INIT
;
111 uint8_t buffer
[1024] = { 0 };
112 struct string_view dest
= {
114 .len
= sizeof(buffer
),
118 in
.u
.ref
.value_type
= i
;
120 case REFTABLE_REF_DELETION
:
122 case REFTABLE_REF_VAL1
:
123 set_hash(in
.u
.ref
.value
.val1
, 1);
125 case REFTABLE_REF_VAL2
:
126 set_hash(in
.u
.ref
.value
.val2
.value
, 1);
127 set_hash(in
.u
.ref
.value
.val2
.target_value
, 2);
129 case REFTABLE_REF_SYMREF
:
130 in
.u
.ref
.value
.symref
= xstrdup("target");
133 in
.u
.ref
.refname
= xstrdup("refs/heads/master");
137 EXPECT(reftable_record_val_type(&in
) == i
);
139 reftable_record_key(&in
, &key
);
140 n
= reftable_record_encode(&in
, dest
, GIT_SHA1_RAWSZ
);
143 /* decode into a non-zero reftable_record to test for leaks. */
144 m
= reftable_record_decode(&out
, key
, i
, dest
, GIT_SHA1_RAWSZ
, &scratch
);
147 EXPECT(reftable_ref_record_equal(&in
.u
.ref
, &out
.u
.ref
,
149 reftable_record_release(&in
);
151 strbuf_release(&key
);
152 reftable_record_release(&out
);
155 strbuf_release(&scratch
);
158 static void test_reftable_log_record_equal(void)
160 struct reftable_log_record in
[2] = {
162 .refname
= xstrdup("refs/heads/master"),
166 .refname
= xstrdup("refs/heads/master"),
171 EXPECT(!reftable_log_record_equal(&in
[0], &in
[1], GIT_SHA1_RAWSZ
));
172 in
[1].update_index
= in
[0].update_index
;
173 EXPECT(reftable_log_record_equal(&in
[0], &in
[1], GIT_SHA1_RAWSZ
));
174 reftable_log_record_release(&in
[0]);
175 reftable_log_record_release(&in
[1]);
178 static void test_reftable_log_record_roundtrip(void)
181 struct reftable_log_record in
[] = {
183 .refname
= xstrdup("refs/heads/master"),
185 .value_type
= REFTABLE_LOG_UPDATE
,
188 .name
= xstrdup("han-wen"),
189 .email
= xstrdup("hanwen@google.com"),
190 .message
= xstrdup("test"),
197 .refname
= xstrdup("refs/heads/master"),
199 .value_type
= REFTABLE_LOG_DELETION
,
202 .refname
= xstrdup("branch"),
204 .value_type
= REFTABLE_LOG_UPDATE
,
207 struct strbuf scratch
= STRBUF_INIT
;
209 set_test_hash(in
[0].value
.update
.new_hash
, 1);
210 set_test_hash(in
[0].value
.update
.old_hash
, 2);
211 set_test_hash(in
[2].value
.update
.new_hash
, 3);
212 set_test_hash(in
[2].value
.update
.old_hash
, 4);
213 for (i
= 0; i
< ARRAY_SIZE(in
); i
++) {
214 struct reftable_record rec
= { .type
= BLOCK_TYPE_LOG
};
215 struct strbuf key
= STRBUF_INIT
;
216 uint8_t buffer
[1024] = { 0 };
217 struct string_view dest
= {
219 .len
= sizeof(buffer
),
221 /* populate out, to check for leaks. */
222 struct reftable_record out
= {
223 .type
= BLOCK_TYPE_LOG
,
225 .refname
= xstrdup("old name"),
226 .value_type
= REFTABLE_LOG_UPDATE
,
229 .name
= xstrdup("old name"),
230 .email
= xstrdup("old@email"),
231 .message
= xstrdup("old message"),
242 reftable_record_key(&rec
, &key
);
244 n
= reftable_record_encode(&rec
, dest
, GIT_SHA1_RAWSZ
);
246 valtype
= reftable_record_val_type(&rec
);
247 m
= reftable_record_decode(&out
, key
, valtype
, dest
,
248 GIT_SHA1_RAWSZ
, &scratch
);
251 EXPECT(reftable_log_record_equal(&in
[i
], &out
.u
.log
,
253 reftable_log_record_release(&in
[i
]);
254 strbuf_release(&key
);
255 reftable_record_release(&out
);
258 strbuf_release(&scratch
);
261 static void test_u24_roundtrip(void)
263 uint32_t in
= 0x112233;
267 out
= get_be24(dest
);
271 static void test_key_roundtrip(void)
273 uint8_t buffer
[1024] = { 0 };
274 struct string_view dest
= {
276 .len
= sizeof(buffer
),
278 struct strbuf last_key
= STRBUF_INIT
;
279 struct strbuf key
= STRBUF_INIT
;
280 struct strbuf roundtrip
= STRBUF_INIT
;
286 strbuf_addstr(&last_key
, "refs/heads/master");
287 strbuf_addstr(&key
, "refs/tags/bla");
289 n
= reftable_encode_key(&restart
, dest
, last_key
, key
, extra
);
293 strbuf_addstr(&roundtrip
, "refs/heads/master");
294 m
= reftable_decode_key(&roundtrip
, &rt_extra
, dest
);
296 EXPECT(0 == strbuf_cmp(&key
, &roundtrip
));
297 EXPECT(rt_extra
== extra
);
299 strbuf_release(&last_key
);
300 strbuf_release(&key
);
301 strbuf_release(&roundtrip
);
304 static void test_reftable_obj_record_roundtrip(void)
306 uint8_t testHash1
[GIT_SHA1_RAWSZ
] = { 1, 2, 3, 4, 0 };
307 uint64_t till9
[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
308 struct reftable_obj_record recs
[3] = {
310 .hash_prefix
= testHash1
,
311 .hash_prefix_len
= 5,
316 .hash_prefix
= testHash1
,
317 .hash_prefix_len
= 5,
322 .hash_prefix
= testHash1
,
323 .hash_prefix_len
= 5,
326 struct strbuf scratch
= STRBUF_INIT
;
329 for (i
= 0; i
< ARRAY_SIZE(recs
); i
++) {
330 uint8_t buffer
[1024] = { 0 };
331 struct string_view dest
= {
333 .len
= sizeof(buffer
),
335 struct reftable_record in
= {
336 .type
= BLOCK_TYPE_OBJ
,
341 struct strbuf key
= STRBUF_INIT
;
342 struct reftable_record out
= { .type
= BLOCK_TYPE_OBJ
};
347 reftable_record_key(&in
, &key
);
348 n
= reftable_record_encode(&in
, dest
, GIT_SHA1_RAWSZ
);
350 extra
= reftable_record_val_type(&in
);
351 m
= reftable_record_decode(&out
, key
, extra
, dest
,
352 GIT_SHA1_RAWSZ
, &scratch
);
355 EXPECT(reftable_record_equal(&in
, &out
, GIT_SHA1_RAWSZ
));
356 strbuf_release(&key
);
357 reftable_record_release(&out
);
360 strbuf_release(&scratch
);
363 static void test_reftable_index_record_roundtrip(void)
365 struct reftable_record in
= {
366 .type
= BLOCK_TYPE_INDEX
,
369 .last_key
= STRBUF_INIT
,
372 uint8_t buffer
[1024] = { 0 };
373 struct string_view dest
= {
375 .len
= sizeof(buffer
),
377 struct strbuf scratch
= STRBUF_INIT
;
378 struct strbuf key
= STRBUF_INIT
;
379 struct reftable_record out
= {
380 .type
= BLOCK_TYPE_INDEX
,
381 .u
.idx
= { .last_key
= STRBUF_INIT
},
386 strbuf_addstr(&in
.u
.idx
.last_key
, "refs/heads/master");
387 reftable_record_key(&in
, &key
);
390 EXPECT(0 == strbuf_cmp(&key
, &in
.u
.idx
.last_key
));
391 n
= reftable_record_encode(&in
, dest
, GIT_SHA1_RAWSZ
);
394 extra
= reftable_record_val_type(&in
);
395 m
= reftable_record_decode(&out
, key
, extra
, dest
, GIT_SHA1_RAWSZ
,
399 EXPECT(reftable_record_equal(&in
, &out
, GIT_SHA1_RAWSZ
));
401 reftable_record_release(&out
);
402 strbuf_release(&key
);
403 strbuf_release(&scratch
);
404 strbuf_release(&in
.u
.idx
.last_key
);
407 int record_test_main(int argc
, const char *argv
[])
409 RUN_TEST(test_reftable_log_record_equal
);
410 RUN_TEST(test_reftable_log_record_roundtrip
);
411 RUN_TEST(test_reftable_ref_record_roundtrip
);
412 RUN_TEST(test_varint_roundtrip
);
413 RUN_TEST(test_key_roundtrip
);
414 RUN_TEST(test_common_prefix
);
415 RUN_TEST(test_reftable_obj_record_roundtrip
);
416 RUN_TEST(test_reftable_index_record_roundtrip
);
417 RUN_TEST(test_u24_roundtrip
);