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

217 lines
20 KiB
C++

// Synthetic: native auction sell/cancel/bid/owned/bidded requests -> Tortoise AuctionHouseHandler bodies,
// including the optional TaintedBy AddOnInfo, whole-item sells only, Source price/duration bounds.
#include "world_translation.hpp"
#include "gateway/turtle_entity_profile.hpp"
#include <iostream>
using namespace gateway;
namespace F=gateway::turtle_entity_fields;
static unsigned checks=0;
static void expect(bool ok,const char* what){++checks;if(!ok)throw Error(what);}
template<class F2>static void rejects(F2 f,const char* what){bool r=false;try{f();}catch(const Error&){r=true;}expect(r,what);}
static Guid128 g(uint64_t lo,uint64_t hi){Guid128 x{};Bytes b;le(b,lo,8);le(b,hi,8);std::copy(b.begin(),b.end(),x.begin());return x;}
static Bytes addon(const std::string& name,const std::string& version){ // AddOnInfo: bits10 name+1, bits10 version+1, 2 flags, data+nulls
Bytes b;uint32_t n=name.empty()?0:uint32_t(name.size()+1),v=version.empty()?0:uint32_t(version.size()+1);uint32_t bits=(n<<14)|(v<<4)|(1<<3);
b.push_back(uint8_t(bits>>16));b.push_back(uint8_t(bits>>8));b.push_back(uint8_t(bits));
if(n){b.insert(b.end(),name.begin(),name.end());b.push_back(0);}if(v){b.insert(b.end(),version.begin(),version.end());b.push_back(0);}return b;}
int main(){try{
NativeWorldTranslation w{};w.realm=70;w.selected.guid=4509;w.selected.race=1;w.selected.cls=1;w.selected.name="Seller";
w.self_created=true;w.location=WorldLocation{0,{0,0,0,0}};w.virtual_realm_address=0x10046;
const uint64_t npc=0xF130000000000505ull;const auto npc_m=g(0x505,0x2000000000000005ull);
NativeEntityTranslation::Actor a;a.kind=3;a.modern=npc_m;a.sent=true;a.generation=1;w.entities.actors[npc]=a;
// Source AuctionHello is exactly u64 NPC + u32 house. Rejected replies
// must not change the auctioneer used by subsequent aux requests.
{auto q=w;const auto other=npc+1;const auto other_m=g(0x506,0x2000000000000005ull);
auto other_actor=a;other_actor.modern=other_m;q.entities.actors[other]=other_actor;
auto hello=[](uint64_t id,uint32_t house){Bytes b;le(b,id,8);le(b,house,4);return b;};
auto valid=[&](uint64_t id,const Guid128& modern,uint32_t house){
Bytes payload=pack_guid(modern);le(payload,0,4);le(payload,0,4);le(payload,house,4);payload.push_back(0x80);
Bytes expected;le(expected,1,4);le(expected,0x470199,4);le(expected,payload.size(),4);append(expected,payload);
expect(q.translate({0x255,hello(id,house)},0x10046,0,nullptr)==expected,"auction hello exact native payload");
expect(q.aux_auctioneer==id && !q.gossip,"validated hello commits target and closes gossip");
};
valid(npc,npc_m,7);q.gossip=GossipMenu{};q.gossip->guid=npc;
auto bad=[&](const Bytes& packet){
rejects([&]{q.translate({0x255,packet},0x10046,0,nullptr);},"invalid auction hello rejected");
expect(q.aux_auctioneer==npc,"rejected auction hello changed aux auctioneer");
expect(q.gossip && q.gossip->guid==npc,"rejected auction hello changed gossip state");
};
const auto complete=hello(other,7);
for(size_t size=0;size<complete.size();++size){auto b=complete;b.resize(size);bad(b);}
{auto b=complete;b.push_back(0);bad(b);}
bad(hello(other,0));bad(hello(other,0x80000000u));
bad(hello(other+1,7));bad(hello(4509,7));
valid(other,other_m,7); // rejection must not prevent a genuine source-authorized switch
}
const uint64_t item=0x4000000000000888ull;
w.inventory.player_fields[F::PLAYER_FIELD_PACK_SLOT_1]=uint32_t(item);w.inventory.player_fields[F::PLAYER_FIELD_PACK_SLOT_1+1]=uint32_t(item>>32);
{LegacyObjectUpdate u;u.guid=item;u.type=1;u.fields[14]=1;w.inventory.items[item]=u;}
const auto item_m=w.inventory.owned_bindings().at(item);
auto sell=[&](uint64_t bid,uint64_t buyout,uint32_t minutes,uint32_t use,bool tainted,uint32_t count=1){
Bytes b=pack_guid(npc_m);le(b,bid,8);le(b,buyout,8);le(b,minutes,4);b.push_back(uint8_t((tainted?0x80:0)|(count<<1)));
if(tainted)append(b,addon("Auctionator","1.2"));for(uint32_t i=0;i<count;++i){append(b,pack_guid(item_m));le(b,use,4);}return b;};
{auto r=w.gameplay(0x3f0062,sell(500,1000,480,1,false));Reader q(r.body);
expect(r.opcode==0x256 && q.le(8)==npc && q.le(8)==item && q.le(4)==500 && q.le(4)==1000 && q.le(4)==480,"sell");q.end();}
expect(w.gameplay(0x3f0062,sell(500,0,1440,1,true)).opcode==0x256,"tainted sell, no buyout");
rejects([&]{w.gameplay(0x3f0062,sell(500,1000,720,1,false));},"12 h is not a Source duration");
rejects([&]{w.gameplay(0x3f0062,sell(900,500,120,1,false));},"bid above buyout");
{const auto r=w.gameplay(0x3f0062,sell(0,500,120,1,false));Reader q(r.body); // native Buyout Mode: start bid = buyout
expect(r.opcode==0x256 && q.le(8)==npc && q.le(8)==item && q.le(4)==500 && q.le(4)==500 && q.le(4)==120,"buyout-mode sell");q.end();}
rejects([&]{w.gameplay(0x3f0062,sell(0,0,120,1,false));},"no bid and no buyout");
w.inventory.items[item].fields[14]=5;
rejects([&]{w.gameplay(0x3f0062,sell(500,1000,120,3,false));},"partial stack");
expect(w.gameplay(0x3f0062,sell(500,1000,120,5,false)).opcode==0x256,"whole stack");
rejects([&]{w.gameplay(0x3f0062,sell(500,1000,120,5,false,2));},"two stacks");
// Cancel and bid.
{// Commodity posts: Source auctions whole stacks only, so the gateway picks whole carried stacks with the exact quantity.
const uint64_t B=0x4000000000000889ull,C=0x400000000000088aull;const uint64_t self=w.selected.guid;
auto carry=[&](uint64_t guid,unsigned slot,uint32_t n){
w.inventory.player_fields[F::PLAYER_FIELD_PACK_SLOT_1+2*slot]=uint32_t(guid);w.inventory.player_fields[F::PLAYER_FIELD_PACK_SLOT_1+2*slot+1]=uint32_t(guid>>32);
auto& u=w.inventory.items[guid];u.guid=guid;u.type=1;u.fields[3]=2589;u.fields[14]=n;
u.fields[F::ITEM_FIELD_OWNER]=uint32_t(self);u.fields[F::ITEM_FIELD_OWNER+1]=0;u.fields[F::ITEM_FIELD_CONTAINED]=uint32_t(self);u.fields[F::ITEM_FIELD_CONTAINED+1]=0;};
carry(item,0,5);carry(B,1,20);carry(C,2,20);
const auto& own=w.inventory.owned_bindings();
auto csell=[&](uint64_t unit,std::vector<std::pair<uint64_t,uint32_t>> use){
Bytes b=pack_guid(npc_m);le(b,unit,8);le(b,480,4);b.push_back(uint8_t(use.size()<<1));
for(auto [guid,n]:use){append(b,pack_guid(own.at(guid)));le(b,n,4);}return b;};
auto stacks=[&](std::span<const uint8_t> body){std::vector<std::pair<uint64_t,uint32_t>> v;for(auto& p:w.commodity_sell_packets(body)){
Reader q(p.body);expect(p.opcode==0x256 && q.le(8)==npc,"commodity sell op");auto it=q.le(8);auto bid=q.le(4);auto buy=q.le(4);
expect(bid==buy && q.le(4)==480,"buyout-mode commodity");v.push_back({it,uint32_t(buy)});}return v;};
// native draws 5 + 15: the 20 stack is sold whole instead
{auto v=stacks(csell(10,{{item,5},{B,15}}));expect(v.size()==1 && v[0].second==200 && (v[0].first==B||v[0].first==C),"one exact 20 stack");}
// whole listed stacks are kept
{auto v=stacks(csell(10,{{B,20},{item,5}}));expect(v.size()==2 && v[0]==std::pair<uint64_t,uint32_t>{B,200} && v[1]==std::pair<uint64_t,uint32_t>{item,50},"listed whole stacks");}
// 45 from partial picks: fewest whole stacks summing to 45 (20+20+5)
{auto v=stacks(csell(10,{{B,20},{C,20},{item,5}}));expect(v.size()==3,"45 = three whole stacks");}
{auto v=stacks(csell(10,{{item,5},{B,20},{C,15}}));expect(v.size()==2 && v[0].second+v[1].second==400,"40 = two 20 stacks");}
rejects([&]{w.commodity_sell_packets(csell(10,{{item,5},{B,2}}));},"7 has no whole-stack form");
rejects([&]{w.commodity_sell_packets(csell(10,{{B,25}}));},"use above stack");
w.inventory.items.erase(B);w.inventory.items.erase(C);w.inventory.items[item].fields[14]=5;
w.inventory.player_fields.erase(F::PLAYER_FIELD_PACK_SLOT_1+2);w.inventory.player_fields.erase(F::PLAYER_FIELD_PACK_SLOT_1+3);
w.inventory.player_fields.erase(F::PLAYER_FIELD_PACK_SLOT_1+4);w.inventory.player_fields.erase(F::PLAYER_FIELD_PACK_SLOT_1+5);}
{auto ops=[](const Bytes& bundle,uint32_t want,Bytes* body){if(bundle.empty())return 0;Reader z(bundle);auto n=z.le(4);int k=0;
for(uint64_t i=0;i<n;++i){auto op=z.le(4);auto sz=z.le(4);auto b=z.take(sz);if(op==want){++k;if(body)*body=Bytes(b.begin(),b.end());}}return k;};
// Service gossip shortcut: a no-gossip service NPC's one-option Source default menu is selected, not shown.
auto menu=[&](uint32_t text,std::vector<std::pair<uint32_t,uint8_t>> opts){Bytes b;le(b,npc,8);le(b,text,4);le(b,opts.size(),4);
for(auto [i,icon]:opts){le(b,i,4);b.push_back(icon);b.push_back(0);for(char c:std::string("GOSSIP_OPTION_AUCTIONEER"))b.push_back(uint8_t(c));b.push_back(0);}
le(b,0,4);return b;};
w.requests.clear();w.entities.actors[npc].npc_flags=4096;
auto out=w.translate({0x17d,menu(16777215,{{0,0}})},0x10046,0,nullptr);
expect(ops(out,0x670018,nullptr)==0,"service menu not shown");
expect(!w.requests.empty() && w.requests.back().opcode==0x17c,"service option selected");
{Reader q(w.requests.back().body);expect(q.le(8)==npc && q.le(4)==0,"selects the one option");q.end();}
// two options: never auto-picked
w.requests.clear();(void)w.translate({0x17d,menu(16777215,{{0,0},{1,0}})},0x10046,0,nullptr);
expect(std::none_of(w.requests.begin(),w.requests.end(),[](auto& r){return r.opcode==0x17c;}),"two options keep the menu");
// gossip NPC (flag 1) that is not a one-option banker keeps its menu
w.requests.clear();w.entities.actors[npc].npc_flags=1|128;(void)w.translate({0x17d,menu(1250,{{0,1}})},0x10046,0,nullptr);
expect(std::none_of(w.requests.begin(),w.requests.end(),[](auto& r){return r.opcode==0x17c;}),"gossip vendor keeps menu");
// one-option banker (gossip|banker, money-bag option) opens the bank
w.requests.clear();w.entities.actors[npc].npc_flags=257;(void)w.translate({0x17d,menu(1250,{{0,6}})},0x10046,0,nullptr);
expect(!w.requests.empty() && w.requests.back().opcode==0x17c,"banker opens directly");
w.requests.clear();w.entities.actors[npc].npc_flags=0;w.gossip.reset();}
{Bytes b=pack_guid(npc_m);le(b,77,4);le(b,0,4);b.push_back(0);auto r=w.gameplay(0x3f0063,b);Reader q(r.body);expect(r.opcode==0x257 && q.le(8)==npc && q.le(4)==77,"cancel");q.end();}
{Bytes b=pack_guid(npc_m);le(b,77,4);le(b,1500,8);b.push_back(0x80);append(b,addon("X",""));auto r=w.gameplay(0x3f0065,b);Reader q(r.body);
expect(r.opcode==0x25a && q.le(8)==npc && q.le(4)==77 && q.le(4)==1500,"tainted bid");q.end();}
{Bytes b=pack_guid(npc_m);le(b,77,4);le(b,3000000000ull,8);b.push_back(0);rejects([&]{w.gameplay(0x3f0065,b);},"bid bound");}
// Owned list (one sort) and bidded list (two ids, no sorts).
{Bytes b=pack_guid(npc_m);le(b,0,4);b.push_back(0x20);b.push_back(3);b.push_back(0x80);auto r=w.gameplay(0x3f0069,b);Reader q(r.body);
expect(r.opcode==0x259 && q.le(8)==npc && q.le(4)==0,"owned list");q.end();}
{Bytes b=pack_guid(npc_m);le(b,0,4);b.push_back(0x02);b.push_back(0x00);le(b,11,4);le(b,12,4);auto r=w.gameplay(0x3f006a,b);Reader q(r.body);
expect(r.opcode==0x264 && q.le(8)==npc && q.le(4)==0 && q.le(4)==2 && q.le(4)==11 && q.le(4)==12,"bidded list keeps tracked outbid ids");q.end();}
// Not an auctioneer creature: rejected.
{Bytes b=pack_guid(item_m);le(b,77,4);le(b,0,4);b.push_back(0);rejects([&]{w.gameplay(0x3f0063,b);},"non-creature auctioneer");}
// Personal list pipeline: owned request (Price sort) pages 52 Source rows, waits for templates, publishes once.
{auto fake=[](uint32_t mode,const uint8_t* in,uint32_t size,uint32_t,uint8_t* out,uint32_t cap,uint32_t* written)->int{
if(mode!=GW_CODEC_AUCTION_PERSONAL && mode!=GW_CODEC_AUCTION_BUCKETS)return GW_CODEC_PACKET;*written=size;if(cap<size)return GW_CODEC_CAPACITY;std::copy(in,in+size,out);return GW_CODEC_OK;};
auto rows=[](uint32_t first,uint32_t count,uint32_t total){Bytes b;le(b,count,4);
for(uint32_t i=0;i<count;++i){le(b,first+i,4);le(b,900,4);le(b,0,4);le(b,0,4);le(b,0,4);le(b,1,4);le(b,0,4);le(b,4509,8);
le(b,10,4);le(b,5,4);le(b,1000-(first+i),4);le(b,60000,4);le(b,0,8);le(b,0,4);}
le(b,total,4);return b;};
auto ops=[](const Bytes& bundle,uint32_t want,Bytes* body){if(bundle.empty())return 0;Reader z(bundle);auto n=z.le(4);int k=0;
for(uint64_t i=0;i<n;++i){auto op=z.le(4);auto sz=z.le(4);auto b=z.take(sz);if(op==want){++k;if(body)*body=Bytes(b.begin(),b.end());}}return k;};
Bytes req=pack_guid(npc_m);le(req,7,4);req.push_back(0x20);req.push_back(0);req.push_back(0); // one sort: Price ascending
w.gameplay_sent(0x3f0069,req);expect(w.auction_personal && w.auction_personal->awaiting,"owned list armed");w.requests.clear();
expect(ops(w.translate({0x25d,rows(1,50,52)},0x10046,0,fake),0x4702f9,nullptr)==0,"held while paging");
expect(w.requests.size()==1 && w.requests.back().opcode==0x259,"next owned page");{Reader x(w.requests.back().body);expect(x.le(8)==npc && x.le(4)==50,"listfrom 50");}
expect(ops(w.translate({0x25d,rows(51,2,52)},0x10046,0,fake),0x4702f9,nullptr)==0 && w.inventory.queries.contains(900),"template requested");
ItemTemplateReply tt;tt.entry=900;tt.found=true;tt.cls=4;tt.item_level=30;tt.required_level=20;tt.stack=1;tt.name="Robe";
w.inventory.peer_templates[900]=tt;w.inventory.queries.erase(900);w.inventory.peer_template_queries.erase(900);
Bytes dto;expect(ops(w.translate({0x1d1,Bytes(8,0)},0x10046,0,fake),0x4702f9,&dto)==1 && !w.auction_personal,"published once");
auto res=auction_personal_from_input(dto);
expect(res.owned && res.items.size()==52 && res.items.front().row.auction_id==52 && res.items.back().row.auction_id==1,"all rows, Price ascending");
expect(res.items.front().key.item==900 && res.items.front().key.item_level==30,"bucket key");
// Empty bidder list: answered without the codec.
Bytes breq=pack_guid(npc_m);le(breq,0,4);breq.push_back(0);breq.push_back(0);w.gameplay_sent(0x3f006a,breq);
Bytes empty;le(empty,0,4);le(empty,0,4);expect(ops(w.translate({0x265,empty},0x10046,0,nullptr),0x4702fa,nullptr)==1 && !w.auction_personal,"empty bidded list");
// Bidder list repeats a tracked id that is also a current bid: counted for paging, shown once.
{Bytes rq=pack_guid(npc_m);le(rq,0,4);rq.push_back(0x01);rq.push_back(0x00);le(rq,2,4);w.gameplay_sent(0x3f006a,rq); // one tracked id
auto two=rows(1,2,3);Bytes rep;le(rep,3,4);rep.insert(rep.end(),two.begin()+4,two.end()-4);rep.insert(rep.end(),two.begin()+4+64,two.end()-4);le(rep,3,4);
Bytes dto2;expect(ops(w.translate({0x265,rep},0x10046,0,fake),0x4702fa,&dto2)==1,"bidded published");
auto r2=auction_personal_from_input(dto2);expect(!r2.owned && r2.items.size()==2,"repeat shown once");}
// All fields after the auctioneer are zero for an unfiltered first browse page.
Bytes empty_browse = pack_guid(npc_m);
empty_browse.resize(empty_browse.size() + 23, 0);
// A second tab while one list runs waits for it (Tortoise drops overlapping AH list requests).
{w.gameplay_sent(0x3f0069,req);w.requests.clear();
Bytes rq=pack_guid(npc_m);le(rq,0,4);rq.push_back(0);rq.push_back(0);
expect(w.defer_personal_list(0x3f006a,rq) && w.requests.empty(),"second tab deferred");
Bytes d;expect(ops(w.translate({0x25d,rows(1,1,1)},0x10046,0,fake),0x4702f9,&d)==1,"first list published");
expect(w.auction_personal && !w.auction_personal->owned && w.requests.size()==1 && w.requests.back().opcode==0x264,"deferred tab sent after");
Bytes empty2;le(empty2,0,4);le(empty2,0,4);expect(ops(w.translate({0x265,empty2},0x10046,0,fake),0x4702fa,nullptr)==1,"deferred answered");
expect(!w.defer_personal_list(0x3f006a,rq),"idle: not deferred");
// A browse while a personal page is in flight also waits; an auctioneer close drops the result and the wait.
w.requests.clear();w.gameplay_sent(0x3f0069,req);
expect(w.defer_personal_list(0x3f0066,empty_browse),"browse deferred behind personal");
w.close_interaction(pack_guid(npc_m));expect(!w.auction_personal && w.auction_personal_deferred.empty() && w.ah_list_inflight && w.ah_list_retired,"close: results dropped, guard kept");
Bytes none;le(none,0,4);le(none,0,4);
expect(ops(w.translate({0x25d,none},0x10046,0,fake),0x4702f9,nullptr)==0 && !w.ah_list_inflight && !w.auction_personal,"late empty reply: no output, guard released");
// A queued, never-sent page is withdrawn on close together with its guard.
w.gameplay_sent(0x3f0069,req);w.requests.clear();w.translate({0x25d,rows(1,50,60)},0x10046,0,fake);
expect(w.requests.size()==1 && w.ah_list_inflight,"page queued");
w.close_interaction(pack_guid(npc_m));expect(w.requests.empty() && !w.ah_list_inflight && !w.ah_list_retired,"queued page withdrawn");
// A different reply kind does not release the guard; a silent decline expires after 15 s.
w.gameplay_sent(0x3f0069,req);w.translate({0x265,none},0x10046,0,fake);expect(w.ah_list_inflight,"other kind keeps guard");
w.ah_list_since-=std::chrono::seconds(16);expect(!w.ah_list_busy() && !w.auction_personal,"declined request expires");
// Expiry also withdraws a queued page; a direct request supersedes a deferred one of the same kind.
w.gameplay_sent(0x3f0069,req);w.requests.clear();w.translate({0x25d,rows(1,50,60)},0x10046,0,fake);
w.ah_list_since-=std::chrono::seconds(16);expect(!w.ah_list_busy() && w.requests.empty(),"expired page withdrawn");
w.gameplay_sent(0x3f0069,req);Bytes s1=pack_guid(npc_m);le(s1,0,4);s1.push_back(0);s1.push_back(0);
expect(w.defer_personal_list(0x3f006a,s1),"bids deferred");w.translate({0x25d,none},0x10046,0,fake);
expect(w.auction_personal_deferred.empty(),"deferred replayed");w.gameplay_sent(0x3f006a,s1);
expect(w.auction_personal_deferred.empty(),"direct send clears same-kind deferral");w.translate({0x265,none},0x10046,0,fake);
w.requests.clear();expect(!w.ah_list_inflight,"settled");}
// A cached browse from one auctioneer may outlive a request sent to another.
{
auto session = w;
const uint64_t other_npc = 0xF130000000000506ull;
const auto other_guid = g(0x506, 0x2000000000000005ull);
auto other_actor = session.entities.actors.at(npc);
other_actor.modern = other_guid;
session.entities.actors.emplace(other_npc, other_actor);
session.gameplay(0x3f0066, empty_browse);
session.gameplay_sent(0x3f0066, empty_browse);
session.translate({0x25c, rows(0, 0, 0)}, 0x10046, 0, fake);
expect(session.auction_browse && session.auction_browse->ordered, "first auctioneer has a cached browse");
Bytes owned = pack_guid(other_guid);
owned.resize(owned.size() + 5, 0);
session.gameplay(0x3f0069, owned);
session.gameplay_sent(0x3f0069, owned);
session.close_interaction(pack_guid(npc_m));
expect(!session.auction_browse && session.auction_personal, "close affects the matching snapshot");
auto result = session.translate({0x25d, rows(0, 0, 0)}, 0x10046, 0, fake);
expect(ops(result, 0x4702f9, nullptr) == 1, "closing the cached browse suppressed the other auctioneer's reply");
}
// Stackable owned rows with an exact whole buyout: commodity rows (unit price, item and key kept), DTO v2.
w.inventory.peer_templates[900].stack=20;w.gameplay_sent(0x3f0069,req);
{Bytes c;expect(ops(w.translate({0x25d,rows(1,2,2)},0x10046,0,fake),0x4702f9,&c)==1 && !w.auction_personal && c[0]==2,"commodity owned rows published");
auto pr=auction_personal_from_input(c);expect(pr.items.size()==2 && pr.items[0].row.unit_price && !pr.items[0].row.buyout && !pr.items[0].row.min_bid &&
pr.items[0].row.count==1 && pr.items[0].key.item==900,"unit price instead of bid/buyout");}
// A bid-only stackable record has no commodity form: the whole list holds (never a partial management list).
w.gameplay_sent(0x3f0069,req);
auto before=w.auction_holds;auto bidonly=rows(1,2,2);for(unsigned i=0;i<4;++i)bidonly[4+44+i]=0;
expect(ops(w.translate({0x25d,bidonly},0x10046,0,fake),0x4702f9,nullptr)==0 && w.auction_holds==before+1 && !w.auction_personal,"bid-only stackable held");}
std::cout<<"PASS native auction requests: "<<checks<<" checks\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}