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-20 19:03:15 +00:00
|
|
|
|
use try_from::TryFrom;
|
2017-04-19 01:27:42 +00:00
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
2017-07-21 00:20:29 +00:00
|
|
|
|
use minidom::{Element, IntoAttributeValue};
|
2017-05-24 21:28:54 +00:00
|
|
|
|
use jid::Jid;
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
use error::Error;
|
2017-04-20 22:16:12 +00:00
|
|
|
|
use ns;
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
2017-06-13 23:50:57 +00:00
|
|
|
|
generate_attribute!(Action, "action", {
|
|
|
|
|
ContentAccept => "content-accept",
|
|
|
|
|
ContentAdd => "content-add",
|
|
|
|
|
ContentModify => "content-modify",
|
|
|
|
|
ContentReject => "content-reject",
|
|
|
|
|
ContentRemove => "content-remove",
|
|
|
|
|
DescriptionInfo => "description-info",
|
|
|
|
|
SecurityInfo => "security-info",
|
|
|
|
|
SessionAccept => "session-accept",
|
|
|
|
|
SessionInfo => "session-info",
|
|
|
|
|
SessionInitiate => "session-initiate",
|
|
|
|
|
SessionTerminate => "session-terminate",
|
|
|
|
|
TransportAccept => "transport-accept",
|
|
|
|
|
TransportInfo => "transport-info",
|
|
|
|
|
TransportReject => "transport-reject",
|
|
|
|
|
TransportReplace => "transport-replace",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
generate_attribute!(Creator, "creator", {
|
|
|
|
|
Initiator => "initiator",
|
|
|
|
|
Responder => "responder",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
generate_attribute!(Senders, "senders", {
|
|
|
|
|
Both => "both",
|
|
|
|
|
Initiator => "initiator",
|
|
|
|
|
None => "none",
|
|
|
|
|
Responder => "responder",
|
2017-06-13 23:53:18 +00:00
|
|
|
|
}, Default = Both);
|
2017-05-24 21:28:54 +00:00
|
|
|
|
|
2017-07-15 10:37:29 +00:00
|
|
|
|
generate_id!(ContentId);
|
|
|
|
|
|
2017-07-29 01:47:33 +00:00
|
|
|
|
// TODO: the list of values is defined, use an enum!
|
|
|
|
|
generate_id!(Disposition);
|
|
|
|
|
|
|
|
|
|
impl Default for Disposition {
|
|
|
|
|
fn default() -> Disposition {
|
|
|
|
|
Disposition(String::from("session"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub struct Content {
|
|
|
|
|
pub creator: Creator,
|
2017-07-29 01:47:33 +00:00
|
|
|
|
pub disposition: Disposition,
|
2017-07-15 10:37:29 +00:00
|
|
|
|
pub name: ContentId,
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub senders: Senders,
|
2017-05-06 13:58:18 +00:00
|
|
|
|
pub description: Option<Element>,
|
|
|
|
|
pub transport: Option<Element>,
|
|
|
|
|
pub security: Option<Element>,
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 21:28:54 +00:00
|
|
|
|
impl TryFrom<Element> for Content {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-24 21:28:54 +00:00
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Content, Error> {
|
|
|
|
|
if !elem.is("content", ns::JINGLE) {
|
|
|
|
|
return Err(Error::ParseError("This is not a content element."));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut content = Content {
|
|
|
|
|
creator: get_attr!(elem, "creator", required),
|
2017-07-29 01:47:33 +00:00
|
|
|
|
disposition: get_attr!(elem, "disposition", default),
|
2017-05-24 21:28:54 +00:00
|
|
|
|
name: get_attr!(elem, "name", required),
|
|
|
|
|
senders: get_attr!(elem, "senders", default),
|
|
|
|
|
description: None,
|
|
|
|
|
transport: None,
|
|
|
|
|
security: None,
|
|
|
|
|
};
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.name() == "description" {
|
|
|
|
|
if content.description.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Content must not have more than one description."));
|
|
|
|
|
}
|
|
|
|
|
content.description = Some(child.clone());
|
|
|
|
|
} else if child.name() == "transport" {
|
|
|
|
|
if content.transport.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Content must not have more than one transport."));
|
|
|
|
|
}
|
|
|
|
|
content.transport = Some(child.clone());
|
|
|
|
|
} else if child.name() == "security" {
|
|
|
|
|
if content.security.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Content must not have more than one security."));
|
|
|
|
|
}
|
|
|
|
|
content.security = Some(child.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Ok(content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Content> for Element {
|
|
|
|
|
fn from(content: Content) -> Element {
|
2017-05-24 21:28:54 +00:00
|
|
|
|
Element::builder("content")
|
|
|
|
|
.ns(ns::JINGLE)
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.attr("creator", content.creator)
|
|
|
|
|
.attr("disposition", content.disposition)
|
|
|
|
|
.attr("name", content.name)
|
|
|
|
|
.attr("senders", content.senders)
|
|
|
|
|
.append(content.description)
|
|
|
|
|
.append(content.transport)
|
|
|
|
|
.append(content.security)
|
2017-05-24 21:28:54 +00:00
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub enum Reason {
|
|
|
|
|
AlternativeSession, //(String),
|
|
|
|
|
Busy,
|
|
|
|
|
Cancel,
|
|
|
|
|
ConnectivityError,
|
|
|
|
|
Decline,
|
|
|
|
|
Expired,
|
|
|
|
|
FailedApplication,
|
|
|
|
|
FailedTransport,
|
|
|
|
|
GeneralError,
|
|
|
|
|
Gone,
|
|
|
|
|
IncompatibleParameters,
|
|
|
|
|
MediaError,
|
|
|
|
|
SecurityError,
|
|
|
|
|
Success,
|
|
|
|
|
Timeout,
|
|
|
|
|
UnsupportedApplications,
|
|
|
|
|
UnsupportedTransports,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FromStr for Reason {
|
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Reason, Error> {
|
2017-04-23 02:41:26 +00:00
|
|
|
|
Ok(match s {
|
|
|
|
|
"alternative-session" => Reason::AlternativeSession,
|
|
|
|
|
"busy" => Reason::Busy,
|
|
|
|
|
"cancel" => Reason::Cancel,
|
|
|
|
|
"connectivity-error" => Reason::ConnectivityError,
|
|
|
|
|
"decline" => Reason::Decline,
|
|
|
|
|
"expired" => Reason::Expired,
|
|
|
|
|
"failed-application" => Reason::FailedApplication,
|
|
|
|
|
"failed-transport" => Reason::FailedTransport,
|
|
|
|
|
"general-error" => Reason::GeneralError,
|
|
|
|
|
"gone" => Reason::Gone,
|
|
|
|
|
"incompatible-parameters" => Reason::IncompatibleParameters,
|
|
|
|
|
"media-error" => Reason::MediaError,
|
|
|
|
|
"security-error" => Reason::SecurityError,
|
|
|
|
|
"success" => Reason::Success,
|
|
|
|
|
"timeout" => Reason::Timeout,
|
|
|
|
|
"unsupported-applications" => Reason::UnsupportedApplications,
|
|
|
|
|
"unsupported-transports" => Reason::UnsupportedTransports,
|
|
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Unknown reason.")),
|
|
|
|
|
})
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Reason> for Element {
|
|
|
|
|
fn from(reason: Reason) -> Element {
|
|
|
|
|
Element::builder(match reason {
|
2017-04-28 22:45:02 +00:00
|
|
|
|
Reason::AlternativeSession => "alternative-session",
|
|
|
|
|
Reason::Busy => "busy",
|
|
|
|
|
Reason::Cancel => "cancel",
|
|
|
|
|
Reason::ConnectivityError => "connectivity-error",
|
|
|
|
|
Reason::Decline => "decline",
|
|
|
|
|
Reason::Expired => "expired",
|
|
|
|
|
Reason::FailedApplication => "failed-application",
|
|
|
|
|
Reason::FailedTransport => "failed-transport",
|
|
|
|
|
Reason::GeneralError => "general-error",
|
|
|
|
|
Reason::Gone => "gone",
|
|
|
|
|
Reason::IncompatibleParameters => "incompatible-parameters",
|
|
|
|
|
Reason::MediaError => "media-error",
|
|
|
|
|
Reason::SecurityError => "security-error",
|
|
|
|
|
Reason::Success => "success",
|
|
|
|
|
Reason::Timeout => "timeout",
|
|
|
|
|
Reason::UnsupportedApplications => "unsupported-applications",
|
|
|
|
|
Reason::UnsupportedTransports => "unsupported-transports",
|
2017-05-04 00:20:28 +00:00
|
|
|
|
}).build()
|
2017-04-28 22:45:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub struct ReasonElement {
|
|
|
|
|
pub reason: Reason,
|
|
|
|
|
pub text: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 21:28:54 +00:00
|
|
|
|
impl TryFrom<Element> for ReasonElement {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-24 21:28:54 +00:00
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<ReasonElement, Error> {
|
|
|
|
|
if !elem.is("reason", ns::JINGLE) {
|
|
|
|
|
return Err(Error::ParseError("This is not a reason element."));
|
|
|
|
|
}
|
|
|
|
|
let mut reason = None;
|
|
|
|
|
let mut text = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.ns() != Some(ns::JINGLE) {
|
|
|
|
|
return Err(Error::ParseError("Reason contains a foreign element."));
|
|
|
|
|
}
|
|
|
|
|
match child.name() {
|
|
|
|
|
"text" => {
|
|
|
|
|
if text.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Reason must not have more than one text."));
|
|
|
|
|
}
|
|
|
|
|
text = Some(child.text());
|
|
|
|
|
},
|
|
|
|
|
name => {
|
|
|
|
|
if reason.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Reason must not have more than one reason."));
|
|
|
|
|
}
|
|
|
|
|
reason = Some(name.parse()?);
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let reason = reason.ok_or(Error::ParseError("Reason doesn’t contain a valid reason."))?;
|
|
|
|
|
Ok(ReasonElement {
|
|
|
|
|
reason: reason,
|
|
|
|
|
text: text,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<ReasonElement> for Element {
|
|
|
|
|
fn from(reason: ReasonElement) -> Element {
|
2017-05-24 21:28:54 +00:00
|
|
|
|
Element::builder("reason")
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.append(Element::from(reason.reason))
|
|
|
|
|
.append(reason.text)
|
2017-05-24 21:28:54 +00:00
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-25 21:14:51 +00:00
|
|
|
|
generate_id!(SessionId);
|
2017-06-14 01:28:42 +00:00
|
|
|
|
|
2017-04-20 23:41:15 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub struct Jingle {
|
|
|
|
|
pub action: Action,
|
|
|
|
|
pub initiator: Option<Jid>,
|
|
|
|
|
pub responder: Option<Jid>,
|
2017-06-25 21:14:51 +00:00
|
|
|
|
pub sid: SessionId,
|
2017-04-19 01:27:42 +00:00
|
|
|
|
pub contents: Vec<Content>,
|
|
|
|
|
pub reason: Option<ReasonElement>,
|
2017-04-28 22:45:27 +00:00
|
|
|
|
pub other: Vec<Element>,
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for Jingle {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-04-19 22:41:54 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(root: Element) -> Result<Jingle, Error> {
|
2017-05-04 00:20:28 +00:00
|
|
|
|
if !root.is("jingle", ns::JINGLE) {
|
|
|
|
|
return Err(Error::ParseError("This is not a Jingle element."));
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 21:28:54 +00:00
|
|
|
|
let mut jingle = Jingle {
|
|
|
|
|
action: get_attr!(root, "action", required),
|
|
|
|
|
initiator: get_attr!(root, "initiator", optional),
|
|
|
|
|
responder: get_attr!(root, "responder", optional),
|
|
|
|
|
sid: get_attr!(root, "sid", required),
|
|
|
|
|
contents: vec!(),
|
|
|
|
|
reason: None,
|
|
|
|
|
other: vec!(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for child in root.children().cloned() {
|
2017-05-04 00:20:28 +00:00
|
|
|
|
if child.is("content", ns::JINGLE) {
|
2017-05-24 22:38:44 +00:00
|
|
|
|
let content = Content::try_from(child)?;
|
2017-05-24 21:28:54 +00:00
|
|
|
|
jingle.contents.push(content);
|
2017-05-04 00:20:28 +00:00
|
|
|
|
} else if child.is("reason", ns::JINGLE) {
|
2017-05-24 21:28:54 +00:00
|
|
|
|
if jingle.reason.is_some() {
|
2017-05-04 00:20:28 +00:00
|
|
|
|
return Err(Error::ParseError("Jingle must not have more than one reason."));
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
2017-05-24 22:38:44 +00:00
|
|
|
|
let reason = ReasonElement::try_from(child)?;
|
2017-05-24 21:28:54 +00:00
|
|
|
|
jingle.reason = Some(reason);
|
2017-05-04 00:20:28 +00:00
|
|
|
|
} else {
|
2017-05-24 22:38:44 +00:00
|
|
|
|
jingle.other.push(child);
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-24 21:28:54 +00:00
|
|
|
|
Ok(jingle)
|
2017-04-24 18:25:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Jingle> for Element {
|
|
|
|
|
fn from(jingle: Jingle) -> Element {
|
2017-05-24 21:28:54 +00:00
|
|
|
|
Element::builder("jingle")
|
|
|
|
|
.ns(ns::JINGLE)
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.attr("action", jingle.action)
|
2017-07-29 05:28:20 +00:00
|
|
|
|
.attr("initiator", jingle.initiator)
|
|
|
|
|
.attr("responder", jingle.responder)
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.attr("sid", jingle.sid)
|
|
|
|
|
.append(jingle.contents)
|
|
|
|
|
.append(jingle.reason)
|
2017-05-24 21:28:54 +00:00
|
|
|
|
.build()
|
2017-04-24 18:25:00 +00:00
|
|
|
|
}
|
2017-05-04 00:20:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-19 01:27:42 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-04 00:20:28 +00:00
|
|
|
|
use super::*;
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(jingle.action, Action::SessionInitiate);
|
2017-06-25 21:14:51 +00:00
|
|
|
|
assert_eq!(jingle.sid, SessionId(String::from("coucou")));
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_jingle() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-24 21:28:54 +00:00
|
|
|
|
assert_eq!(message, "Required attribute 'action' missing.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-info'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-24 21:28:54 +00:00
|
|
|
|
assert_eq!(message, "Required attribute 'sid' missing.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='coucou' sid='coucou'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-06-13 23:50:57 +00:00
|
|
|
|
assert_eq!(message, "Unknown value for 'action' attribute.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_content() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou'><description/><transport/></content></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].creator, Creator::Initiator);
|
2017-07-15 10:37:29 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].name, ContentId(String::from("coucou")));
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].senders, Senders::Both);
|
2017-07-29 01:47:33 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].disposition, Disposition(String::from("session")));
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders='both'><description/><transport/></content></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].senders, Senders::Both);
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' disposition='early-session'><description/><transport/></content></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-07-29 01:47:33 +00:00
|
|
|
|
assert_eq!(jingle.contents[0].disposition, Disposition(String::from("early-session")));
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_content() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-24 21:28:54 +00:00
|
|
|
|
assert_eq!(message, "Required attribute 'creator' missing.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator'/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-05-24 21:28:54 +00:00
|
|
|
|
assert_eq!(message, "Required attribute 'name' missing.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='coucou' name='coucou'/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-06-13 23:50:57 +00:00
|
|
|
|
assert_eq!(message, "Unknown value for 'creator' attribute.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders='coucou'/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-06-13 23:50:57 +00:00
|
|
|
|
assert_eq!(message, "Unknown value for 'senders' attribute.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders=''/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-06-13 23:50:57 +00:00
|
|
|
|
assert_eq!(message, "Unknown value for 'senders' attribute.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_reason() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><success/></reason></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let reason = jingle.reason.unwrap();
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(reason.reason, Reason::Success);
|
2017-04-19 01:27:42 +00:00
|
|
|
|
assert_eq!(reason.text, None);
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><success/><text>coucou</text></reason></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let jingle = Jingle::try_from(elem).unwrap();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let reason = jingle.reason.unwrap();
|
2017-05-04 00:20:28 +00:00
|
|
|
|
assert_eq!(reason.reason, Reason::Success);
|
2017-04-19 01:27:42 +00:00
|
|
|
|
assert_eq!(reason.text, Some(String::from("coucou")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_reason() {
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Reason doesn’t contain a valid reason.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><a/></reason></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown reason.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><a xmlns='http://www.w3.org/1999/xhtml'/></reason></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Reason contains a foreign element.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><decline/></reason><reason/></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:27:42 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Jingle must not have more than one reason.");
|
2017-04-19 01:38:10 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><decline/><text/><text/></reason></jingle>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Jingle::try_from(elem).unwrap_err();
|
2017-04-19 01:38:10 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Reason must not have more than one text.");
|
2017-04-19 01:27:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|