git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
73efeaf5234a87a65174b8ba2434223c08ec94da blob 13883 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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
 
/*
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 "reftable.h"

#include "system.h"

#include "basics.h"
#include "block.h"
#include "blocksource.h"
#include "constants.h"
#include "reader.h"
#include "record.h"
#include "test_framework.h"
#include "reftable-tests.h"

static const int update_index = 5;

static void test_buffer(void)
{
	struct strbuf buf = STRBUF_INIT;
	struct reftable_block_source source = { NULL };
	struct reftable_block out = { 0 };
	int n;
	uint8_t in[] = "hello";
	strbuf_add(&buf, in, sizeof(in));
	block_source_from_strbuf(&source, &buf);
	assert(block_source_size(&source) == 6);
	n = block_source_read_block(&source, &out, 0, sizeof(in));
	assert(n == sizeof(in));
	assert(!memcmp(in, out.data, n));
	reftable_block_done(&out);

	n = block_source_read_block(&source, &out, 1, 2);
	assert(n == 2);
	assert(!memcmp(out.data, "el", 2));

	reftable_block_done(&out);
	block_source_close(&source);
	strbuf_release(&buf);
}

static void write_table(char ***names, struct strbuf *buf, int N,
			int block_size, uint32_t hash_id)
{
	struct reftable_write_options opts = {
		.block_size = block_size,
		.hash_id = hash_id,
	};
	struct reftable_writer *w =
		reftable_new_writer(&strbuf_add_void, buf, &opts);
	struct reftable_ref_record ref = { 0 };
	int i = 0, n;
	struct reftable_log_record log = { 0 };
	const struct reftable_stats *stats = NULL;
	*names = reftable_calloc(sizeof(char *) * (N + 1));
	reftable_writer_set_limits(w, update_index, update_index);
	for (i = 0; i < N; i++) {
		uint8_t hash[SHA256_SIZE] = { 0 };
		char name[100];
		int n;

		set_test_hash(hash, i);

		snprintf(name, sizeof(name), "refs/heads/branch%02d", i);

		ref.refname = name;
		ref.value = hash;
		ref.update_index = update_index;
		(*names)[i] = xstrdup(name);

		n = reftable_writer_add_ref(w, &ref);
		assert(n == 0);
	}

	for (i = 0; i < N; i++) {
		uint8_t hash[SHA256_SIZE] = { 0 };
		char name[100];
		int n;

		set_test_hash(hash, i);

		snprintf(name, sizeof(name), "refs/heads/branch%02d", i);

		log.refname = name;
		log.new_hash = hash;
		log.update_index = update_index;
		log.message = "message";

		n = reftable_writer_add_log(w, &log);
		assert(n == 0);
	}

	n = reftable_writer_close(w);
	assert(n == 0);

	stats = writer_stats(w);
	for (i = 0; i < stats->ref_stats.blocks; i++) {
		int off = i * opts.block_size;
		if (off == 0) {
			off = header_size((hash_id == SHA256_ID) ? 2 : 1);
		}
		assert(buf->buf[off] == 'r');
	}

	assert(stats->log_stats.blocks > 0);
	reftable_writer_free(w);
}

static void test_log_buffer_size(void)
{
	struct strbuf buf = STRBUF_INIT;
	struct reftable_write_options opts = {
		.block_size = 4096,
	};
	int err;
	struct reftable_log_record log = {
		.refname = "refs/heads/master",
		.name = "Han-Wen Nienhuys",
		.email = "hanwen@google.com",
		.tz_offset = 100,
		.time = 0x5e430672,
		.update_index = 0xa,
		.message = "commit: 9\n",
	};
	struct reftable_writer *w =
		reftable_new_writer(&strbuf_add_void, &buf, &opts);

	/* This tests buffer extension for log compression. Must use a random
	   hash, to ensure that the compressed part is larger than the original.
	*/
	uint8_t hash1[SHA1_SIZE], hash2[SHA1_SIZE];
	for (int i = 0; i < SHA1_SIZE; i++) {
		hash1[i] = (uint8_t)(rand() % 256);
		hash2[i] = (uint8_t)(rand() % 256);
	}
	log.old_hash = hash1;
	log.new_hash = hash2;
	reftable_writer_set_limits(w, update_index, update_index);
	err = reftable_writer_add_log(w, &log);
	assert_err(err);
	err = reftable_writer_close(w);
	assert_err(err);
	reftable_writer_free(w);
	strbuf_release(&buf);
}

static void test_log_write_read(void)
{
	int N = 2;
	char **names = reftable_calloc(sizeof(char *) * (N + 1));
	int err;
	struct reftable_write_options opts = {
		.block_size = 256,
	};
	struct reftable_ref_record ref = { 0 };
	int i = 0;
	struct reftable_log_record log = { 0 };
	int n;
	struct reftable_iterator it = { 0 };
	struct reftable_reader rd = { 0 };
	struct reftable_block_source source = { 0 };
	struct strbuf buf = STRBUF_INIT;
	struct reftable_writer *w =
		reftable_new_writer(&strbuf_add_void, &buf, &opts);
	const struct reftable_stats *stats = NULL;
	reftable_writer_set_limits(w, 0, N);
	for (i = 0; i < N; i++) {
		char name[256];
		struct reftable_ref_record ref = { 0 };
		snprintf(name, sizeof(name), "b%02d%0*d", i, 130, 7);
		names[i] = xstrdup(name);
		ref.refname = name;
		ref.update_index = i;

		err = reftable_writer_add_ref(w, &ref);
		assert_err(err);
	}
	for (i = 0; i < N; i++) {
		uint8_t hash1[SHA1_SIZE], hash2[SHA1_SIZE];
		struct reftable_log_record log = { 0 };
		set_test_hash(hash1, i);
		set_test_hash(hash2, i + 1);

		log.refname = names[i];
		log.update_index = i;
		log.old_hash = hash1;
		log.new_hash = hash2;

		err = reftable_writer_add_log(w, &log);
		assert_err(err);
	}

	n = reftable_writer_close(w);
	assert(n == 0);

	stats = writer_stats(w);
	assert(stats->log_stats.blocks > 0);
	reftable_writer_free(w);
	w = NULL;

	block_source_from_strbuf(&source, &buf);

	err = init_reader(&rd, &source, "file.log");
	assert_err(err);

	err = reftable_reader_seek_ref(&rd, &it, names[N - 1]);
	assert_err(err);

	err = reftable_iterator_next_ref(&it, &ref);
	assert_err(err);

	/* end of iteration. */
	err = reftable_iterator_next_ref(&it, &ref);
	assert(0 < err);

	reftable_iterator_destroy(&it);
	reftable_ref_record_clear(&ref);

	err = reftable_reader_seek_log(&rd, &it, "");
	assert_err(err);

	i = 0;
	while (1) {
		int err = reftable_iterator_next_log(&it, &log);
		if (err > 0) {
			break;
		}

		assert_err(err);
		assert_streq(names[i], log.refname);
		assert(i == log.update_index);
		i++;
		reftable_log_record_clear(&log);
	}

	assert(i == N);
	reftable_iterator_destroy(&it);

	/* cleanup. */
	strbuf_release(&buf);
	free_names(names);
	reader_close(&rd);
}

static void test_table_read_write_sequential(void)
{
	char **names;
	struct strbuf buf = STRBUF_INIT;
	int N = 50;
	struct reftable_iterator it = { 0 };
	struct reftable_block_source source = { 0 };
	struct reftable_reader rd = { 0 };
	int err = 0;
	int j = 0;

	write_table(&names, &buf, N, 256, SHA1_ID);

	block_source_from_strbuf(&source, &buf);

	err = init_reader(&rd, &source, "file.ref");
	assert_err(err);

	err = reftable_reader_seek_ref(&rd, &it, "");
	assert_err(err);

	while (1) {
		struct reftable_ref_record ref = { 0 };
		int r = reftable_iterator_next_ref(&it, &ref);
		assert(r >= 0);
		if (r > 0) {
			break;
		}
		assert(0 == strcmp(names[j], ref.refname));
		assert(update_index == ref.update_index);

		j++;
		reftable_ref_record_clear(&ref);
	}
	assert(j == N);
	reftable_iterator_destroy(&it);
	strbuf_release(&buf);
	free_names(names);

	reader_close(&rd);
}

static void test_table_write_small_table(void)
{
	char **names;
	struct strbuf buf = STRBUF_INIT;
	int N = 1;
	write_table(&names, &buf, N, 4096, SHA1_ID);
	assert(buf.len < 200);
	strbuf_release(&buf);
	free_names(names);
}

static void test_table_read_api(void)
{
	char **names;
	struct strbuf buf = STRBUF_INIT;
	int N = 50;
	struct reftable_reader rd = { 0 };
	struct reftable_block_source source = { 0 };
	int err;
	int i;
	struct reftable_log_record log = { 0 };
	struct reftable_iterator it = { 0 };

	write_table(&names, &buf, N, 256, SHA1_ID);

	block_source_from_strbuf(&source, &buf);

	err = init_reader(&rd, &source, "file.ref");
	assert_err(err);

	err = reftable_reader_seek_ref(&rd, &it, names[0]);
	assert_err(err);

	err = reftable_iterator_next_log(&it, &log);
	assert(err == REFTABLE_API_ERROR);

	strbuf_release(&buf);
	for (i = 0; i < N; i++) {
		reftable_free(names[i]);
	}
	reftable_iterator_destroy(&it);
	reftable_free(names);
	reader_close(&rd);
	strbuf_release(&buf);
}

static void test_table_read_write_seek(int index, int hash_id)
{
	char **names;
	struct strbuf buf = STRBUF_INIT;
	int N = 50;
	struct reftable_reader rd = { 0 };
	struct reftable_block_source source = { 0 };
	int err;
	int i = 0;

	struct reftable_iterator it = { 0 };
	struct strbuf pastLast = STRBUF_INIT;
	struct reftable_ref_record ref = { 0 };

	write_table(&names, &buf, N, 256, hash_id);

	block_source_from_strbuf(&source, &buf);

	err = init_reader(&rd, &source, "file.ref");
	assert_err(err);
	assert(hash_id == reftable_reader_hash_id(&rd));

	if (!index) {
		rd.ref_offsets.index_offset = 0;
	} else {
		assert(rd.ref_offsets.index_offset > 0);
	}

	for (i = 1; i < N; i++) {
		int err = reftable_reader_seek_ref(&rd, &it, names[i]);
		assert_err(err);
		err = reftable_iterator_next_ref(&it, &ref);
		assert_err(err);
		assert(0 == strcmp(names[i], ref.refname));
		assert(i == ref.value[0]);

		reftable_ref_record_clear(&ref);
		reftable_iterator_destroy(&it);
	}

	strbuf_addstr(&pastLast, names[N - 1]);
	strbuf_addstr(&pastLast, "/");

	err = reftable_reader_seek_ref(&rd, &it, pastLast.buf);
	if (err == 0) {
		struct reftable_ref_record ref = { 0 };
		int err = reftable_iterator_next_ref(&it, &ref);
		assert(err > 0);
	} else {
		assert(err > 0);
	}

	strbuf_release(&pastLast);
	reftable_iterator_destroy(&it);

	strbuf_release(&buf);
	for (i = 0; i < N; i++) {
		reftable_free(names[i]);
	}
	reftable_free(names);
	reader_close(&rd);
}

static void test_table_read_write_seek_linear(void)
{
	test_table_read_write_seek(0, SHA1_ID);
}

static void test_table_read_write_seek_linear_sha256(void)
{
	test_table_read_write_seek(0, SHA256_ID);
}

static void test_table_read_write_seek_index(void)
{
	test_table_read_write_seek(1, SHA1_ID);
}

static void test_table_refs_for(int indexed)
{
	int N = 50;
	char **want_names = reftable_calloc(sizeof(char *) * (N + 1));
	int want_names_len = 0;
	uint8_t want_hash[SHA1_SIZE];

	struct reftable_write_options opts = {
		.block_size = 256,
	};
	struct reftable_ref_record ref = { 0 };
	int i = 0;
	int n;
	int err;
	struct reftable_reader rd;
	struct reftable_block_source source = { 0 };

	struct strbuf buf = STRBUF_INIT;
	struct reftable_writer *w =
		reftable_new_writer(&strbuf_add_void, &buf, &opts);

	struct reftable_iterator it = { 0 };
	int j;

	set_test_hash(want_hash, 4);

	for (i = 0; i < N; i++) {
		uint8_t hash[SHA1_SIZE];
		char fill[51] = { 0 };
		char name[100];
		uint8_t hash1[SHA1_SIZE];
		uint8_t hash2[SHA1_SIZE];
		struct reftable_ref_record ref = { 0 };

		memset(hash, i, sizeof(hash));
		memset(fill, 'x', 50);
		/* Put the variable part in the start */
		snprintf(name, sizeof(name), "br%02d%s", i, fill);
		name[40] = 0;
		ref.refname = name;

		set_test_hash(hash1, i / 4);
		set_test_hash(hash2, 3 + i / 4);
		ref.value = hash1;
		ref.target_value = hash2;

		/* 80 bytes / entry, so 3 entries per block. Yields 17
		 */
		/* blocks. */
		n = reftable_writer_add_ref(w, &ref);
		assert(n == 0);

		if (!memcmp(hash1, want_hash, SHA1_SIZE) ||
		    !memcmp(hash2, want_hash, SHA1_SIZE)) {
			want_names[want_names_len++] = xstrdup(name);
		}
	}

	n = reftable_writer_close(w);
	assert(n == 0);

	reftable_writer_free(w);
	w = NULL;

	block_source_from_strbuf(&source, &buf);

	err = init_reader(&rd, &source, "file.ref");
	assert_err(err);
	if (!indexed) {
		rd.obj_offsets.is_present = 0;
	}

	err = reftable_reader_seek_ref(&rd, &it, "");
	assert_err(err);
	reftable_iterator_destroy(&it);

	err = reftable_reader_refs_for(&rd, &it, want_hash);
	assert_err(err);

	j = 0;
	while (1) {
		int err = reftable_iterator_next_ref(&it, &ref);
		assert(err >= 0);
		if (err > 0) {
			break;
		}

		assert(j < want_names_len);
		assert(0 == strcmp(ref.refname, want_names[j]));
		j++;
		reftable_ref_record_clear(&ref);
	}
	assert(j == want_names_len);

	strbuf_release(&buf);
	free_names(want_names);
	reftable_iterator_destroy(&it);
	reader_close(&rd);
}

static void test_table_refs_for_no_index(void)
{
	test_table_refs_for(0);
}

static void test_table_refs_for_obj_index(void)
{
	test_table_refs_for(1);
}

static void test_table_empty(void)
{
	struct reftable_write_options opts = { 0 };
	struct strbuf buf = STRBUF_INIT;
	struct reftable_writer *w =
		reftable_new_writer(&strbuf_add_void, &buf, &opts);
	struct reftable_block_source source = { 0 };
	struct reftable_reader *rd = NULL;
	struct reftable_ref_record rec = { 0 };
	struct reftable_iterator it = { 0 };
	int err;

	reftable_writer_set_limits(w, 1, 1);

	err = reftable_writer_close(w);
	assert(err == REFTABLE_EMPTY_TABLE_ERROR);
	reftable_writer_free(w);

	assert(buf.len == header_size(1) + footer_size(1));

	block_source_from_strbuf(&source, &buf);

	err = reftable_new_reader(&rd, &source, "filename");
	assert_err(err);

	err = reftable_reader_seek_ref(rd, &it, "");
	assert_err(err);

	err = reftable_iterator_next_ref(&it, &rec);
	assert(err > 0);

	reftable_iterator_destroy(&it);
	reftable_reader_free(rd);
	strbuf_release(&buf);
}

int reftable_test_main(int argc, const char *argv[])
{
	add_test_case("test_log_write_read", test_log_write_read);
	add_test_case("test_table_read_write_seek_linear_sha256",
		      &test_table_read_write_seek_linear_sha256);
	add_test_case("test_log_buffer_size", test_log_buffer_size);
	add_test_case("test_table_write_small_table",
		      &test_table_write_small_table);
	add_test_case("test_buffer", &test_buffer);
	add_test_case("test_table_read_api", &test_table_read_api);
	add_test_case("test_table_read_write_sequential",
		      &test_table_read_write_sequential);
	add_test_case("test_table_read_write_seek_linear",
		      &test_table_read_write_seek_linear);
	add_test_case("test_table_read_write_seek_index",
		      &test_table_read_write_seek_index);
	add_test_case("test_table_read_write_refs_for_no_index",
		      &test_table_refs_for_no_index);
	add_test_case("test_table_read_write_refs_for_obj_index",
		      &test_table_refs_for_obj_index);
	add_test_case("test_table_empty", &test_table_empty);
	return test_main(argc, argv);
}
debug log:

solving 73efeaf523 ...
found 73efeaf523 in https://public-inbox.org/git/3448fcf82866602ea41c66160c92571f9736f61e.1600283416.git.gitgitgadget@gmail.com/

applying [1/1] https://public-inbox.org/git/3448fcf82866602ea41c66160c92571f9736f61e.1600283416.git.gitgitgadget@gmail.com/
diff --git a/reftable/reftable_test.c b/reftable/reftable_test.c
new file mode 100644
index 0000000000..73efeaf523

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

index at:
100644 73efeaf5234a87a65174b8ba2434223c08ec94da	reftable/reftable_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