From 1373767d7fc936cc9ac87beb0017a189a1b5f576 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Wed, 4 Oct 2023 15:55:34 +0200 Subject: [PATCH] xmpp: add muc proto tests --- crates/proto-xmpp/src/muc/mod.rs | 103 ++++++++++++++++++++++++++-- crates/proto-xmpp/src/xml/ignore.rs | 4 +- 2 files changed, 99 insertions(+), 8 deletions(-) diff --git a/crates/proto-xmpp/src/muc/mod.rs b/crates/proto-xmpp/src/muc/mod.rs index 250d50d..0a6e702 100644 --- a/crates/proto-xmpp/src/muc/mod.rs +++ b/crates/proto-xmpp/src/muc/mod.rs @@ -1,8 +1,10 @@ +#![allow(unused_variables)] + use quick_xml::events::Event; use quick_xml::name::ResolveResult; -use anyhow::{anyhow, Result}; use crate::xml::*; +use anyhow::{anyhow, Result}; pub const XMLNS: &'static str = "http://jabber.org/protocol/muc"; @@ -20,8 +22,8 @@ impl FromXml for History { |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { let mut history = History::default(); let (bytes, end) = match event { - Event::Start(bytes) => (bytes, false), - Event::Empty(bytes) => (bytes, true), + Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => (bytes, false), + Event::Empty(bytes) if bytes.name().0 == Self::NAME.as_bytes() => (bytes, true), _ => return Err(anyhow!("Unexpected XML event: {event:?}")), }; for attr in bytes.attributes() { @@ -72,14 +74,16 @@ impl FromXml for Password { fn parse() -> Self::P { |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { - let Event::Start(bytes) = event else { - return Err(anyhow!("Unexpected XML event: {event:?}")); + let bytes = match event { + Event::Start(bytes) if bytes.name().0 == Self::NAME.as_bytes() => bytes, + _ => return Err(anyhow!("Unexpected XML event: {event:?}")), }; let (namespace, event) = yield; let Event::Text(bytes) = event else { return Err(anyhow!("Unexpected XML event: {event:?}")); }; let s = std::str::from_utf8(bytes)?.to_string(); + let (namespace, event) = yield; let Event::End(bytes) = event else { return Err(anyhow!("Unexpected XML event: {event:?}")); }; @@ -106,7 +110,7 @@ impl FromXml for X { fn parse() -> Self::P { |(namespace, event): (ResolveResult<'static>, &'static Event<'static>)| -> Result { let mut res = X::default(); - let (bytes, end) = match event { + let (_, end) = match event { Event::Start(bytes) => (bytes, false), Event::Empty(bytes) => (bytes, true), _ => return Err(anyhow!("Unexpected XML event: {event:?}")), @@ -138,3 +142,90 @@ impl FromXml for X { } } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_history_success_empty() { + let input = ""; + let res: History = parse(input).unwrap(); + let expected = History { + maxchars: None, + maxstanzas: None, + seconds: None, + }; + assert_eq!(res, expected); + } + + #[test] + fn test_history_success_empty_attrs() { + let input = r#""#; + let res: History = parse(input).unwrap(); + let expected = History { + maxchars: Some(1), + maxstanzas: Some(2), + seconds: Some(4), + }; + assert_eq!(res, expected); + } + + #[test] + fn test_history_success_start_end() { + let input = r#""#; + let res: History = parse(input).unwrap(); + let expected = History { + maxchars: None, + maxstanzas: None, + seconds: None, + }; + assert_eq!(res, expected); + } + + #[test] + fn test_history_incorrect_empty() { + let input = r#""#; + parse::(input).err().unwrap(); + } + + #[test] + fn test_password_success() { + let input = "olala"; + let res: Password = parse(input).unwrap(); + let expected = Password("olala".into()); + assert_eq!(res, expected); + } + + #[test] + fn test_password_incorrect() { + let input = r#"asdsd"#; + parse::(input).err().unwrap(); + } + + #[test] + fn test_x_success_empty() { + let input = ""; + let res: X = parse(input).unwrap(); + let expected = X { + history: None, + password: None, + }; + assert_eq!(res, expected); + } + + #[test] + fn test_x_success_full() { + let input = r#"ololo"#; + let res: X = parse(input).unwrap(); + let expected = X { + history: Some(History { + maxchars: Some(1), + maxstanzas: None, + seconds: None, + }), + password: Some(Password("ololo".into())), + }; + assert_eq!(res, expected); + } +} diff --git a/crates/proto-xmpp/src/xml/ignore.rs b/crates/proto-xmpp/src/xml/ignore.rs index 4fe39e5..1a17534 100644 --- a/crates/proto-xmpp/src/xml/ignore.rs +++ b/crates/proto-xmpp/src/xml/ignore.rs @@ -17,7 +17,7 @@ impl Parser for IgnoreParser { fn consume<'a>( self: Self, - namespace: ResolveResult, + _: ResolveResult, event: &Event<'a>, ) -> Continuation { match self.0 { @@ -58,5 +58,5 @@ impl FromXml for Ignore { } impl ToXml for () { - fn serialize(&self, events: &mut Vec>) {} + fn serialize(&self, _: &mut Vec>) {} }