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"
15 #define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"
17 static git_repository
*g_repo
;
18 static git_clone_options g_options
;
20 static char *_remote_url
= NULL
;
21 static char *_remote_user
= NULL
;
22 static char *_remote_pass
= NULL
;
23 static char *_remote_sslnoverify
= NULL
;
24 static char *_remote_ssh_pubkey
= NULL
;
25 static char *_remote_ssh_privkey
= NULL
;
26 static char *_remote_ssh_passphrase
= NULL
;
27 static char *_remote_ssh_fingerprint
= NULL
;
28 static char *_remote_proxy_scheme
= NULL
;
29 static char *_remote_proxy_host
= NULL
;
30 static char *_remote_proxy_user
= NULL
;
31 static char *_remote_proxy_pass
= NULL
;
32 static char *_remote_proxy_selfsigned
= NULL
;
33 static char *_remote_expectcontinue
= NULL
;
35 static int _orig_proxies_need_reset
= 0;
36 static char *_orig_http_proxy
= NULL
;
37 static char *_orig_https_proxy
= NULL
;
39 static int ssl_cert(git_cert
*cert
, int valid
, const char *host
, void *payload
)
45 if (_remote_sslnoverify
!= NULL
)
48 return valid
? 0 : GIT_ECERTIFICATE
;
51 void test_online_clone__initialize(void)
53 git_checkout_options dummy_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
54 git_fetch_options dummy_fetch
= GIT_FETCH_OPTIONS_INIT
;
58 memset(&g_options
, 0, sizeof(git_clone_options
));
59 g_options
.version
= GIT_CLONE_OPTIONS_VERSION
;
60 g_options
.checkout_opts
= dummy_opts
;
61 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
62 g_options
.fetch_opts
= dummy_fetch
;
63 g_options
.fetch_opts
.callbacks
.certificate_check
= ssl_cert
;
65 _remote_url
= cl_getenv("GITTEST_REMOTE_URL");
66 _remote_user
= cl_getenv("GITTEST_REMOTE_USER");
67 _remote_pass
= cl_getenv("GITTEST_REMOTE_PASS");
68 _remote_sslnoverify
= cl_getenv("GITTEST_REMOTE_SSL_NOVERIFY");
69 _remote_ssh_pubkey
= cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
70 _remote_ssh_privkey
= cl_getenv("GITTEST_REMOTE_SSH_KEY");
71 _remote_ssh_passphrase
= cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
72 _remote_ssh_fingerprint
= cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
73 _remote_proxy_scheme
= cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
74 _remote_proxy_host
= cl_getenv("GITTEST_REMOTE_PROXY_HOST");
75 _remote_proxy_user
= cl_getenv("GITTEST_REMOTE_PROXY_USER");
76 _remote_proxy_pass
= cl_getenv("GITTEST_REMOTE_PROXY_PASS");
77 _remote_proxy_selfsigned
= cl_getenv("GITTEST_REMOTE_PROXY_SELFSIGNED");
78 _remote_expectcontinue
= cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
80 if (_remote_expectcontinue
)
81 git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE
, 1);
83 _orig_proxies_need_reset
= 0;
86 void test_online_clone__cleanup(void)
89 git_repository_free(g_repo
);
92 cl_fixture_cleanup("./foo");
94 git__free(_remote_url
);
95 git__free(_remote_user
);
96 git__free(_remote_pass
);
97 git__free(_remote_sslnoverify
);
98 git__free(_remote_ssh_pubkey
);
99 git__free(_remote_ssh_privkey
);
100 git__free(_remote_ssh_passphrase
);
101 git__free(_remote_ssh_fingerprint
);
102 git__free(_remote_proxy_scheme
);
103 git__free(_remote_proxy_host
);
104 git__free(_remote_proxy_user
);
105 git__free(_remote_proxy_pass
);
106 git__free(_remote_proxy_selfsigned
);
107 git__free(_remote_expectcontinue
);
109 if (_orig_proxies_need_reset
) {
110 cl_setenv("HTTP_PROXY", _orig_http_proxy
);
111 cl_setenv("HTTPS_PROXY", _orig_https_proxy
);
113 git__free(_orig_http_proxy
);
114 git__free(_orig_https_proxy
);
118 void test_online_clone__network_full(void)
122 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
123 cl_assert(!git_repository_is_bare(g_repo
));
124 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
126 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, origin
->download_tags
);
128 git_remote_free(origin
);
131 void test_online_clone__network_bare(void)
135 g_options
.bare
= true;
137 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
138 cl_assert(git_repository_is_bare(g_repo
));
139 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
141 git_remote_free(origin
);
144 void test_online_clone__empty_repository(void)
148 cl_git_pass(git_clone(&g_repo
, LIVE_EMPTYREPO_URL
, "./foo", &g_options
));
150 cl_assert_equal_i(true, git_repository_is_empty(g_repo
));
151 cl_assert_equal_i(true, git_repository_head_unborn(g_repo
));
153 cl_git_pass(git_reference_lookup(&head
, g_repo
, GIT_HEAD_FILE
));
154 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
155 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
157 git_reference_free(head
);
160 static void checkout_progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
162 bool *was_called
= (bool*)payload
;
163 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
164 (*was_called
) = true;
167 static int fetch_progress(const git_indexer_progress
*stats
, void *payload
)
169 bool *was_called
= (bool*)payload
;
171 (*was_called
) = true;
175 void test_online_clone__can_checkout_a_cloned_repo(void)
177 git_buf path
= GIT_BUF_INIT
;
179 bool checkout_progress_cb_was_called
= false,
180 fetch_progress_cb_was_called
= false;
182 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
183 g_options
.checkout_opts
.progress_cb
= &checkout_progress
;
184 g_options
.checkout_opts
.progress_payload
= &checkout_progress_cb_was_called
;
185 g_options
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
186 g_options
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
188 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
190 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "master.txt"));
191 cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path
)));
193 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
194 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
195 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
197 cl_assert_equal_i(true, checkout_progress_cb_was_called
);
198 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
200 git_reference_free(head
);
201 git_buf_dispose(&path
);
204 static int remote_mirror_cb(git_remote
**out
, git_repository
*repo
,
205 const char *name
, const char *url
, void *payload
)
212 if ((error
= git_remote_create_with_fetchspec(&remote
, repo
, name
, url
, "+refs/*:refs/*")) < 0)
219 void test_online_clone__clone_mirror(void)
221 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
224 bool fetch_progress_cb_was_called
= false;
226 opts
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
227 opts
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
230 opts
.remote_cb
= remote_mirror_cb
;
232 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo.git", &opts
));
234 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
235 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
236 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
238 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
240 git_reference_free(head
);
241 git_repository_free(g_repo
);
244 cl_fixture_cleanup("./foo.git");
247 static int update_tips(const char *refname
, const git_oid
*a
, const git_oid
*b
, void *payload
)
249 int *callcount
= (int*)payload
;
250 GIT_UNUSED(refname
); GIT_UNUSED(a
); GIT_UNUSED(b
);
251 *callcount
= *callcount
+ 1;
255 void test_online_clone__custom_remote_callbacks(void)
259 g_options
.fetch_opts
.callbacks
.update_tips
= update_tips
;
260 g_options
.fetch_opts
.callbacks
.payload
= &callcount
;
262 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
263 cl_assert(callcount
> 0);
266 void test_online_clone__custom_headers(void)
268 char *empty_header
= "";
269 char *unnamed_header
= "this is a header about nothing";
270 char *newlines
= "X-Custom: almost OK\n";
271 char *conflict
= "Accept: defined-by-git";
272 char *ok
= "X-Custom: this should be ok";
274 g_options
.fetch_opts
.custom_headers
.count
= 1;
276 g_options
.fetch_opts
.custom_headers
.strings
= &empty_header
;
277 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
279 g_options
.fetch_opts
.custom_headers
.strings
= &unnamed_header
;
280 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
282 g_options
.fetch_opts
.custom_headers
.strings
= &newlines
;
283 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
285 g_options
.fetch_opts
.custom_headers
.strings
= &conflict
;
286 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
288 /* Finally, we got it right! */
289 g_options
.fetch_opts
.custom_headers
.strings
= &ok
;
290 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
293 static int cred_failure_cb(
294 git_credential
**cred
,
296 const char *username_from_url
,
297 unsigned int allowed_types
,
300 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
);
301 GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
305 void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
307 git__free(_remote_url
);
308 git__free(_remote_user
);
310 _remote_url
= git__strdup("https://github.com/libgit2/non-existent");
311 _remote_user
= git__strdup("libgit2test");
313 g_options
.fetch_opts
.callbacks
.credentials
= cred_failure_cb
;
315 cl_git_fail_with(-172, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
318 static int cred_count_calls_cb(git_credential
**cred
, const char *url
, const char *user
,
319 unsigned int allowed_types
, void *data
)
321 size_t *counter
= (size_t *) data
;
323 GIT_UNUSED(url
); GIT_UNUSED(user
); GIT_UNUSED(allowed_types
);
325 if (allowed_types
== GIT_CREDENTIAL_USERNAME
)
326 return git_credential_username_new(cred
, "foo");
333 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
336 void test_online_clone__cred_callback_called_again_on_auth_failure(void)
340 git__free(_remote_url
);
341 git__free(_remote_user
);
343 _remote_url
= git__strdup("https://gitlab.com/libgit2/non-existent");
344 _remote_user
= git__strdup("libgit2test");
346 g_options
.fetch_opts
.callbacks
.credentials
= cred_count_calls_cb
;
347 g_options
.fetch_opts
.callbacks
.payload
= &counter
;
349 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
350 cl_assert_equal_i(3, counter
);
354 git_credential
**cred
,
356 const char *user_from_url
,
357 unsigned int allowed_types
,
361 GIT_UNUSED(user_from_url
);
364 if (!(allowed_types
& GIT_CREDENTIAL_DEFAULT
))
367 return git_credential_default_new(cred
);
370 void test_online_clone__credentials(void)
372 /* Remote URL environment variable must be set.
373 * User and password are optional.
375 git_credential_userpass_payload user_pass
= {
383 if (cl_is_env_set("GITTEST_REMOTE_DEFAULT")) {
384 g_options
.fetch_opts
.callbacks
.credentials
= cred_default
;
386 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
387 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
390 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
391 git_repository_free(g_repo
); g_repo
= NULL
;
392 cl_fixture_cleanup("./foo");
395 void test_online_clone__credentials_via_custom_headers(void)
397 const char *creds
= "libgit3:libgit3";
398 git_buf auth
= GIT_BUF_INIT
;
400 cl_git_pass(git_buf_puts(&auth
, "Authorization: Basic "));
401 cl_git_pass(git_buf_encode_base64(&auth
, creds
, strlen(creds
)));
402 g_options
.fetch_opts
.custom_headers
.count
= 1;
403 g_options
.fetch_opts
.custom_headers
.strings
= &auth
.ptr
;
405 cl_git_pass(git_clone(&g_repo
, "https://bitbucket.org/libgit2/testgitrepository.git", "./foo", &g_options
));
407 git_buf_dispose(&auth
);
410 void test_online_clone__bitbucket_style(void)
412 git_credential_userpass_payload user_pass
= {
416 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
417 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
419 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL
, "./foo", &g_options
));
420 git_repository_free(g_repo
); g_repo
= NULL
;
421 cl_fixture_cleanup("./foo");
424 void test_online_clone__bitbucket_uses_creds_in_url(void)
426 git_credential_userpass_payload user_pass
= {
430 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
431 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
434 * Correct user and pass are in the URL; the (incorrect) creds in
435 * the `git_credential_userpass_payload` should be ignored.
437 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_PASS
, "./foo", &g_options
));
438 git_repository_free(g_repo
); g_repo
= NULL
;
439 cl_fixture_cleanup("./foo");
442 void test_online_clone__bitbucket_falls_back_to_specified_creds(void)
444 git_credential_userpass_payload user_pass
= {
448 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
449 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
452 * TODO: as of March 2018, bitbucket sporadically fails with
453 * 403s instead of replying with a 401 - but only sometimes.
458 * Incorrect user and pass are in the URL; the (correct) creds in
459 * the `git_credential_userpass_payload` should be used as a fallback.
461 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_WRONG_PASS
, "./foo", &g_options
));
462 git_repository_free(g_repo
); g_repo
= NULL
;
463 cl_fixture_cleanup("./foo");
466 static int cancel_at_half(const git_indexer_progress
*stats
, void *payload
)
470 if (stats
->received_objects
> (stats
->total_objects
/2))
475 void test_online_clone__can_cancel(void)
477 g_options
.fetch_opts
.callbacks
.transfer_progress
= cancel_at_half
;
479 cl_git_fail_with(4321,
480 git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
483 static int cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
484 unsigned int allowed_types
, void *payload
)
486 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
488 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
489 return git_credential_username_new(cred
, _remote_user
);
491 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
492 return git_credential_ssh_key_new(cred
,
493 _remote_user
, _remote_ssh_pubkey
,
494 _remote_ssh_privkey
, _remote_ssh_passphrase
);
496 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
500 static int check_ssh_auth_methods(git_credential
**cred
, const char *url
, const char *username_from_url
,
501 unsigned int allowed_types
, void *data
)
503 int *with_user
= (int *) data
;
504 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(data
);
507 cl_assert_equal_i(GIT_CREDENTIAL_USERNAME
, allowed_types
);
509 cl_assert(!(allowed_types
& GIT_CREDENTIAL_USERNAME
));
514 void test_online_clone__ssh_auth_methods(void)
521 g_options
.fetch_opts
.callbacks
.credentials
= check_ssh_auth_methods
;
522 g_options
.fetch_opts
.callbacks
.payload
= &with_user
;
523 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
526 cl_git_fail_with(GIT_EUSER
,
527 git_clone(&g_repo
, SSH_REPO_URL
, "./foo", &g_options
));
530 cl_git_fail_with(GIT_EUSER
,
531 git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
534 static int custom_remote_ssh_with_paths(
536 git_repository
*repo
,
545 if ((error
= git_remote_create(out
, repo
, name
, url
)) < 0)
551 void test_online_clone__ssh_with_paths(void)
553 char *bad_paths
[] = {
557 char *good_paths
[] = {
558 "/usr/bin/git-upload-pack",
559 "/usr/bin/git-receive-pack",
569 if (!_remote_url
|| !_remote_user
|| strncmp(_remote_url
, "ssh://", 5) != 0)
572 g_options
.remote_cb
= custom_remote_ssh_with_paths
;
573 g_options
.fetch_opts
.callbacks
.transport
= git_transport_ssh_with_paths
;
574 g_options
.fetch_opts
.callbacks
.credentials
= cred_cb
;
575 g_options
.fetch_opts
.callbacks
.payload
= &arr
;
576 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
578 cl_git_fail(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
580 arr
.strings
= good_paths
;
581 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
584 static int cred_foo_bar(git_credential
**cred
, const char *url
, const char *username_from_url
,
585 unsigned int allowed_types
, void *data
)
588 GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
590 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
593 void test_online_clone__ssh_cannot_change_username(void)
598 g_options
.fetch_opts
.callbacks
.credentials
= cred_foo_bar
;
600 cl_git_fail(git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
603 int ssh_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
605 git_cert_hostkey
*key
;
606 git_oid expected
= {{0}}, actual
= {{0}};
611 cl_assert(_remote_ssh_fingerprint
);
613 cl_git_pass(git_oid_fromstrp(&expected
, _remote_ssh_fingerprint
));
614 cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2
, cert
->cert_type
);
615 key
= (git_cert_hostkey
*) cert
;
618 * We need to figure out how long our input was to check for
619 * the type. Here we abuse the fact that both hashes fit into
622 if (strlen(_remote_ssh_fingerprint
) == 32 && key
->type
& GIT_CERT_SSH_MD5
) {
623 memcpy(&actual
.id
, key
->hash_md5
, 16);
624 } else if (strlen(_remote_ssh_fingerprint
) == 40 && key
->type
& GIT_CERT_SSH_SHA1
) {
625 memcpy(&actual
, key
->hash_sha1
, 20);
627 cl_fail("Cannot find a usable SSH hash");
630 cl_assert(!memcmp(&expected
, &actual
, 20));
632 cl_assert_equal_s("localhost", host
);
637 void test_online_clone__ssh_cert(void)
639 g_options
.fetch_opts
.callbacks
.certificate_check
= ssh_certificate_check
;
641 if (!_remote_ssh_fingerprint
)
644 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
647 static char *read_key_file(const char *path
)
656 cl_assert((f
= fopen(path
, "r")) != NULL
);
657 cl_assert(fseek(f
, 0, SEEK_END
) != -1);
658 cl_assert((key_length
= ftell(f
)) != -1);
659 cl_assert(fseek(f
, 0, SEEK_SET
) != -1);
660 cl_assert((buf
= malloc(key_length
)) != NULL
);
661 cl_assert(fread(buf
, key_length
, 1, f
) == 1);
667 static int ssh_memory_cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
668 unsigned int allowed_types
, void *payload
)
670 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
672 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
673 return git_credential_username_new(cred
, _remote_user
);
675 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
677 char *pubkey
= read_key_file(_remote_ssh_pubkey
);
678 char *privkey
= read_key_file(_remote_ssh_privkey
);
680 int ret
= git_credential_ssh_key_memory_new(cred
, _remote_user
, pubkey
, privkey
, _remote_ssh_passphrase
);
689 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
693 void test_online_clone__ssh_memory_auth(void)
695 #ifndef GIT_SSH_MEMORY_CREDENTIALS
698 if (!_remote_url
|| !_remote_user
|| !_remote_ssh_privkey
|| strncmp(_remote_url
, "ssh://", 5) != 0)
701 g_options
.fetch_opts
.callbacks
.credentials
= ssh_memory_cred_cb
;
703 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
706 static int fail_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
713 return GIT_ECERTIFICATE
;
716 void test_online_clone__certificate_invalid(void)
718 g_options
.fetch_opts
.callbacks
.certificate_check
= fail_certificate_check
;
720 cl_git_fail_with(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
724 cl_git_fail_with(git_clone(&g_repo
, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
729 static int succeed_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
735 cl_assert_equal_s("github.com", host
);
740 void test_online_clone__certificate_valid(void)
742 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
744 cl_git_pass(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
747 void test_online_clone__start_with_http(void)
749 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
751 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
754 static int called_proxy_creds
;
755 static int proxy_cred_cb(git_credential
**out
, const char *url
, const char *username
, unsigned int allowed
, void *payload
)
758 GIT_UNUSED(username
);
762 called_proxy_creds
= 1;
763 return git_credential_userpass_plaintext_new(out
, _remote_proxy_user
, _remote_proxy_pass
);
766 static int proxy_cert_cb(git_cert
*cert
, int valid
, const char *host
, void *payload
)
775 cl_assert(_remote_proxy_host
);
777 if ((colon
= strchr(_remote_proxy_host
, ':')) != NULL
)
778 host_len
= (colon
- _remote_proxy_host
);
780 host_len
= strlen(_remote_proxy_host
);
782 if (_remote_proxy_selfsigned
!= NULL
&&
783 strlen(host
) == host_len
&&
784 strncmp(_remote_proxy_host
, host
, host_len
) == 0)
787 return valid
? 0 : GIT_ECERTIFICATE
;
790 void test_online_clone__proxy_credentials_request(void)
792 git_buf url
= GIT_BUF_INIT
;
794 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
797 cl_git_pass(git_buf_printf(&url
, "%s://%s/",
798 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
799 _remote_proxy_host
));
801 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
802 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
803 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
804 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
805 called_proxy_creds
= 0;
806 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
807 cl_assert(called_proxy_creds
);
809 git_buf_dispose(&url
);
812 void test_online_clone__proxy_credentials_in_url(void)
814 git_buf url
= GIT_BUF_INIT
;
816 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
819 cl_git_pass(git_buf_printf(&url
, "%s://%s:%s@%s/",
820 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
821 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
823 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
824 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
825 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
826 called_proxy_creds
= 0;
827 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
828 cl_assert(called_proxy_creds
== 0);
830 git_buf_dispose(&url
);
833 void test_online_clone__proxy_credentials_in_environment(void)
835 git_buf url
= GIT_BUF_INIT
;
837 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
840 _orig_http_proxy
= cl_getenv("HTTP_PROXY");
841 _orig_https_proxy
= cl_getenv("HTTPS_PROXY");
842 _orig_proxies_need_reset
= 1;
844 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
845 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
847 cl_git_pass(git_buf_printf(&url
, "%s://%s:%s@%s/",
848 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
849 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
851 cl_setenv("HTTP_PROXY", url
.ptr
);
852 cl_setenv("HTTPS_PROXY", url
.ptr
);
854 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
856 git_buf_dispose(&url
);
859 void test_online_clone__proxy_auto_not_detected(void)
861 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
863 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
866 void test_online_clone__proxy_cred_callback_after_failed_url_creds(void)
868 git_buf url
= GIT_BUF_INIT
;
870 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
873 cl_git_pass(git_buf_printf(&url
, "%s://invalid_user_name:INVALID_pass_WORD@%s/",
874 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
875 _remote_proxy_host
));
877 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
878 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
879 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
880 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
881 called_proxy_creds
= 0;
882 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
883 cl_assert(called_proxy_creds
);
885 git_buf_dispose(&url
);
888 void test_online_clone__azurerepos(void)
890 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/test", "./foo", &g_options
));
891 cl_assert(git_path_exists("./foo/master.txt"));
894 void test_online_clone__path_whitespace(void)
896 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/spaces%20in%20the%20name", "./foo", &g_options
));
897 cl_assert(git_path_exists("./foo/master.txt"));