Files
cproof/tests/unittests/test_forced_encryption.c
Jabber Developer f4405d114a
All checks were successful
CI / Check coding style (pull_request) Successful in 35s
CI / Check spelling (pull_request) Successful in 18s
CI / Linux (debian) (pull_request) Successful in 10m28s
CI / Linux (ubuntu) (pull_request) Successful in 10m51s
CI / Linux (arch) (pull_request) Successful in 13m17s
CI / Check spelling (push) Successful in 17s
CI / Check coding style (push) Successful in 33s
CI / Linux (arch) (push) Successful in 12m53s
CI / Linux (ubuntu) (push) Successful in 12m56s
CI / Linux (debian) (push) Successful in 13m4s
Add unit tests for forced encryption check function
2025-08-27 16:48:27 +02:00

136 lines
5.2 KiB
C

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <glib.h>
#include "event/client_events.h"
#include "command/cmd_funcs.h"
#include "config/preferences.h"
#include "ui/stub_ui.h"
#define CMD_FORCE_ENCRYPTION "/force-encryption"
// Test: /force-encryption with no arguments shows error.
void
test_cmd_force_encryption_no_args_bad_cmd(void** state)
{
char* args[] = { NULL };
expect_string(cons_bad_cmd_usage, cmd, CMD_FORCE_ENCRYPTION);
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
}
// Test: /force-encryption on enables encryption.
void
test_cmd_force_encryption_toggles_on(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, FALSE);
char* args[] = { "on", NULL };
expect_cons_show("Forces encryption enabled.");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
assert_true(prefs_get_boolean(PREF_FORCE_ENCRYPTION));
}
// Test: /force-encryption off disables encryption.
void
test_cmd_force_encryption_toggles_off(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, TRUE);
char* args[] = { "off", NULL };
expect_cons_show("Forces encryption disabled.");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
assert_false(prefs_get_boolean(PREF_FORCE_ENCRYPTION));
}
// Test: /force-encryption off when already off.
void
test_cmd_force_encryption_toggles_off_already(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, FALSE);
char* args[] = { "off", NULL };
expect_cons_show("Forces encryption is already disabled.");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
assert_false(prefs_get_boolean(PREF_FORCE_ENCRYPTION));
}
// Test: /force-encryption on when already on.
void
test_cmd_force_encryption_toggles_on_already(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, TRUE);
char* args[] = { "on", NULL };
expect_cons_show("Forces encryption is already enabled.");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
assert_true(prefs_get_boolean(PREF_FORCE_ENCRYPTION));
}
// Test: /force-encryption policy block sets block mode.
void
test_cmd_force_encryption_sets_policy_block(void** state)
{
char* args[] = { "policy", "block", NULL };
expect_cons_show("Force encryption policy has been set to \"block\".");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
auto_gchar gchar* pref_force_enc_mode = prefs_get_string(PREF_FORCE_ENCRYPTION_MODE);
assert_string_equal(pref_force_enc_mode, "block");
}
// Test: /force-encryption policy resend-to-confirm sets resend mode.
void
test_cmd_force_encryption_sets_policy_resend(void** state)
{
char* args[] = { "policy", "resend-to-confirm", NULL };
expect_cons_show("Force encryption policy has been set to \"resend-to-confirm\".");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
auto_gchar gchar* pref_force_enc_mode = prefs_get_string(PREF_FORCE_ENCRYPTION_MODE);
assert_string_equal(pref_force_enc_mode, "resend-to-confirm");
}
// Test: Unencrypted message blocked when policy is block.
void
test_allow_unencrypted_message_blocks(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, TRUE);
prefs_set_string(PREF_FORCE_ENCRYPTION_MODE, "block");
ProfChatWin win = {};
char* msg = "test message";
expect_win_println("Message not sent: encryption required. Enable (omemo/pgp/otr) encryption or /force-encryption off.");
assert_false(allow_unencrypted_message(&win, msg));
}
// Test: Unencrypted message prompts confirmation, then allows on second attempt.
void
test_allow_unencrypted_message_confirms_on_second_attempt(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, TRUE);
prefs_set_string(PREF_FORCE_ENCRYPTION_MODE, "resend-to-confirm");
ProfChatWin win = { .last_attempted_msg_hash = 0 };
char* msg = "test message";
expect_win_println("Message not sent: encryption required. Enable (omemo/pgp/otr) encryption or press Enter again to send without encryption. Use /force-encryption off to disable this protection.");
assert_false(allow_unencrypted_message(&win, msg));
assert_int_not_equal(win.last_attempted_msg_hash, 0);
assert_true(allow_unencrypted_message(&win, msg));
assert_int_equal(win.last_attempted_msg_hash, 0); // ensure that value resets since we don't want to approve the same message twice
}
// Test: Invalid policy argument fails gracefully.
void
test_cmd_force_encryption_invalid_policy(void** state)
{
char* args[] = { "policy", "invalid", NULL };
expect_cons_show_error("Invalid policy: \"invalid\". See '/help force-encryption' for details.");
assert_true(cmd_force_encryption(NULL, CMD_FORCE_ENCRYPTION, args));
}
// Test: Invalid encryption mode blocks message.
void
test_allow_unencrypted_message_invalid_mode(void** state)
{
prefs_set_boolean(PREF_FORCE_ENCRYPTION, TRUE);
prefs_set_string(PREF_FORCE_ENCRYPTION_MODE, "invalid");
ProfChatWin win = { .last_attempted_msg_hash = 0 };
char* msg = "test message";
expect_win_println("Message not sent: invalid encryption mode (invalid). Use '/force-encryption policy resend-to-confirm'.");
assert_false(allow_unencrypted_message(&win, msg));
assert_int_equal(win.last_attempted_msg_hash, 0);
}