Files
WoW-Hybrid-gateway/tests/priority_client_store_win_test.cpp
2026-11-23 03:28:32 +02:00

81 lines
3.4 KiB
C++

#include "priority_auth.hpp"
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <string>
int main() {
using gw::priority::load_protected_priority_token_file;
using gw::priority::store_protected_priority_token_file;
wchar_t temporary[MAX_PATH]{};
const DWORD count = GetTempPathW(MAX_PATH, temporary);
assert(count && count < MAX_PATH - 80);
std::wstring path(temporary);
path += L"gw-priority-dpapi-fixture-" + std::to_wstring(GetCurrentProcessId()) + L".dat";
DeleteFileW(path.c_str());
const std::string synthetic = "fixture-bearer-1234567890";
std::string loaded = "stale";
assert(!load_protected_priority_token_file(path, loaded) && loaded.empty());
assert(!store_protected_priority_token_file(path, ""));
assert(!store_protected_priority_token_file(path, "bad\nheader"));
assert(store_protected_priority_token_file(path, synthetic));
{
std::ifstream file(std::filesystem::path(path), std::ios::binary);
const std::string disk((std::istreambuf_iterator<char>(file)), {});
assert(disk.size() > synthetic.size());
assert(disk.find(synthetic) == std::string::npos);
}
assert(load_protected_priority_token_file(path, loaded));
assert(loaded == synthetic);
{
HANDLE file = CreateFileW(path.c_str(), GENERIC_READ | GENERIC_WRITE, 0,
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
assert(file != INVALID_HANDLE_VALUE);
LARGE_INTEGER size{};
assert(GetFileSizeEx(file, &size) && size.QuadPart > 8);
LARGE_INTEGER at{};
at.QuadPart = size.QuadPart - 1;
assert(SetFilePointerEx(file, at, nullptr, FILE_BEGIN));
unsigned char byte = 0;
DWORD used = 0;
assert(ReadFile(file, &byte, 1, &used, nullptr) && used == 1);
at.QuadPart = size.QuadPart - 1;
assert(SetFilePointerEx(file, at, nullptr, FILE_BEGIN));
byte ^= 0x5a;
assert(WriteFile(file, &byte, 1, &used, nullptr) && used == 1);
CloseHandle(file);
}
assert(!load_protected_priority_token_file(path, loaded) && loaded.empty());
assert(DeleteFileW(path.c_str()));
struct RejectAddress final : gw::priority::HttpClient {
gw::priority::HttpReply post(std::string_view path, std::string_view,
std::string_view bearer) override {
assert(path == "/api/launcher/address" && bearer == "fixture-bearer-1234567890");
return {401, "{}"};
}
} http;
struct NeverProbe final : gw::priority::RouteProbe {
bool challenge(std::string_view, std::string&) override { assert(false); return false; }
} probe;
struct TestClock final : gw::priority::Clock {
std::uint64_t monotonic_ms() const override { return 1; }
} clock;
gw::priority::Session session;
assert(session.resume_bearer(synthetic) == gw::priority::Status::session_ready);
std::string exported;
assert(session.copy_bearer(exported) && exported == synthetic);
assert(session.select(http, probe, clock).status == gw::priority::Status::token_expired);
assert(!session.signed_in());
assert(!session.copy_bearer(exported) && exported.empty());
assert(session.resume_bearer("bad\nheader") == gw::priority::Status::auth_rejected);
return 0;
}