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

41 lines
4.6 KiB
C++

// Captured source list/loot response are private inputs. Source master role,
// native commands and rejected counterparts are explicit synthetic controls.
#include "loot_translation.hpp"
#include "serializer.h"
#include <algorithm>
#include <dlfcn.h>
#include <fstream>
#include <iostream>
#include <iterator>
using namespace gateway;
static void expect(bool value){if(!value)throw Error("master-loot boundary assertion");}
static Bytes read(const char* path){std::ifstream f(path,std::ios::binary);if(!f)throw Error("private packet unavailable");return Bytes(std::istreambuf_iterator<char>(f),{});}
static bool throws(const std::function<void()>& fn){try{fn();return false;}catch(const Error&){return true;}}
int main(int argc,char** argv){try{
if(argc!=4)throw Error("private candidates/response and matching codec required");
auto list=read(argv[1]),reply=read(argv[2]);auto response=turtle_loot_response(reply);
Reader source(list);auto count=source.le(1);expect(count>0 && count<=40);std::vector<uint64_t> candidates;for(unsigned i=0;i<count;++i)candidates.push_back(source.le(8));source.end();
void* lib=dlopen(argv[3],RTLD_NOW|RTLD_LOCAL);if(!lib)throw Error("matching codec unavailable");auto raw=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(lib,"gw_codec_serialize"));expect(raw);
NativeEntityTranslation::Codec codec=[&](uint32_t mode,const Bytes& input){Bytes out(GW_CODEC_BOUND);uint32_t size=0;expect(raw(mode,input.data(),uint32_t(input.size()),0,out.data(),uint32_t(out.size()),&size)==0);out.resize(size);return out;};
// Synthetic already-admitted actor control, not a source-create/entry
// authority assertion. Match the captured GUID's structural entry bits
// so the existing GUID encoder can construct this fixture's counterpart.
const auto structural_entry=uint32_t((response.owner>>24)&0xffffff);
NativeEntityTranslation entities;auto& actor=entities.actors[response.owner];actor.kind=3;actor.entry=structural_entry;actor.generation=1;actor.sent=true;actor.modern=forever_creature_guid(response.owner,structural_entry,70,1);
Character self{};self.guid=candidates.front();NativeLootTranslation loot;TurtleGroupList group;group.leader=self.guid;group.loot=TurtleGroupLootPolicy{TurtleGroupLootMethod::master,self.guid,2,0};loot.group=group;
auto event=[&](uint32_t opcode,const Bytes& body){return loot.translate({opcode,body},self,70,1,entities,codec);};
expect(event(0x2a4,list).empty() && !loot.pending_master);auto open=pack_guid(actor.modern);loot.sent(0x3e00cc,open,entities);
expect(event(0x2a4,list).empty() && loot.pending_master);auto packets=event(0x160,reply);expect(packets.size()==2 && packets[0].opcode==0x4700bf && packets[1].opcode==0x4700c9 && loot.window && !loot.pending_master);
Reader native(packets[1].body);auto id=unpack_guid(native,16);expect(std::equal(id.begin(),id.end(),loot.window->object.begin()));expect(native.le(4)==count);
for(auto candidate:candidates){auto actual=unpack_guid(native,16);auto expected=forever_player_guid(candidate,70);expect(std::equal(actual.begin(),actual.end(),expected.begin()));}native.end();
// Actual captured slots may be locked/owned for a different receiver.
// Only the explicit source MASTER slot control may authorize assignment.
expect(!loot.window->source.items.empty());auto slot=loot.window->source.items.front().slot;loot.window->source.items.front().ui=TurtleLootSlotUi::master;
Bytes assign;le(assign,1,4);append(assign,pack_guid(forever_player_guid(candidates.front(),70)));append(assign,pack_guid(loot.window->object));le(assign,slot,1);
auto send=loot.master_items(assign,entities,self.guid,70);expect(send.size()==1 && send[0].opcode==0x2a3);Reader request(send[0].body);expect(request.le(8)==response.owner && request.le(1)==slot && request.le(8)==candidates.front());request.end();
auto held=loot;held.group->loot->master=self.guid+1;expect(throws([&]{held.master_items(assign,entities,self.guid,70);}));held=loot;held.window->master_candidates.clear();expect(throws([&]{held.master_items(assign,entities,self.guid,70);}));
held=loot;held.window->source.items.front().ui=TurtleLootSlotUi::locked;expect(throws([&]{held.master_items(assign,entities,self.guid,70);}));held=loot;held.group_transition=true;expect(throws([&]{held.master_items(assign,entities,self.guid,70);}));
actor.generation=2;expect(throws([&]{loot.master_items(assign,entities,self.guid,70);}));
std::cout<<"PASS actual candidate-list/window correlation, original native list writer, source-master assignment and recipient/role/slot/generation holds\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}