upleb.uk

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

summaryrefslogtreecommitdiff
path: root/tests/test_rbf0_encode.py
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:56 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 17:03:56 -0500
commit13c8a8fed9d43f1f2b23f8febd915d023844cd09 (patch)
tree525a0643c474f66a7acd8b27af61d79f390a507e /tests/test_rbf0_encode.py
parent39f5fd3d9b70b0a8a5866d413c7d582f45461fb4 (diff)
parenta2f8aae55c22c3ba1d4cb8f8d39c5eb960543c1c (diff)
Merge pull request #3811 from gbezzuoli
Diffstat (limited to 'tests/test_rbf0_encode.py')
-rw-r--r--tests/test_rbf0_encode.py64
1 files changed, 63 insertions, 1 deletions
diff --git a/tests/test_rbf0_encode.py b/tests/test_rbf0_encode.py
index 4af69ab37..8f9d7886f 100644
--- a/tests/test_rbf0_encode.py
+++ b/tests/test_rbf0_encode.py
@@ -202,6 +202,31 @@ class Refusals(unittest.TestCase):
202 self.assertTrue(doc["tree"].get("attribute_order_violation")) 202 self.assertTrue(doc["tree"].get("attribute_order_violation"))
203 self.assertEqual(self.reason(doc=doc), "order") 203 self.assertEqual(self.reason(doc=doc), "order")
204 204
205 def test_more_attribute_records_than_the_count(self):
206 # An attribute appended by hand without raising attrs_declared would be written after the count's last
207 # attribute and decode as the element's first content value: a different tree, with status "ok".
208 doc = rbf0_tree.parse_resource(Stream().open("r", attrs=1).int("id", 1).close().bytes())
209 self.assertEqual(rbf0_encode.encode(doc), Stream().open("r", attrs=1).int("id", 1).close().bytes())
210 extra = dict(doc["tree"]["attributes"][0], value=2)
211 doc["tree"]["attributes"].append(extra)
212 self.assertEqual(self.reason(doc=doc), "value")
213 doc["tree"]["attrs_declared"] = 2 # the count raised with it: encodes, and reads back
214 back = rbf0_tree.parse_resource(rbf0_encode.encode(doc))
215 self.assertEqual((back["status"], [a["value"] for a in back["tree"]["attributes"]]), ("ok", [1, 2]))
216 self.assertEqual(back["tree"]["content"], [])
217
218 def test_fewer_attribute_records_than_the_count(self):
219 # Short of its count with no value in the content: what the decoder gives for such a resource, and exact.
220 short = Stream().open("r", attrs=2).int("id", 1).close().bytes()
221 doc = rbf0_tree.parse_resource(short)
222 self.assertEqual((doc["status"], len(doc["tree"]["attributes"]), doc["tree"]["attrs_declared"]), ("ok", 1, 2))
223 self.assertEqual(rbf0_encode.encode(doc), short)
224 # A value moved by hand from the attributes into the content would decode as an attribute again.
225 doc = rbf0_tree.parse_resource(Stream().open("r", attrs=2).int("id", 1).int("n", 2).close().bytes())
226 tree = doc["tree"]
227 tree["content"].append(dict(tree["attributes"].pop(), kind="value"))
228 self.assertEqual(self.reason(doc=doc), "order")
229
205 def test_descriptor_out_of_order(self): 230 def test_descriptor_out_of_order(self):
206 doc = rbf0_tree.parse_resource(every_record(True)) 231 doc = rbf0_tree.parse_resource(every_record(True))
207 doc["tree"]["attributes"][0]["descriptor"] = 40 232 doc["tree"]["attributes"][0]["descriptor"] = 40
@@ -275,11 +300,48 @@ class Cli(unittest.TestCase):
275 self.put("00020000.bin", every_record(False)) 300 self.put("00020000.bin", every_record(False))
276 self.put("00030000.bin", Stream().open("r").float("f", math.nan).close().bytes()) 301 self.put("00030000.bin", Stream().open("r").float("f", math.nan).close().bytes())
277 code, out = self.run_main("--check", self.carve) 302 code, out = self.run_main("--check", self.carve)
278 self.assertEqual(code, 0, out) 303 self.assertEqual(code, 1, out) # a refused resource is not a verified one
279 self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out) 304 self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out)
280 self.assertIn("refused (nan): 1", out) 305 self.assertIn("refused (nan): 1", out)
281 self.assertIn("narrow-32: 1 of 1 exact", out) 306 self.assertIn("narrow-32: 1 of 1 exact", out)
282 307
308 def test_check_exit_status_is_zero_only_when_every_resource_is_exact(self):
309 self.put("00001000.bin", every_record(True))
310 self.put("00002000.bin", every_record(False))
311 code, out = self.run_main("--check", self.carve)
312 self.assertEqual(code, 0, out)
313 self.assertIn("2 resource(s): 2 exact, 0 different, 0 refused", out)
314 self.put("00003000.bin", Stream().open("r").float("f", math.nan).close().bytes())
315 code, out = self.run_main("--check", self.carve)
316 self.assertEqual(code, 1, out)
317 self.assertIn("3 resource(s): 2 exact, 0 different, 1 refused", out)
318 # Nothing but refusals: a round trip that verified no resource must not read as success.
319 only = os.path.join(self.carve, "00003000.bin")
320 code, out = self.run_main("--check", only)
321 self.assertEqual(code, 1, out)
322 self.assertIn("1 resource(s): 0 exact, 0 different, 1 refused", out)
323
324 def test_check_compares_the_joined_bytes_with_the_recorded_size(self):
325 blob = every_record(True)
326 doc = rbf0_tree.parse_resource(blob)
327 self.assertEqual(doc["size"], len(blob))
328 real = rbf0_encode.resources
329 self.addCleanup(setattr, rbf0_encode, "resources", real)
330 rbf0_encode.resources = lambda source, limit=None: iter([(doc, blob)])
331 self.assertEqual(rbf0_encode.check("unused")["exact"], 1)
332 rbf0_encode.resources = lambda source, limit=None: iter([({**doc, "size": len(blob) - 1}, blob)])
333 counts = rbf0_encode.check("unused")
334 self.assertEqual((counts["exact"], counts["different"]), (0, 1))
335
336 def test_two_carved_streams_of_one_name_are_refused(self):
337 blob = every_record(True)
338 row = {"offset": 0, "stored": len(blob), "size": len(blob), "head": blob[:8], "name": "same.bin"}
339 real = rbf0_tree._rows
340 self.addCleanup(setattr, rbf0_tree, "_rows", real)
341 rbf0_tree._rows = lambda source: ([dict(row), dict(row, offset=1)], lambda r: blob)
342 with self.assertRaises(rbf0_tree.SelectionError):
343 list(rbf0_encode.resources("unused"))
344
283 def test_encode_report_to_new_directory(self): 345 def test_encode_report_to_new_directory(self):
284 self.put("00000100.bin", every_record(True)) 346 self.put("00000100.bin", every_record(True))
285 docs, summary = rbf0_tree.decode_source(self.carve) 347 docs, summary = rbf0_tree.decode_source(self.carve)