Files
aevolution/tests/quic_smoke.cpp
T
dingoandClaude Opus 5.5 3c421cd0cf
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m28s
Foundation / checks (network) (push) Successful in 8m16s
Foundation / checks (client) (push) Successful in 12m14s
Foundation / checks (tsan) (push) Successful in 7m50s
Foundation / checks (checks) (push) Successful in 31m45s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 25m13s
Select and pin the M10.2 QUIC transport (msquic with OpenSSL)
ADR-0090 records the maintainer's selection of msquic 2.6.2 with OpenSSL 3.5.9 LTS
built from pinned, checksum-verified sources on Windows and Linux (plus the
xdp-for-windows headers msquic needs on Windows), the trust, threading and
channel model, and the known gap that OpenSSL's verifier also trusts its default
paths. Adds the opt-in quic preset, explicit preparation, and a loopback smoke
test (datagram, stream, untrusted CA and wrong name refused) passing on MSVC and
GCC. windows-check picks up newly installed Perl/NASM and pins cl.

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

328 lines
14 KiB
C++

// M10.2a msquic integration smoke test (ADR-0090): loopback TLS 1.3 handshake with the
// dev CA pinned through the OpenSSL provider, one unreliable DATAGRAM and one reliable
// stream message delivered, and refusal of an untrusted CA and of a wrong server name.
// msquic calls back on its own threads; events are recorded under one mutex.
#include "evo/network/dev_credentials.hpp"
#include <array>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <functional>
#include <msquic.h>
#include <mutex>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
void check(bool ok, std::source_location at = std::source_location::current()) {
if (!ok)
throw std::runtime_error("quic smoke line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
const QUIC_API_TABLE* api = nullptr;
constexpr std::string_view server_name = "dev-region.evolution.test";
std::array<std::uint8_t, 7> alpn_bytes{'e', 'v', 'o', '-', 'm', '1', '0'};
const QUIC_BUFFER alpn{static_cast<std::uint32_t>(alpn_bytes.size()), alpn_bytes.data()};
struct Shared {
std::mutex mutex;
std::condition_variable changed;
bool client_connected{}, datagrams_enabled{}, client_closed{}, server_connected{};
QUIC_STATUS client_failure{QUIC_STATUS_SUCCESS};
std::string datagram, stream;
bool stream_finished{};
HQUIC server_config{};
void update(const std::function<void(Shared&)>& change) {
{
const std::scoped_lock lock{mutex};
change(*this);
}
changed.notify_all();
}
bool wait(const std::function<bool(const Shared&)>& done) {
std::unique_lock lock{mutex};
return changed.wait_for(lock, std::chrono::seconds{10}, [&] { return done(*this); });
}
};
QUIC_STATUS QUIC_API server_stream(HQUIC stream, void* context, QUIC_STREAM_EVENT* event) {
auto& shared = *static_cast<Shared*>(context);
switch (event->Type) {
case QUIC_STREAM_EVENT_RECEIVE:
shared.update([&](Shared& s) {
for (std::uint32_t i = 0; i < event->RECEIVE.BufferCount; ++i)
s.stream.append(reinterpret_cast<const char*>(event->RECEIVE.Buffers[i].Buffer),
event->RECEIVE.Buffers[i].Length);
});
break;
case QUIC_STREAM_EVENT_PEER_SEND_SHUTDOWN:
shared.update([](Shared& s) { s.stream_finished = true; });
break;
case QUIC_STREAM_EVENT_SHUTDOWN_COMPLETE:
api->StreamClose(stream);
break;
default:
break;
}
return QUIC_STATUS_SUCCESS;
}
QUIC_STATUS QUIC_API server_connection(HQUIC connection, void* context,
QUIC_CONNECTION_EVENT* event) {
auto& shared = *static_cast<Shared*>(context);
switch (event->Type) {
case QUIC_CONNECTION_EVENT_CONNECTED:
shared.update([](Shared& s) { s.server_connected = true; });
break;
case QUIC_CONNECTION_EVENT_DATAGRAM_RECEIVED: {
const auto* buffer = event->DATAGRAM_RECEIVED.Buffer;
shared.update([&](Shared& s) {
s.datagram.assign(reinterpret_cast<const char*>(buffer->Buffer), buffer->Length);
});
break;
}
case QUIC_CONNECTION_EVENT_PEER_STREAM_STARTED:
api->SetCallbackHandler(event->PEER_STREAM_STARTED.Stream,
reinterpret_cast<void*>(server_stream), context);
break;
case QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE:
api->ConnectionClose(connection);
break;
default:
break;
}
return QUIC_STATUS_SUCCESS;
}
QUIC_STATUS QUIC_API listener_callback(HQUIC /*listener*/, void* context,
QUIC_LISTENER_EVENT* event) {
if (event->Type != QUIC_LISTENER_EVENT_NEW_CONNECTION)
return QUIC_STATUS_SUCCESS;
auto& shared = *static_cast<Shared*>(context);
api->SetCallbackHandler(event->NEW_CONNECTION.Connection,
reinterpret_cast<void*>(server_connection), context);
return api->ConnectionSetConfiguration(event->NEW_CONNECTION.Connection, shared.server_config);
}
QUIC_STATUS QUIC_API client_stream(HQUIC stream, void* /*context*/, QUIC_STREAM_EVENT* event) {
if (event->Type == QUIC_STREAM_EVENT_SHUTDOWN_COMPLETE)
api->StreamClose(stream);
return QUIC_STATUS_SUCCESS;
}
QUIC_STATUS QUIC_API client_connection(HQUIC connection, void* context,
QUIC_CONNECTION_EVENT* event) {
auto& shared = *static_cast<Shared*>(context);
switch (event->Type) {
case QUIC_CONNECTION_EVENT_CONNECTED:
shared.update([](Shared& s) { s.client_connected = true; });
break;
case QUIC_CONNECTION_EVENT_DATAGRAM_STATE_CHANGED:
shared.update([&](Shared& s) {
s.datagrams_enabled = event->DATAGRAM_STATE_CHANGED.SendEnabled != 0;
});
break;
case QUIC_CONNECTION_EVENT_SHUTDOWN_INITIATED_BY_TRANSPORT:
shared.update(
[&](Shared& s) { s.client_failure = event->SHUTDOWN_INITIATED_BY_TRANSPORT.Status; });
break;
case QUIC_CONNECTION_EVENT_SHUTDOWN_COMPLETE:
shared.update([](Shared& s) { s.client_closed = true; });
api->ConnectionClose(connection);
break;
default:
break;
}
return QUIC_STATUS_SUCCESS;
}
QUIC_SETTINGS settings() {
QUIC_SETTINGS s{};
s.IdleTimeoutMs = 5000;
s.IsSet.IdleTimeoutMs = TRUE;
s.HandshakeIdleTimeoutMs = 5000;
s.IsSet.HandshakeIdleTimeoutMs = TRUE;
s.KeepAliveIntervalMs = 1000;
s.IsSet.KeepAliveIntervalMs = TRUE;
s.DatagramReceiveEnabled = TRUE;
s.IsSet.DatagramReceiveEnabled = TRUE;
s.PeerBidiStreamCount = 4;
s.IsSet.PeerBidiStreamCount = TRUE;
return s;
}
void write_file(const std::filesystem::path& path, const std::string& text) {
std::ofstream out{path, std::ios::binary | std::ios::trunc};
out << text;
CHECK(out.good());
}
struct Outcome {
bool connected{}, closed{};
QUIC_STATUS failure{QUIC_STATUS_SUCCESS};
};
// One client attempt against the listener; on success also sends a datagram and a stream.
Outcome attempt(HQUIC registration, Shared& shared, std::uint16_t port,
const std::filesystem::path& ca, std::string_view name, bool exchange) {
auto s = settings();
HQUIC config = nullptr;
CHECK(QUIC_SUCCEEDED(
api->ConfigurationOpen(registration, &alpn, 1, &s, sizeof(s), nullptr, &config)));
const auto ca_text = ca.string();
QUIC_CREDENTIAL_CONFIG credential{};
credential.Type = QUIC_CREDENTIAL_TYPE_NONE;
// msquic flag sets are enums whose valid values are bit combinations (the API's
// own operator| casts the same way).
// OpenSSL's own verifier on every platform (msquic forces it on Linux; on Windows the
// default platform verifier ignores CaCertificateFile). It also trusts OpenSSL's
// default verify paths; exact pinning is ADR-0090 / 10.2b work.
constexpr auto flags =
static_cast<std::uint32_t>(QUIC_CREDENTIAL_FLAG_CLIENT) |
static_cast<std::uint32_t>(QUIC_CREDENTIAL_FLAG_SET_CA_CERTIFICATE_FILE) |
static_cast<std::uint32_t>(QUIC_CREDENTIAL_FLAG_USE_TLS_BUILTIN_CERTIFICATE_VALIDATION);
credential.Flags = static_cast<QUIC_CREDENTIAL_FLAGS>(
flags); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
credential.CaCertificateFile = ca_text.c_str();
CHECK(QUIC_SUCCEEDED(api->ConfigurationLoadCredential(config, &credential)));
shared.update([](Shared& x) {
x.client_connected = x.datagrams_enabled = x.client_closed = x.server_connected =
x.stream_finished = false;
x.client_failure = QUIC_STATUS_SUCCESS;
x.datagram.clear();
x.stream.clear();
});
HQUIC connection = nullptr;
CHECK(
QUIC_SUCCEEDED(api->ConnectionOpen(registration, client_connection, &shared, &connection)));
QUIC_ADDR remote{};
CHECK(QuicAddrFromString("127.0.0.1", port, &remote));
CHECK(QUIC_SUCCEEDED(
api->SetParam(connection, QUIC_PARAM_CONN_REMOTE_ADDRESS, sizeof(remote), &remote)));
const std::string target{name};
CHECK(QUIC_SUCCEEDED(
api->ConnectionStart(connection, config, QUIC_ADDRESS_FAMILY_INET, target.c_str(), port)));
Outcome out;
// Connected, or refused and closed: whichever comes first decides.
out.connected =
shared.wait([](const Shared& x) { return x.client_connected || x.client_closed; }) && [&] {
const std::scoped_lock lock{shared.mutex};
return shared.client_connected;
}();
static std::array<std::uint8_t, 9> datagram{'e', 'v', 'o', '-', 'd', 'g', 'r', 'a', 'm'};
static std::array<std::uint8_t, 10> stream_bytes{'e', 'v', 'o', '-', 's',
't', 'r', 'e', 'a', 'm'};
static QUIC_BUFFER datagram_buffer{static_cast<std::uint32_t>(datagram.size()),
datagram.data()};
static QUIC_BUFFER stream_buffer{static_cast<std::uint32_t>(stream_bytes.size()),
stream_bytes.data()};
if (out.connected && exchange) {
CHECK(shared.wait([](const Shared& x) { return x.datagrams_enabled; }));
CHECK(QUIC_SUCCEEDED(
api->DatagramSend(connection, &datagram_buffer, 1, QUIC_SEND_FLAG_NONE, nullptr)));
HQUIC stream = nullptr;
CHECK(QUIC_SUCCEEDED(api->StreamOpen(connection, QUIC_STREAM_OPEN_FLAG_NONE, client_stream,
&shared, &stream)));
CHECK(QUIC_SUCCEEDED(api->StreamStart(stream, QUIC_STREAM_START_FLAG_NONE)));
CHECK(QUIC_SUCCEEDED(
api->StreamSend(stream, &stream_buffer, 1, QUIC_SEND_FLAG_FIN, nullptr)));
CHECK(shared.wait(
[](const Shared& x) { return x.stream_finished && x.datagram == "evo-dgram"; }));
CHECK(shared.wait(
[](const Shared& x) { return x.stream == "evo-stream" && x.server_connected; }));
}
if (out.connected)
api->ConnectionShutdown(connection, QUIC_CONNECTION_SHUTDOWN_FLAG_NONE,
0); // Graceful close.
out.closed = shared.wait([](const Shared& x) { return x.client_closed; });
{
const std::scoped_lock lock{shared.mutex};
out.failure = shared.client_failure;
}
api->ConfigurationClose(config);
return out;
}
void smoke() {
const auto now = std::chrono::system_clock::now();
const auto good = evo::network::generate_dev_credentials(
evo::network::DeploymentEnvironment::dev, server_name, 1, now);
const auto other = evo::network::generate_dev_credentials(
evo::network::DeploymentEnvironment::dev, server_name, 1, now);
CHECK(good && other);
const auto dir = std::filesystem::temp_directory_path() / "evo-quic-smoke";
std::filesystem::remove_all(dir);
std::filesystem::create_directories(dir);
write_file(dir / "ca.pem", good->authority_pem);
write_file(dir / "other-ca.pem", other->authority_pem);
write_file(dir / "server.pem", good->server_certificate_pem);
write_file(dir / "server.key", good->server_key_pem);
CHECK(QUIC_SUCCEEDED(MsQuicOpen2(&api)));
const QUIC_REGISTRATION_CONFIG registration_config{"evo-quic-smoke",
QUIC_EXECUTION_PROFILE_LOW_LATENCY};
HQUIC registration = nullptr;
CHECK(QUIC_SUCCEEDED(api->RegistrationOpen(&registration_config, &registration)));
Shared shared;
auto s = settings();
CHECK(QUIC_SUCCEEDED(api->ConfigurationOpen(registration, &alpn, 1, &s, sizeof(s), nullptr,
&shared.server_config)));
const auto key = (dir / "server.key").string();
const auto certificate = (dir / "server.pem").string();
QUIC_CERTIFICATE_FILE file{key.c_str(), certificate.c_str()};
QUIC_CREDENTIAL_CONFIG server_credential{};
server_credential.Type = QUIC_CREDENTIAL_TYPE_CERTIFICATE_FILE;
server_credential.CertificateFile = &file;
CHECK(
QUIC_SUCCEEDED(api->ConfigurationLoadCredential(shared.server_config, &server_credential)));
HQUIC listener = nullptr;
CHECK(QUIC_SUCCEEDED(api->ListenerOpen(registration, listener_callback, &shared, &listener)));
QUIC_ADDR local{};
CHECK(QuicAddrFromString("127.0.0.1", 0, &local));
CHECK(QUIC_SUCCEEDED(api->ListenerStart(listener, &alpn, 1, &local)));
std::uint32_t size = sizeof(local);
CHECK(
QUIC_SUCCEEDED(api->GetParam(listener, QUIC_PARAM_LISTENER_LOCAL_ADDRESS, &size, &local)));
const std::uint16_t port = QuicAddrGetPort(&local);
CHECK(port != 0);
const auto ok = attempt(registration, shared, port, dir / "ca.pem", server_name, true);
if (!ok.connected || !ok.closed)
std::fprintf(stderr, "quic: trusted attempt connected=%d closed=%d status=0x%lx\n",
ok.connected ? 1 : 0, ok.closed ? 1 : 0,
static_cast<unsigned long>(ok.failure));
CHECK(ok.connected && ok.closed);
std::printf("quic: handshake, datagram and stream delivered with the pinned dev CA\n");
const auto untrusted =
attempt(registration, shared, port, dir / "other-ca.pem", server_name, false);
CHECK(!untrusted.connected && untrusted.closed && QUIC_FAILED(untrusted.failure));
std::printf("quic: untrusted CA refused (status 0x%lx)\n",
static_cast<unsigned long>(untrusted.failure));
const auto wrong_name =
attempt(registration, shared, port, dir / "ca.pem", "wrong.evolution.test", false);
CHECK(!wrong_name.connected && wrong_name.closed && QUIC_FAILED(wrong_name.failure));
std::printf("quic: wrong server name refused (status 0x%lx)\n",
static_cast<unsigned long>(wrong_name.failure));
api->ListenerClose(listener);
api->ConfigurationClose(shared.server_config);
api->RegistrationClose(registration); // Waits for every connection and stream to close.
MsQuicClose(api);
std::filesystem::remove_all(dir);
}
} // namespace
int main() try {
smoke();
std::puts("quic smoke tests passed");
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
}