Skip to content

Commit

Permalink
test(hsh): ✅ add new unit tests for test_bcrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed May 12, 2024
1 parent f3cd20f commit 6822fe1
Showing 1 changed file with 53 additions and 14 deletions.
67 changes: 53 additions & 14 deletions tests/test_bcrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

#[cfg(test)]
mod tests {
use hsh::models::hash_algorithm::HashingAlgorithm;
use hsh::algorithms::bcrypt::Bcrypt;
use hsh::models::hash::Hash;
use hsh::models::hash_algorithm::{
HashAlgorithm, HashingAlgorithm,
};

#[test]
fn test_hash_differs_from_password() {
let password = "password123";
let salt = "somesalt";
let hashed_password =
hsh::algorithms::bcrypt::Bcrypt::hash_password(
password, salt,
)
.unwrap();
Bcrypt::hash_password(password, salt).unwrap();

assert_ne!(hashed_password, password.as_bytes());
}
Expand All @@ -24,14 +25,8 @@ mod tests {
let salt1 = "salt1";
let salt2 = "salt2";

let hash1 = hsh::algorithms::bcrypt::Bcrypt::hash_password(
password, salt1,
)
.unwrap();
let hash2 = hsh::algorithms::bcrypt::Bcrypt::hash_password(
password, salt2,
)
.unwrap();
let hash1 = Bcrypt::hash_password(password, salt1).unwrap();
let hash2 = Bcrypt::hash_password(password, salt2).unwrap();

assert_ne!(hash1, hash2);
}
Expand All @@ -42,9 +37,53 @@ mod tests {
let password = "password123";

// Intentionally using an invalid cost to force an error
let invalid_cost = 1;
let invalid_cost: u32 = 1;
let result = bcrypt::hash(password, invalid_cost);

assert!(result.is_err());
}

#[test]
fn test_new_bcrypt() {
let password = "password123";
let cost: u32 = 12;
let hash = Hash::new_bcrypt(password, cost).unwrap();

assert_eq!(hash.algorithm, HashAlgorithm::Bcrypt);
assert!(!hash.hash.is_empty());
assert_eq!(hash.salt.len(), 0);
}

#[test]
fn test_new_bcrypt_error() {
let password = "password123";
let invalid_cost: u32 = 0;
let result = Hash::new_bcrypt(password, invalid_cost);

assert!(result.is_err());
}

#[test]
fn test_from_hash() {
let hash_bytes = vec![1, 2, 3, 4];
let hash = Hash::from_hash(&hash_bytes, "bcrypt").unwrap();
assert_eq!(hash.hash, hash_bytes);
assert_eq!(hash.algorithm, HashAlgorithm::Bcrypt);
}

#[test]
fn test_from_hash_error() {
let hash_bytes = vec![1, 2, 3, 4];
let hash = Hash::from_hash(&hash_bytes, "invalid").unwrap_err();
assert_eq!(hash, "Unsupported hash algorithm: invalid");
}

#[test]
fn test_verify_bcrypt() {
let password = "password123";
let hash = Hash::new_bcrypt(password, 12).unwrap();

assert!(hash.verify(password).unwrap());
assert!(!hash.verify("wrong_password").unwrap());
}
}

0 comments on commit 6822fe1

Please sign in to comment.