diff options
| author | Narek Shaghoyan <narekshagoyan7@gmail.com> | 2026-11-22 17:10:56 +0000 |
|---|---|---|
| committer | Narek Shaghoyan <narekshagoyan7@gmail.com> | 2026-11-22 17:10:56 +0000 |
| commit | f616da931f782dc9c753b2a9d2fe4fbaff831f9a (patch) | |
| tree | 4d5c275cffddce4a83c4ebc8999c6a4296f1cb83 /tests/test_rbf0_encode.py | |
| parent | 49475796c226c8511b9470bf97a4daf57b8af323 (diff) | |
rbf0_encode: fail closed on malformed reports, bool indices, direct NaN, and float32 overflow
Harden tools/rbf0_encode.py so edited/malformed trees and JSON reports raise clean EncodeError / SystemExit messages instead of tracebacks: reject bool for descriptor/attrs_declared, catch direct math.nan and struct.pack OverflowError in float_bits(), reject non-dict documents and non-str text, and validate report JSON/shape/offsets/OSError in write_all(). Synthetic tests added in tests/test_rbf0_encode.py.
Diffstat (limited to 'tests/test_rbf0_encode.py')
| -rw-r--r-- | tests/test_rbf0_encode.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/test_rbf0_encode.py b/tests/test_rbf0_encode.py index 0ccf28192..482af7c36 100644 --- a/tests/test_rbf0_encode.py +++ b/tests/test_rbf0_encode.py | |||
| @@ -182,6 +182,27 @@ class Refusals(unittest.TestCase): | |||
| 182 | doc["tree"]["content"][0]["value"] = 2**32 | 182 | doc["tree"]["content"][0]["value"] = 2**32 |
| 183 | self.assertEqual(self.reason(doc=doc), "value") | 183 | self.assertEqual(self.reason(doc=doc), "value") |
| 184 | 184 | ||
| 185 | def test_float32_overflow_and_direct_nan(self): | ||
| 186 | doc = rbf0_tree.parse_resource(Stream(False).open("r").float("f", 1.5).close().bytes()) | ||
| 187 | doc["tree"]["content"][0]["value"] = 1e40 | ||
| 188 | self.assertEqual(self.reason(doc=doc), "value") | ||
| 189 | doc["tree"]["content"][0]["value"] = math.nan | ||
| 190 | self.assertEqual(self.reason(doc=doc), "nan") | ||
| 191 | |||
| 192 | def test_bool_rejected_for_descriptor_and_attrs_declared(self): | ||
| 193 | doc = rbf0_tree.parse_resource(every_record(True)) | ||
| 194 | doc["tree"]["attrs_declared"] = True | ||
| 195 | self.assertEqual(self.reason(doc=doc), "value") | ||
| 196 | doc = rbf0_tree.parse_resource(every_record(True)) | ||
| 197 | doc["tree"]["attributes"][0]["descriptor"] = True | ||
| 198 | self.assertEqual(self.reason(doc=doc), "descriptor") | ||
| 199 | |||
| 200 | def test_non_dict_doc_and_non_str_text(self): | ||
| 201 | self.assertEqual(self.reason(doc="not-a-dict"), "value") | ||
| 202 | doc = rbf0_tree.parse_resource(Stream(True).open("r").text(b"hi\x00").close().bytes()) | ||
| 203 | doc["tree"]["content"][0]["text"] = 123 | ||
| 204 | self.assertEqual(self.reason(doc=doc), "value") | ||
| 205 | |||
| 185 | 206 | ||
| 186 | class Cli(unittest.TestCase): | 207 | class Cli(unittest.TestCase): |
| 187 | def setUp(self): | 208 | def setUp(self): |
| @@ -236,6 +257,33 @@ class Cli(unittest.TestCase): | |||
| 236 | with self.subTest(argv=argv), self.assertRaises(SystemExit), redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): | 257 | with self.subTest(argv=argv), self.assertRaises(SystemExit), redirect_stdout(io.StringIO()), redirect_stderr(io.StringIO()): |
| 237 | rbf0_encode.main(argv) | 258 | rbf0_encode.main(argv) |
| 238 | 259 | ||
| 260 | def test_malformed_reports_raise_clean_system_exit(self): | ||
| 261 | cases = { | ||
| 262 | "bad-json.json": "{not valid json", | ||
| 263 | "list-root.json": "[]", | ||
| 264 | "bad-resources.json": json.dumps({"tool": "rbf0_tree", "resources": "bad"}), | ||
| 265 | } | ||
| 266 | for filename, content in cases.items(): | ||
| 267 | report = os.path.join(self.tmp.name, filename) | ||
| 268 | with open(report, "w", encoding="utf-8", newline="\n") as f: | ||
| 269 | f.write(content) | ||
| 270 | out_dir = os.path.join(self.tmp.name, "out-" + filename) | ||
| 271 | with self.subTest(filename=filename), self.assertRaises(SystemExit): | ||
| 272 | self.run_main(report, out_dir) | ||
| 273 | self.assertFalse(os.path.exists(out_dir)) | ||
| 274 | |||
| 275 | def test_write_all_handles_non_int_offset_and_non_dict_resource(self): | ||
| 276 | doc = rbf0_tree.parse_resource(every_record(True)) | ||
| 277 | doc["offset"] = "0x100" | ||
| 278 | report = os.path.join(self.tmp.name, "mixed.json") | ||
| 279 | with open(report, "w", encoding="utf-8", newline="\n") as f: | ||
| 280 | json.dump({"tool": "rbf0_tree", "resources": [doc, "not-a-dict"]}, f) | ||
| 281 | out_dir = os.path.join(self.tmp.name, "out-mixed") | ||
| 282 | code, out = self.run_main(report, out_dir) | ||
| 283 | self.assertEqual(code, 0, out) | ||
| 284 | self.assertIn("1 resource(s) written, 1 refused", out) | ||
| 285 | self.assertTrue(os.path.isfile(os.path.join(out_dir, "00000000.rbf"))) | ||
| 286 | |||
| 239 | 287 | ||
| 240 | if __name__ == "__main__": | 288 | if __name__ == "__main__": |
| 241 | unittest.main() | 289 | unittest.main() |