use turkos_os_virtio_input::focus::{ compute_input_focus_binding_digest, InputFocusError, InputFocusRequest, VirtioInputFocusCoordinator, EXPECTED_ACTIVE_TARGET_EVENTS_DELIVERED, EXPECTED_ACTIVE_TARGET_SESSION_ID, EXPECTED_SEQUENTIAL_FOCUSED_APPS, EXPECTED_STALE_TARGET_EVENTS_DELIVERED, VIRTIO_INPUT_FOCUS_ACCEPTANCE, VIRTIO_INPUT_FOCUS_COUNTEREXAMPLE, VIRTIO_INPUT_FOCUS_INVARIANT, VIRTIO_INPUT_FOCUS_POSITIVE, VIRTIO_INPUT_FOCUS_SCHEMA, }; fn valid_request() -> InputFocusRequest { let sequential_focused_apps = EXPECTED_SEQUENTIAL_FOCUSED_APPS; let active_target_session_id = EXPECTED_ACTIVE_TARGET_SESSION_ID; let active_target_events_delivered = EXPECTED_ACTIVE_TARGET_EVENTS_DELIVERED; let stale_target_events_delivered = EXPECTED_STALE_TARGET_EVENTS_DELIVERED; let focus_epoch = 42; let previous_focus_epoch = 41; let focus_routing_digest = 0x2260_464f_4355_5332; let focus_binding_digest = compute_input_focus_binding_digest( sequential_focused_apps, active_target_session_id, active_target_events_delivered, stale_target_events_delivered, focus_epoch, focus_routing_digest, ); InputFocusRequest { cross_window_input_theft: false, stale_focus_session_replay: false, sequential_focused_apps, active_target_session_id, active_target_events_delivered, stale_target_events_delivered, only_current_target_receives_events: true, focus_epoch, previous_focus_epoch, focus_routing_digest, focus_binding_digest, } } #[test] fn os_virtio_input_focus_contract_constants() { assert_eq!( VIRTIO_INPUT_FOCUS_SCHEMA, "turkos.os.virtio_input.focus.v1" ); assert_eq!( VIRTIO_INPUT_FOCUS_INVARIANT, "Bir session başka pencereye input çalamaz" ); assert_eq!(VIRTIO_INPUT_FOCUS_POSITIVE, "İki odaklı sırayla uygulama"); assert_eq!( VIRTIO_INPUT_FOCUS_COUNTEREXAMPLE, "Eski focus session replay" ); assert_eq!( VIRTIO_INPUT_FOCUS_ACCEPTANCE, "Yalnız güncel hedef olayı alır" ); } #[test] fn os_virtio_input_focus_positive() { let mut coord = VirtioInputFocusCoordinator::new(); let req = valid_request(); let receipt = coord .commit_focus_routing(&req) .expect("Sequential two-app focus routing must succeed"); assert!(coord.is_committed()); assert_eq!(coord.committed_total(), 1); assert_eq!(coord.rejected_total(), 0); assert_eq!( receipt.sequential_focused_apps, EXPECTED_SEQUENTIAL_FOCUSED_APPS ); assert_eq!( receipt.active_target_session_id, EXPECTED_ACTIVE_TARGET_SESSION_ID ); assert_eq!( receipt.active_target_events_delivered, EXPECTED_ACTIVE_TARGET_EVENTS_DELIVERED ); assert_eq!( receipt.stale_target_events_delivered, EXPECTED_STALE_TARGET_EVENTS_DELIVERED ); assert!(receipt.only_current_target_receives_events); assert_eq!(receipt.focus_epoch, req.focus_epoch); assert_eq!(receipt.focus_routing_digest, req.focus_routing_digest); assert_eq!( receipt.receipt_digest, req.focus_binding_digest ^ 0x2260_ACE0_464f_4353 ); assert_eq!(coord.committed_receipt(), Some(receipt)); let dup = coord.commit_focus_routing(&req); assert_eq!(dup, Err(InputFocusError::AlreadyCommitted)); assert_eq!(coord.rejected_total(), 1); } #[test] fn os_virtio_input_focus_negative() { let mut coord = VirtioInputFocusCoordinator::new(); // 1. Cross-window input theft attempt let mut r1 = valid_request(); r1.cross_window_input_theft = true; assert_eq!( coord.commit_focus_routing(&r1), Err(InputFocusError::CrossWindowInputTheftAttempt) ); assert_eq!(coord.theft_replay_refusals(), 1); // 2. Stale focus session replay let mut r2 = valid_request(); r2.stale_focus_session_replay = true; assert_eq!( coord.commit_focus_routing(&r2), Err(InputFocusError::StaleFocusSessionReplay) ); assert_eq!(coord.theft_replay_refusals(), 2); // 3. Sequential focused apps count mismatch let mut r3 = valid_request(); r3.sequential_focused_apps = 1; assert_eq!( coord.commit_focus_routing(&r3), Err(InputFocusError::SequentialFocusedAppsCountMismatch) ); assert_eq!(coord.routing_target_refusals(), 1); // 4. Active target session ID mismatch let mut r4 = valid_request(); r4.active_target_session_id = 0xDEAD; assert_eq!( coord.commit_focus_routing(&r4), Err(InputFocusError::ActiveTargetSessionIdMismatch) ); assert_eq!(coord.routing_target_refusals(), 2); // 5. Active target events delivered mismatch let mut r5 = valid_request(); r5.active_target_events_delivered = 0; assert_eq!( coord.commit_focus_routing(&r5), Err(InputFocusError::ActiveTargetEventsMismatch) ); assert_eq!(coord.routing_target_refusals(), 3); // 6. Stale target received events let mut r6 = valid_request(); r6.stale_target_events_delivered = 1; assert_eq!( coord.commit_focus_routing(&r6), Err(InputFocusError::StaleTargetReceivedEvents) ); assert_eq!(coord.routing_target_refusals(), 4); // 7. Only current target flag false let mut r7 = valid_request(); r7.only_current_target_receives_events = false; assert_eq!( coord.commit_focus_routing(&r7), Err(InputFocusError::StaleTargetReceivedEvents) ); assert_eq!(coord.routing_target_refusals(), 5); // 8. Unbound input focus digest let mut r8 = valid_request(); r8.focus_binding_digest ^= 0xDEAD; assert_eq!( coord.commit_focus_routing(&r8), Err(InputFocusError::UnboundInputFocusDigest) ); assert_eq!(coord.routing_target_refusals(), 6); assert!(!coord.is_committed()); assert_eq!(coord.committed_receipt(), None); } #[test] fn os_virtio_input_focus_validation_and_bounds() { let mut coord = VirtioInputFocusCoordinator::new(); let mut zero_epoch = valid_request(); zero_epoch.focus_epoch = 0; assert_eq!( coord.commit_focus_routing(&zero_epoch), Err(InputFocusError::ZeroFocusEpoch) ); let mut non_mono = valid_request(); non_mono.focus_epoch = non_mono.previous_focus_epoch; assert_eq!( coord.commit_focus_routing(&non_mono), Err(InputFocusError::NonMonotonicFocusEpoch) ); let mut zero_digest = valid_request(); zero_digest.focus_routing_digest = 0; assert_eq!( coord.commit_focus_routing(&zero_digest), Err(InputFocusError::ZeroFocusRoutingDigest) ); }