//! Existing helper reconciliation semantic cases with portable test-only guard, //! boot and signing adapters. Does not qualify OS locks, file IDs, IPC or SYSTEM. use rmm_agent_core::helper_reconcile as reconcile; use chrono::{DateTime, Duration, Utc}; use ed25519_dalek::{Signer, SigningKey}; use reconcile::*; use rmm_agent_core::auth::{Binding, HelperIdentity, Request}; use rmm_domain::{jobs::*, reconciliation::*}; use rusqlite::{Connection, params}; use std::{fs, path::PathBuf}; use uuid::Uuid; /// Test-only guard: identifies our fixture DB, not an OS lock qualification. struct ExecutorGuard { directory: PathBuf, } impl ExecutorLedgerGuard for ExecutorGuard { fn check(&self, db: &Connection) -> anyhow::Result<()> { let path: String = db.query_row( "SELECT file FROM pragma_database_list WHERE name='main'", [], |r| r.get(0), )?; anyhow::ensure!( PathBuf::from(path).canonicalize()? == self.directory.join("consumption.sqlite").canonicalize()?, "NOT_THE_SHARED_EXECUTOR_LEDGER" ); Ok(()) } } struct TestClock { now: DateTime, trusted: bool, } impl Clock for TestClock { fn now(&self) -> anyhow::Result> { anyhow::ensure!(self.trusted, "CLOCK_UNTRUSTED"); Ok(self.now) } } struct TestBoot(Uuid); impl BootSource for TestBoot { fn current_boot(&self) -> anyhow::Result { Ok(self.0) } } /// Test-only protected-store/peer adapter. These unit checks do not qualify /// Windows ACLs, a live SCM peer, file identities or the real intent CAS. struct TestNoStartGuard { ledger: ExecutorGuard, instance: String, request_sha256: String, anchor_sha256: String, intent_recorded_at: DateTime, original_boot_id: Uuid, dispatch_possible: bool, } impl ExecutorLedgerGuard for TestNoStartGuard { fn check(&self, db: &Connection) -> anyhow::Result<()> { self.ledger.check(db) } } impl NoStartGuard for TestNoStartGuard { fn live_instance_sha256(&self) -> anyhow::Result { Ok(self.instance.clone()) } fn verify_no_dispatch( &self, original: &Request, anchor: &NoStartAnchor, ) -> anyhow::Result { anyhow::ensure!( !self.dispatch_possible && self.request_sha256 == hash(&serde_json::to_vec(original)?) && self.anchor_sha256 == hash(&serde_json::to_vec(anchor)?), "TEST_INTENT_NOT_PROVEN" ); Ok(NoDispatchObservation { intent_id: Uuid::from_u128(900), intent_sha256: hash(b"immutable test-only intent bytes"), intent_recorded_at: self.intent_recorded_at, original_boot_id: self.original_boot_id, }) } } fn denial_case() -> ( Fixture, HelperSync, NoStartAnchor, Request, TestNoStartGuard, ) { let mut f = Fixture::new(); f.identity = HelperIdentity::System; let mut sync = f.sync(); let capture = f.snapshot(&mut sync, 7); let raw = f.request(&capture); let ready = f.decision(&capture, &raw, 7, true); assert!(f.apply(&mut sync, &ready, &raw).unwrap().ready); let job = JobPayload { schema_version: 1, tenant_id: f.binding.tenant_id, asset_id: f.binding.asset_id, agent_id: f.binding.agent_id, enrollment_generation: 1, assignment_version: 3, job_id: Uuid::new_v4(), attempt_id: Uuid::new_v4(), action: Action::SetServiceState { unit: "RustDiscoveryMvpFixture".into(), state: "running".into(), }, execution_identity: "system".into(), not_before: f.clock.now - Duration::seconds(10), start_before: f.clock.now + Duration::minutes(5), max_runtime_seconds: 30, max_output_bytes: 4096, }; let permit = ExecutionPermitPayload { domain: "rmm.execution-permit".into(), schema_version: 1, grant_id: Uuid::new_v4(), authorization_id: Uuid::new_v4(), tenant_id: job.tenant_id, asset_id: job.asset_id, agent_id: job.agent_id, enrollment_generation: 1, assignment_version: 3, job_id: job.job_id, attempt_id: job.attempt_id, plan_hash: hash(b"approved exact service plan"), job_payload_hash: hash(&encode(&job).unwrap()), action: job.action.name().into(), execution_identity: job.execution_identity.clone(), max_runtime_seconds: 30, not_before: f.clock.now - Duration::seconds(1), start_before: f.clock.now + Duration::seconds(59), key_id: hash(&f.key.verifying_key().to_bytes()), }; let bytes = encode(&permit).unwrap(); let request = Request { job_bytes: encode(&job).unwrap(), signature: f.key.sign(&bytes).to_bytes().to_vec(), permit_bytes: bytes, plan_hash: permit.plan_hash, authorization_id: permit.authorization_id, }; let mut guard = TestNoStartGuard { ledger: ExecutorGuard { directory: f.dir.clone(), }, instance: hash(b"same live helper PID/creation/ledger/lock fixture"), request_sha256: hash(&serde_json::to_vec(&request).unwrap()), anchor_sha256: String::new(), intent_recorded_at: f.clock.now, original_boot_id: f.boot.0, dispatch_possible: false, }; let anchor = sync .no_start_anchor(&f.db, &guard, 7, &f.clock, &f.boot) .unwrap(); guard.anchor_sha256 = hash(&serde_json::to_vec(&anchor).unwrap()); (f, sync, anchor, request, guard) } fn denial_ledger_bytes(db: &Connection) -> Vec { let mut statement=db.prepare("SELECT c.grant_id,c.agent,c.generation,c.job,c.attempt,c.result,e.start,e.outcome FROM consumed c LEFT JOIN execution_evidence e USING(grant_id) ORDER BY c.grant_id").unwrap(); type Row = ( String, String, u64, String, String, Option>, Option>, Option>, ); let rows = statement .query_map([], |r| { Ok(( r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?, r.get(6)?, r.get(7)?, )) }) .unwrap() .collect::>>() .unwrap(); serde_json::to_vec(&rows).unwrap() } #[test] fn abandon_after_expiry_records_deny_only_evidence_and_requires_normal_signed_unlock() { let (mut f, mut sync, anchor, request, mut guard) = denial_case(); let permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes).unwrap(); f.clock.now += Duration::seconds(120); assert!(f.binding.verify(&request, f.clock.now).is_err()); let outcome = sync .abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) .unwrap(); assert_eq!(outcome.state, "failed"); assert_eq!(outcome.error_code, "HELPER_ABANDONED_BEFORE_DISPATCH"); assert_eq!(outcome.evidence["not_started"], true); assert_eq!(outcome.evidence["postconditions_passed"], false); assert_eq!( outcome.evidence["observed_at"], serde_json::to_value(f.clock.now).unwrap() ); assert!( outcome.stdout.is_empty() && outcome.stderr.is_empty() && outcome.exit_code.is_none() && !outcome.reboot_required ); assert_eq!( f.db.query_row::( "SELECT count(*) FROM consumed WHERE result IS NULL", [], |r| r.get(0) ) .unwrap(), 1 ); let before = denial_ledger_bytes(&f.db); guard.dispatch_possible = true; // An existing outcome is returned, never reclassified. let repeated = sync .abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) .unwrap(); assert_eq!( serde_json::to_vec(&outcome).unwrap(), serde_json::to_vec(&repeated).unwrap() ); assert_eq!(denial_ledger_bytes(&f.db), before); assert!( rmm_agent_core::ledger::begin_execution( &mut f.db, &permit, &request, f.boot.0, f.clock.now ) .is_err() ); assert!(!sync.can_start(&f.db, &f.guard, 7, &f.clock, &f.boot)); let capture = f.snapshot(&mut sync, 7); assert_eq!( capture.attempts[0].original_request_sha256, hash(&serde_json::to_vec(&request).unwrap()) ); let raw = f.request(&capture); let decision = f.decision(&capture, &raw, 7, true); assert!(f.apply(&mut sync, &decision, &raw).unwrap().ready); assert_eq!( f.db.query_row::( "SELECT count(*) FROM consumed WHERE result IS NOT NULL", [], |r| r.get(0) ) .unwrap(), 1 ); assert!( rmm_agent_core::ledger::begin_execution( &mut f.db, &permit, &request, f.boot.0, f.clock.now ) .is_err() ); } #[test] fn abandon_storage_failure_rolls_back_consumption_start_and_outcome_together() { for trigger in [ "CREATE TRIGGER refuse_start BEFORE INSERT ON execution_evidence BEGIN SELECT RAISE(ABORT,'fixture failure'); END;", "CREATE TRIGGER refuse_outcome BEFORE UPDATE OF outcome ON execution_evidence BEGIN SELECT RAISE(ABORT,'fixture failure'); END;", ] { let (mut f, mut sync, anchor, request, guard) = denial_case(); f.db.execute_batch(trigger).unwrap(); assert!( sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) .is_err() ); for table in ["consumed", "execution_evidence"] { assert_eq!( f.db.query_row::(&format!("SELECT count(*) FROM {table}"), [], |r| r .get(0)) .unwrap(), 0 ); } f.db.execute_batch( "DROP TRIGGER IF EXISTS refuse_start;DROP TRIGGER IF EXISTS refuse_outcome", ) .unwrap(); assert!( sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) .is_ok() ); } } #[test] fn abandon_preserves_existing_known_unknown_missing_start_and_foreign_request_rows() { use rmm_agent_core::{auth::Outcome, ledger}; for variant in [ "known", "unknown", "missing_start", "missing_outcome", "foreign_grant", "changed_request", ] { let (mut f, mut sync, anchor, request, guard) = denial_case(); let mut stored = request.clone(); let mut permit: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes).unwrap(); if variant == "foreign_grant" { permit.grant_id = Uuid::new_v4(); } if variant == "changed_request" { permit.plan_hash = hash(b"other signed plan"); stored.plan_hash = permit.plan_hash.clone(); } stored.permit_bytes = encode(&permit).unwrap(); stored.signature = f.key.sign(&stored.permit_bytes).to_bytes().to_vec(); if variant == "missing_start" { ledger::consume(&mut f.db, &permit).unwrap(); } else { ledger::begin_execution(&mut f.db, &permit, &stored, f.boot.0, f.clock.now).unwrap(); } if variant == "known" || variant == "unknown" { let mut outcome = Outcome::unknown("ORIGINAL_OUTCOME"); if variant == "known" { outcome.state = "failed".into(); } ledger::persist_outcome(&f.db, &permit, &outcome).unwrap(); if variant == "known" { ledger::finish(&f.db, &permit, &outcome).unwrap(); } } let before = denial_ledger_bytes(&f.db); let response = sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot); if matches!(variant, "foreign_grant" | "changed_request") { assert!(response.is_err(), "{variant}"); } else { let outcome = response.unwrap(); assert_eq!( outcome.state, if variant == "known" { "failed" } else { "unknown" }, "{variant}" ); assert_ne!(outcome.error_code, "HELPER_ABANDONED_BEFORE_DISPATCH"); } assert_eq!(denial_ledger_bytes(&f.db), before, "{variant}"); } } #[test] fn abandon_rejects_wrong_signature_scope_clock_boot_lineage_instance_and_dispatch_proof() { for variant in [ "signature", "tenant", "root_identity", "script", "restarted", "clock", "boot", "instance", "history", "checkpoint", "dispatch", "future_intent", ] { let (mut f, mut sync, anchor, mut request, mut guard) = denial_case(); match variant { "signature" => request.signature[0] ^= 1, "tenant" | "root_identity" | "script" | "restarted" => { let mut job: JobPayload = serde_json::from_slice(&request.job_bytes).unwrap(); let mut p: ExecutionPermitPayload = serde_json::from_slice(&request.permit_bytes).unwrap(); if variant == "tenant" { job.tenant_id = Uuid::new_v4(); p.tenant_id = job.tenant_id; } if variant == "root_identity" { job.execution_identity = "root".into(); p.execution_identity = "root".into(); } if variant == "script" { job.action = script(); p.action = job.action.name().into(); } if variant == "restarted" { job.action = Action::SetServiceState { unit: "RustDiscoveryMvpFixture".into(), state: "restarted".into(), }; } request.job_bytes = encode(&job).unwrap(); p.job_payload_hash = hash(&request.job_bytes); request.permit_bytes = encode(&p).unwrap(); request.signature = f.key.sign(&request.permit_bytes).to_bytes().to_vec(); } "clock" => f.clock.trusted = false, "boot" => f.boot.0 = Uuid::new_v4(), "instance" => guard.instance = hash(b"different helper PID or ledger identity"), "history" => { f.db.execute("DELETE FROM helper_sync_decisions", []) .unwrap(); } "checkpoint" => { f.db.execute("UPDATE helper_sync_meta SET checkpoint=checkpoint+1", []) .unwrap(); } "dispatch" => guard.dispatch_possible = true, "future_intent" => guard.intent_recorded_at = f.clock.now + Duration::seconds(1), _ => unreachable!(), } assert!( sync.abandon_no_start(&mut f.db, &guard, &anchor, &request, &f.clock, &f.boot) .is_err(), "{variant}" ); assert_eq!( f.db.query_row::("SELECT count(*) FROM consumed", [], |r| r.get(0)) .unwrap(), 0, "{variant}" ); assert_eq!( f.db.query_row::("SELECT count(*) FROM execution_evidence", [], |r| r .get(0)) .unwrap(), 0, "{variant}" ); } } struct Fixture { dir: PathBuf, db: Connection, guard: ExecutorGuard, binding: Binding, key: SigningKey, clock: TestClock, boot: TestBoot, agent_journal: Uuid, identity: HelperIdentity, } #[test] fn checkpoint_repair_verifies_complete_signed_history_and_detects_removed_predecessor() { let mut f = Fixture::new(); let mut sync = f.sync(); let first = f.snapshot(&mut sync, 7); let request = f.request(&first); let d = f.decision(&first, &request, 7, false); let signed = f.sign(&d); sync.apply(&mut f.db, &f.guard, &signed, &request, &f.clock, &f.boot) .unwrap(); f.clock.now += Duration::seconds(1); let second = f.snapshot(&mut sync, 7); let request = f.request(&second); let d = f.decision(&second, &request, 7, false); let signed = f.sign(&d); sync.apply(&mut f.db, &f.guard, &signed, &request, &f.clock, &f.boot) .unwrap(); verify_checkpoint_repair_history(&f.db, &f.binding, None).unwrap(); f.db.execute( "DELETE FROM helper_sync_decisions WHERE checkpoint=?1", [first.checkpoint], ) .unwrap(); assert!(verify_checkpoint_repair_history(&f.db, &f.binding, None).is_err()); } impl Drop for Fixture { fn drop(&mut self) { drop(std::mem::replace( &mut self.db, Connection::open_in_memory().unwrap(), )); let _ = fs::remove_dir_all(&self.dir); } } impl Fixture { fn new() -> Self { let dir = std::env::temp_dir().join(format!("helper-sync-{}", Uuid::new_v4())); fs::create_dir(&dir).unwrap(); let guard = ExecutorGuard { directory: dir.clone(), }; let db = Connection::open(dir.join("consumption.sqlite")).unwrap(); rmm_agent_core::ledger::initialize_ledger(&db).unwrap(); let key = SigningKey::from_bytes(&[42; 32]); let binding = Binding { tenant_id: Uuid::new_v4(), asset_id: Uuid::new_v4(), agent_id: Uuid::new_v4(), enrollment_generation: 1, assignment_version: 3, public_key_hex: hex::encode(key.verifying_key().to_bytes()), }; let boot = TestBoot(Uuid::from_u128(1234)); Self { dir, db, guard, binding, key, clock: TestClock { now: Utc::now(), trusted: true, }, boot, agent_journal: Uuid::new_v4(), identity: HelperIdentity::Root, } } fn sync(&self) -> HelperSync { HelperSync::new(self.binding.clone(), &self.clock) .unwrap() .with_helper_identity(self.identity) } fn snapshot(&mut self, sync: &mut HelperSync, generation: u64) -> Snapshot { sync.snapshot( &mut self.db, &self.guard, generation, &self.clock, &self.boot, ) .unwrap() } fn request(&self, snapshot: &Snapshot) -> Vec { request( snapshot, self.agent_journal, 100 + snapshot.checkpoint, &snapshot.attempts, ) } fn decision( &self, snapshot: &Snapshot, raw: &[u8], generation: u64, ready: bool, ) -> ReconcileDecisionPayload { let mut d = ReconcileDecisionPayload { domain: "rmm.reconciliation-decision".into(), schema_version: 1, decision_id: Uuid::new_v4(), request_id: Uuid::nil(), request_hash: hash(raw), tenant_id: self.binding.tenant_id, asset_id: self.binding.asset_id, agent_id: self.binding.agent_id, enrollment_generation: 1, assignment_version: 3, connection_generation: generation, boot_id: self.boot.0, journal_id: self.agent_journal, checkpoint: 100 + snapshot.checkpoint, helper_journal_id: Some(snapshot.journal_id), helper_checkpoint: snapshot.checkpoint, issued_at: self.clock.now, valid_until: self.clock.now + Duration::seconds(60), ready, helper_ready: ready, reason: if ready { "CONSISTENT" } else { "ATTEMPT_RECONCILIATION_REQUIRED" } .into(), attempts: snapshot .attempts .iter() .map(|a| AttemptDecision { job_id: a.job_id, attempt_id: a.attempt_id, job_payload_hash: a.job_payload_hash.clone(), grant_id: Some(a.grant_id), authorization_id: Some(a.authorization_id), plan_hash: Some(a.plan_hash.clone()), action: a.action.clone(), execution_identity: self.identity.as_str().into(), revision: a.revision + 1, outcome_state: if matches!( a.state.as_str(), "succeeded" | "failed" | "cancelled" | "expired" ) { a.state.clone() } else { "unknown".into() }, previous_outcome_sha256: (!a.outcome_sha256.is_empty()) .then(|| a.outcome_sha256.clone()), outcome_sha256: matches!( a.state.as_str(), "succeeded" | "failed" | "cancelled" | "expired" ) .then(|| a.outcome_sha256.clone()), outcome_bytes: vec![], original_boot_id: Some(a.original_boot_id), unlock: matches!( a.state.as_str(), "succeeded" | "failed" | "cancelled" | "expired" ), reason: "TEST_SERVER_DECISION".into(), }) .collect(), acknowledged_message_ids: vec![], key_id: hash(&self.key.verifying_key().to_bytes()), }; // The fixture emits a canonical tag-1 request ID first, exactly as prost. let len = usize::from(raw[1]); d.request_id = Uuid::parse_str(std::str::from_utf8(&raw[2..2 + len]).unwrap()).unwrap(); d } fn sign(&self, d: &ReconcileDecisionPayload) -> SignedResponse { let bytes = encode(d).unwrap(); SignedResponse { signature: self.key.sign(&bytes).to_bytes().to_vec(), decision_bytes: bytes, } } fn apply( &mut self, sync: &mut HelperSync, d: &ReconcileDecisionPayload, raw: &[u8], ) -> anyhow::Result { let r = self.sign(d); sync.apply(&mut self.db, &self.guard, &r, raw, &self.clock, &self.boot) } fn add( &mut self, action: Action, outcome: Option, ) -> ExecutionPermitPayload { let job = JobPayload { schema_version: 1, tenant_id: self.binding.tenant_id, asset_id: self.binding.asset_id, agent_id: self.binding.agent_id, enrollment_generation: 1, assignment_version: 3, job_id: Uuid::new_v4(), attempt_id: Uuid::new_v4(), action, execution_identity: self.identity.as_str().into(), not_before: self.clock.now - Duration::seconds(10), start_before: self.clock.now + Duration::minutes(5), max_runtime_seconds: 180, max_output_bytes: 4096, }; let permit = ExecutionPermitPayload { domain: "rmm.execution-permit".into(), schema_version: 1, grant_id: Uuid::new_v4(), authorization_id: Uuid::new_v4(), tenant_id: job.tenant_id, asset_id: job.asset_id, agent_id: job.agent_id, enrollment_generation: 1, assignment_version: 3, job_id: job.job_id, attempt_id: job.attempt_id, plan_hash: hash(b"approved fixed plan"), job_payload_hash: hash(&encode(&job).unwrap()), action: job.action.name().into(), execution_identity: self.identity.as_str().into(), not_before: self.clock.now - Duration::seconds(1), start_before: self.clock.now + Duration::seconds(59), max_runtime_seconds: 180, key_id: hash(&self.key.verifying_key().to_bytes()), }; let bytes = encode(&permit).unwrap(); let original = Request { job_bytes: encode(&job).unwrap(), permit_bytes: bytes.clone(), signature: self.key.sign(&bytes).to_bytes().to_vec(), plan_hash: permit.plan_hash.clone(), authorization_id: permit.authorization_id, }; rmm_agent_core::ledger::consume(&mut self.db, &permit).unwrap(); rmm_agent_core::ledger::save_start( &self.db, &permit, &original, self.boot.0, self.clock.now, ) .unwrap(); if let Some(o) = outcome { self.db .execute( "UPDATE execution_evidence SET outcome=?2 WHERE grant_id=?1", params![ permit.grant_id.to_string(), canonical_outcome_bytes(&o).unwrap() ], ) .unwrap(); } permit } } #[test] fn general_and_apt_instances_share_fencing_but_never_inherit_a_process_gate() { let mut f = Fixture::new(); let mut general = f.sync(); let first = f.snapshot(&mut general, 1); let raw = f.request(&first); let d = f.decision(&first, &raw, 1, true); f.apply(&mut general, &d, &raw).unwrap(); assert!(general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let mut apt = f.sync(); assert!(!apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let second = f.snapshot(&mut apt, 1); assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let raw = f.request(&second); let d = f.decision(&second, &raw, 1, true); f.apply(&mut apt, &d, &raw).unwrap(); assert!(apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); } #[test] fn targeted_final_unlock_does_not_release_an_unrelated_unknown() { let mut f = Fixture::new(); let known = f.add(script(), Some(success())); let actual: rmm_agent_core::auth::Outcome = serde_json::from_slice(&canonical_outcome_bytes(&success()).unwrap()).unwrap(); rmm_agent_core::ledger::finish(&f.db, &known, &actual).unwrap(); let uncertain = f.add(script(), Some(unknown())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, false); let applied = f.apply(&mut sync, &d, &raw).unwrap(); assert_eq!(applied.unlocked_grants, vec![known.grant_id]); assert!(!applied.ready); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let held: bool = f.db.query_row( "SELECT result IS NULL FROM consumed WHERE grant_id=?1", [uncertain.grant_id.to_string()], |r| r.get(0), ) .unwrap(); assert!(held); } #[test] fn independently_remembered_agent_journal_cannot_be_replaced_by_server_ready() { let mut f = Fixture::new(); let mut sync = f.sync(); let first = f.snapshot(&mut sync, 1); let raw = f.request(&first); let d = f.decision(&first, &raw, 1, true); f.apply(&mut sync, &d, &raw).unwrap(); let next = f.snapshot(&mut sync, 2); f.agent_journal = Uuid::new_v4(); let raw = f.request(&next); let d = f.decision(&next, &raw, 2, true); assert!(f.apply(&mut sync, &d, &raw).is_err()); assert!( sync.snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) .is_err() ); } #[test] fn missing_signed_checkpoint_history_closes_the_live_gate_stickily() { let mut f = Fixture::new(); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); f.apply(&mut sync, &d, &raw).unwrap(); assert!(sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); f.db.execute("DELETE FROM helper_sync_decisions", []) .unwrap(); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!( sync.snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) .is_err() ); } #[test] fn stale_process_connection_does_not_poison_a_new_fenced_process() { let mut f = Fixture::new(); let mut general = f.sync(); let snapshot = f.snapshot(&mut general, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); f.apply(&mut general, &d, &raw).unwrap(); let mut apt = f.sync(); let snapshot = f.snapshot(&mut apt, 2); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 2, true); f.apply(&mut apt, &d, &raw).unwrap(); assert!( general .snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) .is_err() ); assert!(apt.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); } #[test] fn signed_noncanonical_outcome_bytes_do_not_unlock_root_consumption() { let mut f = Fixture::new(); f.add(script(), Some(success())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let mut d = f.decision(&snapshot, &raw, 1, true); d.attempts[0].outcome_bytes = serde_json::to_vec_pretty(&success()).unwrap(); d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); assert!(f.apply(&mut sync, &d, &raw).is_err()); assert_eq!( f.db.query_row( "SELECT count(*) FROM consumed WHERE result IS NULL", [], |r| r.get::<_, i64>(0) ) .unwrap(), 1 ); } #[test] fn both_live_profiles_apply_one_signature_and_checkpoint_without_inheriting_readiness() { let mut f = Fixture::new(); let mut general = f.sync(); let mut apt = f.sync(); let first = f.snapshot(&mut general, 1); let second = f.snapshot(&mut apt, 1); assert_eq!(first.checkpoint, second.checkpoint); assert!(!general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(!apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let raw = f.request(&first); let d = f.decision(&first, &raw, 1, true); f.apply(&mut general, &d, &raw).unwrap(); f.apply(&mut apt, &d, &raw).unwrap(); assert!(general.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(apt.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); } fn script() -> Action { Action::RunScript { interpreter: "posix_sh".into(), script_utf8: "printf hello".into(), arguments: vec![], expected_stdout: Some("hello".into()), windows_execution_profile: None, privileged_script_profile: None, } } fn success() -> ReconciledOutcome { ReconciledOutcome { state: "succeeded".into(), stdout: b"hello".to_vec(), stderr: vec![], exit_code: Some(0), output_truncated: false, reboot_required: false, evidence: serde_json::json!({"postconditions_passed":true}), error_code: String::new(), } } fn unknown() -> ReconciledOutcome { ReconciledOutcome { state: "unknown".into(), stdout: vec![], stderr: vec![], exit_code: None, output_truncated: false, reboot_required: false, evidence: serde_json::json!({"postconditions_passed":false}), error_code: "EFFECT_UNKNOWN".into(), } } // Test-only encoder for the actual existing prost field numbers. No substitute // protocol dependency is added to the independently executable helper crate. fn vi(mut n: u64, out: &mut Vec) { loop { let mut byte = (n & 127) as u8; n >>= 7; if n != 0 { byte |= 128; } out.push(byte); if n == 0 { break; } } } fn num(tag: u64, n: u64, out: &mut Vec) { if n != 0 { vi(tag << 3, out); vi(n, out); } } fn blob(tag: u64, b: &[u8], out: &mut Vec) { if !b.is_empty() { vi((tag << 3) | 2, out); vi(b.len() as u64, out); out.extend_from_slice(b); } } fn text(tag: u64, s: &str, out: &mut Vec) { blob(tag, s.as_bytes(), out) } fn request( snapshot: &Snapshot, journal: Uuid, checkpoint: u64, attempts: &[LocalAttempt], ) -> Vec { let mut out = vec![]; text(1, &Uuid::new_v4().to_string(), &mut out); text(2, &journal.to_string(), &mut out); num(3, checkpoint, &mut out); text( 4, &snapshot .previous_decision_id .map(|id| id.to_string()) .unwrap_or_default(), &mut out, ); num(5, 3, &mut out); for a in attempts { let mut row = vec![]; text(1, &a.job_id.to_string(), &mut row); text(2, &a.attempt_id.to_string(), &mut row); text(3, &a.job_payload_hash, &mut row); text(4, &a.grant_id.to_string(), &mut row); text(5, &a.consumption, &mut row); text(6, &a.state, &mut row); num(7, a.revision + 1, &mut row); text(8, &a.original_boot_id.to_string(), &mut row); text(11, &a.outcome_sha256, &mut row); blob(12, &a.outcome_bytes, &mut row); blob(6, &row, &mut out); } num(8, 1, &mut out); text(9, &snapshot.journal_id.to_string(), &mut out); num(10, snapshot.checkpoint, &mut out); text( 11, &snapshot .previous_decision_id .map(|id| id.to_string()) .unwrap_or_default(), &mut out, ); out } #[test] fn clean_ready_shared_lock_and_reopen_start_closed() { let mut f = Fixture::new(); let mut sync = f.sync(); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let applied = f.apply(&mut sync, &d, &raw).unwrap(); assert!(applied.ready); assert_eq!(sync.active_generation(), Some(1)); assert_eq!(applied.snapshot.previous_decision_id, Some(d.decision_id)); assert!(applied.unlocked_grants.is_empty()); assert!(sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); assert!(!sync.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); let response = f.sign(&d); let mut new_process = f.sync(); let mut reopened = Connection::open(f.dir.join("consumption.sqlite")).unwrap(); assert!(!new_process.can_start(&reopened, &f.guard, 1, &f.clock, &f.boot)); assert!( new_process .apply(&mut reopened, &f.guard, &response, &raw, &f.clock, &f.boot) .is_err() ); sync.close(); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); } #[test] fn exact_signatures_fake_key_wrong_domain_and_all_identity_bindings() { let mut f = Fixture::new(); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let base = serde_json::to_value(&d).unwrap(); for (field, value) in [ ("domain", serde_json::json!("rmm.execution-permit")), ("schema_version", serde_json::json!(2)), ("tenant_id", serde_json::json!(Uuid::new_v4())), ("asset_id", serde_json::json!(Uuid::new_v4())), ("agent_id", serde_json::json!(Uuid::new_v4())), ("enrollment_generation", serde_json::json!(2)), ("assignment_version", serde_json::json!(4)), ("connection_generation", serde_json::json!(2)), ("boot_id", serde_json::json!(Uuid::new_v4())), ("journal_id", serde_json::json!(Uuid::new_v4())), ("checkpoint", serde_json::json!(1)), ("helper_journal_id", serde_json::json!(Uuid::new_v4())), ( "helper_checkpoint", serde_json::json!(snapshot.checkpoint + 1), ), ("request_id", serde_json::json!(Uuid::new_v4())), ("request_hash", serde_json::json!(hash(b"changed"))), ("key_id", serde_json::json!(hash(b"wrongkey"))), ] { let mut changed = base.clone(); changed[field] = value; let changed: ReconcileDecisionPayload = serde_json::from_value(changed).unwrap(); assert!(f.apply(&mut sync, &changed, &raw).is_err(), "{field}"); } let mut response = f.sign(&d); response.signature[0] ^= 1; assert!( sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) .is_err() ); let mut response = f.sign(&d); response.decision_bytes.push(b' '); assert!( sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) .is_err() ); let fake = SigningKey::from_bytes(&[43; 32]); let mut response = f.sign(&d); response.signature = fake.sign(&response.decision_bytes).to_bytes().to_vec(); assert!( sync.apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) .is_err() ); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); } #[test] fn definitive_unlock_is_transactional_idempotent_and_keeps_tombstones() { let mut f = Fixture::new(); let grant = f.add(script(), Some(success())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); assert_eq!(snapshot.attempts[0].original_boot_id, f.boot.0); assert_eq!( snapshot.attempts[0].job_payload_hash, grant.job_payload_hash ); assert_eq!(snapshot.attempts[0].original_request_sha256.len(), 64); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let applied = f.apply(&mut sync, &d, &raw).unwrap(); assert_eq!(applied.unlocked_grants, vec![grant.grant_id]); assert!(applied.ready); let again = f.apply(&mut sync, &d, &raw).unwrap(); assert!(again.ready); assert!(again.unlocked_grants.is_empty()); assert_eq!(again.snapshot.checkpoint, snapshot.checkpoint); let body: Vec = f.db.query_row( "SELECT result FROM consumed WHERE grant_id=?1", [grant.grant_id.to_string()], |r| r.get(0), ) .unwrap(); assert_eq!(body, canonical_outcome_bytes(&success()).unwrap()); assert!(rmm_agent_core::ledger::consume(&mut f.db, &grant).is_err()); assert_eq!( f.db.query_row("SELECT count(*) FROM consumed", [], |r| r.get::<_, i64>(0)) .unwrap(), 1 ); } #[test] fn omitted_unknown_root_row_is_sticky_despite_signed_ready() { let mut f = Fixture::new(); f.add(script(), Some(unknown())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = request(&snapshot, f.agent_journal, 100 + snapshot.checkpoint, &[]); let d = f.decision(&snapshot, &raw, 1, true); assert!(f.apply(&mut sync, &d, &raw).is_err()); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let mut restarted = f.sync(); assert!( restarted .snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) .is_err() ); } #[test] fn unknown_code_or_no_result_never_unlocks_even_signed_ready() { for outcome in [None, Some(unknown())] { let mut f = Fixture::new(); f.add(script(), outcome); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); assert_eq!(snapshot.attempts[0].state, "unknown"); let raw = f.request(&snapshot); let mut d = f.decision(&snapshot, &raw, 1, true); let applied = f.apply(&mut sync, &d, &raw).unwrap(); assert!(!applied.ready); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); d = f.decision(&snapshot, &raw, 1, true); d.attempts[0].unlock = true; d.attempts[0].outcome_state = "succeeded".into(); d.attempts[0].outcome_bytes = canonical_outcome_bytes(&success()).unwrap(); d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); assert!(f.apply(&mut sync, &d, &raw).is_err()); assert_eq!( f.db.query_row( "SELECT count(*) FROM consumed WHERE result IS NULL", [], |r| r.get::<_, i64>(0) ) .unwrap(), 1 ); } } #[test] fn old_process_response_fails_after_fresh_capture_even_inside_ttl() { let mut f = Fixture::new(); let mut first = f.sync(); let snapshot = f.snapshot(&mut first, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let response = f.sign(&d); f.clock.now += Duration::seconds(5); let mut second = f.sync(); let newer = f.snapshot(&mut second, 1); assert!(newer.checkpoint > snapshot.checkpoint); assert!( second .apply(&mut f.db, &f.guard, &response, &raw, &f.clock, &f.boot) .is_err() ); assert!(!second.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); } #[test] fn checkpoint_rollback_is_sticky_and_cannot_be_fixed_by_omission() { let mut f = Fixture::new(); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); f.db.execute("UPDATE helper_sync_meta SET checkpoint=0", []) .unwrap(); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); assert!(f.apply(&mut sync, &d, &raw).is_err()); f.db.execute( "UPDATE helper_sync_meta SET checkpoint=?1", [snapshot.checkpoint], ) .unwrap(); assert!( sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) .is_err() ); } #[test] fn mutated_root_ledger_after_capture_is_not_the_signed_snapshot() { let mut f = Fixture::new(); f.add(script(), Some(success())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let mut changed = success(); changed.stdout = b"different".to_vec(); f.db.execute( "UPDATE execution_evidence SET outcome=?1", [canonical_outcome_bytes(&changed).unwrap()], ) .unwrap(); assert!(f.apply(&mut sync, &d, &raw).is_err()); assert!( sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) .is_err() ); } #[test] fn unrelated_action_plan_and_previous_outcome_cannot_unlock() { let mut f = Fixture::new(); f.add(script(), Some(success())); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let mut changed = d.clone(); changed.attempts[0].action = "reboot".into(); assert!(f.apply(&mut sync, &changed, &raw).is_err()); let mut changed = d.clone(); changed.attempts[0].plan_hash = Some(hash(b"changed plan")); assert!(f.apply(&mut sync, &changed, &raw).is_err()); let mut changed = d; changed.attempts[0].previous_outcome_sha256 = Some(hash(b"different outcome")); assert!(f.apply(&mut sync, &changed, &raw).is_err()); } #[test] fn false_clock_expiry_and_invalid_independent_key_stay_closed() { let mut f = Fixture::new(); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); f.clock.trusted = false; assert!(f.apply(&mut sync, &d, &raw).is_err()); assert!(!sync.can_start(&f.db, &f.guard, 1, &f.clock, &f.boot)); f.clock.trusted = true; f.clock.now += Duration::seconds(61); assert!(f.apply(&mut sync, &d, &raw).is_err()); let mut invalid = f.binding.clone(); invalid.public_key_hex = "not a key".into(); assert!(HelperSync::new(invalid, &f.clock).is_err()); let mut changed = f.binding.clone(); changed.public_key_hex = hex::encode(SigningKey::from_bytes(&[43; 32]).verifying_key().to_bytes()); let mut different = HelperSync::new(changed, &f.clock).unwrap(); assert!( different .snapshot(&mut f.db, &f.guard, 2, &f.clock, &f.boot) .is_err() ); } #[test] fn shared_database_only_and_missing_local_start_fail_closed() { let mut f = Fixture::new(); let mut sync = f.sync(); let mut copy = Connection::open_in_memory().unwrap(); assert!( sync.snapshot(&mut copy, &f.guard, 1, &f.clock, &f.boot) .is_err() ); let grant = f.add(script(), None); f.db.execute( "DELETE FROM execution_evidence WHERE grant_id=?1", [grant.grant_id.to_string()], ) .unwrap(); assert!( sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) .is_err() ); } #[test] fn real_local_boot_must_match_signed_reboot_completion() { let mut f = Fixture::new(); let old = f.boot.0; let mut awaiting = unknown(); awaiting.state = "awaiting_reboot".into(); awaiting.reboot_required = true; f.add( Action::Reboot { reason: "mock reboot no native effect".into(), reconnect_timeout_seconds: 120, dependency_group: "acceptance".into(), }, Some(awaiting), ); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let mut d = f.decision(&snapshot, &raw, 1, true); let mut done = success(); done.stdout.clear(); done.exit_code = None; done.evidence = serde_json::json!({"postconditions_passed":true,"new_boot_id":Uuid::new_v4(),"reconciled_after_reboot":true,"inventory_message_id":Uuid::new_v4()}); d.attempts[0].unlock = true; d.attempts[0].outcome_state = "succeeded".into(); d.attempts[0].outcome_bytes = canonical_outcome_bytes(&done).unwrap(); d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); assert!(f.apply(&mut sync, &d, &raw).is_err()); // native current boot is still old f.boot.0 = Uuid::new_v4(); f.clock.now += Duration::seconds(1); let snapshot = f.snapshot(&mut sync, 2); let raw = f.request(&snapshot); let mut d = f.decision(&snapshot, &raw, 2, true); assert_eq!(snapshot.attempts[0].original_boot_id, old); assert_ne!(f.boot.0, old); done.evidence["new_boot_id"] = serde_json::json!(f.boot.0); d.attempts[0].unlock = true; d.attempts[0].outcome_state = "succeeded".into(); d.attempts[0].outcome_bytes = canonical_outcome_bytes(&done).unwrap(); d.attempts[0].outcome_sha256 = Some(hash(&d.attempts[0].outcome_bytes)); let applied = f.apply(&mut sync, &d, &raw).unwrap(); assert!(applied.ready); assert_eq!(applied.snapshot.attempts[0].state, "succeeded"); assert!(sync.can_start(&f.db, &f.guard, 2, &f.clock, &f.boot)); } #[test] fn malformed_original_protobuf_or_digest_does_not_open_gate() { let mut f = Fixture::new(); let mut sync = f.sync(); let snapshot = f.snapshot(&mut sync, 1); let raw = f.request(&snapshot); let d = f.decision(&snapshot, &raw, 1, true); let mut malformed = raw.clone(); malformed.push(0); assert!(f.apply(&mut sync, &d, &malformed).is_err()); let mut malformed = raw.clone(); text(1, &Uuid::new_v4().to_string(), &mut malformed); let mut signed = d.clone(); signed.request_hash = hash(&malformed); assert!(f.apply(&mut sync, &signed, &malformed).is_err()); let mut signed = d; signed.request_hash.clear(); assert!(f.apply(&mut sync, &signed, &raw).is_err()); assert!( sync.snapshot(&mut f.db, &f.guard, 1, &f.clock, &f.boot) .is_err() ); } #[test] fn root_and_system_are_distinct_explicit_helper_domains() { let mut root = Fixture::new(); root.add(script(), Some(unknown())); let before: Vec = root .db .query_row("SELECT start FROM execution_evidence", [], |r| r.get(0)) .unwrap(); let mut wrong = HelperSync::new(root.binding.clone(), &root.clock) .unwrap() .with_helper_identity(HelperIdentity::System); assert!( wrong .snapshot(&mut root.db, &root.guard, 1, &root.clock, &root.boot) .is_err() ); assert_eq!( before, root.db .query_row::, _, _>("SELECT start FROM execution_evidence", [], |r| r.get(0)) .unwrap() ); assert_eq!( root.db .query_row::( "SELECT count(*) FROM consumed WHERE result IS NULL", [], |r| r.get(0) ) .unwrap(), 1 ); let mut system = Fixture::new(); system.identity = HelperIdentity::System; system.add(script(), Some(success())); let mut sync = system.sync(); let snapshot = system.snapshot(&mut sync, 1); assert_eq!(snapshot.attempts[0].execution_identity, "system"); let raw = system.request(&snapshot); let mut wrong = system.decision(&snapshot, &raw, 1, true); wrong.attempts[0].execution_identity = "root".into(); assert!(system.apply(&mut sync, &wrong, &raw).is_err()); assert_eq!( system .db .query_row::( "SELECT count(*) FROM consumed WHERE result IS NULL", [], |r| r.get(0) ) .unwrap(), 1 ); let mut system = Fixture::new(); system.identity = HelperIdentity::System; system.add(script(), Some(success())); let mut sync = system.sync(); let snapshot = system.snapshot(&mut sync, 1); let raw = system.request(&snapshot); let decision = system.decision(&snapshot, &raw, 1, true); assert!(system.apply(&mut sync, &decision, &raw).unwrap().ready); assert!(sync.can_start(&system.db, &system.guard, 1, &system.clock, &system.boot)); let changed = sync.with_helper_identity(HelperIdentity::Root); assert!(!changed.can_start(&system.db, &system.guard, 1, &system.clock, &system.boot)); }