#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 #include #include #include namespace store_compare { using namespace strawberry; struct Pools { std::vector 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 std::vector strs(const std::vector& v) { std::vector out; for (const auto& x : v) out.push_back(str(x)); return out; } template std::string opt(const std::optional& v) { return v ? str(*v) : std::string(""); } template 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