diff options
Diffstat (limited to 'tests-clar/network')
-rw-r--r-- | tests-clar/network/cred.c | 50 | ||||
-rw-r--r-- | tests-clar/network/fetchlocal.c | 78 | ||||
-rw-r--r-- | tests-clar/network/refspecs.c | 84 | ||||
-rw-r--r-- | tests-clar/network/remote/createthenload.c (renamed from tests-clar/network/createremotethenload.c) | 10 | ||||
-rw-r--r-- | tests-clar/network/remote/isvalidname.c | 17 | ||||
-rw-r--r-- | tests-clar/network/remote/local.c | 102 | ||||
-rw-r--r-- | tests-clar/network/remote/remotes.c | 388 | ||||
-rw-r--r-- | tests-clar/network/remote/rename.c | 174 | ||||
-rw-r--r-- | tests-clar/network/remotelocal.c | 128 | ||||
-rw-r--r-- | tests-clar/network/remotes.c | 172 | ||||
-rw-r--r-- | tests-clar/network/urlparse.c | 82 |
11 files changed, 982 insertions, 303 deletions
diff --git a/tests-clar/network/cred.c b/tests-clar/network/cred.c new file mode 100644 index 000000000..6994cc0c3 --- /dev/null +++ b/tests-clar/network/cred.c @@ -0,0 +1,50 @@ +#include "clar_libgit2.h" + +#include "git2/cred_helpers.h" + +void test_network_cred__stock_userpass_validates_args(void) +{ + git_cred_userpass_payload payload = {0}; + + cl_git_fail(git_cred_userpass(NULL, NULL, NULL, 0, NULL)); + + payload.username = "user"; + cl_git_fail(git_cred_userpass(NULL, NULL, NULL, 0, &payload)); + + payload.username = NULL; + payload.username = "pass"; + cl_git_fail(git_cred_userpass(NULL, NULL, NULL, 0, &payload)); +} + +void test_network_cred__stock_userpass_validates_that_method_is_allowed(void) +{ + git_cred *cred; + git_cred_userpass_payload payload = {"user", "pass"}; + + cl_git_fail(git_cred_userpass(&cred, NULL, NULL, 0, &payload)); + cl_git_pass(git_cred_userpass(&cred, NULL, NULL, GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); + cred->free(cred); +} + +void test_network_cred__stock_userpass_properly_handles_username_in_url(void) +{ + git_cred *cred; + git_cred_userpass_plaintext *plain; + git_cred_userpass_payload payload = {"alice", "password"}; + + cl_git_pass(git_cred_userpass(&cred, NULL, NULL, GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); + plain = (git_cred_userpass_plaintext*)cred; + cl_assert_equal_s(plain->username, "alice"); + cred->free(cred); + + cl_git_pass(git_cred_userpass(&cred, NULL, "bob", GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); + plain = (git_cred_userpass_plaintext*)cred; + cl_assert_equal_s(plain->username, "alice"); + cred->free(cred); + + payload.username = NULL; + cl_git_pass(git_cred_userpass(&cred, NULL, "bob", GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); + plain = (git_cred_userpass_plaintext*)cred; + cl_assert_equal_s(plain->username, "bob"); + cred->free(cred); +} diff --git a/tests-clar/network/fetchlocal.c b/tests-clar/network/fetchlocal.c new file mode 100644 index 000000000..bcf298cde --- /dev/null +++ b/tests-clar/network/fetchlocal.c @@ -0,0 +1,78 @@ +#include "clar_libgit2.h" + +#include "buffer.h" +#include "path.h" +#include "remote.h" + +static int transfer_cb(const git_transfer_progress *stats, void *payload) +{ + int *callcount = (int*)payload; + GIT_UNUSED(stats); + (*callcount)++; + return 0; +} + +static void cleanup_local_repo(void *path) +{ + cl_fixture_cleanup((char *)path); +} + +void test_network_fetchlocal__complete(void) +{ + git_repository *repo; + git_remote *origin; + int callcount = 0; + git_strarray refnames = {0}; + + const char *url = cl_git_fixture_url("testrepo.git"); + + cl_set_cleanup(&cleanup_local_repo, "foo"); + cl_git_pass(git_repository_init(&repo, "foo", true)); + + cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); + cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); + cl_git_pass(git_remote_download(origin, transfer_cb, &callcount)); + cl_git_pass(git_remote_update_tips(origin)); + + cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_assert_equal_i(19, (int)refnames.count); + cl_assert(callcount > 0); + + git_strarray_free(&refnames); + git_remote_free(origin); + git_repository_free(repo); +} + +static void cleanup_sandbox(void *unused) +{ + GIT_UNUSED(unused); + cl_git_sandbox_cleanup(); +} + +void test_network_fetchlocal__partial(void) +{ + git_repository *repo = cl_git_sandbox_init("partial-testrepo"); + git_remote *origin; + int callcount = 0; + git_strarray refnames = {0}; + const char *url; + + cl_set_cleanup(&cleanup_sandbox, NULL); + cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_assert_equal_i(1, (int)refnames.count); + + url = cl_git_fixture_url("testrepo.git"); + cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url)); + cl_git_pass(git_remote_connect(origin, GIT_DIRECTION_FETCH)); + cl_git_pass(git_remote_download(origin, transfer_cb, &callcount)); + cl_git_pass(git_remote_update_tips(origin)); + + git_strarray_free(&refnames); + + cl_git_pass(git_reference_list(&refnames, repo, GIT_REF_LISTALL)); + cl_assert_equal_i(20, (int)refnames.count); /* 18 remote + 1 local */ + cl_assert(callcount > 0); + + git_strarray_free(&refnames); + git_remote_free(origin); +} diff --git a/tests-clar/network/refspecs.c b/tests-clar/network/refspecs.c new file mode 100644 index 000000000..b3d80fb85 --- /dev/null +++ b/tests-clar/network/refspecs.c @@ -0,0 +1,84 @@ +#include "clar_libgit2.h" +#include "refspec.h" +#include "remote.h" + +static void assert_refspec(unsigned int direction, const char *input, bool is_expected_to_be_valid) +{ + git_refspec refspec; + int error; + + error = git_refspec__parse(&refspec, input, direction == GIT_DIRECTION_FETCH); + git_refspec__free(&refspec); + + if (is_expected_to_be_valid) + cl_assert_equal_i(0, error); + else + cl_assert_equal_i(GIT_ERROR, error); +} + +void test_network_refspecs__parsing(void) +{ + // Ported from https://github.com/git/git/blob/abd2bde78bd994166900290434a2048e660dabed/t/t5511-refspec.sh + + assert_refspec(GIT_DIRECTION_PUSH, "", false); + assert_refspec(GIT_DIRECTION_PUSH, ":", true); + assert_refspec(GIT_DIRECTION_PUSH, "::", false); + assert_refspec(GIT_DIRECTION_PUSH, "+:", true); + + assert_refspec(GIT_DIRECTION_FETCH, "", true); + assert_refspec(GIT_DIRECTION_PUSH, ":", true); + assert_refspec(GIT_DIRECTION_FETCH, "::", false); + + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz/*", true); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*:refs/remotes/frotz", false); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads:refs/remotes/frotz/*", false); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); + + /* + * These have invalid LHS, but we do not have a formal "valid sha-1 + * expression syntax checker" so they are not checked with the current + * code. They will be caught downstream anyway, but we may want to + * have tighter check later... + */ + //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); + //assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); + + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz/*", true); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*:refs/remotes/frotz", false); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads:refs/remotes/frotz/*", false); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master:refs/remotes/frotz/xyzzy", true); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/master::refs/remotes/frotz/xyzzy", false); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/maste :refs/remotes/frotz/xyzzy", false); + + assert_refspec(GIT_DIRECTION_PUSH, "master~1:refs/remotes/frotz/backup", true); + assert_refspec(GIT_DIRECTION_FETCH, "master~1:refs/remotes/frotz/backup", false); + assert_refspec(GIT_DIRECTION_PUSH, "HEAD~4:refs/remotes/frotz/new", true); + assert_refspec(GIT_DIRECTION_FETCH, "HEAD~4:refs/remotes/frotz/new", false); + + assert_refspec(GIT_DIRECTION_PUSH, "HEAD", true); + assert_refspec(GIT_DIRECTION_FETCH, "HEAD", true); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol", false); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol", false); + + assert_refspec(GIT_DIRECTION_PUSH, "HEAD:", false); + assert_refspec(GIT_DIRECTION_FETCH, "HEAD:", true); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/ nitfol:", false); + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/ nitfol:", false); + + assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/deleteme", true); + assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD-to-me", true); + assert_refspec(GIT_DIRECTION_PUSH, ":refs/remotes/frotz/delete me", false); + assert_refspec(GIT_DIRECTION_FETCH, ":refs/remotes/frotz/HEAD to me", false); + + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*-blah", false); + + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads*/for-linus:refs/remotes/mine/*", false); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads*/for-linus:refs/remotes/mine/*", false); + + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/*/for-linus:refs/remotes/mine/*", false); + + assert_refspec(GIT_DIRECTION_FETCH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); + assert_refspec(GIT_DIRECTION_PUSH, "refs/heads/*/for-linus:refs/remotes/mine/*", true); +} diff --git a/tests-clar/network/createremotethenload.c b/tests-clar/network/remote/createthenload.c index 45931d376..ac6cfccd3 100644 --- a/tests-clar/network/createremotethenload.c +++ b/tests-clar/network/remote/createthenload.c @@ -5,7 +5,7 @@ static git_repository *_repo; static git_config *_config; static char url[] = "http://github.com/libgit2/libgit2.git"; -void test_network_createremotethenload__initialize(void) +void test_network_remote_createthenload__initialize(void) { cl_fixture_sandbox("testrepo.git"); @@ -19,14 +19,18 @@ void test_network_createremotethenload__initialize(void) cl_git_pass(git_remote_load(&_remote, _repo, "origin")); } -void test_network_createremotethenload__cleanup(void) +void test_network_remote_createthenload__cleanup(void) { git_remote_free(_remote); + _remote = NULL; + git_repository_free(_repo); + _repo = NULL; + cl_fixture_cleanup("testrepo.git"); } -void test_network_createremotethenload__parsing(void) +void test_network_remote_createthenload__parsing(void) { cl_assert_equal_s(git_remote_name(_remote), "origin"); cl_assert_equal_s(git_remote_url(_remote), url); diff --git a/tests-clar/network/remote/isvalidname.c b/tests-clar/network/remote/isvalidname.c new file mode 100644 index 000000000..c26fbd0a5 --- /dev/null +++ b/tests-clar/network/remote/isvalidname.c @@ -0,0 +1,17 @@ +#include "clar_libgit2.h" + +void test_network_remote_isvalidname__can_detect_invalid_formats(void) +{ + cl_assert_equal_i(false, git_remote_is_valid_name("/")); + cl_assert_equal_i(false, git_remote_is_valid_name("//")); + cl_assert_equal_i(false, git_remote_is_valid_name(".lock")); + cl_assert_equal_i(false, git_remote_is_valid_name("a.lock")); + cl_assert_equal_i(false, git_remote_is_valid_name("/no/leading/slash")); + cl_assert_equal_i(false, git_remote_is_valid_name("no/trailing/slash/")); +} + +void test_network_remote_isvalidname__wont_hopefully_choke_on_valid_formats(void) +{ + cl_assert_equal_i(true, git_remote_is_valid_name("webmatrix")); + cl_assert_equal_i(true, git_remote_is_valid_name("yishaigalatzer/rules")); +} diff --git a/tests-clar/network/remote/local.c b/tests-clar/network/remote/local.c new file mode 100644 index 000000000..7e847e654 --- /dev/null +++ b/tests-clar/network/remote/local.c @@ -0,0 +1,102 @@ +#include "clar_libgit2.h" +#include "buffer.h" +#include "path.h" +#include "posix.h" + +static git_repository *repo; +static git_buf file_path_buf = GIT_BUF_INIT; +static git_remote *remote; + +void test_network_remote_local__initialize(void) +{ + cl_git_pass(git_repository_init(&repo, "remotelocal/", 0)); + cl_assert(repo != NULL); +} + +void test_network_remote_local__cleanup(void) +{ + git_buf_free(&file_path_buf); + + git_remote_free(remote); + remote = NULL; + + git_repository_free(repo); + repo = NULL; + + cl_fixture_cleanup("remotelocal"); +} + +static int count_ref__cb(git_remote_head *head, void *payload) +{ + int *count = (int *)payload; + + (void)head; + (*count)++; + + return 0; +} + +static int ensure_peeled__cb(git_remote_head *head, void *payload) +{ + GIT_UNUSED(payload); + + if(strcmp(head->name, "refs/tags/test^{}") != 0) + return 0; + + return git_oid_streq(&head->oid, "e90810b8df3e80c413d903f631643c716887138d"); +} + +static void connect_to_local_repository(const char *local_repository) +{ + git_buf_sets(&file_path_buf, cl_git_path_url(local_repository)); + + cl_git_pass(git_remote_create_inmemory(&remote, repo, NULL, git_buf_cstr(&file_path_buf))); + cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH)); + +} + +void test_network_remote_local__connected(void) +{ + connect_to_local_repository(cl_fixture("testrepo.git")); + cl_assert(git_remote_connected(remote)); + + git_remote_disconnect(remote); + cl_assert(!git_remote_connected(remote)); +} + +void test_network_remote_local__retrieve_advertised_references(void) +{ + int how_many_refs = 0; + + connect_to_local_repository(cl_fixture("testrepo.git")); + + cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); + + cl_assert_equal_i(how_many_refs, 28); +} + +void test_network_remote_local__retrieve_advertised_references_from_spaced_repository(void) +{ + int how_many_refs = 0; + + cl_fixture_sandbox("testrepo.git"); + cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git")); + + connect_to_local_repository("spaced testrepo.git"); + + cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); + + cl_assert_equal_i(how_many_refs, 28); + + git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ + remote = NULL; + + cl_fixture_cleanup("spaced testrepo.git"); +} + +void test_network_remote_local__nested_tags_are_completely_peeled(void) +{ + connect_to_local_repository(cl_fixture("testrepo.git")); + + cl_git_pass(git_remote_ls(remote, &ensure_peeled__cb, NULL)); +} diff --git a/tests-clar/network/remote/remotes.c b/tests-clar/network/remote/remotes.c new file mode 100644 index 000000000..a5ff7415f --- /dev/null +++ b/tests-clar/network/remote/remotes.c @@ -0,0 +1,388 @@ +#include "clar_libgit2.h" +#include "buffer.h" +#include "refspec.h" +#include "remote.h" + +static git_remote *_remote; +static git_repository *_repo; +static const git_refspec *_refspec; + +void test_network_remote_remotes__initialize(void) +{ + _repo = cl_git_sandbox_init("testrepo.git"); + + cl_git_pass(git_remote_load(&_remote, _repo, "test")); + + _refspec = git_remote_fetchspec(_remote); + cl_assert(_refspec != NULL); +} + +void test_network_remote_remotes__cleanup(void) +{ + git_remote_free(_remote); + _remote = NULL; + + cl_git_sandbox_cleanup(); +} + +void test_network_remote_remotes__parsing(void) +{ + git_remote *_remote2 = NULL; + + cl_assert_equal_s(git_remote_name(_remote), "test"); + cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); + cl_assert(git_remote_pushurl(_remote) == NULL); + + cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_FETCH), + "git://github.com/libgit2/libgit2"); + cl_assert_equal_s(git_remote__urlfordirection(_remote, GIT_DIRECTION_PUSH), + "git://github.com/libgit2/libgit2"); + + cl_git_pass(git_remote_load(&_remote2, _repo, "test_with_pushurl")); + cl_assert_equal_s(git_remote_name(_remote2), "test_with_pushurl"); + cl_assert_equal_s(git_remote_url(_remote2), "git://github.com/libgit2/fetchlibgit2"); + cl_assert_equal_s(git_remote_pushurl(_remote2), "git://github.com/libgit2/pushlibgit2"); + + cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_FETCH), + "git://github.com/libgit2/fetchlibgit2"); + cl_assert_equal_s(git_remote__urlfordirection(_remote2, GIT_DIRECTION_PUSH), + "git://github.com/libgit2/pushlibgit2"); + + git_remote_free(_remote2); +} + +void test_network_remote_remotes__pushurl(void) +{ + cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/notlibgit2")); + cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/notlibgit2"); + + cl_git_pass(git_remote_set_pushurl(_remote, NULL)); + cl_assert(git_remote_pushurl(_remote) == NULL); +} + +void test_network_remote_remotes__error_when_no_push_available(void) +{ + git_remote *r; + git_transport *t; + git_push *p; + + cl_git_pass(git_remote_create_inmemory(&r, _repo, NULL, cl_fixture("testrepo.git"))); + + cl_git_pass(git_transport_local(&t,r,NULL)); + + /* Make sure that push is really not available */ + t->push = NULL; + cl_git_pass(git_remote_set_transport(r, t)); + + cl_git_pass(git_remote_connect(r, GIT_DIRECTION_PUSH)); + cl_git_pass(git_push_new(&p, r)); + cl_git_pass(git_push_add_refspec(p, "refs/heads/master")); + cl_git_fail_with(git_push_finish(p), GIT_ERROR); + + git_push_free(p); + git_remote_free(r); +} + +void test_network_remote_remotes__parsing_ssh_remote(void) +{ + cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") ); +} + +void test_network_remote_remotes__parsing_local_path_fails_if_path_not_found(void) +{ + cl_assert( !git_remote_valid_url("/home/git/repos/libgit2.git") ); +} + +void test_network_remote_remotes__supported_transport_methods_are_supported(void) +{ + cl_assert( git_remote_supported_url("git://github.com/libgit2/libgit2") ); +} + +void test_network_remote_remotes__unsupported_transport_methods_are_unsupported(void) +{ + cl_assert( !git_remote_supported_url("git@github.com:libgit2/libgit2.git") ); +} + +void test_network_remote_remotes__refspec_parsing(void) +{ + cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); + cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*"); +} + +void test_network_remote_remotes__set_fetchspec(void) +{ + cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*")); + _refspec = git_remote_fetchspec(_remote); + cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); + cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); +} + +void test_network_remote_remotes__set_pushspec(void) +{ + cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*")); + _refspec = git_remote_pushspec(_remote); + cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); + cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); +} + +void test_network_remote_remotes__save(void) +{ + git_remote_free(_remote); + _remote = NULL; + + /* Set up the remote and save it to config */ + cl_git_pass(git_remote_create(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2")); + cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*")); + cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*")); + cl_git_pass(git_remote_set_pushurl(_remote, "git://github.com/libgit2/libgit2_push")); + cl_git_pass(git_remote_save(_remote)); + git_remote_free(_remote); + _remote = NULL; + + /* Load it from config and make sure everything matches */ + cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); + + _refspec = git_remote_fetchspec(_remote); + cl_assert(_refspec != NULL); + cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); + cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*"); + cl_assert_equal_i(0, git_refspec_force(_refspec)); + + _refspec = git_remote_pushspec(_remote); + cl_assert(_refspec != NULL); + cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); + cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*"); + + cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); + cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push"); + + /* remove the pushurl again and see if we can save that too */ + cl_git_pass(git_remote_set_pushurl(_remote, NULL)); + cl_git_pass(git_remote_save(_remote)); + git_remote_free(_remote); + _remote = NULL; + + cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); + cl_assert(git_remote_pushurl(_remote) == NULL); +} + +void test_network_remote_remotes__fnmatch(void) +{ + cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master")); + cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch")); +} + +void test_network_remote_remotes__transform(void) +{ + char ref[1024] = {0}; + + cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master")); + cl_assert_equal_s(ref, "refs/remotes/test/master"); +} + +void test_network_remote_remotes__transform_destination_to_source(void) +{ + char ref[1024] = {0}; + + cl_git_pass(git_refspec_rtransform(ref, sizeof(ref), _refspec, "refs/remotes/test/master")); + cl_assert_equal_s(ref, "refs/heads/master"); +} + +void test_network_remote_remotes__transform_r(void) +{ + git_buf buf = GIT_BUF_INIT; + + cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master")); + cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master"); + git_buf_free(&buf); +} + +void test_network_remote_remotes__missing_refspecs(void) +{ + git_config *cfg; + + git_remote_free(_remote); + _remote = NULL; + + cl_git_pass(git_repository_config(&cfg, _repo)); + cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); + cl_git_pass(git_remote_load(&_remote, _repo, "specless")); + + git_config_free(cfg); +} + +void test_network_remote_remotes__list(void) +{ + git_strarray list; + git_config *cfg; + + cl_git_pass(git_remote_list(&list, _repo)); + cl_assert(list.count == 4); + git_strarray_free(&list); + + cl_git_pass(git_repository_config(&cfg, _repo)); + cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); + cl_git_pass(git_remote_list(&list, _repo)); + cl_assert(list.count == 5); + git_strarray_free(&list); + + git_config_free(cfg); +} + +void test_network_remote_remotes__loading_a_missing_remote_returns_ENOTFOUND(void) +{ + git_remote_free(_remote); + _remote = NULL; + + cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago")); +} + +void test_network_remote_remotes__loading_with_an_invalid_name_returns_EINVALIDSPEC(void) +{ + git_remote_free(_remote); + _remote = NULL; + + cl_assert_equal_i(GIT_EINVALIDSPEC, git_remote_load(&_remote, _repo, "Inv@{id")); +} + +/* + * $ git remote add addtest http://github.com/libgit2/libgit2 + * + * $ cat .git/config + * [...] + * [remote "addtest"] + * url = http://github.com/libgit2/libgit2 + * fetch = +refs/heads/\*:refs/remotes/addtest/\* + */ +void test_network_remote_remotes__add(void) +{ + git_remote_free(_remote); + _remote = NULL; + + cl_git_pass(git_remote_create(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2")); + + git_remote_free(_remote); + _remote = NULL; + + cl_git_pass(git_remote_load(&_remote, _repo, "addtest")); + _refspec = git_remote_fetchspec(_remote); + cl_assert_equal_s("refs/heads/*", git_refspec_src(_refspec)); + cl_assert(git_refspec_force(_refspec) == 1); + cl_assert_equal_s("refs/remotes/addtest/*", git_refspec_dst(_refspec)); + cl_assert_equal_s(git_remote_url(_remote), "http://github.com/libgit2/libgit2"); +} + +void test_network_remote_remotes__cannot_add_a_nameless_remote(void) +{ + git_remote *remote; + + cl_assert_equal_i( + GIT_EINVALIDSPEC, + git_remote_create(&remote, _repo, NULL, "git://github.com/libgit2/libgit2")); +} + +void test_network_remote_remotes__cannot_save_an_inmemory_remote(void) +{ + git_remote *remote; + + cl_git_pass(git_remote_create_inmemory(&remote, _repo, NULL, "git://github.com/libgit2/libgit2")); + + cl_assert_equal_p(NULL, git_remote_name(remote)); + + cl_git_fail(git_remote_save(remote)); + git_remote_free(remote); +} + +void test_network_remote_remotes__cannot_add_a_remote_with_an_invalid_name(void) +{ + git_remote *remote = NULL; + + cl_assert_equal_i( + GIT_EINVALIDSPEC, + git_remote_create(&remote, _repo, "Inv@{id", "git://github.com/libgit2/libgit2")); + cl_assert_equal_p(remote, NULL); + + cl_assert_equal_i( + GIT_EINVALIDSPEC, + git_remote_create(&remote, _repo, "", "git://github.com/libgit2/libgit2")); + cl_assert_equal_p(remote, NULL); +} + +void test_network_remote_remotes__tagopt(void) +{ + const char *opt; + git_config *cfg; + + cl_git_pass(git_repository_config(&cfg, _repo)); + + git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_ALL); + cl_git_pass(git_remote_save(_remote)); + cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); + cl_assert_equal_s("--tags", opt); + + git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_NONE); + cl_git_pass(git_remote_save(_remote)); + cl_git_pass(git_config_get_string(&opt, cfg, "remote.test.tagopt")); + cl_assert_equal_s("--no-tags", opt); + + git_remote_set_autotag(_remote, GIT_REMOTE_DOWNLOAD_TAGS_AUTO); + cl_git_pass(git_remote_save(_remote)); + cl_assert(git_config_get_string(&opt, cfg, "remote.test.tagopt") == GIT_ENOTFOUND); + + git_config_free(cfg); +} + +void test_network_remote_remotes__cannot_load_with_an_empty_url(void) +{ + git_remote *remote = NULL; + + cl_git_fail(git_remote_load(&remote, _repo, "empty-remote-url")); + cl_assert(giterr_last()->klass == GITERR_INVALID); + cl_assert_equal_p(remote, NULL); +} + +void test_network_remote_remotes__check_structure_version(void) +{ + git_transport transport = GIT_TRANSPORT_INIT; + const git_error *err; + + git_remote_free(_remote); + _remote = NULL; + cl_git_pass(git_remote_create_inmemory(&_remote, _repo, NULL, "test-protocol://localhost")); + + transport.version = 0; + cl_git_fail(git_remote_set_transport(_remote, &transport)); + err = giterr_last(); + cl_assert_equal_i(GITERR_INVALID, err->klass); + + giterr_clear(); + transport.version = 1024; + cl_git_fail(git_remote_set_transport(_remote, &transport)); + err = giterr_last(); + cl_assert_equal_i(GITERR_INVALID, err->klass); +} + +void assert_cannot_create_remote(const char *name, int expected_error) +{ + git_remote *remote = NULL; + + cl_git_fail_with( + git_remote_create(&remote, _repo, name, "git://github.com/libgit2/libgit2"), + expected_error); + + cl_assert_equal_p(remote, NULL); +} + +void test_network_remote_remotes__cannot_create_a_remote_which_name_conflicts_with_an_existing_remote(void) +{ + assert_cannot_create_remote("test", GIT_EEXISTS); +} + + +void test_network_remote_remotes__cannot_create_a_remote_which_name_is_invalid(void) +{ + assert_cannot_create_remote("/", GIT_EINVALIDSPEC); + assert_cannot_create_remote("//", GIT_EINVALIDSPEC); + assert_cannot_create_remote(".lock", GIT_EINVALIDSPEC); + assert_cannot_create_remote("a.lock", GIT_EINVALIDSPEC); +} diff --git a/tests-clar/network/remote/rename.c b/tests-clar/network/remote/rename.c new file mode 100644 index 000000000..ed98ee811 --- /dev/null +++ b/tests-clar/network/remote/rename.c @@ -0,0 +1,174 @@ +#include "clar_libgit2.h" +#include "config/config_helpers.h" + +#include "repository.h" + +static git_remote *_remote; +static git_repository *_repo; + +void test_network_remote_rename__initialize(void) +{ + _repo = cl_git_sandbox_init("testrepo.git"); + + cl_git_pass(git_remote_load(&_remote, _repo, "test")); +} + +void test_network_remote_rename__cleanup(void) +{ + git_remote_free(_remote); + _remote = NULL; + + cl_git_sandbox_cleanup(); +} + +static int dont_call_me_cb(const char *fetch_refspec, void *payload) +{ + GIT_UNUSED(fetch_refspec); + GIT_UNUSED(payload); + + cl_assert(false); + + return -1; +} + +void test_network_remote_rename__renaming_a_remote_moves_related_configuration_section(void) +{ + assert_config_entry_existence(_repo, "remote.test.fetch", true); + assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + assert_config_entry_existence(_repo, "remote.test.fetch", false); + assert_config_entry_existence(_repo, "remote.just/renamed.fetch", true); +} + +void test_network_remote_rename__renaming_a_remote_updates_branch_related_configuration_entries(void) +{ + assert_config_entry_value(_repo, "branch.master.remote", "test"); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + assert_config_entry_value(_repo, "branch.master.remote", "just/renamed"); +} + +void test_network_remote_rename__renaming_a_remote_updates_default_fetchrefspec(void) +{ + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/heads/*:refs/remotes/just/renamed/*"); +} + +void test_network_remote_rename__renaming_a_remote_without_a_fetchrefspec_doesnt_create_one(void) +{ + git_config *config; + + git_remote_free(_remote); + cl_git_pass(git_repository_config__weakptr(&config, _repo)); + cl_git_pass(git_config_delete_entry(config, "remote.test.fetch")); + + cl_git_pass(git_remote_load(&_remote, _repo, "test")); + + assert_config_entry_existence(_repo, "remote.test.fetch", false); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + assert_config_entry_existence(_repo, "remote.just/renamed.fetch", false); +} + +static int ensure_refspecs(const char* refspec_name, void *payload) +{ + int i = 0; + bool found = false; + const char ** exp = (const char **)payload; + + while (exp[i]) { + if (strcmp(exp[i++], refspec_name)) + continue; + + found = true; + break; + } + + cl_assert(found); + + return 0; +} + +void test_network_remote_rename__renaming_a_remote_notifies_of_non_default_fetchrefspec(void) +{ + git_config *config; + + char *expected_refspecs[] = { + "+refs/*:refs/*", + NULL + }; + + git_remote_free(_remote); + cl_git_pass(git_repository_config__weakptr(&config, _repo)); + cl_git_pass(git_config_set_string(config, "remote.test.fetch", "+refs/*:refs/*")); + cl_git_pass(git_remote_load(&_remote, _repo, "test")); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", ensure_refspecs, &expected_refspecs)); + + assert_config_entry_value(_repo, "remote.just/renamed.fetch", "+refs/*:refs/*"); +} + +void test_network_remote_rename__new_name_can_contain_dots(void) +{ + cl_git_pass(git_remote_rename(_remote, "just.renamed", dont_call_me_cb, NULL)); + cl_assert_equal_s("just.renamed", git_remote_name(_remote)); +} + +void test_network_remote_rename__new_name_must_conform_to_reference_naming_conventions(void) +{ + cl_assert_equal_i( + GIT_EINVALIDSPEC, + git_remote_rename(_remote, "new@{name", dont_call_me_cb, NULL)); +} + +void test_network_remote_rename__renamed_name_is_persisted(void) +{ + git_remote *renamed; + git_repository *another_repo; + + cl_git_fail(git_remote_load(&renamed, _repo, "just/renamed")); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + cl_git_pass(git_repository_open(&another_repo, "testrepo.git")); + cl_git_pass(git_remote_load(&renamed, _repo, "just/renamed")); + + git_remote_free(renamed); + git_repository_free(another_repo); +} + +void test_network_remote_rename__cannot_overwrite_an_existing_remote(void) +{ + cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test", dont_call_me_cb, NULL)); + cl_assert_equal_i(GIT_EEXISTS, git_remote_rename(_remote, "test_with_pushurl", dont_call_me_cb, NULL)); +} + +void test_network_remote_rename__renaming_a_remote_moves_the_underlying_reference(void) +{ + git_reference *underlying; + + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed")); + cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); + git_reference_free(underlying); + + cl_git_pass(git_remote_rename(_remote, "just/renamed", dont_call_me_cb, NULL)); + + cl_assert_equal_i(GIT_ENOTFOUND, git_reference_lookup(&underlying, _repo, "refs/remotes/test/master")); + cl_git_pass(git_reference_lookup(&underlying, _repo, "refs/remotes/just/renamed/master")); + git_reference_free(underlying); +} + +void test_network_remote_rename__cannot_rename_an_inmemory_remote(void) +{ + git_remote *remote; + + cl_git_pass(git_remote_create_inmemory(&remote, _repo, NULL, "file:///blah")); + cl_git_fail(git_remote_rename(remote, "newname", NULL, NULL)); + + git_remote_free(remote); +} diff --git a/tests-clar/network/remotelocal.c b/tests-clar/network/remotelocal.c deleted file mode 100644 index 98abbbeb9..000000000 --- a/tests-clar/network/remotelocal.c +++ /dev/null @@ -1,128 +0,0 @@ -#include "clar_libgit2.h" -#include "transport.h" -#include "buffer.h" -#include "path.h" -#include "posix.h" - -static git_repository *repo; -static git_buf file_path_buf = GIT_BUF_INIT; -static git_remote *remote; - -static void build_local_file_url(git_buf *out, const char *fixture) -{ - const char *in_buf; - - git_buf path_buf = GIT_BUF_INIT; - - cl_git_pass(git_path_prettify_dir(&path_buf, fixture, NULL)); - cl_git_pass(git_buf_puts(out, "file://")); - -#ifdef _MSC_VER - /* - * A FILE uri matches the following format: file://[host]/path - * where "host" can be empty and "path" is an absolute path to the resource. - * - * In this test, no hostname is used, but we have to ensure the leading triple slashes: - * - * *nix: file:///usr/home/... - * Windows: file:///C:/Users/... - */ - cl_git_pass(git_buf_putc(out, '/')); -#endif - - in_buf = git_buf_cstr(&path_buf); - - /* - * A very hacky Url encoding that only takes care of escaping the spaces - */ - while (*in_buf) { - if (*in_buf == ' ') - cl_git_pass(git_buf_puts(out, "%20")); - else - cl_git_pass(git_buf_putc(out, *in_buf)); - - in_buf++; - } - - git_buf_free(&path_buf); -} - -void test_network_remotelocal__initialize(void) -{ - cl_git_pass(git_repository_init(&repo, "remotelocal/", 0)); - cl_assert(repo != NULL); -} - -void test_network_remotelocal__cleanup(void) -{ - git_remote_free(remote); - git_buf_free(&file_path_buf); - git_repository_free(repo); - cl_fixture_cleanup("remotelocal"); -} - -static int count_ref__cb(git_remote_head *head, void *payload) -{ - int *count = (int *)payload; - - (void)head; - (*count)++; - - return 0; -} - -static int ensure_peeled__cb(git_remote_head *head, void *payload) -{ - GIT_UNUSED(payload); - - if(strcmp(head->name, "refs/tags/test^{}") != 0) - return 0; - - return git_oid_streq(&head->oid, "e90810b8df3e80c413d903f631643c716887138d"); -} - -static void connect_to_local_repository(const char *local_repository) -{ - build_local_file_url(&file_path_buf, local_repository); - - cl_git_pass(git_remote_new(&remote, repo, NULL, git_buf_cstr(&file_path_buf), NULL)); - cl_git_pass(git_remote_connect(remote, GIT_DIR_FETCH)); - -} - -void test_network_remotelocal__retrieve_advertised_references(void) -{ - int how_many_refs = 0; - - connect_to_local_repository(cl_fixture("testrepo.git")); - - cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - - cl_assert(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ -} - -void test_network_remotelocal__retrieve_advertised_references_from_spaced_repository(void) -{ - int how_many_refs = 0; - - cl_fixture_sandbox("testrepo.git"); - cl_git_pass(p_rename("testrepo.git", "spaced testrepo.git")); - - connect_to_local_repository("spaced testrepo.git"); - - cl_git_pass(git_remote_ls(remote, &count_ref__cb, &how_many_refs)); - - cl_assert(how_many_refs == 14); /* 1 HEAD + 6 heads + 1 lightweight tag + 3 annotated tags + 3 peeled target */ - - git_remote_free(remote); /* Disconnect from the "spaced repo" before the cleanup */ - remote = NULL; - - cl_fixture_cleanup("spaced testrepo.git"); -} - -void test_network_remotelocal__nested_tags_are_completely_peeled(void) -{ - connect_to_local_repository(cl_fixture("testrepo.git")); - - cl_git_pass(git_remote_ls(remote, &ensure_peeled__cb, NULL)); -} diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c deleted file mode 100644 index 0649c86dd..000000000 --- a/tests-clar/network/remotes.c +++ /dev/null @@ -1,172 +0,0 @@ -#include "clar_libgit2.h" -#include "buffer.h" -#include "refspec.h" -#include "transport.h" - -static git_remote *_remote; -static git_repository *_repo; -static const git_refspec *_refspec; - -void test_network_remotes__initialize(void) -{ - cl_fixture_sandbox("testrepo.git"); - - cl_git_pass(git_repository_open(&_repo, "testrepo.git")); - cl_git_pass(git_remote_load(&_remote, _repo, "test")); - - _refspec = git_remote_fetchspec(_remote); - cl_assert(_refspec != NULL); -} - -void test_network_remotes__cleanup(void) -{ - git_remote_free(_remote); - git_repository_free(_repo); - cl_fixture_cleanup("testrepo.git"); -} - -void test_network_remotes__parsing(void) -{ - cl_assert_equal_s(git_remote_name(_remote), "test"); - cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2"); -} - -void test_network_remotes__parsing_ssh_remote(void) -{ - cl_assert( git_remote_valid_url("git@github.com:libgit2/libgit2.git") ); -} - -void test_network_remotes__parsing_local_path_fails_if_path_not_found(void) -{ - cl_assert( !git_remote_valid_url("/home/git/repos/libgit2.git") ); -} - -void test_network_remotes__supported_transport_methods_are_supported(void) -{ - cl_assert( git_remote_supported_url("git://github.com/libgit2/libgit2") ); -} - -void test_network_remotes__unsupported_transport_methods_are_unsupported(void) -{ - cl_assert( !git_remote_supported_url("git@github.com:libgit2/libgit2.git") ); -} - -void test_network_remotes__refspec_parsing(void) -{ - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/test/*"); -} - -void test_network_remotes__set_fetchspec(void) -{ - cl_git_pass(git_remote_set_fetchspec(_remote, "refs/*:refs/*")); - _refspec = git_remote_fetchspec(_remote); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); -} - -void test_network_remotes__set_pushspec(void) -{ - cl_git_pass(git_remote_set_pushspec(_remote, "refs/*:refs/*")); - _refspec = git_remote_pushspec(_remote); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/*"); -} - -void test_network_remotes__save(void) -{ - git_remote_free(_remote); - - /* Set up the remote and save it to config */ - cl_git_pass(git_remote_new(&_remote, _repo, "upstream", "git://github.com/libgit2/libgit2", NULL)); - cl_git_pass(git_remote_set_fetchspec(_remote, "refs/heads/*:refs/remotes/upstream/*")); - cl_git_pass(git_remote_set_pushspec(_remote, "refs/heads/*:refs/heads/*")); - cl_git_pass(git_remote_save(_remote)); - git_remote_free(_remote); - _remote = NULL; - - /* Load it from config and make sure everything matches */ - cl_git_pass(git_remote_load(&_remote, _repo, "upstream")); - - _refspec = git_remote_fetchspec(_remote); - cl_assert(_refspec != NULL); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/remotes/upstream/*"); - - _refspec = git_remote_pushspec(_remote); - cl_assert(_refspec != NULL); - cl_assert_equal_s(git_refspec_src(_refspec), "refs/heads/*"); - cl_assert_equal_s(git_refspec_dst(_refspec), "refs/heads/*"); -} - -void test_network_remotes__fnmatch(void) -{ - cl_assert(git_refspec_src_matches(_refspec, "refs/heads/master")); - cl_assert(git_refspec_src_matches(_refspec, "refs/heads/multi/level/branch")); -} - -void test_network_remotes__transform(void) -{ - char ref[1024]; - - memset(ref, 0x0, sizeof(ref)); - cl_git_pass(git_refspec_transform(ref, sizeof(ref), _refspec, "refs/heads/master")); - cl_assert_equal_s(ref, "refs/remotes/test/master"); -} - -void test_network_remotes__transform_r(void) -{ - git_buf buf = GIT_BUF_INIT; - - cl_git_pass(git_refspec_transform_r(&buf, _refspec, "refs/heads/master")); - cl_assert_equal_s(git_buf_cstr(&buf), "refs/remotes/test/master"); - git_buf_free(&buf); -} - -void test_network_remotes__missing_refspecs(void) -{ - git_config *cfg; - - git_remote_free(_remote); - - cl_git_pass(git_repository_config(&cfg, _repo)); - cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); - cl_git_pass(git_remote_load(&_remote, _repo, "specless")); - - git_config_free(cfg); -} - -void test_network_remotes__list(void) -{ - git_strarray list; - git_config *cfg; - - cl_git_pass(git_remote_list(&list, _repo)); - cl_assert(list.count == 1); - git_strarray_free(&list); - - cl_git_pass(git_repository_config(&cfg, _repo)); - cl_git_pass(git_config_set_string(cfg, "remote.specless.url", "http://example.com")); - cl_git_pass(git_remote_list(&list, _repo)); - cl_assert(list.count == 2); - git_strarray_free(&list); - - git_config_free(cfg); -} - -void test_network_remotes__loading_a_missing_remote_returns_ENOTFOUND(void) -{ - cl_assert_equal_i(GIT_ENOTFOUND, git_remote_load(&_remote, _repo, "just-left-few-minutes-ago")); -} - -void test_network_remotes__add(void) -{ - git_remote_free(_remote); - cl_git_pass(git_remote_add(&_remote, _repo, "addtest", "http://github.com/libgit2/libgit2")); - git_remote_free(_remote); - - cl_git_pass(git_remote_load(&_remote, _repo, "addtest")); - _refspec = git_remote_fetchspec(_remote); - cl_assert(!strcmp(git_refspec_src(_refspec), "refs/heads/*")); - cl_assert(!strcmp(git_refspec_dst(_refspec), "refs/remotes/addtest/*")); -} diff --git a/tests-clar/network/urlparse.c b/tests-clar/network/urlparse.c new file mode 100644 index 000000000..173e57d0f --- /dev/null +++ b/tests-clar/network/urlparse.c @@ -0,0 +1,82 @@ +#include "clar_libgit2.h" +#include "netops.h" + +char *host, *port, *user, *pass; + +void test_network_urlparse__initialize(void) +{ + host = port = user = pass = NULL; +} + +void test_network_urlparse__cleanup(void) +{ +#define FREE_AND_NULL(x) if (x) { git__free(x); x = NULL; } + FREE_AND_NULL(host); + FREE_AND_NULL(port); + FREE_AND_NULL(user); + FREE_AND_NULL(pass); +} + +void test_network_urlparse__trivial(void) +{ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "example.com/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "8080"); + cl_assert_equal_p(user, NULL); + cl_assert_equal_p(pass, NULL); +} + +void test_network_urlparse__user(void) +{ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "user@example.com/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "8080"); + cl_assert_equal_s(user, "user"); + cl_assert_equal_p(pass, NULL); +} + +void test_network_urlparse__user_pass(void) +{ + /* user:pass@hostname.tld/resource */ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "user:pass@example.com/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "8080"); + cl_assert_equal_s(user, "user"); + cl_assert_equal_s(pass, "pass"); +} + +void test_network_urlparse__port(void) +{ + /* hostname.tld:port/resource */ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "example.com:9191/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "9191"); + cl_assert_equal_p(user, NULL); + cl_assert_equal_p(pass, NULL); +} + +void test_network_urlparse__user_port(void) +{ + /* user@hostname.tld:port/resource */ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "user@example.com:9191/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "9191"); + cl_assert_equal_s(user, "user"); + cl_assert_equal_p(pass, NULL); +} + +void test_network_urlparse__user_pass_port(void) +{ + /* user:pass@hostname.tld:port/resource */ + cl_git_pass(gitno_extract_url_parts(&host, &port, &user, &pass, + "user:pass@example.com:9191/resource", "8080")); + cl_assert_equal_s(host, "example.com"); + cl_assert_equal_s(port, "9191"); + cl_assert_equal_s(user, "user"); + cl_assert_equal_s(pass, "pass"); +} |