upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/test_classify_carve.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_classify_carve.py')
-rw-r--r--tests/test_classify_carve.py152
1 files changed, 152 insertions, 0 deletions
diff --git a/tests/test_classify_carve.py b/tests/test_classify_carve.py
new file mode 100644
index 000000000..b0af5a543
--- /dev/null
+++ b/tests/test_classify_carve.py
@@ -0,0 +1,152 @@
1"""Tests for tools/classify_carve.py, built from synthetic carve indexes (no game data).
2
3 python -I tests/test_classify_carve.py
4"""
5import contextlib
6import importlib.util
7import io
8import json
9import os
10import tempfile
11import unittest
12from unittest import mock
13
14ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
15
16ZERO_HEAD = "00" * 16
17RBF0_HEAD = "5242463000000000"
18RESPAGE_HEAD = "42400000deadbeef"
19
20
21def load_module():
22 path = os.path.join(ROOT, "tools", "classify_carve.py")
23 spec = importlib.util.spec_from_file_location("classify_carve_under_test", path)
24 module = importlib.util.module_from_spec(spec)
25 spec.loader.exec_module(module)
26 return module
27
28
29carve = load_module()
30
31
32class ClassifyTests(unittest.TestCase):
33 def test_bink2_rows_do_not_need_a_head(self):
34 # rpf9_carve writes bink2 rows with dimensions and no head field.
35 self.assertEqual(carve.classify({"type": "bink2", "size": 12}), "bink2")
36
37 def test_bink2_wins_over_a_signature(self):
38 self.assertEqual(carve.classify({"type": "bink2", "head": RBF0_HEAD, "size": 1}), "bink2")
39
40 def test_rbf0_metadata(self):
41 self.assertEqual(carve.classify({"head": RBF0_HEAD, "size": 1}), "rbf0")
42
43 def test_resource_page(self):
44 self.assertEqual(carve.classify({"head": RESPAGE_HEAD, "size": 1}), "respage")
45
46 def test_gpu_zero_header_needs_sixteen_zero_bytes(self):
47 self.assertEqual(carve.classify({"head": ZERO_HEAD, "size": 1}), "gpu-zerohdr")
48 # Fifteen zero bytes are not enough, and the fallback names four bytes.
49 self.assertEqual(carve.classify({"head": "00" * 15, "size": 1}), "other:00000000")
50
51 def test_other_is_keyed_by_the_first_four_bytes(self):
52 self.assertEqual(carve.classify({"head": "deadbeefcafebabe", "size": 1}), "other:deadbeef")
53
54 def test_short_head_is_used_as_is(self):
55 self.assertEqual(carve.classify({"head": "abcd", "size": 1}), "other:abcd")
56
57 def test_signature_must_be_at_the_start(self):
58 self.assertEqual(carve.classify({"head": "ff" + RBF0_HEAD, "size": 1}), "other:ff524246")
59
60
61class MainTests(unittest.TestCase):
62 def setUp(self):
63 tmp = tempfile.TemporaryDirectory()
64 self.addCleanup(tmp.cleanup)
65 self.root = tmp.name
66
67 def archive(self, name, rows):
68 path = os.path.join(self.root, name)
69 os.makedirs(path, exist_ok=True)
70 with open(os.path.join(path, "index.json"), "w", encoding="utf-8") as f:
71 json.dump({"files": rows}, f)
72 return path
73
74 def run_main(self):
75 out = io.StringIO()
76 with mock.patch("sys.argv", ["classify_carve.py", self.root]), contextlib.redirect_stdout(out):
77 carve.main()
78 return out.getvalue()
79
80 def types(self):
81 with open(os.path.join(self.root, "types.json"), encoding="utf-8") as f:
82 return json.load(f)
83
84 def test_counts_and_bytes_are_aggregated_per_class(self):
85 self.archive("a", [
86 {"head": RBF0_HEAD, "size": 10},
87 {"head": "5242463000000001", "size": 30},
88 {"head": ZERO_HEAD, "size": 5},
89 {"head": "deadbeefcafebabe", "size": 7},
90 {"type": "bink2", "size": 3},
91 ])
92 self.run_main()
93 types = self.types()
94 a = types["per_archive"]["a"]
95 self.assertEqual(a["rbf0"], {"files": 2, "bytes": 40})
96 self.assertEqual(a["gpu-zerohdr"], {"files": 1, "bytes": 5})
97 self.assertEqual(a["other:deadbeef"], {"files": 1, "bytes": 7})
98 self.assertEqual(a["bink2"], {"files": 1, "bytes": 3})
99 self.assertEqual(types["overall"]["rbf0"], {"files": 2, "bytes": 40})
100
101 def test_overall_sums_across_archives(self):
102 self.archive("a", [{"head": RBF0_HEAD, "size": 10}])
103 self.archive("b", [{"head": RBF0_HEAD, "size": 25}])
104 self.run_main()
105 types = self.types()
106 self.assertEqual(types["overall"]["rbf0"], {"files": 2, "bytes": 35})
107 self.assertEqual(types["per_archive"]["a"]["rbf0"], {"files": 1, "bytes": 10})
108
109 def test_archives_are_listed_in_name_order(self):
110 self.archive("b", [{"head": RBF0_HEAD, "size": 1}])
111 self.archive("a", [{"head": RBF0_HEAD, "size": 1}])
112 self.run_main()
113 self.assertEqual(list(self.types()["per_archive"]), ["a", "b"])
114
115 def test_classes_are_ordered_by_bytes_descending(self):
116 self.archive("a", [
117 {"head": RBF0_HEAD, "size": 50},
118 {"head": ZERO_HEAD, "size": 500},
119 ])
120 self.run_main()
121 types = self.types()
122 self.assertEqual(list(types["per_archive"]["a"]), ["gpu-zerohdr", "rbf0"])
123 self.assertEqual(list(types["overall"]), ["gpu-zerohdr", "rbf0"])
124
125 def test_directory_without_an_index_is_skipped(self):
126 self.archive("a", [{"head": RBF0_HEAD, "size": 1}])
127 os.makedirs(os.path.join(self.root, "empty"), exist_ok=True)
128 self.run_main()
129 self.assertEqual(list(self.types()["per_archive"]), ["a"])
130
131 def test_stdout_summary(self):
132 self.archive("a", [{"head": RBF0_HEAD, "size": 1}, {"head": ZERO_HEAD, "size": 2}])
133 out = self.run_main()
134 self.assertIn("GiB", out)
135 self.assertIn("rbf0", out)
136 self.assertIn("2 classes in total", out)
137
138 def test_empty_root(self):
139 out = self.run_main()
140 self.assertEqual(self.types(), {"overall": {}, "per_archive": {}})
141 self.assertIn("0 classes in total", out)
142
143 def test_types_json_uses_one_space_indent(self):
144 self.archive("a", [{"head": RBF0_HEAD, "size": 1}])
145 self.run_main()
146 with open(os.path.join(self.root, "types.json"), encoding="utf-8") as f:
147 text = f.read()
148 self.assertTrue(text.startswith('{\n "overall": {'))
149
150
151if __name__ == "__main__":
152 unittest.main()