// M10.3b session login over QUIC (ADR-0091): grants verified by the region, newest grant // wins, replay and stale grants, scope, expiry, refresh, epochs, rate and capacity // limits, malformed and silent peers, client close and shutdown. The wall clock used for // grants is injected; deadlines use the real monotonic clock. #include "evo/client/region_session.hpp" #include "evo/network/dev_credentials.hpp" #include "evo/network/handoff_ticket.hpp" #include "evo/network/session_grant.hpp" #include "evo/server/probe_ledger.hpp" #include "evo/server/region_sessions.hpp" #include "fixtures/fake_handoff_world.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace evo; using namespace evo::network; using client::RegionSessionClient; using client::RegionSessionState; using server::RegionSessionEvent; using server::RegionSessions; using Clock = std::chrono::steady_clock; using Reason = SessionCloseReason; void check(bool ok, std::source_location at = std::source_location::current()) { if (!ok) throw std::runtime_error("quic sessions line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) template const T& must(const std::optional& o, std::source_location at = std::source_location::current()) { if (!o) throw std::runtime_error("quic sessions missing value line " + std::to_string(at.line())); return *o; } constexpr std::string_view name = "dev-region.evolution.test"; constexpr world::WorldIdentity world_id{7, 3}; constexpr std::uint64_t region = 11; constexpr std::uint64_t start = 1'800'000'000; struct Files { std::filesystem::path dir; std::string ca_pem; std::filesystem::path ca, certificate, key; }; Files credentials() { 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-sessions"; 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; } AccountId account(std::uint8_t tag) { AccountId a; a.bytes.fill(tag); return a; } // The issuer's dev key from a fixed scalar, so the same key can also sign as "stage". std::array scalar() { std::array s{}; s.back() = 0x2A; return s; } struct Issuer { GrantSigningKey key = *GrantSigningKey::import(DeploymentEnvironment::dev, 1, scalar()); WireGrant grant(const AccountId& who, std::uint64_t serial, std::uint64_t issued = start, std::uint64_t lifetime = 60, std::uint64_t in_region = region) const { GrantClaims c; c.grant_id = 500 + serial; c.account = who; c.serial = serial; c.world = world_id; c.region = in_region; c.issued_at = issued; c.expires_at = issued + lifetime; const auto g = key.sign(c); CHECK(g.has_value()); return *g; } }; struct Harness { explicit Harness(const Files& f, std::size_t max_sessions = 4, std::uint32_t region_attempts = 64, ControlLedger* ledger = nullptr, const std::function& tweak = {}) : files{f} { server::RegionSessionsConfig c; c.transport.certificate_pem_file = f.certificate.string(); c.transport.key_pem_file = f.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = region; c.control.epoch = 5; c.control.max_sessions = max_sessions; c.control.max_account_attempts = 16; c.control.max_region_attempts = region_attempts; c.issuer_keys.push_back({1, *issuer.key.public_key()}); c.hello_deadline = std::chrono::milliseconds{500}; c.close_flush = std::chrono::milliseconds{300}; c.ledger = ledger; c.renew_interval = std::chrono::milliseconds{100}; c.safety_margin = std::chrono::milliseconds{50}; if (tweak) tweak(c); auto made = RegionSessions::create(std::move(c)); CHECK(made.has_value()); server = std::move(*made); } std::unique_ptr connect(const WireGrant& grant, std::uint16_t session_version = session_protocol_version) { client::RegionSessionClientConfig c; c.transport.port = server->port(); c.transport.server_name = std::string{name}; c.transport.authority_pem = files.ca_pem; c.transport.authority_pem_file = files.ca.string(); c.grant = grant; c.protocol_version = session_version; auto made = RegionSessionClient::connect(std::move(c), Clock::now()); CHECK(made.has_value()); clients.push_back(made->get()); return std::move(*made); } std::unique_ptr raw() { quic::ClientConfig c; c.port = server->port(); c.server_name = std::string{name}; c.authority_pem = files.ca_pem; c.authority_pem_file = files.ca.string(); auto made = quic::Endpoint::connect(c); CHECK(made.has_value()); return std::move(*made); } void pump() { server->poll({ready, epoch, tick}, now_seconds, Clock::now()); if (source && tick != replicated) { // One snapshot per simulated tick (ADR-0093). replicated = tick; server->replicate(tick, source); } for (auto& e : server->take_events()) events.push_back(std::move(e)); for (auto* c : clients) c->poll(Clock::now()); } bool until(const std::function& 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(); } bool settled(RegionSessionClient& c) { return until([&] { return c.state() == RegionSessionState::established || c.state() == RegionSessionState::closed; }); } bool has_event(RegionSessionEvent::Kind kind, Reason reason, std::uint64_t session = 0) const { return std::ranges::any_of(events, [&](const RegionSessionEvent& e) { return e.kind == kind && (kind == RegionSessionEvent::Kind::opened || e.reason == reason) && (session == 0 || e.session == session); }); } void forget(const RegionSessionClient* c) { std::erase(clients, c); } const Files& files; Issuer issuer; std::unique_ptr server; std::vector clients; std::vector events; std::uint64_t epoch{5}, now_seconds{start}, tick{100}; bool ready{true}; std::function source; std::uint64_t replicated{}; }; bool refused_with(Harness& h, const WireGrant& grant, Reason reason) { auto c = h.connect(grant); const bool ok = h.settled(*c) && c->state() == RegionSessionState::closed && c->close_reason() == reason && !c->welcome(); h.forget(c.get()); return ok; } // A raw QUIC peer: sends `bytes` (or nothing) and returns the server's Close reason. std::optional raw_peer(Harness& h, const std::optional>& bytes) { auto peer = h.raw(); std::optional reason; quic::ConnectionId id = 0; const bool done = h.until([&] { for (auto& e : peer->poll()) { if (e.kind == quic::EventKind::connected) { id = e.connection; if (bytes) CHECK(peer->send_message(id, *bytes).has_value()); } else if (e.kind == quic::EventKind::message) { const auto m = decode_session(e.bytes); if (m && std::holds_alternative(*m)) reason = std::get(*m).reason; } } return reason.has_value(); }); CHECK(done); return reason; } void login_rules(const Files& f) { Harness h{f}; const auto alice = account(1), bob = account(2), carol = account(3); // Login and Welcome. auto a1 = h.connect(h.issuer.grant(alice, 1)); CHECK(h.settled(*a1) && a1->state() == RegionSessionState::established); const auto& w = must(a1->welcome()); CHECK(w.epoch == 5 && w.world == world_id.world && w.frame == world_id.frame && w.region == region && w.expires_at == start + 60 && w.server_seconds == start); CHECK(h.server->holder(alice) == w.session && h.server->sessions() == 1); CHECK(h.has_event(RegionSessionEvent::Kind::opened, {}, w.session)); CHECK(std::ranges::all_of(h.events, [&](const RegionSessionEvent& e) { return e.account.empty() || e.account == log_label(alice) || e.account == log_label(bob) || e.account == log_label(carol); })); // Replay of the same grant. CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::stale_grant)); CHECK(h.server->holder(alice) == w.session); // Duplicate login: the newer grant takes over; the old connection is told why. auto a2 = h.connect(h.issuer.grant(alice, 3)); CHECK(h.settled(*a2) && a2->state() == RegionSessionState::established); CHECK(h.until([&] { return a1->state() == RegionSessionState::closed; })); CHECK(a1->close_reason() == Reason::superseded); CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::superseded, w.session)); CHECK(h.server->holder(alice) == must(a2->welcome()).session && h.server->sessions() == 1); // A stolen older grant (serial 2, still unexpired) cannot take control back. CHECK(refused_with(h, h.issuer.grant(alice, 2), Reason::stale_grant)); CHECK(a2->state() == RegionSessionState::established); // Refresh in place: accepted, then stale, then another account's grant. CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30))); std::optional r; CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); CHECK(must(r).status == 0 && must(r).expires_at == start + 90 && must(a2->welcome()).expires_at == start + 90); CHECK(a2->refresh(h.issuer.grant(alice, 4, start + 30))); CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); CHECK(must(r).status == static_cast(Reason::stale_grant)); CHECK(a2->refresh(h.issuer.grant(bob, 9))); CHECK(h.until([&] { return (r = a2->take_refresh_result()).has_value(); })); CHECK(must(r).status == static_cast(Reason::unauthenticated)); CHECK(a2->state() == RegionSessionState::established && must(a2->welcome()).expires_at == start + 90); // Scope and authenticity. CHECK(refused_with(h, h.issuer.grant(bob, 1, start, 60, region + 1), Reason::wrong_scope)); const auto stage = *GrantSigningKey::import(DeploymentEnvironment::stage, 1, scalar()); GrantClaims staged; staged.grant_id = 1; staged.account = bob; staged.serial = 1; staged.world = world_id; staged.region = region; staged.issued_at = start; staged.expires_at = start + 60; CHECK(refused_with(h, *stage.sign(staged), Reason::wrong_scope)); // Stage grant, dev region. const auto stranger = *GrantSigningKey::generate(DeploymentEnvironment::dev, 1); CHECK(refused_with(h, *stranger.sign(staged), Reason::unauthenticated)); // Not our issuer. auto forged = h.issuer.grant(bob, 1); forged.at(40) ^= 1; CHECK(refused_with(h, forged, Reason::unauthenticated)); // Expiry at login and during a session (injected wall clock). CHECK(refused_with(h, h.issuer.grant(bob, 1, start - 120), Reason::expired)); auto b = h.connect(h.issuer.grant(bob, 2)); CHECK(h.settled(*b) && b->state() == RegionSessionState::established); h.now_seconds = start + 60; // Bob's grant ends; Alice refreshed until start + 90. CHECK(h.until([&] { return b->state() == RegionSessionState::closed; })); CHECK(b->close_reason() == Reason::expired && a2->state() == RegionSessionState::established); // Client close releases control. auto c = h.connect(h.issuer.grant(carol, 1, start + 60)); CHECK(h.settled(*c) && c->state() == RegionSessionState::established); const auto carol_session = must(c->welcome()).session; c->close(); CHECK(h.until([&] { return !h.server->holder(carol); })); CHECK(h.has_event(RegionSessionEvent::Kind::closed, Reason::client_closed, carol_session)); // Malformed and silent peers. CHECK(raw_peer(h, std::nullopt) == Reason::timeout); CHECK(raw_peer(h, std::vector{1, 2, 3}) == Reason::protocol); auto future = encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)}); future.at(4) = 6; CHECK(raw_peer(h, future) == Reason::unsupported_version); const auto unnegotiated_v5 = encode_session(SessionHello{0, h.issuer.grant(carol, 2, start + 60)}, session_protocol_v5); CHECK(raw_peer(h, unnegotiated_v5) == Reason::unsupported_version); CHECK(raw_peer(h, encode_session(SessionClose{Reason::client_closed})) == Reason::protocol); CHECK(raw_peer(h, encode_session(SessionHello{1, h.issuer.grant(carol, 3, start + 60)})) == Reason::protocol); // Unknown required feature. // A new ownership epoch ends every session. h.epoch = 6; CHECK(h.until([&] { return a2->state() == RegionSessionState::closed; })); CHECK(a2->close_reason() == Reason::stale_epoch && h.server->sessions() == 0); auto a3 = h.connect(h.issuer.grant(alice, 5, start + 60)); CHECK(h.settled(*a3) && must(a3->welcome()).epoch == 6); // Shutdown tells every client. h.server->shutdown(Clock::now()); CHECK(h.until([&] { return a3->state() == RegionSessionState::closed; })); CHECK(a3->close_reason() == Reason::shutdown); CHECK(h.until([&] { return h.server->connections() == 0; })); } void limits(const Files& f) { { Harness h{f, 1}; auto a = h.connect(h.issuer.grant(account(1), 1)); CHECK(h.settled(*a) && a->state() == RegionSessionState::established); CHECK(refused_with(h, h.issuer.grant(account(2), 1), Reason::capacity)); a->close(); CHECK(h.until([&] { return h.server->sessions() == 0; })); // The refused grant was not consumed. auto b = h.connect(h.issuer.grant(account(2), 1)); CHECK(h.settled(*b) && b->state() == RegionSessionState::established); } { Harness h{f, 4, 3}; for (std::uint64_t serial = 1; serial <= 3; ++serial) { auto c = h.connect(h.issuer.grant(account(1), serial)); CHECK(h.settled(*c) && c->state() == RegionSessionState::established); h.forget(c.get()); } CHECK(refused_with(h, h.issuer.grant(account(1), 4), Reason::rate_limited)); } } // Scripted durable control (10.3c): records calls, answers as configured. struct FakeLedger final : ControlLedger { struct Claim { AccountId account; std::uint64_t serial{}, session{}; }; std::expected claim(const AccountId& account, std::uint64_t serial, std::uint64_t session) override { if (claim_error) return std::unexpected(*claim_error); claims.push_back({account, serial, session}); return ControlLease{next_generation++, wait, granted}; } std::expected renew(const AccountId& /*account*/, std::uint64_t generation) override { renewed.push_back(generation); if (renew_error) return std::unexpected(*renew_error); return granted; } void release(const AccountId& /*account*/, std::uint64_t generation) override { released.push_back(generation); } std::optional claim_error, renew_error; std::chrono::milliseconds wait{}, granted{std::chrono::milliseconds{20000}}; std::uint64_t next_generation{1}; std::vector claims; std::vector renewed, released; }; void primitive_v5_session(const Files& f) { FakeLedger ledger; Harness h{f, 4, 64, &ledger, [](server::RegionSessionsConfig& c) { c.create_primitive_life = [](const AccountId& a, std::uint64_t generation, std::uint64_t request) { CHECK(a == account(0xA5) && generation == 1); return PrimitiveLifeOutcome{request, PrimitiveLifeResult::born, 77, 88}; }; c.query_primitive_life = [](const AccountId&, std::uint64_t, std::uint64_t request) { return PrimitiveLifeOutcome{request, PrimitiveLifeResult::unknown, 0, 0}; }; }}; auto client = h.connect(h.issuer.grant(account(0xA5), 1), session_protocol_v5); CHECK(h.settled(*client) && client->state() == RegionSessionState::established); CHECK(client->request_primitive_life(42)); std::vector receipts; std::vector outcomes; CHECK(h.until([&] { auto more_receipts = client->take_primitive_life_receipts(); auto more_outcomes = client->take_primitive_life_outcomes(); receipts.insert(receipts.end(), more_receipts.begin(), more_receipts.end()); outcomes.insert(outcomes.end(), more_outcomes.begin(), more_outcomes.end()); return !receipts.empty() && !outcomes.empty(); })); CHECK(receipts.front() == PrimitiveLifeReceipt{42, AdmitStatus::admitted, 100}); CHECK(outcomes.front() == PrimitiveLifeOutcome{42, PrimitiveLifeResult::born, 77, 88}); CHECK(client->query_primitive_life(42)); CHECK(h.until([&] { outcomes = client->take_primitive_life_outcomes(); return !outcomes.empty(); })); CHECK(outcomes.front() == PrimitiveLifeOutcome{42, PrimitiveLifeResult::unknown, 0, 0}); CHECK(!client->send_action(99, ActionKind::probe, 101)); CHECK(!client->query_action(99)); CHECK(!client->send_intention(101, 1, 0)); CHECK(!ledger.claims.empty() && ledger.claims.back().account == account(0xA5)); } void cellular_v6_session(const Files& f) { Harness h{f, 4, 64, nullptr, [](server::RegionSessionsConfig& c) { c.accept_cellular_intentions = true; }}; const auto alice = account(0xA6); auto client = h.connect(h.issuer.grant(alice, 1), session_protocol_v6); CHECK(h.settled(*client) && client->state() == RegionSessionState::established); CHECK(!client->set_interest(SessionInterest{})); // No M10 probe view in cellular v6. CHECK(client->view() == nullptr); h.server->set_default_interest({0, 0, 0, 1}); h.source = [](const SessionInterest&) { return ReplicationSource{.identity = world_id}; }; h.pump(); CHECK(client->view() == nullptr); // Default M10 snapshots are withheld too. CHECK(!client->send_intention(101, 0, 0)); CHECK(!client->send_action(1, ActionKind::probe, 101)); std::optional sequence; CHECK(h.until([&] { return (sequence = client->send_cellular_intention(101, 250, -250, std::pair{3'000u, 2'000u})) .has_value(); })); CHECK(sequence == 1); std::vector receipts; CHECK(h.until([&] { return !(receipts = client->take_intention_receipts()).empty(); })); CHECK(receipts.front() == IntentionReceipt{1, AdmitStatus::admitted, 100}); const auto pending = h.server->cellular_intentions(101); CHECK(pending.size() == 1); CHECK(pending.front().account == alice); CHECK(pending.front().intention == CellularIntention{must(client->welcome()).session, 5, 1, 101, 250, -250, true, 3'000, 2'000}); h.tick = 101; CHECK(h.until([&] { return client->simulated() && client->simulated()->sequence == 1; })); CHECK(h.server->cellular_intentions(101).empty()); Harness legacy{f}; auto refused = legacy.connect(legacy.issuer.grant(account(0xA7), 1), session_protocol_v6); CHECK(legacy.settled(*refused)); CHECK(refused->state() == RegionSessionState::closed); CHECK(refused->close_reason() == SessionCloseReason::unsupported_version); } // M10.4: intentions, actions and their distinct answers against a simulated host. void intentions_and_actions(const Files& f) { Harness h{f}; const auto alice = account(1); auto a = h.connect(h.issuer.grant(alice, 1)); CHECK(h.settled(*a) && must(a->welcome()).server_tick == 100 && a->server_tick() == 100); // Intention: a receipt, then the simulated watermark once its tick runs. // Datagrams may be refused until the QUIC datagram extension is negotiated: retry. std::optional first; CHECK(h.until([&] { return (first = a->send_intention(101, 10, -10)).has_value(); })); CHECK(first == 1u); std::vector receipts; CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); })); CHECK(receipts[0] == IntentionReceipt{1, AdmitStatus::admitted, 100}); CHECK(a->simulated().value_or(SimulatedThrough{}).sequence == 0); // Ticks only, so far. CHECK(a->send_intention(150, 0, 0) == 2u); CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); })); CHECK(receipts[0].status == AdmitStatus::outside_window); h.tick = 101; CHECK(h.until([&] { return a->simulated() && a->simulated()->sequence == 1; })); CHECK(must(a->simulated()) == SimulatedThrough{5, 1, 101} && a->server_tick() == 101); h.ready = false; CHECK(a->send_intention(103, 0, 0) == 3u); CHECK(h.until([&] { return !(receipts = a->take_intention_receipts()).empty(); })); CHECK(receipts[0].status == AdmitStatus::not_ready); h.ready = true; // Action: receipt (scheduled), carried with its tick's batch, final after the commit. CHECK(a->send_action(7, ActionKind::probe, 103)); std::vector action_receipts; CHECK(h.until([&] { return !(action_receipts = a->take_action_receipts()).empty(); })); CHECK(action_receipts[0] == ActionReceipt{7, AdmitStatus::admitted, 101, 103}); CHECK(a->query_action(7)); std::vector outcomes; CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); })); CHECK(outcomes[0].result == ActionResult::pending); CHECK(h.server->carry(102).empty()); const auto carried = h.server->carry(103); CHECK(carried.size() == 1 && carried[0] == CarriedAction{alice, 7, 103, ActionKind::probe}); h.server->committed(carried, 4); CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); })); CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4}); // A retry gets a duplicate receipt and the same final outcome. CHECK(a->send_action(7, ActionKind::probe, 104)); CHECK(h.until([&] { return !(outcomes = a->take_action_outcomes()).empty(); })); CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4}); // The stream is ordered: the duplicate receipt arrived before the repeated outcome. const auto duplicates = a->take_action_receipts(); CHECK(duplicates.size() == 1 && duplicates[0] == ActionReceipt{7, AdmitStatus::duplicate, 101, 103}); // Reconnect (lost connection, new grant): the account's action resolves the same way. auto b = h.connect(h.issuer.grant(alice, 2)); CHECK(h.settled(*b) && b->state() == RegionSessionState::established); CHECK(b->query_action(7)); CHECK(h.until([&] { return !(outcomes = b->take_action_outcomes()).empty(); })); CHECK(outcomes[0] == ActionOutcome{7, ActionResult::committed_no_effect, 103, 4}); // An action scheduled but never carried when the epoch moves on is final: not committed. CHECK(b->send_action(8, ActionKind::probe, 108)); CHECK(h.until([&] { return !b->take_action_receipts().empty(); })); h.epoch = 6; CHECK(h.until([&] { return b->state() == RegionSessionState::closed; })); auto c = h.connect(h.issuer.grant(alice, 3)); CHECK(h.settled(*c) && c->state() == RegionSessionState::established); CHECK(c->query_action(8)); CHECK(h.until([&] { return !(outcomes = c->take_action_outcomes()).empty(); })); CHECK(outcomes[0].result == ActionResult::not_committed); // Intentions of an old epoch are refused. CHECK(h.until([&] { return c->send_intention(h.tick + 1, 0, 0).has_value(); })); CHECK(h.until([&] { return !(receipts = c->take_intention_receipts()).empty(); })); CHECK(receipts[0].status == AdmitStatus::admitted); // c is in epoch 6. // A peer that appends state claims (position, energy) to an intention is closed. auto peer = h.raw(); quic::ConnectionId id = 0; std::optional welcome; std::optional reason; CHECK(h.until([&] { for (auto& e : peer->poll()) { if (e.kind == quic::EventKind::connected) { id = e.connection; CHECK(peer->send_message( id, encode_session(SessionHello{0, h.issuer.grant(account(4), 1)}))); } else if (e.kind == quic::EventKind::message) { const auto m = decode_session(e.bytes); if (m && std::holds_alternative(*m)) { welcome = std::get(*m); auto claim = encode_session( SessionIntention{welcome->session, welcome->epoch, 1, h.tick + 1, 0, 0}); for (int i = 0; i < 16; ++i) claim.push_back(0x7F); // "position" and "energy" CHECK(peer->send_datagram(id, claim)); } else if (m && std::holds_alternative(*m)) { reason = std::get(*m).reason; } } } return reason.has_value(); })); CHECK(welcome && reason == Reason::protocol); } // M10.5: snapshots over QUIC from a simulated host's 21 x 21 slice. client::EnvironmentCell grid_cell(std::int64_t x, std::int64_t y, std::uint64_t quantity) { client::EnvironmentCell c; c.voxel = {x, y, -7}; c.chunk_revision = 40; c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}}; c.volume = {1000ULL * 500 * 10}; c.substrate = {1}; c.terrain_material = {2}; c.terrain_fill = 1000; c.presence[0] = client::FieldPresence::present; c.quantity[0] = quantity; c.presence[1] = client::FieldPresence::missing; c.presence[7] = client::FieldPresence::present; c.quantity[7] = 5; c.moisture_status = world::MoistureStatus::available; c.moisture = {3, 50}; return c; } void snapshots_over_quic(const Files& f) { Harness h{f}; std::uint64_t quantity = 30; h.source = [&](const SessionInterest&) { ReplicationSource s; s.identity = world_id; s.field_revision = h.tick; s.mask = (1U << 0) | (1U << 1) | (1U << 7); // Water requested: moisture is present. for (std::int64_t x = -10; x <= 10; ++x) for (std::int64_t y = -10; y <= 10; ++y) s.environment.push_back(grid_cell(x, y, quantity)); return s; }; h.server->set_default_interest({0, 0, -7, 30}); // Clamped to radius 7. auto a = h.connect(h.issuer.grant(account(1), 1)); CHECK(h.settled(*a) && a->state() == RegionSessionState::established); const auto ticks_until = [&](const std::function& done) { for (int i = 0; i < 400 && !done(); ++i) { ++h.tick; h.until(done, std::chrono::milliseconds{20}); } return done(); }; // The default interest's 225 cells arrive within the per-tick budget. CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 225; })); const auto env = a->environment(); CHECK(env && env->cells().size() == 225 && env->revision().identity == world_id); CHECK(std::ranges::all_of(env->cells(), [](const auto& c) { return c.voxel.x >= -7 && c.voxel.x <= 7 && c.voxel.y >= -7 && c.voxel.y <= 7; })); CHECK(a->snapshot_chunks() > 0 && a->snapshot_bytes() > std::uint64_t{225} * 50); // A change reaches the client; acknowledgements keep deltas small. quantity = 31; CHECK(ticks_until([&] { return a->view() && std::ranges::all_of(a->view()->environment, [](const auto& c) { return c.quantity[0] == 31; }); })); const auto stats = h.server->replication_stats(); CHECK(stats.snapshots > 2 && stats.fresh_baselines < stats.snapshots); // Another interest: only the new square. CHECK(a->set_interest({5, 5, -7, 2})); CHECK(ticks_until([&] { return a->view() && a->view()->environment.size() == 25; })); CHECK(std::ranges::all_of(a->view()->environment, [](const auto& c) { return c.voxel.x >= 3 && c.voxel.x <= 7 && c.voxel.y >= 3 && c.voxel.y <= 7; })); // A snapshot ack datagram is accepted (not a protocol error); the session stays open. CHECK(a->state() == RegionSessionState::established); } // M10.8b: a Hello carrying a handoff ticket (ADR-0096) instead of a grant. struct FakeHandoff final : HandoffLedger { std::expected admit(const TicketClaims& ticket, std::uint64_t session) override { if (error) return std::unexpected(*error); admitted.emplace_back(ticket, session); return HandoffAdmission{ ControlLease{100 + admitted.size(), {}, std::chrono::milliseconds{20000}}, 2, 0, 0}; } std::optional error; std::vector> admitted; }; void ticket_login(const Files& f) { auto source = *HandoffSigningKey::generate(DeploymentEnvironment::dev, 41); const auto ticket = [&](const AccountId& who, std::uint8_t transfer, std::uint64_t destination = region) { TicketClaims c; c.transfer.fill(transfer); c.account = who; c.world = world_id; c.source = 4; c.destination = destination; c.expires_at = start + 30; c.session_expires_at = start + 90; const auto t = source.sign(c); CHECK(t.has_value()); return *t; }; const auto alice = account(1); // A region without handoff keys refuses every ticket. { Harness plain{f}; CHECK(refused_with(plain, ticket(alice, 0x61), Reason::unauthenticated)); } // A handoff ledger needs the control ledger (control renews through it). { FakeHandoff handoff; server::RegionSessionsConfig c; c.transport.certificate_pem_file = f.certificate.string(); c.transport.key_pem_file = f.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = region; c.control.epoch = 5; c.issuer_keys.push_back({1, *Issuer{}.key.public_key()}); c.handoff = &handoff; CHECK(!RegionSessions::create(std::move(c))); } FakeLedger ledger; FakeHandoff handoff; Harness h{f, 4, 64, &ledger, [&](server::RegionSessionsConfig& c) { c.handoff_keys.push_back({4, 41, *source.public_key()}); c.handoff = &handoff; }}; // Admitted: the durable side ran first, for the session Welcome then names; the // session ends at the ticket's session expiry. auto a = h.connect(ticket(alice, 0x62)); CHECK(h.settled(*a) && a->state() == RegionSessionState::established); const auto& w = must(a->welcome()); CHECK(handoff.admitted.size() == 1 && handoff.admitted[0].second == w.session && handoff.admitted[0].first.account == alice && w.expires_at == start + 90); CHECK(h.server->holder(alice) == w.session && ledger.claims.empty()); // Control renews through the control ledger under the handoff's generation. CHECK(h.until([&] { return !ledger.renewed.empty(); })); CHECK(ledger.renewed.front() == 101); // Replay, another destination, and a refused durable step are all refused. CHECK(refused_with(h, ticket(alice, 0x62), Reason::stale_grant)); CHECK(refused_with(h, ticket(alice, 0x63, 12), Reason::wrong_scope)); handoff.error = HandoffLedgerError::refused; CHECK(refused_with(h, ticket(account(2), 0x64), Reason::unauthenticated)); handoff.error = HandoffLedgerError::unavailable; CHECK(refused_with(h, ticket(account(3), 0x65), Reason::capacity)); CHECK(a->state() == RegionSessionState::established && handoff.admitted.size() == 1); // M10.9a (ADR-0097): under load, grant logins are refused before signature work while // tickets (crossings) are still admitted; the refused grant stays usable. handoff.error.reset(); h.server->set_load(true, 3); CHECK(refused_with(h, h.issuer.grant(account(5), 1), Reason::capacity)); CHECK(refused_with(h, h.issuer.grant(account(5), 1), Reason::capacity)); CHECK(h.server->refused_for_load() == 2 && a->state() == RegionSessionState::established); auto crossed = h.connect(ticket(account(6), 0x66)); CHECK(h.settled(*crossed) && crossed->state() == RegionSessionState::established); h.server->set_load(false, 0); auto later = h.connect(h.issuer.grant(account(5), 1)); CHECK(h.settled(*later) && later->state() == RegionSessionState::established && h.server->refused_for_load() == 2); } // M10.8c (ADR-0096): two regions with adjacent areas in one process, over the fake world // database of tests/fixtures/fake_handoff_world.hpp. using evo::test::RegionDb; using evo::test::WorldDb; constexpr std::int64_t m_um = 1'000'000; // One metre (one cell) in micrometres. // Region `id` owns x in [x0, x0 + 10 m), y in [0, 10 m) of slice 0. network::motion::Area area_of(std::int64_t x0) { return {x0, 0, x0 + (10 * m_um) - 1, (10 * m_um) - 1, 0}; } struct Region { Region(const Files& f, std::uint64_t id, std::int64_t x0, WorldDb& world, std::shared_ptr key, const GrantPublicKey& neighbour_key, std::uint64_t neighbour, std::int64_t neighbour_x0, std::uint16_t neighbour_port, std::uint16_t own_port = 0) : db{world, id} { server::RegionSessionsConfig c; c.transport.port = own_port; c.transport.certificate_pem_file = f.certificate.string(); c.transport.key_pem_file = f.key.string(); c.control.environment = DeploymentEnvironment::dev; c.control.world = world_id; c.control.region = id; c.control.epoch = 5; c.control.max_account_attempts = 16; c.issuer_keys.push_back({1, *Issuer{}.key.public_key()}); c.hello_deadline = std::chrono::milliseconds{2000}; c.close_flush = std::chrono::milliseconds{300}; c.ledger = &db; c.handoff = &db; c.probes = &db; c.handoff_key = std::move(key); c.handoff_keys.push_back( {neighbour, static_cast(neighbour + 40), neighbour_key}); c.neighbours.push_back( {{neighbour, "127.0.0.1:" + std::to_string(neighbour_port), std::string{name}}, area_of(neighbour_x0)}); c.ticket_lifetime = 20; c.ghost_poll = std::chrono::milliseconds{20}; c.overdue_poll = std::chrono::milliseconds{20}; auto made = RegionSessions::create(std::move(c)); CHECK(made.has_value()); sessions = std::move(*made); CHECK(sessions->set_motion_area(area_of(x0), x0 + (5 * m_um), 5 * m_um)); sessions->set_default_interest({(x0 / m_um) + 5, 5, 0, 7}); } void pump(std::uint64_t now_seconds) { ++tick; sessions->poll({true, 5, tick}, now_seconds, Clock::now()); sessions->replicate(tick, [](const SessionInterest&) { ReplicationSource s; s.identity = world_id; return s; }); for (auto& e : sessions->take_events()) events.push_back(std::move(e)); } RegionDb db; std::unique_ptr sessions; std::uint64_t tick{100}; std::vector events; }; void two_region_handoff(const Files& f) { WorldDb world; auto key1 = std::make_shared( *HandoffSigningKey::generate(DeploymentEnvironment::dev, 41)); auto key2 = std::make_shared( *HandoffSigningKey::generate(DeploymentEnvironment::dev, 42)); // Ports are only known once listening: region 1 dials region 2 at a port fixed below. std::uint16_t port2 = 0; { // Find a free port for region 2 by binding a throwaway region first. server::RegionSessionsConfig probe; probe.transport.certificate_pem_file = f.certificate.string(); probe.transport.key_pem_file = f.key.string(); probe.control.environment = DeploymentEnvironment::dev; probe.control.world = world_id; probe.control.region = 2; probe.control.epoch = 5; probe.issuer_keys.push_back({1, *Issuer{}.key.public_key()}); auto made = RegionSessions::create(std::move(probe)); CHECK(made.has_value()); port2 = (*made)->port(); } Region r1{f, 1, 0, world, key1, *key2->public_key(), 2, 10 * m_um, port2}; Region r2{f, 2, 10 * m_um, world, key2, *key1->public_key(), 1, 0, r1.sessions->port(), port2}; CHECK(r2.sessions->port() == port2); std::uint64_t now_s = start; Issuer issuer; std::vector clients; const auto dial = [&](std::uint16_t port, const WireGrant& credential) { client::RegionSessionClientConfig c; c.transport.port = port; c.transport.server_name = std::string{name}; c.transport.authority_pem = f.ca_pem; c.transport.authority_pem_file = f.ca.string(); c.grant = credential; auto made = RegionSessionClient::connect(std::move(c), Clock::now()); CHECK(made.has_value()); clients.push_back(made->get()); return std::move(*made); }; const auto pump = [&](const std::function& done, std::chrono::milliseconds limit = std::chrono::milliseconds{15000}) { const auto end = Clock::now() + limit; while (Clock::now() < end) { r1.pump(now_s); r2.pump(now_s); for (auto* c : clients) c->poll(Clock::now()); if (done()) return true; std::this_thread::sleep_for(std::chrono::milliseconds{2}); } return done(); }; // Sends a held vector until the region admits it (ticks here are fast: a refused, late // intention is simply sent again). const auto walk = [&](RegionSessionClient& c, std::int16_t vx) { return pump([&] { for (const auto& r : c.take_intention_receipts()) if (r.status == AdmitStatus::admitted) return true; static_cast(c.send_intention(c.server_tick() + 4, vx, 0)); return false; }); }; const auto own = [](const RegionSessionClient& c) -> std::optional { if (const auto* v = c.view()) for (const auto& m : v->markers) if (m.own) return m; return std::nullopt; }; const auto alice = account(21), bob = account(22); const auto grant = [&](const AccountId& who, std::uint64_t serial, std::uint64_t in_region) { return issuer.grant(who, serial, start, 300, in_region); }; // Bob watches the border from region 2. auto b = dial(r2.sessions->port(), grant(bob, 1, 2)); CHECK(pump([&] { return b->state() == RegionSessionState::established; })); // Alice logs in to region 1: her durable probe is created at the spawn. auto a = dial(r1.sessions->port(), grant(alice, 1, 1)); CHECK(pump([&] { return own(*a).has_value(); })); CHECK(must(own(*a)).state.x == 5 * m_um && world.probes.at(alice).owner == 1); // She walks east (100 mm per tick) to the border: frozen, prepared, offered, closed. CHECK(walk(*a, 1000)); std::optional offer; CHECK(pump([&] { if (!offer) offer = a->take_handoff(); return offer.has_value() && a->state() == RegionSessionState::closed; })); CHECK(a->close_reason() == SessionCloseReason::handed_off && must(offer).destination.region == 2 && must(offer).destination.endpoint == "127.0.0.1:" + std::to_string(r2.sessions->port())); const auto& t = world.transfers.at(must(offer).transfer); CHECK(t.state == WorldDb::State::prepared && t.x >= 10 * m_um && t.x < 11 * m_um && t.y == 5 * m_um && world.probes.at(alice).transfer == must(offer).transfer); // Before the switch, Bob sees her as a read-only ghost at the transferred position. const auto ghost = [&] { if (const auto* v = b->view()) for (const auto& m : v->markers) if (m.ghost && m.state.x == t.x) return true; return false; }; CHECK(pump(ghost)); // Alice continues at region 2 with the ticket: no grant, her probe where it crossed. auto a2 = dial(r2.sessions->port(), must(offer).ticket); CHECK(pump([&] { return own(*a2).has_value(); })); CHECK(must(own(*a2)).state.x == t.x && world.probes.at(alice).owner == 2 && world.probes.at(alice).generation == 2 && world.transfers.at(must(offer).transfer).state == WorldDb::State::committed); CHECK(pump([&] { return !ghost(); })); // The ghost gave way to her marker. // A login at region 1 is now redirected to region 2. auto stale = dial(r1.sessions->port(), grant(alice, 2, 1)); CHECK(pump([&] { return stale->state() == RegionSessionState::closed; })); CHECK(stale->close_reason() == SessionCloseReason::moved && stale->redirect() && must(stale->redirect()).region == 2); // Crossing back west: region 1 owns her again, generation 3. CHECK(walk(*a2, -1000)); std::optional back; CHECK(pump([&] { if (!back) back = a2->take_handoff(); return back.has_value() && a2->state() == RegionSessionState::closed; })); CHECK(must(back).destination.region == 1); auto a3 = dial(r1.sessions->port(), must(back).ticket); CHECK(pump([&] { return own(*a3).has_value(); })); CHECK(world.probes.at(alice).owner == 1 && world.probes.at(alice).generation == 3 && must(own(*a3)).state.x < 10 * m_um); // An abandoned handoff: Carol crosses but never arrives; after the ticket's lifetime the // source aborts and she logs in at region 1 again, where she stopped. const auto carol = account(23); auto c = dial(r1.sessions->port(), grant(carol, 1, 1)); CHECK(pump([&] { return own(*c).has_value(); })); CHECK(walk(*c, 1000)); std::optional unused; CHECK(pump([&] { if (!unused) unused = c->take_handoff(); return unused.has_value() && c->state() == RegionSessionState::closed; })); const auto frozen_at = world.transfers.at(must(unused).transfer).x; CHECK(pump([&] { ++now_s; // A second per round: the ticket's lifetime and the abort deadline pass. return !world.probes.at(carol).transfer.has_value(); })); CHECK(world.transfers.at(must(unused).transfer).state == WorldDb::State::aborted); auto c2 = dial(r1.sessions->port(), grant(carol, 2, 1)); CHECK(pump([&] { return own(*c2).has_value(); })); CHECK(must(own(*c2)).state.x <= 10 * m_um - 1 && world.probes.at(carol).x == frozen_at); // The late ticket is refused: expired by now (and its transfer aborted). auto late = dial(r2.sessions->port(), must(unused).ticket); CHECK(pump([&] { return late->state() == RegionSessionState::closed; })); CHECK(late->close_reason() == SessionCloseReason::expired); CHECK(b->state() == RegionSessionState::established && r1.db.releases > 0); // M10.8d recovery: a transfer prepared by an earlier process (unknown to this one's // in-memory deadlines) is aborted once the store reports it overdue; never before. const auto dave = account(24); TransferId orphan{}; orphan.fill(0x6D); world.probes[dave] = {1, 1, 9 * m_um, 5 * m_um, orphan}; world.transfers[orphan] = {dave, 1, 2, 1, 9 * m_um, 5 * m_um, WorldDb::State::prepared}; const auto reads = world.overdue_reads; CHECK(pump([&] { return world.overdue_reads > reads + 2; })); CHECK(world.transfers.at(orphan).state == WorldDb::State::prepared); world.all_overdue = true; CHECK(pump([&] { return !world.probes.at(dave).transfer.has_value(); })); CHECK(world.transfers.at(orphan).state == WorldDb::State::aborted && world.probes.at(dave).owner == 1); world.all_overdue = false; } void durable_control(const Files& f) { FakeLedger ledger; Harness h{f, 4, 64, &ledger}; const auto alice = account(1), bob = account(2), carol = account(3); // Durable refusals: nothing is admitted and the grant stays usable locally. ledger.claim_error = ControlLedgerError::stale_grant; CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::stale_grant)); ledger.claim_error = ControlLedgerError::fenced; CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::shutdown)); ledger.claim_error = ControlLedgerError::unavailable; CHECK(refused_with(h, h.issuer.grant(alice, 1), Reason::capacity)); CHECK(!h.server->holder(alice) && ledger.claims.empty()); ledger.claim_error.reset(); // The claim names the account, serial and the session that Welcome then reports. auto a = h.connect(h.issuer.grant(alice, 1)); CHECK(h.settled(*a) && a->state() == RegionSessionState::established); CHECK(ledger.claims.size() == 1 && ledger.claims[0].account == alice && ledger.claims[0].serial == 1 && ledger.claims[0].session == must(a->welcome()).session); CHECK(h.until([&] { return ledger.renewed.size() >= 2; })); // Renewed every 100 ms. // Refresh records the newer serial for the same session. CHECK(a->refresh(h.issuer.grant(alice, 2))); std::optional r; CHECK(h.until([&] { return (r = a->take_refresh_result()).has_value(); })); CHECK(must(r).status == 0 && ledger.claims.size() == 2 && ledger.claims[1].serial == 2 && ledger.claims[1].session == must(a->welcome()).session); // Another region took control: the renewal is fenced and the session is superseded, // without releasing control it no longer holds. ledger.renew_error = ControlLedgerError::fenced; CHECK(h.until([&] { return a->state() == RegionSessionState::closed; })); CHECK(a->close_reason() == Reason::superseded && ledger.released.empty()); CHECK(!h.server->holder(alice)); // Control held by another region: Welcome waits it out. ledger.renew_error.reset(); ledger.wait = std::chrono::milliseconds{400}; const auto sent = Clock::now(); auto b = h.connect(h.issuer.grant(bob, 1)); CHECK(h.until([&] { return h.server->holder(bob).has_value(); })); CHECK(b->state() != RegionSessionState::established); CHECK(h.settled(*b) && b->state() == RegionSessionState::established); CHECK(Clock::now() - sent >= std::chrono::milliseconds{400}); // Undecided renewals: the session closes before its control could lapse, and releases. ledger.wait = {}; ledger.granted = std::chrono::milliseconds{300}; auto c = h.connect(h.issuer.grant(carol, 1)); CHECK(h.settled(*c) && c->state() == RegionSessionState::established); const auto carol_generation = ledger.next_generation - 1; ledger.renew_error = ControlLedgerError::unavailable; CHECK(h.until([&] { return c->state() == RegionSessionState::closed; })); CHECK(c->close_reason() == Reason::shutdown); CHECK(std::ranges::find(ledger.released, carol_generation) != ledger.released.end()); // A refresh that finds a newer grant elsewhere closes the session as superseded. ledger.renew_error.reset(); ledger.granted = std::chrono::milliseconds{20000}; ledger.claim_error = ControlLedgerError::stale_grant; CHECK(b->refresh(h.issuer.grant(bob, 2))); CHECK(h.until([&] { return b->state() == RegionSessionState::closed; })); CHECK(b->close_reason() == Reason::superseded && !b->take_refresh_result()); // Client close releases durable control. ledger.claim_error.reset(); auto d = h.connect(h.issuer.grant(alice, 3)); CHECK(h.settled(*d) && d->state() == RegionSessionState::established); const auto alice_generation = ledger.next_generation - 1; d->close(); CHECK(h.until([&] { return std::ranges::find(ledger.released, alice_generation) != ledger.released.end(); })); } // M10.9c (ADR-0097): abuse against one region while an honest session is measured. Each // round is one 50 ms tick: the abuse acts, the honest client sends one intention, and the // server replicates. The honest session must keep at least 90 % of its snapshots and // receipts, and the server's connections and retained state must stay bounded. struct Service { std::uint64_t ticks{}, views{}, sent{}, receipts{}; }; Service serve(Harness& h, RegionSessionClient& honest, std::size_t rounds, const std::function& abuse) { Service out; std::uint64_t last_view = honest.view() ? honest.view()->tick : 0; for (std::size_t round = 0; round < rounds; ++round) { ++h.tick; ++out.ticks; abuse(round); if (honest.send_intention(h.tick + 2, 100, 0)) ++out.sent; const auto end = Clock::now() + std::chrono::milliseconds{50}; while (Clock::now() < end) { h.pump(); for (const auto& r : honest.take_intention_receipts()) out.receipts += r.status == AdmitStatus::admitted; if (const auto* v = honest.view(); v && v->tick != last_view) { last_view = v->tick; ++out.views; } std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } std::printf( "abuse service ticks=%llu views=%llu sent=%llu admitted=%llu\n", static_cast(out.ticks), static_cast(out.views), static_cast(out.sent), static_cast(out.receipts)); return out; } bool served(const Service& s) { return s.views * 10 >= s.ticks * 9 && s.sent * 10 >= s.ticks * 9 && s.receipts * 10 >= s.sent * 9; } std::unique_ptr honest_session(Harness& h) { h.source = [&h](const SessionInterest&) { ReplicationSource s; s.identity = world_id; s.field_revision = h.tick; s.mask = (1U << 0) | (1U << 1) | (1U << 7); for (std::int64_t x = -3; x <= 3; ++x) for (std::int64_t y = -3; y <= 3; ++y) s.environment.push_back(grid_cell(x, y, 30 + (h.tick % 7))); return s; }; h.server->set_default_interest({0, 0, -7, 7}); auto c = h.connect(h.issuer.grant(account(0xA0), 1)); CHECK(h.settled(*c) && c->state() == RegionSessionState::established); // Wait for the datagram channel and the first snapshot. CHECK(h.until([&] { ++h.tick; return c->view() != nullptr && c->send_intention(h.tick + 2, 0, 0).has_value(); })); return c; } // Handshake and Hello floods: many connections that send garbage, forged or no Hello. They // are closed (protocol, unauthenticated, timeout) or refused at the connection limit; the // region's connections never exceed the transport bound; the honest session is served. void hello_flood(const Files& f) { Harness h{f, 4, 64}; auto honest = honest_session(h); // A key the region does not trust (key id 7): its well-formed grants are refused. const auto stranger = *GrantSigningKey::import(DeploymentEnvironment::dev, 7, [] { std::array s{}; s.back() = 0x2B; return s; }()); // Every peer is polled each round and acts once connected: 0 sends garbage, 1 a // well-formed Hello whose grant no trusted key signed, 2 stays silent until the deadline. struct Peer { std::unique_ptr endpoint; std::size_t kind{}; }; std::vector peers; std::size_t most_connections = 0; std::array acted{}; unsigned sent = 0; const auto service = serve(h, *honest, 60, [&](std::size_t round) { if (round < 40) for (std::size_t k = 0; k < 4; ++k) peers.push_back({h.raw(), (round * 4 + k) % 3}); for (auto& peer : peers) for (auto& e : peer.endpoint->poll()) { if (e.kind != quic::EventKind::connected) continue; ++acted.at(peer.kind); if (peer.kind == 0) sent += peer.endpoint ->send_message(e.connection, std::vector{9, 9, 9, 9}) .has_value(); if (peer.kind == 1) { GrantClaims c; c.grant_id = 9000 + round; c.account = account(0xF0); c.serial = 1; c.world = world_id; c.region = region; c.issued_at = start; c.expires_at = start + 60; sent += peer.endpoint ->send_message(e.connection, encode_session(SessionHello{0, *stranger.sign(c)})) .has_value(); } } most_connections = std::max(most_connections, h.server->connections()); }); std::printf("abuse hello_flood peers=%zu most_connections=%zu connected garbage=%u " "forged=%u silent=%u\n", peers.size(), most_connections, acted[0], acted[1], acted[2]); CHECK(sent == acted[0] + acted[1]); // Every garbage and forged message went out. CHECK(served(service) && honest->state() == RegionSessionState::established); CHECK(most_connections <= server::RegionSessionsConfig{}.transport.limits.max_connections); peers.clear(); CHECK(h.until([&] { return h.server->connections() == 1; })); std::map reasons; for (const auto& e : h.events) if (e.kind == RegionSessionEvent::Kind::refused) ++reasons[static_cast(e.reason)]; for (const auto& [why, n] : reasons) std::printf("abuse hello_flood refused reason=%u count=%u\n", why, n); CHECK(h.has_event(RegionSessionEvent::Kind::refused, Reason::protocol) && h.has_event(RegionSessionEvent::Kind::refused, Reason::unauthenticated) && h.has_event(RegionSessionEvent::Kind::refused, Reason::timeout)); } // Datagram flood from an authenticated peer: hundreds of intentions per tick for any tick // (throttled per session: one per tick), then garbage, which closes it (protocol). The // honest session keeps its receipts and snapshots. void datagram_flood(const Files& f) { Harness h{f, 4, 64}; auto honest = honest_session(h); auto peer = h.raw(); quic::ConnectionId id = 0; bool welcomed = false, closed = false; std::optional reason; std::uint64_t flood = 0, refused = 0, garbage = 0; const auto drain = [&] { for (auto& e : peer->poll()) { if (e.kind == quic::EventKind::connected) { id = e.connection; CHECK(peer->send_message( id, encode_session(SessionHello{0, h.issuer.grant(account(0xB0), 1)})) .has_value()); } else if (e.kind == quic::EventKind::message || e.kind == quic::EventKind::datagram) { const auto m = decode_session(e.bytes); if (m && std::holds_alternative(*m)) welcomed = true; if (m && std::holds_alternative(*m)) refused += std::get(*m).status != AdmitStatus::admitted; if (m && std::holds_alternative(*m)) reason = std::get(*m).reason; } else if (e.kind == quic::EventKind::closed) { closed = true; } } }; CHECK(h.until([&] { drain(); return welcomed; })); std::uint64_t sequence = 0; const auto service = serve(h, *honest, 40, [&](std::size_t round) { drain(); if (round < 30) for (int k = 0; k < 400; ++k) { SessionIntention in; in.sequence = ++sequence; in.tick = h.tick + 1 + static_cast(k % 8); in.move_x = 100; flood += peer->send_datagram(id, encode_session(in)).has_value(); } if (round == 30) for (int k = 0; k < 50; ++k) garbage += peer->send_datagram(id, std::vector(64, 0xEE)).has_value(); }); CHECK(h.until([&] { drain(); return closed || reason.has_value(); })); std::printf("abuse datagram_flood sent=%llu refused_receipts=%llu close=%u\n", static_cast(flood), static_cast(refused), static_cast(reason.value_or(Reason::connection_lost))); CHECK(served(service) && honest->state() == RegionSessionState::established); CHECK(flood > 1000 && garbage > 0 && reason == Reason::protocol); } // A slow peer: established, then never polled (it reads nothing and acknowledges no // snapshot). The server keeps at most its baselines for it and stays bounded; the honest // session is served throughout. void slow_peer(const Files& f) { Harness h{f, 4, 64}; auto honest = honest_session(h); auto slow = h.connect(h.issuer.grant(account(0xC0), 1)); CHECK(h.settled(*slow) && slow->state() == RegionSessionState::established); CHECK(slow->set_interest({0, 0, -7, 7})); h.forget(slow.get()); // From now on nobody polls it. std::size_t most_retained = 0; const auto service = serve(h, *honest, 80, [&](std::size_t) { most_retained = std::max(most_retained, h.server->replication_stats().retained_entities); }); std::printf("abuse slow_peer most_retained_entities=%zu sessions=%zu\n", most_retained, h.server->sessions()); CHECK(served(service) && honest->state() == RegionSessionState::established); // Two sessions, 49 cells each, at most 4 baselines (ADR-0089) plus the working state. CHECK(most_retained <= std::size_t{2} * 49 * 5); slow.reset(); CHECK(h.until([&] { return h.server->sessions() == 1; }, std::chrono::milliseconds{15000})); } // Connect/disconnect storm: 120 logins across 12 accounts, each closed politely or dropped // mid-session; the region returns to the honest session alone, which is served throughout. void storm(const Files& f) { Harness h{f, 8, 1024}; auto honest = honest_session(h); std::vector> live; std::array serials{}; std::uint64_t opened = 0; const auto service = serve(h, *honest, 60, [&](std::size_t round) { if (round >= 40) return; for (int k = 0; k < 3; ++k) { const auto who = (round * 3 + static_cast(k)) % serials.size(); auto c = h.connect( h.issuer.grant(account(static_cast(0xD0 + who)), ++serials.at(who))); live.push_back(std::move(c)); } // Older ones leave: politely, or by vanishing (the transport notices). while (live.size() > 6) { auto& c = live.front(); opened += c->state() == RegionSessionState::established; if (opened % 2 == 0) c->close(); h.forget(c.get()); live.erase(live.begin()); } }); for (auto& c : live) { h.forget(c.get()); c->close(); } live.clear(); std::printf("abuse storm logins=120 established_seen=%llu\n", static_cast(opened)); CHECK(served(service) && honest->state() == RegionSessionState::established); CHECK(opened > 20); CHECK(h.until([&] { return h.server->sessions() == 1 && h.server->connections() == 1; }, std::chrono::milliseconds{15000})); } } // namespace int main() try { const auto files = credentials(); login_rules(files); primitive_v5_session(files); cellular_v6_session(files); limits(files); durable_control(files); intentions_and_actions(files); snapshots_over_quic(files); ticket_login(files); two_region_handoff(files); hello_flood(files); datagram_flood(files); slow_peer(files); storm(files); std::filesystem::remove_all(files.dir); std::puts("quic session tests passed"); return 0; } catch (const std::exception& e) { std::fprintf(stderr, "%s\n", e.what()); return 1; }