120 lines
7.2 KiB
Python
120 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Source-only acceptance-runner guards; no Windows/server access."""
|
|
import copy
|
|
import datetime as dt
|
|
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 transport_acceptance as acceptance
|
|
|
|
|
|
def state(transport='wss'):
|
|
return dict(agent_id=acceptance.AGENT,asset_id=acceptance.ASSET,
|
|
transport=transport,config_sha256=acceptance.ORIGINAL_CONFIG,
|
|
config_acl_sha256='original-acl',hashes={'private_key':'retained'},acl_sha256={'private_key':'retained'},
|
|
telemetry_creation_ticks=17,service_state='Running',process_owner_sid='S-1-5-19',
|
|
journal=dict(integrity='ok',read_only=True,agent_id=acceptance.AGENT,sequence=5,pending_inventory_events=0))
|
|
|
|
|
|
class Preservation(unittest.TestCase):
|
|
def test_existing_identity_hash_acl_and_sequence_cannot_drift(self):
|
|
original=state();acceptance.preserved(original,copy.deepcopy(original))
|
|
for mutate in (
|
|
lambda value:value['hashes'].update(private_key='replaced'),
|
|
lambda value:value['acl_sha256'].update(private_key='broader'),
|
|
lambda value:value.update(asset_id='different'),
|
|
lambda value:value.update(telemetry_creation_ticks=99),
|
|
lambda value:value['journal'].update(agent_id='different'),
|
|
lambda value:value['journal'].update(sequence=4),
|
|
lambda value:value['journal'].update(integrity='corrupt'),
|
|
):
|
|
changed=copy.deepcopy(original);mutate(changed)
|
|
with self.subTest(changed=changed),self.assertRaises(acceptance.cb.contract.GuardError):
|
|
acceptance.preserved(original,changed)
|
|
|
|
def test_passive_https_does_not_require_fabricated_job_sync_readiness(self):
|
|
at=dt.datetime(2026,11,13,tzinfo=dt.timezone.utc)
|
|
base=dict(agent_id=acceptance.AGENT,asset_id=acceptance.ASSET,enrollment_generation=1,
|
|
assignment_version=1,certificate_sha256='retained')
|
|
values=[dict(device=dict(base,transport='https',last_contact_at=(at+dt.timedelta(seconds=i)).isoformat(),
|
|
sync_ready=False,restore_blocked=True)) for i in (1,2)]
|
|
with patch.object(acceptance,'observe',side_effect=values),patch.object(acceptance.time,'sleep'):
|
|
actual=acceptance.wait_mode('unused','https',at,base)
|
|
self.assertNotEqual(actual['first']['device']['last_contact_at'],actual['second']['device']['last_contact_at'])
|
|
self.assertFalse(actual['second']['device']['sync_ready'])
|
|
|
|
def test_actual_transport_receipt_cannot_change_identity_generation(self):
|
|
at=dt.datetime(2026,11,13,tzinfo=dt.timezone.utc)
|
|
base=dict(agent_id=acceptance.AGENT,asset_id=acceptance.ASSET,enrollment_generation=1,
|
|
assignment_version=1,certificate_sha256='retained')
|
|
bad=dict(device=dict(base,transport='https',assignment_version=2,last_contact_at=(at+dt.timedelta(seconds=1)).isoformat()))
|
|
with patch.object(acceptance,'observe',return_value=bad),self.assertRaises(acceptance.cb.contract.GuardError):
|
|
acceptance.wait_mode('unused','https',at,base)
|
|
|
|
def test_same_case_restore_intent_is_reused_but_changed_intent_refused(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
folder=pathlib.Path(tmp)
|
|
acceptance.fixed_intent(folder,'intent.json',{'id':'same'})
|
|
acceptance.fixed_intent(folder,'intent.json',{'id':'same'})
|
|
with self.assertRaises(acceptance.cb.contract.GuardError):
|
|
acceptance.fixed_intent(folder,'intent.json',{'id':'changed'})
|
|
|
|
def test_short_pinned_launchers_do_not_embed_full_script(self):
|
|
for value in (acceptance.LAUNCH_SCRIPT,acceptance.PREPARE_SCRIPT):
|
|
# Includes the enclosing fixed bootstrap try/catch overhead.
|
|
self.assertLess((len(value.encode('utf-16le'))+1024)*4//3,7500)
|
|
self.assertNotIn('device.key',acceptance.LAUNCH_SCRIPT)
|
|
self.assertIn('Get-FileHash',acceptance.LAUNCH_SCRIPT)
|
|
|
|
|
|
class Resumption(unittest.TestCase):
|
|
def test_finish_after_saved_https_and_restored_wss_performs_no_mutation(self):
|
|
ident='11111111-1111-4111-8111-111111111111'
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);folder=root/ident;folder.mkdir(mode=0o700)
|
|
source=hashlib.sha256(acceptance.SCRIPT.encode()).hexdigest()
|
|
acceptance.save(folder,'intent.json',dict(id=ident,case='explicit_https',started_at='2026-11-13T00:00:00Z',source_sha256=source))
|
|
acceptance.save(folder,'before.json',dict(native=state(),central={'device':{}},gate=dict(jobs=0,outbox=0,permits=0,unresolved=0)))
|
|
acceptance.save(folder,'https-accepted.json',dict(verified=True,original_receipt=True))
|
|
acceptance.fixed_intent(folder,'restore-intent.json',dict(id=ident,expected_original_config_sha256=acceptance.ORIGINAL_CONFIG,no_jobs=True))
|
|
calls=[]
|
|
def remote(case,action,**kwargs):
|
|
calls.append(action)
|
|
self.assertEqual(case,ident)
|
|
self.assertEqual(action,'state')
|
|
return state()
|
|
with patch.object(acceptance,'STATE',root),patch.object(acceptance,'remote',side_effect=remote), \
|
|
patch.object(acceptance,'wait_mode',return_value={'two_actual_receipts':True}), \
|
|
patch.object(acceptance,'gate',return_value=dict(jobs=0,outbox=0,permits=0,unresolved=0)),patch('builtins.print'):
|
|
acceptance.finish(ident)
|
|
self.assertEqual(calls,['state','state'])
|
|
self.assertTrue(acceptance.cb.contract.protected_json(folder/'result.json')['verified'])
|
|
self.assertTrue(acceptance.cb.contract.protected_json(folder/'https-accepted.json')['original_receipt'])
|
|
|
|
def test_failed_https_observation_restores_same_transition_without_acceptance(self):
|
|
ident='11111111-1111-4111-8111-111111111111'
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
root=pathlib.Path(tmp);folder=root/ident;folder.mkdir(mode=0o700)
|
|
source=hashlib.sha256(acceptance.SCRIPT.encode()).hexdigest()
|
|
acceptance.save(folder,'intent.json',dict(id=ident,case='explicit_https',started_at='2026-11-13T00:00:00Z',source_sha256=source))
|
|
acceptance.save(folder,'before.json',dict(native=state(),central={'device':{}},gate=dict(jobs=0,outbox=0,permits=0,unresolved=0)))
|
|
calls=[]
|
|
def remote(case,action,**kwargs):
|
|
calls.append(action)
|
|
return state('https') if action=='state' else {'restored':True}
|
|
with patch.object(acceptance,'STATE',root),patch.object(acceptance,'remote',side_effect=remote), \
|
|
patch.object(acceptance,'wait_mode',side_effect=acceptance.cb.contract.GuardError('no fresh heartbeats')):
|
|
with self.assertRaises(acceptance.cb.contract.GuardError):acceptance.finish(ident)
|
|
self.assertEqual(calls,['state','state','restore'])
|
|
self.assertFalse((folder/'result.json').exists())
|
|
self.assertFalse((folder/'https-accepted.json').exists())
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|