2013-12-14 11:17:53 -05:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <cmocka.h>
|
|
|
|
#include <stdlib.h>
|
2013-12-15 14:38:23 -05:00
|
|
|
#include <string.h>
|
2013-12-14 12:15:43 -05:00
|
|
|
#include <glib.h>
|
2013-12-14 11:17:53 -05:00
|
|
|
|
|
|
|
#include "xmpp/xmpp.h"
|
2013-12-18 18:06:43 -05:00
|
|
|
#include "xmpp/mock_xmpp.h"
|
|
|
|
|
2013-12-14 11:17:53 -05:00
|
|
|
#include "ui/ui.h"
|
2013-12-19 16:05:39 -05:00
|
|
|
#include "ui/mock_ui.h"
|
2013-12-18 18:06:43 -05:00
|
|
|
|
2013-12-15 11:10:32 -05:00
|
|
|
#include "command/commands.h"
|
2013-12-14 11:17:53 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
|
|
|
|
static jabber_conn_status_t _mock_jabber_connect_with_details_no_altdomain(const char * const jid,
|
|
|
|
const char * const passwd, const char * const altdomain)
|
|
|
|
{
|
|
|
|
check_expected(jid);
|
|
|
|
check_expected(passwd);
|
|
|
|
return (jabber_conn_status_t)mock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static jabber_conn_status_t _mock_jabber_connect_with_details_altdomain(const char * const jid,
|
|
|
|
const char * const passwd, const char * const altdomain)
|
|
|
|
{
|
|
|
|
check_expected(altdomain);
|
|
|
|
return (jabber_conn_status_t)mock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static jabber_conn_status_t _mock_jabber_connect_with_details_result(const char * const jid,
|
|
|
|
const char * const passwd, const char * const altdomain)
|
|
|
|
{
|
|
|
|
return (jabber_conn_status_t)mock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static jabber_conn_status_t _mock_jabber_connect_with_account_result(const ProfAccount * const account)
|
|
|
|
{
|
|
|
|
return (jabber_conn_status_t)mock();
|
|
|
|
}
|
|
|
|
|
|
|
|
static jabber_conn_status_t _mock_jabber_connect_with_account_result_check(const ProfAccount * const account)
|
|
|
|
{
|
|
|
|
check_expected(account);
|
|
|
|
return (jabber_conn_status_t)mock();
|
|
|
|
}
|
|
|
|
|
2013-12-14 13:43:19 -05:00
|
|
|
static void test_with_connection_status(jabber_conn_status_t status)
|
2013-12-14 11:17:53 -05:00
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
mock_cons_show();
|
2013-12-14 12:15:43 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
2013-12-15 15:38:26 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(status);
|
|
|
|
|
2013-12-19 16:05:39 -05:00
|
|
|
expect_cons_show("You are either connected already, or a login is in process.");
|
2013-12-15 15:38:26 -05:00
|
|
|
|
2013-12-15 13:08:26 -05:00
|
|
|
gboolean result = cmd_connect(NULL, *help);
|
2013-12-14 13:43:19 -05:00
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
}
|
|
|
|
|
2013-12-15 13:08:26 -05:00
|
|
|
void cmd_connect_shows_message_when_disconnecting(void **state)
|
2013-12-14 13:43:19 -05:00
|
|
|
{
|
|
|
|
test_with_connection_status(JABBER_DISCONNECTING);
|
|
|
|
}
|
|
|
|
|
2013-12-15 13:08:26 -05:00
|
|
|
void cmd_connect_shows_message_when_connecting(void **state)
|
2013-12-14 13:43:19 -05:00
|
|
|
{
|
|
|
|
test_with_connection_status(JABBER_CONNECTING);
|
|
|
|
}
|
|
|
|
|
2013-12-15 13:08:26 -05:00
|
|
|
void cmd_connect_shows_message_when_connected(void **state)
|
2013-12-14 13:43:19 -05:00
|
|
|
{
|
2013-12-15 13:08:26 -05:00
|
|
|
test_with_connection_status(JABBER_CONNECTED);
|
2013-12-14 13:43:19 -05:00
|
|
|
}
|
|
|
|
|
2013-12-15 13:08:26 -05:00
|
|
|
void cmd_connect_shows_message_when_undefined(void **state)
|
2013-12-14 13:43:19 -05:00
|
|
|
{
|
|
|
|
test_with_connection_status(JABBER_UNDEFINED);
|
|
|
|
}
|
2013-12-15 14:38:23 -05:00
|
|
|
|
|
|
|
void cmd_connect_when_no_account(void **state)
|
2013-12-14 13:43:19 -05:00
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
mock_cons_show();
|
2013-12-22 17:14:15 -05:00
|
|
|
mock_ui_ask_password();
|
2013-12-14 13:43:19 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
2013-12-15 14:38:23 -05:00
|
|
|
gchar *args[] = { "user@server.org", NULL };
|
2013-12-14 13:43:19 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 15:38:26 -05:00
|
|
|
|
2013-12-15 14:38:23 -05:00
|
|
|
expect_string(accounts_get_account, name, "user@server.org");
|
|
|
|
will_return(accounts_get_account, NULL);
|
2013-12-14 11:17:53 -05:00
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
mock_ui_ask_password_returns("password");
|
2013-12-14 11:17:53 -05:00
|
|
|
|
2013-12-19 16:05:39 -05:00
|
|
|
expect_cons_show("Connecting as user@server.org");
|
2013-12-14 13:43:19 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_details = _mock_jabber_connect_with_details_no_altdomain;
|
|
|
|
expect_string(_mock_jabber_connect_with_details_no_altdomain, jid, "user@server.org");
|
|
|
|
expect_string(_mock_jabber_connect_with_details_no_altdomain, passwd, "password");
|
|
|
|
will_return(_mock_jabber_connect_with_details_no_altdomain, JABBER_CONNECTING);
|
2013-12-14 13:43:19 -05:00
|
|
|
|
2013-12-15 14:38:23 -05:00
|
|
|
gboolean result = cmd_connect(args, *help);
|
2013-12-14 13:43:19 -05:00
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
}
|
2013-12-15 14:56:48 -05:00
|
|
|
|
|
|
|
void cmd_connect_with_altdomain_when_provided(void **state)
|
|
|
|
{
|
2013-12-22 17:14:15 -05:00
|
|
|
stub_ui_ask_password();
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-15 14:56:48 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "user@server.org", "altdomain" };
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 15:38:26 -05:00
|
|
|
|
2013-12-15 15:02:25 -05:00
|
|
|
expect_any(accounts_get_account, name);
|
2013-12-15 14:56:48 -05:00
|
|
|
will_return(accounts_get_account, NULL);
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_details = _mock_jabber_connect_with_details_altdomain;
|
|
|
|
expect_string(_mock_jabber_connect_with_details_altdomain, altdomain, "altdomain");
|
|
|
|
will_return(_mock_jabber_connect_with_details_altdomain, JABBER_CONNECTING);
|
2013-12-15 14:56:48 -05:00
|
|
|
|
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmd_connect_fail_message(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-22 17:14:15 -05:00
|
|
|
mock_cons_show_error();
|
|
|
|
stub_ui_ask_password();
|
2013-12-15 14:56:48 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
2013-12-15 15:02:25 -05:00
|
|
|
gchar *args[] = { "user@server.org", NULL };
|
2013-12-15 14:56:48 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 15:38:26 -05:00
|
|
|
|
2013-12-15 15:02:25 -05:00
|
|
|
expect_any(accounts_get_account, name);
|
2013-12-15 14:56:48 -05:00
|
|
|
will_return(accounts_get_account, NULL);
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_details = _mock_jabber_connect_with_details_result;
|
|
|
|
will_return(_mock_jabber_connect_with_details_result, JABBER_DISCONNECTED);
|
2013-12-15 14:56:48 -05:00
|
|
|
|
2013-12-22 17:14:15 -05:00
|
|
|
expect_cons_show_error("Connection attempt for user@server.org failed.");
|
2013-12-15 14:56:48 -05:00
|
|
|
|
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
}
|
2013-12-15 15:38:26 -05:00
|
|
|
|
|
|
|
void cmd_connect_lowercases_argument(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-22 17:14:15 -05:00
|
|
|
stub_ui_ask_password();
|
2013-12-15 15:38:26 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "USER@server.ORG", NULL };
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 15:38:26 -05:00
|
|
|
|
|
|
|
expect_string(accounts_get_account, name, "user@server.org");
|
|
|
|
will_return(accounts_get_account, NULL);
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_details = _mock_jabber_connect_with_details_result;
|
|
|
|
will_return(_mock_jabber_connect_with_details_result, JABBER_CONNECTING);
|
2013-12-15 15:38:26 -05:00
|
|
|
|
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
}
|
2013-12-15 15:52:30 -05:00
|
|
|
|
|
|
|
void cmd_connect_asks_password_when_not_in_account(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-22 17:14:15 -05:00
|
|
|
stub_ui_ask_password();
|
2013-12-15 15:52:30 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "jabber_org", NULL };
|
|
|
|
ProfAccount *account = malloc(sizeof(ProfAccount));
|
|
|
|
account->password = NULL;
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 15:52:30 -05:00
|
|
|
|
|
|
|
expect_any(accounts_get_account, name);
|
|
|
|
will_return(accounts_get_account, account);
|
|
|
|
|
|
|
|
will_return(accounts_create_full_jid, strdup("user@jabber.org"));
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_account = _mock_jabber_connect_with_account_result;
|
|
|
|
will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING);
|
2013-12-15 15:52:30 -05:00
|
|
|
|
2013-12-15 17:00:42 -05:00
|
|
|
expect_any(accounts_free_account, account);
|
|
|
|
|
2013-12-15 15:52:30 -05:00
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
free(account);
|
|
|
|
}
|
2013-12-15 16:23:58 -05:00
|
|
|
|
|
|
|
void cmd_connect_shows_message_when_connecting_with_account(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
mock_cons_show();
|
2013-12-15 16:23:58 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "jabber_org", NULL };
|
|
|
|
ProfAccount *account = malloc(sizeof(ProfAccount));
|
|
|
|
account->password = "password";
|
|
|
|
account->name = "jabber_org";
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 16:23:58 -05:00
|
|
|
|
|
|
|
expect_any(accounts_get_account, name);
|
|
|
|
will_return(accounts_get_account, account);
|
|
|
|
|
|
|
|
will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop"));
|
|
|
|
|
2013-12-19 16:05:39 -05:00
|
|
|
expect_cons_show("Connecting with account jabber_org as user@jabber.org/laptop");
|
2013-12-15 16:31:27 -05:00
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_account = _mock_jabber_connect_with_account_result;
|
|
|
|
will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING);
|
2013-12-15 16:31:27 -05:00
|
|
|
|
2013-12-15 17:00:42 -05:00
|
|
|
expect_any(accounts_free_account, account);
|
|
|
|
|
2013-12-15 16:31:27 -05:00
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
free(account);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmd_connect_connects_with_account(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-15 16:31:27 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "jabber_org", NULL };
|
|
|
|
ProfAccount *account = malloc(sizeof(ProfAccount));
|
|
|
|
account->password = "password";
|
|
|
|
account->name = "jabber_org";
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 16:31:27 -05:00
|
|
|
|
|
|
|
expect_any(accounts_get_account, name);
|
|
|
|
will_return(accounts_get_account, account);
|
|
|
|
|
|
|
|
will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop"));
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_account = _mock_jabber_connect_with_account_result_check;
|
|
|
|
expect_memory(_mock_jabber_connect_with_account_result_check, account, account, sizeof(ProfAccount));
|
|
|
|
will_return(_mock_jabber_connect_with_account_result_check, JABBER_CONNECTING);
|
2013-12-15 16:23:58 -05:00
|
|
|
|
2013-12-15 17:00:42 -05:00
|
|
|
expect_any(accounts_free_account, account);
|
|
|
|
|
2013-12-15 16:23:58 -05:00
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
free(account);
|
2013-12-15 17:00:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmd_connect_frees_account_after_connecting(void **state)
|
|
|
|
{
|
2013-12-19 16:05:39 -05:00
|
|
|
stub_cons_show();
|
2013-12-15 17:00:42 -05:00
|
|
|
CommandHelp *help = malloc(sizeof(CommandHelp));
|
|
|
|
gchar *args[] = { "jabber_org", NULL };
|
|
|
|
ProfAccount *account = malloc(sizeof(ProfAccount));
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
mock_connection_status(JABBER_DISCONNECTED);
|
2013-12-15 17:00:42 -05:00
|
|
|
|
|
|
|
expect_any(accounts_get_account, name);
|
|
|
|
will_return(accounts_get_account, account);
|
2013-12-15 16:31:27 -05:00
|
|
|
|
2013-12-15 17:00:42 -05:00
|
|
|
will_return(accounts_create_full_jid, strdup("user@jabber.org/laptop"));
|
|
|
|
|
2013-12-18 17:49:43 -05:00
|
|
|
jabber_connect_with_account = _mock_jabber_connect_with_account_result;
|
|
|
|
will_return(_mock_jabber_connect_with_account_result, JABBER_CONNECTING);
|
2013-12-15 17:00:42 -05:00
|
|
|
|
|
|
|
expect_memory(accounts_free_account, account, account, sizeof(ProfAccount));
|
|
|
|
|
|
|
|
gboolean result = cmd_connect(args, *help);
|
|
|
|
assert_true(result);
|
|
|
|
|
|
|
|
free(help);
|
|
|
|
free(account);
|
2013-12-15 16:23:58 -05:00
|
|
|
}
|