clone.c 19.4 KB
Newer Older
1 2 3
#include "clar_libgit2.h"

#include "git2/clone.h"
4
#include "git2/cred_helpers.h"
5
#include "remote.h"
6 7
#include "fileops.h"
#include "refs.h"
8

9 10
#define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository"
#define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository"
11 12 13
#define BB_REPO_URL "https://libgit3@bitbucket.org/libgit2/testgitrepository.git"
#define BB_REPO_URL_WITH_PASS "https://libgit3:libgit3@bitbucket.org/libgit2/testgitrepository.git"
#define BB_REPO_URL_WITH_WRONG_PASS "https://libgit3:wrong@bitbucket.org/libgit2/testgitrepository.git"
14

15 16
#define SSH_REPO_URL "ssh://github.com/libgit2/TestGitRepository"

17
static git_repository *g_repo;
18
static git_clone_options g_options;
19

20 21 22 23 24 25 26
static char *_remote_url = NULL;
static char *_remote_user = NULL;
static char *_remote_pass = NULL;
static char *_remote_ssh_pubkey = NULL;
static char *_remote_ssh_privkey = NULL;
static char *_remote_ssh_passphrase = NULL;
static char *_remote_ssh_fingerprint = NULL;
27 28 29
static char *_remote_proxy_url = NULL;
static char *_remote_proxy_user = NULL;
static char *_remote_proxy_pass = NULL;
30 31


32
void test_online_clone__initialize(void)
33
{
34
	git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
35
	git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
36

37
	g_repo = NULL;
38 39 40

	memset(&g_options, 0, sizeof(git_clone_options));
	g_options.version = GIT_CLONE_OPTIONS_VERSION;
41 42
	g_options.checkout_opts = dummy_opts;
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
43
	g_options.fetch_opts = dummy_fetch;
44 45 46 47 48 49 50 51

	_remote_url = cl_getenv("GITTEST_REMOTE_URL");
	_remote_user = cl_getenv("GITTEST_REMOTE_USER");
	_remote_pass = cl_getenv("GITTEST_REMOTE_PASS");
	_remote_ssh_pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
	_remote_ssh_privkey = cl_getenv("GITTEST_REMOTE_SSH_KEY");
	_remote_ssh_passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");
	_remote_ssh_fingerprint = cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
52 53 54
	_remote_proxy_url = cl_getenv("GITTEST_REMOTE_PROXY_URL");
	_remote_proxy_user = cl_getenv("GITTEST_REMOTE_PROXY_USER");
	_remote_proxy_pass = cl_getenv("GITTEST_REMOTE_PROXY_PASS");
55 56
}

57
void test_online_clone__cleanup(void)
58
{
59
	if (g_repo) {
60
		git_repository_free(g_repo);
61 62
		g_repo = NULL;
	}
63
	cl_fixture_cleanup("./foo");
64 65 66 67 68 69 70 71

	git__free(_remote_url);
	git__free(_remote_user);
	git__free(_remote_pass);
	git__free(_remote_ssh_pubkey);
	git__free(_remote_ssh_privkey);
	git__free(_remote_ssh_passphrase);
	git__free(_remote_ssh_fingerprint);
72 73 74
	git__free(_remote_proxy_url);
	git__free(_remote_proxy_user);
	git__free(_remote_proxy_pass);
75 76
}

77
void test_online_clone__network_full(void)
78 79 80
{
	git_remote *origin;

81
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
82
	cl_assert(!git_repository_is_bare(g_repo));
83
	cl_git_pass(git_remote_lookup(&origin, g_repo, "origin"));
nulltoken committed
84

85 86
	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);

nulltoken committed
87
	git_remote_free(origin);
88 89
}

90
void test_online_clone__network_bare(void)
91 92 93
{
	git_remote *origin;

94
	g_options.bare = true;
95

96
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
97
	cl_assert(git_repository_is_bare(g_repo));
98
	cl_git_pass(git_remote_lookup(&origin, g_repo, "origin"));
nulltoken committed
99 100

	git_remote_free(origin);
101 102
}

103
void test_online_clone__empty_repository(void)
104 105 106
{
	git_reference *head;

107
	cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options));
108 109

	cl_assert_equal_i(true, git_repository_is_empty(g_repo));
110
	cl_assert_equal_i(true, git_repository_head_unborn(g_repo));
111 112 113

	cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
114
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
115 116 117

	git_reference_free(head);
}
118

119
static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
120 121
{
	bool *was_called = (bool*)payload;
Ben Straub committed
122
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
123 124 125
	(*was_called) = true;
}

126
static int fetch_progress(const git_transfer_progress *stats, void *payload)
127 128
{
	bool *was_called = (bool*)payload;
Ben Straub committed
129
	GIT_UNUSED(stats);
130
	(*was_called) = true;
131
	return 0;
132 133
}

134
void test_online_clone__can_checkout_a_cloned_repo(void)
135 136
{
	git_buf path = GIT_BUF_INIT;
137
	git_reference *head;
138 139
	bool checkout_progress_cb_was_called = false,
		  fetch_progress_cb_was_called = false;
140

141
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
142 143
	g_options.checkout_opts.progress_cb = &checkout_progress;
	g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
144 145
	g_options.fetch_opts.callbacks.transfer_progress = &fetch_progress;
	g_options.fetch_opts.callbacks.payload = &fetch_progress_cb_was_called;
146

147
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
148 149 150

	cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "master.txt"));
	cl_assert_equal_i(true, git_path_isfile(git_buf_cstr(&path)));
151 152 153

	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
154
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
nulltoken committed
155

156 157
	cl_assert_equal_i(true, checkout_progress_cb_was_called);
	cl_assert_equal_i(true, fetch_progress_cb_was_called);
158

nulltoken committed
159 160
	git_reference_free(head);
	git_buf_free(&path);
161
}
Ben Straub committed
162

163 164
static int remote_mirror_cb(git_remote **out, git_repository *repo,
			    const char *name, const char *url, void *payload)
165
{
166
	int error;
167 168
	git_remote *remote;

169
	GIT_UNUSED(payload);
170

171
	if ((error = git_remote_create_with_fetchspec(&remote, repo, name, url, "+refs/*:refs/*")) < 0)
172
		return error;
173

174 175
	*out = remote;
	return 0;
176 177
}

178 179
void test_online_clone__clone_mirror(void)
{
180
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
181 182 183 184
	git_reference *head;

	bool fetch_progress_cb_was_called = false;

185 186
	opts.fetch_opts.callbacks.transfer_progress = &fetch_progress;
	opts.fetch_opts.callbacks.payload = &fetch_progress_cb_was_called;
187

188 189
	opts.bare = true;
	opts.remote_cb = remote_mirror_cb;
190

191
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
192 193 194 195 196 197 198 199

	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));

	cl_assert_equal_i(true, fetch_progress_cb_was_called);

	git_reference_free(head);
200 201 202
	git_repository_free(g_repo);
	g_repo = NULL;

203 204 205
	cl_fixture_cleanup("./foo.git");
}

Ben Straub committed
206 207 208 209 210 211 212 213
static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *payload)
{
	int *callcount = (int*)payload;
	GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b);
	*callcount = *callcount + 1;
	return 0;
}

214
void test_online_clone__custom_remote_callbacks(void)
Ben Straub committed
215 216 217
{
	int callcount = 0;

218 219
	g_options.fetch_opts.callbacks.update_tips = update_tips;
	g_options.fetch_opts.callbacks.payload = &callcount;
Ben Straub committed
220 221 222 223 224

	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
	cl_assert(callcount > 0);
}

225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
void test_online_clone__custom_headers(void)
{
	char *empty_header = "";
	char *unnamed_header = "this is a header about nothing";
	char *newlines = "X-Custom: almost OK\n";
	char *conflict = "Accept: defined-by-git";
	char *ok = "X-Custom: this should be ok";

	g_options.fetch_opts.custom_headers.count = 1;

	g_options.fetch_opts.custom_headers.strings = &empty_header;
	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));

	g_options.fetch_opts.custom_headers.strings = &unnamed_header;
	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));

	g_options.fetch_opts.custom_headers.strings = &newlines;
	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));

	g_options.fetch_opts.custom_headers.strings = &conflict;
	cl_git_fail(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));

	/* Finally, we got it right! */
	g_options.fetch_opts.custom_headers.strings = &ok;
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
}

252 253 254 255 256 257 258
static int cred_failure_cb(
	git_cred **cred,
	const char *url,
	const char *username_from_url,
	unsigned int allowed_types,
	void *data)
{
Russell Belfer committed
259 260
	GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
	GIT_UNUSED(allowed_types); GIT_UNUSED(data);
Ben Straub committed
261
	return -172;
262 263
}

Ben Straub committed
264
void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
265
{
266
	if (!_remote_url || !_remote_user)
Vicent Marti committed
267
		clar__skip();
268

269
	g_options.fetch_opts.callbacks.credentials = cred_failure_cb;
270

271
	cl_git_fail_with(-172, git_clone(&g_repo, _remote_url, "./foo", &g_options));
272 273
}

274 275 276 277 278 279 280
static int cred_count_calls_cb(git_cred **cred, const char *url, const char *user,
			       unsigned int allowed_types, void *data)
{
	size_t *counter = (size_t *) data;

	GIT_UNUSED(url); GIT_UNUSED(user); GIT_UNUSED(allowed_types);

281 282 283
	if (allowed_types == GIT_CREDTYPE_USERNAME)
		return git_cred_username_new(cred, "foo");

284 285 286 287 288 289 290 291 292 293 294 295
	(*counter)++;

	if (*counter == 3)
		return GIT_EUSER;

	return git_cred_userpass_plaintext_new(cred, "foo", "bar");
}

void test_online_clone__cred_callback_called_again_on_auth_failure(void)
{
	size_t counter = 0;

296
	if (!_remote_url || !_remote_user)
297 298
		clar__skip();

299 300
	g_options.fetch_opts.callbacks.credentials = cred_count_calls_cb;
	g_options.fetch_opts.callbacks.payload = &counter;
301

302
	cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, _remote_url, "./foo", &g_options));
303 304 305
	cl_assert_equal_i(3, counter);
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
int cred_default(
	git_cred **cred,
	const char *url,
	const char *user_from_url,
	unsigned int allowed_types,
	void *payload)
{
	GIT_UNUSED(url);
	GIT_UNUSED(user_from_url);
	GIT_UNUSED(payload);

	if (!(allowed_types & GIT_CREDTYPE_DEFAULT))
		return 0;

	return git_cred_default_new(cred);
}

323
void test_online_clone__credentials(void)
Ben Straub committed
324
{
325 326 327
	/* Remote URL environment variable must be set.
	 * User and password are optional.
	 */
328
	git_cred_userpass_payload user_pass = {
329 330
		_remote_user,
		_remote_pass
Ben Straub committed
331 332
	};

333 334
	if (!_remote_url)
		clar__skip();
Ben Straub committed
335

336
	if (cl_is_env_set("GITTEST_REMOTE_DEFAULT")) {
337
		g_options.fetch_opts.callbacks.credentials = cred_default;
338
	} else {
339 340
		g_options.fetch_opts.callbacks.credentials = git_cred_userpass;
		g_options.fetch_opts.callbacks.payload = &user_pass;
341
	}
Ben Straub committed
342

343
	cl_git_pass(git_clone(&g_repo, _remote_url, "./foo", &g_options));
344 345 346 347 348 349 350 351 352 353
	git_repository_free(g_repo); g_repo = NULL;
	cl_fixture_cleanup("./foo");
}

void test_online_clone__bitbucket_style(void)
{
	git_cred_userpass_payload user_pass = {
		"libgit2", "libgit2"
	};

354 355
	g_options.fetch_opts.callbacks.credentials = git_cred_userpass;
	g_options.fetch_opts.callbacks.payload = &user_pass;
356 357 358 359

	cl_git_pass(git_clone(&g_repo, BB_REPO_URL, "./foo", &g_options));
	git_repository_free(g_repo); g_repo = NULL;
	cl_fixture_cleanup("./foo");
360

361 362
	/* User and pass from URL */
	user_pass.password = "wrong";
363 364 365
	cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_PASS, "./foo", &g_options));
	git_repository_free(g_repo); g_repo = NULL;
	cl_fixture_cleanup("./foo");
366 367 368 369 370 371

	/* Wrong password in URL, fall back to user_pass */
	user_pass.password = "libgit2";
	cl_git_pass(git_clone(&g_repo, BB_REPO_URL_WITH_WRONG_PASS, "./foo", &g_options));
	git_repository_free(g_repo); g_repo = NULL;
	cl_fixture_cleanup("./foo");
Ben Straub committed
372
}
373 374 375 376 377 378

static int cancel_at_half(const git_transfer_progress *stats, void *payload)
{
	GIT_UNUSED(payload);

	if (stats->received_objects > (stats->total_objects/2))
379
		return 4321;
380 381 382 383 384
	return 0;
}

void test_online_clone__can_cancel(void)
{
385
	g_options.fetch_opts.callbacks.transfer_progress = cancel_at_half;
386

387 388
	cl_git_fail_with(
		git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
389
}
390

391 392 393 394 395
static int cred_cb(git_cred **cred, const char *url, const char *user_from_url,
		   unsigned int allowed_types, void *payload)
{
	GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);

396
	if (allowed_types & GIT_CREDTYPE_USERNAME)
397
		return git_cred_username_new(cred, _remote_user);
398

399
	if (allowed_types & GIT_CREDTYPE_SSH_KEY)
400 401 402
		return git_cred_ssh_key_new(cred,
			_remote_user, _remote_ssh_pubkey,
			_remote_ssh_privkey, _remote_ssh_passphrase);
403 404 405 406

	giterr_set(GITERR_NET, "unexpected cred type");
	return -1;
}
407

408 409 410
static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
				  unsigned int allowed_types, void *data)
{
411
	int *with_user = (int *) data;
412
	GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
413

414 415 416 417
	if (!*with_user)
		cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
	else
		cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
418

419 420
	return GIT_EUSER;
}
421

422 423
void test_online_clone__ssh_auth_methods(void)
{
424 425
	int with_user;

426 427 428
#ifndef GIT_SSH
	clar__skip();
#endif
429 430
	g_options.fetch_opts.callbacks.credentials = check_ssh_auth_methods;
	g_options.fetch_opts.callbacks.payload = &with_user;
431

432
	with_user = 0;
433 434
	cl_git_fail_with(GIT_EUSER,
		git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
435 436 437 438 439 440

	with_user = 1;
	cl_git_fail_with(GIT_EUSER,
		git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
}

441 442 443 444 445 446 447 448 449
static int custom_remote_ssh_with_paths(
	git_remote **out,
	git_repository *repo,
	const char *name,
	const char *url,
	void *payload)
{
	int error;

450
	GIT_UNUSED(payload);
451

452
	if ((error = git_remote_create(out, repo, name, url)) < 0)
453
		return error;
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
	return 0;
}

void test_online_clone__ssh_with_paths(void)
{
	char *bad_paths[] = {
		"/bin/yes",
		"/bin/false",
	};
	char *good_paths[] = {
		"/usr/bin/git-upload-pack",
		"/usr/bin/git-receive-pack",
	};
	git_strarray arr = {
		bad_paths,
		2,
	};

473 474 475
#ifndef GIT_SSH
	clar__skip();
#endif
476
	if (!_remote_url || !_remote_user || strncmp(_remote_url, "ssh://", 5) != 0)
477 478 479
		clar__skip();

	g_options.remote_cb = custom_remote_ssh_with_paths;
480
	g_options.fetch_opts.callbacks.transport = git_transport_ssh_with_paths;
481
	g_options.fetch_opts.callbacks.credentials = cred_cb;
482
	g_options.fetch_opts.callbacks.payload = &arr;
483

484
	cl_git_fail(git_clone(&g_repo, _remote_url, "./foo", &g_options));
485

486
	arr.strings = good_paths;
487
	cl_git_pass(git_clone(&g_repo, _remote_url, "./foo", &g_options));
488
}
489

490 491 492 493 494 495 496 497 498 499 500
static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url,
				  unsigned int allowed_types, void *data)

{
	GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data);

	return git_cred_userpass_plaintext_new(cred, "foo", "bar");
}

void test_online_clone__ssh_cannot_change_username(void)
{
501 502 503
#ifndef GIT_SSH
	clar__skip();
#endif
504
	g_options.fetch_opts.callbacks.credentials = cred_foo_bar;
505 506

	cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
507
}
508

509
int ssh_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
510 511 512 513 514 515 516
{
	git_cert_hostkey *key;
	git_oid expected = {{0}}, actual = {{0}};

	GIT_UNUSED(valid);
	GIT_UNUSED(payload);

517
	cl_assert(_remote_ssh_fingerprint);
518

519
	cl_git_pass(git_oid_fromstrp(&expected, _remote_ssh_fingerprint));
520 521
	cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2, cert->cert_type);
	key = (git_cert_hostkey *) cert;
522

523 524 525 526 527
	/*
	 * We need to figure out how long our input was to check for
	 * the type. Here we abuse the fact that both hashes fit into
	 * our git_oid type.
	 */
528
	if (strlen(_remote_ssh_fingerprint) == 32 && key->type & GIT_CERT_SSH_MD5) {
529
		memcpy(&actual.id, key->hash_md5, 16);
530
	} else 	if (strlen(_remote_ssh_fingerprint) == 40 && key->type & GIT_CERT_SSH_SHA1) {
531 532 533 534
		memcpy(&actual, key->hash_sha1, 20);
	} else {
		cl_fail("Cannot find a usable SSH hash");
	}
535

536
	cl_assert(!memcmp(&expected, &actual, 20));
537

538 539
	cl_assert_equal_s("localhost", host);

540 541 542 543 544
	return GIT_EUSER;
}

void test_online_clone__ssh_cert(void)
{
545
	g_options.fetch_opts.callbacks.certificate_check = ssh_certificate_check;
546

547
	if (!_remote_ssh_fingerprint)
548 549
		cl_skip();

550 551 552
	cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options));
}

553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
static char *read_key_file(const char *path)
{
	FILE *f;
	char *buf;
	long key_length;

	if (!path || !*path)
		return NULL;

	cl_assert((f = fopen(path, "r")) != NULL);
	cl_assert(fseek(f, 0, SEEK_END) != -1);
	cl_assert((key_length = ftell(f)) != -1);
	cl_assert(fseek(f, 0, SEEK_SET) != -1);
	cl_assert((buf = malloc(key_length)) != NULL);
	cl_assert(fread(buf, key_length, 1, f) == 1);
	fclose(f);

	return buf;
}

static int ssh_memory_cred_cb(git_cred **cred, const char *url, const char *user_from_url,
		   unsigned int allowed_types, void *payload)
{
	GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);

	if (allowed_types & GIT_CREDTYPE_USERNAME)
579
		return git_cred_username_new(cred, _remote_user);
580 581 582

	if (allowed_types & GIT_CREDTYPE_SSH_KEY)
	{
583 584
		char *pubkey = read_key_file(_remote_ssh_pubkey);
		char *privkey = read_key_file(_remote_ssh_privkey);
585

586
		int ret = git_cred_ssh_key_memory_new(cred, _remote_user, pubkey, privkey, _remote_ssh_passphrase);
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603

		if (privkey)
			free(privkey);
		if (pubkey)
			free(pubkey);
		return ret;
	}

	giterr_set(GITERR_NET, "unexpected cred type");
	return -1;
}

void test_online_clone__ssh_memory_auth(void)
{
#ifndef GIT_SSH_MEMORY_CREDENTIALS
	clar__skip();
#endif
604
	if (!_remote_url || !_remote_user || !_remote_ssh_privkey || strncmp(_remote_url, "ssh://", 5) != 0)
605 606 607 608
		clar__skip();

	g_options.fetch_opts.callbacks.credentials = ssh_memory_cred_cb;

609
	cl_git_pass(git_clone(&g_repo, _remote_url, "./foo", &g_options));
610 611
}

612 613 614 615 616
void test_online_clone__url_with_no_path_returns_EINVALIDSPEC(void)
{
	cl_git_fail_with(git_clone(&g_repo, "http://github.com", "./foo", &g_options),
		GIT_EINVALIDSPEC);
}
617

618
static int fail_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
619
{
620
	GIT_UNUSED(cert);
621
	GIT_UNUSED(valid);
622
	GIT_UNUSED(host);
623 624
	GIT_UNUSED(payload);

625
	return GIT_ECERTIFICATE;
626 627 628 629
}

void test_online_clone__certificate_invalid(void)
{
630
	g_options.fetch_opts.callbacks.certificate_check = fail_certificate_check;
631

632
	cl_git_fail_with(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options),
633
		GIT_ECERTIFICATE);
634

635
#ifdef GIT_SSH
636 637
	cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options),
		GIT_ECERTIFICATE);
638
#endif
639 640
}

641
static int succeed_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
642
{
643
	GIT_UNUSED(cert);
644
	GIT_UNUSED(valid);
645 646
	GIT_UNUSED(payload);

647 648
	cl_assert_equal_s("github.com", host);

649
	return 0;
650 651 652 653
}

void test_online_clone__certificate_valid(void)
{
654
	g_options.fetch_opts.callbacks.certificate_check = succeed_certificate_check;
655

656
	cl_git_pass(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options));
657
}
658 659 660

void test_online_clone__start_with_http(void)
{
661
	g_options.fetch_opts.callbacks.certificate_check = succeed_certificate_check;
662 663 664

	cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
}
665 666 667 668

static int called_proxy_creds;
static int proxy_creds(git_cred **out, const char *url, const char *username, unsigned int allowed, void *payload)
{
669
	GIT_UNUSED(url);
670
	GIT_UNUSED(username);
671 672
	GIT_UNUSED(allowed);
	GIT_UNUSED(payload);
673 674 675 676 677 678 679 680 681 682

	called_proxy_creds = 1;
	return git_cred_userpass_plaintext_new(out, _remote_proxy_user, _remote_proxy_pass);
}

void test_online_clone__proxy_credentials_request(void)
{
	if (!_remote_proxy_url || !_remote_proxy_user || !_remote_proxy_pass)
		cl_skip();

683
	g_options.fetch_opts.proxy_opts.type = GIT_PROXY_SPECIFIED;
684 685 686 687 688 689 690 691 692 693 694 695
	g_options.fetch_opts.proxy_opts.url = _remote_proxy_url;
	g_options.fetch_opts.proxy_opts.credentials = proxy_creds;
	called_proxy_creds = 0;
	cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
	cl_assert(called_proxy_creds);
}

void test_online_clone__proxy_credentials_in_url(void)
{
	if (!_remote_proxy_url)
		cl_skip();

696
	g_options.fetch_opts.proxy_opts.type = GIT_PROXY_SPECIFIED;
697 698 699 700 701
	g_options.fetch_opts.proxy_opts.url = _remote_proxy_url;
	called_proxy_creds = 0;
	cl_git_pass(git_clone(&g_repo, "http://github.com/libgit2/TestGitRepository", "./foo", &g_options));
	cl_assert(called_proxy_creds == 0);
}