106 lines
7.1 KiB
Python
106 lines
7.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Focused local guards for passive automatic-default activation; no VM."""
|
|
import copy
|
|
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 activate_auto_transport as activation
|
|
|
|
|
|
def native(mode='wss',sequence=7):
|
|
return dict(agent_id=activation.base.AGENT,asset_id=activation.base.ASSET,transport=mode,
|
|
config_sha256=activation.base.ORIGINAL_CONFIG if mode=='wss' else 'independently-derived-auto-config',
|
|
config_acl_sha256='original-acl',hashes={'key':'unchanged'},acl_sha256={'key':'unchanged'},
|
|
telemetry_creation_ticks=5,service_state='Running',process_owner_sid='S-1-5-19',
|
|
journal=dict(integrity='ok',read_only=True,agent_id=activation.base.AGENT,sequence=sequence,pending_inventory_events=0))
|
|
|
|
|
|
class ActivationGuards(unittest.TestCase):
|
|
def prepare(self,root,completed=False):
|
|
ident='11111111-1111-4111-8111-111111111111';folder=root/ident;folder.mkdir(mode=0o700)
|
|
source=hashlib.sha256(activation.base.SCRIPT.encode()).hexdigest()
|
|
activation.base.save(folder,'intent.json',dict(id=ident,case='activate_automatic_transport',source_sha256=source,
|
|
started_at='2026-11-13T00:00:00Z',qualification={'actual_earlier_case':'retained'}))
|
|
activation.base.save(folder,'before.json',dict(native=native(),central={'device':{}},server={'closed':'retained'},counts={'targets':'retained'}))
|
|
if completed:
|
|
activation.base.save(folder,'result.json',dict(verified=True,native=native('auto',8)))
|
|
return ident,folder
|
|
|
|
def test_finish_after_lost_enter_reply_only_reads_proven_transition(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);ident,folder=self.prepare(root)
|
|
derived=dict(id=ident,mode='auto',read_only=True,original_config_sha256=activation.base.ORIGINAL_CONFIG,
|
|
original_acl_sha256='original-acl',expected_auto_config_sha256='independently-derived-auto-config',before=native(),entered_receipt_present=False)
|
|
calls=[]
|
|
def remote(case,action,**kwargs):
|
|
calls.append(action);self.assertEqual(action,'state');return native('auto',8)
|
|
with patch.object(activation,'STATE',root),patch.object(activation,'CURRENT',root/'current.json'), \
|
|
patch.object(activation,'transition',return_value=derived),patch.object(activation.base,'remote',side_effect=remote), \
|
|
patch.object(activation.base,'wait_mode',return_value={'two_actual_contacts':'fixture'}), \
|
|
patch.object(activation.auto,'status',return_value={'closed':'retained'}), \
|
|
patch.object(activation.auto,'flags_disabled',return_value=True), \
|
|
patch.object(activation.auto,'scopes',return_value={'targets':'retained'}), \
|
|
patch.object(activation.base,'read_device',return_value={'stored':'fixture'}),patch('builtins.print'):
|
|
activation.finish(ident)
|
|
self.assertEqual(calls,['state','state'])
|
|
result=activation.base.cb.contract.protected_json(folder/'result.json')
|
|
self.assertTrue(result['verified']);self.assertFalse(result['transition']['entered_receipt_present'])
|
|
self.assertFalse((folder/'entered.json').exists())
|
|
current=activation.base.cb.contract.protected_json(root/'current.json')
|
|
self.assertEqual(current['transport'],'auto')
|
|
self.assertEqual(current['config_sha256'],'independently-derived-auto-config')
|
|
|
|
def test_arbitrary_observed_config_hash_cannot_publish_current(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);ident,folder=self.prepare(root)
|
|
derived=dict(id=ident,mode='auto',read_only=True,original_config_sha256=activation.base.ORIGINAL_CONFIG,
|
|
original_acl_sha256='original-acl',expected_auto_config_sha256='independently-derived-auto-config',before=native())
|
|
wrong=native('auto');wrong['config_sha256']='unknown-host-config'
|
|
with patch.object(activation,'STATE',root),patch.object(activation,'CURRENT',root/'current.json'), \
|
|
patch.object(activation,'transition',return_value=derived),patch.object(activation.base,'remote',return_value=wrong):
|
|
with self.assertRaises(activation.base.cb.contract.GuardError):activation.finish(ident)
|
|
self.assertFalse((folder/'result.json').exists());self.assertFalse((root/'current.json').exists())
|
|
|
|
def test_restored_activation_cannot_republish_historical_automatic_index(self):
|
|
for marker in ('restore-result.json','explicit-restore-intent.json'):
|
|
with self.subTest(marker=marker),tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);ident,folder=self.prepare(root,completed=True)
|
|
activation.base.save(folder,marker,{'explicit_restore':'retained'})
|
|
with patch.object(activation,'STATE',root),patch.object(activation,'CURRENT',root/'current.json'), \
|
|
patch.object(activation.base,'remote',side_effect=AssertionError('no repeat transition')):
|
|
with self.assertRaises(activation.base.cb.contract.GuardError):activation.finish(ident)
|
|
self.assertFalse((root/'current.json').exists())
|
|
|
|
def test_completed_automatic_finish_checks_actual_config_before_index_repair(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);ident,_=self.prepare(root,completed=True)
|
|
with patch.object(activation,'STATE',root),patch.object(activation,'CURRENT',root/'current.json'), \
|
|
patch.object(activation.base,'remote',return_value=native('wss')):
|
|
with self.assertRaises(activation.base.cb.contract.GuardError):activation.finish(ident)
|
|
self.assertFalse((root/'current.json').exists())
|
|
|
|
def test_index_restore_versions_previous_auto_and_keeps_original_receipt(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);ident,folder=self.prepare(root,completed=True)
|
|
activation.base.save(folder,'restore-result.json',dict(verified=True,native=native('wss',9)))
|
|
with patch.object(activation,'CURRENT',root/'current.json'):
|
|
activation.publish_current(folder,'auto','result.json')
|
|
before=(root/'current.json').read_bytes()
|
|
activation.publish_current(folder,'wss','restore-result.json')
|
|
after=(root/'current.json').read_bytes()
|
|
activation.publish_current(folder,'wss','restore-result.json')
|
|
self.assertEqual(after,(root/'current.json').read_bytes())
|
|
self.assertNotEqual(before,after)
|
|
saved=activation.base.cb.contract.protected_json(folder/'current-index-before-wss.json')
|
|
self.assertEqual(saved['transport'],'auto')
|
|
self.assertTrue(activation.base.cb.contract.protected_json(folder/'result.json')['verified'])
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|