Files

410 lines
44 KiB
C++

#include "optional_test_codec.hpp"
#include "aura_translation.hpp"
#include "gateway/roster_translation.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include "gateway/aura_visual_profile.hpp"
#include "gateway/source_mount_aura_profile.hpp"
#include "gateway/source_ghost_aura_profile.hpp"
#include "gateway/source_self_applied_aura_profile.hpp"
#include "gateway/shop_illusion_aura_profile.hpp"
#include "gateway/server_cast_identity.hpp"
#include "serializer.h"
#include <algorithm>
#include <dlfcn.h>
#include <cstdlib>
#include <iostream>
using namespace gateway;
namespace F=gateway::turtle_entity_fields;
static void check(bool ok,const char* message){if(!ok)throw Error(message);}
static Bytes neutral(const AuraSlotUpdate& u){
bool contexts=false,timings=false,server_casts=false;for(const auto& s:u.slots)if(s.data){if(s.data->self_cast)contexts=true;if(s.data->remaining_ms)timings=true;if(s.data->server_self_cast)server_casts=true;}
Bytes b(u.unit.begin(),u.unit.end());le(b,uint8_t(u.all)|(contexts?0x80:0)|(timings?0x40:0)|(server_casts?0x20:0),1);le(b,u.slots.size(),1);
for(const auto& s:u.slots){le(b,s.slot,1);le(b,s.data.has_value(),1);if(s.data){const auto& a=*s.data;le(b,a.spell,4);le(b,a.flags,2);le(b,a.level,2);le(b,a.applications,1);le(b,a.active_flags,4);
if(contexts){le(b,a.self_cast.has_value(),1);if(a.self_cast){append(b,a.self_cast->cast);append(b,a.self_cast->item);}}
if(server_casts){le(b,a.server_self_cast.has_value(),1);if(a.server_self_cast)append(b,*a.server_self_cast);}
if(timings){le(b,a.remaining_ms.has_value(),1);if(a.remaining_ms)le(b,*a.remaining_ms,4);}
}}
return b;
}
int main(){try{
auto player=forever_player_guid(4509,70);NativeAuraTranslation state;
for(auto spell:shop_illusion_aura_profile::Existing){
NativeAuraTranslation illusions;LegacyObjectUpdate holder;
holder.guid=4509;holder.kind=2;holder.type=4;
holder.fields={{F::UNIT_FIELD_AURA,spell},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
illusions.consume({holder});auto packets=illusions.flush({{4509,player}});
check(packets.size()==1 && packets[0].body==forever_aura_update({player,true,{{0,AuraSlotData{spell,1,2,60,1}}}}),
"source-attested shop illusion buff not presented");
check(illusions.units.at(4509).shown[0] && illusions.units.at(4509).active_mask(spell)==1,
"shop illusion aura mask drifted");
holder.kind=0;holder.fields={{F::UNIT_FIELD_AURA,0}};illusions.consume({holder});
packets=illusions.flush({{4509,player}});
check(packets.size()==1 && packets[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),
"shop illusion removal not presented");
}
for(auto spell:shop_illusion_aura_profile::PendingCodec)
check(shop_illusion_aura_profile::mask(spell)==1,"paired-codec shop illusion missing from profile");
for(auto spell:{19937u,31961u,31965u,999999u})
check(shop_illusion_aura_profile::mask(spell)==0,"unqualified illusion admitted");
for(auto row:source_ghost_aura_profile::Rows){
NativeAuraTranslation ghosts;LegacyObjectUpdate holder;holder.guid=4509;holder.kind=3;holder.type=4;
holder.fields={{F::UNIT_FIELD_AURA+32,row.spell},{F::UNIT_FIELD_AURAFLAGS+4,8},{F::UNIT_FIELD_AURALEVELS+8,60}};
ghosts.consume({holder});auto packets=ghosts.flush({{4509,player}});
check(packets.size()==1 && ghosts.units.at(4509).shown[32],"genuine ghost holder not presented");
check(ghosts.units.at(4509).active_mask(row.spell)==row.active_flags,"ghost companion effects invented");
holder.kind=0;holder.fields={{F::UNIT_FIELD_AURA+32,0}};ghosts.consume({holder});
packets=ghosts.flush({{4509,player}});check(packets.size()==1 && !ghosts.units.at(4509).shown[32],"source ghost removal lost");
}
{ // Actual source self GO may enrich, never create, a genuine ghost holder.
NativeAuraTranslation ghosts;auto cast=forever_server_cast_guid(70,0,8326,0x8000000001ULL);
check(!ghosts.bind_server_self_cast(4509,8326,cast,70),"missing holder synthesized");
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;u.fields={{F::UNIT_FIELD_AURA+32,8326},{F::UNIT_FIELD_AURAFLAGS+4,8}};
ghosts.consume({u});ghosts.flush({{4509,player}});
check(!ghosts.bind_server_self_cast(4509,8326,cast,71),"foreign realm cast accepted");
check(!ghosts.bind_server_self_cast(4509,8326,Guid128{},70),"empty cast context accepted");
check(ghosts.bind_server_self_cast(4509,8326,cast,70),"genuine holder cast not bound");
AuraSlotData data{8326,1,16,0,1};data.server_self_cast=cast;
auto expected=forever_aura_update({player,false,{{32,data}}});auto packets=ghosts.flush({{4509,player}});
check(packets.size()==1 && packets[0].body==expected,"self GO context not projected");
u.kind=0;u.fields={{F::UNIT_FIELD_AURA+32,0}};ghosts.consume({u});ghosts.flush({{4509,player}});
check(!ghosts.units.at(4509).server_self_cast[32],"removed holder retained caster context");
}
{ // Only the withheld initial owner defers fallback identity allocation.
NativeAuraTranslation t;uint64_t counter=0x8000000000ULL;
LegacyObjectUpdate held;held.guid=4509;held.kind=3;held.type=4;
held.fields={{F::UNIT_FIELD_AURA,1243},{F::UNIT_FIELD_AURAFLAGS,5}};
auto peer=held;peer.guid=4510;t.consume({held,peer});
check(t.bind_casters(70,0,counter,4509)==1 && counter==0x8000000001ULL,
"deferred owner prevented unrelated aura presentation");
check(!t.units.at(4509).caster_cast[0] && t.units.at(4510).caster_cast[0],
"fallback identity assigned to withheld owner");
check(t.bind_casters(70,0,counter)==1 && counter==0x8000000002ULL,
"released owner did not receive one fallback identity");
}
{ // Observed source casters: self-only spell or own GO -> self-cast context (NOCASTER); other caster -> CastUnit.
NativeAuraTranslation t;uint64_t counter=0x8000000000ULL;auto healer=forever_player_guid(4510,70);
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;u.fields={{F::UNIT_FIELD_AURAFLAGS,0x5555}};
t.consume({u});t.flush({{4509,player}});
t.observe_go(4510,1243,{4509});t.observe_go(4509,10901,{4509});t.observe_go(4510,1459,{4509});t.observe_go(4511,1459,{4509});
u.kind=0;u.fields={{F::UNIT_FIELD_AURA,15473},{F::UNIT_FIELD_AURA+1,10901},{F::UNIT_FIELD_AURA+2,1243},{F::UNIT_FIELD_AURA+3,1459}};
t.consume({u});
// Gitea #12: the ambiguous-caster slot 3 gets a CastID too (empty CastID asserts in GetAuraDataBySpellID).
check(!t.bind_casters(0,0,counter) && t.bind_casters(70,0,counter)==4 && counter==0x8000000004ULL,"self casters + other/unknown-caster CastID not bound exactly once");
const auto& h=t.units.at(4509);
check(h.server_self_cast[0] && h.server_self_cast[1] && !h.server_self_cast[2] && !h.server_self_cast[3],"self-cast context on wrong slots");
check(h.caster[2]==4510 && !h.caster[3],"observed/ambiguous caster wrong");
check(h.caster_cast[2] && h.caster_cast[3] && !h.caster_cast[0],"other/unknown-caster CastID on wrong slots");
auto contains=[](const Bytes& b,const Bytes& n){return std::search(b.begin(),b.end(),n.begin(),n.end())!=b.end();};
auto packets=t.flush({{4509,player},{4510,healer}});
check(packets.size()==1 && contains(packets[0].body,pack_guid(healer)),"observed caster CastUnit missing");
check(contains(packets[0].body,pack_guid(*h.caster_cast[3])),"unknown-caster aura sent without its CastID");
u.fields={{F::UNIT_FIELD_AURA+2,0}};t.consume({u});
check(!t.units.at(4509).caster[2] && !t.units.at(4509).caster_cast[2],"removed slot kept caster");
}
{ // Gitea #7 Sacrifice 7812: the Voidwalker caster is destroyed (instakill) before the aura flush.
NativeAuraTranslation t;auto pet=forever_player_guid(4512,70);
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;u.fields={{F::UNIT_FIELD_AURAFLAGS,0x5555}};
t.consume({u});t.flush({{4509,player}});
t.remember_caster(4512,pet);t.observe_go(4512,7812,{4509,4512});
u.kind=0;u.fields={{F::UNIT_FIELD_AURA,7812}};t.consume({u});
auto contains=[](const Bytes& b,const Bytes& n){return std::search(b.begin(),b.end(),n.begin(),n.end())!=b.end();};
auto packets=t.flush({{4509,player}});
check(packets.size()==1 && contains(packets[0].body,pack_guid(pet)),"departed caster CastUnit missing");
}
{ // Sacrifice bubble: an aura from another caster must name a cast (CastID) or the client starts no state kit.
NativeAuraTranslation t;auto pet=forever_player_guid(4512,70);uint64_t counter=0x8000000000ULL;
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;u.fields={{F::UNIT_FIELD_AURAFLAGS,0x5555}};
t.consume({u});t.flush({{4509,player}});
t.remember_caster(4512,pet);t.observe_go(4512,7812,{4509,4512});
u.kind=0;u.fields={{F::UNIT_FIELD_AURA,7812}};t.consume({u});
check(t.bind_casters(70,1,counter)==1,"other-caster aura got no CastID");
auto cast=forever_server_cast_guid(70,1,7812,0x8000000001ULL);
auto contains=[](const Bytes& b,const Bytes& n){return std::search(b.begin(),b.end(),n.begin(),n.end())!=b.end();};
auto packets=t.flush({{4509,player}});
check(packets.size()==1 && contains(packets[0].body,pack_guid(cast)) && contains(packets[0].body,pack_guid(pet)),"other-caster aura lacks CastID+CastUnit");
check(t.bind_casters(70,1,counter)==0,"CastID minted twice");
}
{ // Owner 2026-11-29: auras held at creation (no caster) and refreshed by a recast (no field change) rebind.
NativeAuraTranslation t;uint64_t counter=0x8000000000ULL;auto healer=forever_player_guid(4510,70);
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;
u.fields={{F::UNIT_FIELD_AURA,27681},{F::UNIT_FIELD_AURA+1,21564},{F::UNIT_FIELD_AURA+2,1459},{F::UNIT_FIELD_AURA+3,1459},{F::UNIT_FIELD_AURAFLAGS,0x5555}};
t.consume({u});t.bind_casters(70,0,counter);t.flush({{4509,player}});
check(!t.units.at(4509).caster[0] && !t.units.at(4509).caster[1],"creation auras unexpectedly had casters");
t.observe_go(4510,27681,{4509});t.observe_go(4509,21564,{4509});t.observe_go(4510,1459,{4509});
const auto& h=t.units.at(4509);
check(h.caster[0]==4510 && h.caster[1]==4509 && !h.caster[2] && !h.caster[3],"refresh GO did not rebind the single holding slot");
t.bind_casters(70,0,counter);
check(h.caster_cast[0] && h.server_self_cast[1],"rebound slots without CastID / self context");
auto contains=[](const Bytes& b,const Bytes& n){return std::search(b.begin(),b.end(),n.begin(),n.end())!=b.end();};
auto packets=t.flush({{4509,player},{4510,healer}});
check(packets.size()==1 && contains(packets[0].body,pack_guid(healer)),"refreshed aura not resent with its caster");
}
{ // Source GM visibility AddAura(16380): no cast event, self-only holder.
NativeAuraTranslation gm;uint64_t counter=0x8000000000ULL;
check(!gm.bind_self_applied(4509,70,0,counter),"missing GM holder synthesized");
LegacyObjectUpdate u;u.guid=4509;u.kind=3;u.type=4;u.fields={{F::UNIT_FIELD_AURA+3,16380},{F::UNIT_FIELD_AURAFLAGS,0x55000},{F::UNIT_FIELD_AURALEVELS,60u<<24},{F::UNIT_FIELD_AURA+4,11392}};
gm.consume({u});
check(!gm.bind_self_applied(4509,0,0,counter) && !gm.bind_self_applied(4509,70,0x2000,counter),"invalid cast scope accepted");
check(gm.bind_self_applied(4509,70,0,counter)==1 && counter==0x8000000001ULL,"GM holder self-cast context not bound once");
auto cast=forever_server_cast_guid(70,0,16380,0x8000000001ULL);
check(gm.units.at(4509).server_self_cast[3]==cast && !gm.units.at(4509).server_self_cast[4],"unprofiled holder received self-cast context");
check(!gm.bind_self_applied(4509,70,0,counter) && counter==0x8000000001ULL,"bound GM holder rebound");
AuraSlotData gm_data{16380,1,2,60,1};gm_data.server_self_cast=cast;
auto packets=gm.flush({{4509,player}});
check(packets.size()==1 && packets[0].body==forever_aura_update({player,true,{{3,gm_data},{4,AuraSlotData{11392,1,2,0,1}}}}),"GM self-cast presentation missing");
u.kind=0;u.fields={{F::UNIT_FIELD_AURA+3,0}};gm.consume({u});gm.flush({{4509,player}});
check(!gm.units.at(4509).server_self_cast[3],"removed GM holder retained context");
u.fields={{F::UNIT_FIELD_AURA+3,16380}};gm.consume({u});
check(gm.bind_self_applied(4509,70,0,counter)==1 && gm.units.at(4509).server_self_cast[3]==forever_server_cast_guid(70,0,16380,0x8000000002ULL),"reapplied GM holder reused stale cast");
NativeAuraTranslation creature;LegacyObjectUpdate c=u;c.kind=3;c.type=3;creature.consume({c});
check(!creature.bind_self_applied(4509,70,0,counter),"creature holder received player self-cast context");
NativeAuraTranslation twice;LegacyObjectUpdate d=u;d.kind=3;d.fields={{F::UNIT_FIELD_AURA,16380},{F::UNIT_FIELD_AURA+1,16380}};twice.consume({d});
check(!twice.bind_self_applied(4509,70,0,counter),"ambiguous duplicate holder bound");
check(source_self_applied_aura_profile::contains(16380) && !source_self_applied_aura_profile::contains(8326) && aura_visual_profile::link(16380)==242512,"GM self-applied profile drifted");
}
// Exhaust the generated mount family. A holder alone cannot establish a
// mounted effect; a genuine mount-display update enables it, and clearing
// that field removes it even if holder removal arrives in another packet.
for(const auto& row:source_mount_aura_profile::Rows){
NativeAuraTranslation mounts;LegacyObjectUpdate holder;
holder.kind=2;holder.type=4;holder.guid=4509;
holder.fields={{F::UNIT_FIELD_AURA,row.spell},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
mounts.consume({holder});mounts.flush({{4509,player}});
check(!mounts.units.at(4509).shown[0],"mount holder without source mount display exposed");
LegacyObjectUpdate display;display.guid=4509;display.fields={{F::UNIT_FIELD_MOUNTDISPLAYID,1}};
mounts.consume({display});auto visible=mounts.flush({{4509,player}});
AuraSlotUpdate expected{player,false,{{0,AuraSlotData{row.spell,row.active_flags,2,60,1}}}};
check(visible.size()==1 && visible[0].body==forever_aura_update(expected),"reviewed mounted-effect projection mismatch");
check(mounts.units.at(4509).active_mask(row.spell)==row.active_flags,"mount active mask mismatch");
display.fields[F::UNIT_FIELD_MOUNTDISPLAYID]=0;mounts.consume({display});visible=mounts.flush({{4509,player}});
expected.slots[0].data.reset();check(visible.size()==1 && visible[0].body==forever_aura_update(expected),"source dismount did not clear native mounted aura");
}
LegacyObjectUpdate create;create.kind=2;create.type=4;create.guid=4509;
create.fields={{F::UNIT_FIELD_AURA,433},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
state.consume({create});check(state.flush({}).empty(),"unbound unit must wait");check(state.dirty.size()==1,"pending binding lost");
auto out=state.flush({{4509,player}});check(out.size()==1 && out[0].opcode==0x690011,"initial source aura missing");
{auto bytes=state.snapshot(4509);Reader r(bytes);check(bytes.size()==1376 && r.le(4)==1 && r.le(4)==1376,"public aura ABI mismatch");check(r.le(4)==1 && r.le(4)==0 && r.le(4)==1 && r.le(4)==1 && r.le(4)==1 && r.le(4)==0,"aura diagnostic counters mismatch");check(r.le(4)==433 && r.le(4)==5 && r.le(4)==60 && r.le(4)==0 && r.le(4)==1 && r.le(4)==236680 && r.le(4)==1,"aura source/translation metadata mismatch");}
{auto bytes=state.snapshot(999999);Reader r(bytes);r.take(16);check(r.le(4)==0 && r.le(4)==0 && r.le(4)==0,"unknown unit diagnostics fabricated slots");}
AuraSlotUpdate expected{player,true,{{0,AuraSlotData{433,1,2,60,1}}}};check(out[0].body==forever_aura_update(expected),"initial packing mismatch");check(state.flush({{4509,player}}).empty(),"duplicate aura update");
LegacyObjectUpdate sparse;sparse.guid=4509;sparse.fields={{F::UNIT_FIELD_AURAAPPLICATIONS,254},{F::UNIT_FIELD_AURALEVELS,45}};
state.consume({sparse});expected.all=false;expected.slots[0].data->applications=255;expected.slots[0].data->level=45;
out=state.flush({{4509,player}});check(out.size()==1 && out[0].body==forever_aura_update(expected),"packed application count-minus-one mismatch");
sparse.fields={{F::UNIT_FIELD_AURA,999999}};state.consume({sparse});out=state.flush({{4509,player}});expected.slots[0].data.reset();check(out.size()==1 && out[0].body==forever_aura_update(expected) && state.unsupported,"unsupported replacement must remove old aura");
sparse.fields={{uint16_t(F::UNIT_FIELD_AURA+47),772},{uint16_t(F::UNIT_FIELD_AURAFLAGS+5),8u<<28},{uint16_t(F::UNIT_FIELD_AURALEVELS+11),60u<<24},{uint16_t(F::UNIT_FIELD_AURAAPPLICATIONS+11),3u<<24}};
state.consume({sparse});out=state.flush({{4509,player}});expected.slots={{47,AuraSlotData{772,1,16,60,4}}};check(out.size()==1 && out[0].body==forever_aura_update(expected),"last slot packed fields mismatch");
sparse.fields={{uint16_t(F::UNIT_FIELD_AURA+47),0}};state.consume({sparse});out=state.flush({{4509,player}});expected.slots[0].data.reset();check(out.size()==1 && out[0].body==forever_aura_update(expected),"aura clear missing");
LegacyObjectUpdate removed;removed.kind=4;removed.removed={4509};state.consume({removed});check(state.units.empty() && state.dirty.empty(),"removed source unit cache retained");
state.consume({create});out=state.flush({{4509,player}});check(out.size()==1 && (out[0].body[0]&128),"recreated source unit must initialize afresh");state.forget(4509);
NativeAuraTranslation potion;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,3680},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
potion.consume({create});out=potion.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,true,{{0,AuraSlotData{3680,1,2,60,1}}}}),"genuine lesser invisibility aura initialization missing");
sparse.fields={{F::UNIT_FIELD_AURA,11392}};potion.consume({sparse});out=potion.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,AuraSlotData{11392,1,2,60,1}}}}),"genuine invisibility aura replacement missing");
sparse.fields={{F::UNIT_FIELD_AURA,0}};potion.consume({sparse});out=potion.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),"potion expiry/clear missing");
create.fields={{F::UNIT_FIELD_AURA,433},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
for(auto flags:{0u,2u,12u,15u}){create.fields[F::UNIT_FIELD_AURAFLAGS]=flags;state.consume({create});out=state.flush({{4509,player}});check(out.size()==1 && out[0].body==forever_aura_update({player,true,{}}),"unsupported source aura flags presented");state.forget(4509);}
for(unsigned n=0;n<4096;++n){create.guid=n+1;state.consume({create});}create.guid=4097;bool rejected=false;try{state.consume({create});}catch(const Error&){rejected=true;}check(rejected,"aura cache bound missing");
// A GO only records a bounded candidate. A real fresh source slot in the
// next update establishes application; preexisting/ambiguous state never
// acquires invented caster or item metadata.
Guid128 item{};item[0]=0xa2;item[1]=0x8d;item[15]=0x0c;
AuraSelfCastContext context{forever_server_cast_guid(70,0,21991,0x8000000001ULL),item};
auto empty_player=[&](){NativeAuraTranslation s;LegacyObjectUpdate u;u.guid=4509;u.kind=2;u.type=4;s.consume({u});s.flush({{4509,player}});return s;};
auto fresh_update=[&](){LegacyObjectUpdate u;u.guid=4509;u.fields={{F::UNIT_FIELD_AURA,21991},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};return u;};
auto observe=[&](NativeAuraTranslation& s){return s.observe_equip_cast(4509,21991,context.cast,context.item);};
// Initial login holders can receive metadata only before their first flush,
// after the caller verifies the genuine equip GO. No holder is fabricated.
auto initial_player=[&](){NativeAuraTranslation s;auto u=fresh_update();u.kind=2;u.type=4;s.consume({u});return s;};
auto bind_initial=[&](NativeAuraTranslation& s){return s.bind_initial_equip_cast(4509,21991,context.cast,context.item,70);};
auto initial=initial_player();auto initial_before=initial.units.at(4509);
check(bind_initial(initial),"initial exact equip holder refused metadata");
const auto& initial_after=initial.units.at(4509);
check(initial_after.self_cast[0]==context && initial_after.initial && initial_after.spells==initial_before.spells &&
initial_after.flags==initial_before.flags && initial_after.levels==initial_before.levels && initial_after.applications==initial_before.applications &&
initial_after.dirty==initial_before.dirty && initial_after.shown==initial_before.shown &&
initial_after.presented_realm==initial_before.presented_realm && !initial_after.equip_candidate,
"initial binding changed source holder or presentation state");
check(!bind_initial(initial),"initial holder accepted replacement creation context");
out=initial.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,true,{{0,AuraSlotData{21991,1,2,60,1,context}}}}),"initial enriched holder did not reach original aura writer");
check(!bind_initial(initial),"presented holder accepted initial-only metadata");
NativeAuraTranslation no_initial;check(!bind_initial(no_initial) && no_initial.units.empty(),"initial binding created an unknown unit");
initial=initial_player();initial.units.at(4509).source_type=3;check(!bind_initial(initial) && !initial.units.at(4509).self_cast[0],"initial creature accepted player-equip metadata");
initial=initial_player();auto duplicate=fresh_update();duplicate.fields={{F::UNIT_FIELD_AURA+1,21991}};initial.consume({duplicate});
check(!bind_initial(initial) && !initial.units.at(4509).self_cast[0] && !initial.units.at(4509).self_cast[1],"duplicate initial source holders acquired guessed metadata");
initial=initial_player();
for(uint32_t realm:{0u,71u,0x2000u})check(!initial.bind_initial_equip_cast(4509,21991,context.cast,context.item,realm),"invalid/mismatching initial realm accepted");
check(!initial.bind_initial_equip_cast(4509,21991,forever_server_cast_guid(71,0,21991,1),context.item,70),"foreign cast realm accepted initial binding");
check(!initial.bind_initial_equip_cast(4509,433,context.cast,context.item,70),"wrong initial spell identity accepted");
check(!initial.bind_initial_equip_cast(4509,21991,context.cast,{},70),"missing initial cast item accepted");
auto missing_slot=initial_player();missing_slot.units.at(4509).spells[0]=0;check(!bind_initial(missing_slot),"initial binding fabricated an absent holder");
check(bind_initial(initial),"initial context fixture failed");initial.consume({removed});check(initial.units.empty(),"source removal retained initial binding");
initial=initial_player();check(bind_initial(initial),"initial recreate fixture failed");auto initial_recreate=fresh_update();initial_recreate.kind=2;initial_recreate.type=4;initial.consume({initial_recreate});
check(initial.units.at(4509).initial && !initial.units.at(4509).self_cast[0],"source recreate inherited initial holder metadata");
initial=initial_player();check(bind_initial(initial),"initial clear fixture failed");LegacyObjectUpdate initial_clear;initial_clear.guid=4509;initial_clear.fields={{F::UNIT_FIELD_AURA,0}};initial.consume({initial_clear});
check(!initial.units.at(4509).self_cast[0],"source clear retained initial binding");
auto enriched=empty_player();check(observe(enriched),"valid equip candidate rejected");
check(enriched.flush({{4509,player}}).empty() && !enriched.units.at(4509).self_cast[0],"GO alone fabricated applied aura");
auto applied=fresh_update();enriched.consume({applied});out=enriched.flush({{4509,player}});
AuraSlotUpdate enriched_expected{player,false,{{0,AuraSlotData{21991,1,2,60,1,context}}}};
check(out.size()==1 && out[0].body==forever_aura_update(enriched_expected) && enriched.units.at(4509).self_cast[0]==context,"fresh source slot lost verified creation context");
check(!observe(enriched),"active holder accepted replacement cast identity");
LegacyObjectUpdate refresh;refresh.guid=4509;refresh.fields={{F::UNIT_FIELD_AURALEVELS,61}};enriched.consume({refresh});out=enriched.flush({{4509,player}});enriched_expected.slots[0].data->level=61;
check(out.size()==1 && out[0].body==forever_aura_update(enriched_expected) && enriched.units.at(4509).self_cast[0]==context,"refresh changed initial aura identity");
LegacyObjectUpdate clear;clear.guid=4509;clear.fields={{F::UNIT_FIELD_AURA,0}};enriched.consume({clear});enriched.flush({{4509,player}});
check(!enriched.units.at(4509).self_cast[0],"clear retained old creation metadata");
enriched.consume({applied});out=enriched.flush({{4509,player}});
check(!enriched.units.at(4509).self_cast[0] && out[0].body==forever_aura_update({player,false,{{0,AuraSlotData{21991,1,2,60,1}}}}),"later unobserved holder inherited old cast");
auto lost=empty_player();check(observe(lost),"health-loss candidate rejected");
// Pinned Tortoise UpdateFields.h: UNIT_FIELD_HEALTH=0x10+OBJECT_END(6).
LegacyObjectUpdate health;health.guid=4509;health.fields={{22,100}};lost.consume({health});
check(!lost.units.at(4509).equip_candidate,"health-only next update retained stale candidate");lost.consume({applied});check(!lost.units.at(4509).self_cast[0],"health-discarded candidate enriched later holder");
lost=empty_player();observe(lost);auto different=applied;different.fields[F::UNIT_FIELD_AURA]=433;lost.consume({different});lost.consume({applied});check(!lost.units.at(4509).self_cast[0],"nonmatching update retained stale candidate");
lost=empty_player();observe(lost);check(!observe(lost) && lost.units.at(4509).equip_candidate->ambiguous,"duplicate candidates not invalidated");lost.consume({applied});check(!lost.units.at(4509).self_cast[0],"ambiguous cast enriched holder");
lost=empty_player();observe(lost);auto ambiguous=applied;ambiguous.fields[F::UNIT_FIELD_AURA+1]=21991;lost.consume({ambiguous});check(!lost.units.at(4509).self_cast[0] && !lost.units.at(4509).self_cast[1],"multiple fresh slots fabricated unique association");
lost=empty_player();observe(lost);auto unrelated=health;unrelated.guid=4508;lost.consume({unrelated});check(lost.units.at(4509).equip_candidate.has_value(),"other unit update consumed candidate");lost.consume({applied});check(lost.units.at(4509).self_cast[0]==context,"other unit update lost exact source association");
lost=empty_player();observe(lost);auto recreate=applied;recreate.kind=2;recreate.type=4;lost.consume({recreate});check(!lost.units.at(4509).self_cast[0] && !lost.units.at(4509).equip_candidate,"recreate inherited pending metadata");
check(!observe(lost),"unflushed initial holder accepted context");lost.flush({{4509,player}});check(!observe(lost),"initial login holder accepted later cast identity");
lost=empty_player();observe(lost);lost.consume({removed});check(lost.units.empty(),"removed unit retained equip candidate");
lost=empty_player();observe(lost);lost=NativeAuraTranslation{};check(!observe(lost),"session reset retained source state");
lost=empty_player();check(!lost.observe_equip_cast(9999,21991,context.cast,item),"unknown unit accepted context");
NativeAuraTranslation creature;auto creature_create=create;creature_create.guid=4509;creature_create.type=3;creature_create.fields.clear();creature.consume({creature_create});creature.flush({{4509,player}});check(!observe(creature),"creature accepted direct player-equip context");
for(auto bad:{AuraSelfCastContext{{},item},AuraSelfCastContext{context.cast,{}},AuraSelfCastContext{context.cast,player},AuraSelfCastContext{forever_server_cast_guid(70,0,433,1),item}}){
check(!valid_self_equip_aura_context(21991,bad) && !lost.observe_equip_cast(4509,21991,bad.cast,bad.item),"malformed creation context accepted");
}
check(!lost.observe_equip_cast(4509,21991,forever_server_cast_guid(71,0,21991,1),item),"foreign realm accepted source correlation");
check(!lost.observe_equip_cast(4509,999999,forever_server_cast_guid(70,0,999999,1),item),"unreviewed spell accepted source correlation");
auto wrong_context=context;wrong_context.cast[5]=1;check(!valid_self_equip_aura_context(21991,wrong_context),"unreviewed cast serverId accepted");
wrong_context=context;wrong_context.cast[8]=(wrong_context.cast[8]&~0x3f)|2;check(!valid_self_equip_aura_context(21991,wrong_context),"wrong cast source accepted");
wrong_context=context;wrong_context.item[8]=1;check(!valid_self_equip_aura_context(21991,wrong_context),"malformed item factory bits accepted");
check(!valid_self_equip_aura_context(0,context) && !valid_self_equip_aura_context(0x800000,context),"aura context spell bounds missing");
lost=empty_player();lost.consume({different});lost.flush({{4509,player}});check(observe(lost),"replacement candidate rejected prematurely");lost.consume({applied});check(!lost.units.at(4509).self_cast[0],"nonzero replacement treated as a fresh slot");
lost=empty_player();observe(lost);lost.consume({applied});check(lost.units.at(4509).self_cast[0]==context,"recreate fixture missing context");lost.consume({recreate});check(!lost.units.at(4509).self_cast[0],"recreate retained active holder context");
// Independent pinned original native writer, executed offline only.
const char* codec_path=std::getenv("GW_TEST_CODEC_PATH");
if (!codec_path) codec_path = "build/forever-schema/libforever_codec.so";
if (test_fixture::optional_codec_missing(codec_path)) return 77;
void* library = dlopen(codec_path, RTLD_NOW | RTLD_LOCAL);
if (!library) throw Error(dlerror());
auto codec=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(codec,"source serializer export missing");unsigned variants=0;
auto parity=[&](const AuraSlotUpdate& update){auto wire=neutral(update);Bytes original(GW_CODEC_BOUND);uint32_t written=0;check(codec(GW_CODEC_AURA_UPDATE,wire.data(),wire.size(),0,original.data(),original.size(),&written)==0,"source aura serializer rejected");original.resize(written);check(original==forever_aura_update(update),"source aura writer mismatch");++variants;};
for(bool all:{false,true}){parity({player,all,{}});for(uint16_t slot:{0,47}){parity({player,all,{{slot,std::nullopt}}});for(uint16_t flags:{2,16})for(uint8_t apps:{1,255})for(uint32_t mask:{1,2,4})parity({player,all,{{slot,AuraSlotData{433,mask,flags,uint16_t(apps==1?0:255),apps}}}});}}
AuraSlotUpdate full{player,true,{}};for(unsigned i=0;i<48;++i)full.slots.push_back({uint16_t(i),AuraSlotData{433,1,2,60,1}});parity(full);
for(uint32_t spell:{3680,11392,16380,21991,53202,999999})parity({player,false,{{0,AuraSlotData{spell,1,2,60,1}}}});
{AuraSlotData gm{16380,1,2,60,1};gm.server_self_cast=forever_server_cast_guid(70,0,16380,0x8000000001ULL);for(bool all:{false,true})parity({player,all,{{3,gm}}});}
for(bool all:{false,true})for(uint16_t slot:{0,47})for(uint16_t flags:{2,16})parity({player,all,{{slot,AuraSlotData{21991,1,flags,60,1,context}}}});
parity({player,false,{{0,AuraSlotData{21991,1,2,60,1,context}},{1,AuraSlotData{433,1,2,60,1}},{47,std::nullopt}}});
check(aura_visual_profile::link(11392)==243607 && aura_visual_profile::link(21991)==248700 && !aura_visual_profile::link(999999),"visual link ID domain mismatch");
check(aura_visual_profile::link(53202)==537886,"source-owned timed aura visual link missing");
NativeAuraTranslation timed;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,53202},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
timed.consume({create});out=timed.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,true,{{0,AuraSlotData{53202,1,2,60,1}}}}),"source-owned timed aura filtered by shared mask");
sparse.fields={{F::UNIT_FIELD_AURA,0}};timed.consume({sparse});out=timed.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),"source timed aura removal missing");
NativeAuraTranslation talisman;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,21991},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
talisman.consume({create});out=talisman.flush({{4509,player}});check(out.size()==1 && out[0].body==forever_aura_update({player,true,{{0,AuraSlotData{21991,1,2,60,1}}}}),"source equip aura visual missing");
sparse.fields={{F::UNIT_FIELD_AURA,0}};talisman.consume({sparse});out=talisman.flush({{4509,player}});check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),"equip aura removal missing");
#ifdef GW_TEST_COMPOSITE_AURA
check(aura_visual_profile::link(49369),"staged composite source visual profile required");
#endif
if(aura_visual_profile::link(49369)){
// Source49369has instantaneous extra attacks at effect0 and its sole
// persistent Physical aura at effect1. A nonempty holder must emit
// active mask2, not0or1; only the real source clear removes it.
NativeAuraTranslation composite;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,49369},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,50}};
composite.consume({create});out=composite.flush({{4509,player}});
AuraSlotUpdate expected{player,true,{{0,AuraSlotData{49369,2,2,50,1}}}};
check(out.size()==1 && out[0].body==forever_aura_update(expected) && composite.unsupported==0,"composite source holder lost or instant effect treated as an aura");
parity(expected);
sparse.fields={{F::UNIT_FIELD_AURA,0}};composite.consume({sparse});out=composite.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),"composite source aura removal missing");
}
if(std::getenv("GW_TEST_SET_HASTE_AURA"))check(aura_visual_profile::link(49367)==537889,"staged set-haste profile required");
if(aura_visual_profile::link(49367)){
// Genuine49367has one haste aura at effect0. The server holder, not
// local equipment guesses, determines application and removal.
NativeAuraTranslation set_haste;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,49367},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
set_haste.consume({create});out=set_haste.flush({{4509,player}});
AuraSlotUpdate expected{player,true,{{0,AuraSlotData{49367,1,2,60,1}}}};
check(out.size()==1 && out[0].body==forever_aura_update(expected) && set_haste.unsupported==0,"source set-haste holder lost by shared aura profile");
parity(expected);
check(set_haste.flush({{4509,player}}).empty(),"unchanged set-haste holder resent");
sparse.fields={{F::UNIT_FIELD_AURA,0}};set_haste.consume({sparse});out=set_haste.flush({{4509,player}});
check(out.size()==1 && out[0].body==forever_aura_update({player,false,{{0,std::nullopt}}}),"source set-haste clear missing");
}
// Source duration packets identify the player's slot only and can precede
// creation. No duration is inferred from a spell name or static definition.
auto duration_packet=[](uint32_t slot,uint32_t ms){Bytes b;le(b,slot,1);le(b,ms,4);return b;};
NativeAuraTranslation durations;create.guid=4509;create.fields={{F::UNIT_FIELD_AURA,433},{F::UNIT_FIELD_AURAFLAGS,5},{F::UNIT_FIELD_AURALEVELS,60}};
durations.consume_duration(4509,duration_packet(0,6000),1000);
check(durations.units.empty() && durations.flush({},1100).empty(),"duration fabricated an object");
durations.consume({create});out=durations.flush({{4509,player}},1250);
AuraSlotData timer_data{433,1,2,60,1};timer_data.remaining_ms=5750;
AuraSlotUpdate timer_expected{player,true,{{0,timer_data}}};
check(out.size()==1 && out[0].body==forever_aura_update(timer_expected),"pre-create source timing or delay lost");parity(timer_expected);
check(durations.flush({{4509,player}},2000).empty(),"time passing resent an unchanged aura");
durations.consume_duration(4509,duration_packet(0,9000),2000);timer_expected.all=false;timer_expected.slots[0].data->remaining_ms=9000;
out=durations.flush({{4509,player}},2000);check(out.size()==1 && out[0].body==forever_aura_update(timer_expected),"timer-only refresh lost");parity(timer_expected);
// The slot-only refresh may belong to the next source holder. This mirrors
// the source protocol's ordering without inventing a spell/caster identity.
sparse.fields={{F::UNIT_FIELD_AURA,772}};durations.consume({sparse});timer_expected.slots[0].data->spell=772;timer_expected.slots[0].data->remaining_ms=8500;
out=durations.flush({{4509,player}},2500);check(out.size()==1 && out[0].body==forever_aura_update(timer_expected),"pre-field timing discarded");parity(timer_expected);
sparse.fields={{F::UNIT_FIELD_AURA,433}};durations.consume({sparse});timer_expected.slots[0].data->spell=433;timer_expected.slots[0].data->remaining_ms.reset();
out=durations.flush({{4509,player}},2600);check(out.size()==1 && out[0].body==forever_aura_update(timer_expected),"new holder inherited old timer");
durations.consume_duration(4509,duration_packet(0,100),3000);timer_expected.slots[0].data->remaining_ms=0;
out=durations.flush({{4509,player}},3101);check(out.size()==1 && out[0].body==forever_aura_update(timer_expected) && durations.units.at(4509).spells[0]==433,"timer expiry fabricated source removal");parity(timer_expected);
durations.consume_duration(4509,duration_packet(0,0xffffffff),3200);timer_expected.slots[0].data->remaining_ms.reset();
out=durations.flush({{4509,player}},3200);check(out.size()==1 && out[0].body==forever_aura_update(timer_expected),"permanent duration became a finite timer");
durations.consume_duration(4509,duration_packet(0,1000),3300);sparse.fields={{F::UNIT_FIELD_AURA,0}};durations.consume({sparse});durations.flush({{4509,player}},3300);
check(!durations.durations.at(4509)[0].remaining_ms,"source clear retained duration");
durations.consume({create});durations.consume_duration(4509,duration_packet(0,1000),3400);durations.consume({create});
check(!durations.durations.contains(4509),"object recreation inherited duration");
durations.consume_duration(4509,duration_packet(0,1000),3500);durations.forget(4509);check(durations.durations.empty(),"object removal retained duration");
for(auto bad:{Bytes{},Bytes{0},duration_packet(48,100),duration_packet(0,0x80000000),Bytes{0,1,0,0,0,0}}){
bool fail=false;try{durations.consume_duration(4509,bad,4000);}catch(const Error&){fail=true;}check(fail,"malformed source duration accepted");
}
for(uint64_t bad:{uint64_t(0),uint64_t(0xf130000000000001)}){
bool fail=false;try{durations.consume_duration(bad,duration_packet(0,1000),4000);}catch(const Error&){fail=true;}check(fail,"non-player duration accepted");
}
check(durations.units.empty() && durations.durations.empty(),"invalid duration created source state");
durations.consume_duration(4509,duration_packet(0,1000),5000);
{bool fail=false;try{durations.consume_duration(4509,duration_packet(0,2000),4999);}catch(const Error&){fail=true;}
check(fail && durations.durations.at(4509)[0].remaining_ms==1000,"regressing receive time changed duration");}
durations.consume({create});{bool fail=false;try{durations.flush({{4509,player}},4999);}catch(const Error&){fail=true;}check(fail,"regressing flush time accepted");}
durations.forget(4509);create.type=3;durations.consume({create});
{bool fail=false;try{durations.consume_duration(4509,duration_packet(0,1000),5000);}catch(const Error&){fail=true;}
check(fail && durations.durations.empty(),"creature acquired player-local duration");}create.type=4;durations.forget(4509);
for(unsigned i=1;i<=4096;++i)durations.consume_duration(i,duration_packet(0,1000),5000);
{bool fail=false;try{durations.consume_duration(4097,duration_packet(0,1000),5000);}catch(const Error&){fail=true;}check(fail,"duration cache exceeded bound");}
for(auto ms:{0u,1u,6000u,0x7fffffffu}){timer_expected.slots[0].data->remaining_ms=ms;parity(timer_expected);}
auto timed_context=enriched_expected;timed_context.slots[0].data->remaining_ms=6000;parity(timed_context);
AuraSlotData sourced_ghost{8326,1,16,60,1};sourced_ghost.server_self_cast=forever_server_cast_guid(70,0,8326,0x8000000001ULL);
AuraSlotUpdate sourced_update{player,false,{{32,sourced_ghost}}};parity(sourced_update);
sourced_update.slots[0].data->remaining_ms=1000;parity(sourced_update);
for(auto invalid:{Guid128{},forever_server_cast_guid(71,0,8326,0x8000000001ULL),forever_server_cast_guid(70,0,8327,0x8000000001ULL),forever_server_cast_guid(70,0,8326,0x7fffffffffULL)}){
auto bad=sourced_update;bad.slots[0].data->server_self_cast=invalid;
bool fail=false;try{forever_aura_update(bad);}catch(const Error&){fail=true;}check(fail,"unbound source cast context accepted");
}
auto source_wire=neutral(sourced_update);
for(size_t n=0;n<source_wire.size();++n){Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,source_wire.data(),n,0,original.data(),original.size(),&written)!=0 && written==0,"truncated source cast accepted");}
auto wire=neutral(full);for(size_t n=0;n<wire.size();++n){Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,wire.data(),n,0,original.data(),original.size(),&written)!=0 && written==0,"truncated aura accepted");}
auto timed_wire=neutral(timed_context);
for(size_t n=0;n<timed_wire.size();++n){Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,timed_wire.data(),n,0,original.data(),original.size(),&written)!=0 && written==0,"truncated timing accepted");}
auto context_wire=neutral({player,false,{{0,AuraSlotData{21991,1,2,60,1,context}}}});
for(size_t n=0;n<context_wire.size();++n){Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,context_wire.data(),n,0,original.data(),original.size(),&written)!=0 && written==0,"truncated creation context accepted");}
auto reject_wire=[&](Bytes b){Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,b.data(),b.size(),0,original.data(),original.size(),&written)!=0 && written==0,"malformed context wire accepted");};
auto invalid_timing=timer_expected;invalid_timing.slots[0].data->remaining_ms=0x80000000u;
{bool fail=false;try{forever_aura_update(invalid_timing);}catch(const Error&){fail=true;}check(fail,"invalid native timing accepted");reject_wire(neutral(invalid_timing));}
auto timing_presence=neutral(timer_expected);timing_presence[33]=2;reject_wire(timing_presence);
auto wrong_marker=context_wire;wrong_marker[16]=0x82;reject_wire(wrong_marker);
auto wrong_presence=context_wire;wrong_presence[33]=2;reject_wire(wrong_presence);
auto trailing_context=context_wire;trailing_context.push_back(0);reject_wire(trailing_context);
for(auto bad:{AuraSelfCastContext{{},item},AuraSelfCastContext{context.cast,{}},AuraSelfCastContext{context.cast,player},AuraSelfCastContext{forever_server_cast_guid(71,0,21991,1),item}}){
AuraSlotUpdate invalid{player,false,{{0,AuraSlotData{21991,1,2,60,1,bad}}}};bool fail=false;try{forever_aura_update(invalid);}catch(const Error&){fail=true;}check(fail,"invalid context serialization accepted");reject_wire(neutral(invalid));
}
for(auto invalid:{AuraSlotUpdate{{},true,{}},AuraSlotUpdate{player,true,{{48,std::nullopt}}},AuraSlotUpdate{player,true,{{0,std::nullopt},{0,std::nullopt}}},AuraSlotUpdate{player,false,{{0,AuraSlotData{433,0,2,60,1}}}}}){
bool fail=false;try{forever_aura_update(invalid);}catch(const Error&){fail=true;}check(fail,"invalid aura metadata accepted");auto b=neutral(invalid);Bytes original(GW_CODEC_BOUND);uint32_t written=99;check(codec(GW_CODEC_AURA_UPDATE,b.data(),b.size(),0,original.data(),original.size(),&written)!=0 && written==0,"original bounded adapter accepted invalid domain");}
dlclose(library);std::cout<<variants<<" variants match original aura writer; source packed slots, delayed creation, sparse clears, unknown metadata and cache bounds verified. Offline only.\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}