]>
Commit | Line | Data |
---|---|---|
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 | 17 | static git_repository *g_repo; |
18b2d560 | 18 | static git_clone_options g_options; |
65415ea2 | 19 | |
6443eaf2 | 20 | void 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 | 34 | void 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 | 43 | void 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 | 48 | cl_assert(!git_repository_is_bare(g_repo)); |
209425ce | 49 | cl_git_pass(git_remote_lookup(&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 | 56 | void 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 | 63 | cl_assert(git_repository_is_bare(g_repo)); |
209425ce | 64 | cl_git_pass(git_remote_lookup(&origin, g_repo, "origin")); |
add5efe7 | 65 | |
66 | git_remote_free(origin); | |
65415ea2 BS |
67 | } |
68 | ||
6443eaf2 | 69 | void 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 | 85 | static 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 | 92 | static 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 | 100 | void 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 | |
6c9e86ad | 107 | g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; |
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 |
129 | static 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 |
156 | void 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 |
186 | static 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 | 194 | void 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 |
205 | static 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 | 217 | void 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 |
230 | static 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 | ||
248 | void 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 |
264 | int 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 | 281 | void 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 | ||
306 | void 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 | |
332 | static 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 | ||
341 | void 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 |
349 | static 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 |
369 | static 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 |
383 | void test_online_clone__ssh_auth_methods(void) |
384 | { | |
e26b08d3 CMN |
385 | int with_user; |
386 | ||
fe3b6789 L |
387 | #ifndef GIT_SSH |
388 | clar__skip(); | |
389 | #endif | |
22618906 | 390 | g_options.remote_callbacks.credentials = check_ssh_auth_methods; |
e26b08d3 | 391 | g_options.remote_callbacks.payload = &with_user; |
d31402a3 | 392 | |
e26b08d3 | 393 | with_user = 0; |
22618906 CMN |
394 | cl_git_fail_with(GIT_EUSER, |
395 | git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options)); | |
e26b08d3 CMN |
396 | |
397 | with_user = 1; | |
398 | cl_git_fail_with(GIT_EUSER, | |
399 | git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options)); | |
400 | } | |
401 | ||
d4256ed5 CMN |
402 | static int custom_remote_ssh_with_paths( |
403 | git_remote **out, | |
404 | git_repository *repo, | |
405 | const char *name, | |
406 | const char *url, | |
407 | void *payload) | |
408 | { | |
409 | int error; | |
d4256ed5 CMN |
410 | git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT; |
411 | ||
412 | if ((error = git_remote_create(out, repo, name, url)) < 0) | |
413 | return error; | |
414 | ||
415 | if ((error = git_remote_set_transport(*out, git_transport_ssh_with_paths, payload)) < 0) | |
416 | return error; | |
d31402a3 | 417 | |
d4256ed5 CMN |
418 | callbacks.credentials = cred_cb; |
419 | git_remote_set_callbacks(*out, &callbacks); | |
d31402a3 | 420 | |
d4256ed5 CMN |
421 | return 0; |
422 | } | |
423 | ||
424 | void test_online_clone__ssh_with_paths(void) | |
425 | { | |
426 | char *bad_paths[] = { | |
427 | "/bin/yes", | |
428 | "/bin/false", | |
429 | }; | |
430 | char *good_paths[] = { | |
431 | "/usr/bin/git-upload-pack", | |
432 | "/usr/bin/git-receive-pack", | |
433 | }; | |
434 | git_strarray arr = { | |
435 | bad_paths, | |
436 | 2, | |
437 | }; | |
438 | ||
439 | const char *remote_url = cl_getenv("GITTEST_REMOTE_URL"); | |
440 | const char *remote_user = cl_getenv("GITTEST_REMOTE_USER"); | |
441 | ||
fe3b6789 L |
442 | #ifndef GIT_SSH |
443 | clar__skip(); | |
444 | #endif | |
aea67633 | 445 | if (!remote_url || !remote_user || strncmp(remote_url, "ssh://", 5) != 0) |
d4256ed5 CMN |
446 | clar__skip(); |
447 | ||
448 | g_options.remote_cb = custom_remote_ssh_with_paths; | |
449 | g_options.remote_cb_payload = &arr; | |
d31402a3 | 450 | |
d4256ed5 | 451 | cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options)); |
d31402a3 | 452 | |
d4256ed5 CMN |
453 | arr.strings = good_paths; |
454 | cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options)); | |
455 | } | |
d31402a3 | 456 | |
e26b08d3 CMN |
457 | static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url, |
458 | unsigned int allowed_types, void *data) | |
459 | ||
460 | { | |
461 | GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data); | |
462 | ||
463 | return git_cred_userpass_plaintext_new(cred, "foo", "bar"); | |
464 | } | |
465 | ||
466 | void test_online_clone__ssh_cannot_change_username(void) | |
467 | { | |
fe3b6789 L |
468 | #ifndef GIT_SSH |
469 | clar__skip(); | |
470 | #endif | |
e26b08d3 CMN |
471 | g_options.remote_callbacks.credentials = cred_foo_bar; |
472 | ||
473 | cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options)); | |
22618906 | 474 | } |
f0c53d21 | 475 | |
e6e834a1 | 476 | int ssh_certificate_check(git_cert *cert, int valid, const char *host, void *payload) |
bf8756d6 CMN |
477 | { |
478 | git_cert_hostkey *key; | |
479 | git_oid expected = {{0}}, actual = {{0}}; | |
480 | const char *expected_str; | |
481 | ||
bf8756d6 CMN |
482 | GIT_UNUSED(valid); |
483 | GIT_UNUSED(payload); | |
484 | ||
485 | expected_str = cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT"); | |
52e09724 | 486 | cl_assert(expected_str); |
bf8756d6 | 487 | |
0782fc43 CMN |
488 | cl_git_pass(git_oid_fromstrp(&expected, expected_str)); |
489 | cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2, cert->cert_type); | |
0782fc43 | 490 | key = (git_cert_hostkey *) cert; |
bf8756d6 | 491 | |
1e0aa105 CMN |
492 | /* |
493 | * We need to figure out how long our input was to check for | |
494 | * the type. Here we abuse the fact that both hashes fit into | |
495 | * our git_oid type. | |
496 | */ | |
497 | if (strlen(expected_str) == 32 && key->type & GIT_CERT_SSH_MD5) { | |
498 | memcpy(&actual.id, key->hash_md5, 16); | |
499 | } else if (strlen(expected_str) == 40 && key->type & GIT_CERT_SSH_SHA1) { | |
500 | memcpy(&actual, key->hash_sha1, 20); | |
501 | } else { | |
502 | cl_fail("Cannot find a usable SSH hash"); | |
503 | } | |
286369a8 | 504 | |
1e0aa105 | 505 | cl_assert(!memcmp(&expected, &actual, 20)); |
bf8756d6 | 506 | |
db3d169c SS |
507 | cl_assert_equal_s("localhost", host); |
508 | ||
bf8756d6 CMN |
509 | return GIT_EUSER; |
510 | } | |
511 | ||
512 | void test_online_clone__ssh_cert(void) | |
513 | { | |
514 | g_options.remote_callbacks.certificate_check = ssh_certificate_check; | |
515 | ||
52e09724 CMN |
516 | if (!cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT")) |
517 | cl_skip(); | |
518 | ||
bf8756d6 CMN |
519 | cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options)); |
520 | } | |
521 | ||
f0c53d21 | 522 | void test_online_clone__url_with_no_path_returns_EINVALIDSPEC(void) |
523 | { | |
524 | cl_git_fail_with(git_clone(&g_repo, "http://github.com", "./foo", &g_options), | |
525 | GIT_EINVALIDSPEC); | |
526 | } | |
85acc562 | 527 | |
e6e834a1 | 528 | static int fail_certificate_check(git_cert *cert, int valid, const char *host, void *payload) |
85acc562 | 529 | { |
0782fc43 | 530 | GIT_UNUSED(cert); |
17491f6e | 531 | GIT_UNUSED(valid); |
e6e834a1 | 532 | GIT_UNUSED(host); |
85acc562 CMN |
533 | GIT_UNUSED(payload); |
534 | ||
23ca0ad5 | 535 | return GIT_ECERTIFICATE; |
85acc562 CMN |
536 | } |
537 | ||
538 | void test_online_clone__certificate_invalid(void) | |
539 | { | |
540 | g_options.remote_callbacks.certificate_check = fail_certificate_check; | |
541 | ||
08545d36 | 542 | cl_git_fail_with(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options), |
17491f6e | 543 | GIT_ECERTIFICATE); |
2f5864c5 | 544 | |
08545d36 | 545 | #ifdef GIT_SSH |
2f5864c5 CMN |
546 | cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options), |
547 | GIT_ECERTIFICATE); | |
08545d36 | 548 | #endif |
85acc562 CMN |
549 | } |
550 | ||
e6e834a1 | 551 | static int succeed_certificate_check(git_cert *cert, int valid, const char *host, void *payload) |
85acc562 | 552 | { |
0782fc43 | 553 | GIT_UNUSED(cert); |
17491f6e | 554 | GIT_UNUSED(valid); |
85acc562 CMN |
555 | GIT_UNUSED(payload); |
556 | ||
db3d169c SS |
557 | cl_assert_equal_s("github.com", host); |
558 | ||
23ca0ad5 | 559 | return 0; |
85acc562 CMN |
560 | } |
561 | ||
562 | void test_online_clone__certificate_valid(void) | |
563 | { | |
564 | g_options.remote_callbacks.certificate_check = succeed_certificate_check; | |
565 | ||
08545d36 | 566 | cl_git_pass(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options)); |
85acc562 | 567 | } |