diff --git a/Makefile.am b/Makefile.am index b0b7c8be..107956b9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,7 +43,7 @@ core_sources = \ src/otr.c src/otr.h test_sources = \ - src/contact.c src/contact.h src/log.c src/common.c \ + src/contact.c src/contact.h src/common.c \ src/log.h src/profanity.c src/common.h \ src/profanity.h src/chat_session.c \ src/chat_session.h src/muc.c src/muc.h src/jid.h src/jid.c \ @@ -61,12 +61,16 @@ test_sources = \ src/config/accounts.h \ src/config/preferences.c src/config/preferences.h \ src/config/theme.c src/config/theme.h \ + src/otr.c src/otr.h \ tests/ui/mock_ui.c \ tests/xmpp/mock_xmpp.c \ + tests/log/mock_log.c \ tests/config/mock_accounts.c \ tests/test_autocomplete.c \ tests/test_common.c \ - tests/test_command.c \ + tests/test_cmd_connect.c \ + tests/test_cmd_account.c \ + tests/test_cmd_rooms.c \ tests/test_history.c \ tests/test_jid.c \ tests/test_parser.c \ diff --git a/src/command/commands.c b/src/command/commands.c index 5babefe8..147ccf42 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -45,7 +45,6 @@ #include "xmpp/xmpp.h" #include "xmpp/bookmark.h" -static char * _ask_password(void); static void _update_presence(const resource_presence_t presence, const char * const show, gchar **args); static gboolean _cmd_set_boolean_preference(gchar *arg, struct cmd_help_t help, @@ -74,19 +73,15 @@ cmd_connect(gchar **args, struct cmd_help_t help) ProfAccount *account = accounts_get_account(lower); if (account != NULL) { - if (account->resource != NULL) { - jid = create_fulljid(account->jid, account->resource); - } else { - jid = strdup(account->jid); - } - + jid = accounts_create_full_jid(account); if (account->password == NULL) { - account->password = _ask_password(); + account->password = ui_ask_password(); } cons_show("Connecting with account %s as %s", account->name, jid); conn_status = jabber_connect_with_account(account); + accounts_free_account(account); } else { - char *passwd = _ask_password(); + char *passwd = ui_ask_password(); jid = strdup(lower); cons_show("Connecting as %s", jid); conn_status = jabber_connect_with_details(jid, passwd, altdomain); @@ -99,7 +94,6 @@ cmd_connect(gchar **args, struct cmd_help_t help) log_debug("Connection attempt for %s failed", jid); } - accounts_free_account(account); free(jid); result = TRUE; @@ -2267,20 +2261,6 @@ cmd_otr(gchar **args, struct cmd_help_t help) #endif } - -// helper function that asks the user for a password and saves it in passwd -static char * -_ask_password(void) { - char *passwd = malloc(sizeof(char) * (MAX_PASSWORD_SIZE + 1)); - status_bar_get_password(); - status_bar_refresh(); - inp_block(); - inp_get_password(passwd); - inp_non_block(); - - return passwd; -} - // helper function for status change commands static void _update_presence(const resource_presence_t resource_presence, diff --git a/src/config/accounts.c b/src/config/accounts.c index 0422a991..6431cd9e 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -292,6 +292,16 @@ accounts_get_account(const char * const name) } } +char * +accounts_create_full_jid(ProfAccount *account) +{ + if (account->resource != NULL) { + return create_fulljid(account->jid, account->resource); + } else { + return strdup(account->jid); + } +} + void accounts_free_account(ProfAccount *account) { diff --git a/src/config/accounts.h b/src/config/accounts.h index 96289952..caa8e84c 100644 --- a/src/config/accounts.h +++ b/src/config/accounts.h @@ -81,5 +81,6 @@ void accounts_set_priority_all(const char * const account_name, const gint value gint accounts_get_priority_for_presence_type(const char * const account_name, resource_presence_t presence_type); void accounts_clear_password(const char * const account_name); +char * accounts_create_full_jid(ProfAccount *account); #endif diff --git a/src/ui/core.c b/src/ui/core.c index 507cfb02..1ff41b7d 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -1330,6 +1330,19 @@ ui_win_unread(int index) } } +char * +ui_ask_password(void) +{ + char *passwd = malloc(sizeof(char) * (MAX_PASSWORD_SIZE + 1)); + status_bar_get_password(); + status_bar_refresh(); + inp_block(); + inp_get_password(passwd); + inp_non_block(); + + return passwd; +} + static void _ui_draw_win_title(void) { diff --git a/src/ui/ui.h b/src/ui/ui.h index 5ec4debd..f6bb98ac 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -80,6 +80,7 @@ char * ui_recipient(int index); void ui_close_win(int index); gboolean ui_win_exists(int index); int ui_win_unread(int index); +char * ui_ask_password(void); // ui events void ui_contact_typing(const char * const from); diff --git a/tests/config/mock_accounts.c b/tests/config/mock_accounts.c index e81aff9b..8cd8b93e 100644 --- a/tests/config/mock_accounts.c +++ b/tests/config/mock_accounts.c @@ -41,7 +41,11 @@ char * accounts_find_enabled(char *prefix) void accounts_reset_all_search(void) {} void accounts_reset_enabled_search(void) {} -void accounts_add(const char *jid, const char *altdomain) {} + +void accounts_add(const char *jid, const char *altdomain) +{ + check_expected(jid); +} gchar** accounts_get_list(void) { @@ -50,35 +54,55 @@ gchar** accounts_get_list(void) ProfAccount* accounts_get_account(const char * const name) { + check_expected(name); return (ProfAccount *)mock(); } -void accounts_free_account(ProfAccount *account) {} +void accounts_free_account(ProfAccount *account) +{ + check_expected(account); +} gboolean accounts_enable(const char * const name) { + check_expected(name); return (gboolean)mock(); } gboolean accounts_disable(const char * const name) { + check_expected(name); return (gboolean)mock(); } gboolean accounts_rename(const char * const account_name, const char * const new_name) { + check_expected(account_name); + check_expected(new_name); return (gboolean)mock(); } gboolean accounts_account_exists(const char * const account_name) { + check_expected(account_name); return (gboolean)mock(); } -void accounts_set_jid(const char * const account_name, const char * const value) {} +void accounts_set_jid(const char * const account_name, const char * const value) +{ + check_expected(account_name); + check_expected(value); +} + void accounts_set_server(const char * const account_name, const char * const value) {} -void accounts_set_resource(const char * const account_name, const char * const value) {} + +void accounts_set_resource(const char * const account_name, const char * const value) +{ + check_expected(account_name); + check_expected(value); +} + void accounts_set_password(const char * const account_name, const char * const value) {} void accounts_set_muc_service(const char * const account_name, const char * const value) {} void accounts_set_muc_nick(const char * const account_name, const char * const value) {} @@ -110,3 +134,8 @@ gint accounts_get_priority_for_presence_type(const char * const account_name, void accounts_clear_password(const char * const account_name) {} +char * accounts_create_full_jid(ProfAccount *account) +{ + return (char *)mock(); +} + diff --git a/tests/log/mock_log.c b/tests/log/mock_log.c new file mode 100644 index 00000000..a5e6a950 --- /dev/null +++ b/tests/log/mock_log.c @@ -0,0 +1,59 @@ +/* + * mock_log.c + * + * Copyright (C) 2012, 2013 James Booth + * + * This file is part of Profanity. + * + * Profanity is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Profanity is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Profanity. If not, see . + * + */ + +#include +#include +#include + +#include "log.h" + +void log_init(log_level_t filter) {} +log_level_t log_get_filter(void) +{ + return (log_level_t)mock(); +} +void log_close(void) {} +void log_debug(const char * const msg, ...) {} +void log_info(const char * const msg, ...) {} +void log_warning(const char * const msg, ...) {} +void log_error(const char * const msg, ...) {} +void log_msg(log_level_t level, const char * const area, + const char * const msg) {} + +log_level_t log_level_from_string(char *log_level) +{ + return (log_level_t)mock(); +} + +void chat_log_init(void) {} +void chat_log_chat(const gchar * const login, gchar *other, + const gchar * const msg, chat_log_direction_t direction, GTimeVal *tv_stamp) {} +void chat_log_close(void) {} +GSList * chat_log_get_previous(const gchar * const login, + const gchar * const recipient, GSList *history) +{ + return (GSList *)mock(); +} + +void groupchat_log_init(void) {} +void groupchat_log_chat(const gchar * const login, const gchar * const room, + const gchar * const nick, const gchar * const msg) {} diff --git a/tests/test_cmd_account.c b/tests/test_cmd_account.c new file mode 100644 index 00000000..2e43bca6 --- /dev/null +++ b/tests/test_cmd_account.c @@ -0,0 +1,522 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "xmpp/xmpp.h" +#include "ui/ui.h" +#include "command/commands.h" + +void cmd_account_shows_usage_when_not_connected_and_no_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { NULL }; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_shows_account_when_connected_and_no_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + ProfAccount *account = malloc(sizeof(ProfAccount)); + gchar *args[] = { NULL }; + + 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)); + + expect_any(accounts_free_account, account); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); + free(account); +} + +void cmd_account_list_shows_accounts(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "list", NULL }; + + gchar **accounts = malloc(sizeof(gchar *) * 4); + accounts[0] = strdup("account1"); + accounts[1] = strdup("account2"); + accounts[2] = strdup("account3"); + accounts[3] = NULL; + + will_return(accounts_get_list, accounts); + + expect_memory(cons_show_account_list, accounts, accounts, sizeof(accounts)); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_show_shows_usage_when_no_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "show", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +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_string(cons_show, output, "No such account."); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_show_shows_message_when_account_exists(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "show", "account_name", NULL }; + ProfAccount *account = malloc(sizeof(ProfAccount)); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + expect_memory(cons_show_account, account, account, sizeof(ProfAccount)); + + expect_any(accounts_free_account, account); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_add_shows_usage_when_no_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "add", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_add_adds_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "add", "new_account", NULL }; + + expect_string(accounts_add, jid, "new_account"); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_add_shows_message(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "add", "new_account", NULL }; + + expect_any(accounts_add, jid); + + expect_string(cons_show, output, "Account created.");; + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_enable_shows_usage_when_no_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "enable", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_enable_enables_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "enable", "account_name", NULL }; + + expect_string(accounts_enable, name, "account_name"); + will_return(accounts_enable, TRUE); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_enable_shows_message_when_enabled(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "enable", "account_name", NULL }; + + expect_any(accounts_enable, name); + will_return(accounts_enable, TRUE); + + expect_string(cons_show, output, "Account enabled."); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_enable_shows_message_when_account_doesnt_exist(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "enable", "account_name", NULL }; + + expect_any(accounts_enable, name); + will_return(accounts_enable, FALSE); + + expect_string(cons_show, output, "No such account: account_name"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_disable_shows_usage_when_no_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "disable", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_disable_disables_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "disable", "account_name", NULL }; + + expect_string(accounts_disable, name, "account_name"); + will_return(accounts_disable, TRUE); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_disable_shows_message_when_disabled(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "disable", "account_name", NULL }; + + expect_any(accounts_disable, name); + will_return(accounts_disable, TRUE); + + expect_string(cons_show, output, "Account disabled."); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_disable_shows_message_when_account_doesnt_exist(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "disable", "account_name", NULL }; + + expect_any(accounts_disable, name); + will_return(accounts_disable, FALSE); + + expect_string(cons_show, output, "No such account: account_name"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_rename_shows_usage_when_no_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "rename", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_rename_shows_usage_when_one_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "rename", "original_name", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_rename_renames_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "rename", "original_name", "new_name", NULL }; + + expect_string(accounts_rename, account_name, "original_name"); + expect_string(accounts_rename, new_name, "new_name"); + will_return(accounts_rename, TRUE); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_rename_shows_message_when_renamed(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "rename", "original_name", "new_name", NULL }; + + expect_any(accounts_rename, account_name); + expect_any(accounts_rename, new_name); + will_return(accounts_rename, TRUE); + + expect_string(cons_show, output, "Account renamed."); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_rename_shows_message_when_not_renamed(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "rename", "original_name", "new_name", NULL }; + + expect_any(accounts_rename, account_name); + expect_any(accounts_rename, new_name); + will_return(accounts_rename, FALSE); + + expect_string(cons_show, output, "Either account original_name doesn't exist, or account new_name already exists."); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_usage_when_no_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_usage_when_one_arg(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_usage_when_two_args(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", NULL }; + + expect_string(cons_show, output, "Usage: some usage"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_checks_account_exists(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; + + expect_string(accounts_account_exists, account_name, "a_account"); + will_return(accounts_account_exists, FALSE); + + expect_any_count(cons_show, output, 2); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_shows_message_when_account_doesnt_exist(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "a_property", "a_value", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, FALSE); + + expect_string(cons_show, output, "Account a_account doesn't exist"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_shows_message_for_malformed_jid(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "@malformed", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_string(cons_show, output, "Malformed jid: @malformed"); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_sets_barejid(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_string(accounts_set_jid, account_name, "a_account"); + expect_string(accounts_set_jid, value, "a_local@a_domain"); + + expect_string(cons_show, output, "Updated jid for account a_account: a_local@a_domain"); + + expect_any(accounts_set_resource, account_name); + expect_any(accounts_set_resource, value); + + expect_any(cons_show, output); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} + +void cmd_account_set_jid_sets_resource(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "set", "a_account", "jid", "a_local@a_domain/a_resource", NULL }; + + expect_any(accounts_account_exists, account_name); + will_return(accounts_account_exists, TRUE); + + expect_any(accounts_set_jid, account_name); + expect_any(accounts_set_jid, value); + + expect_any(cons_show, output); + + expect_string(accounts_set_resource, account_name, "a_account"); + expect_string(accounts_set_resource, value, "a_resource"); + + expect_string(cons_show, output, "Updated resource for account a_account: a_resource"); + expect_string(cons_show, output, ""); + + gboolean result = cmd_account(args, *help); + assert_true(result); + + free(help); +} diff --git a/tests/test_cmd_account.h b/tests/test_cmd_account.h new file mode 100644 index 00000000..62783609 --- /dev/null +++ b/tests/test_cmd_account.h @@ -0,0 +1,30 @@ +void cmd_account_shows_usage_when_not_connected_and_no_args(void **state); +void cmd_account_shows_account_when_connected_and_no_args(void **state); +void cmd_account_list_shows_accounts(void **state); +void cmd_account_show_shows_usage_when_no_arg(void **state); +void cmd_account_show_shows_message_when_account_does_not_exist(void **state); +void cmd_account_show_shows_message_when_account_exists(void **state); +void cmd_account_add_shows_usage_when_no_arg(void **state); +void cmd_account_add_adds_account(void **state); +void cmd_account_add_shows_message(void **state); +void cmd_account_enable_shows_usage_when_no_arg(void **state); +void cmd_account_enable_enables_account(void **state); +void cmd_account_enable_shows_message_when_enabled(void **state); +void cmd_account_enable_shows_message_when_account_doesnt_exist(void **state); +void cmd_account_disable_shows_usage_when_no_arg(void **state); +void cmd_account_disable_disables_account(void **state); +void cmd_account_disable_shows_message_when_disabled(void **state); +void cmd_account_disable_shows_message_when_account_doesnt_exist(void **state); +void cmd_account_rename_shows_usage_when_no_args(void **state); +void cmd_account_rename_shows_usage_when_one_arg(void **state); +void cmd_account_rename_renames_account(void **state); +void cmd_account_rename_shows_message_when_renamed(void **state); +void cmd_account_rename_shows_message_when_not_renamed(void **state); +void cmd_account_set_shows_usage_when_no_args(void **state); +void cmd_account_set_shows_usage_when_one_arg(void **state); +void cmd_account_set_shows_usage_when_two_args(void **state); +void cmd_account_set_checks_account_exists(void **state); +void cmd_account_set_shows_message_when_account_doesnt_exist(void **state); +void cmd_account_set_jid_shows_message_for_malformed_jid(void **state); +void cmd_account_set_jid_sets_barejid(void **state); +void cmd_account_set_jid_sets_resource(void **state); diff --git a/tests/test_cmd_connect.c b/tests/test_cmd_connect.c new file mode 100644 index 00000000..3625a521 --- /dev/null +++ b/tests/test_cmd_connect.c @@ -0,0 +1,261 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "xmpp/xmpp.h" +#include "ui/ui.h" +#include "command/commands.h" + +static void test_with_connection_status(jabber_conn_status_t status) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + + will_return(jabber_get_connection_status, status); + expect_string(cons_show, output, "You are either connected already, or a login is in process."); + + gboolean result = cmd_connect(NULL, *help); + assert_true(result); + + free(help); +} + +void cmd_connect_shows_message_when_disconnecting(void **state) +{ + test_with_connection_status(JABBER_DISCONNECTING); +} + +void cmd_connect_shows_message_when_connecting(void **state) +{ + test_with_connection_status(JABBER_CONNECTING); +} + +void cmd_connect_shows_message_when_connected(void **state) +{ + test_with_connection_status(JABBER_CONNECTED); +} + +void cmd_connect_shows_message_when_undefined(void **state) +{ + test_with_connection_status(JABBER_UNDEFINED); +} + +void cmd_connect_when_no_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "user@server.org", NULL }; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_string(accounts_get_account, name, "user@server.org"); + will_return(accounts_get_account, NULL); + + will_return(ui_ask_password, strdup("password")); + + expect_string(cons_show, output, "Connecting as user@server.org"); + + expect_string(jabber_connect_with_details, jid, "user@server.org"); + expect_string(jabber_connect_with_details, passwd, "password"); + expect_any(jabber_connect_with_details, altdomain); + will_return(jabber_connect_with_details, JABBER_CONNECTING); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); +} + +void cmd_connect_with_altdomain_when_provided(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "user@server.org", "altdomain" }; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, NULL); + + will_return(ui_ask_password, strdup("password")); + + expect_any(cons_show, output); + + expect_any(jabber_connect_with_details, jid); + expect_any(jabber_connect_with_details, passwd); + expect_string(jabber_connect_with_details, altdomain, "altdomain"); + will_return(jabber_connect_with_details, JABBER_CONNECTING); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); +} + +void cmd_connect_fail_message(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "user@server.org", NULL }; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, NULL); + + will_return(ui_ask_password, strdup("password")); + + expect_any(cons_show, output); + + expect_any(jabber_connect_with_details, jid); + expect_any(jabber_connect_with_details, passwd); + expect_any(jabber_connect_with_details, altdomain); + will_return(jabber_connect_with_details, JABBER_DISCONNECTED); + + expect_string(cons_show_error, output, "Connection attempt for user@server.org failed."); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); +} + +void cmd_connect_lowercases_argument(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "USER@server.ORG", NULL }; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_string(accounts_get_account, name, "user@server.org"); + will_return(accounts_get_account, NULL); + + will_return(ui_ask_password, strdup("password")); + + expect_any(cons_show, output); + + expect_any(jabber_connect_with_details, jid); + expect_any(jabber_connect_with_details, passwd); + expect_any(jabber_connect_with_details, altdomain); + will_return(jabber_connect_with_details, JABBER_CONNECTING); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); +} + +void cmd_connect_asks_password_when_not_in_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "jabber_org", NULL }; + ProfAccount *account = malloc(sizeof(ProfAccount)); + account->password = NULL; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + will_return(accounts_create_full_jid, strdup("user@jabber.org")); + + will_return(ui_ask_password, strdup("password")); + + expect_any(cons_show, output); + + expect_any(jabber_connect_with_account, account); + will_return(jabber_connect_with_account, JABBER_CONNECTING); + + expect_any(accounts_free_account, account); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); + free(account); +} + +void cmd_connect_shows_message_when_connecting_with_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "jabber_org", NULL }; + ProfAccount *account = malloc(sizeof(ProfAccount)); + account->password = "password"; + account->name = "jabber_org"; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); + + expect_string(cons_show, output, "Connecting with account jabber_org as user@jabber.org/laptop"); + + expect_any(jabber_connect_with_account, account); + will_return(jabber_connect_with_account, JABBER_CONNECTING); + + expect_any(accounts_free_account, account); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); + free(account); +} + +void cmd_connect_connects_with_account(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "jabber_org", NULL }; + ProfAccount *account = malloc(sizeof(ProfAccount)); + account->password = "password"; + account->name = "jabber_org"; + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); + + expect_any(cons_show, output); + + expect_memory(jabber_connect_with_account, account, account, sizeof(ProfAccount)); + will_return(jabber_connect_with_account, JABBER_CONNECTING); + + expect_any(accounts_free_account, account); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); + free(account); +} + +void cmd_connect_frees_account_after_connecting(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "jabber_org", NULL }; + ProfAccount *account = malloc(sizeof(ProfAccount)); + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop")); + + expect_any(cons_show, output); + + expect_any(jabber_connect_with_account, account); + will_return(jabber_connect_with_account, JABBER_CONNECTING); + + expect_memory(accounts_free_account, account, account, sizeof(ProfAccount)); + + gboolean result = cmd_connect(args, *help); + assert_true(result); + + free(help); + free(account); +} diff --git a/tests/test_cmd_connect.h b/tests/test_cmd_connect.h new file mode 100644 index 00000000..2ee1a91b --- /dev/null +++ b/tests/test_cmd_connect.h @@ -0,0 +1,12 @@ +void cmd_connect_shows_message_when_disconnecting(void **state); +void cmd_connect_shows_message_when_connecting(void **state); +void cmd_connect_shows_message_when_connected(void **state); +void cmd_connect_shows_message_when_undefined(void **state); +void cmd_connect_when_no_account(void **state); +void cmd_connect_with_altdomain_when_provided(void **state); +void cmd_connect_fail_message(void **state); +void cmd_connect_lowercases_argument(void **state); +void cmd_connect_asks_password_when_not_in_account(void **state); +void cmd_connect_shows_message_when_connecting_with_account(void **state); +void cmd_connect_connects_with_account(void **state); +void cmd_connect_frees_account_after_connecting(void **state); diff --git a/tests/test_command.c b/tests/test_cmd_rooms.c similarity index 87% rename from tests/test_command.c rename to tests/test_cmd_rooms.c index 6dc51ecb..e39d9088 100644 --- a/tests/test_command.c +++ b/tests/test_cmd_rooms.c @@ -12,10 +12,10 @@ static void test_with_connection_status(jabber_conn_status_t status) { CommandHelp *help = malloc(sizeof(CommandHelp)); - + will_return(jabber_get_connection_status, status); - expect_string(cons_show, msg, "You are not currently connected."); - + expect_string(cons_show, output, "You are not currently connected."); + gboolean result = cmd_rooms(NULL, *help); assert_true(result); @@ -52,13 +52,14 @@ void cmd_rooms_uses_account_default_when_no_arg(void **state) CommandHelp *help = malloc(sizeof(CommandHelp)); ProfAccount *account = malloc(sizeof(ProfAccount)); account->muc_service = "default_conf_server"; - gchar *args[] = { NULL }; + gchar *args[] = { NULL }; will_return(jabber_get_connection_status, JABBER_CONNECTED); will_return(jabber_get_account_name, "account_name"); + expect_string(accounts_get_account, name, "account_name"); will_return(accounts_get_account, account); expect_string(iq_room_list_request, conferencejid, "default_conf_server"); - + gboolean result = cmd_rooms(args, *help); assert_true(result); @@ -67,14 +68,14 @@ void cmd_rooms_uses_account_default_when_no_arg(void **state) free(account); } -void cmd_arg_used_when_passed(void **state) +void cmd_rooms_arg_used_when_passed(void **state) { CommandHelp *help = malloc(sizeof(CommandHelp)); - gchar *args[] = { "conf_server_arg" }; + gchar *args[] = { "conf_server_arg" }; will_return(jabber_get_connection_status, JABBER_CONNECTED); expect_string(iq_room_list_request, conferencejid, "conf_server_arg"); - + gboolean result = cmd_rooms(args, *help); assert_true(result); diff --git a/tests/test_command.h b/tests/test_cmd_rooms.h similarity index 87% rename from tests/test_command.h rename to tests/test_cmd_rooms.h index 0f47be84..a9a7af83 100644 --- a/tests/test_command.h +++ b/tests/test_cmd_rooms.h @@ -4,4 +4,4 @@ void cmd_rooms_shows_message_when_connecting(void **state); void cmd_rooms_shows_message_when_started(void **state); void cmd_rooms_shows_message_when_undefined(void **state); void cmd_rooms_uses_account_default_when_no_arg(void **state); -void cmd_arg_used_when_passed(void **state); +void cmd_rooms_arg_used_when_passed(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index ee18fe6a..44c014df 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -5,7 +5,9 @@ #include "test_autocomplete.h" #include "test_common.h" -#include "test_command.h" +#include "test_cmd_connect.h" +#include "test_cmd_account.h" +#include "test_cmd_rooms.h" #include "test_history.h" #include "test_jid.h" #include "test_parser.h" @@ -13,14 +15,6 @@ int main(int argc, char* argv[]) { const UnitTest tests[] = { - unit_test(cmd_rooms_shows_message_when_disconnected), - unit_test(cmd_rooms_shows_message_when_disconnecting), - unit_test(cmd_rooms_shows_message_when_connecting), - unit_test(cmd_rooms_shows_message_when_started), - unit_test(cmd_rooms_shows_message_when_undefined), - unit_test(cmd_rooms_uses_account_default_when_no_arg), - unit_test(cmd_arg_used_when_passed), - unit_test(replace_one_substr), unit_test(replace_one_substr_beginning), unit_test(replace_one_substr_end), @@ -165,6 +159,58 @@ 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), + unit_test(cmd_connect_shows_message_when_undefined), + unit_test(cmd_connect_when_no_account), + unit_test(cmd_connect_with_altdomain_when_provided), + unit_test(cmd_connect_fail_message), + unit_test(cmd_connect_lowercases_argument), + unit_test(cmd_connect_asks_password_when_not_in_account), + unit_test(cmd_connect_shows_message_when_connecting_with_account), + unit_test(cmd_connect_connects_with_account), + unit_test(cmd_connect_frees_account_after_connecting), + + unit_test(cmd_rooms_shows_message_when_disconnected), + unit_test(cmd_rooms_shows_message_when_disconnecting), + unit_test(cmd_rooms_shows_message_when_connecting), + unit_test(cmd_rooms_shows_message_when_started), + unit_test(cmd_rooms_shows_message_when_undefined), + unit_test(cmd_rooms_uses_account_default_when_no_arg), + unit_test(cmd_rooms_arg_used_when_passed), + + unit_test(cmd_account_shows_usage_when_not_connected_and_no_args), + unit_test(cmd_account_shows_account_when_connected_and_no_args), + unit_test(cmd_account_list_shows_accounts), + unit_test(cmd_account_show_shows_usage_when_no_arg), + unit_test(cmd_account_show_shows_message_when_account_does_not_exist), + unit_test(cmd_account_show_shows_message_when_account_exists), + unit_test(cmd_account_add_shows_usage_when_no_arg), + unit_test(cmd_account_add_adds_account), + unit_test(cmd_account_add_shows_message), + unit_test(cmd_account_enable_shows_usage_when_no_arg), + unit_test(cmd_account_enable_enables_account), + unit_test(cmd_account_enable_shows_message_when_enabled), + unit_test(cmd_account_enable_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_disable_shows_usage_when_no_arg), + unit_test(cmd_account_disable_disables_account), + unit_test(cmd_account_disable_shows_message_when_disabled), + unit_test(cmd_account_disable_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_rename_shows_usage_when_no_args), + unit_test(cmd_account_rename_shows_usage_when_one_arg), + unit_test(cmd_account_rename_renames_account), + unit_test(cmd_account_rename_shows_message_when_renamed), + unit_test(cmd_account_rename_shows_message_when_not_renamed), + unit_test(cmd_account_set_shows_usage_when_no_args), + unit_test(cmd_account_set_shows_usage_when_one_arg), + unit_test(cmd_account_set_shows_usage_when_two_args), + unit_test(cmd_account_set_checks_account_exists), + unit_test(cmd_account_set_shows_message_when_account_doesnt_exist), + unit_test(cmd_account_set_jid_shows_message_for_malformed_jid), + unit_test(cmd_account_set_jid_sets_barejid), + unit_test(cmd_account_set_jid_sets_resource), }; return run_tests(tests); } diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index ed7dd4b1..6e3e190c 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -26,6 +26,8 @@ #include "ui/ui.h" +char output[256]; + // ui startup and control void ui_init(void) {} void ui_load_colours(void) {} @@ -173,6 +175,11 @@ gboolean ui_duck_exists(void) void ui_tidy_wins(void) {} void ui_prune_wins(void) {} +char * ui_ask_password(void) +{ + return (char *)mock(); +} + // create windows void create_title_bar(void) {} void create_status_bar(void) {} @@ -191,7 +198,11 @@ void title_bar_draw(void) {} // console window actions void cons_show(const char * const msg, ...) { - check_expected(msg); + va_list args; + va_start(args, msg); + vsnprintf(output, sizeof(output), msg, args); + check_expected(output); + va_end(args); } void cons_about(void) {} @@ -207,11 +218,25 @@ void cons_show_chat_prefs(void) {} void cons_show_log_prefs(void) {} void cons_show_presence_prefs(void) {} void cons_show_connection_prefs(void) {} -void cons_show_account(ProfAccount *account) {} + +void cons_show_account(ProfAccount *account) +{ + check_expected(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_highlight_show(const char * const cmd) {} void cons_show_contacts(GSList * list) {} void cons_show_roster(GSList * list) {} @@ -225,7 +250,12 @@ void cons_show_login_success(ProfAccount *account) {} void cons_show_software_version(const char * const jid, const char * const presence, const char * const name, const char * const version, const char * const os) {} -void cons_show_account_list(gchar **accounts) {} + +void cons_show_account_list(gchar **accounts) +{ + check_expected(accounts); +} + void cons_show_room_list(GSList *room, const char * const conference_node) {} void cons_show_bookmarks(const GList *list) {} void cons_show_disco_items(GSList *items, const char * const jid) {} diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index 8ba03cab..8907c13e 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -32,11 +32,15 @@ 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) { + check_expected(jid); + check_expected(passwd); + check_expected(altdomain); return (jabber_conn_status_t)mock(); } jabber_conn_status_t jabber_connect_with_account(const ProfAccount * const account) { + check_expected(account); return (jabber_conn_status_t)mock(); }