mirror of
https://github.com/profanity-im/profanity.git
synced 2024-09-15 19:38:07 -04:00
Mock otr module for cmd_otr tests
This commit is contained in:
parent
6ef1174bf4
commit
a21ab6d4c8
@ -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 \
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
46
tests/otr/mock_otr.c
Normal file
46
tests/otr/mock_otr.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <setjmp.h>
|
||||
#include <cmocka.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
#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);
|
||||
}
|
12
tests/otr/mock_otr.h
Normal file
12
tests/otr/mock_otr.h
Normal file
@ -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
|
@ -10,6 +10,8 @@
|
||||
|
||||
#ifdef HAVE_LIBOTR
|
||||
#include <libotr/proto.h>
|
||||
#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);
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef COMMON_MOCKS_H
|
||||
#define COMMON_MOCKS_H
|
||||
#ifndef MOCK_XMPP_H
|
||||
#define MOCK_XMPP_H
|
||||
|
||||
#include "xmpp/xmpp.h"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user