about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/omemo/omemo.c43
1 files changed, 23 insertions, 20 deletions
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 1efe0296..9e46058e 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -72,21 +72,6 @@ void
 omemo_init(void)
 {
     log_info("OMEMO: initialising");
-    signal_crypto_provider crypto_provider = {
-        .random_func = omemo_random_func,
-        .hmac_sha256_init_func = omemo_hmac_sha256_init_func,
-        .hmac_sha256_update_func = omemo_hmac_sha256_update_func,
-        .hmac_sha256_final_func = omemo_hmac_sha256_final_func,
-        .hmac_sha256_cleanup_func = omemo_hmac_sha256_cleanup_func,
-        .sha512_digest_init_func = omemo_sha512_digest_init_func,
-        .sha512_digest_update_func = omemo_sha512_digest_update_func,
-        .sha512_digest_final_func = omemo_sha512_digest_final_func,
-        .sha512_digest_cleanup_func = omemo_sha512_digest_cleanup_func,
-        .encrypt_func = omemo_encrypt_func,
-        .decrypt_func = omemo_decrypt_func,
-        .user_data = NULL
-    };
-
     if (omemo_crypto_init() != 0) {
         cons_show("Error initializing OMEMO crypto");
     }
@@ -95,6 +80,14 @@ omemo_init(void)
     pthread_mutexattr_settype(&omemo_ctx.attr, PTHREAD_MUTEX_RECURSIVE);
     pthread_mutex_init(&omemo_ctx.lock, &omemo_ctx.attr);
 
+    omemo_ctx.fingerprint_ac = autocomplete_new();
+}
+
+void
+omemo_on_connect(ProfAccount *account)
+{
+    GError *error = NULL;
+
     if (signal_context_create(&omemo_ctx.signal, &omemo_ctx) != 0) {
         cons_show("Error initializing OMEMO context");
         return;
@@ -104,6 +97,21 @@ omemo_init(void)
         cons_show("Error initializing OMEMO log");
     }
 
+    signal_crypto_provider crypto_provider = {
+        .random_func = omemo_random_func,
+        .hmac_sha256_init_func = omemo_hmac_sha256_init_func,
+        .hmac_sha256_update_func = omemo_hmac_sha256_update_func,
+        .hmac_sha256_final_func = omemo_hmac_sha256_final_func,
+        .hmac_sha256_cleanup_func = omemo_hmac_sha256_cleanup_func,
+        .sha512_digest_init_func = omemo_sha512_digest_init_func,
+        .sha512_digest_update_func = omemo_sha512_digest_update_func,
+        .sha512_digest_final_func = omemo_sha512_digest_final_func,
+        .sha512_digest_cleanup_func = omemo_sha512_digest_cleanup_func,
+        .encrypt_func = omemo_encrypt_func,
+        .decrypt_func = omemo_decrypt_func,
+        .user_data = NULL
+    };
+
     if (signal_context_set_crypto_provider(omemo_ctx.signal, &crypto_provider) != 0) {
         cons_show("Error initializing OMEMO crypto");
         return;
@@ -166,12 +174,7 @@ omemo_init(void)
     omemo_ctx.known_devices = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_free);
 
     omemo_ctx.fingerprint_ac = autocomplete_new();
-}
 
-void
-omemo_on_connect(ProfAccount *account)
-{
-    GError *error = NULL;
     char *omemodir = files_get_data_path(DIR_OMEMO);
     GString *basedir = g_string_new(omemodir);
     free(omemodir);
3'>203 204 205 206 207
#include <glib.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>

#include <stabber.h>
#include <expect.h>

#include "proftest.h"

void
sends_message_to_barejid_when_contact_offline(void **state)
{
    prof_connect();

    prof_input("/msg buddy1@localhost Hi there");

    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost' type='chat'>"
            "<body>Hi there</body>"
        "</message>"
    ));
}

void
sends_message_to_barejid_when_contact_online(void **state)
{
    prof_connect();

    stbbr_send(
        "<presence to='stabber@localhost/profanity' from='buddy1@localhost/mobile'>"
            "<priority>10</priority>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (mobile) is online"));

    prof_input("/msg buddy1@localhost Hi there");

    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost' type='chat'>"
            "<body>Hi there</body>"
        "</message>"
    ));
}

void
sends_message_to_fulljid_when_received_from_fulljid(void **state)
{
    prof_connect();

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/mobile'>"
            "<priority>10</priority>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (mobile) is online"));

    stbbr_send(
        "<message id='message1' to='stabber@localhost' from='buddy1@localhost/mobile' type='chat'>"
            "<body>First message</body>"
        "</message>"
    );
    assert_true(prof_output_exact("<< chat message: Buddy1/mobile (win 2)"));

    prof_input("/msg buddy1@localhost Hi there");

    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Hi there</body>"
        "</message>"
    ));
}

void
sends_subsequent_messages_to_fulljid(void **state)
{
    prof_connect();

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/mobile'>"
            "<priority>10</priority>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (mobile) is online"));

    stbbr_send(
        "<message id='message1' to='stabber@localhost' from='buddy1@localhost/mobile' type='chat'>"
            "<body>First message</body>"
        "</message>"
    );
    assert_true(prof_output_exact("<< chat message: Buddy1/mobile (win 2)"));

    prof_input("/msg buddy1@localhost Outgoing 1");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Outgoing 1</body>"
        "</message>"
    ));

    prof_input("/msg buddy1@localhost Outgoing 2");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Outgoing 2</body>"
        "</message>"
    ));

    prof_input("/msg buddy1@localhost Outgoing 3");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Outgoing 3</body>"
        "</message>"
    ));
}

void
resets_to_barejid_after_presence_received(void **state)
{
    prof_connect();

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/mobile'>"
            "<priority>10</priority>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (mobile) is online"));

    stbbr_send(
        "<message id='message1' to='stabber@localhost' from='buddy1@localhost/mobile' type='chat'>"
            "<body>First message</body>"
        "</message>"
    );
    assert_true(prof_output_exact("<< chat message: Buddy1/mobile (win 2)"));

    prof_input("/msg buddy1@localhost Outgoing 1");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Outgoing 1</body>"
        "</message>"
    ));

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/laptop'>"
            "<priority>5</priority>"
            "<show>dnd</show>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (laptop) is dnd"));

    prof_input("/msg buddy1@localhost Outgoing 2");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost' type='chat'>"
            "<body>Outgoing 2</body>"
        "</message>"
    ));
}

void
new_session_when_message_received_from_different_fulljid(void **state)
{
    prof_connect();

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/mobile'>"
            "<priority>10</priority>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (mobile) is online"));

    stbbr_send(
        "<presence to='stabber@localhost' from='buddy1@localhost/laptop'>"
            "<priority>8</priority>"
            "<show>away</show>"
        "</presence>"
    );
    assert_true(prof_output_exact("Buddy1 (laptop) is away"));

    stbbr_send(
        "<message id='message1' to='stabber@localhost' from='buddy1@localhost/mobile' type='chat'>"
            "<body>From first resource</body>"
        "</message>"
    );
    assert_true(prof_output_exact("<< chat message: Buddy1/mobile (win 2)"));

    prof_input("/msg buddy1@localhost Outgoing 1");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/mobile' type='chat'>"
            "<body>Outgoing 1</body>"
        "</message>"
    ));

    stbbr_send(
        "<message id='message1' to='stabber@localhost' from='buddy1@localhost/laptop' type='chat'>"
            "<body>From second resource</body>"
        "</message>"
    );
    assert_true(prof_output_regex("Buddy1/laptop:.+From second resource"));

    prof_input("/msg buddy1@localhost Outgoing 2");
    assert_true(stbbr_received(
        "<message id='*' to='buddy1@localhost/laptop' type='chat'>"
            "<body>Outgoing 2</body>"
        "</message>"
    ));
}