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

93 lines
4.9 KiB
Python

"""The existing host publishes a qualified, read-only cross-module facade."""
import struct
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "ops"))
import engine_wire_host as wire_host
class Access:
slot = 0x1020
target = 0x2010
def __init__(self, version=1):
self.data = bytearray(struct.pack("<4I2Q", version, 32 if version == 1 else 40, 0, 0, 0, 0))
if version == 2: self.data.extend(bytes(8))
self.writes = []
self.executable = True
self.select_executable = True
def allowed(self, address, size, permission):
if permission == "x":
return (self.executable and (address, size) == (self.target, 1)) or (self.select_executable and (address, size) == (0x2020, 1))
return permission == "w" and (address, size) == (self.slot, len(self.data))
def read(self, address, size):
start = address - self.slot
return bytes(self.data[start:start + size])
def write(self, address, value):
start = address - self.slot
self.data[start:start + len(value)] = value
self.writes.append((address, bytes(value), bytes(self.data)))
class RealmSnapshotBindingTest(unittest.TestCase):
def bind(self, access, bridge=None, wire=None):
return wire_host.prepare_realm_snapshot(access, 0x1000,
{"exports": {"gw_registration_realm_binding": 0x20}} if bridge is None else bridge,
0x2000, {"exports": {"gw_wire_account_ui_snapshot": 0x10}} if wire is None else wire)
def test_publication_boundary_and_legacy_startup(self):
access = Access()
self.assertEqual(self.bind(access, bridge={}), {"available": False, "ready": False})
self.assertEqual(self.bind(access, wire={})["held"], "snapshot-export-unavailable")
self.assertEqual(access.writes, [])
self.assertTrue(self.bind(access)["ready"])
self.assertEqual(len(access.writes), 2)
pointers, publication = access.writes
self.assertEqual(pointers[0], Access.slot + 16)
self.assertEqual(struct.unpack_from("<I", pointers[2], 8)[0], 0)
self.assertEqual(struct.unpack_from("<2Q", pointers[2], 16), (0x2000, Access.target))
self.assertEqual(publication[:2], (Access.slot + 8, struct.pack("<I", 1)))
self.assertTrue(self.bind(access)["ready"])
self.assertEqual(len(access.writes), 2)
def test_changed_contract_is_held_before_any_write(self):
cases = ["nonexecutable", "abi", "size", "error", "ready", "partial", "different"]
for case in cases:
with self.subTest(case=case):
access = Access()
if case == "nonexecutable": access.executable = False
elif case == "abi": struct.pack_into("<I", access.data, 0, 2)
elif case == "size": struct.pack_into("<I", access.data, 4, 40)
elif case == "error": struct.pack_into("<I", access.data, 12, 1)
elif case == "ready": struct.pack_into("<I", access.data, 8, 2)
elif case == "partial": struct.pack_into("<Q", access.data, 16, 0x2000)
elif case == "different":
struct.pack_into("<I", access.data, 8, 1)
struct.pack_into("<2Q", access.data, 16, 0x3000, 0x3010)
with self.assertRaises(ValueError): self.bind(access)
self.assertEqual(access.writes, [])
def test_request_binding_is_complete_before_ready(self):
access = Access(2)
exports = {"exports": {"gw_wire_account_ui_snapshot": 0x10, "gw_wire_account_ui_select": 0x20}}
self.assertEqual(self.bind(access, wire=exports), {"available": True, "ready": True, "request_ready": True})
pointers, publication = access.writes
self.assertEqual(struct.unpack_from("<I", pointers[2], 8)[0], 0)
self.assertEqual(struct.unpack_from("<3Q", pointers[2], 16), (0x2000, 0x2010, 0x2020))
self.assertEqual(publication[:2], (Access.slot + 8, struct.pack("<I", 1)))
self.assertTrue(self.bind(access, wire=exports)["request_ready"])
self.assertEqual(len(access.writes), 2)
def test_missing_or_changed_request_target_never_publishes(self):
exports = {"exports": {"gw_wire_account_ui_snapshot": 0x10, "gw_wire_account_ui_select": 0x20}}
access = Access(2)
self.assertEqual(self.bind(access)["held"], "select-export-unavailable")
self.assertEqual(access.writes, [])
access.select_executable = False
with self.assertRaises(ValueError): self.bind(access, wire=exports)
self.assertEqual(access.writes, [])
access = Access(2)
struct.pack_into("<4I3Q", access.data, 0, 2, 40, 1, 0, 0x2000, 0x2010, 0x2030)
with self.assertRaises(ValueError): self.bind(access, wire=exports)
self.assertEqual(access.writes, [])
if __name__ == "__main__": unittest.main()