strawberry/tests/test_store_differential.cpp
trvon dc50f06f8d
Some checks failed
Build and Test / build (debug, clang) (push) Has been cancelled
Build and Test / build (debug, gcc) (push) Has been cancelled
Build and Test / build (release, clang) (push) Has been cancelled
Build and Test / build (release, gcc) (push) Has been cancelled
Build and Test / docker (push) Has been cancelled
Drop unqlite-cpp and import legacy databases
Nothing shipped used the library after the event store landed. A small
reader/writer for its file format (src/store/legacy_kv) replaces it for
the legacy code, and the submodule, subproject and library test are
removed.

A writer open now imports a legacy database.db file into the event
store: indicators are replayed in an order consistent with the old
hash/rule indexes, the result is verified against the old file, the
old file is kept as database.db.migrated-<ts>, and an import
interrupted between its final renames is finished on the next open.
Read-only handles read a legacy file directly and switch to the store
once it is imported. Ids re-stored under another hash are reported
(their old index entries cannot be carried over).

Tests: legacy import properties I1-I6 (locked and corrupt files are
left untouched), the differential test now shares its comparisons, and
fuzz_legacy_kv covers the file parser. A copy of a real 137 KB legacy
database imported cleanly into 7 KB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:46:21 -06:00

179 lines
7.6 KiB
C++

// Differential test: the event-store Database must answer every query exactly
// as the old unqlite store did, for the same sequence of writes, transactions
// (commit and rollback) and reopens. A failure prints the seed; re-run with
// STRAWBERRY_PROPERTY_SEED=<seed>.
#include "strawberry/database.hpp"
#include "../src/store/legacy_unqlite.hpp"
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include <unistd.h>
using namespace strawberry;
namespace fs = std::filesystem;
namespace {
uint64_t seed_from_env() {
if (const char* text = std::getenv("STRAWBERRY_PROPERTY_SEED")) {
return std::strtoull(text, nullptr, 10);
}
return std::random_device{}();
}
const uint64_t kSeed = seed_from_env();
#define PROPERTY(cond) \
do { \
if (!(cond)) { \
std::cerr << "PROPERTY FAILED: " #cond " (seed " << kSeed << ", " << __FILE__ << ":" \
<< __LINE__ << ")" << std::endl; \
std::abort(); \
} \
} while (0)
} // namespace
#include "store_compare.hpp"
using namespace store_compare;
namespace {
Pools make_pools(std::mt19937_64& rng) {
Pools p;
for (int i = 0; i < 6; ++i) p.ids.push_back("ind-" + std::to_string(i));
for (int i = 0; i < 4; ++i) p.hashes.push_back(std::string(8, static_cast<char>('a' + i)));
p.hashes.push_back(""); // empty hash, as some callers store
for (int i = 0; i < 3; ++i) p.rules.push_back("Rule_" + std::to_string(i));
for (int i = 0; i < 8; ++i) p.nodes.push_back((i % 2 ? "process_" : "file_") + std::to_string(i));
p.nodes.push_back("weird:node/with|chars\n");
p.types = {"process", "file", "indicator", "network"};
p.rels = {"spawned", "executed", "matched", "content_matched"};
p.words = {"", "high", "medium", "low", "x", "multi word value", "\xff\x00binary"};
(void) rng;
return p;
}
template <typename T>
const T& pick(std::mt19937_64& rng, const std::vector<T>& v) {
return v[rng() % v.size()];
}
IndicatorEntry random_indicator(std::mt19937_64& rng, const Pools& p) {
IndicatorEntry e;
e.id = pick(rng, p.ids);
e.hash = pick(rng, p.hashes);
e.rule_name = pick(rng, p.rules);
e.severity = pick(rng, p.words);
e.description = pick(rng, p.words);
e.timestamp = static_cast<int64_t>(rng() % 5) * (rng() % 4 == 0 ? -1 : 1000000000);
for (int k = 0, n = static_cast<int>(rng() % 3); k < n; ++k) e.metadata[pick(rng, p.words)] = pick(rng, p.words);
return e;
}
GraphNode random_node(std::mt19937_64& rng, const Pools& p) {
GraphNode n;
n.id = pick(rng, p.nodes);
n.type = pick(rng, p.types);
for (int k = 0, c = static_cast<int>(rng() % 3); k < c; ++k) n.properties[pick(rng, p.words)] = pick(rng, p.words);
return n;
}
GraphEdge random_edge(std::mt19937_64& rng, const Pools& p) {
// Timestamps 9 and 10 sort differently as text and as numbers: the old
// key order is textual.
static const int64_t ts[] = {0, 9, 10, 100, -1};
return {pick(rng, p.nodes), pick(rng, p.nodes), pick(rng, p.rels), ts[rng() % 5]};
}
FindingRecord random_finding(std::mt19937_64& rng, const Pools& p) {
FindingRecord f;
f.id = "finding-" + std::to_string(rng() % 5);
f.title = pick(rng, p.words);
f.severity = pick(rng, p.words);
f.score = static_cast<int>(rng() % 4) * 10;
f.summary = pick(rng, p.words);
for (int k = 0, n = static_cast<int>(rng() % 3); k < n; ++k) f.related_node_ids.push_back(pick(rng, p.nodes));
for (int k = 0, n = static_cast<int>(rng() % 3); k < n; ++k) f.related_indicator_ids.push_back(pick(rng, p.ids));
for (int k = 0, n = static_cast<int>(rng() % 2); k < n; ++k) f.metadata[pick(rng, p.words)] = pick(rng, p.words);
f.first_seen = static_cast<int64_t>(rng() % 3);
f.last_seen = static_cast<int64_t>(rng() % 3);
return f;
}
} // namespace
int main(int argc, char** argv) {
const int iterations = argc > 1 ? std::atoi(argv[1]) : 40;
std::cout << "store differential (legacy unqlite vs event store), seed " << kSeed << std::endl;
std::mt19937_64 rng(kSeed);
const Pools pools = make_pools(rng);
const fs::path root = fs::temp_directory_path() / ("sb-diff-" + std::to_string(::getpid()));
uint64_t checks = 0, ops = 0, rollbacks = 0, reopens = 0;
for (int it = 0; it < iterations; ++it) {
fs::remove_all(root);
fs::create_directories(root);
const std::string legacy_path = (root / "legacy.db").string();
const std::string store_path = (root / "store.db").string();
LegacyUnqliteDatabase legacy;
Database current;
PROPERTY(legacy.open(legacy_path) && current.open(store_path));
const int steps = 30 + static_cast<int>(rng() % 120);
bool in_txn = false;
for (int s = 0; s < steps; ++s) {
const uint32_t op = static_cast<uint32_t>(rng() % 20);
if (op < 4) {
const auto e = random_indicator(rng, pools);
PROPERTY(legacy.store_indicator(e) == current.store_indicator(e));
} else if (op < 8) {
const auto n = random_node(rng, pools);
PROPERTY(legacy.add_graph_node(n) == current.add_graph_node(n));
} else if (op < 12) {
const auto e = random_edge(rng, pools);
PROPERTY(legacy.add_graph_edge(e) == current.add_graph_edge(e));
} else if (op < 15) {
const auto f = random_finding(rng, pools);
PROPERTY(legacy.store_finding(f) == current.store_finding(f));
} else if (op < 17 && !in_txn) {
PROPERTY(legacy.begin_transaction() && current.begin_transaction());
in_txn = true;
} else if (op == 17 && in_txn) {
PROPERTY(legacy.commit_transaction() && current.commit_transaction());
in_txn = false;
} else if (op == 18 && in_txn) {
PROPERTY(legacy.rollback_transaction() && current.rollback_transaction());
in_txn = false;
++rollbacks;
} else if (op == 19 && !in_txn) {
legacy.close();
current.close();
PROPERTY(legacy.open(legacy_path) && current.open(store_path));
++reopens;
}
++ops;
if (rng() % 8 == 0) compare(legacy, current, pools, checks);
}
if (in_txn) PROPERTY(legacy.commit_transaction() && current.commit_transaction());
compare(legacy, current, pools, checks);
// Read-only handles see the same state.
legacy.close();
current.close();
LegacyUnqliteDatabase legacy_ro;
Database current_ro;
PROPERTY(legacy_ro.open_read_only(legacy_path) && current_ro.open_read_only(store_path));
compare(legacy_ro, current_ro, pools, checks);
}
fs::remove_all(root);
std::cout << "store differential: identical answers (" << iterations << " runs, " << ops << " ops, " << checks
<< " full comparisons, " << rollbacks << " rollbacks, " << reopens << " reopens)" << std::endl;
return 0;
}