diff options
| author | Russell Belfer <rb@github.com> | 2013-02-04 14:49:28 -0800 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2013-02-04 14:49:28 -0800 |
| commit | de81aee3907e3737ad87e88e14b702f4b3bf12a6 (patch) | |
| tree | e3c8c35378bf5c4c5f922b39d3dc168d1657097e /tests-clar | |
| parent | 3261a3e980b00cf19b971c078d09bc0660ce1f81 (diff) | |
| parent | 630146bd1b71cbb450f1fe658048ca8e25479105 (diff) | |
| download | libgit2-de81aee3907e3737ad87e88e14b702f4b3bf12a6.tar.gz | |
Merge pull request #1298 from ben/user-at
Handle "user@" prefix for credentials partially included in URLs
Diffstat (limited to 'tests-clar')
| -rw-r--r-- | tests-clar/network/cred.c | 33 | ||||
| -rw-r--r-- | tests-clar/network/urlparse.c | 82 | ||||
| -rw-r--r-- | tests-clar/online/clone.c | 31 | ||||
| -rw-r--r-- | tests-clar/online/push.c | 8 |
4 files changed, 148 insertions, 6 deletions
diff --git a/tests-clar/network/cred.c b/tests-clar/network/cred.c index b7f45c23b..6994cc0c3 100644 --- a/tests-clar/network/cred.c +++ b/tests-clar/network/cred.c @@ -6,14 +6,14 @@ void test_network_cred__stock_userpass_validates_args(void) { git_cred_userpass_payload payload = {0}; - cl_git_fail(git_cred_userpass(NULL, NULL, 0, NULL)); + cl_git_fail(git_cred_userpass(NULL, NULL, NULL, 0, NULL)); payload.username = "user"; - cl_git_fail(git_cred_userpass(NULL, NULL, 0, &payload)); + 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, 0, &payload)); + cl_git_fail(git_cred_userpass(NULL, NULL, NULL, 0, &payload)); } void test_network_cred__stock_userpass_validates_that_method_is_allowed(void) @@ -21,7 +21,30 @@ 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, 0, &payload)); - cl_git_pass(git_cred_userpass(&cred, NULL, GIT_CREDTYPE_USERPASS_PLAINTEXT, &payload)); + 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/urlparse.c b/tests-clar/network/urlparse.c new file mode 100644 index 000000000..84d0bfb88 --- /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_sz(user, NULL); + cl_assert_equal_sz(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_sz(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_sz(user, NULL); + cl_assert_equal_sz(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_sz(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"); +} diff --git a/tests-clar/online/clone.c b/tests-clar/online/clone.c index 8226bd054..6a46fa511 100644 --- a/tests-clar/online/clone.c +++ b/tests-clar/online/clone.c @@ -6,6 +6,9 @@ #define LIVE_REPO_URL "http://github.com/libgit2/TestGitRepository" #define LIVE_EMPTYREPO_URL "http://github.com/libgit2/TestEmptyRepository" +#define BB_REPO_URL "https://libgit2@bitbucket.org/libgit2/testgitrepository.git" +#define BB_REPO_URL_WITH_PASS "https://libgit2:libgit2@bitbucket.org/libgit2/testgitrepository.git" +#define BB_REPO_URL_WITH_WRONG_PASS "https://libgit2:wrong@bitbucket.org/libgit2/testgitrepository.git" static git_repository *g_repo; static git_clone_options g_options; @@ -150,4 +153,32 @@ void test_online_clone__credentials(void) g_options.cred_acquire_payload = &user_pass; cl_git_pass(git_clone(&g_repo, remote_url, "./foo", &g_options)); + 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" + }; + + g_options.cred_acquire_cb = git_cred_userpass; + g_options.cred_acquire_payload = &user_pass; + + 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"); + + /* User and pass from URL */ + user_pass.password = "wrong"; + 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"); + + /* 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"); } diff --git a/tests-clar/online/push.c b/tests-clar/online/push.c index 8f92cdd5e..56183473a 100644 --- a/tests-clar/online/push.c +++ b/tests-clar/online/push.c @@ -30,9 +30,15 @@ static git_oid _tag_tree; static git_oid _tag_blob; static git_oid _tag_lightweight; -static int cred_acquire_cb(git_cred **cred, const char *url, unsigned int allowed_types, void *payload) +static int cred_acquire_cb( + 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); *((bool*)payload) = true; |
