"""Synthetic tests for tools/psin_index.py (no game data).""" import json import os import struct import subprocess import sys import tempfile import unittest import unittest.mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "psin_index.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import psin_index as P # noqa: E402 def make_section(tag, payload=b""): size = 8 + len(payload) return tag.encode("latin-1") + struct.pack(">I", size) + payload def make_psin(sections, version=22, table_bytes=0, first=None): body = b"".join(make_section(tag, payload) for tag, payload in sections) first = 16 if first is None else first header = bytearray(max(first, 16)) header[0:4] = b"PSIN" struct.pack_into(">I", header, 4, first) struct.pack_into("I", 5392) + b"\x99\xfe\x66\x9f"), ]) got = P.parse(blob) self.assertTrue(got["complete"]) self.assertEqual(got["rsc_version"], 122) self.assertEqual(got["sections"][0]["rsc_magic"], "0DRP") self.assertEqual(got["checksum_size"], 5392) self.assertEqual(got["checksum"], "99fe669f") def test_not_psin(self): self.assertIsNone(P.parse(b"NOPE" + b"\x00" * 20)) def test_first_section_outside_file(self): blob = make_psin([("LSCH", b"")], first=16) blob = blob[:4] + struct.pack(">I", 9999) + blob[8:] got = P.parse(blob) self.assertFalse(got["complete"]) self.assertIn("outside", got["error"]) def test_first_section_must_exist(self): for padding in (0, 1, 7): with self.subTest(padding=padding): blob = make_psin([], first=32, table_bytes=16) + b"\0" * padding got = P.parse(blob) self.assertFalse(got["complete"]) self.assertEqual(got["sections"], []) self.assertIn("first section header", got["error"]) def test_empty_first_section_payload_is_valid(self): got = P.parse(make_psin([("LSCH", b"")], first=32, table_bytes=16)) self.assertTrue(got["complete"]) self.assertEqual(got["sections"][0]["size"], 8) def test_truncated_section(self): blob = make_psin([("LSCH", b"\x00" * 8)]) got = P.parse(blob[:-2]) self.assertFalse(got["complete"]) def test_zero_size_section(self): blob = make_psin([("LSCH", b"")]) blob = blob[:20] + struct.pack(">I", 0) + blob[24:] got = P.parse(blob) self.assertFalse(got["complete"]) self.assertIn("size 0", got["error"]) def test_trailing_bytes(self): blob = make_psin([("LSCH", b"")]) + b"junk" got = P.parse(blob) self.assertFalse(got["complete"]) self.assertIn("ended at", got["error"]) def make_lsch(structs): """structs: [(hash, signature, size, align_log2, [(hash, offset, type, sub, index, ref), ...]), ...]""" heads = b"".join(struct.pack("> 6 h = (h + (h << 3)) & 0xFFFFFFFF h ^= h >> 11 return (h + (h << 15)) & 0xFFFFFFFF SCHEMA = [ (joaat("Outer"), 0x11111111, 0x20, 3, [ (0x100, 0, 5, 0, 0, joaat("Inner")), # element descriptor of the array below (joaat("items"), 0x00, 0x5C, 0, 0, 0), # array of Inner, element at index 0 (joaat("kind"), 0x10, 0x62, 0, 0, joaat("Kind")), (joaat("scale"), 0x14, 0x21, 0, 0, 0), ]), (joaat("Inner"), 0x22222222, 0x8, 2, [ (joaat("a"), 0, 0x21, 0, 0, 0), (joaat("b"), 4, 0x21, 0, 0, 0), ]), ] ENUMS = [ (joaat("Kind"), 0x33333333, [(0, joaat("KIND_A")), (1, joaat("KIND_B")), (-1, joaat("KIND_NONE"))]), (joaat("Flags"), 0x44444444, [(1, joaat("F_ONE")), (4, joaat("F_FOUR"))]), ] def make_lsns(blocks): """An LSNS payload: block count, (kind, bytes) table, then the blocks (filler records).""" table = struct.pack("I", 9999) + good[8:], # first section past EOF "00000002.bin": make_psin([], first=32, table_bytes=16), # no first section header "00000003.bin": good[:-4], # LENM cut short "00000004.bin": good, } with tempfile.TemporaryDirectory() as tmp: for name, blob in cases.items(): with open(os.path.join(tmp, name), "wb") as f: f.write(blob) p = subprocess.run([sys.executable, "-I", TOOL, tmp, "--json", "--schema"], capture_output=True, text=True) self.assertEqual(p.returncode, 0, p.stderr) files = {r["file"]: r for r in json.loads(p.stdout)["files"]} self.assertEqual(len(files), 4) for r in files.values(): # one shape for every record self.assertEqual(set(r["schema"]), {"structs", "enums", "lsch", "lenm", "reason"}) first = files["00000001.bin"]["schema"] self.assertEqual((first["structs"], first["enums"], first["lsch"], first["lenm"]), ([], [], False, False)) self.assertIn("outside the file", first["reason"]) self.assertIn("first section header", files["00000002.bin"]["schema"]["reason"]) cut = files["00000003.bin"]["schema"] self.assertTrue(cut["lsch"]) self.assertFalse(cut["lenm"]) self.assertEqual(len(cut["structs"]), 2) self.assertIn("no whole LENM section", cut["reason"]) self.assertEqual(files["00000004.bin"]["schema"]["reason"], "") t = subprocess.run([sys.executable, "-I", TOOL, tmp, "--schema"], capture_output=True, text=True) self.assertEqual(t.returncode, 0, t.stderr) self.assertNotIn("Traceback", t.stderr) self.assertIn("schema: no whole LSCH section", t.stdout) def test_schema_reason_for_missing_and_bad_sections(self): got = P.parse(make_psin([("LGID", b"")]), schema=True)["schema"] self.assertEqual(got["reason"], "no LSCH section; no LENM section") got = P.parse(make_psin([("LSCH", b"\x01\x02\x03"), ("LENM", b"")]), schema=True)["schema"] self.assertFalse(got["lsch"]) self.assertTrue(got["lenm"]) self.assertIn("LSCH: no struct count", got["reason"]) def test_json_schema(self): with tempfile.TemporaryDirectory() as tmp: with open(os.path.join(tmp, "00000001.bin"), "wb") as f: f.write(make_psin([("LSCH", make_lsch(SCHEMA)), ("LENM", make_lenm(ENUMS))])) p = subprocess.run([sys.executable, "-I", TOOL, tmp, "--json", "--schema"], capture_output=True, text=True) self.assertEqual(p.returncode, 0, p.stderr) doc = json.loads(p.stdout) s = doc["summary"] self.assertEqual((s["lsch_decoded"], s["lenm_decoded"], s["structs"], s["members"], s["enums"], s["enum_values"], s["schema_errors"]), (1, 1, 2, 6, 2, 5, 0)) self.assertEqual(len(doc["files"][0]["schema"]["structs"][0]["members"]), 4) t = subprocess.run([sys.executable, "-I", TOOL, tmp, "--schema"], capture_output=True, text=True) self.assertEqual(t.returncode, 0, t.stderr) self.assertIn(f"struct {joaat('Outer'):08x}", t.stdout) self.assertIn("structs=2 members=6", t.stdout) def test_json_directory(self): with tempfile.TemporaryDirectory() as tmp: good = make_psin([("LSCH", b""), ("CHKS", b"\x00" * 4)]) with open(os.path.join(tmp, "00000001.bin"), "wb") as f: f.write(good) with open(os.path.join(tmp, "00000002.bin"), "wb") as f: f.write(b"not a psin") with open(os.path.join(tmp, "00000003.bin"), "wb") as f: f.write(make_psin([])) p = subprocess.run([sys.executable, "-I", TOOL, tmp, "--json"], capture_output=True, text=True) self.assertEqual(p.returncode, 0, p.stderr) doc = json.loads(p.stdout) self.assertEqual(doc["summary"]["files"], 2) self.assertEqual(doc["summary"]["complete"], 1) self.assertEqual(doc["summary"]["incomplete"], 1) self.assertEqual(doc["summary"]["section_tags"], {"CHKS": 1, "LSCH": 1}) def test_limit_below_one_is_refused(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "00000001.bin") with open(path, "wb") as f: f.write(make_psin([("LSCH", b"")])) for value in ("0", "-1"): p = subprocess.run([sys.executable, "-I", TOOL, tmp, "--limit", value], capture_output=True, text=True) self.assertEqual(p.returncode, 2) self.assertIn("must be at least 1", p.stderr) def test_missing_source_is_one_line(self): with tempfile.TemporaryDirectory() as tmp: for missing in (os.path.join(tmp, "nope"), os.path.join(tmp, "nope.bin")): p = subprocess.run([sys.executable, "-I", TOOL, missing, "--json"], capture_output=True, text=True) self.assertEqual(p.returncode, 2, p.stderr) self.assertNotIn("Traceback", p.stderr) self.assertIn(missing, p.stderr) self.assertEqual(p.stdout, "") def test_unlistable_folder_is_one_line(self): with tempfile.TemporaryDirectory() as tmp: # A folder that cannot be listed (no permission); patched so the test runs the same on every OS. with unittest.mock.patch("os.listdir", side_effect=PermissionError(13, "Permission denied")): code, err = run_main([tmp]) self.assertEqual(code, 2) self.assertIn("Permission denied", err) def run_main(argv): import io from contextlib import redirect_stderr, redirect_stdout err = io.StringIO() with unittest.mock.patch.object(sys, "argv", ["psin_index.py", *argv]), \ redirect_stdout(io.StringIO()), redirect_stderr(err): code = P.main() return code, err.getvalue() if __name__ == "__main__": unittest.main()