Files
WoW-Hybrid-gateway/tests/engine_registration_realm_request_test.py
2026-11-19 04:16:18 +02:00

106 lines
5.8 KiB
Python

"""Actual C Request facade: exact original primitive ABI is mocked, no client."""
from pathlib import Path
import subprocess
import tempfile
import unittest
ROOT=Path(__file__).resolve().parents[1]
FIXTURE=r'''
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#define __declspec(x)
#define GW_REGISTRATION_TEST 1
#include "engine_registration.c"
struct Cell {uint64_t bits;unsigned char tag,metadata,pad[6];uint64_t auxiliary;};
_Static_assert(sizeof(struct Cell)==24,"Observed TValue stride differs");
static struct Cell args[2],before[2];
static int lua,other,argc_value,slots,gates,selects,pushes,last_result,source_result,null_slot;
static int order[8],order_count;
static uint32_t selected_index,selected_generation;
static jmp_buf denied;
void gw_registration_test_create(void*L,int a,int h){(void)L;(void)a;(void)h;assert(0);}
void gw_registration_test_string(void*L,const char*b,size_t n){(void)L;(void)b;(void)n;assert(0);}
void gw_registration_test_set(void*L,int index){(void)L;(void)index;assert(0);}
const char*gw_registration_test_text(void*L,int index,size_t*length){(void)L;(void)index;(void)length;assert(0);return 0;}
void gw_registration_test_integer(void*L,int value){assert(L==&lua);++pushes;last_result=value;}
int gw_registration_test_top(void*L){assert(L==&lua);return argc_value;}
void*gw_registration_test_slot(void*L,int index){
assert(L==&lua&&index>=1&&index<=2);++slots;order[order_count++]=index;
return null_slot?0:&args[index-1];
}
void gw_registration_test_read_gate(void*L,void*slot,const void*context){
assert(L==&lua&&context==(const void*)(uintptr_t)0x1447ec640);
int index=slot==&args[0]?0:1;assert(slot==&args[index]);++gates;
order[order_count++]=10+index;
if(args[index].metadata)longjmp(denied,1);
}
static int32_t source_select(uint32_t index,uint32_t generation){
assert(gates==2);++selects;order[order_count++]=20;
selected_index=index;selected_generation=generation;return source_result;
}
static void number(int i,double value){union{double n;uint64_t b;}v;v.n=value;args[i].bits=v.b;args[i].tag=3;}
static void reset(void){
memset(args,0,sizeof(args));number(0,0);number(1,7);
args[0].auxiliary=0x11223344;args[1].auxiliary=0x55667788;
gw_registration_data.lua_state=(u64)(uintptr_t)&lua;
gw_registration_realm_binding=(struct RegistrationRealmBinding){2,40,1,0,0x1000,0x2000,(u64)(uintptr_t)source_select};
argc_value=2;slots=gates=selects=pushes=order_count=0;last_result=1234;source_result=0;null_slot=0;
}
static void request(int expected){
memcpy(before,args,sizeof(args));assert(gw_registration_request_realm(&lua)==1);
assert(pushes==1&&last_result==expected);assert(!memcmp(before,args,sizeof(args)));
}
int main(void){
reset();request(0);assert(selects==1&&selected_index==0&&selected_generation==7);
assert(order_count==5&&order[0]==1&&order[1]==10&&order[2]==2&&order[3]==11&&order[4]==20);
reset();number(0,63);number(1,2147483647.0);request(0);assert(selected_index==63&&selected_generation==2147483647u);
reset();source_result=-2;request(-2);assert(selects==1); /* selection disabled stays held */
reset();source_result=-5;request(-5);assert(selects==1); /* Source CAS status is not success */
for(int which=0;which<6;++which){
reset();if(which==0)gw_registration_realm_binding.ready=0;
if(which==1)gw_registration_realm_binding.error=1;
if(which==2)gw_registration_realm_binding.module=0;
if(which==3)gw_registration_realm_binding.snapshot=0;
if(which==4)gw_registration_realm_binding.select=0;
if(which==5){gw_registration_realm_binding.abi=1;gw_registration_realm_binding.size=32;}
request(REALM_SELECT_HELD);assert(!slots&&!gates&&!selects);
}
for(int count=0;count<4;++count)if(count!=2){reset();argc_value=count;request(-4005);assert(!slots&&!selects);}
double invalid_index[]={-1,64,0.5,1e30};
for(unsigned i=0;i<sizeof(invalid_index)/sizeof(*invalid_index);++i){reset();number(0,invalid_index[i]);request(-4005);assert(!selects);}
double invalid_generation[]={0,-1,7.5,2147483648.0};
for(unsigned i=0;i<sizeof(invalid_generation)/sizeof(*invalid_generation);++i){reset();number(1,invalid_generation[i]);request(-4005);assert(!selects);}
uint64_t nonfinite[]={0x7ff0000000000000ULL,0xfff0000000000000ULL,0x7ff8000000000001ULL};
for(unsigned i=0;i<3;++i){reset();args[0].bits=nonfinite[i];request(-4005);assert(!selects);}
reset();args[0].tag=4;request(-4005);assert(gates==1&&!selects); /* strings never coerce */
reset();null_slot=1;request(-4005);assert(slots==1&&!gates&&!selects);
reset();args[0].metadata=1;
if(!setjmp(denied)){gw_registration_request_realm(&lua);assert(0);}
assert(gates==1&&!selects&&!pushes); /* original gate cannot be bypassed */
reset();request(0);assert(selects==1); /* request holds no lock across original errors */
reset();assert(gw_registration_request_realm(&other)==0&&!slots&&!selects&&!pushes);
puts("PASS strict numeric request and original metadata delegation");
return 0;
}
'''
class RegistrationRealmRequest(unittest.TestCase):
def test_actual_C_strict_numeric_original_gate_and_source_proposal(self):
with tempfile.TemporaryDirectory()as temporary:
root=Path(temporary);source=root/'request.c';source.write_text(FIXTURE)
binary=root/'request'
built=subprocess.run(['cc','-std=c11','-Wall','-Wextra','-Werror',
'-Wno-missing-field-initializers','-I',str(ROOT/'native'),
str(source),'-o',str(binary)],capture_output=True,text=True)
self.assertEqual(built.returncode,0,built.stdout+built.stderr)
result=subprocess.run([str(binary)],capture_output=True,text=True)
self.assertEqual(result.returncode,0,result.stdout+result.stderr)
self.assertIn('PASS strict numeric request',result.stdout)
if __name__=='__main__':unittest.main()