#include "evo/network/session_protocol.hpp" #include #include #include #include #include namespace { using namespace evo::network; void check(bool value, std::source_location at = std::source_location::current()) { if (!value) throw std::runtime_error("cellular intent protocol line " + std::to_string(at.line())); } #define CHECK(...) check(static_cast(__VA_ARGS__)) void round_trip_and_version_gate() { const CellularIntention targeted{17, 23, 29, 31, -707, 707, true, 4'000, 5'000}; const SessionMessage message{targeted}; const auto bytes = encode_session(message); CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 48); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v6); CHECK(std::get(*decoded) == targeted); CHECK(encode_session(*decoded) == bytes); CHECK(encode_session(message, session_protocol_v5).empty()); const CellularIntention no_target{1, 2, 3, 4, 0, -1, false, 0, 0}; const auto no_target_bytes = encode_session(SessionMessage{no_target}); CHECK(std::get(*decode_session(no_target_bytes)) == no_target); } void malformed_frames_are_refused() { const auto bytes = encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, true, 1'000, 2'000}}); for (std::size_t size = 0; size < bytes.size(); ++size) { const auto truncated = decode_session(std::span{bytes}.first(size)); CHECK(!truncated); CHECK(truncated.error() == (size < session_header_bytes ? SessionCodecError::truncated : SessionCodecError::wrong_length)); } auto bad = bytes; bad.at(6) = 255; // Unknown message type. const auto wrong_type = decode_session(bad); CHECK(!wrong_type && wrong_type.error() == SessionCodecError::unknown_type); bad = bytes; bad.at(session_header_bytes + 36) = 2; const auto bad_flag = decode_session(bad); CHECK(!bad_flag && bad_flag.error() == SessionCodecError::invalid_value); bad = bytes; bad.at(session_header_bytes + 38) = 1; const auto bad_reserved = decode_session(bad); CHECK(!bad_reserved && bad_reserved.error() == SessionCodecError::invalid_value); bad = encode_session(SessionMessage{CellularIntention{1, 2, 3, 4, 0, 0, false, 0, 0}}); bad.at(session_header_bytes + 40) = 1; // Coordinates must be zero when target is absent. const auto noncanonical = decode_session(bad); CHECK(!noncanonical && noncanonical.error() == SessionCodecError::invalid_value); bad = bytes; bad.at(4) = static_cast(session_protocol_v5); const auto old_version = decode_session(bad); CHECK(!old_version && old_version.error() == SessionCodecError::unsupported_version); CHECK(encode_session(SessionMessage{CellularIntention{1, 2, 3, 4}}, 7).empty()); } void v7_reliable_action_is_stable_and_strict() { const CellularAction action{17, 23, 29, 31, -707, 707, true, 4'000, 5'000}; const auto bytes = encode_session(SessionMessage{action}); CHECK(bytes.size() == session_header_bytes + 48); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v7); CHECK(std::get(*decoded) == action); CHECK(encode_session(*decoded) == bytes); CHECK(encode_session(SessionMessage{action}, session_protocol_v6).empty()); const CellularActionInput input{31, -707, 707, true, 4'000, 5'000}; const auto canonical = encode_cellular_action_input(input); CHECK(canonical.size() == cellular_action_input_bytes); CHECK(decode_cellular_action_input(canonical) == input); const CellularAction retry{99, 101, 29, 31, -707, 707, true, 4'000, 5'000}; CHECK(encode_cellular_action_input({retry.tick, retry.move_x, retry.move_y, retry.has_intake_target, retry.intake_x_um, retry.intake_y_um}) == canonical); const auto outcome = ActionOutcome{29, ActionResult::committed, 31, 41}; const auto outcome_bytes = encode_session(SessionMessage{outcome}, session_protocol_v7); CHECK(std::get(*decode_session(outcome_bytes)) == outcome); CHECK(encode_session(SessionMessage{outcome}, session_protocol_v6).empty()); auto bad = bytes; std::fill(bad.begin() + static_cast(session_header_bytes + 16), bad.begin() + static_cast(session_header_bytes + 24), std::uint8_t{0}); CHECK(!decode_session(bad)); // Stable action id cannot be zero. bad = bytes; bad[session_header_bytes + 37] = 1; CHECK(!decode_session(bad)); // Reserved bytes are canonical. bad = encode_session(SessionMessage{CellularAction{1, 2, 3, 4, 0, 0, false, 0, 0}}); bad[session_header_bytes + 44] = 1; CHECK(!decode_session(bad)); // An absent patch has no coordinates. auto malformed_input = canonical; malformed_input[12] = 2; CHECK(!decode_cellular_action_input(malformed_input)); malformed_input = canonical; malformed_input[13] = 1; CHECK(!decode_cellular_action_input(malformed_input)); malformed_input = canonical; // The canonical biological input itself requires a nonzero tick. std::fill(malformed_input.begin(), malformed_input.begin() + 8, std::uint8_t{0}); CHECK(!decode_cellular_action_input(malformed_input)); } // ADR-0125: target kind 3 (bud) exists only on v10, without coordinates, exclusive with // a patch target and with engulfment. void v10_bud_action_is_versioned_and_exclusive() { CellularAction bud{17, 23, 29, 31, 0, 0, false, 0, 0}; bud.bud = true; const auto bytes = encode_session(SessionMessage{bud}, session_protocol_v10); CHECK(bytes.size() == session_header_bytes + 48 && bytes[session_header_bytes + 36] == 3); const auto decoded = decode_session(bytes); CHECK(decoded && std::get(*decoded) == bud); CHECK(encode_session(SessionMessage{bud}, session_protocol_v9).empty()); CHECK(encode_session(SessionMessage{bud}).empty()); auto feeding = bud; feeding.has_intake_target = true; CHECK(encode_session(SessionMessage{feeding}, session_protocol_v10).empty()); auto engulfing = bud; engulfing.engulf = true; CHECK(encode_session(SessionMessage{engulfing}, session_protocol_v10).empty()); auto as_v9 = bytes; as_v9[4] = static_cast(session_protocol_v9); CHECK(!decode_session(as_v9)); auto kind_four = bytes; kind_four[session_header_bytes + 36] = 4; CHECK(!decode_session(kind_four)); CellularActionInput input{31, 0, 0, false, 0, 0, false, true}; const auto canonical = encode_cellular_action_input(input); CHECK(canonical[12] == 3 && decode_cellular_action_input(canonical) == input); auto placed = canonical; placed[16] = 1; CHECK(!decode_cellular_action_input(placed)); } // ADR-0124: target kind 2 (engulf) exists only on v10, without coordinates or a patch. void v10_engulf_action_is_versioned_and_exclusive() { CellularAction engulf{17, 23, 29, 31, 5, -5, false, 0, 0}; engulf.engulf = true; const auto bytes = encode_session(SessionMessage{engulf}, session_protocol_v10); CHECK(bytes.size() == session_header_bytes + 48 && bytes[session_header_bytes + 36] == 2); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v10 && std::get(*decoded) == engulf); CHECK(encode_session(SessionMessage{engulf}, session_protocol_v9).empty()); CHECK(encode_session(SessionMessage{engulf}).empty()); // The default v7 has no engulf. auto with_patch = engulf; with_patch.has_intake_target = true; CHECK(encode_session(SessionMessage{with_patch}, session_protocol_v10).empty()); // A v9 decoder refuses kind 2 even with a valid layout. auto as_v9 = bytes; as_v9[4] = static_cast(session_protocol_v9); CHECK(!decode_session(as_v9)); // Coordinates are reserved for a patch target. auto placed = bytes; placed[session_header_bytes + 40] = 1; CHECK(!decode_session(placed)); // A patch action on v10 is unchanged. const CellularAction feed{17, 23, 30, 31, 0, 0, true, 1'000, 2'000}; const auto feed_bytes = encode_session(SessionMessage{feed}, session_protocol_v10); CHECK(std::get(*decode_session(feed_bytes)) == feed); CellularActionInput input{31, 5, -5, false, 0, 0, true}; const auto canonical = encode_cellular_action_input(input); CHECK(canonical[12] == 2 && decode_cellular_action_input(canonical) == input); auto coordinated = canonical; coordinated[16] = 1; CHECK(!decode_cellular_action_input(coordinated)); auto unknown_kind = canonical; unknown_kind[12] = 4; // Kind 3 is a bud since ADR-0125. CHECK(!decode_cellular_action_input(unknown_kind)); } void v8_perception_is_bounded_and_canonical() { CellularPerception perception{19, 42, {}, std::nullopt}; perception.patches = { {1'000, 2'000, 3, {4, 5, 6, 7}}, {1'000, 3'000, 8, {9, 10, 11, 12}}, }; const auto bytes = encode_session(SessionMessage{perception}); CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 18 + std::size_t{2} * 48); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v8); CHECK(std::get(*decoded) == perception); CHECK(encode_session(*decoded) == bytes); CHECK(encode_session(SessionMessage{perception}, session_protocol_v7).empty()); CHECK(!decode_session(std::span{bytes}.first(bytes.size() - 1))); auto reversed = perception; std::ranges::reverse(reversed.patches); CHECK(encode_session(SessionMessage{reversed}).empty()); auto duplicate = perception; duplicate.patches[1] = duplicate.patches[0]; CHECK(encode_session(SessionMessage{duplicate}).empty()); const CellularPerception invalid{19, 0, {perception.patches[0]}, std::nullopt}; CHECK(encode_session(SessionMessage{invalid}).empty()); const CellularPerception cleared{19, 0, {}, std::nullopt}; const auto clear_bytes = encode_session(SessionMessage{cleared}); CHECK(std::get(*decode_session(clear_bytes)) == cleared); auto oversized = perception; oversized.patches.clear(); for (std::size_t i = 0; i <= max_cellular_perceived_patches; ++i) oversized.patches.push_back({static_cast(i), 0, 0, {0, 0, 0, 0}}); CHECK(encode_session(SessionMessage{oversized}).empty()); } void v9_perception_carries_only_valid_owner_diagnostics() { CellularPerception perception{27, 82, {{2'000, 3'000, 17, {4, 5, 6, 7}}}, std::nullopt}; perception.diagnostics = CellularLifeDiagnostics{ 900, {11, 12, 13, 14}, 21, 8, CellularMovementResult::moved, CellularIntakeResult::acquired, true}; const auto bytes = encode_session(SessionMessage{perception}); CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 82 + 48); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v9); CHECK(std::get(*decoded) == perception); CHECK(encode_session(*decoded) == bytes); auto malformed = bytes; malformed[session_header_bytes + 77] = 1; // Reserved byte. CHECK(!decode_session(malformed)); malformed = bytes; malformed[session_header_bytes + 74] = 0xFF; // Unknown movement result. CHECK(!decode_session(malformed)); malformed = bytes; malformed[session_header_bytes + 76] = 0; // Costs/status require a tick result. CHECK(!decode_session(malformed)); auto missing = perception; missing.diagnostics.reset(); CHECK(encode_session(SessionMessage{missing}, session_protocol_v9).empty()); CHECK(encode_session(SessionMessage{perception}, session_protocol_v8).empty()); auto cleared = CellularPerception{27, 0, {}, std::nullopt}; const auto clear_bytes = encode_session(SessionMessage{cleared}, session_protocol_v9); CHECK(!clear_bytes.empty()); CHECK(std::get(*decode_session(clear_bytes)) == cleared); } void v10_mutation_choice_is_a_strict_reliable_action() { const CellularMutationAction action{17, 23, 29, 53, 37, 41, 43, 47}; const auto bytes = encode_session(SessionMessage{action}); CHECK(!bytes.empty() && bytes.size() == session_header_bytes + 64); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v10); CHECK(std::get(*decoded) == action); CHECK(encode_session(*decoded) == bytes); CHECK(encode_session(SessionMessage{action}, session_protocol_v9).empty()); auto malformed = bytes; std::fill(malformed.begin() + static_cast(session_header_bytes + 16), malformed.begin() + static_cast(session_header_bytes + 24), std::uint8_t{0}); CHECK(!decode_session(malformed)); // The action id is the retry identity. malformed = bytes; malformed[session_header_bytes + 58] = 1; CHECK(!decode_session(malformed)); // Reserved bytes stay canonical. malformed = bytes; std::fill(malformed.begin() + static_cast(session_header_bytes + 48), malformed.begin() + static_cast(session_header_bytes + 56), std::uint8_t{0xFF}); CHECK(!decode_session(malformed)); // Offer must precede the scheduled tick. malformed = bytes; malformed[4] = static_cast(session_protocol_v9); CHECK(!decode_session(malformed)); // v1-v9 do not know this message. const ActionOutcome outcome{29, ActionResult::committed, 31, 41}; const auto outcome_bytes = encode_session(SessionMessage{outcome}, session_protocol_v10); CHECK(!outcome_bytes.empty()); CHECK(std::get(*decode_session(outcome_bytes)) == outcome); } // 11.4d: the owner's offer lists unranked candidates in operator order with their tradeoffs. void v10_mutation_offer_is_owner_only_and_canonical() { const CellularMutationOffer offer{ 120, 9, 2, 100, {{3, 1, 0, 40, 150'000, 9'000}, {7, 2, 2, 1, 160'000, 9'500}}}; const auto bytes = encode_session(SessionMessage{offer}); CHECK(bytes.size() == session_header_bytes + 40 + (std::size_t{2} * 24)); std::uint16_t version{}; const auto decoded = decode_session(bytes, &version); CHECK(decoded && version == session_protocol_v10); CHECK(std::get(*decoded) == offer); CHECK(encode_session(SessionMessage{offer}, session_protocol_v9).empty()); const CellularMutationOffer cleared{121, 9, 0, 0, {}}; const auto clear_bytes = encode_session(SessionMessage{cleared}); CHECK(!clear_bytes.empty() && std::get(*decode_session(clear_bytes)) == cleared); auto unordered = offer; std::swap(unordered.candidates[0], unordered.candidates[1]); CHECK(encode_session(SessionMessage{unordered}).empty()); // Stable order, never a rank. auto future = offer; future.offer_tick = 121; CHECK(encode_session(SessionMessage{future}).empty()); auto stale_clear = cleared; stale_clear.event_index = 2; CHECK(encode_session(SessionMessage{stale_clear}).empty()); auto malformed = bytes; malformed[session_header_bytes + 40 + 2] = 7; // No eighth capability. CHECK(!decode_session(malformed)); malformed = bytes; malformed[session_header_bytes + 34] = 1; // Reserved bytes stay canonical. CHECK(!decode_session(malformed)); malformed = bytes; malformed.pop_back(); CHECK(!decode_session(malformed)); } } // namespace int main() { try { round_trip_and_version_gate(); malformed_frames_are_refused(); v7_reliable_action_is_stable_and_strict(); v10_engulf_action_is_versioned_and_exclusive(); v10_bud_action_is_versioned_and_exclusive(); v8_perception_is_bounded_and_canonical(); v9_perception_carries_only_valid_owner_diagnostics(); v10_mutation_choice_is_a_strict_reliable_action(); v10_mutation_offer_is_owner_only_and_canonical(); std::puts( "PASS cellular protocol: v6 intention, v7 action, v8-v10 perception/action framing"); } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; } }