From 8119025120b0b511aea8bc3f477d2aff835bd24b Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 27 Jul 2015 00:04:48 +0100 Subject: Fixed tests for new command format --- tests/unittests/test_cmd_connect.c | 315 ++++++++++++++----------------------- 1 file changed, 121 insertions(+), 194 deletions(-) (limited to 'tests/unittests/test_cmd_connect.c') diff --git a/tests/unittests/test_cmd_connect.c b/tests/unittests/test_cmd_connect.c index b812bf23..88ae75d2 100644 --- a/tests/unittests/test_cmd_connect.c +++ b/tests/unittests/test_cmd_connect.c @@ -14,18 +14,16 @@ #include "command/commands.h" #include "config/accounts.h" +#define CMD_CONNECT "/connect" + static void test_with_connection_status(jabber_conn_status_t status) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - will_return(jabber_get_connection_status, status); expect_cons_show("You are either connected already, or a login is in process."); - gboolean result = cmd_connect(NULL, NULL, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, NULL); assert_true(result); - - free(help); } void cmd_connect_shows_message_when_disconnecting(void **state) @@ -48,77 +46,153 @@ void cmd_connect_shows_message_when_undefined(void **state) test_with_connection_status(JABBER_UNDEFINED); } +void cmd_connect_when_no_account(void **state) +{ + 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_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, 0); + will_return(jabber_connect_with_details, JABBER_CONNECTING); + + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); + assert_true(result); +} + +void cmd_connect_fail_message(void **state) +{ + 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_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."); + + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); + assert_true(result); +} + +void cmd_connect_lowercases_argument(void **state) +{ + 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_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(NULL, CMD_CONNECT, args); + assert_true(result); +} + +void cmd_connect_asks_password_when_not_in_account(void **state) +{ + gchar *args[] = { "jabber_org", NULL }; + ProfAccount *account = account_new("jabber_org", "me@jabber.org", NULL, NULL, + TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); + + will_return(jabber_get_connection_status, JABBER_DISCONNECTED); + + expect_any(accounts_get_account, name); + will_return(accounts_get_account, account); + + will_return(ui_ask_password, strdup("password")); + + 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(NULL, CMD_CONNECT, args); + assert_true(result); +} + void cmd_connect_shows_usage_when_no_server_value(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "server", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_server_no_port_value(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "server", "aserver", "port", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_no_port_value(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "port", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_port_no_server_value(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "port", "5678", "server", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_message_when_port_0(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "0", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -126,15 +200,12 @@ void cmd_connect_shows_message_when_port_0(void **state) expect_cons_show("Value 0 out of range. Must be in 1..65535."); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_message_when_port_minus1(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "-1", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -142,15 +213,12 @@ void cmd_connect_shows_message_when_port_minus1(void **state) expect_cons_show("Value -1 out of range. Must be in 1..65535."); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_message_when_port_65536(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "65536", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -158,15 +226,12 @@ void cmd_connect_shows_message_when_port_65536(void **state) expect_cons_show("Value 65536 out of range. Must be in 1..65535."); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_message_when_port_contains_chars(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "52f66", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -174,109 +239,64 @@ void cmd_connect_shows_message_when_port_contains_chars(void **state) expect_cons_show("Could not convert \"52f66\" to a number."); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_server_provided_twice(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "server", "server1", "server", "server2", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_port_provided_twice(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "port", "1111", "port", "1111", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_invalid_first_property(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "wrong", "server", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_shows_usage_when_invalid_second_property(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); - help->usage = "some usage"; gchar *args[] = { "user@server.org", "server", "aserver", "wrong", "1234", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - expect_cons_show("Usage: some usage"); + expect_string(cons_bad_cmd_usage, cmd, CMD_CONNECT); expect_cons_show(""); - gboolean result = cmd_connect(NULL, args, *help); - assert_true(result); - - free(help); -} - -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_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, 0); - will_return(jabber_connect_with_details, JABBER_CONNECTING); - - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_with_server_when_provided(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "server", "aserver", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -294,15 +314,12 @@ void cmd_connect_with_server_when_provided(void **state) expect_value(jabber_connect_with_details, port, 0); will_return(jabber_connect_with_details, JABBER_CONNECTING); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_with_port_when_provided(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "5432", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -320,15 +337,12 @@ void cmd_connect_with_port_when_provided(void **state) expect_value(jabber_connect_with_details, port, 5432); will_return(jabber_connect_with_details, JABBER_CONNECTING); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_with_server_and_port_when_provided(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "user@server.org", "port", "5432", "server", "aserver", NULL }; will_return(jabber_get_connection_status, JABBER_DISCONNECTED); @@ -346,94 +360,12 @@ void cmd_connect_with_server_and_port_when_provided(void **state) expect_value(jabber_connect_with_details, port, 5432); will_return(jabber_connect_with_details, JABBER_CONNECTING); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); 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_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."); - - gboolean result = cmd_connect(NULL, 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_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(NULL, 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 = account_new("jabber_org", "me@jabber.org", NULL, NULL, - TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - - will_return(jabber_get_connection_status, JABBER_DISCONNECTED); - - expect_any(accounts_get_account, name); - will_return(accounts_get_account, account); - - will_return(ui_ask_password, strdup("password")); - - 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(NULL, args, *help); - assert_true(result); - - free(help); } void cmd_connect_shows_message_when_connecting_with_account(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "user@jabber.org", "password", NULL, TRUE, NULL, 0, "laptop", NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); @@ -448,15 +380,12 @@ void cmd_connect_shows_message_when_connecting_with_account(void **state) expect_any(jabber_connect_with_account, account); will_return(jabber_connect_with_account, JABBER_CONNECTING); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } void cmd_connect_connects_with_account(void **state) { - CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "jabber_org", NULL }; ProfAccount *account = account_new("jabber_org", "me@jabber.org", "password", NULL, TRUE, NULL, 0, NULL, NULL, NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL); @@ -471,8 +400,6 @@ void cmd_connect_connects_with_account(void **state) expect_memory(jabber_connect_with_account, account, account, sizeof(account)); will_return(jabber_connect_with_account, JABBER_CONNECTING); - gboolean result = cmd_connect(NULL, args, *help); + gboolean result = cmd_connect(NULL, CMD_CONNECT, args); assert_true(result); - - free(help); } -- cgit 1.4.1-2-gfad0