Commit e26b08d32c627efcbf7ab01a7296de89acc168fd

Carlos Martín Nieto 2014-06-25T17:05:27

ssh: adjust clone and push test credentials to the split user+pass method For urls where we do not specify a username, we must handle the case where the ssh transport asks us for the username. Test also that switching username fails.

diff --git a/tests/online/clone.c b/tests/online/clone.c
index de838e6..ffa414a 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -351,17 +351,45 @@ void test_online_clone__can_cancel(void)
 static int check_ssh_auth_methods(git_cred **cred, const char *url, const char *username_from_url,
 				  unsigned int allowed_types, void *data)
 {
+	int *with_user = (int *) data;
 	GIT_UNUSED(cred); GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(data);
 
-	cl_assert_equal_i(GIT_CREDTYPE_SSH_KEY | GIT_CREDTYPE_SSH_CUSTOM, allowed_types);
+	if (!*with_user)
+		cl_assert_equal_i(GIT_CREDTYPE_USERNAME, allowed_types);
+	else
+		cl_assert(!(allowed_types & GIT_CREDTYPE_USERNAME));
 
 	return GIT_EUSER;
 }
 
 void test_online_clone__ssh_auth_methods(void)
 {
+	int with_user;
+
 	g_options.remote_callbacks.credentials = check_ssh_auth_methods;
+	g_options.remote_callbacks.payload = &with_user;
 
+	with_user = 0;
 	cl_git_fail_with(GIT_EUSER,
 		git_clone(&g_repo, SSH_REPO_URL, "./foo", &g_options));
+
+	with_user = 1;
+	cl_git_fail_with(GIT_EUSER,
+		git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
+}
+
+static int cred_foo_bar(git_cred **cred, const char *url, const char *username_from_url,
+				  unsigned int allowed_types, void *data)
+
+{
+	GIT_UNUSED(url); GIT_UNUSED(username_from_url); GIT_UNUSED(allowed_types); GIT_UNUSED(data);
+
+	return git_cred_userpass_plaintext_new(cred, "foo", "bar");
+}
+
+void test_online_clone__ssh_cannot_change_username(void)
+{
+	g_options.remote_callbacks.credentials = cred_foo_bar;
+
+	cl_git_fail(git_clone(&g_repo, "ssh://git@github.com/libgit2/TestGitRepository", "./foo", &g_options));
 }
diff --git a/tests/online/push.c b/tests/online/push.c
index 6da27bb..50419ef 100644
--- a/tests/online/push.c
+++ b/tests/online/push.c
@@ -50,6 +50,15 @@ static int cred_acquire_cb(
 	GIT_UNUSED(user_from_url);
 	GIT_UNUSED(payload);
 
+	if (GIT_CREDTYPE_USERNAME & allowed_types) {
+		if (!_remote_user) {
+			printf("GITTEST_REMOTE_USER must be set\n");
+			return -1;
+		}
+
+		return git_cred_username_new(cred, _remote_user);
+	}
+
 	if (GIT_CREDTYPE_DEFAULT & allowed_types) {
 		if (!_remote_default) {
 			printf("GITTEST_REMOTE_DEFAULT must be set to use NTLM/Negotiate credentials\n");