#include "strawberry/database.hpp" #include "strawberry/incident_report.hpp" #include #include #include #include namespace fs = std::filesystem; namespace { void test_summarize_process_trace() { const auto db_path = (fs::temp_directory_path() / "strawberry_incident_report.db").string(); std::error_code ec; fs::remove_all(db_path, ec); // the store is a directory strawberry::Database database; assert(database.open(db_path)); strawberry::GraphNode parent_node; parent_node.id = "process_42"; parent_node.type = "process"; parent_node.properties["pid"] = "42"; database.add_graph_node(parent_node); strawberry::GraphNode process_node; process_node.id = "process_654"; process_node.type = "process"; process_node.properties["pid"] = "654"; process_node.properties["parent_pid"] = "42"; process_node.properties["path"] = "/tmp/synthetic_daemon_exec"; process_node.properties["cmdline"] = "synthetic_daemon_exec --daemon-test"; process_node.properties["hash"] = "abc123"; process_node.properties["event_timestamp_ns"] = "123456789"; database.add_graph_node(process_node); strawberry::GraphNode file_node; file_node.id = "file_abc123"; file_node.type = "file"; file_node.properties["path"] = "/tmp/synthetic_daemon_exec"; file_node.properties["hash"] = "abc123"; file_node.properties["kind"] = "executable_image"; database.add_graph_node(file_node); strawberry::GraphNode indicator_node; indicator_node.id = "indicator_process-hit:654:1:0"; indicator_node.type = "indicator"; indicator_node.properties["rule_name"] = "SyntheticDaemonRule"; indicator_node.properties["severity"] = "high"; indicator_node.properties["hash"] = "abc123"; indicator_node.properties["cmdline"] = "synthetic_daemon_exec --daemon-test"; database.add_graph_node(indicator_node); database.add_graph_edge({"process_42", "process_654", "spawned", 100}); database.add_graph_edge({"process_654", "file_abc123", "executed", 110}); database.add_graph_edge({"process_654", "indicator_process-hit:654:1:0", "matched", 120}); database.add_graph_edge({"file_abc123", "indicator_process-hit:654:1:0", "content_matched", 121}); std::cout << "Test summarize_process_trace:" << std::endl; auto report = strawberry::IncidentReporter::summarize_process(database, 654, 2); assert(report.has_value()); assert(report->find("Incident trace for process_654") != std::string::npos); assert(report->find("- pid: 654") != std::string::npos); assert(report->find("- parent_pid: 42") != std::string::npos); assert(report->find("Trace timing") != std::string::npos); assert(report->find("- first_seen: 100 ns") != std::string::npos); assert(report->find("- last_seen: 123456789 ns (~123.457 ms)") != std::string::npos); assert(report->find("- span: 123456689 ns (~123.457 ms)") != std::string::npos); assert(report->find("synthetic_daemon_exec --daemon-test") != std::string::npos); assert(report->find("incoming spawned <- process_42") != std::string::npos); assert(report->find("outgoing executed -> file_abc123") != std::string::npos); assert(report->find("outgoing matched -> indicator_process-hit:654:1:0") != std::string::npos); assert(report->find("SyntheticDaemonRule") != std::string::npos); assert(report->find("file_abc123 -[content_matched]-> indicator_process-hit:654:1:0 @ 121 ns") != std::string::npos); std::cout << " PASSED" << std::endl; database.close(); fs::remove_all(db_path, ec); // the store is a directory } void test_missing_node_returns_empty() { const auto db_path = (fs::temp_directory_path() / "strawberry_incident_report_missing.db").string(); std::error_code ec; fs::remove_all(db_path, ec); // the store is a directory strawberry::Database database; assert(database.open(db_path)); std::cout << "Test missing_node_returns_empty:" << std::endl; auto report = strawberry::IncidentReporter::summarize_node(database, "process_999", 2); assert(!report.has_value()); std::cout << " PASSED" << std::endl; database.close(); fs::remove_all(db_path, ec); // the store is a directory } } // namespace int main() { std::cout << "Running IncidentReporter tests..." << std::endl << std::endl; test_summarize_process_trace(); test_missing_node_returns_empty(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; }