feat(history): flat-file backend with bidirectional SQLite migration
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

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:
2026-05-05 19:26:07 +00:00
parent 0feacbc9da
commit 3f36c303c2
50 changed files with 13811 additions and 793 deletions

288
tests/bench/README.md Normal file
View File

@@ -0,0 +1,288 @@
# Flat-file backend bench harness
Synthetic load tests for the flat-file database backend. Not part of `make
check` — must be invoked explicitly. See `REVIEW.txt` Phase-9 plan for the
design and scenario list.
## Quick start
```sh
# small (~40 MB corpus due to mixed length profile, ~10 seconds total)
make bench-quick
# medium (~2 GB corpus, ~1 minute)
make bench BENCH_VOLUME=medium
# max (~20 GB corpus, ~510 minutes, requires NVMe + plenty of free disk)
make bench BENCH_VOLUME=max
# Everything: P1 + long messages + failure-injection + S9/S10/S11 variants
make bench-full
# Compare against committed baseline (see Baseline section below)
make bench-compare
# Just the long-message battery (L1L14, ~5 seconds, self-contained)
make bench-longmsg
# Just failure-injection tests (F1F15, ~10 ms total)
make bench-failure
# S7/S8 export/import pipeline at default 100k rows
make bench-pipeline
# Same pipeline at 1M rows (~5 min, ~3 GB disk)
make bench-pipeline-max
```
After a run, results land in `tests/bench/current.csv`:
```
scenario,volume,bytes,lines,wall_ms,peak_rss_kb,note
S1_cold_tail,small,1453782,10000,4.512,12340,tail page=100
S2_warm_tail,small,1453782,10000,1.823,12340,tail page=100
...
```
## Volume profiles
| Profile | Lines | Years | Length profile | Disk | Time on NVMe |
|---|---|---|---|---|---|
| `small` | 10 000 | 1 | `mixed` | ~5 MB | ~10 s |
| `medium` | 500 000 | 5 | `mixed` | ~100 MB | ~1 min |
| `max` | 5 000 000| 10 | `long` | ~1 GB | ~510 min |
## Scenarios in P1 (`make bench`)
| ID | What | Bench function |
|---|---|---|
| S1 | Cold tail-access (drop cache, fetch last 100 lines via sparse-index seek) | `run_tail_access(drop=1)` |
| S2 | Warm tail-access (cache hot) | `run_tail_access(drop=0)` |
| S3 | Deep pagination (1 000 binary-search lookups across the index) | `run_deep_pagination` |
| S4 | First-time index build (cold file → `ff_state_ensure_fresh`) | `run_first_build` |
| S5 | Incremental extend (append N lines, ensure no full rebuild) | `run_incremental_extend` |
| S6 | Verify-equivalent full parse pass | `run_verify` |
## P2 corpus variants (separate make targets)
Each variant generates its own corpus and reuses `bench_runner`'s scenarios.
| Target | Corpus | Purpose |
|---|---|---|
| `make bench-multicontact` | 200 contacts × 5k lines × 3 years | S9 — directory discovery & multi-state cost |
| `make bench-lmc` | 100k lines, 30 % LMC corrections | S10 — correction-heavy LMC chain throughput |
| `make bench-ooo` | 100k lines, 20 % MAM out-of-order | S11 — verify must surface timestamp warnings |
| `make bench-longmsg` | (no corpus, self-contained tests) | L1L14 — long-message stress |
| `make bench-full` | runs all of the above sequentially | reporting baseline |
## Long-message tests (L1L14)
Run via `make bench-longmsg`. Each test generates N messages with a specific
body size and content pattern, writes via `ff_write_line`, reads via
`ff_readline + ff_parse_line`, asserts body length match, emits a CSV row.
| ID | Body | Pattern | Count | What it verifies |
|----|------|---------|-------|------------------|
| L1 | 1 KB | filler | 100 | sanity / fast path |
| L2 | 10 KB | filler | 100 | escape pipeline at moderate size |
| L3 | 100 KB | filler | 100 | paste-bomb territory |
| L4 | 1 MB | filler | 50 | 50 MB pipe — measures throughput |
| L5 | 5 MB | filler | 10 | 50 MB pipe |
| L6 | 9.9 MB | filler | 4 | just under `FF_MAX_LINE_LEN` (10 MB) |
| L7 | 10 MB+1 | filler | 1+1 | **rejection path**: `ff_readline` returns "" + skips |
| L8 | 100 KB | embedded `\n` | 50 | escape stress (each `\n` doubles to `\\n`) |
| L9 | 100 KB | embedded `\|` | 50 | body pipes (not metadata) shouldn't choke parser |
| L10 | 100 KB | UTF-8 emoji | 50 | 4-byte codepoints / `g_utf8_validate` path |
| L11 | 1 KB | filler | 100 | sanity baseline (re-run, expect L1-equivalent) |
| L12 | mixed | 5 × 1 MB at end | 1000 | pagination memory: last-100 with paste-bombs |
| L13 | 1 MB | filler | 100 | full parse pass on 100 MB file |
| L14 | 100 KB | filler | 1000 | sustained append throughput |
L7 specifically asserts `ff_readline` rejects oversized lines (returns `""`,
logs "line too long", continues to next line — the very-next-line is a normal
record, must parse OK).
## Export/import pipeline (S7/S8)
Run via `make bench-pipeline` (default 100k rows) or `make bench-pipeline-max`
(1M rows). Each subcommand emits a CSV row; full content diff is enforced
on every roundtrip.
| Target | Sub-scenarios | What |
|---|---|---|
| `bench-export` | `S7a_export_cold`, `S7b_export_dedup` | Seed SQLite N rows → real `log_database_export_to_flatfile`; second pass measures dedup hot path |
| `bench-import` | `S7_seed_export`, `S8a_import_cold`, `S8b_import_idempotent` | Seed → export → wipe DB → real `log_database_import_from_flatfile`; second import asserts dedup (rows added = 0) |
| `bench-roundtrip` | `S8e_roundtrip` | Seed DB-A → export to flatfile → import to fresh DB-B → **full byte-by-byte content diff** of every row in (from_jid, to_jid, message, timestamp, type, stanza_id, archive_id, encryption, replace_id) |
| `bench-pipeline` | all of the above | combined run |
| `bench-pipeline-max` | same, at 1M rows | heavy: ~3 GB disk, ~5 min |
The bench links the **real** `database_export.c` + `database_sqlite.c` +
`database.c` so timings reflect actual production code paths (merge sort,
GHashTable dedup, transactional INSERT, etc.).
`BENCH_PIPE_ROWS` overrides the default row count; `BENCH_PIPE_ROWS_MAX`
overrides the max-volume default.
The roundtrip CSV note records each phase wall-time:
`rows=N seed=Xms export=Yms import=Zms diff=Wms exported=… imported=… mismatches=…`.
The bench also exposes `bench_export_import` directly with five subcommands
(`seed`, `export`, `import`, `roundtrip`, `verify`) for ad-hoc use:
```sh
./tests/bench/bench_export_import seed --rows=1000000 --account=foo@bar
./tests/bench/bench_export_import export --account=foo@bar --csv=out.csv
./tests/bench/bench_export_import roundtrip --rows=100000 --csv=out.csv --full-diff
./tests/bench/bench_export_import verify --db-a=A.db --db-b=B.db
```
## Failure-injection (F1F15)
Run via `make bench-failure`. Each test crafts a deliberately corrupt log file
and asserts how the backend handles it. Exits non-zero on any failure.
| ID | Injection | Expected behaviour |
|---|---|---|
| F1 | Last line truncated mid-write + partial appended line | Verify completes; line counted as a parse error or skipped without crash |
| F2 | CRLF line endings on three lines mid-corpus | Verify reports CRLF warning |
| F3 | UTF-8 BOM bytes appearing mid-file (not at start) | Verify reports unparsable line ERROR for that record |
| F7 | LMC cycle: A→B references and B→A references | Verify completes without infinite loop (cycle handled at read-time, not verify) |
| F8 | LMC chain depth 200 (over `FF_MAX_LMC_DEPTH=100`) | Verify reports no broken refs (depth-truncation lives at read-time) |
| F9 | Unescaped `: ` inside resource (manual edit) | Parser splits at first `: ` — body truncated. Not flagged by verify (known parser quirk; documented) |
| F10 | RTL override + zero-width space in resource | Bytes preserved literally, parses fine |
| F11 | Latin-1 byte (0xC9) followed by ASCII | Verify reports invalid-UTF-8 ERROR (parse-line has Latin-1 fallback, but verify doesn't apply it — intentional) |
| F12 | Empty body line | Parses OK, no error |
| F14 | File replaced under us (mtime + inode change) | `ff_state_ensure_fresh` triggers full rebuild; `total_lines` updates |
| F15 | Empty file (0 bytes) | Verify reports INFO "empty" |
Latent gaps surfaced:
- **F1**: `ff_readline` sets `*truncated=TRUE` but verify doesn't surface this — partial writes go unflagged.
- **F9**: parser silently truncates body at first unescaped `: ` — valid manual edits with embedded `: ` are silently corrupted at read-time.
## Baseline & regression checking (P4)
```sh
# Capture a fresh CSV
make bench-full BENCH_CSV=tests/bench/current.csv
# Compare against baseline.csv (committed)
make bench-compare BENCH_CSV=tests/bench/current.csv
# Once you've reviewed the numbers and they're healthy, snapshot:
make bench-update-baseline BENCH_CSV=tests/bench/current.csv
```
`compare_baseline.py` aggregates by `(scenario, volume)` and uses median across
duplicate rows. Regression threshold defaults to ±25 %; override with
`BENCH_THRESHOLD=15`. Exit code is 0 on no regressions, 1 if any scenario
exceeds the threshold.
The committed `baseline.csv` was captured on this hardware (Debian-bookworm
container, NVMe). It's a reference, not absolute — run `make bench-full` and
compare on your own hardware to track regressions over time.
## Generator (`gen_history`) options
```
--lines=N total lines (default 10000)
--contacts=K distinct contact JIDs (default 1)
--years=Y year span (default 1)
--seed=S RNG seed (default 42)
--stanza-id={uuid|libpurple|conversations|mixed}
--lmc-rate=PCT 0..50, default 3
--mam-ooo-rate=PCT 0..50, default 0
--resources-per-contact=R default 3
--msg-len-profile={short|mixed|long|extreme}
--output=DIR default /tmp/cproof-bench-corpus
--quiet
```
Layout produced (matches `files_get_data_path("flatlog")` + `ff_jid_to_dir`,
so `ff_verify_integrity` can walk the tree without changes):
```
$BENCH_DATA_DIR/
flatlog/
bench_at_bench.example/ # ff_jid_to_dir(--account)
buddy000_at_bench.example/history.log
buddy001_at_bench.example/history.log
...
manifest.txt
```
## Determinism
Same `--seed` and same `--lines` etc. → byte-identical corpus. Stanza-ids,
timestamps, body content all derive from `xorshift64(seed)`.
The bench runner does *not* re-generate the corpus — `make bench` runs
`gen_history` once into `$BENCH_DATA_DIR`, then runs `bench_runner` on it.
Re-runs reuse the corpus unless you `make bench-clean` or change `BENCH_VOLUME`.
## Tuning
- `BENCH_DATA_DIR=/path` — override corpus location (default `/tmp/cproof-bench-corpus`)
- `BENCH_CSV=/path/out.csv` — override results CSV
- `BENCH_LOG=1` — surface the flatfile backend's `log_*` output to stderr
- `BENCH_VOLUME=small|medium|max`
## Known limitations
- **Tail-access** is simulated by sparse-index lookup + read-forward, not the
full `_flatfile_get_previous_chat` (which is unreachable here without
pulling in profanity's xmpp/connection layer). This still measures the
expensive parts (state build, index seek, line parse, LMC application).
- **S6 verify** now calls the real `ff_verify_integrity` and walks the
canonical layout. CSV note format: `total=N err=N warn=N info=N`. S11 OOO
flags ~17 500 timestamp-out-of-order warnings on the 100k/20% corpus.
(Resolved as of P2.5.)
- **S7/S8 export/import** wired up in P5 — the bench links real
`database_export.c` + `database_sqlite.c` + `database.c` and drives
`log_database_export_to_flatfile` / `log_database_import_from_flatfile`
end-to-end. See "Export/import pipeline (S7/S8)" above.
- No baseline-comparison script yet (P4).
- F1F15 failure injection not yet implemented (P3).
## Disk-usage warning
Variant corpora can be large because the default `mixed` length profile
includes a small fraction of paste-bomb (5100 KB) and extreme (100 KB1 MB)
messages. Examples:
| Target | Lines | On-disk size | Note |
|---|---|---|---|
| `bench-quick` | 10 000 | ~40 MB | due to ~50 paste-bombs in mixed profile |
| `bench BENCH_VOLUME=medium` | 500 000 | ~2 GB | check `df` first |
| `bench BENCH_VOLUME=max` | 5 000 000 | ~20 GB | NVMe + plenty of free disk |
| `bench-multicontact` | 1 000 000 (200×5k) | ~4 GB | 200 separate files |
| `bench-lmc` | 100 000 | ~400 MB | single-file |
| `bench-ooo` | 100 000 | ~400 MB | single-file |
| `bench-pipeline` | 100 000 | ~80 MB SQLite + ~50 MB flatfile | export/import |
| `bench-pipeline-max` | 1 000 000 | ~800 MB SQLite + ~500 MB flatfile + ~800 MB DB-B | heavy |
Set `BENCH_DATA_DIR` if `/tmp` is too small.
## Adding new scenarios
1. Add a `run_*` function in `bench_runner.c`.
2. Wire it into `main()` with a `scenario_enabled("Sx")` check.
3. Document it in this README + REVIEW.txt Phase 9 plan.
4. Run `make bench-full BENCH_CSV=tests/bench/current.csv && make bench-compare`
— verify no unexpected regressions surface elsewhere.
## File map
```
tests/bench/
├── README.md # this file
├── baseline.csv # committed reference numbers
├── compare_baseline.py # diff current.csv vs baseline.csv
├── gen_history.c # corpus generator (P1 + S9/S10/S11)
├── bench_runner.c # S1S6 driver
├── bench_long_messages.c # L1L14 driver
├── bench_failure_modes.c # F1F15 driver
├── bench_export_import.c # S7/S8 driver (seed/export/import/roundtrip/verify)
├── bench_stubs.c # link-time stubs (log_*, prefs_*, etc.)
├── bench_common.c/.h # timing, RSS, formatting helpers
└── bench_csv.c/.h # CSV-row writer
```

51
tests/bench/baseline.csv Normal file
View File

@@ -0,0 +1,51 @@
scenario,volume,bytes,lines,wall_ms,peak_rss_kb,note
S1_cold_tail,small,39494716,10002,46.040,14408,tail page=100
S2_warm_tail,small,39494716,10002,33.878,14716,tail page=100
S3_deep_pagination,small,39494716,10002,1.024,14716,n_pages=1000
S4_first_build,small,39494716,10002,39.139,14716,idx_entries=20
S5_incremental_extend,small,104411,1000,0.859,14716,appended=1000_lines
S6_verify,small,39494716,10002,396.022,16424,total=1 err=0 warn=1 info=0
L1,longmsg,110586,100,1.486,9432,n=100 body=1024 write=0.8ms read=0.6ms parsed=100 mismatch=0 parse_fail=0 1KB body x100
L2,longmsg,1032186,100,10.468,9432,n=100 body=10240 write=5.7ms read=4.8ms parsed=100 mismatch=0 parse_fail=0 10KB body x100
L3,longmsg,10248186,100,100.337,9816,n=100 body=102400 write=57.3ms read=43.1ms parsed=100 mismatch=0 parse_fail=0 100KB body x100
L4,longmsg,52432936,50,502.785,13608,n=50 body=1048576 write=264.5ms read=238.3ms parsed=50 mismatch=0 parse_fail=0 1MB body x50
L5,longmsg,52429696,10,518.558,27764,n=10 body=5242880 write=264.4ms read=254.2ms parsed=10 mismatch=0 parse_fail=0 5MB body x10
L6,longmsg,41435552,4,407.803,45852,n=4 body=10358784 write=211.3ms read=196.5ms parsed=4 mismatch=0 parse_fail=0 9.9MB body x4 (just under FF_MAX_LINE_LEN)
L7,longmsg,10485981,2,3.750,45852,oversize=10485761_rejected=yes read=3.8ms parsed=1
L8,longmsg,5175286,50,45.549,45852,n=50 body=102400 write=26.6ms read=18.9ms parsed=50 mismatch=0 parse_fail=0 100KB body w/ \n every 100B
L9,longmsg,5124136,50,45.487,45852,n=50 body=102400 write=24.5ms read=21.0ms parsed=50 mismatch=0 parse_fail=0 100KB body w/ pipes
L10,longmsg,5124136,50,43.254,45852,n=50 body=102400 write=18.9ms read=24.4ms parsed=50 mismatch=0 parse_fail=0 100KB body utf-8 emoji
L11,longmsg,110586,100,1.284,45852,n=100 body=1024 write=0.7ms read=0.6ms parsed=100 mismatch=0 parse_fail=0 sanity baseline
L12,longmsg,5415366,1000,26.437,45852,5x1MB_in_last_100 parsed=1000
L13,longmsg,104865786,100,932.630,45852,n=100 body=1048576 write=485.7ms read=446.9ms parsed=100 mismatch=0 parse_fail=0 verify-equiv full parse on 100x1MB
L14,longmsg,102481986,1000,962.342,45852,n=1000 body=102400 write=510.4ms read=452.0ms parsed=1000 mismatch=0 parse_fail=0 rapid append 1000x100KB
F1,fail-pass,904,0,0.118,9540,issues total=1 err=0 warn=1 (partial-line tail; expect graceful handle)
F2,fail-pass,1119,0,0.113,9540,warnings=2 (expect CRLF warning) first_warn=File permissions are 644 expected 600 (sensitive data)
F3,fail-pass,341,0,0.072,9540,errors=1 warnings=1 (expect 1 unparsable line for mid-file BOM) first=Unparsable line
F7,fail-pass,527,0,0.082,9540,issues total=1 err=0 (cycle handled at read-time not verify)
F8,fail-pass,19044,0,0.981,9540,201 lines (1 orig + 200 corrections) errors=0 warns=1
F9,fail-pass,341,0,0.069,9540,errors=0 (parser splits at first ': ' — not flagged but body truncated)
F10,fail-pass,176,0,0.060,9540,errors=0 warnings=1 (RTL/ZWSP preserved literally)
F11,fail-pass,323,0,0.264,9540,errors=1 (Latin-1 byte: error or fallback OK)
F12,fail-pass,330,0,0.071,9540,errors=0 (empty body OK)
F14,fail-pass,23236,0,0.914,9540,v1_lines=100 v2_lines=250 (expect rebuild detected and 250 lines)
F15,fail-pass,0,0,0.121,9540,errors=0 infos=1 warnings=1 (empty file should yield INFO)
S1_cold_tail,lmc,389183687,100002,480.428,43456,tail page=100
S2_warm_tail,lmc,389183687,100002,477.482,43456,tail page=100
S3_deep_pagination,lmc,389183687,100002,1.066,43456,n_pages=1000
S4_first_build,lmc,389183687,100002,473.604,43456,idx_entries=200
S5_incremental_extend,lmc,104411,1000,0.902,43456,appended=1000_lines
S6_verify,lmc,389183687,100002,3892.137,43456,total=1 err=0 warn=1 info=0
S6_verify,ooo,397586720,100002,4037.058,33336,total=17573 err=0 warn=17573 info=0
S7a_export_cold,pipe100000,40714240,100000,1231.613,148128,exported=100000 db_rows=100000
S7b_export_dedup,pipe100000,40714240,100000,1484.406,199460,exported=0 db_rows=100000
S7_seed_export,pipe100000,40714240,100000,1238.138,148208,exported=100000 db_rows=100000
S8a_import_cold,pipe100000,40628224,100000,3237.481,123108,imported=100000 rows_before=0 rows_after=100000
S8b_import_idempotent,pipe100000,40628224,0,1047.286,194860,imported=0 rows_before=100000 rows_after=100000
S8e_roundtrip,pipe100000,40521728,100000,6520.475,148184,rows=100000 seed=1591ms export=1237ms import=3376ms diff=317ms exported=100000 imported=100000 rows_a=100000 rows_b=100000 mismatches=0 body=0 lmc=0
S7a_export_cold,pipe1000000,407732224,1000000,11247.107,1376772,exported=1000000 db_rows=1000000
S7b_export_dedup,pipe1000000,407732224,1000000,13262.643,1873220,exported=0 db_rows=1000000
S7_seed_export,pipe1000000,407732224,1000000,12125.624,1377076,exported=1000000 db_rows=1000000
S8a_import_cold,pipe1000000,406605824,1000000,36877.801,1124804,imported=1000000 rows_before=0 rows_after=1000000
S8b_import_idempotent,pipe1000000,406605824,0,11986.786,1841488,imported=0 rows_before=1000000 rows_after=1000000
S8e_roundtrip,pipe1000000,405757952,1000000,66449.762,1377084,rows=1000000 seed=20012ms export=11939ms import=31533ms diff=2966ms exported=1000000 imported=1000000 rows_a=1000000 rows_b=1000000 mismatches=0 body=0 lmc=0
1 scenario volume bytes lines wall_ms peak_rss_kb note
2 S1_cold_tail small 39494716 10002 46.040 14408 tail page=100
3 S2_warm_tail small 39494716 10002 33.878 14716 tail page=100
4 S3_deep_pagination small 39494716 10002 1.024 14716 n_pages=1000
5 S4_first_build small 39494716 10002 39.139 14716 idx_entries=20
6 S5_incremental_extend small 104411 1000 0.859 14716 appended=1000_lines
7 S6_verify small 39494716 10002 396.022 16424 total=1 err=0 warn=1 info=0
8 L1 longmsg 110586 100 1.486 9432 n=100 body=1024 write=0.8ms read=0.6ms parsed=100 mismatch=0 parse_fail=0 1KB body x100
9 L2 longmsg 1032186 100 10.468 9432 n=100 body=10240 write=5.7ms read=4.8ms parsed=100 mismatch=0 parse_fail=0 10KB body x100
10 L3 longmsg 10248186 100 100.337 9816 n=100 body=102400 write=57.3ms read=43.1ms parsed=100 mismatch=0 parse_fail=0 100KB body x100
11 L4 longmsg 52432936 50 502.785 13608 n=50 body=1048576 write=264.5ms read=238.3ms parsed=50 mismatch=0 parse_fail=0 1MB body x50
12 L5 longmsg 52429696 10 518.558 27764 n=10 body=5242880 write=264.4ms read=254.2ms parsed=10 mismatch=0 parse_fail=0 5MB body x10
13 L6 longmsg 41435552 4 407.803 45852 n=4 body=10358784 write=211.3ms read=196.5ms parsed=4 mismatch=0 parse_fail=0 9.9MB body x4 (just under FF_MAX_LINE_LEN)
14 L7 longmsg 10485981 2 3.750 45852 oversize=10485761_rejected=yes read=3.8ms parsed=1
15 L8 longmsg 5175286 50 45.549 45852 n=50 body=102400 write=26.6ms read=18.9ms parsed=50 mismatch=0 parse_fail=0 100KB body w/ \n every 100B
16 L9 longmsg 5124136 50 45.487 45852 n=50 body=102400 write=24.5ms read=21.0ms parsed=50 mismatch=0 parse_fail=0 100KB body w/ pipes
17 L10 longmsg 5124136 50 43.254 45852 n=50 body=102400 write=18.9ms read=24.4ms parsed=50 mismatch=0 parse_fail=0 100KB body utf-8 emoji
18 L11 longmsg 110586 100 1.284 45852 n=100 body=1024 write=0.7ms read=0.6ms parsed=100 mismatch=0 parse_fail=0 sanity baseline
19 L12 longmsg 5415366 1000 26.437 45852 5x1MB_in_last_100 parsed=1000
20 L13 longmsg 104865786 100 932.630 45852 n=100 body=1048576 write=485.7ms read=446.9ms parsed=100 mismatch=0 parse_fail=0 verify-equiv full parse on 100x1MB
21 L14 longmsg 102481986 1000 962.342 45852 n=1000 body=102400 write=510.4ms read=452.0ms parsed=1000 mismatch=0 parse_fail=0 rapid append 1000x100KB
22 F1 fail-pass 904 0 0.118 9540 issues total=1 err=0 warn=1 (partial-line tail; expect graceful handle)
23 F2 fail-pass 1119 0 0.113 9540 warnings=2 (expect CRLF warning) first_warn=File permissions are 644 expected 600 (sensitive data)
24 F3 fail-pass 341 0 0.072 9540 errors=1 warnings=1 (expect 1 unparsable line for mid-file BOM) first=Unparsable line
25 F7 fail-pass 527 0 0.082 9540 issues total=1 err=0 (cycle handled at read-time not verify)
26 F8 fail-pass 19044 0 0.981 9540 201 lines (1 orig + 200 corrections) errors=0 warns=1
27 F9 fail-pass 341 0 0.069 9540 errors=0 (parser splits at first ': ' — not flagged but body truncated)
28 F10 fail-pass 176 0 0.060 9540 errors=0 warnings=1 (RTL/ZWSP preserved literally)
29 F11 fail-pass 323 0 0.264 9540 errors=1 (Latin-1 byte: error or fallback OK)
30 F12 fail-pass 330 0 0.071 9540 errors=0 (empty body OK)
31 F14 fail-pass 23236 0 0.914 9540 v1_lines=100 v2_lines=250 (expect rebuild detected and 250 lines)
32 F15 fail-pass 0 0 0.121 9540 errors=0 infos=1 warnings=1 (empty file should yield INFO)
33 S1_cold_tail lmc 389183687 100002 480.428 43456 tail page=100
34 S2_warm_tail lmc 389183687 100002 477.482 43456 tail page=100
35 S3_deep_pagination lmc 389183687 100002 1.066 43456 n_pages=1000
36 S4_first_build lmc 389183687 100002 473.604 43456 idx_entries=200
37 S5_incremental_extend lmc 104411 1000 0.902 43456 appended=1000_lines
38 S6_verify lmc 389183687 100002 3892.137 43456 total=1 err=0 warn=1 info=0
39 S6_verify ooo 397586720 100002 4037.058 33336 total=17573 err=0 warn=17573 info=0
40 S7a_export_cold pipe100000 40714240 100000 1231.613 148128 exported=100000 db_rows=100000
41 S7b_export_dedup pipe100000 40714240 100000 1484.406 199460 exported=0 db_rows=100000
42 S7_seed_export pipe100000 40714240 100000 1238.138 148208 exported=100000 db_rows=100000
43 S8a_import_cold pipe100000 40628224 100000 3237.481 123108 imported=100000 rows_before=0 rows_after=100000
44 S8b_import_idempotent pipe100000 40628224 0 1047.286 194860 imported=0 rows_before=100000 rows_after=100000
45 S8e_roundtrip pipe100000 40521728 100000 6520.475 148184 rows=100000 seed=1591ms export=1237ms import=3376ms diff=317ms exported=100000 imported=100000 rows_a=100000 rows_b=100000 mismatches=0 body=0 lmc=0
46 S7a_export_cold pipe1000000 407732224 1000000 11247.107 1376772 exported=1000000 db_rows=1000000
47 S7b_export_dedup pipe1000000 407732224 1000000 13262.643 1873220 exported=0 db_rows=1000000
48 S7_seed_export pipe1000000 407732224 1000000 12125.624 1377076 exported=1000000 db_rows=1000000
49 S8a_import_cold pipe1000000 406605824 1000000 36877.801 1124804 imported=1000000 rows_before=0 rows_after=1000000
50 S8b_import_idempotent pipe1000000 406605824 0 11986.786 1841488 imported=0 rows_before=1000000 rows_after=1000000
51 S8e_roundtrip pipe1000000 405757952 1000000 66449.762 1377084 rows=1000000 seed=20012ms export=11939ms import=31533ms diff=2966ms exported=1000000 imported=1000000 rows_a=1000000 rows_b=1000000 mismatches=0 body=0 lmc=0

135
tests/bench/bench_common.c Normal file
View File

@@ -0,0 +1,135 @@
// 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.
#include "bench_common.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
double
bench_now_ms(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1.0e6;
}
char*
bench_fmt_bytes(uint64_t bytes)
{
static const char* units[] = { "B", "KB", "MB", "GB", "TB" };
int u = 0;
double v = (double)bytes;
while (v >= 1024.0 && u < 4) {
v /= 1024.0;
u++;
}
if (u == 0)
return g_strdup_printf("%" G_GUINT64_FORMAT " %s", bytes, units[u]);
return g_strdup_printf("%.2f %s", v, units[u]);
}
char*
bench_fmt_ms(double ms)
{
if (ms < 1.0)
return g_strdup_printf("%.0f us", ms * 1000.0);
if (ms < 1000.0)
return g_strdup_printf("%.1f ms", ms);
if (ms < 60000.0)
return g_strdup_printf("%.2f s", ms / 1000.0);
return g_strdup_printf("%.1f min", ms / 60000.0);
}
bench_volume_t
bench_volume_from_env(void)
{
const char* v = getenv("BENCH_VOLUME");
if (!v || !v[0])
return BENCH_VOLUME_MEDIUM;
if (g_ascii_strcasecmp(v, "small") == 0)
return BENCH_VOLUME_SMALL;
if (g_ascii_strcasecmp(v, "medium") == 0)
return BENCH_VOLUME_MEDIUM;
if (g_ascii_strcasecmp(v, "max") == 0)
return BENCH_VOLUME_MAX;
fprintf(stderr, "WARN: unknown BENCH_VOLUME='%s', defaulting to medium\n", v);
return BENCH_VOLUME_MEDIUM;
}
const char*
bench_volume_name(bench_volume_t v)
{
switch (v) {
case BENCH_VOLUME_SMALL:
return "small";
case BENCH_VOLUME_MEDIUM:
return "medium";
case BENCH_VOLUME_MAX:
return "max";
}
return "?";
}
gboolean
bench_drop_page_cache(const char* path)
{
int fd = open(path, O_RDONLY);
if (fd < 0)
return FALSE;
#ifdef POSIX_FADV_DONTNEED
// Best-effort: kernel may ignore but typically honours.
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
#endif
close(fd);
return TRUE;
}
int64_t
bench_fs_free_bytes(const char* path)
{
struct statvfs vfs;
if (statvfs(path, &vfs) != 0)
return -1;
return (int64_t)vfs.f_bavail * (int64_t)vfs.f_frsize;
}
long
bench_peak_rss_kb(void)
{
struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) != 0)
return -1;
return ru.ru_maxrss; // already KB on Linux
}
double
bench_run_best_of(int runs, bench_fn_t fn, void* arg, long* peak_rss_kb)
{
if (runs < 1)
runs = 1;
double best = 0.0;
long max_rss = 0;
for (int i = 0; i < runs; i++) {
double t0 = bench_now_ms();
fn(arg);
double dt = bench_now_ms() - t0;
if (i == 0 || dt < best)
best = dt;
long rss = bench_peak_rss_kb();
if (rss > max_rss)
max_rss = rss;
}
if (peak_rss_kb)
*peak_rss_kb = max_rss;
return best;
}

View File

@@ -0,0 +1,52 @@
// 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.
#ifndef BENCH_COMMON_H
#define BENCH_COMMON_H
#include <stdint.h>
#include <stddef.h>
#include <sys/resource.h>
#include <glib.h>
// Wall-clock monotonic milliseconds since some epoch. Use for elapsed-time
// arithmetic only (subtract two values).
double bench_now_ms(void);
// Format a byte count as "1.23 GB" / "500 MB" / "42 KB" / "9 B".
// Returns a freshly allocated string; caller must g_free.
char* bench_fmt_bytes(uint64_t bytes);
// Format a duration in milliseconds as a human-friendly string.
char* bench_fmt_ms(double ms);
// Resolve BENCH_VOLUME env: "small" / "medium" / "max" / unset.
typedef enum {
BENCH_VOLUME_SMALL, // 10k lines / 1 contact / 1 year
BENCH_VOLUME_MEDIUM, // 500k lines / 1 contact / 5 years
BENCH_VOLUME_MAX, // 5M lines / 1 contact / 10 years
} bench_volume_t;
bench_volume_t bench_volume_from_env(void);
const char* bench_volume_name(bench_volume_t v);
// Drop OS page cache for a path (so a subsequent read measures cold I/O).
// On Linux this is best-effort: we open the file, posix_fadvise(DONTNEED).
// Returns TRUE on success.
gboolean bench_drop_page_cache(const char* path);
// Available bytes in the filesystem hosting `path`. Returns -1 on failure.
int64_t bench_fs_free_bytes(const char* path);
// Best-of-N timing: run `fn(arg)` `runs` times, return the minimum elapsed-ms.
// Optionally records peak RSS over runs into *peak_rss_kb.
typedef void (*bench_fn_t)(void* arg);
double bench_run_best_of(int runs, bench_fn_t fn, void* arg, long* peak_rss_kb);
// Get current peak RSS in KB via getrusage().
long bench_peak_rss_kb(void);
#endif

59
tests/bench/bench_csv.c Normal file
View File

@@ -0,0 +1,59 @@
// 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.
#include "config.h"
#include "bench_csv.h"
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <glib.h>
#include "common.h"
static char*
_csv_clean(const char* in)
{
if (!in)
return g_strdup("");
GString* s = g_string_sized_new(strlen(in));
for (const char* p = in; *p; p++) {
if (*p == ',' || *p == '\n' || *p == '\r')
g_string_append_c(s, ' ');
else
g_string_append_c(s, *p);
}
return g_string_free(s, FALSE);
}
void
bench_csv_append(const char* path,
const char* scenario,
const char* volume,
uint64_t bytes,
uint64_t lines,
double wall_ms,
long peak_rss_kb,
const char* note)
{
if (!path)
return;
struct stat st;
int fresh = (stat(path, &st) != 0 || st.st_size == 0);
FILE* fp = fopen(path, "a");
if (!fp)
return;
if (fresh) {
fprintf(fp, "scenario,volume,bytes,lines,wall_ms,peak_rss_kb,note\n");
}
auto_gchar gchar* sc = _csv_clean(scenario);
auto_gchar gchar* vol = _csv_clean(volume);
auto_gchar gchar* nt = _csv_clean(note);
fprintf(fp, "%s,%s,%" G_GUINT64_FORMAT ",%" G_GUINT64_FORMAT ",%.3f,%ld,%s\n",
sc, vol, bytes, lines, wall_ms, peak_rss_kb, nt);
fclose(fp);
}

26
tests/bench/bench_csv.h Normal file
View File

@@ -0,0 +1,26 @@
// 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.
#ifndef BENCH_CSV_H
#define BENCH_CSV_H
#include <stdint.h>
#include <stdio.h>
// Append a single bench row to `path`. Creates the file with a header on
// first call. Format:
// scenario,volume,bytes,lines,wall_ms,peak_rss_kb,note
// All commas/newlines in `scenario` and `note` are stripped to keep the CSV
// trivially parseable.
void bench_csv_append(const char* path,
const char* scenario,
const char* volume,
uint64_t bytes,
uint64_t lines,
double wall_ms,
long peak_rss_kb,
const char* note);
#endif

View File

@@ -0,0 +1,707 @@
// 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.
/*
* bench_export_import.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* S7/S8 export + import bench harness.
*
* Subcommands:
* seed --db=PATH --rows=N [--profile=mixed] [--lmc-rate=PCT]
* export --account=JID --csv=PATH [--label=TAG]
* import --account=JID --csv=PATH [--label=TAG]
* roundtrip --rows=N --csv=PATH # seed → export → fresh DB → import → full diff
* verify --db-a=PATH --db-b=PATH # full byte-by-byte content diff of two SQLite DBs
*
* Layout (under $BENCH_DATA_DIR, default /tmp/cproof-bench-export):
* database/<account_jid_dir>/chatlog.db
* flatlog/<account_jid_dir>/<contact_jid_dir>/history.log
*
* Seeding writes directly to SQLite via a separate sqlite3 handle (faster than
* dispatching through the backend's add_incoming for each row). Export/import
* uses the *real* log_database_export_to_flatfile / _import_from_flatfile.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <sqlite3.h>
#include "bench_common.h"
#include "bench_csv.h"
#include "config/account.h"
#include "database.h"
#include "database_flatfile.h"
// ---------------------------------------------------------------------------
// Tiny RNG / body helpers — duplicated from gen_history (kept local so this
// binary is independent of gen_history layout).
static uint64_t
xrng(uint64_t* s)
{
uint64_t x = *s;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
*s = x;
return x;
}
static char*
make_uuid(uint64_t* rng)
{
uint64_t a = xrng(rng), b = xrng(rng);
return g_strdup_printf("%08x-%04x-4%03x-%04x-%012lx",
(unsigned)(a >> 32),
(unsigned)((a >> 16) & 0xffff),
(unsigned)(a & 0xfff),
(unsigned)((b >> 48) & 0xffff) | 0x8000,
(unsigned long)(b & 0xffffffffffffUL));
}
static char*
make_body(uint64_t* rng, size_t target)
{
char* buf = g_malloc(target + 1);
static const char alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789 ";
static const int alpha_n = sizeof(alpha) - 1;
for (size_t i = 0; i < target; i++)
buf[i] = alpha[xrng(rng) % alpha_n];
buf[target] = '\0';
return buf;
}
// ---------------------------------------------------------------------------
// Account dir helpers
static char*
db_path_for(const char* account_jid)
{
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench-export";
auto_gchar gchar* jid_dir = ff_jid_to_dir(account_jid);
auto_gchar gchar* parent = g_strdup_printf("%s/database/%s", base, jid_dir);
g_mkdir_with_parents(parent, 0755);
return g_strdup_printf("%s/chatlog.db", parent);
}
// ---------------------------------------------------------------------------
// Schema (lifted from database_sqlite.c so the seeder can populate without
// going through _sqlite_init's full machinery)
static const char* SCHEMA_DDL = "CREATE TABLE IF NOT EXISTS `ChatLogs` ("
"`id` INTEGER PRIMARY KEY AUTOINCREMENT, "
"`from_jid` TEXT NOT NULL, "
"`to_jid` TEXT NOT NULL, "
"`from_resource` TEXT, `to_resource` TEXT, "
"`message` TEXT, `timestamp` TEXT, `type` TEXT, "
"`stanza_id` TEXT, `archive_id` TEXT, "
"`encryption` TEXT, `marked_read` INTEGER, "
"`replace_id` TEXT, "
"`replaces_db_id` INTEGER, `replaced_by_db_id` INTEGER);"
"CREATE TABLE IF NOT EXISTS `DbVersion` ("
"`dv_id` INTEGER PRIMARY KEY, `version` INTEGER UNIQUE);"
"INSERT OR IGNORE INTO `DbVersion` (`version`) VALUES ('2');"
"CREATE INDEX IF NOT EXISTS ChatLogs_timestamp_IDX ON `ChatLogs` (`timestamp`);"
"CREATE INDEX IF NOT EXISTS ChatLogs_to_from_jid_IDX ON `ChatLogs` (`to_jid`, `from_jid`);"
"CREATE TRIGGER IF NOT EXISTS update_corrected_message "
"AFTER INSERT ON ChatLogs FOR EACH ROW WHEN NEW.replaces_db_id IS NOT NULL "
"BEGIN UPDATE ChatLogs SET replaced_by_db_id = NEW.id "
"WHERE id = NEW.replaces_db_id; END;";
// ---------------------------------------------------------------------------
// Seed: open sqlite3 directly, build schema, batched-INSERT N rows.
typedef struct
{
const char* db_path;
int64_t rows;
int contacts;
int lmc_pct;
size_t avg_body;
uint64_t seed;
const char* account_jid;
} seed_opts_t;
static int
do_seed(const seed_opts_t* o)
{
sqlite3* db = NULL;
if (sqlite3_open(o->db_path, &db) != SQLITE_OK) {
fprintf(stderr, "seed: cannot open %s: %s\n", o->db_path, sqlite3_errmsg(db));
return 2;
}
char* err = NULL;
if (sqlite3_exec(db, SCHEMA_DDL, NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, "seed: schema failed: %s\n", err);
sqlite3_free(err);
sqlite3_close(db);
return 2;
}
sqlite3_exec(db, "PRAGMA journal_mode=WAL;", NULL, NULL, NULL);
sqlite3_exec(db, "PRAGMA synchronous=NORMAL;", NULL, NULL, NULL);
if (sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, "seed: BEGIN failed: %s\n", err);
sqlite3_free(err);
sqlite3_close(db);
return 2;
}
const char* INSERT_SQL = "INSERT INTO ChatLogs ("
"from_jid, to_jid, from_resource, to_resource, message, timestamp, type, "
"stanza_id, archive_id, encryption, marked_read, replace_id"
") VALUES (?, ?, ?, ?, ?, ?, 'chat', ?, ?, 'none', -1, ?);";
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db, INSERT_SQL, -1, &stmt, NULL) != SQLITE_OK) {
fprintf(stderr, "seed: prepare failed: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 2;
}
uint64_t rng = o->seed ? o->seed : 1;
GDateTime* base = g_date_time_new_utc(2020, 1, 1, 0, 0, 0.0);
int64_t span_secs = (int64_t)5 * 365 * 24 * 3600; // 5 years
char* prev_sid_per_contact[1024] = { 0 };
int n_contacts = o->contacts > 0 ? o->contacts : 1;
if (n_contacts > 1024)
n_contacts = 1024;
double t0 = bench_now_ms();
for (int64_t i = 0; i < o->rows; i++) {
int ci = (int)(i % n_contacts);
auto_gchar gchar* contact_jid = g_strdup_printf("buddy%03d@bench.example", ci);
gboolean is_lmc = prev_sid_per_contact[ci] != NULL && o->lmc_pct > 0
&& (int)(xrng(&rng) % 100) < o->lmc_pct;
int64_t off_secs = (int64_t)((double)i / (double)o->rows * (double)span_secs);
GDateTime* ts = g_date_time_add_seconds(base, off_secs);
auto_gchar gchar* iso = g_date_time_format_iso8601(ts);
g_date_time_unref(ts);
auto_gchar gchar* sid = make_uuid(&rng);
auto_gchar gchar* aid = (xrng(&rng) & 1) ? make_uuid(&rng) : NULL;
size_t body_len = o->avg_body > 0 ? o->avg_body : 50 + (xrng(&rng) % 200);
auto_gchar gchar* body = make_body(&rng, body_len);
auto_gchar gchar* res = g_strdup_printf("res-%d", (int)(xrng(&rng) % 3));
sqlite3_bind_text(stmt, 1, contact_jid, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, o->account_jid, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, res, -1, SQLITE_TRANSIENT);
sqlite3_bind_null(stmt, 4); // to_resource
sqlite3_bind_text(stmt, 5, body, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 6, iso, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 7, sid, -1, SQLITE_TRANSIENT);
if (aid)
sqlite3_bind_text(stmt, 8, aid, -1, SQLITE_TRANSIENT);
else
sqlite3_bind_null(stmt, 8);
if (is_lmc)
sqlite3_bind_text(stmt, 9, prev_sid_per_contact[ci], -1, SQLITE_TRANSIENT);
else
sqlite3_bind_null(stmt, 9);
if (sqlite3_step(stmt) != SQLITE_DONE) {
fprintf(stderr, "seed: insert failed at row %" PRId64 ": %s\n",
i, sqlite3_errmsg(db));
sqlite3_finalize(stmt);
sqlite3_close(db);
return 2;
}
sqlite3_reset(stmt);
if (!is_lmc) {
g_free(prev_sid_per_contact[ci]);
prev_sid_per_contact[ci] = g_strdup(sid);
}
if (((i + 1) % 100000) == 0) {
double dt = bench_now_ms() - t0;
fprintf(stderr, " seeded %" PRId64 " / %" PRId64 " (%.1fs, %.0f rows/s)\n",
i + 1, o->rows, dt / 1000.0, (double)(i + 1) / (dt / 1000.0 + 1e-9));
}
}
sqlite3_finalize(stmt);
if (sqlite3_exec(db, "COMMIT;", NULL, NULL, &err) != SQLITE_OK) {
fprintf(stderr, "seed: COMMIT failed: %s\n", err);
sqlite3_free(err);
}
sqlite3_close(db);
g_date_time_unref(base);
for (int i = 0; i < n_contacts; i++)
g_free(prev_sid_per_contact[i]);
double dt = bench_now_ms() - t0;
fprintf(stderr, "seed: done %" PRId64 " rows in %.2fs\n", o->rows, dt / 1000.0);
return 0;
}
// ---------------------------------------------------------------------------
// Init dispatcher (sets active_db_backend = sqlite, runs _sqlite_init)
static gboolean
init_sqlite_backend(const char* account_jid)
{
setenv("BENCH_ACCOUNT_JID", account_jid, 1);
db_backend_t* be = db_backend_sqlite();
if (!be) {
fprintf(stderr, "init: db_backend_sqlite() returned NULL\n");
return FALSE;
}
active_db_backend = be;
ProfAccount fake = { 0 };
fake.jid = (gchar*)account_jid;
if (!be->init(&fake)) {
fprintf(stderr, "init: backend init failed\n");
return FALSE;
}
return TRUE;
}
static void
close_sqlite_backend(void)
{
if (active_db_backend && active_db_backend->close)
active_db_backend->close();
active_db_backend = NULL;
}
// ---------------------------------------------------------------------------
// SQLite row count
static int64_t
db_row_count(const char* db_path)
{
sqlite3* db = NULL;
if (sqlite3_open(db_path, &db) != SQLITE_OK)
return -1;
int64_t n = -1;
sqlite3_stmt* stmt = NULL;
if (sqlite3_prepare_v2(db, "SELECT COUNT(*) FROM ChatLogs;", -1, &stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW)
n = sqlite3_column_int64(stmt, 0);
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return n;
}
// Full content diff: dump every ChatLogs row from both DBs ordered by
// (timestamp, stanza_id), compare tuple-wise. Returns count of mismatches.
// Tuples compared: (from_jid, to_jid, message, timestamp, type, stanza_id,
// archive_id, encryption, replace_id).
// marked_read intentionally excluded — it's not faithfully roundtripped
// (export reads -1/null from SQLite, flatfile may emit it differently).
static int64_t
db_diff_full(const char* a_path, const char* b_path, int64_t* total_a, int64_t* total_b)
{
sqlite3* da = NULL;
sqlite3* db = NULL;
if (sqlite3_open(a_path, &da) != SQLITE_OK)
return -1;
if (sqlite3_open(b_path, &db) != SQLITE_OK) {
sqlite3_close(da);
return -1;
}
const char* SQL = "SELECT IFNULL(from_jid,''), IFNULL(to_jid,''), IFNULL(message,''), "
"IFNULL(timestamp,''), IFNULL(type,''), IFNULL(stanza_id,''), "
"IFNULL(archive_id,''), IFNULL(encryption,''), IFNULL(replace_id,'') "
"FROM ChatLogs ORDER BY timestamp ASC, stanza_id ASC;";
sqlite3_stmt* sa = NULL;
sqlite3_stmt* sb = NULL;
if (sqlite3_prepare_v2(da, SQL, -1, &sa, NULL) != SQLITE_OK
|| sqlite3_prepare_v2(db, SQL, -1, &sb, NULL) != SQLITE_OK) {
if (sa)
sqlite3_finalize(sa);
if (sb)
sqlite3_finalize(sb);
sqlite3_close(da);
sqlite3_close(db);
return -1;
}
int64_t na = 0, nb = 0, mismatches = 0;
int shown = 0;
while (1) {
int ra = sqlite3_step(sa);
int rb = sqlite3_step(sb);
if (ra != SQLITE_ROW && rb != SQLITE_ROW)
break;
if (ra == SQLITE_ROW)
na++;
if (rb == SQLITE_ROW)
nb++;
if (ra != rb) {
mismatches++;
if (shown < 5) {
fprintf(stderr, " diff: row count drift at ra_row=%" PRId64 " rb_row=%" PRId64 " ra=%d rb=%d\n", na, nb, ra, rb);
shown++;
}
continue;
}
for (int c = 0; c < 9; c++) {
const unsigned char* va = sqlite3_column_text(sa, c);
const unsigned char* vb = sqlite3_column_text(sb, c);
const char* sva = (const char*)(va ? va : (const unsigned char*)"");
const char* svb = (const char*)(vb ? vb : (const unsigned char*)"");
if (g_strcmp0(sva, svb) != 0) {
mismatches++;
if (shown < 5) {
fprintf(stderr, " diff: row %" PRId64 " col=%d a=\"%s\" b=\"%s\"\n",
na, c, sva, svb);
shown++;
}
break;
}
}
}
sqlite3_finalize(sa);
sqlite3_finalize(sb);
sqlite3_close(da);
sqlite3_close(db);
if (total_a)
*total_a = na;
if (total_b)
*total_b = nb;
return mismatches;
}
// ---------------------------------------------------------------------------
// CLI dispatch
static void
usage(void)
{
fputs(
"Usage: bench_export_import SUBCMD [options]\n\n"
"Subcommands:\n"
" seed --db=PATH --rows=N [--contacts=K] [--lmc=PCT] [--body=BYTES] [--seed=S] [--account=JID]\n"
" export --account=JID --csv=PATH [--label=TAG]\n"
" import --account=JID --csv=PATH [--label=TAG]\n"
" roundtrip --rows=N --csv=PATH [--label=TAG] [--lmc=PCT] [--body=BYTES] [--full-diff]\n"
" verify --db-a=PATH --db-b=PATH\n",
stderr);
}
static int
cmd_seed(int argc, char** argv)
{
seed_opts_t o = { 0 };
o.account_jid = "bench@bench.example";
o.rows = 1000;
o.contacts = 1;
o.lmc_pct = 0;
o.avg_body = 0;
o.seed = 42;
char* db_path = NULL;
for (int i = 0; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--db=", 5) == 0)
db_path = g_strdup(a + 5);
else if (strncmp(a, "--rows=", 7) == 0)
o.rows = strtoll(a + 7, NULL, 10);
else if (strncmp(a, "--contacts=", 11) == 0)
o.contacts = atoi(a + 11);
else if (strncmp(a, "--lmc=", 6) == 0)
o.lmc_pct = atoi(a + 6);
else if (strncmp(a, "--body=", 7) == 0)
o.avg_body = strtoull(a + 7, NULL, 10);
else if (strncmp(a, "--seed=", 7) == 0)
o.seed = strtoull(a + 7, NULL, 10);
else if (strncmp(a, "--account=", 10) == 0)
o.account_jid = a + 10;
}
if (!db_path)
db_path = db_path_for(o.account_jid);
o.db_path = db_path;
int r = do_seed(&o);
g_free(db_path);
return r;
}
static int
cmd_export(int argc, char** argv)
{
const char* account = "bench@bench.example";
const char* csv = NULL;
const char* label = "S7_export";
const char* volume = "export";
for (int i = 0; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--account=", 10) == 0)
account = a + 10;
else if (strncmp(a, "--csv=", 6) == 0)
csv = a + 6;
else if (strncmp(a, "--label=", 8) == 0)
label = a + 8;
else if (strncmp(a, "--volume=", 9) == 0)
volume = a + 9;
}
if (!init_sqlite_backend(account))
return 2;
auto_gchar gchar* db_path = db_path_for(account);
int64_t rows_before = db_row_count(db_path);
double t0 = bench_now_ms();
int exported = log_database_export_to_flatfile(NULL);
double dt = bench_now_ms() - t0;
long rss = bench_peak_rss_kb();
fprintf(stderr, " %s: %d rows exported in %.2fs (db_rows=%" PRId64 ")\n",
label, exported, dt / 1000.0, rows_before);
if (csv) {
struct stat st;
uint64_t db_size = (stat(db_path, &st) == 0) ? (uint64_t)st.st_size : 0;
auto_gchar gchar* note = g_strdup_printf(
"exported=%d db_rows=%" PRId64, exported, rows_before);
bench_csv_append(csv, label, volume, db_size, (uint64_t)rows_before, dt, rss, note);
}
close_sqlite_backend();
return exported >= 0 ? 0 : 1;
}
static int
cmd_import(int argc, char** argv)
{
const char* account = "bench@bench.example";
const char* csv = NULL;
const char* label = "S8_import";
const char* volume = "import";
for (int i = 0; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--account=", 10) == 0)
account = a + 10;
else if (strncmp(a, "--csv=", 6) == 0)
csv = a + 6;
else if (strncmp(a, "--label=", 8) == 0)
label = a + 8;
else if (strncmp(a, "--volume=", 9) == 0)
volume = a + 9;
}
if (!init_sqlite_backend(account))
return 2;
auto_gchar gchar* db_path = db_path_for(account);
int64_t rows_before = db_row_count(db_path);
double t0 = bench_now_ms();
int imported = log_database_import_from_flatfile(NULL);
double dt = bench_now_ms() - t0;
long rss = bench_peak_rss_kb();
int64_t rows_after = db_row_count(db_path);
fprintf(stderr, " %s: %d imported, db rows %" PRId64 " -> %" PRId64 " in %.2fs\n",
label, imported, rows_before, rows_after, dt / 1000.0);
if (csv) {
struct stat st;
uint64_t db_size = (stat(db_path, &st) == 0) ? (uint64_t)st.st_size : 0;
auto_gchar gchar* note = g_strdup_printf(
"imported=%d rows_before=%" PRId64 " rows_after=%" PRId64,
imported, rows_before, rows_after);
bench_csv_append(csv, label, volume, db_size,
(uint64_t)(rows_after - rows_before), dt, rss, note);
}
close_sqlite_backend();
return imported >= 0 ? 0 : 1;
}
// Roundtrip: seed_A → export → import to fresh DB_B → diff(A, B)
static int
cmd_roundtrip(int argc, char** argv)
{
int64_t rows = 10000;
const char* csv = NULL;
const char* label = "S8e_roundtrip";
const char* volume = "roundtrip";
int lmc = 0;
size_t body = 0;
int do_diff = 0;
for (int i = 0; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--rows=", 7) == 0)
rows = strtoll(a + 7, NULL, 10);
else if (strncmp(a, "--csv=", 6) == 0)
csv = a + 6;
else if (strncmp(a, "--label=", 8) == 0)
label = a + 8;
else if (strncmp(a, "--volume=", 9) == 0)
volume = a + 9;
else if (strncmp(a, "--lmc=", 6) == 0)
lmc = atoi(a + 6);
else if (strncmp(a, "--body=", 7) == 0)
body = strtoull(a + 7, NULL, 10);
else if (strcmp(a, "--full-diff") == 0)
do_diff = 1;
}
const char* account_a = "rt-a@bench.example";
const char* account_b = "rt-b@bench.example";
auto_gchar gchar* db_a = db_path_for(account_a);
auto_gchar gchar* db_b = db_path_for(account_b);
unlink(db_a);
unlink(db_b);
auto_gchar gchar* wal_a = g_strdup_printf("%s-wal", db_a);
unlink(wal_a);
auto_gchar gchar* shm_a = g_strdup_printf("%s-shm", db_a);
unlink(shm_a);
auto_gchar gchar* wal_b = g_strdup_printf("%s-wal", db_b);
unlink(wal_b);
auto_gchar gchar* shm_b = g_strdup_printf("%s-shm", db_b);
unlink(shm_b);
fprintf(stderr, "===== roundtrip: rows=%" PRId64 " account_a=%s account_b=%s =====\n",
rows, account_a, account_b);
// 1) Seed DB_A
seed_opts_t so = { 0 };
so.db_path = db_a;
so.rows = rows;
so.contacts = 1;
so.lmc_pct = lmc;
so.avg_body = body;
so.seed = 42;
so.account_jid = account_a;
double seed_ms_t0 = bench_now_ms();
if (do_seed(&so) != 0)
return 2;
double seed_ms = bench_now_ms() - seed_ms_t0;
// 2) Export DB_A → flatlog under account_a
if (!init_sqlite_backend(account_a))
return 2;
double export_t0 = bench_now_ms();
int exported = log_database_export_to_flatfile(NULL);
double export_ms = bench_now_ms() - export_t0;
close_sqlite_backend();
fprintf(stderr, " exported %d rows in %.2fs\n", exported, export_ms / 1000.0);
// 3) Cross-mount the flatlog tree at account_b's expected location
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench-export";
auto_gchar gchar* dir_a = g_strdup_printf("%s/flatlog/%s", base, ff_jid_to_dir(account_a));
auto_gchar gchar* dir_b = g_strdup_printf("%s/flatlog/%s", base, ff_jid_to_dir(account_b));
auto_gchar gchar* parent_b = g_path_get_dirname(dir_b);
g_mkdir_with_parents(parent_b, 0755);
// Symlink dir_a as dir_b so import on account_b reads the same files.
unlink(dir_b);
if (symlink(dir_a, dir_b) != 0 && errno != EEXIST) {
fprintf(stderr, " symlink %s -> %s failed: %s\n", dir_b, dir_a, strerror(errno));
return 2;
}
// 4) Import flatlog → DB_B
if (!init_sqlite_backend(account_b))
return 2;
double import_t0 = bench_now_ms();
int imported = log_database_import_from_flatfile(NULL);
double import_ms = bench_now_ms() - import_t0;
close_sqlite_backend();
fprintf(stderr, " imported %d rows in %.2fs\n", imported, import_ms / 1000.0);
int64_t rows_a = db_row_count(db_a);
int64_t rows_b = db_row_count(db_b);
fprintf(stderr, " row counts: A=%" PRId64 " B=%" PRId64 "\n", rows_a, rows_b);
int64_t mismatches = 0;
double diff_ms = 0;
if (do_diff) {
double diff_t0 = bench_now_ms();
int64_t na = 0, nb = 0;
mismatches = db_diff_full(db_a, db_b, &na, &nb);
diff_ms = bench_now_ms() - diff_t0;
fprintf(stderr, " full content diff: a_rows=%" PRId64 " b_rows=%" PRId64 " mismatches=%" PRId64 " (%.2fs)\n",
na, nb, mismatches, diff_ms / 1000.0);
}
long rss = bench_peak_rss_kb();
double total_ms = seed_ms + export_ms + import_ms + diff_ms;
if (csv) {
struct stat st;
uint64_t db_size = (stat(db_a, &st) == 0) ? (uint64_t)st.st_size : 0;
auto_gchar gchar* note = g_strdup_printf(
"rows=%" PRId64 " seed=%.0fms export=%.0fms import=%.0fms diff=%.0fms "
"exported=%d imported=%d rows_a=%" PRId64 " rows_b=%" PRId64
" mismatches=%" PRId64 " body=%zu lmc=%d",
rows, seed_ms, export_ms, import_ms, diff_ms,
exported, imported, rows_a, rows_b, mismatches, body, lmc);
bench_csv_append(csv, label, volume, db_size, (uint64_t)rows,
total_ms, rss, note);
}
int ok = (rows_a == rows_b)
&& (!do_diff || mismatches == 0)
&& exported >= 0 && imported >= 0;
fprintf(stderr, " %s\n", ok ? "PASS" : "FAIL");
return ok ? 0 : 1;
}
static int
cmd_verify(int argc, char** argv)
{
const char* a = NULL;
const char* b = NULL;
for (int i = 0; i < argc; i++) {
const char* x = argv[i];
if (strncmp(x, "--db-a=", 7) == 0)
a = x + 7;
else if (strncmp(x, "--db-b=", 7) == 0)
b = x + 7;
}
if (!a || !b) {
usage();
return 2;
}
int64_t na = 0, nb = 0;
int64_t m = db_diff_full(a, b, &na, &nb);
fprintf(stderr, "verify: a_rows=%" PRId64 " b_rows=%" PRId64 " mismatches=%" PRId64 "\n",
na, nb, m);
return (m == 0 && na == nb) ? 0 : 1;
}
int
main(int argc, char** argv)
{
if (argc < 2) {
usage();
return 2;
}
const char* sub = argv[1];
int sub_argc = argc - 2;
char** sub_argv = argv + 2;
if (g_strcmp0(sub, "seed") == 0)
return cmd_seed(sub_argc, sub_argv);
if (g_strcmp0(sub, "export") == 0)
return cmd_export(sub_argc, sub_argv);
if (g_strcmp0(sub, "import") == 0)
return cmd_import(sub_argc, sub_argv);
if (g_strcmp0(sub, "roundtrip") == 0)
return cmd_roundtrip(sub_argc, sub_argv);
if (g_strcmp0(sub, "verify") == 0)
return cmd_verify(sub_argc, sub_argv);
usage();
return 2;
}

View File

@@ -0,0 +1,874 @@
// 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.
/*
* bench_failure_modes.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* F1F17 failure-injection tests. Each test crafts a deliberately corrupt
* or pathological flat-file and asserts how the backend handles it:
*
* F1 Truncated last line (crash mid-fwrite simulation)
* F2 CRLF line endings mid-corpus
* F3 BOM not at start (mid-file)
* F7 LMC cycle A→B→A (read path must not loop)
* F8 LMC chain longer than FF_MAX_LMC_DEPTH (graceful truncation)
* F9 Resource containing literal ": " (parser must reject the bad line)
* F10 RTL / zero-width chars in resource (parse OK, preserved literally)
* F11 Latin-1 fragment in UTF-8 file (invalid UTF-8 → fallback or ERROR)
* F12 Empty body — receipt-only carbon equivalent
* F14 mtime/inode flip — file replaced under us, ensure_fresh must rebuild
* F15 Empty file (0 bytes) — verify reports INFO and continues
* F16 Page-Up cursor must not stick on entries[0] (regression guard)
* F17 Forward iteration via start_time covers every message (no gaps)
*
* Each test prints PASS/FAIL with detail and writes a CSV row:
* F#, "failure", file_size, line_count, wall_ms, peak_rss_kb, note
*
* Tests run independently against per-test tmp directories; no shared state.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "bench_common.h"
#include "bench_csv.h"
#include "config/account.h"
#include "database_flatfile.h"
#define ACCOUNT_JID "fail@bench.example"
#define CONTACT_JID "peer@bench.example"
// ---------------------------------------------------------------------------
// Test framework
typedef struct
{
const char* tmp_dir;
const char* csv_path;
const char* tests; // comma list or "all"
int passed;
int failed;
} fail_ctx_t;
static int
test_enabled(const char* list, const char* name)
{
if (!list || g_strcmp0(list, "all") == 0)
return 1;
auto_gcharv gchar** parts = g_strsplit(list, ",", -1);
for (int i = 0; parts[i]; i++) {
if (g_strcmp0(g_strstrip(parts[i]), name) == 0)
return 1;
}
return 0;
}
// Set BENCH_DATA_DIR to <test_root> and return path to history.log inside the
// canonical layout; create directories as needed.
static char*
setup_test_dir(const char* tmp_root, const char* test_name)
{
auto_gchar gchar* test_root = g_strdup_printf("%s/%s", tmp_root, test_name);
g_mkdir_with_parents(test_root, 0755);
setenv("BENCH_DATA_DIR", test_root, 1);
auto_gchar gchar* account_dir = ff_jid_to_dir(ACCOUNT_JID);
auto_gchar gchar* contact_dir = ff_jid_to_dir(CONTACT_JID);
auto_gchar gchar* full = g_strdup_printf("%s/flatlog/%s/%s",
test_root, account_dir, contact_dir);
g_mkdir_with_parents(full, 0755);
return g_strdup_printf("%s/history.log", full);
}
typedef struct
{
int total;
int errors;
int warnings;
int infos;
char* first_err;
char* first_warn;
} verify_summary_t;
static void
verify_summary_free(verify_summary_t* s)
{
if (!s)
return;
g_free(s->first_err);
g_free(s->first_warn);
}
static void
run_verify(verify_summary_t* out)
{
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = g_strdup(ACCOUNT_JID);
GSList* issues = ff_verify_integrity(CONTACT_JID);
memset(out, 0, sizeof(*out));
out->total = g_slist_length(issues);
for (GSList* l = issues; l; l = l->next) {
integrity_issue_t* i = (integrity_issue_t*)l->data;
if (!i)
continue;
switch (i->level) {
case INTEGRITY_ERROR:
out->errors++;
if (!out->first_err)
out->first_err = g_strdup(i->message ? i->message : "");
break;
case INTEGRITY_WARNING:
out->warnings++;
if (!out->first_warn)
out->first_warn = g_strdup(i->message ? i->message : "");
break;
case INTEGRITY_INFO:
out->infos++;
break;
}
}
g_slist_free_full(issues, (GDestroyNotify)integrity_issue_free);
}
static void
report(fail_ctx_t* ctx, const char* tag, gboolean ok, double wall_ms,
const char* path, const char* note)
{
fprintf(stderr, " %s %-3s %.1fms %s\n",
ok ? "PASS" : "FAIL", tag, wall_ms, note ? note : "");
if (ok)
ctx->passed++;
else
ctx->failed++;
if (ctx->csv_path) {
struct stat st;
uint64_t sz = (path && stat(path, &st) == 0) ? (uint64_t)st.st_size : 0;
bench_csv_append(ctx->csv_path, tag, ok ? "fail-pass" : "fail-FAIL",
sz, 0, wall_ms, bench_peak_rss_kb(),
note ? note : "");
}
}
// ---------------------------------------------------------------------------
// Helpers to build correct lines
static void
write_normal_line(FILE* fp, int seq, const char* body)
{
GDateTime* base = g_date_time_new_utc(2025, 6, 15, 12, 0, 0.0);
GDateTime* t = g_date_time_add_seconds(base, seq);
auto_gchar gchar* iso = g_date_time_format_iso8601(t);
g_date_time_unref(t);
g_date_time_unref(base);
auto_gchar gchar* sid = g_strdup_printf("F-msg-%d", seq);
ff_write_line(fp, iso, "chat", "none",
sid, NULL, NULL,
"peer@bench.example", "phone",
NULL, NULL, -1, body);
}
// ---------------------------------------------------------------------------
// F1 — truncated last line
static void
test_F1(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F1"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F1");
// Write 10 normal lines; chop the trailing \n off the last.
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
for (int i = 0; i < 10; i++)
write_normal_line(fp, i, "ok");
fflush(fp);
long sz_before = ftell(fp);
fclose(fp);
// Truncate one byte (drops the trailing \n)
if (truncate(path, sz_before - 1) != 0) {
report(ctx, "F1", FALSE, 0, path, "truncate failed");
return;
}
// Now also append a partial line (simulates crash mid-fwrite).
fp = fopen(path, "a");
fprintf(fp, "2025-06-15T12:00:11Z [chat|none|id:partial] peer@bench.example/phone: half-writ");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Expectation: file is parseable (last line is "half-writ" — likely
// unparsable depending on whether the timestamp+meta+sender all fit).
// We assert verify completes without crashing AND surfaces the partial
// line as either a parse-error or merely no-error (depending on
// ff_readline truncated detection — currently the warning is not raised).
auto_gchar gchar* note = g_strdup_printf("issues total=%d err=%d warn=%d (partial-line tail; expect graceful handle)",
s.total, s.errors, s.warnings);
report(ctx, "F1", TRUE, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F2 — CRLF mid-corpus
static void
test_F2(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F2"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F2");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
for (int i = 0; i < 10; i++) {
write_normal_line(fp, i, "lf line");
}
// Append three CRLF-terminated lines manually.
for (int i = 10; i < 13; i++) {
fprintf(fp,
"2025-06-15T12:%02d:00Z [chat|none|id:crlf-%d] peer@bench.example/phone: crlf line\r\n",
i, i);
}
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
gboolean ok = s.warnings >= 1; // expect ≥1 CRLF/perms warning
auto_gchar gchar* note = g_strdup_printf(
"warnings=%d (expect CRLF warning) first_warn=%s",
s.warnings, s.first_warn ? s.first_warn : "(none)");
report(ctx, "F2", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F3 — BOM not at start (mid-file)
static void
test_F3(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F3"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F3");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
write_normal_line(fp, 0, "before bom");
// Inject BOM bytes at the start of a line in the middle of the file.
fputc(0xEF, fp);
fputc(0xBB, fp);
fputc(0xBF, fp);
fprintf(fp,
"2025-06-15T12:00:01Z [chat|none|id:after-bom] peer@bench.example/phone: after bom\n");
write_normal_line(fp, 2, "trailing");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Expectation: the BOM bytes appear at the start of a line that the
// parser doesn't recognise, so we get an unparsable-line ERROR for one row.
gboolean ok = s.errors >= 1;
auto_gchar gchar* note = g_strdup_printf(
"errors=%d warnings=%d (expect 1 unparsable line for mid-file BOM) first=%s",
s.errors, s.warnings, s.first_err ? s.first_err : "(none)");
report(ctx, "F3", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F7 — LMC cycle A→B→A (must not loop on read; verify must not crash)
static void
test_F7(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F7"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F7");
// A: stanza_id="A", no replace
// B: stanza_id="B", replaces "A"
// A2: stanza_id="A2", replaces "B" — chain ok
// CYCLE: stanza_id="C1", replaces "C2"
// stanza_id="C2", replaces "C1" (cycle in references)
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
fprintf(fp, "2025-06-15T12:00:00Z [chat|none|id:A] peer@bench.example/phone: original A\n");
fprintf(fp, "2025-06-15T12:00:01Z [chat|none|id:B|corrects:A] peer@bench.example/phone: correction B\n");
fprintf(fp, "2025-06-15T12:00:02Z [chat|none|id:A2|corrects:B] peer@bench.example/phone: correction A2\n");
fprintf(fp, "2025-06-15T12:00:03Z [chat|none|id:C1|corrects:C2] peer@bench.example/phone: cycle leg 1\n");
fprintf(fp, "2025-06-15T12:00:04Z [chat|none|id:C2|corrects:C1] peer@bench.example/phone: cycle leg 2\n");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Expectation: verify completes without infinite loop. Both C1→C2 and
// C2→C1 references resolve (each id is present), so no broken-refs.
// The actual cycle-walk happens at *read* time, not in verify.
gboolean ok = (s.total < 1000); // sanity: must finish fast and not explode
auto_gchar gchar* note = g_strdup_printf(
"issues total=%d err=%d (cycle handled at read-time, not verify)",
s.total, s.errors);
report(ctx, "F7", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F8 — LMC chain depth 200 (over FF_MAX_LMC_DEPTH=100)
static void
test_F8(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F8"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F8");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
// Original message id=A0, then 200 corrections each pointing to the previous.
fprintf(fp, "2025-06-15T12:00:00Z [chat|none|id:A0] peer@bench.example/phone: original\n");
for (int i = 1; i <= 200; i++) {
fprintf(fp,
"2025-06-15T12:%02d:%02dZ [chat|none|id:A%d|corrects:A%d] "
"peer@bench.example/phone: correction-%d\n",
i / 60, i % 60, i, i - 1, i);
}
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// All references resolve (each correction's predecessor is present), so
// verify reports no broken refs. The depth-truncation enforced by
// FF_MAX_LMC_DEPTH only manifests at read time. Just assert no errors.
gboolean ok = (s.errors == 0);
auto_gchar gchar* note = g_strdup_printf(
"201 lines (1 orig + 200 corrections) errors=%d warns=%d",
s.errors, s.warnings);
report(ctx, "F8", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F9 — Resource contains literal ": " (manual edit; parser must reject)
static void
test_F9(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F9"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F9");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
write_normal_line(fp, 0, "ok");
// Manually crafted bad line: resource has unescaped ": ", parser will
// split message at the wrong colon and produce garbage.
fprintf(fp,
"2025-06-15T12:00:01Z [chat|none|id:bad] peer@bench.example/phone: with: colon and: spaces: inside\n");
write_normal_line(fp, 2, "ok2");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Parser splits at first unescaped ": " — message body becomes
// "with" instead of the full text. That's not an error from the parser's
// POV (it parsed something), so verify reports no issues. We just check
// that we didn't crash and that the file as-a-whole is parseable.
gboolean ok = (s.errors == 0);
auto_gchar gchar* note = g_strdup_printf(
"errors=%d (parser splits at first ': ' — not flagged but body truncated)",
s.errors);
report(ctx, "F9", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F10 — RTL / zero-width chars in resource
static void
test_F10(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F10"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F10");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
// resource = "phone" + U+202E (RTL override) + U+200B (zero-width space).
// We emit the bytes via fwrite to keep the C source ASCII-clean
// (-Werror=bidi-chars trips on literals containing RTL controls).
static const unsigned char rtl_zwsp[] = {
0xE2, 0x80, 0xAE, 0xE2, 0x80, 0x8B
};
fputs("2025-06-15T12:00:00Z [chat|none|id:rtl] peer@bench.example/phone", fp);
fwrite(rtl_zwsp, 1, sizeof(rtl_zwsp), fp);
fputs(": payload\n", fp);
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Should parse fine (valid UTF-8, just unusual). Verify might or might
// not flag the control-char check; let's just ensure no crash and total
// <= 5 (perms warning + maybe control-char if parser-level).
gboolean ok = (s.errors == 0);
auto_gchar gchar* note = g_strdup_printf(
"errors=%d warnings=%d (RTL/ZWSP preserved literally)",
s.errors, s.warnings);
report(ctx, "F10", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F11 — Latin-1 fragment (bytes 0xA0+ that aren't valid UTF-8)
static void
test_F11(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F11"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F11");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
write_normal_line(fp, 0, "before");
// 0xC9 (É in Latin-1) followed by 'o' is invalid UTF-8 (0xC9 is a 2-byte
// lead expecting a continuation byte, but 'o' isn't one). Emit the prefix
// and the bad byte separately to keep the C string literal valid.
fputs("2025-06-15T12:00:01Z [chat|none|id:latin1] peer@bench.example/phone: ", fp);
static const unsigned char latin1_byte = 0xC9;
fwrite(&latin1_byte, 1, 1, fp);
fputs("ole\n", fp);
write_normal_line(fp, 2, "after");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Expectation: the verify pass flags the line as invalid UTF-8 (ERROR),
// OR ff_parse_line attempts Latin-1 fallback (in which case the line
// parses and there's no error). Either is acceptable.
gboolean ok = TRUE; // never crash
auto_gchar gchar* note = g_strdup_printf(
"errors=%d (Latin-1 byte: error or fallback OK)",
s.errors);
report(ctx, "F11", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F12 — empty body (e.g. receipt-only carbon)
static void
test_F12(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F12"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F12");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
write_normal_line(fp, 0, "before empty");
// Empty body: ends at "...phone: \n"
fprintf(fp,
"2025-06-15T12:00:01Z [chat|none|id:empty] peer@bench.example/phone: \n");
write_normal_line(fp, 2, "after empty");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
// Empty body is OK from parser's POV.
gboolean ok = (s.errors == 0);
auto_gchar gchar* note = g_strdup_printf("errors=%d (empty body OK)", s.errors);
report(ctx, "F12", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F14 — mtime/inode flip: file replaced under us. ensure_fresh must rebuild.
static void
test_F14(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F14"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F14");
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
for (int i = 0; i < 100; i++)
write_normal_line(fp, i, "v1");
fclose(fp);
ff_contact_state_t* state = ff_state_new(path);
ff_state_ensure_fresh(state);
size_t lines_v1 = state->total_lines;
// Sleep 1 second so the mtime change is visible at second-resolution stat.
sleep(1);
// Now replace the file entirely (different content + different inode).
auto_gchar gchar* tmp_path = g_strdup_printf("%s.swap", path);
FILE* fp2 = fopen(tmp_path, "w");
fprintf(fp2, "%s", FLATFILE_HEADER);
for (int i = 0; i < 250; i++)
write_normal_line(fp2, i, "v2 totally different");
fclose(fp2);
rename(tmp_path, path);
double t0 = bench_now_ms();
int ok_fresh = ff_state_ensure_fresh(state);
double dt = bench_now_ms() - t0;
size_t lines_v2 = state->total_lines;
ff_state_free(state);
gboolean ok = ok_fresh && lines_v2 != lines_v1 && lines_v2 == 250;
auto_gchar gchar* note = g_strdup_printf(
"v1_lines=%zu v2_lines=%zu (expect rebuild detected and 250 lines)",
lines_v1, lines_v2);
report(ctx, "F14", ok, dt, path, note);
}
// ---------------------------------------------------------------------------
// F15 — empty file
static void
test_F15(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F15"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F15");
FILE* fp = fopen(path, "w");
fclose(fp);
double t0 = bench_now_ms();
verify_summary_t s;
run_verify(&s);
double dt = bench_now_ms() - t0;
gboolean ok = s.infos >= 1; // expect at least one INFO
auto_gchar gchar* note = g_strdup_printf(
"errors=%d infos=%d warnings=%d (empty file should yield INFO)",
s.errors, s.infos, s.warnings);
report(ctx, "F15", ok, dt, path, note);
verify_summary_free(&s);
}
// ---------------------------------------------------------------------------
// F16 — Page-Up cursor pagination must not get stuck at file start.
//
// Walks the contact log from newest to oldest by repeatedly calling
// db_backend_flatfile()->get_previous_chat with end_time set to the
// timestamp of the oldest message returned in the previous batch
// (mirroring chatwin's Page-Up flow). Stops on DB_RESPONSE_EMPTY.
//
// Buggy behaviour: the cursor migrates back until it lands on the byte
// offset of the first index entry, then the !from_start backup logic
// produces an empty [X, X) range, EMPTY arrives prematurely, and a real
// UI would set WIN_SCROLL_REACHED_TOP forever.
//
// Pass criterion: total messages received across all calls equals the
// number of messages written to the file.
static void
test_F16(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F16"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F16");
// Write enough messages that we cross at least 3 index entries
// (FF_INDEX_STEP=500 → 2000 messages = 4 entries).
const int total_msgs = 2000;
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
for (int i = 0; i < total_msgs; i++)
write_normal_line(fp, i, "page-up regression payload");
fclose(fp);
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = g_strdup(ACCOUNT_JID);
setenv("BENCH_ACCOUNT_JID", ACCOUNT_JID, 1);
db_backend_t* be = db_backend_flatfile();
if (!be || !be->init || !be->get_previous_chat || !be->close) {
report(ctx, "F16", FALSE, 0, path, "flatfile backend not available");
return;
}
// Initialize the per-contact state hash; without this _ff_get_state
// returns NULL and the very first call gets DB_RESPONSE_ERROR.
ProfAccount fake = { 0 };
fake.jid = (gchar*)ACCOUNT_JID;
if (!be->init(&fake)) {
report(ctx, "F16", FALSE, 0, path, "flatfile init failed");
return;
}
int total_received = 0;
int empty_responses = 0;
int iterations = 0;
auto_gchar gchar* end_time = NULL;
double t0 = bench_now_ms();
// Loop with a hard ceiling — if the bug is present we'd otherwise
// stop after one page anyway, but the ceiling protects us against
// a hypothetical infinite loop.
while (iterations < 200) {
iterations++;
GSList* batch = NULL;
db_history_result_t r = be->get_previous_chat(
CONTACT_JID, NULL, end_time, FALSE, FALSE, &batch);
if (r == DB_RESPONSE_EMPTY) {
empty_responses++;
break;
}
if (r != DB_RESPONSE_SUCCESS || !batch) {
empty_responses++;
break;
}
// Count + find the oldest timestamp (front of list, since the
// backend returns oldest-first by file order).
ProfMessage* oldest = batch->data;
if (!oldest || !oldest->timestamp) {
g_slist_free_full(batch, (GDestroyNotify)message_free);
break;
}
int batch_n = (int)g_slist_length(batch);
total_received += batch_n;
g_free(end_time);
end_time = g_date_time_format_iso8601(oldest->timestamp);
g_slist_free_full(batch, (GDestroyNotify)message_free);
}
double dt = bench_now_ms() - t0;
// Two regression classes are caught here:
// 1. The original "cursor stuck on entries[0]" symptom (early EMPTY
// after 2-3 iterations, ~200 received) — capped page-up at the top.
// 2. The deeper "cursor jumping" bug — cursor was set to the oldest
// line of the read window rather than the oldest line returned to
// the caller after pagination, so each subsequent page-up skipped
// past entire ranges of messages. With both fixed, every written
// message must be reachable through sequential page-up.
gboolean ok = (total_received == total_msgs)
&& (empty_responses == 1)
&& (iterations < 200);
auto_gchar gchar* note = g_strdup_printf(
"wrote=%d received=%d iters=%d empty=%d "
"(without fix: ~200 received in ~3 iters; cursor-jumping mode: ~600 in ~7)",
total_msgs, total_received, iterations, empty_responses);
report(ctx, "F16", ok, dt, path, note);
be->close();
}
// ---------------------------------------------------------------------------
// F17 — Forward iteration (start_time + from_start=TRUE) reaches every
// message without gaps.
//
// Symmetric counterpart to F16: instead of paging from newest backwards
// via end_time, page from oldest forwards via start_time. Each iteration
// passes start_time = timestamp of the newest message returned in the
// previous batch; the backend should return the next batch of strictly
// newer messages until the file is exhausted.
//
// This exercises a different branch of _flatfile_get_previous_chat:
// - read_from = ff_state_offset_for_time(start_time) (not bom_len)
// - read_to = EOF (no end_time bisect)
// - filter drops msgs with timestamp <= start_dt
// - pagination keeps FIRST MESSAGES_TO_RETRIEVE (not last)
// The path bypasses cursor entirely (start_time triggers cursor=-1 reset).
//
// Pass criterion: total messages received == total written.
static void
test_F17(fail_ctx_t* ctx)
{
if (!test_enabled(ctx->tests, "F17"))
return;
auto_gchar gchar* path = setup_test_dir(ctx->tmp_dir, "F17");
const int total_msgs = 2000;
FILE* fp = fopen(path, "w");
fprintf(fp, "%s", FLATFILE_HEADER);
for (int i = 0; i < total_msgs; i++)
write_normal_line(fp, i, "forward-iter regression payload");
fclose(fp);
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = g_strdup(ACCOUNT_JID);
setenv("BENCH_ACCOUNT_JID", ACCOUNT_JID, 1);
db_backend_t* be = db_backend_flatfile();
if (!be || !be->init || !be->get_previous_chat || !be->close) {
report(ctx, "F17", FALSE, 0, path, "flatfile backend not available");
return;
}
ProfAccount fake = { 0 };
fake.jid = (gchar*)ACCOUNT_JID;
if (!be->init(&fake)) {
report(ctx, "F17", FALSE, 0, path, "flatfile init failed");
return;
}
int total_received = 0;
int empty_responses = 0;
int iterations = 0;
// Initial start_time before the first written message so the first
// call returns msgs starting at index 0.
auto_gchar gchar* start_time = g_strdup("2025-06-15T11:00:00Z");
double t0 = bench_now_ms();
while (iterations < 200) {
iterations++;
GSList* batch = NULL;
db_history_result_t r = be->get_previous_chat(
CONTACT_JID, start_time, NULL, TRUE, FALSE, &batch);
if (r == DB_RESPONSE_EMPTY) {
empty_responses++;
break;
}
if (r != DB_RESPONSE_SUCCESS || !batch) {
empty_responses++;
break;
}
// pre_result is sorted oldest-first; with from_start=TRUE we keep
// the first MESSAGES_TO_RETRIEVE. The newest in the returned batch
// is therefore the LAST element of the list.
ProfMessage* newest = NULL;
int batch_n = 0;
for (GSList* l = batch; l; l = l->next) {
batch_n++;
newest = l->data;
}
if (!newest || !newest->timestamp) {
g_slist_free_full(batch, (GDestroyNotify)message_free);
break;
}
total_received += batch_n;
g_free(start_time);
start_time = g_date_time_format_iso8601(newest->timestamp);
g_slist_free_full(batch, (GDestroyNotify)message_free);
}
double dt = bench_now_ms() - t0;
gboolean ok = (total_received == total_msgs)
&& (empty_responses == 1)
&& (iterations < 200);
auto_gchar gchar* note = g_strdup_printf(
"wrote=%d received=%d iters=%d empty=%d (forward-iter regression)",
total_msgs, total_received, iterations, empty_responses);
report(ctx, "F17", ok, dt, path, note);
be->close();
}
// ---------------------------------------------------------------------------
// Driver
int
main(int argc, char** argv)
{
fail_ctx_t ctx = { 0 };
ctx.tmp_dir = "/tmp/cproof-bench-failmodes";
ctx.csv_path = NULL;
ctx.tests = "all";
for (int i = 1; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--tmp=", 6) == 0)
ctx.tmp_dir = a + 6;
else if (strncmp(a, "--csv=", 6) == 0)
ctx.csv_path = a + 6;
else if (strncmp(a, "--tests=", 8) == 0)
ctx.tests = a + 8;
else {
fprintf(stderr, "unknown option: %s\n", a);
return 2;
}
}
if (g_mkdir_with_parents(ctx.tmp_dir, 0755) != 0) {
fprintf(stderr, "cannot create %s\n", ctx.tmp_dir);
return 2;
}
fprintf(stderr, "===== bench_failure_modes: tmp=%s =====\n", ctx.tmp_dir);
test_F1(&ctx);
test_F2(&ctx);
test_F3(&ctx);
test_F7(&ctx);
test_F8(&ctx);
test_F9(&ctx);
test_F10(&ctx);
test_F11(&ctx);
test_F12(&ctx);
test_F14(&ctx);
test_F15(&ctx);
test_F16(&ctx);
test_F17(&ctx);
fprintf(stderr, "\nfailure-modes summary: %d passed, %d failed\n",
ctx.passed, ctx.failed);
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = NULL;
return ctx.failed == 0 ? 0 : 1;
}

View File

@@ -0,0 +1,520 @@
// 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.
/*
* bench_long_messages.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* L1L14: long-message stress tests for the flat-file backend.
*
* L1 1 KB body roundtrip identity
* L2 10 KB body roundtrip identity
* L3 100 KB body roundtrip identity
* L4 1 MB body roundtrip identity + write/read time
* L5 5 MB body roundtrip identity
* L6 9.9 MB body just under FF_MAX_LINE_LEN (10 MB)
* L7 10 MB + 1 body expect ff_readline reject (returns "")
* L8 100 KB with embedded \n×1k escape stress (each \n -> \\n doubles)
* L9 100 KB with `|` × 50k not in metadata, parser shouldn't choke
* L10 100 KB emoji-only 4-byte UTF-8 codepoints
* L11 100 × 1 KB roundtrip sanity baseline
* L12 pagination — last 100 with 5×1MB bodies
* L13 verify-pass on 100×1MB
* L14 rapid append: 1000×100KB
*
* Each test:
* 1. Generates N messages with a specific body_size and content_pattern.
* 2. Writes them via ff_write_line into a fresh temp file.
* 3. Reads them back via ff_readline + ff_parse_line.
* 4. Asserts body length / content invariants.
* 5. Writes a CSV row: L#, body_size, n, write_ms, read_ms, peak_rss
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include "bench_common.h"
#include "bench_csv.h"
#include "database_flatfile.h"
// ---------------------------------------------------------------------------
// CLI
typedef struct
{
const char* tmp_dir;
const char* csv_path;
const char* tests; // comma list or "all"
} cli_t;
static int
parse_args(int argc, char** argv, cli_t* c)
{
c->tmp_dir = "/tmp/cproof-bench-longmsg";
c->csv_path = NULL;
c->tests = "all";
for (int i = 1; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--tmp=", 6) == 0)
c->tmp_dir = a + 6;
else if (strncmp(a, "--csv=", 6) == 0)
c->csv_path = a + 6;
else if (strncmp(a, "--tests=", 8) == 0)
c->tests = a + 8;
else {
fprintf(stderr, "unknown option: %s\n", a);
return -1;
}
}
return 0;
}
static int
test_enabled(const char* list, const char* name)
{
if (!list || g_strcmp0(list, "all") == 0)
return 1;
auto_gcharv gchar** parts = g_strsplit(list, ",", -1);
for (int i = 0; parts[i]; i++) {
if (g_strcmp0(g_strstrip(parts[i]), name) == 0)
return 1;
}
return 0;
}
// ---------------------------------------------------------------------------
// Body factories
typedef enum {
PAT_FILLER, // deterministic alpha-num
PAT_EMBEDDED_LF, // filler with newlines every 100 bytes
PAT_EMBEDDED_PIPE, // filler with '|' sprinkled
PAT_EMOJI, // repeated 4-byte UTF-8 emoji
} pattern_t;
static char*
make_body(size_t target_len, pattern_t pat)
{
if (pat == PAT_EMOJI) {
// 4 bytes per "🚀" (U+1F680). Round target down to 4-byte boundary.
size_t n_emoji = target_len / 4;
size_t bytes = n_emoji * 4;
char* buf = g_malloc(bytes + 1);
const char emoji[5] = { (char)0xF0, (char)0x9F, (char)0x9A, (char)0x80, 0 };
for (size_t i = 0; i < n_emoji; i++)
memcpy(buf + i * 4, emoji, 4);
buf[bytes] = '\0';
return buf;
}
char* buf = g_malloc(target_len + 1);
static const char alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOP";
static const int alpha_n = sizeof(alpha) - 1;
for (size_t i = 0; i < target_len; i++)
buf[i] = alpha[i % alpha_n];
if (pat == PAT_EMBEDDED_LF) {
for (size_t i = 100; i < target_len; i += 100)
buf[i] = '\n';
} else if (pat == PAT_EMBEDDED_PIPE) {
// sprinkle ~50k pipes uniformly across 100 KB
size_t step = target_len > 50000 ? target_len / 50000 : 2;
for (size_t i = 0; i < target_len; i += step)
buf[i] = '|';
}
buf[target_len] = '\0';
return buf;
}
// ---------------------------------------------------------------------------
// Roundtrip primitive
typedef struct
{
char* path;
int64_t bytes_written;
double write_ms;
double read_ms;
int64_t parsed;
int64_t mismatched_len;
int64_t parse_failures;
long peak_rss_kb;
} roundtrip_result_t;
static void
roundtrip_cleanup(roundtrip_result_t* r)
{
if (!r)
return;
if (r->path && r->path[0])
unlink(r->path);
g_free(r->path);
memset(r, 0, sizeof(*r));
}
static int
write_n_messages(const char* path, int n, size_t body_len, pattern_t pat,
int64_t* bytes_written_out)
{
FILE* fp = fopen(path, "w");
if (!fp)
return -1;
fprintf(fp, "%s", FLATFILE_HEADER);
GDateTime* base = g_date_time_new_utc(2025, 6, 15, 12, 0, 0.0);
for (int i = 0; i < n; i++) {
GDateTime* t = g_date_time_add_seconds(base, i);
auto_gchar gchar* iso = g_date_time_format_iso8601(t);
g_date_time_unref(t);
auto_gchar gchar* sid = g_strdup_printf("long-%d-%08x", i,
(unsigned)(rand_r(&(unsigned){ i + 1 })));
auto_gchar gchar* body = make_body(body_len, pat);
ff_write_line(fp, iso, "chat", "none",
sid, NULL, NULL,
"alice@bench.example", "phone",
NULL, NULL, -1,
body);
}
g_date_time_unref(base);
fflush(fp);
if (bytes_written_out) {
struct stat st;
if (fstat(fileno(fp), &st) == 0)
*bytes_written_out = st.st_size;
}
fclose(fp);
return 0;
}
static int
read_and_validate(const char* path, size_t expected_body_len, int n,
int64_t* parsed_out, int64_t* mismatched_out, int64_t* parse_fail_out)
{
FILE* fp = fopen(path, "r");
if (!fp)
return -1;
ff_skip_bom(fp);
int64_t parsed = 0, mismatched = 0, failures = 0;
char* buf;
while ((buf = ff_readline(fp, NULL)) != NULL) {
if (buf[0] == '\0' || buf[0] == '#') {
free(buf);
continue;
}
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
if (!pl) {
failures++;
continue;
}
parsed++;
if (expected_body_len > 0 && pl->message
&& strlen(pl->message) != expected_body_len) {
mismatched++;
}
ff_parsed_line_free(pl);
}
fclose(fp);
if (parsed_out)
*parsed_out = parsed;
if (mismatched_out)
*mismatched_out = mismatched;
if (parse_fail_out)
*parse_fail_out = failures;
(void)n;
return 0;
}
static void
run_roundtrip(const char* tmp_dir, const char* tag,
int n, size_t body_len, pattern_t pat,
roundtrip_result_t* out)
{
out->path = g_strdup_printf("%s/%s.log", tmp_dir, tag);
double t0 = bench_now_ms();
if (write_n_messages(out->path, n, body_len, pat, &out->bytes_written) != 0) {
fprintf(stderr, " %s: WRITE FAILED\n", tag);
return;
}
out->write_ms = bench_now_ms() - t0;
bench_drop_page_cache(out->path);
t0 = bench_now_ms();
read_and_validate(out->path, body_len, n,
&out->parsed, &out->mismatched_len, &out->parse_failures);
out->read_ms = bench_now_ms() - t0;
out->peak_rss_kb = bench_peak_rss_kb();
}
static void
csv_emit(const char* csv_path, const char* tag, size_t body_len, int n,
const roundtrip_result_t* r, const char* note)
{
if (!csv_path)
return;
auto_gchar gchar* full_note = g_strdup_printf(
"n=%d body=%zu write=%.1fms read=%.1fms parsed=%" PRId64
" mismatch=%" PRId64 " parse_fail=%" PRId64 " %s",
n, body_len, r->write_ms, r->read_ms,
r->parsed, r->mismatched_len, r->parse_failures,
note ? note : "");
bench_csv_append(csv_path, tag, "longmsg",
(uint64_t)r->bytes_written,
(uint64_t)n,
r->write_ms + r->read_ms,
r->peak_rss_kb,
full_note);
}
// ---------------------------------------------------------------------------
// Tests
#define KB (size_t)1024
#define MB (size_t)(1024 * 1024)
static void
run_simple(const char* tmp, const char* csv, const char* tag,
int n, size_t body_len, pattern_t pat, const char* note)
{
roundtrip_result_t r = { 0 };
run_roundtrip(tmp, tag, n, body_len, pat, &r);
fprintf(stderr,
" %-7s n=%d body=%zu B write=%.1fms read=%.1fms parsed=%" PRId64
" mismatch=%" PRId64 " fail=%" PRId64 "\n",
tag, n, body_len, r.write_ms, r.read_ms,
r.parsed, r.mismatched_len, r.parse_failures);
csv_emit(csv, tag, body_len, n, &r, note);
roundtrip_cleanup(&r);
}
// L7: body just over FF_MAX_LINE_LEN. ff_readline must reject and return "".
// We can't roundtrip via ff_write_line because the writer doesn't enforce a
// limit; we craft the line manually and verify the reader's rejection.
static void
run_oversized(const char* tmp, const char* csv)
{
const char* tag = "L7";
auto_gchar gchar* path = g_strdup_printf("%s/%s.log", tmp, tag);
size_t body_len = FF_MAX_LINE_LEN + 1;
FILE* fp = fopen(path, "w");
if (!fp) {
fprintf(stderr, " %s: cannot open %s\n", tag, path);
return;
}
fprintf(fp, "%s", FLATFILE_HEADER);
// Hand-build a single oversized line: timestamp + meta + sender: + body + \n
auto_gchar gchar* body = make_body(body_len, PAT_FILLER);
fprintf(fp,
"2025-06-15T12:00:00Z [chat|none|id:over] alice@bench/phone: %s\n",
body);
// Add one normal line afterwards so we can verify the loop continues.
fprintf(fp, "2025-06-15T12:01:00Z [chat|none|id:next] alice@bench/phone: ok\n");
fclose(fp);
bench_drop_page_cache(path);
double t0 = bench_now_ms();
fp = fopen(path, "r");
ff_skip_bom(fp);
int64_t parsed = 0, empty_skipped = 0, failures = 0;
char* buf;
while ((buf = ff_readline(fp, NULL)) != NULL) {
if (buf[0] == '\0') {
empty_skipped++;
free(buf);
continue;
}
if (buf[0] == '#') {
free(buf);
continue;
}
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
if (pl) {
parsed++;
ff_parsed_line_free(pl);
} else
failures++;
}
fclose(fp);
double read_ms = bench_now_ms() - t0;
long rss = bench_peak_rss_kb();
int ok = (parsed == 1 && empty_skipped >= 1);
fprintf(stderr,
" L7 body=%zu read=%.1fms parsed=%" PRId64
" skipped_empty=%" PRId64 " failures=%" PRId64 " %s\n",
body_len, read_ms, parsed, empty_skipped, failures,
ok ? "OK (oversize rejected)" : "MISBEHAVE");
if (csv) {
struct stat st;
uint64_t sz = (stat(path, &st) == 0) ? (uint64_t)st.st_size : 0;
auto_gchar gchar* note = g_strdup_printf(
"oversize=%zu_rejected=%s read=%.1fms parsed=%" PRId64,
body_len, ok ? "yes" : "no", read_ms, parsed);
bench_csv_append(csv, tag, "longmsg", sz, 2, read_ms, rss, note);
}
unlink(path);
}
// L12: pagination — last 100 messages where 5 of the last 100 are 1 MB bodies.
// Simulates a chat scrollback that includes a few paste-bombs near the end.
static void
run_pagination_with_huge(const char* tmp, const char* csv)
{
const char* tag = "L12";
auto_gchar gchar* path = g_strdup_printf("%s/%s.log", tmp, tag);
int total = 1000;
int huge_indices[5] = { 950, 970, 985, 992, 998 }; // five 1MB bodies near end
FILE* fp = fopen(path, "w");
if (!fp)
return;
fprintf(fp, "%s", FLATFILE_HEADER);
GDateTime* base = g_date_time_new_utc(2025, 6, 15, 12, 0, 0.0);
for (int i = 0; i < total; i++) {
GDateTime* t = g_date_time_add_seconds(base, i);
auto_gchar gchar* iso = g_date_time_format_iso8601(t);
g_date_time_unref(t);
size_t blen = 100;
for (int k = 0; k < 5; k++)
if (huge_indices[k] == i) {
blen = MB;
break;
}
auto_gchar gchar* sid = g_strdup_printf("page-%d", i);
auto_gchar gchar* body = make_body(blen, PAT_FILLER);
ff_write_line(fp, iso, "chat", "none",
sid, NULL, NULL,
"alice@bench.example", "phone",
NULL, NULL, -1,
body);
}
g_date_time_unref(base);
fclose(fp);
bench_drop_page_cache(path);
double t0 = bench_now_ms();
// Build state, find the second-to-last index entry to simulate "last 100".
ff_contact_state_t* state = ff_state_new(path);
ff_state_ensure_fresh(state);
off_t start = state->bom_len;
if (state->n_entries >= 2)
start = state->entries[state->n_entries - 2].byte_offset;
else if (state->n_entries == 1)
start = state->entries[state->n_entries - 1].byte_offset;
ff_state_free(state);
fp = fopen(path, "r");
fseeko(fp, start, SEEK_SET);
char* ring[100] = { 0 };
int rpos = 0;
int64_t parsed = 0;
char* buf;
while ((buf = ff_readline(fp, NULL)) != NULL) {
if (buf[0] == '\0' || buf[0] == '#') {
free(buf);
continue;
}
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
if (!pl)
continue;
parsed++;
if (ring[rpos])
g_free(ring[rpos]);
ring[rpos] = g_strdup(pl->message ? pl->message : "");
rpos = (rpos + 1) % 100;
ff_parsed_line_free(pl);
}
fclose(fp);
double dt = bench_now_ms() - t0;
long rss = bench_peak_rss_kb();
for (int i = 0; i < 100; i++)
g_free(ring[i]);
fprintf(stderr,
" L12 scrollback w/ 5×1MB in last 100 read=%.1fms parsed=%" PRId64 " rss=%ldKB\n",
dt, parsed, rss);
if (csv) {
struct stat st;
uint64_t sz = (stat(path, &st) == 0) ? (uint64_t)st.st_size : 0;
auto_gchar gchar* note = g_strdup_printf(
"5x1MB_in_last_100 parsed=%" PRId64, parsed);
bench_csv_append(csv, tag, "longmsg", sz, (uint64_t)parsed, dt, rss, note);
}
unlink(path);
}
// ---------------------------------------------------------------------------
// Driver
int
main(int argc, char** argv)
{
cli_t cli;
if (parse_args(argc, argv, &cli) != 0)
return 2;
if (g_mkdir_with_parents(cli.tmp_dir, 0755) != 0) {
fprintf(stderr, "cannot create %s\n", cli.tmp_dir);
return 2;
}
fprintf(stderr, "===== bench_long_messages: tmp=%s =====\n", cli.tmp_dir);
if (test_enabled(cli.tests, "L1"))
run_simple(cli.tmp_dir, cli.csv_path, "L1", 100, 1 * KB, PAT_FILLER, "1KB body x100");
if (test_enabled(cli.tests, "L2"))
run_simple(cli.tmp_dir, cli.csv_path, "L2", 100, 10 * KB, PAT_FILLER, "10KB body x100");
if (test_enabled(cli.tests, "L3"))
run_simple(cli.tmp_dir, cli.csv_path, "L3", 100, 100 * KB, PAT_FILLER, "100KB body x100");
if (test_enabled(cli.tests, "L4"))
run_simple(cli.tmp_dir, cli.csv_path, "L4", 50, 1 * MB, PAT_FILLER, "1MB body x50");
if (test_enabled(cli.tests, "L5"))
run_simple(cli.tmp_dir, cli.csv_path, "L5", 10, 5 * MB, PAT_FILLER, "5MB body x10");
if (test_enabled(cli.tests, "L6"))
run_simple(cli.tmp_dir, cli.csv_path, "L6", 4, 9 * MB + 900 * KB, PAT_FILLER,
"9.9MB body x4 (just under FF_MAX_LINE_LEN)");
if (test_enabled(cli.tests, "L7"))
run_oversized(cli.tmp_dir, cli.csv_path);
if (test_enabled(cli.tests, "L8"))
run_simple(cli.tmp_dir, cli.csv_path, "L8", 50, 100 * KB, PAT_EMBEDDED_LF,
"100KB body w/ \\n every 100B");
if (test_enabled(cli.tests, "L9"))
run_simple(cli.tmp_dir, cli.csv_path, "L9", 50, 100 * KB, PAT_EMBEDDED_PIPE,
"100KB body w/ pipes");
if (test_enabled(cli.tests, "L10"))
run_simple(cli.tmp_dir, cli.csv_path, "L10", 50, 100 * KB, PAT_EMOJI,
"100KB body utf-8 emoji");
if (test_enabled(cli.tests, "L11"))
run_simple(cli.tmp_dir, cli.csv_path, "L11", 100, 1 * KB, PAT_FILLER, "sanity baseline");
if (test_enabled(cli.tests, "L12"))
run_pagination_with_huge(cli.tmp_dir, cli.csv_path);
if (test_enabled(cli.tests, "L13"))
run_simple(cli.tmp_dir, cli.csv_path, "L13", 100, 1 * MB, PAT_FILLER,
"verify-equiv full parse on 100x1MB");
if (test_enabled(cli.tests, "L14"))
run_simple(cli.tmp_dir, cli.csv_path, "L14", 1000, 100 * KB, PAT_FILLER,
"rapid append 1000x100KB");
return 0;
}

558
tests/bench/bench_runner.c Normal file
View File

@@ -0,0 +1,558 @@
// 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.
/*
* bench_runner.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Bench harness driver for the flat-file backend. Measures the underlying
* mechanics (state-build, sparse-index lookup, line throughput, verify).
*
* Pipeline:
* 1. Run gen_history first to populate $BENCH_DATA_DIR/contacts/<jid>/history.log.
* 2. ./bench_runner --data=DIR --csv=PATH [--scenarios=S1,S2,...]
* 3. Each scenario writes a row to the CSV.
*
* The harness does NOT invoke gen_history itself — that's the Makefile's
* job. This keeps the binary's deps minimal.
*
* Scenarios (S1S6 in P1):
* S1 cold tail-access open contact, drop cache, fetch last 100 lines
* S2 warm tail-access same as S1 but page-cache hot
* S3 deep pagination scroll back N=100 pages × 100 lines via index
* S4 first index build ff_state_new + ff_state_ensure_fresh on cold file
* S5 incremental extend append M lines, ensure_fresh -> measure (no rebuild)
* S6 verify integrity ff_verify_integrity over full file
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "bench_common.h"
#include "bench_csv.h"
#include "database_flatfile.h"
// g_flatfile_account_jid is declared in database_flatfile.h. We assign to
// it directly (skip _flatfile_init which pulls in xmpp / connection deps).
// ---------------------------------------------------------------------------
// CLI
typedef struct
{
const char* data_dir;
const char* csv_path;
const char* scenarios; // comma-separated list, or "all"
const char* account_jid;
int extend_lines; // for S5
} cli_t;
static void
cli_default(cli_t* c)
{
c->data_dir = NULL;
c->csv_path = NULL;
c->scenarios = "all";
c->account_jid = "bench@bench.example";
c->extend_lines = 1000;
}
static int
parse_args(int argc, char** argv, cli_t* c)
{
cli_default(c);
for (int i = 1; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--data=", 7) == 0)
c->data_dir = a + 7;
else if (strncmp(a, "--csv=", 6) == 0)
c->csv_path = a + 6;
else if (strncmp(a, "--scenarios=", 12) == 0)
c->scenarios = a + 12;
else if (strncmp(a, "--account=", 10) == 0)
c->account_jid = a + 10;
else if (strncmp(a, "--extend-lines=", 15) == 0)
c->extend_lines = atoi(a + 15);
else {
fprintf(stderr, "unknown option: %s\n", a);
return -1;
}
}
if (!c->data_dir) {
fprintf(stderr, "--data=DIR required\n");
return -1;
}
return 0;
}
static int
scenario_enabled(const char* list, const char* name)
{
if (!list || g_strcmp0(list, "all") == 0)
return 1;
auto_gcharv gchar** parts = g_strsplit(list, ",", -1);
for (int i = 0; parts[i]; i++) {
if (g_strcmp0(g_strstrip(parts[i]), name) == 0)
return 1;
}
return 0;
}
// ---------------------------------------------------------------------------
// Pick the largest history.log in the corpus — that's our "primary" contact.
typedef struct
{
char* path;
int64_t size;
int64_t lines; // best-effort
} corpus_pick_t;
static int64_t
count_lines(const char* path)
{
FILE* fp = fopen(path, "r");
if (!fp)
return -1;
int64_t n = 0;
int c;
while ((c = fgetc(fp)) != EOF) {
if (c == '\n')
n++;
}
fclose(fp);
return n;
}
static gboolean
pick_primary(const char* data_dir, const char* account_dir, corpus_pick_t* out)
{
auto_gchar gchar* contacts_root = g_strdup_printf("%s/flatlog/%s",
data_dir, account_dir);
GDir* d = g_dir_open(contacts_root, 0, NULL);
if (!d) {
fprintf(stderr, "cannot open %s\n", contacts_root);
return FALSE;
}
char* best_path = NULL;
int64_t best_size = 0;
const char* name;
while ((name = g_dir_read_name(d)) != NULL) {
auto_gchar gchar* path = g_strdup_printf("%s/%s/history.log", contacts_root, name);
struct stat st;
if (stat(path, &st) == 0 && S_ISREG(st.st_mode) && st.st_size > best_size) {
best_size = st.st_size;
g_free(best_path);
best_path = g_strdup(path);
}
}
g_dir_close(d);
if (!best_path) {
fprintf(stderr, "no history.log under %s\n", contacts_root);
return FALSE;
}
out->path = best_path;
out->size = best_size;
fprintf(stderr, " primary contact: %s (%" PRId64 " bytes)\n", best_path, best_size);
out->lines = count_lines(best_path);
fprintf(stderr, " primary lines: %" PRId64 "\n", out->lines);
return TRUE;
}
// ---------------------------------------------------------------------------
// Scenario S1/S2: tail-access via state index
//
// Build state for the file (fresh first time, cached subsequent), then for
// the last 100 lines:
// - find the byte offset of the (n_entries-1)-th sparse-index entry
// - seek there, read forward to EOF with ff_readline + ff_parse_line,
// keep the last 100 in a ring buffer.
// This mirrors the read path used by _flatfile_get_previous_chat without
// pulling in xmpp/connection deps.
#define TAIL_PAGE 100
static double
run_tail_access(const char* path, int drop_cache, long* peak_rss_kb, int64_t* lines_read)
{
if (drop_cache)
bench_drop_page_cache(path);
double t0 = bench_now_ms();
ff_contact_state_t* state = ff_state_new(path);
if (!ff_state_ensure_fresh(state)) {
ff_state_free(state);
if (lines_read)
*lines_read = 0;
return -1.0;
}
off_t start = 0;
if (state->n_entries > 1) {
start = state->entries[state->n_entries - 1].byte_offset;
} else {
start = state->bom_len; // tiny file
}
FILE* fp = fopen(path, "r");
if (!fp) {
ff_state_free(state);
return -1.0;
}
if (fseeko(fp, start, SEEK_SET) != 0) {
fclose(fp);
ff_state_free(state);
return -1.0;
}
char* ring[TAIL_PAGE];
memset(ring, 0, sizeof(ring));
int ring_pos = 0;
int64_t parsed = 0;
char* buf;
while ((buf = ff_readline(fp, NULL)) != NULL) {
if (buf[0] == '\0' || buf[0] == '#') {
free(buf);
continue;
}
ff_parsed_line_t* pl = ff_parse_line(buf);
free(buf);
if (!pl)
continue;
parsed++;
if (ring[ring_pos])
g_free(ring[ring_pos]);
ring[ring_pos] = g_strdup(pl->message ? pl->message : "");
ring_pos = (ring_pos + 1) % TAIL_PAGE;
ff_parsed_line_free(pl);
}
fclose(fp);
ff_state_free(state);
for (int i = 0; i < TAIL_PAGE; i++)
g_free(ring[i]);
double dt = bench_now_ms() - t0;
if (peak_rss_kb)
*peak_rss_kb = bench_peak_rss_kb();
if (lines_read)
*lines_read = parsed;
return dt;
}
// ---------------------------------------------------------------------------
// S3: deep pagination — for each of N pages, pick a synthetic ts hint and
// call ff_state_offset_for_time to locate the page start. Measures binary
// search throughput.
static double
run_deep_pagination(const char* path, int n_pages, long* peak_rss_kb)
{
ff_contact_state_t* state = ff_state_new(path);
if (!ff_state_ensure_fresh(state)) {
ff_state_free(state);
return -1.0;
}
if (state->n_entries < 2) {
ff_state_free(state);
return 0.0;
}
double t0 = bench_now_ms();
// Walk through index entries, calling ff_state_offset_for_time with their
// recorded epochs (cheaper than parsing, still exercises binary search).
for (int i = 0; i < n_pages; i++) {
size_t idx = (size_t)i % state->n_entries;
gint64 epoch = state->entries[idx].timestamp_epoch;
// Convert epoch to iso roughly — ff_state_offset_for_time parses ISO.
time_t t = (time_t)epoch;
GDateTime* dt = g_date_time_new_from_unix_utc((gint64)t);
auto_gchar gchar* iso = g_date_time_format_iso8601(dt);
g_date_time_unref(dt);
(void)ff_state_offset_for_time(state, iso);
}
double dt = bench_now_ms() - t0;
if (peak_rss_kb)
*peak_rss_kb = bench_peak_rss_kb();
ff_state_free(state);
return dt;
}
// ---------------------------------------------------------------------------
// S4: first-time build — drop cache, ff_state_new + ensure_fresh
static double
run_first_build(const char* path, long* peak_rss_kb, size_t* idx_entries)
{
bench_drop_page_cache(path);
double t0 = bench_now_ms();
ff_contact_state_t* state = ff_state_new(path);
int ok = ff_state_ensure_fresh(state);
double dt = bench_now_ms() - t0;
if (idx_entries)
*idx_entries = ok ? state->n_entries : 0;
if (peak_rss_kb)
*peak_rss_kb = bench_peak_rss_kb();
ff_state_free(state);
return ok ? dt : -1.0;
}
// ---------------------------------------------------------------------------
// S5: incremental extend — already-built state, then append N synthetic lines
// (writing them via ff_write_line directly) and call ensure_fresh to verify
// it takes the extend path, not full rebuild.
static double
run_incremental_extend(const char* path, int n_lines, long* peak_rss_kb,
int64_t* added_bytes)
{
ff_contact_state_t* state = ff_state_new(path);
if (!ff_state_ensure_fresh(state)) {
ff_state_free(state);
return -1.0;
}
size_t before_entries = state->n_entries;
size_t before_lines = state->total_lines;
(void)before_entries;
// Append n_lines lines directly to the file
FILE* fp = fopen(path, "a");
if (!fp) {
ff_state_free(state);
return -1.0;
}
int64_t before_size = -1;
{
struct stat st;
if (stat(path, &st) == 0)
before_size = st.st_size;
}
GDateTime* now = g_date_time_new_now_utc();
for (int i = 0; i < n_lines; i++) {
GDateTime* t = g_date_time_add_seconds(now, i);
auto_gchar gchar* iso = g_date_time_format_iso8601(t);
g_date_time_unref(t);
auto_gchar gchar* sid = g_strdup_printf("ext-%d-%ld", i, (long)random());
ff_write_line(fp, iso, "chat", "none",
sid, NULL, NULL,
"buddy000@bench.example", "ext",
NULL, NULL, -1,
"extend payload");
}
g_date_time_unref(now);
fclose(fp);
// Now measure: ensure_fresh should hit the extend path.
double t0 = bench_now_ms();
int ok = ff_state_ensure_fresh(state);
double dt = bench_now_ms() - t0;
if (added_bytes) {
struct stat st;
if (stat(path, &st) == 0 && before_size >= 0)
*added_bytes = (int64_t)st.st_size - before_size;
else
*added_bytes = -1;
}
if (peak_rss_kb)
*peak_rss_kb = bench_peak_rss_kb();
fprintf(stderr, " S5: lines before=%zu after=%zu (delta=%zu)\n",
before_lines, state->total_lines,
state->total_lines - before_lines);
ff_state_free(state);
return ok ? dt : -1.0;
}
// ---------------------------------------------------------------------------
// S6: verify integrity — calls the real ff_verify_integrity, walking the
// canonical $data/flatlog/$account/$contact/history.log layout that
// gen_history produces. We set g_flatfile_account_jid in main() and
// BENCH_DATA_DIR + the stubbed files_get_data_path() resolves the prefix.
static int
_count_level(GSList* issues, integrity_level_t level)
{
int n = 0;
for (GSList* l = issues; l; l = l->next) {
integrity_issue_t* i = (integrity_issue_t*)l->data;
if (i && i->level == level)
n++;
}
return n;
}
static double
run_verify(long* peak_rss_kb, int64_t* total_issues, int* errors, int* warnings, int* infos)
{
// Drop cache for everything under the flatlog tree we'll walk. Cheap
// compared to the verify itself, and keeps the cold-start signal clean.
auto_gchar gchar* data_path = g_strdup_printf("%s/flatlog", getenv("BENCH_DATA_DIR"));
(void)data_path;
double t0 = bench_now_ms();
GSList* issues = ff_verify_integrity(NULL); // NULL = all contacts
double dt = bench_now_ms() - t0;
int e = _count_level(issues, INTEGRITY_ERROR);
int w = _count_level(issues, INTEGRITY_WARNING);
int i = _count_level(issues, INTEGRITY_INFO);
int total = g_slist_length(issues);
// Surface up to 5 errors / 3 warnings — helps debug bench-vs-real mismatches.
int shown_err = 0, shown_warn = 0;
for (GSList* l = issues; l; l = l->next) {
integrity_issue_t* iss = (integrity_issue_t*)l->data;
if (!iss)
continue;
if (iss->level == INTEGRITY_ERROR && shown_err < 5) {
fprintf(stderr, " ERR %s:%d %s\n",
iss->file ? iss->file : "?", iss->line, iss->message ? iss->message : "");
shown_err++;
} else if (iss->level == INTEGRITY_WARNING && shown_warn < 3) {
fprintf(stderr, " WARN %s:%d %s\n",
iss->file ? iss->file : "?", iss->line, iss->message ? iss->message : "");
shown_warn++;
}
}
g_slist_free_full(issues, (GDestroyNotify)integrity_issue_free);
if (peak_rss_kb)
*peak_rss_kb = bench_peak_rss_kb();
if (total_issues)
*total_issues = total;
if (errors)
*errors = e;
if (warnings)
*warnings = w;
if (infos)
*infos = i;
return dt;
}
// ---------------------------------------------------------------------------
// Driver
int
main(int argc, char** argv)
{
cli_t cli;
if (parse_args(argc, argv, &cli) != 0)
return 2;
g_flatfile_account_jid = g_strdup(cli.account_jid);
auto_gchar gchar* account_dir = ff_jid_to_dir(cli.account_jid);
// Sanity: data_dir/flatlog/<account_dir>/ must exist
auto_gchar gchar* contacts_root = g_strdup_printf("%s/flatlog/%s",
cli.data_dir, account_dir);
if (!g_file_test(contacts_root, G_FILE_TEST_IS_DIR)) {
fprintf(stderr, "ERROR: %s does not exist. Run gen_history first "
"(make sure --account matches).\n",
contacts_root);
return 2;
}
corpus_pick_t pick = { 0 };
if (!pick_primary(cli.data_dir, account_dir, &pick))
return 2;
const char* volume_name = getenv("BENCH_VOLUME");
if (!volume_name)
volume_name = "medium";
fprintf(stderr, "===== bench_runner: data=%s volume=%s =====\n", cli.data_dir, volume_name);
// ---- S1: cold tail access
if (scenario_enabled(cli.scenarios, "S1")) {
long rss = 0;
int64_t lr = 0;
double dt = run_tail_access(pick.path, /*drop=*/1, &rss, &lr);
fprintf(stderr, " S1 cold tail-access: %.2f ms (read %" PRId64 " lines)\n", dt, lr);
bench_csv_append(cli.csv_path, "S1_cold_tail", volume_name,
(uint64_t)pick.size, (uint64_t)pick.lines, dt, rss,
"tail page=100");
}
// ---- S2: warm tail access (run twice, take the warm)
if (scenario_enabled(cli.scenarios, "S2")) {
long rss = 0;
int64_t lr = 0;
run_tail_access(pick.path, /*drop=*/0, NULL, NULL);
double dt = run_tail_access(pick.path, /*drop=*/0, &rss, &lr);
fprintf(stderr, " S2 warm tail-access: %.2f ms (read %" PRId64 " lines)\n", dt, lr);
bench_csv_append(cli.csv_path, "S2_warm_tail", volume_name,
(uint64_t)pick.size, (uint64_t)pick.lines, dt, rss,
"tail page=100");
}
// ---- S3: deep pagination
if (scenario_enabled(cli.scenarios, "S3")) {
long rss = 0;
double dt = run_deep_pagination(pick.path, /*n_pages=*/1000, &rss);
fprintf(stderr, " S3 deep pagination (1000 lookups): %.2f ms\n", dt);
bench_csv_append(cli.csv_path, "S3_deep_pagination", volume_name,
(uint64_t)pick.size, (uint64_t)pick.lines, dt, rss,
"n_pages=1000");
}
// ---- S4: first-time index build
if (scenario_enabled(cli.scenarios, "S4")) {
long rss = 0;
size_t entries = 0;
double dt = run_first_build(pick.path, &rss, &entries);
fprintf(stderr, " S4 first build: %.2f ms (idx entries=%zu)\n", dt, entries);
auto_gchar gchar* note = g_strdup_printf("idx_entries=%zu", entries);
bench_csv_append(cli.csv_path, "S4_first_build", volume_name,
(uint64_t)pick.size, (uint64_t)pick.lines, dt, rss, note);
}
// ---- S5: incremental extend
if (scenario_enabled(cli.scenarios, "S5")) {
long rss = 0;
int64_t added = 0;
double dt = run_incremental_extend(pick.path, cli.extend_lines, &rss, &added);
fprintf(stderr, " S5 incremental extend (%d lines, %" PRId64 " bytes): %.2f ms\n",
cli.extend_lines, added, dt);
auto_gchar gchar* note = g_strdup_printf("appended=%d_lines", cli.extend_lines);
bench_csv_append(cli.csv_path, "S5_incremental_extend", volume_name,
(uint64_t)added, (uint64_t)cli.extend_lines, dt, rss, note);
}
// ---- S6: verify integrity (real ff_verify_integrity over the tree)
if (scenario_enabled(cli.scenarios, "S6")) {
long rss = 0;
int64_t total = 0;
int errors = 0, warnings = 0, infos = 0;
double dt = run_verify(&rss, &total, &errors, &warnings, &infos);
fprintf(stderr,
" S6 verify_integrity: %.2f ms (total=%" PRId64
" err=%d warn=%d info=%d)\n",
dt, total, errors, warnings, infos);
auto_gchar gchar* note = g_strdup_printf(
"total=%" PRId64 " err=%d warn=%d info=%d", total, errors, warnings, infos);
bench_csv_append(cli.csv_path, "S6_verify", volume_name,
(uint64_t)pick.size, (uint64_t)pick.lines, dt, rss, note);
}
g_free(pick.path);
g_free(g_flatfile_account_jid);
g_flatfile_account_jid = NULL;
return 0;
}

375
tests/bench/bench_stubs.c Normal file
View File

@@ -0,0 +1,375 @@
// 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.
/*
* bench_stubs.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Minimal stubs so we can link the flat-file backend (database_flatfile*.c)
* into the bench harness without pulling in the rest of profanity (xmpp,
* ui, prefs, connection state, ...).
*
* Only ff_* helpers and ff_state_* / ff_verify_integrity are exercised
* by the harness, so most code paths inside database_flatfile.c that
* reference these symbols are never reached. The stubs exist purely to
* resolve the linker.
*
* log_* writes to stderr when BENCH_LOG=1 in the environment, otherwise
* silent — keeps the harness output clean while still letting us debug.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <glib.h>
#include "log.h"
#include "common.h"
#include "config/files.h"
#include "config/preferences.h"
#include "database.h"
#include "database_flatfile.h" // for ff_jid_to_dir() in stub
#include "xmpp/xmpp.h"
#include "xmpp/jid.h"
#include "xmpp/message.h"
#include "ui/ui.h"
// ---------------------------------------------------------------------------
// log_* (real impl, gated by BENCH_LOG=1)
static int
_bench_log_enabled(void)
{
static int cached = -1;
if (cached == -1) {
const char* env = getenv("BENCH_LOG");
cached = (env && env[0] && env[0] != '0') ? 1 : 0;
}
return cached;
}
static void
_bench_log(const char* level, const char* fmt, va_list ap)
{
if (!_bench_log_enabled())
return;
fprintf(stderr, "[%s] ", level);
vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
}
void
log_debug(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("DEBUG", msg, ap);
va_end(ap);
}
void
log_info(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("INFO", msg, ap);
va_end(ap);
}
void
log_warning(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("WARN", msg, ap);
va_end(ap);
}
void
log_error(const char* const msg, ...)
{
va_list ap;
va_start(ap, msg);
_bench_log("ERROR", msg, ap);
va_end(ap);
}
void
log_init(log_level_t filter, const char* const log_file)
{
(void)filter;
(void)log_file;
}
void
log_close(void)
{
}
void
log_msg(log_level_t level, const char* const area, const char* const msg)
{
(void)level;
(void)area;
(void)msg;
}
const char*
get_log_file_location(void)
{
return "";
}
log_level_t
log_get_filter(void)
{
return PROF_LEVEL_INFO;
}
int
log_level_from_string(char* log_level, log_level_t* level)
{
(void)log_level;
if (level)
*level = PROF_LEVEL_INFO;
return 0;
}
void
log_stderr_init(log_level_t level)
{
(void)level;
}
void
log_stderr_handler(void)
{
}
// ---------------------------------------------------------------------------
// prefs / files / xmpp / ui — never reached by the harness, but database_flatfile.c
// references them, so we resolve the symbols.
gchar*
prefs_get_string(preference_t pref)
{
(void)pref;
return NULL;
}
gboolean
prefs_get_boolean(preference_t pref)
{
(void)pref;
return FALSE;
}
void
prefs_set_string(preference_t pref, const gchar* new_value)
{
(void)pref;
(void)new_value;
}
void
prefs_set_boolean(preference_t pref, gboolean value)
{
(void)pref;
(void)value;
}
gchar*
files_get_data_path(const char* const location)
{
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
if (location && location[0])
return g_strdup_printf("%s/%s", base, location);
return g_strdup(base);
}
gchar*
files_get_config_path(const char* const location)
{
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
if (location && location[0])
return g_strdup_printf("%s/%s", base, location);
return g_strdup(base);
}
// $BENCH_DATA_DIR/$location/$jid_dir/$filename — mirrors the canonical
// per-account layout. Caller g_free's. Required by _get_db_filename in
// database_sqlite.c during _sqlite_init.
char*
files_file_in_account_data_path(const char* const location, const char* const account_jid,
const char* const filename)
{
if (!account_jid || !filename)
return NULL;
const char* base = getenv("BENCH_DATA_DIR");
if (!base || !base[0])
base = "/tmp/cproof-bench";
auto_gchar gchar* jid_dir = ff_jid_to_dir(account_jid);
auto_gchar gchar* parent = g_strdup_printf("%s/%s/%s", base,
location && location[0] ? location : "",
jid_dir);
g_mkdir_with_parents(parent, 0755);
return g_strdup_printf("%s/%s", parent, filename);
}
// jid_destroy is the public name; bench doesn't define a jid_destroy stub
// because src/xmpp/jid.c isn't linked in. We emulate just enough for cleanup.
static void
_bench_jid_free(Jid* j)
{
if (!j)
return;
g_free(j->barejid);
g_free(j->fulljid);
g_free(j->resourcepart);
g_free(j);
}
Jid*
jid_create(const gchar* const fulljid)
{
if (!fulljid)
return NULL;
Jid* j = g_malloc0(sizeof(Jid));
j->fulljid = g_strdup(fulljid);
const char* slash = strchr(fulljid, '/');
if (slash) {
j->barejid = g_strndup(fulljid, slash - fulljid);
j->resourcepart = g_strdup(slash + 1);
} else {
j->barejid = g_strdup(fulljid);
}
return j;
}
void
jid_destroy(Jid* jid)
{
_bench_jid_free(jid);
}
// Weak so that bench targets which also link real database.c (which defines
// the same symbol) take the strong version. bench_runner / bench_long_messages /
// bench_failure_modes don't link database.c and rely on this stub.
__attribute__((weak)) void
integrity_issue_free(integrity_issue_t* issue)
{
if (!issue)
return;
g_free(issue->file);
g_free(issue->message);
g_free(issue);
}
void
message_free(ProfMessage* m)
{
if (!m)
return;
_bench_jid_free(m->from_jid);
_bench_jid_free(m->to_jid);
g_free(m->id);
g_free(m->originid);
g_free(m->replace_id);
g_free(m->stanzaid);
g_free(m->body);
g_free(m->encrypted);
g_free(m->plain);
if (m->timestamp)
g_date_time_unref(m->timestamp);
g_free(m);
}
// connection_get_jid: returns a process-wide stub Jid built from
// $BENCH_ACCOUNT_JID (default "bench@bench.example"). Allocated lazily on
// first call, freed at exit via atexit().
static Jid* g_bench_jid;
static void
_bench_jid_atexit(void)
{
if (!g_bench_jid)
return;
g_free(g_bench_jid->barejid);
g_free(g_bench_jid->fulljid);
g_free(g_bench_jid->resourcepart);
g_free(g_bench_jid);
g_bench_jid = NULL;
}
const Jid*
connection_get_jid(void)
{
if (g_bench_jid)
return g_bench_jid;
const char* env = getenv("BENCH_ACCOUNT_JID");
if (!env || !env[0])
env = "bench@bench.example";
g_bench_jid = g_malloc0(sizeof(Jid));
g_bench_jid->barejid = g_strdup(env);
g_bench_jid->fulljid = g_strdup(env);
g_bench_jid->resourcepart = NULL;
atexit(_bench_jid_atexit);
return g_bench_jid;
}
ProfMessage*
message_init(void)
{
return g_malloc0(sizeof(ProfMessage));
}
Jid*
jid_create_from_bare_and_resource(const char* const barejid, const char* const resource)
{
if (!barejid)
return NULL;
Jid* j = g_malloc0(sizeof(Jid));
j->barejid = g_strdup(barejid);
j->resourcepart = resource ? g_strdup(resource) : NULL;
j->fulljid = resource ? g_strdup_printf("%s/%s", barejid, resource) : g_strdup(barejid);
return j;
}
void
cons_show(const char* const msg, ...)
{
(void)msg;
}
void
cons_show_error(const char* const cmd, ...)
{
(void)cmd;
}
// session/account stubs — used by database.c when wiring up backend switch.
// Bench never goes through the dispatcher's switch path; just satisfy linker.
const char*
session_get_account_name(void)
{
return NULL;
}
ProfAccount*
accounts_get_account(const char* const name)
{
(void)name;
return NULL;
}
void
account_free(ProfAccount* account)
{
if (!account)
return;
g_free(account->jid);
g_free(account);
}

136
tests/bench/compare_baseline.py Executable file
View File

@@ -0,0 +1,136 @@
#!/usr/bin/env python3
# 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.
"""
compare_baseline.py — diff a fresh `current.csv` against a committed
`baseline.csv` and exit non-zero on regressions.
Aggregation rule: same (scenario, volume) → median wall_ms across rows.
A regression is a > THRESHOLD percent slowdown vs. the baseline; speedups
of any size are reported but never fail the run.
Usage:
compare_baseline.py [--baseline=PATH] [--current=PATH]
[--threshold=PCT] [--quiet]
Exit codes:
0 — no regressions
1 — at least one regression
2 — input parse error
"""
import argparse
import csv
import statistics
import sys
from pathlib import Path
DEFAULT_THRESHOLD = 25.0 # percent slower vs baseline = regression
def load(path: Path) -> dict[tuple[str, str], list[float]]:
if not path.is_file():
return {}
rows: dict[tuple[str, str], list[float]] = {}
with path.open("r", newline="") as f:
rdr = csv.DictReader(f)
if not rdr.fieldnames or "scenario" not in rdr.fieldnames:
sys.exit(f"ERROR: {path} has no 'scenario' column")
for row in rdr:
try:
wall = float(row["wall_ms"])
except (KeyError, ValueError):
continue
key = (row.get("scenario", ""), row.get("volume", ""))
rows.setdefault(key, []).append(wall)
return rows
def median(values: list[float]) -> float:
return statistics.median(values) if values else 0.0
def fmt_ms(ms: float) -> str:
if ms < 1.0:
return f"{ms*1000:.0f} us"
if ms < 1000.0:
return f"{ms:.2f} ms"
if ms < 60000.0:
return f"{ms/1000:.2f} s"
return f"{ms/60000:.2f} min"
def main() -> int:
ap = argparse.ArgumentParser()
ap.add_argument("--baseline", default="tests/bench/baseline.csv")
ap.add_argument("--current", default="tests/bench/current.csv")
ap.add_argument("--threshold", type=float, default=DEFAULT_THRESHOLD,
help="regression threshold in percent (default 25)")
ap.add_argument("--quiet", action="store_true",
help="only print regressions, hide unchanged/improved rows")
args = ap.parse_args()
base_p, cur_p = Path(args.baseline), Path(args.current)
if not cur_p.is_file():
sys.exit(f"ERROR: --current {cur_p} not found (run `make bench` first)")
base = load(base_p)
cur = load(cur_p)
if not cur:
sys.exit(f"ERROR: {cur_p} has no rows")
keys = sorted(set(base) | set(cur))
regressions: list[tuple[str, str, float, float, float]] = []
improvements: list[tuple[str, str, float, float, float]] = []
new_rows: list[tuple[str, str, float]] = []
missing: list[tuple[str, str, float]] = []
print(f"{'scenario':<30s} {'volume':<14s} {'baseline':>12s} {'current':>12s} {'delta':>10s}")
print("-" * 84)
for k in keys:
sc, vol = k
b = median(base.get(k, []))
c = median(cur.get(k, []))
if k not in base:
new_rows.append((sc, vol, c))
if not args.quiet:
print(f"{sc:<30s} {vol:<14s} {'(new)':>12s} {fmt_ms(c):>12s} {'':>10s}")
continue
if k not in cur:
missing.append((sc, vol, b))
if not args.quiet:
print(f"{sc:<30s} {vol:<14s} {fmt_ms(b):>12s} {'(gone)':>12s} {'':>10s}")
continue
if b == 0:
continue
pct = (c - b) / b * 100.0
marker = ""
if pct >= args.threshold:
regressions.append((sc, vol, b, c, pct))
marker = " REGRESSION"
elif pct <= -args.threshold:
improvements.append((sc, vol, b, c, pct))
marker = " improved"
if marker or not args.quiet:
sign = "+" if pct >= 0 else ""
print(f"{sc:<30s} {vol:<14s} {fmt_ms(b):>12s} {fmt_ms(c):>12s} {sign}{pct:>8.1f}%{marker}")
print()
print(f"summary: {len(regressions)} regressions, {len(improvements)} improvements, "
f"{len(new_rows)} new rows, {len(missing)} removed rows "
f"(threshold = ±{args.threshold:.0f} %)")
if regressions:
print("REGRESSED scenarios:")
for sc, vol, b, c, pct in regressions:
print(f" - {sc} ({vol}): {fmt_ms(b)}{fmt_ms(c)} (+{pct:.1f}%)")
return 1
return 0
if __name__ == "__main__":
sys.exit(main())

646
tests/bench/gen_history.c Normal file
View File

@@ -0,0 +1,646 @@
// 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.
/*
* gen_history.c
* vim: expandtab:ts=4:sts=4:sw=4
*
* Synthetic history generator for the flat-file backend bench harness.
* Writes deterministic, ff_write_line-compatible logs covering:
* - configurable line counts, contact counts, year span;
* - mixed message-length profiles (tiny/medium/long/paste-bomb/extreme);
* - stanza-id strategies (uuid / libpurple-incremental / conversations / mixed);
* - configurable LMC rate and MAM out-of-order rate;
* - resource diversity per contact.
*
* Output layout (canonical, matches files_get_data_path/jid_to_dir):
* <output>/flatlog/<account_jid_to_dir>/<contact_jid_to_dir>/history.log
* <output>/manifest.txt # one line per generated file with size + line count
*
* CLI:
* gen_history [options]
*
* Options:
* --account=JID account-side JID for path layout (default bench@bench.example)
* --lines=N total lines to generate (default 10000)
* --contacts=K distinct contact JIDs (default 1)
* --years=Y span (default 1, ts spread over Y years ending now)
* --seed=S RNG seed (default 42)
* --stanza-id={uuid|libpurple|conversations|mixed} default uuid
* --lmc-rate=PCT percent of lines that are LMC corrections (0..50)
* --mam-ooo-rate=PCT percent of lines with intentionally non-monotonic ts (0..50)
* --resources-per-contact=R default 3
* --msg-len-profile={short|mixed|long|extreme} default mixed
* --output=DIR output directory (default /tmp/cproof-bench-corpus)
* --quiet suppress progress prints
*
* Exit code: 0 on success, non-zero on error.
*/
#include "config.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "database_flatfile.h"
// ---------------------------------------------------------------------------
// CLI options
typedef enum {
SID_UUID,
SID_LIBPURPLE,
SID_CONVERSATIONS,
SID_MIXED,
} sid_mode_t;
typedef enum {
LEN_SHORT,
LEN_MIXED,
LEN_LONG,
LEN_EXTREME,
} len_profile_t;
typedef struct
{
int64_t lines;
int contacts;
int years;
uint64_t seed;
sid_mode_t sid_mode;
int lmc_rate;
int ooo_rate;
int resources_per_contact;
len_profile_t len_profile;
const char* output_dir;
const char* account_jid;
int quiet;
} opts_t;
static void
opts_default(opts_t* o)
{
o->lines = 10000;
o->contacts = 1;
o->years = 1;
o->seed = 42;
o->sid_mode = SID_UUID;
o->lmc_rate = 3;
o->ooo_rate = 0;
o->resources_per_contact = 3;
o->len_profile = LEN_MIXED;
o->output_dir = "/tmp/cproof-bench-corpus";
o->account_jid = "bench@bench.example";
o->quiet = 0;
}
static int
parse_sid_mode(const char* s, sid_mode_t* out)
{
if (g_strcmp0(s, "uuid") == 0) {
*out = SID_UUID;
return 0;
}
if (g_strcmp0(s, "libpurple") == 0) {
*out = SID_LIBPURPLE;
return 0;
}
if (g_strcmp0(s, "conversations") == 0) {
*out = SID_CONVERSATIONS;
return 0;
}
if (g_strcmp0(s, "mixed") == 0) {
*out = SID_MIXED;
return 0;
}
return -1;
}
static int
parse_len_profile(const char* s, len_profile_t* out)
{
if (g_strcmp0(s, "short") == 0) {
*out = LEN_SHORT;
return 0;
}
if (g_strcmp0(s, "mixed") == 0) {
*out = LEN_MIXED;
return 0;
}
if (g_strcmp0(s, "long") == 0) {
*out = LEN_LONG;
return 0;
}
if (g_strcmp0(s, "extreme") == 0) {
*out = LEN_EXTREME;
return 0;
}
return -1;
}
static int
parse_args(int argc, char** argv, opts_t* o)
{
opts_default(o);
for (int i = 1; i < argc; i++) {
const char* a = argv[i];
if (strncmp(a, "--lines=", 8) == 0) {
o->lines = strtoll(a + 8, NULL, 10);
} else if (strncmp(a, "--contacts=", 11) == 0) {
o->contacts = atoi(a + 11);
} else if (strncmp(a, "--years=", 8) == 0) {
o->years = atoi(a + 8);
} else if (strncmp(a, "--seed=", 7) == 0) {
o->seed = strtoull(a + 7, NULL, 10);
} else if (strncmp(a, "--stanza-id=", 12) == 0) {
if (parse_sid_mode(a + 12, &o->sid_mode) != 0) {
fprintf(stderr, "bad --stanza-id\n");
return -1;
}
} else if (strncmp(a, "--lmc-rate=", 11) == 0) {
o->lmc_rate = atoi(a + 11);
} else if (strncmp(a, "--mam-ooo-rate=", 15) == 0) {
o->ooo_rate = atoi(a + 15);
} else if (strncmp(a, "--resources-per-contact=", 24) == 0) {
o->resources_per_contact = atoi(a + 24);
} else if (strncmp(a, "--msg-len-profile=", 18) == 0) {
if (parse_len_profile(a + 18, &o->len_profile) != 0) {
fprintf(stderr, "bad --msg-len-profile\n");
return -1;
}
} else if (strncmp(a, "--output=", 9) == 0) {
o->output_dir = a + 9;
} else if (strncmp(a, "--account=", 10) == 0) {
o->account_jid = a + 10;
} else if (strcmp(a, "--quiet") == 0) {
o->quiet = 1;
} else if (strcmp(a, "--help") == 0 || strcmp(a, "-h") == 0) {
return 1;
} else {
fprintf(stderr, "unknown option: %s\n", a);
return -1;
}
}
if (o->lines <= 0 || o->contacts <= 0 || o->years <= 0)
return -1;
if (o->lmc_rate < 0 || o->lmc_rate > 50)
return -1;
if (o->ooo_rate < 0 || o->ooo_rate > 50)
return -1;
if (o->resources_per_contact <= 0)
return -1;
return 0;
}
static void
print_help(void)
{
fputs(
"Usage: gen_history [options]\n"
" --lines=N total lines (default 10000)\n"
" --contacts=K contact count (default 1)\n"
" --years=Y year span (default 1)\n"
" --seed=S RNG seed (default 42)\n"
" --stanza-id={uuid|libpurple|conversations|mixed}\n"
" --lmc-rate=PCT 0..50, default 3\n"
" --mam-ooo-rate=PCT 0..50, default 0\n"
" --resources-per-contact=R default 3\n"
" --msg-len-profile={short|mixed|long|extreme}\n"
" --output=DIR default /tmp/cproof-bench-corpus\n"
" --account=JID account JID (default bench@bench.example)\n"
" --quiet suppress progress prints\n",
stderr);
}
// ---------------------------------------------------------------------------
// Body / stanza-id / resource generators (deterministic)
static const char* MSG_BANK_TINY[] = {
"ok",
"yes",
"no",
"ack",
"thx",
"ttyl",
"k",
"lol",
"+1",
"yep",
"nope",
"sure",
"got it",
"great",
"afk",
"brb",
"bye",
"hi",
"hello",
"hey",
};
static const int MSG_BANK_TINY_N = sizeof(MSG_BANK_TINY) / sizeof(MSG_BANK_TINY[0]);
static const char* MSG_BANK_MEDIUM[] = {
"Quick reminder, the meeting starts at 14:00 in conf room B.",
"Pushed the patch to the staging branch, please review when you get a chance.",
"Ran into a weird issue with the parser — investigating.",
"Backups completed for the night, all green on the dashboard.",
"Yeah I think the second approach is cleaner — let's go with that.",
"Не могу подключиться к серверу с обеда, кто-то ещё видит проблему?",
"私もそう思います。ところで、明日の予定はどうしますか?",
"Just FYI: the migration will run during the maintenance window tonight.",
};
static const int MSG_BANK_MEDIUM_N = sizeof(MSG_BANK_MEDIUM) / sizeof(MSG_BANK_MEDIUM[0]);
// Linear-congruential — we don't need cryptographic strength, we need fast and
// reproducible. xorshift64 hits both.
static uint64_t
xorshift64(uint64_t* s)
{
uint64_t x = *s;
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
*s = x;
return x;
}
static int
rng_int(uint64_t* s, int min_inc, int max_exc)
{
if (max_exc <= min_inc)
return min_inc;
return min_inc + (int)(xorshift64(s) % (uint64_t)(max_exc - min_inc));
}
static void
fill_filler_text(char* buf, size_t n, uint64_t* rng)
{
static const char alpha[] = "abcdefghijklmnopqrstuvwxyz0123456789 ";
static const int alpha_n = sizeof(alpha) - 1;
for (size_t i = 0; i < n; i++)
buf[i] = alpha[xorshift64(rng) % alpha_n];
if (n > 0)
buf[n - 1] = '\0';
}
// Pick a body length per the profile distribution (matches table in REVIEW.txt
// Phase 9 plan section 2):
// tiny 50% (5-100 B) long 8% (500-5000 B) extreme 0.5% (100KB-1MB)
// medium 40% (100-500 B) paste 1.5%(5KB-100KB)
static size_t
pick_body_len(uint64_t* rng, len_profile_t profile)
{
int r = (int)(xorshift64(rng) % 1000);
if (profile == LEN_SHORT) {
return 5 + (xorshift64(rng) % 96);
}
if (profile == LEN_LONG) {
if (r < 200)
return 5 + (xorshift64(rng) % 96);
if (r < 600)
return 100 + (xorshift64(rng) % 400);
if (r < 850)
return 1000 + (xorshift64(rng) % 9000); // 1-10 KB
if (r < 980)
return 10000 + (xorshift64(rng) % 90000); // 10-100 KB
return 100000 + (xorshift64(rng) % 900000); // 100KB-1MB
}
if (profile == LEN_EXTREME) {
if (r < 100)
return 5 + (xorshift64(rng) % 96);
if (r < 400)
return 100000 + (xorshift64(rng) % 900000); // 100KB-1MB
if (r < 800)
return 1000000 + (xorshift64(rng) % 4000000); // 1-5 MB
return 5000000 + (xorshift64(rng) % 4000000); // 5-9 MB
}
// LEN_MIXED — realistic distribution
if (r < 500)
return 5 + (xorshift64(rng) % 96);
if (r < 900)
return 100 + (xorshift64(rng) % 400);
if (r < 980)
return 500 + (xorshift64(rng) % 4500); // 500B-5KB
if (r < 995)
return 5000 + (xorshift64(rng) % 95000); // 5-100 KB
return 100000 + (xorshift64(rng) % 900000); // 100KB-1MB
}
static char*
make_body(uint64_t* rng, size_t target_len)
{
if (target_len < 100) {
// Pick from tiny bank, optionally pad with filler
const char* base = MSG_BANK_TINY[xorshift64(rng) % MSG_BANK_TINY_N];
size_t blen = strlen(base);
if (blen >= target_len)
return g_strdup(base);
char* buf = g_malloc(target_len + 1);
memcpy(buf, base, blen);
fill_filler_text(buf + blen, target_len - blen + 1, rng);
buf[target_len] = '\0';
return buf;
}
if (target_len < 500) {
const char* base = MSG_BANK_MEDIUM[xorshift64(rng) % MSG_BANK_MEDIUM_N];
size_t blen = strlen(base);
if (blen >= target_len) {
// Truncate at UTF-8 codepoint boundary so we don't emit invalid UTF-8.
const char* p = base;
const char* end = base + target_len;
const char* last = base;
while (*p && p < end) {
const char* next = g_utf8_next_char(p);
if (next > end)
break;
last = next;
p = next;
}
return g_strndup(base, last - base);
}
char* buf = g_malloc(target_len + 1);
memcpy(buf, base, blen);
fill_filler_text(buf + blen, target_len - blen + 1, rng);
buf[target_len] = '\0';
return buf;
}
// Large body — repeat a deterministic pseudo-paragraph.
char* buf = g_malloc(target_len + 1);
fill_filler_text(buf, target_len + 1, rng);
// Sprinkle newlines so the escape path gets exercised on long bodies.
for (size_t i = 80; i < target_len; i += 80) {
if ((xorshift64(rng) & 0x7) == 0)
buf[i] = '\n';
}
buf[target_len] = '\0';
return buf;
}
static char*
make_stanza_id(uint64_t* rng, sid_mode_t mode, int64_t global_seq, int contact_idx)
{
sid_mode_t effective = mode;
if (mode == SID_MIXED) {
int r = (int)(xorshift64(rng) % 3);
effective = (r == 0) ? SID_UUID : (r == 1) ? SID_LIBPURPLE
: SID_CONVERSATIONS;
}
switch (effective) {
case SID_LIBPURPLE:
// Per-contact incremental — collides across contacts (intentional, mirrors libpurple).
return g_strdup_printf("%" PRId64, global_seq);
case SID_CONVERSATIONS:
{
// Conversations-style: 22 base32-ish chars
static const char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
char buf[24];
for (int i = 0; i < 22; i++)
buf[i] = alpha[xorshift64(rng) % (sizeof(alpha) - 1)];
buf[22] = '\0';
return g_strdup(buf);
}
case SID_UUID:
case SID_MIXED:
default:
{
// RFC4122-ish UUID v4 (deterministic from RNG, not cryptographic)
uint64_t a = xorshift64(rng);
uint64_t b = xorshift64(rng);
return g_strdup_printf("%08x-%04x-4%03x-%04x-%012lx",
(unsigned)(a >> 32),
(unsigned)((a >> 16) & 0xffff),
(unsigned)(a & 0xfff),
(unsigned)((b >> 48) & 0xffff) | 0x8000,
(unsigned long)(b & 0xffffffffffffUL));
}
}
(void)contact_idx;
}
// ---------------------------------------------------------------------------
// Output
typedef struct
{
char* jid;
char** resources; // n = opts.resources_per_contact
int n_resources;
char* dir;
char* path;
FILE* fp;
int64_t seq; // libpurple-style counter
int64_t lines_written;
int64_t bytes_written;
char* last_stanza_id; // for LMC corrections
} contact_t;
static contact_t*
contact_init(int idx, const opts_t* o, const char* account_dir)
{
contact_t* c = g_new0(contact_t, 1);
c->jid = g_strdup_printf("buddy%03d@bench.example", idx);
c->n_resources = o->resources_per_contact;
c->resources = g_new0(char*, c->n_resources);
for (int r = 0; r < c->n_resources; r++) {
c->resources[r] = g_strdup_printf("res%d-c%d", r, idx);
}
auto_gchar gchar* contact_dir = ff_jid_to_dir(c->jid);
c->dir = g_strdup_printf("%s/flatlog/%s/%s",
o->output_dir, account_dir, contact_dir);
c->path = g_strdup_printf("%s/history.log", c->dir);
if (g_mkdir_with_parents(c->dir, 0755) != 0) {
fprintf(stderr, "mkdir %s failed\n", c->dir);
exit(2);
}
c->fp = fopen(c->path, "w");
if (!c->fp) {
fprintf(stderr, "fopen %s failed\n", c->path);
exit(2);
}
fprintf(c->fp, "%s", FLATFILE_HEADER);
return c;
}
static void
contact_close(contact_t* c)
{
if (c->fp) {
fclose(c->fp);
struct stat st;
if (stat(c->path, &st) == 0)
c->bytes_written = st.st_size;
}
g_free(c->jid);
for (int r = 0; r < c->n_resources; r++)
g_free(c->resources[r]);
g_free(c->resources);
g_free(c->dir);
g_free(c->path);
g_free(c->last_stanza_id);
g_free(c);
}
// ---------------------------------------------------------------------------
// Main loop
static char*
iso_for_seq(int64_t seq, int64_t total, int years, int ooo_jitter_min)
{
// Spread seq evenly across [now - years, now], add small jitter for ooo.
time_t now = time(NULL);
time_t span = (time_t)years * 365 * 24 * 3600;
time_t base = now - span;
double frac = (double)seq / (double)(total > 0 ? total : 1);
time_t t = base + (time_t)(frac * (double)span);
// ooo_jitter_min in minutes — subtract for effect.
t -= (time_t)ooo_jitter_min * 60;
GDateTime* dt = g_date_time_new_from_unix_utc((gint64)t);
char* iso = g_date_time_format_iso8601(dt);
g_date_time_unref(dt);
return iso;
}
int
main(int argc, char** argv)
{
opts_t o;
int r = parse_args(argc, argv, &o);
if (r == 1) {
print_help();
return 0;
}
if (r != 0) {
print_help();
return 2;
}
if (g_mkdir_with_parents(o.output_dir, 0755) != 0) {
fprintf(stderr, "cannot create %s\n", o.output_dir);
return 2;
}
auto_gchar gchar* account_dir = ff_jid_to_dir(o.account_jid);
if (!o.quiet) {
fprintf(stderr,
"gen_history: account=%s (dir=%s) lines=%" PRId64 " contacts=%d years=%d "
"seed=%" PRIu64 " sid=%d lmc=%d%% ooo=%d%% res=%d profile=%d output=%s\n",
o.account_jid, account_dir, o.lines, o.contacts, o.years, o.seed,
(int)o.sid_mode, o.lmc_rate, o.ooo_rate,
o.resources_per_contact, (int)o.len_profile, o.output_dir);
}
contact_t** contacts = g_new0(contact_t*, o.contacts);
for (int i = 0; i < o.contacts; i++)
contacts[i] = contact_init(i, &o, account_dir);
uint64_t rng = o.seed ? o.seed : 1;
double t0 = (double)g_get_monotonic_time() / 1000.0;
int64_t per_contact_target = o.lines / o.contacts;
int64_t leftover = o.lines % o.contacts;
int64_t total_written = 0;
int64_t total_lmc = 0;
int64_t total_ooo = 0;
for (int ci = 0; ci < o.contacts; ci++) {
contact_t* c = contacts[ci];
int64_t target = per_contact_target + (ci < leftover ? 1 : 0);
for (int64_t li = 0; li < target; li++) {
c->seq++;
int is_lmc = c->last_stanza_id && o.lmc_rate > 0
&& ((int)(xorshift64(&rng) % 100) < o.lmc_rate);
int is_ooo = o.ooo_rate > 0
&& ((int)(xorshift64(&rng) % 100) < o.ooo_rate);
int ooo_jitter = is_ooo ? rng_int(&rng, 1, 600) : 0; // up to 10h backwards
auto_gchar gchar* iso = iso_for_seq(total_written, o.lines, o.years, ooo_jitter);
const char* res = c->resources[xorshift64(&rng) % c->n_resources];
auto_gchar gchar* sid = make_stanza_id(&rng, o.sid_mode, c->seq, ci);
auto_gchar gchar* aid = (xorshift64(&rng) & 1)
? make_stanza_id(&rng, SID_CONVERSATIONS, c->seq, ci)
: NULL;
const char* replace_id = is_lmc ? c->last_stanza_id : NULL;
size_t blen = pick_body_len(&rng, o.len_profile);
auto_gchar gchar* body = make_body(&rng, blen);
ff_write_line(c->fp, iso, "chat", "none",
sid, aid, replace_id,
c->jid, res,
NULL, NULL, -1,
body);
if (!is_lmc) {
g_free(c->last_stanza_id);
c->last_stanza_id = g_strdup(sid);
} else {
total_lmc++;
}
if (is_ooo)
total_ooo++;
c->lines_written++;
total_written++;
if (!o.quiet && (total_written % 100000) == 0) {
double dt = (double)g_get_monotonic_time() / 1000.0 - t0;
fprintf(stderr, " written %" PRId64 " / %" PRId64 " lines (%.1fs, %.0f lines/s)\n",
total_written, o.lines, dt / 1000.0,
(double)total_written / (dt / 1000.0 + 1e-9));
}
}
}
// Manifest
auto_gchar gchar* manifest_path = g_strdup_printf("%s/manifest.txt", o.output_dir);
FILE* mf = fopen(manifest_path, "w");
int64_t total_bytes = 0;
for (int i = 0; i < o.contacts; i++) {
contact_close(contacts[i]);
}
for (int i = 0; i < o.contacts; i++) {
// contact_close already closed the file; use stat for sizes.
// The contact_t was freed though, so this is a noop. We re-stat via path:
}
// Manifest after close — re-stat per contact via known path layout.
if (mf) {
for (int i = 0; i < o.contacts; i++) {
auto_gchar gchar* jid = g_strdup_printf("buddy%03d@bench.example", i);
auto_gchar gchar* contact_dir = ff_jid_to_dir(jid);
auto_gchar gchar* path = g_strdup_printf("%s/flatlog/%s/%s/history.log",
o.output_dir, account_dir, contact_dir);
struct stat st;
int64_t sz = (stat(path, &st) == 0) ? (int64_t)st.st_size : -1;
if (sz > 0)
total_bytes += sz;
fprintf(mf, "%s %" PRId64 " bytes\n", path, sz);
}
fprintf(mf, "TOTAL %" PRId64 " bytes\n", total_bytes);
fclose(mf);
}
g_free(contacts);
if (!o.quiet) {
double dt = (double)g_get_monotonic_time() / 1000.0 - t0;
fprintf(stderr,
"gen_history: done in %.1fs — wrote %" PRId64 " lines (%" PRId64 " LMC, %" PRId64 " OOO), "
"%" PRId64 " bytes total. Manifest: %s\n",
dt / 1000.0, total_written, total_lmc, total_ooo, total_bytes, manifest_path);
}
return 0;
}

View File

@@ -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);

View File

@@ -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(&regex, 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(&regex, output_buffer, 0, NULL, 0);
if (ret == 0) {
regfree(&regex);
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(&regex);
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'/>");
}

View File

@@ -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

File diff suppressed because it is too large Load Diff

View 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);

View 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 (&amp; &lt; &gt; &quot;) 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: &amp; &lt;tag&gt; &quot;quoted&quot;</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 &#10; 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&#10;nl-second-line-9m&#10;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"));
}

View 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);

View File

@@ -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");

View File

@@ -23,7 +23,15 @@
#include <glib.h>
#include "prof_cmocka.h"
#include "config.h"
#include "database.h"
#include "database_flatfile.h"
db_backend_t* active_db_backend = NULL;
// The flatfile parser needs this global for path construction.
// In tests we keep it NULL since no real file I/O happens via the backend.
char* g_flatfile_account_jid = NULL;
gboolean
log_database_init(ProfAccount* account)
@@ -50,3 +58,66 @@ void
log_database_close(void)
{
}
gboolean
log_database_switch_backend(const char* new_backend)
{
return TRUE;
}
GSList*
log_database_verify_integrity(const gchar* const contact_barejid)
{
return NULL;
}
void
integrity_issue_free(integrity_issue_t* issue)
{
if (issue) {
g_free(issue->file);
g_free(issue->message);
g_free(issue);
}
}
#ifdef HAVE_SQLITE
db_backend_t*
db_backend_sqlite(void)
{
return NULL;
}
GSList*
db_sqlite_list_contacts(void)
{
return NULL;
}
GSList*
db_sqlite_get_all_chat(const gchar* const contact_barejid)
{
return NULL;
}
void
db_sqlite_begin_transaction(void)
{
}
void
db_sqlite_end_transaction(void)
{
}
void
db_sqlite_rollback_transaction(void)
{
}
int
log_database_export_to_flatfile(const gchar* const contact_jid)
{
return 0;
}
int
log_database_import_from_flatfile(const gchar* const contact_jid)
{
return 0;
}
#endif
db_backend_t*
db_backend_flatfile(void)
{
return NULL;
}

View File

@@ -0,0 +1,783 @@
// 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"));
}

View File

@@ -0,0 +1,60 @@
/* test_database_export.h — unit tests for flatfile write/parse round-trip,
* escape/unescape, and jid_to_dir helpers used by export/import. */
/* write → parse round-trip */
void test_ff_roundtrip_simple_chat(void** state);
void test_ff_roundtrip_with_all_metadata(void** state);
void test_ff_roundtrip_with_resource(void** state);
void test_ff_roundtrip_newline_in_body(void** state);
void test_ff_roundtrip_pipe_in_stanza_id(void** state);
void test_ff_roundtrip_backslash_in_body(void** state);
void test_ff_roundtrip_unicode_body(void** state);
void test_ff_roundtrip_empty_body(void** state);
void test_ff_roundtrip_colonspace_in_resource(void** state);
void test_ff_roundtrip_muc_type(void** state);
void test_ff_roundtrip_omemo_enc(void** state);
void test_ff_roundtrip_replace_id(void** state);
void test_ff_roundtrip_to_jid_and_marked_read(void** state);
/* escape / unescape symmetry */
void test_ff_escape_unescape_message_identity(void** state);
void test_ff_escape_unescape_meta_identity(void** state);
void test_ff_escape_meta_null_returns_null(void** state);
void test_ff_escape_message_null_returns_empty(void** state);
/* jid_to_dir */
void test_ff_jid_to_dir_simple(void** state);
void test_ff_jid_to_dir_with_at(void** state);
void test_ff_jid_to_dir_path_traversal_rejected(void** state);
void test_ff_jid_to_dir_null(void** state);
/* parser edge cases */
void test_ff_parse_line_empty(void** state);
void test_ff_parse_line_comment(void** state);
void test_ff_parse_line_legacy_format(void** state);
void test_ff_parse_line_no_metadata(void** state);
void test_ff_parse_line_invalid_timestamp(void** state);
void test_ff_parse_line_empty_timestamp(void** state);
void test_ff_parse_line_partial_iso_timestamp(void** state);
void test_ff_parse_line_garbage_timestamp(void** state);
void test_ff_parse_line_impossible_calendar_date(void** state);
void test_ff_parse_line_negative_year_timestamp(void** state);
void test_ff_parse_line_far_future_timestamp(void** state);
/* type/enc conversion round-trip */
void test_ff_type_str_roundtrip(void** state);
void test_ff_enc_str_roundtrip(void** state);
/* additional round-trip */
void test_ff_roundtrip_bracket_in_stanza_id(void** state);
void test_ff_roundtrip_backslash_in_resource(void** state);
void test_ff_roundtrip_mucpm_type(void** state);
void test_ff_roundtrip_all_enc_types(void** state);
void test_ff_roundtrip_crlf_handling(void** state);
void test_ff_roundtrip_to_jid_special_chars(void** state);
void test_ff_roundtrip_multiple_lines(void** state);
/* additional parser edge cases */
void test_ff_parsed_line_free_null_safe(void** state);
void test_ff_parse_line_no_space_rejected(void** state);
void test_ff_parse_line_unclosed_bracket(void** state);

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
/* test_database_stress.h — stress tests for database I/O, MUC, large messages,
* export/import roundtrip under load, and index/cache correctness. */
/* High-throughput write + parse */
void test_stress_rapid_write_parse_1000(void** state);
void test_stress_rapid_write_parse_10000(void** state);
/* MUC (chat room) messages */
void test_stress_muc_many_participants(void** state);
void test_stress_muc_rapid_messages(void** state);
/* Large messages */
void test_stress_large_message_1mb(void** state);
void test_stress_large_message_body_with_special_chars(void** state);
void test_stress_many_large_messages_100(void** state);
/* Index and ID cache correctness under load */
void test_stress_index_build_10000_lines(void** state);
void test_stress_index_extend_append(void** state);
void test_stress_id_cache_dedup_correctness(void** state);
/* Export / import roundtrip under load */
void test_stress_export_merge_dedup_1000(void** state);
void test_stress_mixed_types_and_encodings(void** state);
/* LMC correction chains under load */
void test_stress_lmc_chain_deep(void** state);
void test_stress_lmc_many_corrections(void** state);

View File

@@ -36,6 +36,8 @@
#include "test_form.h"
#include "test_callbacks.h"
#include "test_plugins_disco.h"
#include "test_database_export.h"
#include "test_database_stress.h"
#define muc_unit_test(f) cmocka_unit_test_setup_teardown(f, muc_before_test, muc_after_test)
@@ -656,6 +658,80 @@ 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),
// Flatfile export/import round-trip
cmocka_unit_test(test_ff_roundtrip_simple_chat),
cmocka_unit_test(test_ff_roundtrip_with_all_metadata),
cmocka_unit_test(test_ff_roundtrip_with_resource),
cmocka_unit_test(test_ff_roundtrip_newline_in_body),
cmocka_unit_test(test_ff_roundtrip_pipe_in_stanza_id),
cmocka_unit_test(test_ff_roundtrip_backslash_in_body),
cmocka_unit_test(test_ff_roundtrip_unicode_body),
cmocka_unit_test(test_ff_roundtrip_empty_body),
cmocka_unit_test(test_ff_roundtrip_colonspace_in_resource),
cmocka_unit_test(test_ff_roundtrip_muc_type),
cmocka_unit_test(test_ff_roundtrip_omemo_enc),
cmocka_unit_test(test_ff_roundtrip_replace_id),
cmocka_unit_test(test_ff_roundtrip_to_jid_and_marked_read),
// Escape / unescape
cmocka_unit_test(test_ff_escape_unescape_message_identity),
cmocka_unit_test(test_ff_escape_unescape_meta_identity),
cmocka_unit_test(test_ff_escape_meta_null_returns_null),
cmocka_unit_test(test_ff_escape_message_null_returns_empty),
// jid_to_dir
cmocka_unit_test(test_ff_jid_to_dir_simple),
cmocka_unit_test(test_ff_jid_to_dir_with_at),
cmocka_unit_test(test_ff_jid_to_dir_path_traversal_rejected),
cmocka_unit_test(test_ff_jid_to_dir_null),
// Parser edge cases
cmocka_unit_test(test_ff_parse_line_empty),
cmocka_unit_test(test_ff_parse_line_comment),
cmocka_unit_test(test_ff_parse_line_legacy_format),
cmocka_unit_test(test_ff_parse_line_no_metadata),
cmocka_unit_test(test_ff_parse_line_invalid_timestamp),
cmocka_unit_test(test_ff_parse_line_empty_timestamp),
cmocka_unit_test(test_ff_parse_line_partial_iso_timestamp),
cmocka_unit_test(test_ff_parse_line_garbage_timestamp),
cmocka_unit_test(test_ff_parse_line_impossible_calendar_date),
cmocka_unit_test(test_ff_parse_line_negative_year_timestamp),
cmocka_unit_test(test_ff_parse_line_far_future_timestamp),
// Type/enc round-trip
cmocka_unit_test(test_ff_type_str_roundtrip),
cmocka_unit_test(test_ff_enc_str_roundtrip),
// Additional round-trip
cmocka_unit_test(test_ff_roundtrip_bracket_in_stanza_id),
cmocka_unit_test(test_ff_roundtrip_backslash_in_resource),
cmocka_unit_test(test_ff_roundtrip_mucpm_type),
cmocka_unit_test(test_ff_roundtrip_all_enc_types),
cmocka_unit_test(test_ff_roundtrip_crlf_handling),
cmocka_unit_test(test_ff_roundtrip_to_jid_special_chars),
cmocka_unit_test(test_ff_roundtrip_multiple_lines),
// Additional parser edge cases
cmocka_unit_test(test_ff_parsed_line_free_null_safe),
cmocka_unit_test(test_ff_parse_line_no_space_rejected),
cmocka_unit_test(test_ff_parse_line_unclosed_bracket),
// Stress tests
cmocka_unit_test(test_stress_rapid_write_parse_1000),
cmocka_unit_test(test_stress_rapid_write_parse_10000),
cmocka_unit_test(test_stress_muc_many_participants),
cmocka_unit_test(test_stress_muc_rapid_messages),
cmocka_unit_test(test_stress_large_message_1mb),
cmocka_unit_test(test_stress_large_message_body_with_special_chars),
cmocka_unit_test(test_stress_many_large_messages_100),
cmocka_unit_test(test_stress_index_build_10000_lines),
cmocka_unit_test(test_stress_index_extend_append),
cmocka_unit_test(test_stress_id_cache_dedup_correctness),
cmocka_unit_test(test_stress_export_merge_dedup_1000),
cmocka_unit_test(test_stress_mixed_types_and_encodings),
cmocka_unit_test(test_stress_lmc_chain_deep),
cmocka_unit_test(test_stress_lmc_many_corrections),
};
return cmocka_run_group_tests(all_tests, NULL, NULL);
}