103 lines
7.2 KiB
Python
103 lines
7.2 KiB
Python
import copy
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import spell_modifier_group_profile as profile
|
|
|
|
|
|
def fixture():
|
|
before=[dict(ID=110,SpellID=10,SpellClassSet=6,ModalNextSpell=0,SpellClassMask=[1,0,0,0]),dict(ID=199,SpellID=99,SpellClassSet=6,ModalNextSpell=0,SpellClassMask=[1,0,0,0])]
|
|
after=copy.deepcopy(before);after[0]['SpellClassMask'][3]=-2147483648
|
|
alloc={'native_inventory':after,'allocations':[dict(family=6,baseline_native_inventory_sha256=profile._sha(before),baseline_unused_native_bits=list(range(1,128)),groups=[dict(native_bit=127,spell_ids=[10])],record_replacements=[dict(table_name='SpellClassOptions',id=110,before=before[0],record=after[0])])]}
|
|
routes={'routes':[dict(family=6,spell_ids=[10],source_operation=14,flat_scale=1,kind='percent',native_operations=[14],source_weights={'5':'1','2':'-1'},holds=[],incomplete_spell_ids=[])]}
|
|
return routes,alloc
|
|
|
|
|
|
class Profile(unittest.TestCase):
|
|
def test_deterministic_exact_render_and_input_preservation(self):
|
|
routes,alloc=fixture();before=copy.deepcopy((routes,alloc))
|
|
text,receipt=profile.generate(routes,alloc)
|
|
self.assertIn('std::array<SourceWeight,2>',text)
|
|
self.assertIn(' {2,-1},\n {5,1},',text)
|
|
self.assertIn(' {5,14,true,14,127,0,2,true,1},',text)
|
|
self.assertIn('ReleaseQualified = false;',text)
|
|
self.assertEqual(receipt['routes'],1)
|
|
self.assertEqual(receipt['output_sha256'],hashlib.sha256(text.encode()).hexdigest())
|
|
self.assertEqual(profile.generate(routes,alloc),(text,receipt))
|
|
routes['routes'][0]['source_weights']={'2':'-1','5':'1'}
|
|
self.assertEqual(profile.generate(routes,alloc),(text,receipt))
|
|
self.assertEqual(alloc,before[1])
|
|
self.assertEqual(routes,before[0])
|
|
|
|
def test_corrupt_witness_membership_and_collateral_reject(self):
|
|
mutations=[lambda a:a['allocations'][0]['baseline_unused_native_bits'].remove(127),lambda a:a['allocations'][0]['groups'][0].update(native_bit=128),lambda a:a['allocations'][0]['groups'][0].update(spell_ids=[10,99]),lambda a:a['native_inventory'][1]['SpellClassMask'].__setitem__(3,-2147483648),lambda a:a['allocations'][0]['groups'].append(dict(native_bit=127,spell_ids=[99])),lambda a:a['allocations'][0].update(baseline_native_inventory_sha256='bad')]
|
|
for n,mutation in enumerate(mutations):
|
|
r,a=fixture();mutation(a)
|
|
with self.subTest(n=n),self.assertRaises(ValueError):profile.generate(r,a)
|
|
|
|
def test_changed_route_family_membership_and_weights_reject(self):
|
|
mutations=[dict(family=4),dict(spell_ids=[99]),dict(source_weights={'64':'1'}),dict(source_weights={'2':'1','02':'-1'}),dict(source_weights={'2':'2'}),dict(source_weights={'2':True}),dict(source_weights={}),dict(native_operations=[1,2]),dict(source_operation=256),dict(kind='other'),dict(incomplete_spell_ids=[10])]
|
|
for patch in mutations:
|
|
r,a=fixture();r['routes'][0].update(patch)
|
|
with self.subTest(patch=patch),self.assertRaises(ValueError):profile.generate(r,a)
|
|
|
|
def test_unsupported_held_route_never_emitted(self):
|
|
r,a=fixture();r['routes'][0].update(holds=['unsupported-operation'],native_operations=[1,2],source_weights={'90':'17'})
|
|
text,receipt=profile.generate(r,a)
|
|
self.assertIn('std::array<Route,0>',text);self.assertIn('std::array<SourceWeight,0>',text)
|
|
self.assertEqual(receipt['held_routes'],1);self.assertEqual(receipt['held'][0]['holds'],['unsupported-operation'])
|
|
self.assertFalse(receipt['release_qualified'])
|
|
|
|
def test_uint16_offset_exhaustion_rejects_whole_output(self):
|
|
r,a=fixture();r['routes'][0]['source_weights']={str(i):'1' for i in range(64)}
|
|
r['routes']=[copy.deepcopy(r['routes'][0]) for _ in range(1024)]
|
|
with self.assertRaisesRegex(ValueError,'uint16'):profile.generate(r,a)
|
|
|
|
def test_qualified_scale_is_explicit_bounded_and_does_not_admit_holds(self):
|
|
for scale in (1,100,1000):
|
|
r,a=fixture();r['routes'][0]['flat_scale']=scale
|
|
text,receipt=profile.generate(r,a)
|
|
self.assertIn('bool power_index_zero; double flat_scale;',text)
|
|
self.assertIn('{5,14,true,14,127,0,2,true,'+str(scale)+'},',text)
|
|
for scale in (None,True,0,-1,2,1e100,float('nan'),float('inf'),'100'):
|
|
r,a=fixture();r['routes'][0]['flat_scale']=scale
|
|
with self.subTest(scale=scale),self.assertRaises(ValueError):profile.generate(r,a)
|
|
r,a=fixture();r['routes'][0].update(source_operation=30,holds=['op30-held'],flat_scale=100)
|
|
text,receipt=profile.generate(r,a)
|
|
self.assertEqual(receipt['routes'],0);self.assertEqual(receipt['held_routes'],1)
|
|
|
|
def test_explicit_no_consumer_records_proof_without_emitting(self):
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
path=Path(temp)/'source-proof.json';path.write_text('{"scope":"synthetic consumer proof"}')
|
|
proof={'path':str(path),'sha256':hashlib.sha256(path.read_bytes()).hexdigest()}
|
|
for operation in (21,26,31,32):
|
|
r,a=fixture();r['routes'][0].update(source_operation=operation,disposition='source-no-consumer',source_consumer_proof=proof,holds=['previous unsupported hold'])
|
|
text,receipt=profile.generate(r,a)
|
|
self.assertEqual(receipt['routes'],0);self.assertEqual(receipt['weights'],0)
|
|
self.assertEqual(receipt['held_routes'],0);self.assertEqual(receipt['source_no_consumer_routes'],1)
|
|
self.assertEqual(receipt['source_no_consumer'][0]['source_consumer_proof'],proof)
|
|
self.assertEqual(receipt['source_no_consumer'][0]['holds'],['previous unsupported hold'])
|
|
self.assertIn('ReleaseQualified = false;',text)
|
|
for operation in (30,33):
|
|
r,a=fixture();r['routes'][0].update(source_operation=operation,disposition='source-no-consumer',source_consumer_proof=proof)
|
|
with self.assertRaises(ValueError):profile.generate(r,a)
|
|
for patch in ({'disposition':'unknown'},{'disposition':'source-no-consumer','source_operation':21}, {'disposition':'source-no-consumer','source_operation':21,'source_consumer_proof':dict(proof,sha256='0'*64)}):
|
|
r,a=fixture();r['routes'][0].update(patch)
|
|
with self.assertRaises(ValueError):profile.generate(r,a)
|
|
|
|
def test_explicit_cli_paths_and_receipt(self):
|
|
r,a=fixture()
|
|
with tempfile.TemporaryDirectory() as temp:
|
|
p=Path(temp);rp=p/'routes.json';ap=p/'allocation.json';op=p/'out.hpp';receipt=p/'receipt.json'
|
|
rp.write_text(json.dumps(r));ap.write_text(json.dumps(a))
|
|
subprocess.run([sys.executable,profile.__file__,'--routes',str(rp),'--allocation',str(ap),'--output',str(op),'--receipt',str(receipt)],check=True,capture_output=True)
|
|
self.assertEqual(op.read_text(),profile.generate(r,a)[0]);proof=json.loads(receipt.read_text())
|
|
self.assertEqual(proof['pins'][str(rp)],hashlib.sha256(rp.read_bytes()).hexdigest());self.assertFalse(proof['release_qualified'])
|
|
|
|
if __name__=='__main__':unittest.main(verbosity=2)
|