Skip to main content

age_crypto/errors/
decrypt.rs

1use std::io;
2use thiserror::Error;
3#[derive(Debug, Error)]
4pub enum DecryptError {
5    #[error("Invalid identity: {0}")]
6    InvalidIdentity(String),
7    #[error("Invalid ciphertext: {0}")]
8    InvalidCiphertext(String),
9    #[error("Decryption failed: {0}")]
10    Failed(String),
11    #[error("I/O error: {0}")]
12    Io(#[from] io::Error),
13}
14#[cfg(test)]
15mod tests {
16    use super::*;
17    use std::io::{self, ErrorKind};
18    #[test]
19    fn test_invalid_identity_display() {
20        let err = DecryptError::InvalidIdentity("bad bech32".into());
21        assert_eq!(format!("{}", err), "Invalid identity: bad bech32");
22    }
23    #[test]
24    fn test_invalid_ciphertext_display() {
25        let err = DecryptError::InvalidCiphertext("header too short".into());
26        assert_eq!(format!("{}", err), "Invalid ciphertext: header too short");
27    }
28    #[test]
29    fn test_failed_display() {
30        let err = DecryptError::Failed("wrong key".into());
31        assert_eq!(format!("{}", err), "Decryption failed: wrong key");
32    }
33    #[test]
34    fn test_io_error_display() {
35        let io_err = io::Error::new(ErrorKind::UnexpectedEof, "stream ended");
36        let err = DecryptError::Io(io_err);
37        assert_eq!(format!("{}", err), "I/O error: stream ended");
38    }
39    #[test]
40    fn test_from_io_error_conversion() {
41        let io_err: io::Error = ErrorKind::PermissionDenied.into();
42        let decrypt_err: DecryptError = io_err.into();
43        assert!(matches!(decrypt_err, DecryptError::Io(_)));
44    }
45    #[test]
46    fn test_error_is_send_sync() {
47        fn assert_send_sync<T: Send + Sync>() {}
48        assert_send_sync::<DecryptError>();
49    }
50    #[test]
51    fn test_error_source_chain() {
52        use std::error::Error as StdError;
53        let io_err = io::Error::other("underlying");
54        let decrypt_err = DecryptError::Io(io_err);
55        assert!(decrypt_err.source().is_some());
56    }
57}