vomebook commited on
Commit ·
b2474bc
1
Parent(s): e3b39b1
Test synthetic Reader catalog identity and integrity
Browse files
tests/test_reader_v3_catalog.py
CHANGED
|
@@ -28,6 +28,17 @@ def fixture():
|
|
| 28 |
return pointer, raw
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
class CatalogTests(unittest.TestCase):
|
| 32 |
def test_verified_catalog_projection_preserves_other_formats(self):
|
| 33 |
pointer, raw = fixture()
|
|
@@ -63,6 +74,60 @@ class CatalogTests(unittest.TestCase):
|
|
| 63 |
with self.subTest(generation=invalid), self.assertRaises(ValueError):
|
| 64 |
v3.catalog_path(invalid)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
class Response:
|
| 68 |
def __init__(self, raw, status=200):
|
|
@@ -75,6 +140,24 @@ class Response:
|
|
| 75 |
|
| 76 |
|
| 77 |
class CatalogAsyncTests(unittest.IsolatedAsyncioTestCase):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
async def test_base_and_catalog_refresh_overlap_without_partial_publication(self):
|
| 79 |
pointer,raw=fixture();catalog=v3.decode_catalog(pointer,raw)
|
| 80 |
packed=gzip.compress(json.dumps({'v':1,'f':{'other':{'s':4}}}).encode())
|
|
|
|
| 28 |
return pointer, raw
|
| 29 |
|
| 30 |
|
| 31 |
+
def repack_catalog(catalog, *, generation=None):
|
| 32 |
+
"""Rebuild a self-consistent synthetic pointer after changing catalog contents."""
|
| 33 |
+
body = {key: value for key, value in catalog.items() if key != "generation"}
|
| 34 |
+
encode = lambda value: json.dumps(value, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()
|
| 35 |
+
generation = generation or hashlib.sha256(encode(body)).hexdigest()
|
| 36 |
+
raw = encode({**body, "generation": generation})
|
| 37 |
+
return {"version": 1, "kind": "reader-reading-pointer", "generation": generation,
|
| 38 |
+
"catalog": {"bucket": v3.BUCKET, "path": v3.catalog_path(generation), "role": "runtime",
|
| 39 |
+
"bytes": len(raw), "sha256": hashlib.sha256(raw).hexdigest()}}, raw
|
| 40 |
+
|
| 41 |
+
|
| 42 |
class CatalogTests(unittest.TestCase):
|
| 43 |
def test_verified_catalog_projection_preserves_other_formats(self):
|
| 44 |
pointer, raw = fixture()
|
|
|
|
| 74 |
with self.subTest(generation=invalid), self.assertRaises(ValueError):
|
| 75 |
v3.catalog_path(invalid)
|
| 76 |
|
| 77 |
+
def test_pointer_identity_and_catalog_resource_fields_fail_closed(self):
|
| 78 |
+
pointer, raw = fixture()
|
| 79 |
+
for field, value in (("version", 0), ("kind", "reader-reading-catalog")):
|
| 80 |
+
with self.subTest(field=field), self.assertRaisesRegex(ValueError, "invalid v3 pointer"):
|
| 81 |
+
v3.decode_catalog({**pointer, field: value}, raw)
|
| 82 |
+
ref = pointer["catalog"]
|
| 83 |
+
for field, value in (("role", "archive"), ("bytes", True), ("bytes", 0),
|
| 84 |
+
("bytes", v3.MAX_BYTES + 1), ("sha256", "A" * 64)):
|
| 85 |
+
with self.subTest(field=field, value=value), self.assertRaisesRegex(ValueError, "v3 catalog resource mismatch"):
|
| 86 |
+
v3.decode_catalog({**pointer, "catalog": {**ref, field: value}}, raw)
|
| 87 |
+
|
| 88 |
+
def test_self_consistent_transport_cannot_mask_invalid_catalog_identity(self):
|
| 89 |
+
_, original = fixture()
|
| 90 |
+
catalog = json.loads(original)
|
| 91 |
+
for field, value in (("version", 2), ("kind", "other"), ("files", [])):
|
| 92 |
+
pointer, raw = repack_catalog({**catalog, field: value})
|
| 93 |
+
with self.subTest(field=field), self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):
|
| 94 |
+
v3.decode_catalog(pointer, raw)
|
| 95 |
+
pointer, raw = repack_catalog(catalog, generation="a" * 64)
|
| 96 |
+
with self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):
|
| 97 |
+
v3.decode_catalog(pointer, raw)
|
| 98 |
+
pointer, raw = repack_catalog(catalog)
|
| 99 |
+
changed = json.loads(raw)
|
| 100 |
+
changed["generation"] = "b" * 64
|
| 101 |
+
raw = json.dumps(changed, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode()
|
| 102 |
+
pointer["catalog"].update(bytes=len(raw), sha256=hashlib.sha256(raw).hexdigest())
|
| 103 |
+
with self.assertRaisesRegex(ValueError, "invalid v3 catalog identity"):
|
| 104 |
+
v3.decode_catalog(pointer, raw)
|
| 105 |
+
|
| 106 |
+
def test_catalog_entries_reject_invalid_keys_and_reading_references(self):
|
| 107 |
+
_, raw = fixture()
|
| 108 |
+
original = json.loads(raw)
|
| 109 |
+
key, entry = next(iter(original["files"].items()))
|
| 110 |
+
for files in ({"": entry}, {key: None}, {key: []}):
|
| 111 |
+
pointer, packed = repack_catalog({**original, "files": files})
|
| 112 |
+
with self.subTest(files=files), self.assertRaisesRegex(ValueError, "invalid v3 source key"):
|
| 113 |
+
v3.decode_catalog(pointer, packed)
|
| 114 |
+
resource = entry["resource"]
|
| 115 |
+
variants = (
|
| 116 |
+
{**entry, "source_sha256": "A" * 64}, {**entry, "reading_generation": None},
|
| 117 |
+
{**entry, "resource": {**resource, "bucket": "foreign/bucket"}},
|
| 118 |
+
{**entry, "resource": {**resource, "role": "archive"}},
|
| 119 |
+
{**entry, "resource": {**resource, "path": "https://foreign.example/reading-manifest.json"}},
|
| 120 |
+
{**entry, "resource": {**resource, "path": resource["path"].replace("objects/aa/", "objects/bb/")}},
|
| 121 |
+
{**entry, "resource": {**resource, "path": resource["path"].replace("reading-manifest.json", "other.json")}},
|
| 122 |
+
{**entry, "resource": {**resource, "sha256": "x" * 64}},
|
| 123 |
+
{**entry, "resource": {**resource, "bytes": True}},
|
| 124 |
+
{**entry, "resource": {**resource, "bytes": 0}},
|
| 125 |
+
)
|
| 126 |
+
for index, invalid in enumerate(variants):
|
| 127 |
+
pointer, packed = repack_catalog({**original, "files": {key: invalid}})
|
| 128 |
+
with self.subTest(index=index), self.assertRaisesRegex(ValueError, "invalid v3 reading reference"):
|
| 129 |
+
v3.decode_catalog(pointer, packed)
|
| 130 |
+
|
| 131 |
|
| 132 |
class Response:
|
| 133 |
def __init__(self, raw, status=200):
|
|
|
|
| 140 |
|
| 141 |
|
| 142 |
class CatalogAsyncTests(unittest.IsolatedAsyncioTestCase):
|
| 143 |
+
async def test_corrupted_pinned_base_is_rejected_and_all_responses_released(self):
|
| 144 |
+
_, raw = fixture()
|
| 145 |
+
catalog = json.loads(raw)
|
| 146 |
+
packed = gzip.compress(b'{"v":1,"f":{}}')
|
| 147 |
+
digest = hashlib.sha256(packed).hexdigest()
|
| 148 |
+
catalog["base_sidecar"] = {"bucket": v3.BUCKET, "role": "runtime", "bytes": len(packed),
|
| 149 |
+
"sha256": digest, "path": f"reader-index/snapshots/{digest}/reader_assets.json.gz"}
|
| 150 |
+
pointer, raw = repack_catalog(catalog)
|
| 151 |
+
corrupted = bytes([packed[0] ^ 1]) + packed[1:]
|
| 152 |
+
responses = [Response(json.dumps(pointer).encode()), Response(raw), Response(corrupted)]
|
| 153 |
+
session = SimpleNamespace(get=AsyncMock(side_effect=responses))
|
| 154 |
+
with patch('backend.reader_assets.decode_reader_assets') as decode:
|
| 155 |
+
with self.assertRaisesRegex(ValueError, 'unified base sidecar checksum mismatch'):
|
| 156 |
+
await v3.load_reading_catalog(session)
|
| 157 |
+
decode.assert_not_called()
|
| 158 |
+
self.assertEqual(session.get.await_count, 3)
|
| 159 |
+
self.assertTrue(all(response.released for response in responses))
|
| 160 |
+
|
| 161 |
async def test_base_and_catalog_refresh_overlap_without_partial_publication(self):
|
| 162 |
pointer,raw=fixture();catalog=v3.decode_catalog(pointer,raw)
|
| 163 |
packed=gzip.compress(json.dumps({'v':1,'f':{'other':{'s':4}}}).encode())
|