strawberry/tests/test_database.cpp
trvon ad1676a594
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
Integrate features into the daemon, build and docs
Wire the policy, feeds, containment, exec guard, behavior, governor,
crash safety and event store into the daemon, detection pipeline, CLI
and meson build (zstd now required); Dockerfile, conan, systemd unit
(LimitCORE=0, resource limits) and installer updates; existing tests
adapted (store paths are directories now). Detections are counted and
notified even when evidence cannot be persisted (persist_failures in
status). prevention_timeout_ms is clamped and behavior dedupe handles
out-of-order events. Docs: API contract, developer guide, plan, UX,
agent rules.

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

409 lines
12 KiB
C++

#include "strawberry/database.hpp"
#include <cassert>
#include <cstdio>
#include <filesystem>
#include <iostream>
#include <set>
namespace {
std::string make_test_db_path(const std::string& name) {
return "/tmp/" + name;
}
void cleanup_db_path(const std::string& path) {
std::error_code ec;
std::filesystem::remove_all(path, ec); // the store is a directory
}
void test_open_close() {
const auto path = make_test_db_path("strawberry_test_database.db");
cleanup_db_path(path);
strawberry::Database database;
std::cout << "Test open_close:" << std::endl;
assert(database.open(path));
assert(database.is_open());
database.close();
assert(!database.is_open());
std::cout << " PASSED" << std::endl;
cleanup_db_path(path);
}
void test_indicator_round_trip() {
const auto path = make_test_db_path("strawberry_test_indicator.db");
cleanup_db_path(path);
strawberry::Database database;
assert(database.open(path));
strawberry::IndicatorEntry entry;
entry.id = "indicator-1";
entry.hash = "abc123";
entry.rule_name = "SuspiciousRule";
entry.severity = "high";
entry.description = "Unit test indicator";
entry.metadata["source"] = "unit-test";
entry.metadata["family"] = "test";
entry.timestamp = 1700000000;
std::cout << "Test indicator_round_trip:" << std::endl;
assert(database.store_indicator(entry));
auto fetched = database.find_indicator_by_hash("abc123");
assert(fetched.has_value());
assert(fetched->id == entry.id);
assert(fetched->hash == entry.hash);
assert(fetched->rule_name == entry.rule_name);
assert(fetched->severity == entry.severity);
assert(fetched->description == entry.description);
assert(fetched->timestamp == entry.timestamp);
assert(fetched->metadata == entry.metadata);
std::cout << " PASSED" << std::endl;
database.close();
cleanup_db_path(path);
}
void test_find_indicators_by_rule() {
const auto path = make_test_db_path("strawberry_test_indicator_rule.db");
cleanup_db_path(path);
strawberry::Database database;
assert(database.open(path));
strawberry::IndicatorEntry first{
.id = "1",
.hash = "hash-1",
.rule_name = "RuleA",
.severity = "medium",
.description = "First",
.metadata = {},
.timestamp = 1,
};
strawberry::IndicatorEntry second{
.id = "2",
.hash = "hash-2",
.rule_name = "RuleA",
.severity = "high",
.description = "Second",
.metadata = {},
.timestamp = 2,
};
strawberry::IndicatorEntry third{
.id = "3",
.hash = "hash-3",
.rule_name = "RuleB",
.severity = "low",
.description = "Third",
.metadata = {},
.timestamp = 3,
};
assert(database.store_indicator(first));
assert(database.store_indicator(second));
assert(database.store_indicator(third));
std::cout << "Test find_indicators_by_rule:" << std::endl;
auto matches = database.find_indicators_by_rule("RuleA");
assert(matches.size() == 2);
std::set<std::string> hashes;
for (const auto& entry : matches) {
hashes.insert(entry.hash);
}
assert(hashes.contains("hash-1"));
assert(hashes.contains("hash-2"));
std::cout << " PASSED" << std::endl;
database.close();
cleanup_db_path(path);
}
void test_find_indicators_by_hash_with_multiple_hits() {
const auto path = make_test_db_path("strawberry_test_indicator_hash_multi.db");
cleanup_db_path(path);
strawberry::Database database;
assert(database.open(path));
strawberry::IndicatorEntry first{
.id = "multi-1",
.hash = "shared-hash",
.rule_name = "RuleA",
.severity = "medium",
.description = "First hit",
.metadata = {{"pid", "10"}, {"tags", "alpha"}},
.timestamp = 100,
};
strawberry::IndicatorEntry second{
.id = "multi-2",
.hash = "shared-hash",
.rule_name = "RuleB",
.severity = "high",
.description = "Second hit",
.metadata = {{"pid", "11"}, {"tags", "beta,gamma"}},
.timestamp = 200,
};
assert(database.store_indicator(first));
assert(database.store_indicator(second));
std::cout << "Test find_indicators_by_hash_with_multiple_hits:" << std::endl;
auto by_hash = database.find_indicators_by_hash("shared-hash");
assert(by_hash.size() == 2);
std::set<std::string> ids;
for (const auto& entry : by_hash) {
ids.insert(entry.id);
}
assert(ids.contains("multi-1"));
assert(ids.contains("multi-2"));
auto latest = database.find_indicator_by_hash("shared-hash");
assert(latest.has_value());
assert(latest->id == "multi-2");
assert(latest->rule_name == "RuleB");
assert(latest->metadata.at("tags") == "beta,gamma");
std::cout << " PASSED" << std::endl;
database.close();
cleanup_db_path(path);
}
void test_graph_round_trip() {
const auto path = make_test_db_path("strawberry_test_graph.db");
cleanup_db_path(path);
strawberry::Database database;
assert(database.open(path));
strawberry::GraphNode process_a{
.id = "process_a",
.type = "process",
.properties = {{"path", "/bin/a"}},
};
strawberry::GraphNode process_b{
.id = "process_b",
.type = "process",
.properties = {{"path", "/bin/b"}},
};
strawberry::GraphNode file_c{
.id = "file_c",
.type = "file",
.properties = {{"path", "/tmp/c"}},
};
strawberry::GraphEdge edge_ab{
.from_id = "process_a",
.to_id = "process_b",
.relationship_type = "spawned",
.timestamp = 10,
};
strawberry::GraphEdge edge_bc{
.from_id = "process_b",
.to_id = "file_c",
.relationship_type = "accessed",
.timestamp = 11,
};
assert(database.add_graph_node(process_a));
assert(database.add_graph_node(process_b));
assert(database.add_graph_node(file_c));
assert(database.add_graph_edge(edge_ab));
assert(database.add_graph_edge(edge_bc));
std::cout << "Test graph_round_trip:" << std::endl;
auto related = database.find_related_nodes("process_a", 3);
assert(related.size() == 2);
std::set<std::string> ids;
for (const auto& node : related) {
ids.insert(node.id);
}
assert(ids.contains("process_b"));
assert(ids.contains("file_c"));
auto edges = database.get_node_edges("process_b");
assert(edges.size() == 2);
std::cout << " PASSED" << std::endl;
database.close();
cleanup_db_path(path);
}
void test_persistence_across_reopen() {
const auto path = make_test_db_path("strawberry_test_reopen.db");
cleanup_db_path(path);
{
strawberry::Database database;
assert(database.open(path));
strawberry::IndicatorEntry entry{
.id = "persisted-indicator",
.hash = "persist-hash",
.rule_name = "PersistentRule",
.severity = "critical",
.description = "Should survive reopen",
.metadata = {{"source", "reopen-test"}},
.timestamp = 99,
};
strawberry::GraphNode root{
.id = "persist-root",
.type = "process",
.properties = {{"path", "/bin/root"}},
};
strawberry::GraphNode child{
.id = "persist-child",
.type = "file",
.properties = {{"path", "/tmp/file"}},
};
strawberry::GraphEdge edge{
.from_id = "persist-root",
.to_id = "persist-child",
.relationship_type = "accessed",
.timestamp = 101,
};
assert(database.store_indicator(entry));
assert(database.add_graph_node(root));
assert(database.add_graph_node(child));
assert(database.add_graph_edge(edge));
database.close();
}
{
strawberry::Database reopened;
assert(reopened.open(path));
std::cout << "Test persistence_across_reopen:" << std::endl;
auto indicator = reopened.find_indicator_by_hash("persist-hash");
assert(indicator.has_value());
assert(indicator->rule_name == "PersistentRule");
assert(indicator->metadata.at("source") == "reopen-test");
auto related = reopened.find_related_nodes("persist-root", 2);
assert(related.size() == 1);
assert(related[0].id == "persist-child");
auto edges = reopened.get_node_edges("persist-root");
assert(edges.size() == 1);
assert(edges[0].relationship_type == "accessed");
std::cout << " PASSED" << std::endl;
reopened.close();
}
cleanup_db_path(path);
}
void test_yara_hit_metadata_graph_link_persistence() {
const auto path = make_test_db_path("strawberry_test_yara_hit_graph.db");
cleanup_db_path(path);
{
strawberry::Database database;
assert(database.open(path));
strawberry::GraphNode process{
.id = "process_4242",
.type = "process",
.properties = {{"pid", "4242"}, {"path", "/tmp/fake-sample"}},
};
strawberry::IndicatorEntry hit{
.id = "process-hit:4242:1000:0",
.hash = "sample-hash",
.rule_name = "DetectPayload",
.severity = "high",
.description = "Process matched YARA rule",
.metadata = {
{"source", "yara"},
{"scan_kind", "process_executable"},
{"pid", "4242"},
{"process_path", "/tmp/fake-sample"},
{"namespace", "default"},
{"tags", "unit_test,sample"},
{"meta_info", "description=Detect sample payload; severity=5"},
},
.timestamp = 1000,
};
strawberry::GraphNode indicator{
.id = "indicator_process-hit:4242:1000:0",
.type = "indicator",
.properties = {
{"rule_name", "DetectPayload"},
{"severity", "high"},
{"hash", "sample-hash"},
{"pid", "4242"},
{"process_path", "/tmp/fake-sample"},
{"namespace", "default"},
{"tags", "unit_test,sample"},
},
};
strawberry::GraphEdge edge{
.from_id = process.id,
.to_id = indicator.id,
.relationship_type = "matched",
.timestamp = 1000,
};
assert(database.add_graph_node(process));
assert(database.store_indicator(hit));
assert(database.add_graph_node(indicator));
assert(database.add_graph_edge(edge));
database.close();
}
{
strawberry::Database reopened;
assert(reopened.open(path));
std::cout << "Test yara_hit_metadata_graph_link_persistence:" << std::endl;
auto hits = reopened.find_indicators_by_hash("sample-hash");
assert(hits.size() == 1);
assert(hits[0].rule_name == "DetectPayload");
assert(hits[0].metadata.at("source") == "yara");
assert(hits[0].metadata.at("scan_kind") == "process_executable");
assert(hits[0].metadata.at("tags") == "unit_test,sample");
assert(hits[0].metadata.at("meta_info").find("severity=5") != std::string::npos);
auto related = reopened.find_related_nodes("process_4242", 1);
assert(related.size() == 1);
assert(related[0].id == "indicator_process-hit:4242:1000:0");
assert(related[0].type == "indicator");
auto edges = reopened.get_node_edges("process_4242");
assert(edges.size() == 1);
assert(edges[0].relationship_type == "matched");
std::cout << " PASSED" << std::endl;
reopened.close();
}
cleanup_db_path(path);
}
} // namespace
int main() {
std::cout << "Running Database tests..." << std::endl << std::endl;
test_open_close();
test_indicator_round_trip();
test_find_indicators_by_rule();
test_find_indicators_by_hash_with_multiple_hits();
test_graph_round_trip();
test_persistence_across_reopen();
test_yara_hit_metadata_graph_link_persistence();
std::cout << std::endl << "All tests passed!" << std::endl;
return 0;
}