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

195 lines
15 KiB
C++

#include "world_translation.hpp"
#include "session.h"
#include "gateway/chat.hpp"
#include <algorithm>
#include <cstdlib>
using namespace gateway;
static void check(bool value){if(!value)throw Error("native talent fixture assertion");}
static LegacyPacket initial(std::initializer_list<uint16_t> spells){
Bytes body{0};le(body,spells.size(),2);for(auto spell:spells){le(body,spell,2);le(body,0,2);}le(body,0,2);return {0x12a,std::move(body)};
}
static LegacyPacket values(uint64_t guid,const std::map<uint16_t,uint32_t>& fields,bool full=false){
Bytes body;le(body,1,4);le(body,0,1);le(body,full?3:0,1);Bytes id;le(id,guid,8);append(body,pack_guid(id));
if(full){le(body,4,1);le(body,0,1);}
auto words=fields.rbegin()->first/32+1;std::vector<uint32_t> mask(words);
for(auto [index,value]:fields){(void)value;mask[index/32]|=1u<<(index%32);}
le(body,words,1);for(auto word:mask)le(body,word,4);for(auto [index,value]:fields){(void)index;le(body,value,4);}
return {0xa9,std::move(body)};
}
static std::vector<std::string> messages(const Bytes& bundle){
Reader r(bundle);auto count=r.le(4);std::vector<std::string> out;
for(unsigned i=0;i<count;++i){auto opcode=r.le(4);auto size=r.le(4);auto raw=r.take(size);if(opcode!=0x4c0001)continue;
Reader chat(raw);check(chat.le(1)==0 && chat.le(4)==0);
for(unsigned j=0;j<3;++j)check(unpack_guid(chat,16)==Bytes(16));
chat.le(2);for(unsigned j=0;j<6;++j)chat.le(4);
uint64_t bits=0;for(unsigned j=0;j<7;++j)bits=(bits<<8)|chat.le(1);
check(((bits>>45)&0x7ff)==0);auto text=chat.take((bits>>10)&0xfff);chat.end();out.emplace_back(text.begin(),text.end());
}
r.end();return out;
}
static NativeWorldTranslation world(uint64_t guid=4509){
NativeWorldTranslation w{};w.selected.guid=guid;w.selected.race=1;w.selected.cls=1;w.selected.name="Fixture";w.realm=70;w.self_created=true;return w;
}
static std::vector<std::string> receive(NativeWorldTranslation& w,const LegacyPacket& packet){return messages(w.translate(packet,0x46010046,0,nullptr));}
static LegacyPacket reset_offer(uint64_t guid,uint32_t cost){Bytes body;le(body,guid,8);le(body,cost,4);return {0x2aa,std::move(body)};}
static constexpr uint64_t trainer_guid=(0xf130ULL<<48)|(9010ULL<<24)|99;
static NativeWorldTranslation reset_world(){
auto w=world();receive(w,initial({78,12285}));receive(w,values(4509,{{34,60},{36,257},{1102,50}}));
NativeEntityTranslation::Actor actor;actor.entry=9010;actor.kind=3;actor.sent=true;actor.generation=1;
actor.modern=forever_creature_guid(trainer_guid,9010,70,0);w.entities.actors[trainer_guid]=actor;w.entities.generation=1;
return w;
}
template<class F>static void rejects(F f){try{f();}catch(const Error&){return;}throw Error("expected invalid native talent fixture");}
int main(){
auto w=world();std::optional<LegacyPacket> outgoing;
auto firstSpells = w.translate(initial({78,12664}), 0x46010046, 0, nullptr);
Reader startup(firstSpells);
check(startup.le(4) == 5);
check(startup.le(4) == 0x47007a && startup.le(4) == 25);
check(startup.le(1) == 0 && startup.le(4) == 1491 && startup.le(4) == 1);
check(startup.le(4) == 1491 && startup.take(12) == Bytes(12));
check(startup.le(4) == 0x690019); // Known spells follow their active group.
check(messages(firstSpells).empty() && !w.talent_ready());
auto refreshedSpells = w.translate(initial({78,12664}), 0x46010046, 0, nullptr);
Reader refreshed(refreshedSpells);
check(refreshed.le(4) == 4 && refreshed.le(4) == 0x690019);
auto first=receive(w,values(4509,{{34,60},{36,1u|(1u<<8)},{1102,48}}));
check(first==std::vector<std::string>({"GWTURTLE1 BEGIN 1 4509 1 60 48 2","GWTURTLE1 SPELLS 1 78,12664","GWTURTLE1 END 1"}));
check(w.talent_ready() && w.talent_sequence==1);
check(!w.talent_control("ordinary local chat",outgoing) && !outgoing);
check(w.talent_control(".gwtalent state",outgoing) && !outgoing && w.talent_presentation_pending());
auto state=messages(w.local_presentation(0x46010046));check(state.front()=="GWTURTLE1 BEGIN 2 4509 1 60 48 2" && state.back()=="GWTURTLE1 END 2");
check(w.local_presentation(0x46010046)==Bytes(4) && w.talent_sequence==2);
const auto known=w.spells;check(w.talent_control(".gwtalent learn 57 1",outgoing));
check(outgoing && outgoing->opcode==0x251 && outgoing->body==Bytes({57,0,0,0,0,0,0,0}));
check(w.spells==known && w.talent_points==48 && !w.talent_presentation_pending());
// Independent source facts are reported in either arrival order. The UI
// must require both rank and expected points before advancing its queue.
Bytes learned;le(learned,12295,4);auto rank_first=receive(w,{0x12b,learned});
check(rank_first.front()=="GWTURTLE1 BEGIN 3 4509 1 60 48 3" && w.spells.contains(12295));
auto acknowledged=receive(w,values(4509,{{1102,47}}));
check(acknowledged==std::vector<std::string>({"GWTURTLE1 BEGIN 4 4509 1 60 47 3","GWTURTLE1 SPELLS 4 78,12295,12664","GWTURTLE1 END 4"}));
Bytes ranked;le(ranked,12295,2);le(ranked,12676,2);check(receive(w,{0x12c,ranked}).front()=="GWTURTLE1 BEGIN 5 4509 1 60 47 3");
acknowledged=receive(w,values(4509,{{1102,46}}));check(acknowledged[1]=="GWTURTLE1 SPELLS 6 78,12664,12676");
check(w.talent_control(".gwtalent learn 57 5",outgoing) && outgoing && outgoing->body==Bytes({57,0,0,0,4,0,0,0}));
check(w.talent_control(".gwtalent reset 17379390974119196772",outgoing) && !outgoing);
check(messages(w.local_presentation(0x46010046))==std::vector<std::string>({"GWTURTLE1 ERROR reset-offer-unavailable"}));
for(auto command:{".gwtalent learn 0 1",".gwtalent learn 57 0",".gwtalent learn 57 6",".gwtalent malformed",".gwtalentish"}){
check(w.talent_control(command,outgoing) && !outgoing);
check(messages(w.local_presentation(0x46010046))==std::vector<std::string>({"GWTURTLE1 ERROR invalid-request"}));
}
// Foreign updates cannot alter the selected character's authoritative UI.
check(receive(w,values(4510,{{34,1},{36,1u|(8u<<8)},{1102,99}})).empty());check(w.talent_points==46 && w.talent_class==1);
for(auto fields:{std::map<uint16_t,uint32_t>{{1102,1001}},{{34,61}},{{36,1u|(6u<<8)}},{{36,1u|(8u<<8)}}}){
auto invalid=w;check(receive(invalid,values(4509,fields)).empty() && !invalid.talent_ready());
check(invalid.talent_control(".gwtalent learn 57 1",outgoing) && !outgoing);
check(messages(invalid.local_presentation(0x46010046))==std::vector<std::string>({"GWTURTLE1 ERROR state-not-ready"}));
}
// A full source create's missing zero-valued points word is real zero.
auto reset=world();check(receive(reset,initial({})).empty());
auto empty=receive(reset,values(4509,{{34,60},{36,1u|(1u<<8)}},true));
check(empty==std::vector<std::string>({"GWTURTLE1 BEGIN 1 4509 1 60 0 0","GWTURTLE1 END 1"}));
check(reset.talent_ready() && reset.talent_points==0);
check(reset.talent_control(".gwtalent learn 57 1",outgoing) && !outgoing);
check(messages(reset.local_presentation(0x46010046))==std::vector<std::string>({"GWTURTLE1 ERROR no-available-points"}));
// Same-character recreation preserves known spells. Points must come from the new actor.
Bytes removed;le(removed,4509,8);check(receive(w,{0xaa,removed}).empty());
check(!w.talent_ready() && !w.talent_points && w.talent_spells_ready);
const auto sequence=w.talent_sequence;
auto recreated=receive(w,values(4509,{{34,60},{36,1u|(1u<<8)},{1102,51}},true));
check(w.talent_ready() && recreated.front()=="GWTURTLE1 BEGIN "+std::to_string(sequence+1)+" 4509 1 60 51 3");
auto rejoined=receive(w,initial({78}));check(rejoined.front()=="GWTURTLE1 BEGIN "+std::to_string(sequence+2)+" 4509 1 60 51 1");
auto another=world(4510);receive(another,initial({78}));check(receive(another,values(4509,{{34,60},{36,257},{1102,51}})).empty() && !another.talent_ready());
auto joined=receive(another,values(4510,{{34,60},{36,257},{1102,51}}));check(joined.front()=="GWTURTLE1 BEGIN 1 4510 1 60 51 1");
auto opposite=world();receive(opposite,initial({78,12664}));receive(opposite,values(4509,{{34,60},{36,257},{1102,48}}));
check(opposite.talent_control(".gwtalent learn 57 1",outgoing) && outgoing);
auto points_first=receive(opposite,values(4509,{{1102,47}}));check(points_first.front()=="GWTURTLE1 BEGIN 2 4509 1 60 47 2");
auto rank_last=receive(opposite,{0x12b,learned});check(rank_last.front()=="GWTURTLE1 BEGIN 3 4509 1 60 47 3");
Bytes trained;le(trained,772,4);auto learned_without_points=receive(opposite,{0x12b,trained});
check(learned_without_points.front()=="GWTURTLE1 BEGIN 4 4509 1 60 47 4");
Bytes unlearned;le(unlearned,772,2);auto removed_without_points=receive(opposite,{0x203,unlearned});
check(removed_without_points.front()=="GWTURTLE1 BEGIN 5 4509 1 60 47 3");
// Only a real twelve-byte source offer enables the existing provider's
// normal confirmation UI. CloseGossip legitimately precedes that offer.
auto paid=reset_world();const auto original_spells=paid.spells;
check(receive(paid,{0x17e,{}}).empty());
auto offer=reset_offer(trainer_guid,10000);
check(receive(paid,offer)==std::vector<std::string>{"GWTURTLE1 RESET "+std::to_string(trainer_guid)+" 10000"});
check(paid.talent_reset_offer_bound() && paid.talent_reset_offer->cost==10000);
const auto confirm=".gwtalent reset "+std::to_string(trainer_guid);
check(paid.talent_control(confirm,outgoing) && outgoing && outgoing->opcode==0x2aa);
Bytes expected_guid;le(expected_guid,trainer_guid,8);check(outgoing->body==expected_guid);
const auto prepared=*outgoing;
// Preparing or retrying the output-size query does not consume the offer,
// alter known spells/points, or generate a successful reset snapshot.
check(paid.talent_reset_offer_bound() && paid.talent_control(confirm,outgoing));
check(outgoing && outgoing->body==prepared.body && paid.spells==original_spells && paid.talent_points==50);
check(paid.local_presentation(0x46010046)==Bytes(4));
paid.talent_control_sent({0x251,expected_guid});check(paid.talent_reset_offer_bound());
paid.talent_control_sent({0x2aa,Bytes(7)});check(paid.talent_reset_offer_bound());
Bytes wrong_guid;le(wrong_guid,trainer_guid+1,8);
paid.talent_control_sent({0x2aa,wrong_guid});check(paid.talent_reset_offer_bound());
paid.talent_control_sent(prepared);check(!paid.talent_reset_offer && paid.spells==original_spells && paid.talent_points==50);
check(paid.talent_control(confirm,outgoing) && !outgoing);
check(messages(paid.local_presentation(0x46010046))==std::vector<std::string>{"GWTURTLE1 ERROR reset-offer-unavailable"});
// Insufficient funds can leave the server state unchanged. No local ACK,
// refund, spell removal or point restoration is invented on submission.
check(paid.spells==original_spells && paid.talent_points==50);
// Only the real later unlearn and points updates describe a completed reset.
Bytes removed_rank;le(removed_rank,12285,2);
check(receive(paid,{0x203,removed_rank}).front().find(" 50 1")!=std::string::npos);
auto refunded=receive(paid,values(4509,{{1102,51}}));
check(refunded.front().find(" 51 1")!=std::string::npos && paid.spells==std::set<uint32_t>{78} && paid.talent_points==51);
// Free offers and every unsigned server-authored cost are preserved; zero
// GUID/cost clears only offer context, never the character's talent state.
auto free=reset_world();check(receive(free,reset_offer(trainer_guid,0))==std::vector<std::string>{"GWTURTLE1 RESET "+std::to_string(trainer_guid)+" 0"});
check(free.talent_reset_offer_bound() && free.talent_control(confirm,outgoing) && outgoing);
check(receive(free,reset_offer(trainer_guid,UINT32_MAX))==std::vector<std::string>{"GWTURTLE1 RESET "+std::to_string(trainer_guid)+" 4294967295"});
check(receive(free,reset_offer(0,0))==std::vector<std::string>{"GWTURTLE1 RESET 0 0"} && !free.talent_reset_offer);
check(free.spells==original_spells && free.talent_points==50);
auto requoted=reset_world();receive(requoted,offer);
check(requoted.talent_reset_offer->cost==10000);
check(receive(requoted,reset_offer(trainer_guid,15000))==std::vector<std::string>{"GWTURTLE1 RESET "+std::to_string(trainer_guid)+" 15000"});
check(requoted.talent_reset_offer_bound() && requoted.talent_reset_offer->cost==15000);
check(requoted.talent_control(confirm,outgoing) && outgoing && outgoing->body==expected_guid);
check(requoted.spells==original_spells && requoted.talent_points==50); // A quote is never a reset ACK.
for(size_t size=0;size<offer.body.size();++size){auto bad=reset_world();rejects([&]{receive(bad,{0x2aa,Bytes(offer.body.begin(),offer.body.begin()+size)});});}
auto tail=offer.body;tail.push_back(0);rejects([&]{receive(free,{0x2aa,tail});});
rejects([&]{receive(free,reset_offer(0,1));});
for(unsigned mode=0;mode<7;++mode){
auto bad=reset_world();auto& actor=bad.entities.actors.at(trainer_guid);
if(mode==0)actor.sent=false;
if(mode==1)actor.kind=5;
if(mode==2)actor.generation=0;
if(mode==3)actor.modern={};
if(mode==4)bad.entities.actors.clear();
if(mode==5)bad.self_created=false;
if(mode==6)bad.talent_spells_ready=false;
check(receive(bad,offer)==std::vector<std::string>{"GWTURTLE1 RESET 0 0"} && !bad.talent_reset_offer);
}
auto stale=reset_world();receive(stale,offer);++stale.entities.actors.at(trainer_guid).generation;
check(stale.talent_control(confirm,outgoing) && !outgoing);
check(messages(stale.local_presentation(0x46010046))==std::vector<std::string>{"GWTURTLE1 ERROR reset-offer-unavailable"});
auto foreign=reset_world();check(receive(foreign,reset_offer(4509,10000))==std::vector<std::string>{"GWTURTLE1 RESET 0 0"});
check(receive(foreign,reset_offer(trainer_guid+1,10000))==std::vector<std::string>{"GWTURTLE1 RESET 0 0"});
auto gone=reset_world();gone.location=WorldLocation{};receive(gone,offer);
Bytes remove_trainer;le(remove_trainer,trainer_guid,8);
check(receive(gone,{0xaa,remove_trainer})==std::vector<std::string>{"GWTURTLE1 RESET 0 0"} && !gone.talent_reset_offer);
auto recreate=reset_world();recreate.location=WorldLocation{};receive(recreate,offer);
// A new real source create (even one not yet presentable) is a new actor
// generation. Its absent movement must not resurrect the previous offer.
auto created=values(trainer_guid,{{3,9010}},true);
// Locate the source type after the packed GUID, not a fixed GUID width.
Reader create_reader(created.body);create_reader.le(4);create_reader.le(1);create_reader.le(1);unpack_guid(create_reader,8);
auto type_offset=created.body.size()-create_reader.left();created.body[type_offset]=3;
check(receive(recreate,created)==std::vector<std::string>{"GWTURTLE1 RESET 0 0"} && !recreate.talent_reset_offer);
auto changed_context=reset_world();receive(changed_context,offer);
changed_context.gameplay_sent(0x3f002b,{});
check(!changed_context.talent_reset_offer && messages(changed_context.local_presentation(0x46010046))==std::vector<std::string>{"GWTURTLE1 RESET 0 0"});
auto leaving=reset_world();receive(leaving,offer);leaving.clear_talent_state();check(!leaving.talent_reset_offer);
uint32_t required=99;check(gw_native_transport_say(nullptr,nullptr,0,nullptr,0,&required)==-1 && required==0);
return EXIT_SUCCESS;
}