strawberry/tests/store_compare.hpp
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

90 lines
3.3 KiB
C++

#ifndef STRAWBERRY_TESTS_STORE_COMPARE_HPP
#define STRAWBERRY_TESTS_STORE_COMPARE_HPP
// Shared by the differential and import tests: answers every Database query
// on two stores and requires identical results. PROPERTY must be defined by
// the including test.
#include "strawberry/database.hpp"
#include <optional>
#include <sstream>
#include <string>
#include <vector>
namespace store_compare {
using namespace strawberry;
struct Pools {
std::vector<std::string> ids, hashes, rules, nodes, types, rels, words;
};
std::string str(const IndicatorEntry& e) {
std::ostringstream o;
o << e.id << '|' << e.hash << '|' << e.rule_name << '|' << e.severity << '|' << e.description << '|' << e.timestamp;
for (const auto& [k, v] : e.metadata) o << '|' << k << '=' << v;
return o.str();
}
std::string str(const GraphNode& n) {
std::ostringstream o;
o << n.id << '|' << n.type;
for (const auto& [k, v] : n.properties) o << '|' << k << '=' << v;
return o.str();
}
std::string str(const GraphEdge& e) {
return e.from_id + "->" + e.to_id + "|" + e.relationship_type + "|" + std::to_string(e.timestamp);
}
std::string str(const FindingRecord& f) {
std::ostringstream o;
o << f.id << '|' << f.title << '|' << f.severity << '|' << f.score << '|' << f.summary << '|' << f.first_seen << '|'
<< f.last_seen;
for (const auto& x : f.related_node_ids) o << "|n:" << x;
for (const auto& x : f.related_indicator_ids) o << "|i:" << x;
for (const auto& [k, v] : f.metadata) o << '|' << k << '=' << v;
return o.str();
}
template <typename T>
std::vector<std::string> strs(const std::vector<T>& v) {
std::vector<std::string> out;
for (const auto& x : v) out.push_back(str(x));
return out;
}
template <typename T>
std::string opt(const std::optional<T>& v) {
return v ? str(*v) : std::string("<none>");
}
template <typename A, typename B>
void compare(A& legacy, B& current, const Pools& p, uint64_t& checks, int findings = 5) {
for (const auto& h : p.hashes) {
PROPERTY(opt(legacy.find_indicator_by_hash(h)) == opt(current.find_indicator_by_hash(h)));
PROPERTY(strs(legacy.find_indicators_by_hash(h)) == strs(current.find_indicators_by_hash(h)));
}
for (const auto& r : p.rules) {
PROPERTY(strs(legacy.find_indicators_by_rule(r)) == strs(current.find_indicators_by_rule(r)));
}
PROPERTY(strs(legacy.list_indicators()) == strs(current.list_indicators()));
PROPERTY(strs(legacy.list_graph_nodes()) == strs(current.list_graph_nodes()));
for (const auto& t : p.types) PROPERTY(strs(legacy.list_graph_nodes(t)) == strs(current.list_graph_nodes(t)));
for (const auto& n : p.nodes) {
PROPERTY(opt(legacy.find_graph_node(n)) == opt(current.find_graph_node(n)));
PROPERTY(strs(legacy.get_node_edges(n)) == strs(current.get_node_edges(n)));
for (int d = 0; d <= 3; ++d) PROPERTY(strs(legacy.find_related_nodes(n, d)) == strs(current.find_related_nodes(n, d)));
}
PROPERTY(strs(legacy.list_findings()) == strs(current.list_findings()));
for (int i = 0; i < findings; ++i) {
const std::string id = "finding-" + std::to_string(i);
PROPERTY(opt(legacy.find_finding(id)) == opt(current.find_finding(id)));
}
++checks;
}
} // namespace store_compare
#endif // STRAWBERRY_TESTS_STORE_COMPARE_HPP