#include "../src/firewall/FirewallRuleManager.hpp" #include #include #include #include #include #include #include #include int main() { namespace fs = std::filesystem; char pattern[] = "/tmp/bastionguard-firewall-XXXXXX"; const char* tmp = ::mkdtemp(pattern); assert(tmp); const fs::path root(tmp); const auto log = root / "commands"; const auto pkexec = root / "pkexec"; { std::ofstream out(pkexec); out << "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$BG_TEST_LOG\"\n"; } assert(::chmod(pkexec.c_str(), 0700) == 0); const std::string original_path = std::getenv("PATH") ? std::getenv("PATH") : "/usr/bin:/bin"; assert(::setenv("PATH", (root.string() + ":" + original_path).c_str(), 1) == 0); assert(::setenv("BG_TEST_LOG", log.c_str(), 1) == 0); const std::vector addresses = { "127.0.0.1", "::1", "2001:db8::10", "127.0.0.1;touch /tmp/should-not-exist", "999.22.33.44" }; FirewallRuleManager::apply(FirewallType::FIREWALLD, addresses); FirewallRuleManager::apply(FirewallType::UFW, addresses); std::ifstream input(log); std::string commands((std::istreambuf_iterator(input)), std::istreambuf_iterator()); assert(commands.find("--option=family=inet6") != std::string::npos); assert(commands.find("--option=family=inet") != std::string::npos); assert(commands.find("--add-entry=::1") != std::string::npos); assert(commands.find("--add-entry=127.0.0.1") != std::string::npos); assert(commands.find("2001:db8::10") != std::string::npos); assert(commands.find("touch") == std::string::npos); fs::remove_all(root); }