signer_recovery.rs (5577B)
1 //! Scope mechanics; concrete local cryptography is exercised by the facade. 2 use super::*; 3 use radroots_signing::{ 4 issuance::{ 5 ExactRecoveryCapability, ExactRecoveryKind, ExactRecoveryOutcome, ExactRecoveryRequest, 6 RejectionReason, 7 }, 8 request::CancellationSignal, 9 }; 10 fn supported(second: bool) -> ProviderState { 11 state(second, false).with_exact_recovery(ExactRecoveryCapability::LocalExactReplay) 12 } 13 fn request(second: bool, operation: u8) -> ExactRecoveryRequest { 14 ExactRecoveryRequest::new( 15 exact_intent(second, operation), 16 ProviderCall::new( 17 key(second), 18 core::num::NonZeroU64::new(1_700_000_020_000).unwrap(), 19 CancellationSignal::new(), 20 ), 21 ) 22 .unwrap() 23 } 24 #[test] 25 fn recovery_refuses_unobserved_unsupported_changed_identity_and_changed_capability_without_fallback() 26 { 27 let Fixture { 28 session, control, .. 29 } = scope(); 30 *control.current.lock().unwrap() = ProviderState::new( 31 key(false), 32 PrivateCryptoCapability::Unavailable, 33 PrivateCryptoCapability::Nip44V2, 34 ReplayCapability::LocalReplaySafe, 35 ); 36 for observed in [false, true] { 37 if observed { 38 block_on(session.observe()).unwrap(); 39 } 40 let request = request(false, 1); 41 let expected = request.binding(); 42 let result = block_on(session.recover_exact(request)); 43 assert_eq!(result.binding(), expected); 44 assert!(matches!( 45 result.kind(), 46 ExactRecoveryKind::Indeterminate(ProviderError::CapabilityUnavailable) 47 )); 48 } 49 *control.current.lock().unwrap() = supported(false); 50 block_on(session.observe()).unwrap(); 51 assert!(matches!( 52 block_on(session.recover_exact(request(true, 1))).kind(), 53 ExactRecoveryKind::Indeterminate(ProviderError::IdentityChanged) 54 )); 55 assert!(!session.identity_changed()); 56 let result = block_on(session.recover_exact(request(false, 1))); 57 assert!(matches!( 58 result.kind(), 59 ExactRecoveryKind::Indeterminate(ProviderError::ProviderFailure) 60 )); 61 assert_eq!(control.recovery_calls.load(Ordering::SeqCst), 1); 62 *control.current.lock().unwrap() = state(false, false); 63 assert!(matches!( 64 block_on(session.recover_exact(request(false, 1))).kind(), 65 ExactRecoveryKind::Indeterminate(ProviderError::CapabilityUnavailable) 66 )); 67 assert_eq!(control.recovery_calls.load(Ordering::SeqCst), 1); 68 *control.current.lock().unwrap() = supported(false); 69 block_on(session.observe()).unwrap(); 70 *control.current.lock().unwrap() = supported(true); 71 assert!(matches!( 72 block_on(session.recover_exact(request(false, 1))).kind(), 73 ExactRecoveryKind::Indeterminate(ProviderError::IdentityChanged) 74 )); 75 assert!(session.identity_changed()); 76 assert_eq!(control.recovery_calls.load(Ordering::SeqCst), 1); 77 assert_eq!(control.sign_calls.load(Ordering::SeqCst), 0); 78 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 0); 79 block_on(session.close()).unwrap(); 80 assert!(matches!( 81 block_on(session.recover_exact(request(false, 1))).kind(), 82 ExactRecoveryKind::Indeterminate(ProviderError::CapabilityUnavailable) 83 )); 84 } 85 86 #[test] 87 fn pending_recovery_keeps_callbacks_until_settlement_and_rejects_a_foreign_callback() { 88 let Fixture { 89 session, 90 control, 91 provider, 92 callbacks, 93 } = scope(); 94 *control.current.lock().unwrap() = supported(false); 95 block_on(session.observe()).unwrap(); 96 let input = request(false, 1); 97 let expected = input.binding(); 98 let (send, receive) = oneshot::channel(); 99 *control.recovery_response.lock().unwrap() = Some(receive); 100 let mut recover = Box::pin(session.recover_exact(input)); 101 let mut cx = Context::from_waker(noop_waker_ref()); 102 assert!(recover.as_mut().poll(&mut cx).is_pending()); 103 let mut close = Box::pin(session.close()); 104 assert!(close.as_mut().poll(&mut cx).is_pending()); 105 assert_eq!(control.close_attempts.load(Ordering::SeqCst), 0); 106 assert!(provider.upgrade().is_some() && callbacks.upgrade().is_some()); 107 send.send(ExactRecoveryOutcome::indeterminate( 108 request(false, 2).binding(), 109 ProviderError::ProviderFailure, 110 )) 111 .unwrap(); 112 let Poll::Ready(result) = recover.as_mut().poll(&mut cx) else { 113 panic!("recovery did not settle") 114 }; 115 assert_eq!(result.binding(), expected); 116 let ExactRecoveryKind::Rejected(rejected) = result.kind() else { 117 panic!("foreign callback promoted") 118 }; 119 assert_eq!(rejected.reason(), RejectionReason::BindingMismatch); 120 assert!(rejected.fingerprint().is_none()); 121 assert!(matches!(close.as_mut().poll(&mut cx), Poll::Ready(Ok(())))); 122 assert!(provider.upgrade().is_none() && callbacks.upgrade().is_none()); 123 assert_eq!(control.sign_calls.load(Ordering::SeqCst), 0); 124 } 125 126 #[test] 127 fn failed_initial_observation_cannot_authorize_recovery() { 128 let Fixture { 129 session, control, .. 130 } = scope(); 131 *control.current.lock().unwrap() = supported(false); 132 let (send, receive) = oneshot::channel(); 133 drop(send); 134 *control.response.lock().unwrap() = Some(receive); 135 assert!(block_on(session.observe()).is_err()); 136 assert!(matches!( 137 block_on(session.recover_exact(request(false, 1))).kind(), 138 ExactRecoveryKind::Indeterminate(ProviderError::CapabilityUnavailable) 139 )); 140 assert_eq!(control.recovery_calls.load(Ordering::SeqCst), 0); 141 block_on(session.close()).unwrap(); 142 }