"""Hand-built headings for tools/bvh_index.py. No game data is read. python -I tests/test_bvh_index.py """ import contextlib import io import json import os import struct import sys import tempfile import unittest from unittest import mock import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import bvh_index # noqa: E402 def bvh_payload(n=10, tag=bvh_index.TAG, end_delta=0, root=0x136AC): b = bytearray(0x140 + n * 64) struct.pack_into("x" def make_rpf(payloads): """An RPF9-shaped buffer: 4-byte magic, u32 data start at 4, then zlib streams separated by zeros.""" body = b"" for p in payloads: body += zlib.compress(p, 9) + b"\0" * 16 head = bytearray(0x40) head[0:4] = b"9FPR" struct.pack_into("= 0x30 bytes, a capped 208-byte instance record falls back to its 48-byte descriptor prefix cleanly. row = bvh_index.classify(instance_payload()[:128], 208) self.assertEqual(row["kind"], "descriptor") self.assertEqual(row["signature"], "desc:0x5000xxxx") def test_bvh4_chunk(self): b = bytearray(65536) struct.pack_into("\nx' row_enc = bvh_index.classify(with_enc, len(with_enc)) self.assertEqual(row_enc["kind"], "xml") self.assertFalse(row_enc["fragment"]) self.assertEqual(row_enc["root_element"], "CMapData") def test_xml_fragment(self): text = b"/americas/map/x_bvh.rpf" + b" " * 40 row = bvh_index.classify(text, len(text)) self.assertEqual(row["kind"], "xml") self.assertTrue(row["fragment"]) class ScanTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.dir = self.tmp.name def tearDown(self): self.tmp.cleanup() def test_two_streams_are_both_found(self): # The old carver advanced by the 1 MiB read size, so a small first stream hid the second one. payloads = [b"a" * 5000, bvh_payload(5)] path = os.path.join(self.dir, "two.rpf") with open(path, "wb") as f: f.write(make_rpf(payloads)) rows, summary = bvh_index.scan_rpf(path) self.assertEqual(summary[0]["streams"], 2) self.assertEqual([r["kind"] for r in rows], ["raw", "bvh4"]) self.assertEqual(rows[1]["node_count"], 5) self.assertEqual(summary[0]["inflated"], 5000 + 0x140 + 5 * 64) def test_fill_requires_all_bvh4_node_bytes(self): path = os.path.join(self.dir, "one.rpf") with open(path, "wb") as f: f.write(make_rpf([bvh_payload(1)])) rows, _ = bvh_index.scan_rpf(path, max_inflate=384) self.assertEqual(rows[0]["fill"], {"81": 64, "total": 64}) for cap in (320, 352): with self.subTest(cap=cap): rows, _ = bvh_index.scan_rpf(path, max_inflate=cap) self.assertEqual(rows[0]["kind"], "bvh4") self.assertEqual(rows[0]["node_bytes"], 64) self.assertNotIn("fill", rows[0]) def test_directory_with_an_unreadable_archive_keeps_the_others_but_exits_nonzero(self): good = os.path.join(self.dir, "good.rpf") with open(good, "wb") as f: f.write(make_rpf([bvh_payload(3)])) bad = bytearray(make_rpf([bvh_payload(2)])) struct.pack_into(" None: n = 1200 complete = bvh_payload(n) chunk = complete[:65536] for name, payload in (("complete.bin", complete), ("chunk.bin", chunk)): with open(os.path.join(self.dir, name), "wb") as f: f.write(payload) rows, _ = bvh_index.scan_dir(self.dir) by_kind = {row["kind"]: row for row in rows} self.assertEqual(by_kind["bvh4"]["fill"]["total"], n * 64) self.assertEqual(by_kind["bvh4-chunk"]["node_count"], n) self.assertNotIn("fill", by_kind["bvh4-chunk"]) archive = os.path.join(self.dir, "chunks.rpf") with open(archive, "wb") as f: f.write(make_rpf([chunk])) (archived,), _ = bvh_index.scan_rpf(archive) self.assertEqual(archived["kind"], "bvh4-chunk") self.assertNotIn("fill", archived) def test_folder_name_with_glob_characters_is_a_path(self): # glob read "[v1]" as a character class, so a folder named like this indexed nothing and exited 0. folder = os.path.join(self.dir, "carve [v1]") os.makedirs(folder) with open(os.path.join(folder, "a.bin"), "wb") as f: f.write(bvh_payload(4)) rows, summary = bvh_index.scan_dir(folder) self.assertEqual([r["kind"] for r in rows], ["bvh4"]) archives = os.path.join(self.dir, "pack [1]") os.makedirs(archives) with open(os.path.join(archives, "a.rpf"), "wb") as f: f.write(make_rpf([bvh_payload(2)])) rows, summary = bvh_index.scan_dir(archives) self.assertEqual([r["kind"] for r in rows], ["bvh4"]) def test_empty_and_short_archives_have_no_streams(self): for name, data in (("empty.rpf", b""), ("short.rpf", b"9FPR")): path = os.path.join(self.dir, name) with open(path, "wb") as f: f.write(data) rows, summary = bvh_index.scan_rpf(path) self.assertEqual(rows, []) self.assertEqual((summary[0]["streams"], summary[0]["span"]), (0, 0)) def test_start_outside_the_archive_is_refused(self): path = os.path.join(self.dir, "one.rpf") with open(path, "wb") as f: f.write(make_rpf([bvh_payload(2)])) with self.assertRaises(ValueError): bvh_index.scan_rpf(path, start=1 << 20) def test_directory_keeps_good_archives_next_to_a_bad_one(self): with open(os.path.join(self.dir, "a_good.rpf"), "wb") as f: f.write(make_rpf([bvh_payload(2)])) with open(os.path.join(self.dir, "b_bad.rpf"), "wb") as f: f.write(b"9FPR" + struct.pack("