From 82ad0cd306968ae6821127829e9d52f01932e82e Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 17 Feb 2014 21:52:42 +0000 Subject: [PATCH 1/5] Added /otr myfp test --- src/ui/ui.h | 2 +- tests/otr/mock_otr.c | 14 +++++++++++--- tests/otr/mock_otr.h | 3 ++- tests/test_cmd_otr.c | 22 +++++++++++++++++++++- tests/test_cmd_otr.h | 1 + tests/testsuite.c | 1 + tests/ui/mock_ui.c | 26 ++++++++++++++++++++++++++ tests/ui/mock_ui.h | 3 +++ 8 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/ui/ui.h b/src/ui/ui.h index af254ae6..77d081ab 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -83,7 +83,7 @@ gboolean (*ui_current_win_is_otr)(void); void (*ui_current_set_otr)(gboolean value); char* (*ui_current_recipient)(void); void (*ui_current_print_line)(const char * const msg, ...); -void (*ui_current_print_formatted_line)(const char show_chat, int attrs, const char * const msg, ...); +void (*ui_current_print_formatted_line)(const char show_char, int attrs, const char * const msg, ...); void (*ui_current_error_line)(const char * const msg); void (*ui_current_page_off)(void); void (*ui_current_refresh)(void); diff --git a/tests/otr/mock_otr.c b/tests/otr/mock_otr.c index b8b43d9b..2fb2eab0 100644 --- a/tests/otr/mock_otr.c +++ b/tests/otr/mock_otr.c @@ -21,10 +21,10 @@ _mock_otr_libotr_version(void) return (char *)mock(); } -void -mock_otr_libotr_version(void) +static char * +_mock_otr_get_my_fingerprint(void) { - otr_libotr_version = _mock_otr_libotr_version; + return (char *)mock(); } void @@ -37,5 +37,13 @@ otr_keygen_expect(ProfAccount *account) void otr_libotr_version_returns(char *version) { + otr_libotr_version = _mock_otr_libotr_version; will_return(_mock_otr_libotr_version, version); } + +void +otr_get_my_fingerprint_returns(char *fingerprint) +{ + otr_get_my_fingerprint = _mock_otr_get_my_fingerprint; + will_return(_mock_otr_get_my_fingerprint, fingerprint); +} diff --git a/tests/otr/mock_otr.h b/tests/otr/mock_otr.h index d9a6c492..e3908a64 100644 --- a/tests/otr/mock_otr.h +++ b/tests/otr/mock_otr.h @@ -5,7 +5,8 @@ void otr_keygen_expect(ProfAccount *account); -void mock_otr_libotr_version(void); void otr_libotr_version_returns(char *version); +void otr_get_my_fingerprint_returns(char *fingerprint); + #endif diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index f4469ae5..9a731d12 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -254,7 +254,6 @@ void cmd_otr_libver_shows_libotr_version(void **state) char *version = "9.9.9"; GString *message = g_string_new("Using libotr version "); g_string_append(message, version); - mock_otr_libotr_version(); otr_libotr_version_returns(version); expect_cons_show(message->str); @@ -367,6 +366,27 @@ void cmd_otr_myfp_shows_message_when_disconnecting(void **state) test_with_command_and_connection_status("myfp", JABBER_DISCONNECTING); } +void cmd_otr_myfp_shows_my_fingerprint(void **state) +{ + char *fingerprint = "AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD EEEEEEEE"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "myfp", NULL }; + mock_connection_status(JABBER_CONNECTED); + otr_get_my_fingerprint_returns(strdup(fingerprint)); + mock_ui_current_print_formatted_line(); + + GString *message = g_string_new("Your OTR fingerprint: "); + g_string_append(message, fingerprint); + + ui_current_print_formatted_line_expect('!', 0, message->str); + + gboolean result = cmd_otr(args, *help); + assert_true(result); + + g_string_free(message, TRUE); + free(help); +} + #else void cmd_otr_shows_message_when_otr_unsupported(void **state) { diff --git a/tests/test_cmd_otr.h b/tests/test_cmd_otr.h index e645867d..4fda3559 100644 --- a/tests/test_cmd_otr.h +++ b/tests/test_cmd_otr.h @@ -27,6 +27,7 @@ void cmd_otr_myfp_shows_message_when_undefined(void **state); void cmd_otr_myfp_shows_message_when_started(void **state); void cmd_otr_myfp_shows_message_when_connecting(void **state); void cmd_otr_myfp_shows_message_when_disconnecting(void **state); +void cmd_otr_myfp_shows_my_fingerprint(void **state); #else void cmd_otr_shows_message_when_otr_unsupported(void **state); #endif diff --git a/tests/testsuite.c b/tests/testsuite.c index e1b95b2e..b49ff7b2 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -468,6 +468,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_otr_myfp_shows_message_when_started), unit_test(cmd_otr_myfp_shows_message_when_connecting), unit_test(cmd_otr_myfp_shows_message_when_disconnecting), + unit_test(cmd_otr_myfp_shows_my_fingerprint), #else unit_test(cmd_otr_shows_message_when_otr_unsupported), #endif diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index 7175d2b9..af9d1a6b 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -159,6 +159,18 @@ void _stub_ui_current_refresh(void) { } +static +void _mock_ui_current_print_formatted_line(const char show_char, int attrs, const char * const msg, ...) +{ + check_expected(show_char); + check_expected(attrs); + va_list args; + va_start(args, msg); + vsnprintf(output, sizeof(output), msg, args); + check_expected(output); + va_end(args); +} + // bind mocks and stubs void @@ -252,6 +264,12 @@ stub_ui_current_refresh(void) ui_current_refresh = _stub_ui_current_refresh; } +void +mock_ui_current_print_formatted_line(void) +{ + ui_current_print_formatted_line = _mock_ui_current_print_formatted_line; +} + // expectations void @@ -368,3 +386,11 @@ ui_current_recipient_returns(char *jid) { will_return(_mock_ui_current_recipeint, jid); } + +void +ui_current_print_formatted_line_expect(char show_char, int attrs, char *message) +{ + expect_value(_mock_ui_current_print_formatted_line, show_char, show_char); + expect_value(_mock_ui_current_print_formatted_line, attrs, attrs); + expect_string(_mock_ui_current_print_formatted_line, output, message); +} diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index 3d264970..9cdd6b34 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -51,4 +51,7 @@ void ui_current_recipient_returns(char *jid); void stub_ui_current_refresh(void); +void mock_ui_current_print_formatted_line(void); +void ui_current_print_formatted_line_expect(char show_char, int attrs, char *message); + #endif From 4afec6ab5e6385e15e605130e5f275aa759fab9a Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 17 Feb 2014 22:14:33 +0000 Subject: [PATCH 2/5] Added test for /otr theirfp when in console --- tests/test_cmd_otr.c | 16 ++++++++++++++++ tests/test_cmd_otr.h | 1 + tests/testsuite.c | 1 + tests/ui/mock_ui.c | 22 ++++++++++++++++++++++ tests/ui/mock_ui.h | 3 +++ 5 files changed, 43 insertions(+) diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index 9a731d12..707bb3d2 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -387,6 +387,22 @@ void cmd_otr_myfp_shows_my_fingerprint(void **state) free(help); } +void cmd_otr_theirfp_shows_message_when_in_console(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "theirfp", NULL }; + mock_connection_status(JABBER_CONNECTED); + mock_current_win_type(WIN_CONSOLE); + mock_ui_current_print_line(); + + ui_current_print_line_expect("You must be in a regular chat window to view a recipient's fingerprint."); + + gboolean result = cmd_otr(args, *help); + assert_true(result); + + free(help); +} + #else void cmd_otr_shows_message_when_otr_unsupported(void **state) { diff --git a/tests/test_cmd_otr.h b/tests/test_cmd_otr.h index 4fda3559..8d2b7040 100644 --- a/tests/test_cmd_otr.h +++ b/tests/test_cmd_otr.h @@ -28,6 +28,7 @@ void cmd_otr_myfp_shows_message_when_started(void **state); void cmd_otr_myfp_shows_message_when_connecting(void **state); void cmd_otr_myfp_shows_message_when_disconnecting(void **state); void cmd_otr_myfp_shows_my_fingerprint(void **state); +void cmd_otr_theirfp_shows_message_when_in_console(void **state); #else void cmd_otr_shows_message_when_otr_unsupported(void **state); #endif diff --git a/tests/testsuite.c b/tests/testsuite.c index b49ff7b2..9396a06b 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -469,6 +469,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_otr_myfp_shows_message_when_connecting), unit_test(cmd_otr_myfp_shows_message_when_disconnecting), unit_test(cmd_otr_myfp_shows_my_fingerprint), + unit_test(cmd_otr_theirfp_shows_message_when_in_console), #else unit_test(cmd_otr_shows_message_when_otr_unsupported), #endif diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index af9d1a6b..d2f6944e 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -171,6 +171,16 @@ void _mock_ui_current_print_formatted_line(const char show_char, int attrs, cons va_end(args); } +static +void _mock_ui_current_print_line(const char * const msg, ...) +{ + va_list args; + va_start(args, msg); + vsnprintf(output, sizeof(output), msg, args); + check_expected(output); + va_end(args); +} + // bind mocks and stubs void @@ -270,6 +280,12 @@ mock_ui_current_print_formatted_line(void) ui_current_print_formatted_line = _mock_ui_current_print_formatted_line; } +void +mock_ui_current_print_line(void) +{ + ui_current_print_line = _mock_ui_current_print_line; +} + // expectations void @@ -394,3 +410,9 @@ ui_current_print_formatted_line_expect(char show_char, int attrs, char *message) expect_value(_mock_ui_current_print_formatted_line, attrs, attrs); expect_string(_mock_ui_current_print_formatted_line, output, message); } + +void +ui_current_print_line_expect(char *message) +{ + expect_string(_mock_ui_current_print_line, output, message); +} diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index 9cdd6b34..e7db7247 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -54,4 +54,7 @@ void stub_ui_current_refresh(void); void mock_ui_current_print_formatted_line(void); void ui_current_print_formatted_line_expect(char show_char, int attrs, char *message); +void mock_ui_current_print_line(void); +void ui_current_print_line_expect(char *message); + #endif From a25714b6dcab354e5802344755b57e893361e4d6 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 17 Feb 2014 22:44:27 +0000 Subject: [PATCH 3/5] Added test for /otr theirfp when in other non chat windows --- tests/test_cmd_otr.c | 25 +++++++++++++++++++++++-- tests/test_cmd_otr.h | 3 +++ tests/testsuite.c | 3 +++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index 707bb3d2..34496ea1 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -387,12 +387,13 @@ void cmd_otr_myfp_shows_my_fingerprint(void **state) free(help); } -void cmd_otr_theirfp_shows_message_when_in_console(void **state) +static void +test_cmd_otr_theirfp_from_wintype(win_type_t wintype) { CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "theirfp", NULL }; mock_connection_status(JABBER_CONNECTED); - mock_current_win_type(WIN_CONSOLE); + mock_current_win_type(wintype); mock_ui_current_print_line(); ui_current_print_line_expect("You must be in a regular chat window to view a recipient's fingerprint."); @@ -403,6 +404,26 @@ void cmd_otr_theirfp_shows_message_when_in_console(void **state) free(help); } +void cmd_otr_theirfp_shows_message_when_in_console(void **state) +{ + test_cmd_otr_theirfp_from_wintype(WIN_CONSOLE); +} + +void cmd_otr_theirfp_shows_message_when_in_muc(void **state) +{ + test_cmd_otr_theirfp_from_wintype(WIN_MUC); +} + +void cmd_otr_theirfp_shows_message_when_in_private(void **state) +{ + test_cmd_otr_theirfp_from_wintype(WIN_PRIVATE); +} + +void cmd_otr_theirfp_shows_message_when_in_duck(void **state) +{ + test_cmd_otr_theirfp_from_wintype(WIN_DUCK); +} + #else void cmd_otr_shows_message_when_otr_unsupported(void **state) { diff --git a/tests/test_cmd_otr.h b/tests/test_cmd_otr.h index 8d2b7040..c741c0d8 100644 --- a/tests/test_cmd_otr.h +++ b/tests/test_cmd_otr.h @@ -29,6 +29,9 @@ void cmd_otr_myfp_shows_message_when_connecting(void **state); void cmd_otr_myfp_shows_message_when_disconnecting(void **state); void cmd_otr_myfp_shows_my_fingerprint(void **state); void cmd_otr_theirfp_shows_message_when_in_console(void **state); +void cmd_otr_theirfp_shows_message_when_in_muc(void **state); +void cmd_otr_theirfp_shows_message_when_in_private(void **state); +void cmd_otr_theirfp_shows_message_when_in_duck(void **state); #else void cmd_otr_shows_message_when_otr_unsupported(void **state); #endif diff --git a/tests/testsuite.c b/tests/testsuite.c index 9396a06b..fd73f147 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -470,6 +470,9 @@ int main(int argc, char* argv[]) { unit_test(cmd_otr_myfp_shows_message_when_disconnecting), unit_test(cmd_otr_myfp_shows_my_fingerprint), unit_test(cmd_otr_theirfp_shows_message_when_in_console), + unit_test(cmd_otr_theirfp_shows_message_when_in_muc), + unit_test(cmd_otr_theirfp_shows_message_when_in_private), + unit_test(cmd_otr_theirfp_shows_message_when_in_duck), #else unit_test(cmd_otr_shows_message_when_otr_unsupported), #endif From b4b463998bec925c9f04def3ce48bbd475fd8aae Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 17 Feb 2014 22:57:36 +0000 Subject: [PATCH 4/5] Test for /otr theirfp in non OTR window --- tests/test_cmd_otr.c | 17 +++++++++++++++++ tests/test_cmd_otr.h | 1 + tests/testsuite.c | 1 + tests/ui/mock_ui.c | 13 +++++++++++++ tests/ui/mock_ui.h | 2 ++ 5 files changed, 34 insertions(+) diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index 34496ea1..f16eee19 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -424,6 +424,23 @@ void cmd_otr_theirfp_shows_message_when_in_duck(void **state) test_cmd_otr_theirfp_from_wintype(WIN_DUCK); } +void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state) +{ + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "theirfp", NULL }; + mock_connection_status(JABBER_CONNECTED); + mock_current_win_type(WIN_CHAT); + ui_current_win_is_otr_returns(FALSE); + mock_ui_current_print_formatted_line(); + + ui_current_print_formatted_line_expect('!', 0, "You are not currently in an OTR session."); + + gboolean result = cmd_otr(args, *help); + assert_true(result); + + free(help); +} + #else void cmd_otr_shows_message_when_otr_unsupported(void **state) { diff --git a/tests/test_cmd_otr.h b/tests/test_cmd_otr.h index c741c0d8..58089071 100644 --- a/tests/test_cmd_otr.h +++ b/tests/test_cmd_otr.h @@ -32,6 +32,7 @@ void cmd_otr_theirfp_shows_message_when_in_console(void **state); void cmd_otr_theirfp_shows_message_when_in_muc(void **state); void cmd_otr_theirfp_shows_message_when_in_private(void **state); void cmd_otr_theirfp_shows_message_when_in_duck(void **state); +void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state); #else void cmd_otr_shows_message_when_otr_unsupported(void **state); #endif diff --git a/tests/testsuite.c b/tests/testsuite.c index fd73f147..c49d0de8 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -473,6 +473,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_otr_theirfp_shows_message_when_in_muc), unit_test(cmd_otr_theirfp_shows_message_when_in_private), unit_test(cmd_otr_theirfp_shows_message_when_in_duck), + unit_test(cmd_otr_theirfp_shows_message_when_non_otr_chat_window), #else unit_test(cmd_otr_shows_message_when_otr_unsupported), #endif diff --git a/tests/ui/mock_ui.c b/tests/ui/mock_ui.c index d2f6944e..a79f0521 100644 --- a/tests/ui/mock_ui.c +++ b/tests/ui/mock_ui.c @@ -181,6 +181,12 @@ void _mock_ui_current_print_line(const char * const msg, ...) va_end(args); } +static +gboolean _mock_ui_current_win_is_otr(void) +{ + return (gboolean)mock(); +} + // bind mocks and stubs void @@ -416,3 +422,10 @@ ui_current_print_line_expect(char *message) { expect_string(_mock_ui_current_print_line, output, message); } + +void +ui_current_win_is_otr_returns(gboolean result) +{ + ui_current_win_is_otr = _mock_ui_current_win_is_otr; + will_return(_mock_ui_current_win_is_otr, result); +} diff --git a/tests/ui/mock_ui.h b/tests/ui/mock_ui.h index e7db7247..a814f06e 100644 --- a/tests/ui/mock_ui.h +++ b/tests/ui/mock_ui.h @@ -57,4 +57,6 @@ void ui_current_print_formatted_line_expect(char show_char, int attrs, char *mes void mock_ui_current_print_line(void); void ui_current_print_line_expect(char *message); +void ui_current_win_is_otr_returns(gboolean result); + #endif From 037ca818214dad46745576bf9af740f032ca0fc7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 17 Feb 2014 23:10:00 +0000 Subject: [PATCH 5/5] Test /otr theirfp --- tests/otr/mock_otr.c | 15 +++++++++++++++ tests/otr/mock_otr.h | 1 + tests/test_cmd_otr.c | 27 +++++++++++++++++++++++++++ tests/test_cmd_otr.h | 1 + tests/testsuite.c | 1 + 5 files changed, 45 insertions(+) diff --git a/tests/otr/mock_otr.c b/tests/otr/mock_otr.c index 2fb2eab0..a259c156 100644 --- a/tests/otr/mock_otr.c +++ b/tests/otr/mock_otr.c @@ -27,6 +27,13 @@ _mock_otr_get_my_fingerprint(void) return (char *)mock(); } +static char * +_mock_otr_get_their_fingerprint(const char * const recipient) +{ + check_expected(recipient); + return (char *)mock(); +} + void otr_keygen_expect(ProfAccount *account) { @@ -47,3 +54,11 @@ otr_get_my_fingerprint_returns(char *fingerprint) otr_get_my_fingerprint = _mock_otr_get_my_fingerprint; will_return(_mock_otr_get_my_fingerprint, fingerprint); } + +void +otr_get_their_fingerprint_expect_and_return(char *recipient, char *fingerprint) +{ + otr_get_their_fingerprint = _mock_otr_get_their_fingerprint; + expect_string(_mock_otr_get_their_fingerprint, recipient, recipient); + will_return(_mock_otr_get_their_fingerprint, fingerprint); +} diff --git a/tests/otr/mock_otr.h b/tests/otr/mock_otr.h index e3908a64..3296c145 100644 --- a/tests/otr/mock_otr.h +++ b/tests/otr/mock_otr.h @@ -8,5 +8,6 @@ void otr_keygen_expect(ProfAccount *account); void otr_libotr_version_returns(char *version); void otr_get_my_fingerprint_returns(char *fingerprint); +void otr_get_their_fingerprint_expect_and_return(char *recipient, char *fingerprint); #endif diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index f16eee19..57b26df3 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -441,6 +441,33 @@ void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state) free(help); } +void cmd_otr_theirfp_shows_fingerprint(void **state) +{ + char *recipient = "someone@chat.com"; + char *fingerprint = "AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD EEEEEEEE"; + CommandHelp *help = malloc(sizeof(CommandHelp)); + gchar *args[] = { "theirfp", NULL }; + mock_connection_status(JABBER_CONNECTED); + mock_current_win_type(WIN_CHAT); + ui_current_win_is_otr_returns(TRUE); + mock_ui_current_recipient(); + ui_current_recipient_returns(recipient); + mock_ui_current_print_formatted_line(); + + GString *message = g_string_new(recipient); + g_string_append(message, "'s OTR fingerprint: "); + g_string_append(message, fingerprint); + + otr_get_their_fingerprint_expect_and_return(recipient, strdup(fingerprint)); + ui_current_print_formatted_line_expect('!', 0, message->str); + + gboolean result = cmd_otr(args, *help); + assert_true(result); + + g_string_free(message, TRUE); + free(help); +} + #else void cmd_otr_shows_message_when_otr_unsupported(void **state) { diff --git a/tests/test_cmd_otr.h b/tests/test_cmd_otr.h index 58089071..95c67a46 100644 --- a/tests/test_cmd_otr.h +++ b/tests/test_cmd_otr.h @@ -33,6 +33,7 @@ void cmd_otr_theirfp_shows_message_when_in_muc(void **state); void cmd_otr_theirfp_shows_message_when_in_private(void **state); void cmd_otr_theirfp_shows_message_when_in_duck(void **state); void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state); +void cmd_otr_theirfp_shows_fingerprint(void **state); #else void cmd_otr_shows_message_when_otr_unsupported(void **state); #endif diff --git a/tests/testsuite.c b/tests/testsuite.c index c49d0de8..0e3911ad 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -474,6 +474,7 @@ int main(int argc, char* argv[]) { unit_test(cmd_otr_theirfp_shows_message_when_in_private), unit_test(cmd_otr_theirfp_shows_message_when_in_duck), unit_test(cmd_otr_theirfp_shows_message_when_non_otr_chat_window), + unit_test(cmd_otr_theirfp_shows_fingerprint), #else unit_test(cmd_otr_shows_message_when_otr_unsupported), #endif