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

281 lines
22 KiB
C++

#include "optional_test_codec.hpp"
#include "world_translation.hpp"
#include "gateway/roster_translation.hpp"
#include "gateway/server_cast_identity.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include <dlfcn.h>
#include <iostream>
#include <algorithm>
using namespace gateway;
namespace F=gateway::turtle_entity_fields;
static void check(bool b,const char* why){if(!b)throw Error(why);}
static constexpr uint64_t self=4509,item=(uint64_t(0x4000)<<48)|36258;
static Bytes packed(uint64_t value){Bytes b;le(b,value,8);return pack_guid(b);}
static Bytes event(uint64_t caster=item,uint64_t unit=self,uint32_t spell=21991,uint64_t target=self){
auto b=packed(caster);append(b,packed(unit));le(b,spell,4);le(b,0x100,2);le(b,1,1);le(b,target,8);le(b,0,1);le(b,2,2);append(b,packed(target));return b;
}
static NativeWorldTranslation world(){
NativeWorldTranslation w;w.selected.guid=self;w.selected.cls=1;w.realm=70;w.self_created=true;w.location=WorldLocation{};
LegacyObjectUpdate player;player.kind=2;player.type=4;player.guid=self;
w.auras.consume({player});w.auras.flush({{self,forever_player_guid(self,70)}});
LegacyObjectUpdate i;i.kind=2;i.type=1;i.guid=item;i.fields={{F::OBJECT_FIELD_ENTRY,17782},{F::ITEM_FIELD_OWNER,self}};w.inventory.items[item]=i;
ItemTemplateReply d;d.entry=17782;d.found=true;d.inventory_type=2;d.spells[0].spell=21991;d.spells[0].trigger=1;w.inventory.templates[d.entry]=d;return w;
}
static std::vector<LegacyPacket> packets(const Bytes& bundle){
Reader r(bundle);std::vector<LegacyPacket> out;auto count=r.le(4);
while(count--){auto opcode=uint32_t(r.le(4));out.push_back({opcode,r.take(r.le(4))});}r.end();return out;
}
static Bytes normalize_server_time(Bytes body){
Reader r(body);for(unsigned i=0;i<4;++i)unpack_guid(r,16);
// Pinned SpellPackets.cpp operator<<(SpellCastData): SpellID, two
// Visual fields, CastFlags, CastFlagsEx, CastFlagsEx2 precede CastTime.
r.take(24);
auto offset=body.size()-r.left();r.take(4);
std::fill(body.begin()+offset,body.begin()+offset+4,0);return body;
}
static LegacyObjectUpdate initial_aura(){
LegacyObjectUpdate u;u.kind=2;u.type=4;u.guid=self;
u.fields={{F::UNIT_FIELD_AURA,21991},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};return u;
}
static void equip(NativeWorldTranslation& w,unsigned slot=1){
auto index=F::PLAYER_FIELD_INV_SLOT_HEAD+2*slot;
w.inventory.player_fields[index]=uint32_t(item);w.inventory.player_fields[index+1]=uint32_t(item>>32);
}
static LegacyPacket update(uint64_t guid,uint8_t type,uint8_t kind,const std::map<uint16_t,uint32_t>& fields){
Bytes b;le(b,1,4);le(b,0,1);le(b,kind,1);append(b,packed(guid));
if(kind==2 || kind==3){le(b,type,1);le(b,0,1);}
auto n=fields.rbegin()->first/32+1;le(b,n,1);std::vector<uint32_t> mask(n);
for(auto [index,value]:fields){(void)value;mask[index/32]|=1u<<(index%32);}
for(auto word:mask)le(b,word,4);
for(auto [index,value]:fields){(void)index;le(b,value,4);}return {0xa9,b};
}
int main(){try{
if (test_fixture::optional_codec_missing(GW_ENTITY_CODEC)) return 77;
void* library = dlopen(GW_ENTITY_CODEC, RTLD_NOW | RTLD_LOCAL);
if (!library) {
std::cerr << dlerror() << '\n';
return 1;
}
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(codec,"source codec export missing");
{ // A periodic self GO precedes the sparse Plainsrunning speed holder.
auto w=world();
auto go=packets(w.translate({0x132,event(self,self,12568)},0x46010046,0,codec));
check(go.size()==1 && go[0].opcode==0x69002c,"source speed GO missing");
Reader go_data(go[0].body);unpack_guid(go_data,16);unpack_guid(go_data,16);
auto cast_bytes=unpack_guid(go_data,16);
Guid128 cast{};std::copy(cast_bytes.begin(),cast_bytes.end(),cast.begin());
auto fresh=update(self,4,0,{{F::UNIT_FIELD_AURA,12568},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}});
// Unrelated owner health fields can arrive between GO and aura fields.
LegacyObjectUpdate health;health.guid=self;health.kind=0;health.fields[F::UNIT_FIELD_HEALTH]=50;
w.auras.consume({health});
auto shown=packets(w.translate(fresh,0x46010046,0,codec));
auto aura=std::find_if(shown.begin(),shown.end(),[](const auto& row){return row.opcode==0x690011;});
AuraSlotData expected{12568,1,2,60,1};expected.server_self_cast=cast;
check(aura!=shown.end() && aura->body==forever_aura_update({forever_player_guid(self,70),false,{{0,expected}}}),
"stage-three aura did not retain its successful self cast");
w.translate({0x132,event(self,self,12568)},0x46010046,0,codec);
check(w.auras.units.at(self).server_self_cast[0]==cast,"speed refresh replaced active identity");
w.translate(update(self,4,0,{{F::UNIT_FIELD_AURA,0}}),0x46010046,0,codec);
check(!w.auras.units.at(self).server_self_cast[0],"speed holder survived removal");
auto late=world();
auto holder_only=packets(late.translate(fresh,0x46010046,0,codec));
const auto holder_cast=late.auras.units.at(self).server_self_cast[0];
check(holder_cast && valid_server_self_aura_cast(12568,*holder_cast),"genuine holder lacks native presentation identity");
check(std::none_of(holder_only.begin(),holder_only.end(),[](const auto& p){return p.opcode==0x69002c;}),
"holder alone invented SPELL_GO");
late.translate({0x132,event(self,self,12568)},0x46010046,0,codec);
check(late.auras.units.at(self).server_self_cast[0]==holder_cast,"late source GO replaced existing holder identity");
auto ambiguous=world();
for(int i=0;i<2;++i)ambiguous.translate({0x132,event(self,self,12568)},0x46010046,0,codec);
const auto prior_counter=ambiguous.server_cast_counter;
auto holder_after_ambiguous=packets(ambiguous.translate(fresh,0x46010046,0,codec));
check(ambiguous.auras.units.at(self).server_self_cast[0]==forever_server_cast_guid(70,0,12568,prior_counter+1),
"ambiguous speed GO attributed to holder instead of independent presentation identity");
check(std::none_of(holder_after_ambiguous.begin(),holder_after_ambiguous.end(),[](const auto& p){return p.opcode==0x69002c;}),
"ambiguous holder emitted an extra SPELL_GO");
auto stale=world();
stale.translate({0x132,event(self,self,12568)},0x46010046,0,codec);
stale.auras.pending_self_hits.at(12568).expires_ms=0;
const auto stale_counter=stale.server_cast_counter;
auto holder_after_expiry=packets(stale.translate(fresh,0x46010046,0,codec));
check(stale.auras.units.at(self).server_self_cast[0]==forever_server_cast_guid(70,0,12568,stale_counter+1),
"expired speed GO identity reused");
check(std::none_of(holder_after_expiry.begin(),holder_after_expiry.end(),[](const auto& p){return p.opcode==0x69002c;}),
"expired holder emitted an extra SPELL_GO");
for(uint32_t spell:{11,47,2983,4104,4105,8696,11305,12568}){
auto sibling=world();sibling.translate({0x132,event(self,self,spell)},0x46010046,0,codec);
sibling.translate(update(self,4,0,{{F::UNIT_FIELD_AURA,spell},{F::UNIT_FIELD_AURAFLAGS,5}}),0x46010046,0,codec);
check(sibling.auras.units.at(self).server_self_cast[0].has_value(),"source-attested ribbon family lost its self identity");
}
auto recreated=world();recreated.translate({0x132,event(self,self,12568)},0x46010046,0,codec);
auto player_update=initial_aura();player_update.fields[F::UNIT_FIELD_AURA]=12568;
recreated.auras.consume({player_update});
check(!recreated.auras.units.at(self).server_self_cast[0],"recreated character inherited previous speed cast");
for(uint32_t spell:{12566,12567,52885}){
auto untouched=world();untouched.translate({0x132,event(self,self,spell)},0x46010046,0,codec);
check(untouched.auras.pending_self_hits.empty(),"different source aura admitted");
}
}
{ // Original release notification precedes the genuine sparse ghost update.
auto released=world();released.source_ghost=false;
auto source_flags=update(self,4,0,{{F::PLAYER_FLAGS,16}});
auto out=packets(released.translate(source_flags,0x46010046,0,codec));
check(!out.empty() && out.front().opcode==0x470212,"fresh release lifecycle notification missing or late");
check(out.front().body==pack_guid(forever_player_guid(self,70)),"release notification owner differs");
out=packets(released.translate(source_flags,0x46010046,0,codec));
check(std::none_of(out.begin(),out.end(),[](const auto& p){return p.opcode==0x470212;}),"repeated ghost restarted lifecycle");
auto login=world();login.source_ghost.reset();
out=packets(login.translate(source_flags,0x46010046,0,codec));
check(std::none_of(out.begin(),out.end(),[](const auto& p){return p.opcode==0x470212;}),"initial ghost login treated as fresh release");
}
{ // Genuine unit-caster GO for an existing reviewed ghost holder.
auto w=world();w.source_ghost=true;
LegacyObjectUpdate ghost;ghost.guid=self;ghost.kind=0;
ghost.fields={{F::UNIT_FIELD_AURA+32,8326},{F::UNIT_FIELD_AURAFLAGS+4,8},{F::UNIT_FIELD_AURALEVELS+8,60}};
w.auras.consume({ghost});w.auras.flush({{self,forever_player_guid(self,70)}});
auto out=packets(w.translate({0x132,event(self,self,8326)},0x46010046,0,codec));
check(out.size()==2 && out[0].opcode==0x69002c && out[1].opcode==0x690011,"source ghost GO not delivered before enriched holder");
check(w.auras.units.at(self).server_self_cast[32]==forever_server_cast_guid(70,0,8326,0x8000000001ULL),"source ghost cast identity not retained");
auto not_ghost=world();auto ignored=packets(not_ghost.translate({0x132,event(self,self,8326)},0x46010046,0,codec));
check(ignored.empty() && not_ghost.server_cast_counter==0x8000000000ULL,"ghost cast without authoritative state accepted");
}
{
auto deferred=world();deferred.self_created=false;deferred.source_ghost.reset();
check(packets(deferred.translate({0x132,event(self,self,8326)},0x46010046,0,codec)).empty() && deferred.pending_ghost_cast.has_value(),"pre-object self GO not retained");
deferred.self_created=true;
auto fields=update(self,4,0,{{190,16},{F::UNIT_FIELD_AURA+32,8326},{F::UNIT_FIELD_AURAFLAGS+4,8},{F::UNIT_FIELD_AURALEVELS+8,60}});
auto out=packets(deferred.translate(fields,0x46010046,0,codec));
check(std::any_of(out.begin(),out.end(),[](const auto& p){return p.opcode==0x69002c;}) && !deferred.pending_ghost_cast,"source-confirmed deferred self GO not delivered");
auto expired=world();expired.source_ghost.reset();expired.translate({0x132,event(self,self,8326)},0x46010046,0,codec);
for(unsigned i=0;i<64;++i)expired.translate({1,{}},0x46010046,0,codec);
check(!expired.pending_ghost_cast && expired.server_cast_counter==0x8000000000ULL,"unconfirmed ghost GO did not expire");
}
auto w=world();auto run=[&](NativeWorldTranslation& s,const Bytes& b){return s.translate({0x132,b},0x46010046,12345,codec);};
check(event().size()==28,"reviewed equip GO shape differs");
auto first=run(w,event());Reader bundle(first);check(bundle.le(4)==1 && bundle.le(4)==0x69002c,"real equip GO not translated or fabricated PREPARE emitted");auto bytes=bundle.take(bundle.le(4));bundle.end();Reader r(bytes);
auto owned=w.inventory.owned_bindings();check(unpack_guid(r,16)==Bytes(owned.at(item).begin(),owned.at(item).end()),"cast item identity differs");
auto player=forever_player_guid(self,70);check(unpack_guid(r,16)==Bytes(player.begin(),player.end()),"casting unit differs");
auto id=forever_server_cast_guid(70,0,21991,0x8000000001ULL);check(unpack_guid(r,16)==Bytes(id.begin(),id.end()),"server correlation identity differs");
check(unpack_guid(r,16)==Bytes(16),"unknown original cast was fabricated");
check(r.le(4)==21991 && r.le(4)==248700 && r.le(4)==0 && r.le(4)==0x101,"source spell/visual or server-trigger flags differ");
check(!w.pending_cast && w.server_cast_counter==0x8000000001ULL,"server event captured client pending state");
check(w.auras.units.at(self).equip_candidate && w.auras.units.at(self).equip_candidate->context.cast==id &&
w.auras.units.at(self).equip_candidate->context.item==owned.at(item),"genuine equip event context not retained for later source confirmation");
auto second=run(w,event());check(second!=first && w.server_cast_counter==0x8000000002ULL,"server event identity reused");
auto absent=[&](NativeWorldTranslation state,const Bytes& b){auto before=state.server_cast_counter;auto out=run(state,b);check(out==Bytes({0,0,0,0}) && state.server_cast_counter==before,"unsupported server event fabricated success");};
absent(world(),event(item+1));absent(world(),event(item,self+1));absent(world(),event(item,self,21991,self+1));absent(world(),event(item,self,999999));
auto full=event();for(size_t n=0;n<full.size();++n)absent(world(),Bytes(full.begin(),full.begin()+n));
auto trailing=full;trailing.push_back(0);absent(world(),trailing);
auto projectile=full;projectile[11]|=0x20;absent(world(),projectile);
auto none=world();none.inventory.items.clear();absent(none,full);
none=world();none.inventory.templates.clear();absent(none,full);
// A peer carrying the same public item is not a source of SELF on-equip spells.
none=world();none.inventory.peer_templates.insert(none.inventory.templates.extract(17782));
none.inventory.peer_template_order.push_back(17782);
check(none.inventory.peer_template(17782)->spells[0].spell==21991,"peer proc metadata retained for presentation");
absent(none,full);
none=world();none.inventory.templates.at(17782).found=false;absent(none,full);
none=world();none.inventory.templates.at(17782).spells[0].trigger=0;absent(none,full);
none=world();none.inventory.templates.at(17782).spells[1]={21991,0};absent(none,full);
none=world();none.self_created=false;auto queued_counter=none.server_cast_counter;
check(run(none,full)==Bytes({0,0,0,0}) && none.server_cast_counter==queued_counter && none.login_equip_casts.size()==1,
"precreate event emitted output or lost its bounded evidence");
none=world();none.location.reset();absent(none,full);
none=world();none.location->map=8192;absent(none,full);
none=world();none.realm=0;absent(none,full);
none=world();none.server_cast_counter=0xffffffffffULL;absent(none,full);
none=world();NativeWorldTranslation::PendingCast pending{};pending.spell.spell=772;none.pending_cast=pending;
auto pending_counter=none.server_cast_counter;check(run(none,full)==Bytes({0,0,0,0}) && none.pending_cast &&
none.pending_cast->spell.spell==772 && none.server_cast_counter==pending_counter,"unrelated pending client cast changed");
// Simulate only already-parsed authoritative state, not engine acceptance:
// the real GO arrives first, then self/item creation and source aura state;
// the item-query reply may arrive later still. Nothing may be fabricated
// while any of these independent pieces of evidence is unavailable.
auto tick=[&](NativeWorldTranslation& state){return state.translate({0,{}},0x46010046,12345,codec);};
auto deferred=[&](){
auto state=world();state.self_created=false;state.inventory.templates.clear();
auto counter=state.server_cast_counter;
check(run(state,full)==Bytes({0,0,0,0}) && state.login_equip_casts.size()==1 && state.server_cast_counter==counter,
"precreate source GO was presented before corroborating state");
state.self_created=true;state.auras.consume({initial_aura()});equip(state);return state;
};
const auto profile=world().inventory.templates.at(17782);
auto bootstrap=deferred();
check(tick(bootstrap)==Bytes({0,0,0,0}) && bootstrap.login_equip_casts.at(21991).slot==0 &&
bootstrap.auras.units.at(self).initial && !bootstrap.auras.units.at(self).self_cast[0],
"missing item template flushed or enriched initial source aura");
check(!bootstrap.auras.units.at(self).server_self_cast[0] && !bootstrap.auras.units.at(self).caster_cast[0] &&
bootstrap.server_cast_counter==0x8000000000ULL,
"deferred login aura allocated fallback cast before equipment proof");
bootstrap.inventory.templates[17782]=profile;
auto ready=packets(tick(bootstrap));AuraSelfCastContext context{id,owned.at(item)};
check(ready.size()==2 && ready[0].opcode==0x69002c && normalize_server_time(ready[0].body)==normalize_server_time(bytes) && ready[1].opcode==0x690011 &&
ready[1].body==forever_aura_update({player,true,{{0,AuraSlotData{21991,1,2,60,1,context}}}}),
"confirmed login GO/Aura order, exact original cast/item context, or no-PREPARE contract differs");
check(bootstrap.login_equip_casts.empty() && bootstrap.server_cast_counter==0x8000000001ULL &&
bootstrap.auras.units.at(self).self_cast[0]==context && !bootstrap.pending_cast &&
tick(bootstrap)==Bytes({0,0,0,0}),"initial equip context duplicated or altered client cast ownership");
auto released=[&](NativeWorldTranslation& state,const Bytes& output){
auto result=packets(output);unsigned aura_count=0;
for(const auto& packet:result){check(packet.opcode!=0x69002c && packet.opcode!=0x69002b,
"uncorroborated login evidence fabricated a cast");aura_count+=packet.opcode==0x690011;}
check(aura_count==1 && state.login_equip_casts.empty(),
"rejected/expired login evidence withheld the genuine initial aura");
const auto& source=state.auras.units.at(self);
unsigned fallback_count=0;
for(unsigned slot=0;slot<source.spells.size();++slot){
check(!source.self_cast[slot],"unconfirmed initial holder acquired equipment context");
if(!source.spells[slot])continue;
const auto& cast=source.server_self_cast[slot]?source.server_self_cast[slot]:source.caster_cast[slot];
check(cast && valid_server_self_aura_cast(source.spells[slot],*cast),"released genuine holder lacks presentation identity");
++fallback_count;
}
check(state.server_cast_counter==0x8000000000ULL+fallback_count,
"released holders allocated duplicate or event cast identities");
check(!source.initial,"rejected login evidence failed to release the initial source snapshot");
};
for(unsigned slot:{19u,23u}){
auto state=deferred();state.inventory.player_fields.clear();equip(state,slot);state.inventory.templates[17782]=profile;
released(state,tick(state)); // Equipped-bag/backpack references are not equipped item proof.
}
{auto state=deferred();state.inventory.player_fields.clear();state.inventory.templates[17782]=profile;released(state,tick(state));}
{auto state=deferred();equip(state,2);state.inventory.templates[17782]=profile;released(state,tick(state));}
{auto state=deferred();state.self_created=false;check(run(state,full)==Bytes({0,0,0,0}) && state.login_equip_casts.at(21991).ambiguous,
"duplicate precreate GO silently selected one event");state.self_created=true;state.inventory.templates[17782]=profile;released(state,tick(state));}
{auto state=deferred();LegacyObjectUpdate duplicate;duplicate.guid=self;
duplicate.fields={{uint16_t(F::UNIT_FIELD_AURA+1),21991},{F::UNIT_FIELD_AURAFLAGS,0x55}};
state.auras.consume({duplicate});state.inventory.templates[17782]=profile;released(state,tick(state));}
for(unsigned invalid=0;invalid<3;++invalid){
auto state=deferred();auto bad=profile;
if(invalid==0)bad.found=false;
if(invalid==1)bad.spells[0].trigger=0;
if(invalid==2)bad.spells[1]={21991,1};
state.inventory.templates[17782]=bad;released(state,tick(state));
}
{auto state=deferred();state.login_equip_packet_budget=2;
check(tick(state)==Bytes({0,0,0,0}) && state.login_equip_packet_budget==1 && state.login_equip_casts.size()==1,
"login evidence expired before its packet budget");released(state,tick(state));}
// Source removal/recreation invalidates deferred evidence even if the
// original item-query metadata becomes available afterward.
for(auto removed:{self,item}){
auto state=deferred();Bytes body;le(body,removed,8);
auto removed_output=packets(state.translate({0xaa,body},0x46010046,12345,codec));
check(state.login_equip_casts.empty(),"removed source retained deferred cast");
check(state.server_cast_counter==0x8000000000ULL+(removed==item?1:0),
"removal allocated more than the surviving holder identity");
if(auto unit=state.auras.units.find(self);unit!=state.auras.units.end())
check(!unit->second.self_cast[0],"removed equipment event retained item provenance");
state.inventory.templates[17782]=profile;auto out=packets(tick(state));
removed_output.insert(removed_output.end(),out.begin(),out.end());
for(const auto& packet:removed_output)check(packet.opcode!=0x69002c && packet.opcode!=0x69002b,"removed source replayed an old cast");
}
{auto state=deferred();auto recreated=initial_aura();
auto output=state.translate(update(self,4,2,recreated.fields),0x46010046,12345,codec);released(state,output);}
{auto state=deferred();auto output=state.translate(update(item,1,2,{{F::OBJECT_FIELD_ENTRY,17782},{F::ITEM_FIELD_OWNER,self}}),
0x46010046,12345,codec);released(state,output);}
{auto state=deferred();check(tick(state)==Bytes({0,0,0,0}),"deferred clear fixture did not wait");
auto output=state.translate(update(self,0,0,{{F::UNIT_FIELD_AURA,0}}),0x46010046,12345,codec);released(state,output);}
std::cout<<"Authenticated-source equip GO and bounded precreate replay, exact initial aura context/order, no PREPARE, rejected ambiguity, expiry/removal/recreate, and packet bounds passed. Offline only.\n";
dlclose(library);return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}