Compare commits
5 Commits
58c23c4089
...
test/db-fu
| Author | SHA1 | Date | |
|---|---|---|---|
|
b289d1a753
|
|||
|
31538580fb
|
|||
|
37ca2de308
|
|||
|
4fce333c9a
|
|||
|
467222d0ca
|
3
.github/workflows/ci-code.yml
vendored
3
.github/workflows/ci-code.yml
vendored
@@ -50,6 +50,9 @@ jobs:
|
||||
run: |
|
||||
grep -P 'auto_(char|gchar|gcharv|guchar|jid|sqlite|gfd|FILE)[\w *]*;$' -r src && exit -1 || true
|
||||
|
||||
- name: Check CWE-134 format string vulnerabilities
|
||||
run: ./check-cwe134.sh
|
||||
|
||||
- name: Install clang-format
|
||||
run: |
|
||||
sudo apt-get update
|
||||
|
||||
@@ -144,6 +144,16 @@ scan-build make
|
||||
scan-view ...
|
||||
```
|
||||
|
||||
### Security checks
|
||||
|
||||
We have a static analyzer `check-cwe134.sh` that detects CWE-134 format string vulnerabilities. It runs automatically in CI but you can also run it locally:
|
||||
|
||||
```bash
|
||||
./check-cwe134.sh
|
||||
```
|
||||
|
||||
This checks for unsafe patterns where data could be passed directly as a format string to functions like `printf`, `cons_show`, etc. Never pass a raw string for formatting; use `"%s"` format specifier instead.
|
||||
|
||||
### Finding typos
|
||||
|
||||
We include a `.codespellrc` configuration file for `codespell` in the root directory.
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
FROM archlinux
|
||||
FROM archlinux:latest
|
||||
|
||||
ENV TERM=xterm
|
||||
ENV CC="ccache gcc"
|
||||
|
||||
RUN pacman -Syu --noconfirm
|
||||
RUN pacman -Syyu --noconfirm
|
||||
|
||||
# reflector is optional - if it fails due to network issues, continue with default mirrorlist
|
||||
RUN pacman -S --needed --noconfirm reflector && \
|
||||
(reflector --latest 20 --protocol https --sort rate --save /etc/pacman.d/mirrorlist || true)
|
||||
|
||||
@@ -168,8 +168,6 @@ unittest_sources = \
|
||||
tests/unittests/test_callbacks.c tests/unittests/test_callbacks.h \
|
||||
tests/unittests/test_plugins_disco.c tests/unittests/test_plugins_disco.h \
|
||||
tests/unittests/test_forced_encryption.c tests/unittests/test_forced_encryption.h \
|
||||
tests/unittests/test_helpers.c tests/unittests/test_helpers.h \
|
||||
tests/unittests/test_autoping.c tests/unittests/test_autoping.h \
|
||||
tests/unittests/unittests.c
|
||||
|
||||
functionaltest_sources = \
|
||||
@@ -187,6 +185,7 @@ functionaltest_sources = \
|
||||
tests/functionaltests/test_software.c tests/functionaltests/test_software.h \
|
||||
tests/functionaltests/test_muc.c tests/functionaltests/test_muc.h \
|
||||
tests/functionaltests/test_disconnect.c tests/functionaltests/test_disconnect.h \
|
||||
tests/functionaltests/test_history.c tests/functionaltests/test_history.h \
|
||||
tests/functionaltests/test_lastactivity.c tests/functionaltests/test_lastactivity.h \
|
||||
tests/functionaltests/functionaltests.c
|
||||
|
||||
|
||||
@@ -168,10 +168,6 @@ num_cores()
|
||||
# Run test failure detection verification first
|
||||
verify_test_failure_detection
|
||||
|
||||
# Run CWE-134 format string vulnerability check
|
||||
echo "=== Running CWE-134 security check ==="
|
||||
./check-cwe134.sh || { echo "CWE-134 check failed!"; exit 1; }
|
||||
|
||||
# Parse arguments
|
||||
COVERAGE_ONLY=no
|
||||
for arg in "$@"; do
|
||||
|
||||
@@ -77,6 +77,8 @@ _db_teardown(const char* ctx)
|
||||
}
|
||||
g_chatlog_database = NULL;
|
||||
}
|
||||
// Safe to call unconditionally; no-op if not initialized.
|
||||
// See: https://www.sqlite.org/c3ref/initialize.html
|
||||
sqlite3_shutdown();
|
||||
}
|
||||
|
||||
|
||||
@@ -148,7 +148,8 @@ create_input_window(void)
|
||||
* Fail gracefully instead of aborting in production.
|
||||
*/
|
||||
if (MB_CUR_MAX > PROF_MB_CUR_MAX) {
|
||||
cons_show_error("Your locale's MB_CUR_MAX (%zu) exceeds PROF_MB_CUR_MAX (%d); input window disabled.", (size_t)MB_CUR_MAX, PROF_MB_CUR_MAX);
|
||||
log_error("Locale MB_CUR_MAX (%zu) exceeds compiled limit (%d)", (size_t)MB_CUR_MAX, PROF_MB_CUR_MAX);
|
||||
cons_show_error("Unsupported locale. Before running, execute in terminal: export LC_ALL=C.UTF-8");
|
||||
return;
|
||||
}
|
||||
#ifdef NCURSES_REENTRANT
|
||||
@@ -167,7 +168,7 @@ create_input_window(void)
|
||||
|
||||
inp_win = newpad(1, INP_WIN_MAX);
|
||||
if (!inp_win) {
|
||||
// Failed to allocate input pad; leave inp_win NULL and avoid further use
|
||||
log_error("Failed to allocate input window pad");
|
||||
return;
|
||||
}
|
||||
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "log.h"
|
||||
|
||||
#ifdef HAVE_NCURSESW_NCURSES_H
|
||||
#include <ncursesw/ncurses.h>
|
||||
#elif HAVE_NCURSES_H
|
||||
@@ -112,6 +114,7 @@ status_bar_init(void)
|
||||
int row = screen_statusbar_row();
|
||||
int cols = getmaxx(stdscr);
|
||||
if (cols <= 0) {
|
||||
log_warning("status_bar_init: invalid cols %d, defaulting to 1", cols);
|
||||
cols = 1;
|
||||
}
|
||||
statusbar_win = newwin(1, cols, row, 0);
|
||||
@@ -157,6 +160,7 @@ status_bar_resize(void)
|
||||
}
|
||||
int cols = getmaxx(stdscr);
|
||||
if (cols <= 0) {
|
||||
log_warning("status_bar_resize: invalid cols %d, defaulting to 1", cols);
|
||||
cols = 1;
|
||||
}
|
||||
werase(statusbar_win);
|
||||
|
||||
@@ -79,15 +79,7 @@ static void _win_print_wrapped(WINDOW* win, const char* const message, size_t in
|
||||
static int
|
||||
_check_subwin_width(int cols, int width)
|
||||
{
|
||||
if (cols > 1) {
|
||||
if (width < 1)
|
||||
width = 1;
|
||||
if (width >= cols)
|
||||
width = cols - 1;
|
||||
} else {
|
||||
width = 1;
|
||||
}
|
||||
return width;
|
||||
return cols <= 1 ? 1 : CLAMP(width, 1, cols - 1);
|
||||
}
|
||||
|
||||
int
|
||||
|
||||
@@ -2532,9 +2532,6 @@ _disco_items_result_handler(xmpp_stanza_t* const stanza)
|
||||
}
|
||||
|
||||
xmpp_stanza_t* child = xmpp_stanza_get_children(query);
|
||||
if (child == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (child) {
|
||||
const char* stanza_name = xmpp_stanza_get_name(child);
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
* functional tests run less frequently than unit tests.
|
||||
*
|
||||
* Tests are organized into groups for better maintainability and parallel execution:
|
||||
* Group 1: Connect, Ping, Rooms, Software
|
||||
* Group 2: Message, Receipts, Roster, Chat Session
|
||||
* Group 3: Presence, Disconnect
|
||||
* Group 4: MUC, Carbons
|
||||
* Group 1: Connect, Ping, Rooms, Software, Last Activity (19 tests)
|
||||
* Group 2: Message, Receipts, Roster, DB History (22 tests)
|
||||
* Group 3: Chat Session, Presence, Disconnect (22 tests)
|
||||
* Group 4: MUC, Carbons (21 tests)
|
||||
*
|
||||
* Parallel execution:
|
||||
* ./functionaltests - run all tests sequentially
|
||||
@@ -53,6 +53,7 @@
|
||||
#include "test_muc.h"
|
||||
#include "test_disconnect.h"
|
||||
#include "test_lastactivity.h"
|
||||
#include "test_history.h"
|
||||
|
||||
/* Macro to wrap each test with setup/teardown functions */
|
||||
#define PROF_FUNC_TEST(test) cmocka_unit_test_setup_teardown(test, init_prof_test, close_prof_test)
|
||||
@@ -77,8 +78,9 @@ main(int argc, char* argv[])
|
||||
fprintf(stderr, "[PROF_TEST] Starting functional tests, group=%d\n", group);
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 1: Connect, Ping, Rooms, Software
|
||||
* GROUP 1: Connect, Ping, Rooms, Software, Last Activity
|
||||
* Basic XMPP session establishment and server queries
|
||||
* (19 tests)
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group1_tests[] = {
|
||||
/* Connection tests - verify login, roster, bookmarks */
|
||||
@@ -112,8 +114,9 @@ main(int argc, char* argv[])
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 2: Message, Receipts, Roster, Chat Session
|
||||
* GROUP 2: Message, Receipts, Roster, DB History
|
||||
* Core messaging and contact management
|
||||
* (23 tests)
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group2_tests[] = {
|
||||
/* Basic message send/receive */
|
||||
@@ -133,6 +136,29 @@ main(int argc, char* argv[])
|
||||
PROF_FUNC_TEST(sends_remove_item_nick),
|
||||
PROF_FUNC_TEST(sends_nick_change),
|
||||
|
||||
/* Database persistence - message write+read round-trip */
|
||||
PROF_FUNC_TEST(message_db_history_on_reopen),
|
||||
PROF_FUNC_TEST(message_db_history_multiple),
|
||||
PROF_FUNC_TEST(message_db_history_contact_isolation),
|
||||
PROF_FUNC_TEST(message_db_history_special_chars),
|
||||
PROF_FUNC_TEST(message_db_history_outgoing),
|
||||
PROF_FUNC_TEST(message_db_history_dialog),
|
||||
PROF_FUNC_TEST(message_db_history_empty),
|
||||
PROF_FUNC_TEST(message_db_history_long_message),
|
||||
PROF_FUNC_TEST(message_db_history_newline),
|
||||
PROF_FUNC_TEST(message_db_history_service_chars),
|
||||
#ifdef HAVE_HISTORY_VERIFY
|
||||
PROF_FUNC_TEST(message_db_history_verify),
|
||||
#endif
|
||||
PROF_FUNC_TEST(message_db_history_lmc),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 3: Chat Session, Presence, Disconnect
|
||||
* Session routing, status management, clean teardown
|
||||
* (22 tests)
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group3_tests[] = {
|
||||
/* Chat session management - bare/full JID routing */
|
||||
PROF_FUNC_TEST(sends_message_to_barejid_when_contact_offline),
|
||||
PROF_FUNC_TEST(sends_message_to_barejid_when_contact_online),
|
||||
@@ -140,13 +166,8 @@ main(int argc, char* argv[])
|
||||
PROF_FUNC_TEST(sends_subsequent_messages_to_fulljid),
|
||||
PROF_FUNC_TEST(resets_to_barejid_after_presence_received),
|
||||
PROF_FUNC_TEST(new_session_when_message_received_from_different_fulljid),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 3: Presence, Disconnect
|
||||
* Online/away/xa/dnd/chat status management
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group3_tests[] = {
|
||||
/* Presence - online/away/xa/dnd/chat status management */
|
||||
PROF_FUNC_TEST(presence_online),
|
||||
PROF_FUNC_TEST(presence_online_with_message),
|
||||
PROF_FUNC_TEST(presence_away),
|
||||
@@ -170,6 +191,7 @@ main(int argc, char* argv[])
|
||||
/* ============================================================
|
||||
* GROUP 4: MUC, Carbons
|
||||
* Multi-user chat and message synchronization
|
||||
* (21 tests)
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group4_tests[] = {
|
||||
/* MUC room join with various options - XEP-0045 */
|
||||
@@ -212,9 +234,9 @@ main(int argc, char* argv[])
|
||||
const struct CMUnitTest* tests;
|
||||
size_t count;
|
||||
} groups[] = {
|
||||
{ "Group 1: Connect/Ping/Rooms/Software", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 2: Message/Receipts/Roster/Session", group2_tests, ARRAY_SIZE(group2_tests) },
|
||||
{ "Group 3: Presence/Disconnect", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 1: Connect/Ping/Rooms/Software/LastActivity", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 2: Message/Receipts/Roster/DBHistory", group2_tests, ARRAY_SIZE(group2_tests) },
|
||||
{ "Group 3: Session/Presence/Disconnect", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 4: MUC/Carbons", group4_tests, ARRAY_SIZE(group4_tests) },
|
||||
};
|
||||
const int num_groups = ARRAY_SIZE(groups);
|
||||
|
||||
475
tests/functionaltests/test_history.c
Normal file
475
tests/functionaltests/test_history.c
Normal file
@@ -0,0 +1,475 @@
|
||||
/*
|
||||
* test_history.c
|
||||
*
|
||||
* Functional test for database message persistence.
|
||||
* Verifies that a received chat message is written to the database
|
||||
* and loaded back as history when the chat window is reopened.
|
||||
*
|
||||
* These tests only exercise the public log_database API through normal
|
||||
* profanity UI operations, so they work with any storage backend.
|
||||
*/
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stabber.h>
|
||||
|
||||
#include "proftest.h"
|
||||
|
||||
/*
|
||||
* Test: message written to DB is loaded as history on chat window reopen.
|
||||
*
|
||||
* Flow:
|
||||
* 1. Connect and enable /history on (enables PREF_CHLOG + PREF_HISTORY)
|
||||
* 2. Receive a chat message while on the console window — the message
|
||||
* body is NOT rendered to the terminal (only a console notification
|
||||
* "<< chat message: ... (win N)" appears), but the message IS
|
||||
* written to the database via chat_log_msg_in()
|
||||
* 3. Close the auto-created chat window (destroying its in-memory buffer)
|
||||
* 4. Reopen the chat window with /msg — chatwin_new() calls
|
||||
* _chatwin_history() which reads from log_database_get_previous_chat()
|
||||
* 5. Assert the message body appears in the terminal output, proving
|
||||
* it was persisted to and read back from the database
|
||||
*/
|
||||
void
|
||||
message_db_history_on_reopen(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
/* Enable chat logging and history display */
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Receive a message while on the console window.
|
||||
* The body text is NOT printed to the visible terminal — only the
|
||||
* console notification appears. The message is logged to the DB. */
|
||||
stbbr_send(
|
||||
"<message id='hist-db-test-1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>persistence-roundtrip-check-42</body>"
|
||||
"</message>");
|
||||
|
||||
/* Wait for the console notification (proves message was received).
|
||||
* buddy1@localhost is in the roster as "Buddy1", and PREF_RESOURCE_MESSAGE
|
||||
* is on by default, so the display name is "Buddy1/phone". */
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
/* Close the chat window that was auto-created for the incoming message.
|
||||
* This destroys its in-memory buffer so the text is gone from UI. */
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
/* Reopen the chat window — chatwin_new() will load history from DB.
|
||||
* The message body should now appear on screen for the first time. */
|
||||
prof_input("/msg buddy1@localhost");
|
||||
|
||||
/* The history-loaded message must contain the body we sent earlier.
|
||||
* Since the body was never rendered to the terminal before (it was
|
||||
* only in the hidden window's ncurses buffer which was destroyed),
|
||||
* finding it now proves the database write+read round-trip works. */
|
||||
assert_true(prof_output_exact("persistence-roundtrip-check-42"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: multiple messages from the same contact are all preserved.
|
||||
*
|
||||
* Uses different resources so each console notification is unique and
|
||||
* we can reliably synchronise on each delivery before proceeding.
|
||||
*/
|
||||
void
|
||||
message_db_history_multiple(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Three messages from different resources → distinct notifications */
|
||||
stbbr_send(
|
||||
"<message id='multi1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>db-multi-first-aaa</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='multi2' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/laptop' type='chat'>"
|
||||
"<body>db-multi-second-bbb</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/laptop (win 2)"));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='multi3' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/tablet' type='chat'>"
|
||||
"<body>db-multi-third-ccc</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/tablet (win 2)"));
|
||||
|
||||
/* Close and reopen */
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
|
||||
/* All three must be present in history */
|
||||
assert_true(prof_output_exact("db-multi-first-aaa"));
|
||||
assert_true(prof_output_exact("db-multi-second-bbb"));
|
||||
assert_true(prof_output_exact("db-multi-third-ccc"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: messages are stored per-contact — buddy1's history does not
|
||||
* leak into buddy2's window.
|
||||
*
|
||||
* 1. Receive from buddy1 and buddy2 while on console (bodies hidden)
|
||||
* 2. Close all chat windows
|
||||
* 3. Open buddy1 → verify buddy1's body present, buddy2's body absent
|
||||
*/
|
||||
void
|
||||
message_db_history_contact_isolation(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Receive from buddy1 */
|
||||
stbbr_send(
|
||||
"<message id='iso1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>isolation-buddy1-only-xyzzy</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
/* Receive from buddy2 */
|
||||
stbbr_send(
|
||||
"<message id='iso2' to='stabber@localhost' "
|
||||
"from='buddy2@localhost/phone' type='chat'>"
|
||||
"<body>isolation-buddy2-only-plugh</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy2/phone (win 3)"));
|
||||
|
||||
/* Close all chat windows at once */
|
||||
prof_input("/close all");
|
||||
assert_true(prof_output_exact("Closed 2 windows."));
|
||||
|
||||
/* Open buddy1 — history should contain ONLY buddy1's message */
|
||||
prof_input("/msg buddy1@localhost");
|
||||
assert_true(prof_output_exact("isolation-buddy1-only-xyzzy"));
|
||||
|
||||
/* buddy2's body was never printed to the terminal (received on
|
||||
* console), so finding it now would mean cross-contact leakage */
|
||||
prof_timeout(3);
|
||||
assert_false(prof_output_exact("isolation-buddy2-only-plugh"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: special characters survive the DB write+read round-trip.
|
||||
*
|
||||
* The XMPP body uses XML entities (& < > ") which the
|
||||
* XML parser decodes before storage. The DB must preserve the decoded
|
||||
* characters and return them unchanged.
|
||||
*/
|
||||
void
|
||||
message_db_history_special_chars(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='spec1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>chars: & <tag> "quoted"</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
|
||||
/* The decoded text must appear exactly as-is */
|
||||
assert_true(prof_output_exact("chars: & <tag> \"quoted\""));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: outgoing message (sent via /msg) persists in DB.
|
||||
*
|
||||
* NOTE: The outgoing body IS rendered to the terminal when sent. Since
|
||||
* prof_output_exact searches the cumulative buffer, the assertion alone
|
||||
* does not conclusively prove DB persistence. However, combined with
|
||||
* the incoming tests, this verifies the end-to-end outgoing write path
|
||||
* and catches crashes in the outgoing DB code.
|
||||
*/
|
||||
void
|
||||
message_db_history_outgoing(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Send outgoing — opens and focuses chat window */
|
||||
prof_input("/msg buddy1@localhost db-outgoing-sent-42");
|
||||
assert_true(prof_output_regex("me: .+db-outgoing-sent-42"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
/* Reopen — history should load the outgoing message */
|
||||
prof_input("/msg buddy1@localhost");
|
||||
assert_true(prof_output_exact("db-outgoing-sent-42"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: a dialog (outgoing + incoming) is fully preserved in history.
|
||||
*
|
||||
* Sends an outgoing message, then closes the window. A reply arrives
|
||||
* while on the console (body never rendered). After reopen, both sides
|
||||
* of the conversation appear — proving the incoming read from DB works
|
||||
* and the outgoing write path completed without error.
|
||||
*/
|
||||
void
|
||||
message_db_history_dialog(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Phase 1: send outgoing (renders to terminal) */
|
||||
prof_input("/msg buddy1@localhost dialog-out-msg-42");
|
||||
assert_true(prof_output_regex("me: .+dialog-out-msg-42"));
|
||||
|
||||
/* Return to console */
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
/* Phase 2: receive reply on console (body NOT in terminal buffer) */
|
||||
stbbr_send(
|
||||
"<message id='dlg-in' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>dialog-in-reply-77</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
/* Close the auto-created window */
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
/* Phase 3: reopen — history should have both directions */
|
||||
prof_input("/msg buddy1@localhost");
|
||||
|
||||
/* Incoming body conclusively proves DB round-trip */
|
||||
assert_true(prof_output_exact("dialog-in-reply-77"));
|
||||
/* Outgoing body is consistent with DB persistence */
|
||||
assert_true(prof_output_exact("dialog-out-msg-42"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: opening a chat window for a contact with no prior messages
|
||||
* does not crash and the window is usable.
|
||||
*/
|
||||
void
|
||||
message_db_history_empty(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* buddy2 has never exchanged messages — history is empty */
|
||||
prof_input("/msg buddy2@localhost");
|
||||
assert_true(prof_output_exact("buddy2@localhost"));
|
||||
|
||||
/* Verify the window is functional by sending a message */
|
||||
prof_input("empty-history-smoke-test");
|
||||
assert_true(prof_output_regex("me: .+empty-history-smoke-test"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: a long message (1000+ characters) is not truncated by the DB.
|
||||
*/
|
||||
void
|
||||
message_db_history_long_message(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Build a 1020-character body: 1000 x 'A' + unique suffix */
|
||||
char body[1100];
|
||||
memset(body, 'A', 1000);
|
||||
strcpy(body + 1000, "-long-db-end-MARKER");
|
||||
|
||||
char xml[1500];
|
||||
snprintf(xml, sizeof(xml),
|
||||
"<message id='long1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>%s</body>"
|
||||
"</message>", body);
|
||||
stbbr_send(xml);
|
||||
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
assert_true(prof_output_exact("-long-db-end-MARKER"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: message body containing embedded newlines (LF) is stored and
|
||||
* loaded correctly.
|
||||
*
|
||||
* Uses XML character reference for literal newline in body.
|
||||
*/
|
||||
void
|
||||
message_db_history_newline(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='nl1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>nl-first-line-7k nl-second-line-9m nl-third-line-2p</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
/* Each fragment must survive the round-trip */
|
||||
assert_true(prof_output_exact("nl-first-line-7k"));
|
||||
assert_true(prof_output_exact("nl-second-line-9m"));
|
||||
assert_true(prof_output_exact("nl-third-line-2p"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: characters that could break storage format (backslash, pipe,
|
||||
* percent, braces, brackets, equals) survive DB round-trip.
|
||||
*/
|
||||
void
|
||||
message_db_history_service_chars(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='svc1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>svc: a\\b c|d e%f {g} [h] i=j</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
assert_true(prof_output_exact("svc: a\\b c|d e%f {g} [h] i=j"));
|
||||
}
|
||||
|
||||
#ifdef HAVE_HISTORY_VERIFY
|
||||
/*
|
||||
* Test: /history verify reports no issues after normal message writes.
|
||||
*/
|
||||
void
|
||||
message_db_history_verify(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Write a few messages to create non-trivial DB state */
|
||||
stbbr_send(
|
||||
"<message id='ver1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>verify-msg-one</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='ver2' to='stabber@localhost' "
|
||||
"from='buddy2@localhost/phone' type='chat'>"
|
||||
"<body>verify-msg-two</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy2/phone (win 3)"));
|
||||
|
||||
/* Run verify — should find no issues */
|
||||
prof_input("/history verify");
|
||||
assert_true(prof_output_exact("Verification complete: no issues found."));
|
||||
}
|
||||
#endif /* HAVE_HISTORY_VERIFY */
|
||||
|
||||
/*
|
||||
* Test: LMC (Last Message Correction, XEP-0308) — incoming correction
|
||||
* replaces the original in DB history.
|
||||
*
|
||||
* Both messages arrive while focused on the console so their bodies
|
||||
* are never rendered to the terminal. After reopen, only the corrected
|
||||
* text should appear — the original should be absent.
|
||||
*/
|
||||
void
|
||||
message_db_history_lmc(void **state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
/* Original message (body never displayed — received on console) */
|
||||
stbbr_send(
|
||||
"<message id='lmc-orig-1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>lmc-before-correction-aaa</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
/* Correction replacing the original (XEP-0308).
|
||||
* Use buddy2 message as sync barrier to ensure correction is processed. */
|
||||
stbbr_send(
|
||||
"<message id='lmc-corr-1' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>lmc-after-correction-zzz</body>"
|
||||
"<replace id='lmc-orig-1' xmlns='urn:xmpp:message-correct:0'/>"
|
||||
"</message>");
|
||||
|
||||
/* Sync barrier: send from buddy2, wait for its notification */
|
||||
stbbr_send(
|
||||
"<message id='lmc-sync' to='stabber@localhost' "
|
||||
"from='buddy2@localhost/phone' type='chat'>"
|
||||
"<body>lmc-sync-barrier</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy2/phone (win 3)"));
|
||||
|
||||
/* Close buddy1 and buddy2 windows */
|
||||
prof_input("/close all");
|
||||
assert_true(prof_output_exact("Closed 2 windows."));
|
||||
|
||||
/* Reopen buddy1 — should have corrected text only */
|
||||
prof_input("/msg buddy1@localhost");
|
||||
assert_true(prof_output_exact("lmc-after-correction-zzz"));
|
||||
|
||||
/* Original body was never in the terminal buffer (received on console).
|
||||
* Its absence in history proves the correction was applied in the DB. */
|
||||
prof_timeout(3);
|
||||
assert_false(prof_output_exact("lmc-before-correction-aaa"));
|
||||
prof_timeout_reset();
|
||||
}
|
||||
14
tests/functionaltests/test_history.h
Normal file
14
tests/functionaltests/test_history.h
Normal file
@@ -0,0 +1,14 @@
|
||||
void message_db_history_on_reopen(void **state);
|
||||
void message_db_history_multiple(void **state);
|
||||
void message_db_history_contact_isolation(void **state);
|
||||
void message_db_history_special_chars(void **state);
|
||||
void message_db_history_outgoing(void **state);
|
||||
void message_db_history_dialog(void **state);
|
||||
void message_db_history_empty(void **state);
|
||||
void message_db_history_long_message(void **state);
|
||||
void message_db_history_newline(void **state);
|
||||
void message_db_history_service_chars(void **state);
|
||||
#ifdef HAVE_HISTORY_VERIFY
|
||||
void message_db_history_verify(void **state);
|
||||
#endif
|
||||
void message_db_history_lmc(void **state);
|
||||
@@ -23,7 +23,7 @@ responds_to_last_activity_request(void **state)
|
||||
"</iq>"
|
||||
);
|
||||
|
||||
// Verify profanity responds with last activity info
|
||||
// Verify that CProof responds with last activity info
|
||||
// The 'seconds' attribute indicates idle time
|
||||
assert_true(stbbr_received(
|
||||
"<iq id='last1' type='result' to='buddy1@localhost/mobile'>"
|
||||
|
||||
@@ -1,441 +0,0 @@
|
||||
/*
|
||||
* test_autoping.c
|
||||
* Unit tests for autoping functionality (XEP-0199)
|
||||
*
|
||||
* Tests the autoping logic including:
|
||||
* - Timer state management
|
||||
* - Timeout checking
|
||||
* - Connection state validation
|
||||
*/
|
||||
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <glib.h>
|
||||
|
||||
/*
|
||||
* Since autoping functions use static variables and are tightly coupled
|
||||
* to the connection and xmpp libraries, we test the logic patterns here
|
||||
* rather than calling the actual functions.
|
||||
*
|
||||
* These tests validate the correctness of the autoping algorithm:
|
||||
* - State machine for ping wait
|
||||
* - Timeout calculation
|
||||
* - Connection state checks
|
||||
*/
|
||||
|
||||
/* Connection status enum (isolated from xmpp.h to avoid conflicts) */
|
||||
typedef enum {
|
||||
CONN_DISCONNECTED,
|
||||
CONN_CONNECTED,
|
||||
CONN_CONNECTING
|
||||
} ConnStatus;
|
||||
|
||||
/* Test structure simulating autoping state */
|
||||
typedef struct {
|
||||
gboolean wait;
|
||||
GTimer* timer;
|
||||
int timeout_seconds;
|
||||
ConnStatus conn_status;
|
||||
} AutopingState;
|
||||
|
||||
static AutopingState*
|
||||
autoping_state_new(void)
|
||||
{
|
||||
AutopingState* state = calloc(1, sizeof(AutopingState));
|
||||
state->wait = FALSE;
|
||||
state->timer = NULL;
|
||||
state->timeout_seconds = 30;
|
||||
state->conn_status = CONN_DISCONNECTED;
|
||||
return state;
|
||||
}
|
||||
|
||||
static void
|
||||
autoping_state_free(AutopingState* state)
|
||||
{
|
||||
if (state) {
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
}
|
||||
free(state);
|
||||
}
|
||||
}
|
||||
|
||||
/* Simulates iq_autoping_timer_cancel logic */
|
||||
static void
|
||||
autoping_cancel(AutopingState* state)
|
||||
{
|
||||
state->wait = FALSE;
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
state->timer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* Simulates ping send logic */
|
||||
static gboolean
|
||||
autoping_send(AutopingState* state)
|
||||
{
|
||||
if (state->conn_status != CONN_CONNECTED) {
|
||||
return FALSE;
|
||||
}
|
||||
if (state->wait) {
|
||||
/* Already waiting for pong */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
state->wait = TRUE;
|
||||
if (state->timer) {
|
||||
g_timer_destroy(state->timer);
|
||||
}
|
||||
state->timer = g_timer_new();
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/* Simulates iq_autoping_check logic */
|
||||
typedef enum {
|
||||
AUTOPING_OK,
|
||||
AUTOPING_NOT_CONNECTED,
|
||||
AUTOPING_NOT_WAITING,
|
||||
AUTOPING_NO_TIMER,
|
||||
AUTOPING_TIMEOUT
|
||||
} AutopingCheckResult;
|
||||
|
||||
static AutopingCheckResult
|
||||
autoping_check(AutopingState* state)
|
||||
{
|
||||
if (state->conn_status != CONN_CONNECTED) {
|
||||
return AUTOPING_NOT_CONNECTED;
|
||||
}
|
||||
if (state->wait == FALSE) {
|
||||
return AUTOPING_NOT_WAITING;
|
||||
}
|
||||
if (state->timer == NULL) {
|
||||
return AUTOPING_NO_TIMER;
|
||||
}
|
||||
|
||||
gdouble elapsed = g_timer_elapsed(state->timer, NULL);
|
||||
unsigned long seconds_elapsed = (unsigned long)(elapsed * 1.0);
|
||||
|
||||
if (state->timeout_seconds > 0 && seconds_elapsed >= (unsigned long)state->timeout_seconds) {
|
||||
return AUTOPING_TIMEOUT;
|
||||
}
|
||||
|
||||
return AUTOPING_OK;
|
||||
}
|
||||
|
||||
/* Simulates timer extend logic (reset timer on activity) */
|
||||
static void
|
||||
autoping_extend(AutopingState* state)
|
||||
{
|
||||
if (state->timer) {
|
||||
g_timer_start(state->timer);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_cancel resets state correctly
|
||||
*/
|
||||
void
|
||||
test_autoping_cancel_resets_state(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* Setup: send a ping */
|
||||
autoping_send(as);
|
||||
assert_true(as->wait);
|
||||
assert_non_null(as->timer);
|
||||
|
||||
/* Cancel should reset everything */
|
||||
autoping_cancel(as);
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send fails when disconnected
|
||||
*/
|
||||
void
|
||||
test_autoping_send_fails_when_disconnected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
|
||||
gboolean result = autoping_send(as);
|
||||
|
||||
assert_false(result);
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send succeeds when connected
|
||||
*/
|
||||
void
|
||||
test_autoping_send_succeeds_when_connected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
gboolean result = autoping_send(as);
|
||||
|
||||
assert_true(result);
|
||||
assert_true(as->wait);
|
||||
assert_non_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_send returns false if already waiting
|
||||
*/
|
||||
void
|
||||
test_autoping_send_fails_if_already_waiting(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* First send succeeds */
|
||||
gboolean result1 = autoping_send(as);
|
||||
assert_true(result1);
|
||||
|
||||
/* Second send fails - already waiting */
|
||||
gboolean result2 = autoping_send(as);
|
||||
assert_false(result2);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NOT_CONNECTED when disconnected
|
||||
*/
|
||||
void
|
||||
test_autoping_check_not_connected(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NOT_CONNECTED);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NOT_WAITING when not waiting for pong
|
||||
*/
|
||||
void
|
||||
test_autoping_check_not_waiting(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->wait = FALSE;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NOT_WAITING);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns NO_TIMER when timer is null
|
||||
*/
|
||||
void
|
||||
test_autoping_check_no_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->wait = TRUE;
|
||||
as->timer = NULL;
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_NO_TIMER);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns OK when within timeout
|
||||
*/
|
||||
void
|
||||
test_autoping_check_ok_within_timeout(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 30;
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Immediately after send, should be OK */
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_OK);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: autoping_check returns OK when timeout is 0 (disabled)
|
||||
*/
|
||||
void
|
||||
test_autoping_check_ok_when_timeout_disabled(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 0; /* Disabled */
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
|
||||
assert_int_equal(result, AUTOPING_OK);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timer extend resets the timer
|
||||
*/
|
||||
void
|
||||
test_autoping_timer_extend_resets_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Wait a tiny bit */
|
||||
g_usleep(10000); /* 10ms */
|
||||
|
||||
gdouble elapsed_before = g_timer_elapsed(as->timer, NULL);
|
||||
|
||||
/* Extend should reset timer */
|
||||
autoping_extend(as);
|
||||
|
||||
gdouble elapsed_after = g_timer_elapsed(as->timer, NULL);
|
||||
|
||||
/* After extend, elapsed time should be very small */
|
||||
assert_true(elapsed_after < elapsed_before);
|
||||
assert_true(elapsed_after < 0.005); /* Less than 5ms */
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timer extend does nothing when timer is null
|
||||
*/
|
||||
void
|
||||
test_autoping_timer_extend_noop_without_timer(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->timer = NULL;
|
||||
|
||||
/* Should not crash */
|
||||
autoping_extend(as);
|
||||
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: pong received cancels wait state
|
||||
*/
|
||||
void
|
||||
test_autoping_pong_received_cancels_wait(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* Send ping */
|
||||
autoping_send(as);
|
||||
assert_true(as->wait);
|
||||
|
||||
/* Simulate pong received - calls cancel */
|
||||
autoping_cancel(as);
|
||||
|
||||
assert_false(as->wait);
|
||||
assert_null(as->timer);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: multiple pings can be sent after pong received
|
||||
*/
|
||||
void
|
||||
test_autoping_can_send_after_pong(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
|
||||
/* First ping */
|
||||
gboolean result1 = autoping_send(as);
|
||||
assert_true(result1);
|
||||
|
||||
/* Pong received */
|
||||
autoping_cancel(as);
|
||||
|
||||
/* Second ping should succeed */
|
||||
gboolean result2 = autoping_send(as);
|
||||
assert_true(result2);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: connection status changes affect behavior
|
||||
*/
|
||||
void
|
||||
test_autoping_respects_connection_state_changes(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
|
||||
/* Start disconnected */
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
assert_false(autoping_send(as));
|
||||
|
||||
/* Connect */
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
assert_true(autoping_send(as));
|
||||
|
||||
/* Disconnect - check should handle gracefully */
|
||||
as->conn_status = CONN_DISCONNECTED;
|
||||
AutopingCheckResult result = autoping_check(as);
|
||||
assert_int_equal(result, AUTOPING_NOT_CONNECTED);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: timeout with very short timeout value
|
||||
*/
|
||||
void
|
||||
test_autoping_timeout_detection(void** state)
|
||||
{
|
||||
AutopingState* as = autoping_state_new();
|
||||
as->conn_status = CONN_CONNECTED;
|
||||
as->timeout_seconds = 1; /* 1 second timeout */
|
||||
|
||||
autoping_send(as);
|
||||
|
||||
/* Immediately should be OK */
|
||||
AutopingCheckResult result1 = autoping_check(as);
|
||||
assert_int_equal(result1, AUTOPING_OK);
|
||||
|
||||
/* Manipulate timer to simulate timeout */
|
||||
/* We can't easily wait 1 second in a unit test, so we test the logic */
|
||||
/* by checking that elapsed time is calculated correctly */
|
||||
gdouble elapsed = g_timer_elapsed(as->timer, NULL);
|
||||
assert_true(elapsed < 1.0);
|
||||
|
||||
autoping_state_free(as);
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* test_autoping.h
|
||||
* Header for autoping unit tests
|
||||
*/
|
||||
|
||||
void test_autoping_cancel_resets_state(void** state);
|
||||
void test_autoping_send_fails_when_disconnected(void** state);
|
||||
void test_autoping_send_succeeds_when_connected(void** state);
|
||||
void test_autoping_send_fails_if_already_waiting(void** state);
|
||||
void test_autoping_check_not_connected(void** state);
|
||||
void test_autoping_check_not_waiting(void** state);
|
||||
void test_autoping_check_no_timer(void** state);
|
||||
void test_autoping_check_ok_within_timeout(void** state);
|
||||
void test_autoping_check_ok_when_timeout_disabled(void** state);
|
||||
void test_autoping_timer_extend_resets_timer(void** state);
|
||||
void test_autoping_timer_extend_noop_without_timer(void** state);
|
||||
void test_autoping_pong_received_cancels_wait(void** state);
|
||||
void test_autoping_can_send_after_pong(void** state);
|
||||
void test_autoping_respects_connection_state_changes(void** state);
|
||||
void test_autoping_timeout_detection(void** state);
|
||||
@@ -1,261 +0,0 @@
|
||||
/*
|
||||
* test_helpers.c
|
||||
* Unit tests for helper functions added in refactoring
|
||||
*/
|
||||
|
||||
#include "prof_cmocka.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
|
||||
/*
|
||||
* Tests for _check_subwin_width logic (from src/ui/window.c)
|
||||
* Since _check_subwin_width is static, we test the logic here
|
||||
*/
|
||||
|
||||
static int
|
||||
check_subwin_width(int cols, int width)
|
||||
{
|
||||
if (cols > 1) {
|
||||
if (width < 1)
|
||||
width = 1;
|
||||
if (width >= cols)
|
||||
width = cols - 1;
|
||||
} else {
|
||||
width = 1;
|
||||
}
|
||||
return width;
|
||||
}
|
||||
|
||||
/* Test: width is clamped to minimum 1 when cols > 1 */
|
||||
void
|
||||
test_subwin_width_clamps_to_min(void** state)
|
||||
{
|
||||
assert_int_equal(check_subwin_width(80, 0), 1);
|
||||
assert_int_equal(check_subwin_width(80, -5), 1);
|
||||
assert_int_equal(check_subwin_width(100, -100), 1);
|
||||
}
|
||||
|
||||
/* Test: width is clamped to cols-1 when >= cols */
|
||||
void
|
||||
test_subwin_width_clamps_to_max(void** state)
|
||||
{
|
||||
assert_int_equal(check_subwin_width(80, 80), 79);
|
||||
assert_int_equal(check_subwin_width(80, 100), 79);
|
||||
assert_int_equal(check_subwin_width(80, 1000), 79);
|
||||
}
|
||||
|
||||
/* Test: valid width passes through unchanged */
|
||||
void
|
||||
test_subwin_width_valid_unchanged(void** state)
|
||||
{
|
||||
assert_int_equal(check_subwin_width(80, 20), 20);
|
||||
assert_int_equal(check_subwin_width(80, 1), 1);
|
||||
assert_int_equal(check_subwin_width(80, 79), 79);
|
||||
assert_int_equal(check_subwin_width(100, 50), 50);
|
||||
}
|
||||
|
||||
/* Test: edge case when cols <= 1 */
|
||||
void
|
||||
test_subwin_width_small_cols(void** state)
|
||||
{
|
||||
assert_int_equal(check_subwin_width(1, 50), 1);
|
||||
assert_int_equal(check_subwin_width(0, 50), 1);
|
||||
assert_int_equal(check_subwin_width(-1, 50), 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests for hash table iteration pattern
|
||||
* Verifies that g_hash_table_iter produces same results as get_keys+list
|
||||
*/
|
||||
|
||||
void
|
||||
test_hash_table_iter_finds_all_keys(void** state)
|
||||
{
|
||||
GHashTable* ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||
|
||||
g_hash_table_insert(ht, g_strdup("key1"), g_strdup("value1"));
|
||||
g_hash_table_insert(ht, g_strdup("key2"), g_strdup("value2"));
|
||||
g_hash_table_insert(ht, g_strdup("key3"), g_strdup("value3"));
|
||||
|
||||
GHashTableIter iter;
|
||||
gpointer key, value;
|
||||
int count = 0;
|
||||
gboolean found_key1 = FALSE, found_key2 = FALSE, found_key3 = FALSE;
|
||||
|
||||
g_hash_table_iter_init(&iter, ht);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
count++;
|
||||
if (strcmp((char*)key, "key1") == 0) found_key1 = TRUE;
|
||||
if (strcmp((char*)key, "key2") == 0) found_key2 = TRUE;
|
||||
if (strcmp((char*)key, "key3") == 0) found_key3 = TRUE;
|
||||
}
|
||||
|
||||
assert_int_equal(count, 3);
|
||||
assert_true(found_key1);
|
||||
assert_true(found_key2);
|
||||
assert_true(found_key3);
|
||||
|
||||
g_hash_table_destroy(ht);
|
||||
}
|
||||
|
||||
void
|
||||
test_hash_table_iter_empty_table(void** state)
|
||||
{
|
||||
GHashTable* ht = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
|
||||
GHashTableIter iter;
|
||||
gpointer key, value;
|
||||
int count = 0;
|
||||
|
||||
g_hash_table_iter_init(&iter, ht);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
count++;
|
||||
}
|
||||
|
||||
assert_int_equal(count, 0);
|
||||
|
||||
g_hash_table_destroy(ht);
|
||||
}
|
||||
|
||||
void
|
||||
test_hash_table_iter_early_exit(void** state)
|
||||
{
|
||||
GHashTable* ht = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
|
||||
|
||||
g_hash_table_insert(ht, g_strdup("target"), g_strdup("found"));
|
||||
g_hash_table_insert(ht, g_strdup("other1"), g_strdup("skip"));
|
||||
g_hash_table_insert(ht, g_strdup("other2"), g_strdup("skip"));
|
||||
|
||||
GHashTableIter iter;
|
||||
gpointer key, value;
|
||||
const char* result = NULL;
|
||||
|
||||
g_hash_table_iter_init(&iter, ht);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
if (strcmp((char*)value, "found") == 0) {
|
||||
result = (const char*)key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert_non_null(result);
|
||||
assert_string_equal(result, "target");
|
||||
|
||||
g_hash_table_destroy(ht);
|
||||
}
|
||||
|
||||
/*
|
||||
* Tests for nested hash table lookup (like features_by_jid)
|
||||
*/
|
||||
void
|
||||
test_nested_hash_table_feature_lookup(void** state)
|
||||
{
|
||||
// Simulate conn.features_by_jid structure
|
||||
GHashTable* features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal,
|
||||
g_free, (GDestroyNotify)g_hash_table_destroy);
|
||||
|
||||
// Add jid1 with features
|
||||
GHashTable* jid1_features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
g_hash_table_insert(jid1_features, g_strdup("feature_a"), GINT_TO_POINTER(1));
|
||||
g_hash_table_insert(jid1_features, g_strdup("feature_b"), GINT_TO_POINTER(1));
|
||||
g_hash_table_insert(features_by_jid, g_strdup("jid1@server"), jid1_features);
|
||||
|
||||
// Add jid2 with different features
|
||||
GHashTable* jid2_features = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
g_hash_table_insert(jid2_features, g_strdup("feature_c"), GINT_TO_POINTER(1));
|
||||
g_hash_table_insert(features_by_jid, g_strdup("jid2@server"), jid2_features);
|
||||
|
||||
// Test: find feature_a (should be in jid1)
|
||||
GHashTableIter iter;
|
||||
gpointer key, value;
|
||||
gboolean found_a = FALSE;
|
||||
|
||||
g_hash_table_iter_init(&iter, features_by_jid);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
GHashTable* features = (GHashTable*)value;
|
||||
if (features && g_hash_table_lookup(features, "feature_a")) {
|
||||
found_a = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_true(found_a);
|
||||
|
||||
// Test: find feature_c (should be in jid2)
|
||||
gboolean found_c = FALSE;
|
||||
g_hash_table_iter_init(&iter, features_by_jid);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
GHashTable* features = (GHashTable*)value;
|
||||
if (features && g_hash_table_lookup(features, "feature_c")) {
|
||||
found_c = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_true(found_c);
|
||||
|
||||
// Test: feature_x not found
|
||||
gboolean found_x = FALSE;
|
||||
g_hash_table_iter_init(&iter, features_by_jid);
|
||||
while (g_hash_table_iter_next(&iter, &key, &value)) {
|
||||
GHashTable* features = (GHashTable*)value;
|
||||
if (features && g_hash_table_lookup(features, "feature_x")) {
|
||||
found_x = TRUE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_false(found_x);
|
||||
|
||||
g_hash_table_destroy(features_by_jid);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test for calloc array loop fix (connection.c queued_messages bug)
|
||||
* The bug was: for (n = 0; n < len && array[n]; ++n)
|
||||
* After calloc, array[n] is always NULL, so loop exits immediately
|
||||
*/
|
||||
void
|
||||
test_calloc_array_iteration_bug(void** state)
|
||||
{
|
||||
int len = 5;
|
||||
|
||||
// Simulate the buggy pattern
|
||||
char** array = calloc(len + 1, sizeof(char*));
|
||||
int buggy_count = 0;
|
||||
for (int n = 0; n < len && array[n]; ++n) {
|
||||
buggy_count++;
|
||||
}
|
||||
// Bug: loop never executes because calloc zeros everything
|
||||
assert_int_equal(buggy_count, 0);
|
||||
|
||||
// Correct pattern
|
||||
int correct_count = 0;
|
||||
for (int n = 0; n < len; ++n) {
|
||||
correct_count++;
|
||||
// In real code, we'd store values here: array[n] = get_value();
|
||||
}
|
||||
assert_int_equal(correct_count, 5);
|
||||
|
||||
free(array);
|
||||
}
|
||||
|
||||
/*
|
||||
* Test for format string safety
|
||||
* Verifies that using "%s" format prevents interpretation of % in strings
|
||||
*/
|
||||
void
|
||||
test_format_string_with_percent(void** state)
|
||||
{
|
||||
char buffer[256];
|
||||
const char* dangerous_input = "test %s %n %x string";
|
||||
|
||||
// Safe: using %s format
|
||||
int ret = snprintf(buffer, sizeof(buffer), "%s", dangerous_input);
|
||||
assert_true(ret > 0);
|
||||
assert_string_equal(buffer, dangerous_input);
|
||||
|
||||
// The string should be preserved exactly, including % characters
|
||||
assert_non_null(strstr(buffer, "%s"));
|
||||
assert_non_null(strstr(buffer, "%n"));
|
||||
assert_non_null(strstr(buffer, "%x"));
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
/*
|
||||
* test_helpers.h
|
||||
* Header for helper function tests
|
||||
*/
|
||||
|
||||
#ifndef TEST_HELPERS_H
|
||||
#define TEST_HELPERS_H
|
||||
|
||||
/* Subwindow width clamping tests */
|
||||
void test_subwin_width_clamps_to_min(void** state);
|
||||
void test_subwin_width_clamps_to_max(void** state);
|
||||
void test_subwin_width_valid_unchanged(void** state);
|
||||
void test_subwin_width_small_cols(void** state);
|
||||
|
||||
/* Hash table iteration tests */
|
||||
void test_hash_table_iter_finds_all_keys(void** state);
|
||||
void test_hash_table_iter_empty_table(void** state);
|
||||
void test_hash_table_iter_early_exit(void** state);
|
||||
void test_nested_hash_table_feature_lookup(void** state);
|
||||
|
||||
/* Bug fix verification tests */
|
||||
void test_calloc_array_iteration_bug(void** state);
|
||||
void test_format_string_with_percent(void** state);
|
||||
|
||||
#endif
|
||||
@@ -36,8 +36,6 @@
|
||||
#include "test_form.h"
|
||||
#include "test_callbacks.h"
|
||||
#include "test_plugins_disco.h"
|
||||
#include "test_helpers.h"
|
||||
#include "test_autoping.h"
|
||||
|
||||
#define muc_unit_test(f) cmocka_unit_test_setup_teardown(f, muc_before_test, muc_after_test)
|
||||
|
||||
@@ -658,35 +656,6 @@ main(int argc, char* argv[])
|
||||
cmocka_unit_test_setup_teardown(test_allow_unencrypted_message_confirms_on_second_attempt, load_preferences, close_preferences),
|
||||
cmocka_unit_test_setup_teardown(test_cmd_force_encryption_invalid_policy, load_preferences, close_preferences),
|
||||
cmocka_unit_test_setup_teardown(test_allow_unencrypted_message_invalid_mode, load_preferences, close_preferences),
|
||||
|
||||
// Helper function tests
|
||||
cmocka_unit_test(test_subwin_width_clamps_to_min),
|
||||
cmocka_unit_test(test_subwin_width_clamps_to_max),
|
||||
cmocka_unit_test(test_subwin_width_valid_unchanged),
|
||||
cmocka_unit_test(test_subwin_width_small_cols),
|
||||
cmocka_unit_test(test_hash_table_iter_finds_all_keys),
|
||||
cmocka_unit_test(test_hash_table_iter_empty_table),
|
||||
cmocka_unit_test(test_hash_table_iter_early_exit),
|
||||
cmocka_unit_test(test_nested_hash_table_feature_lookup),
|
||||
cmocka_unit_test(test_calloc_array_iteration_bug),
|
||||
cmocka_unit_test(test_format_string_with_percent),
|
||||
|
||||
// Autoping tests (XEP-0199)
|
||||
cmocka_unit_test(test_autoping_cancel_resets_state),
|
||||
cmocka_unit_test(test_autoping_send_fails_when_disconnected),
|
||||
cmocka_unit_test(test_autoping_send_succeeds_when_connected),
|
||||
cmocka_unit_test(test_autoping_send_fails_if_already_waiting),
|
||||
cmocka_unit_test(test_autoping_check_not_connected),
|
||||
cmocka_unit_test(test_autoping_check_not_waiting),
|
||||
cmocka_unit_test(test_autoping_check_no_timer),
|
||||
cmocka_unit_test(test_autoping_check_ok_within_timeout),
|
||||
cmocka_unit_test(test_autoping_check_ok_when_timeout_disabled),
|
||||
cmocka_unit_test(test_autoping_timer_extend_resets_timer),
|
||||
cmocka_unit_test(test_autoping_timer_extend_noop_without_timer),
|
||||
cmocka_unit_test(test_autoping_pong_received_cancels_wait),
|
||||
cmocka_unit_test(test_autoping_can_send_after_pong),
|
||||
cmocka_unit_test(test_autoping_respects_connection_state_changes),
|
||||
cmocka_unit_test(test_autoping_timeout_detection),
|
||||
};
|
||||
return cmocka_run_group_tests(all_tests, NULL, NULL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user