"""Synthetic-data tests for tools/skin_streams.py (ASSET-12, the joint-index and skin-weight format). python -I tests/test_skin_streams.py Every byte here is built by the test: a synthetic drawable system page, a synthetic graphics page and a synthetic RPF9-style archive made of page-aligned zlib streams. No game file, value or name is used. """ import contextlib import io import json import os import struct import subprocess import sys import tempfile import unittest import zlib from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "skin_streams.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import skin_streams as ss # noqa: E402 class Page: """A little big-endian-free allocator for the synthetic system page.""" def __init__(self, size=0x1000): self.buf = bytearray(size) self.at = 0x180 # leave room for the drawable header def put(self, data): at = self.at self.buf[at:at + len(data)] = data self.at += len(data) return at def u16(self, at, value): struct.pack_into(" (size, fmt, offset, data bytes); slot 0 must be present as the position. """ geometry = page.put(b"\0" * 160) vb = page.put(b"\0" * 80) fvf = page.put(b"\0" * 0x13C) lod = page.put(b"\0" * 16) model = page.put(b"\0" * 56) geo_array = page.put(struct.pack(" 8 else b"".join(struct.pack(" None: slots = { 0: (8, 16, 96, bytes(24)), 16: (4, 28, 128, bytes((255, 0, 0, 0)) * 3), 20: (8, 12, 144, struct.pack("<12H", 300, 700, 0, 0, 403, 700, 0, 0, 399, 700, 0, 0)), } system, gfx = build_resource(slots, vertices=3, bones=400) stats = self.run_scan(system, gfx) validated = stats["validated"]["weighted-4inf-16bit"] self.assertEqual(validated["descriptor-mismatch"], 0) self.assertEqual(validated["weighted-components"], 3) self.assertEqual(validated["components-out-of-range"], 1) def test_unaligned_resource_is_not_a_candidate(self): system, gfx, _mesh = rigid_mesh() stats = self.run_scan(system, gfx, extra=zlib.compress(system, 9)) self.assertEqual(stats["resources"], 1) # only the aligned copy counts def test_empty_archive_is_zero_resources_not_a_traceback(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "empty.rpf") open(path, "wb").close() stats = ss.scan_archive(path, 64, lambda _text: None) self.assertEqual((stats["resources"], stats["meshes"], stats["candidates"]), (0, 0, 0)) self.assertEqual(ss.main([path]), 0) def test_rigid_scan(self): system, gfx, _mesh = rigid_mesh(sixteen=True) stats = self.run_scan(system, gfx) self.assertEqual(stats["layouts"].get("rigid-16bit", {"meshes": 0})["meshes"], 1) self.assertEqual(stats["validated"]["rigid-16bit"]["components-out-of-range"], 0) def test_scan_finds_fragment_with_null_optional_pointers(self): system, gfx, _mesh = weighted_mesh("weighted-8inf-8bit", vertices=12, bones=7) b = bytearray(system) for at in (16, 32, 40): struct.pack_into(" ok 20: (8, 30, 104, bytes(16))}, 2, 6) # format 30 with 8 bytes must be refused with self.assertRaises(ss.Skip): ss.walk_resource(system, "drawable") class TestCli(unittest.TestCase): @staticmethod def _archive(tmp, name="synthetic.rpf"): system, gfx, _mesh = weighted_mesh("weighted-8inf-8bit", vertices=12, bones=7) data = build_archive(system, gfx) path = os.path.join(tmp, name) with open(path, "wb") as fh: fh.write(data) return path, data def test_json_over_an_input_archive_is_refused(self): with tempfile.TemporaryDirectory() as tmp: path, data = self._archive(tmp) other = os.path.join(tmp, ".", "synthetic.rpf") r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", other], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("would overwrite the input", r.stderr) self.assertNotIn("Traceback", r.stderr) self.assertEqual(r.stdout, "") with open(path, "rb") as fh: self.assertEqual(fh.read(), data) def test_json_that_is_a_directory_is_refused_before_the_scan(self): with tempfile.TemporaryDirectory() as tmp: path, _ = self._archive(tmp) r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", tmp], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("is a directory", r.stderr) self.assertNotIn("Traceback", r.stderr) self.assertEqual(r.stdout, "") def test_json_in_a_missing_folder_is_created(self): with tempfile.TemporaryDirectory() as tmp: path, _ = self._archive(tmp) out = os.path.join(tmp, "reports", "sub", "out.json") r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", out], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 0, r.stderr) with open(out, encoding="utf-8") as fh: report = json.load(fh) self.assertIn(path, report) self.assertEqual(report[path]["resources"], 1) def test_json_that_cannot_be_written_is_one_error_line(self): with tempfile.TemporaryDirectory() as tmp: path, _ = self._archive(tmp) blocker = os.path.join(tmp, "blocker") with open(blocker, "w", encoding="utf-8") as fh: fh.write("a file, not a folder") out = os.path.join(blocker, "out.json") r = subprocess.run([sys.executable, "-I", TOOL, path, "--json", out], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("cannot write", r.stderr) self.assertNotIn("Traceback", r.stderr) self.assertIn("synthetic.rpf: 1 resources", r.stdout) def test_invalid_sample_is_refused(self): with tempfile.TemporaryDirectory() as tmp: path, _ = self._archive(tmp) for sample in ("0", "-1"): with self.subTest(sample=sample): r = subprocess.run([sys.executable, "-I", TOOL, path, "--sample", sample], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("--sample must be at least 1", r.stderr) self.assertNotIn("Traceback", r.stderr) def test_unreadable_archive_reports_its_path_without_writing_json(self): with tempfile.TemporaryDirectory() as tmp: path, _ = self._archive(tmp) out = os.path.join(tmp, "report.json") stderr = io.StringIO() error = PermissionError(13, "input is locked", path) with (mock.patch.object(ss, "open", create=True, side_effect=error), contextlib.redirect_stderr(stderr)): self.assertEqual(ss.main([path, "--json", out]), 2) self.assertEqual(stderr.getvalue().splitlines(), [f"error: cannot read {path}: input is locked"]) self.assertFalse(os.path.exists(out)) def test_missing_or_directory_input_is_refused(self): with tempfile.TemporaryDirectory() as tmp: missing = os.path.join(tmp, "missing.rpf") r = subprocess.run([sys.executable, "-I", TOOL, missing], capture_output=True, text=True, errors="replace") self.assertEqual(r.returncode, 2, r.stderr) self.assertIn("not a file", r.stderr) self.assertNotIn("Traceback", r.stderr) r2 = subprocess.run([sys.executable, "-I", TOOL, tmp], capture_output=True, text=True, errors="replace") self.assertEqual(r2.returncode, 2, r2.stderr) self.assertIn("not a file", r2.stderr) self.assertNotIn("Traceback", r2.stderr) if __name__ == "__main__": unittest.main(verbosity=2)