fetch.c 7.28 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"

static git_repository *_repo;
static int counter;

6 7 8 9 10
static char *_remote_proxy_scheme = NULL;
static char *_remote_proxy_host = NULL;
static char *_remote_proxy_user = NULL;
static char *_remote_proxy_pass = NULL;

11
void test_online_fetch__initialize(void)
12 13
{
	cl_git_pass(git_repository_init(&_repo, "./fetch", 0));
14 15 16 17 18

    _remote_proxy_scheme = cl_getenv("GITTEST_REMOTE_PROXY_SCHEME");
    _remote_proxy_host = cl_getenv("GITTEST_REMOTE_PROXY_HOST");
    _remote_proxy_user = cl_getenv("GITTEST_REMOTE_PROXY_USER");
    _remote_proxy_pass = cl_getenv("GITTEST_REMOTE_PROXY_PASS");
19 20
}

21
void test_online_fetch__cleanup(void)
22 23
{
	git_repository_free(_repo);
24 25
	_repo = NULL;

26
	cl_fixture_cleanup("./fetch");
27 28 29 30 31

    git__free(_remote_proxy_scheme);
    git__free(_remote_proxy_host);
    git__free(_remote_proxy_user);
    git__free(_remote_proxy_pass);
32 33 34 35
}

static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
36
	GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data);
37 38 39 40 41 42

	++counter;

	return 0;
}

43
static int progress(const git_indexer_progress *stats, void *payload)
44
{
45
	size_t *bytes_received = (size_t *)payload;
46
	*bytes_received = stats->received_bytes;
47
	return 0;
48 49
}

nulltoken committed
50
static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
51 52
{
	git_remote *remote;
53
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
54
	size_t bytes_received = 0;
55

56 57 58
	options.callbacks.transfer_progress = progress;
	options.callbacks.update_tips = update_tips;
	options.callbacks.payload = &bytes_received;
59
	options.download_tags = flag;
60 61
	counter = 0;

Ben Straub committed
62
	cl_git_pass(git_remote_create(&remote, _repo, "test", url));
63
	cl_git_pass(git_remote_fetch(remote, NULL, &options, NULL));
64
	cl_assert_equal_i(counter, n);
65
	cl_assert(bytes_received > 0);
nulltoken committed
66 67

	git_remote_free(remote);
68 69
}

70
void test_online_fetch__default_git(void)
71
{
72
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
73 74
}

75
void test_online_fetch__default_http(void)
76
{
77
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
78 79
}

80 81
void test_online_fetch__default_https(void)
{
82
	do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
83 84
}

85
void test_online_fetch__no_tags_git(void)
86 87 88 89
{
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}

90
void test_online_fetch__no_tags_http(void)
91 92 93
{
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
nulltoken committed
94

95 96 97 98
void test_online_fetch__fetch_twice(void)
{
	git_remote *remote;
	cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
99
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
100
	cl_git_pass(git_remote_download(remote, NULL, NULL));
101
    	git_remote_disconnect(remote);
102

103
	git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL);
104
	cl_git_pass(git_remote_download(remote, NULL, NULL));
105
	git_remote_disconnect(remote);
106

107 108 109
	git_remote_free(remote);
}

110
static int transferProgressCallback(const git_indexer_progress *stats, void *payload)
nulltoken committed
111 112
{
	bool *invoked = (bool *)payload;
nulltoken committed
113 114

	GIT_UNUSED(stats);
nulltoken committed
115
	*invoked = true;
116
	return 0;
nulltoken committed
117 118
}

119
void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
nulltoken committed
120 121 122
{
	git_repository *_repository;
	bool invoked = false;
123
	git_remote *remote;
124
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
125 126
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
	opts.bare = true;
nulltoken committed
127

128 129
	cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git",
				"./fetch/lg2", &opts));
nulltoken committed
130 131 132 133
	git_repository_free(_repository);

	cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));

134
	cl_git_pass(git_remote_lookup(&remote, _repository, "origin"));
135
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
nulltoken committed
136 137 138

	cl_assert_equal_i(false, invoked);

139 140 141
	options.callbacks.transfer_progress = &transferProgressCallback;
	options.callbacks.payload = &invoked;
	cl_git_pass(git_remote_download(remote, NULL, &options));
nulltoken committed
142 143 144

	cl_assert_equal_i(false, invoked);

145
	cl_git_pass(git_remote_update_tips(remote, &options.callbacks, 1, options.download_tags, NULL));
nulltoken committed
146 147 148 149 150
	git_remote_disconnect(remote);

	git_remote_free(remote);
	git_repository_free(_repository);
}
151

152
static int cancel_at_half(const git_indexer_progress *stats, void *payload)
153 154 155 156
{
	GIT_UNUSED(payload);

	if (stats->received_objects > (stats->total_objects/2))
157
		return -4321;
158 159 160 161 162 163 164
	return 0;
}

void test_online_fetch__can_cancel(void)
{
	git_remote *remote;
	size_t bytes_received = 0;
165
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
166 167 168

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
169

170 171
	options.callbacks.transfer_progress = cancel_at_half;
	options.callbacks.payload = &bytes_received;
172

173
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
174
	cl_git_fail_with(git_remote_download(remote, NULL, &options), -4321);
175 176 177
	git_remote_disconnect(remote);
	git_remote_free(remote);
}
178 179 180

void test_online_fetch__ls_disconnected(void)
{
181 182
	const git_remote_head **refs;
	size_t refs_len_before, refs_len_after;
183 184 185 186
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
187
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
188
	cl_git_pass(git_remote_ls(&refs, &refs_len_before, remote));
189
	git_remote_disconnect(remote);
190
	cl_git_pass(git_remote_ls(&refs, &refs_len_after, remote));
191

192
	cl_assert_equal_i(refs_len_before, refs_len_after);
193 194 195

	git_remote_free(remote);
}
196 197 198 199 200 201 202 203 204

void test_online_fetch__remote_symrefs(void)
{
	const git_remote_head **refs;
	size_t refs_len;
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
205
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
206 207 208 209 210 211 212 213
	git_remote_disconnect(remote);
	cl_git_pass(git_remote_ls(&refs, &refs_len, remote));

	cl_assert_equal_s("HEAD", refs[0]->name);
	cl_assert_equal_s("refs/heads/master", refs[0]->symref_target);

	git_remote_free(remote);
}
214 215 216 217 218 219

void test_online_fetch__twice(void)
{
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test", "http://github.com/libgit2/TestGitRepository.git"));
220 221
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
222 223 224

	git_remote_free(remote);
}
225 226 227 228

void test_online_fetch__proxy(void)
{
    git_remote *remote;
229
    git_str url = GIT_STR_INIT;
230 231 232 233 234
    git_fetch_options fetch_opts;

    if (!_remote_proxy_host || !_remote_proxy_user || !_remote_proxy_pass)
        cl_skip();

235
    cl_git_pass(git_str_printf(&url, "%s://%s:%s@%s/",
236 237 238 239 240 241 242 243 244 245 246 247
        _remote_proxy_scheme ? _remote_proxy_scheme : "http",
        _remote_proxy_user, _remote_proxy_pass, _remote_proxy_host));

    cl_git_pass(git_fetch_options_init(&fetch_opts, GIT_FETCH_OPTIONS_VERSION));
    fetch_opts.proxy_opts.type = GIT_PROXY_SPECIFIED;
    fetch_opts.proxy_opts.url = url.ptr;

    cl_git_pass(git_remote_create(&remote, _repo, "test", "https://github.com/libgit2/TestGitRepository.git"));
    cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, &fetch_opts.proxy_opts, NULL));
    cl_git_pass(git_remote_fetch(remote, NULL, &fetch_opts, NULL));

    git_remote_free(remote);
248
    git_str_dispose(&url);
249
}