BastionGuard/tests/test_safe_process.cpp

39 lines
1.6 KiB
C++

#include "../src/SafeProcess.hpp"
#include <cassert>
#include <filesystem>
#include <iostream>
int main() {
const auto marker = std::filesystem::temp_directory_path() /
"bastionguard-argv-regression-should-not-exist";
std::filesystem::remove(marker);
const std::string hostile = "https://site.invalid/\"; touch " + marker.string() + "; #";
const auto safe = bg_process::run({"/usr/bin/printf", "%s", hostile});
assert(safe.ok());
assert(safe.output == hostile);
assert(!std::filesystem::exists(marker));
const auto env = bg_process::run({"/usr/bin/printenv", "BASTIONGUARD_TEST_ARG"}, false,
4096, {{"BASTIONGUARD_TEST_ARG", "semi;$(id) `touch no`"}});
assert(env.ok());
assert(env.output == "semi;$(id) `touch no`\n");
const auto failure = bg_process::run({"/bin/sh", "-c", "exit 42"});
assert(failure.started && failure.exit_code == 42 && !failure.ok());
const auto missing = bg_process::run({"/nonexistent-bastionguard-binary"});
assert(!missing.started && !missing.ok());
const auto oversized = bg_process::run({"/usr/bin/printf", "%s", "0123456789"}, false, 4);
assert(oversized.truncated && !oversized.ok());
std::string lines;
const auto streamed = bg_process::run({"/usr/bin/printf", "line 1\nline 2"}, false,
1000, {}, [&](const std::string& line) {
lines += line;
return true;
});
assert(streamed.ok() && lines == "line 1\nline 2");
std::cout << "safe process argv, environment, output bounds and streaming: OK\n";
}