test_td_reaudit.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import hashlib,json
  2. from pathlib import Path
  3. import pytest
  4. from tools import run_6400_td as td
  5. @pytest.fixture
  6. def native_run(tmp_path,monkeypatch):
  7. root=tmp_path/'source';root.mkdir()
  8. native=tmp_path/'native';native.mkdir()
  9. inputs={'project.json':'a'*64,'closure.json':'b'*64}
  10. monkeypatch.setattr(td,'ROOT',root)
  11. monkeypatch.setattr(td,'HARDWARE_PROFILES',{'fixture':{'project':'project.json','closure':'closure.json','artifact_basename':'firmware'}})
  12. monkeypatch.setattr(td,'input_manifest',lambda _:inputs)
  13. monkeypatch.setattr(td,'source_identity',lambda _: {'source_kind':'unit-fixture'})
  14. 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')}
  15. metrics.update(fmax_mhz=50,violation_endpoint_counts=[0,0],all_violation_endpoint_counts_zero=True)
  16. monkeypatch.setattr(td,'parse_metrics',lambda *_:dict(metrics))
  17. stages={}
  18. for name,artifact in [('syn','firmware_gate.db'),('phy','firmware_pr.db'),('bitgen','firmware.bit')]:
  19. folder=native/name;folder.mkdir();(folder/artifact).write_bytes(b'unit-fixture')
  20. log=folder/(name+'.log');log.write_text('SYN-5025 WARNING: Using 0 for all undriven pins and nets\n')
  21. stages[name]={'exit_code':0,'timed_out':False,'log':str(log),'log_sha256':td.sha256(log)}
  22. receipt={'schema':'prod2u-6400-td-receipt-v1','run_id':'fixture-native','profile':'fixture','status':'FAIL',
  23. 'production':True,'simulation_only':False,'missing_outputs':[],'inputs':inputs,
  24. 'project_sha256':inputs['project.json'],'source_closure_sha256':inputs['closure.json'],
  25. 'source_manifest_sha256':hashlib.sha256(json.dumps(inputs,sort_keys=True,separators=(',',':')).encode()).hexdigest(),
  26. 'bitstream_sha256':td.sha256(native/'bitgen/firmware.bit'),'stages':stages}
  27. path=native/'td-receipt.json';path.write_text(json.dumps(receipt))
  28. return path,receipt,metrics
  29. def test_reaudit_creates_new_hash_bound_receipt(native_run,tmp_path):
  30. path,_,_=native_run;before=path.read_bytes()
  31. assert td.reaudit_receipt(path,tmp_path/'review')==0
  32. result=json.loads((tmp_path/'review/td-receipt.json').read_text())
  33. assert result['status']=='PASS' and result['reaudit']['original_status']=='FAIL'
  34. assert result['reaudit']['native_receipt_sha256']==hashlib.sha256(before).hexdigest()
  35. assert path.read_bytes()==before
  36. @pytest.mark.parametrize('change',['log','bit','stage','inputs','schema','not_production'])
  37. def test_reaudit_refuses_changed_or_incomplete_native_evidence(native_run,tmp_path,change):
  38. path,receipt,_=native_run
  39. if change=='log':Path(receipt['stages']['syn']['log']).write_text('changed')
  40. elif change=='bit':(path.parent/'bitgen/firmware.bit').write_bytes(b'changed')
  41. elif change=='stage':receipt['stages']['phy']['timed_out']=True
  42. elif change=='inputs':receipt['project_sha256']='0'*64
  43. elif change=='schema':receipt['schema']='not-a-td-receipt'
  44. else:receipt['production']=False
  45. if change not in ('log','bit'):path.write_text(json.dumps(receipt))
  46. with pytest.raises(ValueError):td.reaudit_receipt(path,tmp_path/'review')
  47. assert not (tmp_path/'review/td-receipt.json').exists()
  48. def test_reaudit_keeps_timing_failure(native_run,tmp_path):
  49. path,_,metrics=native_run;metrics['hold_slack_ns']=-0.001
  50. assert td.reaudit_receipt(path,tmp_path/'review')==1
  51. assert json.loads((tmp_path/'review/td-receipt.json').read_text())['status']=='FAIL'