mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 21:48:37 +00:00
This test suite verifies that the script 'check_no_game_data.py' correctly identifies game data signatures in various file formats while allowing ordinary files to pass without detection.
123 lines
5.2 KiB
Python
123 lines
5.2 KiB
Python
"""scripts/check_no_game_data.py catches game-data magics under any file name.
|
|
|
|
python -I tests/test_check_no_game_data.py
|
|
|
|
Every input is a few hand-written header bytes in a temporary directory (no game files): each format under a harmless
|
|
name, files that must pass (text, PNG, a lone "MZ", magics past the start), and the default mode over a throwaway git
|
|
repository. The script runs as itself, through its own command line.
|
|
"""
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
SCRIPT = os.path.join(ROOT, "scripts", "check_no_game_data.py")
|
|
|
|
|
|
def pe_header(e_lfanew=0x80):
|
|
head = bytearray(e_lfanew + 4)
|
|
head[0:2] = b"MZ"
|
|
head[0x3C:0x40] = e_lfanew.to_bytes(4, "little")
|
|
head[e_lfanew:e_lfanew + 4] = b"PE\0\0"
|
|
return bytes(head)
|
|
|
|
|
|
# file name (deliberately not the format's extension), contents, format the script must report
|
|
CAUGHT = [
|
|
("archive.bin", b"9FPR" + bytes(12), "RPF9 archive"),
|
|
("pool.dat", b"TTPF" + bytes(12), "TTPF texture pool"),
|
|
("resource.dat", b"RSC8" + bytes(12), "RAGE resource"),
|
|
("page.dat", b"0DRP" + bytes(12), "RAGE page directory"),
|
|
("tree.dat", b"RBF0" + bytes(12), "RBF0 tree"),
|
|
("shader.txt", b"XBCH" + (3).to_bytes(4, "little") + bytes(8), "XBCH shader container"),
|
|
("shader2.txt", b"FSSM" + bytes(12), "FSSM shader"),
|
|
("container.txt", b"DXBC" + bytes(12), "DXBC container"),
|
|
("module.txt", bytes.fromhex("03022307") + bytes(12), "SPIR-V module"),
|
|
("module_be.txt", bytes.fromhex("07230203") + bytes(12), "SPIR-V module"),
|
|
("texture.png.txt", b"DDS " + (124).to_bytes(4, "little") + bytes(8), "DDS texture"),
|
|
("bank.json", b"ADAT" + bytes(12), "AWC audio bank"),
|
|
("music.md", b"OggS\0\x02" + bytes(10), "Ogg stream"),
|
|
("sound.csv", b"RIFF" + bytes(4) + b"WAVEfmt ", "RIFF WAVE audio"),
|
|
("movie.txt", b"KB2j" + bytes(12), "Bink 2 video"),
|
|
("model.json", b"glTF" + (2).to_bytes(4, "little") + bytes(8), "glTF binary"),
|
|
("game.txt", pe_header(), "PE executable"),
|
|
("far_pe.txt", pe_header(0x400), "PE executable"),
|
|
("image.bin", bytes(0x200) + b"msft-xvd" + bytes(8), "XVD image"),
|
|
]
|
|
|
|
PASSED = [
|
|
("README.md", b"# Notes\n\nThe RPF9 magic is stored as 9FPR; DDS files start with 'DDS '.\n"),
|
|
("logo.png", b"\x89PNG\r\n\x1a\n" + bytes(16)),
|
|
("mz.txt", b"MZ is not enough on its own\n"),
|
|
("metadata.txt", b"METADATA ADAT later in the line\n"),
|
|
("riff.bin", b"RIFF" + bytes(4) + b"AVI LIST"),
|
|
("empty.txt", b""),
|
|
]
|
|
|
|
|
|
def run(args, cwd=None):
|
|
return subprocess.run([sys.executable, "-I", SCRIPT, *args], cwd=cwd, capture_output=True, text=True)
|
|
|
|
|
|
class CheckNoGameData(unittest.TestCase):
|
|
def setUp(self):
|
|
self.dir = tempfile.mkdtemp()
|
|
self.addCleanup(shutil.rmtree, self.dir)
|
|
|
|
def write(self, name, data):
|
|
path = os.path.join(self.dir, name)
|
|
with open(path, "wb") as f:
|
|
f.write(data)
|
|
return path
|
|
|
|
def test_each_format_is_caught_under_another_name(self):
|
|
for name, data, fmt in CAUGHT:
|
|
with self.subTest(name=name):
|
|
result = run([self.write(name, data)])
|
|
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
|
self.assertIn(f"{name}: {fmt}", result.stdout)
|
|
|
|
def test_ordinary_files_pass(self):
|
|
paths = [self.write(name, data) for name, data in PASSED]
|
|
result = run(paths)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
self.assertEqual(result.stdout, "")
|
|
|
|
def test_reports_every_match_among_clean_files(self):
|
|
clean = self.write("README.md", b"# fine\n")
|
|
bad1 = self.write("a.bin", b"DDS " + bytes(12))
|
|
bad2 = self.write("b.bin", b"OggS" + bytes(12))
|
|
result = run([clean, bad1, bad2])
|
|
self.assertEqual(result.returncode, 1)
|
|
self.assertEqual(len(result.stdout.splitlines()), 2, result.stdout)
|
|
self.assertIn("2 file(s)", result.stderr)
|
|
|
|
def test_directories_and_missing_paths_are_skipped(self):
|
|
os.mkdir(os.path.join(self.dir, "sub"))
|
|
result = run([os.path.join(self.dir, "sub"), os.path.join(self.dir, "missing.bin")])
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
|
|
@unittest.skipUnless(shutil.which("git"), "git not installed")
|
|
def test_default_mode_reads_tracked_files_only(self):
|
|
git = ["git", "-c", "init.defaultBranch=main"]
|
|
subprocess.run([*git, "init", "-q", self.dir], check=True)
|
|
self.write("notes.txt", b"plain text\n")
|
|
self.write("untracked.bin", b"DDS " + bytes(12))
|
|
subprocess.run(["git", "add", "notes.txt"], cwd=self.dir, check=True)
|
|
result = run([], cwd=self.dir)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
|
|
self.write("renamed.txt", b"FSSM" + bytes(12))
|
|
subprocess.run(["git", "add", "renamed.txt"], cwd=self.dir, check=True)
|
|
result = run([], cwd=self.dir)
|
|
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
|
|
self.assertIn("renamed.txt: FSSM shader", result.stdout)
|
|
self.assertNotIn("untracked.bin", result.stdout)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|