provider.rs (5028B)
1 use radroots_identity::PublicKey; 2 use radroots_signing::{provider::*, recovery::ReplayCapability, request::CancellationSignal}; 3 use std::{error::Error as _, num::NonZeroU64, str::FromStr}; 4 5 fn identity() -> PublicKey { 6 PublicKey::from_str("585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df").unwrap() 7 } 8 fn other() -> PublicKey { 9 PublicKey::from_str("e0266e3cfb0d2886f91c73f5f868f3b98273713e5fcd97c081663f5518a4b3af").unwrap() 10 } 11 12 #[test] 13 fn private_values_admit_exact_byte_bounds_without_diagnostic_or_wire_exposure() { 14 let private = "private buyer question — do not log"; 15 let text = PrivateText::new(private).unwrap(); 16 assert_eq!(text.expose(), private); 17 assert_eq!(format!("{text:?}"), "PrivateText([redacted])"); 18 assert!(matches!( 19 PrivateText::new(""), 20 Err(ProviderError::InvalidInput) 21 )); 22 assert!(matches!( 23 PrivateText::from_owned(String::new()), 24 Err(ProviderError::InvalidInput) 25 )); 26 for text in [ 27 "a".repeat(PRIVATE_CRYPTO_MAX_BYTES), 28 "é".repeat(PRIVATE_CRYPTO_MAX_BYTES / 2), 29 ] { 30 assert_eq!( 31 PrivateText::new(&text).unwrap().expose().len(), 32 PRIVATE_CRYPTO_MAX_BYTES 33 ); 34 assert_eq!( 35 PrivateText::from_owned(text).unwrap().expose().len(), 36 PRIVATE_CRYPTO_MAX_BYTES 37 ); 38 } 39 let too_long = "a".repeat(PRIVATE_CRYPTO_MAX_BYTES + 1); 40 assert!(matches!( 41 PrivateText::new(&too_long), 42 Err(ProviderError::LimitExceeded) 43 )); 44 assert!(matches!( 45 PrivateText::from_owned(too_long), 46 Err(ProviderError::LimitExceeded) 47 )); 48 for bad in ["", "abc", "!", "a".repeat(131).as_str()] { 49 assert!(matches!( 50 PrivateCiphertext::parse(bad), 51 Err(ProviderError::InvalidInput) 52 )); 53 } 54 assert!(matches!( 55 PrivateCiphertext::parse("#future"), 56 Err(ProviderError::UnsupportedVersion) 57 )); 58 assert!(matches!( 59 PrivateCiphertext::parse(&"a".repeat(PRIVATE_CRYPTO_MAX_BYTES + 1)), 60 Err(ProviderError::LimitExceeded) 61 )); 62 assert!(matches!( 63 PrivateCiphertext::parse(&(String::from("!") + &"a".repeat(132))), 64 Err(ProviderError::InvalidInput) 65 )); 66 let encoded = "a".repeat(PRIVATE_CRYPTO_MAX_BYTES); 67 let ciphertext = PrivateCiphertext::parse(&encoded).unwrap(); 68 assert_eq!(ciphertext.as_str(), encoded); 69 assert_eq!(format!("{ciphertext:?}"), "PrivateCiphertext([redacted])"); 70 } 71 72 #[test] 73 fn call_binding_is_not_interchangeable_with_another_identity_or_expired_budget() { 74 let signal = CancellationSignal::new(); 75 let call = ProviderCall::new(identity(), NonZeroU64::new(200).unwrap(), signal.clone()); 76 assert_eq!(call.identity(), identity()); 77 assert_eq!(call.check_before_effect(identity(), 100), Ok(())); 78 assert_eq!( 79 call.check_before_effect(other(), 100), 80 Err(ProviderError::IdentityChanged) 81 ); 82 assert_eq!( 83 call.check_before_effect(identity(), 0), 84 Err(ProviderError::ProviderFailure) 85 ); 86 assert_eq!( 87 call.check_before_effect(identity(), 200), 88 Err(ProviderError::DeadlineExceeded) 89 ); 90 assert_eq!( 91 call.check_before_effect(identity(), 201), 92 Err(ProviderError::DeadlineExceeded) 93 ); 94 signal.cancel(); 95 assert_eq!( 96 call.check_before_effect(identity(), 100), 97 Err(ProviderError::Cancelled) 98 ); 99 assert_eq!( 100 call.clone().check_before_effect(identity(), 100), 101 Err(ProviderError::Cancelled) 102 ); 103 } 104 105 #[test] 106 fn capabilities_are_separate_method_observations_and_errors_retain_no_cause() { 107 for encryption in [ 108 PrivateCryptoCapability::Unavailable, 109 PrivateCryptoCapability::Nip44V2, 110 ] { 111 for decryption in [ 112 PrivateCryptoCapability::Unavailable, 113 PrivateCryptoCapability::Nip44V2, 114 ] { 115 for replay in [ 116 ReplayCapability::ExactReplayByRequestId, 117 ReplayCapability::LocalReplaySafe, 118 ReplayCapability::NonReplayable, 119 ] { 120 let state = ProviderState::new(identity(), encryption, decryption, replay); 121 assert_eq!(state.identity(), identity()); 122 assert_eq!(state.encryption(), encryption); 123 assert_eq!(state.decryption(), decryption); 124 assert_eq!(state.signing_replay(), replay); 125 } 126 } 127 } 128 for error in [ 129 ProviderError::InvalidInput, 130 ProviderError::LimitExceeded, 131 ProviderError::UnsupportedVersion, 132 ProviderError::CapabilityUnavailable, 133 ProviderError::IdentityChanged, 134 ProviderError::AuthenticationFailed, 135 ProviderError::Cancelled, 136 ProviderError::DeadlineExceeded, 137 ProviderError::ProviderFailure, 138 ] { 139 assert_eq!(error.to_string(), error.as_str()); 140 assert!(error.source().is_none()); 141 assert!(format!("{error:?}").len() < 32); 142 } 143 }