import hashlib,json from pathlib import Path import pytest from tools import run_6400_td as td @pytest.fixture def native_run(tmp_path,monkeypatch): root=tmp_path/'source';root.mkdir() native=tmp_path/'native';native.mkdir() inputs={'project.json':'a'*64,'closure.json':'b'*64} monkeypatch.setattr(td,'ROOT',root) monkeypatch.setattr(td,'HARDWARE_PROFILES',{'fixture':{'project':'project.json','closure':'closure.json','artifact_basename':'firmware'}}) monkeypatch.setattr(td,'input_manifest',lambda _:inputs) monkeypatch.setattr(td,'source_identity',lambda _: {'source_kind':'unit-fixture'}) metrics={k:0 for k in ('total_luts','lut','registers','dsp','eram_bram','mslice','lslice','packed_slices','setup_slack_ns','hold_slack_ns','setup_total_negative_slack_ns','hold_total_negative_slack_ns')} metrics.update(fmax_mhz=50,violation_endpoint_counts=[0,0],all_violation_endpoint_counts_zero=True) monkeypatch.setattr(td,'parse_metrics',lambda *_:dict(metrics)) stages={} for name,artifact in [('syn','firmware_gate.db'),('phy','firmware_pr.db'),('bitgen','firmware.bit')]: folder=native/name;folder.mkdir();(folder/artifact).write_bytes(b'unit-fixture') log=folder/(name+'.log');log.write_text('SYN-5025 WARNING: Using 0 for all undriven pins and nets\n') stages[name]={'exit_code':0,'timed_out':False,'log':str(log),'log_sha256':td.sha256(log)} receipt={'schema':'prod2u-6400-td-receipt-v1','run_id':'fixture-native','profile':'fixture','status':'FAIL', 'production':True,'simulation_only':False,'missing_outputs':[],'inputs':inputs, 'project_sha256':inputs['project.json'],'source_closure_sha256':inputs['closure.json'], 'source_manifest_sha256':hashlib.sha256(json.dumps(inputs,sort_keys=True,separators=(',',':')).encode()).hexdigest(), 'bitstream_sha256':td.sha256(native/'bitgen/firmware.bit'),'stages':stages} path=native/'td-receipt.json';path.write_text(json.dumps(receipt)) return path,receipt,metrics def test_reaudit_creates_new_hash_bound_receipt(native_run,tmp_path): path,_,_=native_run;before=path.read_bytes() assert td.reaudit_receipt(path,tmp_path/'review')==0 result=json.loads((tmp_path/'review/td-receipt.json').read_text()) assert result['status']=='PASS' and result['reaudit']['original_status']=='FAIL' assert result['reaudit']['native_receipt_sha256']==hashlib.sha256(before).hexdigest() assert path.read_bytes()==before @pytest.mark.parametrize('change',['log','bit','stage','inputs','schema','not_production']) def test_reaudit_refuses_changed_or_incomplete_native_evidence(native_run,tmp_path,change): path,receipt,_=native_run if change=='log':Path(receipt['stages']['syn']['log']).write_text('changed') elif change=='bit':(path.parent/'bitgen/firmware.bit').write_bytes(b'changed') elif change=='stage':receipt['stages']['phy']['timed_out']=True elif change=='inputs':receipt['project_sha256']='0'*64 elif change=='schema':receipt['schema']='not-a-td-receipt' else:receipt['production']=False if change not in ('log','bit'):path.write_text(json.dumps(receipt)) with pytest.raises(ValueError):td.reaudit_receipt(path,tmp_path/'review') assert not (tmp_path/'review/td-receipt.json').exists() def test_reaudit_keeps_timing_failure(native_run,tmp_path): path,_,metrics=native_run;metrics['hold_slack_ns']=-0.001 assert td.reaudit_receipt(path,tmp_path/'review')==1 assert json.loads((tmp_path/'review/td-receipt.json').read_text())['status']=='FAIL'