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

145 lines
11 KiB
Python

#!/usr/bin/env python3
"""Actual optional-barrier entry/hold/drain functions, socket-free fixtures."""
import os
from pathlib import Path
import subprocess
import unittest
ROOT = Path(__file__).resolve().parents[1]
class FailOpenTest(unittest.TestCase):
def test_disabled_and_readiness_failure_preserve_genuine_fifo(self):
source = (ROOT / "native/wine_pipe_host.c").read_text()
cancel = source[source.index("static void spell_barrier_cancel("):source.index("static void spell_barrier_reset(")]
fail = source[source.index("static int spell_barrier_hard_fail("):source.index("static int spell_barrier_identity(")]
drain = source[source.index("static int spell_barrier_flush("):source.index("static void spell_barrier_poll(")]
validators = source[source.index("static int spell_barrier_initial_ids_result("):source.index("static int spell_barrier_begin(")]
begin_hold = source[source.index("static int spell_barrier_begin("):source.index("static int spell_barrier_classify(")]
out = Path(os.environ.get("GW_TEST_OUTPUT_DIR", ROOT / "build/test-world-locator-fail-open")).resolve()
out.mkdir(parents=True, exist_ok=True)
unit = out / "world-locator-fail-open.c"
unit.write_text(r'''
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "wine_pipe_host.h"
#include "world_locator_fifo.h"
typedef uint32_t DWORD;
typedef int32_t NTSTATUS;
struct gw_wire_call {uint32_t operation,capacity,required,input_bytes,consumed;int32_t result;const uint8_t* input;unsigned char* output;};
#define GW_WIRE_INITIAL_SPELL_IDS 45
#define GW_SOURCE_ONLY_PROFILE 5
#define MemoryBarrier() ((void)0)
static struct gw_pipe_spell_locator_barrier gw_wire_spell_locator_barrier;
static struct gw_pipe_initial_spell_ids_call gw_wire_initial_spell_ids_call;
static struct gw_pipe_world_control gw_wire_world_data;
static struct gw_pipe_world_failure gw_wire_world_failure;
static struct gw_world_locator_fifo spell_barrier_fifo;
static uint32_t pipe_generation=1,spell_barrier_bypassed,spell_barrier_missing_count,spell_barrier_missing_ids[32];
static uint64_t spell_barrier_owner,spell_barrier_connection,spell_barrier_pipe;
static struct gw_pipe_control gw_wire_pipe_data;
static unsigned char spell_barrier_ids[12+4096*4],converted[64];
static uint32_t spell_barrier_known[4096];
static const char *option;
static uint64_t source_guid;
static int core_result,retire_after_copy;
static int identity,main_ok,main_calls,core_calls,allocation_fail,submit_failure;
static uint32_t submitted,ops[5000],values[5000],freed,missing_logged,reason_logged;
static uint64_t now=100;
static int spell_barrier_fail_open(uint32_t);
static void* spell_barrier_alloc(void* context,size_t bytes){(void)context;return allocation_fail?0:malloc(bytes);}
static void spell_barrier_free(void* context,void* pointer){(void)context;++freed;free(pointer);}
static int spell_barrier_identity(void){return identity;}
static int spell_barrier_main_qualified(int first){(void)first;++main_calls;return main_ok;}
static uint64_t GetTickCount64(void){return now;}
static DWORD GetEnvironmentVariableA(const char* key,char* output,DWORD capacity){
assert(!strstr(key,"SOURCE_GUID"));const char* value=option;size_t n=value?strlen(value):0;
if(!value)return 0;
if(n>=capacity)return (DWORD)n+1;
memcpy(output,value,n+1);return (DWORD)n;
}
static void init(struct gw_wire_call* c,uint32_t op){memset(c,0,sizeof(*c));c->operation=op;}
static NTSTATUS gw_wire_account_transport(struct gw_wire_call* c){
++core_calls;assert(c->operation==45&&c->capacity==sizeof(spell_barrier_ids));
c->result=core_result;if(core_result)return 0;
uint32_t ids[3]={23925,45595,51602};memset(c->output,0,24);c->required=24;
for(unsigned i=0;i<8;++i)c->output[i]=(unsigned char)(source_guid>>(i*8));
c->output[8]=3;for(unsigned j=0;j<3;++j)for(unsigned i=0;i<4;++i)c->output[12+4*j+i]=(unsigned char)(ids[j]>>(i*8));
if(retire_after_copy)identity=0;
return 0;
}
static void initial_spell_trace_note(const unsigned char* b,uint32_t n){(void)b;(void)n;}
static void pipe_note(const char* prefix,uint32_t value){if(strstr(prefix,"missing-observed-id"))++missing_logged;if(strstr(prefix,"fail-open reason"))reason_logged=value;}
static uint32_t u32(const unsigned char* p){return gw_world_locator_u32(p);}
static int transfer_presentation(uint32_t opcode){(void)opcode;return 0;}
static int submit(uint32_t opcode,const unsigned char* bytes,uint32_t size){
if(submit_failure&&submitted+1u==(uint32_t)submit_failure)return -1;
assert(submitted<5000&&size==4);ops[submitted]=opcode;values[submitted]=u32(bytes);++submitted;return 0;
}
static void world_trace(uint32_t direction,uint32_t opcode,uint32_t bytes){(void)direction;(void)opcode;(void)bytes;}
''' + validators + cancel + fail + drain + begin_hold + r'''
static void put(unsigned char* p,uint32_t value){for(unsigned i=0;i<4;++i)p[i]=(unsigned char)(value>>(8*i));}
static void bundle(uint32_t opcode,uint32_t value){put(converted,1);put(converted+4,opcode);put(converted+8,4);put(converted+12,value);}
static void append(uint32_t opcode,uint32_t value){bundle(opcode,value);assert(!gw_world_locator_fifo_append(&spell_barrier_fifo,converted,16,spell_barrier_alloc,0));}
static void reset(void){
gw_world_locator_fifo_clear(&spell_barrier_fifo,spell_barrier_free,0);
gw_wire_spell_locator_barrier=(struct gw_pipe_spell_locator_barrier){.abi=1,.size=sizeof(struct gw_pipe_spell_locator_barrier)};
memset(&gw_wire_initial_spell_ids_call,0,sizeof(gw_wire_initial_spell_ids_call));memset(&gw_wire_world_data,0,sizeof(gw_wire_world_data));memset(&gw_wire_world_failure,0,sizeof(gw_wire_world_failure));
option="1";source_guid=4509;core_result=retire_after_copy=0;identity=main_ok=1;main_calls=core_calls=allocation_fail=submit_failure=0;
spell_barrier_bypassed=spell_barrier_missing_count=submitted=freed=missing_logged=reason_logged=0;now=100;
}
static void holding(void){gw_wire_spell_locator_barrier.state=1;gw_wire_spell_locator_barrier.started_ms=now;gw_wire_spell_locator_barrier.generation=1;}
int main(void){
assert(GW_WORLD_LOCATOR_TIMEOUT_MS==30000u);
assert(!gw_world_locator_expired(100,30099)&&gw_world_locator_expired(100,30100));
reset();assert(spell_barrier_begin()==1&&main_calls==1&&core_calls==1&&gw_wire_spell_locator_barrier.state==1&&gw_wire_spell_locator_barrier.source_guid==4509&&gw_wire_spell_locator_barrier.known_count==3&&spell_barrier_known[2]==51602);
reset();option=0;assert(!spell_barrier_begin()&&!main_calls&&!core_calls&&!gw_wire_spell_locator_barrier.state);
reset();option="0";assert(!spell_barrier_begin()&&!main_calls&&!core_calls&&!gw_wire_spell_locator_barrier.state);
reset();source_guid=4526;assert(spell_barrier_begin()==1&&gw_wire_spell_locator_barrier.source_guid==4526);
reset();option="1";source_guid=123456;assert(spell_barrier_begin()==1&&gw_wire_spell_locator_barrier.source_guid==123456);
reset();option="2";assert(!spell_barrier_begin()&&!main_calls&&!core_calls&&spell_barrier_bypassed&&gw_wire_spell_locator_barrier.error==1);
reset();main_ok=0;assert(!spell_barrier_begin()&&main_calls==1&&!core_calls&&gw_wire_spell_locator_barrier.state==3&&gw_wire_spell_locator_barrier.error==3);
reset();core_result=-1;assert(!spell_barrier_begin()&&core_calls==1&&!gw_wire_spell_locator_barrier.source_guid&&gw_wire_spell_locator_barrier.error==4);
reset();source_guid=0;assert(!spell_barrier_begin()&&gw_wire_spell_locator_barrier.error==4&&!gw_wire_spell_locator_barrier.known_count);
reset();retire_after_copy=1;assert(spell_barrier_begin()==-1&&gw_wire_spell_locator_barrier.error==2);
reset();source_guid=4526;gw_wire_world_data.last_legacy=0x12a;bundle(0x690019,51602);
assert(spell_barrier_hold(1,16)==1&&core_calls==1&&!submitted&&spell_barrier_fifo.records==1);
bundle(0x69001a,2);assert(spell_barrier_hold(0,16)==1&&!submitted);
bundle(0x5e0000,3);assert(spell_barrier_hold(0,16)==1&&!submitted);
assert(!spell_barrier_flush()&&submitted==3&&values[0]==51602&&ops[0]==0x690019&&ops[1]==0x69001a&&ops[2]==0x5e0000);
assert(!spell_barrier_hold(1,16)&&core_calls==1&&submitted==3&&!spell_barrier_fifo.head); /* Genuine later worldport is never gated/replayed. */
reset();holding();append(0x690019,1);append(0x69001a,2);append(0x5e0000,3);
spell_barrier_missing_ids[0]=12727;spell_barrier_missing_ids[1]=45595;spell_barrier_missing_count=2;gw_wire_spell_locator_barrier.required_count=26;
assert(!spell_barrier_fail(7)&&submitted==3&&freed==3&&missing_logged==2&&reason_logged==7);
assert(ops[0]==0x690019&&ops[1]==0x69001a&&ops[2]==0x5e0000&&values[0]==1&&values[2]==3);
assert(spell_barrier_bypassed&&gw_wire_spell_locator_barrier.state==3&&gw_wire_spell_locator_barrier.error==7&&!gw_wire_world_failure.phase&&!spell_barrier_fifo.head);
bundle(0x5e0001,4);assert(!spell_barrier_hold(0,16)&&submitted==3&&!spell_barrier_fifo.head);
reset();holding();for(unsigned i=0;i<130;++i)append(0x690019,i);assert(!spell_barrier_flush()&&submitted==128&&spell_barrier_fifo.head);
assert(!spell_barrier_fail(8)&&submitted==130&&!spell_barrier_fifo.head);for(unsigned i=0;i<130;++i)assert(values[i]==i);
reset();holding();for(unsigned i=0;i<4096;++i)append(0x690019,i);bundle(0x5e0000,4096);
assert(!spell_barrier_hold(0,16)&&submitted==4096&&gw_wire_spell_locator_barrier.error==6&&!spell_barrier_fifo.head&&u32(converted+12)==4096);
for(unsigned i=0;i<4096;++i)assert(values[i]==i); /* Current unappended bundle remains for the original caller. */
reset();holding();append(0x690019,1);allocation_fail=1;bundle(0x5e0000,2);
assert(!spell_barrier_hold(0,16)&&submitted==1&&u32(converted+12)==2&&gw_wire_spell_locator_barrier.error==6);
reset();holding();append(0x690019,1);now+=29999;bundle(0x5e0000,2);
assert(spell_barrier_hold(0,16)==1&&!submitted);now+=1;bundle(0x5e0001,3);
assert(!spell_barrier_hold(0,16)&&submitted==2&&values[0]==1&&values[1]==2&&u32(converted+12)==3&&gw_wire_spell_locator_barrier.error==7&&!gw_wire_world_failure.phase);
reset();holding();append(0x690019,1);identity=0;assert(spell_barrier_fail(7)==-1&&!submitted&&gw_wire_spell_locator_barrier.error==2&&gw_wire_world_failure.phase==13);
reset();holding();append(0x690019,1);append(0x5e0000,2);submit_failure=2;
assert(spell_barrier_fail(7)==-1&&submitted==1&&gw_wire_spell_locator_barrier.error==9&&!spell_barrier_fifo.head);
reset();holding();append(0x690019,1);spell_barrier_fifo.head->cursor=99;
assert(spell_barrier_fail(7)==-1&&!submitted&&gw_wire_spell_locator_barrier.error==6);
reset();assert(!freed&&!spell_barrier_fifo.head);return 0;
}
''')
command = ["gcc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-fsanitize=address,undefined", "-fno-omit-frame-pointer", "-I", str(ROOT / "native"), str(unit), "-o", str(out / "world-locator-fail-open")]
with (out / "compile-and-run.private.log").open("w") as log:
subprocess.run(command, check=True, stdout=log, stderr=subprocess.STDOUT)
subprocess.run([str(out / "world-locator-fail-open")], check=True, stdout=log, stderr=subprocess.STDOUT)
if __name__ == "__main__":
unittest.main()