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
=
20 reftable_new_record(reftable_record_type(rec
));
21 reftable_record_copy_from(©
, rec
, GIT_SHA1_RAWSZ
);
22 /* do it twice to catch memory leaks */
23 reftable_record_copy_from(©
, rec
, GIT_SHA1_RAWSZ
);
24 switch (reftable_record_type(©
)) {
26 EXPECT(reftable_ref_record_equal(reftable_record_as_ref(©
),
27 reftable_record_as_ref(rec
),
31 EXPECT(reftable_log_record_equal(reftable_record_as_log(©
),
32 reftable_record_as_log(rec
),
36 reftable_record_destroy(©
);
39 static void test_varint_roundtrip(void)
41 uint64_t inputs
[] = { 0,
49 ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
51 for (i
= 0; i
< ARRAY_SIZE(inputs
); i
++) {
54 struct string_view out
= {
58 uint64_t in
= inputs
[i
];
59 int n
= put_var_int(&out
, in
);
64 n
= get_var_int(&got
, &out
);
71 static void test_common_prefix(void)
84 for (i
= 0; i
< ARRAY_SIZE(cases
); i
++) {
85 struct strbuf a
= STRBUF_INIT
;
86 struct strbuf b
= STRBUF_INIT
;
87 strbuf_addstr(&a
, cases
[i
].a
);
88 strbuf_addstr(&b
, cases
[i
].b
);
89 EXPECT(common_prefix_size(&a
, &b
) == cases
[i
].want
);
96 static void set_hash(uint8_t *h
, int j
)
99 for (i
= 0; i
< hash_size(GIT_SHA1_FORMAT_ID
); i
++) {
100 h
[i
] = (j
>> i
) & 0xff;
104 static void test_reftable_ref_record_roundtrip(void)
108 for (i
= REFTABLE_REF_DELETION
; i
< REFTABLE_NR_REF_VALUETYPES
; i
++) {
109 struct reftable_ref_record in
= { NULL
};
110 struct reftable_ref_record out
= { NULL
};
111 struct reftable_record rec_out
= { NULL
};
112 struct strbuf key
= STRBUF_INIT
;
113 struct reftable_record rec
= { NULL
};
114 uint8_t buffer
[1024] = { 0 };
115 struct string_view dest
= {
117 .len
= sizeof(buffer
),
124 case REFTABLE_REF_DELETION
:
126 case REFTABLE_REF_VAL1
:
127 in
.value
.val1
= reftable_malloc(GIT_SHA1_RAWSZ
);
128 set_hash(in
.value
.val1
, 1);
130 case REFTABLE_REF_VAL2
:
131 in
.value
.val2
.value
= reftable_malloc(GIT_SHA1_RAWSZ
);
132 set_hash(in
.value
.val2
.value
, 1);
133 in
.value
.val2
.target_value
=
134 reftable_malloc(GIT_SHA1_RAWSZ
);
135 set_hash(in
.value
.val2
.target_value
, 2);
137 case REFTABLE_REF_SYMREF
:
138 in
.value
.symref
= xstrdup("target");
141 in
.refname
= xstrdup("refs/heads/master");
143 reftable_record_from_ref(&rec
, &in
);
146 EXPECT(reftable_record_val_type(&rec
) == i
);
148 reftable_record_key(&rec
, &key
);
149 n
= reftable_record_encode(&rec
, dest
, GIT_SHA1_RAWSZ
);
152 /* decode into a non-zero reftable_record to test for leaks. */
154 reftable_record_from_ref(&rec_out
, &out
);
155 m
= reftable_record_decode(&rec_out
, key
, i
, dest
,
159 EXPECT(reftable_ref_record_equal(&in
, &out
, GIT_SHA1_RAWSZ
));
160 reftable_record_release(&rec_out
);
162 strbuf_release(&key
);
163 reftable_ref_record_release(&in
);
167 static void test_reftable_log_record_equal(void)
169 struct reftable_log_record in
[2] = {
171 .refname
= xstrdup("refs/heads/master"),
175 .refname
= xstrdup("refs/heads/master"),
180 EXPECT(!reftable_log_record_equal(&in
[0], &in
[1], GIT_SHA1_RAWSZ
));
181 in
[1].update_index
= in
[0].update_index
;
182 EXPECT(reftable_log_record_equal(&in
[0], &in
[1], GIT_SHA1_RAWSZ
));
183 reftable_log_record_release(&in
[0]);
184 reftable_log_record_release(&in
[1]);
187 static void test_reftable_log_record_roundtrip(void)
190 struct reftable_log_record in
[2] = {
192 .refname
= xstrdup("refs/heads/master"),
194 .value_type
= REFTABLE_LOG_UPDATE
,
197 .old_hash
= reftable_malloc(GIT_SHA1_RAWSZ
),
198 .new_hash
= reftable_malloc(GIT_SHA1_RAWSZ
),
199 .name
= xstrdup("han-wen"),
200 .email
= xstrdup("hanwen@google.com"),
201 .message
= xstrdup("test"),
208 .refname
= xstrdup("refs/heads/master"),
210 .value_type
= REFTABLE_LOG_DELETION
,
213 set_test_hash(in
[0].value
.update
.new_hash
, 1);
214 set_test_hash(in
[0].value
.update
.old_hash
, 2);
215 for (i
= 0; i
< ARRAY_SIZE(in
); i
++) {
216 struct reftable_record rec
= { NULL
};
217 struct strbuf key
= STRBUF_INIT
;
218 uint8_t buffer
[1024] = { 0 };
219 struct string_view dest
= {
221 .len
= sizeof(buffer
),
223 /* populate out, to check for leaks. */
224 struct reftable_log_record out
= {
225 .refname
= xstrdup("old name"),
226 .value_type
= REFTABLE_LOG_UPDATE
,
229 .new_hash
= reftable_calloc(GIT_SHA1_RAWSZ
),
230 .old_hash
= reftable_calloc(GIT_SHA1_RAWSZ
),
231 .name
= xstrdup("old name"),
232 .email
= xstrdup("old@email"),
233 .message
= xstrdup("old message"),
237 struct reftable_record rec_out
= { NULL
};
240 reftable_record_from_log(&rec
, &in
[i
]);
244 reftable_record_key(&rec
, &key
);
246 n
= reftable_record_encode(&rec
, dest
, GIT_SHA1_RAWSZ
);
248 reftable_record_from_log(&rec_out
, &out
);
249 valtype
= reftable_record_val_type(&rec
);
250 m
= reftable_record_decode(&rec_out
, key
, valtype
, dest
,
254 EXPECT(reftable_log_record_equal(&in
[i
], &out
, GIT_SHA1_RAWSZ
));
255 reftable_log_record_release(&in
[i
]);
256 strbuf_release(&key
);
257 reftable_record_release(&rec_out
);
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 m
= reftable_decode_key(&roundtrip
, &rt_extra
, last_key
, dest
);
295 EXPECT(0 == strbuf_cmp(&key
, &roundtrip
));
296 EXPECT(rt_extra
== extra
);
298 strbuf_release(&last_key
);
299 strbuf_release(&key
);
300 strbuf_release(&roundtrip
);
303 static void test_reftable_obj_record_roundtrip(void)
305 uint8_t testHash1
[GIT_SHA1_RAWSZ
] = { 1, 2, 3, 4, 0 };
306 uint64_t till9
[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
307 struct reftable_obj_record recs
[3] = { {
308 .hash_prefix
= testHash1
,
309 .hash_prefix_len
= 5,
314 .hash_prefix
= testHash1
,
315 .hash_prefix_len
= 5,
320 .hash_prefix
= testHash1
,
321 .hash_prefix_len
= 5,
324 for (i
= 0; i
< ARRAY_SIZE(recs
); i
++) {
325 struct reftable_obj_record in
= recs
[i
];
326 uint8_t buffer
[1024] = { 0 };
327 struct string_view dest
= {
329 .len
= sizeof(buffer
),
331 struct reftable_record rec
= { NULL
};
332 struct strbuf key
= STRBUF_INIT
;
333 struct reftable_obj_record out
= { NULL
};
334 struct reftable_record rec_out
= { NULL
};
338 reftable_record_from_obj(&rec
, &in
);
340 reftable_record_key(&rec
, &key
);
341 n
= reftable_record_encode(&rec
, dest
, GIT_SHA1_RAWSZ
);
343 extra
= reftable_record_val_type(&rec
);
344 reftable_record_from_obj(&rec_out
, &out
);
345 m
= reftable_record_decode(&rec_out
, key
, extra
, dest
,
349 EXPECT(in
.hash_prefix_len
== out
.hash_prefix_len
);
350 EXPECT(in
.offset_len
== out
.offset_len
);
352 EXPECT(!memcmp(in
.hash_prefix
, out
.hash_prefix
,
353 in
.hash_prefix_len
));
354 EXPECT(0 == memcmp(in
.offsets
, out
.offsets
,
355 sizeof(uint64_t) * in
.offset_len
));
356 strbuf_release(&key
);
357 reftable_record_release(&rec_out
);
361 static void test_reftable_index_record_roundtrip(void)
363 struct reftable_index_record in
= {
365 .last_key
= STRBUF_INIT
,
367 uint8_t buffer
[1024] = { 0 };
368 struct string_view dest
= {
370 .len
= sizeof(buffer
),
372 struct strbuf key
= STRBUF_INIT
;
373 struct reftable_record rec
= { NULL
};
374 struct reftable_index_record out
= { .last_key
= STRBUF_INIT
};
375 struct reftable_record out_rec
= { NULL
};
379 strbuf_addstr(&in
.last_key
, "refs/heads/master");
380 reftable_record_from_index(&rec
, &in
);
381 reftable_record_key(&rec
, &key
);
384 EXPECT(0 == strbuf_cmp(&key
, &in
.last_key
));
385 n
= reftable_record_encode(&rec
, dest
, GIT_SHA1_RAWSZ
);
388 extra
= reftable_record_val_type(&rec
);
389 reftable_record_from_index(&out_rec
, &out
);
390 m
= reftable_record_decode(&out_rec
, key
, extra
, dest
, GIT_SHA1_RAWSZ
);
393 EXPECT(in
.offset
== out
.offset
);
395 reftable_record_release(&out_rec
);
396 strbuf_release(&key
);
397 strbuf_release(&in
.last_key
);
400 int record_test_main(int argc
, const char *argv
[])
402 RUN_TEST(test_reftable_log_record_equal
);
403 RUN_TEST(test_reftable_log_record_roundtrip
);
404 RUN_TEST(test_reftable_ref_record_roundtrip
);
405 RUN_TEST(test_varint_roundtrip
);
406 RUN_TEST(test_key_roundtrip
);
407 RUN_TEST(test_common_prefix
);
408 RUN_TEST(test_reftable_obj_record_roundtrip
);
409 RUN_TEST(test_reftable_index_record_roundtrip
);
410 RUN_TEST(test_u24_roundtrip
);