740 lines
29 KiB
C++
740 lines
29 KiB
C++
#include <Void/Async/Executor.hpp>
|
|
#include <Void/Async/Operation.hpp>
|
|
#include <Void/Async/TaskScope.hpp>
|
|
#include <Void/Binary/ByteBuffer.hpp>
|
|
#include <Void/IO/Driver.hpp>
|
|
#include <Void/IPC/IPC.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
#include <Void/Time/Clock.hpp>
|
|
|
|
#include <fcntl.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/un.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include <array>
|
|
#include <atomic>
|
|
#include <cerrno>
|
|
#include <chrono>
|
|
#include <csignal>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <format>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
|
|
using Void::Testing::expect;
|
|
using Void::Testing::expect_eq;
|
|
using Void::Testing::require;
|
|
|
|
[[nodiscard]] Void::Binary::FrozenBytes make_bytes(std::string_view sv)
|
|
{
|
|
Void::Binary::ByteBuffer buf{sv.size()};
|
|
(void)buf.writeString(sv);
|
|
return buf.freeze();
|
|
}
|
|
|
|
[[nodiscard]] std::string as_string(const Void::Binary::FrozenBytes& fb)
|
|
{
|
|
return std::string{reinterpret_cast<const char*>(fb.data()), fb.size()};
|
|
}
|
|
|
|
void pump_loop(Void::IO::Driver& driver,
|
|
Void::Async::ManualExecutor& exec,
|
|
Void::Async::TaskScope& scope,
|
|
int max_iters = 4000)
|
|
{
|
|
for (int i = 0; i < max_iters && scope.active_count() > 0; ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
const bool drained = (scope.active_count() == 0);
|
|
if (!drained) {
|
|
scope.request_stop();
|
|
for (int i = 0; i < 200 && scope.active_count() > 0; ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{0});
|
|
(void)exec.drain();
|
|
}
|
|
}
|
|
require(drained);
|
|
}
|
|
|
|
struct ShortIpcDir {
|
|
std::filesystem::path dir{};
|
|
|
|
ShortIpcDir()
|
|
{
|
|
static std::atomic<std::uint64_t> seq{0};
|
|
const auto id = seq.fetch_add(1, std::memory_order_relaxed);
|
|
dir = std::filesystem::path{
|
|
std::format("/tmp/v_ipc_{}_{}", ::getpid(), id)};
|
|
std::filesystem::remove_all(dir);
|
|
require(::mkdir(dir.c_str(), 0700) == 0);
|
|
}
|
|
|
|
~ShortIpcDir()
|
|
{
|
|
std::error_code ec;
|
|
std::filesystem::remove_all(dir, ec);
|
|
}
|
|
|
|
ShortIpcDir(const ShortIpcDir&) = delete;
|
|
ShortIpcDir& operator=(const ShortIpcDir&) = delete;
|
|
};
|
|
|
|
[[nodiscard]] Void::Coro::Task<void> run_handshake_task(
|
|
Void::IPC::Session* sess,
|
|
Void::IPC::HelloInfo info,
|
|
Void::Async::OperationOptions opts,
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error>* out)
|
|
{
|
|
*out = co_await sess->handshake(std::move(info), std::move(opts));
|
|
}
|
|
|
|
[[nodiscard]] Void::Coro::Task<void> run_request_task(
|
|
Void::IPC::Session* sess,
|
|
Void::Binary::FrozenBytes req,
|
|
Void::Async::OperationOptions opts,
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure>* out)
|
|
{
|
|
*out = co_await sess->request(std::move(req), std::move(opts));
|
|
}
|
|
|
|
[[nodiscard]] Void::Coro::Task<void> run_close_session_task(
|
|
Void::IPC::Session* sess,
|
|
Void::Async::OperationOptions opts,
|
|
Void::Result::Result<void, Void::Result::Error>* out)
|
|
{
|
|
*out = co_await sess->close_and_join(std::move(opts));
|
|
}
|
|
|
|
[[nodiscard]] Void::Coro::Task<
|
|
Void::Result::Result<Void::Binary::FrozenBytes, Void::Result::Error>>
|
|
echo_upper_handler(Void::Binary::FrozenBytes payload, std::stop_token)
|
|
{
|
|
std::string s = as_string(payload);
|
|
for (char& ch : s) {
|
|
if (ch >= 'a' && ch <= 'z') {
|
|
ch = static_cast<char>(ch - 'a' + 'A');
|
|
}
|
|
}
|
|
co_return make_bytes(s);
|
|
}
|
|
|
|
[[nodiscard]] Void::Coro::Task<
|
|
Void::Result::Result<Void::Binary::FrozenBytes, Void::Result::Error>>
|
|
gated_echo_handler(
|
|
Void::Binary::FrozenBytes payload,
|
|
std::stop_token cancel,
|
|
std::shared_ptr<Void::Async::OperationState<void>> gate,
|
|
std::atomic<bool>* entered)
|
|
{
|
|
(void)cancel;
|
|
if (entered != nullptr) {
|
|
entered->store(true, std::memory_order_release);
|
|
}
|
|
(void)co_await gate->awaitable();
|
|
co_return payload;
|
|
}
|
|
|
|
[[nodiscard]] Void::Coro::Task<
|
|
Void::Result::Result<Void::Binary::FrozenBytes, Void::Result::Error>>
|
|
cancel_aware_wait_handler(Void::Binary::FrozenBytes payload,
|
|
std::stop_token cancel,
|
|
Void::Async::Executor* exec,
|
|
std::atomic<bool>* entered)
|
|
{
|
|
if (entered != nullptr) {
|
|
entered->store(true, std::memory_order_release);
|
|
}
|
|
auto op = Void::Async::OperationState<void>::create(
|
|
Void::Async::OperationOptions{.stop = cancel},
|
|
exec,
|
|
Void::Async::StopBehavior::settle_cancelled);
|
|
(void)co_await op->awaitable(exec);
|
|
co_return payload;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Testing::Test ipc_wire_codec_fragmentation_coalescing_and_quotas()
|
|
{
|
|
// 1. V1 24B golden big-endian header + payload validation.
|
|
Void::IPC::FrameHeader hdr;
|
|
hdr.type = Void::IPC::FrameType::request;
|
|
hdr.request_id = 0x0102030405060708ULL;
|
|
auto f1 = Void::IPC::encode_frame(hdr, make_bytes("alpha").view());
|
|
require(f1.has_value());
|
|
expect_eq(f1->size(), Void::IPC::kFrameHeaderSize + std::size_t{5});
|
|
expect_eq(static_cast<char>((*f1).data()[0]), 'V');
|
|
expect_eq(static_cast<char>((*f1).data()[1]), 'D');
|
|
expect_eq(static_cast<char>((*f1).data()[2]), 'I');
|
|
expect_eq(static_cast<char>((*f1).data()[3]), 'P');
|
|
|
|
// Request/response/cancel require nonzero request_id; hello/one_way/goaway forbid it.
|
|
expect(!Void::IPC::validate_frame_header(
|
|
Void::IPC::FrameHeader{.type = Void::IPC::FrameType::request,
|
|
.request_id = 0},
|
|
Void::IPC::IpcLimits{})
|
|
.has_value());
|
|
expect(!Void::IPC::validate_frame_header(
|
|
Void::IPC::FrameHeader{.type = Void::IPC::FrameType::hello,
|
|
.request_id = 7},
|
|
Void::IPC::IpcLimits{})
|
|
.has_value());
|
|
expect(!Void::IPC::validate_frame_header(
|
|
Void::IPC::FrameHeader{.version = 99,
|
|
.type = Void::IPC::FrameType::request,
|
|
.request_id = 1},
|
|
Void::IPC::IpcLimits{})
|
|
.has_value());
|
|
expect(!Void::IPC::validate_frame_header(
|
|
Void::IPC::FrameHeader{.stream_id = 1,
|
|
.request_id = 1},
|
|
Void::IPC::IpcLimits{})
|
|
.has_value());
|
|
|
|
// 2. Fragmentation (1 byte at a time) + coalescing + exact global/session quota enforcement.
|
|
auto gq = Void::IPC::BufferQuota::create(128);
|
|
auto sq = Void::IPC::BufferQuota::create(128);
|
|
Void::IPC::IpcLimits lim;
|
|
lim.max_frame_payload = 64;
|
|
|
|
Void::IPC::FrameDecoder decoder{lim, gq, sq};
|
|
auto f2 = Void::IPC::encode_frame(
|
|
Void::IPC::FrameHeader{.type = Void::IPC::FrameType::response,
|
|
.request_id = 42},
|
|
make_bytes("beta").view());
|
|
require(f2.has_value());
|
|
|
|
std::vector<std::byte> combined;
|
|
combined.insert(combined.end(), f1->view().begin(), f1->view().end());
|
|
combined.insert(combined.end(), f2->view().begin(), f2->view().end());
|
|
|
|
std::vector<Void::IPC::DecodedFrame> out_frames;
|
|
for (std::size_t i = 0; i < combined.size(); ++i) {
|
|
auto fed = decoder.feed(std::span<const std::byte>{&combined[i], 1});
|
|
require(fed.has_value());
|
|
for (auto& df : *fed) {
|
|
out_frames.push_back(std::move(df));
|
|
}
|
|
}
|
|
require(out_frames.size() == 2);
|
|
expect_eq(as_string(out_frames[0].payload), std::string{"alpha"});
|
|
expect_eq(out_frames[1].header.request_id, std::uint64_t{42});
|
|
expect_eq(as_string(out_frames[1].payload), std::string{"beta"});
|
|
expect_eq(gq->used(),
|
|
Void::IPC::FrameDecoder::frame_quota_cost(5) +
|
|
Void::IPC::FrameDecoder::frame_quota_cost(4));
|
|
out_frames.clear();
|
|
expect_eq(gq->used(), std::size_t{0});
|
|
expect_eq(sq->used(), std::size_t{0});
|
|
|
|
// Oversize payload_len fails BEFORE reserving quota.
|
|
std::array<std::byte, Void::IPC::kFrameHeaderSize> big_hdr{};
|
|
require(Void::IPC::encode_frame_header(
|
|
Void::IPC::FrameHeader{.type = Void::IPC::FrameType::request,
|
|
.payload_len = 65,
|
|
.request_id = 9},
|
|
big_hdr)
|
|
.has_value());
|
|
Void::IPC::FrameDecoder oversize_dec{lim, gq, sq};
|
|
auto ov_res = oversize_dec.feed(big_hdr);
|
|
require(!ov_res.has_value());
|
|
expect_eq(ov_res.error().code().value,
|
|
static_cast<std::int32_t>(Void::IPC::IpcErrorCode::frame_too_large));
|
|
expect_eq(gq->used(), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test ipc_uds_security_stale_recovery_and_pipe_pair_session()
|
|
{
|
|
ShortIpcDir tmp;
|
|
Void::Async::ManualExecutor exec;
|
|
auto drv_res = Void::IO::Driver::create(exec);
|
|
require(drv_res.has_value());
|
|
auto& driver = **drv_res;
|
|
|
|
// 1. Reject >= 108B sun_path, 0755 dir, and symlink paths.
|
|
{
|
|
const std::string long_path = "/tmp/" + std::string(110, 'x') + ".sock";
|
|
auto r = Void::IPC::LocalListener::listen(long_path);
|
|
require(!r.has_value());
|
|
expect_eq(r.error().code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::invalid_argument));
|
|
}
|
|
{
|
|
const auto bad_dir = tmp.dir / "world";
|
|
require(::mkdir(bad_dir.c_str(), 0755) == 0);
|
|
(void)::chmod(bad_dir.c_str(), 0755);
|
|
auto r = Void::IPC::LocalListener::listen(bad_dir / "s.sock");
|
|
require(!r.has_value());
|
|
expect_eq(r.error().code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::permission_denied));
|
|
}
|
|
{
|
|
const auto priv_dir = tmp.dir / "priv";
|
|
require(::mkdir(priv_dir.c_str(), 0700) == 0);
|
|
const auto sym = priv_dir / "link.sock";
|
|
std::filesystem::create_symlink(tmp.dir / "missing", sym);
|
|
auto r = Void::IPC::LocalListener::listen(sym);
|
|
require(!r.has_value());
|
|
expect_eq(r.error().code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::permission_denied));
|
|
}
|
|
|
|
// 2. Stale socket recovery succeeds safely without disturbing active sockets.
|
|
const auto sock_path = tmp.dir / "svc.sock";
|
|
{
|
|
const int stale_fd = ::socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
|
|
require(stale_fd >= 0);
|
|
sockaddr_un un{};
|
|
un.sun_family = AF_UNIX;
|
|
std::memcpy(un.sun_path, sock_path.native().data(), sock_path.native().size());
|
|
require(::bind(stale_fd,
|
|
reinterpret_cast<const sockaddr*>(&un),
|
|
static_cast<socklen_t>(
|
|
offsetof(sockaddr_un, sun_path) +
|
|
sock_path.native().size() + 1)) == 0);
|
|
::close(stale_fd); // leaves stale unbound/non-listening socket file!
|
|
}
|
|
|
|
auto listener_res = Void::IPC::LocalListener::listen(sock_path);
|
|
require(listener_res.has_value());
|
|
auto& listener = *listener_res;
|
|
|
|
struct stat sock_st {};
|
|
require(::lstat(sock_path.c_str(), &sock_st) == 0);
|
|
expect_eq(static_cast<unsigned>(sock_st.st_mode & 0777), 0600U);
|
|
|
|
// Live listener refuses second concurrent listener.
|
|
auto dup_listener = Void::IPC::LocalListener::listen(sock_path);
|
|
require(!dup_listener.has_value());
|
|
expect_eq(dup_listener.error().code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::already_exists));
|
|
|
|
// Wrong UID policy rejected cleanly on connect.
|
|
auto deny_res = Void::IPC::connect_local(
|
|
driver,
|
|
sock_path,
|
|
Void::IPC::PeerPolicy{.require_uid_match = true,
|
|
.set_allowed_uid_to_current_user = false,
|
|
.allowed_uid = ::geteuid() + 1000});
|
|
require(!deny_res.has_value());
|
|
expect_eq(deny_res.error().code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::permission_denied));
|
|
{
|
|
auto stale = listener.try_accept(driver);
|
|
(void)stale;
|
|
(void)driver.poll_once(std::chrono::milliseconds{0});
|
|
}
|
|
|
|
// Accept & full-duplex handshake + echo request over AF_UNIX.
|
|
auto client_tr = Void::IPC::connect_local(driver, sock_path);
|
|
require(client_tr.has_value());
|
|
auto server_opt = listener.try_accept(driver);
|
|
require(server_opt.has_value() && server_opt->has_value());
|
|
expect((*server_opt)->credentials().available);
|
|
|
|
Void::IPC::Session client_sess{driver, exec, std::move(*client_tr)};
|
|
Void::IPC::Session server_sess{driver, exec, std::move(**server_opt)};
|
|
require(server_sess
|
|
.start([](Void::Binary::FrozenBytes payload, std::stop_token st) {
|
|
return echo_upper_handler(std::move(payload), st);
|
|
})
|
|
.has_value());
|
|
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> h1;
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> h2;
|
|
{
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope
|
|
.spawn(run_handshake_task(&client_sess, {}, {}, &h1))
|
|
.has_value());
|
|
require(scope
|
|
.spawn(run_handshake_task(&server_sess, {}, {}, &h2))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(h1.has_value() && h2.has_value());
|
|
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure> rpc_out{
|
|
std::unexpected{Void::IPC::CallFailure{}}};
|
|
{
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope
|
|
.spawn(run_request_task(
|
|
&client_sess, make_bytes("hello_uds"), {}, &rpc_out))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(rpc_out.has_value());
|
|
expect_eq(as_string(*rpc_out), std::string{"HELLO_UDS"});
|
|
|
|
Void::Result::Result<void, Void::Result::Error> c1;
|
|
Void::Result::Result<void, Void::Result::Error> c2;
|
|
{
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope.spawn(run_close_session_task(&client_sess, {}, &c1))
|
|
.has_value());
|
|
require(scope.spawn(run_close_session_task(&server_sess, {}, &c2))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(c1.has_value() && c2.has_value());
|
|
listener.close();
|
|
|
|
// 3. adopt_pipe_pair with supervised VoidProcessHelper ipc-pipe-echo child.
|
|
int to_child[2]{-1, -1};
|
|
int from_child[2]{-1, -1};
|
|
require(::pipe2(to_child, O_CLOEXEC) == 0);
|
|
require(::pipe2(from_child, O_CLOEXEC) == 0);
|
|
|
|
const pid_t cpid = ::fork();
|
|
require(cpid >= 0);
|
|
if (cpid == 0) {
|
|
::dup2(to_child[0], STDIN_FILENO);
|
|
::dup2(from_child[1], STDOUT_FILENO);
|
|
::close(to_child[0]);
|
|
::close(to_child[1]);
|
|
::close(from_child[0]);
|
|
::close(from_child[1]);
|
|
const std::string helper = VOID_PROCESS_HELPER_PATH;
|
|
char arg0[] = "VoidProcessHelper";
|
|
char arg1[] = "ipc-pipe-echo";
|
|
char* argv[] = {arg0, arg1, nullptr};
|
|
::execv(helper.c_str(), argv);
|
|
::_exit(127);
|
|
}
|
|
::close(to_child[0]);
|
|
::close(from_child[1]);
|
|
|
|
auto pipe_tr = Void::IPC::adopt_pipe_pair(driver, from_child[0], to_child[1]);
|
|
require(pipe_tr.has_value());
|
|
expect(!pipe_tr->credentials().available);
|
|
|
|
Void::IPC::Session pipe_sess{driver, exec, std::move(*pipe_tr)};
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> ph;
|
|
{
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope
|
|
.spawn(run_handshake_task(&pipe_sess, {}, {}, &ph))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(ph.has_value());
|
|
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure> pipe_reply{
|
|
std::unexpected{Void::IPC::CallFailure{}}};
|
|
{
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope
|
|
.spawn(run_request_task(
|
|
&pipe_sess, make_bytes("pipe_echo_ok"), {}, &pipe_reply))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(pipe_reply.has_value());
|
|
expect_eq(as_string(*pipe_reply), std::string{"pipe_echo_ok"});
|
|
|
|
{
|
|
Void::Result::Result<void, Void::Result::Error> pc;
|
|
Void::Async::TaskScope scope{exec};
|
|
require(scope
|
|
.spawn(run_close_session_task(&pipe_sess, {}, &pc))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
require(pc.has_value());
|
|
}
|
|
|
|
int cstat = 0;
|
|
while (::waitpid(cpid, &cstat, 0) < 0 && errno == EINTR) {
|
|
}
|
|
expect(WIFEXITED(cstat) && WEXITSTATUS(cstat) == 0);
|
|
(void)driver.shutdown_and_join();
|
|
}
|
|
|
|
Void::Testing::Test ipc_session_control_lane_timeouts_tombstones_and_disconnect()
|
|
{
|
|
Void::Async::ManualExecutor exec;
|
|
Void::Time::ManualClock clock{
|
|
Void::Time::MonotonicClock::time_point{std::chrono::seconds{10}}};
|
|
auto drv_res =
|
|
Void::IO::Driver::create(exec, Void::IO::BackendOptions{}, clock);
|
|
require(drv_res.has_value());
|
|
auto& driver = **drv_res;
|
|
|
|
// 1. Handshake timeout via injected ManualClock.
|
|
{
|
|
auto pair = Void::IPC::create_socket_pair_transports(driver);
|
|
require(pair.has_value());
|
|
Void::IPC::IpcLimits lim;
|
|
lim.handshake_timeout = std::chrono::milliseconds{100};
|
|
Void::IPC::Session client{driver, exec, std::move(pair->first), lim, clock};
|
|
Void::IPC::Session silent_server{
|
|
driver, exec, std::move(pair->second), lim, clock};
|
|
(void)silent_server;
|
|
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> hs_res;
|
|
Void::Async::TaskScope scope{exec, {}, clock};
|
|
require(scope.spawn(run_handshake_task(&client, {}, {}, &hs_res))
|
|
.has_value());
|
|
for (int i = 0; i < 4; ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{0});
|
|
(void)exec.drain();
|
|
}
|
|
(void)clock.advance(std::chrono::milliseconds{150});
|
|
pump_loop(driver, exec, scope, 200);
|
|
require(!hs_res.has_value());
|
|
expect_eq(hs_res.error().code().value,
|
|
static_cast<std::int32_t>(Void::IPC::IpcErrorCode::timed_out));
|
|
}
|
|
|
|
// 2. Saturated handlers + reserved control-lane overload + late response tombstone + cancel.
|
|
{
|
|
auto pair = Void::IPC::create_socket_pair_transports(driver);
|
|
require(pair.has_value());
|
|
Void::IPC::IpcLimits lim;
|
|
lim.max_active_handlers = 1;
|
|
lim.max_outbound_frames = 32;
|
|
lim.rate_limit_burst = 64.0;
|
|
lim.rate_limit_per_sec = 10000.0;
|
|
|
|
Void::IPC::Session client{driver, exec, std::move(pair->first), lim, clock};
|
|
Void::IPC::Session server{driver, exec, std::move(pair->second), lim, clock};
|
|
|
|
auto gate = Void::Async::OperationState<void>::create({}, &exec);
|
|
std::atomic<bool> gate_acquired{false};
|
|
require(server
|
|
.start([gate, &gate_acquired](
|
|
Void::Binary::FrozenBytes payload,
|
|
std::stop_token cancel) {
|
|
return gated_echo_handler(
|
|
std::move(payload), cancel, gate, &gate_acquired);
|
|
})
|
|
.has_value());
|
|
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> h1;
|
|
Void::Result::Result<Void::IPC::HelloInfo, Void::Result::Error> h2;
|
|
{
|
|
Void::Async::TaskScope scope{exec, {}, clock};
|
|
require(scope.spawn(run_handshake_task(&client, {}, {}, &h1))
|
|
.has_value());
|
|
require(scope.spawn(run_handshake_task(&server, {}, {}, &h2))
|
|
.has_value());
|
|
pump_loop(driver, exec, scope);
|
|
}
|
|
require(h1.has_value() && h2.has_value());
|
|
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure> slow_call{
|
|
std::unexpected{Void::IPC::CallFailure{}}};
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure> ov_call{
|
|
std::unexpected{Void::IPC::CallFailure{}}};
|
|
|
|
Void::Async::TaskScope scope{exec, {}, clock};
|
|
gate->acquire_backend_ref();
|
|
require(scope
|
|
.spawn(run_request_task(
|
|
&client,
|
|
make_bytes("slow_one"),
|
|
Void::Async::OperationOptions{
|
|
.deadline =
|
|
clock.now() + std::chrono::milliseconds{100}},
|
|
&slow_call))
|
|
.has_value());
|
|
|
|
// Pump until server handler enters gate.
|
|
for (int i = 0; i < 400 && !gate_acquired.load(); ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
require(gate_acquired.load());
|
|
|
|
// 2nd concurrent request hits max_active_handlers == 1 -> immediate control-lane overloaded error!
|
|
require(scope
|
|
.spawn(run_request_task(&client, make_bytes("second"), {}, &ov_call))
|
|
.has_value());
|
|
for (int i = 0; i < 400 && ov_call.error().error.message().empty(); ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
require(!ov_call.has_value());
|
|
expect_eq(ov_call.error().error.code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::overloaded));
|
|
expect(ov_call.error().effect == Void::IPC::EffectStatus::not_sent);
|
|
|
|
// Advance ManualClock so slow_call times out cleanly -> EffectStatus::unknown & tombstone added.
|
|
(void)clock.advance(std::chrono::milliseconds{150});
|
|
for (int i = 0; i < 400 && slow_call.error().error.message().empty();
|
|
++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
require(!slow_call.has_value());
|
|
expect_eq(slow_call.error().error.code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::timed_out));
|
|
expect(slow_call.error().effect == Void::IPC::EffectStatus::unknown);
|
|
expect(slow_call.error().may_have_executed);
|
|
|
|
// Release server handler -> late response arrives and is dropped cleanly via tombstone!
|
|
if (auto t = gate->try_commit_value(); t.has_value()) {
|
|
t->dispatch();
|
|
}
|
|
gate->release_backend_ref_and_dispatch();
|
|
for (int i = 0;
|
|
i < 400 && client.stats().late_responses_dropped == 0;
|
|
++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
pump_loop(driver, exec, scope);
|
|
expect(client.stats().late_responses_dropped >= 1);
|
|
|
|
// Self-join rejected inside synchronous OneWayHandler.
|
|
std::optional<Void::Result::Error> self_join_err;
|
|
Void::IPC::Session* client_ptr = &client;
|
|
require(client
|
|
.start(echo_upper_handler,
|
|
[client_ptr, &self_join_err](
|
|
Void::Binary::FrozenBytes) {
|
|
auto task = client_ptr->close_and_join();
|
|
auto res = task.run();
|
|
if (!res.has_value()) {
|
|
self_join_err = res.error();
|
|
}
|
|
})
|
|
.has_value());
|
|
require(server.send_one_way(make_bytes("trigger_sj")).has_value());
|
|
for (int i = 0; i < 400 && !self_join_err.has_value(); ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
require(self_join_err.has_value());
|
|
expect_eq(self_join_err->code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::self_join));
|
|
|
|
// Peer disconnect faults in-flight request once with transport_lost + unknown.
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure> disc_call{
|
|
std::unexpected{Void::IPC::CallFailure{}}};
|
|
std::atomic<bool> entered_stuck{false};
|
|
require(server
|
|
.start([&exec, &entered_stuck](
|
|
Void::Binary::FrozenBytes payload,
|
|
std::stop_token cancel) {
|
|
return cancel_aware_wait_handler(
|
|
std::move(payload), cancel, &exec, &entered_stuck);
|
|
})
|
|
.has_value());
|
|
|
|
Void::Async::TaskScope dc_scope{exec, {}, clock};
|
|
require(dc_scope
|
|
.spawn(run_request_task(
|
|
&client, make_bytes("in_flight"), {}, &disc_call))
|
|
.has_value());
|
|
for (int i = 0; i < 400 && !entered_stuck.load(); ++i) {
|
|
(void)exec.drain();
|
|
(void)driver.poll_once(std::chrono::milliseconds{1});
|
|
(void)exec.drain();
|
|
}
|
|
require(entered_stuck.load());
|
|
// Close server cleanly while client has an unfulfilled request -> client sees transport_lost / closed.
|
|
Void::Result::Result<void, Void::Result::Error> s_close;
|
|
require(dc_scope
|
|
.spawn(run_close_session_task(&server, {}, &s_close))
|
|
.has_value());
|
|
pump_loop(driver, exec, dc_scope);
|
|
require(!disc_call.has_value());
|
|
expect(disc_call.error().error.code().value ==
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::transport_lost) ||
|
|
disc_call.error().error.code().value ==
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::closed));
|
|
expect(disc_call.error().effect == Void::IPC::EffectStatus::unknown);
|
|
expect(disc_call.error().may_have_executed);
|
|
|
|
// Rate limit rejects bursts beyond token bucket capacity via control lane.
|
|
{
|
|
auto rpair = Void::IPC::create_socket_pair_transports(driver);
|
|
require(rpair.has_value());
|
|
Void::IPC::IpcLimits rlim;
|
|
rlim.rate_limit_burst = 1.0;
|
|
rlim.rate_limit_per_sec = 0.001;
|
|
Void::IPC::Session rcli{
|
|
driver, exec, std::move(rpair->first), rlim, clock};
|
|
Void::IPC::Session rsrv{
|
|
driver, exec, std::move(rpair->second), rlim, clock};
|
|
require(rsrv.start(echo_upper_handler).has_value());
|
|
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure>
|
|
rok{std::unexpected{Void::IPC::CallFailure{}}};
|
|
std::expected<Void::Binary::FrozenBytes, Void::IPC::CallFailure>
|
|
rlim_res{std::unexpected{Void::IPC::CallFailure{}}};
|
|
Void::Async::TaskScope rscope{exec, {}, clock};
|
|
require(rscope
|
|
.spawn(run_request_task(
|
|
&rcli, make_bytes("ok"), {}, &rok))
|
|
.has_value());
|
|
require(rscope
|
|
.spawn(run_request_task(
|
|
&rcli, make_bytes("excess"), {}, &rlim_res))
|
|
.has_value());
|
|
pump_loop(driver, exec, rscope);
|
|
require(rok.has_value());
|
|
expect_eq(as_string(*rok), std::string{"OK"});
|
|
require(!rlim_res.has_value());
|
|
expect_eq(rlim_res.error().error.code().value,
|
|
static_cast<std::int32_t>(
|
|
Void::IPC::IpcErrorCode::rate_limited));
|
|
expect(rlim_res.error().effect == Void::IPC::EffectStatus::not_sent);
|
|
|
|
Void::Result::Result<void, Void::Result::Error> rc1;
|
|
Void::Result::Result<void, Void::Result::Error> rc2;
|
|
Void::Async::TaskScope rc_scope{exec, {}, clock};
|
|
require(rc_scope.spawn(run_close_session_task(&rcli, {}, &rc1))
|
|
.has_value());
|
|
require(rc_scope.spawn(run_close_session_task(&rsrv, {}, &rc2))
|
|
.has_value());
|
|
pump_loop(driver, exec, rc_scope);
|
|
require(rc1.has_value() && rc2.has_value());
|
|
}
|
|
|
|
Void::Result::Result<void, Void::Result::Error> c_close;
|
|
Void::Async::TaskScope fin_scope{exec, {}, clock};
|
|
require(fin_scope
|
|
.spawn(run_close_session_task(&client, {}, &c_close))
|
|
.has_value());
|
|
pump_loop(driver, exec, fin_scope);
|
|
}
|
|
|
|
(void)driver.shutdown_and_join();
|
|
}
|