1 #include "clar_libgit2.h"
3 #include "git2/clone.h"
4 #include "git2/cred_helpers.h"
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 #define GOOGLESOURCE_REPO_URL "https://chromium.googlesource.com/external/github.com/sergi/go-diff"
16 #define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"
18 static git_repository
*g_repo
;
19 static git_clone_options g_options
;
21 static char *_remote_url
= NULL
;
22 static char *_remote_user
= NULL
;
23 static char *_remote_pass
= NULL
;
24 static char *_remote_sslnoverify
= NULL
;
25 static char *_remote_ssh_pubkey
= NULL
;
26 static char *_remote_ssh_privkey
= NULL
;
27 static char *_remote_ssh_passphrase
= NULL
;
28 static char *_remote_ssh_fingerprint
= NULL
;
29 static char *_remote_proxy_scheme
= NULL
;
30 static char *_remote_proxy_host
= NULL
;
31 static char *_remote_proxy_user
= NULL
;
32 static char *_remote_proxy_pass
= NULL
;
33 static char *_remote_proxy_selfsigned
= NULL
;
34 static char *_remote_expectcontinue
= NULL
;
36 static int _orig_proxies_need_reset
= 0;
37 static char *_orig_http_proxy
= NULL
;
38 static char *_orig_https_proxy
= NULL
;
40 static int ssl_cert(git_cert
*cert
, int valid
, const char *host
, void *payload
)
46 if (_remote_sslnoverify
!= NULL
)
49 return valid
? 0 : GIT_ECERTIFICATE
;
52 void test_online_clone__initialize(void)
54 git_checkout_options dummy_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
55 git_fetch_options dummy_fetch
= GIT_FETCH_OPTIONS_INIT
;
59 memset(&g_options
, 0, sizeof(git_clone_options
));
60 g_options
.version
= GIT_CLONE_OPTIONS_VERSION
;
61 g_options
.checkout_opts
= dummy_opts
;
62 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
63 g_options
.fetch_opts
= dummy_fetch
;
64 g_options
.fetch_opts
.callbacks
.certificate_check
= ssl_cert
;
66 _remote_url
= cl_getenv("GITTEST_REMOTE_URL");
67 _remote_user
= cl_getenv("GITTEST_REMOTE_USER");
68 _remote_pass
= cl_getenv("GITTEST_REMOTE_PASS");
69 _remote_sslnoverify
= cl_getenv("GITTEST_REMOTE_SSL_NOVERIFY");
70 _remote_ssh_pubkey
= cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
71 _remote_ssh_privkey
= cl_getenv("GITTEST_REMOTE_SSH_KEY");
72 _remote_ssh_passphrase
= cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
73 _remote_ssh_fingerprint
= cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
74 _remote_proxy_scheme
= cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
75 _remote_proxy_host
= cl_getenv("GITTEST_REMOTE_PROXY_HOST");
76 _remote_proxy_user
= cl_getenv("GITTEST_REMOTE_PROXY_USER");
77 _remote_proxy_pass
= cl_getenv("GITTEST_REMOTE_PROXY_PASS");
78 _remote_proxy_selfsigned
= cl_getenv("GITTEST_REMOTE_PROXY_SELFSIGNED");
79 _remote_expectcontinue
= cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
81 if (_remote_expectcontinue
)
82 git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE
, 1);
84 _orig_proxies_need_reset
= 0;
87 void test_online_clone__cleanup(void)
90 git_repository_free(g_repo
);
93 cl_fixture_cleanup("./foo");
95 git__free(_remote_url
);
96 git__free(_remote_user
);
97 git__free(_remote_pass
);
98 git__free(_remote_sslnoverify
);
99 git__free(_remote_ssh_pubkey
);
100 git__free(_remote_ssh_privkey
);
101 git__free(_remote_ssh_passphrase
);
102 git__free(_remote_ssh_fingerprint
);
103 git__free(_remote_proxy_scheme
);
104 git__free(_remote_proxy_host
);
105 git__free(_remote_proxy_user
);
106 git__free(_remote_proxy_pass
);
107 git__free(_remote_proxy_selfsigned
);
108 git__free(_remote_expectcontinue
);
110 if (_orig_proxies_need_reset
) {
111 cl_setenv("HTTP_PROXY", _orig_http_proxy
);
112 cl_setenv("HTTPS_PROXY", _orig_https_proxy
);
114 git__free(_orig_http_proxy
);
115 git__free(_orig_https_proxy
);
119 void test_online_clone__network_full(void)
123 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
124 cl_assert(!git_repository_is_bare(g_repo
));
125 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
127 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, origin
->download_tags
);
129 git_remote_free(origin
);
132 void test_online_clone__network_bare(void)
136 g_options
.bare
= true;
138 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
139 cl_assert(git_repository_is_bare(g_repo
));
140 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
142 git_remote_free(origin
);
145 void test_online_clone__empty_repository(void)
149 cl_git_pass(git_clone(&g_repo
, LIVE_EMPTYREPO_URL
, "./foo", &g_options
));
151 cl_assert_equal_i(true, git_repository_is_empty(g_repo
));
152 cl_assert_equal_i(true, git_repository_head_unborn(g_repo
));
154 cl_git_pass(git_reference_lookup(&head
, g_repo
, GIT_HEAD_FILE
));
155 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
156 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
158 git_reference_free(head
);
161 static void checkout_progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
163 bool *was_called
= (bool*)payload
;
164 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
165 (*was_called
) = true;
168 static int fetch_progress(const git_indexer_progress
*stats
, void *payload
)
170 bool *was_called
= (bool*)payload
;
172 (*was_called
) = true;
176 void test_online_clone__can_checkout_a_cloned_repo(void)
178 git_buf path
= GIT_BUF_INIT
;
179 git_reference
*head
, *remote_head
;
180 bool checkout_progress_cb_was_called
= false,
181 fetch_progress_cb_was_called
= false;
183 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
184 g_options
.checkout_opts
.progress_cb
= &checkout_progress
;
185 g_options
.checkout_opts
.progress_payload
= &checkout_progress_cb_was_called
;
186 g_options
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
187 g_options
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
189 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
191 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "master.txt"));
192 cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path
)));
194 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
195 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
196 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
198 cl_git_pass(git_reference_lookup(&remote_head
, g_repo
, "refs/remotes/origin/HEAD"));
199 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(remote_head
));
200 cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head
));
202 cl_assert_equal_i(true, checkout_progress_cb_was_called
);
203 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
205 git_reference_free(remote_head
);
206 git_reference_free(head
);
207 git_buf_dispose(&path
);
210 static int remote_mirror_cb(git_remote
**out
, git_repository
*repo
,
211 const char *name
, const char *url
, void *payload
)
218 if ((error
= git_remote_create_with_fetchspec(&remote
, repo
, name
, url
, "+refs/*:refs/*")) < 0)
225 void test_online_clone__clone_mirror(void)
227 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
230 bool fetch_progress_cb_was_called
= false;
232 opts
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
233 opts
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
236 opts
.remote_cb
= remote_mirror_cb
;
238 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo.git", &opts
));
240 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
241 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
242 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
244 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
246 git_reference_free(head
);
247 git_repository_free(g_repo
);
250 cl_fixture_cleanup("./foo.git");
253 static int update_tips(const char *refname
, const git_oid
*a
, const git_oid
*b
, void *payload
)
255 int *callcount
= (int*)payload
;
256 GIT_UNUSED(refname
); GIT_UNUSED(a
); GIT_UNUSED(b
);
257 *callcount
= *callcount
+ 1;
261 void test_online_clone__custom_remote_callbacks(void)
265 g_options
.fetch_opts
.callbacks
.update_tips
= update_tips
;
266 g_options
.fetch_opts
.callbacks
.payload
= &callcount
;
268 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
269 cl_assert(callcount
> 0);
272 void test_online_clone__custom_headers(void)
274 char *empty_header
= "";
275 char *unnamed_header
= "this is a header about nothing";
276 char *newlines
= "X-Custom: almost OK\n";
277 char *conflict
= "Accept: defined-by-git";
278 char *ok
= "X-Custom: this should be ok";
280 g_options
.fetch_opts
.custom_headers
.count
= 1;
282 g_options
.fetch_opts
.custom_headers
.strings
= &empty_header
;
283 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
285 g_options
.fetch_opts
.custom_headers
.strings
= &unnamed_header
;
286 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
288 g_options
.fetch_opts
.custom_headers
.strings
= &newlines
;
289 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
291 g_options
.fetch_opts
.custom_headers
.strings
= &conflict
;
292 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
294 /* Finally, we got it right! */
295 g_options
.fetch_opts
.custom_headers
.strings
= &ok
;
296 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
299 static int cred_failure_cb(
300 git_credential
**cred
,
302 const char *username_from_url
,
303 unsigned int allowed_types
,
306 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
);
307 GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
311 void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
313 git__free(_remote_url
);
314 git__free(_remote_user
);
316 _remote_url
= git__strdup("https://github.com/libgit2/non-existent");
317 _remote_user
= git__strdup("libgit2test");
319 g_options
.fetch_opts
.callbacks
.credentials
= cred_failure_cb
;
321 cl_git_fail_with(-172, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
324 static int cred_count_calls_cb(git_credential
**cred
, const char *url
, const char *user
,
325 unsigned int allowed_types
, void *data
)
327 size_t *counter
= (size_t *) data
;
329 GIT_UNUSED(url
); GIT_UNUSED(user
); GIT_UNUSED(allowed_types
);
331 if (allowed_types
== GIT_CREDENTIAL_USERNAME
)
332 return git_credential_username_new(cred
, "foo");
339 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
342 void test_online_clone__cred_callback_called_again_on_auth_failure(void)
346 git__free(_remote_url
);
347 git__free(_remote_user
);
349 _remote_url
= git__strdup("https://gitlab.com/libgit2/non-existent");
350 _remote_user
= git__strdup("libgit2test");
352 g_options
.fetch_opts
.callbacks
.credentials
= cred_count_calls_cb
;
353 g_options
.fetch_opts
.callbacks
.payload
= &counter
;
355 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
356 cl_assert_equal_i(3, counter
);
360 git_credential
**cred
,
362 const char *user_from_url
,
363 unsigned int allowed_types
,
367 GIT_UNUSED(user_from_url
);
370 if (!(allowed_types
& GIT_CREDENTIAL_DEFAULT
))
373 return git_credential_default_new(cred
);
376 void test_online_clone__credentials(void)
378 /* Remote URL environment variable must be set.
379 * User and password are optional.
381 git_credential_userpass_payload user_pass
= {
389 if (cl_is_env_set("GITTEST_REMOTE_DEFAULT")) {
390 g_options
.fetch_opts
.callbacks
.credentials
= cred_default
;
392 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
393 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
396 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
397 git_repository_free(g_repo
); g_repo
= NULL
;
398 cl_fixture_cleanup("./foo");
401 void test_online_clone__credentials_via_custom_headers(void)
403 const char *creds
= "libgit3:libgit3";
404 git_buf auth
= GIT_BUF_INIT
;
406 cl_git_pass(git_buf_puts(&auth
, "Authorization: Basic "));
407 cl_git_pass(git_buf_encode_base64(&auth
, creds
, strlen(creds
)));
408 g_options
.fetch_opts
.custom_headers
.count
= 1;
409 g_options
.fetch_opts
.custom_headers
.strings
= &auth
.ptr
;
411 cl_git_pass(git_clone(&g_repo
, "https://bitbucket.org/libgit2/testgitrepository.git", "./foo", &g_options
));
413 git_buf_dispose(&auth
);
416 void test_online_clone__bitbucket_style(void)
418 git_credential_userpass_payload user_pass
= {
422 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
423 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
425 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL
, "./foo", &g_options
));
426 git_repository_free(g_repo
); g_repo
= NULL
;
427 cl_fixture_cleanup("./foo");
430 void test_online_clone__bitbucket_uses_creds_in_url(void)
432 git_credential_userpass_payload user_pass
= {
436 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
437 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
440 * Correct user and pass are in the URL; the (incorrect) creds in
441 * the `git_credential_userpass_payload` should be ignored.
443 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_PASS
, "./foo", &g_options
));
444 git_repository_free(g_repo
); g_repo
= NULL
;
445 cl_fixture_cleanup("./foo");
448 void test_online_clone__bitbucket_falls_back_to_specified_creds(void)
450 git_credential_userpass_payload user_pass
= {
454 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
455 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
458 * TODO: as of March 2018, bitbucket sporadically fails with
459 * 403s instead of replying with a 401 - but only sometimes.
464 * Incorrect user and pass are in the URL; the (correct) creds in
465 * the `git_credential_userpass_payload` should be used as a fallback.
467 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_WRONG_PASS
, "./foo", &g_options
));
468 git_repository_free(g_repo
); g_repo
= NULL
;
469 cl_fixture_cleanup("./foo");
472 void test_online_clone__googlesource(void)
474 cl_git_pass(git_clone(&g_repo
, GOOGLESOURCE_REPO_URL
, "./foo", &g_options
));
475 git_repository_free(g_repo
); g_repo
= NULL
;
476 cl_fixture_cleanup("./foo");
479 static int cancel_at_half(const git_indexer_progress
*stats
, void *payload
)
483 if (stats
->received_objects
> (stats
->total_objects
/2))
488 void test_online_clone__can_cancel(void)
490 g_options
.fetch_opts
.callbacks
.transfer_progress
= cancel_at_half
;
492 cl_git_fail_with(4321,
493 git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
496 static int cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
497 unsigned int allowed_types
, void *payload
)
499 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
501 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
502 return git_credential_username_new(cred
, _remote_user
);
504 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
505 return git_credential_ssh_key_new(cred
,
506 _remote_user
, _remote_ssh_pubkey
,
507 _remote_ssh_privkey
, _remote_ssh_passphrase
);
509 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
513 static int check_ssh_auth_methods(git_credential
**cred
, const char *url
, const char *username_from_url
,
514 unsigned int allowed_types
, void *data
)
516 int *with_user
= (int *) data
;
517 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(data
);
520 cl_assert_equal_i(GIT_CREDENTIAL_USERNAME
, allowed_types
);
522 cl_assert(!(allowed_types
& GIT_CREDENTIAL_USERNAME
));
527 void test_online_clone__ssh_auth_methods(void)
534 g_options
.fetch_opts
.callbacks
.credentials
= check_ssh_auth_methods
;
535 g_options
.fetch_opts
.callbacks
.payload
= &with_user
;
536 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
539 cl_git_fail_with(GIT_EUSER
,
540 git_clone(&g_repo
, SSH_REPO_URL
, "./foo", &g_options
));
543 cl_git_fail_with(GIT_EUSER
,
544 git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
547 static int custom_remote_ssh_with_paths(
549 git_repository
*repo
,
558 if ((error
= git_remote_create(out
, repo
, name
, url
)) < 0)
564 void test_online_clone__ssh_with_paths(void)
566 char *bad_paths
[] = {
570 char *good_paths
[] = {
571 "/usr/bin/git-upload-pack",
572 "/usr/bin/git-receive-pack",
582 if (!_remote_url
|| !_remote_user
|| strncmp(_remote_url
, "ssh://", 5) != 0)
585 g_options
.remote_cb
= custom_remote_ssh_with_paths
;
586 g_options
.fetch_opts
.callbacks
.transport
= git_transport_ssh_with_paths
;
587 g_options
.fetch_opts
.callbacks
.credentials
= cred_cb
;
588 g_options
.fetch_opts
.callbacks
.payload
= &arr
;
589 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
591 cl_git_fail(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
593 arr
.strings
= good_paths
;
594 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
597 static int cred_foo_bar(git_credential
**cred
, const char *url
, const char *username_from_url
,
598 unsigned int allowed_types
, void *data
)
601 GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
603 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
606 void test_online_clone__ssh_cannot_change_username(void)
611 g_options
.fetch_opts
.callbacks
.credentials
= cred_foo_bar
;
613 cl_git_fail(git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
616 int ssh_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
618 git_cert_hostkey
*key
;
619 git_oid expected
= {{0}}, actual
= {{0}};
624 cl_assert(_remote_ssh_fingerprint
);
626 cl_git_pass(git_oid_fromstrp(&expected
, _remote_ssh_fingerprint
));
627 cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2
, cert
->cert_type
);
628 key
= (git_cert_hostkey
*) cert
;
631 * We need to figure out how long our input was to check for
632 * the type. Here we abuse the fact that both hashes fit into
635 if (strlen(_remote_ssh_fingerprint
) == 32 && key
->type
& GIT_CERT_SSH_MD5
) {
636 memcpy(&actual
.id
, key
->hash_md5
, 16);
637 } else if (strlen(_remote_ssh_fingerprint
) == 40 && key
->type
& GIT_CERT_SSH_SHA1
) {
638 memcpy(&actual
, key
->hash_sha1
, 20);
640 cl_fail("Cannot find a usable SSH hash");
643 cl_assert(!memcmp(&expected
, &actual
, 20));
645 cl_assert_equal_s("localhost", host
);
650 void test_online_clone__ssh_cert(void)
652 g_options
.fetch_opts
.callbacks
.certificate_check
= ssh_certificate_check
;
654 if (!_remote_ssh_fingerprint
)
657 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
660 static char *read_key_file(const char *path
)
669 cl_assert((f
= fopen(path
, "r")) != NULL
);
670 cl_assert(fseek(f
, 0, SEEK_END
) != -1);
671 cl_assert((key_length
= ftell(f
)) != -1);
672 cl_assert(fseek(f
, 0, SEEK_SET
) != -1);
673 cl_assert((buf
= malloc(key_length
)) != NULL
);
674 cl_assert(fread(buf
, key_length
, 1, f
) == 1);
680 static int ssh_memory_cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
681 unsigned int allowed_types
, void *payload
)
683 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
685 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
686 return git_credential_username_new(cred
, _remote_user
);
688 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
690 char *pubkey
= read_key_file(_remote_ssh_pubkey
);
691 char *privkey
= read_key_file(_remote_ssh_privkey
);
693 int ret
= git_credential_ssh_key_memory_new(cred
, _remote_user
, pubkey
, privkey
, _remote_ssh_passphrase
);
702 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
706 void test_online_clone__ssh_memory_auth(void)
708 #ifndef GIT_SSH_MEMORY_CREDENTIALS
711 if (!_remote_url
|| !_remote_user
|| !_remote_ssh_privkey
|| strncmp(_remote_url
, "ssh://", 5) != 0)
714 g_options
.fetch_opts
.callbacks
.credentials
= ssh_memory_cred_cb
;
716 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
719 static int fail_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
726 return GIT_ECERTIFICATE
;
729 void test_online_clone__certificate_invalid(void)
731 g_options
.fetch_opts
.callbacks
.certificate_check
= fail_certificate_check
;
733 cl_git_fail_with(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
737 cl_git_fail_with(git_clone(&g_repo
, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
742 static int succeed_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
748 cl_assert_equal_s("github.com", host
);
753 void test_online_clone__certificate_valid(void)
755 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
757 cl_git_pass(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
760 void test_online_clone__start_with_http(void)
762 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
764 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
767 static int called_proxy_creds
;
768 static int proxy_cred_cb(git_credential
**out
, const char *url
, const char *username
, unsigned int allowed
, void *payload
)
771 GIT_UNUSED(username
);
775 called_proxy_creds
= 1;
776 return git_credential_userpass_plaintext_new(out
, _remote_proxy_user
, _remote_proxy_pass
);
779 static int proxy_cert_cb(git_cert
*cert
, int valid
, const char *host
, void *payload
)
788 cl_assert(_remote_proxy_host
);
790 if ((colon
= strchr(_remote_proxy_host
, ':')) != NULL
)
791 host_len
= (colon
- _remote_proxy_host
);
793 host_len
= strlen(_remote_proxy_host
);
795 if (_remote_proxy_selfsigned
!= NULL
&&
796 strlen(host
) == host_len
&&
797 strncmp(_remote_proxy_host
, host
, host_len
) == 0)
800 return valid
? 0 : GIT_ECERTIFICATE
;
803 void test_online_clone__proxy_credentials_request(void)
805 git_buf url
= GIT_BUF_INIT
;
807 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
810 cl_git_pass(git_buf_printf(&url
, "%s://%s/",
811 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
812 _remote_proxy_host
));
814 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
815 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
816 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
817 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
818 called_proxy_creds
= 0;
819 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
820 cl_assert(called_proxy_creds
);
822 git_buf_dispose(&url
);
825 void test_online_clone__proxy_credentials_in_url(void)
827 git_buf url
= GIT_BUF_INIT
;
829 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
832 cl_git_pass(git_buf_printf(&url
, "%s://%s:%s@%s/",
833 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
834 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
836 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
837 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
838 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
839 called_proxy_creds
= 0;
840 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
841 cl_assert(called_proxy_creds
== 0);
843 git_buf_dispose(&url
);
846 void test_online_clone__proxy_credentials_in_environment(void)
848 git_buf url
= GIT_BUF_INIT
;
850 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
853 _orig_http_proxy
= cl_getenv("HTTP_PROXY");
854 _orig_https_proxy
= cl_getenv("HTTPS_PROXY");
855 _orig_proxies_need_reset
= 1;
857 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
858 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
860 cl_git_pass(git_buf_printf(&url
, "%s://%s:%s@%s/",
861 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
862 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
864 cl_setenv("HTTP_PROXY", url
.ptr
);
865 cl_setenv("HTTPS_PROXY", url
.ptr
);
867 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
869 git_buf_dispose(&url
);
872 void test_online_clone__proxy_auto_not_detected(void)
874 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
876 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
879 void test_online_clone__proxy_cred_callback_after_failed_url_creds(void)
881 git_buf url
= GIT_BUF_INIT
;
883 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
886 cl_git_pass(git_buf_printf(&url
, "%s://invalid_user_name:INVALID_pass_WORD@%s/",
887 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
888 _remote_proxy_host
));
890 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
891 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
892 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
893 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
894 called_proxy_creds
= 0;
895 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
896 cl_assert(called_proxy_creds
);
898 git_buf_dispose(&url
);
901 void test_online_clone__azurerepos(void)
903 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/test", "./foo", &g_options
));
904 cl_assert(git_path_exists("./foo/master.txt"));
907 void test_online_clone__path_whitespace(void)
909 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/spaces%20in%20the%20name", "./foo", &g_options
));
910 cl_assert(git_path_exists("./foo/master.txt"));