Files
aevolution/tests/quic_transport.cpp
T
dingoandClaude Opus 5.5 ca762bff75
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m47s
Foundation / checks (network) (push) Successful in 8m11s
Foundation / checks (client) (push) Successful in 12m20s
Foundation / checks (tsan) (push) Successful in 7m44s
Foundation / checks (checks) (push) Successful in 31m12s
Foundation / checks (postgres) (push) Successful in 33m27s
Foundation / image (push) Successful in 2m14s
Foundation / postgres-tools (push) Successful in 6m24s
Foundation / server-image (push) Successful in 18m6s
Add M10.2b bounded QUIC channels with exact CA pinning
evo_network_quic wraps msquic in a single-owner Endpoint: newest-wins datagram
channel and one length-framed reliable stream, both bounded, with explicit close
causes. The client pins exactly one CA and name with an owned Mbed TLS check that
is the sole authority (OpenSSL's verdict advisory), callbacks never call msquic
under the lock, and MinimumMtu starts at 1280 so 1,200-byte datagrams fit minimum
paths. An owned seeded UDP impairment relay drives loss, duplication, reordering,
corruption, MTU and outage tests, passing on MSVC, GCC and Clang ASan/UBSan.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 16:11:59 -04:00

576 lines
23 KiB
C++

// M10.2b QUIC transport tests (ADR-0090): framing, bounded channels, exact CA pinning,
// and behaviour through the owned impairment relay (loss, duplication, reordering,
// jitter, MTU, corruption, outage), plus malformed peers and limits.
#include "evo/network/quic_transport.hpp"
#include "evo/network/dev_credentials.hpp"
#include "udp_relay.hpp"
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <functional>
#include <set>
#include <source_location>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#endif
namespace {
using namespace evo::network;
using namespace evo::network::quic;
using Clock = std::chrono::steady_clock;
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("quic transport line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr std::string_view name = "dev-region.evolution.test";
struct Files {
std::filesystem::path dir;
std::string ca_pem;
std::filesystem::path ca, certificate, key;
};
Files credentials(const std::string& tag) {
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, name, 1,
std::chrono::system_clock::now());
CHECK(made.has_value());
Files f;
f.dir = std::filesystem::temp_directory_path() / ("evo-quic-transport-" + tag);
std::filesystem::remove_all(f.dir);
std::filesystem::create_directories(f.dir);
f.ca_pem = made->authority_pem;
f.ca = f.dir / "ca.pem";
f.certificate = f.dir / "server.pem";
f.key = f.dir / "server.key";
for (const auto& [path, text] : {std::pair{f.ca, made->authority_pem},
std::pair{f.certificate, made->server_certificate_pem},
std::pair{f.key, made->server_key_pem}}) {
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << text;
CHECK(out.good());
}
return f;
}
void set_env(const char* key, const std::string& value) {
#ifdef _WIN32
CHECK(_putenv_s(key, value.c_str()) == 0);
#else
CHECK(value.empty() ? ::unsetenv(key) == 0 : ::setenv(key, value.c_str(), 1) == 0);
#endif
}
struct Pair {
std::unique_ptr<Endpoint> server, client;
std::vector<Event> server_events, client_events;
ConnectionId server_id{}, client_id{};
void pump() {
for (auto& e : server->poll())
server_events.push_back(std::move(e));
for (auto& e : client->poll())
client_events.push_back(std::move(e));
}
bool until(const std::function<bool()>& done,
std::chrono::milliseconds limit = std::chrono::milliseconds{10000}) {
const auto end = Clock::now() + limit;
while (Clock::now() < end) {
pump();
if (done())
return true;
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
pump();
return done();
}
static const Event* find(const std::vector<Event>& events, EventKind kind) {
const auto it = std::find_if(events.begin(), events.end(),
[&](const Event& e) { return e.kind == kind; });
return it == events.end() ? nullptr : &*it;
}
bool connected() {
const bool ok = until([&] {
return find(server_events, EventKind::connected) &&
find(client_events, EventKind::connected);
});
if (ok) {
server_id = find(server_events, EventKind::connected)->connection;
client_id = find(client_events, EventKind::connected)->connection;
}
return ok;
}
std::vector<std::vector<std::uint8_t>> payloads(bool at_server, EventKind kind) const {
std::vector<std::vector<std::uint8_t>> out;
for (const auto& e : at_server ? server_events : client_events)
if (e.kind == kind)
out.push_back(e.bytes);
return out;
}
};
std::unique_ptr<Endpoint> serve(const Files& f, Limits limits = {}) {
ServerConfig c;
c.certificate_pem_file = f.certificate.string();
c.key_pem_file = f.key.string();
c.limits = limits;
auto made = Endpoint::serve(c);
CHECK(made.has_value());
return std::move(*made);
}
std::unique_ptr<Endpoint> dial(const Files& trusted, std::uint16_t port, Limits limits = {},
std::string server = std::string{name}) {
ClientConfig c;
c.port = port;
c.server_name = std::move(server);
c.authority_pem = trusted.ca_pem;
c.authority_pem_file = trusted.ca.string();
c.limits = limits;
auto made = Endpoint::connect(c);
CHECK(made.has_value());
return std::move(*made);
}
std::vector<std::uint8_t> numbered(std::uint32_t n, std::size_t size) {
std::vector<std::uint8_t> v(std::max<std::size_t>(size, 4), static_cast<std::uint8_t>(n * 7U));
v[0] = static_cast<std::uint8_t>(n);
v[1] = static_cast<std::uint8_t>(n >> 8U);
v[2] = static_cast<std::uint8_t>(n >> 16U);
v[3] = static_cast<std::uint8_t>(n >> 24U);
return v;
}
std::uint32_t number(const std::vector<std::uint8_t>& v) {
return static_cast<std::uint32_t>(v[0]) | (static_cast<std::uint32_t>(v[1]) << 8U) |
(static_cast<std::uint32_t>(v[2]) << 16U) | (static_cast<std::uint32_t>(v[3]) << 24U);
}
void framing() {
std::vector<std::vector<std::uint8_t>> out;
MessageReassembler r{1024};
std::vector<std::uint8_t> stream;
std::uint64_t state = 42;
const auto next = [&] {
state = state * 6364136223846793005ULL + 1442695040888963407ULL;
return static_cast<std::uint32_t>(state >> 33U);
};
std::vector<std::vector<std::uint8_t>> sent;
for (int i = 0; i < 2000; ++i) {
std::vector<std::uint8_t> m(next() % 1025, static_cast<std::uint8_t>(i));
const auto framed = frame_message(m);
stream.insert(stream.end(), framed.begin(), framed.end());
sent.push_back(std::move(m));
}
std::size_t at = 0;
while (at < stream.size()) { // Random chunking, including 1-byte pieces.
const std::size_t n = std::min<std::size_t>(stream.size() - at, 1 + next() % 3000);
CHECK(r.feed({stream.data() + at, n}, out));
at += n;
}
CHECK(out == sent && r.buffered() == 0);
MessageReassembler small{16};
std::vector<std::vector<std::uint8_t>> none;
const auto oversized = frame_message(std::vector<std::uint8_t>(17, 1));
CHECK(!small.feed(oversized, none) && none.empty()); // The stream is unusable afterwards.
MessageReassembler fresh{16};
CHECK(fresh.feed(frame_message({}), none) && none.size() == 1 &&
none[0].empty()); // Empty messages are valid.
}
void channels() {
const auto f = credentials("channels");
Pair p;
Limits server_limits;
server_limits.reliable_message = 1024;
p.server = serve(f, server_limits);
Limits client_limits;
client_limits.outbound_reliable_bytes = std::size_t{8} * 1024;
p.client = dial(f, p.server->port(), client_limits);
CHECK(p.connected());
// Reliable: 100 ordered messages each way.
for (std::uint32_t i = 0; i < 100; ++i) {
CHECK(p.client->send_message(p.client_id, numbered(i, 32)).has_value() || p.until([&] {
return p.client->send_message(p.client_id, numbered(i, 32)).has_value();
}));
CHECK(p.server->send_message(p.server_id, numbered(i, 32)).has_value());
}
CHECK(p.until([&] {
return p.payloads(true, EventKind::message).size() == 100 &&
p.payloads(false, EventKind::message).size() == 100;
}));
for (std::uint32_t i = 0; i < 100; ++i) {
CHECK(number(p.payloads(true, EventKind::message)[i]) == i);
CHECK(number(p.payloads(false, EventKind::message)[i]) == i);
}
// Realtime: datagrams both ways; size limits enforced locally.
CHECK(p.until([&] {
return p.client->datagram_limit(p.client_id) > 0 &&
p.server->datagram_limit(p.server_id) > 0;
}));
CHECK(p.client->datagram_limit(p.client_id) <= 1200);
CHECK(p.client->send_datagram(p.client_id, numbered(7, 100)).has_value());
CHECK(p.server->send_datagram(p.server_id, numbered(8, 100)).has_value());
CHECK(p.until([&] {
return !p.payloads(true, EventKind::datagram).empty() &&
!p.payloads(false, EventKind::datagram).empty();
}));
CHECK(p.client->send_datagram(p.client_id, std::vector<std::uint8_t>(1201, 1)).error() ==
SendError::too_large);
CHECK(
p.client
->send_message(p.client_id, std::vector<std::uint8_t>((std::size_t{64} * 1024) + 1, 1))
.error() == SendError::too_large);
CHECK(p.client->send_message(p.client_id + 99, numbered(1, 4)).error() ==
SendError::unknown_connection);
// Outbound reliable budget (8 KiB): a burst is refused rather than buffered without bound.
bool busy = false;
for (int i = 0; i < 64 && !busy; ++i)
busy = p.client->send_message(p.client_id, std::vector<std::uint8_t>(1000, 2))
.error_or(SendError::transport) == SendError::busy;
CHECK(busy && p.client->counters().messages_refused_outbound > 0);
// The server's 1 KiB message limit: a larger peer message is a protocol violation.
CHECK(p.until([&] {
return p.client->send_message(p.client_id, std::vector<std::uint8_t>(2000, 3)).has_value();
}));
CHECK(p.until([&] {
const auto* closed = Pair::find(p.server_events, EventKind::closed);
return closed != nullptr && closed->cause == CloseCause::protocol;
}));
CHECK(p.until([&] { return Pair::find(p.client_events, EventKind::closed) != nullptr; }));
std::filesystem::remove_all(f.dir);
}
void pinning() {
const auto good = credentials("pin-good");
const auto attacker = credentials("pin-attacker"); // Same name, another CA.
// OpenSSL would also trust SSL_CERT_FILE; the owned pin must not.
set_env("SSL_CERT_FILE", attacker.ca.string());
{
Pair p;
p.server = serve(attacker);
p.client = dial(good, p.server->port());
CHECK(p.until([&] {
const auto* closed = Pair::find(p.client_events, EventKind::closed);
return closed != nullptr && closed->cause == CloseCause::untrusted;
}));
CHECK(Pair::find(p.client_events, EventKind::connected) == nullptr);
CHECK(Pair::find(p.server_events, EventKind::connected) == nullptr);
}
{
// The genuine server still connects with the same environment.
Pair p;
p.server = serve(good);
p.client = dial(good, p.server->port());
const bool ok = p.connected();
if (!ok)
if (const auto* c = Pair::find(p.client_events, EventKind::closed))
std::fprintf(stderr,
"genuine server with SSL_CERT_FILE set: client closed, cause %d\n",
static_cast<int>(c->cause));
CHECK(ok);
}
set_env("SSL_CERT_FILE", "");
{
Pair p;
p.server = serve(good);
p.client = dial(good, p.server->port(), {}, "wrong.evolution.test");
CHECK(p.until([&] {
const auto* closed = Pair::find(p.client_events, EventKind::closed);
return closed != nullptr && closed->cause == CloseCause::untrusted;
}));
}
std::filesystem::remove_all(good.dir);
std::filesystem::remove_all(attacker.dir);
}
void impaired() {
const auto f = credentials("impaired");
impair::Impairment rough;
rough.loss = 0.10;
rough.duplicate = 0.10;
rough.reorder = 0.20;
rough.corrupt = 0.05;
rough.delay = std::chrono::milliseconds{5};
rough.jitter = std::chrono::milliseconds{10};
Pair p;
p.server = serve(f);
const auto relay = impair::UdpRelay::start(p.server->port(), rough, rough, 0xE70);
CHECK(relay != nullptr);
p.client = dial(f, relay->port());
CHECK(p.connected());
constexpr std::uint32_t messages = 200, datagrams = 300;
std::uint32_t sent = 0;
for (std::uint32_t i = 0; i < datagrams; ++i) {
if (!p.client->send_datagram(
p.client_id,
numbered(i, 200))) { /* Busy or too large: dropped like network loss. */
} // Busy/too large: dropped like loss.
if (sent < messages && p.client->send_message(p.client_id, numbered(sent, 300)).has_value())
++sent;
p.pump();
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
CHECK(p.until(
[&] {
while (sent < messages &&
p.client->send_message(p.client_id, numbered(sent, 300)).has_value())
++sent;
return p.payloads(true, EventKind::message).size() == messages;
},
std::chrono::milliseconds{30000}));
const auto got = p.payloads(true, EventKind::message);
for (std::uint32_t i = 0; i < messages; ++i)
CHECK(number(got[i]) == i); // Exactly once, in order, despite loss/duplication/reordering.
std::set<std::uint32_t> seen;
for (const auto& d : p.payloads(true, EventKind::datagram))
CHECK(seen.insert(number(d)).second); // Never duplicated to the application.
CHECK(!seen.empty() && seen.size() < datagrams); // Some lost, none invented.
const auto counters = relay->counters();
CHECK(counters.dropped > 0 && counters.duplicated > 0 && counters.reordered > 0 &&
counters.corrupted > 0);
std::printf("quic impaired: %zu/%u datagrams, %u/%u messages; relay forwarded=%llu "
"dropped=%llu dup=%llu "
"reordered=%llu corrupted=%llu\n",
seen.size(), datagrams, messages, messages,
static_cast<unsigned long long>(counters.forwarded),
static_cast<unsigned long long>(counters.dropped),
static_cast<unsigned long long>(counters.duplicated),
static_cast<unsigned long long>(counters.reordered),
static_cast<unsigned long long>(counters.corrupted));
std::filesystem::remove_all(f.dir);
}
void mtu() {
const auto f = credentials("mtu");
impair::Impairment narrow;
narrow.max_payload = 1252; // An IPv6 minimum path (1280) less IP/UDP headers.
Pair p;
p.server = serve(f);
const auto relay = impair::UdpRelay::start(p.server->port(), narrow, narrow, 7);
CHECK(relay != nullptr);
p.client = dial(f, relay->port());
CHECK(p.connected());
CHECK(p.until([&] { return p.client->datagram_limit(p.client_id) > 0; }));
// A full 1,200-byte budget datagram crosses a minimum path both ways and keeps
// crossing it (path discovery may probe larger packets; the path drops only those).
CHECK(p.connected());
const auto limit = p.client->datagram_limit(p.client_id);
CHECK(limit == 1200);
CHECK(p.client->send_datagram(p.client_id, std::vector<std::uint8_t>(limit + 1, 1)).error() ==
SendError::too_large);
for (int i = 0; i < 20; ++i) {
if (!p.client->send_datagram(
p.client_id,
std::vector<std::uint8_t>(
1200, static_cast<std::uint8_t>(
i)))) { /* Busy or too large: dropped like network loss. */
}
if (!p.server->send_datagram(
p.server_id,
std::vector<std::uint8_t>(
1200, static_cast<std::uint8_t>(
i)))) { /* Busy or too large: dropped like network loss. */
}
CHECK(p.client->send_message(p.client_id, numbered(static_cast<std::uint32_t>(i), 1100))
.has_value());
p.pump();
std::this_thread::sleep_for(std::chrono::milliseconds{50});
}
CHECK(p.until([&] {
return p.payloads(true, EventKind::message).size() == 20 &&
!p.payloads(true, EventKind::datagram).empty() &&
!p.payloads(false, EventKind::datagram).empty();
}));
CHECK(p.payloads(true, EventKind::datagram)[0].size() == 1200);
CHECK(Pair::find(p.client_events, EventKind::closed) == nullptr);
std::printf("quic mtu: 1200-byte datagrams over a 1252-byte UDP path (%llu larger probe "
"packets dropped)\n",
static_cast<unsigned long long>(relay->counters().oversized));
std::filesystem::remove_all(f.dir);
}
void outage() {
const auto f = credentials("outage");
Limits fast;
fast.idle_timeout = std::chrono::milliseconds{1000};
fast.keep_alive = std::chrono::milliseconds{300};
Pair p;
p.server = serve(f, fast);
const auto relay = impair::UdpRelay::start(p.server->port(), {}, {}, 9);
CHECK(relay != nullptr);
p.client = dial(f, relay->port(), fast);
CHECK(p.connected());
impair::Impairment dead;
dead.loss = 1.0;
relay->set(dead, dead);
const auto start = Clock::now();
CHECK(p.until(
[&] {
const auto* s = Pair::find(p.server_events, EventKind::closed);
const auto* c = Pair::find(p.client_events, EventKind::closed);
return s != nullptr && c != nullptr && s->cause == CloseCause::idle &&
c->cause == CloseCause::idle;
},
std::chrono::milliseconds{8000}));
std::printf(
"quic outage: both sides idle-closed after %lld ms\n",
static_cast<long long>(
std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - start).count()));
std::filesystem::remove_all(f.dir);
}
void bounds() {
const auto f = credentials("bounds");
Limits tight;
tight.inbound_datagrams = 4;
tight.inbound_reliable_bytes = 4096;
tight.max_connections = 1;
Pair p;
p.server = serve(f, tight);
p.client = dial(f, p.server->port());
CHECK(p.until([&] { return Pair::find(p.client_events, EventKind::connected) != nullptr; }));
p.client_id = Pair::find(p.client_events, EventKind::connected)->connection;
CHECK(p.until([&] {
for (auto& e : p.client->poll())
p.client_events.push_back(std::move(e));
return p.client->datagram_limit(p.client_id) > 0;
}));
// Datagrams pile up while the server does not poll: only the newest four survive.
for (std::uint32_t i = 0; i < 50; ++i) {
if (!p.client->send_datagram(
p.client_id, numbered(i, 64))) { /* Busy or too large: dropped like network loss. */
}
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
std::this_thread::sleep_for(std::chrono::milliseconds{200});
auto server_events = p.server->poll();
std::vector<std::uint32_t> kept;
for (const auto& e : server_events)
if (e.kind == EventKind::datagram)
kept.push_back(number(e.bytes));
p.server_events.insert(p.server_events.end(), server_events.begin(), server_events.end());
CHECK(!kept.empty() && kept.size() <= 4 && std::is_sorted(kept.begin(), kept.end()));
CHECK(p.server->counters().datagrams_dropped_inbound > 0);
const auto* accepted = Pair::find(p.server_events, EventKind::connected);
CHECK(accepted != nullptr);
p.server_id = accepted->connection;
// A second client exceeds max_connections and is refused.
auto extra = dial(f, p.server->port());
bool refused = false;
for (int i = 0; i < 5000 && !refused; ++i) {
for (const auto& e : extra->poll())
refused = refused || e.kind == EventKind::closed;
(void)p.server->poll(); // Keep the first connection serviced.
std::this_thread::sleep_for(std::chrono::milliseconds{1});
}
CHECK(refused && p.server->counters().connections_refused > 0);
// Reliable bytes beyond 4 KiB awaiting poll close the connection as overloaded.
for (int i = 0; i < 40; ++i)
if (!p.client->send_message(
p.client_id, std::vector<std::uint8_t>(
1000, 4))) { /* Busy or too large: dropped like network loss. */
}
const auto end = Clock::now() + std::chrono::seconds{10};
const Event* closed = nullptr;
std::vector<Event> late;
while (Clock::now() < end && closed == nullptr) {
std::this_thread::sleep_for(std::chrono::milliseconds{50}); // Server lags behind.
for (auto& e : p.server->poll())
late.push_back(std::move(e));
closed = Pair::find(late, EventKind::closed);
(void)p.client->poll();
}
CHECK(closed != nullptr && closed->cause == CloseCause::overloaded);
std::filesystem::remove_all(f.dir);
}
void garbage_and_close() {
const auto f = credentials("garbage");
Pair p;
p.server = serve(f);
// Random UDP datagrams at the listener: ignored, nothing surfaces.
#ifdef _WIN32
using Native = SOCKET;
#else
using Native = int;
#endif
const Native s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#ifdef _WIN32
if (s == INVALID_SOCKET)
throw std::runtime_error("quic transport: no UDP socket");
#else
if (s < 0)
throw std::runtime_error("quic transport: no UDP socket");
#endif
sockaddr_in to{};
to.sin_family = AF_INET;
to.sin_port = htons(p.server->port());
to.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
std::uint64_t state = 1;
for (std::size_t i = 0; i < 500; ++i) {
std::vector<char> junk(1 + (i * 37) % 1400);
for (auto& b : junk) {
state = state * 6364136223846793005ULL + 1;
b = static_cast<char>(state >> 56U);
}
#ifdef _WIN32
const auto length = static_cast<int>(junk.size());
#else
const auto length = junk.size();
#endif
(void)::sendto(s, junk.data(), length, 0, reinterpret_cast<const sockaddr*>(&to),
sizeof to);
}
#ifdef _WIN32
closesocket(s);
#else
::close(s);
#endif
std::this_thread::sleep_for(std::chrono::milliseconds{200});
CHECK(p.server->poll().empty());
// The listener still serves a genuine client; then a clean local close.
p.client = dial(f, p.server->port());
CHECK(p.connected());
p.client->close(p.client_id);
CHECK(p.until([&] {
const auto* c = Pair::find(p.client_events, EventKind::closed);
const auto* s2 = Pair::find(p.server_events, EventKind::closed);
return c != nullptr && s2 != nullptr && c->cause == CloseCause::local &&
s2->cause == CloseCause::peer;
}));
CHECK(p.client->send_message(p.client_id, numbered(1, 4)).error() ==
SendError::unknown_connection);
std::filesystem::remove_all(f.dir);
}
} // namespace
int main() try {
framing();
channels();
pinning();
impaired();
mtu();
outage();
bounds();
garbage_and_close();
std::puts("quic transport tests passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}