diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 17:01:48 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 17:01:48 -0500 |
| commit | ade14c9fa79a8fc5874ecd8722de59d6ca9384b8 (patch) | |
| tree | a090ac4a67cc94951741fce32a47153274af97c7 /tests/test_rbf0_encode.py | |
| parent | d51752aa2db5892ae91ea3dae10760512a9e4abd (diff) | |
| parent | 0264ef1e5f80815217ad99a1ba4f6d03c4d51443 (diff) | |
Merge pull request #3384 from Shrey Ratna
Diffstat (limited to 'tests/test_rbf0_encode.py')
| -rw-r--r-- | tests/test_rbf0_encode.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/test_rbf0_encode.py b/tests/test_rbf0_encode.py index ee44e7923..4af69ab37 100644 --- a/tests/test_rbf0_encode.py +++ b/tests/test_rbf0_encode.py | |||
| @@ -9,6 +9,7 @@ import json | |||
| 9 | import math | 9 | import math |
| 10 | import os | 10 | import os |
| 11 | import struct | 11 | import struct |
| 12 | import subprocess | ||
| 12 | import sys | 13 | import sys |
| 13 | import tempfile | 14 | import tempfile |
| 14 | import unittest | 15 | import unittest |
| @@ -101,7 +102,33 @@ def roundtrip(blob): | |||
| 101 | return rbf0_encode.encode(doc) | 102 | return rbf0_encode.encode(doc) |
| 102 | 103 | ||
| 103 | 104 | ||
| 105 | def edited_unicode_doc(field, value, wide=True): | ||
| 106 | doc = rbf0_tree.parse_resource(Stream(wide).open("root").string("label", b"x").text(b"t").close().bytes()) | ||
| 107 | if field == "descriptor": | ||
| 108 | doc["tree"]["name"] = value | ||
| 109 | doc["descriptors"][0]["name"] = value | ||
| 110 | elif field == "string": | ||
| 111 | node = doc["tree"]["content"][0] | ||
| 112 | node["value"] = value | ||
| 113 | node.pop("byte_length", None) | ||
| 114 | else: | ||
| 115 | node = doc["tree"]["content"][1] | ||
| 116 | node["text"] = value | ||
| 117 | node.pop("length", None) | ||
| 118 | return doc | ||
| 119 | |||
| 120 | |||
| 104 | class RoundTrip(unittest.TestCase): | 121 | class RoundTrip(unittest.TestCase): |
| 122 | def test_non_bmp_unicode_survives_json_and_encoding(self): | ||
| 123 | for wide in (True, False): | ||
| 124 | for field in ("descriptor", "string", "text"): | ||
| 125 | with self.subTest(wide=wide, field=field): | ||
| 126 | doc = edited_unicode_doc(field, "caf\u00e9 \U0001f9ea", wide) | ||
| 127 | doc = json.loads(json.dumps(doc)) # a valid escaped surrogate pair becomes one scalar | ||
| 128 | encoded = rbf0_encode.encode(doc) | ||
| 129 | self.assertIn("caf\u00e9 \U0001f9ea".encode("utf-8"), encoded) | ||
| 130 | self.assertEqual(roundtrip(encoded), encoded) | ||
| 131 | |||
| 105 | def test_every_record_wide_and_narrow(self): | 132 | def test_every_record_wide_and_narrow(self): |
| 106 | for wide in (True, False): | 133 | for wide in (True, False): |
| 107 | blob = every_record(wide) | 134 | blob = every_record(wide) |
| @@ -156,6 +183,14 @@ class Refusals(unittest.TestCase): | |||
| 156 | bad_name = b"RBF0" + PROLOGUE + bytes((0, 0)) + struct.pack("<H", 2) + b"\xc3\x28" + bytes(6) + b"\xff\xff" | 183 | bad_name = b"RBF0" + PROLOGUE + bytes((0, 0)) + struct.pack("<H", 2) + b"\xc3\x28" + bytes(6) + b"\xff\xff" |
| 157 | self.assertEqual(self.reason(bad_name), "utf8") | 184 | self.assertEqual(self.reason(bad_name), "utf8") |
| 158 | 185 | ||
| 186 | def test_unpaired_surrogates_in_edited_json_fields(self): | ||
| 187 | for wide in (True, False): | ||
| 188 | for field in ("descriptor", "string", "text"): | ||
| 189 | for value in ("\ud800", "\udfff", "prefix\ud800suffix"): | ||
| 190 | with self.subTest(wide=wide, field=field, value=repr(value)): | ||
| 191 | doc = json.loads(json.dumps(edited_unicode_doc(field, value, wide))) | ||
| 192 | self.assertEqual(self.reason(doc=doc), "utf8") | ||
| 193 | |||
| 159 | def test_incomplete_parses(self): | 194 | def test_incomplete_parses(self): |
| 160 | whole = Stream().open("r").int("i", 1).close().bytes() | 195 | whole = Stream().open("r").int("i", 1).close().bytes() |
| 161 | self.assertEqual(self.reason(whole[:-3]), "status") # truncated | 196 | self.assertEqual(self.reason(whole[:-3]), "status") # truncated |
| @@ -259,6 +294,28 @@ class Cli(unittest.TestCase): | |||
| 259 | with self.assertRaises(SystemExit): # never into a non-empty directory | 294 | with self.assertRaises(SystemExit): # never into a non-empty directory |
| 260 | self.run_main(report, out_dir) | 295 | self.run_main(report, out_dir) |
| 261 | 296 | ||
| 297 | def test_cli_refuses_surrogates_and_continues_with_valid_resources(self): | ||
| 298 | good = every_record(True) | ||
| 299 | docs = [rbf0_tree.parse_resource(good)] | ||
| 300 | docs += [edited_unicode_doc(field, "\ud800") for field in ("descriptor", "string", "text")] | ||
| 301 | docs.append(rbf0_tree.parse_resource(good)) | ||
| 302 | for i, doc in enumerate(docs): | ||
| 303 | doc["offset"] = 0x100 + i | ||
| 304 | report = os.path.join(self.tmp.name, "surrogates.json") | ||
| 305 | with open(report, "w", encoding="utf-8", newline="\n") as f: | ||
| 306 | json.dump({"tool": "rbf0_tree", "resources": docs}, f) | ||
| 307 | out_dir = os.path.join(self.tmp.name, "surrogate-out") | ||
| 308 | result = subprocess.run([sys.executable, "-I", os.path.join(ROOT, "tools", "rbf0_encode.py"), | ||
| 309 | report, out_dir], capture_output=True, text=True) | ||
| 310 | self.assertEqual(result.returncode, 0, result.stderr) | ||
| 311 | self.assertIn("2 resource(s) written, 3 refused", result.stdout) | ||
| 312 | self.assertEqual(result.stdout.count("utf8:"), 3) | ||
| 313 | self.assertEqual(result.stderr, "") | ||
| 314 | self.assertEqual(sorted(os.listdir(out_dir)), ["00000100.rbf", "00000104.rbf"]) | ||
| 315 | for name in os.listdir(out_dir): | ||
| 316 | with open(os.path.join(out_dir, name), "rb") as f: | ||
| 317 | self.assertEqual(f.read(), good) | ||
| 318 | |||
| 262 | def test_argument_errors(self): | 319 | def test_argument_errors(self): |
| 263 | for argv in ([], ["--check", self.carve, "extra"], ["--check", self.carve, "--limit", "0"], | 320 | for argv in ([], ["--check", self.carve, "extra"], ["--check", self.carve, "--limit", "0"], |
| 264 | ["only-report.json"], ["--check", os.path.join(self.tmp.name, "missing")]): | 321 | ["only-report.json"], ["--check", os.path.join(self.tmp.name, "missing")]): |