#include "lan_session.hpp" #include "lan_wire.hpp" #include #include #include #include #include #include #include namespace { using namespace std::chrono_literals; using namespace std::string_view_literals; using namespace settlement; using namespace settlement::viewer; using Void::Testing::expect_throw; using Void::Testing::require; using Void::Testing::Test; constexpr auto hello_bytes = "\x0d\x00\x00\x00\x01\x0b\x00\x00\x00\x10\x32\x54\x76\x98\xba\xdc\xfe"sv; constexpr auto command_bytes = "\x23\x00\x00\x00\x03" "\x11\x22\x33\x44\x55\x66\x77\x88\x07" "\xfe\xff\xff\xff\x78\x56\x34\x12\xa5" "\x04\x03\x02\x01\xfc\xfc\xfd\xfe\x87\x96\xa5\xb4\xc3\xd2\xe1\xf0"sv; constexpr auto receipt_bytes = "\x0a\x00\x00\x00\x04\x04\x10\x32\x54\x76\x98\xba\xdc\xfe"sv; constexpr auto ping_bytes = "\x01\x00\x00\x00\x06"sv; struct LanTests : Void::Testing::Suite { [[= Void::Testing::tag("wire")]] Test test_protocol_11_fixed_messages(); [[= Void::Testing::tag("wire")]] Test test_fragmented_and_coalesced_frames(); [[= Void::Testing::tag("wire")]] Test test_frame_size_limits(); [[= Void::Testing::tag("lan")]] Test test_shared_colony_over_loopback(); }; Test LanTests::test_protocol_11_fixed_messages() { using namespace lan_wire; constexpr Hello hello{protocol_version, 0xfedcba9876543210ULL}; constexpr PlayerCommand command{ 1, 0x8877665544332211ULL, CommandKind::MoveObject, {-2, 0x12345678}, 0xa5, {0x01020304, -0x01020304}, 0xf0e1d2c3b4a59687ULL }; constexpr Receipt receipt{CommandStatus::InvalidTarget, 0xfedcba9876543210ULL}; std::string encoded; const auto hello_payload = encode_hello(hello); append_frame(encoded, Message::Hello, std::string_view(hello_payload)); require(encoded == hello_bytes); encoded.clear(); const auto command_payload = encode_command(command); append_frame(encoded, Message::Command, std::string_view(command_payload)); require(encoded == command_bytes); encoded.clear(); const auto receipt_payload = encode_receipt(receipt); append_frame(encoded, Message::Receipt, std::string_view(receipt_payload)); require(encoded == receipt_bytes); const auto hello_frame = next_frame(hello_bytes); require(hello_frame && hello_frame->message == Message::Hello && hello_frame->size == hello_bytes.size()); const auto decoded_hello = decode_hello(hello_frame->payload); require(decoded_hello && decoded_hello->version == 11 && decoded_hello->key == hello.key); require(!decode_hello(hello_frame->payload.substr(1))); require(!decode_hello(std::string(hello_frame->payload) + '\0')); const auto command_frame = next_frame(command_bytes); require(command_frame && command_frame->message == Message::Command && command_frame->size == command_bytes.size()); const auto decoded_command = decode_command(command_frame->payload); require(decoded_command.has_value()); require(decoded_command->player == 2); require(decoded_command->sequence == command.sequence); require(decoded_command->kind == command.kind); require(decoded_command->cell == command.cell); require(decoded_command->value == command.value); require(decoded_command->destination == command.destination); require(decoded_command->resident == command.resident); require(!decode_command(command_frame->payload.substr(1))); require(!decode_command(std::string(command_frame->payload) + '\0')); const auto receipt_frame = next_frame(receipt_bytes); require(receipt_frame && receipt_frame->message == Message::Receipt && receipt_frame->size == receipt_bytes.size()); const auto decoded_receipt = decode_receipt(receipt_frame->payload); require(decoded_receipt && decoded_receipt->status == receipt.status && decoded_receipt->next_sequence == receipt.next_sequence); require(!decode_receipt(receipt_frame->payload.substr(1))); require(!decode_receipt(std::string(receipt_frame->payload) + '\0')); } Test LanTests::test_fragmented_and_coalesced_frames() { using namespace lan_wire; std::string received(command_bytes.substr(0, 3)); require(!next_frame(received)); received.append(command_bytes.substr(3, 8)); require(!next_frame(received)); received.append(command_bytes.substr(11)); received.append(ping_bytes); received.append(hello_bytes.substr(0, 5)); std::size_t consumed = 0; { const auto command = next_frame(received); require(command && command->message == Message::Command && command->size == command_bytes.size()); require(command->payload.data() == received.data() + 5); require(command->payload == command_bytes.substr(5)); const auto ping = next_frame(std::string_view(received).substr(command->size)); require(ping && ping->message == Message::Ping && ping->payload.empty() && ping->size == ping_bytes.size()); consumed = command->size + ping->size; require(!next_frame(std::string_view(received).substr(consumed))); } received.erase(0, consumed); received.append(hello_bytes.substr(5)); const auto hello = next_frame(received); require(hello && hello->message == Message::Hello && hello->size == received.size()); require(hello->payload == hello_bytes.substr(5)); } Test LanTests::test_frame_size_limits() { using namespace lan_wire; constexpr auto empty_prefix = "\x00\x00\x00\x00"sv; constexpr auto max_prefix = "\x00\x00\x40\x00"sv; constexpr auto oversized_prefix = "\x01\x00\x40\x00"sv; require(!next_frame(empty_prefix.substr(0, 3))); require(!next_frame(oversized_prefix.substr(0, 3))); expect_throw([] { static_cast(next_frame(empty_prefix)); }); expect_throw([] { static_cast(next_frame(oversized_prefix)); }); require(!next_frame(max_prefix)); std::string output; append_frame(output, Message::Ping); require(output == ping_bytes); std::string payload(4 * 1024 * 1024 - 1, '\xa5'); output.clear(); append_frame(output, Message::Snapshot, payload); require(output.starts_with(max_prefix)); { const auto snapshot = next_frame(output); require(snapshot && snapshot->message == Message::Snapshot && snapshot->size == output.size()); require(snapshot->payload == payload); payload.front() = '\0'; require(snapshot->payload.front() == '\xa5'); } payload.push_back('\0'); expect_throw([&] { append_frame(output, Message::Snapshot, payload); }); require(output.size() == ping_bytes.size() + payload.size() - 1); } Test LanTests::test_shared_colony_over_loopback() { auto authoritative = make_first_colony(4); auto client_view = make_first_colony(4); for (int i = 0; i < 25; ++i) authoritative->step(); LanSession host; LanSession guest; host.host(0, true); const auto port = host.port(); const auto code = host.join_code(); auto wrong_code = code; wrong_code.front() = wrong_code.front() == 'f' ? 'e' : 'f'; const auto pump_until = [&](auto success, const char* message) { const auto deadline = std::chrono::steady_clock::now() + 3s; while (std::chrono::steady_clock::now() < deadline) { static_cast(host.poll(*authoritative)); if (auto snapshot = guest.poll(*client_view)) client_view = std::move(snapshot); if (success()) return; std::this_thread::sleep_for(1ms); } throw std::runtime_error(message); }; guest.join("127.0.0.1", wrong_code, port); pump_until([&] { return guest.state() == LanState::GuestDisconnected; }, "a guest with the wrong code was not disconnected"); require(host.state() == LanState::Hosting); guest.join("127.0.0.1", code, port); pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not join"); require(client_view->tick() == 25 && client_view->agents().size() == authoritative->agents().size()); require(!guest.submit(CommandKind::SetPaused, {}, 1)); require(guest.submit(CommandKind::PlaceBed, {16, 18}, static_cast(BedBase::Stone))); pump_until([&] { return client_view->production_orders().size() == 1 && client_view->production_orders().front().position == GridPosition{16, 18} && client_view->production_orders().front().bed_base == BedBase::Stone; }, "the guest's bed was not applied and synchronized"); require(authoritative->production_orders().front().bed_base == BedBase::Stone); require(guest.submit(CommandKind::PlaceBed, {16, 18}, 0)); pump_until([&] { const auto rejected = guest.take_rejection(); return rejected && rejected->status == CommandStatus::InvalidTarget && rejected->kind == CommandKind::PlaceBed && rejected->cell == GridPosition{16, 18} && rejected->value == 0; }, "a conflicting guest command did not return a rejection"); require(authoritative->production_orders().size() == 1); guest.leave(); pump_until([&] { return host.state() == LanState::Hosting; }, "the guest slot was not released"); for (int i = 0; i < 10; ++i) authoritative->step(); guest.join("127.0.0.1", code, port); pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not rejoin"); require(client_view->tick() == authoritative->tick() && client_view->next_command_sequence(2) == 2 && client_view->production_orders().size() == 1); require(guest.submit(CommandKind::SetForaging, {14, 12}, 1)); pump_until([&] { if (client_view->next_command_sequence(2) != 3) return false; for (const auto& node : client_view->resource_nodes()) { if (node.position == GridPosition{14, 12}) return node.foraging_enabled; } return false; }, "the reconnected guest's command did not synchronize"); const auto owner_sleeping = [](const World& simulation) { for (const auto& agent : simulation.agents()) { if (agent.state == AgentState::Sleeping && simulation.assigned_bed(agent.id) != nullptr) return true; } return false; }; for (int i = 0; i < 600 && !owner_sleeping(*authoritative); ++i) authoritative->step(); require(owner_sleeping(*authoritative)); pump_until([&] { return client_view->tick() == authoritative->tick() && owner_sleeping(*client_view); }, "the host's sleep state did not reach the guest"); const GridPosition field{14, 15}; const GridPosition new_field{14, 17}; require(guest.submit(CommandKind::PlaceBerryField, field, 0)); const auto has_field = [&](GridPosition position) { for (const auto& node : client_view->resource_nodes()) { if (node.site == ResourceSite::BerryField && node.position == position) return true; } return false; }; pump_until([&] { return has_field(field); }, "the planted field was not synchronized"); require(guest.submit(CommandKind::MoveObject, field, 0, new_field)); pump_until([&] { return has_field(new_field) && !has_field(field); }, "the move destination was not synchronized"); require(guest.submit(CommandKind::DemolishObject, new_field, 0)); pump_until([&] { return !has_field(new_field); }, "demolishing a field was not synchronized"); const auto resident = authoritative->agents().back().id; require(guest.submit(CommandKind::AssignProfession, {}, static_cast(Profession::Miner), {}, resident)); pump_until([&] { return client_view->agents().back().id == resident && client_view->agents().back().profession == Profession::Miner; }, "the assigned profession was not synchronized"); require(guest.submit(CommandKind::PlaceFlaxField, field, 0)); pump_until([&] { return client_view->resource_nodes().back().site == ResourceSite::FlaxField && client_view->resource_nodes().back().position == field; }, "the flax field did not arrive in the guest snapshot"); require(guest.submit(CommandKind::PlaceLoom, new_field, 0)); pump_until([&] { const auto& orders = client_view->production_orders(); return orders.size() >= 2 && orders.back().recipe == RecipeId::CraftLoom && orders[orders.size() - 2].recipe == RecipeId::InstallLoom && orders[orders.size() - 2].position == new_field; }, "the loom installation and workshop order did not arrive in the guest snapshot"); for (int i = 0; i < 160 && authoritative->resource_nodes().back().reserved_workers == 0; ++i) { authoritative->step(); } require(authoritative->resource_nodes().back().needs_tending() && authoritative->resource_nodes().back().reserved_workers == 1); pump_until([&] { return client_view->tick() == authoritative->tick() && client_view->resource_nodes().back().needs_tending() && client_view->resource_nodes().back().reserved_workers == 1; }, "a snapshot lost the resident's sowing assignment"); RoomPlan room{}; bool found_room = false; for (int y = 2; y < 29 && !found_room; ++y) { for (int x = 2; x < 29 && !found_room; ++x) { const auto candidate = make_room_plan({x, y}, {x + 2, y + 2}); if (!authoritative->can_place_room(candidate)) continue; room = candidate; found_room = true; } } require(found_room); const auto entrance = static_cast(room.entrance); const auto previous_size = authoritative->production_orders().size(); require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last)); pump_until([&] { return client_view->production_orders().size() == previous_size + 10 && client_view->production_orders().back().recipe == RecipeId::CraftWoodenDoor; }, "snapshot did not carry the room and carpenter's order"); require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last)); pump_until([&] { const auto rejected = guest.take_rejection(); return rejected && rejected->kind == CommandKind::PlaceRoom && rejected->cell == room.first && rejected->destination == room.last && rejected->value == entrance && rejected->status == CommandStatus::InvalidTarget; }, "room rejection lost the geometry needed to restore the draft"); require(authoritative->production_orders().size() == previous_size + 10); } } // namespace