1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Added cmd_connect tests

This commit is contained in:
James Booth 2014-12-24 21:34:11 +00:00
parent 3487100c0f
commit 5319a03aeb
8 changed files with 132 additions and 115 deletions

View File

@ -69,6 +69,7 @@ tests_sources = \
tests/test_cmd_account.c tests/test_cmd_account.h \
tests/test_cmd_alias.c tests/test_cmd_alias.h \
tests/test_cmd_bookmark.c tests/test_cmd_bookmark.h \
tests/test_cmd_connect.c tests/test_cmd_connect.h \
tests/test_autocomplete.c tests/test_autocomplete.h \
tests/testsuite.c

View File

@ -41,6 +41,7 @@ gchar** accounts_get_list(void)
ProfAccount* accounts_get_account(const char * const name)
{
check_expected(name);
return (ProfAccount*)mock();
}

View File

@ -41,6 +41,7 @@ void cmd_account_shows_account_when_connected_and_no_args(void **state)
will_return(jabber_get_connection_status, JABBER_CONNECTED);
will_return(jabber_get_account_name, "account_name");
expect_any(accounts_get_account, name);
will_return(accounts_get_account, account);
expect_memory(cons_show_account, account, account, sizeof(ProfAccount));
@ -91,6 +92,7 @@ void cmd_account_show_shows_message_when_account_does_not_exist(void **state)
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "show", "account_name", NULL };
expect_any(accounts_get_account, name);
will_return(accounts_get_account, NULL);
expect_cons_show("No such account.");
@ -109,6 +111,7 @@ void cmd_account_show_shows_account_when_exists(void **state)
ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL,
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
expect_any(accounts_get_account, name);
will_return(accounts_get_account, account);
expect_memory(cons_show_account, account, account, sizeof(account));

View File

@ -7,22 +7,18 @@
#include <glib.h>
#include "xmpp/xmpp.h"
#include "xmpp/mock_xmpp.h"
#include "ui/ui.h"
#include "ui/mock_ui.h"
#include "ui/stub_ui.h"
#include "command/commands.h"
#include "config/accounts.h"
#include "config/mock_accounts.h"
static void test_with_connection_status(jabber_conn_status_t status)
{
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
mock_connection_status(status);
will_return(jabber_get_connection_status, status);
expect_cons_show("You are either connected already, or a login is in process.");
@ -54,13 +50,11 @@ void cmd_connect_shows_message_when_undefined(void **state)
void cmd_connect_shows_usage_when_no_server_value(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "server", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -73,13 +67,11 @@ void cmd_connect_shows_usage_when_no_server_value(void **state)
void cmd_connect_shows_usage_when_server_no_port_value(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "server", "aserver", "port", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -92,13 +84,11 @@ void cmd_connect_shows_usage_when_server_no_port_value(void **state)
void cmd_connect_shows_usage_when_no_port_value(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "port", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -111,13 +101,11 @@ void cmd_connect_shows_usage_when_no_port_value(void **state)
void cmd_connect_shows_usage_when_port_no_server_value(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "port", "5678", "server", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -130,12 +118,10 @@ void cmd_connect_shows_usage_when_port_no_server_value(void **state)
void cmd_connect_shows_message_when_port_0(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "0", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Value 0 out of range. Must be in 1..65535.");
expect_cons_show("");
@ -148,12 +134,10 @@ void cmd_connect_shows_message_when_port_0(void **state)
void cmd_connect_shows_message_when_port_minus1(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "-1", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Value -1 out of range. Must be in 1..65535.");
expect_cons_show("");
@ -166,12 +150,10 @@ void cmd_connect_shows_message_when_port_minus1(void **state)
void cmd_connect_shows_message_when_port_65536(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "65536", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Value 65536 out of range. Must be in 1..65535.");
expect_cons_show("");
@ -184,12 +166,10 @@ void cmd_connect_shows_message_when_port_65536(void **state)
void cmd_connect_shows_message_when_port_contains_chars(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "52f66", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Could not convert \"52f66\" to a number.");
expect_cons_show("");
@ -202,13 +182,11 @@ void cmd_connect_shows_message_when_port_contains_chars(void **state)
void cmd_connect_shows_usage_when_server_provided_twice(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "server", "server1", "server", "server2", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -221,13 +199,11 @@ void cmd_connect_shows_usage_when_server_provided_twice(void **state)
void cmd_connect_shows_usage_when_port_provided_twice(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "port", "1111", "port", "1111", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -240,13 +216,11 @@ void cmd_connect_shows_usage_when_port_provided_twice(void **state)
void cmd_connect_shows_usage_when_invalid_first_property(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "wrong", "server", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -259,13 +233,11 @@ void cmd_connect_shows_usage_when_invalid_first_property(void **state)
void cmd_connect_shows_usage_when_invalid_second_property(void **state)
{
stub_ui_ask_password();
mock_cons_show();
CommandHelp *help = malloc(sizeof(CommandHelp));
help->usage = "some usage";
gchar *args[] = { "user@server.org", "server", "aserver", "wrong", "1234", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
expect_cons_show("Usage: some usage");
expect_cons_show("");
@ -278,22 +250,23 @@ void cmd_connect_shows_usage_when_invalid_second_property(void **state)
void cmd_connect_when_no_account(void **state)
{
mock_cons_show();
mock_accounts_get_account();
mock_ui_ask_password();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_expect_and_return("user@server.org", NULL);
expect_string(accounts_get_account, name, "user@server.org");
will_return(accounts_get_account, NULL);
mock_ui_ask_password_returns("password");
will_return(ui_ask_password, strdup("password"));
expect_cons_show("Connecting as user@server.org");
jabber_connect_with_details_expect_and_return("user@server.org", "password", NULL, 0, JABBER_CONNECTING);
expect_string(jabber_connect_with_details, jid, "user@server.org");
expect_string(jabber_connect_with_details, passwd, "password");
expect_value(jabber_connect_with_details, altdomain, NULL);
expect_value(jabber_connect_with_details, port, 0);
will_return(jabber_connect_with_details, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -303,20 +276,23 @@ void cmd_connect_when_no_account(void **state)
void cmd_connect_with_server_when_provided(void **state)
{
mock_ui_ask_password();
stub_cons_show();
mock_accounts_get_account();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "server", "aserver", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(NULL);
expect_string(accounts_get_account, name, "user@server.org");
will_return(accounts_get_account, NULL);
mock_ui_ask_password_returns("password");
will_return(ui_ask_password, strdup("password"));
jabber_connect_with_details_expect_and_return("user@server.org", "password", "aserver", 0, JABBER_CONNECTING);
expect_cons_show("Connecting as user@server.org");
expect_string(jabber_connect_with_details, jid, "user@server.org");
expect_string(jabber_connect_with_details, passwd, "password");
expect_string(jabber_connect_with_details, altdomain, "aserver");
expect_value(jabber_connect_with_details, port, 0);
will_return(jabber_connect_with_details, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -326,20 +302,23 @@ void cmd_connect_with_server_when_provided(void **state)
void cmd_connect_with_port_when_provided(void **state)
{
mock_ui_ask_password();
stub_cons_show();
mock_accounts_get_account();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "5432", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(NULL);
expect_string(accounts_get_account, name, "user@server.org");
will_return(accounts_get_account, NULL);
mock_ui_ask_password_returns("password");
will_return(ui_ask_password, strdup("password"));
jabber_connect_with_details_expect_and_return("user@server.org", "password", NULL, 5432, JABBER_CONNECTING);
expect_cons_show("Connecting as user@server.org");
expect_string(jabber_connect_with_details, jid, "user@server.org");
expect_string(jabber_connect_with_details, passwd, "password");
expect_value(jabber_connect_with_details, altdomain, NULL);
expect_value(jabber_connect_with_details, port, 5432);
will_return(jabber_connect_with_details, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -349,20 +328,23 @@ void cmd_connect_with_port_when_provided(void **state)
void cmd_connect_with_server_and_port_when_provided(void **state)
{
mock_ui_ask_password();
stub_cons_show();
mock_accounts_get_account();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", "port", "5432", "server", "aserver", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(NULL);
expect_string(accounts_get_account, name, "user@server.org");
will_return(accounts_get_account, NULL);
mock_ui_ask_password_returns("password");
will_return(ui_ask_password, strdup("password"));
jabber_connect_with_details_expect_and_return("user@server.org", "password", "aserver", 5432, JABBER_CONNECTING);
expect_cons_show("Connecting as user@server.org");
expect_string(jabber_connect_with_details, jid, "user@server.org");
expect_string(jabber_connect_with_details, passwd, "password");
expect_string(jabber_connect_with_details, altdomain, "aserver");
expect_value(jabber_connect_with_details, port, 5432);
will_return(jabber_connect_with_details, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -372,19 +354,23 @@ void cmd_connect_with_server_and_port_when_provided(void **state)
void cmd_connect_fail_message(void **state)
{
stub_cons_show();
mock_cons_show_error();
stub_ui_ask_password();
mock_accounts_get_account();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "user@server.org", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(NULL);
expect_any(accounts_get_account, name);
will_return(accounts_get_account, NULL);
jabber_connect_with_details_return(JABBER_DISCONNECTED);
will_return(ui_ask_password, strdup("password"));
expect_cons_show("Connecting as user@server.org");
expect_any(jabber_connect_with_details, jid);
expect_any(jabber_connect_with_details, passwd);
expect_any(jabber_connect_with_details, altdomain);
expect_any(jabber_connect_with_details, port);
will_return(jabber_connect_with_details, JABBER_DISCONNECTED);
expect_cons_show_error("Connection attempt for user@server.org failed.");
@ -396,18 +382,23 @@ void cmd_connect_fail_message(void **state)
void cmd_connect_lowercases_argument(void **state)
{
stub_cons_show();
stub_ui_ask_password();
mock_accounts_get_account();
mock_jabber_connect_with_details();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "USER@server.ORG", NULL };
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_expect_and_return("user@server.org", NULL);
expect_string(accounts_get_account, name, "user@server.org");
will_return(accounts_get_account, NULL);
jabber_connect_with_details_return(JABBER_CONNECTING);
will_return(ui_ask_password, strdup("password"));
expect_cons_show("Connecting as user@server.org");
expect_any(jabber_connect_with_details, jid);
expect_any(jabber_connect_with_details, passwd);
expect_any(jabber_connect_with_details, altdomain);
expect_any(jabber_connect_with_details, port);
will_return(jabber_connect_with_details, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -417,22 +408,22 @@ void cmd_connect_lowercases_argument(void **state)
void cmd_connect_asks_password_when_not_in_account(void **state)
{
stub_cons_show();
mock_ui_ask_password();
mock_accounts_get_account();
mock_jabber_connect_with_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL,
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(account);
expect_any(accounts_get_account, name);
will_return(accounts_get_account, account);
mock_ui_ask_password_returns("password");
will_return(ui_ask_password, strdup("password"));
jabber_connect_with_account_return(JABBER_CONNECTING);
expect_cons_show("Connecting with account jabber_org as me@jabber.org");
expect_any(jabber_connect_with_account, account);
will_return(jabber_connect_with_account, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -442,21 +433,20 @@ void cmd_connect_asks_password_when_not_in_account(void **state)
void cmd_connect_shows_message_when_connecting_with_account(void **state)
{
mock_cons_show();
mock_accounts_get_account();
mock_jabber_connect_with_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password",
TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(account);
expect_any(accounts_get_account, name);
will_return(accounts_get_account, account);
expect_cons_show("Connecting with account jabber_org as user@jabber.org/laptop");
jabber_connect_with_account_return(JABBER_CONNECTING);
expect_any(jabber_connect_with_account, account);
will_return(jabber_connect_with_account, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);
@ -466,19 +456,20 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state)
void cmd_connect_connects_with_account(void **state)
{
stub_cons_show();
mock_accounts_get_account();
mock_jabber_connect_with_account();
CommandHelp *help = malloc(sizeof(CommandHelp));
gchar *args[] = { "jabber_org", NULL };
ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password",
TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL);
mock_connection_status(JABBER_DISCONNECTED);
will_return(jabber_get_connection_status, JABBER_DISCONNECTED);
accounts_get_account_return(account);
expect_any(accounts_get_account, name);
will_return(accounts_get_account, account);
jabber_connect_with_account_expect_and_return(account, JABBER_CONNECTING);
expect_cons_show("Connecting with account jabber_org as me@jabber.org");
expect_memory(jabber_connect_with_account, account, account, sizeof(account));
will_return(jabber_connect_with_account, JABBER_CONNECTING);
gboolean result = cmd_connect(args, *help);
assert_true(result);

View File

@ -204,7 +204,7 @@ int main(int argc, char* argv[]) {
unit_test(find_twice_returns_second_when_two_match),
unit_test(find_five_times_finds_fifth),
unit_test(find_twice_returns_first_when_two_match_and_reset),
*/
unit_test(cmd_connect_shows_message_when_disconnecting),
unit_test(cmd_connect_shows_message_when_connecting),
unit_test(cmd_connect_shows_message_when_connected),
@ -230,7 +230,7 @@ int main(int argc, char* argv[]) {
unit_test(cmd_connect_shows_usage_when_port_provided_twice),
unit_test(cmd_connect_shows_usage_when_invalid_first_property),
unit_test(cmd_connect_shows_usage_when_invalid_second_property),
/*
unit_test(cmd_rooms_shows_message_when_disconnected),
unit_test(cmd_rooms_shows_message_when_disconnecting),
unit_test(cmd_rooms_shows_message_when_connecting),

View File

@ -19,6 +19,12 @@ expect_cons_show(char *expected)
expect_string(cons_show, output, expected);
}
void
expect_cons_show_error(char *expected)
{
expect_string(cons_show_error, output, expected);
}
// stubs
void ui_init(void) {}
@ -129,7 +135,7 @@ int ui_win_unread(int index)
char * ui_ask_password(void)
{
return NULL;
return mock_ptr_type(char *);
}
void ui_handle_stanza(const char * const msg) {}
@ -305,7 +311,16 @@ void cons_show_account(ProfAccount *account)
void cons_debug(const char * const msg, ...) {}
void cons_show_time(void) {}
void cons_show_word(const char * const word) {}
void cons_show_error(const char * const cmd, ...) {}
void cons_show_error(const char * const cmd, ...)
{
va_list args;
va_start(args, cmd);
vsnprintf(output, sizeof(output), cmd, args);
check_expected(output);
va_end(args);
}
void cons_show_contacts(GSList * list) {}
void cons_show_roster(GSList * list) {}
void cons_show_roster_group(const char * const group, GSList * list) {}

View File

@ -1 +1,2 @@
void expect_cons_show(char *expected);
void expect_cons_show_error(char *expected);

View File

@ -11,12 +11,17 @@ void jabber_init(const int disable_tls) {}
jabber_conn_status_t jabber_connect_with_details(const char * const jid,
const char * const passwd, const char * const altdomain, const int port)
{
return JABBER_CONNECTED;
check_expected(jid);
check_expected(passwd);
check_expected(altdomain);
check_expected(port);
return (jabber_conn_status_t)mock();
}
jabber_conn_status_t jabber_connect_with_account(const ProfAccount * const account)
{
return JABBER_CONNECTED;
check_expected(account);
return (jabber_conn_status_t)mock();
}
void jabber_disconnect(void) {}