strawberry/tests/test_config.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

138 lines
4.7 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";
}
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);
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);
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);
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;
}
int main() {
std::cout << "Running Config tests..." << std::endl << std::endl;
try {
test_default_values();
test_load_config();
test_save_config();
test_governor_keys();
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;
}
}