263 lines
13 KiB
C++
263 lines
13 KiB
C++
// Synthetic client-local Edit Mode layout storage and wire regression.
|
|
#include "../native/account_data_store.hpp"
|
|
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <random>
|
|
#include <stdexcept>
|
|
#include <thread>
|
|
|
|
using gateway::Bytes;
|
|
using gateway::Guid128;
|
|
namespace ad = gateway::account_data;
|
|
namespace fs = std::filesystem;
|
|
|
|
static void check(bool condition, const char* message) {
|
|
if (!condition) throw std::runtime_error(message);
|
|
}
|
|
|
|
struct Scratch {
|
|
fs::path root;
|
|
Scratch() {
|
|
std::random_device rd;
|
|
for (int i = 0; i < 32; ++i) {
|
|
auto path = fs::temp_directory_path() / ("gateway-layout-test-" + std::to_string(rd()));
|
|
if (fs::create_directory(path)) {
|
|
root = std::move(path);
|
|
return;
|
|
}
|
|
}
|
|
throw std::runtime_error("cannot create private test directory");
|
|
}
|
|
~Scratch() {
|
|
std::error_code ec;
|
|
fs::remove_all(root, ec);
|
|
}
|
|
};
|
|
|
|
static Bytes compress_fixture(const Bytes& plaintext, int level) {
|
|
const auto size = static_cast<uLong>(plaintext.size());
|
|
uLongf count = compressBound(size);
|
|
Bytes result(count);
|
|
check(compress2(result.data(), &count, plaintext.data(), size, level) == Z_OK, "fixture compression");
|
|
result.resize(count);
|
|
return result;
|
|
}
|
|
|
|
static Bytes zipped(size_t size, int level = Z_DEFAULT_COMPRESSION) {
|
|
Bytes plaintext(size);
|
|
for (size_t i = 0; i < plaintext.size(); ++i) plaintext[i] = uint8_t(32 + i % 83);
|
|
return compress_fixture(plaintext, level);
|
|
}
|
|
|
|
// Repeated deterministic noise yields ordinary compressed data at the two target wire lengths.
|
|
static ad::Entry compressed_layout(size_t target_length, int64_t time) {
|
|
uint32_t state = 3214567;
|
|
Bytes noise(target_length + 64);
|
|
for (auto& byte : noise) {
|
|
state = 1664525u * state + 1013904223u;
|
|
byte = uint8_t(state >> 24);
|
|
}
|
|
// Different zlib builds may choose different block boundaries for the same prefix.
|
|
for (size_t count = 1; count <= noise.size(); ++count) {
|
|
std::span<const uint8_t> seed(noise.data(), count);
|
|
Bytes plain;
|
|
for (int repeat = 0; repeat < 3; ++repeat) gateway::append(plain, seed);
|
|
auto compressed = compress_fixture(plain, 6);
|
|
if (compressed.size() == target_length && compressed.size() < plain.size())
|
|
return {time, static_cast<uint32_t>(plain.size()), std::move(compressed)};
|
|
}
|
|
throw std::runtime_error("cannot construct synthetic update at target length");
|
|
}
|
|
|
|
static Bytes update(const Guid128& guid, uint32_t type, const ad::Entry& entry) {
|
|
Bytes b;
|
|
gateway::le(b, uint64_t(entry.time), 8);
|
|
gateway::le(b, entry.size, 4);
|
|
gateway::append(b, gateway::pack_guid(guid));
|
|
gateway::le(b, type, 4);
|
|
gateway::le(b, entry.compressed.size(), 4);
|
|
gateway::append(b, entry.compressed);
|
|
return b;
|
|
}
|
|
|
|
static Bytes request(const Guid128& guid, uint32_t type) {
|
|
Bytes b = gateway::pack_guid(guid);
|
|
gateway::le(b, type, 4);
|
|
return b;
|
|
}
|
|
|
|
static void expect_ack(const std::optional<ad::Reply>& r, const Guid128& guid, uint32_t type, uint32_t result) {
|
|
check(r && r->opcode == ad::CompleteOpcode, "missing update acknowledgment");
|
|
gateway::Reader reader(r->body);
|
|
check(std::ranges::equal(gateway::unpack_guid(reader, 16), guid) && reader.le(4) == type &&
|
|
reader.le(4) == result, "acknowledgment player/type/result");
|
|
reader.end();
|
|
}
|
|
|
|
static void expect_data(const std::optional<ad::Reply>& r, const Guid128& guid, uint32_t type,
|
|
const ad::Entry& e) {
|
|
check(r && r->opcode == ad::DataOpcode, "missing account data reply");
|
|
gateway::Reader reader(r->body);
|
|
check(int64_t(reader.le(8)) == e.time && reader.le(4) == e.size &&
|
|
std::ranges::equal(gateway::unpack_guid(reader, 16), guid) && reader.le(4) == type,
|
|
"layout time, length, session player or type");
|
|
auto count = reader.le(4);
|
|
const Bytes expected = e.size ? e.compressed : Bytes(compressBound(0), 0);
|
|
check(reader.take(size_t(count)) == expected, "layout bytes on reload");
|
|
reader.end();
|
|
}
|
|
|
|
template <typename F> static void expect_error(F call) {
|
|
try { call(); }
|
|
catch (const gateway::Error&) { return; }
|
|
throw std::runtime_error("accepted malformed wire framing");
|
|
}
|
|
|
|
int main() {
|
|
Scratch scratch;
|
|
constexpr uint32_t account = 0x1234abcd;
|
|
constexpr auto global = ad::GlobalEditMode;
|
|
constexpr auto character = ad::CharacterEditMode;
|
|
Guid128 none{}, player{}, alt{};
|
|
player[0] = 0x2a; player[8] = 1; player[15] = 8;
|
|
alt = player; ++alt[0];
|
|
auto send = [&](uint32_t opcode, const Bytes& body, std::span<const uint8_t> selected = {}) {
|
|
return ad::handle(account, opcode, body, selected, scratch.root);
|
|
};
|
|
const ad::Entry first{41, 60, zipped(60)}, second{42, 984, zipped(984)};
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, first)), none, global, 0);
|
|
expect_data(send(ad::RequestOpcode, request(none, global)), none, global, first);
|
|
expect_ack(send(ad::UpdateOpcode, update(player, global, second), player), player, global, 0);
|
|
expect_data(send(ad::RequestOpcode, request(none, global), alt), alt, global, second);
|
|
check(ad::stored_time(account, global, none, scratch.root) == second.time, "global startup time");
|
|
expect_data(ad::handle(account + 1, ad::RequestOpcode, request(none, global), {}, scratch.root),
|
|
none, global, {});
|
|
|
|
// Both complete CMSG bodies have their observed lengths without using captured client data.
|
|
for (auto wire_size : {60u, 984u}) {
|
|
auto e = compressed_layout(wire_size - update(player, character, {}).size(), wire_size);
|
|
auto b = update(player, character, e);
|
|
check(b.size() == wire_size, "synthetic update length");
|
|
expect_ack(send(ad::UpdateOpcode, b, player), player, character, 0);
|
|
expect_data(send(ad::RequestOpcode, request(player, character), player), player, character, e);
|
|
check(ad::stored_time(account, character, player, scratch.root) == e.time, "character startup time");
|
|
}
|
|
expect_data(send(ad::RequestOpcode, request(alt, character), alt), alt, character, {});
|
|
check(!send(ad::UpdateOpcode, update(player, character, first)), "unspecified character accepted");
|
|
check(!send(ad::UpdateOpcode, update(none, character, first), none), "empty character accepted");
|
|
check(!send(ad::UpdateOpcode, update(alt, character, first), player), "other character accepted");
|
|
check(!send(ad::RequestOpcode, request(alt, character), player), "other character read");
|
|
check(!ad::handle(0, ad::RequestOpcode, request(none, global), {}, scratch.root), "missing account accepted");
|
|
// #80: every type is stored now. Global config (0) and bindings (2) round-trip; a per-character type (3, 7)
|
|
// still needs the selected character.
|
|
for (uint32_t t : {0u, 2u}) {
|
|
expect_ack(send(ad::UpdateOpcode, update(none, t, first)), none, t, 0);
|
|
check(ad::stored_time(account, t, none, scratch.root) == first.time, "global type startup time");
|
|
}
|
|
check(!send(ad::RequestOpcode, request(none, 7), none), "per-character request without character handled");
|
|
check(!send(ad::UpdateOpcode, update(alt, 3, first), player), "other character bindings accepted");
|
|
expect_ack(send(ad::UpdateOpcode, update(player, 3, first), player), player, 3, 0);
|
|
check(ad::stored_time(account, 3, player, scratch.root) == first.time, "character bindings startup time");
|
|
check(ad::per_character(3) && !ad::per_character(2) && ad::per_character(14) && !ad::per_character(13), "type masks");
|
|
|
|
// Report 7e7116: the client saves per-character config while logging out or quitting, after the world
|
|
// session ended. The last in-world character still owns its data; no other guid is accepted.
|
|
{
|
|
ad::CharacterOwner owner;
|
|
check(owner.current(false, player).empty(), "character owned before entering world");
|
|
check(std::ranges::equal(owner.current(true, player), player), "in-world character not owner");
|
|
auto after_logout = owner.current(false, {});
|
|
expect_ack(send(ad::UpdateOpcode, update(player, 1, second), after_logout), player, 1, 0);
|
|
check(ad::stored_time(account, 1, player, scratch.root) == second.time, "logout config save lost");
|
|
check(!send(ad::UpdateOpcode, update(alt, 1, first), after_logout), "other character saved after logout");
|
|
check(std::ranges::equal(owner.current(true, alt), alt), "next character not owner");
|
|
check(!send(ad::UpdateOpcode, update(player, 1, first), owner.current(false, {})), "previous character kept ownership");
|
|
}
|
|
|
|
auto invalid = [&](const ad::Entry& bad) {
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, bad)), none, global, 1);
|
|
expect_data(send(ad::RequestOpcode, request(none, global)), none, global, second);
|
|
};
|
|
invalid({-1, second.size, second.compressed});
|
|
invalid({1, 3, {}});
|
|
invalid({1, 3, {1, 2, 3}});
|
|
invalid({1, second.size - 1, second.compressed});
|
|
invalid({1, second.size + 1, second.compressed});
|
|
auto broken = second;
|
|
broken.compressed.back() ^= 0xff;
|
|
invalid(broken);
|
|
broken = second;
|
|
broken.compressed.pop_back();
|
|
invalid(broken);
|
|
broken = second;
|
|
broken.compressed.push_back(0);
|
|
invalid(broken);
|
|
broken = second;
|
|
gateway::append(broken.compressed, first.compressed);
|
|
invalid(broken);
|
|
auto extra = update(none, global, second);
|
|
extra.push_back(0);
|
|
expect_error([&] { send(ad::UpdateOpcode, extra); });
|
|
auto short_body = update(none, global, first);
|
|
short_body.pop_back();
|
|
expect_error([&] { send(ad::UpdateOpcode, short_body); });
|
|
expect_error([&] { send(ad::UpdateOpcode, update(none, global, {0, ad::MaxSize + 1, {}})); });
|
|
expect_error([&] { send(ad::UpdateOpcode, update(none, global, {0, 1, Bytes(ad::MaxCompressed + 1)})); });
|
|
expect_error([&] { send(ad::RequestOpcode, request(none, ad::Types)); });
|
|
|
|
const ad::Entry upper{48, ad::MaxSize, zipped(ad::MaxSize, Z_NO_COMPRESSION)};
|
|
expect_ack(send(ad::UpdateOpcode, update(player, character, upper), player), player, character, 0);
|
|
expect_data(send(ad::RequestOpcode, request(player, character), player), player, character, upper);
|
|
for (const auto& erase : {ad::Entry{9, 0, {}}, ad::Entry{9, 0, zipped(0)}}) {
|
|
expect_ack(send(ad::UpdateOpcode, update(player, character, erase), player), player, character, 0);
|
|
expect_data(send(ad::RequestOpcode, request(player, character), player), player, character, {});
|
|
check(ad::stored_time(account, character, player, scratch.root) == 0, "deleted startup time");
|
|
}
|
|
|
|
auto path = ad::file(account, global, none, scratch.root);
|
|
{
|
|
std::ofstream out(path, std::ios::binary | std::ios::app);
|
|
out.put('!');
|
|
}
|
|
check(!ad::load(account, global, none, scratch.root), "trailing store bytes");
|
|
fs::resize_file(path, 15);
|
|
check(!ad::load(account, global, none, scratch.root), "truncated store header");
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, second)), none, global, 0);
|
|
{
|
|
std::fstream out(path, std::ios::binary | std::ios::in | std::ios::out);
|
|
out.seekp(16);
|
|
out.put(0);
|
|
}
|
|
check(!ad::load(account, global, none, scratch.root), "corrupt stored zlib stream");
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, second)), none, global, 0);
|
|
{
|
|
std::fstream out(path, std::ios::binary | std::ios::in | std::ios::out);
|
|
out.seekp(7);
|
|
out.put(static_cast<char>(0x80));
|
|
}
|
|
check(!ad::load(account, global, none, scratch.root), "negative stored timestamp");
|
|
|
|
// Concurrent writers must publish exactly one complete version.
|
|
bool a = false, b = false;
|
|
std::thread writer1([&] { a = ad::save(account, global, none, first, scratch.root); });
|
|
std::thread writer2([&] { b = ad::save(account, global, none, second, scratch.root); });
|
|
writer1.join();
|
|
writer2.join();
|
|
auto result = ad::load(account, global, none, scratch.root);
|
|
check(a && b && result && ((result->time == first.time && result->compressed == first.compressed) ||
|
|
(result->time == second.time && result->compressed == second.compressed)), "concurrent replacement");
|
|
|
|
// A failed replacement must preserve everything at the destination and clean the writer's temporary.
|
|
fs::remove(path);
|
|
fs::create_directory(path);
|
|
const auto marker = path / "keep";
|
|
{ std::ofstream out(marker); out << "keep"; }
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, second)), none, global, 1);
|
|
expect_ack(send(ad::UpdateOpcode, update(none, global, {})), none, global, 1);
|
|
check(fs::is_regular_file(marker), "failed mutation removed prior data");
|
|
for (const auto& item : fs::directory_iterator(path.parent_path()))
|
|
check(item.path().filename().string().find(".tmp-") == std::string::npos, "orphaned temporary");
|
|
std::cout << "PASS edit mode storage, scope, wire replies, bounded zlib, replacement and erase\n";
|
|
}
|