Improve error display in tests

This commit is contained in:
Rodolphe Bréard 2024-03-02 11:05:56 +01:00
parent 423476c987
commit a0bbd6bf00
3 changed files with 18 additions and 17 deletions

View file

@ -6,6 +6,7 @@ use crate::{storage, InputKeyMaterialList};
pub(crate) type DecryptionFunction = dyn Fn(&[u8], &EncryptedData, &str) -> Result<Vec<u8>>; pub(crate) type DecryptionFunction = dyn Fn(&[u8], &EncryptedData, &str) -> Result<Vec<u8>>;
pub(crate) type EncryptionFunction = dyn Fn(&[u8], &[u8], &str) -> Result<EncryptedData>; pub(crate) type EncryptionFunction = dyn Fn(&[u8], &[u8], &str) -> Result<EncryptedData>;
#[derive(Debug)]
pub(crate) struct EncryptedData { pub(crate) struct EncryptedData {
pub(crate) nonce: Vec<u8>, pub(crate) nonce: Vec<u8>,
pub(crate) ciphertext: Vec<u8>, pub(crate) ciphertext: Vec<u8>,
@ -67,14 +68,14 @@ mod tests {
// Encrypt // Encrypt
let lst = get_ikm_lst(); let lst = get_ikm_lst();
let res = encrypt(&lst, &[], TEST_DATA, EMPTY_DATA_CTX); let res = encrypt(&lst, &[], TEST_DATA, EMPTY_DATA_CTX);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let ciphertext = res.unwrap(); let ciphertext = res.unwrap();
assert!(ciphertext.starts_with("AQAAAA:")); assert!(ciphertext.starts_with("AQAAAA:"));
assert_eq!(ciphertext.len(), 98); assert_eq!(ciphertext.len(), 98);
// Decrypt // Decrypt
let res = decrypt(&lst, &[], &ciphertext, EMPTY_DATA_CTX); let res = decrypt(&lst, &[], &ciphertext, EMPTY_DATA_CTX);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let plaintext = res.unwrap(); let plaintext = res.unwrap();
assert_eq!(plaintext, TEST_DATA); assert_eq!(plaintext, TEST_DATA);
} }
@ -84,14 +85,14 @@ mod tests {
// Encrypt // Encrypt
let lst = get_ikm_lst(); let lst = get_ikm_lst();
let res = encrypt(&lst, TEST_KEY_CTX, TEST_DATA, TEST_DATA_CTX); let res = encrypt(&lst, TEST_KEY_CTX, TEST_DATA, TEST_DATA_CTX);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let ciphertext = res.unwrap(); let ciphertext = res.unwrap();
assert!(ciphertext.starts_with("AQAAAA:")); assert!(ciphertext.starts_with("AQAAAA:"));
assert_eq!(ciphertext.len(), 98); assert_eq!(ciphertext.len(), 98);
// Decrypt // Decrypt
let res = decrypt(&lst, TEST_KEY_CTX, &ciphertext, TEST_DATA_CTX); let res = decrypt(&lst, TEST_KEY_CTX, &ciphertext, TEST_DATA_CTX);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let plaintext = res.unwrap(); let plaintext = res.unwrap();
assert_eq!(plaintext, TEST_DATA); assert_eq!(plaintext, TEST_DATA);
} }

View file

@ -181,7 +181,7 @@ mod tests {
assert_eq!(lst.ikm_lst.len(), 0); assert_eq!(lst.ikm_lst.len(), 0);
let res = lst.add_ikm(); let res = lst.add_ikm();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
assert_eq!(lst.id_counter, 1); assert_eq!(lst.id_counter, 1);
assert_eq!(lst.ikm_lst.len(), 1); assert_eq!(lst.ikm_lst.len(), 1);
assert!(lst.ikm_lst.first().is_some()); assert!(lst.ikm_lst.first().is_some());
@ -193,12 +193,12 @@ mod tests {
Scheme::XChaCha20Poly1305WithBlake3, Scheme::XChaCha20Poly1305WithBlake3,
Duration::from_secs(crate::DEFAULT_IKM_DURATION), Duration::from_secs(crate::DEFAULT_IKM_DURATION),
); );
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
assert_eq!(lst.id_counter, 2); assert_eq!(lst.id_counter, 2);
assert_eq!(lst.ikm_lst.len(), 2); assert_eq!(lst.ikm_lst.len(), 2);
let res = lst.add_ikm(); let res = lst.add_ikm();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
assert_eq!(lst.id_counter, 3); assert_eq!(lst.id_counter, 3);
assert_eq!(lst.ikm_lst.len(), 3); assert_eq!(lst.ikm_lst.len(), 3);
} }
@ -211,7 +211,7 @@ mod tests {
assert_eq!(lst.ikm_lst.len(), 0); assert_eq!(lst.ikm_lst.len(), 0);
let res = lst.export(); let res = lst.export();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let s = res.unwrap(); let s = res.unwrap();
assert_eq!(&s, "AAAAAA"); assert_eq!(&s, "AAAAAA");
} }
@ -223,7 +223,7 @@ mod tests {
let _ = lst.add_ikm(); let _ = lst.add_ikm();
let res = lst.export(); let res = lst.export();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let s = res.unwrap(); let s = res.unwrap();
assert_eq!(s.len(), 83); assert_eq!(s.len(), 83);
} }
@ -233,7 +233,7 @@ mod tests {
let s = let s =
"AQAAAA:AQAAAAEAAAC_vYEw1ujVG5i-CtoPYSzik_6xaAq59odjPm5ij01-e6zz4mUAAAAALJGBiwAAAAAA"; "AQAAAA:AQAAAAEAAAC_vYEw1ujVG5i-CtoPYSzik_6xaAq59odjPm5ij01-e6zz4mUAAAAALJGBiwAAAAAA";
let res = InputKeyMaterialList::import(s); let res = InputKeyMaterialList::import(s);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let lst = res.unwrap(); let lst = res.unwrap();
assert_eq!(lst.id_counter, 1); assert_eq!(lst.id_counter, 1);
assert_eq!(lst.ikm_lst.len(), 1); assert_eq!(lst.ikm_lst.len(), 1);
@ -256,11 +256,11 @@ mod tests {
let lst = InputKeyMaterialList::new(); let lst = InputKeyMaterialList::new();
let res = lst.export(); let res = lst.export();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let s = res.unwrap(); let s = res.unwrap();
let res = InputKeyMaterialList::import(&s); let res = InputKeyMaterialList::import(&s);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let lst_bis = res.unwrap(); let lst_bis = res.unwrap();
assert_eq!(lst_bis.id_counter, lst.id_counter); assert_eq!(lst_bis.id_counter, lst.id_counter);
assert_eq!(lst_bis.id_counter, 0); assert_eq!(lst_bis.id_counter, 0);
@ -277,11 +277,11 @@ mod tests {
} }
let res = lst.export(); let res = lst.export();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let s = res.unwrap(); let s = res.unwrap();
let res = InputKeyMaterialList::import(&s); let res = InputKeyMaterialList::import(&s);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let lst_bis = res.unwrap(); let lst_bis = res.unwrap();
assert_eq!(lst_bis.id_counter, lst.id_counter); assert_eq!(lst_bis.id_counter, lst.id_counter);
assert_eq!(lst_bis.id_counter, 10); assert_eq!(lst_bis.id_counter, 10);
@ -362,7 +362,7 @@ mod tests {
Duration::from_secs(crate::DEFAULT_IKM_DURATION), Duration::from_secs(crate::DEFAULT_IKM_DURATION),
); );
let res = lst.get_latest_ikm(); let res = lst.get_latest_ikm();
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let latest_ikm = res.unwrap(); let latest_ikm = res.unwrap();
assert_eq!(latest_ikm.id, 3); assert_eq!(latest_ikm.id, 3);
assert_eq!(latest_ikm.scheme, Scheme::XChaCha20Poly1305WithBlake3); assert_eq!(latest_ikm.scheme, Scheme::XChaCha20Poly1305WithBlake3);
@ -386,7 +386,7 @@ mod tests {
let _ = lst.add_ikm(); let _ = lst.add_ikm();
for i in 1..=3 { for i in 1..=3 {
let res = lst.get_ikm_by_id(i); let res = lst.get_ikm_by_id(i);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let latest_ikm = res.unwrap(); let latest_ikm = res.unwrap();
assert_eq!(latest_ikm.id, i); assert_eq!(latest_ikm.id, i);
} }

View file

@ -106,7 +106,7 @@ mod tests {
#[test] #[test]
fn decode() { fn decode() {
let res = super::decode_cipher(TEST_STR); let res = super::decode_cipher(TEST_STR);
assert!(res.is_ok()); assert!(res.is_ok(), "res: {res:?}");
let (id, data) = res.unwrap(); let (id, data) = res.unwrap();
assert_eq!(id, TEST_IKM_ID); assert_eq!(id, TEST_IKM_ID);
assert_eq!(data.nonce, TEST_NONCE); assert_eq!(data.nonce, TEST_NONCE);