Files
WoW-Hybrid-gateway/tests/native_account_memory_fixture.c
2026-11-23 03:28:32 +02:00

316 lines
20 KiB
C

/* Own Windows worker only, with a deterministic dispatcher/thread fixture.
* Real SRP/M2 is tested separately by native-wire-memory-auth. No game calls.
*/
#include <windows.h>
#include "wine_wire_abi.h"
#include "wine_wire_host.h"
#ifndef GW_ACCOUNT_TEST_PORT
#define GW_ACCOUNT_TEST_PORT 3724
#endif
static LPTHREAD_START_ROUTINE pending;
static void* pending_argument;
static unsigned int thread_failure,auth_failure,memory_calls,file_calls,wipe_calls,explicit_calls,explicit_transports;
static unsigned int inspect_prepared,snapshot_checks,realm_reads;
#ifdef GW_ACCOUNT_SINGLETON_TEST
static unsigned int singleton_realm_count,singleton_waits,singleton_bad_realm,singleton_open_index;
#endif
static const char* fixture_expected_explicit_auth="192.0.2.7";
static const unsigned char* borrowed_password;
static HANDLE WINAPI fixture_create_thread(LPSECURITY_ATTRIBUTES a,SIZE_T size,LPTHREAD_START_ROUTINE run,LPVOID arg,DWORD flags,LPDWORD id);
static DWORD WINAPI fixture_wait(HANDLE handle,DWORD timeout);
static BOOL WINAPI fixture_close(HANDLE handle);
static VOID WINAPI fixture_sleep(DWORD milliseconds);
static void* fixture_wipe(void* data,SIZE_T size);
#undef SecureZeroMemory
#define SecureZeroMemory fixture_wipe
#define CreateThread fixture_create_thread
#define WaitForSingleObject fixture_wait
#define CloseHandle fixture_close
#ifdef GW_ACCOUNT_IDLE_TEST
enum chooser_case { ordinary_wait, select_after_idle, refuse_refresh, cancel_refresh, missed_deadline };
static enum chooser_case chooser_test;
static ULONGLONG chooser_clock=300000;
static unsigned chooser_refreshes;
static ULONGLONG WINAPI fixture_tick(void){return chooser_clock;}
#define GetTickCount64 fixture_tick
#endif
#define Sleep fixture_sleep
#define GW_ENGINE_REALM_EXPORT
#include "../native/wine_wire_account.c"
#include "../native/wine_engine_realm.c"
#ifdef GW_ACCOUNT_IDLE_TEST
/* Production's two journal sinks write diagnostics; this standalone worker has no journal. */
void gw_wire_realm_note_entered_original(void){}
void gw_wire_account_identity_note(uint32_t gen,uint32_t observed,uint32_t expected,uint32_t comparison){
(void)gen;(void)observed;(void)expected;(void)comparison;
}
#endif
static void message(const char* text){unsigned int size=0;DWORD written;while(text[size])++size;WriteFile(GetStdHandle(STD_OUTPUT_HANDLE),text,size,&written,0);}
static void check(int okay,unsigned int code){
if(!okay){
char reverse[10],number[11];unsigned int n=0,i,value=code;
do{reverse[n++]=(char)('0'+value%10);value/=10;}while(value);
for(i=0;i<n;++i)number[i]=reverse[n-i-1];number[n]=0;
message("GW_MEMORY_FIXTURE fail=");message(number);message("\n");ExitProcess(code);
}
}
static int all_zero(const void* input,unsigned int size){const unsigned char* b=input;unsigned int i;for(i=0;i<size;++i)if(b[i])return 0;return 1;}
static void* fixture_wipe(void* data,SIZE_T size){
volatile unsigned char* bytes=data;SIZE_T i;
for(i=0;i<size;++i)bytes[i]=0;
if(data==borrowed_password && size==64)++wipe_calls;
return data;
}
static HANDLE WINAPI fixture_create_thread(LPSECURITY_ATTRIBUTES a,SIZE_T size,LPTHREAD_START_ROUTINE run,LPVOID arg,DWORD flags,LPDWORD id){
(void)a;(void)size;(void)flags;(void)id;
if(thread_failure)return 0;
pending=run;pending_argument=arg;return (HANDLE)(ULONG_PTR)1;
}
static DWORD WINAPI fixture_wait(HANDLE handle,DWORD timeout){(void)handle;(void)timeout;return pending?WAIT_TIMEOUT:WAIT_OBJECT_0;}
static BOOL WINAPI fixture_close(HANDLE handle){(void)handle;return TRUE;}
static VOID WINAPI fixture_sleep(DWORD milliseconds){
#ifdef GW_ACCOUNT_SINGLETON_TEST
#ifdef GW_ACCOUNT_IDLE_TEST
if(gw_wire_auth_data.state==5 && chooser_test!=ordinary_wait){
++singleton_waits;
check(singleton_waits<=6,120);
chooser_clock+=chooser_test==missed_deadline?130000:30000;
if(singleton_waits==6)gw_wire_auth_data.selected_index=1;
return;
}
#endif
if(gw_wire_auth_data.state==5){++singleton_waits;gw_wire_auth_data.cancel=1;return;}
#endif
struct gw_native_realm frozen;uint32_t index,kind;uint16_t port;struct gw_engine_realm_address built;
const char other[]="203.0.113.44:8090";unsigned int expected_kind=(unsigned int)gw_wire_account_endpoint_kind();
(void)milliseconds;check(inspect_prepared && gw_wire_auth_data.state==2 && realm_reads==2,100);
// A public row with the SAME port must not redirect the prepared channel.
gw_wire_auth_data.selected_index=63;__builtin_memset(gw_wire_auth_realms[0].address,0,sizeof(gw_wire_auth_realms[0].address));
__builtin_memcpy(gw_wire_auth_realms[0].address,other,sizeof(other));
check(!gw_wire_account_selected_realm(&frozen,&index,&kind,&port) && index==0 && kind==expected_kind && port==8090,101);
if(kind){
check(!__builtin_memcmp(frozen.address,"198.51.100.23:8090",18) && !gw_engine_realm_address_build_ipv4(&built,&frozen,port),102);
check(built.bytes[0]==198 && built.bytes[1]==51 && built.bytes[2]==100 && built.bytes[3]==23,103);
}else{
check(!__builtin_memcmp(frozen.address,"127.0.0.1:8090",15) && !gw_engine_realm_address_build(&built,&frozen,port),104);
check(built.bytes[0]==127 && built.bytes[1]==0 && built.bytes[2]==0 && built.bytes[3]==1,105);
}
++snapshot_checks;gw_wire_auth_data.cancel=1;
}
NTSTATUS WINAPI gw_wire_dispatch(struct gw_wire_call* call){
#if defined(GW_ACCOUNT_HASH_DISPATCH) && defined(GW_ACCOUNT_FORWARD_LOGIN_LIFECYCLE)
/* The full-DLL fixture forwards real login close and can stop discovery
* after genuine M2 without constructing a world or changing account.c. */
if(call->operation==GW_WIRE_DISCOVER || call->operation==GW_WIRE_LOGIN_CLOSE)
return GW_ACCOUNT_HASH_DISPATCH(call);
#endif
call->result=0;
switch(call->operation){
#ifdef GW_ACCOUNT_HASH_DISPATCH
case GW_WIRE_AUTHENTICATE_MEMORY_IPV4_HASH:
return GW_ACCOUNT_HASH_DISPATCH(call);
#endif
case GW_WIRE_AUTHENTICATE_MEMORY_IPV4:{
struct gw_native_auth_memory_ipv4_config config;
++explicit_calls;check(call->input && call->input_bytes==sizeof(config),70);__builtin_memcpy(&config,call->input,sizeof(config));
check(!call->auth.abi && !call->auth.auth_port && !call->auth.username && !call->auth.password_path && !call->auth.version_hash_path,71);
check(config.abi==GW_NATIVE_IPV4_ABI && config.size==sizeof(config) && !config.reserved && config.auth_port==GW_ACCOUNT_TEST_PORT &&
!__builtin_memcmp(config.auth_ipv4,fixture_expected_explicit_auth,10) && config.username && config.password && config.password_bytes==18,72);
borrowed_password=config.password;
check(gw_wire_auth_credentials_data.state==3 && all_zero(gw_wire_auth_credentials_data.username,100),73);
if(auth_failure){call->result=-2;break;}
call->login=11;break;
}
case GW_WIRE_AUTHENTICATE_MEMORY:
++memory_calls;borrowed_password=call->input;
check(call->auth.password_path==0 && call->auth.username && call->input && call->input_bytes && call->input_bytes<=64,31);
check(gw_wire_auth_credentials_data.state==3 && !gw_wire_auth_credentials_data.username_bytes && !gw_wire_auth_credentials_data.password_bytes &&
all_zero(gw_wire_auth_credentials_data.username,100),32);
if(auth_failure){call->result=-2;break;}
call->login=11;break;
case GW_WIRE_AUTHENTICATE:
++file_calls;check(call->auth.password_path && !call->input && !call->input_bytes,33);call->login=11;break;
#ifdef GW_ACCOUNT_IDLE_TEST
case GW_WIRE_CONFIRM_REALM_LIST:
check(call->login==11 && gw_wire_auth_data.server_proof_verified==1,121);
++chooser_refreshes;
if(chooser_test==refuse_refresh)call->result=-2;
if(chooser_test==cancel_refresh)gw_wire_auth_data.cancel=1;
break;
#endif
case GW_WIRE_DISCOVER:
check(!call->auth.username && !call->auth.password_path && !call->input && !call->input_bytes,34);
if(borrowed_password)check(all_zero(borrowed_password,64),35);
check(gw_wire_auth_data.server_proof_verified==1,36);break;
case GW_WIRE_REALM_COUNT:
#ifdef GW_ACCOUNT_SINGLETON_TEST
call->value=singleton_realm_count;
#else
call->value=1;
#endif
break;
case GW_WIRE_REALM_AT:{
const char address[]="127.0.0.1:8090",explicit_address[]="198.51.100.23:8090";call->realm=(struct gw_native_realm){0};
#ifdef GW_ACCOUNT_SINGLETON_TEST
const char name[]="Local realm";
check(call->index<singleton_realm_count,106);++realm_reads;
__builtin_memcpy(call->realm.name,name,sizeof(name));
if(gw_wire_account_endpoint_kind()==1)__builtin_memcpy(call->realm.address,explicit_address,sizeof(explicit_address));
else __builtin_memcpy(call->realm.address,address,sizeof(address));
if(singleton_bad_realm)call->realm.flags=2;
#else
++realm_reads;check(call->index==0 && realm_reads<=2,106);
if(realm_reads==2){
// Re-fetch returns the genuine core row even when a public copy
// is altered after discovery and the choice has been captured.
gw_wire_auth_data.selected_index=63;gw_wire_auth_realms[0].address[0]='9';
}
if(gw_wire_account_endpoint_kind()==1)__builtin_memcpy(call->realm.address,explicit_address,sizeof(explicit_address));
else __builtin_memcpy(call->realm.address,address,sizeof(address));
#endif
break;
}
case GW_WIRE_TRANSPORT_OPEN:check(call->index==0 && realm_reads==2,107);call->transport=12;gw_wire_auth_data.cancel=inspect_prepared?0:1;break;
case GW_WIRE_TRANSPORT_OPEN_IPV4:
++explicit_transports;
#ifdef GW_ACCOUNT_SINGLETON_TEST
check(gw_wire_account_endpoint_kind()==1 && call->index<singleton_realm_count && realm_reads==singleton_realm_count+1,74);
singleton_open_index=call->index;
#else
check(gw_wire_account_endpoint_kind()==1 && call->index==0 && realm_reads==2,74);
#endif
call->transport=12;gw_wire_auth_data.cancel=inspect_prepared?0:1;break;
case GW_WIRE_PORT:check(call->transport==12,108);call->value=8090;break;
case GW_WIRE_TRANSPORT_CLOSE:call->transport=0;break;
case GW_WIRE_LOGIN_CLOSE:call->login=0;break;
default:call->result=-1;break;
}
return 0;
}
static void configure(void){
const char version[]="synthetic-version",addons[]="synthetic-addons";
check(!gw_wire_account_active(),40);
gw_wire_auth_data=(struct gw_wire_auth_control){.abi=1,.size=sizeof(gw_wire_auth_data),.selected_index=-1,.auth_port=3724,.retention_ms=100};
gw_wire_endpoint_profile_data=(struct gw_wire_endpoint_profile){.abi=1,.size=sizeof(gw_wire_endpoint_profile_data)};
__builtin_memcpy(gw_wire_auth_data.version_hash_path,version,sizeof(version));
__builtin_memcpy(gw_wire_auth_data.addon_path,addons,sizeof(addons));
borrowed_password=0;wipe_calls=0;
inspect_prepared=snapshot_checks=realm_reads=0;
}
static void publish(unsigned int generation,unsigned int username_size,unsigned int password_size){
unsigned int i;struct gw_wire_auth_credentials* data=&gw_wire_auth_credentials_data;
data->abi=1;data->size=sizeof(*data);data->state=0;data->generation=generation;data->reserved=0;
for(i=0;i<sizeof(data->username);++i)data->username[i]=0;
for(i=0;i<sizeof(data->password);++i)data->password[i]=0;
for(i=0;i<username_size && i<32;++i)data->username[i]='A';
for(i=0;i<password_size && i<64;++i)data->password[i]='X';
data->username_bytes=username_size;data->password_bytes=password_size;
InterlockedExchange((LONG*)&data->state,1);
}
static void run_worker(void){LPTHREAD_START_ROUTINE run=pending;void* arg=pending_argument;check(run!=0,41);pending=0;pending_argument=0;run(arg);}
static void wiped(LONG state,unsigned int code){
check(gw_wire_auth_credentials_data.state==state && !gw_wire_auth_credentials_data.username_bytes && !gw_wire_auth_credentials_data.password_bytes &&
all_zero(gw_wire_auth_credentials_data.username,100),code);
}
static void set_explicit_profile(void){
const char address[]="192.0.2.7";gw_wire_endpoint_profile_data.kind=1;
__builtin_memcpy(gw_wire_endpoint_profile_data.auth_ipv4,address,sizeof(address));
}
static void endpoint_checks(void){
const char* accepted[]={"127.0.0.1","127.0.0.2","10.0.0.1","172.16.0.1","192.168.0.1","192.0.2.1","223.255.255.254"};
const char* rejected[]={"","localhost","play.example.test","127.1","2130706433","0x7f000001","127.000.0.1"," 127.0.0.1","127.0.0.1 ",
"+127.0.0.1","127.0.0.256","::1","[::1]","0.0.0.0","0.1.2.3","224.0.0.1","239.255.255.255","240.0.0.1","255.255.255.255"};
const char* rejected_realms[]={"127.0.0.1","127.0.0.1:","127.0.0.1:0","127.0.0.1:65536","127.0.0.1:-1","127.0.0.1:+1",
"127.0.0.1:8090x","127.0.0.1:8090:1","play.example.test:8090","::1:8090","255.255.255.255:8090"};
unsigned char parsed[4],expected[160]={0};unsigned int i;uint64_t inline_pointer;
struct gw_native_realm realm={0};struct gw_engine_realm_address built;
const char remote[]="198.51.100.23:8090",hostname[]="play.example.test:8090";
for(i=0;i<sizeof(accepted)/sizeof(*accepted);++i)check(!gw_engine_numeric_ipv4_parse(accepted[i],16,parsed),80);
for(i=0;i<sizeof(rejected)/sizeof(*rejected);++i)check(gw_engine_numeric_ipv4_parse(rejected[i],16,parsed)==-1,81);
check(gw_engine_numeric_ipv4_parse("127.0.0.1",8,parsed)==-1,82);
__builtin_memcpy(realm.address,remote,sizeof(remote));
check(gw_engine_realm_address_build(&built,&realm,8090)==-1 && !gw_engine_realm_address_build_ipv4(&built,&realm,8090),83);
expected[0]=198;expected[1]=51;expected[2]=100;expected[3]=23;expected[0x10]=1;expected[0x18]=0x9a;expected[0x19]=0x1f;
inline_pointer=(uint64_t)(built.bytes+0x60);__builtin_memcpy(expected+0x48,&inline_pointer,8);expected[0x58]=15;expected[0x5f]=0x80;
check(!__builtin_memcmp(expected,built.bytes,sizeof(expected)),84);
check(gw_engine_realm_address_build_ipv4(&built,&realm,8091)==-1,85);
realm.flags=2;check(gw_engine_realm_address_build_ipv4(&built,&realm,8090)==-1,86);
realm.flags=0;__builtin_memset(realm.address,0,sizeof(realm.address));__builtin_memcpy(realm.address,hostname,sizeof(hostname));
check(gw_engine_realm_address_build_ipv4(&built,&realm,8090)==-1,87);
for(i=0;i<sizeof(rejected_realms)/sizeof(*rejected_realms);++i){
unsigned int n=0;__builtin_memset(realm.address,0,sizeof(realm.address));
while(rejected_realms[i][n]){realm.address[n]=rejected_realms[i][n];++n;}
check(gw_engine_realm_address_build_ipv4(&built,&realm,8090)==-1,98);
}
__builtin_memset(realm.address,'1',sizeof(realm.address));check(gw_engine_realm_address_build_ipv4(&built,&realm,8090)==-1,99);
}
void fixture_start(void){
unsigned int before,i;const char file_username[]="SYNTHETIC",file_path[]="synthetic-password";
message("GW_MEMORY_FIXTURE entry\n");
_Static_assert(sizeof(struct gw_wire_auth_control)==1732,"Existing account ABI changed");
_Static_assert(sizeof(struct gw_wire_auth_credentials)==132,"Credential ABI changed");
_Static_assert(sizeof(struct gw_wire_endpoint_profile)==32,"Endpoint profile ABI changed");
endpoint_checks();
configure();publish(1,14,18);check(!gw_wire_account_start(),1);
check(gw_wire_auth_credentials_data.state==2 && gw_wire_account_start()==-1,2); /* Active submission is immutable. */
check(gw_wire_auth_credentials_data.password[0]=='X',3);run_worker();wiped(3,4);
check(memory_calls==1 && !file_calls && wipe_calls && gw_wire_auth_credentials_data.consumed_generation==1 &&
gw_wire_auth_data.state==4 && gw_wire_auth_data.server_proof_verified==1,5);
configure();publish(2,14,18);auth_failure=1;check(!gw_wire_account_start(),6);run_worker();auth_failure=0;wiped(3,7);
check(wipe_calls && gw_wire_auth_data.state==3 && !gw_wire_auth_data.server_proof_verified && gw_wire_auth_data.auth_result==-2,8);
configure();publish(3,14,18);before=memory_calls;check(!gw_wire_account_start(),9);gw_wire_auth_data.cancel=1;run_worker();wiped(3,10);
check(memory_calls==before && gw_wire_auth_data.state==4 && !gw_wire_auth_data.server_proof_verified,11);
configure();publish(4,14,18);thread_failure=1;check(gw_wire_account_start()==-2,12);thread_failure=0;wiped(-1,13);
check(!gw_wire_account_active() && !gw_wire_auth_data.server_proof_verified,14);
configure();publish(5,14,18);__builtin_memcpy(gw_wire_auth_data.password_path,file_path,sizeof(file_path));
check(gw_wire_account_start()==-1,15);wiped(-1,16);
for(i=0;i<13;++i){
configure();publish(i==0?0:i==1?1:5,14,18);
if(i==2)gw_wire_auth_credentials_data.password_bytes=65;
if(i==3)gw_wire_auth_credentials_data.password[0]=0;
if(i==4)gw_wire_auth_credentials_data.password[0]=128;
if(i==5)gw_wire_auth_credentials_data.username_bytes=33;
if(i==6)gw_wire_auth_credentials_data.username[0]=0;
if(i==7)gw_wire_auth_credentials_data.reserved=1;
if(i==8)gw_wire_auth_credentials_data.password_bytes=0;
if(i==9)gw_wire_auth_credentials_data.username_bytes=0;
if(i==10)gw_wire_auth_credentials_data.username[14]='A';
if(i==11)gw_wire_auth_credentials_data.abi=99;
if(i==12)gw_wire_auth_credentials_data.size=0;
check(gw_wire_account_start()==-1,50+i);wiped(-1,25);
}
configure();publish(5,32,64);check(!gw_wire_account_start(),26);run_worker();wiped(3,27);
check(wipe_calls && gw_wire_auth_credentials_data.consumed_generation==5,28);
configure();publish(6,14,18);before=memory_calls;check(!gw_wire_account_start(),63);
gw_wire_auth_credentials_data.password_bytes=65;run_worker();wiped(-1,64);
check(memory_calls==before && gw_wire_auth_data.state==3 && !gw_wire_auth_data.server_proof_verified,65);
configure();__builtin_memcpy(gw_wire_auth_data.username,file_username,sizeof(file_username));
__builtin_memcpy(gw_wire_auth_data.password_path,file_path,sizeof(file_path));before=file_calls;
inspect_prepared=1;check(!gw_wire_account_start(),29);run_worker();check(file_calls==before+1 && gw_wire_auth_data.server_proof_verified==1 && snapshot_checks==1,30);
configure();publish(7,14,18);set_explicit_profile();inspect_prepared=1;check(!gw_wire_account_start(),88);
// Mutable producer input cannot redirect the already scheduled session.
gw_wire_endpoint_profile_data=(struct gw_wire_endpoint_profile){.abi=1,.size=sizeof(gw_wire_endpoint_profile_data)};
run_worker();wiped(3,89);check(explicit_calls==1 && explicit_transports==1 && gw_wire_auth_data.server_proof_verified==1 && snapshot_checks==1,90);
for(i=0;i<5;++i){
configure();publish(8,14,18);set_explicit_profile();
if(i==0)gw_wire_endpoint_profile_data.kind=2;
if(i==1)gw_wire_endpoint_profile_data.auth_ipv4[0]='0';
if(i==2)gw_wire_endpoint_profile_data.reserved=1;
if(i==3)gw_wire_endpoint_profile_data.abi=2;
if(i==4)gw_wire_endpoint_profile_data.kind=0; /* Nonempty address is not a local default. */
check(gw_wire_account_start()==-1,91+i);wiped(-1,96);
}
configure();set_explicit_profile();__builtin_memcpy(gw_wire_auth_data.username,file_username,sizeof(file_username));
__builtin_memcpy(gw_wire_auth_data.password_path,file_path,sizeof(file_path));
check(gw_wire_account_start()==-1,97); /* Explicit profile never silently consumes a file password. */
configure();publish(8,14,18);set_explicit_profile();auth_failure=1;before=explicit_calls;
check(!gw_wire_account_start(),109);run_worker();auth_failure=0;wiped(3,110);
check(explicit_calls==before+1 && wipe_calls && gw_wire_auth_data.state==3 && !gw_wire_auth_data.server_proof_verified && gw_wire_auth_data.auth_result==-2,111);
configure();publish(9,14,18);set_explicit_profile();before=explicit_calls;check(!gw_wire_account_start(),112);
gw_wire_auth_data.cancel=1;run_worker();wiped(3,113);check(explicit_calls==before && gw_wire_auth_data.state==4 && !gw_wire_auth_data.server_proof_verified,114);
configure();publish(10,14,18);set_explicit_profile();thread_failure=1;check(gw_wire_account_start()==-2,115);thread_failure=0;
wiped(-1,116);check(!gw_wire_account_active() && !gw_wire_auth_data.server_proof_verified && explicit_calls==before,117);
message("GW_MEMORY_FIXTURE complete\n");
ExitProcess(0);
}