strawberry/tests/test_exec_guard.cpp
trvon 1d1ab9f57e
Add exec prevention, behavior telemetry and persistence watch
fanotify exec-permission guard (audit/enforce, fail-open deadline),
eBPF behavior engine (IOC connections, ptrace, module loads,
download-then-execute) and fanotify persistence watch, with unit tests
and privileged Docker live tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:17:16 -06:00

74 lines
2.9 KiB
C++

#include "strawberry/exec_guard.hpp"
#include <cassert>
#include <iostream>
using namespace strawberry;
namespace {
ExecAssessment hit(const std::string& severity, bool ioc = false) {
ExecAssessment a;
a.scanned = true;
a.top_rule = "Rule";
a.top_severity = severity;
a.ioc_match = ioc;
return a;
}
void test_decisions() {
std::cout << "Test verdict logic:" << std::endl;
// Mode off / unscanned / clean -> allow.
assert(decide_exec("off", "critical", hit("critical"), false) == ExecVerdict::allow);
ExecAssessment unscanned = hit("critical");
unscanned.scanned = false;
assert(decide_exec("enforce", "critical", unscanned, false) == ExecVerdict::allow);
ExecAssessment clean;
clean.scanned = true;
assert(decide_exec("enforce", "medium", clean, false) == ExecVerdict::allow);
// Threshold.
assert(decide_exec("enforce", "critical", hit("high"), false) == ExecVerdict::allow);
assert(decide_exec("enforce", "high", hit("high"), false) == ExecVerdict::block);
assert(decide_exec("audit", "high", hit("critical"), false) == ExecVerdict::would_block);
// IOC hash matches always count, whatever the YARA threshold.
assert(decide_exec("enforce", "critical", hit("", true), false) == ExecVerdict::block);
// System paths are never blocked automatically.
assert(decide_exec("enforce", "high", hit("critical", true), true) == ExecVerdict::would_block);
// Allowlist wins; operator quarantine beats allowlist, mode and path.
ExecAssessment allowed = hit("critical", true);
allowed.allowlisted = true;
assert(decide_exec("enforce", "high", allowed, false) == ExecVerdict::allow);
ExecAssessment quarantined = clean;
quarantined.quarantined = true;
assert(decide_exec("audit", "critical", quarantined, true) == ExecVerdict::block);
assert(decide_exec("off", "critical", quarantined, false) == ExecVerdict::block);
std::cout << " PASSED" << std::endl;
}
void test_mountinfo() {
std::cout << "Test mount point selection:" << std::endl;
const std::string mountinfo =
"22 1 259:2 / / rw,relatime shared:1 - btrfs /dev/nvme0n1p2 rw\n"
"23 22 259:2 /@home /home rw,relatime shared:2 - btrfs /dev/nvme0n1p2 rw\n" // same superblock
"24 22 0:5 / /proc rw - proc proc rw\n"
"25 22 0:30 / /tmp rw - tmpfs tmpfs rw\n"
"26 22 0:31 / /dev/shm rw - tmpfs tmpfs rw\n"
"27 22 0:40 / /run/user/1000/doc rw - fuse.portal portal rw\n"
"28 22 0:41 / /media/My\\040Disk rw - exfat /dev/sdb1 rw\n";
const auto points = ExecGuard::local_mount_points(mountinfo);
assert((points == std::vector<std::string>{"/", "/tmp", "/dev/shm", "/media/My Disk"}));
std::cout << " PASSED" << std::endl;
}
} // namespace
int main() {
std::cout << "Running exec guard tests..." << std::endl << std::endl;
test_decisions();
test_mountinfo();
std::cout << std::endl << "All tests passed!" << std::endl;
return 0;
}