Files
WoW-Hybrid-gateway/tests/native_registration_exact_spellbook_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

89 lines
6.2 KiB
Python

"""One exact namespace/state/row boundary; synthetic bytes, no target operations."""
from pathlib import Path
from types import SimpleNamespace
from unittest.mock import patch
import struct,sys
ROOT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(ROOT/'ops'))
import native_registration_trace as t
Q=lambda n:struct.pack('<Q',n)
BASE=0x20000000;END=BASE+0x9000000;TABLE=0x50000000;NAMESPACE=TABLE+0x1000;STATE=0x60000000
NAME=BASE+t.TOOLTIP_NAME_RVA;GLOBAL=BASE+(t.LUA_GLOBAL-0x140000000);CALLBACK=BASE+0x2000
def fixture(namespace=t.TOOLTIP_NAMESPACE,names=(0xdead,NAME,0xbeef),state=STATE,callback=CALLBACK):
memory={(NAMESPACE,len(t.TOOLTIP_NAMESPACE)):namespace,(GLOBAL,8):Q(state)}
for i,name in enumerate(names):
memory[TABLE+16*i,8]=Q(name)
if name==NAME:memory[TABLE+16*i+8,8]=Q(callback)
reads=[]
def read(address,size):
reads.append((address,size));assert(address,size)in memory,'read escaped exact metadata cells'
return memory[address,size]
def readable(address,size):
return (address,size)in memory or(address==TABLE and size==16*len(names))or(address==STATE and size==1)
return memory,reads,read,readable
def protected_target_boundary():
owned='/fixture/owned/WowB.exe'
maps=(f'{BASE:x}-{END:x} r--p 00000000 00:00 1 {owned}\n'
f'{TABLE:x}-{TABLE+8192:x} rw-p 00000000 00:00 0\n'
f'{STATE:x}-{STATE+4096:x} rw-p 00000000 00:00 0')
memory,reads,read,readable=fixture()
probe=object.__new__(t.RegistrationTrace);probe.pid=4242;probe.owned_image_path=owned
probe.exact_stops=0;probe.exact_rows=0;probe.events=[];probe.exact_name_pointer=NAME
probe.exact_image_bounds=(BASE,END);probe.exact_lua_global=GLOBAL;statuses=[];probe.finish=statuses.append
with patch.object(t.Path,'read_text',return_value=maps),patch.object(t.os,'open',return_value=7),patch.object(t.os,'close'),patch.object(t.os,'pread',side_effect=lambda fd,n,a:read(a,n)):
probe.observe_exact_spellbook(SimpleNamespace(rcx=TABLE,rdx=3,r8=NAMESPACE))
assert statuses==['exact-spellbook-binding-observed-code-held']and len(probe.events)==1
event=probe.events[0]
assert event['callback']==hex(CALLBACK)and event['registration_lua_state']==hex(STATE)
assert event['callback_code_ready']is False and event['native_call_admitted']is False
assert event['code_page_read']is False and not any(CALLBACK<=a<CALLBACK+4096 for a,n in reads)
if '--protected-target-only'in sys.argv:
protected_target_boundary()
print('PASS observed namespace binding retained while protected code remains held; metadata only')
raise SystemExit(0)
memory,reads,read,readable=fixture()
r=t.exact_spellbook_callback(read,readable,TABLE,3,NAMESPACE,NAME,(BASE,END),GLOBAL)
assert r['status']=='exact-namespace-binding-candidate'and r['callback']==CALLBACK and r['lua_state']==STATE
assert reads==[(NAMESPACE,len(t.TOOLTIP_NAMESPACE)),(GLOBAL,8),(TABLE,8),(TABLE+16,8),(TABLE+32,8),(TABLE+24,8)]
assert not any(address==STATE for address,size in reads)
# Same literal and callback cells in another namespace must not be inspected.
memory,reads,read,readable=fixture(namespace=b'C_OtherInfo\0')
assert t.exact_spellbook_callback(read,readable,TABLE,3,NAMESPACE,NAME,(BASE,END),GLOBAL)['status']=='not-this-namespace'
assert reads==[(NAMESPACE,len(t.TOOLTIP_NAMESPACE))]
memory,reads,read,readable=fixture(state=0)
assert t.exact_spellbook_callback(read,readable,TABLE,3,NAMESPACE,NAME,(BASE,END),GLOBAL)['status']=='registration-state-held'
assert not any(TABLE<=a<TABLE+48 for a,n in reads)
memory,reads,read,readable=fixture(names=(NAME,NAME))
assert t.exact_spellbook_callback(read,readable,TABLE,2,NAMESPACE,NAME,(BASE,END),GLOBAL)['status']=='ambiguous-target-held'
assert(TABLE+8,8)not in reads and(TABLE+24,8)not in reads
for count in [0,-1,True,t.REGIONAL_MAX_ROWS+1]:
def denied(*args):raise AssertionError('invalid bound reached reader')
assert t.exact_spellbook_callback(denied,denied,TABLE,count,NAMESPACE,NAME,(BASE,END),GLOBAL)['status']=='row-count-held'
# The optional observer binds the current owned image/state, never a saved heap pointer.
owned='/fixture/owned/WowB.exe'
maps=(f'{BASE:x}-{BASE+4096:x} r--p 00000000 00:00 1 {owned}\n'
f'{BASE+4096:x}-{END:x} r-xp 00001000 00:00 1 {owned}\n'
f'{TABLE:x}-{TABLE+8192:x} rw-p 00000000 00:00 0\n'
f'{STATE:x}-{STATE+4096:x} rw-p 00000000 00:00 0')
dos=bytearray(64);dos[:2]=b'MZ';struct.pack_into('<I',dos,60,128)
nt=bytearray(84);nt[:4]=b'PE\0\0';struct.pack_into('<H',nt,24,0x20b);struct.pack_into('<I',nt,80,END-BASE)
memory,reads,read,readable=fixture();memory.update({(BASE,64):bytes(dos),(BASE+128,84):bytes(nt),(NAME,len(t.TOOLTIP_EXACT_NAME)):t.TOOLTIP_EXACT_NAME})
probe=object.__new__(t.RegistrationTrace);probe.pid=4242;probe.owned_image_path=owned;probe.exact_regional_global=False;probe.exact_spellbook_tooltip=True
probe.exact_stops=0;probe.exact_rows=0;probe.events=[];statuses=[];probe.finish=statuses.append
with patch.object(t.Path,'read_text',return_value=maps),patch.object(t.os,'open',return_value=7),patch.object(t.os,'close'),patch.object(t.os,'pread',side_effect=lambda fd,n,a:read(a,n)):
probe.exact_rebase();probe.observe_exact_spellbook(SimpleNamespace(rcx=TABLE,rdx=3,r8=NAMESPACE))
assert statuses==['exact-spellbook-namespace-binding-observed']and len(probe.events)==1
assert probe.events[0]['registration_lua_state']==hex(STATE)and not probe.events[0]['closure_layout_assumed']
assert probe.targets==(BASE+t.REGIONAL_REGISTER_RVA,0,0,0)
writes=[];probe.debug=lambda i,v=None:writes.append((i,v));probe.arm();assert writes[-1]==(7,1)
# A nonexecutable target remains useful binding metadata, with code/calls held.
probe.events=[];statuses.clear()
with patch.object(t.Path,'read_text',return_value=maps.replace('r-xp','r--p')),patch.object(t.os,'open',return_value=7),patch.object(t.os,'close'),patch.object(t.os,'pread',side_effect=lambda fd,n,a:read(a,n)):
probe.observe_exact_spellbook(SimpleNamespace(rcx=TABLE,rdx=3,r8=NAMESPACE))
assert len(probe.events)==1 and statuses==['exact-spellbook-binding-observed-code-held']
assert not probe.events[0]['callback_code_ready']and not probe.events[0]['native_call_admitted']
print('PASS exact namespace before rows, current state, one callback, executable binding and refusals; no native operations')