#include "strawberry/database.hpp" #include "strawberry/process_query.hpp" #include #include #include #include namespace fs = std::filesystem; namespace { void seed_process_query_fixture(strawberry::Database& database) { assert(database.add_graph_node({ .id = "process_42", .type = "process", .properties = {{"pid", "42"}, {"path", "/usr/bin/parent"}}, })); assert(database.add_graph_node({ .id = "process_654", .type = "process", .properties = { {"pid", "654"}, {"parent_pid", "42"}, {"path", "/opt/demo/synthetic_daemon_exec"}, {"hash", "demo_hash_abc123"}, {"cmdline", "synthetic_daemon_exec --demo"}, {"event_timestamp_ns", "101"}, }, })); assert(database.add_graph_node({ .id = "process_655", .type = "process", .properties = { {"pid", "655"}, {"path", "/opt/demo/child_worker"}, {"cmdline", "child_worker --demo"}, }, })); assert(database.add_graph_node({ .id = "file_demo_hash_abc123", .type = "file", .properties = {{"path", "/opt/demo/synthetic_daemon_exec"}, {"hash", "demo_hash_abc123"}}, })); assert(database.add_graph_node({ .id = "indicator_process-hit:654:demo:0", .type = "indicator", .properties = {{"rule_name", "SyntheticDemoRule"}, {"hash", "demo_hash_abc123"}}, })); assert(database.add_graph_edge({"process_42", "process_654", "spawned", 100})); assert(database.add_graph_edge({"process_654", "process_655", "spawned", 105})); assert(database.add_graph_edge({"process_654", "file_demo_hash_abc123", "executed", 110})); assert(database.add_graph_edge({"process_654", "indicator_process-hit:654:demo:0", "matched", 120})); assert(database.store_indicator({ .id = "process-hit:654:demo:0", .hash = "demo_hash_abc123", .rule_name = "SyntheticDemoRule", .severity = "high", .description = "Synthetic hit", .metadata = { {"source", "demo"}, {"scan_kind", "demo_trace"}, {"pid", "654"}, {"parent_pid", "42"}, {"process_path", "/opt/demo/synthetic_daemon_exec"}, {"cmdline", "synthetic_daemon_exec --demo"}, }, .timestamp = 123, })); assert(database.store_finding({ .id = "finding_process_yara_demo", .title = "YARA match on executed process image", .severity = "high", .score = 80, .summary = "Process 654 executed synthetic daemon and matched SyntheticDemoRule.", .related_node_ids = {"process_654", "file_demo_hash_abc123", "indicator_process-hit:654:demo:0"}, .related_indicator_ids = {"process-hit:654:demo:0"}, .metadata = {{"rule", "process_executable_yara_match"}, {"pid", "654"}}, .first_seen = 123, .last_seen = 123, })); } void test_search_by_pid_path_hash_rule_and_finding() { const auto db_path = (fs::temp_directory_path() / "strawberry_process_query.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)); seed_process_query_fixture(database); std::cout << "Test search_by_pid_path_hash_rule_and_finding:" << std::endl; auto by_pid = strawberry::ProcessQuery::search(database, "654", 1); assert(by_pid.size() == 1); assert(by_pid[0].process.id == "process_654"); assert(by_pid[0].parent_process.has_value()); assert(by_pid[0].parent_process->id == "process_42"); assert(by_pid[0].child_processes.size() == 1); assert(by_pid[0].child_processes[0].id == "process_655"); assert(by_pid[0].first_seen.has_value()); assert(*by_pid[0].first_seen == 100); assert(by_pid[0].last_seen.has_value()); assert(*by_pid[0].last_seen == 123); assert(!by_pid[0].findings.empty()); assert(by_pid[0].findings[0].id == "finding_process_yara_demo"); auto by_path = strawberry::ProcessQuery::search(database, "synthetic_daemon", 1); assert(by_path.size() == 1); assert(by_path[0].process.properties.at("path") == "/opt/demo/synthetic_daemon_exec"); auto by_hash = strawberry::ProcessQuery::search(database, "demo_hash_abc123", 1); assert(by_hash.size() == 1); assert(by_hash[0].process.id == "process_654"); auto by_rule = strawberry::ProcessQuery::search(database, "SyntheticDemoRule", 1); assert(by_rule.size() == 1); assert(by_rule[0].process.id == "process_654"); auto by_finding = strawberry::ProcessQuery::search(database, "process_executable_yara_match", 1); assert(by_finding.size() == 1); assert(by_finding[0].process.id == "process_654"); std::set related_ids; for (const auto& node : by_pid[0].related_nodes) { related_ids.insert(node.id); } assert(related_ids.contains("file_demo_hash_abc123")); assert(related_ids.contains("indicator_process-hit:654:demo:0")); std::cout << " PASSED" << std::endl; database.close(); fs::remove_all(db_path, ec); // the store is a directory } } // namespace int main() { std::cout << "Running ProcessQuery tests..." << std::endl << std::endl; test_search_by_pid_path_hash_rule_and_finding(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; }