clone.c 15.3 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
void test_online_clone__initialize(void)
21
{
22
	git_checkout_options dummy_opts = GIT_CHECKOUT_OPTIONS_INIT;
23
	git_remote_callbacks dummy_callbacks = GIT_REMOTE_CALLBACKS_INIT;
24

25
	g_repo = NULL;
26 27 28

	memset(&g_options, 0, sizeof(git_clone_options));
	g_options.version = GIT_CLONE_OPTIONS_VERSION;
29 30
	g_options.checkout_opts = dummy_opts;
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
31
	g_options.remote_callbacks = dummy_callbacks;
32 33
}

34
void test_online_clone__cleanup(void)
35
{
36
	if (g_repo) {
37
		git_repository_free(g_repo);
38 39
		g_repo = NULL;
	}
40
	cl_fixture_cleanup("./foo");
41 42
}

43
void test_online_clone__network_full(void)
44 45 46
{
	git_remote *origin;

47
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
48
	cl_assert(!git_repository_is_bare(g_repo));
49
	cl_git_pass(git_remote_lookup(&origin, g_repo, "origin"));
nulltoken committed
50

51 52
	cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, origin->download_tags);

nulltoken committed
53
	git_remote_free(origin);
54 55
}

56
void test_online_clone__network_bare(void)
57 58 59
{
	git_remote *origin;

60
	g_options.bare = true;
61

62
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
63
	cl_assert(git_repository_is_bare(g_repo));
64
	cl_git_pass(git_remote_lookup(&origin, g_repo, "origin"));
nulltoken committed
65 66

	git_remote_free(origin);
67 68
}

69
void test_online_clone__empty_repository(void)
70 71 72
{
	git_reference *head;

73
	cl_git_pass(git_clone(&g_repo, LIVE_EMPTYREPO_URL, "./foo", &g_options));
74 75

	cl_assert_equal_i(true, git_repository_is_empty(g_repo));
76
	cl_assert_equal_i(true, git_repository_head_unborn(g_repo));
77 78 79

	cl_git_pass(git_reference_lookup(&head, g_repo, GIT_HEAD_FILE));
	cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
80
	cl_assert_equal_s("refs/heads/master", git_reference_symbolic_target(head));
81 82 83

	git_reference_free(head);
}
84

85
static void checkout_progress(const char *path, size_t cur, size_t tot, void *payload)
86 87
{
	bool *was_called = (bool*)payload;
Ben Straub committed
88
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
89 90 91
	(*was_called) = true;
}

92
static int fetch_progress(const git_transfer_progress *stats, void *payload)
93 94
{
	bool *was_called = (bool*)payload;
Ben Straub committed
95
	GIT_UNUSED(stats);
96
	(*was_called) = true;
97
	return 0;
98 99
}

100
void test_online_clone__can_checkout_a_cloned_repo(void)
101 102
{
	git_buf path = GIT_BUF_INIT;
103
	git_reference *head;
104 105
	bool checkout_progress_cb_was_called = false,
		  fetch_progress_cb_was_called = false;
106

107
	g_options.checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
108 109
	g_options.checkout_opts.progress_cb = &checkout_progress;
	g_options.checkout_opts.progress_payload = &checkout_progress_cb_was_called;
110 111
	g_options.remote_callbacks.transfer_progress = &fetch_progress;
	g_options.remote_callbacks.payload = &fetch_progress_cb_was_called;
112

113
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
114 115 116

	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)));
117 118 119

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

122 123
	cl_assert_equal_i(true, checkout_progress_cb_was_called);
	cl_assert_equal_i(true, fetch_progress_cb_was_called);
124

nulltoken committed
125 126
	git_reference_free(head);
	git_buf_free(&path);
127
}
Ben Straub committed
128

129 130
static int remote_mirror_cb(git_remote **out, git_repository *repo,
			    const char *name, const char *url, void *payload)
131
{
132
	int error;
133
	git_remote *remote;
134
	git_remote_callbacks *callbacks = (git_remote_callbacks *) payload;
135 136


137 138
	if ((error = git_remote_create(&remote, repo, name, url)) < 0)
		return error;
139

140 141 142 143
	if ((error = git_remote_set_callbacks(remote, callbacks)) < 0) {
		git_remote_free(remote);
		return error;
	}
144

145
	git_remote_clear_refspecs(remote);
146

147 148 149 150
	if ((error = git_remote_add_fetch(remote, "+refs/*:refs/*")) < 0) {
		git_remote_free(remote);
		return error;
	}
151

152 153
	*out = remote;
	return 0;
154 155
}

156 157
void test_online_clone__clone_mirror(void)
{
158
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
159 160 161 162 163 164 165 166
	git_reference *head;
	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;

	bool fetch_progress_cb_was_called = false;

	callbacks.transfer_progress = &fetch_progress;
	callbacks.payload = &fetch_progress_cb_was_called;

167 168 169
	opts.bare = true;
	opts.remote_cb = remote_mirror_cb;
	opts.remote_cb_payload = &callbacks;
170

171
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo.git", &opts));
172 173 174 175 176 177 178 179

	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);
180 181 182
	git_repository_free(g_repo);
	g_repo = NULL;

183 184 185
	cl_fixture_cleanup("./foo.git");
}

Ben Straub committed
186 187 188 189 190 191 192 193
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;
}

194
void test_online_clone__custom_remote_callbacks(void)
Ben Straub committed
195 196 197
{
	int callcount = 0;

198 199
	g_options.remote_callbacks.update_tips = update_tips;
	g_options.remote_callbacks.payload = &callcount;
Ben Straub committed
200 201 202 203 204

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

205 206 207 208 209 210 211
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
212 213
	GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url);
	GIT_UNUSED(allowed_types); GIT_UNUSED(data);
Ben Straub committed
214
	return -172;
215 216
}

Ben Straub committed
217
void test_online_clone__cred_callback_failure_return_code_is_tunnelled(void)
218 219
{
	const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
220
	const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
221

Vicent Marti committed
222 223
	if (!remote_url || !remote_user)
		clar__skip();
224

225 226
	g_options.remote_callbacks.credentials = cred_failure_cb;

227
	cl_git_fail_with(-172, git_clone(&g_repo, remote_url, "./foo", &g_options));
228 229
}

230 231 232 233 234 235 236
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);

237 238 239
	if (allowed_types == GIT_CREDTYPE_USERNAME)
		return git_cred_username_new(cred, "foo");

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	(*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)
{
	const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
	const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
	size_t counter = 0;

	if (!remote_url || !remote_user)
		clar__skip();

	g_options.remote_callbacks.credentials = cred_count_calls_cb;
	g_options.remote_callbacks.payload = &counter;

	cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, remote_url, "./foo", &g_options));
	cl_assert_equal_i(3, counter);
}

264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
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);
}

281
void test_online_clone__credentials(void)
Ben Straub committed
282
{
283 284 285
	/* Remote URL environment variable must be set.
	 * User and password are optional.
	 */
Ben Straub committed
286
	const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
287
	git_cred_userpass_payload user_pass = {
Ben Straub committed
288 289 290 291 292 293
		cl_getenv("GITTEST_REMOTE_USER"),
		cl_getenv("GITTEST_REMOTE_PASS")
	};

	if (!remote_url) return;

294 295 296 297 298 299
	if (cl_getenv("GITTEST_REMOTE_DEFAULT")) {
		g_options.remote_callbacks.credentials = cred_default;
	} else {
		g_options.remote_callbacks.credentials = git_cred_userpass;
		g_options.remote_callbacks.payload = &user_pass;
	}
Ben Straub committed
300 301

	cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
302 303 304 305 306 307 308 309 310 311
	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"
	};

312 313
	g_options.remote_callbacks.credentials = git_cred_userpass;
	g_options.remote_callbacks.payload = &user_pass;
314 315 316 317

	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");
318

319 320
	/* User and pass from URL */
	user_pass.password = "wrong";
321 322 323
	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");
324 325 326 327 328 329

	/* 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
330
}
331 332 333 334 335 336

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

	if (stats->received_objects > (stats->total_objects/2))
337
		return 4321;
338 339 340 341 342
	return 0;
}

void test_online_clone__can_cancel(void)
{
343
	g_options.remote_callbacks.transfer_progress = cancel_at_half;
344

345 346
	cl_git_fail_with(
		git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options), 4321);
347
}
348

349 350 351 352 353 354 355 356 357 358
static int cred_cb(git_cred **cred, const char *url, const char *user_from_url,
		   unsigned int allowed_types, void *payload)
{
	const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");
	const char *pubkey = cl_getenv("GITTEST_REMOTE_SSH_PUBKEY");
	const char *privkey = cl_getenv("GITTEST_REMOTE_SSH_KEY");
	const char *passphrase = cl_getenv("GITTEST_REMOTE_SSH_PASSPHRASE");

	GIT_UNUSED(url); GIT_UNUSED(user_from_url); GIT_UNUSED(payload);

359 360 361
	if (allowed_types & GIT_CREDTYPE_USERNAME)
		return git_cred_username_new(cred, remote_user);

362 363 364 365 366 367
	if (allowed_types & GIT_CREDTYPE_SSH_KEY)
		return git_cred_ssh_key_new(cred, remote_user, pubkey, privkey, passphrase);

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

369 370 371
static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
				  unsigned int allowed_types, void *data)
{
372
	int *with_user = (int *) data;
373
	GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
374

375 376 377 378
	if (!*with_user)
		cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
	else
		cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
379

380 381
	return GIT_EUSER;
}
382

383 384
void test_online_clone__ssh_auth_methods(void)
{
385 386
	int with_user;

387 388 389
#ifndef GIT_SSH
	clar__skip();
#endif
390
	g_options.remote_callbacks.credentials = check_ssh_auth_methods;
391
	g_options.remote_callbacks.payload = &with_user;
392

393
	with_user = 0;
394 395
	cl_git_fail_with(GIT_EUSER,
		git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
396 397 398 399 400 401

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

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static int custom_remote_ssh_with_paths(
	git_remote **out,
	git_repository *repo,
	const char *name,
	const char *url,
	void *payload)
{
	int error;
	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;

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

	if ((error = git_remote_set_transport(*out, git_transport_ssh_with_paths, payload)) < 0)
		return error;
417

418 419
	callbacks.credentials = cred_cb;
	git_remote_set_callbacks(*out, &callbacks);
420

421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	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,
	};

	const char *remote_url = cl_getenv("GITTEST_REMOTE_URL");
	const char *remote_user = cl_getenv("GITTEST_REMOTE_USER");

442 443 444
#ifndef GIT_SSH
	clar__skip();
#endif
445
	if (!remote_url || !remote_user || strncmp(remote_url, "ssh://", 5) != 0)
446 447 448 449
		clar__skip();

	g_options.remote_cb = custom_remote_ssh_with_paths;
	g_options.remote_cb_payload = &arr;
450

451
	cl_git_fail(git_clone(&g_repo, remote_url, "./foo", &g_options));
452

453 454 455
	arr.strings = good_paths;
	cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options));
}
456

457 458 459 460 461 462 463 464 465 466 467
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)
{
468 469 470
#ifndef GIT_SSH
	clar__skip();
#endif
471 472 473
	g_options.remote_callbacks.credentials = cred_foo_bar;

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

476
int ssh_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
477 478 479 480 481 482 483 484 485
{
	git_cert_hostkey *key;
	git_oid expected = {{0}}, actual = {{0}};
	const char *expected_str;

	GIT_UNUSED(valid);
	GIT_UNUSED(payload);

	expected_str = cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT");
486
	cl_assert(expected_str);
487

488 489 490
	cl_git_pass(git_oid_fromstrp(&expected, expected_str));
	cl_assert_equal_i(GIT_CERT_HOSTKEY_LIBSSH2, cert->cert_type);
	key = (git_cert_hostkey *) cert;
491

492 493 494 495 496 497 498 499 500 501 502 503
	/*
	 * 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.
	 */
	if (strlen(expected_str) == 32 && key->type & GIT_CERT_SSH_MD5) {
		memcpy(&actual.id, key->hash_md5, 16);
	} else 	if (strlen(expected_str) == 40 && key->type & GIT_CERT_SSH_SHA1) {
		memcpy(&actual, key->hash_sha1, 20);
	} else {
		cl_fail("Cannot find a usable SSH hash");
	}
504

505
	cl_assert(!memcmp(&expected, &actual, 20));
506

507 508
	cl_assert_equal_s("localhost", host);

509 510 511 512 513 514 515
	return GIT_EUSER;
}

void test_online_clone__ssh_cert(void)
{
	g_options.remote_callbacks.certificate_check = ssh_certificate_check;

516 517 518
	if (!cl_getenv("GITTEST_REMOTE_SSH_FINGERPRINT"))
		cl_skip();

519 520 521
	cl_git_fail_with(GIT_EUSER, git_clone(&g_repo, "ssh://localhost/foo", "./foo", &g_options));
}

522 523 524 525 526
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);
}
527

528
static int fail_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
529
{
530
	GIT_UNUSED(cert);
531
	GIT_UNUSED(valid);
532
	GIT_UNUSED(host);
533 534
	GIT_UNUSED(payload);

535
	return GIT_ECERTIFICATE;
536 537 538 539 540 541
}

void test_online_clone__certificate_invalid(void)
{
	g_options.remote_callbacks.certificate_check = fail_certificate_check;

542
	cl_git_fail_with(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options),
543
		GIT_ECERTIFICATE);
544

545
#ifdef GIT_SSH
546 547
	cl_git_fail_with(git_clone(&g_repo, "ssh://github.com/libgit2/TestGitRepository", "./foo", &g_options),
		GIT_ECERTIFICATE);
548
#endif
549 550
}

551
static int succeed_certificate_check(git_cert *cert, int valid, const char *host, void *payload)
552
{
553
	GIT_UNUSED(cert);
554
	GIT_UNUSED(valid);
555 556
	GIT_UNUSED(payload);

557 558
	cl_assert_equal_s("github.com", host);

559
	return 0;
560 561 562 563 564 565
}

void test_online_clone__certificate_valid(void)
{
	g_options.remote_callbacks.certificate_check = succeed_certificate_check;

566
	cl_git_pass(git_clone(&g_repo, "https://github.com/libgit2/TestGitRepository", "./foo", &g_options));
567
}