Skip to content

Commit

Permalink
feat: Add error type conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Jul 27, 2024
1 parent 6dd9675 commit 07b99ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
14 changes: 14 additions & 0 deletions crates/scryptenc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl std::error::Error for Error {
}
}

impl From<InvalidParams> for Error {
fn from(err: InvalidParams) -> Self {
Self::InvalidParams(err)
}
}

/// A specialized [`Result`](result::Result) type for read and write operations
/// for the scrypt encrypted data format.
///
Expand Down Expand Up @@ -340,6 +346,14 @@ mod tests {
.is::<MacError>());
}

#[test]
fn from_invalid_params_to_error() {
assert_eq!(
Error::from(InvalidParams),
Error::InvalidParams(InvalidParams)
);
}

#[test]
fn result_type() {
use core::any;
Expand Down
5 changes: 2 additions & 3 deletions crates/scryptenc/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ impl Header {
.try_into()
.expect("size of `p` parameter should be 4 bytes"),
);
let params = scrypt::Params::new(log_n, r, p, scrypt::Params::RECOMMENDED_LEN)
.map(Params::from)
.map_err(Error::InvalidParams)?;
let params =
scrypt::Params::new(log_n, r, p, scrypt::Params::RECOMMENDED_LEN).map(Params::from)?;
let salt = data[16..48]
.try_into()
.expect("size of salt should be 32 bytes");
Expand Down
6 changes: 3 additions & 3 deletions crates/scryptenc/tests/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ fn invalid_params() {
{
data[7] = 65;
let err = Decryptor::new(&data, PASSPHRASE).unwrap_err();
assert_eq!(err, Error::InvalidParams(InvalidParams));
assert_eq!(err, InvalidParams.into());
}

{
data[8..12].copy_from_slice(&u32::to_be_bytes(0));
let err = Decryptor::new(&data, PASSPHRASE).unwrap_err();
assert_eq!(err, Error::InvalidParams(InvalidParams));
assert_eq!(err, InvalidParams.into());
}

{
data[12..16].copy_from_slice(&u32::to_be_bytes(0));
let err = Decryptor::new(&data, PASSPHRASE).unwrap_err();
assert_eq!(err, Error::InvalidParams(InvalidParams));
assert_eq!(err, InvalidParams.into());
}
}

Expand Down

0 comments on commit 07b99ae

Please sign in to comment.