summaryrefslogtreecommitdiffstats
path: root/t/unit-tests/t-reftable-record.c
blob: cb649ee419a015ea9583c3be232b964df2e75f67 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
  Copyright 2020 Google LLC

  Use of this source code is governed by a BSD-style
  license that can be found in the LICENSE file or at
  https://developers.google.com/open-source/licenses/bsd
*/

#include "test-lib.h"
#include "reftable/constants.h"
#include "reftable/record.h"

static void t_copy(struct reftable_record *rec)
{
	struct reftable_record copy;
	uint8_t typ;

	typ = reftable_record_type(rec);
	reftable_record_init(&copy, typ);
	reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
	/* do it twice to catch memory leaks */
	reftable_record_copy_from(&copy, rec, GIT_SHA1_RAWSZ);
	check(reftable_record_equal(rec, &copy, GIT_SHA1_RAWSZ));

	reftable_record_release(&copy);
}

static void t_varint_roundtrip(void)
{
	uint64_t inputs[] = { 0,
			      1,
			      27,
			      127,
			      128,
			      257,
			      4096,
			      ((uint64_t)1 << 63),
			      ((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };

	for (size_t i = 0; i < ARRAY_SIZE(inputs); i++) {
		uint8_t dest[10];

		struct string_view out = {
			.buf = dest,
			.len = sizeof(dest),
		};
		uint64_t in = inputs[i];
		int n = put_var_int(&out, in);
		uint64_t got = 0;

		check_int(n, >, 0);
		out.len = n;
		n = get_var_int(&got, &out);
		check_int(n, >, 0);

		check_int(got, ==, in);
	}
}

static void set_hash(uint8_t *h, int j)
{
	for (int i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++)
		h[i] = (j >> i) & 0xff;
}

static void t_reftable_ref_record_comparison(void)
{
	struct reftable_record in[3] = {
		{
			.type = BLOCK_TYPE_REF,
			.u.ref.refname = (char *) "refs/heads/master",
			.u.ref.value_type = REFTABLE_REF_VAL1,
		},
		{
			.type = BLOCK_TYPE_REF,
			.u.ref.refname = (char *) "refs/heads/master",
			.u.ref.value_type = REFTABLE_REF_DELETION,
		},
		{
			.type = BLOCK_TYPE_REF,
			.u.ref.refname = (char *) "HEAD",
			.u.ref.value_type = REFTABLE_REF_SYMREF,
			.u.ref.value.symref = (char *) "refs/heads/master",
		},
	};

	check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));

	check(!reftable_record_equal(&in[1], &in[2], GIT_SHA1_RAWSZ));
	check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);

	in[1].u.ref.value_type = in[0].u.ref.value_type;
	check(reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));
}

static void t_reftable_ref_record_compare_name(void)
{
	struct reftable_ref_record recs[3] = {
		{
			.refname = (char *) "refs/heads/a"
		},
		{
			.refname = (char *) "refs/heads/b"
		},
		{
			.refname = (char *) "refs/heads/a"
		},
	};

	check_int(reftable_ref_record_compare_name(&recs[0], &recs[1]), <, 0);
	check_int(reftable_ref_record_compare_name(&recs[1], &recs[0]), >, 0);
	check_int(reftable_ref_record_compare_name(&recs[0], &recs[2]), ==, 0);
}

static void t_reftable_ref_record_roundtrip(void)
{
	struct strbuf scratch = STRBUF_INIT;

	for (int i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
		struct reftable_record in = {
			.type = BLOCK_TYPE_REF,
			.u.ref.value_type = i,
		};
		struct reftable_record out = { .type = BLOCK_TYPE_REF };
		struct strbuf key = STRBUF_INIT;
		uint8_t buffer[1024] = { 0 };
		struct string_view dest = {
			.buf = buffer,
			.len = sizeof(buffer),
		};
		int n, m;

		in.u.ref.value_type = i;
		switch (i) {
		case REFTABLE_REF_DELETION:
			break;
		case REFTABLE_REF_VAL1:
			set_hash(in.u.ref.value.val1, 1);
			break;
		case REFTABLE_REF_VAL2:
			set_hash(in.u.ref.value.val2.value, 1);
			set_hash(in.u.ref.value.val2.target_value, 2);
			break;
		case REFTABLE_REF_SYMREF:
			in.u.ref.value.symref = xstrdup("target");
			break;
		}
		in.u.ref.refname = xstrdup("refs/heads/master");

		t_copy(&in);

		check_int(reftable_record_val_type(&in), ==, i);
		check_int(reftable_record_is_deletion(&in), ==, i == REFTABLE_REF_DELETION);

		reftable_record_key(&in, &key);
		n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
		check_int(n, >, 0);

		/* decode into a non-zero reftable_record to test for leaks. */
		m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
		check_int(n, ==, m);

		check(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
						 GIT_SHA1_RAWSZ));
		reftable_record_release(&in);

		strbuf_release(&key);
		reftable_record_release(&out);
	}

	strbuf_release(&scratch);
}

static void t_reftable_log_record_comparison(void)
{
	struct reftable_record in[3] = {
		{
			.type = BLOCK_TYPE_LOG,
			.u.log.refname = (char *) "refs/heads/master",
			.u.log.update_index = 42,
		},
		{
			.type = BLOCK_TYPE_LOG,
			.u.log.refname = (char *) "refs/heads/master",
			.u.log.update_index = 22,
		},
		{
			.type = BLOCK_TYPE_LOG,
			.u.log.refname = (char *) "refs/heads/main",
			.u.log.update_index = 22,
		},
	};

	check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_equal(&in[1], &in[2], GIT_SHA1_RAWSZ));
	check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
	/* comparison should be reversed for equal keys, because
	 * comparison is now performed on the basis of update indices */
	check_int(reftable_record_cmp(&in[0], &in[1]), <, 0);

	in[1].u.log.update_index = in[0].u.log.update_index;
	check(reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));
}

static void t_reftable_log_record_compare_key(void)
{
	struct reftable_log_record logs[3] = {
		{
			.refname = (char *) "refs/heads/a",
			.update_index = 1,
		},
		{
			.refname = (char *) "refs/heads/b",
			.update_index = 2,
		},
		{
			.refname = (char *) "refs/heads/a",
			.update_index = 3,
		},
	};

	check_int(reftable_log_record_compare_key(&logs[0], &logs[1]), <, 0);
	check_int(reftable_log_record_compare_key(&logs[1], &logs[0]), >, 0);

	logs[1].update_index = logs[0].update_index;
	check_int(reftable_log_record_compare_key(&logs[0], &logs[1]), <, 0);

	check_int(reftable_log_record_compare_key(&logs[0], &logs[2]), >, 0);
	check_int(reftable_log_record_compare_key(&logs[2], &logs[0]), <, 0);
	logs[2].update_index = logs[0].update_index;
	check_int(reftable_log_record_compare_key(&logs[0], &logs[2]), ==, 0);
}

static void t_reftable_log_record_roundtrip(void)
{
	struct reftable_log_record in[] = {
		{
			.refname = xstrdup("refs/heads/master"),
			.update_index = 42,
			.value_type = REFTABLE_LOG_UPDATE,
			.value = {
				.update = {
					.name = xstrdup("han-wen"),
					.email = xstrdup("hanwen@google.com"),
					.message = xstrdup("test"),
					.time = 1577123507,
					.tz_offset = 100,
				},
			}
		},
		{
			.refname = xstrdup("refs/heads/master"),
			.update_index = 22,
			.value_type = REFTABLE_LOG_DELETION,
		},
		{
			.refname = xstrdup("branch"),
			.update_index = 33,
			.value_type = REFTABLE_LOG_UPDATE,
		}
	};
	struct strbuf scratch = STRBUF_INIT;
	set_hash(in[0].value.update.new_hash, 1);
	set_hash(in[0].value.update.old_hash, 2);
	set_hash(in[2].value.update.new_hash, 3);
	set_hash(in[2].value.update.old_hash, 4);

	check(!reftable_log_record_is_deletion(&in[0]));
	check(reftable_log_record_is_deletion(&in[1]));
	check(!reftable_log_record_is_deletion(&in[2]));

	for (size_t i = 0; i < ARRAY_SIZE(in); i++) {
		struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
		struct strbuf key = STRBUF_INIT;
		uint8_t buffer[1024] = { 0 };
		struct string_view dest = {
			.buf = buffer,
			.len = sizeof(buffer),
		};
		/* populate out, to check for leaks. */
		struct reftable_record out = {
			.type = BLOCK_TYPE_LOG,
			.u.log = {
				.refname = xstrdup("old name"),
				.value_type = REFTABLE_LOG_UPDATE,
				.value = {
					.update = {
						.name = xstrdup("old name"),
						.email = xstrdup("old@email"),
						.message = xstrdup("old message"),
					},
				},
			},
		};
		int n, m, valtype;

		rec.u.log = in[i];

		t_copy(&rec);

		reftable_record_key(&rec, &key);

		n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
		check_int(n, >=, 0);
		valtype = reftable_record_val_type(&rec);
		m = reftable_record_decode(&out, key, valtype, dest,
					   GIT_SHA1_RAWSZ, &scratch);
		check_int(n, ==, m);

		check(reftable_log_record_equal(&in[i], &out.u.log,
						 GIT_SHA1_RAWSZ));
		reftable_log_record_release(&in[i]);
		strbuf_release(&key);
		reftable_record_release(&out);
	}

	strbuf_release(&scratch);
}

static void t_key_roundtrip(void)
{
	uint8_t buffer[1024] = { 0 };
	struct string_view dest = {
		.buf = buffer,
		.len = sizeof(buffer),
	};
	struct strbuf last_key = STRBUF_INIT;
	struct strbuf key = STRBUF_INIT;
	struct strbuf roundtrip = STRBUF_INIT;
	int restart;
	uint8_t extra;
	int n, m;
	uint8_t rt_extra;

	strbuf_addstr(&last_key, "refs/heads/master");
	strbuf_addstr(&key, "refs/tags/bla");
	extra = 6;
	n = reftable_encode_key(&restart, dest, last_key, key, extra);
	check(!restart);
	check_int(n, >, 0);

	strbuf_addstr(&roundtrip, "refs/heads/master");
	m = reftable_decode_key(&roundtrip, &rt_extra, dest);
	check_int(n, ==, m);
	check(!strbuf_cmp(&key, &roundtrip));
	check_int(rt_extra, ==, extra);

	strbuf_release(&last_key);
	strbuf_release(&key);
	strbuf_release(&roundtrip);
}

static void t_reftable_obj_record_comparison(void)
{

	uint8_t id_bytes[] = { 0, 1, 2, 3, 4, 5, 6 };
	uint64_t offsets[] = { 0, 16, 32, 48, 64, 80, 96, 112};
	struct reftable_record in[3] = {
		{
			.type = BLOCK_TYPE_OBJ,
			.u.obj.hash_prefix = id_bytes,
			.u.obj.hash_prefix_len = 7,
			.u.obj.offsets = offsets,
			.u.obj.offset_len = 8,
		},
		{
			.type = BLOCK_TYPE_OBJ,
			.u.obj.hash_prefix = id_bytes,
			.u.obj.hash_prefix_len = 7,
			.u.obj.offsets = offsets,
			.u.obj.offset_len = 5,
		},
		{
			.type = BLOCK_TYPE_OBJ,
			.u.obj.hash_prefix = id_bytes,
			.u.obj.hash_prefix_len = 5,
		},
	};

	check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));

	check(!reftable_record_equal(&in[1], &in[2], GIT_SHA1_RAWSZ));
	check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);

	in[1].u.obj.offset_len = in[0].u.obj.offset_len;
	check(reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));
}

static void t_reftable_obj_record_roundtrip(void)
{
	uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
	uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
	struct reftable_obj_record recs[3] = {
		{
			.hash_prefix = testHash1,
			.hash_prefix_len = 5,
			.offsets = till9,
			.offset_len = 3,
		},
		{
			.hash_prefix = testHash1,
			.hash_prefix_len = 5,
			.offsets = till9,
			.offset_len = 9,
		},
		{
			.hash_prefix = testHash1,
			.hash_prefix_len = 5,
		},
	};
	struct strbuf scratch = STRBUF_INIT;

	for (size_t i = 0; i < ARRAY_SIZE(recs); i++) {
		uint8_t buffer[1024] = { 0 };
		struct string_view dest = {
			.buf = buffer,
			.len = sizeof(buffer),
		};
		struct reftable_record in = {
			.type = BLOCK_TYPE_OBJ,
			.u = {
				.obj = recs[i],
			},
		};
		struct strbuf key = STRBUF_INIT;
		struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
		int n, m;
		uint8_t extra;

		check(!reftable_record_is_deletion(&in));
		t_copy(&in);
		reftable_record_key(&in, &key);
		n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
		check_int(n, >, 0);
		extra = reftable_record_val_type(&in);
		m = reftable_record_decode(&out, key, extra, dest,
					   GIT_SHA1_RAWSZ, &scratch);
		check_int(n, ==, m);

		check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
		strbuf_release(&key);
		reftable_record_release(&out);
	}

	strbuf_release(&scratch);
}

static void t_reftable_index_record_comparison(void)
{
	struct reftable_record in[3] = {
		{
			.type = BLOCK_TYPE_INDEX,
			.u.idx.offset = 22,
			.u.idx.last_key = STRBUF_INIT,
		},
		{
			.type = BLOCK_TYPE_INDEX,
			.u.idx.offset = 32,
			.u.idx.last_key = STRBUF_INIT,
		},
		{
			.type = BLOCK_TYPE_INDEX,
			.u.idx.offset = 32,
			.u.idx.last_key = STRBUF_INIT,
		},
	};
	strbuf_addstr(&in[0].u.idx.last_key, "refs/heads/master");
	strbuf_addstr(&in[1].u.idx.last_key, "refs/heads/master");
	strbuf_addstr(&in[2].u.idx.last_key, "refs/heads/branch");

	check(!reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));

	check(!reftable_record_equal(&in[1], &in[2], GIT_SHA1_RAWSZ));
	check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);

	in[1].u.idx.offset = in[0].u.idx.offset;
	check(reftable_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
	check(!reftable_record_cmp(&in[0], &in[1]));

	for (size_t i = 0; i < ARRAY_SIZE(in); i++)
		reftable_record_release(&in[i]);
}

static void t_reftable_index_record_roundtrip(void)
{
	struct reftable_record in = {
		.type = BLOCK_TYPE_INDEX,
		.u.idx = {
			.offset = 42,
			.last_key = STRBUF_INIT,
		},
	};
	uint8_t buffer[1024] = { 0 };
	struct string_view dest = {
		.buf = buffer,
		.len = sizeof(buffer),
	};
	struct strbuf scratch = STRBUF_INIT;
	struct strbuf key = STRBUF_INIT;
	struct reftable_record out = {
		.type = BLOCK_TYPE_INDEX,
		.u.idx = { .last_key = STRBUF_INIT },
	};
	int n, m;
	uint8_t extra;

	strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
	reftable_record_key(&in, &key);
	t_copy(&in);

	check(!reftable_record_is_deletion(&in));
	check(!strbuf_cmp(&key, &in.u.idx.last_key));
	n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
	check_int(n, >, 0);

	extra = reftable_record_val_type(&in);
	m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
				   &scratch);
	check_int(m, ==, n);

	check(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));

	reftable_record_release(&out);
	strbuf_release(&key);
	strbuf_release(&scratch);
	strbuf_release(&in.u.idx.last_key);
}

int cmd_main(int argc, const char *argv[])
{
	TEST(t_reftable_ref_record_comparison(), "comparison operations work on ref record");
	TEST(t_reftable_log_record_comparison(), "comparison operations work on log record");
	TEST(t_reftable_index_record_comparison(), "comparison operations work on index record");
	TEST(t_reftable_obj_record_comparison(), "comparison operations work on obj record");
	TEST(t_reftable_ref_record_compare_name(), "reftable_ref_record_compare_name works");
	TEST(t_reftable_log_record_compare_key(), "reftable_log_record_compare_key works");
	TEST(t_reftable_log_record_roundtrip(), "record operations work on log record");
	TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record");
	TEST(t_varint_roundtrip(), "put_var_int and get_var_int work");
	TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work");
	TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record");
	TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");

	return test_done();
}