2017-05-28 15:30:43 +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/.
|
|
|
|
|
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
2018-05-14 14:30:28 +00:00
|
|
|
|
generate_elem_id!(Group, "group", ROSTER);
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-06-13 23:50:57 +00:00
|
|
|
|
generate_attribute!(Subscription, "subscription", {
|
|
|
|
|
None => "none",
|
|
|
|
|
From => "from",
|
|
|
|
|
To => "to",
|
|
|
|
|
Both => "both",
|
|
|
|
|
Remove => "remove",
|
2017-10-10 16:40:29 +00:00
|
|
|
|
}, Default = None);
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-11-24 05:09:25 +00:00
|
|
|
|
generate_element_with_children!(
|
|
|
|
|
/// Contact from the user’s contact list.
|
|
|
|
|
#[derive(PartialEq)]
|
2018-05-14 14:30:28 +00:00
|
|
|
|
Item, "item", ROSTER,
|
2017-11-24 05:09:25 +00:00
|
|
|
|
attributes: [
|
|
|
|
|
/// JID of this contact.
|
|
|
|
|
jid: Jid = "jid" => required,
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-11-24 05:09:25 +00:00
|
|
|
|
/// Name of this contact.
|
|
|
|
|
name: Option<String> = "name" => optional_empty,
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-11-24 05:09:25 +00:00
|
|
|
|
/// Subscription status of this contact.
|
|
|
|
|
subscription: Subscription = "subscription" => default
|
|
|
|
|
],
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-11-24 05:09:25 +00:00
|
|
|
|
children: [
|
|
|
|
|
/// Groups this contact is part of.
|
2018-05-14 14:30:28 +00:00
|
|
|
|
groups: Vec<Group> = ("group", ROSTER) => Group
|
2017-11-24 05:09:25 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
2017-11-23 16:45:05 +00:00
|
|
|
|
generate_element_with_children!(
|
|
|
|
|
/// The contact list of the user.
|
2018-05-14 14:30:28 +00:00
|
|
|
|
Roster, "query", ROSTER,
|
2017-11-23 16:45:05 +00:00
|
|
|
|
attributes: [
|
|
|
|
|
/// Version of the contact list.
|
|
|
|
|
///
|
|
|
|
|
/// This is an opaque string that should only be sent back to the server on
|
|
|
|
|
/// a new connection, if this client is storing the contact list between
|
|
|
|
|
/// connections.
|
|
|
|
|
ver: Option<String> = "ver" => optional
|
|
|
|
|
],
|
|
|
|
|
children: [
|
|
|
|
|
/// List of the contacts of the user.
|
2018-05-14 14:30:28 +00:00
|
|
|
|
items: Vec<Item> = ("item", ROSTER) => Item
|
2017-11-23 16:45:05 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2018-05-14 14:07:15 +00:00
|
|
|
|
use try_from::TryFrom;
|
2018-05-14 14:17:21 +00:00
|
|
|
|
use minidom::Element;
|
2018-05-14 14:21:39 +00:00
|
|
|
|
use error::Error;
|
2018-05-14 14:11:22 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-08-18 23:04:18 +00:00
|
|
|
|
use compare_elements::NamespaceAwareCompare;
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get() {
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'/>".parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert!(roster.ver.is_none());
|
|
|
|
|
assert!(roster.items.is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_result() {
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster' ver='ver7'><item jid='nurse@example.com'/><item jid='romeo@example.net'/></query>".parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(roster.ver, Some(String::from("ver7")));
|
|
|
|
|
assert_eq!(roster.items.len(), 2);
|
|
|
|
|
|
2017-05-29 02:42:11 +00:00
|
|
|
|
let elem2: Element = "<query xmlns='jabber:iq:roster' ver='ver7'><item jid='nurse@example.com'/><item jid='romeo@example.net' name=''/></query>".parse().unwrap();
|
|
|
|
|
let roster2 = Roster::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(roster.items, roster2.items);
|
|
|
|
|
|
2017-05-28 15:30:43 +00:00
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster' ver='ver9'/>".parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(roster.ver, Some(String::from("ver9")));
|
|
|
|
|
assert!(roster.items.is_empty());
|
|
|
|
|
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='jabber:iq:roster' ver='ver11'>
|
|
|
|
|
<item jid='romeo@example.net'
|
|
|
|
|
name='Romeo'
|
|
|
|
|
subscription='both'>
|
|
|
|
|
<group>Friends</group>
|
|
|
|
|
</item>
|
|
|
|
|
<item jid='mercutio@example.com'
|
|
|
|
|
name='Mercutio'
|
|
|
|
|
subscription='from'/>
|
|
|
|
|
<item jid='benvolio@example.net'
|
|
|
|
|
name='Benvolio'
|
|
|
|
|
subscription='both'/>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(roster.ver, Some(String::from("ver11")));
|
|
|
|
|
assert_eq!(roster.items.len(), 3);
|
|
|
|
|
assert_eq!(roster.items[0].jid, Jid::from_str("romeo@example.net").unwrap());
|
|
|
|
|
assert_eq!(roster.items[0].name, Some(String::from("Romeo")));
|
2017-10-10 16:40:29 +00:00
|
|
|
|
assert_eq!(roster.items[0].subscription, Subscription::Both);
|
2017-07-29 04:36:59 +00:00
|
|
|
|
assert_eq!(roster.items[0].groups, vec!(Group::from_str("Friends").unwrap()));
|
2017-05-28 15:30:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 22:27:38 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_multiple_groups() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='jabber:iq:roster'>
|
|
|
|
|
<item jid='test@example.org'>
|
|
|
|
|
<group>A</group>
|
|
|
|
|
<group>B</group>
|
|
|
|
|
</item>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert!(roster.ver.is_none());
|
|
|
|
|
assert_eq!(roster.items.len(), 1);
|
|
|
|
|
assert_eq!(roster.items[0].jid, Jid::from_str("test@example.org").unwrap());
|
|
|
|
|
assert_eq!(roster.items[0].name, None);
|
|
|
|
|
assert_eq!(roster.items[0].groups.len(), 2);
|
2017-07-29 04:36:59 +00:00
|
|
|
|
assert_eq!(roster.items[0].groups[0], Group::from_str("A").unwrap());
|
|
|
|
|
assert_eq!(roster.items[0].groups[1], Group::from_str("B").unwrap());
|
2017-06-25 22:27:38 +00:00
|
|
|
|
let elem2 = roster.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2017-06-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-28 15:30:43 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_set() {
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'><item jid='nurse@example.com'/></query>".parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert!(roster.ver.is_none());
|
|
|
|
|
assert_eq!(roster.items.len(), 1);
|
|
|
|
|
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='jabber:iq:roster'>
|
|
|
|
|
<item jid='nurse@example.com'
|
|
|
|
|
name='Nurse'>
|
|
|
|
|
<group>Servants</group>
|
|
|
|
|
</item>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert!(roster.ver.is_none());
|
|
|
|
|
assert_eq!(roster.items.len(), 1);
|
|
|
|
|
assert_eq!(roster.items[0].jid, Jid::from_str("nurse@example.com").unwrap());
|
|
|
|
|
assert_eq!(roster.items[0].name, Some(String::from("Nurse")));
|
|
|
|
|
assert_eq!(roster.items[0].groups.len(), 1);
|
2017-07-29 04:36:59 +00:00
|
|
|
|
assert_eq!(roster.items[0].groups[0], Group::from_str("Servants").unwrap());
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='jabber:iq:roster'>
|
|
|
|
|
<item jid='nurse@example.com'
|
|
|
|
|
subscription='remove'/>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
|
|
|
|
let roster = Roster::try_from(elem).unwrap();
|
|
|
|
|
assert!(roster.ver.is_none());
|
|
|
|
|
assert_eq!(roster.items.len(), 1);
|
|
|
|
|
assert_eq!(roster.items[0].jid, Jid::from_str("nurse@example.com").unwrap());
|
|
|
|
|
assert!(roster.items[0].name.is_none());
|
|
|
|
|
assert!(roster.items[0].groups.is_empty());
|
2017-10-10 16:40:29 +00:00
|
|
|
|
assert_eq!(roster.items[0].subscription, Subscription::Remove);
|
2017-05-28 15:30:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid() {
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'><coucou/></query>".parse().unwrap();
|
|
|
|
|
let error = Roster::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-11-23 16:45:05 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in query element.");
|
2017-05-28 15:30:43 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster' coucou=''/>".parse().unwrap();
|
|
|
|
|
let error = Roster::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-11-23 16:45:05 +00:00
|
|
|
|
assert_eq!(message, "Unknown attribute in query element.");
|
2017-05-28 15:30:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_item() {
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'><item/></query>".parse().unwrap();
|
|
|
|
|
let error = Roster::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Required attribute 'jid' missing.");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'><item jid=''/></query>".parse().unwrap();
|
|
|
|
|
let error = Roster::try_from(elem).unwrap_err();
|
|
|
|
|
let error = match error {
|
|
|
|
|
Error::JidParseError(error) => error,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(error.description(), "Invalid JID, I guess?");
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<query xmlns='jabber:iq:roster'><item jid='coucou'><coucou/></item></query>".parse().unwrap();
|
|
|
|
|
let error = Roster::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-11-24 05:09:25 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in item element.");
|
2017-05-28 15:30:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|