]> git.proxmox.com Git - libgit2.git/blob - tests/online/clone.c
Merge pull request #2499 from csware/hard-reset-checkout-callbacks
[libgit2.git] / tests / online / clone.c
1 #include "clar_libgit2.h"
2
3 #include "git2/clone.h"
4 #include "git2/cred_helpers.h"
5 #include "remote.h"
6 #include "fileops.h"
7 #include "refs.h"
8
9 #define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
10 #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
11 #define BB_REPO_URL "https://libgit3@bitbucket.org/libgit2/testgitrepository.git"
12 #define BB_REPO_URL_WITH_PASS "https://libgit3:libgit3@bitbucket.org/libgit2/testgitrepository.git"
13 #define BB_REPO_URL_WITH_WRONG_PASS "https://libgit3:wrong@bitbucket.org/libgit2/testgitrepository.git"
14
15 #define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"
16
17 static git_repository *g_repo;
18 static git_clone_options g_options;
19
20 void test_online_clone__initialize(void)
21 {
22 git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
23 git_remote_callbacks dummy_callbacks = GIT_REMOTE_CALLBACKS_INIT;
24
25 g_repo = NULL;
26
27 memset(&g_options, 0, sizeof(git_clone_options));
28 g_options.version = GIT_CLONE_OPTIONS_VERSION;
29 g_options.checkout_opts = dummy_opts;
30 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
31 g_options.remote_callbacks = dummy_callbacks;
32 }
33
34 void test_online_clone__cleanup(void)
35 {
36 if (g_repo) {
37 git_repository_free(g_repo);
38 g_repo = NULL;
39 }
40 cl_fixture_cleanup("./foo");
41 }
42
43 void test_online_clone__network_full(void)
44 {
45 git_remote *origin;
46
47 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
48 cl_assert(!git_repository_is_bare(g_repo));
49 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
50
51 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);
52
53 git_remote_free(origin);
54 }
55
56 void test_online_clone__network_bare(void)
57 {
58 git_remote *origin;
59
60 g_options.bare = true;
61
62 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
63 cl_assert(git_repository_is_bare(g_repo));
64 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
65
66 git_remote_free(origin);
67 }
68
69 void test_online_clone__empty_repository(void)
70 {
71 git_reference *head;
72
73 cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options));
74
75 cl_assert_equal_i(true, git_repository_is_empty(g_repo));
76 cl_assert_equal_i(true, git_repository_head_unborn(g_repo));
77
78 cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
79 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
80 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
81
82 git_reference_free(head);
83 }
84
85 static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
86 {
87 bool *was_called = (bool*)payload;
88 GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
89 (*was_called) = true;
90 }
91
92 static int fetch_progress(const git_transfer_progress *stats, void *payload)
93 {
94 bool *was_called = (bool*)payload;
95 GIT_UNUSED(stats);
96 (*was_called) = true;
97 return 0;
98 }
99
100 void test_online_clone__can_checkout_a_cloned_repo(void)
101 {
102 git_buf path = GIT_BUF_INIT;
103 git_reference *head;
104 bool checkout_progress_cb_was_called = false,
105 fetch_progress_cb_was_called = false;
106
107 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
108 g_options.checkout_opts.progress_cb = &checkout_progress;
109 g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
110 g_options.remote_callbacks.transfer_progress = &fetch_progress;
111 g_options.remote_callbacks.payload = &fetch_progress_cb_was_called;
112
113 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
114
115 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
116 cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path)));
117
118 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
119 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
120 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
121
122 cl_assert_equal_i(true, checkout_progress_cb_was_called);
123 cl_assert_equal_i(true, fetch_progress_cb_was_called);
124
125 git_reference_free(head);
126 git_buf_free(&path);
127 }
128
129 static int remote_mirror_cb(git_remote **out, git_repository *repo,
130 const char *name, const char *url, void *payload)
131 {
132 int error;
133 git_remote *remote;
134 git_remote_callbacks *callbacks = (git_remote_callbacks *) payload;
135
136
137 if ((error = git_remote_create(&remote, repo, name, url)) < 0)
138 return error;
139
140 if ((error = git_remote_set_callbacks(remote, callbacks)) < 0) {
141 git_remote_free(remote);
142 return error;
143 }
144
145 git_remote_clear_refspecs(remote);
146
147 if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
148 git_remote_free(remote);
149 return error;
150 }
151
152 *out = remote;
153 return 0;
154 }
155
156 void test_online_clone__clone_mirror(void)
157 {
158 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
159 git_reference *head;
160 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
161
162 bool fetch_progress_cb_was_called = false;
163
164 callbacks.transfer_progress = &fetch_progress;
165 callbacks.payload = &fetch_progress_cb_was_called;
166
167 opts.bare = true;
168 opts.remote_cb = remote_mirror_cb;
169 opts.remote_cb_payload = &callbacks;
170
171 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
172
173 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
174 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
175 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
176
177 cl_assert_equal_i(true, fetch_progress_cb_was_called);
178
179 git_reference_free(head);
180 git_repository_free(g_repo);
181 g_repo = NULL;
182
183 cl_fixture_cleanup("./foo.git");
184 }
185
186 static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *payload)
187 {
188 int *callcount = (int*)payload;
189 GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b);
190 *callcount = *callcount + 1;
191 return 0;
192 }
193
194 void test_online_clone__custom_remote_callbacks(void)
195 {
196 int callcount = 0;
197
198 g_options.remote_callbacks.update_tips = update_tips;
199 g_options.remote_callbacks.payload = &callcount;
200
201 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
202 cl_assert(callcount > 0);
203 }
204
205 static int cred_failure_cb(
206 git_cred **cred,
207 const char *url,
208 const char *username_from_url,
209 unsigned int allowed_types,
210 void *data)
211 {
212 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
213 GIT_UNUSED(allowed_types); GIT_UNUSED(data);
214 return -172;
215 }
216
217 void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
218 {
219 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
220 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
221
222 if (!remote_url || !remote_user)
223 clar__skip();
224
225 g_options.remote_callbacks.credentials = cred_failure_cb;
226
227 cl_git_fail_with(-172, git_clone(&g_repo, remote_url, "./foo", &g_options));
228 }
229
230 static int cred_count_calls_cb(git_cred **cred, const char *url, const char *user,
231 unsigned int allowed_types, void *data)
232 {
233 size_t *counter = (size_t *) data;
234
235 GIT_UNUSED(url); GIT_UNUSED(user); GIT_UNUSED(allowed_types);
236
237 if (allowed_types == GIT_CREDTYPE_USERNAME)
238 return git_cred_username_new(cred, "foo");
239
240 (*counter)++;
241
242 if (*counter == 3)
243 return GIT_EUSER;
244
245 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
246 }
247
248 void test_online_clone__cred_callback_called_again_on_auth_failure(void)
249 {
250 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
251 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
252 size_t counter = 0;
253
254 if (!remote_url || !remote_user)
255 clar__skip();
256
257 g_options.remote_callbacks.credentials = cred_count_calls_cb;
258 g_options.remote_callbacks.payload = &counter;
259
260 cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, remote_url, "./foo", &g_options));
261 cl_assert_equal_i(3, counter);
262 }
263
264 int cred_default(
265 git_cred **cred,
266 const char *url,
267 const char *user_from_url,
268 unsigned int allowed_types,
269 void *payload)
270 {
271 GIT_UNUSED(url);
272 GIT_UNUSED(user_from_url);
273 GIT_UNUSED(payload);
274
275 if (!(allowed_types & GIT_CREDTYPE_DEFAULT))
276 return 0;
277
278 return git_cred_default_new(cred);
279 }
280
281 void test_online_clone__credentials(void)
282 {
283 /* Remote URL environment variable must be set.
284 * User and password are optional.
285 */
286 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
287 git_cred_userpass_payload user_pass = {
288 cl_getenv("GITTEST_REMOTE_USER"),
289 cl_getenv("GITTEST_REMOTE_PASS")
290 };
291
292 if (!remote_url) return;
293
294 if (cl_getenv("GITTEST_REMOTE_DEFAULT")) {
295 g_options.remote_callbacks.credentials = cred_default;
296 } else {
297 g_options.remote_callbacks.credentials = git_cred_userpass;
298 g_options.remote_callbacks.payload = &user_pass;
299 }
300
301 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
302 git_repository_free(g_repo); g_repo = NULL;
303 cl_fixture_cleanup("./foo");
304 }
305
306 void test_online_clone__bitbucket_style(void)
307 {
308 git_cred_userpass_payload user_pass = {
309 "libgit2", "libgit2"
310 };
311
312 g_options.remote_callbacks.credentials = git_cred_userpass;
313 g_options.remote_callbacks.payload = &user_pass;
314
315 cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
316 git_repository_free(g_repo); g_repo = NULL;
317 cl_fixture_cleanup("./foo");
318
319 /* User and pass from URL */
320 user_pass.password = "wrong";
321 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_PASS, "./foo", &g_options));
322 git_repository_free(g_repo); g_repo = NULL;
323 cl_fixture_cleanup("./foo");
324
325 /* Wrong password in URL, fall back to user_pass */
326 user_pass.password = "libgit2";
327 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_WRONG_PASS, "./foo", &g_options));
328 git_repository_free(g_repo); g_repo = NULL;
329 cl_fixture_cleanup("./foo");
330 }
331
332 static int cancel_at_half(const git_transfer_progress *stats, void *payload)
333 {
334 GIT_UNUSED(payload);
335
336 if (stats->received_objects > (stats->total_objects/2))
337 return 4321;
338 return 0;
339 }
340
341 void test_online_clone__can_cancel(void)
342 {
343 g_options.remote_callbacks.transfer_progress = cancel_at_half;
344
345 cl_git_fail_with(
346 git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
347 }
348
349 static int cred_cb(git_cred **cred, const char *url, const char *user_from_url,
350 unsigned int allowed_types, void *payload)
351 {
352 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
353 const char *pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
354 const char *privkey = cl_getenv("GITTEST_REMOTE_SSH_KEY");
355 const char *passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
356
357 GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);
358
359 if (allowed_types & GIT_CREDTYPE_USERNAME)
360 return git_cred_username_new(cred, remote_user);
361
362 if (allowed_types & GIT_CREDTYPE_SSH_KEY)
363 return git_cred_ssh_key_new(cred, remote_user, pubkey, privkey, passphrase);
364
365 giterr_set(GITERR_NET, "unexpected cred type");
366 return -1;
367 }
368
369 static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
370 unsigned int allowed_types, void *data)
371 {
372 int *with_user = (int *) data;
373 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
374
375 if (!*with_user)
376 cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
377 else
378 cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
379
380 return GIT_EUSER;
381 }
382
383 void test_online_clone__ssh_auth_methods(void)
384 {
385 int with_user;
386
387 #ifndef GIT_SSH
388 clar__skip();
389 #endif
390 g_options.remote_callbacks.credentials = check_ssh_auth_methods;
391 g_options.remote_callbacks.payload = &with_user;
392
393 with_user = 0;
394 cl_git_fail_with(GIT_EUSER,
395 git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
396
397 with_user = 1;
398 cl_git_fail_with(GIT_EUSER,
399 git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
400 }
401
402 static int custom_remote_ssh_with_paths(
403 git_remote **out,
404 git_repository *repo,
405 const char *name,
406 const char *url,
407 void *payload)
408 {
409 int error;
410 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
411
412 if ((error = git_remote_create(out, repo, name, url)) < 0)
413 return error;
414
415 if ((error = git_remote_set_transport(*out, git_transport_ssh_with_paths, payload)) < 0)
416 return error;
417
418 callbacks.credentials = cred_cb;
419 git_remote_set_callbacks(*out, &callbacks);
420
421 return 0;
422 }
423
424 void test_online_clone__ssh_with_paths(void)
425 {
426 char *bad_paths[] = {
427 "/bin/yes",
428 "/bin/false",
429 };
430 char *good_paths[] = {
431 "/usr/bin/git-upload-pack",
432 "/usr/bin/git-receive-pack",
433 };
434 git_strarray arr = {
435 bad_paths,
436 2,
437 };
438
439 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
440 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
441
442 #ifndef GIT_SSH
443 clar__skip();
444 #endif
445 if (!remote_url || !remote_user || strncmp(remote_url, "ssh://", 5) != 0)
446 clar__skip();
447
448 g_options.remote_cb = custom_remote_ssh_with_paths;
449 g_options.remote_cb_payload = &arr;
450
451 cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options));
452
453 arr.strings = good_paths;
454 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
455 }
456
457 static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url,
458 unsigned int allowed_types, void *data)
459
460 {
461 GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data);
462
463 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
464 }
465
466 void test_online_clone__ssh_cannot_change_username(void)
467 {
468 #ifndef GIT_SSH
469 clar__skip();
470 #endif
471 g_options.remote_callbacks.credentials = cred_foo_bar;
472
473 cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
474 }
475
476 int ssh_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
477 {
478 git_cert_hostkey *key;
479 git_oid expected = {{0}}, actual = {{0}};
480 const char *expected_str;
481
482 GIT_UNUSED(valid);
483 GIT_UNUSED(payload);
484
485 expected_str = cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
486 cl_assert(expected_str);
487
488 cl_git_pass(git_oid_fromstrp(&expected, expected_str));
489 cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2, cert->cert_type);
490 key = (git_cert_hostkey *) cert;
491
492 /*
493 * We need to figure out how long our input was to check for
494 * the type. Here we abuse the fact that both hashes fit into
495 * our git_oid type.
496 */
497 if (strlen(expected_str) == 32 && key->type & GIT_CERT_SSH_MD5) {
498 memcpy(&actual.id, key->hash_md5, 16);
499 } else if (strlen(expected_str) == 40 && key->type & GIT_CERT_SSH_SHA1) {
500 memcpy(&actual, key->hash_sha1, 20);
501 } else {
502 cl_fail("Cannot find a usable SSH hash");
503 }
504
505 cl_assert(!memcmp(&expected, &actual, 20));
506
507 cl_assert_equal_s("localhost", host);
508
509 return GIT_EUSER;
510 }
511
512 void test_online_clone__ssh_cert(void)
513 {
514 g_options.remote_callbacks.certificate_check = ssh_certificate_check;
515
516 if (!cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT"))
517 cl_skip();
518
519 cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options));
520 }
521
522 void test_online_clone__url_with_no_path_returns_EINVALIDSPEC(void)
523 {
524 cl_git_fail_with(git_clone(&g_repo, "http://github.com", "./foo", &g_options),
525 GIT_EINVALIDSPEC);
526 }
527
528 static int fail_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
529 {
530 GIT_UNUSED(cert);
531 GIT_UNUSED(valid);
532 GIT_UNUSED(host);
533 GIT_UNUSED(payload);
534
535 return GIT_ECERTIFICATE;
536 }
537
538 void test_online_clone__certificate_invalid(void)
539 {
540 g_options.remote_callbacks.certificate_check = fail_certificate_check;
541
542 cl_git_fail_with(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options),
543 GIT_ECERTIFICATE);
544
545 #ifdef GIT_SSH
546 cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options),
547 GIT_ECERTIFICATE);
548 #endif
549 }
550
551 static int succeed_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
552 {
553 GIT_UNUSED(cert);
554 GIT_UNUSED(valid);
555 GIT_UNUSED(payload);
556
557 cl_assert_equal_s("github.com", host);
558
559 return 0;
560 }
561
562 void test_online_clone__certificate_valid(void)
563 {
564 g_options.remote_callbacks.certificate_check = succeed_certificate_check;
565
566 cl_git_pass(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options));
567 }