diff options
author | khai96_ <hvksmr1996@gmail.com> | 2019-04-19 14:13:38 +0000 |
---|---|---|
committer | khai96_ <hvksmr1996@gmail.com> | 2019-04-19 14:13:38 +0000 |
commit | 57d923329adaae200c5595e3dcb04e207460d59c (patch) | |
tree | b4cf0f19738ab29a61f77fad5683b6842b390774 /src/args.rs | |
parent | Fix false positive (diff) | |
download | miniserve-57d923329adaae200c5595e3dcb04e207460d59c.tar.gz miniserve-57d923329adaae200c5595e3dcb04e207460d59c.zip |
Fix parse_auth and add some tests
Diffstat (limited to 'src/args.rs')
-rw-r--r-- | src/args.rs | 46 |
1 files changed, 45 insertions, 1 deletions
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<IpAddr, std::net::AddrParseError> { /// Checks wether the auth string is valid, i.e. it follows the syntax username:password fn parse_auth(src: &str) -> Result<auth::RequiredAuth, String> { - 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 |