Files
WoW-Hybrid-gateway/tests/ground_source_capture_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

95 lines
4.7 KiB
Python

#!/usr/bin/env python3
"""Exercise the actual opt-in Wire capture's scope and byte boundaries."""
import pathlib
import subprocess
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[1]
class GroundCapture(unittest.TestCase):
def test_filtered_owned_world_capture(self):
source = (ROOT / 'native/wine_pipe_host.c').read_text()
begin = source.index('static void ground_source_capture(')
end = source.index('\n#endif', begin)
function = source[begin:end]
number_begin = source.index('static char* pipe_note_number(')
number_end = source.index('\nstatic void pipe_note(', number_begin)
fixture = r'''
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
typedef uint32_t DWORD;typedef void* HANDLE;
#define GW_PRIVATE_TEST_GROUND_SOURCE_GUID 4528
#define MEM_RESERVE 1
#define MEM_COMMIT 2
#define MEM_RELEASE 4
#define PAGE_READWRITE 8
#define FILE_APPEND_DATA 1
#define FILE_SHARE_READ 2
#define FILE_SHARE_WRITE 4
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 1
#define INVALID_HANDLE_VALUE ((void*)(intptr_t)-1)
static struct {uint32_t world_auth,closed,error;} gw_wire_pipe_data;
static struct {uint32_t login_sent,self_create;} gw_wire_world_data;
static struct {uint64_t source_guid;} gw_wire_spell_locator_barrier;
static unsigned char incoming[65535];static uint64_t tick=100;
static int option=1,writes,short_write;static char output[132000];
static DWORD GetEnvironmentVariableA(const char* name,char* out,DWORD capacity){
const char* value=!strcmp(name,"GW_PRIVATE_GROUND_CAPTURE")?(option?"1":"0"):"owned-existing.log";
size_t n=strlen(value);if(n<capacity)memcpy(out,value,n+1);return (DWORD)n;
}
static uint64_t GetTickCount64(void){return tick;}
static void* VirtualAlloc(void* p,size_t size,unsigned a,unsigned b){(void)p;(void)a;(void)b;return malloc(size);}
static int VirtualFree(void* p,size_t size,unsigned flags){(void)size;(void)flags;free(p);return 1;}
static HANDLE CreateFileA(const char* p,unsigned a,unsigned b,void* c,unsigned d,unsigned e,void* f){
(void)a;(void)b;(void)c;(void)e;(void)f;assert(!strcmp(p,"owned-existing.log")&&d==OPEN_EXISTING);return (HANDLE)1;
}
static int WriteFile(HANDLE h,const char* p,DWORD size,DWORD* written,void* unused){
(void)h;(void)unused;assert(size<sizeof(output));memcpy(output,p,size);output[size]=0;++writes;*written=size-(short_write?1:0);return 1;
}
static int CloseHandle(HANDLE h){(void)h;return 1;}
'''
fixture += source[number_begin:number_end] + '\n' + function
fixture += r'''
int main(int argc,char** argv){
assert(argc==2);incoming[0]=1;incoming[1]=2;incoming[2]=255;
/* Neither unauthenticated nor partially created worlds may expose a body. */
ground_source_capture(0xa9,3);assert(!writes);
gw_wire_pipe_data.world_auth=1;ground_source_capture(0xa9,3);assert(!writes);
gw_wire_world_data.login_sent=gw_wire_world_data.self_create=1;
gw_wire_spell_locator_barrier.source_guid=4526;ground_source_capture(0xa9,3);assert(!writes);
gw_wire_spell_locator_barrier.source_guid=4528;option=0;ground_source_capture(0xa9,3);assert(!writes);option=1;
gw_wire_pipe_data.closed=1;ground_source_capture(0xa9,3);assert(!writes);gw_wire_pipe_data.closed=0;
gw_wire_pipe_data.error=1;ground_source_capture(0xa9,3);assert(!writes);gw_wire_pipe_data.error=0;
for(unsigned i=0;i<3;++i){uint32_t denied[]={0x1ee,0x96,0xffff};ground_source_capture(denied[i],3);}assert(!writes);
ground_source_capture(0xa9,0);ground_source_capture(0xa9,65536);assert(!writes);
if(!strcmp(argv[1],"short"))short_write=1;
ground_source_capture(0xa9,3);assert(writes==1);
assert(strstr(output,"opcode=169 bytes=3 guid=4528 arrival_ms=100 body=0102ff\n"));
if(short_write){ground_source_capture(0xa9,3);assert(writes==1);return 0;}
if(!strcmp(argv[1],"time")){tick=30101;ground_source_capture(0xa9,3);assert(writes==1);return 0;}
if(!strcmp(argv[1],"cap")){
for(unsigned i=0;i<200;++i)ground_source_capture(0x1f6,65535);
int stopped=writes;assert(stopped>1&&stopped<200);ground_source_capture(0xa9,3);assert(writes==stopped);return 0;
}
ground_source_capture(0x131,3);ground_source_capture(0x132,3);assert(writes==3);return 0;
}
'''
with tempfile.TemporaryDirectory() as directory:
p = pathlib.Path(directory)
(p / 'capture.c').write_text(fixture)
subprocess.run(['cc', '-std=c11', '-fsanitize=address,undefined',
'-fno-omit-frame-pointer', str(p / 'capture.c'),
'-o', str(p / 'capture')], check=True)
for case in ('normal', 'short', 'time', 'cap'):
subprocess.run([str(p / 'capture'), case], check=True)
if __name__ == '__main__':
unittest.main()