Commit 953427b379b127d75596f723f358f7f0ea8a2d8d

Edward Thomson 2017-06-14T10:47:55

Merge pull request #4269 from pks-t/pks/tests Test improvements

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
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 354ae3e..4783e3e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -468,9 +468,6 @@ IF (MSVC)
 	SET(CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL}")
 
 	SET(WIN_RC "src/win32/git2.rc")
-
-   # Precompiled headers
-
 ELSE ()
 	SET(CMAKE_C_FLAGS "-D_GNU_SOURCE ${CMAKE_C_FLAGS}")
 
@@ -481,9 +478,7 @@ ELSE ()
 		SET(CMAKE_C_FLAGS "-std=c99 -D_POSIX_C_SOURCE=200112L -D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS ${CMAKE_C_FLAGS}")
 	ENDIF()
 
-	IF (WIN32 AND NOT CYGWIN)
-		SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG")
-	ENDIF ()
+	SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D_DEBUG -O0")
 
 	IF (MINGW OR MSYS) # MinGW and MSYS always do PIC and complain if we tell them to
 		STRING(REGEX REPLACE "-fPIC" "" CMAKE_SHARED_LIBRARY_C_FLAGS "${CMAKE_SHARED_LIBRARY_C_FLAGS}")
diff --git a/tests/core/structinit.c b/tests/core/structinit.c
index e9f7b4a..2d21440 100644
--- a/tests/core/structinit.c
+++ b/tests/core/structinit.c
@@ -165,4 +165,9 @@ void test_core_structinit__compare(void)
 	CHECK_MACRO_FUNC_INIT_EQUAL( \
 		git_submodule_update_options, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, \
 		GIT_SUBMODULE_UPDATE_OPTIONS_INIT, git_submodule_update_init_options);
+
+	/* submodule update */
+	CHECK_MACRO_FUNC_INIT_EQUAL( \
+		git_proxy_options, GIT_PROXY_OPTIONS_VERSION, \
+		GIT_PROXY_OPTIONS_INIT, git_proxy_init_options);
 }
diff --git a/tests/network/remote/local.c b/tests/network/remote/local.c
index 7bae038..76872e5 100644
--- a/tests/network/remote/local.c
+++ b/tests/network/remote/local.c
@@ -2,6 +2,7 @@
 #include "buffer.h"
 #include "path.h"
 #include "posix.h"
+#include "git2/sys/repository.h"
 
 static git_repository *repo;
 static git_buf file_path_buf = GIT_BUF_INIT;
diff --git a/tests/odb/backend/backend_helpers.c b/tests/odb/backend/backend_helpers.c
index 2653702..37a8fd2 100644
--- a/tests/odb/backend/backend_helpers.c
+++ b/tests/odb/backend/backend_helpers.c
@@ -4,7 +4,7 @@
 
 static int search_object(const fake_object **out, fake_backend *fake, const git_oid *oid, size_t len)
 {
-	const fake_object *obj = fake->objects;
+	const fake_object *obj = fake->objects, *found = NULL;
 
 	while (obj && obj->oid) {
 		git_oid current_oid;
@@ -12,15 +12,18 @@ static int search_object(const fake_object **out, fake_backend *fake, const git_
 		git_oid_fromstr(&current_oid, obj->oid);
 
 		if (git_oid_ncmp(&current_oid, oid, len) == 0) {
-			if (out)
-				*out = obj;
-			return 0;
+			if (found)
+				return GIT_EAMBIGUOUS;
+			found = obj;
 		}
 
 		obj++;
 	}
 
-	return GIT_ENOTFOUND;
+	if (found && out)
+		*out = found;
+
+	return found ? GIT_OK : GIT_ENOTFOUND;
 }
 
 static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
@@ -31,7 +34,27 @@ static int fake_backend__exists(git_odb_backend *backend, const git_oid *oid)
 
 	fake->exists_calls++;
 
-	return search_object(NULL, fake, oid, GIT_OID_RAWSZ) == GIT_OK;
+	return search_object(NULL, fake, oid, GIT_OID_HEXSZ) == GIT_OK;
+}
+
+static int fake_backend__exists_prefix(
+	git_oid *out, git_odb_backend *backend, const git_oid *oid, size_t len)
+{
+	const fake_object *obj;
+	fake_backend *fake;
+	int error;
+
+	fake = (fake_backend *)backend;
+
+	fake->exists_prefix_calls++;
+
+	if ((error = search_object(&obj, fake, oid, len)) < 0)
+		return error;
+
+	if (out)
+		git_oid_fromstr(out, obj->oid);
+
+	return 0;
 }
 
 static int fake_backend__read(
@@ -40,19 +63,20 @@ static int fake_backend__read(
 {
 	const fake_object *obj;
 	fake_backend *fake;
+	int error;
 
 	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;
-	}
+	if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+		return error;
 
-	return GIT_ENOTFOUND;
+	*len_p = strlen(obj->content);
+	*buffer_p = git__strdup(obj->content);
+	*type_p = GIT_OBJ_BLOB;
+
+	return 0;
 }
 
 static int fake_backend__read_header(
@@ -61,18 +85,19 @@ static int fake_backend__read_header(
 {
 	const fake_object *obj;
 	fake_backend *fake;
+	int error;
 
 	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;
-	}
+	if ((error = search_object(&obj, fake, oid, GIT_OID_HEXSZ)) < 0)
+		return error;
 
-	return GIT_ENOTFOUND;
+	*len_p = strlen(obj->content);
+	*type_p = GIT_OBJ_BLOB;
+
+	return 0;
 }
 
 static int fake_backend__read_prefix(
@@ -81,20 +106,21 @@ static int fake_backend__read_prefix(
 {
 	const fake_object *obj;
 	fake_backend *fake;
+	int error;
 
 	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;
-	}
+	if ((error = search_object(&obj, fake, short_oid, len)) < 0)
+		return error;
 
-	return GIT_ENOTFOUND;
+	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;
 }
 
 static void fake_backend__free(git_odb_backend *_backend)
@@ -124,6 +150,7 @@ int build_fake_backend(
 	backend->parent.read_prefix = fake_backend__read_prefix;
 	backend->parent.read_header = fake_backend__read_header;
 	backend->parent.exists = fake_backend__exists;
+	backend->parent.exists_prefix = fake_backend__exists_prefix;
 	backend->parent.free = &fake_backend__free;
 
 	*out = (git_odb_backend *)backend;
diff --git a/tests/odb/backend/backend_helpers.h b/tests/odb/backend/backend_helpers.h
index 6cc1ce9..5c393c0 100644
--- a/tests/odb/backend/backend_helpers.h
+++ b/tests/odb/backend/backend_helpers.h
@@ -9,6 +9,7 @@ typedef struct {
 	git_odb_backend parent;
 
 	int exists_calls;
+	int exists_prefix_calls;
 	int read_calls;
 	int read_header_calls;
 	int read_prefix_calls;
diff --git a/tests/odb/backend/simple.c b/tests/odb/backend/simple.c
new file mode 100644
index 0000000..c0fcd40
--- /dev/null
+++ b/tests/odb/backend/simple.c
@@ -0,0 +1,232 @@
+#include "clar_libgit2.h"
+#include "repository.h"
+#include "backend_helpers.h"
+
+#define EMPTY_HASH "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"
+
+static git_repository *_repo;
+static git_odb *_odb;
+static git_odb_object *_obj;
+static git_oid _oid;
+
+static void setup_backend(const fake_object *objs)
+{
+	git_odb_backend *backend;
+
+	cl_git_pass(build_fake_backend(&backend, objs));
+
+	cl_git_pass(git_repository_odb__weakptr(&_odb, _repo));
+	cl_git_pass(git_odb_add_backend(_odb, backend, 10));
+}
+
+static void assert_object_contains(git_odb_object *obj, const char *expected)
+{
+	const char *actual = (const char *) git_odb_object_data(obj);
+
+	cl_assert_equal_s(actual, expected);
+}
+
+void test_odb_backend_simple__initialize(void)
+{
+	_repo = cl_git_sandbox_init("testrepo.git");
+	_odb = NULL;
+	_obj = NULL;
+}
+
+void test_odb_backend_simple__cleanup(void)
+{
+	git_odb_object_free(_obj);
+	cl_git_sandbox_cleanup();
+	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 1));
+}
+
+void test_odb_backend_simple__read_of_object_succeeds(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+	assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_of_nonexisting_object_fails(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
+	cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_hash_mismatch_fails(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890123456789012345678901234567890", "nonmatching content" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_fail_with(GIT_EMISMATCH, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_hash_mismatch_succeeds_without_verification(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890123456789012345678901234567890", "nonmatching content" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+
+	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+	assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_prefix_succeeds(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4632"));
+	cl_git_pass(git_odb_read(&_obj, _odb, &_oid));
+
+	assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__read_prefix_of_nonexisting_object_fails(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+	char *hash = "f6ea0495187600e8";
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstrn(&_oid, hash, strlen(hash)));
+	cl_git_fail_with(GIT_ENOTFOUND, git_odb_read(&_obj, _odb, &_oid));
+}
+
+void test_odb_backend_simple__read_with_ambiguous_prefix_fails(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890111111111111111111111111111111", "first content" },
+		{ "1234567890222222222222222222222222222222", "second content" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 7));
+}
+
+void test_odb_backend_simple__read_with_highly_ambiguous_prefix(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890111111111111111111111111111111", "first content" },
+		{ "1234567890111111111111111111111111111112", "second content" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_read_prefix(&_obj, _odb, &_oid, 39));
+	cl_git_pass(git_odb_read_prefix(&_obj, _odb, &_oid, 40));
+	assert_object_contains(_obj, objs[0].content);
+}
+
+void test_odb_backend_simple__exists_succeeds(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_assert(git_odb_exists(_odb, &_oid));
+}
+
+void test_odb_backend_simple__exists_fails_for_nonexisting_object(void)
+{
+	const fake_object objs[] = {
+		{ "f6ea0495187600e7b2288c8ac19c5886383a4632", "foobar" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, "f6ea0495187600e7b2288c8ac19c5886383a4633"));
+	cl_assert(git_odb_exists(_odb, &_oid) == 0);
+}
+
+void test_odb_backend_simple__exists_prefix_succeeds(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890111111111111111111111111111111", "first content" },
+		{ "1234567890222222222222222222222222222222", "second content" },
+		{ NULL, NULL }
+	};
+	git_oid found;
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 12));
+	cl_assert(git_oid_equal(&found, &_oid));
+}
+
+void test_odb_backend_simple__exists_with_ambiguous_prefix_fails(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890111111111111111111111111111111", "first content" },
+		{ "1234567890222222222222222222222222222222", "second content" },
+		{ NULL, NULL }
+	};
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(NULL, _odb, &_oid, 7));
+}
+
+void test_odb_backend_simple__exists_with_highly_ambiguous_prefix(void)
+{
+	const fake_object objs[] = {
+		{ "1234567890111111111111111111111111111111", "first content" },
+		{ "1234567890111111111111111111111111111112", "second content" },
+		{ NULL, NULL }
+	};
+	git_oid found;
+
+	setup_backend(objs);
+
+	cl_git_pass(git_oid_fromstr(&_oid, objs[0].oid));
+	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION, 0));
+	cl_git_fail_with(GIT_EAMBIGUOUS, git_odb_exists_prefix(&found, _odb, &_oid, 39));
+	cl_git_pass(git_odb_exists_prefix(&found, _odb, &_oid, 40));
+	cl_assert(git_oid_equal(&found, &_oid));
+}