]> git.proxmox.com Git - libgit2.git/blob - tests/network/remote/remotes.c
Merge remote-tracking branch 'upstream/master' into cmn/host-cert-info
[libgit2.git] / tests / network / remote / remotes.c
1 #include "clar_libgit2.h"
2 #include "buffer.h"
3 #include "refspec.h"
4 #include "remote.h"
5
6 static git_remote *_remote;
7 static git_repository *_repo;
8 static const git_refspec *_refspec;
9
10 void test_network_remote_remotes__initialize(void)
11 {
12 _repo = cl_git_sandbox_init("testrepo.git");
13
14 cl_git_pass(git_remote_load(&_remote, _repo, "test"));
15
16 _refspec = git_remote_get_refspec(_remote, 0);
17 cl_assert(_refspec != NULL);
18 }
19
20 void test_network_remote_remotes__cleanup(void)
21 {
22 git_remote_free(_remote);
23 _remote = NULL;
24
25 cl_git_sandbox_cleanup();
26 }
27
28 void test_network_remote_remotes__parsing(void)
29 {
30 git_remote *_remote2 = NULL;
31
32 cl_assert_equal_s(git_remote_name(_remote), "test");
33 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
34 cl_assert(git_remote_pushurl(_remote) == NULL);
35
36 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH),
37 "git://github.com/libgit2/libgit2");
38 cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH),
39 "git://github.com/libgit2/libgit2");
40
41 cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl"));
42 cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl");
43 cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2");
44 cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2");
45
46 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH),
47 "git://github.com/libgit2/fetchlibgit2");
48 cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH),
49 "git://github.com/libgit2/pushlibgit2");
50
51 git_remote_free(_remote2);
52 }
53
54 void test_network_remote_remotes__pushurl(void)
55 {
56 cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2"));
57 cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2");
58
59 cl_git_pass(git_remote_set_pushurl(_remote, NULL));
60 cl_assert(git_remote_pushurl(_remote) == NULL);
61 }
62
63 void test_network_remote_remotes__error_when_not_found(void)
64 {
65 git_remote *r;
66 cl_git_fail_with(git_remote_load(&r, _repo, "does-not-exist"), GIT_ENOTFOUND);
67
68 cl_assert(giterr_last() != NULL);
69 cl_assert(giterr_last()->klass == GITERR_CONFIG);
70 }
71
72 void test_network_remote_remotes__error_when_no_push_available(void)
73 {
74 git_remote *r;
75 git_push *p;
76
77 cl_git_pass(git_remote_create_anonymous(&r, _repo, cl_fixture("testrepo.git"), NULL));
78
79 cl_git_pass(git_remote_set_transport(r, git_transport_local, NULL));
80
81 cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH));
82
83 /* Make sure that push is really not available */
84 r->transport->push = NULL;
85
86 cl_git_pass(git_push_new(&p, r));
87 cl_git_pass(git_push_add_refspec(p, "refs/heads/master"));
88 cl_git_fail_with(git_push_finish(p), GIT_ERROR);
89
90 git_push_free(p);
91 git_remote_free(r);
92 }
93
94 void test_network_remote_remotes__supported_urls(void)
95 {
96 int ssh_supported = 0, https_supported = 0;
97
98 #ifdef GIT_SSH
99 ssh_supported = 1;
100 #endif
101
102 #if defined(GIT_SSL) || defined(GIT_WINHTTP)
103 https_supported = 1;
104 #endif
105
106 cl_assert(git_remote_supported_url("git://github.com/libgit2/libgit2"));
107 cl_assert(git_remote_supported_url("http://github.com/libgit2/libgit2"));
108
109 cl_assert_equal_i(ssh_supported, git_remote_supported_url("git@github.com:libgit2/libgit2.git"));
110 cl_assert_equal_i(ssh_supported, git_remote_supported_url("ssh://git@github.com/libgit2/libgit2.git"));
111 cl_assert_equal_i(https_supported, git_remote_supported_url("https://github.com/libgit2/libgit2.git"));
112 }
113
114 void test_network_remote_remotes__refspec_parsing(void)
115 {
116 cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*");
117 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*");
118 }
119
120 void test_network_remote_remotes__add_fetchspec(void)
121 {
122 size_t size;
123
124 size = git_remote_refspec_count(_remote);
125
126 cl_git_pass(git_remote_add_fetch(_remote, "refs/*:refs/*"));
127
128 size++;
129 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
130
131 _refspec = git_remote_get_refspec(_remote, size - 1);
132 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
133 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
134 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
135 cl_assert_equal_b(_refspec->push, false);
136 }
137
138 void test_network_remote_remotes__dup(void)
139 {
140 git_strarray array;
141 git_remote *dup;
142
143 cl_git_pass(git_remote_dup(&dup, _remote));
144
145 cl_assert_equal_s(git_remote_name(dup), git_remote_name(_remote));
146 cl_assert_equal_s(git_remote_url(dup), git_remote_url(_remote));
147 cl_assert_equal_s(git_remote_pushurl(dup), git_remote_pushurl(_remote));
148
149 cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote));
150 cl_assert_equal_i(1, (int)array.count);
151 cl_assert_equal_s("+refs/heads/*:refs/remotes/test/*", array.strings[0]);
152 git_strarray_free(&array);
153
154 cl_git_pass(git_remote_get_push_refspecs(&array, _remote));
155 cl_assert_equal_i(0, (int)array.count);
156 git_strarray_free(&array);
157
158 git_remote_free(dup);
159 }
160
161 void test_network_remote_remotes__add_pushspec(void)
162 {
163 size_t size;
164
165 size = git_remote_refspec_count(_remote);
166
167 cl_git_pass(git_remote_add_push(_remote, "refs/*:refs/*"));
168 size++;
169 cl_assert_equal_i((int)size, (int)git_remote_refspec_count(_remote));
170
171 _refspec = git_remote_get_refspec(_remote, size - 1);
172 cl_assert_equal_s(git_refspec_src(_refspec), "refs/*");
173 cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*");
174 cl_assert_equal_s(git_refspec_string(_refspec), "refs/*:refs/*");
175
176 cl_assert_equal_b(_refspec->push, true);
177 }
178
179 void test_network_remote_remotes__save(void)
180 {
181 git_strarray array;
182 const char *fetch_refspec1 = "refs/heads/ns1/*:refs/remotes/upstream/ns1/*";
183 const char *fetch_refspec2 = "refs/heads/ns2/*:refs/remotes/upstream/ns2/*";
184 const char *push_refspec1 = "refs/heads/ns1/*:refs/heads/ns1/*";
185 const char *push_refspec2 = "refs/heads/ns2/*:refs/heads/ns2/*";
186
187 git_remote_free(_remote);
188 _remote = NULL;
189
190 /* Set up the remote and save it to config */
191 cl_git_pass(git_remote_create(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2"));
192 git_remote_clear_refspecs(_remote);
193
194 cl_git_pass(git_remote_add_fetch(_remote, fetch_refspec1));
195 cl_git_pass(git_remote_add_fetch(_remote, fetch_refspec2));
196 cl_git_pass(git_remote_add_push(_remote, push_refspec1));
197 cl_git_pass(git_remote_add_push(_remote, push_refspec2));
198 cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push"));
199 cl_git_pass(git_remote_save(_remote));
200 git_remote_free(_remote);
201 _remote = NULL;
202
203 /* Load it from config and make sure everything matches */
204 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
205
206 cl_git_pass(git_remote_get_fetch_refspecs(&array, _remote));
207 cl_assert_equal_i(2, (int)array.count);
208 cl_assert_equal_s(fetch_refspec1, array.strings[0]);
209 cl_assert_equal_s(fetch_refspec2, array.strings[1]);
210 git_strarray_free(&array);
211
212 cl_git_pass(git_remote_get_push_refspecs(&array, _remote));
213 cl_assert_equal_i(2, (int)array.count);
214 cl_assert_equal_s(push_refspec1, array.strings[0]);
215 cl_assert_equal_s(push_refspec2, array.strings[1]);
216 git_strarray_free(&array);
217
218 cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
219 cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push");
220
221 /* remove the pushurl again and see if we can save that too */
222 cl_git_pass(git_remote_set_pushurl(_remote, NULL));
223 cl_git_pass(git_remote_save(_remote));
224 git_remote_free(_remote);
225 _remote = NULL;
226
227 cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
228 cl_assert(git_remote_pushurl(_remote) == NULL);
229 }
230
231 void test_network_remote_remotes__fnmatch(void)
232 {
233 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master"));
234 cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch"));
235 }
236
237 void test_network_remote_remotes__transform(void)
238 {
239 git_buf ref = GIT_BUF_INIT;
240
241 cl_git_pass(git_refspec_transform(&ref, _refspec, "refs/heads/master"));
242 cl_assert_equal_s(ref.ptr, "refs/remotes/test/master");
243 git_buf_free(&ref);
244 }
245
246 void test_network_remote_remotes__transform_destination_to_source(void)
247 {
248 git_buf ref = GIT_BUF_INIT;
249
250 cl_git_pass(git_refspec_rtransform(&ref, _refspec, "refs/remotes/test/master"));
251 cl_assert_equal_s(ref.ptr, "refs/heads/master");
252 git_buf_free(&ref);
253 }
254
255 void test_network_remote_remotes__missing_refspecs(void)
256 {
257 git_config *cfg;
258
259 git_remote_free(_remote);
260 _remote = NULL;
261
262 cl_git_pass(git_repository_config(&cfg, _repo));
263 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
264 cl_git_pass(git_remote_load(&_remote, _repo, "specless"));
265
266 git_config_free(cfg);
267 }
268
269 void test_network_remote_remotes__list(void)
270 {
271 git_strarray list;
272 git_config *cfg;
273
274 cl_git_pass(git_remote_list(&list, _repo));
275 cl_assert(list.count == 5);
276 git_strarray_free(&list);
277
278 cl_git_pass(git_repository_config(&cfg, _repo));
279
280 /* Create a new remote */
281 cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com"));
282
283 /* Update a remote (previously without any url/pushurl entry) */
284 cl_git_pass(git_config_set_string(cfg, "remote.no-remote-url.pushurl", "http://example.com"));
285
286 cl_git_pass(git_remote_list(&list, _repo));
287 cl_assert(list.count == 7);
288 git_strarray_free(&list);
289
290 git_config_free(cfg);
291 }
292
293 void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void)
294 {
295 git_remote_free(_remote);
296 _remote = NULL;
297
298 cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago"));
299 }
300
301 void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void)
302 {
303 git_remote_free(_remote);
304 _remote = NULL;
305
306 cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id"));
307 }
308
309 /*
310 * $ git remote add addtest http://github.com/libgit2/libgit2
311 *
312 * $ cat .git/config
313 * [...]
314 * [remote "addtest"]
315 * url = http://github.com/libgit2/libgit2
316 * fetch = +refs/heads/\*:refs/remotes/addtest/\*
317 */
318 void test_network_remote_remotes__add(void)
319 {
320 git_remote_free(_remote);
321 _remote = NULL;
322
323 cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2"));
324 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
325
326 git_remote_free(_remote);
327 _remote = NULL;
328
329 cl_git_pass(git_remote_load(&_remote, _repo, "addtest"));
330 cl_assert_equal_i(GIT_REMOTE_DOWNLOAD_TAGS_AUTO, git_remote_autotag(_remote));
331
332 _refspec = git_vector_get(&_remote->refspecs, 0);
333 cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec));
334 cl_assert(git_refspec_force(_refspec) == 1);
335 cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec));
336 cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2");
337 }
338
339 void test_network_remote_remotes__cannot_add_a_nameless_remote(void)
340 {
341 git_remote *remote;
342
343 cl_assert_equal_i(
344 GIT_EINVALIDSPEC,
345 git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2"));
346 }
347
348 void test_network_remote_remotes__cannot_save_an_inmemory_remote(void)
349 {
350 git_remote *remote;
351
352 cl_git_pass(git_remote_create_anonymous(&remote, _repo, "git://github.com/libgit2/libgit2", NULL));
353
354 cl_assert_equal_p(NULL, git_remote_name(remote));
355
356 cl_git_fail(git_remote_save(remote));
357 git_remote_free(remote);
358 }
359
360 void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void)
361 {
362 git_remote *remote = NULL;
363
364 cl_assert_equal_i(
365 GIT_EINVALIDSPEC,
366 git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2"));
367 cl_assert_equal_p(remote, NULL);
368
369 cl_assert_equal_i(
370 GIT_EINVALIDSPEC,
371 git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2"));
372 cl_assert_equal_p(remote, NULL);
373 }
374
375 void test_network_remote_remotes__tagopt(void)
376 {
377 const char *opt;
378 git_config *cfg;
379
380 cl_git_pass(git_repository_config(&cfg, _repo));
381
382 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL);
383 cl_git_pass(git_remote_save(_remote));
384 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
385 cl_assert_equal_s("--tags", opt);
386
387 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE);
388 cl_git_pass(git_remote_save(_remote));
389 cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt"));
390 cl_assert_equal_s("--no-tags", opt);
391
392 git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO);
393 cl_git_pass(git_remote_save(_remote));
394 cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND);
395
396 git_config_free(cfg);
397 }
398
399 void test_network_remote_remotes__can_load_with_an_empty_url(void)
400 {
401 git_remote *remote = NULL;
402
403 cl_git_pass(git_remote_load(&remote, _repo, "empty-remote-url"));
404
405 cl_assert(remote->url == NULL);
406 cl_assert(remote->pushurl == NULL);
407
408 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH));
409
410 cl_assert(giterr_last() != NULL);
411 cl_assert(giterr_last()->klass == GITERR_INVALID);
412
413 git_remote_free(remote);
414 }
415
416 void test_network_remote_remotes__can_load_with_only_an_empty_pushurl(void)
417 {
418 git_remote *remote = NULL;
419
420 cl_git_pass(git_remote_load(&remote, _repo, "empty-remote-pushurl"));
421
422 cl_assert(remote->url == NULL);
423 cl_assert(remote->pushurl == NULL);
424
425 cl_git_fail(git_remote_connect(remote, GIT_DIRECTION_FETCH));
426
427 git_remote_free(remote);
428 }
429
430 void test_network_remote_remotes__returns_ENOTFOUND_when_neither_url_nor_pushurl(void)
431 {
432 git_remote *remote = NULL;
433
434 cl_git_fail_with(
435 git_remote_load(&remote, _repo, "no-remote-url"), GIT_ENOTFOUND);
436 }
437
438 void assert_cannot_create_remote(const char *name, int expected_error)
439 {
440 git_remote *remote = NULL;
441
442 cl_git_fail_with(
443 git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"),
444 expected_error);
445
446 cl_assert_equal_p(remote, NULL);
447 }
448
449 void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void)
450 {
451 assert_cannot_create_remote("test", GIT_EEXISTS);
452 }
453
454 void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void)
455 {
456 assert_cannot_create_remote("/", GIT_EINVALIDSPEC);
457 assert_cannot_create_remote("//", GIT_EINVALIDSPEC);
458 assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC);
459 assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC);
460 }
461
462 void test_network_remote_remote__git_remote_create_with_fetchspec(void)
463 {
464 git_remote *remote;
465 git_strarray array;
466
467 cl_git_pass(git_remote_create_with_fetchspec(&remote, _repo, "test-new", "git://github.com/libgit2/libgit2", "+refs/*:refs/*"));
468 git_remote_get_fetch_refspecs(&array, remote);
469 cl_assert_equal_s("+refs/*:refs/*", array.strings[0]);
470 git_remote_free(remote);
471 }
472
473 static const char *fetch_refspecs[] = {
474 "+refs/heads/*:refs/remotes/origin/*",
475 "refs/tags/*:refs/tags/*",
476 "+refs/pull/*:refs/pull/*",
477 };
478
479 static const char *push_refspecs[] = {
480 "refs/heads/*:refs/heads/*",
481 "refs/tags/*:refs/tags/*",
482 "refs/notes/*:refs/notes/*",
483 };
484
485 void test_network_remote_remotes__query_refspecs(void)
486 {
487 git_remote *remote;
488 git_strarray array;
489 int i;
490
491 cl_git_pass(git_remote_create_anonymous(&remote, _repo, "git://github.com/libgit2/libgit2", NULL));
492
493 for (i = 0; i < 3; i++) {
494 cl_git_pass(git_remote_add_fetch(remote, fetch_refspecs[i]));
495 cl_git_pass(git_remote_add_push(remote, push_refspecs[i]));
496 }
497
498 cl_git_pass(git_remote_get_fetch_refspecs(&array, remote));
499 for (i = 0; i < 3; i++) {
500 cl_assert_equal_s(fetch_refspecs[i], array.strings[i]);
501 }
502 git_strarray_free(&array);
503
504 cl_git_pass(git_remote_get_push_refspecs(&array, remote));
505 for (i = 0; i < 3; i++) {
506 cl_assert_equal_s(push_refspecs[i], array.strings[i]);
507 }
508 git_strarray_free(&array);
509
510 git_remote_free(remote);
511 }
512
513 static int remote_single_branch(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
514 {
515 char *fetch_refspecs[] = {
516 "refs/heads/first-merge:refs/remotes/origin/first-merge",
517 };
518 git_strarray fetch_refspecs_strarray = {
519 fetch_refspecs,
520 1,
521 };
522
523 GIT_UNUSED(payload);
524
525 cl_git_pass(git_remote_create(out, repo, name, url));
526 cl_git_pass(git_remote_set_fetch_refspecs(*out, &fetch_refspecs_strarray));
527
528 return 0;
529 }
530
531 void test_network_remote_remotes__single_branch(void)
532 {
533 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
534 git_repository *repo;
535 git_strarray refs;
536 size_t i, count = 0;
537
538 opts.remote_cb = remote_single_branch;
539 opts.checkout_branch = "first-merge";
540
541 cl_git_pass(git_clone(&repo, "git://github.com/libgit2/TestGitRepository", "./single-branch", &opts));
542 cl_git_pass(git_reference_list(&refs, repo));
543
544 for (i = 0; i < refs.count; i++) {
545 if (!git__prefixcmp(refs.strings[i], "refs/heads/"))
546 count++;
547 }
548 cl_assert_equal_i(1, count);
549
550 git_repository_free(repo);
551 }
552
553 void test_network_remote_remotes__restricted_refspecs(void)
554 {
555 git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
556 git_repository *repo;
557
558 opts.remote_cb = remote_single_branch;
559
560 cl_git_fail_with(GIT_EINVALIDSPEC, git_clone(&repo, "git://github.com/libgit2/TestGitRepository", "./restrict-refspec", &opts));
561 }