2017-04-29 21:14:34 +00: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/.
|
|
|
|
|
2017-07-29 03:51:51 +00:00
|
|
|
#![deny(missing_docs)]
|
|
|
|
|
2017-07-20 19:03:15 +00:00
|
|
|
use try_from::TryFrom;
|
2017-05-06 19:51:39 +00:00
|
|
|
|
2017-07-21 00:20:29 +00:00
|
|
|
use minidom::Element;
|
2017-07-21 16:33:58 +00:00
|
|
|
use jid::Jid;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
use error::Error;
|
2017-04-20 22:16:12 +00:00
|
|
|
use ns;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-05-06 19:51:39 +00:00
|
|
|
use data_forms::{DataForm, DataFormType};
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-07-29 03:51:51 +00: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.
|
2017-07-20 16:39:59 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DiscoInfoQuery {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Node on which we are doing the discovery.
|
2017-07-20 16:39:59 +00:00
|
|
|
pub node: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for DiscoInfoQuery {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-07-20 16:39:59 +00:00
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<DiscoInfoQuery, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "query", ns::DISCO_INFO);
|
|
|
|
check_no_children!(elem, "query");
|
|
|
|
check_no_unknown_attributes!(elem, "query", ["node"]);
|
2017-07-20 16:39:59 +00:00
|
|
|
Ok(DiscoInfoQuery {
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DiscoInfoQuery> for Element {
|
|
|
|
fn from(disco: DiscoInfoQuery) -> Element {
|
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_INFO)
|
|
|
|
.attr("node", disco.node)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Structure representing a `<feature xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
2017-04-20 23:41:15 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-18 19:44:36 +00:00
|
|
|
pub struct Feature {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Namespace of the feature we want to represent.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub var: String,
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:35:15 +00:00
|
|
|
impl TryFrom<Element> for Feature {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Feature, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "feature", ns::DISCO_INFO, "disco#info feature");
|
|
|
|
check_no_children!(elem, "disco#info feature");
|
|
|
|
check_no_unknown_attributes!(elem, "disco#info feature", ["var"]);
|
2017-07-29 03:35:15 +00:00
|
|
|
Ok(Feature {
|
|
|
|
var: get_attr!(elem, "var", required)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Feature> for Element {
|
|
|
|
fn from(feature: Feature) -> Element {
|
2017-05-24 22:47:27 +00:00
|
|
|
Element::builder("feature")
|
|
|
|
.ns(ns::DISCO_INFO)
|
2017-07-20 19:36:13 +00:00
|
|
|
.attr("var", feature.var)
|
2017-05-24 22:47:27 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Structure representing an `<identity xmlns='http://jabber.org/protocol/disco#info'/>` element.
|
2017-04-20 23:41:15 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2017-04-18 19:44:36 +00:00
|
|
|
pub struct Identity {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Category of this identity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub category: String, // TODO: use an enum here.
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// Type of this identity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub type_: String, // TODO: use an enum here.
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// Lang of the name of this identity.
|
2017-05-27 21:10:00 +00:00
|
|
|
pub lang: Option<String>,
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// Name of this identity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:35:15 +00:00
|
|
|
impl TryFrom<Element> for Identity {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Identity, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "identity", ns::DISCO_INFO, "disco#info identity");
|
|
|
|
check_no_children!(elem, "disco#info identity");
|
|
|
|
check_no_unknown_attributes!(elem, "disco#info identity", ["category", "type", "xml:lang", "name"]);
|
2017-07-29 03:35:15 +00:00
|
|
|
|
|
|
|
let category = get_attr!(elem, "category", required);
|
|
|
|
if category == "" {
|
|
|
|
return Err(Error::ParseError("Identity must have a non-empty 'category' attribute."))
|
|
|
|
}
|
|
|
|
|
|
|
|
let type_ = get_attr!(elem, "type", required);
|
|
|
|
if type_ == "" {
|
|
|
|
return Err(Error::ParseError("Identity must have a non-empty 'type' attribute."))
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Identity {
|
|
|
|
category: category,
|
|
|
|
type_: type_,
|
|
|
|
lang: get_attr!(elem, "xml:lang", optional),
|
|
|
|
name: get_attr!(elem, "name", optional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<Identity> for Element {
|
|
|
|
fn from(identity: Identity) -> Element {
|
2017-05-24 22:47:27 +00:00
|
|
|
Element::builder("identity")
|
|
|
|
.ns(ns::DISCO_INFO)
|
2017-07-20 19:36:13 +00:00
|
|
|
.attr("category", identity.category)
|
|
|
|
.attr("type", identity.type_)
|
|
|
|
.attr("xml:lang", identity.lang)
|
|
|
|
.attr("name", identity.name)
|
2017-05-24 22:47:27 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00: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.
|
2017-04-20 23:41:15 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2017-07-20 16:39:59 +00:00
|
|
|
pub struct DiscoInfoResult {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Node on which we have done this discovery.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub node: Option<String>,
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// List of identities exposed by this entity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub identities: Vec<Identity>,
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// List of features supported by this entity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub features: Vec<Feature>,
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// List of extensions reported by this entity.
|
2017-04-18 19:44:36 +00:00
|
|
|
pub extensions: Vec<DataForm>,
|
|
|
|
}
|
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
impl TryFrom<Element> for DiscoInfoResult {
|
2017-07-20 19:03:15 +00:00
|
|
|
type Err = Error;
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-07-20 16:39:59 +00:00
|
|
|
fn try_from(elem: Element) -> Result<DiscoInfoResult, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "query", ns::DISCO_INFO, "disco#info result");
|
|
|
|
check_no_unknown_attributes!(elem, "disco#info result", ["node"]);
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-07-29 03:25:55 +00:00
|
|
|
let mut result = DiscoInfoResult {
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
identities: vec!(),
|
|
|
|
features: vec!(),
|
|
|
|
extensions: vec!(),
|
|
|
|
};
|
2017-05-06 20:01:15 +00:00
|
|
|
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("feature", ns::DISCO_INFO) {
|
2017-07-29 03:35:15 +00:00
|
|
|
let feature = Feature::try_from(child.clone())?;
|
|
|
|
result.features.push(feature);
|
2017-05-06 20:01:15 +00:00
|
|
|
} else if child.is("identity", ns::DISCO_INFO) {
|
2017-07-29 03:35:15 +00:00
|
|
|
let identity = Identity::try_from(child.clone())?;
|
|
|
|
result.identities.push(identity);
|
2017-05-06 20:01:15 +00:00
|
|
|
} else if child.is("x", ns::DATA_FORMS) {
|
2017-05-23 22:31:33 +00:00
|
|
|
let data_form = DataForm::try_from(child.clone())?;
|
2017-05-22 18:00:04 +00:00
|
|
|
if data_form.type_ != DataFormType::Result_ {
|
|
|
|
return Err(Error::ParseError("Data form must have a 'result' type in disco#info."));
|
2017-05-06 20:01:15 +00:00
|
|
|
}
|
2017-07-29 03:25:55 +00:00
|
|
|
if data_form.form_type.is_none() {
|
|
|
|
return Err(Error::ParseError("Data form found without a FORM_TYPE."));
|
2017-05-06 20:01:15 +00:00
|
|
|
}
|
2017-07-29 03:25:55 +00:00
|
|
|
result.extensions.push(data_form);
|
2017-05-06 20:01:15 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown element in disco#info."));
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:25:55 +00:00
|
|
|
if result.identities.is_empty() {
|
2017-05-06 20:01:15 +00:00
|
|
|
return Err(Error::ParseError("There must be at least one identity in disco#info."));
|
|
|
|
}
|
2017-07-29 03:25:55 +00:00
|
|
|
if result.features.is_empty() {
|
2017-05-06 20:01:15 +00:00
|
|
|
return Err(Error::ParseError("There must be at least one feature in disco#info."));
|
|
|
|
}
|
2017-07-29 03:25:55 +00:00
|
|
|
if !result.features.contains(&Feature { var: ns::DISCO_INFO.to_owned() }) {
|
2017-05-06 20:01:15 +00:00
|
|
|
return Err(Error::ParseError("disco#info feature not present in disco#info."));
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:25:55 +00:00
|
|
|
Ok(result)
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
impl From<DiscoInfoResult> for Element {
|
|
|
|
fn from(disco: DiscoInfoResult) -> Element {
|
2017-05-24 22:47:27 +00:00
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_INFO)
|
2017-07-20 19:36:13 +00:00
|
|
|
.attr("node", disco.node)
|
|
|
|
.append(disco.identities)
|
|
|
|
.append(disco.features)
|
2017-07-29 03:39:50 +00:00
|
|
|
.append(disco.extensions)
|
2017-05-24 22:47:27 +00:00
|
|
|
.build()
|
2017-04-20 20:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00: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.
|
2017-07-21 16:33:58 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DiscoItemsQuery {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Node on which we are doing the discovery.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub node: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for DiscoItemsQuery {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<DiscoItemsQuery, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query");
|
|
|
|
check_no_children!(elem, "disco#items query");
|
|
|
|
check_no_unknown_attributes!(elem, "disco#items query", ["node"]);
|
2017-07-21 16:33:58 +00:00
|
|
|
Ok(DiscoItemsQuery {
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DiscoItemsQuery> for Element {
|
|
|
|
fn from(disco: DiscoItemsQuery) -> Element {
|
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_ITEMS)
|
|
|
|
.attr("node", disco.node)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Structure representing an `<item xmlns='http://jabber.org/protocol/disco#items'/>` element.
|
2017-07-21 16:33:58 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Item {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// JID of the entity pointed by this item.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub jid: Jid,
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Node of the entity pointed by this item.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub node: Option<String>,
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Name of the entity pointed by this item.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub name: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Item {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Item, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "item", ns::DISCO_ITEMS);
|
|
|
|
check_no_children!(elem, "item");
|
|
|
|
check_no_unknown_attributes!(elem, "item", ["jid", "node", "name"]);
|
2017-07-21 16:33:58 +00:00
|
|
|
Ok(Item {
|
|
|
|
jid: get_attr!(elem, "jid", required),
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
name: get_attr!(elem, "name", optional),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Item> for Element {
|
|
|
|
fn from(item: Item) -> Element {
|
|
|
|
Element::builder("item")
|
|
|
|
.ns(ns::DISCO_ITEMS)
|
2017-07-29 05:28:20 +00:00
|
|
|
.attr("jid", item.jid)
|
2017-07-21 16:33:58 +00:00
|
|
|
.attr("node", item.node)
|
|
|
|
.attr("name", item.name)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:51:51 +00: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.
|
2017-07-21 16:33:58 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct DiscoItemsResult {
|
2017-07-29 03:51:51 +00:00
|
|
|
/// Node on which we have done this discovery.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub node: Option<String>,
|
2017-07-29 03:51:51 +00:00
|
|
|
|
|
|
|
/// List of items pointed by this entity.
|
2017-07-21 16:33:58 +00:00
|
|
|
pub items: Vec<Item>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom<Element> for DiscoItemsResult {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<DiscoItemsResult, Error> {
|
2017-10-10 18:00:42 +00:00
|
|
|
check_self!(elem, "query", ns::DISCO_ITEMS, "disco#items query");
|
|
|
|
check_no_unknown_attributes!(elem, "disco#items query", ["node"]);
|
2017-07-21 16:33:58 +00:00
|
|
|
|
|
|
|
let mut items: Vec<Item> = vec!();
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("item", ns::DISCO_ITEMS) {
|
|
|
|
items.push(Item::try_from(child.clone())?);
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown element in disco#items."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(DiscoItemsResult {
|
|
|
|
node: get_attr!(elem, "node", optional),
|
|
|
|
items: items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<DiscoItemsResult> for Element {
|
|
|
|
fn from(disco: DiscoItemsResult) -> Element {
|
|
|
|
Element::builder("query")
|
|
|
|
.ns(ns::DISCO_ITEMS)
|
|
|
|
.attr("node", disco.node)
|
|
|
|
.append(disco.items)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-18 19:44:36 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:01:15 +00:00
|
|
|
use super::*;
|
2017-08-18 23:04:18 +00:00
|
|
|
use compare_elements::NamespaceAwareCompare;
|
2017-07-21 16:33:58 +00:00
|
|
|
use std::str::FromStr;
|
2017-04-18 19:44:36 +00: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 16:39:59 +00:00
|
|
|
let query = DiscoInfoResult::try_from(elem).unwrap();
|
2017-04-18 19:44:36 +00:00
|
|
|
assert!(query.node.is_none());
|
|
|
|
assert_eq!(query.identities.len(), 1);
|
|
|
|
assert_eq!(query.features.len(), 1);
|
|
|
|
assert!(query.extensions.is_empty());
|
|
|
|
}
|
|
|
|
|
2017-07-29 03:39:50 +00: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();
|
2017-08-18 23:04:18 +00:00
|
|
|
assert!(elem1.compare_to(&elem2));
|
2017-07-29 03:39:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-18 19:44:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid() {
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><coucou/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown element in disco#info.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-04-20 20:02:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_identity() {
|
2017-04-18 19:44:36 +00:00
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'category' missing.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category=''/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Identity must have a non-empty 'category' attribute.");
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou'/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'type' missing.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='coucou' type=''/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Identity must have a non-empty 'type' attribute.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
|
2017-04-20 20:02:51 +00:00
|
|
|
#[test]
|
|
|
|
fn test_invalid_feature() {
|
2017-04-18 19:44:36 +00:00
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><feature/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
2017-05-22 18:00:04 +00:00
|
|
|
assert_eq!(message, "Required attribute 'var' missing.");
|
2017-04-20 20:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_invalid_result() {
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'/>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-20 20:02:51 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "There must be at least one identity in disco#info.");
|
2017-04-18 19:44:36 +00:00
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "There must be at least one feature in disco#info.");
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#info'><identity category='client' type='pc'/><feature var='http://jabber.org/protocol/disco#items'/></query>".parse().unwrap();
|
2017-07-20 16:39:59 +00:00
|
|
|
let error = DiscoInfoResult::try_from(elem).unwrap_err();
|
2017-04-18 19:44:36 +00:00
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "disco#info feature not present in disco#info.");
|
|
|
|
}
|
2017-07-21 16:33:58 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple_items() {
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
|
|
|
|
let query = DiscoItemsQuery::try_from(elem).unwrap();
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>".parse().unwrap();
|
|
|
|
let query = DiscoItemsQuery::try_from(elem).unwrap();
|
|
|
|
assert_eq!(query.node, Some(String::from("coucou")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple_items_result() {
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
|
|
|
|
let query = DiscoItemsResult::try_from(elem).unwrap();
|
|
|
|
assert!(query.node.is_none());
|
|
|
|
assert!(query.items.is_empty());
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='http://jabber.org/protocol/disco#items' node='coucou'/>".parse().unwrap();
|
|
|
|
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();
|
|
|
|
assert_eq!(query.items.len(), 2);
|
|
|
|
assert_eq!(query.items[0].jid, Jid::from_str("component").unwrap());
|
|
|
|
assert_eq!(query.items[0].node, None);
|
|
|
|
assert_eq!(query.items[0].name, None);
|
|
|
|
assert_eq!(query.items[1].jid, Jid::from_str("component2").unwrap());
|
|
|
|
assert_eq!(query.items[1].node, Some(String::from("test")));
|
|
|
|
assert_eq!(query.items[1].name, Some(String::from("A component")));
|
|
|
|
}
|
2017-04-18 19:44:36 +00:00
|
|
|
}
|