xmpp: add muc proto tests

This commit is contained in:
Nikita Vilunov 2023-10-04 15:55:34 +02:00
parent 887fd95194
commit 1373767d7f
2 changed files with 99 additions and 8 deletions

View File

@ -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<Self> {
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<Self> {
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<Self> {
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 = "<history/>";
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#"<history maxchars="1" maxstanzas="2" seconds="4"/>"#;
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#"<history></history>"#;
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#"<iq/>"#;
parse::<History>(input).err().unwrap();
}
#[test]
fn test_password_success() {
let input = "<password>olala</password>";
let res: Password = parse(input).unwrap();
let expected = Password("olala".into());
assert_eq!(res, expected);
}
#[test]
fn test_password_incorrect() {
let input = r#"<iq>asdsd</iq>"#;
parse::<Password>(input).err().unwrap();
}
#[test]
fn test_x_success_empty() {
let input = "<x/>";
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#"<x><password>ololo</password><history maxchars="1"/></x>"#;
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);
}
}

View File

@ -17,7 +17,7 @@ impl Parser for IgnoreParser {
fn consume<'a>(
self: Self,
namespace: ResolveResult,
_: ResolveResult,
event: &Event<'a>,
) -> Continuation<Self, Self::Output> {
match self.0 {
@ -58,5 +58,5 @@ impl FromXml for Ignore {
}
impl ToXml for () {
fn serialize(&self, events: &mut Vec<Event<'static>>) {}
fn serialize(&self, _: &mut Vec<Event<'static>>) {}
}