aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/args.rs46
-rw-r--r--src/auth.rs94
2 files changed, 136 insertions, 4 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
diff --git a/src/auth.rs b/src/auth.rs
index c53d26a..e96a0ce 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -17,14 +17,14 @@ pub struct BasicAuthParams {
pub password: String,
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
pub enum RequiredAuthPassword {
Plain(String),
Sha256(String),
Sha512(String),
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
/// Authentication structure to match BasicAuthParams against
pub struct RequiredAuth {
pub username: String,
@@ -65,7 +65,7 @@ pub fn compare_hash<T: Digest>(password: String, hash: &String) -> bool {
get_hash_hex::<T>(password) == *hash
}
-fn get_hash_hex<T: Digest>(text: String) -> String {
+pub fn get_hash_hex<T: Digest>(text: String) -> String {
let mut hasher = T::new();
hasher.input(text);
hex::encode(hasher.result())
@@ -105,3 +105,91 @@ impl Middleware<crate::MiniserveConfig> for Auth {
Ok(Response::Done(resp))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn get_hash_hex_sha256() {
+ let expectation = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".to_owned();
+ let received = get_hash_hex::<Sha256>("abc".to_owned());
+ assert_eq!(expectation, received);
+ }
+
+ #[test]
+ fn get_hash_hex_sha512() {
+ let expectation = "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f".to_owned();
+ let received = get_hash_hex::<Sha512>("abc".to_owned());
+ assert_eq!(expectation, received);
+ }
+
+ fn create_auth_params (username: &str, password: &str) -> BasicAuthParams {
+ BasicAuthParams {
+ username: username.to_owned(),
+ password: password.to_owned(),
+ }
+ }
+
+ fn create_required_auth (username: &str, password: &str, encrypt: &str) -> RequiredAuth {
+ use RequiredAuthPassword::*;
+
+ RequiredAuth {
+ username: username.to_owned(),
+ password: match encrypt {
+ "plain" => Plain(password.to_owned()),
+ "sha256" => Sha256(get_hash_hex::<sha2::Sha256>(password.to_owned())),
+ "sha512" => Sha512(get_hash_hex::<sha2::Sha512>(password.to_owned())),
+ _ => panic!("Unknown encryption type")
+ },
+ }
+ }
+
+ #[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"),
+ ));
+ }
+}