"""Synthetic dehydrated BLASes for tools/bvh_packing_check.py: a clean two-level tree passes, and each broken invariant is reported under its own reason. Also scans a small fake archive with a split stream. No game data. python -I tests/test_bvh_packing_check.py """ import contextlib import io import json import os import struct import sys import tempfile import unittest import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(ROOT, "tools")) import bvh_packing_check as C # noqa: E402 import bvh_rehydrate as R # noqa: E402 FLAGS = 0x80000000 | R.FLAG_DEHYDRATED | 0x00020006 ELEMENTS, NODES = 0x80, 0x180 # the console layout for four leaves and three BOX16 units (ยง3.1) BOUNDS = (0.0, 0.0, 0.0, 4.0, 2.0, 1.0) GOOD_ORDER = 0x09 def ptr(offset, kind): return (offset >> 6) << 3 | kind def leaf(i): return ptr(ELEMENTS + 64 * i, R.TRI0) def group(nodes, wide=False): """nodes: [(children, {bit: value})] -> group bytes (node words, child pointers, stored values).""" words, kids, values = [], [], [] for children, overrides in nodes: mask = 0 for bit in sorted(overrides): mask |= 1 << bit values.append(overrides[bit]) words.append(len(children) << 24 | mask) kids += children body = struct.pack("<%dI" % len(words), *words) + struct.pack("<%dI" % len(kids), *kids) if wide: return body + struct.pack("<%df" % len(values), *values) return body + struct.pack("<%dH" % len(values), *(R.float_to_half(v) for v in values)) def build(level0=None, level1=None, orders=None, n_leaves=4, flags=FLAGS, root=None, total=0x300, table=None, elements=ELEMENTS, nodes=NODES, parents=0x40): """Four leaves; level 0: two BOX16 nodes over leaves (0, 1) and (2, 3); level 1: the BOX16 root over both.""" a, b = ptr(nodes, R.BOX16), ptr(nodes + 64, R.BOX16) root = ptr(nodes + 128, R.BOX16) if root is None else root lf = [ptr(elements + 64 * i, R.TRI0) for i in range(4)] level0 = [([lf[0], lf[1]], {}), ([lf[2], lf[3]], {})] if level0 is None else level0 level1 = [([a, b], {})] if level1 is None else level1 src = bytearray(total) src[0:64] = struct.pack("<4I6f6I", flags, total // 64 - 1, 0, n_leaves, *BOUNDS, nodes, 0, root, elements, parents, R.NO_TRANSFORM) orders = [GOOD_ORDER] * n_leaves if orders is None else orders chunk = bytes(3 * n_leaves) + bytes([1] * n_leaves) + bytes(3 * n_leaves) + b"\xff" * (3 * n_leaves) + bytes(orders) src[elements:elements + len(chunk)] = chunk g0, g1 = group(level0), group(level1) src[nodes:nodes + len(g0)] = g0 src[nodes + 128:nodes + 128 + len(g1)] = g1 table = [(0, 128), (128, 192)] if table is None else table lv = b"".join(struct.pack("<2I", x, y) for x, y in table) + struct.pack(" 0x408 bytes of leaf entries, 0x400 of them below the header end want, first = C.parent_table(0x1000, 0x5080) self.assertEqual(want, 0xFFFFFC40) self.assertEqual(first, 0x48) def test_small_element_region_keeps_0x40(self): # fewer than 32 element units: no whole 128-byte block of leaf entries to drop self.assertEqual(C.parent_table(0x80, 0x100), (0x40, 0x48)) self.assertEqual(C.parent_table(0x80, 0x800), (0x40, 0xB8)) # 32 element units fill one 128-byte block, which goes below the header end self.assertEqual(C.parent_table(0x80, 0x880), (0xFFFFFFC0, 0x40)) self.assertEqual(C.parent_table(0x80, 0x900), (0xFFFFFFC0, 0x48)) def test_elements_after_parents(self): self.assertEqual(C.elements_after_parents(0x50, 3), 0x80) self.assertEqual(C.elements_after_parents(0x40, 16), 0x80) self.assertEqual(C.elements_after_parents(0x40, 17), 0x100) self.assertEqual(C.elements_after_parents(0x48, 0), 0x80) class TestBroken(unittest.TestCase): def test_not_dehydrated(self): self.assertEqual(fails(build(flags=FLAGS & ~R.FLAG_DEHYDRATED)), {"not_dehydrated": 1}) def test_short_input(self): self.assertEqual(fails(build()[:0x200]), {"short_input": 1}) def test_shared_and_unreferenced_leaf(self): f = fails(build(level0=[([leaf(0), leaf(1)], {}), ([leaf(1), leaf(3)], {})])) self.assertEqual(f, {"leaf_shared": 1, "leaf_unreferenced": 1}) def test_child_not_a_leaf(self): f = fails(build(level0=[([leaf(0), leaf(1)], {}), ([leaf(2), leaf(9)], {})])) self.assertIn("child_not_a_leaf", f) def test_child_wrong_width(self): f = fails(build(level1=[([ptr(NODES, R.BOX32), ptr(NODES + 64, R.BOX16)], {})])) self.assertIn("child_wrong_width", f) self.assertIn("node_unreferenced", f) def test_child_not_below(self): # a level-0 node pointing at its sibling on the same level f = fails(build(level0=[([leaf(0), leaf(1), ptr(NODES + 64, R.BOX16)], {}), ([leaf(2), leaf(3)], {})])) self.assertIn("child_not_below", f) self.assertIn("node_shared", f) def test_root_referenced(self): f = fails(build(level0=[([leaf(0), leaf(1), ptr(NODES + 128, R.BOX16)], {}), ([leaf(2), leaf(3)], {})])) self.assertIn("root_referenced", f) def test_too_many_children(self): # a node word claiming five children (the fifth pointer is read from the group as well) level0 = [([leaf(0), leaf(1), leaf(2), leaf(3), ptr(NODES + 64, R.BOX16)], {}), ([], {})] self.assertIn("too_many_children", fails(build(level0=level0))) def test_child_not_a_node(self): f = fails(build(level1=[([ptr(NODES, R.BOX16), ptr(NODES + 192, R.BOX16)], {})])) self.assertIn("child_not_a_node", f) self.assertNotIn("child_wrong_width", f) def test_invalid_child(self): f = fails(build(level0=[([leaf(0), leaf(1)], {}), ([leaf(2), R.INVALID], {})])) self.assertIn("invalid_child", f) self.assertIn("leaf_unreferenced", f) def test_root_not_a_box(self): # a TRI0 root over four leaves is not the single-leaf form self.assertEqual(fails(build(root=leaf(0))), {"root_not_a_box": 1}) def test_leaves_past_end(self): self.assertEqual(fails(build(n_leaves=20, orders=[GOOD_ORDER] * 20)), {"leaves_past_end": 1}) def test_other_child_type(self): f = fails(build(level0=[([leaf(0), leaf(1) | 6], {}), ([leaf(2), leaf(3)], {})])) self.assertIn("child_type_6", f) def test_no_children(self): self.assertIn("no_children", fails(build(level0=[([], {}), ([leaf(2), leaf(3)], {})], n_leaves=4))) def test_mask_past_children(self): f = fails(build(level0=[([leaf(0), leaf(1)], {12: 0.5}), ([leaf(2), leaf(3)], {})])) self.assertEqual(f, {"mask_past_children": 1}) def test_override_outside_bounds(self): f = fails(build(level0=[([leaf(0), leaf(1)], {3: 9.0}), ([leaf(2), leaf(3)], {})])) self.assertEqual(f, {"override_outside_bounds": 1}) def test_infinite_box16_override_inside_half_range(self): f = fails(build(level0=[([leaf(0), leaf(1)], {3: float("inf")}), ([leaf(2), leaf(3)], {})])) self.assertEqual(f, {"override_outside_bounds": 1}) def test_root_union_not_bounds(self): src = build(level1=[([ptr(NODES, R.BOX16), ptr(NODES + 64, R.BOX16)], {3: 2.0, 6 + 3: 3.0})]) self.assertEqual(fails(src), {"root_union_not_bounds": 1}) def test_bad_vertex_order(self): # 0x05: first = 1, second = 1 -> two vertices in one slot self.assertEqual(fails(build(orders=[GOOD_ORDER, 0x05, GOOD_ORDER, 0x05])), {"leaf_vertex_order": 2}) def test_level_table_out_of_order(self): self.assertEqual(fails(build(table=[(0, 100), (128, 192)])), {"level_table": 1}) def test_levels_past_table(self): self.assertEqual(fails(build(table=[(0, 128), (128, 0x300)])), {"levels_past_table": 1}) def test_group_overflow(self): # a single BOX16 node (64 bytes) cannot hold a word, four children and 24 halves (4 + 16 + 48 bytes) many = {bit: 0.5 for bit in range(24)} level0 = [([leaf(0), leaf(1), leaf(2), leaf(3)], many)] src = build(level0=level0, level1=[([ptr(NODES, R.BOX16)], {})], table=[(0, 64), (64, 128)], root=ptr(NODES + 64, R.BOX16)) self.assertIn("group_overflow", fails(src)) def test_root_not_a_node(self): self.assertEqual(fails(build(root=ptr(NODES + 192, R.BOX16))), {"root_not_a_node": 1}) def test_parent_offset(self): self.assertEqual(fails(build(parents=0x80)), {"parent_offset": 1}) def test_parent_area_nonzero(self): src = bytearray(build()) src[0x60] = 1 self.assertEqual(fails(bytes(src)), {"parent_area_nonzero": 1}) def test_parent_extent(self): # a whole unused 128-byte block between the three box units' entries and the elements self.assertEqual(fails(build(elements=0x100, nodes=0x200)), {"parent_extent": 1}) def test_root_leaf(self): src = build(n_leaves=1, root=leaf(0), level0=[], level1=[], table=[]) res = C.check_stream(src) self.assertEqual(dict(res.fails), {}) self.assertEqual(res.stats["root_leaf"], 1) def test_root_union_and_short_total_edge_cases(self): # _root_equal must reject NaN (even NaN vs NaN in BOX16) and non-finite BOX32 bounds. self.assertFalse(C._root_equal(float("nan"), float("nan"), 0, False)) self.assertFalse(C._root_equal(1.0, float("inf"), 0, True)) self.assertFalse(C._root_equal(float("inf"), 1.0, 3, True)) # A root with a valid override on child 0 and NaN on child 1 must fail root_union_not_bounds # (Python min([0.0, nan]) would otherwise silently return 0.0). level1_nan = [([ptr(NODES, R.BOX16), ptr(NODES + 64, R.BOX16)], {0: 0.0, 6: float("nan")})] res_nan = C.check_stream(build(level1=level1_nan)) self.assertIn("root_union_not_bounds", res_nan.fails) # Negative header offset raises ValueError. with self.assertRaises(ValueError): C.check_stream(build(), -4) def payload(structure): """A whole bvh4 payload: the 0x100 prefix with the wrapper words, then the structure.""" end = 0x100 + len(structure) prefix = bytearray(0x100) struct.pack_into("" + bytes(64), bad, big[:0x10000], big[0x10000:]]) for workers, window in ((1, 1 << 20), (2, 64)): tally, _ = C.scan_archive(path, workers, window, progress=False) rep = tally.report() self.assertEqual((rep["checked"], rep["passed"]), (3, 2), (workers, window)) self.assertEqual(rep["fail_streams"], {"leaf_vertex_order": 1}) self.assertEqual(rep["kinds"].get("bvh4-chunk-joined"), 1) self.assertEqual(rep["kinds"].get("continuation"), 1) self.assertNotIn("raw", rep["kinds"]) def test_split_stream_skips_false_zlib_header_and_failed_join_does_not_steal_streams(self): good = payload(build()) big = payload(build(total=0x20000)) # False-positive 0x78 0x9c padding bytes between chunks must not abort continuation joining. body = zlib.compress(big[:0x10000]) + b"\x78\x9c\xff\xff\x00\x00" + zlib.compress(big[0x10000:]) + bytes(16) fd, path_fp = tempfile.mkstemp(suffix=".rpf") with os.fdopen(fd, "wb") as f: f.write(b"RPF9" + struct.pack("" + bytes(0x20000)]) tally_xml, _ = C.scan_archive(path_xml, 1, 1 << 20, progress=False) rep_xml = tally_xml.report() self.assertEqual((rep_xml["checked"], rep_xml["passed"]), (1, 0)) self.assertEqual(rep_xml["fail_streams"], {"short_input": 1}) self.assertEqual(rep_xml["kinds"].get("xml"), 1) self.assertNotIn("continuation", rep_xml["kinds"]) def test_stream_starting_at_window_boundary_is_not_dropped(self): good_z = zlib.compress(payload(build())) bad_z = zlib.compress(payload(build(orders=[GOOD_ORDER, 0x05, GOOD_ORDER, GOOD_ORDER]))) # Header has data_start = 16; with window = 64, window 0 is [16, 80). # Place good_z at offset 79 (end - 1) so its 2-byte zlib header straddles 79..80, # and place bad_z on an exact later window boundary to verify no double-counting. window = 64 pad_before_good = (16 + window - 1) - 16 after_good = 16 + pad_before_good + len(good_z) next_boundary = 16 + ((after_good - 16 + window - 1) // window) * window if next_boundary == after_good: next_boundary += window pad_before_bad = next_boundary - after_good raw = b"RPF9" + struct.pack("