38 lines
1.7 KiB
C++
38 lines
1.7 KiB
C++
#include "../src/firewall/FirewallRuleManager.hpp"
|
|
#include <cassert>
|
|
#include <cstdlib>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <iterator>
|
|
#include <string>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
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<std::string> 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<char>(input)), std::istreambuf_iterator<char>());
|
|
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);
|
|
}
|