diff options
| author | Alpaca-exe <20233126@etud.univ-evry.fr> | 2026-11-23 02:35:53 +0100 |
|---|---|---|
| committer | Alpaca-exe <20233126@etud.univ-evry.fr> | 2026-11-23 02:35:53 +0100 |
| commit | 45cbda2549c7d1b104b500065f8f0e365f6bc8ee (patch) | |
| tree | 188f58a703b2fecbfd2aff85ae36d4b443529104 /tests/test_script_qtable.py | |
| parent | 03b09a086592a197db64d91655c6e65c30764d96 (diff) | |
script_qtable: decode 0x71717171 object-tree container in script.rpf
Diffstat (limited to 'tests/test_script_qtable.py')
| -rw-r--r-- | tests/test_script_qtable.py | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/tests/test_script_qtable.py b/tests/test_script_qtable.py new file mode 100644 index 000000000..fedfb643f --- /dev/null +++ b/tests/test_script_qtable.py | |||
| @@ -0,0 +1,182 @@ | |||
| 1 | """script_qtable.py must decode synthetic 0x71717171 object-tree containers without game data. | ||
| 2 | |||
| 3 | python -I tests/test_script_qtable.py | ||
| 4 | |||
| 5 | Every fixture is synthetic: 40-byte header + u64 offset table + 16-byte-aligned | ||
| 6 | struct, wrapper and array records linked by 0x71717171-tagged pointers. | ||
| 7 | """ | ||
| 8 | import json | ||
| 9 | import os | ||
| 10 | import struct | ||
| 11 | import subprocess | ||
| 12 | import sys | ||
| 13 | import tempfile | ||
| 14 | import unittest | ||
| 15 | |||
| 16 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
| 17 | TOOLS = os.path.join(ROOT, "tools") | ||
| 18 | TOOL = os.path.join(TOOLS, "script_qtable.py") | ||
| 19 | sys.path.insert(0, TOOLS) | ||
| 20 | |||
| 21 | import script_qtable # noqa: E402 | ||
| 22 | |||
| 23 | Q_PTR_HIGH = 0x71717171 << 32 | ||
| 24 | |||
| 25 | |||
| 26 | def _pad16(buf: bytes) -> bytes: | ||
| 27 | rem = len(buf) % 16 | ||
| 28 | return buf if rem == 0 else buf + b"\x00" * (16 - rem) | ||
| 29 | |||
| 30 | |||
| 31 | def build_synthetic_qtable(*, pad_to_bytes: int = 0) -> bytes: | ||
| 32 | """Build a valid 7-record, 2-tree synthetic object-tree payload.""" | ||
| 33 | # Preorder record layout for 7 records (odd count => zero gap after 0x28 + 7*8): | ||
| 34 | # Tree 0 (root 0, hash 0x11111111): | ||
| 35 | # [0] 32B wrapper struct (type 0xAAAA0001, flags 0xFFFF0000) -> ptr array [1] | ||
| 36 | # [1] pointer array (count=2, stride=8, flags=0x10000, elem=0xBBBB0001) -> [2], [3] (64B) | ||
| 37 | # [2] 48B struct (type 0xAAAA0002, leaf or 0 ptrs) | ||
| 38 | # [3] 64B struct (type 0xAAAA0003) | ||
| 39 | # Tree 1 (root 4, hash 0x22222222): | ||
| 40 | # [4] 48B struct (type 0xAAAA0004) -> [5] and [6] | ||
| 41 | # [5] value array (count=3, stride=12, flags=0, elem=0xBBBB0002) -> align16(48+36)=96B | ||
| 42 | # [6] 32B leaf struct (type 0xAAAA0005) | ||
| 43 | sizes = [32, 64, 48, 64, 48, 96, 32] | ||
| 44 | if pad_to_bytes: | ||
| 45 | # Enlarge last leaf value array? Better: add padding elements to record [5] so align16 stays exact. | ||
| 46 | extra_elems = max(0, (pad_to_bytes - 0x200) // 12) | ||
| 47 | cnt5 = 3 + extra_elems | ||
| 48 | sizes[5] = ((48 + cnt5 * 12) + 15) & ~15 | ||
| 49 | else: | ||
| 50 | cnt5 = 3 | ||
| 51 | |||
| 52 | table_end = 40 + len(sizes) * 8 # 96 = 0x60, 16-byte aligned because 7 is odd | ||
| 53 | offsets = [] | ||
| 54 | cur = table_end | ||
| 55 | for sz in sizes: | ||
| 56 | offsets.append(cur) | ||
| 57 | cur += sz | ||
| 58 | |||
| 59 | def qptr(target_idx: int) -> int: | ||
| 60 | return Q_PTR_HIGH | (offsets[target_idx] + 8) | ||
| 61 | |||
| 62 | recs = [ | ||
| 63 | # [0] 32B wrapper -> [1] | ||
| 64 | struct.pack("<4I2Q", 0x11111111, 0xFFFF0000, 0xAAAA0001, 0, qptr(1), 0), | ||
| 65 | # [1] ptr array count=2, stride=8, flags=0x10000 -> 48 + 16 = 64B | ||
| 66 | struct.pack("<12I2Q", 0, 0, 0x14, 0, 2, 2, 8, 0x10000, 0, 0, 0xBBBB0001, 0, qptr(2), qptr(3)), | ||
| 67 | # [2] 48B struct (leaf) | ||
| 68 | struct.pack("<4I", 0, 0, 0xAAAA0002, 0) + b"\x01" * 32, | ||
| 69 | # [3] 64B struct (leaf) | ||
| 70 | struct.pack("<4I", 0, 0, 0xAAAA0003, 0) + b"\x02" * 48, | ||
| 71 | # [4] 48B struct (root 0x22222222) -> qptr(5) at +0x10 and qptr(6) at +0x20 | ||
| 72 | struct.pack("<4IQ2Q", 0x22222222, 0, 0xAAAA0004, 0, qptr(5), 0, qptr(6)) + bytes(8), | ||
| 73 | # [5] value array (cnt5, stride=12, flags=0, elem=0xBBBB0002) | ||
| 74 | _pad16(struct.pack("<12I", 0, 0, 0x14, 0, cnt5, cnt5, 12, 0, 0, 0, 0xBBBB0002, 0) + b"\x03" * (cnt5 * 12)), | ||
| 75 | # [6] 32B leaf struct | ||
| 76 | struct.pack("<4I2Q", 0, 0, 0xAAAA0005, 0, 42, 99), | ||
| 77 | ] | ||
| 78 | for i, r in enumerate(recs): | ||
| 79 | assert len(r) == sizes[i], f"rec {i} len {len(r)} != {sizes[i]}" | ||
| 80 | |||
| 81 | header = struct.pack("<10I", 0, 0, 0, 0, 0, len(offsets), 0, 0, 0, 0) | ||
| 82 | offset_table = struct.pack(f"<{len(offsets)}Q", *offsets) | ||
| 83 | return header + offset_table + b"".join(recs) | ||
| 84 | |||
| 85 | |||
| 86 | class ScriptQtableTests(unittest.TestCase): | ||
| 87 | def test_parse_validates_synthetic_forest_and_invariants(self): | ||
| 88 | blob = build_synthetic_qtable() | ||
| 89 | rep = script_qtable.parse_qtable(blob) | ||
| 90 | self.assertTrue(rep["valid"]) | ||
| 91 | self.assertEqual(rep["record_count"], 7) | ||
| 92 | self.assertEqual(rep["root_count"], 2) | ||
| 93 | self.assertEqual(rep["distinct_root_hashes"], 2) | ||
| 94 | self.assertTrue(rep["root_bijection_holds"]) | ||
| 95 | self.assertTrue(rep["contiguous_preorder"]) | ||
| 96 | self.assertEqual(rep["total_pointers"], 5) | ||
| 97 | self.assertEqual(rep["resolved_pointers"], 5) | ||
| 98 | self.assertEqual(rep["unresolved_pointers"], 0) | ||
| 99 | self.assertEqual(rep["array_records"], 2) | ||
| 100 | self.assertEqual(rep["array_size_exact"], 2) | ||
| 101 | self.assertEqual(rep["array_pointer_records"], 1) | ||
| 102 | self.assertEqual(rep["array_value_records"], 1) | ||
| 103 | self.assertEqual(rep["struct_records"], 5) | ||
| 104 | self.assertEqual(rep["struct_wrappers"], 1) | ||
| 105 | self.assertEqual(rep["array_strides"], {"8": 1, "12": 1}) | ||
| 106 | self.assertEqual(rep["struct_sizes"], {"32": 2, "48": 2, "64": 1}) | ||
| 107 | |||
| 108 | def test_load_joins_split_64k_streams_from_carve_dir(self): | ||
| 109 | big = build_synthetic_qtable(pad_to_bytes=70_000) | ||
| 110 | self.assertGreater(len(big), 65536) | ||
| 111 | chunk0 = big[:65536] | ||
| 112 | chunk1 = big[65536:] | ||
| 113 | |||
| 114 | with tempfile.TemporaryDirectory() as tmp: | ||
| 115 | carve = os.path.join(tmp, "carve") | ||
| 116 | os.makedirs(carve) | ||
| 117 | rows = [ | ||
| 118 | {"off": 0x1000, "stored": 500, "size": len(b"PSIN" + bytes(120)), "head": (b"PSIN" + bytes(12)).hex()}, | ||
| 119 | {"off": 0x2000, "stored": 10000, "size": len(chunk0), "head": chunk0[:16].hex()}, | ||
| 120 | {"off": 0x5000, "stored": 2000, "size": len(chunk1), "head": chunk1[:16].hex()}, | ||
| 121 | {"off": 0x9000, "stored": 400, "size": 128, "head": (b"PSIN" + bytes(12)).hex()}, | ||
| 122 | ] | ||
| 123 | with open(os.path.join(carve, f"{rows[0]['off']:012x}.bin"), "wb") as f: | ||
| 124 | f.write(b"PSIN" + bytes(124)) | ||
| 125 | with open(os.path.join(carve, f"{rows[1]['off']:012x}.bin"), "wb") as f: | ||
| 126 | f.write(chunk0) | ||
| 127 | with open(os.path.join(carve, f"{rows[2]['off']:012x}.bin"), "wb") as f: | ||
| 128 | f.write(chunk1) | ||
| 129 | with open(os.path.join(carve, f"{rows[3]['off']:012x}.bin"), "wb") as f: | ||
| 130 | f.write(b"PSIN" + bytes(124)) | ||
| 131 | with open(os.path.join(carve, "index.json"), "w", encoding="utf-8", newline="\n") as f: | ||
| 132 | json.dump({"summary": {"archive": "synthetic", "files": len(rows)}, "files": rows}, f) | ||
| 133 | |||
| 134 | out_json = os.path.join(tmp, "out", "qtable.json") | ||
| 135 | p = subprocess.run( | ||
| 136 | [sys.executable, "-I", TOOL, carve, "--root-hash", "0x11111111", "--type-hash", "0xBBBB0002", "--json", out_json], | ||
| 137 | capture_output=True, text=True, encoding="utf-8", errors="replace", | ||
| 138 | ) | ||
| 139 | self.assertEqual(p.returncode, 0, p.stdout + p.stderr) | ||
| 140 | with open(out_json, "rb") as f: | ||
| 141 | raw = f.read() | ||
| 142 | self.assertIn(b"\n", raw) | ||
| 143 | self.assertNotIn(b"\r\n", raw) | ||
| 144 | rep = json.loads(raw.decode("utf-8")) | ||
| 145 | self.assertEqual(rep["location"]["offset_hex"], "0x2000") | ||
| 146 | self.assertEqual(rep["location"]["chunks"], 2) | ||
| 147 | self.assertEqual(rep["root_query"]["matches"], 1) | ||
| 148 | self.assertEqual(rep["root_query"]["trees"][0]["nodes"], 4) | ||
| 149 | self.assertEqual(rep["type_query"]["array_matches"], 1) | ||
| 150 | self.assertEqual(rep["type_query"]["distinct_trees"], 1) | ||
| 151 | |||
| 152 | def test_detects_corrupt_pointer_and_refuses_input_overwrite(self): | ||
| 153 | bad = bytearray(build_synthetic_qtable()) | ||
| 154 | # Corrupt the last record's u64 at +0x10 into a dangling 0x71717171 pointer | ||
| 155 | struct.pack_into("<Q", bad, len(bad) - 16, Q_PTR_HIGH | 0xDEADBEEF) | ||
| 156 | rep = script_qtable.parse_qtable(bytes(bad)) | ||
| 157 | self.assertFalse(rep["valid"]) | ||
| 158 | self.assertEqual(rep["unresolved_pointers"], 1) | ||
| 159 | |||
| 160 | with tempfile.TemporaryDirectory() as tmp: | ||
| 161 | bin_path = os.path.join(tmp, "qtable.bin") | ||
| 162 | with open(bin_path, "wb") as f: | ||
| 163 | f.write(build_synthetic_qtable()) | ||
| 164 | p = subprocess.run( | ||
| 165 | [sys.executable, "-I", TOOL, bin_path, "--json", bin_path], | ||
| 166 | capture_output=True, text=True, encoding="utf-8", errors="replace", | ||
| 167 | ) | ||
| 168 | self.assertEqual(p.returncode, 2) | ||
| 169 | self.assertIn("overwrite", p.stderr) | ||
| 170 | |||
| 171 | bad_file = os.path.join(tmp, "not_qtable.bin") | ||
| 172 | with open(bad_file, "wb") as f: | ||
| 173 | f.write(b"NOTQ" * 16) | ||
| 174 | p2 = subprocess.run( | ||
| 175 | [sys.executable, "-I", TOOL, bad_file], | ||
| 176 | capture_output=True, text=True, encoding="utf-8", errors="replace", | ||
| 177 | ) | ||
| 178 | self.assertEqual(p2.returncode, 1) | ||
| 179 | |||
| 180 | |||
| 181 | if __name__ == "__main__": | ||
| 182 | unittest.main() | ||