git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
dcf1c74cb7a84c6bf5c93e628f6e1cf042fbe3c8 blob 10506 bytes (raw)

  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
 
/*
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 "record.h"

#include "system.h"
#include "basics.h"
#include "constants.h"
#include "reftable.h"
#include "test_framework.h"
#include "reftable-tests.h"

static void test_copy(struct reftable_record *rec)
{
	struct reftable_record copy =
		reftable_new_record(reftable_record_type(rec));
	reftable_record_copy_from(&copy, rec, SHA1_SIZE);
	/* do it twice to catch memory leaks */
	reftable_record_copy_from(&copy, rec, SHA1_SIZE);
	switch (reftable_record_type(&copy)) {
	case BLOCK_TYPE_REF:
		assert(reftable_ref_record_equal(reftable_record_as_ref(&copy),
						 reftable_record_as_ref(rec),
						 SHA1_SIZE));
		break;
	case BLOCK_TYPE_LOG:
		assert(reftable_log_record_equal(reftable_record_as_log(&copy),
						 reftable_record_as_log(rec),
						 SHA1_SIZE));
		break;
	}
	reftable_record_destroy(&copy);
}

static void test_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 };
	int i = 0;
	for (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;

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

		assert(got == in);
	}
}

static void test_common_prefix(void)
{
	struct {
		const char *a, *b;
		int want;
	} cases[] = {
		{ "abc", "ab", 2 },
		{ "", "abc", 0 },
		{ "abc", "abd", 2 },
		{ "abc", "pqr", 0 },
	};

	int i = 0;
	for (i = 0; i < ARRAY_SIZE(cases); i++) {
		struct strbuf a = STRBUF_INIT;
		struct strbuf b = STRBUF_INIT;
		strbuf_addstr(&a, cases[i].a);
		strbuf_addstr(&b, cases[i].b);
		assert(common_prefix_size(&a, &b) == cases[i].want);

		strbuf_release(&a);
		strbuf_release(&b);
	}
}

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

static void test_reftable_ref_record_roundtrip(void)
{
	int i = 0;

	for (i = 0; i <= 3; i++) {
		struct reftable_ref_record in = { NULL };
		struct reftable_ref_record out = {
			.refname = xstrdup("old name"),
			.value = reftable_calloc(SHA1_SIZE),
			.target_value = reftable_calloc(SHA1_SIZE),
			.target = xstrdup("old value"),
		};
		struct reftable_record rec_out = { NULL };
		struct strbuf key = STRBUF_INIT;
		struct reftable_record rec = { NULL };
		uint8_t buffer[1024] = { 0 };
		struct string_view dest = {
			.buf = buffer,
			.len = sizeof(buffer),
		};

		int n, m;

		switch (i) {
		case 0:
			break;
		case 1:
			in.value = reftable_malloc(SHA1_SIZE);
			set_hash(in.value, 1);
			break;
		case 2:
			in.value = reftable_malloc(SHA1_SIZE);
			set_hash(in.value, 1);
			in.target_value = reftable_malloc(SHA1_SIZE);
			set_hash(in.target_value, 2);
			break;
		case 3:
			in.target = xstrdup("target");
			break;
		}
		in.refname = xstrdup("refs/heads/master");

		reftable_record_from_ref(&rec, &in);
		test_copy(&rec);

		assert(reftable_record_val_type(&rec) == i);

		reftable_record_key(&rec, &key);
		n = reftable_record_encode(&rec, dest, SHA1_SIZE);
		assert(n > 0);

		/* decode into a non-zero reftable_record to test for leaks. */

		reftable_record_from_ref(&rec_out, &out);
		m = reftable_record_decode(&rec_out, key, i, dest, SHA1_SIZE);
		assert(n == m);

		assert((out.value != NULL) == (in.value != NULL));
		assert((out.target_value != NULL) == (in.target_value != NULL));
		assert((out.target != NULL) == (in.target != NULL));
		reftable_record_clear(&rec_out);

		strbuf_release(&key);
		reftable_ref_record_clear(&in);
	}
}

static void test_reftable_log_record_equal(void)
{
	struct reftable_log_record in[2] = {
		{
			.refname = xstrdup("refs/heads/master"),
			.update_index = 42,
		},
		{
			.refname = xstrdup("refs/heads/master"),
			.update_index = 22,
		}
	};

	assert(!reftable_log_record_equal(&in[0], &in[1], SHA1_SIZE));
	in[1].update_index = in[0].update_index;
	assert(reftable_log_record_equal(&in[0], &in[1], SHA1_SIZE));
	reftable_log_record_clear(&in[0]);
	reftable_log_record_clear(&in[1]);
}

static void test_reftable_log_record_roundtrip(void)
{
	struct reftable_log_record in[2] = {
		{
			.refname = xstrdup("refs/heads/master"),
			.old_hash = reftable_malloc(SHA1_SIZE),
			.new_hash = reftable_malloc(SHA1_SIZE),
			.name = xstrdup("han-wen"),
			.email = xstrdup("hanwen@google.com"),
			.message = xstrdup("test"),
			.update_index = 42,
			.time = 1577123507,
			.tz_offset = 100,
		},
		{
			.refname = xstrdup("refs/heads/master"),
			.update_index = 22,
		}
	};
	set_test_hash(in[0].new_hash, 1);
	set_test_hash(in[0].old_hash, 2);
	for (int i = 0; i < ARRAY_SIZE(in); i++) {
		struct reftable_record rec = { NULL };
		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_log_record out = {
			.refname = xstrdup("old name"),
			.new_hash = reftable_calloc(SHA1_SIZE),
			.old_hash = reftable_calloc(SHA1_SIZE),
			.name = xstrdup("old name"),
			.email = xstrdup("old@email"),
			.message = xstrdup("old message"),
		};
		struct reftable_record rec_out = { NULL };
		int n, m, valtype;

		reftable_record_from_log(&rec, &in[i]);

		test_copy(&rec);

		reftable_record_key(&rec, &key);

		n = reftable_record_encode(&rec, dest, SHA1_SIZE);
		assert(n >= 0);
		reftable_record_from_log(&rec_out, &out);
		valtype = reftable_record_val_type(&rec);
		m = reftable_record_decode(&rec_out, key, valtype, dest,
					   SHA1_SIZE);
		assert(n == m);

		assert(reftable_log_record_equal(&in[i], &out, SHA1_SIZE));
		reftable_log_record_clear(&in[i]);
		strbuf_release(&key);
		reftable_record_clear(&rec_out);
	}
}

static void test_u24_roundtrip(void)
{
	uint32_t in = 0x112233;
	uint8_t dest[3];
	uint32_t out;
	put_be24(dest, in);
	out = get_be24(dest);
	assert(in == out);
}

static void test_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);
	assert(!restart);
	assert(n > 0);

	m = reftable_decode_key(&roundtrip, &rt_extra, last_key, dest);
	assert(n == m);
	assert(0 == strbuf_cmp(&key, &roundtrip));
	assert(rt_extra == extra);

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

static void test_reftable_obj_record_roundtrip(void)
{
	uint8_t testHash1[SHA1_SIZE] = { 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,
					       } };
	int i = 0;
	for (i = 0; i < ARRAY_SIZE(recs); i++) {
		struct reftable_obj_record in = recs[i];
		uint8_t buffer[1024] = { 0 };
		struct string_view dest = {
			.buf = buffer,
			.len = sizeof(buffer),
		};
		struct reftable_record rec = { NULL };
		struct strbuf key = STRBUF_INIT;
		struct reftable_obj_record out = { NULL };
		struct reftable_record rec_out = { NULL };
		int n, m;
		uint8_t extra;

		reftable_record_from_obj(&rec, &in);
		test_copy(&rec);
		reftable_record_key(&rec, &key);
		n = reftable_record_encode(&rec, dest, SHA1_SIZE);
		assert(n > 0);
		extra = reftable_record_val_type(&rec);
		reftable_record_from_obj(&rec_out, &out);
		m = reftable_record_decode(&rec_out, key, extra, dest,
					   SHA1_SIZE);
		assert(n == m);

		assert(in.hash_prefix_len == out.hash_prefix_len);
		assert(in.offset_len == out.offset_len);

		assert(!memcmp(in.hash_prefix, out.hash_prefix,
			       in.hash_prefix_len));
		assert(0 == memcmp(in.offsets, out.offsets,
				   sizeof(uint64_t) * in.offset_len));
		strbuf_release(&key);
		reftable_record_clear(&rec_out);
	}
}

static void test_reftable_index_record_roundtrip(void)
{
	struct reftable_index_record in = {
		.offset = 42,
		.last_key = STRBUF_INIT,
	};
	uint8_t buffer[1024] = { 0 };
	struct string_view dest = {
		.buf = buffer,
		.len = sizeof(buffer),
	};
	struct strbuf key = STRBUF_INIT;
	struct reftable_record rec = { NULL };
	struct reftable_index_record out = { .last_key = STRBUF_INIT };
	struct reftable_record out_rec = { NULL };
	int n, m;
	uint8_t extra;

	strbuf_addstr(&in.last_key, "refs/heads/master");
	reftable_record_from_index(&rec, &in);
	reftable_record_key(&rec, &key);
	test_copy(&rec);

	assert(0 == strbuf_cmp(&key, &in.last_key));
	n = reftable_record_encode(&rec, dest, SHA1_SIZE);
	assert(n > 0);

	extra = reftable_record_val_type(&rec);
	reftable_record_from_index(&out_rec, &out);
	m = reftable_record_decode(&out_rec, key, extra, dest, SHA1_SIZE);
	assert(m == n);

	assert(in.offset == out.offset);

	reftable_record_clear(&out_rec);
	strbuf_release(&key);
	strbuf_release(&in.last_key);
}

int record_test_main(int argc, const char *argv[])
{
	add_test_case("test_reftable_log_record_equal",
		      &test_reftable_log_record_equal);
	add_test_case("test_reftable_log_record_roundtrip",
		      &test_reftable_log_record_roundtrip);
	add_test_case("test_reftable_ref_record_roundtrip",
		      &test_reftable_ref_record_roundtrip);
	add_test_case("test_varint_roundtrip", &test_varint_roundtrip);
	add_test_case("test_key_roundtrip", &test_key_roundtrip);
	add_test_case("test_common_prefix", &test_common_prefix);
	add_test_case("test_reftable_obj_record_roundtrip",
		      &test_reftable_obj_record_roundtrip);
	add_test_case("test_reftable_index_record_roundtrip",
		      &test_reftable_index_record_roundtrip);
	add_test_case("test_u24_roundtrip", &test_u24_roundtrip);
	return test_main(argc, argv);
}
debug log:

solving dcf1c74cb7 ...
found dcf1c74cb7 in https://public-inbox.org/git/8eb944ea9b1c0bdc4a3ebb4e79fd12187f6c988c.1601568663.git.gitgitgadget@gmail.com/

applying [1/1] https://public-inbox.org/git/8eb944ea9b1c0bdc4a3ebb4e79fd12187f6c988c.1601568663.git.gitgitgadget@gmail.com/
diff --git a/reftable/record_test.c b/reftable/record_test.c
new file mode 100644
index 0000000000..dcf1c74cb7

Checking patch reftable/record_test.c...
Applied patch reftable/record_test.c cleanly.

index at:
100644 dcf1c74cb7a84c6bf5c93e628f6e1cf042fbe3c8	reftable/record_test.c

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/inbox.comp.version-control.git
	nntp://ie5yzdi7fg72h7s4sdcztq5evakq23rdt33mfyfcddc5u3ndnw24ogqd.onion/inbox.comp.version-control.git
	nntp://4uok3hntl7oi7b4uf4rtfwefqeexfzil2w6kgk2jn5z2f764irre7byd.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git