<commit_before>#include <glib.h>
#include <libssh/libssh.h>
#include <stdlib.h>

#include "ssh.h"

extern GQuark WSH_SSH_ERROR;

static const gchar* username = "worr";
static const gchar* password = "<PASSWORD>";
static const gchar* remote = "127.0.0.1";
static const gint 	port = 22;

static void host_not_reachable(void) {
	set_ssh_connect_res(SSH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError* err;
	gint ret = wsh_ssh_host(session, &err);

	g_assert(ret == -1);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 4);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void change_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_KNOWN_CHANGED);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError* err;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, FALSE, FALSE, &err);

	g_assert(ret == WSH_SSH_NEED_ADD_HOST_KEY);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 2);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void fail_add_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_NOT_KNOWN);
	set_ssh_write_knownhost_res(SSH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, TRUE, FALSE, &err);

	g_assert(ret == WSH_SSH_HOST_KEY_ERROR);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 1);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void add_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_NOT_KNOWN);
	set_ssh_write_knownhost_res(SSH_OK);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, TRUE, FALSE, &err);

	g_assert(ret == 0);
	g_assert(session->session != NULL);
	g_assert_no_error(err);

	g_free(session->session);
	g_slice_free(wsh_ssh_session_t, session);
}

static void authenticate_password_unsuccessfully(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_KNOWN_OK);
	set_ssh_userauth_list_ret(SSH_AUTH_METHOD_PASSWORD);
	set_ssh_userauth_password_ret(SSH_AUTH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	session->auth_type = WSH_SSH_AUTH_PASSWORD;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_ssh_authenticate(session, &err);

	g_assert(ret != 0);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 6);

	g_error_free(err);
	g_free(session->session);
	g_slice_free(wsh_ssh_session_t, session);
}

int main(int argc, char** argv) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/Library/SSH/UnreachableHost", host_not_reachable);
	g_test_add_func("/Library/SSH/ChangedHostKey", change_host_key);
	g_test_add_func("/Library/SSH/FailToAddHostKey", fail_add_host_key);
	g_test_add_func("/Library/SSH/AddHostKey", add_host_key);
	g_test_add_func("/Library/SSH/AuthenticateUnsuccessfully", authenticate_password_unsuccessfully);

	return g_test_run();
}
<commit_msg>Ensure that password denied error handled gracefully<commit_after>#include <glib.h>
#include <libssh/libssh.h>
#include <stdlib.h>

#include "ssh.h"

extern GQuark WSH_SSH_ERROR;

static const gchar* username = "worr";
static const gchar* password = "<PASSWORD>";
static const gchar* remote = "127.0.0.1";
static const gint 	port = 22;

static void host_not_reachable(void) {
	set_ssh_connect_res(SSH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError* err;
	gint ret = wsh_ssh_host(session, &err);

	g_assert(ret == -1);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 4);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void change_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_KNOWN_CHANGED);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError* err;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, FALSE, FALSE, &err);

	g_assert(ret == WSH_SSH_NEED_ADD_HOST_KEY);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 2);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void fail_add_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_NOT_KNOWN);
	set_ssh_write_knownhost_res(SSH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, TRUE, FALSE, &err);

	g_assert(ret == WSH_SSH_HOST_KEY_ERROR);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 1);

	g_error_free(err);
	g_slice_free(wsh_ssh_session_t, session);
}

static void add_host_key(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_NOT_KNOWN);
	set_ssh_write_knownhost_res(SSH_OK);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_verify_host_key(session, TRUE, FALSE, &err);

	g_assert(ret == 0);
	g_assert(session->session != NULL);
	g_assert_no_error(err);

	g_free(session->session);
	g_slice_free(wsh_ssh_session_t, session);
}

static void authenticate_password_unsuccessfully(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_KNOWN_OK);
	set_ssh_userauth_list_ret(SSH_AUTH_METHOD_PASSWORD);
	set_ssh_userauth_password_ret(SSH_AUTH_ERROR);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	session->auth_type = WSH_SSH_AUTH_PASSWORD;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_ssh_authenticate(session, &err);

	g_assert(ret != 0);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 6);

	g_error_free(err);
	g_free(session->session);
	g_slice_free(wsh_ssh_session_t, session);
}

static void authenticate_password_denied(void) {
	set_ssh_connect_res(SSH_OK);
	set_ssh_is_server_known_res(SSH_SERVER_KNOWN_OK);
	set_ssh_userauth_list_ret(SSH_AUTH_METHOD_PASSWORD);
	set_ssh_userauth_password_ret(SSH_AUTH_DENIED);

	wsh_ssh_session_t* session = g_slice_new0(wsh_ssh_session_t);
	session->hostname = remote;
	session->username = username;
	session->password = password;
	session->port = port;
	session->auth_type = WSH_SSH_AUTH_PASSWORD;
	GError *err = NULL;

	wsh_ssh_host(session, &err);
	gint ret = wsh_ssh_authenticate(session, &err);

	g_assert(ret != 0);
	g_assert(session->session == NULL);
	g_assert_error(err, WSH_SSH_ERROR, 7);

	g_error_free(err);
	g_free(session->session);
	g_slice_free(wsh_ssh_session_t, session);
}

int main(int argc, char** argv) {
	g_test_init(&argc, &argv, NULL);

	g_test_add_func("/Library/SSH/UnreachableHost", host_not_reachable);
	g_test_add_func("/Library/SSH/ChangedHostKey", change_host_key);
	g_test_add_func("/Library/SSH/FailToAddHostKey", fail_add_host_key);
	g_test_add_func("/Library/SSH/AddHostKey", add_host_key);

	g_test_add_func("/Library/SSH/AuthenticatePasswordUnsuccessfully",
		authenticate_password_unsuccessfully);
	g_test_add_func("/Library/SSH/AuthenticatePasswordFailure",
		authenticate_password_denied);

	return g_test_run();
}

{'fraction_non_alphanumeric': 0.12049180327868853, 'fraction_numerical': 0.005035128805620609, 'mean_word_length': 7.348973607038123, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4085060', 'n_tokens_mistral': 3581, 'n_tokens_neox': 3418, 'n_words': 512}