From 190118974a5b89e58ae37670e1f908e4cecd2da3 Mon Sep 17 00:00:00 2001 From: JustTestingV Date: Sun, 8 Oct 2023 14:18:51 +0300 Subject: [PATCH] added tests for AuthBody::from_str --- Cargo.lock | 1 + crates/proto-xmpp/Cargo.toml | 3 ++ crates/proto-xmpp/src/sasl.rs | 63 +++++++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d155325..7a498b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1313,6 +1313,7 @@ name = "proto-xmpp" version = "0.0.1-dev" dependencies = [ "anyhow", + "assert_matches", "base64", "derive_more", "lazy_static", diff --git a/crates/proto-xmpp/Cargo.toml b/crates/proto-xmpp/Cargo.toml index 3a29257..df0f03c 100644 --- a/crates/proto-xmpp/Cargo.toml +++ b/crates/proto-xmpp/Cargo.toml @@ -11,3 +11,6 @@ anyhow.workspace = true tokio.workspace = true derive_more.workspace = true base64.workspace = true + +[dev-dependencies] +assert_matches.workspace = true diff --git a/crates/proto-xmpp/src/sasl.rs b/crates/proto-xmpp/src/sasl.rs index 53b3c96..71f4945 100644 --- a/crates/proto-xmpp/src/sasl.rs +++ b/crates/proto-xmpp/src/sasl.rs @@ -29,6 +29,7 @@ impl Mechanism { } } +#[derive(PartialEq, Debug)] pub struct AuthBody { pub login: String, pub password: String, @@ -40,9 +41,10 @@ impl AuthBody { Ok(decoded_body) => { match String::from_utf8(decoded_body) { Ok(parsed_to_string) => { - let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::>().clone(); - if separated_words.len() == 2 { - Ok(AuthBody { login: separated_words[0].to_string(), password: separated_words[1].to_string() }) + let separated_words: Vec<&str> = parsed_to_string.split("\x00").collect::>().clone(); + if separated_words.len() == 3 { + // first segment ignored (might be needed in the future) + Ok(AuthBody { login: separated_words[1].to_string(), password: separated_words[2].to_string() }) } else { return Err(anyhow!("Incorrect auth format")) } }, Err(e) => return Err(anyhow!(e)) @@ -53,6 +55,61 @@ impl AuthBody { } } +#[cfg(test)] +mod test { + use super::*; + #[test] + fn test_returning_auth_body() { + let orig = b"\x00login\x00pass"; + let encoded = general_purpose::STANDARD.encode(orig); + let expected = AuthBody {login: "login".to_string(), password: "pass".to_string()}; + let result = AuthBody::from_str(encoded.as_bytes()).unwrap(); + + assert_eq!(expected, result); + } + + #[test] + fn test_ignoring_first_segment() { + let orig = b"ignored\x00login\x00pass"; + let encoded = general_purpose::STANDARD.encode(orig); + let expected = AuthBody {login: "login".to_string(), password: "pass".to_string()}; + let result = AuthBody::from_str(encoded.as_bytes()).unwrap(); + + assert_eq!(expected, result); + } + + #[test] + fn test_returning_auth_body_with_empty_strings() { + let orig = b"\x00\x00"; + let encoded = general_purpose::STANDARD.encode(orig); + let expected = AuthBody {login: "".to_string(), password: "".to_string()}; + let result = AuthBody::from_str(encoded.as_bytes()).unwrap(); + + assert_eq!(expected, result); + } + + + #[test] + fn test_fail_if_size_less_then_3() { + let orig = b"login\x00pass"; + let encoded = general_purpose::STANDARD.encode(orig); + let expected = AuthBody {login: "login".to_string(), password: "pass".to_string()}; + let result = AuthBody::from_str(encoded.as_bytes()); + + assert!(result.is_err()); + } + + #[test] + fn test_fail_if_size_greater_then_3() { + let orig = b"first\x00login\x00pass\x00other"; + let encoded = general_purpose::STANDARD.encode(orig); + let expected = AuthBody {login: "login".to_string(), password: "pass".to_string()}; + let result = AuthBody::from_str(encoded.as_bytes()); + + assert!(result.is_err()); + } + +} pub struct Auth { pub mechanism: Mechanism,