From 57d923329adaae200c5595e3dcb04e207460d59c Mon Sep 17 00:00:00 2001 From: khai96_ Date: Fri, 19 Apr 2019 21:13:38 +0700 Subject: Fix parse_auth and add some tests --- src/args.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/args.rs') diff --git a/src/args.rs b/src/args.rs index 9c96fd7..c7a4917 100644 --- a/src/args.rs +++ b/src/args.rs @@ -77,7 +77,7 @@ fn parse_interface(src: &str) -> Result { /// Checks wether the auth string is valid, i.e. it follows the syntax username:password fn parse_auth(src: &str) -> Result { - let mut split = src.splitn(2, ':'); + let mut split = src.splitn(3, ':'); let errmsg = "Invalid credentials string, expected format is username:password".to_owned(); let username = match split.next() { @@ -152,3 +152,47 @@ pub fn parse_args() -> crate::MiniserveConfig { file_upload: args.file_upload, } } + +#[cfg(test)] +mod tests { + use super::*; + + fn create_required_auth (username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth { + use auth::*; + use RequiredAuthPassword::*; + + RequiredAuth { + username: username.to_owned(), + password: match encrypt { + "plain" => Plain(password.to_owned()), + "sha256" => Sha256(password.to_owned()), + "sha512" => Sha512(password.to_owned()), + _ => panic!("Unknown encryption type") + }, + } + } + + #[test] + fn parse_auth_plain() { + assert_eq!( + parse_auth("username:password").unwrap(), + create_required_auth("username", "password", "plain") + ); + } + + #[test] + fn parse_auth_sha256() { + assert_eq!( + parse_auth("username:sha256:hash").unwrap(), + create_required_auth("username", "hash", "sha256") + ); + } + + #[test] + fn parse_auth_sha512() { + assert_eq!( + parse_auth("username:sha512:hash").unwrap(), + create_required_auth("username", "hash", "sha512") + ); + } +} \ No newline at end of file -- cgit v1.2.3