aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkhai96_ <hvksmr1996@gmail.com>2019-04-25 12:13:18 +0000
committerkhai96_ <hvksmr1996@gmail.com>2019-04-25 12:13:18 +0000
commit0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb (patch)
tree5826f8710efc79376a92c0f50e284e3dea80fafc
parentUpdate Cargo.lock (diff)
downloadminiserve-0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb.tar.gz
miniserve-0b731cf0a0337f4e406baf4cbbcc407e7d9af9eb.zip
Use rstest_parametrize for unit tests
Diffstat (limited to '')
-rw-r--r--src/args.rs85
-rw-r--r--src/auth.rs112
2 files changed, 71 insertions, 126 deletions
diff --git a/src/args.rs b/src/args.rs
index f13d14f..496d697 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -160,6 +160,7 @@ pub fn parse_args() -> crate::MiniserveConfig {
#[cfg(test)]
mod tests {
use super::*;
+ use rstest::rstest_parametrize;
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth {
use auth::*;
@@ -176,67 +177,35 @@ mod tests {
}
}
- #[test]
- fn parse_auth_plain() -> Result<(), String> {
- assert_eq!(
- parse_auth("username:password")?,
- create_required_auth("username", "password", "plain")
- );
-
- Ok(())
- }
-
- #[test]
- fn parse_auth_sha256() -> Result<(), String> {
- assert_eq!(
- parse_auth("username:sha256:abcd")?,
- create_required_auth("username", "abcd", "sha256")
- );
-
- Ok(())
- }
-
- #[test]
- fn parse_auth_sha512() -> Result<(), String> {
- assert_eq!(
- parse_auth("username:sha512:abcd")?,
- create_required_auth("username", "abcd", "sha512")
- );
-
- Ok(())
- }
-
- #[test]
- fn parse_auth_invalid_syntax() {
- assert_eq!(
- parse_auth("foo").unwrap_err(),
- "Invalid credentials string, expected format is username:password".to_owned()
- );
- }
-
- #[test]
- fn parse_auth_invalid_hash_method() {
- assert_eq!(
- parse_auth("username:blahblah:abcd").unwrap_err(),
- "Invalid hash method, only accept either sha256 or sha512".to_owned()
- );
- }
-
- #[test]
- fn parse_auth_invalid_hash_string() {
+ #[rstest_parametrize(
+ auth_string, username, password, encrypt,
+ case("username:password", "username", "password", "plain"),
+ case("username:sha256:abcd", "username", "abcd", "sha256"),
+ case("username:sha512:abcd", "username", "abcd", "sha512")
+ )]
+ fn parse_auth_valid(auth_string: &str, username: &str, password: &str, encrypt: &str) {
assert_eq!(
- parse_auth("username:sha256:invalid").unwrap_err(),
- "Hash string is not a valid hex code".to_owned()
+ parse_auth(auth_string).unwrap(),
+ create_required_auth(username, password, encrypt),
);
}
- #[test]
- fn parse_auth_excessive_length() {
- let auth_string = format!("username:{}", "x".repeat(256));
-
- assert_eq!(
- parse_auth(&*auth_string).unwrap_err(),
- "Password length cannot exceed 255 characters".to_owned()
- );
+ #[rstest_parametrize(
+ auth_string, err_msg,
+ case(
+ "foo",
+ "Invalid credentials string, expected format is username:password"
+ ),
+ case(
+ "username:blahblah:abcd",
+ "Invalid hash method, only accept either sha256 or sha512"
+ ),
+ case(
+ "username:sha256:invalid",
+ "Hash string is not a valid hex code"
+ ),
+ )]
+ fn parse_auth_invalid(auth_string: &str, err_msg: &str) {
+ assert_eq!(parse_auth(auth_string).unwrap_err(), err_msg.to_owned(),);
}
}
diff --git a/src/auth.rs b/src/auth.rs
index a4b3555..2db422d 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -115,31 +115,26 @@ impl Middleware<crate::MiniserveConfig> for Auth {
#[cfg(test)]
mod tests {
use super::*;
+ use rstest::rstest_parametrize;
- fn assert_hex_eq(expectation: &str, received: Vec<u8>) {
- let bin = hex::decode(expectation).expect("Provided string is not a valid hex code");
- assert_eq!(bin, received);
- }
-
- #[test]
- fn get_hash_hex_sha256() {
- let expectation = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
- let received = get_hash::<Sha256>("abc".to_owned());
- assert_hex_eq(expectation, received);
+ fn get_hash_func(name: &str) -> impl FnOnce(String) -> Vec<u8> {
+ match name {
+ "sha256" => get_hash::<Sha256>,
+ "sha512" => get_hash::<Sha512>,
+ _ => panic!("Invalid hash method"),
+ }
}
- #[test]
- fn get_hash_hex_sha512() {
- let expectation = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f";
- let received = get_hash::<Sha512>("abc".to_owned());
- assert_hex_eq(expectation, received);
- }
-
- fn create_auth_params(username: &str, password: &str) -> BasicAuthParams {
- BasicAuthParams {
- username: username.to_owned(),
- password: password.to_owned(),
- }
+ #[rstest_parametrize(
+ password, hash_method, hash,
+ case("abc", "sha256", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"),
+ case("abc", "sha512", "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"),
+ )]
+ 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());
+ assert_eq!(received, expected);
}
fn create_required_auth(username: &str, password: &str, encrypt: &str) -> RequiredAuth {
@@ -156,51 +151,32 @@ mod tests {
}
}
- #[test]
- fn match_auth_plain_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "plain"),
- ));
- }
-
- #[test]
- fn match_auth_plain_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "plain"),
- ));
- }
-
- #[test]
- fn match_auth_sha256_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "sha256"),
- ));
- }
-
- #[test]
- fn match_auth_sha256_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "sha256"),
- ));
- }
-
- #[test]
- fn match_auth_sha512_password_should_pass() {
- assert!(match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hello there", "sha512"),
- ));
- }
-
- #[test]
- fn match_auth_sha512_password_should_fail() {
- assert!(!match_auth(
- create_auth_params("obi", "hello there"),
- &create_required_auth("obi", "hi!", "sha512"),
- ));
+ #[rstest_parametrize(
+ should_pass, param_username, param_password, required_username, required_password, encrypt,
+ case(true, "obi", "hello there", "obi", "hello there", "plain"),
+ case(false, "obi", "hello there", "obi", "hi!", "plain"),
+ case(true, "obi", "hello there", "obi", "hello there", "sha256"),
+ case(false, "obi", "hello there", "obi", "hi!", "sha256"),
+ case(true, "obi", "hello there", "obi", "hello there", "sha512"),
+ case(false, "obi", "hello there", "obi", "hi!", "sha512"),
+ )]
+ fn test_auth(
+ should_pass: bool,
+ param_username: &str,
+ param_password: &str,
+ required_username: &str,
+ required_password: &str,
+ encrypt: &str,
+ ) {
+ assert_eq!(
+ match_auth(
+ BasicAuthParams {
+ username: param_username.to_owned(),
+ password: param_password.to_owned(),
+ },
+ &create_required_auth(required_username, required_password, encrypt),
+ ),
+ should_pass,
+ )
}
}