diff options
author | khai96_ <hvksmr1996@gmail.com> | 2019-05-16 12:52:12 +0000 |
---|---|---|
committer | khai96_ <hvksmr1996@gmail.com> | 2019-05-16 12:52:12 +0000 |
commit | 67a0af2bb3b171b6c3cce1b81e08be5929a3ccad (patch) | |
tree | 92a3d8f50c19b1da0516ad5763ecb0b67bdf8bd0 | |
parent | Make some changes (diff) | |
download | miniserve-67a0af2bb3b171b6c3cce1b81e08be5929a3ccad.tar.gz miniserve-67a0af2bb3b171b6c3cce1b81e08be5929a3ccad.zip |
Update unit tests
-rw-r--r-- | src/args.rs | 49 | ||||
-rw-r--r-- | src/auth.rs | 35 |
2 files changed, 30 insertions, 54 deletions
diff --git a/src/args.rs b/src/args.rs index 0ec8a22..39efe7e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -172,19 +172,18 @@ mod tests { fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth { use auth::*; use RequiredAuthPassword::*; - let mut required_auth = RequiredAuth::new(); - - required_auth.insert( - username.to_owned(), - match encrypt { - "plain" => Plain(password.to_owned()), - "sha256" => Sha256(hex::decode(password.to_owned()).unwrap()), - "sha512" => Sha512(hex::decode(password.to_owned()).unwrap()), - _ => panic!("Unknown encryption type"), - }, - ); - required_auth + let password = match encrypt { + "plain" => Plain(password.to_owned()), + "sha256" => Sha256(hex::decode(password.to_owned()).unwrap()), + "sha512" => Sha512(hex::decode(password.to_owned()).unwrap()), + _ => panic!("Unknown encryption type"), + }; + + auth::RequiredAuth { + username: username.to_owned(), + password, + } } #[rstest_parametrize( @@ -193,7 +192,7 @@ mod tests { case("username:sha256:abcd", "username", "abcd", "sha256"), case("username:sha512:abcd", "username", "abcd", "sha512") )] - fn parse_single_auth_valid(auth_string: &str, username: &str, password: &str, encrypt: &str) { + fn parse_auth_valid(auth_string: &str, username: &str, password: &str, encrypt: &str) { assert_eq!( parse_auth(auth_string).unwrap(), create_required_auth(username, password, encrypt), @@ -219,30 +218,8 @@ mod tests { "Invalid format for password hash. Expected hex code" ), )] - fn parse_single_auth_invalid(auth_string: &str, err_msg: &str) { + fn parse_auth_invalid(auth_string: &str, err_msg: &str) { let err = parse_auth(auth_string).unwrap_err(); assert_eq!(format!("{}", err), err_msg.to_owned()); } - - #[test] - fn parse_multiple_auth_valid() -> Result<(), ContextualError> { - let hex2str = |x: &str| hex::decode(x).expect("Invalid hex code"); - - let expected: auth::RequiredAuth = [ - ("usr0", auth::RequiredAuthPassword::Plain("pwd0".to_owned())), - ("usr1", auth::RequiredAuthPassword::Plain("pwd1".to_owned())), - ("usr2", auth::RequiredAuthPassword::Sha256(hex2str("abcd"))), - ("usr3", auth::RequiredAuthPassword::Sha512(hex2str("abcd"))), - ] - .iter() - .map(|(username, password)| (username.to_owned().to_string(), password.clone())) - .collect(); - - assert_eq!( - parse_auth("usr0:pwd0 usr1:pwd1 usr2:sha256:abcd usr3:sha512:abcd")?, - expected, - ); - - Ok(()) - } } diff --git a/src/auth.rs b/src/auth.rs index a30fe60..da5f4a9 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -22,7 +22,7 @@ pub enum RequiredAuthPassword { Sha512(Vec<u8>), } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, PartialEq)] /// Authentication structure to match `BasicAuthParams` against pub struct RequiredAuth { pub username: String, @@ -138,7 +138,7 @@ mod tests { use rstest::rstest_parametrize; /// Return a hashing function corresponds to given name - fn get_hash_func(name: &str) -> impl FnOnce(String) -> Vec<u8> { + fn get_hash_func(name: &str) -> impl FnOnce(&String) -> Vec<u8> { match name { "sha256" => get_hash::<Sha256>, "sha512" => get_hash::<Sha512>, @@ -154,26 +154,25 @@ mod tests { fn test_get_hash(password: &str, hash_method: &str, hash: &str) { let hash_func = get_hash_func(hash_method); let expected = hex::decode(hash).expect("Provided hash is not a valid hex code"); - let received = hash_func(password.to_owned()); + let received = hash_func(&password.to_owned()); assert_eq!(received, expected); } /// Helper function that creates a `RequiredAuth` structure and encrypt `password` if necessary fn create_required_auth(username: &str, password: &str, encrypt: &str) -> RequiredAuth { use RequiredAuthPassword::*; - let mut required_auth = RequiredAuth::new(); - - required_auth.insert( - username.to_owned(), - match encrypt { - "plain" => Plain(password.to_owned()), - "sha256" => Sha256(get_hash::<sha2::Sha256>(password.to_owned())), - "sha512" => Sha512(get_hash::<sha2::Sha512>(password.to_owned())), - _ => panic!("Unknown encryption type"), - }, - ); - - required_auth + + let password = match encrypt { + "plain" => Plain(password.to_owned()), + "sha256" => Sha256(get_hash::<sha2::Sha256>(&password.to_owned())), + "sha512" => Sha512(get_hash::<sha2::Sha512>(&password.to_owned())), + _ => panic!("Unknown encryption type"), + }; + + RequiredAuth { + username: username.to_owned(), + password, + } } #[rstest_parametrize( @@ -185,7 +184,7 @@ mod tests { case(true, "obi", "hello there", "obi", "hello there", "sha512"), case(false, "obi", "hello there", "obi", "hi!", "sha512"), )] - fn test_auth( + fn test_single_auth( should_pass: bool, param_username: &str, param_password: &str, @@ -199,7 +198,7 @@ mod tests { username: param_username.to_owned(), password: param_password.to_owned(), }, - &create_required_auth(required_username, required_password, encrypt), + &[create_required_auth(required_username, required_password, encrypt)], ), should_pass, ) |