Files
WoW-Hybrid-gateway/tests/private_club_membership_observation_test.py

165 lines
11 KiB
Python

"""Actual game-service copy tail + numeric-only optional membership journal."""
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
FIXTURE = r'''
#include "private_gameplay_capture.hpp"
#include "club_service.hpp"
#include <fstream>
#include <iterator>
#include <optional>
#include <stdexcept>
#include <iostream>
using Bytes=std::vector<uint8_t>;
namespace gateway {
@ACTUAL_READER@
}
static void check(bool value,const char* reason){if(!value)throw std::runtime_error(reason);}
static void le(Bytes& out,uint64_t value,unsigned size){for(unsigned i=0;i<size;++i)out.push_back(uint8_t(value>>(8*i)));}
static Bytes field(unsigned key,const Bytes& value){Bytes out{uint8_t((key<<3)|2),uint8_t(value.size())};out.insert(out.end(),value.begin(),value.end());return out;}
static Bytes rpc(uint64_t member,uint32_t service=0x94b94786,uint32_t method=1,uint32_t status=0,bool empty=false){
Bytes id{0x10};do{uint8_t b=member&127;member>>=7;id.push_back(b|(member?128:0));}while(member);
// Account and description sentinels are present but must never be copied.
id.insert(id.end(),{0x18,0xb4,0xf1,0x04});
Bytes club{0x08,0x21};auto name=field(3,Bytes{'N','O','_','T','E','X','T'});club.insert(club.end(),name.begin(),name.end());
Bytes description=field(1,id);auto text=field(2,club);description.insert(description.end(),text.begin(),text.end());
auto payload=field(1,field(1,description));if(empty)payload.clear();
Bytes out;le(out,status,4);le(out,(uint64_t(service)<<32)|0x80000000u|method,8);le(out,1,8);le(out,999,4);le(out,payload.size(),4);out.insert(out.end(),payload.begin(),payload.end());return out;
}
static Bytes bundle(uint32_t opcode,const Bytes& payload){Bytes out;le(out,1,4);le(out,opcode,4);le(out,payload.size(),4);out.insert(out.end(),payload.begin(),payload.end());return out;}
struct World {struct {uint64_t guid=4510;} selected;uint32_t realm=70;bool club_membership_reply_recorded=false;};
struct gw_native_transport {World* world;bool scope=true;PrivateGameplayCapture private_capture;std::optional<Bytes> world_encoded;std::optional<gateway::LegacyPacket> world_packet;};
static thread_local unsigned object_updates_rejected_in_row=0;
static bool private_gameplay_scope(const gw_native_transport* t){return t->scope;}
static void capture_source_input(gw_native_transport*){}
@ACTUAL_OBSERVATION@
static int actual_copy_tail(gw_native_transport* t,uint8_t* bytes,uint32_t capacity,uint32_t* required){
@ACTUAL_COPY_TAIL@
}
int main(){try{
char directory[]="/tmp/gw-club-member-observation-XXXXXX";check(mkdtemp(directory),"private directory");
std::string path=std::string(directory)+"/membership.jsonl";setenv("GW_PRIVATE_GAMEPLAY_CAPTURE",path.c_str(),1);
World world;gw_native_transport transport{&world};
const uint64_t mismatched=0x123400000000aabbULL;
const auto wire=bundle(0x4702b0,rpc(mismatched));
auto& capture=transport.private_capture;
const auto observe=[&](const Bytes& value){transport.world_encoded=value;capture_club_membership_reply(&transport);};
transport.scope=false;observe(wire);transport.scope=true;
world.selected.guid=0;observe(wire);world.selected.guid=4510;
world.realm=0;observe(wire);world.realm=70;
for(const auto& ignored:{bundle(0x440124,rpc(mismatched)),bundle(0x4702b1,rpc(mismatched)),
bundle(0x4702b0,rpc(mismatched,0xe273de0e)),bundle(0x4702b0,rpc(mismatched,0x94b94786,3)),
bundle(0x4702b0,rpc(mismatched,0x94b94786,1,1)),bundle(0x4702b0,rpc(mismatched,0x94b94786,1,0,true)),Bytes{1,0,0,0}})
observe(ignored);
auto malformed=wire;malformed.pop_back();observe(malformed);
auto duplicate=wire;duplicate.push_back(0);observe(duplicate);
check(!std::filesystem::exists(path)&&capture.count()==0,"scope/unrelated/empty/malformed opened journal or consumed slot");
transport.world_encoded=wire;uint32_t required=0;
check(actual_copy_tail(&transport,nullptr,0,&required)==-3&&required==wire.size(),"sizing result changed");
Bytes output(required,0);check(actual_copy_tail(&transport,output.data(),required-1,&required)==-3,"short output result changed");
check(capture.count()==0&&!std::filesystem::exists(path)&&transport.world_encoded.has_value(),"sizing/retry must not log");
check(actual_copy_tail(&transport,output.data(),output.size(),&required)==0&&output==wire&&!transport.world_encoded,"successful copy changed");
check(capture.count()==1&&capture.error()==0,"successful copy records once");
transport.world_encoded=bundle(0x4702b0,rpc(123));output.resize(transport.world_encoded->size());
check(actual_copy_tail(&transport,output.data(),output.size(),&required)==0&&capture.count()==1,"later reply retried observation");
// A character logout retains the transport and gives the next entry its own world.
World same_character;
transport.world=&same_character;transport.world_encoded=wire;output.resize(wire.size());
check(actual_copy_tail(&transport,output.data(),output.size(),&required)==0&&capture.count()==2,"same-character re-entry suppressed observation");
World other_character;other_character.selected.guid=4509;
transport.world=&other_character;transport.world_encoded=wire;
check(actual_copy_tail(&transport,output.data(),output.size(),&required)==0&&capture.count()==3,"new character suppressed observation");
observe(wire);
check(capture.count()==3,"repeated membership after character change");
std::ifstream file(path);std::string text((std::istreambuf_iterator<char>(file)),{});
check(text.find("\"member_unique_id\":\""+std::to_string(mismatched)+"\"")!=std::string::npos,"actual mismatched member ID must not be recomputed");
check(text.find("\"source_guid\":\"4510\"")!=std::string::npos&&text.find("\"realm\":70")!=std::string::npos,"current world context missing");
check(text.find("\"bytes\":"+std::to_string(wire.size()-12)+",")!=std::string::npos,"observed RPC length missing");
check(text.find("NO_TEXT")==std::string::npos&&text.find("80052")==std::string::npos&&text.find("body_hex")==std::string::npos&&text.find("account")==std::string::npos&&text.find("token")==std::string::npos,"text/account/raw RPC metadata leaked");
struct stat mode{};check(!stat(path.c_str(),&mode)&&(mode.st_mode&0777)==0600,"journal mode changed");
// Explicit zero is an observed mismatch; an absent ID is not synthesized.
std::string zero_path=std::string(directory)+"/zero.jsonl";setenv("GW_PRIVATE_GAMEPLAY_CAPTURE",zero_path.c_str(),1);
World zero_world;gw_native_transport zero{&zero_world};zero.world_encoded=bundle(0x4702b0,rpc(0));
capture_club_membership_reply(&zero);
std::ifstream zero_file(zero_path);std::string zero_text((std::istreambuf_iterator<char>(zero_file)),{});
check(zero.private_capture.count()==1&&zero_text.find("\"member_unique_id\":\"0\"")!=std::string::npos,"explicit zero member must remain observable");
std::string unsafe_path=std::string(directory)+"/unsafe.jsonl";
check(!symlink(path.c_str(),unsafe_path.c_str()),"unsafe output fixture");
setenv("GW_PRIVATE_GAMEPLAY_CAPTURE",unsafe_path.c_str(),1);
World unsafe_world;gw_native_transport unsafe{&unsafe_world};unsafe.world_encoded=wire;output.resize(wire.size());
check(actual_copy_tail(&unsafe,output.data(),output.size(),&required)==0&&output==wire,
"journal failure changed copied service output");
check(unsafe.private_capture.error()!=0&&!unsafe.world_encoded,"journal failure changed service completion");
// The extracted production copy tail also owns the update-rejection reset.
// Sizing and short copies must preserve both the packet and its streak.
for(const uint32_t opcode : {0xa9u,0x1f6u,0x120u}){
object_updates_rejected_in_row=7;
transport.world_packet.emplace();transport.world_packet->opcode=opcode;
transport.world_encoded=wire;output.resize(wire.size());
check(actual_copy_tail(&transport,nullptr,0,&required)==-3,
"update sizing result changed");
check(actual_copy_tail(&transport,output.data(),required-1,&required)==-3,
"update short-copy result changed");
check(object_updates_rejected_in_row==7&&transport.world_packet.has_value(),
"retry consumed packet or reset rejection streak");
check(actual_copy_tail(&transport,output.data(),output.size(),&required)==0&&output==wire,
"update successful copy changed");
check(!transport.world_packet&&!transport.world_encoded,
"successful copy retained packet");
check(object_updates_rejected_in_row==(opcode==0x120u?7u:0u),
"only copied object updates reset rejection streak");
}
std::filesystem::remove_all(directory);std::cout<<"PASS actual copy-only numeric Subscribe observation, mismatched value retained, private filters and once bound\n";return 0;
}catch(const std::exception& error){std::cerr<<error.what()<<'\n';return 1;}}
'''
class PrivateClubMembershipObservation(unittest.TestCase):
def test_actual_copy_boundary_and_numeric_only_projection(self):
source = (ROOT / 'native/session.cpp').read_text()
start = source.index(' capture_source_input(t);', source.index('extern "C" int gw_native_transport_world_packet('))
end = source.index(' }catch(', start)
tail = source[start:end]
self.assertEqual(tail.count('club_membership_reply('), 1)
start = source.index('static void capture_club_membership_reply(')
end = source.index('\nstatic void capture_source_input(', start)
observation = source[start:end]
protocol = (ROOT / 'src/protocol.cpp').read_text()
# Compile the production game-data Reader functions, with no auth or
# crypto translation unit/archive in this isolated fixture.
readers = []
for signature in ('Bytes Reader::take(', 'uint64_t Reader::le(', 'void Reader::end()'):
start = protocol.index(signature)
opening = protocol.index('{', start)
depth = 1
end = opening + 1
while depth:
depth += (protocol[end] == '{') - (protocol[end] == '}')
end += 1
readers.append(protocol[start:end])
compiler = shutil.which('c++')
self.assertIsNotNone(compiler)
with tempfile.TemporaryDirectory() as temporary:
path = Path(temporary)
unit = path / 'check.cpp'
unit.write_text(FIXTURE.replace('@ACTUAL_COPY_TAIL@', tail)
.replace('@ACTUAL_OBSERVATION@', observation)
.replace('@ACTUAL_READER@', '\n'.join(readers)))
build = subprocess.run([compiler, '-std=c++20', '-Wall', '-Wextra', '-Werror',
'-Wno-missing-field-initializers', '-I', str(ROOT / 'native'),
'-I', str(ROOT / 'include'),
str(unit), '-o', str(path / 'check')], capture_output=True, text=True)
self.assertEqual(build.returncode, 0, build.stdout + build.stderr)
run = subprocess.run([str(path / 'check')], capture_output=True, text=True)
self.assertEqual(run.returncode, 0, run.stdout + run.stderr)
self.assertIn('PASS actual copy-only', run.stdout)
if __name__ == '__main__':
unittest.main()