about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-06-15 23:16:22 +0100
committerJames Booth <boothj5@gmail.com>2015-06-15 23:16:22 +0100
commitfb0e0659023e6483418de2a06626d640644e94d9 (patch)
tree38e9ba8a3cb8b63a8267de6780183fda6d93cbee
parent6097a5bade7fdcd333b8127c36d31024ae29284b (diff)
downloadprofani-tty-fb0e0659023e6483418de2a06626d640644e94d9.tar.gz
Tidied cmd_otr, added check-unit target to Makefile
-rw-r--r--Makefile.am3
-rw-r--r--src/command/commands.c197
-rw-r--r--src/ui/core.c13
-rw-r--r--src/ui/ui.h1
-rw-r--r--tests/unittests/test_cmd_otr.c41
-rw-r--r--tests/unittests/ui/stub_ui.c5
6 files changed, 157 insertions, 103 deletions
diff --git a/Makefile.am b/Makefile.am
index a18bedcc..b416e1b2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -174,3 +174,6 @@ $(git_include): $(git_include).in
 clean-local:
 	rm -f $(git_include) $(git_include).in
 endif
+
+check-unit: tests/unittests/unittests
+	tests/unittests/unittests
diff --git a/src/command/commands.c b/src/command/commands.c
index 1590629e..6ae4cf91 100644
--- a/src/command/commands.c
+++ b/src/command/commands.c
@@ -4200,29 +4200,34 @@ cmd_otr(gchar **args, struct cmd_help_t help)
     } else if (strcmp(args[0], "myfp") == 0) {
         if (!otr_key_loaded()) {
             ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
-        } else {
-            char *fingerprint = otr_get_my_fingerprint();
-            ui_current_print_formatted_line('!', 0, "Your OTR fingerprint: %s", fingerprint);
-            free(fingerprint);
+            return TRUE;
         }
+
+        char *fingerprint = otr_get_my_fingerprint();
+        ui_current_print_formatted_line('!', 0, "Your OTR fingerprint: %s", fingerprint);
+        free(fingerprint);
         return TRUE;
 
     } else if (strcmp(args[0], "theirfp") == 0) {
         win_type_t win_type = ui_current_win_type();
-
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in a regular chat window to view a recipient's fingerprint.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            ProfChatWin *chatwin = wins_get_current_chat();
-            char *fingerprint = otr_get_their_fingerprint(chatwin->barejid);
-            ui_current_print_formatted_line('!', 0, "%s's OTR fingerprint: %s", chatwin->barejid, fingerprint);
-            free(fingerprint);
+            return TRUE;
         }
+
+        char *fingerprint = otr_get_their_fingerprint(chatwin->barejid);
+        ui_current_print_formatted_line('!', 0, "%s's OTR fingerprint: %s", chatwin->barejid, fingerprint);
+        free(fingerprint);
         return TRUE;
 
     } else if (strcmp(args[0], "start") == 0) {
+        // recipient supplied
         if (args[1]) {
             char *contact = args[1];
             char *barejid = roster_barejid_from_name(contact);
@@ -4236,131 +4241,165 @@ cmd_otr(gchar **args, struct cmd_help_t help)
             }
             ui_ev_focus_win((ProfWin*)chatwin);
 
-            if (ui_current_win_is_otr()) {
+            if (chatwin->enc_mode == PROF_ENC_OTR) {
                 ui_current_print_formatted_line('!', 0, "You are already in an OTR session.");
-            } else {
-                if (!otr_key_loaded()) {
-                    ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
-                } else if (!otr_is_secure(barejid)) {
-                    char *otr_query_message = otr_start_query();
-                    message_send_chat_encrypted(barejid, otr_query_message);
-                } else {
-                    ui_gone_secure(barejid, otr_is_trusted(barejid));
-                }
+                return TRUE;
+            }
+
+            if (!otr_key_loaded()) {
+                ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
+                return TRUE;
+            }
+
+            if (!otr_is_secure(barejid)) {
+                char *otr_query_message = otr_start_query();
+                message_send_chat_encrypted(barejid, otr_query_message);
+                return TRUE;
             }
+
+            ui_gone_secure(barejid, otr_is_trusted(barejid));
+            return TRUE;
+
+        // no recipient, use current chat
         } else {
             win_type_t win_type = ui_current_win_type();
-
             if (win_type != WIN_CHAT) {
                 ui_current_print_line("You must be in a regular chat window to start an OTR session.");
-            } else if (ui_current_win_is_otr()) {
+                return TRUE;
+            }
+
+            ProfChatWin *chatwin = wins_get_current_chat();
+            if (chatwin->enc_mode == PROF_ENC_OTR) {
                 ui_current_print_formatted_line('!', 0, "You are already in an OTR session.");
-            } else {
-                if (!otr_key_loaded()) {
-                    ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
-                } else {
-                    ProfChatWin *chatwin = wins_get_current_chat();
-                    char *otr_query_message = otr_start_query();
-                    message_send_chat_encrypted(chatwin->barejid, otr_query_message);
-                }
+                return TRUE;
+            }
+
+            if (!otr_key_loaded()) {
+                ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
+                return TRUE;
             }
+
+            char *otr_query_message = otr_start_query();
+            message_send_chat_encrypted(chatwin->barejid, otr_query_message);
+            return TRUE;
         }
-        return TRUE;
 
     } else if (strcmp(args[0], "end") == 0) {
         win_type_t win_type = ui_current_win_type();
-
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in a regular chat window to use OTR.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            ProfChatWin *chatwin = wins_get_current_chat();
-            ui_gone_insecure(chatwin->barejid);
-            otr_end_session(chatwin->barejid);
+            return TRUE;
         }
+
+        ui_gone_insecure(chatwin->barejid);
+        otr_end_session(chatwin->barejid);
         return TRUE;
 
     } else if (strcmp(args[0], "trust") == 0) {
         win_type_t win_type = ui_current_win_type();
-
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in an OTR session to trust a recipient.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            ProfChatWin *chatwin = wins_get_current_chat();
-            ui_trust(chatwin->barejid);
-            otr_trust(chatwin->barejid);
+            return TRUE;
         }
+
+        ui_trust(chatwin->barejid);
+        otr_trust(chatwin->barejid);
         return TRUE;
 
     } else if (strcmp(args[0], "untrust") == 0) {
         win_type_t win_type = ui_current_win_type();
-
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in an OTR session to untrust a recipient.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            ProfChatWin *chatwin = wins_get_current_chat();
-            ui_untrust(chatwin->barejid);
-            otr_untrust(chatwin->barejid);
+            return TRUE;
         }
+
+        ui_untrust(chatwin->barejid);
+        otr_untrust(chatwin->barejid);
         return TRUE;
 
     } else if (strcmp(args[0], "secret") == 0) {
         win_type_t win_type = ui_current_win_type();
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in an OTR session to trust a recipient.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            char *secret = args[1];
-            if (secret == NULL) {
-                cons_show("Usage: %s", help.usage);
-            } else {
-                ProfChatWin *chatwin = wins_get_current_chat();
-                otr_smp_secret(chatwin->barejid, secret);
-            }
+            return TRUE;
+        }
+
+        char *secret = args[1];
+        if (secret == NULL) {
+            cons_show("Usage: %s", help.usage);
+            return TRUE;
         }
+
+        otr_smp_secret(chatwin->barejid, secret);
         return TRUE;
 
     } else if (strcmp(args[0], "question") == 0) {
         char *question = args[1];
         char *answer = args[2];
-
         if (question == NULL || answer == NULL) {
             cons_show("Usage: %s", help.usage);
             return TRUE;
-        } else {
-            win_type_t win_type = ui_current_win_type();
-            if (win_type != WIN_CHAT) {
-                ui_current_print_line("You must be in an OTR session to trust a recipient.");
-            } else if (!ui_current_win_is_otr()) {
-                ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-            } else {
-                ProfChatWin *chatwin = wins_get_current_chat();
-                otr_smp_question(chatwin->barejid, question, answer);
-            }
+        }
+
+        win_type_t win_type = ui_current_win_type();
+        if (win_type != WIN_CHAT) {
+            ui_current_print_line("You must be in an OTR session to trust a recipient.");
             return TRUE;
         }
 
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
+            ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
+            return TRUE;
+        }
+
+        otr_smp_question(chatwin->barejid, question, answer);
+        return TRUE;
+
     } else if (strcmp(args[0], "answer") == 0) {
         win_type_t win_type = ui_current_win_type();
         if (win_type != WIN_CHAT) {
             ui_current_print_line("You must be in an OTR session to trust a recipient.");
-        } else if (!ui_current_win_is_otr()) {
+            return TRUE;
+        }
+
+        ProfChatWin *chatwin = wins_get_current_chat();
+        if (chatwin->enc_mode != PROF_ENC_OTR) {
             ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
-        } else {
-            char *answer = args[1];
-            if (answer == NULL) {
-                cons_show("Usage: %s", help.usage);
-            } else {
-                ProfChatWin *chatwin = wins_get_current_chat();
-                otr_smp_answer(chatwin->barejid, answer);
-            }
+            return TRUE;
         }
+
+        char *answer = args[1];
+        if (answer == NULL) {
+            cons_show("Usage: %s", help.usage);
+            return TRUE;
+        }
+
+        otr_smp_answer(chatwin->barejid, answer);
         return TRUE;
 
     } else {
diff --git a/src/ui/core.c b/src/ui/core.c
index 6cfa8594..f517d51d 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -1151,19 +1151,6 @@ ui_current_win_type(void)
     return current->type;
 }
 
-gboolean
-ui_current_win_is_otr(void)
-{
-    ProfWin *current = wins_get_current();
-    if (current->type == WIN_CHAT) {
-        ProfChatWin *chatwin = (ProfChatWin*)current;
-        assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK);
-        return chatwin->enc_mode == PROF_ENC_OTR;
-    } else {
-        return FALSE;
-    }
-}
-
 win_type_t
 ui_win_type(int index)
 {
diff --git a/src/ui/ui.h b/src/ui/ui.h
index cd5349d2..abe50435 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -194,7 +194,6 @@ int ui_close_read_wins(void);
 
 // current window actions
 win_type_t ui_current_win_type(void);
-gboolean ui_current_win_is_otr(void);
 
 void ui_current_print_line(const char * const msg, ...);
 void ui_current_print_formatted_line(const char show_char, int attrs, const char * const msg, ...);
diff --git a/tests/unittests/test_cmd_otr.c b/tests/unittests/test_cmd_otr.c
index 54e578e0..90724aed 100644
--- a/tests/unittests/test_cmd_otr.c
+++ b/tests/unittests/test_cmd_otr.c
@@ -422,12 +422,21 @@ void cmd_otr_theirfp_shows_message_when_in_private(void **state)
 
 void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state)
 {
+    char *recipient = "someuser@someserver.com";
     CommandHelp *help = malloc(sizeof(CommandHelp));
     gchar *args[] = { "theirfp", NULL };
 
     will_return(jabber_get_connection_status, JABBER_CONNECTED);
     will_return(ui_current_win_type, WIN_CHAT);
-    will_return(ui_current_win_is_otr, FALSE);
+
+    ProfChatWin *chatwin = malloc(sizeof(ProfChatWin));
+    chatwin->barejid = strdup(recipient);
+    chatwin->memcheck = PROFCHATWIN_MEMCHECK;
+    will_return(win_create_chat, &chatwin->window);
+
+    wins_init();
+    wins_new_chat(recipient);
+    wins_set_current_by_num(2);
 
     expect_ui_current_print_formatted_line('!', 0, "You are not currently in an OTR session.");
 
@@ -435,6 +444,7 @@ void cmd_otr_theirfp_shows_message_when_non_otr_chat_window(void **state)
     assert_true(result);
 
     free(help);
+    wins_close_current();
 }
 
 void cmd_otr_theirfp_shows_fingerprint(void **state)
@@ -450,6 +460,7 @@ void cmd_otr_theirfp_shows_fingerprint(void **state)
     ProfChatWin *chatwin = malloc(sizeof(ProfChatWin));
     chatwin->barejid = strdup(recipient);
     chatwin->memcheck = PROFCHATWIN_MEMCHECK;
+    chatwin->enc_mode = PROF_ENC_OTR;
     will_return(win_create_chat, &chatwin->window);
 
     wins_init();
@@ -458,7 +469,6 @@ void cmd_otr_theirfp_shows_fingerprint(void **state)
 
     will_return(jabber_get_connection_status, JABBER_CONNECTED);
     will_return(ui_current_win_type, WIN_CHAT);
-    will_return(ui_current_win_is_otr, TRUE);
 
     expect_string(otr_get_their_fingerprint, recipient, chatwin->barejid);
     will_return(otr_get_their_fingerprint, strdup(fingerprint));
@@ -507,12 +517,22 @@ void cmd_otr_start_shows_message_when_in_private(void **state)
 
 void cmd_otr_start_shows_message_when_already_started(void **state)
 {
+    char *recipient = "someone@server.org";
     CommandHelp *help = malloc(sizeof(CommandHelp));
     gchar *args[] = { "start", NULL };
 
     will_return(jabber_get_connection_status, JABBER_CONNECTED);
     will_return(ui_current_win_type, WIN_CHAT);
-    will_return(ui_current_win_is_otr, TRUE);
+
+    ProfChatWin *chatwin = malloc(sizeof(ProfChatWin));
+    chatwin->barejid = strdup(recipient);
+    chatwin->memcheck = PROFCHATWIN_MEMCHECK;
+    chatwin->enc_mode = PROF_ENC_OTR;
+    will_return(win_create_chat, &chatwin->window);
+
+    wins_init();
+    wins_new_chat(recipient);
+    wins_set_current_by_num(2);
 
     expect_ui_current_print_formatted_line('!', 0, "You are already in an OTR session.");
 
@@ -520,24 +540,36 @@ void cmd_otr_start_shows_message_when_already_started(void **state)
     assert_true(result);
 
     free(help);
+    wins_close_current();
 }
 
 void cmd_otr_start_shows_message_when_no_key(void **state)
 {
+    char *recipient = "someone@server.org";
     CommandHelp *help = malloc(sizeof(CommandHelp));
     gchar *args[] = { "start", NULL };
 
     will_return(jabber_get_connection_status, JABBER_CONNECTED);
     will_return(ui_current_win_type, WIN_CHAT);
-    will_return(ui_current_win_is_otr, FALSE);
     will_return(otr_key_loaded, FALSE);
 
+    ProfChatWin *chatwin = malloc(sizeof(ProfChatWin));
+    chatwin->barejid = strdup(recipient);
+    chatwin->memcheck = PROFCHATWIN_MEMCHECK;
+    chatwin->enc_mode = PROF_ENC_NONE;
+    will_return(win_create_chat, &chatwin->window);
+
+    wins_init();
+    wins_new_chat(recipient);
+    wins_set_current_by_num(2);
+
     expect_ui_current_print_formatted_line('!', 0, "You have not generated or loaded a private key, use '/otr gen'");
 
     gboolean result = cmd_otr(args, *help);
     assert_true(result);
 
     free(help);
+    wins_close_current();
 }
 
 void
@@ -559,7 +591,6 @@ cmd_otr_start_sends_otr_query_message_to_current_recipeint(void **state)
 
     will_return(jabber_get_connection_status, JABBER_CONNECTED);
     will_return(ui_current_win_type, WIN_CHAT);
-    will_return(ui_current_win_is_otr, FALSE);
     will_return(otr_key_loaded, TRUE);
     will_return(otr_start_query, query_message);
 
diff --git a/tests/unittests/ui/stub_ui.c b/tests/unittests/ui/stub_ui.c
index ac1ad1b8..c2626158 100644
--- a/tests/unittests/ui/stub_ui.c
+++ b/tests/unittests/ui/stub_ui.c
@@ -126,11 +126,6 @@ win_type_t ui_current_win_type(void)
     return (win_type_t)mock();
 }
 
-gboolean ui_current_win_is_otr(void)
-{
-    return (gboolean)mock();
-}
-
 void ui_current_print_line(const char * const msg, ...)
 {
     va_list args;