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

289 lines
19 KiB
C++

#include "world_translation.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include "gateway/chat.hpp"
#include <algorithm>
#include <iostream>
using namespace gateway;
namespace {
void check(bool ok,const char* what){if(!ok)throw Error(what);}
template<class F>void rejects(F f){try{f();}catch(const Error&){return;}throw Error("expected Inspect rejection");}
Bytes guid64(uint64_t guid){Bytes bytes;le(bytes,guid,8);return bytes;}
constexpr uint32_t SelectOpcode=0x3f00cd,InspectOpcode=0x3f00ce;
constexpr uint64_t Peer=0x12345678,Second=0x11111111;
void add_peer(NativeWorldTranslation& world,uint64_t guid,uint64_t generation){
auto& peer=world.entities.peer_players[guid];
peer.named=true;peer.identity.guid=guid;peer.identity.name="Fixture";
peer.identity.race=1;peer.identity.cls=1;peer.generation=generation;
auto& actor=world.entities.actors[guid];
actor.kind=4;actor.eligible=true;actor.sent=true;actor.generation=generation;
actor.presentation_realm=world.realm;actor.presentation_map=world.location->map;
actor.modern=forever_player_guid(guid,world.realm);
}
Bytes note(const NativeWorldTranslation& world,uint64_t guid){return pack_guid(forever_player_guid(guid,world.realm));}
void select(NativeWorldTranslation& world,uint64_t guid){
auto body=guid?note(world,guid):pack_guid(Guid128{});
auto source=world.gameplay(SelectOpcode,body);
check(source.opcode==0x13d && source.body==guid64(guid),"ordinary target selection");
world.gameplay_sent(SelectOpcode,body);
}
void request(NativeWorldTranslation& world,uint64_t guid){
auto body=note(world,guid);
auto out=world.gameplay(InspectOpcode,body);
check(out.opcode==0x114 && out.body==guid64(guid),"authorized source request bytes");
world.gameplay_sent(InspectOpcode,body);
}
bool text_at_end(const Bytes& packet,const std::string& tail){
return packet.size()>=tail.size() && std::equal(tail.rbegin(),tail.rend(),packet.rbegin(),
[](char a,uint8_t b){return uint8_t(a)==b;});
}
LegacyPacket template_reply(uint32_t entry,std::string_view name,uint32_t source_spell=0){
Bytes data;le(data,name.empty()?entry|0x80000000:entry,4);
if(!name.empty()){
le(data,4,4);le(data,0,4);cstring(data,std::string(name));
for(unsigned i=0;i<3;++i)cstring(data,"");
for(unsigned i=0;i<20+20+15+7+3+30+1;++i)le(data,i==20+20+15+7+3?source_spell:0,4);
cstring(data,"");for(unsigned i=0;i<14;++i)le(data,0,4);
}
return {0x58,std::move(data)};
}
std::vector<std::string> name_replies(NativeWorldTranslation& world,const LegacyPacket& packet){
auto serialized=world.translate(packet,70,1,nullptr);
Reader read(serialized);
std::vector<std::string> result;
for(unsigned count=read.le(4);count;--count){
const auto opcode=read.le(4);
auto body=read.take(read.le(4));
if(opcode!=0x4c0001)continue;
std::string wire(body.begin(),body.end());
auto start=wire.find("GWInspectGearM ");
if(start!=wire.npos){
auto payload=wire.substr(start+13);
check(payload.size()<=255,"name fragment exceeds addon event capacity");
result.push_back(std::move(payload));
}
}
read.end();return result;
}
void inspect_template_scenario(){
NativeWorldTranslation world{};
world.realm=70;world.selected.guid=52;world.selected.name="Self";
world.self_created=true;world.location=WorldLocation{1,{}};
add_peer(world,Peer,7);select(world,Peer);
constexpr uint32_t ring=18000011,other=18000012,replaced=18000013,
lost=18000014,late=18000015,stale=18000016;
auto& fields=world.entities.peer_players.at(Peer).fields;
const auto first=turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0;
fields[first+10*12]=ring;fields[first+11*12]=ring;
fields[first+12*12]=other;fields[first+13*12]=replaced;
fields[first+17*12]=lost;fields[first+9*12]=late;fields[first+6*12]=stale;
fields[turtle_entity_fields::PLAYER_FIELD_INV_SLOT_HEAD]=19000000;
check(name_replies(world,template_reply(ring,"Unsolicited")).empty() &&
!world.inventory.templates.contains(ring),"unsolicited template is ignored");
world.inventory.request_peer_template(ring);
check(name_replies(world,template_reply(ring,"Ring of Two",46499)).empty(),"preapproval only warms positive cache");
check(world.inventory.peer_template(ring)->spells[0].spell==46499 && world.inventory.templates.empty() &&
!world.spells.contains(46499),"observed item spell stays outside owned and learned state");
auto reset=world.translate({0x1e1,guid64(world.selected.guid)},70,1,nullptr);
Reader reset_reader(reset);check(reset_reader.le(4)==0,"peer item cannot seed a SELF cooldown reset");reset_reader.end();
request(world,Peer);
auto names=name_replies(world,{0x115,guid64(Peer)});
check(names==std::vector<std::string>{"M 70 305419896 11 18000011 0 11 Ring of Two",
"M 70 305419896 12 18000011 0 11 Ring of Two"},
"both currently occupied slots receive their cached exact visible name on ACK");
check(world.inventory.queries==std::set<uint32_t>{other,replaced,lost,late,stale},
"missing visible entries requested once; private equipment field skipped");
check(name_replies(world,template_reply(other,"A Shiny Trinket"))==
std::vector<std::string>{"M 70 305419896 13 18000012 0 15 A Shiny Trinket"},
"one delayed matching visible slot receives its original name");
check(name_replies(world,template_reply(other,"Changed unsolicited")).empty(),"duplicate cannot rename it");
fields[first+13*12]=999;
check(name_replies(world,template_reply(replaced,"Retired")).empty(),"changed slot cannot consume old template");
check(name_replies(world,template_reply(lost,"")).empty(),"server negative retains numeric placeholder");
world.inspect_permission_until=std::chrono::steady_clock::now();
check(name_replies(world,template_reply(late,"Expired")).empty(),"lapsed grant suppresses name");
request(world,Peer);(void)name_replies(world,{0x115,guid64(Peer)});
add_peer(world,Peer,8);
check(name_replies(world,template_reply(stale,"New generation")).empty(),"recreated peer cannot inherit name routing");
auto check_name=[&](std::string_view value){
return forever_inspect_item_name(world.selected,{8191,70,0},UINT32_MAX,19,0x7fffffff,value);
};
std::string first_part(191,'x'),second_part="é"+std::string(62,'z');
auto chunks=check_name(first_part+second_part);
check(chunks.size()==2 && text_at_end(chunks[0],"M 8191 4294967295 19 2147483647 0 255 "+first_part)
&& text_at_end(chunks[1],"M 8191 4294967295 19 2147483647 191 255 "+second_part),
"maximum localized source name fragments preserve UTF-8 boundaries");
select(world,Peer);
const std::string long_name=first_part+second_part;
for(unsigned slot=0;slot<19;++slot){
const uint32_t id=19001000+slot;
fields[first+slot*12]=id;
world.inventory.request_peer_template(id);
check(world.inventory.cache_pending_template_reply(template_reply(id,long_name).body),
"every max-size cached name originated in a pending server reply");
}
request(world,Peer);
const auto full=name_replies(world,{0x115,guid64(Peer)});
check(full.size()==38,"19 full original names fit in one ordinary approved batch");
check(check_name(std::string(256,'x')).empty() && check_name("bad\nname").empty()
&& check_name(std::string(1,char(0xff))).empty(),"source name size, control and UTF-8 guards");
}
}
int main(){
inspect_template_scenario();
NativeWorldTranslation world{};
world.realm=70;world.selected.guid=52;world.selected.name="Self";
world.self_created=true;world.location=WorldLocation{1,{}};
add_peer(world,Peer,7);add_peer(world,Second,8);
select(world,Second);request(world,Second);
const auto unknown=world.inspect_ack(guid64(Second),70);
check(unknown && text_at_end(unknown->at(0),
"GWInspectGear70 286331153 - - - - - - - - - - - - - - - - - - -"),
"approved peer with no visible-item fields returns nineteen unknowns");
check(world.authorized_inspect_peer()==std::pair<uint64_t,uint64_t>{Second,8},
"missing equipment fields preserve the original peer approval");
select(world,0);
auto body=note(world,Peer);
rejects([&]{world.inspect_control("70 305419896");});
rejects([&]{world.gameplay(InspectOpcode,body);});
select(world,Peer);
check(world.inspect_control("70 305419896")==body,"explicit addon action binds same current peer");
rejects([&]{world.inspect_control("70 123");});
rejects([&]{world.gameplay(InspectOpcode,note(world,Second));});
rejects([&]{world.gameplay(InspectOpcode,note(world,world.selected.guid));});
auto tail=body;tail.push_back(0);rejects([&]{world.gameplay(InspectOpcode,tail);});
auto short_body=body;short_body.pop_back();rejects([&]{world.gameplay(InspectOpcode,short_body);});
auto noncanonical=body;noncanonical[0]|=0x80;noncanonical.insert(noncanonical.begin()+6,0);
rejects([&]{world.gameplay(InspectOpcode,noncanonical);});
world.entities.peer_players.at(Peer).blocked=true;rejects([&]{world.gameplay(InspectOpcode,body);});
world.entities.peer_players.at(Peer).blocked=false;
world.entities.peer_players.at(Peer).named=false;rejects([&]{world.gameplay(InspectOpcode,body);});
world.entities.peer_players.at(Peer).named=true;
(void)world.gameplay(InspectOpcode,body);
check(!world.inspect_ack(guid64(Peer),70),"sizing alone has no authorization");
world.begin_inspect_talents("request WrongName");
check(!world.inspect_talents,"wrong requested identity has no intent");
world.begin_inspect_talents("request Fixture");
check(world.inspect_talents && !world.next_inspect_talents(),"pre-ACK talent click waits for Source");
request(world,Peer);
check(!world.authorized_inspect_peer(),"no pre-ACK peer");
check(!world.inspect_control("70 305419896"),"delayed UI action coalesces with stock request");
check(!world.inspect_ack(guid64(Second),70),"wrong reply peer");
auto extra=guid64(Peer);extra.push_back(1);
check(!world.inspect_ack(extra,70),"unverified extension");
auto& fields=world.entities.peer_players.at(Peer).fields;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0]=413;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+12]=0;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+24]=19019;
// Signed peer renderer maps this visible entry to IMA 321079. Inspect uses the item's own ID.
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+15*12]=80222;
fields[turtle_entity_fields::PLAYER_FIELD_INV_SLOT_HEAD]=999;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+1]=1900;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+12*15+2]=33;
fields[turtle_entity_fields::PLAYER_VISIBLE_ITEM_1_0+12*15+1]=0;
auto packet=world.inspect_ack(guid64(Peer),70);
check(packet && text_at_end(packet->at(0),"GWInspectGear70 305419896 413 0 19019 - - - - - - - - - - - - 80222 - - -"),"real visible entries, explicit zero and unknown slots");
check(text_at_end(packet->at(1),"GWInspectGearP 70 305419896 1900 - - - - - - - - - - - - - - 0 - - -"),"observed permanent enchant, reported zero and missing fields");
check(text_at_end(packet->at(2),"GWInspectGearT 70 305419896 - - - - - - - - - - - - - - - 33 - - -"),"observed temporary enchant on main hand");
const auto approved=world.authorized_inspect_peer();
check(approved && approved->first==Peer && approved->second==7,"authorized live actor");
auto talent=world.next_inspect_talents();
auto retry=world.next_inspect_talents();
check(talent && talent->opcode==0x95 && retry && retry->body==talent->body &&
!world.inspect_talents->sent,"sizing retries preserve authorized talent request");
check(text_at_end(talent->body,std::string("TW_CHAT_MSG_WHISPER<Fixture>\tINSTalentShow")+'\0' ),
"actual talent message addresses current server-approved named peer");
auto transmog=world.next_inspect_transmogs();
check(transmog && transmog->opcode==0x95 &&
text_at_end(transmog->body,std::string("TW_CHAT_MSG_WHISPER<Fixture>\tINSShowTransmogs")+'\0'),
"approved inspect asks the peer's original client for its transmog slots");
world.inspect_transmogs->sent=true;
check(!world.next_inspect_transmogs(),"one transmog query per approval");
auto transmog_message=[](std::string text){ServerAddonMessage m{Peer,0,"TW_CHAT_MSG_WHISPER",std::move(text)};return m;};
check(inspect_transmog_reply(transmog_message("INSTransmogs;start;0;0"))->start,"start marker");
auto parsed=inspect_transmog_reply(transmog_message("INSTransmogs;5;16922;19682"));
check(parsed && !parsed->start && parsed->slot==5 && parsed->appearance==16922 && parsed->real==19682,"slot appearance real");
for(auto bad:{"INSTransmogs;20;1;2","INSTransmogs;5;1","INSTransmogs;5;x;2","INSTransmogs;5;1;2;3","INSTransmogs;0;1;2","INSTransmogs;5;12345678901;2"})
check(!inspect_transmog_reply(transmog_message(bad)),"malformed transmog reply rejected");
{
Character self{};self.guid=0x77;self.name="Self";
auto out=forever_inspect_transmog_reply(self,{70,1,0},uint32_t(Peer),*parsed);
check(text_at_end(out,"GWInspectGearX 70 305419896 5 16922 19682"),"client transmog line");
out=forever_inspect_transmog_reply(self,{70,1,0},uint32_t(Peer),InspectTransmog{true});
check(text_at_end(out,"GWInspectGearX 70 305419896 0 0 0"),"client reset line");
}
world.inspect_talents->sent=true;world.begin_inspect_talents("request Fixture");
check(!world.next_inspect_talents(),"same-peer duplicate does not send another whisper");
auto talent_reply=[&](uint64_t sender){
std::string message="TW_CHAT_MSG_WHISPER\tINSTalentEND;";
Bytes body{3};le(body,0xffffffffu,4);le(body,sender,8);le(body,message.size()+1,4);cstring(body,message);le(body,0,1);
return world.translate({0x96,body},70,1,nullptr);
};
auto wrong=talent_reply(Second);
check(std::search(wrong.begin(),wrong.end(),InspectTalentPrefix.begin(),InspectTalentPrefix.end())==wrong.end(),
"another sender cannot populate read-only talent view");
auto correct=talent_reply(Peer);
check(std::search(correct.begin(),correct.end(),InspectTalentPrefix.begin(),InspectTalentPrefix.end())!=correct.end(),
"bound peer talent reply presented");
{
// A peer's Octo client inspecting us: the server relays INSShowTransmogs; we answer from the addon.
auto ask=[&](uint64_t sender,std::string text){
std::string message="TW_CHAT_MSG_WHISPER"+text;
Bytes body{3};le(body,0xffffffffu,4);le(body,sender,8);le(body,message.size()+1,4);cstring(body,message);le(body,0,1);
return world.translate({0x96,body},70,1,nullptr);
};
check(!world.inspect_transmog_answer("Fixture;INSTransmogs;start;0;0"),"no answer before a peer asks");
auto out=ask(Peer,"\t\tINSShowTransmogs");
const std::string prefix(InspectTransmogAskPrefix);
check(std::search(out.begin(),out.end(),prefix.begin(),prefix.end())!=out.end(),"peer query handed to the client addon");
auto start=world.inspect_transmog_answer("Fixture;INSTransmogs;start;0;0");
check(start && text_at_end(*start,std::string("TW_CHAT_MSG_WHISPER<Fixture>\tINSTransmogs;start;0;0")+'\0'),"start row whispered to the asker");
auto row=world.inspect_transmog_answer("Fixture;INSTransmogs;3;33210;19695");
check(row && text_at_end(*row,std::string("TW_CHAT_MSG_WHISPER<Fixture>\tINSTransmogs;3;33210;19695")+'\0'),"slot row: look item then equipped item");
check(!world.inspect_transmog_answer("Other;INSTransmogs;start;0;0"),"never whisper a peer that did not ask");
for(auto bad:{"Fixture;INSTransmogs;20;1;2","Fixture;INSTransmogs;3;0;2","Fix>ture;INSTransmogs;start;0;0","Fixture","Fixture;INSTalentEND;"})
rejects([&]{(void)world.inspect_transmog_answer(bad);});
for(int i=0;i<18;++i)check(!!world.inspect_transmog_answer("Fixture;INSTransmogs;3;33210;19695"),"slot rows within bound");
check(!world.inspect_transmog_answer("Fixture;INSTransmogs;3;33210;19695"),"at most start plus 19 rows per ask");
}
select(world,Second);select(world,Peer);
check(!world.inspect_talents && !world.authorized_inspect_peer() && !world.next_inspect_talents(),
"A/B/A selection retires approval and talent intent");
request(world,Peer);(void)world.inspect_ack(guid64(Peer),70);
check(!world.inspect_ack(guid64(Peer),70),"duplicate ACK drops");
world.realm=71;check(!world.authorized_inspect_peer(),"realm changed");world.realm=70;
world.location->map=2;check(!world.authorized_inspect_peer(),"map changed");world.location->map=1;
world.inspect_permission_until=std::chrono::steady_clock::now();check(!world.authorized_inspect_peer(),"expired permission");
request(world,Peer);select(world,0);
check(!world.inspect_ack(guid64(Peer),70),"deselected reply held");
select(world,Peer);request(world,Peer);add_peer(world,Peer,9);
check(!world.authorized_inspect_peer(),"recreation revokes previous authorization");
rejects([&]{world.gameplay(InspectOpcode,body);});select(world,Peer);request(world,Peer);
check(!world.inspect_ack(guid64(Peer),70),"late ACK cannot attach to recreated peer");
check(world.inspect_ack(guid64(Peer),70).has_value(),"new generation's own ACK");
request(world,Peer);world.inspect_requests.pending->sent-=std::chrono::seconds(6);
check(!world.inspect_ack(guid64(Peer),70),"expired request");
request(world,Peer);
auto bundle=world.translate({0x115,guid64(Peer)},70,1,nullptr);
Reader output(bundle);auto count=output.le(4);bool delivered=false;
for(size_t i=0;i<count;++i){
const auto opcode=output.le(4);auto payload=output.take(output.le(4));
if(opcode==0x4c0001)delivered|=text_at_end(payload,"GWInspectGear70 305419896 413 0 19019 - - - - - - - - - - - - 80222 - - -");
}
output.end();check(delivered,"ordinary inbound world dispatcher emits the approved gear result");
request(world,Peer);world.clear_inspect_selection();
check(!world.inspect_ack(guid64(Peer),70),"retired world selection");
// Tortoise can legally deny every inspect without a reply. A late reply
// must be scoped to its original request, while unrelated peers stay usable.
for(uint64_t guid=7000;guid<7096;++guid){
add_peer(world,guid,1);select(world,guid);request(world,guid);
}
add_peer(world,8000,1);select(world,8000);request(world,8000);
check(world.inspect_ack(guid64(8000),70).has_value(),"valid inspect after 96 server denials");
select(world,7000);request(world,7000);
check(!world.inspect_ack(guid64(7000),70),"prior unanswered selection cannot authorize new Inspect");
check(world.inspect_ack(guid64(7000),70).has_value(),"fresh approval after prior reply consumed");
std::cout<<"PASS selected named peer, exact request, source approval, item appearances, stale/recreated/expired controls\n";
}