]> git.proxmox.com Git - libgit2.git/blame - tests/online/clone.c
ssh: do ssh cert info before asking for credentials
[libgit2.git] / tests / online / clone.c
CommitLineData
65415ea2
BS
1#include "clar_libgit2.h"
2
3#include "git2/clone.h"
520dcc1c 4#include "git2/cred_helpers.h"
6f748f38 5#include "remote.h"
114f5a6c
RB
6#include "fileops.h"
7#include "refs.h"
65415ea2 8
44f36f6e
BS
9#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
10#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
3382d8b1
CMN
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"
65415ea2 14
22618906
CMN
15#define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"
16
65415ea2 17static git_repository *g_repo;
18b2d560 18static git_clone_options g_options;
65415ea2 19
6443eaf2 20void test_online_clone__initialize(void)
65415ea2 21{
6affd71f 22 git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
0e0cf787 23 git_remote_callbacks dummy_callbacks = GIT_REMOTE_CALLBACKS_INIT;
730df6d0 24
65415ea2 25 g_repo = NULL;
18b2d560
BS
26
27 memset(&g_options, 0, sizeof(git_clone_options));
28 g_options.version = GIT_CLONE_OPTIONS_VERSION;
730df6d0
BS
29 g_options.checkout_opts = dummy_opts;
30 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
0e0cf787 31 g_options.remote_callbacks = dummy_callbacks;
65415ea2
BS
32}
33
6443eaf2 34void test_online_clone__cleanup(void)
65415ea2 35{
9094d30b 36 if (g_repo) {
65415ea2 37 git_repository_free(g_repo);
9094d30b
SC
38 g_repo = NULL;
39 }
7761ce21 40 cl_fixture_cleanup("./foo");
65415ea2
BS
41}
42
6443eaf2 43void test_online_clone__network_full(void)
65415ea2
BS
44{
45 git_remote *origin;
46
b412d563 47 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
65415ea2
BS
48 cl_assert(!git_repository_is_bare(g_repo));
49 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
add5efe7 50
6f748f38
JM
51 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);
52
add5efe7 53 git_remote_free(origin);
65415ea2
BS
54}
55
6443eaf2 56void test_online_clone__network_bare(void)
65415ea2
BS
57{
58 git_remote *origin;
59
18b2d560 60 g_options.bare = true;
65415ea2 61
b412d563 62 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
65415ea2
BS
63 cl_assert(git_repository_is_bare(g_repo));
64 cl_git_pass(git_remote_load(&origin, g_repo, "origin"));
add5efe7 65
66 git_remote_free(origin);
65415ea2
BS
67}
68
6443eaf2 69void test_online_clone__empty_repository(void)
65415ea2
BS
70{
71 git_reference *head;
72
b412d563 73 cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options));
65415ea2
BS
74
75 cl_assert_equal_i(true, git_repository_is_empty(g_repo));
605da51a 76 cl_assert_equal_i(true, git_repository_head_unborn(g_repo));
65415ea2
BS
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));
2508cc66 80 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
65415ea2
BS
81
82 git_reference_free(head);
83}
4d968f13 84
9c05c17b 85static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
183d8bdd 86{
183d8bdd 87 bool *was_called = (bool*)payload;
1fc375e6 88 GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
183d8bdd
BS
89 (*was_called) = true;
90}
91
fe95ac1b 92static int fetch_progress(const git_transfer_progress *stats, void *payload)
aa1e8674 93{
aa1e8674 94 bool *was_called = (bool*)payload;
1fc375e6 95 GIT_UNUSED(stats);
aa1e8674 96 (*was_called) = true;
fe95ac1b 97 return 0;
aa1e8674
BS
98}
99
6443eaf2 100void test_online_clone__can_checkout_a_cloned_repo(void)
4d968f13 101{
4d968f13 102 git_buf path = GIT_BUF_INIT;
c4f68b32 103 git_reference *head;
aa1e8674
BS
104 bool checkout_progress_cb_was_called = false,
105 fetch_progress_cb_was_called = false;
4d968f13 106
b3fb9237 107 g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
730df6d0
BS
108 g_options.checkout_opts.progress_cb = &checkout_progress;
109 g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
0e0cf787
CMN
110 g_options.remote_callbacks.transfer_progress = &fetch_progress;
111 g_options.remote_callbacks.payload = &fetch_progress_cb_was_called;
4d968f13 112
b412d563 113 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
4d968f13 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)));
c4f68b32 117
118 cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
119 cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
2508cc66 120 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
add5efe7 121
aa1e8674
BS
122 cl_assert_equal_i(true, checkout_progress_cb_was_called);
123 cl_assert_equal_i(true, fetch_progress_cb_was_called);
183d8bdd 124
add5efe7 125 git_reference_free(head);
126 git_buf_free(&path);
4d968f13 127}
621b50e4 128
6812afaf
CMN
129static int remote_mirror_cb(git_remote **out, git_repository *repo,
130 const char *name, const char *url, void *payload)
d19870d9 131{
6812afaf 132 int error;
d19870d9 133 git_remote *remote;
6812afaf 134 git_remote_callbacks *callbacks = (git_remote_callbacks *) payload;
d19870d9 135
d19870d9 136
6812afaf
CMN
137 if ((error = git_remote_create(&remote, repo, name, url)) < 0)
138 return error;
d19870d9 139
6812afaf
CMN
140 if ((error = git_remote_set_callbacks(remote, callbacks)) < 0) {
141 git_remote_free(remote);
142 return error;
143 }
d19870d9 144
6812afaf 145 git_remote_clear_refspecs(remote);
d19870d9 146
6812afaf
CMN
147 if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
148 git_remote_free(remote);
149 return error;
150 }
d19870d9 151
6812afaf
CMN
152 *out = remote;
153 return 0;
d19870d9
CMN
154}
155
b2067248
CMN
156void test_online_clone__clone_mirror(void)
157{
6812afaf 158 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
b2067248
CMN
159 git_reference *head;
160 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
161
162 bool fetch_progress_cb_was_called = false;
163
b2067248
CMN
164 callbacks.transfer_progress = &fetch_progress;
165 callbacks.payload = &fetch_progress_cb_was_called;
b2067248 166
6812afaf
CMN
167 opts.bare = true;
168 opts.remote_cb = remote_mirror_cb;
169 opts.remote_cb_payload = &callbacks;
b2067248 170
6812afaf 171 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
b2067248
CMN
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
b2067248 179 git_reference_free(head);
6d1b0438
PK
180 git_repository_free(g_repo);
181 g_repo = NULL;
182
b2067248
CMN
183 cl_fixture_cleanup("./foo.git");
184}
185
621b50e4
BS
186static 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
ffb02b16 194void test_online_clone__custom_remote_callbacks(void)
621b50e4 195{
621b50e4
BS
196 int callcount = 0;
197
0e0cf787
CMN
198 g_options.remote_callbacks.update_tips = update_tips;
199 g_options.remote_callbacks.payload = &callcount;
621b50e4
BS
200
201 cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
202 cl_assert(callcount > 0);
203}
204
80fc7d6b
ET
205static 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{
8f2a3d62
RB
212 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
213 GIT_UNUSED(allowed_types); GIT_UNUSED(data);
fe45922d 214 return -172;
80fc7d6b
ET
215}
216
fe45922d 217void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
80fc7d6b
ET
218{
219 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
5dae3ffe 220 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
80fc7d6b 221
0f65733b
VM
222 if (!remote_url || !remote_user)
223 clar__skip();
5dae3ffe 224
80fc7d6b
ET
225 g_options.remote_callbacks.credentials = cred_failure_cb;
226
b529c5f9 227 cl_git_fail_with(-172, git_clone(&g_repo, remote_url, "./foo", &g_options));
80fc7d6b
ET
228}
229
d7f962f4
CMN
230static 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
ccb85c8f
CMN
237 if (allowed_types == GIT_CREDTYPE_USERNAME)
238 return git_cred_username_new(cred, "foo");
239
d7f962f4
CMN
240 (*counter)++;
241
242 if (*counter == 3)
243 return GIT_EUSER;
244
245 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
246}
247
248void 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
adcdeb36
ET
264int 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
ffb02b16 281void test_online_clone__credentials(void)
621b50e4 282{
adcdeb36
ET
283 /* Remote URL environment variable must be set.
284 * User and password are optional.
285 */
621b50e4 286 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
520dcc1c 287 git_cred_userpass_payload user_pass = {
621b50e4
BS
288 cl_getenv("GITTEST_REMOTE_USER"),
289 cl_getenv("GITTEST_REMOTE_PASS")
290 };
291
292 if (!remote_url) return;
293
adcdeb36
ET
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 }
621b50e4
BS
300
301 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
5f10853e
BS
302 git_repository_free(g_repo); g_repo = NULL;
303 cl_fixture_cleanup("./foo");
304}
305
306void test_online_clone__bitbucket_style(void)
307{
308 git_cred_userpass_payload user_pass = {
309 "libgit2", "libgit2"
310 };
311
0e0cf787
CMN
312 g_options.remote_callbacks.credentials = git_cred_userpass;
313 g_options.remote_callbacks.payload = &user_pass;
5f10853e
BS
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");
cf7038a6 318
54ffc1f7
BS
319 /* User and pass from URL */
320 user_pass.password = "wrong";
cf7038a6
BS
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");
54ffc1f7
BS
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");
621b50e4 330}
fe95ac1b
BS
331
332static 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))
25e0b157 337 return 4321;
fe95ac1b
BS
338 return 0;
339}
340
341void test_online_clone__can_cancel(void)
342{
0e0cf787 343 g_options.remote_callbacks.transfer_progress = cancel_at_half;
d31402a3 344
25e0b157
RB
345 cl_git_fail_with(
346 git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
fe95ac1b 347}
d31402a3 348
d4256ed5
CMN
349static 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
c13b6844
CMN
359 if (allowed_types & GIT_CREDTYPE_USERNAME)
360 return git_cred_username_new(cred, remote_user);
361
d4256ed5
CMN
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}
d31402a3 368
22618906
CMN
369static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
370 unsigned int allowed_types, void *data)
371{
e26b08d3 372 int *with_user = (int *) data;
22618906 373 GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
d31402a3 374
e26b08d3
CMN
375 if (!*with_user)
376 cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
377 else
378 cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
d31402a3 379
22618906
CMN
380 return GIT_EUSER;
381}
d31402a3 382
22618906
CMN
383void test_online_clone__ssh_auth_methods(void)
384{
e26b08d3
CMN
385 int with_user;
386
22618906 387 g_options.remote_callbacks.credentials = check_ssh_auth_methods;
e26b08d3 388 g_options.remote_callbacks.payload = &with_user;
d31402a3 389
e26b08d3 390 with_user = 0;
22618906
CMN
391 cl_git_fail_with(GIT_EUSER,
392 git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
e26b08d3
CMN
393
394 with_user = 1;
395 cl_git_fail_with(GIT_EUSER,
396 git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
397}
398
d4256ed5
CMN
399static int custom_remote_ssh_with_paths(
400 git_remote **out,
401 git_repository *repo,
402 const char *name,
403 const char *url,
404 void *payload)
405{
406 int error;
d4256ed5
CMN
407 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
408
409 if ((error = git_remote_create(out, repo, name, url)) < 0)
410 return error;
411
412 if ((error = git_remote_set_transport(*out, git_transport_ssh_with_paths, payload)) < 0)
413 return error;
d31402a3 414
d4256ed5
CMN
415 callbacks.credentials = cred_cb;
416 git_remote_set_callbacks(*out, &callbacks);
d31402a3 417
d4256ed5
CMN
418 return 0;
419}
420
421void test_online_clone__ssh_with_paths(void)
422{
423 char *bad_paths[] = {
424 "/bin/yes",
425 "/bin/false",
426 };
427 char *good_paths[] = {
428 "/usr/bin/git-upload-pack",
429 "/usr/bin/git-receive-pack",
430 };
431 git_strarray arr = {
432 bad_paths,
433 2,
434 };
435
436 const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
437 const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
438
aea67633 439 if (!remote_url || !remote_user || strncmp(remote_url, "ssh://", 5) != 0)
d4256ed5
CMN
440 clar__skip();
441
442 g_options.remote_cb = custom_remote_ssh_with_paths;
443 g_options.remote_cb_payload = &arr;
d31402a3 444
d4256ed5 445 cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options));
d31402a3 446
d4256ed5
CMN
447 arr.strings = good_paths;
448 cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
449}
d31402a3 450
e26b08d3
CMN
451static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url,
452 unsigned int allowed_types, void *data)
453
454{
455 GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data);
456
457 return git_cred_userpass_plaintext_new(cred, "foo", "bar");
458}
459
460void test_online_clone__ssh_cannot_change_username(void)
461{
462 g_options.remote_callbacks.credentials = cred_foo_bar;
463
464 cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
22618906 465}
f0c53d21 466
467void test_online_clone__url_with_no_path_returns_EINVALIDSPEC(void)
468{
469 cl_git_fail_with(git_clone(&g_repo, "http://github.com", "./foo", &g_options),
470 GIT_EINVALIDSPEC);
471}
85acc562 472
17491f6e 473static int fail_certificate_check(git_cert_t type, void *data, size_t len, int valid, void *payload)
85acc562
CMN
474{
475 GIT_UNUSED(type);
476 GIT_UNUSED(data);
477 GIT_UNUSED(len);
17491f6e 478 GIT_UNUSED(valid);
85acc562
CMN
479 GIT_UNUSED(payload);
480
17491f6e 481 return 0;
85acc562
CMN
482}
483
484void test_online_clone__certificate_invalid(void)
485{
486 g_options.remote_callbacks.certificate_check = fail_certificate_check;
487
488 cl_git_fail_with(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options),
17491f6e 489 GIT_ECERTIFICATE);
2f5864c5
CMN
490
491 cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options),
492 GIT_ECERTIFICATE);
85acc562
CMN
493}
494
17491f6e 495static int succeed_certificate_check(git_cert_t type, void *data, size_t len, int valid, void *payload)
85acc562
CMN
496{
497 GIT_UNUSED(type);
498 GIT_UNUSED(data);
499 GIT_UNUSED(len);
17491f6e 500 GIT_UNUSED(valid);
85acc562
CMN
501 GIT_UNUSED(payload);
502
17491f6e 503 return 1;
85acc562
CMN
504}
505
506void test_online_clone__certificate_valid(void)
507{
508 g_options.remote_callbacks.certificate_check = succeed_certificate_check;
509
510 cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
511}