Files
rust-discovery/tests/windows_clock_readiness.py
2026-11-14 02:39:39 +01:00

62 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Focused readback/error gates for the LocalService readiness bootstrap. No VM."""
import hashlib
import pathlib
import sys
import tempfile
import unittest
from unittest.mock import patch
ROOT=pathlib.Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'deploy/windows'))
import clock_readiness as clock
class ReadinessGuards(unittest.TestCase):
def case(self,root):
ident='11111111-1111-4111-8111-111111111111'
folder=root/ident;folder.mkdir(mode=0o700)
clock.save(folder,'intent.json',dict(id=ident,binary='a'*64,source=hashlib.sha256(clock.SCRIPT.encode()).hexdigest(),no_jobs=True))
return ident,folder
def result(self,ident,trusted=False):
return dict(id=ident,native_report=dict(case_id=ident,token_sid='S-1-5-19',elevated=False,read_only=True,
samples=[dict(trusted=trusted,reason='CLOCK_PERMISSION_DENIED',rpc_code=5)]*2),service_removed=True,no_jobs=True)
def test_lost_start_reply_only_readbacks_and_retains_untrusted_evidence(self):
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp);ident,folder=self.case(root);calls=[]
def remote(script,payload,**kwargs):
self.assertEqual(script,clock.LAUNCH);calls.append(payload['action']);return self.result(ident)
with patch.object(clock,'STATE',root),patch.object(sys,'argv',['clock','readback','--id',ident]),patch.object(clock.cb,'powershell',side_effect=remote),patch('builtins.print'):
clock.main();clock.main()
self.assertEqual(calls,['readback','readback'])
report=clock.cb.contract.protected_json(folder/'result.json')
self.assertTrue(report['native_execution_verified']);self.assertFalse(report['clock_ready'])
self.assertFalse((folder/'start-intent.json').exists())
def test_good_raw_status_cannot_claim_production_clock_without_qualification(self):
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp);ident,folder=self.case(root)
report=self.result(ident,trusted=True)
for sample in report['native_report']['samples']:sample['adapter_qualified']=False
with patch.object(clock,'STATE',root),patch.object(sys,'argv',['clock','readback','--id',ident]),patch.object(clock.cb,'powershell',return_value=report),patch('builtins.print'):
clock.main()
result=clock.cb.contract.protected_json(folder/'result.json')
self.assertTrue(result['diagnostic_status_ready']);self.assertFalse(result['adapter_qualified']);self.assertFalse(result['clock_ready'])
def test_changed_source_cannot_resume_same_case(self):
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp);ident,_=self.case(root)
with patch.object(clock,'STATE',root),patch.object(clock,'SCRIPT',clock.SCRIPT+'\n'),patch.object(sys,'argv',['clock','readback','--id',ident]),patch.object(clock.cb,'powershell',side_effect=AssertionError('no remote call')):
with self.assertRaisesRegex(RuntimeError,'QUALIFIED_SOURCE_CHANGED'):clock.main()
def test_missing_native_report_cannot_claim_qualification(self):
with tempfile.TemporaryDirectory() as tmp:
root=pathlib.Path(tmp);ident,folder=self.case(root)
with patch.object(clock,'STATE',root),patch.object(sys,'argv',['clock','readback','--id',ident]),patch.object(clock.cb,'powershell',return_value=dict(native_report=None,service_removed=True)):
with self.assertRaisesRegex(RuntimeError,'INCOMPLETE'):clock.main()
self.assertFalse((folder/'result.json').exists())
if __name__=='__main__':unittest.main()