"""Tests for tools/classify_carve.py, built from synthetic carve indexes (no game data). python -I tests/test_classify_carve.py """ import contextlib import importlib.util import io import json import os import subprocess import sys import tempfile import unittest from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ZERO_HEAD = "00" * 16 RBF0_HEAD = "5242463000000000" RESPAGE_HEAD = "42400000deadbeef" def load_module(): path = os.path.join(ROOT, "tools", "classify_carve.py") spec = importlib.util.spec_from_file_location("classify_carve_under_test", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module carve = load_module() def counts(entry): return {k: entry[k] for k in ("files", "bytes")} class ClassifyTests(unittest.TestCase): def test_bink2_rows_do_not_need_a_head(self): # rpf9_carve writes bink2 rows with dimensions and no head field. self.assertEqual(carve.classify({"type": "bink2", "size": 12}), "bink2") def test_bink2_wins_over_a_signature(self): self.assertEqual(carve.classify({"type": "bink2", "head": RBF0_HEAD, "size": 1}), "bink2") def test_rbf0_metadata(self): self.assertEqual(carve.classify({"head": RBF0_HEAD, "size": 1}), "rbf0") def test_resource_page(self): self.assertEqual(carve.classify({"head": RESPAGE_HEAD, "size": 1}), "respage") def test_gpu_zero_header_needs_sixteen_zero_bytes(self): self.assertEqual(carve.classify({"head": ZERO_HEAD, "size": 1}), "gpu-zerohdr") # Fifteen zero bytes are not enough, and the fallback names four bytes. self.assertEqual(carve.classify({"head": "00" * 15, "size": 1}), "other:00000000") def test_other_is_keyed_by_the_first_four_bytes(self): self.assertEqual(carve.classify({"head": "deadbeefcafebabe", "size": 1}), "other:deadbeef") def test_short_head_is_used_as_is(self): self.assertEqual(carve.classify({"head": "abcd", "size": 1}), "other:abcd") def test_signature_must_be_at_the_start(self): self.assertEqual(carve.classify({"head": "ff" + RBF0_HEAD, "size": 1}), "other:ff524246") def test_uppercase_hex_signatures_classify_the_same_as_lowercase(self): self.assertEqual(carve.classify({"head": RBF0_HEAD.upper(), "size": 1}), "rbf0") self.assertEqual(carve.classify({"head": RESPAGE_HEAD.upper(), "size": 1}), "respage") self.assertEqual(carve.classify({"head": "DEADBEEFCAFEBABE", "size": 1}), "other:deadbeef") class BomIndexTests(unittest.TestCase): def test_utf8_bom_index_is_accepted(self): rows = [{"head": RBF0_HEAD, "size": 1}] with tempfile.TemporaryDirectory() as root: index = os.path.join(root, "index.json") with open(index, "w", encoding="utf-8-sig") as f: json.dump({"files": rows}, f) self.assertEqual(list(carve.index_rows(index)), rows) class SizeStatsTests(unittest.TestCase): def setUp(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.root = tmp.name # Archive a: rbf0 sizes 40, 10, 30 (odd), gpu-zerohdr 5 (single). # Archive b: rbf0 sizes 20 (single), gpu-zerohdr 8, 2, 6, 100 (even). self.archive("a", [ {"head": RBF0_HEAD, "size": 40}, {"head": RBF0_HEAD, "size": 10}, {"head": RBF0_HEAD, "size": 30}, {"head": ZERO_HEAD, "size": 5}, ]) self.archive("b", [ {"head": RBF0_HEAD, "size": 20}, {"head": ZERO_HEAD, "size": 8}, {"head": ZERO_HEAD, "size": 2}, {"head": ZERO_HEAD, "size": 6}, {"head": ZERO_HEAD, "size": 100}, ]) with mock.patch("sys.argv", ["classify_carve.py", self.root]), contextlib.redirect_stdout(io.StringIO()): carve.main() with open(os.path.join(self.root, "types.json"), encoding="utf-8") as f: self.types = json.load(f) def archive(self, name, rows): path = os.path.join(self.root, name) os.makedirs(path) with open(os.path.join(path, "index.json"), "w", encoding="utf-8") as f: json.dump({"files": rows}, f) def test_per_archive_stats(self): per = self.types["per_archive"] self.assertEqual(per["a"]["rbf0"], {"files": 3, "bytes": 80, "min": 10, "max": 40, "median": 30}) self.assertEqual(per["a"]["gpu-zerohdr"], {"files": 1, "bytes": 5, "min": 5, "max": 5, "median": 5}) self.assertEqual(per["b"]["rbf0"], {"files": 1, "bytes": 20, "min": 20, "max": 20, "median": 20}) # Even count: sizes 2, 6, 8, 100 -> lower middle value 6, an integer. self.assertEqual(per["b"]["gpu-zerohdr"], {"files": 4, "bytes": 116, "min": 2, "max": 100, "median": 6}) def test_overall_stats_span_archives(self): overall = self.types["overall"] # rbf0: 10, 20, 30, 40 -> even, lower middle 20. self.assertEqual(overall["rbf0"], {"files": 4, "bytes": 100, "min": 10, "max": 40, "median": 20}) # gpu-zerohdr: 2, 5, 6, 8, 100 -> odd, middle 6. self.assertEqual(overall["gpu-zerohdr"], {"files": 5, "bytes": 121, "min": 2, "max": 100, "median": 6}) def test_existing_keys_keep_their_values_and_order(self): for scope in (self.types["overall"], *self.types["per_archive"].values()): for stats in scope.values(): self.assertEqual(list(stats), ["files", "bytes", "min", "max", "median"]) self.assertTrue(all(type(v) is int for v in stats.values())) self.assertEqual(self.types["overall"]["rbf0"]["files"], 4) self.assertEqual(self.types["overall"]["rbf0"]["bytes"], 100) self.assertEqual(list(self.types["overall"]), ["gpu-zerohdr", "rbf0"]) def test_size_beyond_64_bits_is_accepted(self): big = 2 ** 64 with tempfile.TemporaryDirectory() as root: os.makedirs(os.path.join(root, "a")) with open(os.path.join(root, "a", "index.json"), "w", encoding="utf-8") as f: json.dump({"files": [{"head": ZERO_HEAD, "size": big}]}, f) with mock.patch("sys.argv", ["classify_carve.py", root]), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(carve.main(), 0) with open(os.path.join(root, "types.json"), encoding="utf-8") as f: types = json.load(f) want = {"files": 1, "bytes": big, "min": big, "max": big, "median": big} self.assertEqual(types["overall"]["gpu-zerohdr"], want) self.assertEqual(types["per_archive"]["a"]["gpu-zerohdr"], want) def test_empty_report_has_no_entries_to_carry_stats(self): with tempfile.TemporaryDirectory() as empty: with mock.patch("sys.argv", ["classify_carve.py", empty]), contextlib.redirect_stdout(io.StringIO()): carve.main() with open(os.path.join(empty, "types.json"), encoding="utf-8") as f: self.assertEqual(json.load(f), {"overall": {}, "per_archive": {}}) class MainTests(unittest.TestCase): def setUp(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.root = tmp.name def archive(self, name, rows): path = os.path.join(self.root, name) os.makedirs(path, exist_ok=True) with open(os.path.join(path, "index.json"), "w", encoding="utf-8") as f: json.dump({"files": rows}, f) return path def run_main(self): out = io.StringIO() with mock.patch("sys.argv", ["classify_carve.py", self.root]), contextlib.redirect_stdout(out): carve.main() return out.getvalue() def types(self): with open(os.path.join(self.root, "types.json"), encoding="utf-8") as f: return json.load(f) def test_counts_and_bytes_are_aggregated_per_class(self): self.archive("a", [ {"head": RBF0_HEAD, "size": 10}, {"head": "5242463000000001", "size": 30}, {"head": ZERO_HEAD, "size": 5}, {"head": "deadbeefcafebabe", "size": 7}, {"type": "bink2", "size": 3}, ]) self.run_main() types = self.types() a = types["per_archive"]["a"] self.assertEqual(counts(a["rbf0"]), {"files": 2, "bytes": 40}) self.assertEqual(counts(a["gpu-zerohdr"]), {"files": 1, "bytes": 5}) self.assertEqual(counts(a["other:deadbeef"]), {"files": 1, "bytes": 7}) self.assertEqual(counts(a["bink2"]), {"files": 1, "bytes": 3}) self.assertEqual(counts(types["overall"]["rbf0"]), {"files": 2, "bytes": 40}) def test_overall_sums_across_archives(self): self.archive("a", [{"head": RBF0_HEAD, "size": 10}]) self.archive("b", [{"head": RBF0_HEAD, "size": 25}]) self.run_main() types = self.types() self.assertEqual(counts(types["overall"]["rbf0"]), {"files": 2, "bytes": 35}) self.assertEqual(counts(types["per_archive"]["a"]["rbf0"]), {"files": 1, "bytes": 10}) def test_archives_are_listed_in_name_order(self): self.archive("b", [{"head": RBF0_HEAD, "size": 1}]) self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() self.assertEqual(list(self.types()["per_archive"]), ["a", "b"]) def test_classes_are_ordered_by_bytes_descending(self): self.archive("a", [ {"head": RBF0_HEAD, "size": 50}, {"head": ZERO_HEAD, "size": 500}, ]) self.run_main() types = self.types() self.assertEqual(list(types["per_archive"]["a"]), ["gpu-zerohdr", "rbf0"]) self.assertEqual(list(types["overall"]), ["gpu-zerohdr", "rbf0"]) def test_directory_without_an_index_is_skipped(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) os.makedirs(os.path.join(self.root, "empty"), exist_ok=True) self.run_main() self.assertEqual(list(self.types()["per_archive"]), ["a"]) def test_stdout_summary(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}, {"head": ZERO_HEAD, "size": 2}]) out = self.run_main() self.assertIn("GiB", out) self.assertIn("rbf0", out) self.assertIn("2 classes in total", out) def test_empty_root(self): out = self.run_main() self.assertEqual(self.types(), {"overall": {}, "per_archive": {}}) self.assertIn("0 classes in total", out) def test_types_json_uses_one_space_indent(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() with open(os.path.join(self.root, "types.json"), encoding="utf-8") as f: text = f.read() self.assertTrue(text.startswith('{\n "overall": {')) def test_invalid_index_names_its_path_and_preserves_previous_report(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() before = self.types() index = os.path.join(self.archive("b", []), "index.json") for data, message in ( ("{", "invalid JSON"), ('{"files": {}}', '"files" list'), ('{"files": [{"head": "00"}]}', "row 1"), ('{"files": [{"size": 12, "head": 42}]}', "row 1"), ): with self.subTest(data=data): with open(index, "w", encoding="utf-8") as f: f.write(data) proc = subprocess.run( [sys.executable, "-I", os.path.join(ROOT, "tools", "classify_carve.py"), self.root], capture_output=True, text=True, ) self.assertEqual(proc.returncode, 2) self.assertIn(index, proc.stderr) self.assertIn(message, proc.stderr) self.assertNotIn("Traceback", proc.stderr) self.assertEqual(self.types(), before) def test_bad_arguments_and_roots_are_one_line(self): tool = os.path.join(ROOT, "tools", "classify_carve.py") a_file = os.path.join(self.root, "index.json") with open(a_file, "w", encoding="utf-8") as f: f.write("{}") for args, message in (([], "usage"), ([self.root, "extra"], "usage"), ([os.path.join(self.root, "missing")], "does not exist"), ([a_file], "is not a folder")): with self.subTest(args=[os.path.basename(a) for a in args]): proc = subprocess.run([sys.executable, "-I", tool, *args], capture_output=True, text=True) self.assertEqual(proc.returncode, 2) self.assertIn(message, proc.stderr) self.assertNotIn("Traceback", proc.stderr) self.assertEqual(proc.stdout, "") self.assertEqual(sorted(os.listdir(self.root)), ["index.json"]) # no types.json was written def test_types_json_directory_is_one_error_line(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) report = os.path.join(self.root, "types.json") os.mkdir(report) proc = subprocess.run( [sys.executable, "-I", os.path.join(ROOT, "tools", "classify_carve.py"), self.root], capture_output=True, text=True, ) self.assertEqual(proc.returncode, 2) self.assertIn("types.json", proc.stderr) self.assertIn("is a directory", proc.stderr) self.assertIn("remove or rename", proc.stderr) self.assertIn("before rerun", proc.stderr) self.assertNotIn("Traceback", proc.stderr) self.assertEqual(proc.stdout, "") self.assertEqual(os.listdir(report), []) self.assertEqual([n for n in os.listdir(self.root) if n.startswith(".types-")], []) def test_help_prints_usage_and_writes_nothing(self): tool = os.path.join(ROOT, "tools", "classify_carve.py") for flag in ("-h", "--help"): with self.subTest(flag=flag): proc = subprocess.run([sys.executable, "-I", tool, flag], capture_output=True, text=True, cwd=self.root) self.assertEqual(proc.returncode, 0) self.assertIn("usage", proc.stdout) # The printed line is meant to be copied from the repository root, so it names the tool's folder. self.assertIn("python -I tools/classify_carve.py", proc.stdout) self.assertEqual(proc.stderr, "") self.assertEqual(os.listdir(self.root), []) # nothing read or written def test_failed_write_preserves_previous_report(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() before = self.types() files = sorted(os.listdir(self.root)) def fail_dump(_data, handle, **_kwargs): handle.write("{") raise OSError("synthetic write failure") with mock.patch.object(carve.json, "dump", side_effect=fail_dump): with self.assertRaisesRegex(OSError, "write failure"): self.run_main() self.assertEqual(self.types(), before) self.assertEqual(sorted(os.listdir(self.root)), files) def test_recursion_error_in_index_is_one_error_line(self): index = os.path.join(self.archive("a", []), "index.json") err = io.StringIO() with mock.patch.object(carve.json, "load", side_effect=RecursionError("maximum recursion depth exceeded")): with mock.patch.object(sys, "argv", ["classify_carve.py", self.root]): with contextlib.redirect_stderr(err): rc = carve.main() self.assertEqual(rc, 2) self.assertIn(index, err.getvalue()) self.assertIn("invalid JSON", err.getvalue()) def test_unreadable_root_is_one_error_line(self): err = io.StringIO() with mock.patch.object(carve.os, "listdir", side_effect=PermissionError("denied")): with mock.patch.object(sys, "argv", ["classify_carve.py", self.root]): with contextlib.redirect_stderr(err): rc = carve.main() self.assertEqual(rc, 2) self.assertIn(self.root, err.getvalue()) def test_symlinked_types_json_updates_target_in_place(self): self.archive("a", [{"head": RBF0_HEAD, "size": 42}]) with tempfile.TemporaryDirectory() as other: target = os.path.join(other, "real_types.json") with open(target, "w", encoding="utf-8") as f: f.write("{}") link = os.path.join(self.root, "types.json") try: os.symlink(target, link) except OSError as exc: self.skipTest(f"symlink unavailable: {exc}") self.run_main() self.assertTrue(os.path.islink(link)) with open(target, encoding="utf-8") as f: data = json.load(f) self.assertEqual(counts(data["overall"]["rbf0"]), {"files": 1, "bytes": 42}) def _restore_writable_root(self): for entry in os.listdir(self.root): try: os.chmod(os.path.join(self.root, entry), 0o600) except OSError: pass def test_failed_replace_over_readonly_report_cleans_staging_file(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() before = self.types() dest = os.path.join(self.root, "types.json") os.chmod(dest, 0o444) self.addCleanup(self._restore_writable_root) with mock.patch.object(carve.os, "replace", side_effect=PermissionError("replace failed")): with self.assertRaisesRegex(PermissionError, "replace failed"): self.run_main() self.assertEqual(self.types(), before) self.assertEqual([n for n in os.listdir(self.root) if n.startswith(".types-")], []) @unittest.skipUnless(os.name == "nt", "Windows enforces read-only file replace and unlink") def test_windows_readonly_report_failure_cleans_staging_file(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) self.run_main() before = self.types() dest = os.path.join(self.root, "types.json") os.chmod(dest, 0o444) self.addCleanup(self._restore_writable_root) with self.assertRaises(PermissionError): self.run_main() self.assertEqual(self.types(), before) self.assertEqual([n for n in os.listdir(self.root) if n.startswith(".types-")], []) def test_fdopen_failure_closes_descriptor_and_cleans_staging_file(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) real_mkstemp = carve.tempfile.mkstemp real_close = os.close created = [] closed = [] def spy_mkstemp(*args, **kwargs): fd, temp = real_mkstemp(*args, **kwargs) created.append((fd, temp)) return fd, temp def spy_close(fd): closed.append(fd) return real_close(fd) try: with ( mock.patch.object(carve.tempfile, "mkstemp", side_effect=spy_mkstemp), mock.patch.object(carve.os, "fdopen", side_effect=OSError("fdopen failed")), mock.patch.object(carve.os, "close", side_effect=spy_close), ): with self.assertRaisesRegex(OSError, "fdopen failed"): self.run_main() self.assertEqual(len(created), 1) self.assertIn(created[0][0], closed) self.assertEqual([n for n in os.listdir(self.root) if n.startswith(".types-")], []) finally: for fd, temp in created: if fd not in closed: try: real_close(fd) except OSError: pass try: os.unlink(temp) except OSError: pass def test_main_accepts_explicit_argv_and_writes_trailing_lf_newline(self): self.archive("a", [{"head": "DEADBEEFCAFEBABE", "size": 9}]) with contextlib.redirect_stdout(io.StringIO()): rc = carve.main([self.root]) self.assertEqual(rc, 0) with open(os.path.join(self.root, "types.json"), "rb") as f: raw = f.read() self.assertTrue(raw.endswith(b"\n")) self.assertNotIn(b"\r\n", raw) self.assertEqual(self.types()["overall"]["other:deadbeef"]["files"], 1) def test_resolved_report_directory_or_non_dir_parent_stops_before_scanning(self): self.archive("a", [{"head": RBF0_HEAD, "size": 1}]) existing_dir = os.path.join(self.root, "existing_dir") existing_file = os.path.join(self.root, "existing_file") os.mkdir(existing_dir) with open(existing_file, "wb") as f: f.write(b"x") for resolved in (existing_dir, os.path.join(existing_file, "types.json")): with self.subTest(resolved=os.path.basename(resolved)): err = io.StringIO() with mock.patch.object(carve.os.path, "realpath", return_value=resolved), \ mock.patch.object(carve.os, "listdir", side_effect=AssertionError("scanned unexpectedly")), \ contextlib.redirect_stderr(err): rc = carve.main([self.root]) self.assertEqual(rc, 2) self.assertNotEqual(err.getvalue().strip(), "") if __name__ == "__main__": unittest.main()