244 lines
8.4 KiB
C++
244 lines
8.4 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_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();
|
|
|
|
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;
|
|
}
|
|
}
|