Files
WoW-Hybrid-gateway/tests/name_response_contract_test.cpp
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

38 lines
2.1 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
// Source NameQuery producer/gateway parser contract, not a retained crash payload.
#include "gateway/chat.hpp"
#include <functional>
#include <iostream>
using namespace gateway;
namespace {
unsigned checks=0;
void check(bool value){++checks;if(!value)throw Error("name-response contract fixture");}
void rejects(const std::function<void()>& f){++checks;try{f();}catch(const Error&){return;}throw Error("expected name-response refusal");}
Bytes response(uint32_t race=1,uint32_t gender=0,uint32_t cls=1,std::string name="PlayerUnit",std::string realm="",uint64_t guid=117){
Bytes b;le(b,guid,8);append(b,std::span(reinterpret_cast<const uint8_t*>(name.data()),name.size()));le(b,0,1);
append(b,std::span(reinterpret_cast<const uint8_t*>(realm.data()),realm.size()));le(b,0,1);
le(b,race,4);le(b,gender,4);le(b,cls,4);return b;
}
}
int main(){
// Both custom races use the SAME source uint32 metadata layout. A32byte
// response is valid for this10-character name, with no extra layout fields.
for(auto race:{1u,9u,10u}){
auto raw=response(race);check(raw.size()==32);auto player=turtle_chat_player(raw);
check(player.guid==117 && player.name=="PlayerUnit" && player.race==race && player.cls==1 && player.gender==0);
}
auto raw=response();
for(size_t n=0;n<raw.size();++n)rejects([&](){turtle_chat_player(std::span(raw).first(n));});
auto trailing=raw;trailing.push_back(0);rejects([&](){turtle_chat_player(trailing);});
rejects([&](){turtle_chat_player(response(0));});
rejects([&](){turtle_chat_player(response(256));});
rejects([&](){turtle_chat_player(response(1,2));});
rejects([&](){turtle_chat_player(response(1,0,0));});
rejects([&](){turtle_chat_player(response(1,0,256));});
rejects([&](){turtle_chat_player(response(1,0,1,""));});
rejects([&](){turtle_chat_player(response(1,0,1,"PlayerUnit","OtherRealm"));});
rejects([&](){turtle_chat_player(response(1,0,1,"PlayerUnit","",0));});
rejects([&](){turtle_chat_player(response(1,0,1,"PlayerUnit","",UINT64_C(1)<<32));});
std::cout<<"name-response contract checks: "<<checks<<"\n";
}