Files
WoW-Hybrid-gateway/tests/sdhd_account_preference_test.cpp

62 lines
2.8 KiB
C++

#include "../native/sdhd_preference.hpp"
#include <random>
#include <stdexcept>
#include <thread>
namespace pref = gateway::sdhd_preference;
namespace fs = std::filesystem;
using gateway::Bytes;
static void check(bool ok, const char* reason) {
if (!ok) throw std::runtime_error(reason);
}
int main() {
struct Scratch {
fs::path root = "sdhd-account-test-" + std::to_string(std::random_device{}());
Scratch() { check(fs::create_directory(root), "cannot create private test directory"); }
~Scratch() {
std::error_code ignored;
fs::remove_all(root, ignored);
}
} scratch;
const auto root = scratch.root / "GatewayAccountData";
constexpr uint32_t account = 0x1234abcd, other = account + 1;
const Bytes hd{0}, sd{0x80};
auto send = [&](uint32_t key, const Bytes& body) { return pref::set_from_client(key, body, root); };
auto load = [&](uint32_t key) { return pref::load(key, root); };
check(!load(account).has_value() && !load(0).has_value(), "missing preference must be unspecified");
check(!send(0, sd) && !send(account, {}) && !send(account, {0x80, 0}), "invalid scope or length accepted");
check(!fs::exists(root), "rejected input created storage");
check(send(account, sd) && send(account, sd) && load(account) == true, "SD or retry did not persist");
check(!load(other).has_value() && send(other, hd) && load(other) == false, "separate authenticated account");
for (unsigned value = 1; value <= 255; ++value) {
if (value != 0x80)
check(!send(account, {static_cast<uint8_t>(value)}), "accepted nonzero padding");
}
check(load(account) == true && send(account, hd) && load(account) == false, "SD to HD replacement");
bool wrote_sd = false, wrote_hd = false;
std::thread writer1([&] { wrote_sd = send(account, sd); });
std::thread writer2([&] { wrote_hd = send(account, hd); });
writer1.join();
writer2.join();
check(wrote_sd && wrote_hd && load(account).has_value() && load(other) == false, "concurrent atomic writes");
const auto path = root / "1234abcd" / "sdhd.bin";
for (const auto& bad : {Bytes{}, Bytes{1}, Bytes{0x80, 0}}) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
for (uint8_t b : bad) out.put(static_cast<char>(b));
out.close();
check(!load(account).has_value(), "malformed stored value accepted");
}
check(send(account, sd) && load(account) == true, "valid retry did not replace malformed file");
fs::remove(path);
fs::create_directory(path);
check(!send(account, hd) && fs::is_directory(path), "failed write destroyed destination");
for (const auto& entry : fs::directory_iterator(path.parent_path()))
check(entry.path() == path, "temporary file left behind");
}