forked from lavina/lavina
added tests for AuthBody::from_str
This commit is contained in:
parent
06691bfc44
commit
190118974a
|
@ -1313,6 +1313,7 @@ name = "proto-xmpp"
|
|||
version = "0.0.1-dev"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_matches",
|
||||
"base64",
|
||||
"derive_more",
|
||||
"lazy_static",
|
||||
|
|
|
@ -11,3 +11,6 @@ anyhow.workspace = true
|
|||
tokio.workspace = true
|
||||
derive_more.workspace = true
|
||||
base64.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
assert_matches.workspace = true
|
||||
|
|
|
@ -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::<Vec<_>>().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::<Vec<_>>().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,
|
||||
|
|
Loading…
Reference in New Issue