From a21ab6d4c8d9de9abce9dddd1d23668779ca15e7 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 16 Feb 2014 19:14:15 +0000 Subject: [PATCH] Mock otr module for cmd_otr tests --- Makefile.am | 1 + src/main.c | 6 +++ src/otr/otr.c | 80 ++++++++++++++++++++++-------------- src/otr/otr.h | 32 ++++++++------- tests/config/mock_accounts.h | 5 +++ tests/otr/mock_otr.c | 46 +++++++++++++++++++++ tests/otr/mock_otr.h | 12 ++++++ tests/test_cmd_otr.c | 7 +++- tests/xmpp/mock_xmpp.h | 4 +- 9 files changed, 144 insertions(+), 49 deletions(-) create mode 100644 tests/otr/mock_otr.c create mode 100644 tests/otr/mock_otr.h diff --git a/Makefile.am b/Makefile.am index a2d5058e..b5eea266 100644 --- a/Makefile.am +++ b/Makefile.am @@ -66,6 +66,7 @@ test_sources = \ src/ui/window.c src/ui/window.h \ src/server_events.c src/server_events.h \ tests/xmpp/mock_xmpp.h tests/xmpp/mock_xmpp.c \ + tests/otr/mock_otr.h tests/otr/mock_otr.c \ tests/ui/mock_ui.h tests/ui/mock_ui.c \ tests/config/mock_accounts.h tests/config/mock_accounts.c \ tests/helpers.c tests/helpers.h \ diff --git a/src/main.c b/src/main.c index a87b9280..7796ed96 100644 --- a/src/main.c +++ b/src/main.c @@ -29,6 +29,9 @@ #include "profanity.h" +#ifdef HAVE_LIBOTR +#include "otr/otr.h" +#endif #include "xmpp/xmpp.h" #include "ui/ui.h" @@ -56,6 +59,9 @@ _init_modules(void) titlebar_init_module(); accounts_init_module(); +#ifdef HAVE_LIBOTR + otr_init_module(); +#endif } int diff --git a/src/otr/otr.c b/src/otr/otr.c index 73d30bd6..f28406f2 100644 --- a/src/otr/otr.c +++ b/src/otr/otr.c @@ -93,14 +93,14 @@ cb_gone_secure(void *opdata, ConnContext *context) ui_gone_secure(context->username, otr_is_trusted(context->username)); } -char * -otr_libotr_version(void) +static char * +_otr_libotr_version(void) { return OTRL_VERSION; } -void -otr_init(void) +static void +_otr_init(void) { log_info("Initialising OTR"); OTRL_INIT; @@ -116,8 +116,8 @@ otr_init(void) data_loaded = FALSE; } -void -otr_on_connect(ProfAccount *account) +static void +_otr_on_connect(ProfAccount *account) { jid = strdup(account->jid); log_info("Loading OTR key for %s", jid); @@ -191,8 +191,8 @@ otr_on_connect(ProfAccount *account) return; } -void -otr_keygen(ProfAccount *account) +static void +_otr_keygen(ProfAccount *account) { if (data_loaded) { cons_show("OTR key already generated."); @@ -280,14 +280,14 @@ otr_keygen(ProfAccount *account) return; } -gboolean -otr_key_loaded(void) +static gboolean +_otr_key_loaded(void) { return data_loaded; } -gboolean -otr_is_secure(const char * const recipient) +static gboolean +_otr_is_secure(const char * const recipient) { ConnContext *context = otrlib_context_find(user_state, recipient, jid); @@ -302,8 +302,8 @@ otr_is_secure(const char * const recipient) } } -gboolean -otr_is_trusted(const char * const recipient) +static gboolean +_otr_is_trusted(const char * const recipient) { ConnContext *context = otrlib_context_find(user_state, recipient, jid); @@ -323,8 +323,8 @@ otr_is_trusted(const char * const recipient) return FALSE; } -void -otr_trust(const char * const recipient) +static void +_otr_trust(const char * const recipient) { ConnContext *context = otrlib_context_find(user_state, recipient, jid); @@ -344,8 +344,8 @@ otr_trust(const char * const recipient) return; } -void -otr_untrust(const char * const recipient) +static void +_otr_untrust(const char * const recipient) { ConnContext *context = otrlib_context_find(user_state, recipient, jid); @@ -365,14 +365,14 @@ otr_untrust(const char * const recipient) return; } -void -otr_end_session(const char * const recipient) +static void +_otr_end_session(const char * const recipient) { otrlib_end_session(user_state, recipient, jid, &ops); } -char * -otr_get_my_fingerprint(void) +static char * +_otr_get_my_fingerprint(void) { char fingerprint[45]; otrl_privkey_fingerprint(user_state, fingerprint, jid, "xmpp"); @@ -381,8 +381,8 @@ otr_get_my_fingerprint(void) return result; } -char * -otr_get_their_fingerprint(const char * const recipient) +static char * +_otr_get_their_fingerprint(const char * const recipient) { ConnContext *context = otrlib_context_find(user_state, recipient, jid); @@ -396,8 +396,8 @@ otr_get_their_fingerprint(const char * const recipient) } } -char * -otr_encrypt_message(const char * const to, const char * const message) +static char * +_otr_encrypt_message(const char * const to, const char * const message) { char *newmessage = NULL; gcry_error_t err = otrlib_encrypt_message(user_state, &ops, jid, to, message, &newmessage); @@ -409,8 +409,8 @@ otr_encrypt_message(const char * const to, const char * const message) } } -char * -otr_decrypt_message(const char * const from, const char * const message, gboolean *was_decrypted) +static char * +_otr_decrypt_message(const char * const from, const char * const message, gboolean *was_decrypted) { char *decrypted = NULL; OtrlTLV *tlvs = NULL; @@ -443,8 +443,28 @@ otr_decrypt_message(const char * const from, const char * const message, gboolea } } -void -otr_free_message(char *message) +static void +_otr_free_message(char *message) { otrl_message_free(message); } + +void +otr_init_module(void) +{ + otr_init = _otr_init; + otr_libotr_version = _otr_libotr_version; + otr_on_connect = _otr_on_connect; + otr_keygen = _otr_keygen; + otr_key_loaded = _otr_key_loaded; + otr_is_secure = _otr_is_secure; + otr_is_trusted = _otr_is_trusted; + otr_trust = _otr_trust; + otr_untrust = _otr_untrust; + otr_end_session = _otr_end_session; + otr_get_my_fingerprint = _otr_get_my_fingerprint; + otr_get_their_fingerprint = _otr_get_their_fingerprint; + otr_encrypt_message = _otr_encrypt_message; + otr_decrypt_message = _otr_decrypt_message; + otr_free_message = _otr_free_message; +} diff --git a/src/otr/otr.h b/src/otr/otr.h index 85ae09cc..eaf975d3 100644 --- a/src/otr/otr.h +++ b/src/otr/otr.h @@ -25,27 +25,29 @@ #include "config/accounts.h" -void otr_init(void); -char* otr_libotr_version(void); -void otr_on_connect(ProfAccount *account); -void otr_keygen(ProfAccount *account); +void otr_init_module(void); -gboolean otr_key_loaded(void); -gboolean otr_is_secure(const char * const recipient); +void (*otr_init)(void); +char* (*otr_libotr_version)(void); +void (*otr_on_connect)(ProfAccount *account); +void (*otr_keygen)(ProfAccount *account); -gboolean otr_is_trusted(const char * const recipient); -void otr_trust(const char * const recipient); -void otr_untrust(const char * const recipient); +gboolean (*otr_key_loaded)(void); +gboolean (*otr_is_secure)(const char * const recipient); -void otr_end_session(const char * const recipient); +gboolean (*otr_is_trusted)(const char * const recipient); +void (*otr_trust)(const char * const recipient); +void (*otr_untrust)(const char * const recipient); -char * otr_get_my_fingerprint(void); -char * otr_get_their_fingerprint(const char * const recipient); +void (*otr_end_session)(const char * const recipient); -char * otr_encrypt_message(const char * const to, const char * const message); -char * otr_decrypt_message(const char * const from, const char * const message, +char * (*otr_get_my_fingerprint)(void); +char * (*otr_get_their_fingerprint)(const char * const recipient); + +char * (*otr_encrypt_message)(const char * const to, const char * const message); +char * (*otr_decrypt_message)(const char * const from, const char * const message, gboolean *was_decrypted); -void otr_free_message(char *message); +void (*otr_free_message)(char *message); #endif diff --git a/tests/config/mock_accounts.h b/tests/config/mock_accounts.h index f70be745..ee36f5ed 100644 --- a/tests/config/mock_accounts.h +++ b/tests/config/mock_accounts.h @@ -20,6 +20,9 @@ * */ +#ifndef MOCK_ACCOUNTS_H +#define MOCK_ACCOUNTS_H + void mock_accounts_get_account(void); void accounts_get_account_expect_and_return(const char * const name, ProfAccount *account); void accounts_get_account_return(ProfAccount *account); @@ -85,3 +88,5 @@ void accounts_set_login_presence_expect(char *account_name, char *presence); void mock_accounts_get_last_presence(void); void accounts_get_last_presence_return(resource_presence_t presence); + +#endif diff --git a/tests/otr/mock_otr.c b/tests/otr/mock_otr.c new file mode 100644 index 00000000..8750feb7 --- /dev/null +++ b/tests/otr/mock_otr.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "otr/otr.h" +#include "config/account.h" + +static void +_mock_otr_keygen(ProfAccount *account) +{ + check_expected(account); +} + +static char * +_mock_otr_libotr_version(void) +{ + return (char *)mock(); +} + +void +mock_otr_keygen(void) +{ + otr_keygen = _mock_otr_keygen; +} + +void +mock_otr_libotr_version(void) +{ + otr_libotr_version = _mock_otr_libotr_version; +} + +void +otr_keygen_expect(ProfAccount *account) +{ + expect_memory(_mock_otr_keygen, account, account, sizeof(ProfAccount)); +} + +void +otr_libotr_version_returns(char *version) +{ + will_return(_mock_otr_libotr_version, version); +} diff --git a/tests/otr/mock_otr.h b/tests/otr/mock_otr.h new file mode 100644 index 00000000..fd71e3da --- /dev/null +++ b/tests/otr/mock_otr.h @@ -0,0 +1,12 @@ +#ifndef MOCK_OTR_H +#define MOCK_OTR_H + +#include "config/account.h" + +void mock_otr_keygen(void); +void otr_keygen_expect(ProfAccount *account); + +void mock_otr_libotr_version(void); +void otr_libotr_version_returns(char *version); + +#endif diff --git a/tests/test_cmd_otr.c b/tests/test_cmd_otr.c index 92ff5e6e..7d6e8adb 100644 --- a/tests/test_cmd_otr.c +++ b/tests/test_cmd_otr.c @@ -10,6 +10,8 @@ #ifdef HAVE_LIBOTR #include +#include "otr/otr.h" +#include "otr/mock_otr.h" #endif #include "config/preferences.h" @@ -248,10 +250,11 @@ void cmd_otr_libver_shows_libotr_version(void **state) mock_cons_show(); CommandHelp *help = malloc(sizeof(CommandHelp)); gchar *args[] = { "libver", NULL }; - - char *version = OTRL_VERSION; + 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); gboolean result = cmd_otr(args, *help); diff --git a/tests/xmpp/mock_xmpp.h b/tests/xmpp/mock_xmpp.h index 82b4f8d0..5c4c19d0 100644 --- a/tests/xmpp/mock_xmpp.h +++ b/tests/xmpp/mock_xmpp.h @@ -1,5 +1,5 @@ -#ifndef COMMON_MOCKS_H -#define COMMON_MOCKS_H +#ifndef MOCK_XMPP_H +#define MOCK_XMPP_H #include "xmpp/xmpp.h"