mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-20 03:26:21 +00:00
feat(history): flat-file backend with bidirectional SQLite migration
A flat-file alternative to the SQLite chatlog backend with runtime
switching, full migration tooling, integrity verification, and a
synthetic load harness. SQLite remains the default; both backends share
one dispatch layer (db_backend_t vtable) so callers don't change.
Storage layout
- Per-contact append-only `flatlog/<account>/<contact>/history.log`
under XDG_DATA_HOME, one line per message
- Single-line file header with embedded format-version marker
(FLATFILE_FORMAT_VERSION); reader warns on missing or mismatched
marker, writer and checker stay in sync via preprocessor
stringification
- Deterministic key=value metadata (`id`, `aid`, `corrects`, `to`,
`to_res`, `read`) plus escaped body \u2014 `\|`, `\]`, `\\`, `\n`, `\r`
literals prevent log injection
- Sparse byte-offset index (FF_INDEX_STEP=500) per contact for
O(log n) time-range lookups; rebuilt on inode / size / mtime
change, extended in-place when the file just grew
- Per-contact GHashTable caches for archive_id presence and
stanza_id \u2192 from_jid mapping (O(1) MAM dedup, O(1) LMC sender
validation)
Hardening
- Path-traversal protection: JID directory name normalisation
(`@` \u2192 `_at_`, slashes and `..` rejected at construction); every
per-contact path is anchored under the account's flatlog/
directory and validated before open
- Symlink-attack protection: every fopen / open uses O_NOFOLLOW; on
ELOOP the operation aborts with an error rather than following
- Filesystem permissions: log files created with mode 0600,
directories with mode 0700; both enforced at creation, verified
on each open and reported on drift by `/history verify`
- Atomic crash-safe export: write to a temp file via mkstemp (mode
0600, random suffix, no name collisions between concurrent
exports), fsync, then rename \u2014 partial state never replaces the
live file
- Concurrency: advisory flock(LOCK_EX) held for the duration of
every write, including append from live messages and full rewrite
from export, so two profanity processes can't interleave bytes
on the same log
- DoS / abuse guards:
* FF_MAX_LINE_LEN = 10 MB \u2014 lines longer than this are rejected
at read with a warning; the parser will not allocate
unbounded memory for a single record
* FF_MAX_LMC_DEPTH = 100 \u2014 `corrects:` chain walk stops at this
depth and emits a warning, preventing a malicious correction
cycle from spinning the apply pass
* FF_VERSION_SCAN_MAX = 16 \u2014 header version probe never reads
past 16 leading comment lines, even on garbage input
* Empty / inverted byte-range early-return in page-up read path
so a malformed time filter cannot cause an unbounded scan
* Zero-entry index guard so a file whose every line failed to
parse cannot cause a NULL deref on later page-up
- LMC sender validation: an incoming correction whose sender does
not match the original message's sender is rejected at write
time and surfaced via cons_show_error; a cycle in the apply pass
is broken via a visited-set
- jid_create_from_bare_and_resource treats NULL, empty string, and
the literal "(null)" as no resource and returns a bare jid;
similar normalisation for barejid eliminates the legacy
"user@host/(null)" artefact that leaked into stored fulljids
whenever g_strdup_printf("%s", NULL) ran inside create_fulljid
Commands
- `/history switch sqlite|flatfile` \u2014 runtime backend swap, closes
the old backend and opens the new one without reconnecting
- `/history export [<jid>]` \u2014 SQLite -> flat-file, merging with any
existing flatlog (dedup keyed on a SHA-256 hash mixing stanza_id,
timestamp, from_jid, body \u2014 robust against id reuse by older
clients)
- `/history import [<jid>]` \u2014 flat-file -> SQLite, same merge
semantics, runs inside a single SQLite transaction with rollback
on per-contact failure
- `/history verify [<jid>]` \u2014 integrity check; emits a structured
list of issues (ERROR / WARNING / INFO) per file:
* file-level: missing log, wrong permissions (\u2260 0600), UTF-8
BOM present, CRLF line endings, empty file
* line-level: invalid UTF-8 (with byte offset), embedded
control characters, unparsable lines, timestamps out of
order, duplicate `id:` and `aid:` (tracked separately so a
stanza/archive id collision isn't double-reported)
* cross-line: broken `corrects:` references whose target id is
not present in the file
- `/history backend` \u2014 show currently active backend
- Active backend indicator `[sqlite]` / `[flatfile]` in the status
bar next to the JID
- Roster-JID autocomplete for verify / export / import
- export and import open a SQLite handle on demand when the
flatfile backend is currently active, so migration works
regardless of which backend is live
Tests
- Unit: database_export (parser round-trip, escape/unescape, dedup
key stability, JID normalisation), database_stress (14 cases
exercising rapid writes, large messages, deep LMC chains, MAM
dedup, concurrent contacts)
- Functional: history persistence across reconnects, export /
import round-trip with content equality, MUC migration,
timestamp normalisation across timezones
- Bench harness P1\u2013P5 (synthetic load: bulk insert, time-range
read, page-up scroll, MAM ingest, mixed workload) and failure
modes F1\u2013F17 (page-up cursor and forward-iteration symmetry,
oversized lines, MAM dedup, LMC depth and cycles, BOM/CRLF,
missing log, empty file, mtime+inode flip, broken corrects, etc.)
- All bench tests integrate with the existing make targets and
emit CSV rows for baseline comparison
Author: jabber.developer2 <jabber.developer2@jabber.space>
Reviewed-by: jabber.developer <jabber.developer@jabber.space>
This commit is contained in:
@@ -14,11 +14,11 @@
|
||||
* flaky tests caused by leftover state. The overhead is acceptable since
|
||||
* functional tests run less frequently than unit tests.
|
||||
*
|
||||
* Tests are organized into groups for better maintainability and parallel execution:
|
||||
* Group 1: Connect, Ping, Autoping (fast), Rooms, Software, Last Activity
|
||||
* Group 2: Message, Receipts, Roster, Chat Session
|
||||
* Group 3: Presence, Disconnect, Autoping (slow)
|
||||
* Group 4: MUC, Carbons
|
||||
* Tests are organized into groups balanced for parallel execution:
|
||||
* Group 1: Connect, Ping, Rooms, Software, Last Activity, Autoping, Disco, Roster
|
||||
* Group 2: Export/Import (core + bidirectional), Receipts
|
||||
* Group 3: Presence, Disconnect, DB History, Message, MUC Database
|
||||
* Group 4: Chat Session, MUC, Carbons, Migration Stress
|
||||
*
|
||||
* Parallel execution:
|
||||
* ./functionaltests - run all tests sequentially
|
||||
@@ -55,14 +55,33 @@
|
||||
#include "test_lastactivity.h"
|
||||
#include "test_autoping.h"
|
||||
#include "test_disco.h"
|
||||
#include "test_history.h"
|
||||
#ifdef HAVE_SQLITE
|
||||
#include "test_export_import.h"
|
||||
#endif
|
||||
|
||||
/* 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)
|
||||
#define PROF_FUNC_TEST(test) \
|
||||
{ \
|
||||
#test, test, init_prof_test, close_prof_test, #test \
|
||||
}
|
||||
|
||||
#ifdef HAVE_SQLITE
|
||||
/* Custom init that sets a non-UTC timezone (POSIX TST-3 = UTC+3) */
|
||||
static int
|
||||
init_tz_plus3_test(void** state)
|
||||
{
|
||||
prof_test_tz = "TST-3";
|
||||
int ret = init_prof_test(state);
|
||||
prof_test_tz = "UTC";
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
int group = 0; /* 0 = all groups */
|
||||
int group = 0; /* 0 = all groups */
|
||||
if (argc > 1) {
|
||||
group = atoi(argv[1]);
|
||||
if (group < 1 || group > 4) {
|
||||
@@ -75,12 +94,12 @@ main(int argc, char* argv[])
|
||||
char group_env[16];
|
||||
snprintf(group_env, sizeof(group_env), "%d", group);
|
||||
setenv("PROF_TEST_GROUP", group_env, 1);
|
||||
|
||||
|
||||
fprintf(stderr, "[PROF_TEST] Starting functional tests, group=%d\n", group);
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 1: Connect, Ping, Rooms, Software
|
||||
* Basic XMPP session establishment and server queries
|
||||
* GROUP 1: Connect, Ping, Rooms, Software, Last Activity, Disco, Roster
|
||||
* Basic XMPP session establishment, server queries, contact mgmt
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group1_tests[] = {
|
||||
/* Connection tests - verify login, roster, bookmarks */
|
||||
@@ -121,22 +140,22 @@ main(int argc, char* argv[])
|
||||
/* Autoping slow tests - require sleep for timer triggers (~2s each) */
|
||||
PROF_FUNC_TEST(autoping_sends_ping_after_interval),
|
||||
PROF_FUNC_TEST(autoping_server_not_supporting_ping),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 2: Message, Receipts, Roster, Chat Session
|
||||
* Core messaging and contact management
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group2_tests[] = {
|
||||
/* Basic message send/receive */
|
||||
PROF_FUNC_TEST(message_send),
|
||||
PROF_FUNC_TEST(message_receive_console),
|
||||
PROF_FUNC_TEST(message_receive_chatwin),
|
||||
|
||||
/* Message receipts - XEP-0184 */
|
||||
PROF_FUNC_TEST(does_not_send_receipt_request_to_barejid),
|
||||
PROF_FUNC_TEST(send_receipt_request),
|
||||
PROF_FUNC_TEST(send_receipt_on_request),
|
||||
/* Service Discovery - XEP-0030 */
|
||||
PROF_FUNC_TEST(disco_info_shows_identity),
|
||||
PROF_FUNC_TEST(disco_info_shows_features),
|
||||
PROF_FUNC_TEST(disco_info_to_server),
|
||||
PROF_FUNC_TEST(disco_info_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_not_found),
|
||||
PROF_FUNC_TEST(disco_items_shows_items),
|
||||
PROF_FUNC_TEST(disco_items_empty_result),
|
||||
PROF_FUNC_TEST(disco_requires_connection),
|
||||
PROF_FUNC_TEST(disco_items_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_empty_result),
|
||||
PROF_FUNC_TEST(disco_info_multiple_identities),
|
||||
PROF_FUNC_TEST(disco_info_without_name),
|
||||
PROF_FUNC_TEST(disco_items_without_name),
|
||||
PROF_FUNC_TEST(disco_info_service_unavailable),
|
||||
|
||||
/* Roster management - add/remove/rename contacts */
|
||||
PROF_FUNC_TEST(sends_new_item),
|
||||
@@ -144,21 +163,48 @@ main(int argc, char* argv[])
|
||||
PROF_FUNC_TEST(sends_remove_item),
|
||||
PROF_FUNC_TEST(sends_remove_item_nick),
|
||||
PROF_FUNC_TEST(sends_nick_change),
|
||||
|
||||
/* 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),
|
||||
PROF_FUNC_TEST(sends_message_to_fulljid_when_received_from_fulljid),
|
||||
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, Disco
|
||||
* Online/away/xa/dnd/chat status management
|
||||
* GROUP 2: Export/Import (core + bidirectional), Receipts
|
||||
* Cross-backend migration, message timestamp round-trip
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group2_tests[] = {
|
||||
#ifdef HAVE_SQLITE
|
||||
/* Export/Import — cross-backend migration (SQLite ↔ flat-file) */
|
||||
PROF_FUNC_TEST(export_sqlite_to_flatfile),
|
||||
PROF_FUNC_TEST(import_flatfile_to_sqlite),
|
||||
PROF_FUNC_TEST(export_idempotent_no_duplicates),
|
||||
PROF_FUNC_TEST(export_lmc_correction_survives),
|
||||
PROF_FUNC_TEST(switch_preserves_old_backend_data),
|
||||
PROF_FUNC_TEST(export_all_contacts),
|
||||
PROF_FUNC_TEST(import_double_dedup),
|
||||
PROF_FUNC_TEST(verify_after_export),
|
||||
PROF_FUNC_TEST(switch_backends_independent_messages),
|
||||
PROF_FUNC_TEST(export_empty_contact),
|
||||
|
||||
/* Bidirectional migration — split timelines merged across backends */
|
||||
PROF_FUNC_TEST(bidirectional_merge_to_flatfile),
|
||||
PROF_FUNC_TEST(bidirectional_merge_to_sqlite),
|
||||
PROF_FUNC_TEST(migration_incremental_accumulation),
|
||||
PROF_FUNC_TEST(roundtrip_full_cycle),
|
||||
PROF_FUNC_TEST(export_timestamps_preserved),
|
||||
PROF_FUNC_TEST(import_timestamps_preserved),
|
||||
{ "export_timestamps_non_utc", export_timestamps_non_utc, init_tz_plus3_test, close_prof_test, "export_timestamps_non_utc" },
|
||||
#endif
|
||||
|
||||
/* Message receipts - XEP-0184 */
|
||||
PROF_FUNC_TEST(does_not_send_receipt_request_to_barejid),
|
||||
PROF_FUNC_TEST(send_receipt_request),
|
||||
PROF_FUNC_TEST(send_receipt_on_request),
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 3: Presence, Disconnect, DB History, Message, MUC Database
|
||||
* Status management, message persistence, MUC storage
|
||||
* ============================================================ */
|
||||
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),
|
||||
@@ -178,28 +224,48 @@ main(int argc, char* argv[])
|
||||
/* Disconnect - clean session termination */
|
||||
PROF_FUNC_TEST(disconnect_ends_session),
|
||||
|
||||
/* Service Discovery - XEP-0030 */
|
||||
PROF_FUNC_TEST(disco_info_shows_identity),
|
||||
PROF_FUNC_TEST(disco_info_shows_features),
|
||||
PROF_FUNC_TEST(disco_info_to_server),
|
||||
PROF_FUNC_TEST(disco_info_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_not_found),
|
||||
PROF_FUNC_TEST(disco_items_shows_items),
|
||||
PROF_FUNC_TEST(disco_items_empty_result),
|
||||
PROF_FUNC_TEST(disco_requires_connection),
|
||||
PROF_FUNC_TEST(disco_items_to_jid),
|
||||
PROF_FUNC_TEST(disco_info_empty_result),
|
||||
PROF_FUNC_TEST(disco_info_multiple_identities),
|
||||
PROF_FUNC_TEST(disco_info_without_name),
|
||||
PROF_FUNC_TEST(disco_items_without_name),
|
||||
PROF_FUNC_TEST(disco_info_service_unavailable),
|
||||
/* 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),
|
||||
PROF_FUNC_TEST(message_db_history_verify),
|
||||
PROF_FUNC_TEST(message_db_history_lmc),
|
||||
PROF_FUNC_TEST(message_db_history_multi_resource),
|
||||
|
||||
/* Basic message send/receive */
|
||||
PROF_FUNC_TEST(message_send),
|
||||
PROF_FUNC_TEST(message_receive_console),
|
||||
PROF_FUNC_TEST(message_receive_chatwin),
|
||||
|
||||
#ifdef HAVE_SQLITE
|
||||
/* MUC (groupchat) database — export/import of type="muc" messages */
|
||||
PROF_FUNC_TEST(muc_export_sqlite_to_flatfile),
|
||||
PROF_FUNC_TEST(muc_import_flatfile_to_sqlite),
|
||||
PROF_FUNC_TEST(muc_export_timestamps_preserved),
|
||||
PROF_FUNC_TEST(muc_export_multiple_rooms),
|
||||
#endif
|
||||
};
|
||||
|
||||
/* ============================================================
|
||||
* GROUP 4: MUC, Carbons
|
||||
* Multi-user chat and message synchronization
|
||||
* GROUP 4: Chat Session, MUC, Carbons, Migration Stress
|
||||
* Session routing, multi-user chat, message sync, stress tests
|
||||
* ============================================================ */
|
||||
const struct CMUnitTest group4_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),
|
||||
PROF_FUNC_TEST(sends_message_to_fulljid_when_received_from_fulljid),
|
||||
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),
|
||||
|
||||
/* MUC room join with various options - XEP-0045 */
|
||||
PROF_FUNC_TEST(sends_room_join),
|
||||
PROF_FUNC_TEST(sends_room_join_with_nick),
|
||||
@@ -232,18 +298,26 @@ main(int argc, char* argv[])
|
||||
PROF_FUNC_TEST(receive_carbon),
|
||||
PROF_FUNC_TEST(receive_self_carbon),
|
||||
PROF_FUNC_TEST(receive_private_carbon),
|
||||
|
||||
#ifdef HAVE_SQLITE
|
||||
/* Migration stress — pool accumulation, LMC chains, large bodies */
|
||||
PROF_FUNC_TEST(export_message_pool_dated),
|
||||
PROF_FUNC_TEST(export_lmc_pool_multiple),
|
||||
PROF_FUNC_TEST(export_large_body_survives),
|
||||
#endif
|
||||
};
|
||||
|
||||
/* Test group registry for easy extension */
|
||||
struct {
|
||||
struct
|
||||
{
|
||||
const char* name;
|
||||
const struct CMUnitTest* tests;
|
||||
size_t count;
|
||||
} groups[] = {
|
||||
{ "Group 1: Connect/Ping/Rooms/Software/Autoping", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 2: Message/Receipts/Roster/Session", group2_tests, ARRAY_SIZE(group2_tests) },
|
||||
{ "Group 3: Presence/Disconnect/Disco", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 4: MUC/Carbons", group4_tests, ARRAY_SIZE(group4_tests) },
|
||||
{ "Group 1: Connect/Ping/Disco/Roster", group1_tests, ARRAY_SIZE(group1_tests) },
|
||||
{ "Group 2: ExportImport/Receipts", group2_tests, ARRAY_SIZE(group2_tests) },
|
||||
{ "Group 3: Presence/Disconnect/DBHistory/Message/MUC-DB", group3_tests, ARRAY_SIZE(group3_tests) },
|
||||
{ "Group 4: Session/MUC/Carbons/MigrationStress", group4_tests, ARRAY_SIZE(group4_tests) },
|
||||
};
|
||||
const int num_groups = ARRAY_SIZE(groups);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <sys/select.h>
|
||||
#include <regex.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <stabber.h>
|
||||
|
||||
@@ -25,34 +26,41 @@
|
||||
#define PORTS_PER_GROUP 50
|
||||
|
||||
/* Base port and fallback scan range for stabber */
|
||||
#define BASE_PORT 5230
|
||||
#define FALLBACK_PORT_RANGE (TEST_GROUPS * PORTS_PER_GROUP)
|
||||
#define BASE_PORT 5230
|
||||
#define FALLBACK_PORT_RANGE (TEST_GROUPS * PORTS_PER_GROUP)
|
||||
|
||||
/* Shutdown polling: interval and maximum attempts before escalating signals */
|
||||
#define MS_TO_US 1000
|
||||
#define SHUTDOWN_POLL_MS 50
|
||||
#define SHUTDOWN_POLL_MAX 100 /* 100 x 50ms = 5s */
|
||||
#define SIGTERM_POLL_MAX 20 /* 20 x 50ms = 1s */
|
||||
#define QUIT_GRACE_MS 100 /* grace for /quit to be read from pty */
|
||||
#define INIT_WAIT_MS 50 /* wait for child process to initialize */
|
||||
#define INPUT_DELAY_MS 10 /* let profanity process input */
|
||||
#define OUTPUT_POLL_MS 50 /* polling interval for output checks */
|
||||
#define READ_TIMEOUT_MS 100 /* select() timeout for pty reads */
|
||||
#define MS_TO_US 1000
|
||||
#define SHUTDOWN_POLL_MS 50
|
||||
#define SHUTDOWN_POLL_MAX 100 /* 100 x 50ms = 5s */
|
||||
#define SIGTERM_POLL_MAX 20 /* 20 x 50ms = 1s */
|
||||
#define QUIT_GRACE_MS 100 /* grace for /quit to be read from pty */
|
||||
#define INIT_WAIT_MS 50 /* wait for child process to initialize */
|
||||
#define INPUT_DELAY_MS 10 /* let profanity process input */
|
||||
#define OUTPUT_POLL_MS 50 /* polling interval for output checks */
|
||||
#define READ_TIMEOUT_MS 100 /* select() timeout for pty reads */
|
||||
|
||||
/* Expect timeouts (seconds) */
|
||||
#define EXPECT_TIMEOUT_DEFAULT 30
|
||||
#define EXPECT_TIMEOUT_CONNECT 60
|
||||
#define EXPECT_TIMEOUT_DEFAULT 30
|
||||
#define EXPECT_TIMEOUT_CONNECT 60
|
||||
|
||||
/* Test duration thresholds (seconds) */
|
||||
#define SLOW_TEST_THRESHOLD_S 20
|
||||
#define FAST_TEST_THRESHOLD_S 0.3
|
||||
|
||||
/* Sentinel "infinity" for min-elapsed tracking (any real test is faster) */
|
||||
#define MIN_ELAPSED_INIT 1e9
|
||||
|
||||
/* Terminal dimensions for forkpty */
|
||||
#define PTY_ROWS 24
|
||||
#define PTY_COLS 300
|
||||
#define PTY_ROWS 24
|
||||
#define PTY_COLS 300
|
||||
|
||||
/* Preprocessor stringification (for setenv from numeric #define) */
|
||||
#define STRINGIFY_(x) #x
|
||||
#define STRINGIFY(x) STRINGIFY_(x)
|
||||
|
||||
char *config_orig;
|
||||
char *data_orig;
|
||||
char* config_orig;
|
||||
char* data_orig;
|
||||
|
||||
int fd = 0;
|
||||
int stub_port = BASE_PORT;
|
||||
@@ -77,8 +85,21 @@ static size_t output_len = 0;
|
||||
/* Timeout for expect operations in seconds */
|
||||
static int expect_timeout = EXPECT_TIMEOUT_DEFAULT;
|
||||
|
||||
/* Per-test wall-clock timer */
|
||||
static struct timespec test_start_ts;
|
||||
static const char* current_test_name;
|
||||
static double min_elapsed = MIN_ELAPSED_INIT;
|
||||
static const char* min_test_name = "(none)";
|
||||
|
||||
/* Per-test threshold overrides (0 = use default) */
|
||||
double prof_test_slow_threshold = 0;
|
||||
double prof_test_fast_threshold = 0;
|
||||
|
||||
/* Timezone for profanity child process (see proftest.h) */
|
||||
const char* prof_test_tz = "UTC";
|
||||
|
||||
gboolean
|
||||
_create_dir(const char *name)
|
||||
_create_dir(const char* name)
|
||||
{
|
||||
struct stat sb;
|
||||
|
||||
@@ -96,14 +117,14 @@ _create_dir(const char *name)
|
||||
}
|
||||
|
||||
gboolean
|
||||
_mkdir_recursive(const char *dir)
|
||||
_mkdir_recursive(const char* dir)
|
||||
{
|
||||
int i;
|
||||
gboolean result = TRUE;
|
||||
|
||||
for (i = 1; i <= strlen(dir); i++) {
|
||||
if (dir[i] == '/' || dir[i] == '\0') {
|
||||
gchar *next_dir = g_strndup(dir, i);
|
||||
gchar* next_dir = g_strndup(dir, i);
|
||||
result = _create_dir(next_dir);
|
||||
g_free(next_dir);
|
||||
if (!result) {
|
||||
@@ -118,7 +139,7 @@ _mkdir_recursive(const char *dir)
|
||||
void
|
||||
_create_config_dir(void)
|
||||
{
|
||||
GString *profanity_dir = g_string_new(xdg_config_home);
|
||||
GString* profanity_dir = g_string_new(xdg_config_home);
|
||||
g_string_append(profanity_dir, "/profanity");
|
||||
|
||||
if (!_mkdir_recursive(profanity_dir->str)) {
|
||||
@@ -131,7 +152,7 @@ _create_config_dir(void)
|
||||
void
|
||||
_create_data_dir(void)
|
||||
{
|
||||
GString *profanity_dir = g_string_new(xdg_data_home);
|
||||
GString* profanity_dir = g_string_new(xdg_data_home);
|
||||
g_string_append(profanity_dir, "/profanity");
|
||||
|
||||
if (!_mkdir_recursive(profanity_dir->str)) {
|
||||
@@ -144,7 +165,7 @@ _create_data_dir(void)
|
||||
void
|
||||
_create_chatlogs_dir(void)
|
||||
{
|
||||
GString *chatlogs_dir = g_string_new(xdg_data_home);
|
||||
GString* chatlogs_dir = g_string_new(xdg_data_home);
|
||||
g_string_append(chatlogs_dir, "/profanity/chatlogs");
|
||||
|
||||
if (!_mkdir_recursive(chatlogs_dir->str)) {
|
||||
@@ -157,7 +178,7 @@ _create_chatlogs_dir(void)
|
||||
void
|
||||
_create_logs_dir(void)
|
||||
{
|
||||
GString *logs_dir = g_string_new(xdg_data_home);
|
||||
GString* logs_dir = g_string_new(xdg_data_home);
|
||||
g_string_append(logs_dir, "/profanity/logs");
|
||||
|
||||
if (!_mkdir_recursive(logs_dir->str)) {
|
||||
@@ -170,12 +191,12 @@ _create_logs_dir(void)
|
||||
void
|
||||
_cleanup_dirs(void)
|
||||
{
|
||||
const char *group_env = getenv("PROF_TEST_GROUP");
|
||||
const char* group_env = getenv("PROF_TEST_GROUP");
|
||||
int group = group_env ? atoi(group_env) : 0;
|
||||
int dir_id = (group >= 1 && group <= TEST_GROUPS) ? group : stub_port;
|
||||
|
||||
|
||||
printf("[PROF_TEST] Cleaning up directories for group %d (dir_id %d)\n", group, dir_id);
|
||||
|
||||
|
||||
char cmd[512];
|
||||
snprintf(cmd, sizeof(cmd), "rm -rf ./test-files/%d", dir_id);
|
||||
int res = system(cmd);
|
||||
@@ -199,26 +220,26 @@ _read_output(int timeout_ms)
|
||||
{
|
||||
fd_set readfds;
|
||||
struct timeval tv;
|
||||
|
||||
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(fd, &readfds);
|
||||
|
||||
|
||||
tv.tv_sec = timeout_ms / MS_TO_US;
|
||||
tv.tv_usec = (timeout_ms % MS_TO_US) * MS_TO_US;
|
||||
|
||||
|
||||
int ret = select(fd + 1, &readfds, NULL, NULL, &tv);
|
||||
if (ret <= 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
size_t space = OUTPUT_BUF_SIZE - output_len - 1;
|
||||
if (space <= 0) {
|
||||
/* Buffer full, shift content */
|
||||
memmove(output_buffer, output_buffer + OUTPUT_BUF_SIZE/2, OUTPUT_BUF_SIZE/2);
|
||||
output_len = OUTPUT_BUF_SIZE/2;
|
||||
memmove(output_buffer, output_buffer + OUTPUT_BUF_SIZE / 2, OUTPUT_BUF_SIZE / 2);
|
||||
output_len = OUTPUT_BUF_SIZE / 2;
|
||||
space = OUTPUT_BUF_SIZE - output_len - 1;
|
||||
}
|
||||
|
||||
|
||||
ssize_t n = read(fd, output_buffer + output_len, space);
|
||||
if (n > 0) {
|
||||
output_len += n;
|
||||
@@ -239,50 +260,60 @@ prof_start(void)
|
||||
ws.ws_col = PTY_COLS;
|
||||
ws.ws_xpixel = 0;
|
||||
ws.ws_ypixel = 0;
|
||||
|
||||
|
||||
/* Reset output buffer */
|
||||
output_len = 0;
|
||||
output_buffer[0] = '\0';
|
||||
|
||||
|
||||
/* Force UTC so delay-stamp timestamps render deterministically
|
||||
* regardless of the host timezone (needed for functional tests
|
||||
* that verify rendered timestamp strings). */
|
||||
setenv("TZ", prof_test_tz, 1);
|
||||
tzset();
|
||||
|
||||
child_pid = forkpty(&fd, NULL, NULL, &ws);
|
||||
|
||||
|
||||
if (child_pid < 0) {
|
||||
fd = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (child_pid == 0) {
|
||||
/* Child process */
|
||||
setenv("COLUMNS", STRINGIFY(PTY_COLS), 1);
|
||||
setenv("TERM", "xterm", 1);
|
||||
|
||||
|
||||
execl("./profanity", "./profanity", "-l", "DEBUG", NULL);
|
||||
|
||||
|
||||
/* If exec fails */
|
||||
fprintf(stderr, "execl failed: %s\n", strerror(errno));
|
||||
_exit(127);
|
||||
}
|
||||
|
||||
|
||||
/* Parent process */
|
||||
/* Set non-blocking mode for reading */
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
|
||||
/* Brief wait for process to initialize */
|
||||
sleep_ms(INIT_WAIT_MS);
|
||||
}
|
||||
|
||||
int
|
||||
init_prof_test(void **state)
|
||||
init_prof_test(void** state)
|
||||
{
|
||||
clock_gettime(CLOCK_MONOTONIC, &test_start_ts);
|
||||
current_test_name = state && *state ? (const char*)*state : "unknown";
|
||||
prof_test_slow_threshold = 0;
|
||||
prof_test_fast_threshold = 0;
|
||||
/* Get test group from environment for static resource allocation */
|
||||
const char *group_env = getenv("PROF_TEST_GROUP");
|
||||
const char* group_env = getenv("PROF_TEST_GROUP");
|
||||
int group = group_env ? atoi(group_env) : 0;
|
||||
|
||||
|
||||
/* Get build index for port offset (for parallel CI builds) */
|
||||
const char *build_env = getenv("PROF_BUILD_INDEX");
|
||||
const char* build_env = getenv("PROF_BUILD_INDEX");
|
||||
int build_idx = build_env ? atoi(build_env) : 0;
|
||||
|
||||
|
||||
/* Calculate port base: each build uses a different range of
|
||||
* TEST_GROUPS * PORTS_PER_GROUP ports.
|
||||
* Build 0 (local/default): 5230-5429, Full: 5230-5429, Minimal: 5430-5629, etc.
|
||||
@@ -290,7 +321,7 @@ init_prof_test(void **state)
|
||||
* or sequential run (no parallel builds), while Full/Minimal/NoEncrypt/Default
|
||||
* are used in CI where they run in parallel. */
|
||||
int port_base = BASE_PORT + ((build_idx > 0 ? build_idx - 1 : 0) * TEST_GROUPS * PORTS_PER_GROUP);
|
||||
|
||||
|
||||
/* Each group gets a dedicated range of ports for parallel execution.
|
||||
* Group 1: port_base..port_base+PORTS_PER_GROUP-1
|
||||
* Group 2: port_base+PORTS_PER_GROUP..port_base+2*PORTS_PER_GROUP-1, etc.
|
||||
@@ -322,7 +353,7 @@ init_prof_test(void **state)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!started) {
|
||||
fprintf(stderr, "[PROF_TEST] ERROR: could not start stabber on any port\n");
|
||||
return -1;
|
||||
@@ -335,8 +366,8 @@ init_prof_test(void **state)
|
||||
"./test-files/%d/xdg_config_home", dir_id);
|
||||
snprintf(xdg_data_home, sizeof(xdg_data_home),
|
||||
"./test-files/%d/xdg_data_home", dir_id);
|
||||
|
||||
printf("[PROF_TEST] Group %d using directories: config=%s, data=%s\n",
|
||||
|
||||
printf("[PROF_TEST] Group %d using directories: config=%s, data=%s\n",
|
||||
group, xdg_config_home, xdg_data_home);
|
||||
|
||||
config_orig = getenv("XDG_CONFIG_HOME");
|
||||
@@ -387,7 +418,7 @@ init_prof_test(void **state)
|
||||
}
|
||||
|
||||
int
|
||||
close_prof_test(void **state)
|
||||
close_prof_test(void** state)
|
||||
{
|
||||
if (fd > 0 && child_pid > 0) {
|
||||
prof_input("/quit");
|
||||
@@ -408,7 +439,10 @@ close_prof_test(void **state)
|
||||
if (!exited) {
|
||||
kill(child_pid, SIGTERM);
|
||||
for (int i = 0; i < SIGTERM_POLL_MAX; i++) {
|
||||
if (waitpid(child_pid, NULL, WNOHANG) != 0) { exited = 1; break; }
|
||||
if (waitpid(child_pid, NULL, WNOHANG) != 0) {
|
||||
exited = 1;
|
||||
break;
|
||||
}
|
||||
sleep_ms(SHUTDOWN_POLL_MS);
|
||||
}
|
||||
if (!exited) {
|
||||
@@ -427,18 +461,39 @@ close_prof_test(void **state)
|
||||
}
|
||||
|
||||
stbbr_stop();
|
||||
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
double elapsed = (now.tv_sec - test_start_ts.tv_sec)
|
||||
+ (now.tv_nsec - test_start_ts.tv_nsec) / 1e9;
|
||||
if (elapsed < min_elapsed) {
|
||||
min_elapsed = elapsed;
|
||||
min_test_name = current_test_name;
|
||||
}
|
||||
printf("[PROF_TEST] test '%s' took %.3f s (min: %.3fs '%s')\n",
|
||||
current_test_name, elapsed, min_elapsed, min_test_name);
|
||||
double slow_thr = prof_test_slow_threshold > 0 ? prof_test_slow_threshold : SLOW_TEST_THRESHOLD_S;
|
||||
double fast_thr = prof_test_fast_threshold > 0 ? prof_test_fast_threshold : FAST_TEST_THRESHOLD_S;
|
||||
if (elapsed >= slow_thr) {
|
||||
print_message("[PROF_TEST] WARNING: slow test '%s' \u2014 %.1fs (threshold %.1fs)\n",
|
||||
current_test_name, elapsed, slow_thr);
|
||||
} else if (elapsed < fast_thr) {
|
||||
print_message("[PROF_TEST] WARNING: suspiciously fast test '%s' \u2014 %.3fs (min %.3fs)\n",
|
||||
current_test_name, elapsed, fast_thr);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
prof_input(const char *input)
|
||||
prof_input(const char* input)
|
||||
{
|
||||
GString *inp_str = g_string_new(input);
|
||||
GString* inp_str = g_string_new(input);
|
||||
g_string_append(inp_str, "\r");
|
||||
ssize_t _wn = write(fd, inp_str->str, inp_str->len);
|
||||
(void)_wn;
|
||||
g_string_free(inp_str, TRUE);
|
||||
|
||||
|
||||
/* Small delay to let profanity process input */
|
||||
sleep_ms(INPUT_DELAY_MS);
|
||||
}
|
||||
@@ -448,24 +503,24 @@ prof_input(const char *input)
|
||||
* Returns 1 if found, 0 if timeout.
|
||||
*/
|
||||
int
|
||||
prof_output_exact(const char *text)
|
||||
prof_output_exact(const char* text)
|
||||
{
|
||||
time_t start = time(NULL);
|
||||
|
||||
|
||||
while (time(NULL) - start < expect_timeout) {
|
||||
/* Read any available output */
|
||||
while (_read_output(READ_TIMEOUT_MS) > 0) {
|
||||
/* Keep reading while data available */
|
||||
}
|
||||
|
||||
|
||||
/* Check if text is in buffer */
|
||||
if (strstr(output_buffer, text) != NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
sleep_ms(OUTPUT_POLL_MS);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -474,34 +529,34 @@ prof_output_exact(const char *text)
|
||||
* Returns 1 if found, 0 if timeout.
|
||||
*/
|
||||
int
|
||||
prof_output_regex(const char *pattern)
|
||||
prof_output_regex(const char* pattern)
|
||||
{
|
||||
regex_t regex;
|
||||
int ret;
|
||||
|
||||
|
||||
ret = regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB);
|
||||
if (ret != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
time_t start = time(NULL);
|
||||
|
||||
|
||||
while (time(NULL) - start < expect_timeout) {
|
||||
/* Read any available output */
|
||||
while (_read_output(READ_TIMEOUT_MS) > 0) {
|
||||
/* Keep reading while data available */
|
||||
}
|
||||
|
||||
|
||||
/* Check if pattern matches */
|
||||
ret = regexec(®ex, output_buffer, 0, NULL, 0);
|
||||
if (ret == 0) {
|
||||
regfree(®ex);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
sleep_ms(OUTPUT_POLL_MS);
|
||||
}
|
||||
|
||||
|
||||
/* Timeout reached - log diagnostic info */
|
||||
fprintf(stderr, "Timeout waiting for regex '%s' after %d seconds. Last output:\n", pattern, expect_timeout);
|
||||
size_t len = strlen(output_buffer);
|
||||
@@ -511,23 +566,21 @@ prof_output_regex(const char *pattern)
|
||||
fprintf(stderr, "%s", output_buffer);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
|
||||
|
||||
regfree(®ex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
prof_connect_with_roster(const char *roster)
|
||||
prof_connect_with_roster(const char* roster)
|
||||
{
|
||||
GString *roster_str = g_string_new(
|
||||
GString* roster_str = g_string_new(
|
||||
"<iq type='result' to='stabber@localhost/profanity'>"
|
||||
"<query xmlns='jabber:iq:roster' ver='362'>"
|
||||
);
|
||||
"<query xmlns='jabber:iq:roster' ver='362'>");
|
||||
g_string_append(roster_str, roster);
|
||||
g_string_append(roster_str,
|
||||
"</query>"
|
||||
"</iq>"
|
||||
);
|
||||
"</query>"
|
||||
"</iq>");
|
||||
|
||||
stbbr_for_query("jabber:iq:roster", roster_str->str);
|
||||
g_string_free(roster_str, TRUE);
|
||||
@@ -537,7 +590,7 @@ prof_connect_with_roster(const char *roster)
|
||||
char connect_cmd[128];
|
||||
snprintf(connect_cmd, sizeof(connect_cmd), "/connect stabber@localhost server 127.0.0.1 port %d tls disable auth legacy", stub_port);
|
||||
prof_input(connect_cmd);
|
||||
|
||||
|
||||
assert_true(prof_output_regex("password:"));
|
||||
prof_input("password");
|
||||
|
||||
@@ -545,15 +598,14 @@ prof_connect_with_roster(const char *roster)
|
||||
assert_true(prof_output_regex("Connecting as stabber@localhost"));
|
||||
assert_true(prof_output_regex("logged in successfully"));
|
||||
assert_true(prof_output_regex(".+online.+ \\(priority 0\\)\\."));
|
||||
|
||||
|
||||
expect_timeout = EXPECT_TIMEOUT_CONNECT;
|
||||
// Wait for presence stanza to be sent (content-based, not ID-based)
|
||||
// Match the actual attribute order from stanza_attach_caps
|
||||
assert_true(stbbr_received(
|
||||
"<presence id=\"*\">"
|
||||
"<c xmlns=\"http://jabber.org/protocol/caps\" hash=\"sha-1\" node=\"http://profanity-im.github.io\" ver=\"*\"/>"
|
||||
"</presence>"
|
||||
));
|
||||
"<c xmlns=\"http://jabber.org/protocol/caps\" hash=\"sha-1\" node=\"http://profanity-im.github.io\" ver=\"*\"/>"
|
||||
"</presence>"));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -573,6 +625,5 @@ prof_connect(void)
|
||||
{
|
||||
prof_connect_with_roster(
|
||||
"<item jid='buddy1@localhost' subscription='both' name='Buddy1'/>"
|
||||
"<item jid='buddy2@localhost' subscription='both' name='Buddy2'/>"
|
||||
);
|
||||
"<item jid='buddy2@localhost' subscription='both' name='Buddy2'/>");
|
||||
}
|
||||
|
||||
@@ -11,18 +11,28 @@ extern char xdg_data_home[256];
|
||||
|
||||
extern int stub_port;
|
||||
|
||||
int init_prof_test(void **state);
|
||||
int close_prof_test(void **state);
|
||||
int init_prof_test(void** state);
|
||||
int close_prof_test(void** state);
|
||||
|
||||
void prof_start(void);
|
||||
void prof_connect(void);
|
||||
void prof_connect_with_roster(const char *roster);
|
||||
void prof_input(const char *input);
|
||||
void prof_connect_with_roster(const char* roster);
|
||||
void prof_input(const char* input);
|
||||
|
||||
int prof_output_exact(const char *text);
|
||||
int prof_output_regex(const char *text);
|
||||
int prof_output_exact(const char* text);
|
||||
int prof_output_regex(const char* text);
|
||||
|
||||
void prof_timeout(int timeout);
|
||||
void prof_timeout_reset(void);
|
||||
|
||||
/* Short timeout for negative assertions (seconds) */
|
||||
#define NEGATIVE_ASSERT_TIMEOUT 3
|
||||
|
||||
/* Per-test threshold overrides (0 = use default) */
|
||||
extern double prof_test_slow_threshold;
|
||||
extern double prof_test_fast_threshold;
|
||||
|
||||
/* Timezone used for profanity child process (default "UTC") */
|
||||
extern const char* prof_test_tz;
|
||||
|
||||
#endif
|
||||
|
||||
1551
tests/functionaltests/test_export_import.c
Normal file
1551
tests/functionaltests/test_export_import.c
Normal file
File diff suppressed because it is too large
Load Diff
24
tests/functionaltests/test_export_import.h
Normal file
24
tests/functionaltests/test_export_import.h
Normal file
@@ -0,0 +1,24 @@
|
||||
void export_sqlite_to_flatfile(void** state);
|
||||
void import_flatfile_to_sqlite(void** state);
|
||||
void export_idempotent_no_duplicates(void** state);
|
||||
void export_lmc_correction_survives(void** state);
|
||||
void switch_preserves_old_backend_data(void** state);
|
||||
void export_all_contacts(void** state);
|
||||
void import_double_dedup(void** state);
|
||||
void verify_after_export(void** state);
|
||||
void switch_backends_independent_messages(void** state);
|
||||
void export_empty_contact(void** state);
|
||||
void export_message_pool_dated(void** state);
|
||||
void export_lmc_pool_multiple(void** state);
|
||||
void export_large_body_survives(void** state);
|
||||
void bidirectional_merge_to_flatfile(void** state);
|
||||
void bidirectional_merge_to_sqlite(void** state);
|
||||
void migration_incremental_accumulation(void** state);
|
||||
void roundtrip_full_cycle(void** state);
|
||||
void export_timestamps_preserved(void** state);
|
||||
void import_timestamps_preserved(void** state);
|
||||
void export_timestamps_non_utc(void** state);
|
||||
void muc_export_sqlite_to_flatfile(void** state);
|
||||
void muc_import_flatfile_to_sqlite(void** state);
|
||||
void muc_export_timestamps_preserved(void** state);
|
||||
void muc_export_multiple_rooms(void** state);
|
||||
538
tests/functionaltests/test_history.c
Normal file
538
tests/functionaltests/test_history.c
Normal file
@@ -0,0 +1,538 @@
|
||||
/*
|
||||
* 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"));
|
||||
}
|
||||
|
||||
/*
|
||||
* 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."));
|
||||
}
|
||||
|
||||
/*
|
||||
* 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();
|
||||
}
|
||||
|
||||
/*
|
||||
* Test: messages from the SAME bare JID but with DIFFERENT resources are
|
||||
* stored together in one contact's history AND remain distinguishable on
|
||||
* reopen — the resource part of the sender JID must be preserved per row,
|
||||
* not collapsed.
|
||||
*
|
||||
* Flow:
|
||||
* 1. /history on
|
||||
* 2. buddy1@localhost/phone sends msg A
|
||||
* 3. buddy1@localhost/laptop sends msg B
|
||||
* 4. buddy1@localhost/tablet sends msg C
|
||||
* 5. Close the auto-opened chat window so its in-memory buffer is gone.
|
||||
* 6. /msg buddy1@localhost — chatwin_new() rehydrates from DB.
|
||||
* 7. All three bodies must reappear, AND the resource markers
|
||||
* "/phone", "/laptop", "/tablet" must each be visible on at least one
|
||||
* history line (PREF_RESOURCE_MESSAGE is on by default in the test
|
||||
* profile, so the renderer prefixes each line with "Buddy1/<res>").
|
||||
*/
|
||||
void
|
||||
message_db_history_multi_resource(void** state)
|
||||
{
|
||||
prof_connect();
|
||||
|
||||
prof_input("/history on");
|
||||
assert_true(prof_output_regex("Chat history enabled\\."));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='res-multi-A' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/phone' type='chat'>"
|
||||
"<body>multi-resource-A-from-phone</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/phone (win 2)"));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='res-multi-B' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/laptop' type='chat'>"
|
||||
"<body>multi-resource-B-from-laptop</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/laptop (win 2)"));
|
||||
|
||||
stbbr_send(
|
||||
"<message id='res-multi-C' to='stabber@localhost' "
|
||||
"from='buddy1@localhost/tablet' type='chat'>"
|
||||
"<body>multi-resource-C-from-tablet</body>"
|
||||
"</message>");
|
||||
assert_true(prof_output_exact("<< chat message: Buddy1/tablet (win 2)"));
|
||||
|
||||
prof_input("/close 2");
|
||||
assert_true(prof_output_exact("Closed window 2"));
|
||||
|
||||
prof_input("/msg buddy1@localhost");
|
||||
|
||||
/* All three bodies must be present in history (single-contact aggregation) */
|
||||
assert_true(prof_output_exact("multi-resource-A-from-phone"));
|
||||
assert_true(prof_output_exact("multi-resource-B-from-laptop"));
|
||||
assert_true(prof_output_exact("multi-resource-C-from-tablet"));
|
||||
|
||||
/* Each resource must remain identifiable on rehydrate — proves we don't
|
||||
* lose per-row resource info when loading the same JID's chat back. */
|
||||
assert_true(prof_output_regex("Buddy1/phone"));
|
||||
assert_true(prof_output_regex("Buddy1/laptop"));
|
||||
assert_true(prof_output_regex("Buddy1/tablet"));
|
||||
}
|
||||
13
tests/functionaltests/test_history.h
Normal file
13
tests/functionaltests/test_history.h
Normal file
@@ -0,0 +1,13 @@
|
||||
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);
|
||||
void message_db_history_verify(void** state);
|
||||
void message_db_history_lmc(void** state);
|
||||
void message_db_history_multi_resource(void** state);
|
||||
@@ -44,11 +44,11 @@ send_receipt_request(void **state)
|
||||
"<presence to='stabber@localhost' from='buddy1@localhost/laptop'>"
|
||||
"<priority>15</priority>"
|
||||
"<status>My status</status>"
|
||||
"<c hash='sha-256' xmlns='http://jabber.org/protocol/caps' node='http://profanity-im.github.io' ver='hAkb1xZdJV9BQpgGNw8zG5Xsals='/>"
|
||||
"<c hash='sha-1' xmlns='http://jabber.org/protocol/caps' node='http://profanity-im.github.io' ver='hAkb1xZdJV9BQpgGNw8zG5Xsals='/>"
|
||||
"</presence>"
|
||||
);
|
||||
|
||||
prof_output_exact("Buddy1 is online, \"My status\"");
|
||||
assert_true(prof_output_exact("Buddy1 (laptop) is online, \"My status\""));
|
||||
|
||||
prof_input("/msg Buddy1");
|
||||
prof_input("/resource set laptop");
|
||||
|
||||
Reference in New Issue
Block a user