]> git.proxmox.com Git - libgit2.git/blob - tests/online/fetch.c
Added a test, that fails for #2133
[libgit2.git] / tests / online / fetch.c
1 #include "clar_libgit2.h"
2
3 static git_repository *_repo;
4 static int counter;
5
6 void test_online_fetch__initialize(void)
7 {
8 cl_git_pass(git_repository_init(&_repo, "./fetch", 0));
9 }
10
11 void test_online_fetch__cleanup(void)
12 {
13 git_repository_free(_repo);
14 _repo = NULL;
15
16 cl_fixture_cleanup("./fetch");
17 }
18
19 static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
20 {
21 GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data);
22
23 ++counter;
24
25 return 0;
26 }
27
28 static int progress(const git_transfer_progress *stats, void *payload)
29 {
30 size_t *bytes_received = (size_t *)payload;
31 *bytes_received = stats->received_bytes;
32 return 0;
33 }
34
35 static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
36 {
37 git_remote *remote;
38 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
39 size_t bytes_received = 0;
40
41 callbacks.transfer_progress = progress;
42 callbacks.update_tips = update_tips;
43 callbacks.payload = &bytes_received;
44 counter = 0;
45
46 cl_git_pass(git_remote_create(&remote, _repo, "test", url));
47 git_remote_set_callbacks(remote, &callbacks);
48 git_remote_set_autotag(remote, flag);
49 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
50 cl_git_pass(git_remote_download(remote));
51 cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
52 git_remote_disconnect(remote);
53 cl_assert_equal_i(counter, n);
54 cl_assert(bytes_received > 0);
55
56 git_remote_free(remote);
57 }
58
59 void test_online_fetch__default_git(void)
60 {
61 do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
62 }
63
64 void test_online_fetch__default_http(void)
65 {
66 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
67 }
68
69 void test_online_fetch__default_https(void)
70 {
71 do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
72 }
73
74 void test_online_fetch__no_tags_git(void)
75 {
76 do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
77 }
78
79 void test_online_fetch__no_tags_http(void)
80 {
81 do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
82 }
83
84 void test_online_fetch__fetch_twice(void)
85 {
86 git_remote *remote;
87 cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
88 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
89 cl_git_pass(git_remote_download(remote));
90 git_remote_disconnect(remote);
91
92 git_remote_connect(remote, GIT_DIRECTION_FETCH);
93 cl_git_pass(git_remote_download(remote));
94 git_remote_disconnect(remote);
95
96 git_remote_free(remote);
97 }
98
99 static int transferProgressCallback(const git_transfer_progress *stats, void *payload)
100 {
101 bool *invoked = (bool *)payload;
102
103 GIT_UNUSED(stats);
104 *invoked = true;
105 return 0;
106 }
107
108 void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
109 {
110 git_repository *_repository;
111 bool invoked = false;
112 git_remote *remote;
113 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
114 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
115 opts.bare = true;
116
117 cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git",
118 "./fetch/lg2", &opts));
119 git_repository_free(_repository);
120
121 cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));
122
123 cl_git_pass(git_remote_load(&remote, _repository, "origin"));
124 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
125
126 cl_assert_equal_i(false, invoked);
127
128 callbacks.transfer_progress = &transferProgressCallback;
129 callbacks.payload = &invoked;
130 git_remote_set_callbacks(remote, &callbacks);
131 cl_git_pass(git_remote_download(remote));
132
133 cl_assert_equal_i(false, invoked);
134
135 cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
136 git_remote_disconnect(remote);
137
138 git_remote_free(remote);
139 git_repository_free(_repository);
140 }
141
142 static int cancel_at_half(const git_transfer_progress *stats, void *payload)
143 {
144 GIT_UNUSED(payload);
145
146 if (stats->received_objects > (stats->total_objects/2))
147 return -4321;
148 return 0;
149 }
150
151 void test_online_fetch__can_cancel(void)
152 {
153 git_remote *remote;
154 size_t bytes_received = 0;
155 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
156
157 cl_git_pass(git_remote_create(&remote, _repo, "test",
158 "http://github.com/libgit2/TestGitRepository.git"));
159
160 callbacks.transfer_progress = cancel_at_half;
161 callbacks.payload = &bytes_received;
162 git_remote_set_callbacks(remote, &callbacks);
163
164 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
165 cl_git_fail_with(git_remote_download(remote), -4321);
166 git_remote_disconnect(remote);
167 git_remote_free(remote);
168 }
169
170 void test_online_fetch__ls_disconnected(void)
171 {
172 const git_remote_head **refs;
173 size_t refs_len_before, refs_len_after;
174 git_remote *remote;
175
176 cl_git_pass(git_remote_create(&remote, _repo, "test",
177 "http://github.com/libgit2/TestGitRepository.git"));
178 cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
179 cl_git_pass(git_remote_ls(&refs, &refs_len_before, remote));
180 git_remote_disconnect(remote);
181 cl_git_pass(git_remote_ls(&refs, &refs_len_after, remote));
182
183 cl_assert_equal_i(refs_len_before, refs_len_after);
184
185 git_remote_free(remote);
186 }