2017-04-29 21:14:34 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2019-06-26 00:06:38 +00:00
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep@bouah.net>
|
2017-04-29 21:14:34 +00:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::ns;
|
|
|
|
use crate::stanza_error::StanzaError;
|
2019-10-22 23:32:41 +00:00
|
|
|
use jid::Jid;
|
2024-07-24 18:28:22 +00:00
|
|
|
use minidom::Element;
|
2018-12-18 14:32:05 +00:00
|
|
|
use minidom::IntoAttributeValue;
|
2024-06-21 14:27:43 +00:00
|
|
|
use xso::error::{Error, FromElementError};
|
2017-04-23 20:12:27 +00:00
|
|
|
|
2018-05-16 12:48:29 +00:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='get'/>`.
|
|
|
|
pub trait IqGetPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 17:31:17 +00:00
|
|
|
|
2018-05-16 12:48:29 +00:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='set'/>`.
|
|
|
|
pub trait IqSetPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 17:31:17 +00:00
|
|
|
|
2018-05-16 12:48:29 +00:00
|
|
|
/// Should be implemented on every known payload of an `<iq type='result'/>`.
|
|
|
|
pub trait IqResultPayload: TryFrom<Element> + Into<Element> {}
|
2017-07-20 17:31:17 +00:00
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Represents one of the four possible iq types.
|
2023-05-29 11:59:05 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-23 19:38:13 +00:00
|
|
|
pub enum IqType {
|
2018-08-08 17:52:37 +00:00
|
|
|
/// This is a request for accessing some data.
|
2017-05-18 23:04:42 +00:00
|
|
|
Get(Element),
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// This is a request for modifying some data.
|
2017-05-18 23:04:42 +00:00
|
|
|
Set(Element),
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// This is a result containing some data.
|
2017-05-18 23:04:42 +00:00
|
|
|
Result(Option<Element>),
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// A get or set request failed.
|
2017-05-06 20:13:53 +00:00
|
|
|
Error(StanzaError),
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 20:35:09 +00:00
|
|
|
impl<'a> IntoAttributeValue for &'a IqType {
|
2017-04-23 19:38:13 +00:00
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
2018-12-18 14:32:05 +00:00
|
|
|
Some(
|
|
|
|
match *self {
|
|
|
|
IqType::Get(_) => "get",
|
|
|
|
IqType::Set(_) => "set",
|
|
|
|
IqType::Result(_) => "result",
|
|
|
|
IqType::Error(_) => "error",
|
|
|
|
}
|
|
|
|
.to_owned(),
|
|
|
|
)
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:10:13 +00:00
|
|
|
/// The main structure representing the `<iq/>` stanza.
|
2023-05-29 11:59:05 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-23 19:38:13 +00:00
|
|
|
pub struct Iq {
|
2018-08-08 17:52:37 +00:00
|
|
|
/// The JID emitting this stanza.
|
2017-04-23 19:38:13 +00:00
|
|
|
pub from: Option<Jid>,
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
2017-04-23 19:38:13 +00:00
|
|
|
pub to: Option<Jid>,
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
/// request with its result/error.
|
2019-02-21 19:48:02 +00:00
|
|
|
pub id: String,
|
2018-08-08 17:52:37 +00:00
|
|
|
|
|
|
|
/// The payload content of this stanza.
|
2017-04-23 19:38:13 +00:00
|
|
|
pub payload: IqType,
|
|
|
|
}
|
|
|
|
|
2018-05-16 12:49:00 +00:00
|
|
|
impl Iq {
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Creates an `<iq/>` stanza containing a get request.
|
2019-02-26 18:25:43 +00:00
|
|
|
pub fn from_get<S: Into<String>>(id: S, payload: impl IqGetPayload) -> Iq {
|
2018-05-16 12:49:00 +00:00
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
to: None,
|
2019-02-26 18:25:43 +00:00
|
|
|
id: id.into(),
|
2018-05-16 12:49:00 +00:00
|
|
|
payload: IqType::Get(payload.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Creates an `<iq/>` stanza containing a set request.
|
2019-02-26 18:25:43 +00:00
|
|
|
pub fn from_set<S: Into<String>>(id: S, payload: impl IqSetPayload) -> Iq {
|
2018-05-16 12:49:00 +00:00
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
to: None,
|
2019-02-26 18:25:43 +00:00
|
|
|
id: id.into(),
|
2018-05-16 12:49:00 +00:00
|
|
|
payload: IqType::Set(payload.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-27 19:47:53 +00:00
|
|
|
/// Creates an empty `<iq type="result"/>` stanza.
|
|
|
|
pub fn empty_result<S: Into<String>>(to: Jid, id: S) -> Iq {
|
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
to: Some(to),
|
|
|
|
id: id.into(),
|
|
|
|
payload: IqType::Result(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Creates an `<iq/>` stanza containing a result.
|
2019-02-26 18:25:43 +00:00
|
|
|
pub fn from_result<S: Into<String>>(id: S, payload: Option<impl IqResultPayload>) -> Iq {
|
2018-05-16 12:49:00 +00:00
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
to: None,
|
2019-02-26 18:25:43 +00:00
|
|
|
id: id.into(),
|
2019-02-21 20:00:58 +00:00
|
|
|
payload: IqType::Result(payload.map(Into::into)),
|
2018-05-16 12:49:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Creates an `<iq/>` stanza containing an error.
|
2019-02-26 18:25:43 +00:00
|
|
|
pub fn from_error<S: Into<String>>(id: S, payload: StanzaError) -> Iq {
|
2018-05-16 12:49:00 +00:00
|
|
|
Iq {
|
|
|
|
from: None,
|
|
|
|
to: None,
|
2019-02-26 18:25:43 +00:00
|
|
|
id: id.into(),
|
2018-05-16 12:49:00 +00:00
|
|
|
payload: IqType::Error(payload),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Sets the recipient of this stanza.
|
2018-05-16 12:49:00 +00:00
|
|
|
pub fn with_to(mut self, to: Jid) -> Iq {
|
|
|
|
self.to = Some(to);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Sets the emitter of this stanza.
|
2018-05-16 12:49:00 +00:00
|
|
|
pub fn with_from(mut self, from: Jid) -> Iq {
|
|
|
|
self.from = Some(from);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-08-08 17:52:37 +00:00
|
|
|
/// Sets the id of this stanza, in order to later match its response.
|
2018-05-16 12:49:00 +00:00
|
|
|
pub fn with_id(mut self, id: String) -> Iq {
|
2019-02-21 19:48:02 +00:00
|
|
|
self.id = id;
|
2018-05-16 12:49:00 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
impl TryFrom<Element> for Iq {
|
2024-06-21 14:27:43 +00:00
|
|
|
type Error = FromElementError;
|
2017-04-23 19:38:13 +00:00
|
|
|
|
2024-06-21 14:27:43 +00:00
|
|
|
fn try_from(root: Element) -> Result<Iq, FromElementError> {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(root, "iq", DEFAULT_NS);
|
2019-02-24 19:48:19 +00:00
|
|
|
let from = get_attr!(root, "from", Option);
|
|
|
|
let to = get_attr!(root, "to", Option);
|
|
|
|
let id = get_attr!(root, "id", Required);
|
|
|
|
let type_: String = get_attr!(root, "type", Required);
|
2017-05-06 20:16:56 +00:00
|
|
|
|
|
|
|
let mut payload = None;
|
|
|
|
let mut error_payload = None;
|
|
|
|
for elem in root.children() {
|
|
|
|
if payload.is_some() {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
2017-05-06 20:16:56 +00:00
|
|
|
if type_ == "error" {
|
2017-07-29 05:49:02 +00:00
|
|
|
if elem.is("error", ns::DEFAULT_NS) {
|
2017-05-06 20:16:56 +00:00
|
|
|
if error_payload.is_some() {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-05-23 22:31:33 +00:00
|
|
|
error_payload = Some(StanzaError::try_from(elem.clone())?);
|
2017-07-20 22:08:23 +00:00
|
|
|
} else if root.children().count() != 2 {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-04-23 20:12:27 +00:00
|
|
|
} else {
|
2017-05-18 23:07:51 +00:00
|
|
|
payload = Some(elem.clone());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
let type_ = if type_ == "get" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Get(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
|
|
|
} else if type_ == "set" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Set(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
|
|
|
} else if type_ == "result" {
|
2017-05-18 23:07:51 +00:00
|
|
|
if let Some(payload) = payload {
|
|
|
|
IqType::Result(Some(payload))
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
|
|
|
IqType::Result(None)
|
|
|
|
}
|
|
|
|
} else if type_ == "error" {
|
2017-05-24 20:35:09 +00:00
|
|
|
if let Some(payload) = error_payload {
|
2017-05-18 23:07:51 +00:00
|
|
|
IqType::Error(payload)
|
2017-05-06 20:16:56 +00:00
|
|
|
} else {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Wrong number of children in iq element.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
} else {
|
2024-06-21 14:27:43 +00:00
|
|
|
return Err(Error::Other("Unknown iq type.").into());
|
2017-05-06 20:16:56 +00:00
|
|
|
};
|
2017-04-23 19:38:13 +00:00
|
|
|
|
2017-05-06 20:16:56 +00:00
|
|
|
Ok(Iq {
|
2019-02-21 20:00:58 +00:00
|
|
|
from,
|
|
|
|
to,
|
|
|
|
id,
|
2017-05-06 20:16:56 +00:00
|
|
|
payload: type_,
|
|
|
|
})
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Iq> for Element {
|
|
|
|
fn from(iq: Iq) -> Element {
|
2020-03-28 12:07:26 +00:00
|
|
|
let mut stanza = Element::builder("iq", ns::DEFAULT_NS)
|
2018-12-18 14:32:05 +00:00
|
|
|
.attr("from", iq.from)
|
|
|
|
.attr("to", iq.to)
|
|
|
|
.attr("id", iq.id)
|
|
|
|
.attr("type", &iq.payload)
|
|
|
|
.build();
|
2017-07-20 19:36:13 +00:00
|
|
|
let elem = match iq.payload {
|
2018-12-18 14:32:05 +00:00
|
|
|
IqType::Get(elem) | IqType::Set(elem) | IqType::Result(Some(elem)) => elem,
|
2017-05-23 22:31:33 +00:00
|
|
|
IqType::Error(error) => error.into(),
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Result(None) => return stanza,
|
|
|
|
};
|
|
|
|
stanza.append_child(elem);
|
|
|
|
stanza
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:13:53 +00:00
|
|
|
use super::*;
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::disco::DiscoInfoQuery;
|
2018-12-18 14:32:05 +00:00
|
|
|
use crate::stanza_error::{DefinedCondition, ErrorType};
|
2017-04-23 19:38:13 +00:00
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2024-07-10 14:38:00 +00:00
|
|
|
assert_size!(IqType, 108);
|
|
|
|
assert_size!(Iq, 152);
|
2018-10-28 12:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
2024-07-10 14:38:00 +00:00
|
|
|
assert_size!(IqType, 216);
|
|
|
|
assert_size!(Iq, 304);
|
2018-10-26 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-23 19:38:13 +00:00
|
|
|
#[test]
|
|
|
|
fn test_require_type() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 19:38:13 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
2017-04-23 19:38:13 +00:00
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(message, "Required attribute 'id' missing.");
|
|
|
|
|
|
|
|
#[cfg(not(feature = "component"))]
|
|
|
|
let elem: Element = "<iq xmlns='jabber:client' id='coucou'/>".parse().unwrap();
|
|
|
|
#[cfg(feature = "component")]
|
2019-10-22 23:32:41 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' id='coucou'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2019-02-21 19:48:02 +00:00
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-02-21 19:48:02 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'type' missing.");
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get' id='foo'>
|
2017-07-29 05:49:02 +00:00
|
|
|
<foo xmlns='bar'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='foo'>
|
2017-07-29 05:49:02 +00:00
|
|
|
<foo xmlns='bar'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
let query: Element = "<foo xmlns='bar'/>".parse().unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(&iq.id, "foo");
|
2017-04-23 19:38:13 +00:00
|
|
|
assert!(match iq.payload {
|
2019-11-29 13:33:17 +00:00
|
|
|
IqType::Get(element) => element == query,
|
2018-12-18 14:32:05 +00:00
|
|
|
_ => false,
|
2017-04-23 19:38:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_set() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='set' id='vcard'>
|
2017-04-23 19:38:13 +00:00
|
|
|
<vCard xmlns='vcard-temp'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='set' id='vcard'>
|
2017-07-29 05:49:02 +00:00
|
|
|
<vCard xmlns='vcard-temp'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
|
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(&iq.id, "vcard");
|
2017-04-23 19:38:13 +00:00
|
|
|
assert!(match iq.payload {
|
2019-11-29 13:33:17 +00:00
|
|
|
IqType::Set(element) => element == vcard,
|
2018-12-18 14:32:05 +00:00
|
|
|
_ => false,
|
2017-04-23 19:38:13 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result_empty() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-10-22 23:32:41 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(&iq.id, "res");
|
2017-04-23 19:38:13 +00:00
|
|
|
assert!(match iq.payload {
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Result(None) => true,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_result() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'>
|
2017-04-23 19:38:13 +00:00
|
|
|
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'>
|
2017-07-29 05:49:02 +00:00
|
|
|
<query xmlns='http://jabber.org/protocol/disco#items'/>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(&iq.id, "res");
|
2017-04-23 19:38:13 +00:00
|
|
|
assert!(match iq.payload {
|
2019-11-29 13:33:17 +00:00
|
|
|
IqType::Result(Some(element)) => element == query,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_error() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error' id='err1'>
|
2017-04-23 19:38:13 +00:00
|
|
|
<ping xmlns='urn:xmpp:ping'/>
|
|
|
|
<error type='cancel'>
|
|
|
|
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
|
|
|
</error>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='error' id='err1'>
|
2017-07-29 05:49:02 +00:00
|
|
|
<ping xmlns='urn:xmpp:ping'/>
|
|
|
|
<error type='cancel'>
|
|
|
|
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
|
|
|
|
</error>
|
2018-12-18 14:32:05 +00:00
|
|
|
</iq>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(iq.from, None);
|
|
|
|
assert_eq!(iq.to, None);
|
2019-02-21 19:48:02 +00:00
|
|
|
assert_eq!(iq.id, "err1");
|
2017-05-01 00:23:56 +00:00
|
|
|
match iq.payload {
|
2017-05-06 20:16:56 +00:00
|
|
|
IqType::Error(error) => {
|
2017-05-06 20:13:53 +00:00
|
|
|
assert_eq!(error.type_, ErrorType::Cancel);
|
2017-05-01 00:50:38 +00:00
|
|
|
assert_eq!(error.by, None);
|
2018-12-18 14:32:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
error.defined_condition,
|
|
|
|
DefinedCondition::ServiceUnavailable
|
|
|
|
);
|
2017-05-01 00:50:38 +00:00
|
|
|
assert_eq!(error.texts.len(), 0);
|
|
|
|
assert_eq!(error.other, None);
|
2018-12-18 14:32:05 +00:00
|
|
|
}
|
2017-05-01 00:23:56 +00:00
|
|
|
_ => panic!(),
|
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_children_invalid() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='error' id='error'/>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='error' id='error'/>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let error = Iq::try_from(elem).unwrap_err();
|
2017-04-23 19:38:13 +00:00
|
|
|
let message = match error {
|
2024-06-21 14:27:43 +00:00
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-23 19:38:13 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Wrong number of children in iq element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-10-22 23:32:41 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='result' id='res'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='result' id='res'/>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-05-06 20:16:56 +00:00
|
|
|
let iq2 = Iq {
|
2017-04-23 19:38:13 +00:00
|
|
|
from: None,
|
|
|
|
to: None,
|
2019-02-21 19:48:02 +00:00
|
|
|
id: String::from("res"),
|
2017-05-06 20:16:56 +00:00
|
|
|
payload: IqType::Result(None),
|
2017-04-23 19:38:13 +00:00
|
|
|
};
|
2017-05-23 22:31:33 +00:00
|
|
|
let elem2 = iq2.into();
|
2017-04-23 19:38:13 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
2017-04-23 20:12:27 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disco() {
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(not(feature = "component"))]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:client' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
#[cfg(feature = "component")]
|
2019-02-21 19:48:02 +00:00
|
|
|
let elem: Element = "<iq xmlns='jabber:component:accept' type='get' id='disco'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
let iq = Iq::try_from(elem).unwrap();
|
2018-05-16 12:48:29 +00:00
|
|
|
let disco_info = match iq.payload {
|
|
|
|
IqType::Get(payload) => DiscoInfoQuery::try_from(payload).unwrap(),
|
2017-05-18 23:04:42 +00:00
|
|
|
_ => panic!(),
|
|
|
|
};
|
2018-05-16 12:48:29 +00:00
|
|
|
assert!(disco_info.node.is_none());
|
2017-04-23 20:12:27 +00:00
|
|
|
}
|
2017-04-23 19:38:13 +00:00
|
|
|
}
|