strawberry/tests/test_config.cpp
trvon db4c49155f
yara: rule partitions — scan each file only with the rules that can match it
Why YARA-X scanned ~1.6x faster than libyara (perf + source audit, 2026-11-14):
not the language — both spend ~5 s in the atom search loop — but verification
(libyara 2.0 s vs 0.3 s). YARA-X derives filesize bounds and header constraints
per rule at compile time and disables those patterns before the search, so their
atom hits are dropped with one bit test. In the yara-forge core pack 1787 rules
require an MZ header and 2498 bound filesize, so for big Linux ELFs most of
libyara's verification is wasted.

Rule partitioning gets the same win with libyara, and drops the rules' atoms from
the automaton entirely:
- src/rule_partition.cpp (pure): lexer-aware splitter (comments, text, regex
  after = or matches, hex), facts from TOP-LEVEL `and` terms only (MZ /
  pe.is_pe, ELF, Mach-O magic; filesize < / <=), exclusion per (MZ / non-MZ) x
  size bucket (<1, 1-4, 4-16, >=16 MiB). Global rules and rules referenced by
  kept rules always stay.
- YaraScanner builds subsets in a background thread after each load, in
  priority order (big non-PE first) under rule_partition_budget_pct (default
  100 ~ one extra full set: 3 subsets, +59 MiB; all 16 class x size subsets
  would cost +428 MiB). Cached under rules_cache_dir/partitions/. File, buffer
  and fd scans pick their subset; process-memory scans use the full set.
- Fails closed to the full rules: include directives, a split count that
  differs from libyara's, or a subset compile / count error.
- Status rules.partitions (state, reason, subsets, kept_rules/scans per
  partition, full_scans); config rule_partitions (default true) and
  rule_partition_budget_pct.

Measured on the 5132-rule pack: big ASan ELFs 1.64x, 1200 system binaries
1.59x, 0 match-set differences, including 348 positive files across
MZ/ELF/raw x small/5 MiB. Tests: splitter traps, facts, exclusion soundness
property, real pack = 5132 rules; differential test (24 file shapes x memory /
ranges / fd scans, every subset built) and fail-closed; bench --partitions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 06:58:39 -07:00

292 lines
10 KiB
C++

#include "strawberry/config.hpp"
#include <iostream>
#include <cassert>
#include <fstream>
#include <utility>
#include <cstdio>
#include <iterator>
#include <string>
void test_load_config() {
const char* test_config = "/tmp/strawberry_test_config.conf";
// Create test config file
{
std::ofstream file(test_config);
file << "# Test configuration\n";
file << "db_path=/tmp/strawberry-test.db\n";
file << "yara_rules_path=/tmp/test_rules\n";
file << "daemonize=true\n";
file << "enable_bpf_monitoring=false\n";
file << "scan_existing_processes_on_start=false\n";
file << "rule_auto_fetch=false\n";
file << "store_reduce_enabled=true\n";
}
strawberry::Config config;
bool result = config.load_from_file(test_config);
std::cout << "Test load_config:" << std::endl;
assert(result == true);
assert(config.db_path == "/tmp/strawberry-test.db");
assert(config.yara_rules_path == "/tmp/test_rules");
assert(config.daemonize == true);
assert(config.enable_bpf_monitoring == false);
assert(config.scan_existing_processes_on_start == false);
assert(config.rule_auto_fetch == false);
assert(strawberry::Config{}.rule_auto_fetch == true);
assert(config.store_reduce_enabled == true);
{
// "1" enables; absent keeps the default (false).
std::ofstream file(test_config);
file << "store_reduce_enabled=1\n";
}
strawberry::Config one;
assert(one.load_from_file(test_config) && one.store_reduce_enabled == true);
{
std::ofstream file(test_config);
file << "daemonize=true\n";
}
strawberry::Config absent;
assert(absent.load_from_file(test_config) && absent.store_reduce_enabled == false);
std::cout << " PASSED" << std::endl;
// Cleanup
std::remove(test_config);
}
void test_save_config() {
const char* test_config = "/tmp/strawberry_test_save.conf";
strawberry::Config config;
config.db_path = "/tmp/strawberry-save.db";
config.yara_rules_path = "/save/rules";
bool result = config.save_to_file(test_config);
std::cout << "Test save_config:" << std::endl;
assert(result == true);
// Verify file was created
std::ifstream file(test_config);
assert(file.good());
std::string contents((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
assert(contents.find("db_path=/tmp/strawberry-save.db") != std::string::npos);
assert(contents.find("scan_existing_processes_on_start=true") != std::string::npos);
assert(contents.find("rule_auto_fetch=true") != std::string::npos);
assert(contents.find("store_reduce_enabled=false") != std::string::npos);
std::cout << " PASSED" << std::endl;
// Cleanup
std::remove(test_config);
}
void test_default_values() {
strawberry::Config config;
std::cout << "Test default_values:" << std::endl;
assert(config.db_path.empty());
assert(!config.yara_rules_path.empty());
assert(config.daemonize == true);
assert(config.enable_bpf_monitoring == true);
assert(config.scan_existing_processes_on_start == true);
assert(config.store_reduce_enabled == false);
std::cout << " PASSED" << std::endl;
}
void test_governor_keys() {
const char* path = "/tmp/strawberry_test_governor.conf";
{
std::ofstream file(path);
file << "scan_profile=efficient\nscan_timeout_seconds=7\ngovernor_tick_ms=500\n"
<< "max_scan_rate_mb_s=-1\nbackground_max_deferral_s=60\nmemory_budget_mb=32\n";
}
std::cout << "Test governor keys:" << std::endl;
strawberry::Config config;
assert(config.scan_profile == "balanced" && config.scan_timeout_seconds == 0);
assert(config.load_from_file(path));
assert(config.scan_profile == "efficient");
assert(config.scan_timeout_seconds == 7);
assert(config.governor_tick_ms == 500);
assert(config.max_scan_rate_mb_s == -1);
assert(config.background_max_deferral_s == 60);
assert(config.memory_budget_mb == 64); // clamped up from 32
// Out-of-range / unknown values are ignored; defaults stay.
{
std::ofstream file(path);
file << "scan_profile=turbo\ngovernor_tick_ms=5\nscan_timeout_seconds=9999\nmax_scan_rate_mb_s=fast\n";
}
strawberry::Config defaults;
assert(defaults.load_from_file(path));
assert(defaults.scan_profile == "balanced");
assert(defaults.governor_tick_ms == 1000);
assert(defaults.scan_timeout_seconds == 0);
assert(defaults.max_scan_rate_mb_s == 0);
// prevention_timeout_ms is clamped to [100, 30000]: the pipeline adds to
// it in int arithmetic, which overflowed for huge values.
for (const auto& [text, want] : {std::pair{"2147483647", 30000}, std::pair{"5", 100}, std::pair{"2500", 2500}}) {
{
std::ofstream file(path);
file << "prevention_timeout_ms=" << text << "\n";
}
strawberry::Config c;
assert(c.load_from_file(path));
assert(c.prevention_timeout_ms == want);
}
std::remove(path);
std::cout << " PASSED" << std::endl;
}
void test_export_egress() {
const char* path = "/tmp/strawberry_test_export_egress.conf";
const char* saved = "/tmp/strawberry_test_export_egress_saved.conf";
std::cout << "Test export egress keys:" << std::endl;
strawberry::Config d;
assert(d.export_dir.empty() && d.export_retention_hours == 0 && d.export_max_mb == 0); // default off
{
std::ofstream file(path);
file << "export_dir=/var/lib/strawberry/export\nexport_retention_hours=48\nexport_max_mb=512\n";
}
strawberry::Config c;
assert(c.load_from_file(path));
assert(c.export_dir == "/var/lib/strawberry/export");
assert(c.export_retention_hours == 48);
assert(c.export_max_mb == 512);
assert(c.save_to_file(saved));
strawberry::Config r;
assert(r.load_from_file(saved));
assert(r.export_dir == "/var/lib/strawberry/export");
assert(r.export_retention_hours == 48);
assert(r.export_max_mb == 512);
{
std::ofstream file(path);
file << "export_retention_hours=-1\nexport_max_mb=lots\n";
}
strawberry::Config bad;
assert(bad.load_from_file(path));
assert(bad.export_retention_hours == 0 && bad.export_max_mb == 0); // invalid ignored
std::remove(path);
std::remove(saved);
std::cout << " PASSED" << std::endl;
}
void test_node_id() {
const char* path = "/tmp/strawberry_test_node_id.conf";
std::cout << "Test node_id:" << std::endl;
{
std::ofstream file(path);
file << "node_id = fleet-node-7\n";
}
strawberry::Config c;
assert(c.node_id.empty());
assert(c.load_from_file(path));
assert(c.node_id == "fleet-node-7");
{
std::ofstream file(path);
file << "scan_profile=balanced\n";
}
strawberry::Config absent;
assert(absent.load_from_file(path));
assert(absent.node_id.empty());
std::remove(path);
std::cout << " PASSED" << std::endl;
}
void test_guard_loaded_segments_only() {
const char* path = "/tmp/strawberry_test_guard_loaded.conf";
const char* saved = "/tmp/strawberry_test_guard_loaded_saved.conf";
std::cout << "Test guard_loaded_segments_only:" << std::endl;
strawberry::Config d;
assert(!d.guard_loaded_segments_only); // off by default
{
std::ofstream file(path);
file << "guard_loaded_segments_only=true\n";
}
strawberry::Config c;
assert(c.load_from_file(path) && c.guard_loaded_segments_only);
assert(c.save_to_file(saved));
strawberry::Config r;
assert(r.load_from_file(saved) && r.guard_loaded_segments_only);
std::remove(path);
std::remove(saved);
std::cout << " PASSED" << std::endl;
}
void test_rule_partitions_config() {
const char* path = "/tmp/strawberry_test_partitions.conf";
const char* saved = "/tmp/strawberry_test_partitions_saved.conf";
std::cout << "Test rule_partitions config:" << std::endl;
strawberry::Config d;
assert(d.rule_partitions && d.rule_partition_budget_pct == 100);
{
std::ofstream file(path);
file << "rule_partitions=false\nrule_partition_budget_pct=40\n";
}
strawberry::Config c;
assert(c.load_from_file(path) && !c.rule_partitions && c.rule_partition_budget_pct == 40);
assert(c.save_to_file(saved));
strawberry::Config r;
assert(r.load_from_file(saved) && !r.rule_partitions && r.rule_partition_budget_pct == 40);
{
std::ofstream file(path);
file << "rule_partition_budget_pct=-5\n"; // invalid: ignored, default kept
}
strawberry::Config bad;
assert(bad.load_from_file(path) && bad.rule_partition_budget_pct == 100);
std::remove(path);
std::remove(saved);
std::cout << " PASSED" << std::endl;
}
void test_rules_cache_dir() {
const char* path = "/tmp/strawberry_test_rules_cache.conf";
const char* saved = "/tmp/strawberry_test_rules_cache_saved.conf";
std::cout << "Test rules_cache_dir:" << std::endl;
strawberry::Config d;
assert(d.rules_cache_dir == "/var/lib/strawberry/rules-cache");
{
std::ofstream file(path);
file << "rules_cache_dir=/srv/rc\n";
}
strawberry::Config c;
assert(c.load_from_file(path));
assert(c.rules_cache_dir == "/srv/rc");
assert(c.save_to_file(saved));
strawberry::Config r;
assert(r.load_from_file(saved));
assert(r.rules_cache_dir == "/srv/rc");
c.rules_cache_dir.clear(); // empty = disabled must survive a round trip
assert(c.save_to_file(saved));
strawberry::Config e;
assert(e.load_from_file(saved));
assert(e.rules_cache_dir.empty());
std::remove(path);
std::remove(saved);
std::cout << " PASSED" << std::endl;
}
int main() {
std::cout << "Running Config tests..." << std::endl << std::endl;
try {
test_default_values();
test_load_config();
test_save_config();
test_governor_keys();
test_node_id();
test_export_egress();
test_rules_cache_dir();
test_guard_loaded_segments_only();
test_rule_partitions_config();
std::cout << std::endl << "All tests passed!" << std::endl;
return 0;
} catch (const std::exception& e) {
std::cerr << "Test failed with exception: " << e.what() << std::endl;
return 1;
}
}