Run a non-blocking TLS 1.3 session server inside evo-server's loop: environment views come from the region's own field/terrain stores with an explicitly empty organism presentation; admitted intentions ride the next synthetic input (content unchanged) and return a durable no-effect outcome from the host's final acknowledgement, or not_committed when dropped. Sessions are bound to their connection, epochs close stale sessions, and connections have bounded output, input rate and deadlines. SessionClient and evo-client --connect consume one single-use grant per Hello (refusals before admission keep it), reset to a fresh baseline on every session, keep stale views visible and report in-flight intentions as unknown after loss. evo-dev-session generates dev credentials, grant bundles and runs a restart probe. The dev deployment gains a drill-time, loopback-only listener with an optional Secret, enforced by the policy check, and tools/ops/session-drill.sh. Protocol v1 gains IntentOutcome and the capacity close reason before any deployment. ADR-0083 records the maintainer-confirmed decisions; M8-dev/stage remains not passed and no cluster change was made. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
512 lines
25 KiB
C++
512 lines
25 KiB
C++
#include "evo/network/admission.hpp"
|
|
#include "evo/network/dev_credentials.hpp"
|
|
#include "evo/network/dev_grant.hpp"
|
|
#include "evo/network/protocol.hpp"
|
|
#include "network_test.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <random>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace evo::network_test {
|
|
namespace {
|
|
using namespace evo::network;
|
|
constexpr world::WorldIdentity identity{11, 12};
|
|
constexpr std::uint32_t env_mask = (1U << 0) | (1U << 1) | (1U << 7);
|
|
using Bytes = std::vector<std::uint8_t>;
|
|
|
|
client::EnvironmentPresentation environment(std::size_t count = 3) {
|
|
std::vector<client::EnvironmentCell> cells;
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
client::EnvironmentCell c;
|
|
c.voxel = {static_cast<std::int64_t>(i) - 1, 2, -7};
|
|
c.chunk_revision = 40 + i;
|
|
c.habitat = {{100}, {200}, {0}, {1000}, {500}, {10}};
|
|
c.volume = {1000ULL * 500 * 10};
|
|
c.substrate = {1};
|
|
c.terrain_material = {2};
|
|
c.terrain_fill = static_cast<std::uint16_t>(1000 * i);
|
|
c.presence[0] = client::FieldPresence::present;
|
|
c.quantity[0] = 30 + i;
|
|
c.presence[1] = client::FieldPresence::missing;
|
|
c.presence[7] = client::FieldPresence::present;
|
|
c.quantity[7] = i == 1 ? 0 : 0xFFFF'FFFF'FFFF'0000ULL;
|
|
c.moisture_status = world::MoistureStatus::available;
|
|
c.moisture = {3, 50};
|
|
cells.push_back(c);
|
|
}
|
|
auto view = client::EnvironmentPresentation::create({identity, 9, 4}, env_mask, -7, cells);
|
|
CHECK(view);
|
|
return std::move(*view);
|
|
}
|
|
client::OrganismPresentation organisms(std::size_t count = 2) {
|
|
using biology::Capability;
|
|
const auto genome = biology::Genome::create({7,
|
|
1,
|
|
1,
|
|
{},
|
|
{{Capability::energy_conversion, 400, 0},
|
|
{Capability::membrane, 10, 0},
|
|
{Capability::movement, 50, 0},
|
|
{Capability::target_size, 8'000'000, 0}}});
|
|
CHECK(genome);
|
|
const biology::EnvironmentState temperate{
|
|
293'150, {{1'000'000'000}, {accounting::Femtograms{1'000'000'000}, {}, {}, {}}}};
|
|
std::vector<client::OrganismCell> cells;
|
|
for (std::size_t i = 0; i < count; ++i) {
|
|
auto cell = client::present_organism(
|
|
i + 1, {-3, 4, -7}, {static_cast<std::uint64_t>(100 * i)}, {999'999}, *genome,
|
|
{static_cast<std::uint32_t>(500 + i % 500), 0}, temperate,
|
|
i % 3 == 2 ? client::OrganismState::dead : client::OrganismState::alive);
|
|
CHECK(cell);
|
|
cells.push_back(*cell);
|
|
}
|
|
auto view = client::OrganismPresentation::create({identity, 9, 5}, -7, cells);
|
|
CHECK(view);
|
|
return std::move(*view);
|
|
}
|
|
constexpr ViewHeader view_header{3, 4, 9, 8, 77};
|
|
bool same(const ViewHeader& a, const ViewHeader& b) {
|
|
return a.session == b.session && a.epoch == b.epoch && a.server_tick == b.server_tick &&
|
|
a.commit_watermark == b.commit_watermark && a.baseline == b.baseline;
|
|
}
|
|
FrameView frame(const Bytes& bytes) {
|
|
auto f = decode_frame(bytes);
|
|
CHECK(f);
|
|
return *f;
|
|
}
|
|
Bytes with_header(MessageType type, std::uint16_t version, std::uint8_t flags, std::uint32_t length,
|
|
std::size_t payload) {
|
|
Bytes b{0x45, 0x56, 0x44, 0x53};
|
|
b.push_back(static_cast<std::uint8_t>(version));
|
|
b.push_back(static_cast<std::uint8_t>(version >> 8));
|
|
b.push_back(static_cast<std::uint8_t>(type));
|
|
b.push_back(flags);
|
|
for (int i = 0; i < 4; ++i)
|
|
b.push_back(static_cast<std::uint8_t>(length >> (8 * i)));
|
|
b.resize(b.size() + payload);
|
|
return b;
|
|
}
|
|
|
|
void round_trips() {
|
|
const Hello hello{0, Bytes(dev_grant_bytes, 0xA5)};
|
|
const auto hello_bytes = encode(hello);
|
|
CHECK(hello_bytes);
|
|
const auto h = decode_hello(frame(*hello_bytes));
|
|
CHECK(h && h->grant == hello.grant && h->required_features == 0);
|
|
|
|
const Welcome welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16};
|
|
const auto w = decode_welcome(frame(encode(welcome)));
|
|
CHECK(w && w->session == 1 && w->world == 11 && w->frame == 12 && w->region == 13 &&
|
|
w->epoch == 14 && w->server_tick == 15 && w->commit_watermark == 16 &&
|
|
w->grant_expires_at == 17 && w->intent_window == 8 && w->window_ticks == 8 &&
|
|
w->max_intents_per_window == 16);
|
|
|
|
// Explicit little-endian golden: no native layout, padding or byte order leaks.
|
|
const IntentMessage intent{1, 2, 3, 4, -1, 1000};
|
|
const auto intent_bytes = encode(intent);
|
|
const Bytes golden{
|
|
0x45, 0x56, 0x44, 0x53, 1, 0, 3, 0, 36, 0, 0, 0, // header
|
|
1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, // session, epoch
|
|
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, // sequence, tick
|
|
0xFF, 0xFF, 0xE8, 0x03}; // -1, 1000
|
|
CHECK(intent_bytes == golden);
|
|
const auto i = decode_intent(frame(intent_bytes));
|
|
CHECK(i && i->session == 1 && i->epoch == 2 && i->sequence == 3 && i->tick == 4 &&
|
|
i->move_x == -1 && i->move_y == 1000);
|
|
const auto from_client = make_intent(5, 6, client::Intention{7, 8, -707, 707});
|
|
CHECK(from_client.session == 5 && from_client.epoch == 6 && from_client.sequence == 7 &&
|
|
from_client.tick == 8 && from_client.move_x == -707 && from_client.move_y == 707);
|
|
|
|
const auto r = decode_intent_result(frame(encode(IntentResult{9, IntentStatus::replayed, 10})));
|
|
CHECK(r && r->sequence == 9 && r->status == IntentStatus::replayed && r->server_tick == 10);
|
|
const auto c = decode_close(frame(encode(CloseReason::stale_epoch)));
|
|
CHECK(c && *c == CloseReason::stale_epoch);
|
|
CHECK(decode_close(frame(encode(CloseReason::capacity))) == CloseReason::capacity);
|
|
const auto out = decode_intent_outcome(
|
|
frame(encode(IntentOutcome{4, IntentFinality::committed_no_effect, 120, 9})));
|
|
CHECK(out && out->sequence == 4 && out->finality == IntentFinality::committed_no_effect &&
|
|
out->tick == 120 && out->commit_sequence == 9);
|
|
CHECK(decode_intent_outcome(
|
|
frame(encode(IntentOutcome{5, IntentFinality::not_committed, 0, 0}))));
|
|
// Finality and commit facts must agree.
|
|
CHECK(refused(decode_intent_outcome(
|
|
frame(encode(IntentOutcome{4, IntentFinality::committed_no_effect, 120, 0}))),
|
|
ProtocolError::invalid_value));
|
|
CHECK(refused(decode_intent_outcome(
|
|
frame(encode(IntentOutcome{4, IntentFinality::not_committed, 120, 0}))),
|
|
ProtocolError::invalid_value));
|
|
|
|
const auto env = environment();
|
|
const auto env_bytes = encode(view_header, env);
|
|
const auto e = decode_environment_view(frame(env_bytes));
|
|
CHECK(e && same(e->first, view_header) && e->second.revision() == env.revision() &&
|
|
e->second.requested_mask() == env_mask && e->second.slice_z() == -7);
|
|
CHECK(e->second.cells().size() == 3 &&
|
|
e->second.cells()[2].quantity[7] == 0xFFFF'FFFF'FFFF'0000ULL);
|
|
CHECK(encode(e->first, e->second) == env_bytes); // Canonical.
|
|
const auto orgs = organisms();
|
|
const auto org_bytes = encode(view_header, orgs);
|
|
const auto o = decode_organism_view(frame(org_bytes));
|
|
CHECK(o && o->second.revision() == orgs.revision() && o->second.cells().size() == 2 &&
|
|
o->second.cells()[1].phenotype == orgs.cells()[1].phenotype &&
|
|
o->second.cells()[1].y.value == 999'999);
|
|
CHECK(encode(o->first, o->second) == org_bytes);
|
|
// Full-size views fit the frame bound.
|
|
CHECK(encode(view_header, environment(client::max_environment_cells)).size() <=
|
|
frame_header_bytes + max_frame_payload);
|
|
CHECK(encode(view_header, organisms(client::max_organism_cells)).size() <=
|
|
frame_header_bytes + max_frame_payload);
|
|
}
|
|
|
|
void frame_rejection() {
|
|
const auto intent = encode(IntentMessage{1, 1, 1, 1, 0, 0});
|
|
CHECK(refused(decode_frame(Bytes(11, 0)), ProtocolError::truncated));
|
|
auto bad = intent;
|
|
bad[0] ^= 1;
|
|
CHECK(refused(decode_frame(bad), ProtocolError::bad_magic));
|
|
CHECK(refused(decode_frame(with_header(MessageType::intent, 2, 0, 36, 36)),
|
|
ProtocolError::unsupported_version));
|
|
CHECK(refused(decode_frame(with_header(MessageType::intent, 0, 0, 36, 36)),
|
|
ProtocolError::unsupported_version));
|
|
// Out-of-range enumerators model untrusted wire bytes; rejecting them is the point.
|
|
// NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
|
|
CHECK(refused(decode_frame(with_header(MessageType{0}, 1, 0, 0, 0)),
|
|
ProtocolError::unknown_type));
|
|
// NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
|
|
CHECK(refused(decode_frame(with_header(MessageType{9}, 1, 0, 0, 0)),
|
|
ProtocolError::unknown_type));
|
|
CHECK(refused(decode_frame(with_header(MessageType::intent, 1, 1, 36, 36)),
|
|
ProtocolError::reserved_flags));
|
|
CHECK(refused(
|
|
decode_frame(with_header(MessageType::environment_view, 1, 0, max_frame_payload + 1, 0)),
|
|
ProtocolError::oversized));
|
|
auto shorter = intent;
|
|
shorter.pop_back();
|
|
CHECK(refused(decode_frame(shorter), ProtocolError::truncated));
|
|
auto longer = intent;
|
|
longer.push_back(0);
|
|
CHECK(refused(decode_frame(longer), ProtocolError::trailing_bytes));
|
|
// Payload-level checks.
|
|
CHECK(refused(decode_welcome(frame(intent)), ProtocolError::wrong_type));
|
|
CHECK(refused(decode_intent(frame(with_header(MessageType::intent, 1, 0, 35, 35))),
|
|
ProtocolError::truncated));
|
|
CHECK(refused(decode_intent(frame(with_header(MessageType::intent, 1, 0, 37, 37))),
|
|
ProtocolError::trailing_bytes));
|
|
auto feature = *encode(Hello{0, Bytes(4, 1)});
|
|
feature[frame_header_bytes] = 1;
|
|
CHECK(refused(decode_hello(frame(feature)), ProtocolError::unsupported_feature));
|
|
CHECK(refused(encode(Hello{0, {}}), ProtocolError::invalid_value));
|
|
CHECK(refused(encode(Hello{0, Bytes(max_grant_bytes + 1, 1)}), ProtocolError::invalid_value));
|
|
auto empty_grant = with_header(MessageType::hello, 1, 0, 6, 6);
|
|
CHECK(refused(decode_hello(frame(empty_grant)), ProtocolError::invalid_value));
|
|
auto status = encode(IntentResult{1, IntentStatus::admitted, 1});
|
|
status[frame_header_bytes + 8] = 8;
|
|
CHECK(refused(decode_intent_result(frame(status)), ProtocolError::invalid_value));
|
|
auto reason = encode(CloseReason::normal);
|
|
reason[frame_header_bytes] = 10;
|
|
CHECK(refused(decode_close(frame(reason)), ProtocolError::invalid_value));
|
|
CHECK(refused(decode_welcome(frame(encode(Welcome{}))), ProtocolError::invalid_value));
|
|
// A well-formed encoding of an invalid view is rejected by the presentation rules.
|
|
auto env = encode(view_header, environment(1));
|
|
const std::size_t habitat_x = frame_header_bytes + 40 + 32 + 4 + 8 + 2 + 16 + 8;
|
|
env[habitat_x + 2] = 0x10; // x = 100 + 0x100000 um: outside the voxel.
|
|
CHECK(refused(decode_environment_view(frame(env)), ProtocolError::invalid_value));
|
|
auto count = encode(view_header, environment(1));
|
|
count[frame_header_bytes + 40 + 32 + 4 + 8] = 1; // 257 cells.
|
|
count[frame_header_bytes + 40 + 32 + 4 + 8 + 1] = 1;
|
|
CHECK(refused(decode_environment_view(frame(count)), ProtocolError::invalid_value));
|
|
auto state = encode(view_header, organisms(1));
|
|
state.back() = 2;
|
|
CHECK(refused(decode_organism_view(frame(state)), ProtocolError::invalid_value));
|
|
}
|
|
|
|
void reader() {
|
|
Bytes stream = encode(IntentMessage{1, 1, 1, 1, 0, 0});
|
|
const auto second = encode(view_header, environment());
|
|
stream.insert(stream.end(), second.begin(), second.end());
|
|
FrameReader r;
|
|
std::vector<OwnedFrame> frames;
|
|
for (const auto byte : stream) {
|
|
const auto fed = r.feed({&byte, 1});
|
|
CHECK(fed && *fed == 1); // Completed frames are taken immediately below.
|
|
if (auto f = r.take())
|
|
frames.push_back(std::move(*f));
|
|
}
|
|
CHECK(frames.size() == 2 && frames[0].type == MessageType::intent &&
|
|
frames[1].type == MessageType::environment_view);
|
|
CHECK(decode_environment_view({frames[1].type, frames[1].payload}));
|
|
// Whole-stream feed stops at the frame boundary.
|
|
FrameReader bulk;
|
|
const auto first = bulk.feed(stream);
|
|
CHECK(first && *first == frame_header_bytes + 36 && bulk.take() && !bulk.take());
|
|
// An oversized header is rejected after 12 bytes, before any payload is buffered.
|
|
FrameReader guard;
|
|
const auto huge = with_header(MessageType::environment_view, 1, 0, max_frame_payload + 1, 64);
|
|
CHECK(refused(guard.feed(huge), ProtocolError::oversized));
|
|
CHECK(refused(guard.feed(stream), ProtocolError::oversized) && !guard.take()); // Latched.
|
|
}
|
|
|
|
// Mutation fuzzing: decoding never crashes, and anything accepted re-encodes to the
|
|
// identical bytes (canonical, no hidden fields trusted from the wire).
|
|
void fuzz() {
|
|
std::mt19937_64 random{0x9E3779B97F4A7C15ULL};
|
|
const std::vector<Bytes> seeds{
|
|
*encode(Hello{0, Bytes(dev_grant_bytes, 7)}),
|
|
encode(Welcome{1, 11, 12, 13, 14, 15, 16, 17, 8, 8, 16}),
|
|
encode(IntentMessage{1, 2, 3, 4, 5, 6}),
|
|
encode(IntentResult{1, IntentStatus::outside_window, 2}),
|
|
encode(CloseReason::rate_limited),
|
|
encode(IntentOutcome{3, IntentFinality::committed_no_effect, 4, 5}),
|
|
encode(view_header, environment()),
|
|
encode(view_header, organisms(3))};
|
|
std::size_t accepted = 0;
|
|
for (int round = 0; round < 30000; ++round) {
|
|
auto bytes = seeds[random() % seeds.size()];
|
|
switch (random() % 4) {
|
|
case 0:
|
|
for (int flips = 1 + static_cast<int>(random() % 4); flips > 0; --flips)
|
|
bytes[random() % bytes.size()] ^= static_cast<std::uint8_t>(1U << (random() % 8));
|
|
break;
|
|
case 1:
|
|
bytes.resize(random() % bytes.size());
|
|
break;
|
|
case 2: // Mutate the payload only, keeping a valid header.
|
|
if (bytes.size() > frame_header_bytes)
|
|
bytes[frame_header_bytes + random() % (bytes.size() - frame_header_bytes)] =
|
|
static_cast<std::uint8_t>(random());
|
|
break;
|
|
default:
|
|
bytes.resize(random() % 96);
|
|
for (auto& b : bytes)
|
|
b = static_cast<std::uint8_t>(random());
|
|
}
|
|
const auto f = decode_frame(bytes);
|
|
if (!f)
|
|
continue;
|
|
Bytes again;
|
|
switch (f->type) {
|
|
case MessageType::hello:
|
|
if (const auto m = decode_hello(*f))
|
|
again = *encode(*m);
|
|
break;
|
|
case MessageType::welcome:
|
|
if (const auto m = decode_welcome(*f))
|
|
again = encode(*m);
|
|
break;
|
|
case MessageType::intent:
|
|
if (const auto m = decode_intent(*f))
|
|
again = encode(*m);
|
|
break;
|
|
case MessageType::intent_result:
|
|
if (const auto m = decode_intent_result(*f))
|
|
again = encode(*m);
|
|
break;
|
|
case MessageType::close:
|
|
if (const auto m = decode_close(*f))
|
|
again = encode(*m);
|
|
break;
|
|
case MessageType::intent_outcome:
|
|
if (const auto m = decode_intent_outcome(*f))
|
|
again = encode(*m);
|
|
break;
|
|
case MessageType::environment_view:
|
|
if (const auto m = decode_environment_view(*f))
|
|
again = encode(m->first, m->second);
|
|
break;
|
|
case MessageType::organism_view:
|
|
if (const auto m = decode_organism_view(*f))
|
|
again = encode(m->first, m->second);
|
|
break;
|
|
}
|
|
if (!again.empty()) {
|
|
++accepted;
|
|
CHECK(again == bytes);
|
|
}
|
|
FrameReader r;
|
|
if (const auto fed = r.feed(bytes))
|
|
CHECK(*fed <= bytes.size());
|
|
}
|
|
CHECK(accepted > 1000); // The mutations exercised acceptance paths as well.
|
|
std::printf("network_fuzz rounds=30000 accepted=%zu\n", accepted);
|
|
}
|
|
|
|
DevGrantKey key(std::uint8_t fill = 0x5A) {
|
|
std::array<std::uint8_t, dev_key_bytes> secret{};
|
|
secret.fill(fill);
|
|
secret[0] = 1;
|
|
auto k = DevGrantKey::import(DeploymentEnvironment::dev, secret);
|
|
CHECK(k);
|
|
return std::move(*k);
|
|
}
|
|
DevGrantClaims claims(std::uint64_t grant, std::uint64_t subject, std::uint64_t issued = 1000,
|
|
std::uint64_t expires = 1600) {
|
|
return {grant, subject, identity, 21, issued, expires};
|
|
}
|
|
|
|
void dev_grants() {
|
|
std::array<std::uint8_t, dev_key_bytes> secret{};
|
|
secret.fill(3);
|
|
CHECK(refused(DevGrantKey::import(DeploymentEnvironment::stage, secret),
|
|
GrantError::not_dev_environment));
|
|
CHECK(refused(DevGrantKey::import(DeploymentEnvironment::world0, secret),
|
|
GrantError::not_dev_environment));
|
|
CHECK(refused(DevGrantKey::import(DeploymentEnvironment::dev, std::span{secret}.first(31)),
|
|
GrantError::invalid_key));
|
|
secret.fill(0);
|
|
CHECK(
|
|
refused(DevGrantKey::import(DeploymentEnvironment::dev, secret), GrantError::invalid_key));
|
|
const auto k = key();
|
|
CHECK(refused(k.issue(claims(0, 1)), GrantError::invalid_claims));
|
|
CHECK(refused(k.issue(claims(1, 1, 5, 5)), GrantError::invalid_claims));
|
|
const auto grant = k.issue(claims(1, 2));
|
|
CHECK(grant);
|
|
const Bytes marker(grant->begin(), grant->begin() + 12);
|
|
CHECK(marker == Bytes({'E', 'V', 'O', '-', 'D', 'E', 'V', '-', 'O', 'N', 'L', 'Y'}));
|
|
const auto verified = k.verify(*grant);
|
|
CHECK(verified && verified->claims().grant_id == 1 && verified->claims().subject == 2 &&
|
|
verified->claims().world == identity && verified->claims().region == 21);
|
|
// Every single-bit change anywhere in the record fails authentication.
|
|
for (std::size_t i = 0; i < dev_grant_bytes; ++i) {
|
|
auto tampered = *grant;
|
|
tampered.at(i) ^= 0x01;
|
|
CHECK(refused(k.verify(tampered), GrantError::bad_tag));
|
|
}
|
|
CHECK(refused(key(0x77).verify(*grant), GrantError::bad_tag));
|
|
CHECK(refused(k.verify(std::span{*grant}.first(dev_grant_bytes - 1)), GrantError::malformed));
|
|
auto moved = key();
|
|
auto target = std::move(moved);
|
|
CHECK(target.verify(*target.issue(claims(5, 5))));
|
|
// Moved-from keys refuse use; checking that is the point.
|
|
// NOLINTNEXTLINE(bugprone-use-after-move)
|
|
CHECK(refused(moved.issue(claims(5, 5)), GrantError::invalid_key));
|
|
}
|
|
|
|
void dev_credentials() {
|
|
const auto now = std::chrono::system_clock::now();
|
|
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::stage, "dev.test", 30, now),
|
|
GrantError::not_dev_environment));
|
|
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::world0, "dev.test", 30, now),
|
|
GrantError::not_dev_environment));
|
|
for (const auto* name : {"", "a,CN=evil", "a b", "x\n"})
|
|
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, name, 30, now),
|
|
GrantError::invalid_claims));
|
|
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 0, now),
|
|
GrantError::invalid_claims));
|
|
CHECK(refused(generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 826, now),
|
|
GrantError::invalid_claims));
|
|
const auto made = generate_dev_credentials(DeploymentEnvironment::dev, "dev.test", 30, now);
|
|
CHECK(made && made->authority_pem.starts_with("-----BEGIN CERTIFICATE-----") &&
|
|
made->server_certificate_pem.starts_with("-----BEGIN CERTIFICATE-----") &&
|
|
made->server_key_pem.find("PRIVATE KEY") != std::string::npos);
|
|
CHECK(std::ranges::any_of(made->grant_key, [](std::uint8_t b) { return b != 0; }));
|
|
const auto text = format_grant_key(made->grant_key);
|
|
CHECK(text.size() == 64 && parse_grant_key(" " + text + "\n") == made->grant_key);
|
|
CHECK(refused(parse_grant_key(text.substr(1)), GrantError::invalid_key));
|
|
auto upper = text;
|
|
upper[0] = 'G';
|
|
CHECK(refused(parse_grant_key(upper), GrantError::invalid_key));
|
|
// The generated key is usable for grants.
|
|
CHECK(DevGrantKey::import(DeploymentEnvironment::dev, made->grant_key));
|
|
const auto a = random_grant_id(), b = random_grant_id();
|
|
CHECK(a && b && *a != 0 && *a != *b);
|
|
}
|
|
|
|
SessionAdmission admission(std::size_t max_sessions = 4, std::size_t remembered = 8) {
|
|
AdmissionConfig config;
|
|
config.world = identity;
|
|
config.region = 21;
|
|
config.epoch = 3;
|
|
config.max_sessions = max_sessions;
|
|
config.max_remembered_grants = remembered;
|
|
config.intent_window = 4;
|
|
config.window_ticks = 10;
|
|
config.max_intents_per_window = 5;
|
|
config.max_grant_lifetime = 900;
|
|
config.clock_skew = 30;
|
|
auto a = SessionAdmission::create(config);
|
|
CHECK(a);
|
|
return std::move(*a);
|
|
}
|
|
|
|
void admission_rules() {
|
|
auto bad = AdmissionConfig{};
|
|
CHECK(refused(SessionAdmission::create(bad), AdmissionError::invalid_config));
|
|
const auto k = key();
|
|
const auto verify = [&](DevGrantClaims c) {
|
|
auto g = k.verify(*k.issue(c));
|
|
CHECK(g);
|
|
return *g;
|
|
};
|
|
auto a = admission();
|
|
auto foreign = claims(1, 1);
|
|
foreign.world = {11, 99};
|
|
CHECK(refused(a.open(verify(foreign), 1100, 50, 40), AdmissionError::wrong_world));
|
|
foreign = claims(1, 1);
|
|
foreign.region = 22;
|
|
CHECK(refused(a.open(verify(foreign), 1100, 50, 40), AdmissionError::wrong_region));
|
|
CHECK(refused(a.open(verify(claims(1, 1, 1000, 2000)), 1100, 50, 40),
|
|
AdmissionError::invalid_lifetime));
|
|
CHECK(refused(a.open(verify(claims(1, 1)), 900, 50, 40), AdmissionError::not_yet_valid));
|
|
CHECK(refused(a.open(verify(claims(1, 1)), 1600, 50, 40), AdmissionError::expired));
|
|
const auto welcome = a.open(verify(claims(1, 1)), 975, 50, 40); // Within clock skew.
|
|
CHECK(welcome && welcome->session == 1 && welcome->epoch == 3 && welcome->region == 21 &&
|
|
welcome->world == 11 && welcome->frame == 12 && welcome->server_tick == 50 &&
|
|
welcome->commit_watermark == 40 && welcome->grant_expires_at == 1600);
|
|
// Grants are single use; a subject never holds two sessions.
|
|
CHECK(refused(a.open(verify(claims(1, 9)), 1100, 50, 40), AdmissionError::grant_reused));
|
|
CHECK(refused(a.open(verify(claims(2, 1)), 1100, 50, 40), AdmissionError::duplicate_subject));
|
|
CHECK(a.close(1) && refused(a.close(1), AdmissionError::unknown_session));
|
|
CHECK(refused(a.open(verify(claims(1, 1)), 1100, 50, 40), AdmissionError::grant_reused));
|
|
const auto again = a.open(verify(claims(2, 1)), 1100, 50, 40); // Reconnect: fresh grant.
|
|
CHECK(again && again->session == 2);
|
|
for (std::uint64_t s = 2; s <= 4; ++s)
|
|
CHECK(a.open(verify(claims(10 + s, 100 + s)), 1100, 50, 40));
|
|
CHECK(refused(a.open(verify(claims(20, 200)), 1100, 50, 40), AdmissionError::sessions_full));
|
|
auto small = admission(1, 1);
|
|
CHECK(small.open(verify(claims(1, 1)), 1100, 50, 40) && small.close(1));
|
|
CHECK(refused(small.open(verify(claims(2, 2)), 1100, 50, 40), AdmissionError::grants_full));
|
|
CHECK(small.open(verify(claims(3, 3, 1500, 2000)), 1600, 50, 40)); // Expired id forgotten.
|
|
|
|
// Intent admission for session 2.
|
|
const auto status = [&](IntentMessage m, std::uint64_t tick = 100, std::uint64_t now = 1100) {
|
|
return a.admit(m, tick, now).status;
|
|
};
|
|
CHECK(status({99, 3, 1, 100, 0, 0}) == IntentStatus::unknown_session);
|
|
CHECK(status({2, 2, 1, 100, 0, 0}) == IntentStatus::stale_epoch);
|
|
CHECK(status({2, 3, 1, 100, 0, 0}, 100, 1600) == IntentStatus::expired);
|
|
CHECK(status({2, 3, 1, 100, 1000, 1}) == IntentStatus::invalid_vector);
|
|
CHECK(status({2, 3, 1, 100, 0, -1001}) == IntentStatus::invalid_vector);
|
|
CHECK(status({2, 3, 0, 100, 0, 0}, 111) == IntentStatus::replayed);
|
|
CHECK(status({2, 3, 1, 110, 707, -707}, 111) == IntentStatus::outside_window);
|
|
CHECK(status({2, 3, 1, 116, 0, 0}, 111) == IntentStatus::outside_window);
|
|
const auto ok = a.admit({2, 3, 1, 115, 707, -707}, 111, 1100);
|
|
CHECK(ok.status == IntentStatus::admitted && ok.sequence == 1 && ok.server_tick == 111);
|
|
CHECK(status({2, 3, 1, 112, 0, 0}, 111) == IntentStatus::replayed);
|
|
// The window opened at tick 111 has had 5 attempts (rejections count too).
|
|
CHECK(status({2, 3, 2, 112, 0, 0}, 112) == IntentStatus::rate_limited);
|
|
CHECK(status({2, 3, 2, 120, 0, 0}, 120) == IntentStatus::rate_limited);
|
|
CHECK(status({2, 3, 2, 121, 0, 0}, 121) == IntentStatus::admitted); // New window.
|
|
CHECK(status({2, 3, 3, 122, 0, 0}, 122) == IntentStatus::admitted);
|
|
// A new ownership epoch makes every existing session stale.
|
|
CHECK(refused(a.advance_epoch(3), AdmissionError::invalid_config) && a.advance_epoch(4));
|
|
CHECK(status({2, 4, 4, 122, 0, 0}, 122) == IntentStatus::stale_epoch);
|
|
CHECK(status({2, 3, 4, 122, 0, 0}, 122) == IntentStatus::stale_epoch);
|
|
CHECK(a.close(2) && a.open(verify(claims(30, 1)), 1100, 121, 40)->epoch == 4);
|
|
}
|
|
} // namespace
|
|
|
|
void protocol_tests() {
|
|
round_trips();
|
|
frame_rejection();
|
|
reader();
|
|
fuzz();
|
|
dev_grants();
|
|
dev_credentials();
|
|
admission_rules();
|
|
}
|
|
} // namespace evo::network_test
|