"""Synthetic unit and CLI tests for tools/odrp_7a.py. python -I tests/test_odrp_7a.py Every byte in this file is constructed synthetically. No game file, carved payload, asset name, path string, or asset hash is used. """ import csv import json import os import struct import subprocess import sys import tempfile import unittest import unittest.mock import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "odrp_7a.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import odrp_7a # noqa: E402 def _align16(n): return (n + 15) & ~15 def build_standalone_resource( *, root_objects=4, enums=1, as_map=False, entity_count=3, include_physical_block=False, ): """Build a complete synthetic standalone `0DRP`/`0x7a` resource.""" # Struct 0: custom root struct (or CMapData when as_map=True), 32 bytes, 3 members: # member 0: scalar field at offset 0 # member 1: 0x100 array-element descriptor (field_off may exceed struct size) # member 2: 0x5c array member at offset 16 targeting descriptor index 1 root_hash = odrp_7a.MAPDATA_STRUCT if as_map else 0x11223344 structs_spec = [ ( root_hash, 32, [ (0xAABBCC01, 0, 0x04, 0, 0, 0), (odrp_7a.ARRAY_ELEMENT_HASH, 64, 0x04, 0, 0, 0), (0xAABBCC02, 16, odrp_7a.ARRAY_MEMBER_TYPE, 0, 1, 0), ], ) ] if as_map: structs_spec.append( ( odrp_7a.ENTITY_STRUCT, 48, [ (0xAABBCC10, 0, 0x04, 0, 0, 0), (0xAABBCC11, 16, 0x04, 0, 0, 0), ], ) ) nst = len(structs_spec) blocks_spec = [(root_hash, 32, True)] if as_map: blocks_spec.append((odrp_7a.ENTITY_STRUCT, 48 * entity_count, True)) if include_physical_block: blocks_spec.append((0x778899AA, 128, False)) nbl = len(blocks_spec) pos = odrp_7a.STANDALONE_HEADER_SIZE # 0x60 block_offsets = [] for _bhash, bsize, is_virtual in blocks_spec: if is_virtual: pos = _align16(pos) block_offsets.append(pos) pos += bsize else: block_offsets.append(None) pos = _align16(pos) q38 = pos pos += 16 * nbl member_offsets = [] for _shash, _ssize, members in structs_spec: pos = _align16(pos) member_offsets.append(pos) pos += 16 * len(members) pos = _align16(pos) q28 = pos pos += 32 * nst q30 = 0 if enums > 0: pos = _align16(pos) q30 = pos pos += 24 * enums total_size = _align16(pos) buf = bytearray(total_size) # +0x00..+0x0f: 8 zero bytes + virtual self/page-map pointer struct.pack_into(" 0 else 0) struct.pack_into("= struct size -> hard schema error (not continuation) bad_member = bytearray(valid) struct.pack_into("= size 32", res["error"]) # 2. Array member (0x5c) targeting non-0x100 member -> hard schema error bad_array = bytearray(valid) struct.pack_into(" hard schema error phys_only = bytearray(valid) struct.pack_into(" needs_continuation=True truncated = bytes(valid[:q28]) res = odrp_7a.parse_standalone(truncated) self.assertFalse(res["complete"]) self.assertTrue(res["needs_continuation"]) def test_psin_mismatch_and_non_lrsc_psin(self): mismatch_pay = build_lrsc_payload(root_block=1, structs=9, enums=1, blocks=2) bad_psin = build_psin_container( structs=2, enums=1, block_sizes=(16, 16), lrsc_override=mismatch_pay, ) res = odrp_7a.classify_blob(bad_psin) self.assertIsNotNone(res) self.assertEqual(res["placement"], "psin_lrsc") self.assertFalse(res["complete"]) self.assertFalse(res["schema_checked"]) self.assertIn("structs 9 != LSCH 2", res["error"]) # A PSIN container without LRSC (e.g. LSCH+LENM+LSNS+LGID+CHKS or PMAP/PSCH) emits None. no_lrsc = build_psin_container(include_lrsc=False) self.assertIsNone(odrp_7a.classify_blob(no_lrsc)) class ArchiveAndCliTest(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.root = self.tmp.name self.meta_single = build_standalone_resource(root_objects=3, enums=1, as_map=False) self.map_full = build_standalone_resource(root_objects=1, enums=0, as_map=True, entity_count=4) # Split `map_full` at 0x80 so q38/q28 sit in the second zlib stream. self.map_part1 = self.map_full[:0x80] self.map_part2 = self.map_full[0x80:] self.assertTrue(odrp_7a.parse_standalone(self.map_part1)["needs_continuation"]) self.psin_single = build_psin_container(structs=2, enums=1, block_sizes=(16, 32), root_block=2) self.psin_multi = build_psin_container( structs=3, enums=1, block_sizes=(32, 32, 32), root_block=3, lsns_payload_size=240, ) # Split `psin_multi` into 3 contiguous 128-byte-ish chunks (>= 64 B each for zlib scanner). c1 = 128 c2 = len(self.psin_multi) - 96 self.psin_p1 = self.psin_multi[:c1] self.psin_p2 = self.psin_multi[c1:c2] self.psin_p3 = self.psin_multi[c2:] # Unjoined psin_tail stream preceded by a non-zero raw gap (emulating an uncompressed LSNS block). tail_pay = build_lrsc_payload(root_block=1, structs=4, enums=0, blocks=2) self.psin_tail_stream = ( bytes([0x5A]) * 96 + odrp_7a.LRSC_TAG + (88).to_bytes(4, "big") + tail_pay + odrp_7a.CHKS_TAG + (20).to_bytes(4, "big") + bytes(12) ) self.no_lrsc_psin = build_psin_container(include_lrsc=False) self.zero_page = bytes(128) self.bink = build_bink2(64) entries = [ ("zlib", self.meta_single), # 0: standalone meta (1 stream) ("zlib", self.map_part1), # 1: standalone map part 1 ("zlib", self.map_part2), # 2: standalone map part 2 (joined -> 2 streams) ("bink", self.bink), # 3: KB2n skipped cleanly ("zlib", self.psin_single), # 4: single-stream PSIN LRSC ("zlib", self.psin_p1), # 5: 3-stream PSIN part 1 ("zlib", self.psin_p2), # 6: 3-stream PSIN part 2 ("zlib", self.psin_p3), # 7: 3-stream PSIN part 3 (joined -> 3 streams) ("raw_gap", b"\xaa" * 64), # 8: unread gap before unjoined PSIN tail ("zlib", self.psin_tail_stream), # 9: psin_tail (1 stream) ("zlib", self.no_lrsc_psin), # 10: PSIN without LRSC (ignored) ("zlib", self.zero_page), # 11: plain system page (ignored) ] rpf_bytes, self.offsets, self.stored_sizes = build_rpf9(entries) self.rpf = os.path.join(self.root, "synthetic.rpf") with open(self.rpf, "wb") as fh: fh.write(rpf_bytes) def tearDown(self): self.tmp.cleanup() def test_scan_rpf_joins_split_containers_and_distinguishes_all_records(self): rows, summary = odrp_7a.scan_rpf(self.rpf) self.assertEqual(summary["streams"], 11) expected_unread = ((-self.stored_sizes[7]) % 16) + 64 self.assertEqual(summary["unread_bytes"], expected_unread) self.assertEqual(summary["records"], 5) self.assertEqual(summary["complete"], 5) self.assertEqual(summary["incomplete"], 0) self.assertEqual(summary["standalone"], 2) self.assertEqual(summary["standalone_complete"], 2) self.assertEqual(summary["standalone_meta"], 1) self.assertEqual(summary["standalone_map"], 1) self.assertEqual(summary["standalone_joined"], 1) self.assertEqual(summary["standalone_entities"], 4) self.assertEqual(summary["psin_lrsc"], 3) self.assertEqual(summary["psin_lrsc_complete"], 3) self.assertEqual(summary["psin_lrsc_joined"], 1) self.assertEqual(summary["psin_lrsc_schema_checked"], 2) self.assertEqual(summary["framings"], {"psin": 2, "psin_tail": 1, "resource": 2}) self.assertEqual( [r["offset"] for r in rows], [self.offsets[0], self.offsets[1], self.offsets[4], self.offsets[5], self.offsets[9]], ) self.assertEqual([r["streams"] for r in rows], [1, 2, 1, 3, 1]) self.assertEqual( [r["placement"] for r in rows], ["standalone", "standalone", "psin_lrsc", "psin_lrsc", "psin_lrsc"], ) self.assertEqual([r["framing"] for r in rows], ["resource", "resource", "psin", "psin", "psin_tail"]) self.assertEqual(rows[1]["size"], len(self.map_full)) self.assertEqual(rows[1]["stored"], self.stored_sizes[1] + self.stored_sizes[2]) self.assertEqual(rows[3]["size"], len(self.psin_multi)) self.assertEqual(rows[3]["root_block"], 3) self.assertEqual(rows[4]["root_block"], 1) def test_scan_limit_excludes_stream_extending_past_window(self): # Cut 1 byte before the end of stream 0's compressed body: stream 0 must be left out. limit = self.stored_sizes[0] - 1 rows, summary = odrp_7a.scan_rpf(self.rpf, limit=limit) self.assertEqual(rows, []) self.assertEqual(summary["streams"], 0) self.assertEqual(summary["span"], limit) def test_scan_dir_joins_with_manifest_and_respects_max_inflate(self): folder = os.path.join(self.root, "carved") os.makedirs(folder) manifest_files = [] # Write map_part1 and map_part2 with contiguous 16-byte-aligned archive offsets in index.json, # plus psin_single and an oversized .bin that exceeds max_inflate=512. off1 = 0x1000 stored1 = 0x93 off2 = _align16(off1 + stored1) stored2 = 0xA4 off3 = 0x3000 stored3 = 0x80 off4 = 0x4000 items = [ (off1, stored1, self.map_part1), (off2, stored2, self.map_part2), (off3, stored3, self.psin_single), ] for off, st, data in items: with open(os.path.join(folder, f"{off:012x}.bin"), "wb") as fh: fh.write(data) manifest_files.append({"off": off, "stored": st, "size": len(data)}) with open(os.path.join(folder, f"{off4:012x}.bin"), "wb") as fh: fh.write(bytes(600)) with open(os.path.join(folder, f"{0x5000:012x}.bk2"), "wb") as fh: fh.write(self.bink) with open(os.path.join(folder, "index.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump({"files": manifest_files}, fh) rows, summary = odrp_7a.scan_dir(folder, max_inflate=512) self.assertEqual(summary["mode"], "directory") self.assertEqual(summary["streams"], 3) self.assertEqual(summary["skipped_candidates"], 1) self.assertEqual(len(rows), 2) self.assertEqual((rows[0]["placement"], rows[0]["streams"], rows[0]["entities"]), ("standalone", 2, 4)) self.assertEqual((rows[1]["placement"], rows[1]["framing"], rows[1]["complete"]), ("psin_lrsc", "psin", True)) def test_single_file_cli_and_lf_line_endings(self): single_meta = os.path.join(self.root, "meta.bin") with open(single_meta, "wb") as fh: fh.write(self.meta_single) out_json = os.path.join(self.root, "single.json") proc = subprocess.run( [sys.executable, "-I", TOOL, single_meta, out_json, "--quiet"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) with open(out_json, "rb") as fh: raw_json = fh.read() self.assertNotIn(b"\r", raw_json) doc = json.loads(raw_json.decode("utf-8")) self.assertEqual(doc["summary"]["mode"], "file") self.assertEqual(doc["summary"]["standalone"], 1) self.assertEqual(doc["records"][0]["root_objects"], 3) out_csv = os.path.join(self.root, "rpf.csv") proc = subprocess.run( [sys.executable, "-I", TOOL, self.rpf, out_csv, "--quiet"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr) with open(out_csv, "rb") as fh: raw_csv = fh.read() self.assertNotIn(b"\r", raw_csv) table = list(csv.reader(raw_csv.decode("utf-8").splitlines())) self.assertEqual(table[0], odrp_7a.COLUMNS) self.assertEqual(len(table), 6) def test_reports_can_be_replaced_inside_the_carve_folder(self): folder = os.path.join(self.root, "carved") os.mkdir(folder) source = os.path.join(folder, "00000001.bin") with open(source, "wb") as f: f.write(self.meta_single) for name in ("report.json", "report.csv"): with self.subTest(name=name): out = os.path.join(folder, name) with open(out, "wb") as f: f.write(b"old report") result = subprocess.run([sys.executable, "-I", TOOL, folder, out, "--quiet"], capture_output=True, text=True) self.assertEqual(result.returncode, 0, result.stderr) with open(out, "rb") as f: self.assertNotEqual(f.read(), b"old report") with open(source, "rb") as f: self.assertEqual(f.read(), self.meta_single) def test_report_alias_to_a_binary_payload_is_rejected(self): folder = os.path.join(self.root, "carved") os.mkdir(folder) source = os.path.join(folder, "00000001.bin") alias = os.path.join(folder, "report.json") with open(source, "wb") as f: f.write(self.meta_single) try: os.link(source, alias) except (OSError, NotImplementedError) as e: self.skipTest(str(e)) result = subprocess.run([sys.executable, "-I", TOOL, folder, alias, "--quiet"], capture_output=True, text=True) self.assertEqual(result.returncode, 2, result.stderr) self.assertIn("would overwrite", result.stderr) with open(source, "rb") as f: self.assertEqual(f.read(), self.meta_single) def test_cli_validation_and_error_handling(self): # Bad numeric flags exit 2 without creating output. for flag, value, message in ( ("--start", "-1", "must be 0 or more"), ("--limit", "0", "must be at least 1"), ("--limit", "-5", "must be at least 1"), ("--max-inflate", "0", "must be at least 1"), ): out = os.path.join(self.root, "bad_num.json") proc = subprocess.run( [sys.executable, "-I", TOOL, self.rpf, out, "--quiet", flag, value], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 2, proc.stderr) self.assertIn(message, proc.stderr) self.assertNotIn("Traceback", proc.stderr) self.assertFalse(os.path.exists(out)) # Missing source exits 1 without traceback. missing_out = os.path.join(self.root, "missing.json") proc = subprocess.run( [sys.executable, "-I", TOOL, os.path.join(self.root, "absent.rpf"), missing_out, "--quiet"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 1, proc.stderr) self.assertNotIn("Traceback", proc.stderr) self.assertFalse(os.path.exists(missing_out)) # Unwritable/bad output paths and source aliasing exit 2. for bad_out in ( self.root, os.path.join(self.root, "missing_parent", "out.json"), os.path.join(self.root, "out.txt"), ): proc = subprocess.run( [sys.executable, "-I", TOOL, self.rpf, bad_out, "--quiet"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 2, proc.stderr) self.assertNotIn("Traceback", proc.stderr) json_src = os.path.join(self.root, "same.json") with open(json_src, "wb") as fh: fh.write(self.meta_single) alias_out = os.path.join(self.root, ".", "same.json") proc = subprocess.run( [sys.executable, "-I", TOOL, json_src, alias_out, "--quiet"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 2, proc.stderr) self.assertIn("output must not overwrite the source", proc.stderr) # --start / --limit rejected on directory and non-RPF single file. for non_rpf in (self.root, json_src): proc = subprocess.run( [sys.executable, "-I", TOOL, non_rpf, os.path.join(self.root, "x.json"), "--start", "0"], capture_output=True, text=True, encoding="utf-8", errors="replace", ) self.assertEqual(proc.returncode, 2, proc.stderr) def test_scan_rpf_closes_mmap_on_success_and_out_of_range_start(self): created = [] real_mmap = odrp_7a.mmap.mmap def track_mmap(*args, **kwargs): m = real_mmap(*args, **kwargs) created.append(m) return m with open(self.rpf, "rb") as src: raw_bytes = src.read() rpf_ok = os.path.join(self.root, "ok.rpf") rpf_err = os.path.join(self.root, "err.rpf") for path in (rpf_ok, rpf_err): with open(path, "wb") as dst: dst.write(raw_bytes) try: with unittest.mock.patch.object(odrp_7a.mmap, "mmap", side_effect=track_mmap): odrp_7a.scan_rpf(rpf_ok) self.assertEqual(len(created), 1) self.assertTrue(created[-1].closed) os.remove(rpf_ok) self.assertFalse(os.path.exists(rpf_ok)) with self.assertRaisesRegex(ValueError, "start offset is outside the archive"): odrp_7a.scan_rpf(rpf_err, start=len(raw_bytes) + 1) self.assertEqual(len(created), 2) self.assertTrue(created[-1].closed) os.remove(rpf_err) self.assertFalse(os.path.exists(rpf_err)) finally: for m in created: if not m.closed: m.close() def test_failed_output_write_preserves_existing_json_and_csv_reports(self): real_named_temp = tempfile.NamedTemporaryFile def failing(*args, **kwargs): handle = real_named_temp(*args, **kwargs) def fail_write(data): handle.file.write(data[:4]) raise OSError(28, "No space left on device") handle.write = fail_write return handle for name, old_text in (("kept.json", '{"previous": true}\n'), ("kept.csv", "offset,kind\n0x0,old\n")): out = os.path.join(self.root, name) with open(out, "w", encoding="utf-8", newline="\n") as f: f.write(old_text) for label, patch in ( ("write", unittest.mock.patch.object(odrp_7a.tempfile, "NamedTemporaryFile", side_effect=failing)), ("replace", unittest.mock.patch.object(odrp_7a.os, "replace", side_effect=PermissionError(13, "Permission denied"))), ): with self.subTest(name=name, label=label), patch: with self.assertRaises(SystemExit) as cm: odrp_7a.main([self.rpf, out, "--quiet"]) self.assertNotEqual(cm.exception.code, 0) with open(out, encoding="utf-8") as f: self.assertEqual(f.read(), old_text) self.assertFalse([n for n in os.listdir(self.root) if n.startswith(".odrp_7a-")]) if __name__ == "__main__": unittest.main()