Skip to content

Commit

Permalink
Test password generation and split tests into functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 2, 2023
1 parent 0e9d04b commit 4b2cc9b
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions crates/bitwarden/src/tool/generators/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,20 @@ mod test {
chars.iter().collect()
}

fn count(chars: &[char], pass: &str) -> usize {
pass.chars().filter(|c| chars.contains(c)).count()
}

#[test]
fn test_password_characters() {
// All characters excluding ambiguous
fn test_password_characters_all() {
let set = PasswordGeneratorCharSet::new(true, true, true, true, true);
assert_eq!(set.lower, LOWER_CHARS);
assert_eq!(set.upper, UPPER_CHARS);
assert_eq!(set.number, NUMBER_CHARS);
assert_eq!(set.special, SPECIAL_CHARS);

// All characters including ambiguous
}
#[test]
fn test_password_characters_all_ambiguous() {
let set = PasswordGeneratorCharSet::new(true, true, true, true, false);
assert!(to_string(&set.lower).contains(&to_string(LOWER_CHARS)));
assert!(to_string(&set.lower).contains(&to_string(LOWER_CHARS_AMBIGUOUS)));
Expand All @@ -205,14 +209,17 @@ mod test {
assert!(to_string(&set.number).contains(&to_string(NUMBER_CHARS)));
assert!(to_string(&set.number).contains(&to_string(NUMBER_CHARS_AMBIGUOUS)));
assert_eq!(set.special, SPECIAL_CHARS);

// Only lowercase
}
#[test]
fn test_password_characters_lower() {
let set = PasswordGeneratorCharSet::new(true, false, false, false, true);
assert_eq!(set.lower, LOWER_CHARS);
assert_eq!(set.upper, Vec::new());
assert_eq!(set.number, Vec::new());
assert_eq!(set.special, Vec::new());

}
#[test]
fn test_password_characters_upper_ambiguous() {
// Only uppercase including ambiguous
let set = PasswordGeneratorCharSet::new(false, true, false, false, false);
assert_eq!(set.lower, Vec::new());
Expand All @@ -221,4 +228,60 @@ mod test {
assert_eq!(set.number, Vec::new());
assert_eq!(set.special, Vec::new());
}

#[test]
fn test_password_gen_all_characters() {
let pass = password(PasswordGeneratorRequest {
lowercase: true,
uppercase: true,
numbers: true,
special: true,
length: Some(100),
avoid_ambiguous: Some(true),
min_lowercase: Some(1),
min_uppercase: Some(1),
min_number: Some(1),
min_special: Some(1),
})
.unwrap();

assert_eq!(pass.len(), 100);

assert!(count(LOWER_CHARS, &pass) > 1);
assert!(count(UPPER_CHARS, &pass) > 1);
assert!(count(NUMBER_CHARS, &pass) > 1);
assert!(count(SPECIAL_CHARS, &pass) > 1);

assert_eq!(count(LOWER_CHARS_AMBIGUOUS, &pass), 0);
assert_eq!(count(UPPER_CHARS_AMBIGUOUS, &pass), 0);
assert_eq!(count(NUMBER_CHARS_AMBIGUOUS, &pass), 0);
}

#[test]
fn test_password_gen_some_characters() {
let pass = password(PasswordGeneratorRequest {
lowercase: true,
uppercase: false,
numbers: false,
special: true,
length: Some(10),
avoid_ambiguous: Some(true),
min_lowercase: Some(9),
min_uppercase: None,
min_number: None,
min_special: Some(1),
})
.unwrap();

assert_eq!(pass.len(), 10);

assert_eq!(count(LOWER_CHARS, &pass), 9);
assert_eq!(count(UPPER_CHARS, &pass), 0);
assert_eq!(count(NUMBER_CHARS, &pass), 0);
assert_eq!(count(SPECIAL_CHARS, &pass), 1);

assert_eq!(count(LOWER_CHARS_AMBIGUOUS, &pass), 0);
assert_eq!(count(UPPER_CHARS_AMBIGUOUS, &pass), 0);
assert_eq!(count(NUMBER_CHARS_AMBIGUOUS, &pass), 0);
}
}

0 comments on commit 4b2cc9b

Please sign in to comment.