#include "strawberry/opensnitch.hpp" #include #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using json = nlohmann::json; using namespace strawberry; namespace { fs::path make_root() { const auto root = fs::temp_directory_path() / ("strawberry_opensnitch_test_" + std::to_string(::getpid())); std::error_code ec; fs::remove_all(root, ec); fs::create_directories(root / "rules"); return root; } json read_json(const fs::path& path) { std::ifstream input(path); return json::parse(input); } void test_rule_shapes() { std::cout << "Test rule JSON shapes:" << std::endl; const std::string md5(32, 'a'); auto pinned = json::parse(opensnitch_contain_rule_json("strawberry-contain-abc", "/tmp/x", md5, true, "d")); assert(pinned["name"] == "strawberry-contain-abc"); assert(pinned["action"] == "deny" && pinned["duration"] == "always"); assert(pinned["precedence"] == true && pinned["enabled"] == true); assert(pinned["operator"]["type"] == "list" && pinned["operator"]["operand"] == "list"); const auto& list = pinned["operator"]["list"]; assert(list.size() == 2); assert(list[0]["type"] == "simple" && list[0]["operand"] == "process.path" && list[0]["data"] == "/tmp/x"); assert(list[1]["operand"] == "process.hash.md5" && list[1]["data"] == md5); assert(pinned["created"].get().ends_with("Z")); auto path_only = json::parse(opensnitch_contain_rule_json("strawberry-contain-abc", "/tmp/x", md5, false, "d")); assert(path_only["operator"]["type"] == "simple" && path_only["operator"]["operand"] == "process.path"); auto lists = json::parse(opensnitch_list_rule_json("strawberry-ioc-domains", "lists.domains", "/var/l/domains", "d")); assert(lists["operator"]["type"] == "lists" && lists["operator"]["operand"] == "lists.domains"); assert(lists["operator"]["data"] == "/var/l/domains"); std::cout << " PASSED" << std::endl; } void test_write_remove_and_names() { std::cout << "Test rule write/remove and name safety:" << std::endl; const auto root = make_root(); const auto rules = (root / "rules").string(); std::string error; assert(write_opensnitch_rule(rules, "strawberry-contain-0123456789ab", "{}", &error)); struct stat st{}; assert(::stat((root / "rules" / "strawberry-contain-0123456789ab.json").c_str(), &st) == 0); assert((st.st_mode & 0777) == 0600); assert(!write_opensnitch_rule(rules, "../evil", "{}", &error)); assert(!write_opensnitch_rule(rules, "user-rule", "{}", &error)); // never touch non-Strawberry rules assert(!write_opensnitch_rule((root / "missing").string(), "strawberry-x", "{}", &error)); std::ofstream(root / "rules" / "allow-always-firefox.json") << "{}"; const auto names = list_strawberry_rules(rules); assert(names.size() == 1 && names[0] == "strawberry-contain-0123456789ab"); assert(remove_opensnitch_rule(rules, "strawberry-contain-0123456789ab")); assert(!remove_opensnitch_rule(rules, "allow-always-firefox")); assert(fs::exists(root / "rules" / "allow-always-firefox.json")); assert(containment_rule_name(std::string(64, 'f')) == "strawberry-contain-ffffffffffff"); fs::remove_all(root); std::cout << " PASSED" << std::endl; } void test_blocklist_sync() { std::cout << "Test blocklist sync:" << std::endl; const auto root = make_root(); std::ofstream(root / "d1.txt") << "evil.example\nbad.test\n"; std::ofstream(root / "d2.txt") << "bad.test\nworse.test\n"; std::ofstream(root / "n.txt") << "1.10.16.0/20\n"; auto result = sync_opensnitch_blocklists( {{"domains", {(root / "d1.txt").string(), (root / "d2.txt").string()}}, {"nets", {(root / "n.txt").string()}}}, (root / "lists").string(), (root / "rules").string()); assert(result.error.empty()); assert(result.entries.at("domains") == 3 && result.entries.at("nets") == 1); std::ifstream domains(root / "lists" / "domains" / "strawberry.txt"); std::string line; std::getline(domains, line); assert(line == "0.0.0.0 bad.test"); // hosts format, sorted const auto rule = read_json(root / "rules" / "strawberry-ioc-domains.json"); assert(rule["operator"]["data"] == (root / "lists" / "domains").string()); assert(fs::exists(root / "rules" / "strawberry-ioc-nets.json")); assert(!fs::exists(root / "rules" / "strawberry-ioc-ips.json")); // Same input again: nothing is rewritten, so OpenSnitch does not reload. const auto list_file = root / "lists" / "domains" / "strawberry.txt"; const auto rule_file = root / "rules" / "strawberry-ioc-domains.json"; const auto old_time = fs::last_write_time(list_file) - std::chrono::hours(1); fs::last_write_time(list_file, old_time); fs::last_write_time(rule_file, old_time); result = sync_opensnitch_blocklists( {{"domains", {(root / "d1.txt").string(), (root / "d2.txt").string()}}, {"nets", {(root / "n.txt").string()}}}, (root / "lists").string(), (root / "rules").string()); assert(result.error.empty() && result.entries.at("domains") == 3); assert(fs::last_write_time(list_file) == old_time); assert(fs::last_write_time(rule_file) == old_time); // Feed disabled: rule and list removed. result = sync_opensnitch_blocklists({{"nets", {(root / "n.txt").string()}}}, (root / "lists").string(), (root / "rules").string()); assert(!fs::exists(root / "rules" / "strawberry-ioc-domains.json")); assert(!fs::exists(root / "lists" / "domains")); assert(fs::exists(root / "rules" / "strawberry-ioc-nets.json")); fs::remove_all(root); std::cout << " PASSED" << std::endl; } void test_detect_and_enable_checksums() { std::cout << "Test detection and enabling checksums:" << std::endl; const auto root = make_root(); const auto config = root / "default-config.json"; std::ofstream(config) << R"({"Server":{"Address":"unix:///tmp/osui.sock"},"Rules":{"Path":"/etc/opensnitchd/rules/","EnableChecksums":false},"LogLevel":2})"; ::chmod(config.c_str(), 0644); auto info = detect_opensnitch((root / "rules").string(), config.string()); assert(info.installed && !info.checksums_enabled); assert(!detect_opensnitch((root / "nope").string(), config.string()).installed); std::string error; assert(enable_opensnitch_checksums(config.string(), &error)); info = detect_opensnitch((root / "rules").string(), config.string()); assert(info.checksums_enabled); const auto updated = read_json(config); assert(updated["Server"]["Address"] == "unix:///tmp/osui.sock" && updated["LogLevel"] == 2); // rest preserved assert(fs::exists(config.string() + ".bak")); struct stat st{}; assert(::stat(config.c_str(), &st) == 0 && (st.st_mode & 0777) == 0644); assert(enable_opensnitch_checksums(config.string(), &error)); // idempotent fs::remove_all(root); std::cout << " PASSED" << std::endl; } void test_protected_paths() { std::cout << "Test protected system paths:" << std::endl; for (const char* path : {"/usr/bin/curl", "/bin/sh", "/lib64/ld-linux-x86-64.so.2", "/etc/cron.daily/x", "/boot/vmlinuz"}) { assert(is_protected_system_path(path)); } for (const char* path : {"/tmp/payload", "/home/u/Downloads/x", "/opt/app/bin/app", "/var/tmp/x", "/usrlocal/x"}) { assert(!is_protected_system_path(path)); } #if defined(__APPLE__) for (const char* path : {"/System/Library/CoreServices/x", "/Library/Apple/usr/bin/x", "/private/etc/zshrc"}) { assert(is_protected_system_path(path)); } for (const char* path : {"/Applications/App.app/Contents/MacOS/App", "/opt/homebrew/bin/x", "/private/tmp/x"}) { assert(!is_protected_system_path(path)); } #endif std::cout << " PASSED" << std::endl; } } // namespace int main() { std::cout << "Running OpenSnitch integration tests..." << std::endl << std::endl; test_rule_shapes(); test_write_remove_and_names(); test_blocklist_sync(); test_detect_and_enable_checksums(); test_protected_paths(); std::cout << std::endl << "All tests passed!" << std::endl; return 0; }