From 9a7f13f8b93a78e92d4c7fd7281a00a40f1b9b75 Mon Sep 17 00:00:00 2001 From: Ninja3047 Date: Sun, 10 Jul 2022 23:25:54 -0400 Subject: [PATCH] add crypto tests --- common/src/crypto.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/common/src/crypto.rs b/common/src/crypto.rs index d0c274c..2b666b8 100644 --- a/common/src/crypto.rs +++ b/common/src/crypto.rs @@ -294,3 +294,43 @@ fn get_argon2() -> Argon2<'static> { pub fn get_csrng() -> impl CryptoRng + Rng { rand::thread_rng() } + +#[cfg(test)] +mod test { + use super::open_in_place; + use super::seal_in_place; + use crate::crypto::SecretVec; + + macro_rules! test_encryption { + ($($name:ident, $content:expr, $password:expr),*) => { + $( + #[test] + fn $name() { + let mut m = $content; + let n: Vec = $content; + let key = seal_in_place(&mut m, $password).unwrap(); + assert_ne!(m, n); + assert!(open_in_place(&mut m, &key, $password).is_ok()); + assert_eq!(m, n); + } + )* + }; + } + + test_encryption!(empty, vec![], None); + test_encryption!( + empty_password, + vec![], + Some(SecretVec::from(b"password".to_vec())) + ); + test_encryption!( + normal, + vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + None + ); + test_encryption!( + normal_password, + vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], + Some(SecretVec::from(b"password".to_vec())) + ); +}