Skip to content

Commit 6822fe1

Browse files
test(hsh): ✅ add new unit tests for test_bcrypt
1 parent f3cd20f commit 6822fe1

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

tests/test_bcrypt.rs

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33

44
#[cfg(test)]
55
mod tests {
6-
use hsh::models::hash_algorithm::HashingAlgorithm;
6+
use hsh::algorithms::bcrypt::Bcrypt;
7+
use hsh::models::hash::Hash;
8+
use hsh::models::hash_algorithm::{
9+
HashAlgorithm, HashingAlgorithm,
10+
};
711

812
#[test]
913
fn test_hash_differs_from_password() {
1014
let password = "password123";
1115
let salt = "somesalt";
1216
let hashed_password =
13-
hsh::algorithms::bcrypt::Bcrypt::hash_password(
14-
password, salt,
15-
)
16-
.unwrap();
17+
Bcrypt::hash_password(password, salt).unwrap();
1718

1819
assert_ne!(hashed_password, password.as_bytes());
1920
}
@@ -24,14 +25,8 @@ mod tests {
2425
let salt1 = "salt1";
2526
let salt2 = "salt2";
2627

27-
let hash1 = hsh::algorithms::bcrypt::Bcrypt::hash_password(
28-
password, salt1,
29-
)
30-
.unwrap();
31-
let hash2 = hsh::algorithms::bcrypt::Bcrypt::hash_password(
32-
password, salt2,
33-
)
34-
.unwrap();
28+
let hash1 = Bcrypt::hash_password(password, salt1).unwrap();
29+
let hash2 = Bcrypt::hash_password(password, salt2).unwrap();
3530

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

4439
// Intentionally using an invalid cost to force an error
45-
let invalid_cost = 1;
40+
let invalid_cost: u32 = 1;
4641
let result = bcrypt::hash(password, invalid_cost);
4742

4843
assert!(result.is_err());
4944
}
45+
46+
#[test]
47+
fn test_new_bcrypt() {
48+
let password = "password123";
49+
let cost: u32 = 12;
50+
let hash = Hash::new_bcrypt(password, cost).unwrap();
51+
52+
assert_eq!(hash.algorithm, HashAlgorithm::Bcrypt);
53+
assert!(!hash.hash.is_empty());
54+
assert_eq!(hash.salt.len(), 0);
55+
}
56+
57+
#[test]
58+
fn test_new_bcrypt_error() {
59+
let password = "password123";
60+
let invalid_cost: u32 = 0;
61+
let result = Hash::new_bcrypt(password, invalid_cost);
62+
63+
assert!(result.is_err());
64+
}
65+
66+
#[test]
67+
fn test_from_hash() {
68+
let hash_bytes = vec![1, 2, 3, 4];
69+
let hash = Hash::from_hash(&hash_bytes, "bcrypt").unwrap();
70+
assert_eq!(hash.hash, hash_bytes);
71+
assert_eq!(hash.algorithm, HashAlgorithm::Bcrypt);
72+
}
73+
74+
#[test]
75+
fn test_from_hash_error() {
76+
let hash_bytes = vec![1, 2, 3, 4];
77+
let hash = Hash::from_hash(&hash_bytes, "invalid").unwrap_err();
78+
assert_eq!(hash, "Unsupported hash algorithm: invalid");
79+
}
80+
81+
#[test]
82+
fn test_verify_bcrypt() {
83+
let password = "password123";
84+
let hash = Hash::new_bcrypt(password, 12).unwrap();
85+
86+
assert!(hash.verify(password).unwrap());
87+
assert!(!hash.verify("wrong_password").unwrap());
88+
}
5089
}

0 commit comments

Comments
 (0)