Commit 2ca088bd2b253649943a112c40179d544e5083fa

Edward Thomson 2017-06-12T22:47:54

Merge pull request #4265 from pks-t/pks/read-prefix-tests Read prefix tests

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
diff --git a/tests/odb/backend/backend_helpers.c b/tests/odb/backend/backend_helpers.c
new file mode 100644
index 0000000..2653702
--- /dev/null
+++ b/tests/odb/backend/backend_helpers.c
@@ -0,0 +1,132 @@
+#include "clar_libgit2.h"
+#include "git2/sys/odb_backend.h"
+#include "backend_helpers.h"
+
+static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
+{
+	const fake_object *obj = fake->objects;
+
+	while (obj && obj->oid) {
+		git_oid current_oid;
+
+		git_oid_fromstr(&current_oid, obj->oid);
+
+		if (git_oid_ncmp(&current_oid, oid, len) == 0) {
+			if (out)
+				*out = obj;
+			return 0;
+		}
+
+		obj++;
+	}
+
+	return GIT_ENOTFOUND;
+}
+
+static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
+{
+	fake_backend *fake;
+
+	fake = (fake_backend *)backend;
+
+	fake->exists_calls++;
+
+	return search_object(NULL, fake, oid, GIT_OID_RAWSZ) == GIT_OK;
+}
+
+static int fake_backend__read(
+	void **buffer_p, size_t *len_p, git_otype *type_p,
+	git_odb_backend *backend, const git_oid *oid)
+{
+	const fake_object *obj;
+	fake_backend *fake;
+
+	fake = (fake_backend *)backend;
+
+	fake->read_calls++;
+
+	if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
+		*len_p = strlen(obj->content);
+		*buffer_p = git__strdup(obj->content);
+		*type_p = GIT_OBJ_BLOB;
+		return 0;
+	}
+
+	return GIT_ENOTFOUND;
+}
+
+static int fake_backend__read_header(
+	size_t *len_p, git_otype *type_p,
+	git_odb_backend *backend, const git_oid *oid)
+{
+	const fake_object *obj;
+	fake_backend *fake;
+
+	fake = (fake_backend *)backend;
+
+	fake->read_header_calls++;
+
+	if (search_object(&obj, fake, oid, GIT_OID_RAWSZ) == 0) {
+		*len_p = strlen(obj->content);
+		*type_p = GIT_OBJ_BLOB;
+		return 0;
+	}
+
+	return GIT_ENOTFOUND;
+}
+
+static int fake_backend__read_prefix(
+	git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
+	git_odb_backend *backend, const git_oid *short_oid, size_t len)
+{
+	const fake_object *obj;
+	fake_backend *fake;
+
+	fake = (fake_backend *)backend;
+
+	fake->read_prefix_calls++;
+
+	if (search_object(&obj, fake, short_oid, len) == 0) {
+		git_oid_fromstr(out_oid, obj->oid);
+		*len_p = strlen(obj->content);
+		*buffer_p = git__strdup(obj->content);
+		*type_p = GIT_OBJ_BLOB;
+		return 0;
+	}
+
+	return GIT_ENOTFOUND;
+}
+
+static void fake_backend__free(git_odb_backend *_backend)
+{
+	fake_backend *backend;
+
+	backend = (fake_backend *)_backend;
+
+	git__free(backend);
+}
+
+int build_fake_backend(
+	git_odb_backend **out,
+	const fake_object *objects)
+{
+	fake_backend *backend;
+
+	backend = git__calloc(1, sizeof(fake_backend));
+	GITERR_CHECK_ALLOC(backend);
+
+	backend->parent.version = GIT_ODB_BACKEND_VERSION;
+
+	backend->parent.refresh = NULL;
+	backend->objects = objects;
+
+	backend->parent.read = fake_backend__read;
+	backend->parent.read_prefix = fake_backend__read_prefix;
+	backend->parent.read_header = fake_backend__read_header;
+	backend->parent.exists = fake_backend__exists;
+	backend->parent.free = &fake_backend__free;
+
+	*out = (git_odb_backend *)backend;
+
+	return 0;
+}
diff --git a/tests/odb/backend/backend_helpers.h b/tests/odb/backend/backend_helpers.h
new file mode 100644
index 0000000..6cc1ce9
--- /dev/null
+++ b/tests/odb/backend/backend_helpers.h
@@ -0,0 +1,21 @@
+#include "git2/sys/odb_backend.h"
+
+typedef struct {
+	const char *oid;
+	const char *content;
+} fake_object;
+
+typedef struct {
+	git_odb_backend parent;
+
+	int exists_calls;
+	int read_calls;
+	int read_header_calls;
+	int read_prefix_calls;
+
+	const fake_object *objects;
+} fake_backend;
+
+int build_fake_backend(
+	git_odb_backend **out,
+	const fake_object *objects);
diff --git a/tests/odb/backend/multiple.c b/tests/odb/backend/multiple.c
new file mode 100644
index 0000000..1c6068d
--- /dev/null
+++ b/tests/odb/backend/multiple.c
@@ -0,0 +1,121 @@
+#include "clar_libgit2.h"
+#include "repository.h"
+#include "backend_helpers.h"
+
+#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
+
+static git_repository *_repo;
+static git_odb_object *_obj;
+static fake_backend *_fake_empty;
+static fake_backend *_fake_filled;
+
+static git_oid _existing_oid;
+
+static const fake_object _objects_filled[] = {
+	{ EXISTING_HASH, "" },
+	{ NULL, NULL }
+};
+
+static const fake_object _objects_empty[] = {
+	{ NULL, NULL }
+};
+
+void test_odb_backend_multiple__initialize(void)
+{
+	git_odb_backend *backend;
+
+	git_oid_fromstr(&_existing_oid, EXISTING_HASH);
+
+	_obj = NULL;
+	_repo = cl_git_sandbox_init("testrepo.git");
+
+	cl_git_pass(build_fake_backend(&backend, _objects_filled));
+	_fake_filled = (fake_backend *)backend;
+
+	cl_git_pass(build_fake_backend(&backend, _objects_empty));
+	_fake_empty = (fake_backend *)backend;
+}
+
+void test_odb_backend_multiple__cleanup(void)
+{
+	git_odb_object_free(_obj);
+	cl_git_sandbox_cleanup();
+}
+
+void test_odb_backend_multiple__read_with_empty_first_succeeds(void)
+{
+	git_odb *odb;
+
+	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
+
+	cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
+
+	cl_assert_equal_i(1, _fake_filled->read_calls);
+	cl_assert_equal_i(1, _fake_empty->read_calls);
+}
+
+void test_odb_backend_multiple__read_with_first_matching_stops(void)
+{
+	git_odb *odb;
+
+	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 10));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+	cl_git_pass(git_odb_read(&_obj, odb, &_existing_oid));
+
+	cl_assert_equal_i(1, _fake_filled->read_calls);
+	cl_assert_equal_i(0, _fake_empty->read_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_empty_succeeds(void)
+{
+	git_odb *odb;
+
+	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 10));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, 50));
+
+	cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+	cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+	cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_matching_reads_both(void)
+{
+	git_odb *odb;
+
+	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+	cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+	cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+	cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+}
+
+void test_odb_backend_multiple__read_prefix_with_first_matching_succeeds_without_hash_verification(void)
+{
+	git_odb *odb;
+
+	git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0);
+
+	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_empty, -10));
+	cl_git_pass(git_odb_add_backend(odb, (git_odb_backend *)_fake_filled, 50));
+
+	cl_git_pass(git_odb_read_prefix(&_obj, odb, &_existing_oid, 7));
+
+	/*
+	 * Both backends should be checked as we have to check
+	 * for collisions
+	 */
+	cl_assert_equal_i(1, _fake_filled->read_prefix_calls);
+	cl_assert_equal_i(1, _fake_empty->read_prefix_calls);
+
+	git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1);
+}
diff --git a/tests/odb/backend/nonrefreshing.c b/tests/odb/backend/nonrefreshing.c
index f12ac74..6abc0c6 100644
--- a/tests/odb/backend/nonrefreshing.c
+++ b/tests/odb/backend/nonrefreshing.c
@@ -1,153 +1,41 @@
 #include "clar_libgit2.h"
-#include "git2/sys/odb_backend.h"
 #include "repository.h"
-
-typedef struct fake_backend {
-	git_odb_backend parent;
-
-	git_error_code error_code;
-
-	int exists_calls;
-	int read_calls;
-	int read_header_calls;
-	int read_prefix_calls;
-} fake_backend;
+#include "backend_helpers.h"
 
 static git_repository *_repo;
 static fake_backend *_fake;
-static git_oid _oid;
-
-#define HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
-#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
 
-static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
-{
-	fake_backend *fake;
-
-	GIT_UNUSED(oid);
+#define NONEXISTING_HASH "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
+#define EXISTING_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
 
-	fake = (fake_backend *)backend;
+static const fake_object _objects[] = {
+	{ EXISTING_HASH, "" },
+	{ NULL, NULL }
+};
 
-	fake->exists_calls++;
-
-	return (fake->error_code == GIT_OK);
-}
+static git_oid _nonexisting_oid;
+static git_oid _existing_oid;
 
-static int fake_backend__read(
-	void **buffer_p, size_t *len_p, git_otype *type_p,
-	git_odb_backend *backend, const git_oid *oid)
-{
-	fake_backend *fake;
-
-	GIT_UNUSED(buffer_p);
-	GIT_UNUSED(len_p);
-	GIT_UNUSED(type_p);
-	GIT_UNUSED(oid);
-
-	fake = (fake_backend *)backend;
-
-	fake->read_calls++;
-
-	*len_p = 0;
-	*buffer_p = NULL;
-	*type_p = GIT_OBJ_BLOB;
-
-	return fake->error_code;
-}
-
-static int fake_backend__read_header(
-	size_t *len_p, git_otype *type_p,
-	git_odb_backend *backend, const git_oid *oid)
-{
-	fake_backend *fake;
-
-	GIT_UNUSED(len_p);
-	GIT_UNUSED(type_p);
-	GIT_UNUSED(oid);
-
-	fake = (fake_backend *)backend;
-
-	fake->read_header_calls++;
-
-	*len_p = 0;
-	*type_p = GIT_OBJ_BLOB;
-
-	return fake->error_code;
-}
-
-static int fake_backend__read_prefix(
-	git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p,
-	git_odb_backend *backend, const git_oid *short_oid,	size_t len)
-{
-	fake_backend *fake;
-
-	GIT_UNUSED(buffer_p);
-	GIT_UNUSED(len_p);
-	GIT_UNUSED(type_p);
-	GIT_UNUSED(short_oid);
-	GIT_UNUSED(len);
-
-	fake = (fake_backend *)backend;
-
-	fake->read_prefix_calls++;
-
-	git_oid_cpy(out_oid, &_oid);
-	*len_p = 0;
-	*buffer_p = NULL;
-	*type_p = GIT_OBJ_BLOB;
-
-	return fake->error_code;
-}
-
-static void fake_backend__free(git_odb_backend *_backend)
-{
-	fake_backend *backend;
-
-	backend = (fake_backend *)_backend;
-
-	git__free(backend);
-}
-
-static int build_fake_backend(
-	git_odb_backend **out,
-	git_error_code error_code)
-{
-	fake_backend *backend;
-
-	backend = git__calloc(1, sizeof(fake_backend));
-	GITERR_CHECK_ALLOC(backend);
-
-	backend->parent.version = GIT_ODB_BACKEND_VERSION;
-
-	backend->parent.refresh = NULL;
-	backend->error_code = error_code;
-
-	backend->parent.read = fake_backend__read;
-	backend->parent.read_prefix = fake_backend__read_prefix;
-	backend->parent.read_header = fake_backend__read_header;
-	backend->parent.exists = fake_backend__exists;
-	backend->parent.free = &fake_backend__free;
-
-	*out = (git_odb_backend *)backend;
-
-	return 0;
-}
-
-static void setup_repository_and_backend(git_error_code error_code, const char *hash)
+static void setup_repository_and_backend(void)
 {
 	git_odb *odb = NULL;
 	git_odb_backend *backend = NULL;
 
 	_repo = cl_git_sandbox_init("testrepo.git");
 
-	cl_git_pass(build_fake_backend(&backend, error_code));
+	cl_git_pass(build_fake_backend(&backend, _objects));
 
 	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
 	cl_git_pass(git_odb_add_backend(odb, backend, 10));
 
 	_fake = (fake_backend *)backend;
+}
 
-	cl_git_pass(git_oid_fromstr(&_oid, hash));
+void test_odb_backend_nonrefreshing__initialize(void)
+{
+	git_oid_fromstr(&_nonexisting_oid, NONEXISTING_HASH);
+	git_oid_fromstr(&_existing_oid, EXISTING_HASH);
+	setup_repository_and_backend();
 }
 
 void test_odb_backend_nonrefreshing__cleanup(void)
@@ -159,10 +47,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_failure(void)
 {
 	git_odb *odb;
 
-	setup_repository_and_backend(GIT_ENOTFOUND, HASH);
-
 	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
-	cl_assert_equal_b(false, git_odb_exists(odb, &_oid));
+	cl_assert_equal_b(false, git_odb_exists(odb, &_nonexisting_oid));
 
 	cl_assert_equal_i(1, _fake->exists_calls);
 }
@@ -171,10 +57,8 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_failure(void)
 {
 	git_object *obj;
 
-	setup_repository_and_backend(GIT_ENOTFOUND, HASH);
-
 	cl_git_fail_with(
-		git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY),
+		git_object_lookup(&obj, _repo, &_nonexisting_oid, GIT_OBJ_ANY),
 		GIT_ENOTFOUND);
 
 	cl_assert_equal_i(1, _fake->read_calls);
@@ -184,10 +68,8 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_failure(void)
 {
 	git_object *obj;
 
-	setup_repository_and_backend(GIT_ENOTFOUND, HASH);
-
 	cl_git_fail_with(
-		git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY),
+		git_object_lookup_prefix(&obj, _repo, &_nonexisting_oid, 7, GIT_OBJ_ANY),
 		GIT_ENOTFOUND);
 
 	cl_assert_equal_i(1, _fake->read_prefix_calls);
@@ -199,12 +81,10 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_failure(void)
 	size_t len;
 	git_otype type;
 
-	setup_repository_and_backend(GIT_ENOTFOUND, HASH);
-
 	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
 
 	cl_git_fail_with(
-		git_odb_read_header(&len, &type, odb, &_oid),
+		git_odb_read_header(&len, &type, odb, &_nonexisting_oid),
 		GIT_ENOTFOUND);
 
 	cl_assert_equal_i(1, _fake->read_header_calls);
@@ -214,10 +94,8 @@ void test_odb_backend_nonrefreshing__exists_is_invoked_once_on_success(void)
 {
 	git_odb *odb;
 
-	setup_repository_and_backend(GIT_OK, HASH);
-
 	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
-	cl_assert_equal_b(true, git_odb_exists(odb, &_oid));
+	cl_assert_equal_b(true, git_odb_exists(odb, &_existing_oid));
 
 	cl_assert_equal_i(1, _fake->exists_calls);
 }
@@ -226,9 +104,7 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_on_success(void)
 {
 	git_object *obj;
 
-	setup_repository_and_backend(GIT_OK, EMPTY_HASH);
-
-	cl_git_pass(git_object_lookup(&obj, _repo, &_oid, GIT_OBJ_ANY));
+	cl_git_pass(git_object_lookup(&obj, _repo, &_existing_oid, GIT_OBJ_ANY));
 
 	cl_assert_equal_i(1, _fake->read_calls);
 
@@ -239,9 +115,7 @@ void test_odb_backend_nonrefreshing__readprefix_is_invoked_once_on_success(void)
 {
 	git_object *obj;
 
-	setup_repository_and_backend(GIT_OK, EMPTY_HASH);
-
-	cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_oid, 7, GIT_OBJ_ANY));
+	cl_git_pass(git_object_lookup_prefix(&obj, _repo, &_existing_oid, 7, GIT_OBJ_ANY));
 
 	cl_assert_equal_i(1, _fake->read_prefix_calls);
 
@@ -254,11 +128,9 @@ void test_odb_backend_nonrefreshing__readheader_is_invoked_once_on_success(void)
 	size_t len;
 	git_otype type;
 
-	setup_repository_and_backend(GIT_OK, HASH);
-
 	cl_git_pass(git_repository_odb__weakptr(&odb, _repo));
 
-	cl_git_pass(git_odb_read_header(&len, &type, odb, &_oid));
+	cl_git_pass(git_odb_read_header(&len, &type, odb, &_existing_oid));
 
 	cl_assert_equal_i(1, _fake->read_header_calls);
 }
@@ -267,8 +139,6 @@ void test_odb_backend_nonrefreshing__read_is_invoked_once_when_revparsing_a_full
 {
 	git_object *obj;
 
-	setup_repository_and_backend(GIT_ENOTFOUND, HASH);
-
 	cl_git_fail_with(
 		git_revparse_single(&obj, _repo, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
 		GIT_ENOTFOUND);