added tests for AuthBody::from_str

This commit is contained in:
JustTestingV 2023-10-08 14:18:51 +03:00
parent 06691bfc44
commit 190118974a
3 changed files with 64 additions and 3 deletions

1
Cargo.lock generated
View File

@ -1313,6 +1313,7 @@ name = "proto-xmpp"
version = "0.0.1-dev" version = "0.0.1-dev"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"assert_matches",
"base64", "base64",
"derive_more", "derive_more",
"lazy_static", "lazy_static",

View File

@ -11,3 +11,6 @@ anyhow.workspace = true
tokio.workspace = true tokio.workspace = true
derive_more.workspace = true derive_more.workspace = true
base64.workspace = true base64.workspace = true
[dev-dependencies]
assert_matches.workspace = true

View File

@ -29,6 +29,7 @@ impl Mechanism {
} }
} }
#[derive(PartialEq, Debug)]
pub struct AuthBody { pub struct AuthBody {
pub login: String, pub login: String,
pub password: String, pub password: String,
@ -40,9 +41,10 @@ impl AuthBody {
Ok(decoded_body) => { Ok(decoded_body) => {
match String::from_utf8(decoded_body) { match String::from_utf8(decoded_body) {
Ok(parsed_to_string) => { Ok(parsed_to_string) => {
let separated_words: Vec<&str> = parsed_to_string.split("\x00").filter(|&part| !part.is_empty()).collect::<Vec<_>>().clone(); let separated_words: Vec<&str> = parsed_to_string.split("\x00").collect::<Vec<_>>().clone();
if separated_words.len() == 2 { if separated_words.len() == 3 {
Ok(AuthBody { login: separated_words[0].to_string(), password: separated_words[1].to_string() }) // 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")) } } else { return Err(anyhow!("Incorrect auth format")) }
}, },
Err(e) => return Err(anyhow!(e)) 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 struct Auth {
pub mechanism: Mechanism, pub mechanism: Mechanism,