Files
rust-discovery/deploy/windows/test_system_delegation_upgrade.py
T
2026-11-14 08:09:58 +01:00

87 lines
5.0 KiB
Python

"""Local guard checks only. No credentials, Windows API, VM or RMM operation."""
import contextlib
import copy
import io
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import system_delegation_upgrade as upgrade
def settings():
return dict(schema_version=1, binding={'test_public_binding': 'fixture'},
agent=dict(binary_sha256=upgrade.OLD_AGENT, user_sid='S-1-5-19'),
helper=dict(binary_sha256=upgrade.OLD_HELPER, user_sid='S-1-5-18'),
allowed_services=[], max_runtime_seconds=30, enable_effects=False, allow_reboot=False)
class JointUpgradeGuards(unittest.TestCase):
def test_only_two_image_pins_change_and_reciprocal_policy_hash_matches_exact_bytes(self):
original = settings()
preserved = copy.deepcopy(original)
raw, pin, policy = upgrade.passive_settings(original, 'a' * 64, 'b' * 64)
new = json.loads(raw)
restored = copy.deepcopy(new)
restored['agent']['binary_sha256'] = upgrade.OLD_AGENT
restored['helper']['binary_sha256'] = upgrade.OLD_HELPER
self.assertEqual(restored, preserved)
self.assertEqual(original, preserved)
self.assertEqual(policy, upgrade.raw_sha(raw))
public = json.loads(pin)
self.assertEqual(public['policy_sha256'], policy)
self.assertEqual(public['binding'], new['binding'])
self.assertEqual(public['agent'], new['agent'])
self.assertEqual(public['helper'], new['helper'])
def test_scope_enablement_extra_fields_wrong_images_and_mistyped_values_are_refused(self):
for field, value in [('enable_effects', True), ('enable_effects', 'false'),
('allow_reboot', True), ('allowed_services', ['foreign-service']),
('max_runtime_seconds', 31), ('max_runtime_seconds', 30.0), ('schema_version', True),
('extra_command', 'not-allowed')]:
changed = settings(); changed[field] = value
with self.subTest(field=field, value=value), self.assertRaises(RuntimeError):
upgrade.passive_settings(changed, 'a' * 64, 'b' * 64)
changed = settings(); changed['helper']['binary_sha256'] = 'f' * 64
with self.assertRaises(RuntimeError): upgrade.passive_settings(changed, 'a' * 64, 'b' * 64)
with self.assertRaises(RuntimeError): upgrade.passive_settings(settings(), 'not-a-hash', 'b' * 64)
def test_readback_uses_readonly_phase_and_never_starts_publishes_or_queries_server(self):
upgrade.script_guards()
ident = '00000000-0000-4000-8000-000000000001'
with tempfile.TemporaryDirectory() as folder, patch.object(upgrade, 'load_case', return_value=(Path(folder), {})), \
patch.object(upgrade, 'remote', return_value={'id': ident, 'read_only': True, 'phase_receipts': {}}) as remote, \
patch.object(upgrade, 'save'), patch.object(upgrade, 'publish', side_effect=AssertionError('publication forbidden')), \
patch.object(upgrade.auto, 'scopes', side_effect=AssertionError('server contact forbidden')), \
contextlib.redirect_stdout(io.StringIO()):
upgrade.finish(ident, read_only=True)
remote.assert_called_once_with({}, 'readback')
def test_local_index_cas_recovers_partial_publication_idempotently_and_rejects_foreign_index(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory); case = root / 'unit-fixture'; case.mkdir(mode=0o700)
current = root / 'current.json'; components = root / 'components.json'
upgrade.save(current, {'old_unit_fixture': True})
result = case / 'result.json'; upgrade.save(result, {'unit_fixture_only': True})
intent = dict(original_runtime_index_sha256=upgrade.sha(current), original_helper_intent_sha256='0' * 64,
payload=dict(new_agent='a' * 64, new_helper='b' * 64, new_policy='c' * 64, new_pin='d' * 64))
with patch.object(upgrade, 'STATE', root), patch.object(upgrade, 'CURRENT', current), patch.object(upgrade, 'COMPONENTS', components):
with patch.object(upgrade.os, 'replace', side_effect=OSError('unit fixture: local reply lost before replace')):
with self.assertRaises(OSError): upgrade.publish(case, intent, result)
self.assertEqual(upgrade.sha(current), intent['original_runtime_index_sha256'])
upgrade.publish(case, intent, result)
before = (current.read_bytes(), components.read_bytes())
upgrade.publish(case, intent, result)
self.assertEqual(before, (current.read_bytes(), components.read_bytes()))
current.write_text('{"foreign_unit_fixture":true}')
foreign = current.read_bytes()
with self.assertRaises(RuntimeError): upgrade.publish(case, intent, result)
self.assertEqual(current.read_bytes(), foreign)
self.assertEqual(components.read_bytes(), before[1])
if __name__ == '__main__':
unittest.main()