Files
WoW-Hybrid-gateway/tests/casc_native_reuse_test.py
praxisandClaude Fable 5.2 7f2139d8d0 Offline test fixtures
883 files, from working commit b1a079a.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 01:32:48 +02:00

93 lines
6.5 KiB
Python

#!/usr/bin/env python3
"""Local proof contracts with a fake exporter; no installed client access."""
import hashlib,json
from pathlib import Path
import sys,tempfile,unittest
from unittest.mock import patch
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
import casc_asset_package as package
from casc_encoding_stage_test import fixture,key
from casc_tvfs_test import fixture as tvfs_fixture,asset as tvfs_asset
import casc_tvfs
class ReuseTest(unittest.TestCase):
def test_framing_preserves_native_alias_and_rejects_missing_or_conflicting_payload(self):
raw=tvfs_fixture();row=tvfs_asset();row.pop('file_data_id');row['content_key']='aa'*16
audit=dict(staged_root_md5='12'*16,selected_same_id_variant_fallback=dict(
kind='existing-same-file-id-content-variant-fallback',ids_groups_names_preserved=True,
staged_root_md5='12'*16,changes=[dict(file_data_id=100,new_content_key='aa'*16)]))
assets=[dict(file='texture.blp',content_key='aa'*16)]
changed,proof=package.preserve_registered_tvfs_variants(raw,audit,assets,{'texture.blp':row})
name='00000064'+'AA'*16;paths=casc_tvfs.paths(changed)
actual=casc_tvfs.container_record(changed,casc_tvfs.spans(changed,paths[name])[0])
self.assertEqual(actual['encoding_key_prefix'],row['encoding_key'][:18])
self.assertEqual(proof['replaced_paths'],[name])
self.assertEqual(package.preserve_registered_tvfs_variants(raw,{},[],{}),(raw,None))
with self.assertRaisesRegex(ValueError,'preserved framed payload'):
package.preserve_registered_tvfs_variants(raw,audit,[],{})
with self.assertRaisesRegex(ValueError,'preserved framed payload'):
package.preserve_registered_tvfs_variants(raw,audit,assets+[dict(file='second.blp',content_key='aa'*16)],
{'texture.blp':row,'second.blp':dict(row,encoding_key='ee'*16)})
audit['selected_same_id_variant_fallback']['staged_root_md5']='00'*16
with self.assertRaisesRegex(ValueError,'provenance'):
package.preserve_registered_tvfs_variants(raw,audit,assets,{'texture.blp':row})
def setUp(self):
self.temp=tempfile.TemporaryDirectory();self.root=Path(self.temp.name)
self.payload=b'x'*100;self.ck=hashlib.md5(self.payload).hexdigest()
base=fixture();enc,_=package.extend_encoding(base,[dict(content_key=self.ck,encoding_key=key(20000).hex(),decoded_size=100,encoded_size=109)],expected_encoding_md5=hashlib.md5(base).hexdigest())
self.encoding=self.root/'encoding.bin';self.encoding.write_bytes(enc)
self.source=self.root/'asset.bin';self.source.write_bytes(self.payload)
self.manifest=self.root/'manifest.json';self.asset=dict(file='asset.bin',content_key=self.ck,bytes=100,sha256=hashlib.sha256(self.payload).hexdigest(),file_data_id=10)
self.manifest.write_text(json.dumps(dict(assets=[self.asset])))
self.client=self.root/'client';self.client.mkdir();(self.client/'.build.info').write_text('fixture')
self.exporter=self.root/'exporter';self.exporter.write_bytes(b'fake exporter')
self.output=self.root/'proof'
def tearDown(self):self.temp.cleanup()
def run_proof(self):return package.verify_native_asset_reuse(self.manifest,self.encoding,self.client,self.output,self.exporter)
def export(self,argv,**kwargs):Path(argv[-1]).write_bytes(self.payload)
def test_real_exported_bytes_required_and_preserved_encoding_keys_reported(self):
with patch('subprocess.run',side_effect=self.export)as call:r=self.run_proof()
self.assertEqual(call.call_count,1);self.assertEqual(len(r['matches']),1)
self.assertEqual(r['matches'][0]['original_encoding_keys'],[key(20000).hex()])
self.assertTrue(r['matches'][0]['native_casc_readback_identical']);self.assertFalse(r['client_storage_modified'])
def test_batch_export_has_one_storage_open_and_still_compares_bytes(self):
# Two aliases of one content key must not trigger duplicate storage reads.
self.manifest.write_text(json.dumps(dict(assets=[self.asset,dict(self.asset,file_data_id=11)])))
def batch(argv,**kwargs):
self.assertEqual(argv[1],'--export-ckeys')
requests=Path(argv[3]).read_text().splitlines()
self.assertEqual(requests,[self.ck+' 100'])
(Path(argv[4])/(self.ck+'.bin')).write_bytes(self.payload)
with patch('subprocess.run',side_effect=batch)as call:
result=package.verify_native_asset_reuse(self.manifest,self.encoding,self.client,self.output,self.exporter,batch_exporter=self.exporter)
self.assertEqual(call.call_count,1);self.assertEqual(len(result['matches']),2)
self.assertEqual(result['batch_exporter_sha256'],hashlib.sha256(self.exporter.read_bytes()).hexdigest())
def test_wrong_native_bytes_never_publish_success(self):
with patch('subprocess.run',side_effect=lambda argv,**kw:Path(argv[-1]).write_bytes(b'wrong')):
with self.assertRaisesRegex(ValueError,'readback differs'):self.run_proof()
self.assertFalse((self.output/'report.json').exists())
def test_source_hash_change_and_native_size_conflict_reject(self):
self.source.write_bytes(b'y'*100)
with self.assertRaisesRegex(ValueError,'bytes changed'):self.run_proof()
self.assertFalse((self.output/'report.json').exists())
def test_changed_client_identity_during_readback_is_rejected(self):
def changing(argv,**kw):self.export(argv,**kw);(self.client/'.build.info').write_text('changed')
with patch('subprocess.run',side_effect=changing):
with self.assertRaisesRegex(ValueError,'inputs changed'):self.run_proof()
def test_missing_content_key_is_not_a_failed_or_fabricated_match(self):
row=dict(self.asset,content_key=key(60000).hex());self.manifest.write_text(json.dumps(dict(assets=[row])))
with patch('subprocess.run',side_effect=AssertionError('not in native encoding')):r=self.run_proof()
self.assertEqual(r['matches'],[]);self.assertEqual(r['absent_from_original_encoding'],1)
def test_framing_rejects_wrong_native_build_metadata_before_output(self):
with patch('subprocess.run',side_effect=self.export):self.run_proof()
native=self.root/'native';native.mkdir();(native/'target-encoding.bin').write_bytes(b'wrong metadata')
output=self.root/'frames'
with self.assertRaisesRegex(ValueError,'Pinned70334 native metadata'):
package.stage_framed_native_assets(self.manifest,native,self.output,output)
self.assertFalse(output.exists())
if __name__=='__main__':unittest.main()