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

183 lines
13 KiB
C++

// Peer addon relay + BigWigs pull timer -> native START_TIMER. Wire bytes follow Tortoise
// ChatHandler::BuildChatPacket (LANG_ADDON) and Actual70334 CMSG_CHAT_ADDON_MESSAGE.
#include "world_translation.hpp"
#include "aura_translation.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include <algorithm>
#include <cstdlib>
#include "gateway/chat.hpp"
#include <iostream>
#include <vector>
using namespace gateway;
static void check(bool value,const char* text){if(!value)throw Error(text);}
static NativeWorldTranslation world(uint64_t owner){NativeWorldTranslation w;w.selected.guid=owner;w.selected.name="Owner";w.realm=70;w.location=WorldLocation{};w.self_created=true;return w;}
static Bytes addon(uint8_t type,uint64_t sender,const std::string& text){
Bytes body{type};le(body,0xffffffffu,4);le(body,sender,8);if(type==1)le(body,sender,8);
le(body,text.size()+1,4);cstring(body,text);le(body,0,1);return body;
}
static Bytes name_reply(uint64_t sender){
Bytes body;le(body,sender,8);cstring(body,"Raider");cstring(body,"");le(body,1,4);le(body,0,4);le(body,1,4);return body;
}
struct Out{uint32_t opcode;Bytes body;};
static std::vector<Out> packets(const Bytes& wire){
Reader r(wire);auto n=r.le(4);std::vector<Out> out;
for(uint32_t i=0;i<n;++i){auto op=uint32_t(r.le(4));auto len=r.le(4);out.push_back({op,r.take(len)});}
r.end();return out;
}
static Bytes legacy_guid(uint64_t guid){
Bytes out{0};for(unsigned i=0;i<8;++i)if(uint8_t b=uint8_t(guid>>(8*i))){out[0]|=uint8_t(1u<<i);out.push_back(b);}
return out;
}
static bool carries(const std::vector<Out>& out,uint32_t opcode,const std::string& text){
for(const auto& p:out)if(p.opcode==opcode &&
std::search(p.body.begin(),p.body.end(),text.begin(),text.end())!=p.body.end())return true;
return false;
}
int main(){try{
// OctoReplay grave service replies come from self even for a guildless viewer.
{
auto w = world(7001);
const std::string facts = "G|17365880163140632691|9007199254740993|Test|18|1|2|3|0|ok|";
auto incoming = [&](uint64_t sender, const std::string& line) {
return packets(w.translate({0x96, addon(3, sender, "OWLDTH\t" + line)}, 0x46010046, 0, nullptr));
};
auto result = incoming(7001, facts);
check(result.size() == 1 && carries(result, 0x4c0001, "OWLDTH" + facts), "guildless grave self reply");
w.guild_id = 8;
check(incoming(7002, facts).empty() && w.chat_name_queries.empty(), "peer grave notice refused");
auto longWords = incoming(7001, "W|" + std::string(320, 'w'));
check(longWords.size() == 2 && carries(longWords, 0x4c0001, std::string("OWLDTH\1W|"))
&& carries(longWords, 0x4c0001, std::string("OWLDTH\2")), "long last words split on service route");
check(!turtle_peer_addon_message(addon(3, 7001, "OWLDTH\tGW|start")), "grave prefix reserved");
check(!turtle_peer_addon_message(addon(3, 7002, "GWGRAVE\tCAPS")), "private grave request cannot arrive from a peer");
}
// Tortoise rejects LANG_ADDON WHISPER before forwarding it. OCTO_TS uses
// the existing shared-channel relay, with its addressee in the payload.
check(!turtle_peer_addon_message(addon(6,7002,"OCTO_TS\tOwner REQ 197")),"addon whisper is not a peer transport");
for(auto type:{uint8_t(1),uint8_t(2),uint8_t(3)}){
auto peer=turtle_peer_addon_message(addon(type,7002,"OCTO_TS\tOwner REQ 197"));
check(peer && peer->native_type==type+1 && peer->prefix=="OCTO_TS" && peer->text=="Owner REQ 197","addressed trade shared-channel relay");
}
// 1. Tortoise raid addon message parsing; reviewed service prefixes are not peer traffic.
auto raid=turtle_peer_addon_message(addon(2,7002,"BigWigs\tPulltimerSync 10"));
check(raid && raid->native_type==3 && raid->sender==7002 && raid->prefix=="BigWigs" && raid->text=="PulltimerSync 10","raid parse");
check(bigwigs_pull_seconds(*raid)==10u,"pull seconds");
auto party=turtle_peer_addon_message(addon(1,7002,"DBM\tx"));
check(party && party->native_type==2,"party layout repeats sender");
check(!turtle_peer_addon_message(addon(2,7002,"TW_SHOP\tfoo")),"service prefix stays on its route");
check(!turtle_peer_addon_message(addon(2,7002,"no tab")),"no prefix separator");
auto stop=turtle_peer_addon_message(addon(2,7002,"BigWigs\tPulltimerStopSync"));
check(stop && bigwigs_pull_seconds(*stop)==0u,"stop");
auto bcast=turtle_peer_addon_message(addon(0x57,7002,"BigWigs\tPulltimerBroadcastSync 7.5"));
check(bcast && bcast->native_type==0x27 && bigwigs_pull_seconds(*bcast)==7u,"broadcast remainder");
for(auto bad:{"PulltimerSync abc","PulltimerSync ","PulltimerSync 99999","PulltimerSync 0","PulltimerSync 5x"}){
auto m=turtle_peer_addon_message(addon(2,7002,std::string("BigWigs\t")+bad));
check(m && !bigwigs_pull_seconds(*m),"malformed pull rejected");
}
auto guild=turtle_peer_addon_message(addon(3,7002,"BigWigs\tPulltimerSync 10"));
check(guild && !bigwigs_pull_seconds(*guild),"guild channel is not a raid pull");
// 1b. Tortoise TWThreat reply: prefix "TWT " (space), up to 3096 bytes -> prefix TWT, native pieces <=255.
{
std::string rows="TWTv4=";for(int i=0;i<20;++i)rows+="Raider"+std::to_string(i)+":0:12345:87.5:1;";rows.pop_back();
auto twt=turtle_peer_addon_message(addon(2,7001,"TWT \t"+rows));
check(twt && twt->prefix=="TWT" && twt->text==rows && twt->native_type==3,"threat reply parsed as TWT");
check(!turtle_peer_addon_message(addon(2,7001,"TWT \tother")),"only the threat api text takes the TWT route");
Character me;me.guid=7001;me.name="Owner";RosterRealm realm;realm.realm_id=70;realm.virtual_address=70;
auto parts=forever_peer_addon_messages(*twt,me,realm);
check(parts.size()==(rows.size()+253)/254,"threat reply split");
std::string joined;
for(size_t i=0;i<parts.size();++i){
// payload = trailing bytes after name+prefix; read lengths from the bit-packed header end.
const auto& b=parts[i];bool last=i+1==parts.size();
size_t n=1+(last?rows.size()-254*i:254);check(b.size()>n,"piece size");
std::string tail(b.end()-n,b.end());
check(tail[0]==(last?'\2':'\1'),"piece marker");joined+=tail.substr(1);
}
check(joined==rows,"pieces join to the full reply");
auto w=world(7001);
auto out=packets(w.translate({0x96,addon(2,7001,"TWT \t"+rows)},0x46010046,0,nullptr));
size_t chats=0;for(auto& o:out)if(o.opcode==0x4c0001)++chats;
check(chats==parts.size(),"world emits every threat piece");
}
// 2. START_TIMER layout: int64 total, int32 type 2, int64 left, no player GUID.
auto timer=forever_start_timer(10,10,nullptr);
{Reader r(timer);check(r.le(8)==10 && r.le(4)==2 && r.le(8)==10 && r.le(1)==0,"start timer");r.end();}
// 3. Native CMSG_CHAT_ADDON_MESSAGE -> Tortoise CMSG_MESSAGECHAT (raid = 2, LANG_ADDON).
{
std::string p="BigWigs",t="PulltimerSync 10";
Bytes in;auto bits=(uint32_t(p.size())<<11)|(uint32_t(t.size())<<3);le(in,uint8_t(bits>>8),1);le(in,uint8_t(bits),1);
le(in,3,4);in.insert(in.end(),p.begin(),p.end());in.insert(in.end(),t.begin(),t.end());
auto req=forever_peer_addon_request(in);check(req.native_type==3 && req.prefix==p && req.text==t,"native request");
auto out=turtle_peer_addon_request(req);
Bytes want;le(want,2,4);le(want,0xffffffffu,4);cstring(want,p+"\t"+t);check(out==want,"tortoise request bytes");
for (std::string sp : {"TW_SHOP", "GWGRAVE"}) {
Bytes s2;auto sb=(uint32_t(sp.size())<<11)|(uint32_t(t.size())<<3);le(s2,uint8_t(sb>>8),1);le(s2,uint8_t(sb),1);
le(s2,3,4);s2.insert(s2.end(),sp.begin(),sp.end());s2.insert(s2.end(),t.begin(),t.end());
bool threw=false;try{(void)forever_peer_addon_request(s2);}catch(const Error&){threw=true;}check(threw,"service prefix not sent as peer");
}
}
// 4. World: unknown raider queues behind a name query, then presents addon + native timer.
{
auto w=world(7001);
auto first=packets(w.translate({0x96,addon(2,7002,"BigWigs\tPulltimerSync 10")},0x46010046,0,nullptr));
check(first.empty() && w.pending_chats.size()==1 && w.chat_name_queries.count(7002),"queued behind name query");
auto out=packets(w.translate({0x51,name_reply(7002)},0x46010046,0,nullptr));
bool chat=false,start=false;
for(auto& o:out){
if(o.opcode==0x4c0001){Reader m(o.body);chat=m.le(1)==3 && m.le(4)==183;}
if(o.opcode==0x47003e)start=o.body==timer;
}
check(chat && start,"addon presented and native timer started");
// Known sender: immediate.
out=packets(w.translate({0x96,addon(2,7002,"BigWigs\tPulltimerStopSync")},0x46010046,0,nullptr));
check(out.size()==2 && out[1].opcode==0x47003e && out[1].body==forever_start_timer(0,0,nullptr),"stop cancels");
// Our own raid echo: addon delivered, timer not doubled (shown locally at send).
out=packets(w.translate({0x96,addon(2,7001,"BigWigs\tPulltimerSync 10")},0x46010046,0,nullptr));
check(out.size()==1 && out[0].opcode==0x4c0001,"self echo shows no second timer");
// Guild addon traffic needs guild membership.
out=packets(w.translate({0x96,addon(3,7002,"BigWigs\tx")},0x46010046,0,nullptr));
check(out.empty(),"guild addon without guild");
}
{ // Built-in boss mods feed: subscription, server spell start -> local "OctoBM" fact, aura transitions.
auto w=world(1001);
const uint64_t boss=0xF130002ECF000005ull;
w.entities.actors[boss].kind=3;w.entities.actors[boss].entry=11983;
Bytes start=legacy_guid(boss);append(start,legacy_guid(boss));le(start,23339,4);le(start,2,2);le(start,1000,4);
le(start,2,2);append(start,legacy_guid(1001));
auto out=packets(w.translate({0x131,start},0x46010046,0,nullptr));
check(!carries(out,0x4c0001,"OctoBM"),"feed silent before subscription");
w.boss_feed_control("R");w.boss_feed_control("S 22539 23339");
check(w.boss_feed_on && w.boss_feed_spells.size()==2,"subscription parsed");
bool threw=false;try{w.boss_feed_control("S 12x");}catch(const Error&){threw=true;}
check(threw,"malformed subscription rejected");
out=packets(w.translate({0x131,start},0x46010046,0,nullptr));
if(std::getenv("GW_DEBUG_FEED"))for(auto& p:out){std::cerr<<std::hex<<p.opcode<<" "<<std::string(p.body.begin(),p.body.end()).substr(0,80)<<"\n";}
check(carries(out,0x4c0001,"SPELL_CAST_START;23339;C11983:5;POwner;0;"),"spell start fact");
Bytes other=legacy_guid(boss);append(other,legacy_guid(boss));le(other,133,4);le(other,2,2);le(other,1000,4);
le(other,2,2);append(other,legacy_guid(1001));
out=packets(w.translate({0x131,other},0x46010046,0,nullptr));
check(!carries(out,0x4c0001,"OctoBM"),"unsubscribed spell not fed");
w.boss_feed_control("O");
out=packets(w.translate({0x131,start},0x46010046,0,nullptr));
check(!carries(out,0x4c0001,"OctoBM"),"feed off");
// Aura slot transitions: creation is silent, then dose up, swap, removal.
NativeAuraTranslation a;namespace F=gateway::turtle_entity_fields;
LegacyObjectUpdate create;create.kind=2;create.type=4;create.guid=1001;create.fields[F::UNIT_FIELD_AURA]=23341;
a.consume({create});check(a.take_transitions().empty(),"creation silent");
LegacyObjectUpdate dose;dose.kind=0;dose.guid=1001;dose.fields[F::UNIT_FIELD_AURAAPPLICATIONS]=7; // 8 stacks
a.consume({dose});auto t=a.take_transitions();
check(t.size()==1 && t[0].kind==2 && t[0].spell==23341 && t[0].stacks==8,"dose up with stack count");
LegacyObjectUpdate swap;swap.kind=0;swap.guid=1001;swap.fields[F::UNIT_FIELD_AURA]=23340;swap.fields[F::UNIT_FIELD_AURAAPPLICATIONS]=0;
a.consume({swap});t=a.take_transitions();
check(t.size()==2 && t[0].kind==1 && t[0].spell==23341 && t[1].kind==0 && t[1].spell==23340 && t[1].stacks==1,"swap = removed + applied");
LegacyObjectUpdate gone;gone.kind=0;gone.guid=1001;gone.fields[F::UNIT_FIELD_AURA]=0;
a.consume({gone});t=a.take_transitions();
check(t.size()==1 && t[0].kind==1 && t[0].spell==23340,"removal");
// Formatter refuses a name that could split the feed line.
Character self;self.guid=1001;self.name="Owner";RosterRealm realm;realm.realm_id=70;realm.virtual_address=0x46010046;
threw=false;try{forever_boss_feed(self,realm,"SPELL_DAMAGE",1,{'P',0,0,"a;b"},{},0,"");}catch(const Error&){threw=true;}
check(threw,"feed separator in name rejected");
}
std::cout<<"peer addon relay OK\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<"\n";return 1;}}