// Real process tests for M9.7 dev sessions (ADR-0083): evo-server with PostgreSQL, // synthetic inputs and the TLS session listener, driven by the real SessionClient. // Run by tools/ci/postgres-check.sh when the build has EVO_BUILD_NETWORK. #include "evo/network/dev_credentials.hpp" #include "evo/network/session_client.hpp" #include "fixtures/server_process.hpp" #include #include #include #include #include #include #include void run_region_session_tests(); namespace { using namespace evo::test::process; using namespace evo; using evo::test::region_identity; using network::ClientEvent; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("region session test 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("region session missing value line " + std::to_string(at.line())); return *o; } constexpr const char* name = "dev-region.evolution.test"; void write_secret(const fs::path& path, const std::string& text) { const int fd = ::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); expect(fd >= 0 && ::write(fd, text.data(), text.size()) == static_cast(text.size()) && ::close(fd) == 0, "write secret"); } struct Credentials { network::DevCredentials made; std::vector args(const ServerRegion& r, std::uint16_t port, const char* environment = "dev") const { write_secret(r.root / "server.pem", made.server_certificate_pem); write_secret(r.root / "server.key", made.server_key_pem); write_secret(r.root / "grant.key", network::format_grant_key(made.grant_key)); return server_args(r, {"--synthetic-inputs", "development-v1", "--session-listen", "127.0.0.1:" + std::to_string(port), "--session-cert", (r.root / "server.pem").string(), "--session-key", (r.root / "server.key").string(), "--session-grant-ledger", (r.root / "grants.ledger").string(), "--session-grant-key", (r.root / "grant.key").string(), "--session-environment", environment}); } std::vector grants(const ServerRegion& r, std::uint64_t subject, std::size_t count, std::uint64_t first) const { const auto key = network::DevGrantKey::import(network::DeploymentEnvironment::dev, made.grant_key); expect(key.has_value(), "grant key"); const auto now = static_cast(std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count()); std::vector out; for (std::size_t i = 0; i < count; ++i) { const auto g = key->issue( {first + i, subject, region_identity_of(r), r.key.region, now - 5, now + 900}); expect(g.has_value(), "issue grant"); out.push_back(*g); } return out; } static world::WorldIdentity region_identity_of(const ServerRegion& r) { return {r.key.world, region_identity.frame}; } }; std::unique_ptr client(const Credentials& c, std::uint16_t port, std::vector grants) { network::SessionClientConfig config; config.host = "127.0.0.1"; config.port = port; config.trust = {c.made.authority_pem, name}; config.grants = std::move(grants); config.first_retry = 100ms; config.max_retry = 1000ms; auto made = network::SessionClient::create(std::move(config)); expect(made.has_value(), "client"); return std::move(*made); } std::uint16_t listening_port(Child& server) { std::uint16_t port = 0; CHECK(server.wait_for( [&](const std::string& l) { unsigned p = 0; if (std::sscanf(l.c_str(), "session listening %u", &p) != 1) return false; port = static_cast(p); return true; }, 15s)); return port; } // Pumps the client while draining the server's output (it must never block on a pipe). bool pump(Child& server, std::vector clients, std::vector& events, const std::function& done, std::chrono::milliseconds limit = 20s) { const auto end = Clock::now() + limit; while (Clock::now() < end) { for (auto* c : clients) { c->poll(Clock::now()); auto e = c->take_events(); if (c == clients.front()) events.insert(events.end(), e.begin(), e.end()); } if (done()) return true; (void)server.wait_for([](const std::string&) { return false; }, 2ms); } return false; } bool has(const std::vector& e, ClientEvent::Kind k) { return std::ranges::any_of(e, [&](const ClientEvent& x) { return x.kind == k; }); } const ClientEvent* last(const std::vector& e, ClientEvent::Kind k) { const auto it = std::ranges::find_if(e.rbegin(), e.rend(), [&](const ClientEvent& x) { return x.kind == k; }); return it == e.rend() ? nullptr : &*it; } std::string hex(const std::array& b) { char out[9]{}; std::snprintf(out, sizeof out, "%02x%02x%02x%02x", b[0], b[1], b[2], b[3]); return out; } void refuses_unsafe_configurations(const Credentials& c) { const auto r = provision(141, 0x53); // The dev identity is refused outside dev, before anything is served. Child stage{c.args(r, 0, "stage")}; CHECK(stage.wait_exit(15s) == 2 && stage.printed("error session-environment")); // Sessions without the synthetic carrier are a usage error. auto args = c.args(r, 0); args.erase(std::ranges::find(args, "--synthetic-inputs"), std::ranges::find(args, "--synthetic-inputs") + 2); Child usage{args}; CHECK(usage.wait_exit(15s) == 2); } void connects_acknowledges_and_recovers(const Credentials& c) { const auto r = provision(140, 0x54); std::uint16_t port = 0; auto client_grants = c.grants(r, 7, 6, 1000); std::vector events; std::uint64_t first_commit = 0; std::unique_ptr session; { Child server{c.args(r, 0)}; port = listening_port(server); CHECK(server.wait_for([](const std::string& l) { return l == "ready 1"; }, 15s)); session = client(c, port, client_grants); CHECK(pump(server, {session.get()}, events, [&] { return session->latest_view().has_value(); })); const auto w = must(session->welcome()); // Copy: welcome() returns by value. CHECK(w.epoch == 1 && w.world == r.key.world && w.region == r.key.region); // Snapshot display: the region's real field stores, explicitly no individuals. CHECK(session->environment().freshness() == client::Freshness::current); CHECK(!session->environment().current()->cells().empty()); CHECK(pump(server, {session.get()}, events, [&] { return session->organisms().current() != nullptr; })); CHECK(session->organisms().current()->cells().empty()); CHECK(server.printed("session opened 1 subject=7")); // Minimal admitted intention: durable acknowledgement, no biological effect. CHECK(session->send({1, 0, 1000, 0})); CHECK(pump(server, {session.get()}, events, [&] { return has(events, ClientEvent::Kind::outcome); })); CHECK(last(events, ClientEvent::Kind::result)->result.status == network::IntentStatus::admitted); const auto* outcome = last(events, ClientEvent::Kind::outcome); CHECK(outcome->outcome.finality == network::IntentFinality::committed_no_effect); first_commit = outcome->outcome.commit_sequence; // The host acknowledged exactly that carrier ('I','N' + 48-bit epoch 1 + ordinal) // at that tick and commit. const std::string prefix = "ack " + hex({'I', 'N', 0, 0}) + "00000001"; const auto ack = std::ranges::find_if( server.lines(), [&](const std::string& l) { return starts(l, prefix); }); CHECK(ack != server.lines().end()); CHECK(ack->ends_with(" " + std::to_string(outcome->outcome.tick) + " " + std::to_string(outcome->outcome.commit_sequence))); // Rejected: a replay is never scheduled. events.clear(); CHECK(session->send({1, 0, 0, 1000})); CHECK(pump(server, {session.get()}, events, [&] { return has(events, ClientEvent::Kind::result); })); CHECK(last(events, ClientEvent::Kind::result)->result.status == network::IntentStatus::replayed); // Duplicate session for the same subject is refused; the first stays. auto twin = client(c, port, c.grants(r, 7, 1, 2000)); std::vector twin_events; CHECK(pump(server, {twin.get(), session.get()}, twin_events, [&] { return has(twin_events, ClientEvent::Kind::closed); })); CHECK(last(twin_events, ClientEvent::Kind::closed)->reason == network::CloseReason::duplicate_session); CHECK(session->phase() == network::SessionPhase::established); // Pod restart analogue: SIGKILL. The client loses the session; any admitted // intention without a final outcome is reported unknown, never assumed. events.clear(); CHECK(session->send({2, 0, -1000, 0})); server.signal(SIGKILL); CHECK(server.wait_exit(10s) == 128 + SIGKILL); } { const auto end = Clock::now() + 5s; while (Clock::now() < end && !has(events, ClientEvent::Kind::lost)) { session->poll(Clock::now()); auto e = session->take_events(); events.insert(events.end(), e.begin(), e.end()); std::this_thread::sleep_for(5ms); } CHECK(has(events, ClientEvent::Kind::lost)); CHECK(session->environment().freshness() == client::Freshness::stale); } // Successor on the same port: ready only after the lease expires; the client // reconnects with a fresh grant and a fresh baseline at the new epoch. Child successor{c.args(r, port)}; CHECK(listening_port(successor) == port); CHECK(pump( successor, {session.get()}, events, [&] { return session->phase() == network::SessionPhase::established && session->latest_view().has_value(); }, 30s)); CHECK(must(session->welcome()).epoch == 2 && must(session->welcome()).session == 1); CHECK(session->environment().freshness() == client::Freshness::current); events.clear(); CHECK(session->send({3, 0, 0, -1000})); CHECK(pump(successor, {session.get()}, events, [&] { return has(events, ClientEvent::Kind::outcome); })); CHECK(last(events, ClientEvent::Kind::outcome)->outcome.commit_sequence > first_commit); successor.signal(SIGTERM); CHECK(successor.wait_exit(20s) == 0); std::printf("region_session port=%u first_commit=%llu grants_left=%zu\n", static_cast(port), static_cast(first_commit), session->grants_left()); } } // namespace void run_region_session_tests() { Credentials c; auto made = network::generate_dev_credentials(network::DeploymentEnvironment::dev, name, 1, std::chrono::system_clock::now()); expect(made.has_value(), "credentials"); c.made = std::move(*made); refuses_unsafe_configurations(c); connects_acknowledges_and_recovers(c); }