Files
cproof/tests/unittests/test_database_export.c
Jabber Developer 3f36c303c2
All checks were successful
CI Code / Check spelling (push) Successful in 21s
CI Code / Check coding style (push) Successful in 31s
CI Code / Code Coverage (push) Successful in 2m21s
CI Code / Linux (ubuntu) (push) Successful in 4m30s
CI Code / Linux (debian) (push) Successful in 6m43s
CI Code / Linux (arch) (push) Successful in 10m8s
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>
2026-05-05 19:26:07 +00:00

784 lines
23 KiB
C

// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of CProof.
// See LICENSE for the full GPLv3 text and the special OpenSSL linking exception.
/*
* test_database_export.c
*
* Unit tests for the flatfile write→parse round-trip, escape/unescape helpers,
* jid_to_dir path construction, and parser edge cases — these are the core
* functions exercised by database_export.c during export and import.
*/
#include "prof_cmocka.h"
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "database_flatfile.h"
/* ================================================================
* Helper: write a single line to a temporary file, then read and
* parse it back. Returns the parsed result; caller must free with
* ff_parsed_line_free(). The temp file is removed automatically.
* ================================================================ */
static ff_parsed_line_t*
_roundtrip(const char* timestamp, const char* type, const char* enc,
const char* stanza_id, const char* archive_id, const char* replace_id,
const char* from_jid, const char* from_resource,
const char* to_jid, const char* to_resource, int marked_read,
const char* message)
{
char tmppath[] = "/tmp/proftest_rt_XXXXXX";
int fd = mkstemp(tmppath);
assert_true(fd >= 0);
FILE* fp = fdopen(fd, "w");
assert_non_null(fp);
ff_write_line(fp, timestamp, type, enc,
stanza_id, archive_id, replace_id,
from_jid, from_resource,
to_jid, to_resource, marked_read,
message);
fclose(fp);
/* Read & parse */
fp = fopen(tmppath, "r");
assert_non_null(fp);
gboolean truncated = FALSE;
char* buf = ff_readline(fp, &truncated);
fclose(fp);
unlink(tmppath);
assert_non_null(buf);
assert_false(truncated);
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
return pl;
}
/* ================================================================
* Write → parse round-trip tests
* ================================================================ */
void
test_ff_roundtrip_simple_chat(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-06-15T12:30:00+00:00", "chat", "none",
NULL, NULL, NULL,
"alice@example.com", NULL,
NULL, NULL, -1,
"Hello, world!");
assert_non_null(pl);
assert_string_equal(pl->timestamp_str, "2025-06-15T12:30:00+00:00");
assert_string_equal(pl->type, "chat");
assert_string_equal(pl->enc, "none");
assert_string_equal(pl->from_jid, "alice@example.com");
assert_null(pl->stanza_id);
assert_null(pl->archive_id);
assert_null(pl->replace_id);
assert_string_equal(pl->message, "Hello, world!");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_with_all_metadata(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-06-15T12:30:00+00:00", "chat", "omemo",
"sid-abc-123", "aid-xyz-789", "corrects-old-id",
"bob@example.com", "phone",
NULL, NULL, -1,
"Encrypted message.");
assert_non_null(pl);
assert_string_equal(pl->stanza_id, "sid-abc-123");
assert_string_equal(pl->archive_id, "aid-xyz-789");
assert_string_equal(pl->replace_id, "corrects-old-id");
assert_string_equal(pl->enc, "omemo");
assert_string_equal(pl->from_jid, "bob@example.com");
assert_string_equal(pl->from_resource, "phone");
assert_string_equal(pl->message, "Encrypted message.");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_with_resource(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-01-01T00:00:00Z", "chat", "none",
NULL, NULL, NULL,
"user@jabber.org", "Profanity.abc123",
NULL, NULL, -1,
"hi");
assert_non_null(pl);
assert_string_equal(pl->from_jid, "user@jabber.org");
assert_string_equal(pl->from_resource, "Profanity.abc123");
assert_string_equal(pl->message, "hi");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_newline_in_body(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"alice@x.com", NULL,
NULL, NULL, -1,
"line1\nline2\nline3");
assert_non_null(pl);
assert_string_equal(pl->message, "line1\nline2\nline3");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_pipe_in_stanza_id(void** state)
{
/* pipes in stanza_id must be escaped in metadata */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
"id|with|pipes", "aid|also|has|pipes", NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"test");
assert_non_null(pl);
assert_string_equal(pl->stanza_id, "id|with|pipes");
assert_string_equal(pl->archive_id, "aid|also|has|pipes");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_backslash_in_body(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"path\\to\\file C:\\Users\\test");
assert_non_null(pl);
assert_string_equal(pl->message, "path\\to\\file C:\\Users\\test");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_unicode_body(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"Привет мир 🌍 日本語テスト");
assert_non_null(pl);
assert_string_equal(pl->message, "Привет мир 🌍 日本語テスト");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_empty_body(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"");
assert_non_null(pl);
assert_string_equal(pl->message, "");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_colonspace_in_resource(void** state)
{
/* ": " in resource must be escaped during write and unescaped on parse */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", "res: with: colons",
NULL, NULL, -1,
"msg");
assert_non_null(pl);
assert_string_equal(pl->from_jid, "a@b.com");
assert_string_equal(pl->from_resource, "res: with: colons");
assert_string_equal(pl->message, "msg");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_muc_type(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "muc", "none",
NULL, NULL, NULL,
"room@conference.x.com", "nick",
NULL, NULL, -1,
"hello room");
assert_non_null(pl);
assert_string_equal(pl->type, "muc");
assert_string_equal(pl->from_jid, "room@conference.x.com");
assert_string_equal(pl->from_resource, "nick");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_omemo_enc(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "omemo",
"sid-1", NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"secret");
assert_non_null(pl);
assert_string_equal(pl->enc, "omemo");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_replace_id(void** state)
{
/* LMC (Last Message Correction): corrects: field round-trip */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
"new-id", NULL, "old-id-to-correct",
"a@b.com", NULL,
NULL, NULL, -1,
"corrected text");
assert_non_null(pl);
assert_string_equal(pl->stanza_id, "new-id");
assert_string_equal(pl->replace_id, "old-id-to-correct");
assert_string_equal(pl->message, "corrected text");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_to_jid_and_marked_read(void** state)
{
/* to_jid, to_resource, and marked_read round-trip */
ff_parsed_line_t* pl = _roundtrip(
"2025-06-15T12:30:00+00:00", "chat", "none",
"sid-1", NULL, NULL,
"alice@example.com", "phone",
"bob@example.com", "laptop", 1,
"Hello Bob!");
assert_non_null(pl);
assert_string_equal(pl->from_jid, "alice@example.com");
assert_string_equal(pl->from_resource, "phone");
assert_string_equal(pl->to_jid, "bob@example.com");
assert_string_equal(pl->to_resource, "laptop");
assert_int_equal(pl->marked_read, 1);
assert_string_equal(pl->message, "Hello Bob!");
ff_parsed_line_free(pl);
/* marked_read = 0 (unread) */
pl = _roundtrip(
"2025-06-15T12:31:00+00:00", "chat", "none",
NULL, NULL, NULL,
"bob@example.com", NULL,
"alice@example.com", NULL, 0,
"Reply");
assert_non_null(pl);
assert_string_equal(pl->to_jid, "alice@example.com");
assert_null(pl->to_resource);
assert_int_equal(pl->marked_read, 0);
ff_parsed_line_free(pl);
/* marked_read = -1 (unset) — should NOT appear in output */
pl = _roundtrip(
"2025-06-15T12:32:00+00:00", "chat", "none",
NULL, NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"no read flag");
assert_non_null(pl);
assert_null(pl->to_jid);
assert_null(pl->to_resource);
assert_int_equal(pl->marked_read, -1);
ff_parsed_line_free(pl);
}
/* ================================================================
* Escape / unescape symmetry tests
* ================================================================ */
void
test_ff_escape_unescape_message_identity(void** state)
{
const char* inputs[] = {
"simple text",
"has\\backslash",
"has\nnewline",
"has\rcarriage return",
"all\\together\nnow\r!",
"already escaped \\n \\r \\\\",
"",
};
for (size_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
char* escaped = ff_escape_message(inputs[i]);
assert_non_null(escaped);
/* escaped must not contain raw newlines */
assert_null(strchr(escaped, '\n'));
assert_null(strchr(escaped, '\r'));
char* unescaped = ff_unescape_message(escaped);
assert_string_equal(unescaped, inputs[i]);
g_free(escaped);
g_free(unescaped);
}
}
void
test_ff_escape_unescape_meta_identity(void** state)
{
const char* inputs[] = {
"simple-id",
"has|pipe",
"has]bracket",
"has\\backslash",
"has\nnewline",
"all|to]ge\\th\ner",
};
for (size_t i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
char* escaped = ff_escape_meta_value(inputs[i]);
assert_non_null(escaped);
char* unescaped = ff_unescape_meta_value(escaped);
assert_string_equal(unescaped, inputs[i]);
g_free(escaped);
g_free(unescaped);
}
}
void
test_ff_escape_meta_null_returns_null(void** state)
{
assert_null(ff_escape_meta_value(NULL));
assert_null(ff_escape_meta_value(""));
assert_null(ff_unescape_meta_value(NULL));
}
void
test_ff_escape_message_null_returns_empty(void** state)
{
char* result = ff_escape_message(NULL);
assert_non_null(result);
assert_string_equal(result, "");
g_free(result);
result = ff_unescape_message(NULL);
assert_non_null(result);
assert_string_equal(result, "");
g_free(result);
}
/* ================================================================
* jid_to_dir tests
* ================================================================ */
void
test_ff_jid_to_dir_simple(void** state)
{
char* dir = ff_jid_to_dir("alice");
assert_non_null(dir);
assert_string_equal(dir, "alice");
g_free(dir);
}
void
test_ff_jid_to_dir_with_at(void** state)
{
char* dir = ff_jid_to_dir("alice@example.com");
assert_non_null(dir);
assert_string_equal(dir, "alice_at_example.com");
g_free(dir);
}
void
test_ff_jid_to_dir_path_traversal_rejected(void** state)
{
/* Slashes and '..' must be sanitized */
char* dir = ff_jid_to_dir("../../etc/passwd");
assert_non_null(dir);
/* No slashes or '..' sequences should remain */
assert_null(strchr(dir, '/'));
assert_null(strstr(dir, ".."));
g_free(dir);
}
void
test_ff_jid_to_dir_null(void** state)
{
assert_null(ff_jid_to_dir(NULL));
assert_null(ff_jid_to_dir(""));
}
/* ================================================================
* Parser edge-case tests
* ================================================================ */
void
test_ff_parse_line_empty(void** state)
{
assert_null(ff_parse_line(NULL));
assert_null(ff_parse_line(""));
}
void
test_ff_parse_line_comment(void** state)
{
assert_null(ff_parse_line("# this is a comment"));
assert_null(ff_parse_line("# cproof chat log — UTF-8, LF line endings"));
}
void
test_ff_parse_line_legacy_format(void** state)
{
/* Legacy chatlog.c format: {timestamp} - {sender}: {message} */
ff_parsed_line_t* pl = ff_parse_line(
"2025-06-15T12:30:00+00:00 - alice@example.com: Hello legacy");
assert_non_null(pl);
assert_string_equal(pl->timestamp_str, "2025-06-15T12:30:00+00:00");
assert_string_equal(pl->from_jid, "alice@example.com");
assert_string_equal(pl->message, "Hello legacy");
assert_string_equal(pl->type, "chat");
assert_string_equal(pl->enc, "none");
ff_parsed_line_free(pl);
}
void
test_ff_parse_line_no_metadata(void** state)
{
/* Simple format without [] brackets */
ff_parsed_line_t* pl = ff_parse_line(
"2025-06-15T12:30:00Z alice@example.com: Simple message");
assert_non_null(pl);
assert_string_equal(pl->from_jid, "alice@example.com");
assert_string_equal(pl->message, "Simple message");
ff_parsed_line_free(pl);
}
void
test_ff_parse_line_invalid_timestamp(void** state)
{
/* Invalid timestamp should cause parse failure */
ff_parsed_line_t* pl = ff_parse_line(
"not-a-timestamp [chat|none] a@b.com: msg");
assert_null(pl);
}
void
test_ff_parse_line_empty_timestamp(void** state)
{
/* The space before "[" makes the timestamp empty -> g_date_time_new_from_iso8601
must reject it. */
ff_parsed_line_t* pl = ff_parse_line(
" [chat|none] a@b.com: msg");
assert_null(pl);
}
void
test_ff_parse_line_partial_iso_timestamp(void** state)
{
/* Year-only or yyyy-mm without time portion is not ISO-8601 enough for
g_date_time_new_from_iso8601 — must fail rather than silently succeed. */
assert_null(ff_parse_line("2025 [chat|none] a@b.com: msg"));
assert_null(ff_parse_line("2025-06-15 [chat|none] a@b.com: msg"));
}
void
test_ff_parse_line_garbage_timestamp(void** state)
{
/* Random characters where timestamp should be — parse failure, no segfault. */
assert_null(ff_parse_line("@@@@@@ [chat|none] a@b.com: msg"));
assert_null(ff_parse_line("12345 [chat|none] a@b.com: msg"));
}
void
test_ff_parse_line_impossible_calendar_date(void** state)
{
/* Day 32 / Month 13 — well-formed ISO syntax but impossible date,
g_date_time_new_from_iso8601 returns NULL. */
assert_null(ff_parse_line(
"2025-13-01T12:00:00Z [chat|none] a@b.com: msg"));
assert_null(ff_parse_line(
"2025-06-32T12:00:00Z [chat|none] a@b.com: msg"));
assert_null(ff_parse_line(
"2025-06-15T25:00:00Z [chat|none] a@b.com: msg"));
}
void
test_ff_parse_line_negative_year_timestamp(void** state)
{
/* Negative year is not parseable by g_date_time_new_from_iso8601. */
assert_null(ff_parse_line(
"-0001-06-15T12:00:00Z [chat|none] a@b.com: msg"));
}
void
test_ff_parse_line_far_future_timestamp(void** state)
{
/* Year 9999 is valid ISO-8601 and should parse — sanity check that we
don't artificially reject valid far-future dates. */
ff_parsed_line_t* pl = ff_parse_line(
"9999-12-31T23:59:59Z [chat|none] a@b.com: hi");
assert_non_null(pl);
assert_non_null(pl->timestamp);
ff_parsed_line_free(pl);
}
/* ================================================================
* Type / enc conversion round-trip
* ================================================================ */
void
test_ff_type_str_roundtrip(void** state)
{
assert_int_equal(PROF_MSG_TYPE_CHAT, ff_get_message_type_type(ff_get_message_type_str(PROF_MSG_TYPE_CHAT)));
assert_int_equal(PROF_MSG_TYPE_MUC, ff_get_message_type_type(ff_get_message_type_str(PROF_MSG_TYPE_MUC)));
assert_int_equal(PROF_MSG_TYPE_MUCPM, ff_get_message_type_type(ff_get_message_type_str(PROF_MSG_TYPE_MUCPM)));
}
void
test_ff_enc_str_roundtrip(void** state)
{
assert_int_equal(PROF_MSG_ENC_NONE, ff_get_message_enc_type(ff_get_message_enc_str(PROF_MSG_ENC_NONE)));
assert_int_equal(PROF_MSG_ENC_OTR, ff_get_message_enc_type(ff_get_message_enc_str(PROF_MSG_ENC_OTR)));
assert_int_equal(PROF_MSG_ENC_PGP, ff_get_message_enc_type(ff_get_message_enc_str(PROF_MSG_ENC_PGP)));
assert_int_equal(PROF_MSG_ENC_OX, ff_get_message_enc_type(ff_get_message_enc_str(PROF_MSG_ENC_OX)));
assert_int_equal(PROF_MSG_ENC_OMEMO, ff_get_message_enc_type(ff_get_message_enc_str(PROF_MSG_ENC_OMEMO)));
}
/* ================================================================
* Additional round-trip tests
* ================================================================ */
void
test_ff_roundtrip_bracket_in_stanza_id(void** state)
{
/* ']' in stanza_id must be escaped as \] to avoid breaking metadata parser */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
"id]with]brackets", "aid]also]has]brackets", NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"test brackets");
assert_non_null(pl);
assert_string_equal(pl->stanza_id, "id]with]brackets");
assert_string_equal(pl->archive_id, "aid]also]has]brackets");
assert_string_equal(pl->message, "test brackets");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_backslash_in_resource(void** state)
{
/* backslash in from_resource must survive round-trip */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", "res\\with\\backslash",
NULL, NULL, -1,
"msg");
assert_non_null(pl);
assert_string_equal(pl->from_jid, "a@b.com");
assert_string_equal(pl->from_resource, "res\\with\\backslash");
assert_string_equal(pl->message, "msg");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_mucpm_type(void** state)
{
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "mucpm", "none",
NULL, NULL, NULL,
"room@conference.x.com", "nick",
NULL, NULL, -1,
"private message");
assert_non_null(pl);
assert_string_equal(pl->type, "mucpm");
assert_string_equal(pl->from_resource, "nick");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_all_enc_types(void** state)
{
const char* enc_types[] = { "none", "otr", "pgp", "ox", "omemo" };
for (size_t i = 0; i < sizeof(enc_types) / sizeof(enc_types[0]); i++) {
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", enc_types[i],
NULL, NULL, NULL,
"a@b.com", NULL,
NULL, NULL, -1,
"msg");
assert_non_null(pl);
assert_string_equal(pl->enc, enc_types[i]);
ff_parsed_line_free(pl);
}
}
void
test_ff_roundtrip_crlf_handling(void** state)
{
/* Write a line, then read it with \r\n ending — parser should strip \r */
char tmppath[] = "/tmp/proftest_crlf_XXXXXX";
int fd = mkstemp(tmppath);
assert_true(fd >= 0);
FILE* fp = fdopen(fd, "w");
assert_non_null(fp);
/* Write a raw line with \r\n ending */
fprintf(fp, "2025-03-01T10:00:00Z [chat|none] a@b.com: hello\r\n");
fclose(fp);
fp = fopen(tmppath, "r");
assert_non_null(fp);
gboolean truncated = FALSE;
char* buf = ff_readline(fp, &truncated);
fclose(fp);
unlink(tmppath);
assert_non_null(buf);
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
assert_non_null(pl);
assert_string_equal(pl->from_jid, "a@b.com");
assert_string_equal(pl->message, "hello");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_to_jid_special_chars(void** state)
{
/* to: and to_res: with pipe and bracket chars that need escaping */
ff_parsed_line_t* pl = _roundtrip(
"2025-03-01T10:00:00Z", "chat", "none",
NULL, NULL, NULL,
"a@b.com", NULL,
"to|user@c.com", "res]with|special", -1,
"msg with special to");
assert_non_null(pl);
assert_string_equal(pl->to_jid, "to|user@c.com");
assert_string_equal(pl->to_resource, "res]with|special");
assert_string_equal(pl->message, "msg with special to");
ff_parsed_line_free(pl);
}
void
test_ff_roundtrip_multiple_lines(void** state)
{
/* Write several lines, read and parse them all sequentially */
char tmppath[] = "/tmp/proftest_multi_XXXXXX";
int fd = mkstemp(tmppath);
assert_true(fd >= 0);
FILE* fp = fdopen(fd, "w");
assert_non_null(fp);
ff_write_line(fp, "2025-01-01T00:00:01Z", "chat", "none",
NULL, NULL, NULL, "a@b.com", NULL,
NULL, NULL, -1, "first");
ff_write_line(fp, "2025-01-01T00:00:02Z", "chat", "none",
NULL, NULL, NULL, "c@d.com", NULL,
NULL, NULL, -1, "second");
ff_write_line(fp, "2025-01-01T00:00:03Z", "muc", "omemo",
"sid-3", NULL, NULL, "room@conf.com", "nick",
NULL, NULL, -1, "third");
fclose(fp);
fp = fopen(tmppath, "r");
assert_non_null(fp);
int count = 0;
const char* expected_msgs[] = { "first", "second", "third" };
const char* expected_from[] = { "a@b.com", "c@d.com", "room@conf.com" };
while (1) {
gboolean truncated = FALSE;
char* buf = ff_readline(fp, &truncated);
if (!buf)
break;
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
if (pl) {
assert_true(count < 3);
assert_string_equal(pl->message, expected_msgs[count]);
assert_string_equal(pl->from_jid, expected_from[count]);
ff_parsed_line_free(pl);
count++;
}
}
fclose(fp);
unlink(tmppath);
assert_int_equal(count, 3);
}
/* ================================================================
* Additional parser edge-case tests
* ================================================================ */
void
test_ff_parsed_line_free_null_safe(void** state)
{
/* ff_parsed_line_free(NULL) must not crash */
ff_parsed_line_free(NULL);
}
void
test_ff_parse_line_no_space_rejected(void** state)
{
/* A line with no spaces at all cannot be parsed */
assert_null(ff_parse_line("noseparatoratall"));
}
void
test_ff_parse_line_unclosed_bracket(void** state)
{
/* Unclosed metadata bracket should return NULL */
assert_null(ff_parse_line("2025-03-01T10:00:00Z [chat|none a@b.com: msg"));
}