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

96 lines
8.2 KiB
C++

#include "world_translation.hpp"
#include "gateway/party_invite_realm.hpp"
#include <algorithm>
#include <iostream>
#include <source_location>
using namespace gateway;
static void expect(bool ok,std::source_location at=std::source_location::current()){if(!ok)throw Error("party invite assertion at line "+std::to_string(at.line()));}
static int fake_codec(uint32_t mode,const uint8_t* in,uint32_t size,uint32_t,uint8_t* out,uint32_t capacity,uint32_t* written){
if(mode!=GW_CODEC_PARTY_INVITE)return GW_CODEC_PACKET;
*written=size;if(capacity<size)return GW_CODEC_CAPACITY;std::copy(in,in+size,out);return GW_CODEC_OK;
}
template<class F> static void rejects(F f){bool rejected=false;try{f();}catch(const Error&){rejected=true;}expect(rejected);}
static Bytes request(std::string name){
Bytes b{0,uint8_t(name.size()>>1),uint8_t((name.size()&1)<<7),0,0,0,0,0,0,0};
b.insert(b.end(),name.begin(),name.end());b.push_back(0);return b;
}
int main(){try{
auto req=request("Gatewayref");auto mapped=source_party_invite_request(req,0x10046);
Bytes expected;cstring(expected,"Gatewayref");expect(mapped==expected);
// Stock writer: mandatory voice-provider selector u8 (0..2) after the realm, then PartyIndex when flagged.
auto live=req;live.back()=1;expect(source_party_invite_request(live,0x10046)==expected); // live aea6a201 19-byte form
auto two=req;two.back()=2;expect(source_party_invite_request(two,0x10046)==expected);
auto malformed=req;malformed.back()=3;rejects([&]{source_party_invite_request(malformed,0x10046);});
malformed=req;malformed[3]=1;rejects([&]{source_party_invite_request(malformed,0x10046);});
auto tc=req;tc.pop_back();rejects([&]{source_party_invite_request(tc,0x10046);}); // selector is never omitted
auto indexed=live;indexed[0]=0x80;indexed.push_back(0);expect(source_party_invite_request(indexed,0x10046)==expected);
auto foreign=indexed;foreign.back()=1;rejects([&]{source_party_invite_request(foreign,0x10046);}); // instance party
malformed=indexed;malformed.pop_back();rejects([&]{source_party_invite_request(malformed,0x10046);});
malformed=req;malformed.push_back(0);rejects([&]{source_party_invite_request(malformed,0x10046);});
expect(source_party_invite_response(Bytes{0x40}));expect(!source_party_invite_response(Bytes{0}));
expect(source_party_invite_response(Bytes{0xe0,0,0}));
rejects([]{source_party_invite_response(Bytes{0x41});});rejects([]{source_party_invite_response(Bytes{0x80,1});});
NativeWorldTranslation w{};w.realm=70;w.realm_name="Local Realm";w.selected.guid=4509;w.selected.race=1;w.selected.name="Receiver";
w.self_created=true;w.location=WorldLocation{1,{0,0,0,0}};w.virtual_realm_address=0x10046;
expect(w.gameplay(0x440030,req).opcode==0x6e);
rejects([&]{w.gameplay(0x440032,Bytes{0x40});});
expect(w.party_response_idle(0x440032,Bytes{0}) && !w.party_response_idle(0x440032,Bytes{0,0})); // live 2026-11-22: fatal before
Bytes invite;cstring(invite,"Gatewayref");Bytes empty(4,0);
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)!=empty && w.party_invitation_current());
auto& peer=w.entities.peer_players[4514];peer.generation=7;peer.named=true;peer.identity.guid=4514;peer.identity.name="Gatewayref";peer.identity.race=1;
auto& actor=w.entities.actors[4514];actor.generation=7;actor.kind=4;actor.sent=true;actor.presentation_map=1;actor.presentation_realm=70;
auto bundle=w.translate({0x6f,invite},0x10046,0,fake_codec);Reader outer(bundle);expect(outer.le(4)==1 && outer.le(4)==0x470061);auto size=outer.le(4);auto wire=outer.take(size);outer.end();
Reader r(wire);expect(r.le(2)==70 && r.le(4)==0x10046 && r.le(8)==4514 && r.cstring()=="Gatewayref" && r.cstring(127)=="Local Realm");r.end();
expect(!w.party_response_idle(0x440032,Bytes{0x40}) && w.gameplay(0x440032,Bytes{0x40}).opcode==0x72 && w.party_invitation);
const auto deadline=w.party_invitation->expires;
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)==empty);
expect(w.party_invitation->expires==deadline && w.party_invitation_current());
expect(party_invite_normalized_realm(w.realm_name)=="LocalRealm");
for(auto bad:{"", " ", "Bad\nRealm"})rejects([&]{party_invite_normalized_realm(bad);});
auto stale=w;stale.entities.actors[4514].generation++;rejects([&]{stale.gameplay(0x440032,Bytes{0});});
w.gameplay_sent(0x440032,Bytes{0x40});expect(w.party_invitation && w.party_invitation->accepted);rejects([&]{w.gameplay(0x440032,Bytes{0x40});});
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)==empty);
expect(w.party_invitation->expires==deadline && !w.party_invitation_current());
// A solo roster finishes the accepted offer; a subsequent new invite works.
w.translate({0x7d,Bytes(24,0)},0x10046,0,fake_codec);expect(!w.party_invitation);
w.translate({0x6f,invite},0x10046,0,fake_codec);expect(w.gameplay(0x440032,Bytes{0}).opcode==0x73);
w.gameplay_sent(0x440032,Bytes{0});expect(!w.party_invitation);
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)!=empty);
w.party_invitation->expires=std::chrono::steady_clock::now()-std::chrono::seconds(1);
rejects([&]{w.gameplay(0x440032,Bytes{0});});
w.translate({0x6f,invite},0x10046,0,fake_codec);expect(w.party_invitation.has_value());
w.translate({0x7d,Bytes(24,0)},0x10046,0,fake_codec);expect(!w.party_invitation);
auto copy=peer;copy.identity.guid=4515;w.entities.peer_players[4515]=copy;auto actor2=actor;w.entities.actors[4515]=actor2;
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)==empty && !w.party_invitation);
// #110: an inviter out of view (or of another race) is resolved from the Source guild roster and stays answerable.
w.entities.peer_players.clear();w.entities.actors.clear();
Bytes unresolved;cstring(unresolved,"Gatewaytest"); // supplied 476174657761797465737400
auto notice=w.translate({0x6f,unresolved},0x10046,0,fake_codec);
Reader notice_reader(notice);expect(notice_reader.le(4)==1 && notice_reader.le(4)==0x4c0001);
const std::string notice_bytes(notice.begin(),notice.end());
expect(notice_bytes.find("Gatewaytest invites you")!=std::string::npos);
expect(notice_bytes.find("/run AcceptGroup()")!=std::string::npos);
expect(w.party_invitation && !w.party_invitation->guid && w.party_invitation_current());
expect(w.translate({0x6f,unresolved},0x10046,0,fake_codec)==empty); // bounded duplicate
expect(w.gameplay(0x440032,Bytes{0x40}).opcode==0x72);
w.gameplay_sent(0x440032,Bytes{0x40});expect(!w.party_invitation_current());
w.party_invitation.reset();w.translate({0x6f,unresolved},0x10046,0,fake_codec);
expect(w.gameplay(0x440032,Bytes{0}).opcode==0x73);
w.gameplay_sent(0x440032,Bytes{0});expect(!w.party_invitation);
w.translate({0x6f,unresolved},0x10046,0,fake_codec);
w.party_invitation->expires=std::chrono::steady_clock::now()-std::chrono::seconds(1);
rejects([&]{w.gameplay(0x440032,Bytes{0x40});});
w.party_invitation.reset();
GuildRoster roster;GuildRosterMember gm;gm.guid=4600;gm.name="Gatewayref";roster.members.push_back(gm);w.guild_management_roster=roster;
bundle=w.translate({0x6f,invite},0x10046,0,fake_codec);Reader o2(bundle);expect(o2.le(4)==1 && o2.le(4)==0x470061);
{auto n=o2.le(4);auto w2=o2.take(n);Reader r2(w2);expect(r2.le(2)==70 && r2.le(4)==0x10046 && r2.le(8)==4600 && r2.cstring()=="Gatewayref" && r2.cstring(127)=="Local Realm");r2.end();}
expect(w.party_invitation && w.party_invitation_current() && w.gameplay(0x440032,Bytes{0x40}).opcode==0x72);
gm.guid=4601;w.guild_management_roster->members.push_back(gm); // two roster rows with one name: held
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)==empty && !w.party_invitation);
w.guild_management_roster.reset();Character c{};c.guid=4602;c.name="gatewayref";w.chat_players[4602]=c;
w.translate({0x6f,invite},0x10046,0,fake_codec);expect(w.party_invitation && w.party_invitation->guid==4602);
w.party_invitation.reset();w.loot.group.emplace();w.loot.group->explicit_solo=false;
expect(w.translate({0x6f,invite},0x10046,0,fake_codec)==empty && !w.party_invitation);
std::cout<<"PASS bounded original invite request/response and Source identity/pending guards\n";return 0;
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}