"""Tests for tools/world_index.py, built from synthetic 0x7a headers (no game data). python -I tests/test_world_index.py """ import contextlib import csv import errno import importlib.util import io import json import os import struct import sys import tempfile import unittest import zlib from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import report_paths def load_tool(): path = os.path.join(ROOT, "tools", "world_index.py") spec = importlib.util.spec_from_file_location("world_index_under_test", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module WI = load_tool() PTR = 0x50000000 def ptr(off): return PTR | off def member(hash_, off, ty, sub=0, ref=0, extra=0): return struct.pack(".bin per stream plus index.json (off, stored).""" folder = os.path.join(self.dir, "carve") os.makedirs(folder) files, off = [], 0x1000 for part in parts: stored = len(zlib.compress(part)) with open(os.path.join(folder, f"{off:012x}.bin"), "wb") as f: f.write(part) files.append({"off": off, "stored": stored, "size": len(part)}) off = ((off + stored + 15) & ~15) + gap with open(os.path.join(folder, "index.json"), "w", encoding="utf-8", newline="\n") as f: json.dump({"summary": {}, "files": files}, f) return folder def test_carve_folder_joins_a_resource_split_over_two_files(self): """scan_rpf appends the next contiguous stream while a 0DRPz resource needs a continuation; a carve folder holds the same streams as files, and index.json says which ones were contiguous.""" blob = make_resource() rows, stats = WI.scan_path(self.carve([blob[:0x150], blob[0x150:], make_resource(map_file=False)])) maps = [r for r in rows if r["kind"] == "map"] self.assertEqual([(r["file"], r["size"]) for r in maps], [("000000001000.bin", len(blob))]) self.assertEqual(len(rows), 2) def test_carve_folder_does_not_join_streams_that_were_not_contiguous(self): blob = make_resource() rows, _ = WI.scan_path(self.carve([blob[:0x150], blob[0x150:]], gap=0x10)) self.assertEqual(rows, []) def test_carved_file_inflate_cap_matches_directory_scan(self): resource = make_resource() path = os.path.join(self.dir, "sample.bin") with open(path, "wb") as f: f.write(resource) for source in (path, self.dir): for cap, expected in ((len(resource) - 1, 0), (len(resource), 1)): with self.subTest(source=source, cap=cap): entities = [] rows, stats = WI.scan_path(source, max_inflate=cap, collect=entities) self.assertEqual(len(rows), expected) self.assertEqual(len(entities), expected) self.assertEqual(stats["files"], expected) self.assertEqual(stats["scanned_bytes"], expected * len(resource)) def test_directory_inflate_cap_does_not_exclude_large_archives(self): resource = make_resource() cap = len(resource) with open(os.path.join(self.dir, "large.rpf"), "wb") as f: f.write(make_rpf_streams([resource]) + bytes(cap)) with open(os.path.join(self.dir, "oversized.bin"), "wb") as f: f.write(resource + b"\0") rows, stats = WI.scan_path(self.dir, max_inflate=cap) self.assertEqual(len(rows), 1) self.assertEqual(rows[0]["kind"], "map") self.assertEqual(stats["files"], 1) def test_archetypes_out_counts_each_entity_once(self): blob = make_resource() path = os.path.join(self.dir, "sample.rpf") with open(path, "wb") as f: # One resource split over two streams (joined, parsed again) and one whole copy. f.write(make_rpf_streams([blob[:0x150], blob[0x150:], blob, make_resource(map_file=False)])) out = os.path.join(self.dir, "out.json") arch = os.path.join(self.dir, "archetypes.json") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(WI.main([path, "--out", out, "--archetypes-out", arch]), 0) with open(arch, encoding="utf-8") as f: doc = json.load(f) self.assertEqual(doc["archetypes"], {"deadbeef": 2}) self.assertEqual(doc["keys"], {"deadbeef": "1122334455667788"}) self.assertEqual(doc["summary"], {"entities": 2, "archetypes": 1, "with_key": 1, "key_conflicts": 0, "incomplete_entities": 0}) def test_conflicting_keys_leave_the_hash_unverified(self): agg = WI.ArchetypeCounts() agg.append({"complete": True, "archetype": 1, "archetype_key": 5}) agg.append({"complete": True, "archetype": 1, "archetype_key": 6}) agg.append({"complete": True, "archetype": 2, "archetype_key": 7}) agg.append({"complete": True, "archetype": None, "archetype_key": 8}) doc = agg.to_json() self.assertEqual(doc["archetypes"], {"00000001": 2, "00000002": 1}) self.assertEqual(doc["keys"], {"00000002": "0000000000000007"}) self.assertEqual(doc["conflicting_keys"], {"00000001": ["0000000000000005", "0000000000000006"]}) self.assertEqual(doc["summary"]["key_conflicts"], 1) def test_entity_past_the_inflated_bytes_is_not_aggregated(self): # The entity block starts at 0x400 (0x90 bytes); the bytes end at 0x448, just after archetypeName. agg = WI.ArchetypeCounts() row = WI.parse_resource(make_resource()[:0x448], agg) self.assertIsNotNone(row) self.assertEqual((agg.counts, agg.incomplete), ({}, 1)) def test_entity_block_starting_past_the_inflated_bytes_is_incomplete(self): # The entity block is moved to 0x700, after the bytes kept; its entities must still be reported. b = bytearray(make_resource()) + bytearray(0x90) struct.pack_into("