Skip to content

Commit

Permalink
Added rand_chacha for some repeatable testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Oct 10, 2023
1 parent e4729ac commit 071295a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/bitwarden/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ bitwarden-api-identity = { path = "../bitwarden-api-identity", version = "=0.2.1
bitwarden-api-api = { path = "../bitwarden-api-api", version = "=0.2.1" }

[dev-dependencies]
rand_chacha = "0.3.1"
tokio = { version = "1.28.2", features = ["rt", "macros"] }
wiremock = "0.5.18"
31 changes: 20 additions & 11 deletions crates/bitwarden/src/tool/generators/passphrase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,21 @@ fn capitalize_first_letter(s: &str) -> String {

#[cfg(test)]
mod tests {
use rand::SeedableRng;

use super::*;

#[test]
fn test_gen_words() {
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0u8; 32]);
assert_eq!(
&gen_words(&mut rng, 4),
&["subsystem", "undertook", "silenced", "dinginess"]
);
assert_eq!(&gen_words(&mut rng, 1), &["numbing"]);
assert_eq!(&gen_words(&mut rng, 2), &["catnip", "jokester"]);
}

#[test]
fn test_capitalize() {
assert_eq!(capitalize_first_letter("hello"), "Hello");
Expand All @@ -88,25 +101,21 @@ mod tests {

#[test]
fn test_capitalize_words() {
let mut words = vec!["hello".to_string(), "world".to_string()];
let mut words = vec!["hello".into(), "world".into()];
capitalize_words(&mut words);
assert_eq!(words, &["Hello", "World"]);
}

#[test]
fn test_include_number() {
let mut rng = rand::thread_rng();
let mut rng = rand_chacha::ChaCha8Rng::from_seed([0u8; 32]);

fn count_numbers(words: &[String]) -> usize {
words
.iter()
.map(|w| w.chars().filter(|c| c.is_numeric()).count())
.sum()
}
let mut words = vec!["hello".into(), "world".into()];
include_number_in_words(&mut rng, &mut words);
assert_eq!(words, &["hello", "world7"]);

let mut words = vec!["hello".to_string(), "world".to_string()];
assert_eq!(count_numbers(&words), 0);
let mut words = vec!["This".into(), "is".into(), "a".into(), "test".into()];
include_number_in_words(&mut rng, &mut words);
assert_eq!(count_numbers(&words), 1);
assert_eq!(words, &["This", "is", "a1", "test"]);
}
}

0 comments on commit 071295a

Please sign in to comment.