diff --git a/src/muc.c b/src/muc.c index 1dcc457f..3cc410bf 100644 --- a/src/muc.c +++ b/src/muc.c @@ -54,6 +54,16 @@ muc_init(void) invite_ac = autocomplete_new(); } +void +muc_close(void) +{ + autocomplete_free(invite_ac); + if (rooms != NULL) { + g_hash_table_destroy(rooms); + rooms = NULL; + } +} + void muc_add_invite(char *room) { diff --git a/src/muc.h b/src/muc.h index 3d12452e..ce5602c4 100644 --- a/src/muc.h +++ b/src/muc.h @@ -30,6 +30,7 @@ #include "tools/autocomplete.h" void muc_init(void); +void muc_close(void); void muc_join_room(const char * const room, const char * const nick); void muc_leave_room(const char * const room); gboolean muc_room_is_active(Jid *jid); diff --git a/src/profanity.c b/src/profanity.c index 18e15900..c378d1d8 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -324,6 +324,7 @@ _shutdown(void) jabber_shutdown(); plugins_on_shutdown(); roster_free(); + muc_close(); caps_close(); ui_close(); chat_log_close(); diff --git a/src/server_events.c b/src/server_events.c index 36a1a4b1..14df41e8 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -44,7 +44,7 @@ handle_presence_error(const char *from, const char * const type, const char *err_msg) { // handle nickname conflict on entering room - if (g_strcmp0(err_msg, "conflict") == 0) { + if ((from != NULL) && g_strcmp0(err_msg, "conflict") == 0) { // remove the room from muc Jid *room_jid = jid_create(from); if (!muc_get_roster_received(room_jid->barejid)) { @@ -68,20 +68,20 @@ void handle_message_error(const char * const from, const char * const type, const char * const err_msg) { + // handle errors from no recipient + if (from == NULL) { + ui_handle_error(err_msg); + // handle recipient not found ('from' contains a value and type is 'cancel') - if ((from != NULL) && ((type != NULL && (strcmp(type, "cancel") == 0)))) { + } else if (type != NULL && (strcmp(type, "cancel") == 0)) { ui_handle_recipient_not_found(from, err_msg); if (prefs_get_boolean(PREF_STATES) && chat_session_exists(from)) { chat_session_set_recipient_supports(from, FALSE); } // handle any other error from recipient - } else if (from != NULL) { - ui_handle_recipient_error(from, err_msg); - - // handle errors from no recipient } else { - ui_handle_error(err_msg); + ui_handle_recipient_error(from, err_msg); } } diff --git a/tests/test_server_events.c b/tests/test_server_events.c index 547e3d70..ef5620fd 100644 --- a/tests/test_server_events.c +++ b/tests/test_server_events.c @@ -12,6 +12,7 @@ #include "config/preferences.h" #include "ui/ui.h" #include "ui/mock_ui.h" +#include "muc.h" void console_doesnt_show_online_presence_when_set_none(void **state) { @@ -106,49 +107,129 @@ void console_shows_dnd_presence_when_set_all(void **state) roster_clear(); } -void handle_message_stanza_error_when_no_from(void **state) +void handle_message_error_when_no_recipient(void **state) { char *err_msg = "Some error."; + char *from = NULL; + char *type = "cancel"; expect_ui_handle_error(err_msg); - handle_message_error(NULL, "cancel", err_msg); + handle_message_error(from, type, err_msg); } -void handle_message_stanza_error_from_cancel(void **state) +void handle_message_error_when_recipient_cancel(void **state) { char *err_msg = "Some error."; char *from = "bob@server.com"; + char *type = "cancel"; + prefs_set_boolean(PREF_STATES, FALSE); chat_sessions_init(); expect_ui_handle_recipient_not_found(from, err_msg); - handle_message_error(from, "cancel", err_msg); + handle_message_error(from, type, err_msg); } -void handle_message_stanza_error_from_cancel_disables_chat_session(void **state) +void handle_message_error_when_recipient_cancel_disables_chat_session(void **state) { char *err_msg = "Some error."; char *from = "bob@server.com"; + char *type = "cancel"; + stub_ui_handle_recipient_not_found(); prefs_set_boolean(PREF_STATES, TRUE); chat_sessions_init(); chat_session_start(from, TRUE); - handle_message_error(from, "cancel", err_msg); + handle_message_error(from, type, err_msg); gboolean chat_session_supported = chat_session_get_recipient_supports(from); assert_false(chat_session_supported); chat_sessions_clear(); } -void handle_message_stanza_error_from_no_type(void **state) +void handle_message_error_when_recipient_and_no_type(void **state) { char *err_msg = "Some error."; char *from = "bob@server.com"; + char *type = NULL; expect_ui_handle_recipient_error(from, err_msg); - handle_message_error(from, NULL, err_msg); + handle_message_error(from, type, err_msg); +} + +void handle_presence_error_when_no_recipient(void **state) +{ + char *err_msg = "Some error."; + char *from = NULL; + char *type = NULL; + + expect_ui_handle_error(err_msg); + + handle_presence_error(from, type, err_msg); +} + +void handle_presence_error_when_no_recipient_and_conflict(void **state) +{ + char *err_msg = "conflict"; + char *from = NULL; + char *type = NULL; + + expect_ui_handle_error(err_msg); + + handle_presence_error(from, type, err_msg); +} + +void handle_presence_error_when_nick_conflict_shows_recipient_error(void **state) +{ + char *err_msg = "conflict"; + char *from = "room@rooms.org/nick"; + char *barejid = "room@rooms.org"; + char *nick = "nick"; + char *type = NULL; + + muc_init(); + muc_join_room(barejid, nick); + + expect_ui_handle_recipient_error(barejid, err_msg); + + handle_presence_error(from, type, err_msg); + + muc_close(); +} + +void handle_presence_error_when_nick_conflict_does_not_join_room(void **state) +{ + char *err_msg = "conflict"; + char *from = "room@rooms.org/nick"; + char *barejid = "room@rooms.org"; + char *nick = "nick"; + char *type = NULL; + Jid *jidp = jid_create(from); + stub_ui_handle_recipient_error(); + + muc_init(); + muc_join_room(barejid, nick); + + handle_presence_error(from, type, err_msg); + + gboolean room_is_active = muc_room_is_active(jidp); + assert_false(room_is_active); + + muc_close(); + jid_destroy(jidp); +} + +void handle_presence_error_when_from_recipient_not_conflict(void **state) +{ + char *err_msg = "Some error."; + char *from = "bob@server.com"; + char *type = NULL; + + expect_ui_handle_recipient_error(from, err_msg); + + handle_presence_error(from, type, err_msg); } diff --git a/tests/test_server_events.h b/tests/test_server_events.h index c0f5dec4..1356de15 100644 --- a/tests/test_server_events.h +++ b/tests/test_server_events.h @@ -4,7 +4,12 @@ void console_shows_online_presence_when_set_all(void **state); void console_doesnt_show_dnd_presence_when_set_none(void **state); void console_doesnt_show_dnd_presence_when_set_online(void **state); void console_shows_dnd_presence_when_set_all(void **state); -void handle_message_stanza_error_when_no_from(void **state); -void handle_message_stanza_error_from_cancel(void **stanza); -void handle_message_stanza_error_from_cancel_disables_chat_session(void **stanza); -void handle_message_stanza_error_from_no_type(void **state); +void handle_message_error_when_no_recipient(void **state); +void handle_message_error_when_recipient_cancel(void **stanza); +void handle_message_error_when_recipient_cancel_disables_chat_session(void **stanza); +void handle_message_error_when_recipient_and_no_type(void **state); +void handle_presence_error_when_no_recipient(void **state); +void handle_presence_error_when_no_recipient_and_conflict(void **state); +void handle_presence_error_when_nick_conflict_shows_recipient_error(void **state); +void handle_presence_error_when_nick_conflict_does_not_join_room(void **state); +void handle_presence_error_when_from_recipient_not_conflict(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index f60e6d11..2f940af1 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -390,14 +390,19 @@ int main(int argc, char* argv[]) { unit_test_setup_teardown(console_shows_dnd_presence_when_set_all, init_preferences, close_preferences), - unit_test(handle_message_stanza_error_when_no_from), - unit_test_setup_teardown(handle_message_stanza_error_from_cancel, + unit_test(handle_message_error_when_no_recipient), + unit_test_setup_teardown(handle_message_error_when_recipient_cancel, init_preferences, close_preferences), - unit_test_setup_teardown(handle_message_stanza_error_from_cancel_disables_chat_session, + unit_test_setup_teardown(handle_message_error_when_recipient_cancel_disables_chat_session, init_preferences, close_preferences), - unit_test(handle_message_stanza_error_from_no_type), + unit_test(handle_message_error_when_recipient_and_no_type), + unit_test(handle_presence_error_when_no_recipient), + unit_test(handle_presence_error_when_no_recipient_and_conflict), + unit_test(handle_presence_error_when_nick_conflict_shows_recipient_error), + unit_test(handle_presence_error_when_nick_conflict_does_not_join_room), + unit_test(handle_presence_error_when_from_recipient_not_conflict), }; const UnitTest cmd_alias_tests[] = { diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index feeed9db..a82ad1e7 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -108,6 +108,12 @@ void _mock_ui_handle_recipient_error(const char * const recipient, check_expected(err_msg); } +static +void _stub_ui_handle_recipient_error(const char * const recipient, + const char * const err_msg) +{ +} + static void _mock_ui_handle_recipient_not_found(const char * const recipient, const char * const err_msg) @@ -195,6 +201,12 @@ stub_ui_handle_recipient_not_found(void) ui_handle_recipient_not_found = _stub_ui_handle_recipient_not_found; } +void +stub_ui_handle_recipient_error(void) +{ + ui_handle_recipient_error = _stub_ui_handle_recipient_error; +} + // expectations void diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index 258cb10c..30bec25e 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -19,6 +19,7 @@ void mock_cons_show_error(void); void expect_cons_show_error(char *output); void stub_ui_handle_recipient_not_found(void); +void stub_ui_handle_recipient_error(void); void expect_ui_handle_error(char *err_msg); void expect_ui_handle_recipient_error(char *recipient, char *err_msg); void expect_ui_handle_recipient_not_found(char *recipient, char *err_msg);