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

91 lines
5.6 KiB
C++

// Gitea #13: every Blizzard host literal of the 70334 client is found as a whole host and replaced by a
// same-length .invalid host; longer names that merely contain one, and unrelated text, stay untouched.
#include "nohelper/blizzard_egress.h"
#include <cstdio>
#include <cstdlib>
#include <string>
static void check(bool ok, const char* what){if(!ok){std::fprintf(stderr,"FAIL: %s\n",what);std::exit(1);}}
static std::string neutralise(std::string s){
size_t at=0,length=0;
auto* data=reinterpret_cast<uint8_t*>(s.data());
while((at=gw_egress_find(data,s.size(),at,&length))<s.size()){
char rep[64];gw_egress_replacement(rep,length);s.replace(at,length,rep,length);at+=length;
}
return s;
}
int main(){
// Literals as they sit in WowB.exe / WowB_loader.dll (NUL separated).
std::string image=std::string("https://breaking-news.support.blizzard.com/service/%s/%s/%s/%s\0",63)+
std::string("https://breaking-news.support.battlenet.com.cn/service/%s/%s/%s/%s\0",67)+
std::string("https://wow.ingest.gdp.blizzard.com\0",36)+
std::string("https://telemetry-in.battle.net/\0",33)+
std::string("telemetry-in.battlenet.com.cn\0",30)+
std::string("https://support.blizzard.com/article/31024\0",43)+
std::string("play.octowow.st\0",16);
const auto out=neutralise(image);
check(out.size()==image.size(),"image size changed");
for(size_t h=0;h<GW_EGRESS_HOST_COUNT;++h)
check(out.find(gw_egress_hosts[h])==std::string::npos,"a Blizzard host survived");
check(out.find("https://xxxxxxxxxxxxxxxxxxxxxxxxxx.invalid/service/%s/%s/%s/%s")!=std::string::npos,"breaking news URL shape");
check(out.find("https://xxxxxxxxxxxx.invalid/article/31024")!=std::string::npos,"support article link neutralised");
check(out.find("play.octowow.st")!=std::string::npos,"game host must stay");
// Whole-host matching: a longer host containing a blocked one is not rewritten.
check(neutralise("notsupport.blizzard.community")=="notsupport.blizzard.community","prefix guard");
check(neutralise("wow.ingest.gdp.blizzard.community")=="wow.ingest.gdp.blizzard.community","suffix guard");
check(neutralise("eu.wow.ingest.gdp.blizzard.com").find(".invalid")!=std::string::npos,"subdomain dot is a boundary");
// UTF-16LE literals (strings -el): nydus redirect and checkout hosts.
static const char16_t lit[]=u"https://enGB.nydus.battle.net/WoW/x\0https://us.checkout.battle.net/\0play.octowow.st";
std::u16string wide(lit,sizeof(lit)/2-1);
std::string bytes(reinterpret_cast<const char*>(wide.data()),wide.size()*2);
auto* wb=reinterpret_cast<uint8_t*>(bytes.data());size_t at=0,len=0,hits=0;
while((at=gw_egress_find_stride(wb,bytes.size(),at,&len,2))<bytes.size()){gw_egress_write(wb+at,len,2);at+=len*2;++hits;}
check(hits==2,"two wide hosts");
std::u16string after(reinterpret_cast<const char16_t*>(bytes.data()),wide.size());
check(after.find(u"enGB.nydus.battle.net")==std::u16string::npos && after.find(u"xxxxxxxxxxxxx.invalid/WoW")!=std::u16string::npos,"wide nydus rewritten");
check(after.find(u"play.octowow.st")!=std::u16string::npos,"wide game host stays");
// Browser guard: Blizzard web addresses (any subdomain, neutralised .invalid) refused; Octo, files, other schemes pass.
auto blocked=[](const char16_t* u){return gw_blizzard_url_blocked(reinterpret_cast<const uint16_t*>(u))!=0;};
check(blocked(u"https://support.blizzard.com/article/31024"),"support refused");
check(blocked(u"HTTPS://Shop.Battle.NET/x"),"case-insensitive refusal");
check(blocked(u"https://xxxxxxxxxxxx.invalid/article/31024"),"neutralised host refused");
check(blocked(u"http://user@eu.battle.net:443/"),"userinfo and port");
check(blocked(u"https://battle.net"),"bare domain");
check(!blocked(u"https://octowow.st/git/praxis/WoW-Hybrid-gateway/issues"),"octo link passes");
check(!blocked(u"https://notbattle.net/"),"lookalike domain passes");
check(!blocked(u"C:\\Games\\WTF\\Account"),"local folder passes");
check(!blocked(nullptr),"null passes");
std::puts("blizzard egress ok");
/* #119: the fast anchor search must give exactly the reference results, both strides. */
for(size_t h=0;h<GW_EGRESS_HOST_COUNT;++h){
const char* host=gw_egress_hosts[h]; bool anchored=false;
for(size_t k=0;host[k+1];++k) if(host[k]=='b') anchored=true;
check(anchored,"every host needs a 'b' anchor for the fast search");
}
{
uint32_t seed=0x1197u; auto rnd=[&](){seed=seed*1664525u+1013904223u;return seed>>8;};
const char alphabet[]="abcdefghijklmnopqrstuvwxyz.-/:0123456789 ABZ\0";
for(int round=0;round<400;++round){
for(size_t stride=1;stride<=2;++stride){
std::string buf(512+rnd()%2048,'\0');
for(char& c:buf) c=alphabet[rnd()%(sizeof(alphabet)-1)];
for(int k=0;k<6;++k){
const char* host=gw_egress_hosts[rnd()%GW_EGRESS_HOST_COUNT];
size_t n=strlen(host), at=rnd()%buf.size();
for(size_t i=0;i<n && at+i*stride+stride<=buf.size();++i){buf[at+i*stride]=host[i]; if(stride==2) buf[at+i*stride+1]='\0';}
}
const uint8_t* d=(const uint8_t*)buf.data(); size_t a=0,b=0,la=0,lb=0;
for(;;){
a=gw_egress_find_stride(d,buf.size(),a,&la,stride); b=gw_egress_find_stride_reference(d,buf.size(),b,&lb,stride);
check(a==b && la==lb,"fast search differs from reference");
if(a>=buf.size()||a!=b) break;
a+=la*stride; b+=lb*stride;
}
}
}
}
return 0;
}