Files
WoW-Hybrid-gateway/tests/current_realm_record_test.c

81 lines
4.8 KiB
C

#include "current_realm_record.h"
#include "engine_shipped_main_header.h"
#include <stdio.h>
#include <stdlib.h>
#define CHECK(x) do{if(!(x)){fprintf(stderr,"failed line %d: %s\n",__LINE__,#x);return 1;}}while(0)
static int shipped_header_test(const char* path){
unsigned char bytes[1024],changed[1024];FILE* f=fopen(path,"rb");
CHECK(f);CHECK(fread(bytes,1,sizeof(bytes),f)==sizeof(bytes));CHECK(!ferror(f));fclose(f);
CHECK(!memcmp(bytes,gw_engine_shipped_main_header_bytes,sizeof(bytes)));
CHECK(gw_engine_shipped_main_header_matches(bytes,sizeof(bytes)));
CHECK(!gw_engine_original_main_header_matches(bytes,sizeof(bytes)));
CHECK(!gw_engine_spell_main_header_matches(bytes,sizeof(bytes),0));
CHECK(gw_engine_spell_main_header_matches(bytes,sizeof(bytes),1));
CHECK(!gw_engine_spell_main_header_matches(bytes,sizeof(bytes),2));
CHECK(gw_engine_spell_main_header_matches(gw_engine_original_main_header_bytes,1024,0));
CHECK(!gw_engine_shipped_main_header_matches(gw_engine_original_main_header_bytes,1024));
CHECK(!gw_engine_shipped_main_header_matches(bytes,1023));
CHECK(!gw_engine_shipped_main_header_matches(NULL,1024));
/* Pin every byte, including unrelated flags/sections; no masked matcher. */
for(unsigned i=0;i<sizeof(bytes);++i){
memcpy(changed,bytes,sizeof(bytes));changed[i]^=1;
CHECK(!gw_engine_shipped_main_header_matches(changed,sizeof(changed)));
}
return 0;
}
int main(int argc,char** argv){
if(argc==1)CHECK(!shipped_header_test(GW_CURRENT_MAIN_HEADER_FIXTURE));
for(int i=1;i<argc;++i)CHECK(!shipped_header_test(argv[i]));
const size_t image_bytes=GW_CURRENT_REALM_OWNER_RVA+8u;
unsigned char* image=(unsigned char*)calloc(1,image_bytes);
unsigned char owner[0xc60],before[sizeof(owner)],expected[sizeof(owner)];
uint32_t address=0x46010046,count=1,id;uint64_t pointer=(uint64_t)(uintptr_t)owner;
const char name[]="ThunderClan Warriors";
CHECK(image);
for(unsigned test=0;test<16;++test){
memset(owner,0xa5,sizeof(owner));memcpy(owner+0x898,&address,4);memcpy(owner+0x828,&count,4);
memset(owner+0x89c,0,4);owner[0x8a0]=0;
memcpy(image+GW_CURRENT_REALM_OWNER_RVA,&pointer,8);
uint32_t build=70334,requested=address;int auth=1;size_t ib=image_bytes,ob=sizeof(owner),nb=sizeof(name);
const char* text=name;char invalid[129];memset(invalid,'x',sizeof(invalid));
if(test==1)build=70333;
if(test==2)auth=0;
if(test==3)requested=0x46010047;
if(test==4)memset(owner+0x828,0,4);
if(test==5)memset(image+GW_CURRENT_REALM_OWNER_RVA,0,8);
if(test==6)ib--;
if(test==7)ob=0x8a0+sizeof(name)-1;
if(test==8)text="";
if(test==9)text="bad\nname";
if(test==10)nb=sizeof(name)-1;
if(test==11){text=invalid;nb=sizeof(invalid);}
if(test==12){requested=0x46010000;memcpy(owner+0x898,&requested,4);}
if(test==13)text=NULL;
if(test==14)ob=0x828;
if(test==15)requested=0;
memcpy(before,owner,sizeof(owner));
int result=gw_current_realm_record(image,ib,owner,ob,build,auth,requested,text,nb);
if(test){CHECK(result==-1);CHECK(!memcmp(owner,before,sizeof(owner)));continue;}
CHECK(result==0);memcpy(&id,owner+0x89c,4);CHECK(id==70);CHECK(!strcmp((char*)owner+0x8a0,name));
memcpy(expected,before,sizeof(owner));memcpy(expected+0x89c,&id,4);memcpy(expected+0x8a0,name,sizeof(name));
CHECK(!memcmp(expected,owner,sizeof(owner))); /* All other bytes untouched. */
CHECK(!gw_current_realm_record(image,ib,owner,ob,build,auth,requested,text,nb));
CHECK(!memcmp(expected,owner,sizeof(owner))); /* Idempotent. */
}
/* Bounded 127-byte names, UTF-8 bytes and changing to a shorter new realm.
* The helper never normalizes the AuthResponse display name or wipes tail. */
memcpy(owner+0x898,&address,4);memcpy(owner+0x828,&count,4);
memcpy(image+GW_CURRENT_REALM_OWNER_RVA,&pointer,8);
char maximum[128];memset(maximum,'r',127);maximum[127]=0;
CHECK(!gw_current_realm_record(image,image_bytes,owner,sizeof(owner),70334,1,address,maximum,sizeof(maximum)));
CHECK(!memcmp(owner+0x8a0,maximum,sizeof(maximum)));
const char unicode[]="R\xc3\xa9";
CHECK(!gw_current_realm_record(image,image_bytes,owner,sizeof(owner),70334,1,address,unicode,sizeof(unicode)));
CHECK(!memcmp(owner+0x8a0,unicode,sizeof(unicode)));
CHECK(owner[0x8a0+sizeof(unicode)]=='r');
CHECK(gw_current_realm_record(NULL,image_bytes,owner,sizeof(owner),70334,1,address,name,sizeof(name))==-1);
CHECK(gw_current_realm_record(image,image_bytes,NULL,sizeof(owner),70334,1,address,name,sizeof(name))==-1);
free(image);puts("current realm record: PASS (fake image, guards, exact writes, repeat/name bounds)");return 0;
}