Skip to content

Commit

Permalink
[refactor] #3422: clean up some ursa tests: remove redundant result.i…
Browse files Browse the repository at this point in the history
…s_ok() checks

Signed-off-by: Nikita Strygin <dcnick3@users.noreply.github.com>
  • Loading branch information
DCNick3 committed Nov 20, 2023
1 parent 08e28dc commit 1f7bb71
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
25 changes: 8 additions & 17 deletions crypto/src/encryption/chacha20poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ mod tests {
let cipher = ChaCha20Poly1305::new(&ChaCha20Poly1305::key_gen().unwrap());
let aad = Vec::new();
let message = b"Hello and Goodbye!".to_vec();
let res = cipher.encrypt_easy(&aad, &message);
assert!(res.is_ok());
let ciphertext = res.unwrap();
let res = cipher.decrypt_easy(&aad, &ciphertext);
assert!(res.is_ok());
assert_eq!(message, res.unwrap());
let ciphertext = cipher.encrypt_easy(&aad, &message).unwrap();
let decrypted_message = cipher.decrypt_easy(&aad, &ciphertext).unwrap();
assert_eq!(message, decrypted_message);
}

#[test]
Expand All @@ -86,16 +83,13 @@ mod tests {
msg: message.as_slice(),
aad: aad.as_slice(),
};
let res = cipher.encrypt(&nonce, payload);
assert!(res.is_ok());
let ciphertext = res.unwrap();
let ciphertext = cipher.encrypt(&nonce, payload).unwrap();
let payload = Payload {
msg: ciphertext.as_slice(),
aad: aad.as_slice(),
};
let res = cipher.decrypt(&nonce, payload);
assert!(res.is_ok());
assert_eq!(message, res.unwrap());
let decrypted_message = cipher.decrypt(&nonce, payload).unwrap();
assert_eq!(message, decrypted_message);
}

#[test]
Expand All @@ -104,17 +98,14 @@ mod tests {
let aad = b"decrypt should fail".to_vec();
let message = b"Hello and Goodbye!".to_vec();
let res = cipher.encrypt_easy(&aad, &message);
assert!(res.is_ok());
let mut ciphertext = res.unwrap();

let aad = b"decrypt should succeed".to_vec();
let res = cipher.decrypt_easy(&aad, &ciphertext);
assert!(res.is_err());
cipher.decrypt_easy(&aad, &ciphertext).unwrap_err();

let aad = b"decrypt should fail".to_vec();
ciphertext[0] ^= ciphertext[1];
let res = cipher.decrypt_easy(&aad, &ciphertext);
assert!(res.is_err());
cipher.decrypt_easy(&aad, &ciphertext).unwrap_err();
}

// TODO: this should be tested for, but only after we integrate with secrecy/zeroize
Expand Down
1 change: 0 additions & 1 deletion crypto/src/encryption/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ fn random_bytes<T: ArrayLength<u8>>() -> Result<GenericArray<u8, T>, Error> {
///
/// let ciphertext = res.unwrap();
/// let res = encryptor.decrypt_easy(aad.as_ref(), ciphertext.as_slice());
/// assert!(res.is_ok());
/// assert_eq!(res.unwrap().as_slice(), message);
/// ```
#[derive(Debug, Clone)]
Expand Down
10 changes: 4 additions & 6 deletions crypto/src/kex/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ mod tests {
#[test]
fn key_exchange() {
let scheme = X25519Sha256::new();
let res = scheme.keypair(None);
assert!(res.is_ok());
let (public_key1, secret_key1) = res.unwrap();
let (public_key1, secret_key1) = scheme.keypair(None).unwrap();
let _res = scheme.compute_shared_secret(&secret_key1, &public_key1);
let res = scheme.keypair(None);
let (public_key2, secret_key2) = res.unwrap();
let _res = scheme.compute_shared_secret(&secret_key2, &public_key1);
let _res = scheme.compute_shared_secret(&secret_key1, &public_key2);

let res = scheme.keypair(Some(KeyGenOption::FromPrivateKey(secret_key1)));
assert!(res.is_ok());
let (public_key2, secret_key1) = res.unwrap();
let (public_key2, secret_key1) = scheme
.keypair(Some(KeyGenOption::FromPrivateKey(secret_key1)))
.unwrap();
assert_eq!(public_key2, public_key1);
assert_eq!(secret_key1, secret_key1);
}
Expand Down
14 changes: 7 additions & 7 deletions p2p/tests/integration/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,11 @@ fn test_encryption() {
let encryptor = SymmetricEncryptor::<ChaCha20Poly1305>::new_with_key(TEST_KEY);
let message = b"Some ciphertext";
let aad = b"Iroha2 AAD";
let res = encryptor.encrypt_easy(aad.as_ref(), message.as_ref());
assert!(res.is_ok());

let ciphertext = res.unwrap();
let res_cipher = encryptor.decrypt_easy(aad.as_ref(), ciphertext.as_slice());
assert!(res_cipher.is_ok());
assert_eq!(res_cipher.unwrap().as_slice(), message);
let ciphertext = encryptor
.encrypt_easy(aad.as_ref(), message.as_ref())
.unwrap();
let decrypted = encryptor
.decrypt_easy(aad.as_ref(), ciphertext.as_slice())
.unwrap();
assert_eq!(decrypted.as_slice(), message);
}

0 comments on commit 1f7bb71

Please sign in to comment.