vomebook commited on
Commit ·
b2474bc
1
Parent(s): e3b39b1
Test synthetic Reader catalog identity and integrity
Browse files27 unmodified lines28293031323329 unmodified lines6364656667686 unmodified lines75767778798027 unmodified linesreturn pointer, rawclass CatalogTests(unittest.TestCase):def test_verified_catalog_projection_preserves_other_formats(self):pointer, raw = fixture()29 unmodified lineswith self.subTest(generation=invalid), self.assertRaises(ValueError):v3.catalog_path(invalid)class Response:def __init__(self, raw, status=200):6 unmodified linesclass CatalogAsyncTests(unittest.IsolatedAsyncioTestCase):async def test_base_and_catalog_refresh_overlap_without_partial_publication(self):pointer,raw=fixture();catalog=v3.decode_catalog(pointer,raw)packed=gzip.compress(json.dumps({'v':1,'f':{'other':{'s':4}}}).encode())27 unmodified lines282930313233343536373839404142434429 unmodified lines74757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321336 unmodified lines14014114214314414514614714814915015115215315415515615715815916016116216327 unmodified linesreturn pointer, rawdef repack_catalog(catalog, *, generation=None):"""Rebuild a self-consistent synthetic pointer after changing catalog contents."""body = {key: value for key, value in catalog.items() if key != "generation"}encode = lambda value: json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()generation = generation or hashlib.sha256(encode(body)).hexdigest()raw = encode({**body, "generation": generation})return {"version": 1, "kind": "reader-reading-pointer", "generation": generation,"catalog": {"bucket": v3.BUCKET, "path": v3.catalog_path(generation), "role": "runtime","bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest()}}, rawclass CatalogTests(unittest.TestCase):def test_verified_catalog_projection_preserves_other_formats(self):pointer, raw = fixture()29 unmodified lineswith self.subTest(generation=invalid), self.assertRaises(ValueError):v3.catalog_path(invalid)def test_pointer_identity_and_catalog_resource_fields_fail_closed(self):pointer, raw = fixture()for field, value in (("version", 0), ("kind", "reader-reading-catalog")):with self.subTest(field=field), self.assertRaisesRegex(ValueError, "invalid v3 pointer"):v3.decode_catalog({**pointer, field: value}, raw)ref = pointer["catalog"]for field, value in (("role", "archive"), ("bytes", True), ("bytes", 0),("bytes", v3.MAX_BYTES + 1), ("sha256", "A" * 64)):with self.subTest(field=field, value=value), self.assertRaisesRegex(ValueError, "v3 catalog resource mismatch"):v3.decode_catalog({**pointer, "catalog": {**ref, field: value}}, raw)def test_self_consistent_transport_cannot_mask_invalid_catalog_identity(self):_, original = fixture()catalog = json.loads(original)for field, value in (("version", 2), ("kind", "other"), ("files", [])):pointer, raw = repack_catalog({**catalog, field: value})with self.subTest(field=field), self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):v3.decode_catalog(pointer, raw)pointer, raw = repack_catalog(catalog, generation="a" * 64)with self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):v3.decode_catalog(pointer, raw)pointer, raw = repack_catalog(catalog)changed = json.loads(raw)changed["generation"] = "b" * 64raw = json.dumps(changed, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()pointer["catalog"].update(bytes=len(raw), sha256=hashlib.sha256(raw).hexdigest())with self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):v3.decode_catalog(pointer, raw)def test_catalog_entries_reject_invalid_keys_and_reading_references(self):_, raw = fixture()original = json.loads(raw)key, entry = next(iter(original["files"].items()))for files in ({"": entry}, {key: None}, {key: []}):pointer, packed = repack_catalog({**original, "files": files})with self.subTest(files=files), self.assertRaisesRegex(ValueError, "invalid v3 source key"):v3.decode_catalog(pointer, packed)resource = entry["resource"]variants = ({**entry, "source_sha256": "A" * 64}, {**entry, "reading_generation": None},{**entry, "resource": {**resource, "bucket": "foreign/bucket"}},{**entry, "resource": {**resource, "role": "archive"}},{**entry, "resource": {**resource, "path": "https://foreign.example/reading-manifest.json"}},{**entry, "resource": {**resource, "path": resource["path"].replace("objects/aa/", "objects/bb/")}},{**entry, "resource": {**resource, "path": resource["path"].replace("reading-manifest.json", "other.json")}},{**entry, "resource": {**resource, "sha256": "x" * 64}},{**entry, "resource": {**resource, "bytes": True}},{**entry, "resource": {**resource, "bytes": 0}},)for index, invalid in enumerate(variants):pointer, packed = repack_catalog({**original, "files": {key: invalid}})with self.subTest(index=index), self.assertRaisesRegex(ValueError, "invalid v3 reading reference"):v3.decode_catalog(pointer, packed)class Response:def __init__(self, raw, status=200):6 unmodified linesclass CatalogAsyncTests(unittest.IsolatedAsyncioTestCase):async def test_corrupted_pinned_base_is_rejected_and_all_responses_released(self):_, raw = fixture()catalog = json.loads(raw)packed = gzip.compress(b'{"v":1,"f":{}}')digest = hashlib.sha256(packed).hexdigest()catalog["base_sidecar"] = {"bucket": v3.BUCKET, "role": "runtime", "bytes": len(packed),"sha256": digest, "path": f"reader-index/snapshots/{digest}/reader_assets.json.gz"}pointer, raw = repack_catalog(catalog)corrupted = bytes([packed[0] ^ 1]) + packed[1:]responses = [Response(json.dumps(pointer).encode()), Response(raw), Response(corrupted)]session = SimpleNamespace(get=AsyncMock(side_effect=responses))with patch('backend.reader_assets.decode_reader_assets') as decode:with self.assertRaisesRegex(ValueError, 'unified base sidecar checksum mismatch'):await v3.load_reading_catalog(session)decode.assert_not_called()self.assertEqual(session.get.await_count, 3)self.assertTrue(all(response.released for response in responses))async def test_base_and_catalog_refresh_overlap_without_partial_publication(self):pointer,raw=fixture();catalog=v3.decode_catalog(pointer,raw)packed=gzip.compress(json.dumps({'v':1,'f':{'other':{'s':4}}}).encode())
tests/test_reader_v3_catalog.py+83