From bcafba2de6cdc9a056b0d69823ad62be0bfed159 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 16 Mar 2014 16:00:10 +0000 Subject: [PATCH 1/4] Added /roster remove tests --- tests/test_cmd_roster.c | 36 ++++++++++++++++++++++++++++++++++-- tests/test_cmd_roster.h | 2 ++ tests/testsuite.c | 2 ++ tests/xmpp/mock_xmpp.c | 18 ++++++++++++++++++ tests/xmpp/mock_xmpp.h | 3 +++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/tests/test_cmd_roster.c b/tests/test_cmd_roster.c index b29019bb..e59f5e5e 100644 --- a/tests/test_cmd_roster.c +++ b/tests/test_cmd_roster.c @@ -69,7 +69,7 @@ void cmd_roster_shows_roster_when_no_args(void **state) roster_free(); } -void cmd_roster_add_shows_message_when_no_jid(void) +void cmd_roster_add_shows_message_when_no_jid(void **state) { mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); @@ -85,7 +85,7 @@ void cmd_roster_add_shows_message_when_no_jid(void) free(help); } -void cmd_roster_add_sends_roster_add_request(void) +void cmd_roster_add_sends_roster_add_request(void **state) { char *jid = "bob@server.org"; char *nick = "bob"; @@ -101,3 +101,35 @@ void cmd_roster_add_sends_roster_add_request(void) free(help); } + +void cmd_roster_remove_shows_message_when_no_jid(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "remove", NULL }; + + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Usage: some usage"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); +} + +void cmd_roster_remove_sends_roster_remove_request(void) +{ + char *jid = "bob@server.org"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "remove", jid, NULL }; + + mock_roster_send_remove(); + mock_connection_status(JABBER_CONNECTED); + roster_send_remove_expect(jid); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); +} diff --git a/tests/test_cmd_roster.h b/tests/test_cmd_roster.h index cfed5875..418b3bc5 100644 --- a/tests/test_cmd_roster.h +++ b/tests/test_cmd_roster.h @@ -5,3 +5,5 @@ void cmd_roster_shows_message_when_undefined(void **state); void cmd_roster_shows_roster_when_no_args(void **state); void cmd_roster_add_shows_message_when_no_jid(void **state); void cmd_roster_add_sends_roster_add_request(void **state); +void cmd_roster_remove_shows_message_when_no_jid(void **state); +void cmd_roster_remove_sends_roster_remove_request(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index 937e4642..bc669735 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -509,6 +509,8 @@ int main(int argc, char* argv[]) { unit_test(cmd_roster_shows_roster_when_no_args), unit_test(cmd_roster_add_shows_message_when_no_jid), unit_test(cmd_roster_add_sends_roster_add_request), + unit_test(cmd_roster_remove_shows_message_when_no_jid), + unit_test(cmd_roster_remove_sends_roster_remove_request), }; return run_tests(all_tests); diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index c25156be..43f31e6c 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -103,6 +103,12 @@ _mock_roster_send_add_new(const char *const barejid, const char * const name) check_expected(name); } +static void +_mock_roster_send_remove(const char * const barejid) +{ + check_expected(barejid); +} + void mock_jabber_connect_with_details(void) { @@ -152,6 +158,12 @@ mock_roster_send_add_new(void) roster_send_add_new = _mock_roster_send_add_new; } +void +mock_roster_send_remove(void) +{ + roster_send_remove = _mock_roster_send_remove; +} + void bookmark_get_list_returns(GList *bookmarks) { @@ -280,3 +292,9 @@ roster_send_add_new_expect(char *jid, char *nick) expect_string(_mock_roster_send_add_new, barejid, jid); expect_string(_mock_roster_send_add_new, name, nick); } + +void +roster_send_remove_expect(char *jid) +{ + expect_string(_mock_roster_send_remove, barejid, jid); +} diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h index aafe1ba7..a917f9b8 100644 --- a/tests/xmpp/mock_xmpp.h +++ b/tests/xmpp/mock_xmpp.h @@ -39,4 +39,7 @@ void presence_join_room_expect(char *room, char *nick, char *passwd); void mock_roster_send_add_new(void); void roster_send_add_new_expect(char *jid, char *nick); +void mock_roster_send_remove(void); +void roster_send_remove_expect(char *jid); + #endif From 11c04d9fcd0706109ea88f081fa073d802e27032 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 16 Mar 2014 17:53:41 +0000 Subject: [PATCH 2/4] Added /roster clearnick command --- src/command/command.c | 5 ++ src/command/commands.c | 84 ++++++++++++++---------- tests/test_cmd_roster.c | 141 +++++++++++++++++++++++++++++++++++++++- tests/test_cmd_roster.h | 7 ++ tests/testsuite.c | 7 ++ tests/xmpp/mock_xmpp.c | 27 ++++++++ tests/xmpp/mock_xmpp.h | 3 + 7 files changed, 237 insertions(+), 37 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 8d4ccf8e..dc8a685f 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -1015,6 +1015,7 @@ cmd_init(void) roster_ac = autocomplete_new(); autocomplete_add(roster_ac, "add"); autocomplete_add(roster_ac, "nick"); + autocomplete_add(roster_ac, "clearnick"); autocomplete_add(roster_ac, "remove"); group_ac = autocomplete_new(); @@ -1564,6 +1565,10 @@ _roster_autocomplete(char *input, int *size) if (result != NULL) { return result; } + result = autocomplete_param_with_func(input, size, "/roster clearnick", roster_find_jid); + if (result != NULL) { + return result; + } result = autocomplete_param_with_func(input, size, "/roster remove", roster_find_jid); if (result != NULL) { return result; diff --git a/src/command/commands.c b/src/command/commands.c index c7a8e0d5..4a7418f4 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -1200,49 +1200,41 @@ cmd_roster(gchar **args, struct cmd_help_t help) GSList *list = roster_get_contacts(); cons_show_roster(list); return TRUE; - } // add contact - if (strcmp(args[0], "add") == 0) { - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - + } else if (strcmp(args[0], "add") == 0) { char *jid = args[1]; - char *name = args[2]; - - roster_send_add_new(jid, name); - + if (jid == NULL) { + cons_show("Usage: %s", help.usage); + } else { + char *name = args[2]; + roster_send_add_new(jid, name); + } return TRUE; - } // remove contact - if (strcmp(args[0], "remove") == 0) { - - if (args[1] == NULL) { - cons_show("Usage: %s", help.usage); - return TRUE; - } - + } else if (strcmp(args[0], "remove") == 0) { char *jid = args[1]; - - roster_send_remove(jid); - + if (jid == NULL) { + cons_show("Usage: %s", help.usage); + } else { + roster_send_remove(jid); + } return TRUE; - } // change nickname - if (strcmp(args[0], "nick") == 0) { - - if (args[1] == NULL) { + } else if (strcmp(args[0], "nick") == 0) { + char *jid = args[1]; + if (jid == NULL) { cons_show("Usage: %s", help.usage); return TRUE; } - char *jid = args[1]; char *name = args[2]; + if (name == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } // contact does not exist PContact contact = roster_get_contact(jid); @@ -1256,17 +1248,37 @@ cmd_roster(gchar **args, struct cmd_help_t help) GSList *groups = p_contact_groups(contact); roster_send_name_change(barejid, name, groups); - if (name == NULL) { - cons_show("Nickname for %s removed.", jid); - } else { - cons_show("Nickname for %s set to: %s.", jid, name); - } + cons_show("Nickname for %s set to: %s.", jid, name); return TRUE; - } - cons_show("Usage: %s", help.usage); - return TRUE; + // remove nickname + } else if (strcmp(args[0], "clearnick") == 0) { + char *jid = args[1]; + if (jid == NULL) { + cons_show("Usage: %s", help.usage); + return TRUE; + } + + // contact does not exist + PContact contact = roster_get_contact(jid); + if (contact == NULL) { + cons_show("Contact not found in roster: %s", jid); + return TRUE; + } + + const char *barejid = p_contact_barejid(contact); + roster_change_name(contact, NULL); + GSList *groups = p_contact_groups(contact); + roster_send_name_change(barejid, NULL, groups); + + cons_show("Nickname for %s removed.", jid); + + return TRUE; + } else { + cons_show("Usage: %s", help.usage); + return TRUE; + } } gboolean diff --git a/tests/test_cmd_roster.c b/tests/test_cmd_roster.c index e59f5e5e..3624cd77 100644 --- a/tests/test_cmd_roster.c +++ b/tests/test_cmd_roster.c @@ -118,7 +118,7 @@ void cmd_roster_remove_shows_message_when_no_jid(void **state) free(help); } -void cmd_roster_remove_sends_roster_remove_request(void) +void cmd_roster_remove_sends_roster_remove_request(void **state) { char *jid = "bob@server.org"; CommandHelp *help = malloc(sizeof(CommandHelp)); @@ -133,3 +133,142 @@ void cmd_roster_remove_sends_roster_remove_request(void) free(help); } + +void cmd_roster_nick_shows_message_when_no_jid(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "nick", NULL }; + + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Usage: some usage"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); +} + +void cmd_roster_nick_shows_message_when_no_nick(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "nick", "bob@server.org", NULL }; + + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Usage: some usage"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); +} + +void cmd_roster_nick_shows_message_when_no_contact_exists(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "nick", "bob@server.org", "bobster", NULL }; + + roster_init(); + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Contact not found in roster: bob@server.org"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); + roster_free(); +} + +void cmd_roster_nick_sends_name_change_request(void **state) +{ + char *jid = "bob@server.org"; + char *nick = "bobster"; + mock_cons_show(); + mock_roster_send_name_change(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "nick", jid, nick, NULL }; + + roster_init(); + GSList *groups = NULL; + groups = g_slist_append(groups, "group1"); + roster_add(jid, "bob", groups, "both", FALSE); + + mock_connection_status(JABBER_CONNECTED); + roster_send_name_change_expect(jid, nick, groups); + expect_cons_show("Nickname for bob@server.org set to: bobster."); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + PContact contact = roster_get_contact(jid); + assert_string_equal(p_contact_name(contact), nick); + + free(help); + roster_free(); +} + +void cmd_roster_clearnick_shows_message_when_no_jid(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "clearnick", NULL }; + + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Usage: some usage"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); +} + +void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state) +{ + mock_cons_show(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + help->usage = "some usage"; + gchar *args[] = { "clearnick", "bob@server.org", NULL }; + + roster_init(); + mock_connection_status(JABBER_CONNECTED); + expect_cons_show("Contact not found in roster: bob@server.org"); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + free(help); + roster_free(); +} + +void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state) +{ + char *jid = "bob@server.org"; + mock_cons_show(); + mock_roster_send_name_change(); + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "clearnick", jid, NULL }; + + roster_init(); + GSList *groups = NULL; + groups = g_slist_append(groups, "group1"); + roster_add(jid, "bob", groups, "both", FALSE); + + mock_connection_status(JABBER_CONNECTED); + roster_send_name_change_expect(jid, NULL, groups); + expect_cons_show("Nickname for bob@server.org removed."); + + gboolean result = cmd_roster(args, *help); + assert_true(result); + + PContact contact = roster_get_contact(jid); + assert_null(p_contact_name(contact)); + + free(help); + roster_free(); +} diff --git a/tests/test_cmd_roster.h b/tests/test_cmd_roster.h index 418b3bc5..79f69ec8 100644 --- a/tests/test_cmd_roster.h +++ b/tests/test_cmd_roster.h @@ -7,3 +7,10 @@ void cmd_roster_add_shows_message_when_no_jid(void **state); void cmd_roster_add_sends_roster_add_request(void **state); void cmd_roster_remove_shows_message_when_no_jid(void **state); void cmd_roster_remove_sends_roster_remove_request(void **state); +void cmd_roster_nick_shows_message_when_no_jid(void **state); +void cmd_roster_nick_shows_message_when_no_nick(void **state); +void cmd_roster_nick_shows_message_when_no_contact_exists(void **state); +void cmd_roster_nick_sends_name_change_request(void **state); +void cmd_roster_clearnick_shows_message_when_no_jid(void **state); +void cmd_roster_clearnick_shows_message_when_no_contact_exists(void **state); +void cmd_roster_clearnick_sends_name_change_request_with_empty_nick(void **state); diff --git a/tests/testsuite.c b/tests/testsuite.c index bc669735..f5bc8faf 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -511,6 +511,13 @@ int main(int argc, char* argv[]) { unit_test(cmd_roster_add_sends_roster_add_request), unit_test(cmd_roster_remove_shows_message_when_no_jid), unit_test(cmd_roster_remove_sends_roster_remove_request), + unit_test(cmd_roster_nick_shows_message_when_no_jid), + unit_test(cmd_roster_nick_shows_message_when_no_nick), + unit_test(cmd_roster_nick_shows_message_when_no_contact_exists), + unit_test(cmd_roster_nick_sends_name_change_request), + unit_test(cmd_roster_clearnick_shows_message_when_no_jid), + unit_test(cmd_roster_clearnick_shows_message_when_no_contact_exists), + unit_test(cmd_roster_clearnick_sends_name_change_request_with_empty_nick), }; return run_tests(all_tests); diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index 43f31e6c..9174e569 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -109,6 +109,15 @@ _mock_roster_send_remove(const char * const barejid) check_expected(barejid); } +static void +_mock_roster_send_name_change(const char * const barejid, const char * const new_name, + GSList *groups) +{ + check_expected(barejid); + check_expected(new_name); + check_expected(groups); +} + void mock_jabber_connect_with_details(void) { @@ -164,6 +173,12 @@ mock_roster_send_remove(void) roster_send_remove = _mock_roster_send_remove; } +void +mock_roster_send_name_change(void) +{ + roster_send_name_change = _mock_roster_send_name_change; +} + void bookmark_get_list_returns(GList *bookmarks) { @@ -298,3 +313,15 @@ roster_send_remove_expect(char *jid) { expect_string(_mock_roster_send_remove, barejid, jid); } + +void +roster_send_name_change_expect(char *jid, char *nick, GSList *groups) +{ + expect_string(_mock_roster_send_name_change, barejid, jid); + if (nick == NULL) { + expect_value(_mock_roster_send_name_change, new_name, NULL); + } else { + expect_string(_mock_roster_send_name_change, new_name, nick); + } + expect_memory(_mock_roster_send_name_change, groups, groups, sizeof(GSList)); +} diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h index a917f9b8..a5f93fd2 100644 --- a/tests/xmpp/mock_xmpp.h +++ b/tests/xmpp/mock_xmpp.h @@ -42,4 +42,7 @@ void roster_send_add_new_expect(char *jid, char *nick); void mock_roster_send_remove(void); void roster_send_remove_expect(char *jid); +void mock_roster_send_name_change(void); +void roster_send_name_change_expect(char *jid, char *name, GSList *groups); + #endif From 89f306ceb251a1ffe6ea8ea47f5417fc1da24693 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 16 Mar 2014 17:59:01 +0000 Subject: [PATCH 3/4] Updated /roster help --- src/command/command.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index dc8a685f..eb00931f 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -144,22 +144,22 @@ static struct cmd_t command_defs[] = { "/roster", cmd_roster, parse_args_with_freetext, 0, 3, NULL, - { "/roster [add|remove|nick] [jid] [handle]", "Manage your roster.", - { "/roster [add|remove|nick] [jid] [handle]", - "----------------------------------------", + { "/roster [add|remove|nick|clearnick] [jid] [nickname]", "Manage your roster.", + { "/roster [add|remove|nick|clearnick] [jid] [nickname]", + "----------------------------------------------------", "View, add to, and remove from your roster.", "Passing no arguments lists all contacts in your roster.", - "The 'add' command will add a new item, the jid is required, the handle is an optional nickname", - "The 'remove' command removes a contact, the jid is required.", - "The 'nick' command changes a contacts nickname, the jid is required,", - "if no handle is supplied, the current one is removed.", + "The 'add' command will add a new item, jid is required, nickname is optional.", + "The 'remove' command removes a contact, jid is required.", + "The 'nick' command changes a contacts nickname, both jid and nickname are required,", + "The 'clearnick' command removes the current nickname, jid is required.", "", "Example : /roster (show your roster)", "Example : /roster add someone@contacts.org (add the contact)", "Example : /roster add someone@contacts.org Buddy (add the contact with nickname 'Buddy')", "Example : /roster remove someone@contacts.org (remove the contact)", "Example : /roster nick myfriend@chat.org My Friend", - "Example : /roster nick kai@server.com (clears handle)", + "Example : /roster clearnick kai@server.com (clears nickname)", NULL } } }, { "/group", From f4d52f3d40287a9426ec7e0390a8fcfce78a4ee9 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 16 Mar 2014 18:02:18 +0000 Subject: [PATCH 4/4] Use expect_value == NULL for NULL parameters in tests --- tests/ui/mock_ui.c | 2 +- tests/xmpp/mock_xmpp.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index cbdbd486..8fcf277a 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -350,7 +350,7 @@ expect_cons_show_contact_online(PContact contact, Resource *resource, GDateTime expect_memory(_mock_cons_show_contact_online, contact, contact, sizeof(contact)); expect_memory(_mock_cons_show_contact_online, resource, resource, sizeof(Resource)); if (last_activity == NULL) { - expect_any(_mock_cons_show_contact_online, last_activity); + expect_value(_mock_cons_show_contact_online, last_activity, NULL); } else { expect_memory(_mock_cons_show_contact_online, last_activity, last_activity, sizeof(last_activity)); } diff --git a/tests/xmpp/mock_xmpp.c b/tests/xmpp/mock_xmpp.c index 9174e569..c2ded651 100644 --- a/tests/xmpp/mock_xmpp.c +++ b/tests/xmpp/mock_xmpp.c @@ -214,7 +214,7 @@ jabber_connect_with_details_expect_and_return(char *jid, expect_string(_mock_jabber_connect_with_details, jid, jid); expect_string(_mock_jabber_connect_with_details, passwd, password); if (altdomain == NULL) { - expect_any(_mock_jabber_connect_with_details, altdomain); + expect_value(_mock_jabber_connect_with_details, altdomain, NULL); } else { expect_string(_mock_jabber_connect_with_details, altdomain, altdomain); } @@ -264,7 +264,7 @@ expect_and_return_bookmark_add(char *expected_jid, char *expected_nick, if (expected_nick != NULL) { expect_string(_mock_bookmark_add, nick, expected_nick); } else { - expect_any(_mock_bookmark_add, nick); + expect_value(_mock_bookmark_add, nick, NULL); } expect_value(_mock_bookmark_add, autojoin, expected_autojoin); @@ -295,7 +295,7 @@ presence_join_room_expect(char *room, char *nick, char *passwd) expect_string(_mock_presence_join_room, room, room); expect_string(_mock_presence_join_room, nick, nick); if (passwd == NULL) { - expect_any(_mock_presence_join_room, passwd); + expect_value(_mock_presence_join_room, passwd, NULL); } else { expect_string(_mock_presence_join_room, passwd, passwd); }