diff options
| author | gbezzuoli <guilhermebezzuoli@outlook.com> | 2026-11-22 18:46:57 -0300 |
|---|---|---|
| committer | gbezzuoli <guilhermebezzuoli@outlook.com> | 2026-11-22 18:46:57 -0300 |
| commit | a2f8aae55c22c3ba1d4cb8f8d39c5eb960543c1c (patch) | |
| tree | 452b899f605e2774850f9c0e4a354f8df12808a3 /tests/test_rbf0_encode.py | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
rbf0_encode: --check fails on a refused resource; refuse attribute records the count does not cover (BUILD-1084)
Four findings by @HQasper on tools/rbf0_encode.py:
- `--check` exited 0 when resources were refused, so a round trip that verified nothing read as success.
It now exits 0 only when every resource is byte-identical, as rbf0_tree.py does for a resource that is
not ok.
- The element header count came from `attrs_declared` while every node of `attributes` was written, so an
attribute appended by hand encoded as the element's first content value: a different tree with status
ok. encode() now refuses more attribute records than the count (value), and an element short of its
count that has a value in its content, which would decode as an attribute (order). A parsed element
short of its count with no such value still round-trips exactly.
- `blob[:doc["size"]]` in resources() could not trim anything. The slice is gone and check() compares
the joined length with the recorded size; two carved streams of one name are refused instead of the
last one silently winning.
- docs/rbf0.md 9.6 lists the `value` refusals and says what `--check` exits with.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_rbf0_encode.py')
| -rw-r--r-- | tests/test_rbf0_encode.py | 64 |
1 files changed, 63 insertions, 1 deletions
diff --git a/tests/test_rbf0_encode.py b/tests/test_rbf0_encode.py index 482af7c36..fc685e3a1 100644 --- a/tests/test_rbf0_encode.py +++ b/tests/test_rbf0_encode.py | |||
| @@ -167,6 +167,31 @@ class Refusals(unittest.TestCase): | |||
| 167 | self.assertTrue(doc["tree"].get("attribute_order_violation")) | 167 | self.assertTrue(doc["tree"].get("attribute_order_violation")) |
| 168 | self.assertEqual(self.reason(doc=doc), "order") | 168 | self.assertEqual(self.reason(doc=doc), "order") |
| 169 | 169 | ||
| 170 | def test_more_attribute_records_than_the_count(self): | ||
| 171 | # An attribute appended by hand without raising attrs_declared would be written after the count's last | ||
| 172 | # attribute and decode as the element's first content value: a different tree, with status "ok". | ||
| 173 | doc = rbf0_tree.parse_resource(Stream().open("r", attrs=1).int("id", 1).close().bytes()) | ||
| 174 | self.assertEqual(rbf0_encode.encode(doc), Stream().open("r", attrs=1).int("id", 1).close().bytes()) | ||
| 175 | extra = dict(doc["tree"]["attributes"][0], value=2) | ||
| 176 | doc["tree"]["attributes"].append(extra) | ||
| 177 | self.assertEqual(self.reason(doc=doc), "value") | ||
| 178 | doc["tree"]["attrs_declared"] = 2 # the count raised with it: encodes, and reads back | ||
| 179 | back = rbf0_tree.parse_resource(rbf0_encode.encode(doc)) | ||
| 180 | self.assertEqual((back["status"], [a["value"] for a in back["tree"]["attributes"]]), ("ok", [1, 2])) | ||
| 181 | self.assertEqual(back["tree"]["content"], []) | ||
| 182 | |||
| 183 | def test_fewer_attribute_records_than_the_count(self): | ||
| 184 | # Short of its count with no value in the content: what the decoder gives for such a resource, and exact. | ||
| 185 | short = Stream().open("r", attrs=2).int("id", 1).close().bytes() | ||
| 186 | doc = rbf0_tree.parse_resource(short) | ||
| 187 | self.assertEqual((doc["status"], len(doc["tree"]["attributes"]), doc["tree"]["attrs_declared"]), ("ok", 1, 2)) | ||
| 188 | self.assertEqual(rbf0_encode.encode(doc), short) | ||
| 189 | # A value moved by hand from the attributes into the content would decode as an attribute again. | ||
| 190 | doc = rbf0_tree.parse_resource(Stream().open("r", attrs=2).int("id", 1).int("n", 2).close().bytes()) | ||
| 191 | tree = doc["tree"] | ||
| 192 | tree["content"].append(dict(tree["attributes"].pop(), kind="value")) | ||
| 193 | self.assertEqual(self.reason(doc=doc), "order") | ||
| 194 | |||
| 170 | def test_descriptor_out_of_order(self): | 195 | def test_descriptor_out_of_order(self): |
| 171 | doc = rbf0_tree.parse_resource(every_record(True)) | 196 | doc = rbf0_tree.parse_resource(every_record(True)) |
| 172 | doc["tree"]["attributes"][0]["descriptor"] = 40 | 197 | doc["tree"]["attributes"][0]["descriptor"] = 40 |
| @@ -232,11 +257,48 @@ class Cli(unittest.TestCase): | |||
| 232 | self.put("00020000.bin", every_record(False)) | 257 | self.put("00020000.bin", every_record(False)) |
| 233 | self.put("00030000.bin", Stream().open("r").float("f", math.nan).close().bytes()) | 258 | self.put("00030000.bin", Stream().open("r").float("f", math.nan).close().bytes()) |
| 234 | code, out = self.run_main("--check", self.carve) | 259 | code, out = self.run_main("--check", self.carve) |
| 235 | self.assertEqual(code, 0, out) | 260 | self.assertEqual(code, 1, out) # a refused resource is not a verified one |
| 236 | self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out) | 261 | self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out) |
| 237 | self.assertIn("refused (nan): 1", out) | 262 | self.assertIn("refused (nan): 1", out) |
| 238 | self.assertIn("narrow-32: 1 of 1 exact", out) | 263 | self.assertIn("narrow-32: 1 of 1 exact", out) |
| 239 | 264 | ||
| 265 | def test_check_exit_status_is_zero_only_when_every_resource_is_exact(self): | ||
| 266 | self.put("00001000.bin", every_record(True)) | ||
| 267 | self.put("00002000.bin", every_record(False)) | ||
| 268 | code, out = self.run_main("--check", self.carve) | ||
| 269 | self.assertEqual(code, 0, out) | ||
| 270 | self.assertIn("2 resource(s): 2 exact, 0 different, 0 refused", out) | ||
| 271 | self.put("00003000.bin", Stream().open("r").float("f", math.nan).close().bytes()) | ||
| 272 | code, out = self.run_main("--check", self.carve) | ||
| 273 | self.assertEqual(code, 1, out) | ||
| 274 | self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out) | ||
| 275 | # Nothing but refusals: a round trip that verified no resource must not read as success. | ||
| 276 | only = os.path.join(self.carve, "00003000.bin") | ||
| 277 | code, out = self.run_main("--check", only) | ||
| 278 | self.assertEqual(code, 1, out) | ||
| 279 | self.assertIn("1 resource(s): 0 exact, 0 different, 1 refused", out) | ||
| 280 | |||
| 281 | def test_check_compares_the_joined_bytes_with_the_recorded_size(self): | ||
| 282 | blob = every_record(True) | ||
| 283 | doc = rbf0_tree.parse_resource(blob) | ||
| 284 | self.assertEqual(doc["size"], len(blob)) | ||
| 285 | real = rbf0_encode.resources | ||
| 286 | self.addCleanup(setattr, rbf0_encode, "resources", real) | ||
| 287 | rbf0_encode.resources = lambda source, limit=None: iter([(doc, blob)]) | ||
| 288 | self.assertEqual(rbf0_encode.check("unused")["exact"], 1) | ||
| 289 | rbf0_encode.resources = lambda source, limit=None: iter([({**doc, "size": len(blob) - 1}, blob)]) | ||
| 290 | counts = rbf0_encode.check("unused") | ||
| 291 | self.assertEqual((counts["exact"], counts["different"]), (0, 1)) | ||
| 292 | |||
| 293 | def test_two_carved_streams_of_one_name_are_refused(self): | ||
| 294 | blob = every_record(True) | ||
| 295 | row = {"offset": 0, "stored": len(blob), "size": len(blob), "head": blob[:8], "name": "same.bin"} | ||
| 296 | real = rbf0_tree._rows | ||
| 297 | self.addCleanup(setattr, rbf0_tree, "_rows", real) | ||
| 298 | rbf0_tree._rows = lambda source: ([dict(row), dict(row, offset=1)], lambda r: blob) | ||
| 299 | with self.assertRaises(rbf0_tree.SelectionError): | ||
| 300 | list(rbf0_encode.resources("unused")) | ||
| 301 | |||
| 240 | def test_encode_report_to_new_directory(self): | 302 | def test_encode_report_to_new_directory(self): |
| 241 | self.put("00000100.bin", every_record(True)) | 303 | self.put("00000100.bin", every_record(True)) |
| 242 | docs, summary = rbf0_tree.decode_source(self.carve) | 304 | docs, summary = rbf0_tree.decode_source(self.carve) |