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://libgit2-test@bitbucket.org/libgit2-test/testgitrepository.git"
12 #define BB_REPO_URL_WITH_PASS "https://libgit2-test:YT77Ppm2nq8w4TYjGS8U@bitbucket.org/libgit2-test/testgitrepository.git"
13 #define BB_REPO_URL_WITH_WRONG_PASS "https://libgit2-test:wrong@bitbucket.org/libgit2-test/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
;
35 static char *_remote_redirect_initial
= NULL
;
36 static char *_remote_redirect_subsequent
= NULL
;
38 static int _orig_proxies_need_reset
= 0;
39 static char *_orig_http_proxy
= NULL
;
40 static char *_orig_https_proxy
= NULL
;
41 static char *_orig_no_proxy
= NULL
;
43 static int ssl_cert(git_cert
*cert
, int valid
, const char *host
, void *payload
)
49 if (_remote_sslnoverify
!= NULL
)
52 return valid
? 0 : GIT_ECERTIFICATE
;
55 void test_online_clone__initialize(void)
57 git_checkout_options dummy_opts
= GIT_CHECKOUT_OPTIONS_INIT
;
58 git_fetch_options dummy_fetch
= GIT_FETCH_OPTIONS_INIT
;
62 memset(&g_options
, 0, sizeof(git_clone_options
));
63 g_options
.version
= GIT_CLONE_OPTIONS_VERSION
;
64 g_options
.checkout_opts
= dummy_opts
;
65 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
66 g_options
.fetch_opts
= dummy_fetch
;
67 g_options
.fetch_opts
.callbacks
.certificate_check
= ssl_cert
;
69 _remote_url
= cl_getenv("GITTEST_REMOTE_URL");
70 _remote_user
= cl_getenv("GITTEST_REMOTE_USER");
71 _remote_pass
= cl_getenv("GITTEST_REMOTE_PASS");
72 _remote_sslnoverify
= cl_getenv("GITTEST_REMOTE_SSL_NOVERIFY");
73 _remote_ssh_pubkey
= cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
74 _remote_ssh_privkey
= cl_getenv("GITTEST_REMOTE_SSH_KEY");
75 _remote_ssh_passphrase
= cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
76 _remote_ssh_fingerprint
= cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
77 _remote_proxy_scheme
= cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
78 _remote_proxy_host
= cl_getenv("GITTEST_REMOTE_PROXY_HOST");
79 _remote_proxy_user
= cl_getenv("GITTEST_REMOTE_PROXY_USER");
80 _remote_proxy_pass
= cl_getenv("GITTEST_REMOTE_PROXY_PASS");
81 _remote_proxy_selfsigned
= cl_getenv("GITTEST_REMOTE_PROXY_SELFSIGNED");
82 _remote_expectcontinue
= cl_getenv("GITTEST_REMOTE_EXPECTCONTINUE");
83 _remote_redirect_initial
= cl_getenv("GITTEST_REMOTE_REDIRECT_INITIAL");
84 _remote_redirect_subsequent
= cl_getenv("GITTEST_REMOTE_REDIRECT_SUBSEQUENT");
86 if (_remote_expectcontinue
)
87 git_libgit2_opts(GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE
, 1);
89 _orig_proxies_need_reset
= 0;
92 void test_online_clone__cleanup(void)
95 git_repository_free(g_repo
);
98 cl_fixture_cleanup("./foo");
99 cl_fixture_cleanup("./initial");
100 cl_fixture_cleanup("./subsequent");
102 git__free(_remote_url
);
103 git__free(_remote_user
);
104 git__free(_remote_pass
);
105 git__free(_remote_sslnoverify
);
106 git__free(_remote_ssh_pubkey
);
107 git__free(_remote_ssh_privkey
);
108 git__free(_remote_ssh_passphrase
);
109 git__free(_remote_ssh_fingerprint
);
110 git__free(_remote_proxy_scheme
);
111 git__free(_remote_proxy_host
);
112 git__free(_remote_proxy_user
);
113 git__free(_remote_proxy_pass
);
114 git__free(_remote_proxy_selfsigned
);
115 git__free(_remote_expectcontinue
);
116 git__free(_remote_redirect_initial
);
117 git__free(_remote_redirect_subsequent
);
119 if (_orig_proxies_need_reset
) {
120 cl_setenv("HTTP_PROXY", _orig_http_proxy
);
121 cl_setenv("HTTPS_PROXY", _orig_https_proxy
);
122 cl_setenv("NO_PROXY", _orig_no_proxy
);
124 git__free(_orig_http_proxy
);
125 git__free(_orig_https_proxy
);
126 git__free(_orig_no_proxy
);
129 git_libgit2_opts(GIT_OPT_SET_SSL_CERT_LOCATIONS
, NULL
, NULL
);
132 void test_online_clone__network_full(void)
136 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
137 cl_assert(!git_repository_is_bare(g_repo
));
138 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
140 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO
, origin
->download_tags
);
142 git_remote_free(origin
);
145 void test_online_clone__network_bare(void)
149 g_options
.bare
= true;
151 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
152 cl_assert(git_repository_is_bare(g_repo
));
153 cl_git_pass(git_remote_lookup(&origin
, g_repo
, "origin"));
155 git_remote_free(origin
);
158 void test_online_clone__empty_repository(void)
162 cl_git_pass(git_clone(&g_repo
, LIVE_EMPTYREPO_URL
, "./foo", &g_options
));
164 cl_assert_equal_i(true, git_repository_is_empty(g_repo
));
165 cl_assert_equal_i(true, git_repository_head_unborn(g_repo
));
167 cl_git_pass(git_reference_lookup(&head
, g_repo
, GIT_HEAD_FILE
));
168 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
169 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
171 git_reference_free(head
);
174 static void checkout_progress(const char *path
, size_t cur
, size_t tot
, void *payload
)
176 bool *was_called
= (bool*)payload
;
177 GIT_UNUSED(path
); GIT_UNUSED(cur
); GIT_UNUSED(tot
);
178 (*was_called
) = true;
181 static int fetch_progress(const git_indexer_progress
*stats
, void *payload
)
183 bool *was_called
= (bool*)payload
;
185 (*was_called
) = true;
189 void test_online_clone__can_checkout_a_cloned_repo(void)
191 git_str path
= GIT_STR_INIT
;
192 git_reference
*head
, *remote_head
;
193 bool checkout_progress_cb_was_called
= false,
194 fetch_progress_cb_was_called
= false;
196 g_options
.checkout_opts
.checkout_strategy
= GIT_CHECKOUT_SAFE
;
197 g_options
.checkout_opts
.progress_cb
= &checkout_progress
;
198 g_options
.checkout_opts
.progress_payload
= &checkout_progress_cb_was_called
;
199 g_options
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
200 g_options
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
202 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
204 cl_git_pass(git_str_joinpath(&path
, git_repository_workdir(g_repo
), "master.txt"));
205 cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&path
)));
207 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
208 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
209 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
211 cl_git_pass(git_reference_lookup(&remote_head
, g_repo
, "refs/remotes/origin/HEAD"));
212 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(remote_head
));
213 cl_assert_equal_s("refs/remotes/origin/master", git_reference_symbolic_target(remote_head
));
215 cl_assert_equal_i(true, checkout_progress_cb_was_called
);
216 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
218 git_reference_free(remote_head
);
219 git_reference_free(head
);
220 git_str_dispose(&path
);
223 static int remote_mirror_cb(git_remote
**out
, git_repository
*repo
,
224 const char *name
, const char *url
, void *payload
)
231 if ((error
= git_remote_create_with_fetchspec(&remote
, repo
, name
, url
, "+refs/*:refs/*")) < 0)
238 void test_online_clone__clone_mirror(void)
240 git_clone_options opts
= GIT_CLONE_OPTIONS_INIT
;
243 bool fetch_progress_cb_was_called
= false;
245 opts
.fetch_opts
.callbacks
.transfer_progress
= &fetch_progress
;
246 opts
.fetch_opts
.callbacks
.payload
= &fetch_progress_cb_was_called
;
249 opts
.remote_cb
= remote_mirror_cb
;
251 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo.git", &opts
));
253 cl_git_pass(git_reference_lookup(&head
, g_repo
, "HEAD"));
254 cl_assert_equal_i(GIT_REFERENCE_SYMBOLIC
, git_reference_type(head
));
255 cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head
));
257 cl_assert_equal_i(true, fetch_progress_cb_was_called
);
259 git_reference_free(head
);
260 git_repository_free(g_repo
);
263 cl_fixture_cleanup("./foo.git");
266 static int update_tips(const char *refname
, const git_oid
*a
, const git_oid
*b
, void *payload
)
268 int *callcount
= (int*)payload
;
269 GIT_UNUSED(refname
); GIT_UNUSED(a
); GIT_UNUSED(b
);
270 *callcount
= *callcount
+ 1;
274 void test_online_clone__custom_remote_callbacks(void)
278 g_options
.fetch_opts
.callbacks
.update_tips
= update_tips
;
279 g_options
.fetch_opts
.callbacks
.payload
= &callcount
;
281 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
282 cl_assert(callcount
> 0);
285 void test_online_clone__custom_headers(void)
287 char *empty_header
= "";
288 char *unnamed_header
= "this is a header about nothing";
289 char *newlines
= "X-Custom: almost OK\n";
290 char *conflict
= "Accept: defined-by-git";
291 char *ok
= "X-Custom: this should be ok";
293 g_options
.fetch_opts
.custom_headers
.count
= 1;
295 g_options
.fetch_opts
.custom_headers
.strings
= &empty_header
;
296 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
298 g_options
.fetch_opts
.custom_headers
.strings
= &unnamed_header
;
299 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
301 g_options
.fetch_opts
.custom_headers
.strings
= &newlines
;
302 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
304 g_options
.fetch_opts
.custom_headers
.strings
= &conflict
;
305 cl_git_fail(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
307 /* Finally, we got it right! */
308 g_options
.fetch_opts
.custom_headers
.strings
= &ok
;
309 cl_git_pass(git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
312 static int cred_failure_cb(
313 git_credential
**cred
,
315 const char *username_from_url
,
316 unsigned int allowed_types
,
319 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
);
320 GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
324 void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
326 git__free(_remote_url
);
327 git__free(_remote_user
);
329 _remote_url
= git__strdup("https://github.com/libgit2/non-existent");
330 _remote_user
= git__strdup("libgit2test");
332 g_options
.fetch_opts
.callbacks
.credentials
= cred_failure_cb
;
334 cl_git_fail_with(-172, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
337 static int cred_count_calls_cb(git_credential
**cred
, const char *url
, const char *user
,
338 unsigned int allowed_types
, void *data
)
340 size_t *counter
= (size_t *) data
;
342 GIT_UNUSED(url
); GIT_UNUSED(user
); GIT_UNUSED(allowed_types
);
344 if (allowed_types
== GIT_CREDENTIAL_USERNAME
)
345 return git_credential_username_new(cred
, "foo");
352 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
355 void test_online_clone__cred_callback_called_again_on_auth_failure(void)
359 git__free(_remote_url
);
360 git__free(_remote_user
);
362 _remote_url
= git__strdup("https://gitlab.com/libgit2/non-existent");
363 _remote_user
= git__strdup("libgit2test");
365 g_options
.fetch_opts
.callbacks
.credentials
= cred_count_calls_cb
;
366 g_options
.fetch_opts
.callbacks
.payload
= &counter
;
368 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
369 cl_assert_equal_i(3, counter
);
372 static int cred_default(
373 git_credential
**cred
,
375 const char *user_from_url
,
376 unsigned int allowed_types
,
380 GIT_UNUSED(user_from_url
);
383 if (!(allowed_types
& GIT_CREDENTIAL_DEFAULT
))
386 return git_credential_default_new(cred
);
389 void test_online_clone__credentials(void)
391 /* Remote URL environment variable must be set.
392 * User and password are optional.
394 git_credential_userpass_payload user_pass
= {
402 if (cl_is_env_set("GITTEST_REMOTE_DEFAULT")) {
403 g_options
.fetch_opts
.callbacks
.credentials
= cred_default
;
405 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
406 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
409 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
410 git_repository_free(g_repo
); g_repo
= NULL
;
411 cl_fixture_cleanup("./foo");
414 void test_online_clone__credentials_via_custom_headers(void)
416 const char *creds
= "libgit2-test:YT77Ppm2nq8w4TYjGS8U";
417 git_str auth
= GIT_STR_INIT
;
419 cl_git_pass(git_str_puts(&auth
, "Authorization: Basic "));
420 cl_git_pass(git_str_encode_base64(&auth
, creds
, strlen(creds
)));
421 g_options
.fetch_opts
.custom_headers
.count
= 1;
422 g_options
.fetch_opts
.custom_headers
.strings
= &auth
.ptr
;
424 cl_git_pass(git_clone(&g_repo
, "https://bitbucket.org/libgit2-test/testgitrepository.git", "./foo", &g_options
));
426 git_str_dispose(&auth
);
429 void test_online_clone__bitbucket_style(void)
431 git_credential_userpass_payload user_pass
= {
432 "libgit2-test", "YT77Ppm2nq8w4TYjGS8U"
435 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
436 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
438 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL
, "./foo", &g_options
));
439 git_repository_free(g_repo
); g_repo
= NULL
;
440 cl_fixture_cleanup("./foo");
443 void test_online_clone__bitbucket_uses_creds_in_url(void)
445 git_credential_userpass_payload user_pass
= {
446 "libgit2-test", "wrong"
449 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
450 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
453 * Correct user and pass are in the URL; the (incorrect) creds in
454 * the `git_credential_userpass_payload` should be ignored.
456 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_PASS
, "./foo", &g_options
));
457 git_repository_free(g_repo
); g_repo
= NULL
;
458 cl_fixture_cleanup("./foo");
461 void test_online_clone__bitbucket_falls_back_to_specified_creds(void)
463 git_credential_userpass_payload user_pass
= {
464 "libgit2-test", "libgit2"
467 g_options
.fetch_opts
.callbacks
.credentials
= git_credential_userpass
;
468 g_options
.fetch_opts
.callbacks
.payload
= &user_pass
;
471 * TODO: as of March 2018, bitbucket sporadically fails with
472 * 403s instead of replying with a 401 - but only sometimes.
477 * Incorrect user and pass are in the URL; the (correct) creds in
478 * the `git_credential_userpass_payload` should be used as a fallback.
480 cl_git_pass(git_clone(&g_repo
, BB_REPO_URL_WITH_WRONG_PASS
, "./foo", &g_options
));
481 git_repository_free(g_repo
); g_repo
= NULL
;
482 cl_fixture_cleanup("./foo");
485 void test_online_clone__googlesource(void)
487 cl_git_pass(git_clone(&g_repo
, GOOGLESOURCE_REPO_URL
, "./foo", &g_options
));
488 git_repository_free(g_repo
); g_repo
= NULL
;
489 cl_fixture_cleanup("./foo");
492 static int cancel_at_half(const git_indexer_progress
*stats
, void *payload
)
496 if (stats
->received_objects
> (stats
->total_objects
/2))
501 void test_online_clone__can_cancel(void)
503 g_options
.fetch_opts
.callbacks
.transfer_progress
= cancel_at_half
;
505 cl_git_fail_with(4321,
506 git_clone(&g_repo
, LIVE_REPO_URL
, "./foo", &g_options
));
509 static int cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
510 unsigned int allowed_types
, void *payload
)
512 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
514 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
515 return git_credential_username_new(cred
, _remote_user
);
517 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
518 return git_credential_ssh_key_new(cred
,
519 _remote_user
, _remote_ssh_pubkey
,
520 _remote_ssh_privkey
, _remote_ssh_passphrase
);
522 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
526 static int check_ssh_auth_methods(git_credential
**cred
, const char *url
, const char *username_from_url
,
527 unsigned int allowed_types
, void *data
)
529 int *with_user
= (int *) data
;
530 GIT_UNUSED(cred
); GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(data
);
533 cl_assert_equal_i(GIT_CREDENTIAL_USERNAME
, allowed_types
);
535 cl_assert(!(allowed_types
& GIT_CREDENTIAL_USERNAME
));
540 void test_online_clone__ssh_auth_methods(void)
547 g_options
.fetch_opts
.callbacks
.credentials
= check_ssh_auth_methods
;
548 g_options
.fetch_opts
.callbacks
.payload
= &with_user
;
549 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
552 cl_git_fail_with(GIT_EUSER
,
553 git_clone(&g_repo
, SSH_REPO_URL
, "./foo", &g_options
));
556 cl_git_fail_with(GIT_EUSER
,
557 git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
560 static int custom_remote_ssh_with_paths(
562 git_repository
*repo
,
571 if ((error
= git_remote_create(out
, repo
, name
, url
)) < 0)
577 void test_online_clone__ssh_with_paths(void)
579 char *bad_paths
[] = {
583 char *good_paths
[] = {
584 "/usr/bin/git-upload-pack",
585 "/usr/bin/git-receive-pack",
595 if (!_remote_url
|| !_remote_user
|| strncmp(_remote_url
, "ssh://", 5) != 0)
598 g_options
.remote_cb
= custom_remote_ssh_with_paths
;
599 g_options
.fetch_opts
.callbacks
.transport
= git_transport_ssh_with_paths
;
600 g_options
.fetch_opts
.callbacks
.credentials
= cred_cb
;
601 g_options
.fetch_opts
.callbacks
.payload
= &arr
;
602 g_options
.fetch_opts
.callbacks
.certificate_check
= NULL
;
604 cl_git_fail(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
606 arr
.strings
= good_paths
;
607 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
610 static int cred_foo_bar(git_credential
**cred
, const char *url
, const char *username_from_url
,
611 unsigned int allowed_types
, void *data
)
614 GIT_UNUSED(url
); GIT_UNUSED(username_from_url
); GIT_UNUSED(allowed_types
); GIT_UNUSED(data
);
616 return git_credential_userpass_plaintext_new(cred
, "foo", "bar");
619 void test_online_clone__ssh_cannot_change_username(void)
624 g_options
.fetch_opts
.callbacks
.credentials
= cred_foo_bar
;
626 cl_git_fail(git_clone(&g_repo
, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options
));
629 static int ssh_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
631 git_cert_hostkey
*key
;
632 git_oid expected
= {{0}}, actual
= {{0}};
637 cl_assert(_remote_ssh_fingerprint
);
639 cl_git_pass(git_oid_fromstrp(&expected
, _remote_ssh_fingerprint
));
640 cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2
, cert
->cert_type
);
641 key
= (git_cert_hostkey
*) cert
;
644 * We need to figure out how long our input was to check for
645 * the type. Here we abuse the fact that both hashes fit into
648 if (strlen(_remote_ssh_fingerprint
) == 32 && key
->type
& GIT_CERT_SSH_MD5
) {
649 memcpy(&actual
.id
, key
->hash_md5
, 16);
650 } else if (strlen(_remote_ssh_fingerprint
) == 40 && key
->type
& GIT_CERT_SSH_SHA1
) {
651 memcpy(&actual
, key
->hash_sha1
, 20);
653 cl_fail("Cannot find a usable SSH hash");
656 cl_assert(!memcmp(&expected
, &actual
, 20));
658 cl_assert_equal_s("localhost", host
);
663 void test_online_clone__ssh_cert(void)
665 g_options
.fetch_opts
.callbacks
.certificate_check
= ssh_certificate_check
;
667 if (!_remote_ssh_fingerprint
)
670 cl_git_fail_with(GIT_EUSER
, git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
673 static char *read_key_file(const char *path
)
682 cl_assert((f
= fopen(path
, "r")) != NULL
);
683 cl_assert(fseek(f
, 0, SEEK_END
) != -1);
684 cl_assert((key_length
= ftell(f
)) != -1);
685 cl_assert(fseek(f
, 0, SEEK_SET
) != -1);
686 cl_assert((buf
= malloc(key_length
)) != NULL
);
687 cl_assert(fread(buf
, key_length
, 1, f
) == 1);
693 static int ssh_memory_cred_cb(git_credential
**cred
, const char *url
, const char *user_from_url
,
694 unsigned int allowed_types
, void *payload
)
696 GIT_UNUSED(url
); GIT_UNUSED(user_from_url
); GIT_UNUSED(payload
);
698 if (allowed_types
& GIT_CREDENTIAL_USERNAME
)
699 return git_credential_username_new(cred
, _remote_user
);
701 if (allowed_types
& GIT_CREDENTIAL_SSH_KEY
)
703 char *pubkey
= read_key_file(_remote_ssh_pubkey
);
704 char *privkey
= read_key_file(_remote_ssh_privkey
);
706 int ret
= git_credential_ssh_key_memory_new(cred
, _remote_user
, pubkey
, privkey
, _remote_ssh_passphrase
);
715 git_error_set(GIT_ERROR_NET
, "unexpected cred type");
719 void test_online_clone__ssh_memory_auth(void)
721 #ifndef GIT_SSH_MEMORY_CREDENTIALS
724 if (!_remote_url
|| !_remote_user
|| !_remote_ssh_privkey
|| strncmp(_remote_url
, "ssh://", 5) != 0)
727 g_options
.fetch_opts
.callbacks
.credentials
= ssh_memory_cred_cb
;
729 cl_git_pass(git_clone(&g_repo
, _remote_url
, "./foo", &g_options
));
732 static int fail_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
739 return GIT_ECERTIFICATE
;
742 void test_online_clone__certificate_invalid(void)
744 g_options
.fetch_opts
.callbacks
.certificate_check
= fail_certificate_check
;
746 cl_git_fail_with(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
750 cl_git_fail_with(git_clone(&g_repo
, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options
),
755 static int succeed_certificate_check(git_cert
*cert
, int valid
, const char *host
, void *payload
)
761 cl_assert_equal_s("github.com", host
);
766 void test_online_clone__certificate_valid(void)
768 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
770 cl_git_pass(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
773 void test_online_clone__start_with_http(void)
775 g_options
.fetch_opts
.callbacks
.certificate_check
= succeed_certificate_check
;
777 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
780 static int called_proxy_creds
;
781 static int proxy_cred_cb(git_credential
**out
, const char *url
, const char *username
, unsigned int allowed
, void *payload
)
784 GIT_UNUSED(username
);
788 called_proxy_creds
= 1;
789 return git_credential_userpass_plaintext_new(out
, _remote_proxy_user
, _remote_proxy_pass
);
792 static int proxy_cert_cb(git_cert
*cert
, int valid
, const char *host
, void *payload
)
801 cl_assert(_remote_proxy_host
);
803 if ((colon
= strchr(_remote_proxy_host
, ':')) != NULL
)
804 host_len
= (colon
- _remote_proxy_host
);
806 host_len
= strlen(_remote_proxy_host
);
808 if (_remote_proxy_selfsigned
!= NULL
&&
809 strlen(host
) == host_len
&&
810 strncmp(_remote_proxy_host
, host
, host_len
) == 0)
813 return valid
? 0 : GIT_ECERTIFICATE
;
816 void test_online_clone__proxy_credentials_request(void)
818 git_str url
= GIT_STR_INIT
;
820 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
823 cl_git_pass(git_str_printf(&url
, "%s://%s/",
824 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
825 _remote_proxy_host
));
827 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
828 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
829 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
830 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
831 called_proxy_creds
= 0;
832 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
833 cl_assert(called_proxy_creds
);
835 git_str_dispose(&url
);
838 void test_online_clone__proxy_credentials_in_url(void)
840 git_str url
= GIT_STR_INIT
;
842 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
845 cl_git_pass(git_str_printf(&url
, "%s://%s:%s@%s/",
846 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
847 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
849 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
850 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
851 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
852 called_proxy_creds
= 0;
853 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
854 cl_assert(called_proxy_creds
== 0);
856 git_str_dispose(&url
);
859 void test_online_clone__proxy_credentials_in_environment(void)
861 git_str url
= GIT_STR_INIT
;
863 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
866 _orig_http_proxy
= cl_getenv("HTTP_PROXY");
867 _orig_https_proxy
= cl_getenv("HTTPS_PROXY");
868 _orig_no_proxy
= cl_getenv("NO_PROXY");
869 _orig_proxies_need_reset
= 1;
871 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
872 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
874 cl_git_pass(git_str_printf(&url
, "%s://%s:%s@%s/",
875 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
876 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
878 cl_setenv("HTTP_PROXY", url
.ptr
);
879 cl_setenv("HTTPS_PROXY", url
.ptr
);
880 cl_setenv("NO_PROXY", NULL
);
882 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
884 git_str_dispose(&url
);
887 void test_online_clone__proxy_credentials_in_url_https(void)
889 git_str url
= GIT_STR_INIT
;
891 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
894 cl_git_pass(git_str_printf(&url
, "%s://%s:%s@%s/",
895 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
896 _remote_proxy_user
, _remote_proxy_pass
, _remote_proxy_host
));
898 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
899 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
900 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
901 g_options
.fetch_opts
.callbacks
.certificate_check
= ssl_cert
;
902 called_proxy_creds
= 0;
903 cl_git_pass(git_clone(&g_repo
, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
904 cl_assert(called_proxy_creds
== 0);
906 git_str_dispose(&url
);
909 void test_online_clone__proxy_auto_not_detected(void)
911 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_AUTO
;
913 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
916 void test_online_clone__proxy_cred_callback_after_failed_url_creds(void)
918 git_str url
= GIT_STR_INIT
;
920 if (!_remote_proxy_host
|| !_remote_proxy_user
|| !_remote_proxy_pass
)
923 cl_git_pass(git_str_printf(&url
, "%s://invalid_user_name:INVALID_pass_WORD@%s/",
924 _remote_proxy_scheme
? _remote_proxy_scheme
: "http",
925 _remote_proxy_host
));
927 g_options
.fetch_opts
.proxy_opts
.type
= GIT_PROXY_SPECIFIED
;
928 g_options
.fetch_opts
.proxy_opts
.url
= url
.ptr
;
929 g_options
.fetch_opts
.proxy_opts
.credentials
= proxy_cred_cb
;
930 g_options
.fetch_opts
.proxy_opts
.certificate_check
= proxy_cert_cb
;
931 called_proxy_creds
= 0;
932 cl_git_pass(git_clone(&g_repo
, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options
));
933 cl_assert(called_proxy_creds
);
935 git_str_dispose(&url
);
938 void test_online_clone__azurerepos(void)
940 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/test", "./foo", &g_options
));
941 cl_assert(git_fs_path_exists("./foo/master.txt"));
944 void test_online_clone__path_whitespace(void)
946 cl_git_pass(git_clone(&g_repo
, "https://libgit2@dev.azure.com/libgit2/test/_git/spaces%20in%20the%20name", "./foo", &g_options
));
947 cl_assert(git_fs_path_exists("./foo/master.txt"));
950 void test_online_clone__redirect_default_succeeds_for_initial(void)
952 git_clone_options options
= GIT_CLONE_OPTIONS_INIT
;
954 if (!_remote_redirect_initial
|| !_remote_redirect_subsequent
)
957 cl_git_pass(git_clone(&g_repo
, _remote_redirect_initial
, "./initial", &options
));
960 void test_online_clone__redirect_default_fails_for_subsequent(void)
962 git_clone_options options
= GIT_CLONE_OPTIONS_INIT
;
964 if (!_remote_redirect_initial
|| !_remote_redirect_subsequent
)
967 cl_git_fail(git_clone(&g_repo
, _remote_redirect_subsequent
, "./fail", &options
));
970 void test_online_clone__redirect_none(void)
972 git_clone_options options
= GIT_CLONE_OPTIONS_INIT
;
974 if (!_remote_redirect_initial
)
977 options
.fetch_opts
.follow_redirects
= GIT_REMOTE_REDIRECT_NONE
;
979 cl_git_fail(git_clone(&g_repo
, _remote_redirect_initial
, "./fail", &options
));
982 void test_online_clone__redirect_initial_succeeds_for_initial(void)
984 git_clone_options options
= GIT_CLONE_OPTIONS_INIT
;
986 if (!_remote_redirect_initial
|| !_remote_redirect_subsequent
)
989 options
.fetch_opts
.follow_redirects
= GIT_REMOTE_REDIRECT_INITIAL
;
991 cl_git_pass(git_clone(&g_repo
, _remote_redirect_initial
, "./initial", &options
));
994 void test_online_clone__redirect_initial_fails_for_subsequent(void)
996 git_clone_options options
= GIT_CLONE_OPTIONS_INIT
;
998 if (!_remote_redirect_initial
|| !_remote_redirect_subsequent
)
1001 options
.fetch_opts
.follow_redirects
= GIT_REMOTE_REDIRECT_INITIAL
;
1003 cl_git_fail(git_clone(&g_repo
, _remote_redirect_subsequent
, "./fail", &options
));