"""Unit tests for tools/recomp_census.py: the .pdata recompiler census. The PE fixture is built byte by byte — a minimal PE32+ image whose .pdata lists three functions over .text: one clean, one containing an unhandled (x87) insn, one exercising indirect control flow — so the test needs no game files and no third-party packages. python -I tests/test_recomp_census.py """ import os import struct import sys import tempfile import unittest ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) if ROOT not in sys.path: sys.path.insert(0, ROOT) TOOLS_DIR = os.path.join(ROOT, "tools") if TOOLS_DIR not in sys.path: sys.path.insert(0, TOOLS_DIR) import recomp_census as rc # noqa: E402 import pe_inspect # noqa: E402 TEXT_RVA = 0x1000 TEXT_RAW = 0x200 TEXT_SIZE = 0x2000 IMAGE_BASE = 0x140000000 def build_pe() -> bytes: """PE32+ with .text holding 3 functions and .pdata/.xdata describing them.""" opt_size = 240 buf = bytearray(TEXT_RAW + TEXT_SIZE) buf[0:2] = b"MZ" struct.pack_into(" None: at = TEXT_RAW + (rva - TEXT_RVA) buf[at:at + len(blob)] = blob # fn1 @ 0x1000: mov eax,5; add eax,3; ret (clean) place(0x1000, bytes.fromhex("b805000000" "83c003" "c3")) # fn2 @ 0x1010: mov eax,1; fldz (x87); ret (gap) place(0x1010, bytes.fromhex("b801000000" "d9e8" "c3")) # fn3 @ 0x1020: mov rax,target; jmp rax; ret (indirect) target = IMAGE_BASE + 0x102F place(0x1020, bytes.fromhex("48b8" + target.to_bytes(8, "little").hex() + "ffe0" "c3")) place(0x102F, bytes.fromhex("c3")) # .pdata: (begin, end, unwind_rva) entries = [(0x1000, 0x1009, 0x2100), (0x1010, 0x1018, 0x2104), (0x1020, 0x1030, 0x2108)] for i, (b, e, u) in enumerate(entries): place(0x2000 + i * 12, struct.pack("