291 lines
10 KiB
C++
291 lines
10 KiB
C++
#include <Void/RPC/Codec.hpp>
|
|
#include <Void/RPC/Rpc.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
Void::RPC::OwnedBytes to_bytes(std::string_view s)
|
|
{
|
|
Void::RPC::OwnedBytes out(s.size());
|
|
if (!s.empty()) {
|
|
std::memcpy(out.data(), s.data(), s.size());
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string from_bytes(const Void::RPC::OwnedBytes& b)
|
|
{
|
|
return std::string(reinterpret_cast<const char*>(b.data()), b.size());
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test rpc_wire_v1_codec_roundtrip_and_malformed_rejection()
|
|
{
|
|
using namespace Void::RPC;
|
|
|
|
WireEnvelope env;
|
|
env.kind = FrameKind::UnaryRequest;
|
|
env.call_id = CallId::generate();
|
|
env.method = MethodId{0x0102030405060708ULL, 42U};
|
|
env.schema_version = 3U;
|
|
env.remaining_timeout_ms = 5000U;
|
|
env.metadata = {{"trace-id", "t-123"}, {"tenant", "alpha"}};
|
|
env.payload = to_bytes("hello-vrpc");
|
|
|
|
auto encoded = encode_envelope(env);
|
|
Void::Testing::require(encoded.has_value());
|
|
Void::Testing::expect(encoded->size() >= kWireFixedHeaderBytes);
|
|
|
|
auto decoded = decode_envelope(*encoded);
|
|
Void::Testing::require(decoded.has_value());
|
|
Void::Testing::expect(decoded->call_id == env.call_id);
|
|
Void::Testing::expect(decoded->method == env.method);
|
|
Void::Testing::expect_eq(decoded->schema_version, 3U);
|
|
Void::Testing::expect_eq(decoded->remaining_timeout_ms, 5000U);
|
|
Void::Testing::expect(decoded->metadata == env.metadata);
|
|
Void::Testing::expect_eq(from_bytes(decoded->payload),
|
|
std::string{"hello-vrpc"});
|
|
|
|
// 1. Bad magic rejected.
|
|
auto bad_magic = *encoded;
|
|
bad_magic[0] = std::byte{'X'};
|
|
Void::Testing::expect(!decode_envelope(bad_magic).has_value());
|
|
|
|
// 2. Unknown major version rejected with IncompatibleVersion.
|
|
auto bad_major = *encoded;
|
|
bad_major[4] = std::byte{0};
|
|
bad_major[5] = std::byte{2};
|
|
auto r_maj = decode_envelope(bad_major);
|
|
Void::Testing::expect(!r_maj.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_maj.error()) ==
|
|
RpcStatusCode::IncompatibleVersion);
|
|
|
|
// 3. Non-zero reserved bytes rejected.
|
|
auto bad_res = *encoded;
|
|
bad_res[18] = std::byte{0x01};
|
|
Void::Testing::expect(!decode_envelope(bad_res).has_value());
|
|
|
|
// 4. Unknown required flag fails closed.
|
|
auto bad_flag = *encoded;
|
|
bad_flag[17] = std::byte{0x10};
|
|
auto r_flag = decode_envelope(bad_flag);
|
|
Void::Testing::expect(!r_flag.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_flag.error()) ==
|
|
RpcStatusCode::IncompatibleVersion);
|
|
|
|
// 5. Truncated frame rejected.
|
|
auto trunc = *encoded;
|
|
trunc.pop_back();
|
|
Void::Testing::expect(!decode_envelope(trunc).has_value());
|
|
|
|
// 6. Oversized payload limit rejected before allocation.
|
|
RpcLimits tiny;
|
|
tiny.max_payload_bytes = 4;
|
|
auto r_big = decode_envelope(*encoded, tiny);
|
|
Void::Testing::expect(!r_big.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_big.error()) ==
|
|
RpcStatusCode::ResourceExhausted);
|
|
|
|
// 7. Minor > 0 optional trailing header extension is length-skippable!
|
|
auto minor_ext = *encoded;
|
|
// Bump minor = 1, header_len += 4, total_len += 4, insert 4 extension bytes
|
|
// right before payload.
|
|
const std::size_t orig_hdr_len = encoded->size() - env.payload.size();
|
|
minor_ext[7] = std::byte{1};
|
|
const std::uint32_t new_hdr_len = static_cast<std::uint32_t>(orig_hdr_len + 4U);
|
|
const std::uint32_t new_tot_len = static_cast<std::uint32_t>(encoded->size() + 4U);
|
|
minor_ext[8] = static_cast<std::byte>((new_hdr_len >> 24U) & 0xFFU);
|
|
minor_ext[9] = static_cast<std::byte>((new_hdr_len >> 16U) & 0xFFU);
|
|
minor_ext[10] = static_cast<std::byte>((new_hdr_len >> 8U) & 0xFFU);
|
|
minor_ext[11] = static_cast<std::byte>(new_hdr_len & 0xFFU);
|
|
minor_ext[12] = static_cast<std::byte>((new_tot_len >> 24U) & 0xFFU);
|
|
minor_ext[13] = static_cast<std::byte>((new_tot_len >> 16U) & 0xFFU);
|
|
minor_ext[14] = static_cast<std::byte>((new_tot_len >> 8U) & 0xFFU);
|
|
minor_ext[15] = static_cast<std::byte>(new_tot_len & 0xFFU);
|
|
const std::array<std::byte, 4> ext_bytes{
|
|
std::byte{0xAA}, std::byte{0xBB}, std::byte{0xCC}, std::byte{0xDD}};
|
|
minor_ext.insert(
|
|
minor_ext.begin() + static_cast<std::ptrdiff_t>(orig_hdr_len),
|
|
ext_bytes.begin(), ext_bytes.end());
|
|
auto dec_minor = decode_envelope(minor_ext);
|
|
Void::Testing::require(dec_minor.has_value());
|
|
Void::Testing::expect_eq(dec_minor->minor, std::uint16_t{1});
|
|
Void::Testing::expect_eq(from_bytes(dec_minor->payload),
|
|
std::string{"hello-vrpc"});
|
|
}
|
|
|
|
Void::Testing::Test rpc_unary_http_dispatch_auth_schema_clamp_and_unregister_drain()
|
|
{
|
|
using namespace Void::RPC;
|
|
|
|
static constexpr MethodId kEcho{100, 1};
|
|
static constexpr MethodId kThrow{100, 2};
|
|
static constexpr MethodId kSlow{100, 3};
|
|
|
|
ServerOptions sopts;
|
|
sopts.limits.max_server_timeout = std::chrono::seconds{5};
|
|
sopts.auth.authenticate =
|
|
[](const std::vector<Void::Net::Header>& hdrs,
|
|
const std::vector<MetadataEntry>&)
|
|
-> Void::Result::Result<PrincipalView> {
|
|
for (const auto& h : hdrs) {
|
|
if (h.name == "X-Test-Token" && h.value == "valid-admin") {
|
|
return PrincipalView{
|
|
.authenticated = true,
|
|
.subject = "alice",
|
|
.roles = {"admin"},
|
|
};
|
|
}
|
|
if (h.name == "X-Test-Token" && h.value == "valid-guest") {
|
|
return PrincipalView{
|
|
.authenticated = true,
|
|
.subject = "bob",
|
|
.roles = {"guest"},
|
|
};
|
|
}
|
|
}
|
|
return std::unexpected{
|
|
make_rpc_error(RpcStatusCode::Unauthenticated, "missing token")};
|
|
};
|
|
sopts.auth.authorize = [](const MethodDescriptor& m,
|
|
const PrincipalView& p)
|
|
-> Void::Result::Result<void> {
|
|
if (m.id == kThrow &&
|
|
std::find(p.roles.begin(), p.roles.end(), "admin") ==
|
|
p.roles.end()) {
|
|
return std::unexpected{make_rpc_error(
|
|
RpcStatusCode::PermissionDenied, "admin role required")};
|
|
}
|
|
return {};
|
|
};
|
|
|
|
Server server{std::move(sopts)};
|
|
|
|
std::atomic<long long> observed_timeout_ms{0};
|
|
auto reg_echo = server.bind(
|
|
MethodDescriptor{kEcho, "demo", "echo", 2, true},
|
|
[&](const CallContext& ctx, OwnedBytes body)
|
|
-> Void::Result::Result<OwnedBytes> {
|
|
observed_timeout_ms.store(ctx.remaining_timeout().count(),
|
|
std::memory_order_release);
|
|
return to_bytes(ctx.principal.subject + ":" + from_bytes(body));
|
|
});
|
|
Void::Testing::require(reg_echo.has_value());
|
|
|
|
auto reg_throw = server.bind(
|
|
MethodDescriptor{kThrow, "demo", "throw", 1, false},
|
|
[](const CallContext&, OwnedBytes) -> Void::Result::Result<OwnedBytes> {
|
|
throw std::runtime_error("sensitive-internal-secret");
|
|
});
|
|
Void::Testing::require(reg_throw.has_value());
|
|
|
|
std::atomic<bool> slow_entered{false};
|
|
std::atomic<bool> slow_release{false};
|
|
std::atomic<bool> slow_finished{false};
|
|
auto reg_slow = server.bind(
|
|
MethodDescriptor{kSlow, "demo", "slow", 1, false},
|
|
[&](const CallContext&, OwnedBytes body)
|
|
-> Void::Result::Result<OwnedBytes> {
|
|
slow_entered.store(true, std::memory_order_release);
|
|
while (!slow_release.load(std::memory_order_acquire)) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
}
|
|
slow_finished.store(true, std::memory_order_release);
|
|
return body;
|
|
});
|
|
Void::Testing::require(reg_slow.has_value());
|
|
|
|
Void::Testing::require(server.start().has_value());
|
|
|
|
ClientOptions copts;
|
|
copts.endpoint_base_url = server.base_url();
|
|
Client client{std::move(copts)};
|
|
|
|
// 1. Authorized unary call succeeds and clamps excessive caller timeout
|
|
// (60s -> <= 5s server max).
|
|
CallOptions ok_opts;
|
|
ok_opts.schema_version = 2;
|
|
ok_opts.timeout = std::chrono::seconds{60};
|
|
ok_opts.transport_headers = {{"X-Test-Token", "valid-admin"}};
|
|
auto r_ok = client.call(kEcho, to_bytes("ping"), ok_opts);
|
|
Void::Testing::require(r_ok.has_value());
|
|
Void::Testing::expect_eq(from_bytes(*r_ok), std::string{"alice:ping"});
|
|
Void::Testing::expect(observed_timeout_ms.load() <= 5000);
|
|
Void::Testing::expect(observed_timeout_ms.load() > 0);
|
|
|
|
// 2. Schema version mismatch rejected with IncompatibleVersion.
|
|
CallOptions bad_schema = ok_opts;
|
|
bad_schema.schema_version = 99;
|
|
auto r_ver = client.call(kEcho, to_bytes("ping"), bad_schema);
|
|
Void::Testing::expect(!r_ver.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_ver.error()) ==
|
|
RpcStatusCode::IncompatibleVersion);
|
|
|
|
// 3. Unauthenticated request denied before handler admission.
|
|
CallOptions no_auth;
|
|
no_auth.schema_version = 2;
|
|
auto r_unauth = client.call(kEcho, to_bytes("x"), no_auth);
|
|
Void::Testing::expect(!r_unauth.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_unauth.error()) ==
|
|
RpcStatusCode::Unauthenticated);
|
|
|
|
// 4. Unauthorized guest denied on admin-only method.
|
|
CallOptions guest_opts;
|
|
guest_opts.schema_version = 1;
|
|
guest_opts.transport_headers = {{"X-Test-Token", "valid-guest"}};
|
|
auto r_perm = client.call(kThrow, to_bytes("x"), guest_opts);
|
|
Void::Testing::expect(!r_perm.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_perm.error()) ==
|
|
RpcStatusCode::PermissionDenied);
|
|
|
|
// 5. Handler exception returns sanitized Internal error without leaking
|
|
// exception message.
|
|
CallOptions admin_v1;
|
|
admin_v1.schema_version = 1;
|
|
admin_v1.transport_headers = {{"X-Test-Token", "valid-admin"}};
|
|
auto r_throw = client.call(kThrow, to_bytes("x"), admin_v1);
|
|
Void::Testing::expect(!r_throw.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(r_throw.error()) ==
|
|
RpcStatusCode::Internal);
|
|
Void::Testing::expect(r_throw.error().message().find("sensitive") ==
|
|
std::string_view::npos);
|
|
|
|
// 6. Registration::unregister_and_drain() waits for in-flight call to
|
|
// finish before returning, then rejects subsequent calls.
|
|
Void::Result::Result<OwnedBytes> slow_out =
|
|
std::unexpected{make_rpc_error(RpcStatusCode::Internal, "pending")};
|
|
std::thread slow_caller(
|
|
[&]() { slow_out = client.call(kSlow, to_bytes("drain-ok"), admin_v1); });
|
|
|
|
while (!slow_entered.load(std::memory_order_acquire)) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
}
|
|
std::thread releaser([&]() {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{25});
|
|
slow_release.store(true, std::memory_order_release);
|
|
});
|
|
reg_slow->unregister_and_drain();
|
|
Void::Testing::expect(slow_finished.load(std::memory_order_acquire));
|
|
releaser.join();
|
|
slow_caller.join();
|
|
Void::Testing::require(slow_out.has_value());
|
|
Void::Testing::expect_eq(from_bytes(*slow_out), std::string{"drain-ok"});
|
|
|
|
auto after_unreg = client.call(kSlow, to_bytes("late"), admin_v1);
|
|
Void::Testing::expect(!after_unreg.has_value());
|
|
Void::Testing::expect(rpc_status_from_error(after_unreg.error()) ==
|
|
RpcStatusCode::Unimplemented);
|
|
|
|
server.shutdown();
|
|
}
|