2017-04-29 22:14:34 +01:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
|
//
|
|
|
|
|
|
// 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/.
|
|
|
|
|
|
|
2024-12-18 18:45:17 +01:00
|
|
|
|
use xso::{AsXml, FromXml};
|
2024-06-29 16:34:27 +02:00
|
|
|
|
|
2024-12-18 18:45:17 +01:00
|
|
|
|
use crate::data_forms::DataForm;
|
2018-12-18 15:27:30 +01:00
|
|
|
|
use crate::iq::{IqGetPayload, IqResultPayload};
|
2018-12-18 15:32:05 +01:00
|
|
|
|
use crate::ns;
|
2024-03-03 14:44:39 +01:00
|
|
|
|
use crate::rsm::{SetQuery, SetResult};
|
2019-10-23 01:32:41 +02:00
|
|
|
|
use jid::Jid;
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// It should only be used in an `<iq type='get'/>`, as it can only represent
|
|
|
|
|
|
/// the request, and not a result.
|
2024-07-09 17:01:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(namespace = ns::DISCO_INFO, name = "query")]
|
|
|
|
|
|
pub struct DiscoInfoQuery {
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Node on which we are doing the discovery.
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub node: Option<String>,
|
|
|
|
|
|
}
|
2017-07-20 17:39:59 +01:00
|
|
|
|
|
2018-05-17 19:24:51 +02:00
|
|
|
|
impl IqGetPayload for DiscoInfoQuery {}
|
2018-05-16 15:08:17 +02:00
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Structure representing a `<feature xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
2024-07-09 17:01:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq, Eq, Hash)]
|
2024-06-30 21:42:50 +02:00
|
|
|
|
#[xml(namespace = ns::DISCO_INFO, name = "feature")]
|
|
|
|
|
|
pub struct Feature {
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Namespace of the feature we want to represent.
|
2024-06-30 21:42:50 +02:00
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
|
pub var: String,
|
|
|
|
|
|
}
|
2017-05-24 23:47:27 +01:00
|
|
|
|
|
2019-01-27 17:18:58 +01:00
|
|
|
|
impl Feature {
|
|
|
|
|
|
/// Create a new `<feature/>` with the according `@var`.
|
2019-02-26 19:43:29 +01:00
|
|
|
|
pub fn new<S: Into<String>>(var: S) -> Feature {
|
2019-10-23 01:32:41 +02:00
|
|
|
|
Feature { var: var.into() }
|
2019-01-27 17:18:58 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-08 18:42:56 +02:00
|
|
|
|
/// Structure representing an `<identity xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
|
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
|
#[xml(namespace = ns::DISCO_INFO, name = "identity")]
|
|
|
|
|
|
pub struct Identity {
|
|
|
|
|
|
/// Category of this identity.
|
|
|
|
|
|
// TODO: use an enum here.
|
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
|
pub category: String,
|
|
|
|
|
|
|
|
|
|
|
|
/// Type of this identity.
|
|
|
|
|
|
// TODO: use an enum here.
|
|
|
|
|
|
#[xml(attribute = "type")]
|
|
|
|
|
|
pub type_: String,
|
|
|
|
|
|
|
|
|
|
|
|
/// Lang of the name of this identity.
|
|
|
|
|
|
#[xml(attribute(default, name = "xml:lang"))]
|
|
|
|
|
|
pub lang: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// Name of this identity.
|
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub name: Option<String>,
|
|
|
|
|
|
}
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2019-02-26 19:43:29 +01:00
|
|
|
|
impl Identity {
|
|
|
|
|
|
/// Create a new `<identity/>`.
|
|
|
|
|
|
pub fn new<C, T, L, N>(category: C, type_: T, lang: L, name: N) -> Identity
|
2019-10-23 01:32:41 +02:00
|
|
|
|
where
|
|
|
|
|
|
C: Into<String>,
|
|
|
|
|
|
T: Into<String>,
|
|
|
|
|
|
L: Into<String>,
|
|
|
|
|
|
N: Into<String>,
|
2019-02-26 19:43:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
Identity {
|
|
|
|
|
|
category: category.into(),
|
|
|
|
|
|
type_: type_.into(),
|
|
|
|
|
|
lang: Some(lang.into()),
|
|
|
|
|
|
name: Some(name.into()),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Create a new `<identity/>` without a name.
|
|
|
|
|
|
pub fn new_anonymous<C, T, L, N>(category: C, type_: T) -> Identity
|
2019-10-23 01:32:41 +02:00
|
|
|
|
where
|
|
|
|
|
|
C: Into<String>,
|
|
|
|
|
|
T: Into<String>,
|
2019-02-26 19:43:29 +01:00
|
|
|
|
{
|
|
|
|
|
|
Identity {
|
|
|
|
|
|
category: category.into(),
|
|
|
|
|
|
type_: type_.into(),
|
|
|
|
|
|
lang: None,
|
|
|
|
|
|
name: None,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// It should only be used in an `<iq type='result'/>`, as it can only
|
|
|
|
|
|
/// represent the result, and not a request.
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone)]
|
|
|
|
|
|
#[xml(namespace = ns::DISCO_INFO, name = "query")]
|
2017-07-20 17:39:59 +01:00
|
|
|
|
pub struct DiscoInfoResult {
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Node on which we have done this discovery.
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[xml(attribute(default))]
|
2017-04-18 20:44:36 +01:00
|
|
|
|
pub node: Option<String>,
|
2017-07-29 04:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
/// List of identities exposed by this entity.
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[xml(child(n = ..))]
|
2017-04-18 20:44:36 +01:00
|
|
|
|
pub identities: Vec<Identity>,
|
2017-07-29 04:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
/// List of features supported by this entity.
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[xml(child(n = ..))]
|
2017-04-18 20:44:36 +01:00
|
|
|
|
pub features: Vec<Feature>,
|
2017-07-29 04:51:51 +01:00
|
|
|
|
|
|
|
|
|
|
/// List of extensions reported by this entity.
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[xml(child(n = ..))]
|
2017-04-18 20:44:36 +01:00
|
|
|
|
pub extensions: Vec<DataForm>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-16 15:08:17 +02:00
|
|
|
|
impl IqResultPayload for DiscoInfoResult {}
|
|
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Structure representing a `<query xmlns='http://jabber.org/protocol/disco#items'/>` element.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// It should only be used in an `<iq type='get'/>`, as it can only represent
|
|
|
|
|
|
/// the request, and not a result.
|
2024-06-30 09:42:55 +02:00
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
|
|
|
|
|
#[xml(namespace = ns::DISCO_ITEMS, name = "query")]
|
|
|
|
|
|
pub struct DiscoItemsQuery {
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Node on which we are doing the discovery.
|
2024-06-30 09:42:55 +02:00
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub node: Option<String>,
|
|
|
|
|
|
|
2024-03-03 14:44:39 +01:00
|
|
|
|
/// Optional paging via Result Set Management
|
2024-06-30 09:42:55 +02:00
|
|
|
|
#[xml(child(default))]
|
|
|
|
|
|
pub rsm: Option<SetQuery>,
|
|
|
|
|
|
}
|
2017-07-21 17:33:58 +01:00
|
|
|
|
|
2018-05-17 19:24:51 +02:00
|
|
|
|
impl IqGetPayload for DiscoItemsQuery {}
|
2018-05-16 15:08:17 +02:00
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Structure representing an `<item xmlns='http://jabber.org/protocol/disco#items'/>` element.
|
2024-07-09 17:01:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(namespace = ns::DISCO_ITEMS, name = "item")]
|
|
|
|
|
|
pub struct Item {
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// JID of the entity pointed by this item.
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
|
pub jid: Jid,
|
|
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Node of the entity pointed by this item.
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub node: Option<String>,
|
|
|
|
|
|
|
2017-07-29 04:51:51 +01:00
|
|
|
|
/// Name of the entity pointed by this item.
|
2024-06-29 16:34:27 +02:00
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub name: Option<String>,
|
|
|
|
|
|
}
|
2017-07-21 17:33:58 +01:00
|
|
|
|
|
2024-08-03 13:04:56 +02:00
|
|
|
|
/// Structure representing a `<query
|
|
|
|
|
|
/// xmlns='http://jabber.org/protocol/disco#items'/>` element.
|
|
|
|
|
|
///
|
|
|
|
|
|
/// It should only be used in an `<iq type='result'/>`, as it can only
|
|
|
|
|
|
/// represent the result, and not a request.
|
|
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
|
|
|
|
|
#[xml(namespace = ns::DISCO_ITEMS, name = "query")]
|
|
|
|
|
|
pub struct DiscoItemsResult {
|
|
|
|
|
|
/// Node on which we have done this discovery.
|
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub node: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// List of items pointed by this entity.
|
|
|
|
|
|
#[xml(child(n = ..))]
|
|
|
|
|
|
pub items: Vec<Item>,
|
|
|
|
|
|
|
|
|
|
|
|
/// Optional paging via Result Set Management
|
|
|
|
|
|
#[xml(child(default))]
|
|
|
|
|
|
pub rsm: Option<SetResult>,
|
|
|
|
|
|
}
|
2017-07-21 17:33:58 +01:00
|
|
|
|
|
2018-05-16 15:08:17 +02:00
|
|
|
|
impl IqResultPayload for DiscoItemsResult {}
|
|
|
|
|
|
|
2017-04-18 20:44:36 +01:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
2017-05-06 21:01:15 +01:00
|
|
|
|
use super::*;
|
2020-12-10 20:45:30 +01:00
|
|
|
|
use jid::BareJid;
|
2024-12-18 18:45:17 +01:00
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
use xso::error::{Error, FromElementError};
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2018-10-28 13:10:48 +01:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
|
|
|
|
|
assert_size!(Identity, 48);
|
|
|
|
|
|
assert_size!(Feature, 12);
|
|
|
|
|
|
assert_size!(DiscoInfoQuery, 12);
|
|
|
|
|
|
assert_size!(DiscoInfoResult, 48);
|
|
|
|
|
|
|
2024-04-15 17:03:57 +02:00
|
|
|
|
assert_size!(Item, 40);
|
2024-03-03 14:44:39 +01:00
|
|
|
|
assert_size!(DiscoItemsQuery, 52);
|
|
|
|
|
|
assert_size!(DiscoItemsResult, 64);
|
2018-10-28 13:10:48 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 14:26:16 +02:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
|
|
|
|
|
assert_size!(Identity, 96);
|
|
|
|
|
|
assert_size!(Feature, 24);
|
|
|
|
|
|
assert_size!(DiscoInfoQuery, 24);
|
|
|
|
|
|
assert_size!(DiscoInfoResult, 96);
|
|
|
|
|
|
|
2024-04-15 17:03:57 +02:00
|
|
|
|
assert_size!(Item, 80);
|
2024-03-03 14:44:39 +01:00
|
|
|
|
assert_size!(DiscoItemsQuery, 104);
|
|
|
|
|
|
assert_size!(DiscoItemsResult, 128);
|
2018-10-26 14:26:16 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-18 20:44:36 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
|
|
assert!(query.extensions.is_empty());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-11-02 16:27:51 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_identity_after_feature() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature var='http://jabber.org/protocol/disco#info'/><identity category='client' type='pc'/></query>".parse().unwrap();
|
|
|
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
|
|
assert!(query.extensions.is_empty());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_feature_after_dataform() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>coucou</value></field></x><feature var='http://jabber.org/protocol/disco#info'/></query>".parse().unwrap();
|
|
|
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
|
|
assert_eq!(query.extensions.len(), 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-07-29 04:39:50 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_extension() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#info'/><x xmlns='jabber:x:data' type='result'><field var='FORM_TYPE' type='hidden'><value>example</value></field></x></query>".parse().unwrap();
|
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
|
|
assert_eq!(query.extensions.len(), 1);
|
|
|
|
|
|
assert_eq!(query.extensions[0].form_type, Some(String::from("example")));
|
|
|
|
|
|
|
|
|
|
|
|
let elem2 = query.into();
|
2019-11-29 14:33:17 +01:00
|
|
|
|
assert_eq!(elem1, elem2);
|
2017-07-29 04:39:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-18 20:44:36 +01:00
|
|
|
|
#[test]
|
2024-12-20 08:57:46 +01:00
|
|
|
|
#[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
|
2017-04-18 20:44:36 +01:00
|
|
|
|
fn test_invalid() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-12-18 18:45:17 +01:00
|
|
|
|
assert_eq!(message, "Unknown child in DiscoInfoResult element.");
|
2017-04-20 21:02:51 +01:00
|
|
|
|
}
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2017-04-20 21:02:51 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_identity() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-08 18:42:56 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'category' on Identity element missing."
|
|
|
|
|
|
);
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
2024-08-08 18:42:56 +02:00
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><identity type='coucou'/></query>"
|
2018-12-18 15:32:05 +01:00
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-08 18:42:56 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'category' on Identity element missing."
|
|
|
|
|
|
);
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-08 18:42:56 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'type_' on Identity element missing."
|
|
|
|
|
|
);
|
2017-04-20 21:02:51 +01:00
|
|
|
|
}
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
2017-04-20 21:02:51 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_feature() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element =
|
|
|
|
|
|
"<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-30 21:42:50 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'var' on Feature element missing."
|
|
|
|
|
|
);
|
2017-04-20 21:02:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-18 18:45:17 +01:00
|
|
|
|
// TODO: We stopped validating that there are enough identities and features in this result,
|
|
|
|
|
|
// this is a limitation of xso which accepts n = .. only, and not n = 1.., so let’s wait until
|
|
|
|
|
|
// xso implements this to reenable this test.
|
2017-04-20 21:02:51 +01:00
|
|
|
|
#[test]
|
2024-12-18 18:45:17 +01:00
|
|
|
|
#[ignore]
|
2017-04-20 21:02:51 +01:00
|
|
|
|
fn test_invalid_result() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-20 21:02:51 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-20 21:02:51 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"There must be at least one identity in disco#info."
|
|
|
|
|
|
);
|
2017-04-18 20:44:36 +01:00
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
|
2017-07-20 17:39:59 +01:00
|
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 20:44:36 +01:00
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-04-18 20:44:36 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
|
|
|
|
|
assert_eq!(message, "There must be at least one feature in disco#info.");
|
|
|
|
|
|
}
|
2017-07-21 17:33:58 +01:00
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_simple_items() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-21 17:33:58 +01:00
|
|
|
|
let query = DiscoItemsQuery::try_from(elem).unwrap();
|
|
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-21 17:33:58 +01:00
|
|
|
|
let query = DiscoItemsQuery::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(query.node, Some(String::from("coucou")));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_simple_items_result() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-21 17:33:58 +01:00
|
|
|
|
let query = DiscoItemsResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
|
|
assert!(query.items.is_empty());
|
|
|
|
|
|
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-07-21 17:33:58 +01:00
|
|
|
|
let query = DiscoItemsResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(query.node, Some(String::from("coucou")));
|
|
|
|
|
|
assert!(query.items.is_empty());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_answers_items_result() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'><item jid='component'/><item jid='component2' node='test' name='A component'/></query>".parse().unwrap();
|
|
|
|
|
|
let query = DiscoItemsResult::try_from(elem).unwrap();
|
2017-11-23 16:30:53 +00:00
|
|
|
|
let elem2 = Element::from(query);
|
|
|
|
|
|
let query = DiscoItemsResult::try_from(elem2).unwrap();
|
2017-07-21 17:33:58 +01:00
|
|
|
|
assert_eq!(query.items.len(), 2);
|
2023-06-20 14:07:50 +02:00
|
|
|
|
assert_eq!(query.items[0].jid, BareJid::new("component").unwrap());
|
2017-07-21 17:33:58 +01:00
|
|
|
|
assert_eq!(query.items[0].node, None);
|
|
|
|
|
|
assert_eq!(query.items[0].name, None);
|
2023-06-20 14:07:50 +02:00
|
|
|
|
assert_eq!(query.items[1].jid, BareJid::new("component2").unwrap());
|
2017-07-21 17:33:58 +01:00
|
|
|
|
assert_eq!(query.items[1].node, Some(String::from("test")));
|
|
|
|
|
|
assert_eq!(query.items[1].name, Some(String::from("A component")));
|
|
|
|
|
|
}
|
2024-12-15 21:56:25 +01:00
|
|
|
|
|
|
|
|
|
|
// WORKAROUND FOR PROSODY BUG 1664, DO NOT REMOVE BEFORE 2028-12-17 (5 YEARS AFTER FIX)
|
|
|
|
|
|
// https://issues.prosody.im/1664
|
|
|
|
|
|
// See also:
|
|
|
|
|
|
// https://gitlab.com/xmpp-rs/xmpp-rs/-/issues/128
|
|
|
|
|
|
// https://gitlab.com/xmpp-rs/xmpp-rs/-/merge_requests/302
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_missing_disco_info_feature_workaround() {
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/muc#user'/></query>".parse().unwrap();
|
|
|
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
|
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
|
|
assert!(query.extensions.is_empty());
|
|
|
|
|
|
}
|
2017-04-18 20:44:36 +01:00
|
|
|
|
}
|