# SPDX-License-Identifier: MIT import hashlib import json from pathlib import Path import struct import unittest from runner import fixtures, framing, response, validate_frame class FramingTests(unittest.TestCase): def test_strict_capture_guard(self): for length in range(40): with self.subTest(length=length): with self.assertRaises(ValueError): validate_frame(response()[:length]) validate_frame(response()) with self.assertRaises(ValueError): validate_frame(response() + b"\\x00") def test_published_observations(self): for filename in ("results.json", "results-pinned.json"): path = Path(__file__).with_name(filename) if not path.exists(): self.skipTest("Both published results are required") records = json.loads(path.read_text())["cases"] self.assertEqual([r["id"] for r in records], [name for name, _ in fixtures()]) for record, (_, data) in zip(records, fixtures()): self.assertEqual(record["framing"], framing(data)) self.assertEqual(record["input_hex"], data.hex()) shortened = records[:40] self.assertEqual(sum(r["exit_code"] == 0 for r in shortened), 36) for length in (8, 24, 40): record = records[length] self.assertEqual(record["exit_code"], 0) self.assertEqual(record["stdout"].count("LBA:"), 2) self.assertIn("provisioning status: 15", records[44]["stdout"].lower()) def test_wire_fixture(self): expected = bytes.fromhex( "00000024 00000007 0000000000000000 00000010 01000000 " "0000000000000010 00000010 03000000") self.assertEqual(response(), expected) def test_all_prefixes_against_offsets(self): data = response() for n in range(41): with self.subTest(n=n): expected = sum(n >= end for end in (24, 40)) self.assertEqual(framing(data[:n])["complete_descriptors"], expected) self.assertEqual(framing(data[:n])["complete_frame"], n == 40) def test_unknown_status_is_not_deallocated(self): data = response(status=15) self.assertEqual(data[20] & 15, 15) self.assertEqual(struct.unpack(">I", data[16:20])[0], 16) def test_completion_and_rtp_are_separate(self): for completion in range(4): for rtp in range(2): result = framing(response(completion=completion, rtp=rtp)) self.assertTrue(result["complete_frame"]) self.assertEqual(result["completion"], completion) self.assertEqual(result["rtp"], rtp) def test_fixture_ids_are_unique(self): cases = fixtures() self.assertEqual(len(cases), 49) self.assertEqual(len({name for name, _ in cases}), 49) def test_published_evidence_matches_inputs(self): path = Path(__file__).with_name("results.json") if not path.exists(): self.skipTest("Generate results.json to validate stored observations") records = json.loads(path.read_text())["cases"] self.assertEqual(len(records), 49) for record, (name, data) in zip(records, fixtures()): self.assertEqual(record["id"], name) self.assertEqual(record["input_hex"], data.hex()) self.assertEqual(record["input_sha256"], hashlib.sha256(data).hexdigest()) self.assertEqual(record["framing"], framing(data)) if __name__ == "__main__": unittest.main()