forked from lavina/lavina
xmpp: handle correctly unavailable self-presence and improve basic test scenario
This commit is contained in:
parent
ea81ddadfc
commit
a047d55ab5
|
@ -447,6 +447,7 @@ impl<'a> XmppConnection<'a> {
|
||||||
ServerStreamEnd.serialize(output);
|
ServerStreamEnd.serialize(output);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
ClientPacket::Eos => true,
|
||||||
};
|
};
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ impl<'a> XmppConnection<'a> {
|
||||||
pub async fn handle_presence(&mut self, output: &mut Vec<Event<'static>>, p: Presence<Ignore>) -> Result<()> {
|
pub async fn handle_presence(&mut self, output: &mut Vec<Event<'static>>, p: Presence<Ignore>) -> Result<()> {
|
||||||
match p.to {
|
match p.to {
|
||||||
None => {
|
None => {
|
||||||
self.self_presence(output).await;
|
self.self_presence(output, p.r#type.as_deref()).await;
|
||||||
}
|
}
|
||||||
Some(Jid {
|
Some(Jid {
|
||||||
name: Some(name),
|
name: Some(name),
|
||||||
|
@ -33,7 +33,12 @@ impl<'a> XmppConnection<'a> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn self_presence(&mut self, output: &mut Vec<Event<'static>>) {
|
async fn self_presence(&mut self, output: &mut Vec<Event<'static>>, r#type: Option<&str>) {
|
||||||
|
match r#type {
|
||||||
|
Some("unavailable") => {
|
||||||
|
// do not print anything
|
||||||
|
}
|
||||||
|
None => {
|
||||||
let response = Presence::<()> {
|
let response = Presence::<()> {
|
||||||
to: Some(Jid {
|
to: Some(Jid {
|
||||||
name: Some(self.user.xmpp_name.clone()),
|
name: Some(self.user.xmpp_name.clone()),
|
||||||
|
@ -49,6 +54,9 @@ impl<'a> XmppConnection<'a> {
|
||||||
};
|
};
|
||||||
response.serialize(output);
|
response.serialize(output);
|
||||||
}
|
}
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async fn muc_presence(&mut self, name: Name, output: &mut Vec<Event<'static>>) -> Result<()> {
|
async fn muc_presence(&mut self, name: Name, output: &mut Vec<Event<'static>>) -> Result<()> {
|
||||||
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?;
|
let a = self.user_handle.join_room(RoomId::from(name.0.clone())?).await?;
|
||||||
|
|
|
@ -52,6 +52,7 @@ pub enum ClientPacket {
|
||||||
Message(Message<Ignore>),
|
Message(Message<Ignore>),
|
||||||
Presence(Presence<Ignore>),
|
Presence(Presence<Ignore>),
|
||||||
StreamEnd,
|
StreamEnd,
|
||||||
|
Eos,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromXml for ClientPacket {
|
impl FromXml for ClientPacket {
|
||||||
|
@ -83,6 +84,7 @@ impl FromXml for ClientPacket {
|
||||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Event::Eof => Ok(ClientPacket::Eos),
|
||||||
_ => {
|
_ => {
|
||||||
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
return Err(anyhow!("Unexpected XML event: {event:?}"));
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ impl<'a> TestScopeTls<'a> {
|
||||||
fn new(stream: &'a mut TlsStream<TcpStream>, buffer: Vec<u8>) -> TestScopeTls<'a> {
|
fn new(stream: &'a mut TlsStream<TcpStream>, buffer: Vec<u8>) -> TestScopeTls<'a> {
|
||||||
let (reader, writer) = tokio::io::split(stream);
|
let (reader, writer) = tokio::io::split(stream);
|
||||||
let reader = NsReader::from_reader(BufReader::new(reader));
|
let reader = NsReader::from_reader(BufReader::new(reader));
|
||||||
let timeout = Duration::from_millis(100);
|
let timeout = Duration::from_millis(500);
|
||||||
|
|
||||||
TestScopeTls {
|
TestScopeTls {
|
||||||
reader,
|
reader,
|
||||||
|
@ -203,6 +203,35 @@ async fn scenario_basic() -> Result<()> {
|
||||||
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||||
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||||
|
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"PLAIN"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanism"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"mechanisms"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||||
|
|
||||||
|
// base64-encoded b"\x00tester\x00password"
|
||||||
|
s.send(r#"<auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" mechanism="PLAIN">AHRlc3RlcgBwYXNzd29yZA==</auth>"#)
|
||||||
|
.await?;
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"success"));
|
||||||
|
s.send(r#"<?xml version="1.0"?>"#).await?;
|
||||||
|
s.send(r#"<stream:stream xmlns:stream="http://etherx.jabber.org/streams" to="127.0.0.1" xml:lang="en" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns="jabber:client" version="1.0">"#).await?;
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Decl(_) => {});
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"stream"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Empty(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"features"));
|
||||||
|
s.send(r#"<iq id="bind_1" type="set"><bind xmlns="urn:ietf:params:xml:ns:xmpp-bind"><resource>kek</resource></bind></iq>"#).await?;
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"iq"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Start(b) => assert_eq!(b.local_name().into_inner(), b"jid"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::Text(b) => assert_eq!(&*b, b"tester@localhost/tester"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"jid"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"bind"));
|
||||||
|
assert_matches!(s.next_xml_event().await?, Event::End(b) => assert_eq!(b.local_name().into_inner(), b"iq"));
|
||||||
|
s.send(r#"<presence xmlns="jabber:client" type="unavailable"><status>Logged out</status></presence>"#).await?;
|
||||||
|
|
||||||
stream.shutdown().await?;
|
stream.shutdown().await?;
|
||||||
|
|
||||||
// wrap up
|
// wrap up
|
||||||
|
|
Loading…
Reference in New Issue