]> git.proxmox.com Git - libgit2.git/blob - tests/online/clone.c
Merge pull request #3165 from ethomson/downcase
[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_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_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.fetch_opts = dummy_fetch;
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_lookup(&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_lookup(&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;
108 g_options.checkout_opts.progress_cb = &checkout_progress;
109 g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
110 g_options.fetch_opts.callbacks.transfer_progress = &fetch_progress;
111 g_options.fetch_opts.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
135 GIT_UNUSED(payload);
136
137 if ((error = git_remote_create_with_fetchspec(&remote, repo, name, url, "+refs/*:refs/*")) < 0)
138 return error;
139
140 *out = remote;
141 return 0;
142 }
143
144 void test_online_clone__clone_mirror(void)
145 {
146 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
147 git_reference *head;
148
149 bool fetch_progress_cb_was_called = false;
150
151 opts.fetch_opts.callbacks.transfer_progress = &fetch_progress;
152 opts.fetch_opts.callbacks.payload = &fetch_progress_cb_was_called;
153
154 opts.bare = true;
155 opts.remote_cb = remote_mirror_cb;
156
157 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
158
159 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
160 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
161 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
162
163 cl_assert_equal_i(true, fetch_progress_cb_was_called);
164
165 git_reference_free(head);
166 git_repository_free(g_repo);
167 g_repo = NULL;
168
169 cl_fixture_cleanup("./foo.git");
170 }
171
172 static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *payload)
173 {
174 int *callcount = (int*)payload;
175 GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b);
176 *callcount = *callcount + 1;
177 return 0;
178 }
179
180 void test_online_clone__custom_remote_callbacks(void)
181 {
182 int callcount = 0;
183
184 g_options.fetch_opts.callbacks.update_tips = update_tips;
185 g_options.fetch_opts.callbacks.payload = &callcount;
186
187 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
188 cl_assert(callcount > 0);
189 }
190
191 static int cred_failure_cb(
192 git_cred **cred,
193 const char *url,
194 const char *username_from_url,
195 unsigned int allowed_types,
196 void *data)
197 {
198 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
199 GIT_UNUSED(allowed_types); GIT_UNUSED(data);
200 return -172;
201 }
202
203 void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
204 {
205 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
206 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
207
208 if (!remote_url || !remote_user)
209 clar__skip();
210
211 g_options.fetch_opts.callbacks.credentials = cred_failure_cb;
212
213 cl_git_fail_with(-172, git_clone(&g_repo, remote_url, "./foo", &g_options));
214 }
215
216 static int cred_count_calls_cb(git_cred **cred, const char *url, const char *user,
217 unsigned int allowed_types, void *data)
218 {
219 size_t *counter = (size_t *) data;
220
221 GIT_UNUSED(url); GIT_UNUSED(user); GIT_UNUSED(allowed_types);
222
223 if (allowed_types == GIT_CREDTYPE_USERNAME)
224 return git_cred_username_new(cred, "foo");
225
226 (*counter)++;
227
228 if (*counter == 3)
229 return GIT_EUSER;
230
231 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
232 }
233
234 void test_online_clone__cred_callback_called_again_on_auth_failure(void)
235 {
236 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
237 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
238 size_t counter = 0;
239
240 if (!remote_url || !remote_user)
241 clar__skip();
242
243 g_options.fetch_opts.callbacks.credentials = cred_count_calls_cb;
244 g_options.fetch_opts.callbacks.payload = &counter;
245
246 cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, remote_url, "./foo", &g_options));
247 cl_assert_equal_i(3, counter);
248 }
249
250 int cred_default(
251 git_cred **cred,
252 const char *url,
253 const char *user_from_url,
254 unsigned int allowed_types,
255 void *payload)
256 {
257 GIT_UNUSED(url);
258 GIT_UNUSED(user_from_url);
259 GIT_UNUSED(payload);
260
261 if (!(allowed_types & GIT_CREDTYPE_DEFAULT))
262 return 0;
263
264 return git_cred_default_new(cred);
265 }
266
267 void test_online_clone__credentials(void)
268 {
269 /* Remote URL environment variable must be set.
270 * User and password are optional.
271 */
272 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
273 git_cred_userpass_payload user_pass = {
274 cl_getenv("GITTEST_REMOTE_USER"),
275 cl_getenv("GITTEST_REMOTE_PASS")
276 };
277
278 if (!remote_url) return;
279
280 if (cl_getenv("GITTEST_REMOTE_DEFAULT")) {
281 g_options.fetch_opts.callbacks.credentials = cred_default;
282 } else {
283 g_options.fetch_opts.callbacks.credentials = git_cred_userpass;
284 g_options.fetch_opts.callbacks.payload = &user_pass;
285 }
286
287 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
288 git_repository_free(g_repo); g_repo = NULL;
289 cl_fixture_cleanup("./foo");
290 }
291
292 void test_online_clone__bitbucket_style(void)
293 {
294 git_cred_userpass_payload user_pass = {
295 "libgit2", "libgit2"
296 };
297
298 g_options.fetch_opts.callbacks.credentials = git_cred_userpass;
299 g_options.fetch_opts.callbacks.payload = &user_pass;
300
301 cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
302 git_repository_free(g_repo); g_repo = NULL;
303 cl_fixture_cleanup("./foo");
304
305 /* User and pass from URL */
306 user_pass.password = "wrong";
307 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_PASS, "./foo", &g_options));
308 git_repository_free(g_repo); g_repo = NULL;
309 cl_fixture_cleanup("./foo");
310
311 /* Wrong password in URL, fall back to user_pass */
312 user_pass.password = "libgit2";
313 cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_WRONG_PASS, "./foo", &g_options));
314 git_repository_free(g_repo); g_repo = NULL;
315 cl_fixture_cleanup("./foo");
316 }
317
318 static int cancel_at_half(const git_transfer_progress *stats, void *payload)
319 {
320 GIT_UNUSED(payload);
321
322 if (stats->received_objects > (stats->total_objects/2))
323 return 4321;
324 return 0;
325 }
326
327 void test_online_clone__can_cancel(void)
328 {
329 g_options.fetch_opts.callbacks.transfer_progress = cancel_at_half;
330
331 cl_git_fail_with(
332 git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
333 }
334
335 static int cred_cb(git_cred **cred, const char *url, const char *user_from_url,
336 unsigned int allowed_types, void *payload)
337 {
338 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
339 const char *pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
340 const char *privkey = cl_getenv("GITTEST_REMOTE_SSH_KEY");
341 const char *passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
342
343 GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);
344
345 if (allowed_types & GIT_CREDTYPE_USERNAME)
346 return git_cred_username_new(cred, remote_user);
347
348 if (allowed_types & GIT_CREDTYPE_SSH_KEY)
349 return git_cred_ssh_key_new(cred, remote_user, pubkey, privkey, passphrase);
350
351 giterr_set(GITERR_NET, "unexpected cred type");
352 return -1;
353 }
354
355 static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
356 unsigned int allowed_types, void *data)
357 {
358 int *with_user = (int *) data;
359 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
360
361 if (!*with_user)
362 cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
363 else
364 cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
365
366 return GIT_EUSER;
367 }
368
369 void test_online_clone__ssh_auth_methods(void)
370 {
371 int with_user;
372
373 #ifndef GIT_SSH
374 clar__skip();
375 #endif
376 g_options.fetch_opts.callbacks.credentials = check_ssh_auth_methods;
377 g_options.fetch_opts.callbacks.payload = &with_user;
378
379 with_user = 0;
380 cl_git_fail_with(GIT_EUSER,
381 git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
382
383 with_user = 1;
384 cl_git_fail_with(GIT_EUSER,
385 git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
386 }
387
388 static int custom_remote_ssh_with_paths(
389 git_remote **out,
390 git_repository *repo,
391 const char *name,
392 const char *url,
393 void *payload)
394 {
395 int error;
396
397 GIT_UNUSED(payload);
398
399 if ((error = git_remote_create(out, repo, name, url)) < 0)
400 return error;
401
402 return 0;
403 }
404
405 void test_online_clone__ssh_with_paths(void)
406 {
407 char *bad_paths[] = {
408 "/bin/yes",
409 "/bin/false",
410 };
411 char *good_paths[] = {
412 "/usr/bin/git-upload-pack",
413 "/usr/bin/git-receive-pack",
414 };
415 git_strarray arr = {
416 bad_paths,
417 2,
418 };
419
420 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
421 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
422
423 #ifndef GIT_SSH
424 clar__skip();
425 #endif
426 if (!remote_url || !remote_user || strncmp(remote_url, "ssh://", 5) != 0)
427 clar__skip();
428
429 g_options.remote_cb = custom_remote_ssh_with_paths;
430 g_options.fetch_opts.callbacks.transport = git_transport_ssh_with_paths;
431 g_options.fetch_opts.callbacks.payload = &arr;
432
433 cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options));
434
435 arr.strings = good_paths;
436 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
437 }
438
439 static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url,
440 unsigned int allowed_types, void *data)
441
442 {
443 GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data);
444
445 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
446 }
447
448 void test_online_clone__ssh_cannot_change_username(void)
449 {
450 #ifndef GIT_SSH
451 clar__skip();
452 #endif
453 g_options.fetch_opts.callbacks.credentials = cred_foo_bar;
454
455 cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
456 }
457
458 int ssh_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
459 {
460 git_cert_hostkey *key;
461 git_oid expected = {{0}}, actual = {{0}};
462 const char *expected_str;
463
464 GIT_UNUSED(valid);
465 GIT_UNUSED(payload);
466
467 expected_str = cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
468 cl_assert(expected_str);
469
470 cl_git_pass(git_oid_fromstrp(&expected, expected_str));
471 cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2, cert->cert_type);
472 key = (git_cert_hostkey *) cert;
473
474 /*
475 * We need to figure out how long our input was to check for
476 * the type. Here we abuse the fact that both hashes fit into
477 * our git_oid type.
478 */
479 if (strlen(expected_str) == 32 && key->type & GIT_CERT_SSH_MD5) {
480 memcpy(&actual.id, key->hash_md5, 16);
481 } else if (strlen(expected_str) == 40 && key->type & GIT_CERT_SSH_SHA1) {
482 memcpy(&actual, key->hash_sha1, 20);
483 } else {
484 cl_fail("Cannot find a usable SSH hash");
485 }
486
487 cl_assert(!memcmp(&expected, &actual, 20));
488
489 cl_assert_equal_s("localhost", host);
490
491 return GIT_EUSER;
492 }
493
494 void test_online_clone__ssh_cert(void)
495 {
496 g_options.fetch_opts.callbacks.certificate_check = ssh_certificate_check;
497
498 if (!cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT"))
499 cl_skip();
500
501 cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options));
502 }
503
504 static char *read_key_file(const char *path)
505 {
506 FILE *f;
507 char *buf;
508 long key_length;
509
510 if (!path || !*path)
511 return NULL;
512
513 cl_assert((f = fopen(path, "r")) != NULL);
514 cl_assert(fseek(f, 0, SEEK_END) != -1);
515 cl_assert((key_length = ftell(f)) != -1);
516 cl_assert(fseek(f, 0, SEEK_SET) != -1);
517 cl_assert((buf = malloc(key_length)) != NULL);
518 cl_assert(fread(buf, key_length, 1, f) == 1);
519 fclose(f);
520
521 return buf;
522 }
523
524 static int ssh_memory_cred_cb(git_cred **cred, const char *url, const char *user_from_url,
525 unsigned int allowed_types, void *payload)
526 {
527 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
528 const char *pubkey_path = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
529 const char *privkey_path = cl_getenv("GITTEST_REMOTE_SSH_KEY");
530 const char *passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
531
532 GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);
533
534 if (allowed_types & GIT_CREDTYPE_USERNAME)
535 return git_cred_username_new(cred, remote_user);
536
537 if (allowed_types & GIT_CREDTYPE_SSH_KEY)
538 {
539 char *pubkey = read_key_file(pubkey_path);
540 char *privkey = read_key_file(privkey_path);
541
542 int ret = git_cred_ssh_key_memory_new(cred, remote_user, pubkey, privkey, passphrase);
543
544 if (privkey)
545 free(privkey);
546 if (pubkey)
547 free(pubkey);
548 return ret;
549 }
550
551 giterr_set(GITERR_NET, "unexpected cred type");
552 return -1;
553 }
554
555 void test_online_clone__ssh_memory_auth(void)
556 {
557 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
558 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
559 const char *privkey = cl_getenv("GITTEST_REMOTE_SSH_KEY");
560
561 #ifndef GIT_SSH_MEMORY_CREDENTIALS
562 clar__skip();
563 #endif
564 if (!remote_url || !remote_user || !privkey || strncmp(remote_url, "ssh://", 5) != 0)
565 clar__skip();
566
567 g_options.fetch_opts.callbacks.credentials = ssh_memory_cred_cb;
568
569 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
570 }
571
572 void test_online_clone__url_with_no_path_returns_EINVALIDSPEC(void)
573 {
574 cl_git_fail_with(git_clone(&g_repo, "http://github.com", "./foo", &g_options),
575 GIT_EINVALIDSPEC);
576 }
577
578 static int fail_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
579 {
580 GIT_UNUSED(cert);
581 GIT_UNUSED(valid);
582 GIT_UNUSED(host);
583 GIT_UNUSED(payload);
584
585 return GIT_ECERTIFICATE;
586 }
587
588 void test_online_clone__certificate_invalid(void)
589 {
590 g_options.fetch_opts.callbacks.certificate_check = fail_certificate_check;
591
592 cl_git_fail_with(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options),
593 GIT_ECERTIFICATE);
594
595 #ifdef GIT_SSH
596 cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options),
597 GIT_ECERTIFICATE);
598 #endif
599 }
600
601 static int succeed_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
602 {
603 GIT_UNUSED(cert);
604 GIT_UNUSED(valid);
605 GIT_UNUSED(payload);
606
607 cl_assert_equal_s("github.com", host);
608
609 return 0;
610 }
611
612 void test_online_clone__certificate_valid(void)
613 {
614 g_options.fetch_opts.callbacks.certificate_check = succeed_certificate_check;
615
616 cl_git_pass(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options));
617 }
618
619 void test_online_clone__start_with_http(void)
620 {
621 g_options.fetch_opts.callbacks.certificate_check = succeed_certificate_check;
622
623 cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
624 }