311 lines
9.8 KiB
C++
311 lines
9.8 KiB
C++
#include <Void/System/Process.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <fcntl.h>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
|
|
using Void::Testing::expect;
|
|
using Void::Testing::expect_eq;
|
|
using Void::Testing::require;
|
|
|
|
constexpr auto kTestTimeout = std::chrono::seconds{10};
|
|
|
|
std::vector<std::byte> make_pattern(std::size_t size)
|
|
{
|
|
std::vector<std::byte> data(size);
|
|
for (std::size_t i = 0; i < size; ++i) {
|
|
// Include NUL and 0xFF so the transfer is provably binary-safe.
|
|
switch (i % 5) {
|
|
case 0:
|
|
data[i] = std::byte{0x00};
|
|
break;
|
|
case 1:
|
|
data[i] = std::byte{0xFF};
|
|
break;
|
|
case 2:
|
|
data[i] = std::byte{0x0A};
|
|
break;
|
|
case 3:
|
|
data[i] = static_cast<std::byte>(i & 0xFF);
|
|
break;
|
|
default:
|
|
data[i] = std::byte{0x7F};
|
|
break;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
std::vector<std::byte> read_until_eof(Void::Process::BinarySession& session,
|
|
bool from_stdout,
|
|
std::chrono::milliseconds timeout = kTestTimeout)
|
|
{
|
|
std::vector<std::byte> collected;
|
|
std::array<std::byte, 8192> chunk{};
|
|
for (;;) {
|
|
auto read = from_stdout ? session.read_stdout(chunk, timeout)
|
|
: session.read_stderr(chunk, timeout);
|
|
require(read.has_value());
|
|
if (read->bytes > 0) {
|
|
collected.insert(collected.end(),
|
|
chunk.begin(),
|
|
chunk.begin() + static_cast<std::ptrdiff_t>(read->bytes));
|
|
}
|
|
if (read->eof) {
|
|
break;
|
|
}
|
|
}
|
|
return collected;
|
|
}
|
|
|
|
int open_fd_count()
|
|
{
|
|
int count = 0;
|
|
for (int fd = 0; fd < 1024; ++fd) {
|
|
if (::fcntl(fd, F_GETFD) != -1) {
|
|
++count;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
Void::Process::SpawnOptions helper_options(std::vector<std::string> args)
|
|
{
|
|
Void::Process::SpawnOptions options;
|
|
options.executable = VOID_PROCESS_HELPER_PATH;
|
|
options.arguments = std::move(args);
|
|
return options;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Testing::Test binary_session_round_trip_larger_than_pipe_capacity()
|
|
{
|
|
Void::Process::BinarySessionOptions session_options;
|
|
session_options.max_stdout_buffer = 4ULL * 1024ULL * 1024ULL;
|
|
session_options.max_stderr_buffer = 4ULL * 1024ULL * 1024ULL;
|
|
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"cat"}), session_options);
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
const auto payload = make_pattern(1024ULL * 1024ULL);
|
|
auto written = session.write_stdin(payload, kTestTimeout);
|
|
require(written.has_value());
|
|
expect_eq(written->bytes, payload.size());
|
|
|
|
auto closed = session.close_stdin();
|
|
require(closed.has_value());
|
|
|
|
const auto echoed = read_until_eof(session, true);
|
|
expect_eq(echoed.size(), payload.size());
|
|
expect(echoed == payload);
|
|
|
|
auto waited = session.wait(kTestTimeout);
|
|
require(waited.has_value());
|
|
expect(waited->exited);
|
|
expect_eq(waited->code, 0);
|
|
}
|
|
|
|
Void::Testing::Test binary_session_drains_stderr_while_stdout_read()
|
|
{
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"flood", "262144", "524288"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
// Only stdout is read; stderr must still be drained into its bounded
|
|
// buffer so the child never blocks on a full stderr pipe.
|
|
const auto out = read_until_eof(session, true);
|
|
expect_eq(out.size(), std::size_t{262144});
|
|
for (const auto byte : out) {
|
|
expect(byte == std::byte{'O'});
|
|
}
|
|
|
|
const auto err = read_until_eof(session, false);
|
|
expect_eq(err.size(), std::size_t{524288});
|
|
for (const auto byte : err) {
|
|
expect(byte == std::byte{'E'});
|
|
}
|
|
|
|
auto waited = session.wait(kTestTimeout);
|
|
require(waited.has_value());
|
|
expect_eq(waited->code, 0);
|
|
}
|
|
|
|
Void::Testing::Test binary_session_partial_write_to_slow_sink()
|
|
{
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"ignore-sigterm", "5000"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
const auto payload = make_pattern(1024ULL * 1024ULL);
|
|
auto written = session.write_stdin(payload, std::chrono::milliseconds{100});
|
|
require(!written.has_value());
|
|
expect_eq(written.error().failure, Void::Process::BinaryFailureCode::timeout);
|
|
expect(written.error().bytes_transferred > 0);
|
|
expect(written.error().bytes_transferred < payload.size());
|
|
|
|
auto terminated = session.terminate(std::chrono::milliseconds{100});
|
|
require(terminated.has_value());
|
|
}
|
|
|
|
Void::Testing::Test binary_session_half_close_child_sees_eof()
|
|
{
|
|
auto spawned = Void::Process::BinarySession::spawn(helper_options({"cat"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
const auto payload = make_pattern(4096);
|
|
auto written = session.write_stdin(payload, kTestTimeout);
|
|
require(written.has_value());
|
|
expect_eq(written->bytes, payload.size());
|
|
|
|
auto closed = session.close_stdin();
|
|
require(closed.has_value());
|
|
// Closing twice is a no-op.
|
|
expect(session.close_stdin().has_value());
|
|
|
|
const auto echoed = read_until_eof(session, true);
|
|
expect(echoed == payload);
|
|
|
|
auto waited = session.wait(kTestTimeout);
|
|
require(waited.has_value());
|
|
expect_eq(waited->code, 0);
|
|
}
|
|
|
|
Void::Testing::Test binary_session_cancellation_race()
|
|
{
|
|
for (int iteration = 0; iteration < 5; ++iteration) {
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"ignore-sigterm", "5000"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
std::atomic<bool> cancel_now{false};
|
|
std::thread canceller{[&]() {
|
|
while (!cancel_now.load(std::memory_order_acquire)) {
|
|
std::this_thread::yield();
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{20});
|
|
session.cancel();
|
|
}};
|
|
|
|
std::array<std::byte, 256> chunk{};
|
|
bool observed_cancel = false;
|
|
for (;;) {
|
|
auto read = session.read_stdout(chunk);
|
|
if (!read.has_value()) {
|
|
observed_cancel =
|
|
read.error().failure == Void::Process::BinaryFailureCode::cancelled;
|
|
break;
|
|
}
|
|
cancel_now.store(true, std::memory_order_release);
|
|
}
|
|
cancel_now.store(true, std::memory_order_release);
|
|
canceller.join();
|
|
expect(observed_cancel);
|
|
|
|
auto terminated = session.terminate(std::chrono::milliseconds{100});
|
|
require(terminated.has_value());
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test binary_session_descendant_held_pipe_times_out_and_cleans_up()
|
|
{
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"descendant-holds-stdout", "5000"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
std::array<std::byte, 64> chunk{};
|
|
std::string ready;
|
|
while (ready.find("ready\n") == std::string::npos) {
|
|
auto read = session.read_stdout(chunk, kTestTimeout);
|
|
require(read.has_value());
|
|
ready.append(reinterpret_cast<const char*>(chunk.data()), read->bytes);
|
|
require(!read->eof);
|
|
}
|
|
|
|
// The leader exits but its descendant keeps the stdout pipe open, so no
|
|
// EOF arrives; a bounded read must time out instead of hanging.
|
|
auto read = session.read_stdout(chunk, std::chrono::milliseconds{100});
|
|
require(!read.has_value());
|
|
expect_eq(read.error().failure, Void::Process::BinaryFailureCode::timeout);
|
|
|
|
auto terminated = session.terminate(std::chrono::milliseconds{60});
|
|
require(terminated.has_value());
|
|
expect(!session.child().process_group_alive());
|
|
}
|
|
|
|
Void::Testing::Test binary_session_cleanup_leaves_no_fds_or_children()
|
|
{
|
|
const int fds_before = open_fd_count();
|
|
long long last_pid = -1;
|
|
{
|
|
auto spawned = Void::Process::BinarySession::spawn(helper_options({"cat"}));
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
last_pid = session.pid();
|
|
|
|
std::array<std::byte, 4> payload{std::byte{'a'},
|
|
std::byte{0},
|
|
std::byte{0xFF},
|
|
std::byte{'z'}};
|
|
auto written = session.write_stdin(payload, kTestTimeout);
|
|
require(written.has_value());
|
|
require(session.close_stdin().has_value());
|
|
const auto echoed = read_until_eof(session, true);
|
|
expect_eq(echoed.size(), payload.size());
|
|
auto waited = session.wait(kTestTimeout);
|
|
require(waited.has_value());
|
|
expect_eq(waited->code, 0);
|
|
}
|
|
expect_eq(open_fd_count(), fds_before);
|
|
// The session reaped the child before releasing its process slot.
|
|
expect(::kill(static_cast<pid_t>(last_pid), 0) == -1);
|
|
expect_eq(errno, ESRCH);
|
|
}
|
|
|
|
Void::Testing::Test binary_session_truncates_when_configured()
|
|
{
|
|
Void::Process::BinarySessionOptions session_options;
|
|
session_options.max_stdout_buffer = 1024;
|
|
session_options.truncate_output_on_overflow = true;
|
|
|
|
auto spawned = Void::Process::BinarySession::spawn(
|
|
helper_options({"flood", "65536", "0"}), session_options);
|
|
require(spawned.has_value());
|
|
auto& session = *spawned;
|
|
|
|
const auto out = read_until_eof(session, true);
|
|
expect(out.size() < std::size_t{65536});
|
|
expect(session.stdout_truncated());
|
|
for (const auto byte : out) {
|
|
expect(byte == std::byte{'O'});
|
|
}
|
|
|
|
auto waited = session.wait(kTestTimeout);
|
|
require(waited.has_value());
|
|
expect_eq(waited->code, 0);
|
|
}
|