signer_commerce.rs (6432B)
1 //! Controlled scope behavior; real cryptography remains in Nostr's retained tests. 2 use super::*; 3 use radroots_protocol::{ 4 command::CapturedOrderAcceptance, 5 commerce::{self, CommerceSigningPlan, EncodedCommerceRumor}, 6 }; 7 use radroots_signing::request::CancellationSignal; 8 9 fn data() -> (EncodedCommerceRumor, CommerceSigningPlan) { 10 let command = CapturedOrderAcceptance::parse(include_bytes!( 11 "../fixtures/commerce_verification/command_wire.json" 12 )) 13 .unwrap(); 14 let record = commerce::decode(include_bytes!( 15 "../fixtures/commerce_verification/record_wire.json" 16 )) 17 .unwrap(); 18 let rumor = EncodedCommerceRumor::capture(record.content()).unwrap(); 19 let plan = CommerceSigningPlan::parse( 20 command.context().operation_id(), 21 command.digest(), 22 &rumor, 23 include_bytes!("../fixtures/commerce_verification/plan_wire.bin"), 24 ) 25 .unwrap(); 26 (rumor, plan) 27 } 28 fn supported() -> ProviderState { 29 state(false, true).with_exact_issuance(Some(ReplayCapability::NonReplayable)) 30 } 31 fn call(second: bool) -> ProviderCall { 32 ProviderCall::new( 33 key(second), 34 core::num::NonZeroU64::new(600000).unwrap(), 35 CancellationSignal::new(), 36 ) 37 } 38 fn configure(control: &Control) { 39 *control.current.lock().unwrap() = supported(); 40 *control.commerce_plaintext.lock().unwrap() = Some(data().0.protected_wire().to_owned()); 41 } 42 #[test] 43 fn scope_verifies_complete_original_plan_and_refuses_unobserved_foreign_changed_and_closed_calls() { 44 let Fixture { 45 session, 46 control, 47 provider, 48 callbacks, 49 } = scope(); 50 configure(&control); 51 let (rumor, plan) = data(); 52 assert!(matches!( 53 block_on(session.verify_commerce_plan(call(false), &rumor, plan)), 54 Err(ProviderError::CapabilityUnavailable) 55 )); 56 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 0); 57 block_on(session.observe()).unwrap(); 58 let (rumor, plan) = data(); 59 assert!(matches!( 60 block_on(session.verify_commerce_plan(call(true), &rumor, plan)), 61 Err(ProviderError::IdentityChanged) 62 )); 63 assert!(!session.identity_changed()); 64 let (rumor, plan) = data(); 65 let verified = block_on(session.verify_commerce_plan(call(false), &rumor, plan)).unwrap(); 66 assert_eq!( 67 verified.plan().protected_wire(), 68 include_bytes!("../fixtures/commerce_verification/plan_wire.bin") 69 ); 70 assert_eq!(verified.deadline_unix_ms(), 600000); 71 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 2); 72 for unavailable in [ 73 state(false, false).with_exact_issuance(Some(ReplayCapability::NonReplayable)), 74 state(false, true), 75 ] { 76 *control.current.lock().unwrap() = unavailable; 77 let (rumor, plan) = data(); 78 assert!(matches!( 79 block_on(session.verify_commerce_plan(call(false), &rumor, plan)), 80 Err(ProviderError::CapabilityUnavailable) 81 )); 82 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 2); 83 } 84 *control.current.lock().unwrap() = 85 state(true, true).with_exact_issuance(Some(ReplayCapability::NonReplayable)); 86 let (rumor, plan) = data(); 87 assert!(matches!( 88 block_on(session.verify_commerce_plan(call(false), &rumor, plan)), 89 Err(ProviderError::IdentityChanged) 90 )); 91 assert!(session.identity_changed()); 92 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 2); 93 assert_eq!(control.sign_calls.load(Ordering::SeqCst), 0); 94 block_on(session.close()).unwrap(); 95 assert!(provider.upgrade().is_none() && callbacks.upgrade().is_none()); 96 let (rumor, plan) = data(); 97 assert!(matches!( 98 block_on(session.verify_commerce_plan(call(false), &rumor, plan)), 99 Err(ProviderError::CapabilityUnavailable) 100 )); 101 } 102 103 #[test] 104 fn pending_actual_decryption_keeps_callback_roots_until_settlement_and_close_refuses_completion() { 105 let Fixture { 106 session, 107 control, 108 provider, 109 callbacks, 110 } = scope(); 111 configure(&control); 112 block_on(session.observe()).unwrap(); 113 let (send, receive) = oneshot::channel(); 114 *control.crypto_response.lock().unwrap() = Some(receive); 115 let (rumor, plan) = data(); 116 let mut verify = Box::pin(session.verify_commerce_plan(call(false), &rumor, plan)); 117 let mut cx = Context::from_waker(noop_waker_ref()); 118 assert!(verify.as_mut().poll(&mut cx).is_pending()); 119 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 1); 120 let mut close = Box::pin(session.close()); 121 assert!(close.as_mut().poll(&mut cx).is_pending()); 122 assert_eq!(control.close_attempts.load(Ordering::SeqCst), 0); 123 assert!(provider.upgrade().is_some() && callbacks.upgrade().is_some()); 124 send.send(()).unwrap(); 125 assert!(matches!( 126 verify.as_mut().poll(&mut cx), 127 Poll::Ready(Err(ProviderError::CapabilityUnavailable)) 128 )); 129 assert!(matches!(close.as_mut().poll(&mut cx), Poll::Ready(Ok(())))); 130 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 1); 131 assert_eq!(control.sign_calls.load(Ordering::SeqCst), 0); 132 assert!(provider.upgrade().is_none() && callbacks.upgrade().is_none()); 133 } 134 135 #[test] 136 fn identity_change_during_decryption_latches_and_never_confirms_a_plan_or_calls_signing() { 137 let Fixture { 138 session, control, .. 139 } = scope(); 140 configure(&control); 141 block_on(session.observe()).unwrap(); 142 let (send, receive) = oneshot::channel(); 143 *control.crypto_response.lock().unwrap() = Some(receive); 144 let (rumor, plan) = data(); 145 let mut verify = Box::pin(session.verify_commerce_plan(call(false), &rumor, plan)); 146 let mut cx = Context::from_waker(noop_waker_ref()); 147 assert!(verify.as_mut().poll(&mut cx).is_pending()); 148 *control.current.lock().unwrap() = 149 state(true, true).with_exact_issuance(Some(ReplayCapability::NonReplayable)); 150 send.send(()).unwrap(); 151 assert!(matches!( 152 block_on(verify), 153 Err(ProviderError::IdentityChanged) 154 )); 155 assert!(session.identity_changed()); 156 configure(&control); 157 let (rumor, plan) = data(); 158 assert!(matches!( 159 block_on(session.verify_commerce_plan(call(false), &rumor, plan)), 160 Err(ProviderError::IdentityChanged) 161 )); 162 assert_eq!(control.crypto_calls.load(Ordering::SeqCst), 1); 163 assert_eq!(control.sign_calls.load(Ordering::SeqCst), 0); 164 block_on(session.close()).unwrap(); 165 }