"""Historical configure-only proof is FAILED, never retrospective acceptance.""" import copy from pathlib import Path from unittest import TestCase from unittest.mock import patch from test_guard import apt import test_native_acceptance as fixture from test_native_recovery import recovery_files class ConfiguredReconciliationTests(TestCase): def scenario(self,variation=None): with recovery_files() as (base,tx,ctx,action,partial,previous): active=apt.ACTIVE # This deliberately models the historical runtime with NO HOST-before # observation, not a current schema2-prechecks execution. (active/'host-reboot-before.json').unlink() protocol=apt.digest(apt.canonical([apt.Action('rmm-mvp-test','1.0','all','none','=','1.0','all','none','**CONFIGURE**').__dict__])) records={'launch.json':{'transaction_sha256':ctx.transaction_sha256,'effect_armed_ledger_id':ctx.effect_armed_ledger_id}, 'native-prechecks.json':fixture.prechecks(ctx), 'sandbox-ready.json':{'transaction_sha256':ctx.transaction_sha256,'sandbox_version':1}, 'hook-armed.json':{'armed_at_ms':2000,'effect_armed_ledger_id':ctx.effect_armed_ledger_id,'grant_id':ctx.grant_id,'transaction_sha256':ctx.transaction_sha256,'protocol_sha256':protocol}} for name,obj in records.items():(active/name).write_bytes(apt.canonical(obj)) path,binding=apt.bootstrap_binding(tx,ctx,action);apt.durable_once(path,binding) pins=dict(grant_id=ctx.grant_id,previous_grant_id=partial['grant_id'],partial_proof_sha256=action['native_recovery']['partial_proof_sha256'],provider_sha256=action['provider_sha256']) fields='transaction_sha256 job_payload_sha256 effect_armed_ledger_id grant_id job_id attempt_id authorization_id plan_sha256'.split() request={k:getattr(ctx,k) for k in fields} observed=copy.deepcopy(partial['snapshot']);observed['rmm-mvp-test:all']['status']='install ok installed' if variation=='different-package':observed['other:amd64']={'version':'1','status':'install ok installed'} elif variation=='unpacked':observed['rmm-mvp-test:all']['status']='install ok unpacked' elif variation=='wrong-context':request['job_payload_sha256']='0'*64 elif variation=='wrong-grant':pins['grant_id']='different' elif variation=='missing-hook':(active/'hook-armed.json').unlink() elif variation in ('apt-failed','child-reboot'): obj={'transaction_sha256':ctx.transaction_sha256,'apt_exit_code':100 if variation=='apt-failed' else 0,'reboot_required':variation=='child-reboot'} (active/'sandbox-result.json').write_bytes(apt.canonical(obj)) elif variation=='fake-before':(active/'host-reboot-before.json').write_bytes(apt.canonical(fixture.host_before(ctx,True))) elif variation=='new-prechecks':(active/'native-prechecks.json').write_bytes(apt.canonical(fixture.prechecks(ctx,fixture.host_before(ctx,True)))) elif variation=='partial-proof':(active/'partial-proof.json').write_bytes(b'{}') elif variation=='record-created':(base/('recovery-'+partial['grant_id']+'.json')).write_bytes(b'{}') elif variation=='history':(previous/'sandbox-result.json').write_bytes(b'{}') elif variation=='protocol': records['hook-armed.json']['protocol_sha256']='0'*64 (active/'hook-armed.json').write_bytes(apt.canonical(records['hook-armed.json'])) before={str(p.relative_to(base)):p.read_bytes() for p in base.rglob('*') if p.is_file()} old_read=apt.sealed_read;original_exists=Path.exists def read(path,*a,**k): if str(path)=='/run/reboot-required':return b'Current HOST restart required\n' if str(path)=='/usr/share/rmm-mvp-test/version' and variation=='payload':return b'1.1\n' return old_read(path,*a,**k) with patch.object(apt,'HISTORICAL_CONFIGURED_RECOVERY',pins),patch.object(apt.os,'geteuid',return_value=0), \ patch.object(apt,'sealed_read',side_effect=read),patch.object(Path,'read_text',return_value='other' if variation=='boot' else ctx.boot_id), \ patch.object(Path,'exists',lambda p:variation!='host-false' if str(p)=='/run/reboot-required' else original_exists(p)), \ patch.object(apt,'observe_host_frontend_lock',return_value=variation=='foreign-lock'), \ patch.object(apt,'status_fingerprint',return_value='a'*64,side_effect=['a'*64,'b'*64] if variation=='drift' else None), \ patch.object(apt,'installed_snapshot',return_value=observed),patch.object(apt,'verify_archive'), \ patch.object(apt,'run_read',return_value=b'not clean' if variation=='audit' else b''), \ patch.object(apt.subprocess,'Popen',side_effect=AssertionError('No package effects allowed')): if variation is None: result=apt.verify_configured_recovery(request) self.assertEqual(result['effect_state'],'known_failed') self.assertEqual(result['code'],'APT_RECOVERY_CONFIGURED_ACCEPTANCE_REBOOT_FLAG_VERIFIED') proof=result['proof'];self.assertEqual(len(proof),24) self.assertTrue(proof['effect_complete']);self.assertFalse(proof['postconditions_passed']) self.assertFalse(proof['partial_effect']);self.assertTrue(proof['host_reboot_required']);self.assertTrue(proof['reboot_required']) self.assertEqual(proof['snapshot'],observed) self.assertNotIn('host_reboot_required_before',proof) else: with self.assertRaises((apt.Veto,OSError,ValueError)):apt.verify_configured_recovery(request) after={str(p.relative_to(base)):p.read_bytes() for p in base.rglob('*') if p.is_file()} self.assertEqual(before,after,'readonly classification must preserve EVERY witness and marker') def test_complete_effect_with_missing_historical_before_is_failed_without_writes(self): self.scenario() def test_binding_history_native_result_or_retroactive_before_mismatch_never_closes(self): for variation in ('wrong-context','wrong-grant','missing-hook','apt-failed','child-reboot','fake-before','new-prechecks','partial-proof','record-created','history','protocol'): with self.subTest(variation=variation):self.scenario(variation) def test_current_state_must_remain_exact_configured_fixture_and_host_flag(self): for variation in ('different-package','unpacked','payload','boot','host-false','foreign-lock','drift','audit'): with self.subTest(variation=variation):self.scenario(variation)