"""Only local fixtures for peer-control reporting and exactly-once starts.""" import copy import hashlib from pathlib import Path import tempfile import unittest from unittest.mock import patch import uuid import helper_peer_controls as control class PeerControlGuards(unittest.TestCase): def fixture(self): ident=str(uuid.uuid4());payload={'helper_pid':6240,'agent_pid':5140} value=dict(schema_version=1,case_id=ident,mode='service',token={'user_sid':'S-1-5-19','control_service_sid_enabled':True,'agent_service_sid_enabled':False},pipe={'sdk_self_check_used':False,'rejected':True,'opened':False,'win32_error':5},no_jobs=True,read_only=True) report={'writer_binding_checked':True,'original_preserved':True,'own_services_removed':True,'reports':{'service-result':value}} return report,payload,ident def test_busy_timeout_or_sdk_self_refusal_is_never_server_peer_proof(self): report,payload,ident=self.fixture() self.assertTrue(control.summarize(report,payload,ident)['controls']['service']['verified_negative']) unbound=copy.deepcopy(report);unbound['writer_binding_checked']=False with self.assertRaises(Exception):control.summarize(unbound,payload,ident) for field,value in [('win32_error',231),('win32_error',1460),('sdk_self_check_used',True),('rejected',False)]: bad=copy.deepcopy(report);bad['reports']['service-result']['pipe'][field]=value with self.assertRaises(Exception,msg=field):control.summarize(bad,payload,ident) bad=copy.deepcopy(report);bad['reports']['service-result']['token']['agent_service_sid_enabled']=True with self.assertRaises(Exception):control.summarize(bad,payload,ident) def test_raw_closed_pipe_must_name_the_original_helper_process(self): report,payload,ident=self.fixture();pipe=report['reports']['service-result']['pipe'];pipe.update(opened=True,win32_error=109,actual_server_pid=6240) self.assertTrue(control.summarize(report,payload,ident)['controls']['service']['verified_negative']) pipe['actual_server_pid']=999 with self.assertRaises(Exception):control.summarize(report,payload,ident) def test_direct_system_proof_is_not_reported_as_a_pipe_control(self): report,payload,ident=self.fixture();report['reports'].clear();payload['direct_peer_proof']=True report['reports']['parent-token']={'user_sid':'S-1-5-18','parent_service_sid_enabled':True} value={'source':'own_system_direct_production_agent_peer_proof','case_id':ident,'expected_agent_pid':5140,'no_jobs':True,'read_only':True,'pipe_used':False,'token_duplicated':False,'service_or_process_acl_changed':False,'result':{'direct_agent_peer_proof':True,'verified':False,'error_codes':['HELPER_SCM_IMAGE_BINDING']}} report['reports']['parent-result']=value result=control.summarize(report,payload,ident) self.assertFalse(result['direct_agent_peer_proof']['result']['verified']);self.assertEqual(result['controls'],{}) value['pipe_used']=True with self.assertRaises(Exception):control.summarize(report,payload,ident) def test_lost_service_start_reply_cannot_repeat_service_operation(self): with tempfile.TemporaryDirectory() as temporary: state=Path(temporary);ident=str(uuid.uuid4());folder=state/ident;folder.mkdir(mode=0o700) manifest=control.helper.store(state,'manifest.json',{'fixture':True});index=control.helper.store(state,'index.json',{'fixture':True}) intent={'payload':{'id':ident,'source':hashlib.sha256(control.SCRIPT.encode()).hexdigest()},'source_file_sha256':control.helper.sha(control.__file__),'manifest_path':str(manifest),'manifest_sha256':control.helper.sha(manifest),'runtime_index':{'path':str(index),'sha256':control.helper.sha(index)}} control.helper.store(folder,'intent.json',intent);control.helper.store(folder,'prepared.json',{'prepared':True}) with patch.object(control,'STATE',state),patch.object(control.cb,'powershell',side_effect=RuntimeError('lost reply')) as rpc: with self.assertRaisesRegex(RuntimeError,'lost reply'):control.run(ident,'service') with self.assertRaises(FileExistsError):control.run(ident,'service') self.assertEqual(rpc.call_count,1) def test_remote_readback_has_no_cleanup_mutation_and_explicit_cleanup_is_separate(self): # The complete remote script is parsed separately. This guard fixes the # security-critical mutation dispatch: readback must not enter cleanup. self.assertIn("if($p.action-in@('service','parent','cleanup')){",control.SCRIPT) self.assertNotIn("if($p.action-in@('service','parent','cleanup','readback'))",control.SCRIPT) self.assertIn("Protect $case $true @($agentSid,$controlSid) @()",control.SCRIPT) self.assertIn("if($mask-ne1180063-or$directory)",control.SCRIPT) if __name__=='__main__':unittest.main()