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/.
|
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::date::DateTime;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use crate::hashes::Hash;
|
|
|
|
|
use crate::jingle::{ContentId, Creator};
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::ns;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use crate::util::error::Error;
|
2019-07-24 22:20:38 +00:00
|
|
|
|
use minidom::{Element, Node};
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::collections::BTreeMap;
|
2019-04-12 08:58:42 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-04-22 16:39:21 +00:00
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Represents a range in a file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
#[derive(PartialEq, Default)]
|
2018-05-14 14:30:28 +00:00
|
|
|
|
Range, "range", JINGLE_FT,
|
2017-11-23 16:33:08 +00:00
|
|
|
|
attributes: [
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// The offset in bytes from the beginning of the file.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
offset: Default<u64> = "offset",
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The length in bytes of the range, or None to be the entire
|
|
|
|
|
/// remaining of the file.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
length: Option<u64> = "length"
|
2017-11-23 16:33:08 +00:00
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// List of hashes for this range.
|
2018-05-14 14:30:28 +00:00
|
|
|
|
hashes: Vec<Hash> = ("hash", HASHES) => Hash
|
2017-11-23 16:33:08 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-24 18:52:41 +00:00
|
|
|
|
|
2018-05-04 17:10:45 +00:00
|
|
|
|
impl Range {
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Creates a new range.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn new() -> Range {
|
|
|
|
|
Default::default()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-27 00:22:15 +00:00
|
|
|
|
type Lang = String;
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
generate_id!(
|
|
|
|
|
/// Wrapper for a file description.
|
|
|
|
|
Desc
|
|
|
|
|
);
|
2017-08-27 00:22:15 +00:00
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Represents a file to be transferred.
|
2019-02-21 20:00:58 +00:00
|
|
|
|
#[derive(Debug, Clone, Default)]
|
2017-04-22 16:39:21 +00:00
|
|
|
|
pub struct File {
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// The date of last modification of this file.
|
2017-10-31 19:41:45 +00:00
|
|
|
|
pub date: Option<DateTime>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The MIME type of this file.
|
2017-04-22 16:39:21 +00:00
|
|
|
|
pub media_type: Option<String>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The name of this file.
|
2017-04-22 16:39:21 +00:00
|
|
|
|
pub name: Option<String>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The description of this file, possibly localised.
|
2017-08-27 00:22:15 +00:00
|
|
|
|
pub descs: BTreeMap<Lang, Desc>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The size of this file, in bytes.
|
2017-04-24 18:52:41 +00:00
|
|
|
|
pub size: Option<u64>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// Used to request only a part of this file.
|
2017-04-22 16:39:21 +00:00
|
|
|
|
pub range: Option<Range>,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// A list of hashes matching this entire file.
|
2017-04-22 16:39:21 +00:00
|
|
|
|
pub hashes: Vec<Hash>,
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-04 17:10:45 +00:00
|
|
|
|
impl File {
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Creates a new file descriptor.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn new() -> File {
|
2019-02-21 20:00:58 +00:00
|
|
|
|
File::default()
|
2018-05-04 17:10:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets the date of last modification on this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_date(mut self, date: DateTime) -> File {
|
|
|
|
|
self.date = Some(date);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets the date of last modification on this file from an ISO-8601
|
|
|
|
|
/// string.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_date_str(mut self, date: &str) -> Result<File, Error> {
|
|
|
|
|
self.date = Some(DateTime::from_str(date)?);
|
|
|
|
|
Ok(self)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets the MIME type of this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_media_type(mut self, media_type: String) -> File {
|
|
|
|
|
self.media_type = Some(media_type);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets the name of this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_name(mut self, name: String) -> File {
|
|
|
|
|
self.name = Some(name);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets a description for this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn add_desc(mut self, lang: &str, desc: Desc) -> File {
|
|
|
|
|
self.descs.insert(Lang::from(lang), desc);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Sets the file size of this file, in bytes.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_size(mut self, size: u64) -> File {
|
|
|
|
|
self.size = Some(size);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Request only a range of this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn with_range(mut self, range: Range) -> File {
|
|
|
|
|
self.range = Some(range);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// Add a hash on this file.
|
2018-05-04 17:10:45 +00:00
|
|
|
|
pub fn add_hash(mut self, hash: Hash) -> File {
|
|
|
|
|
self.hashes.push(hash);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 16:25:01 +00:00
|
|
|
|
impl TryFrom<Element> for File {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = Error;
|
2017-10-31 16:09:28 +00:00
|
|
|
|
|
2017-10-31 16:25:01 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<File, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "file", JINGLE_FT);
|
2017-10-31 16:25:01 +00:00
|
|
|
|
check_no_attributes!(elem, "file");
|
2017-04-28 22:43:47 +00:00
|
|
|
|
|
2017-10-31 16:25:01 +00:00
|
|
|
|
let mut file = File {
|
|
|
|
|
date: None,
|
|
|
|
|
media_type: None,
|
|
|
|
|
name: None,
|
|
|
|
|
descs: BTreeMap::new(),
|
|
|
|
|
size: None,
|
|
|
|
|
range: None,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
hashes: vec![],
|
2017-10-31 16:25:01 +00:00
|
|
|
|
};
|
2017-04-22 16:39:21 +00:00
|
|
|
|
|
2017-10-31 16:25:01 +00:00
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("date", ns::JINGLE_FT) {
|
|
|
|
|
if file.date.is_some() {
|
|
|
|
|
return Err(Error::ParseError("File must not have more than one date."));
|
|
|
|
|
}
|
|
|
|
|
file.date = Some(child.text().parse()?);
|
|
|
|
|
} else if child.is("media-type", ns::JINGLE_FT) {
|
|
|
|
|
if file.media_type.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"File must not have more than one media-type.",
|
|
|
|
|
));
|
2017-10-31 16:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
file.media_type = Some(child.text());
|
|
|
|
|
} else if child.is("name", ns::JINGLE_FT) {
|
|
|
|
|
if file.name.is_some() {
|
|
|
|
|
return Err(Error::ParseError("File must not have more than one name."));
|
|
|
|
|
}
|
|
|
|
|
file.name = Some(child.text());
|
|
|
|
|
} else if child.is("desc", ns::JINGLE_FT) {
|
2019-02-24 19:48:19 +00:00
|
|
|
|
let lang = get_attr!(child, "xml:lang", Default);
|
2017-10-31 16:25:01 +00:00
|
|
|
|
let desc = Desc(child.text());
|
|
|
|
|
if file.descs.insert(lang, desc).is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Desc element present twice for the same xml:lang.",
|
|
|
|
|
));
|
2017-10-31 16:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
} else if child.is("size", ns::JINGLE_FT) {
|
|
|
|
|
if file.size.is_some() {
|
|
|
|
|
return Err(Error::ParseError("File must not have more than one size."));
|
|
|
|
|
}
|
|
|
|
|
file.size = Some(child.text().parse()?);
|
|
|
|
|
} else if child.is("range", ns::JINGLE_FT) {
|
|
|
|
|
if file.range.is_some() {
|
|
|
|
|
return Err(Error::ParseError("File must not have more than one range."));
|
|
|
|
|
}
|
2017-10-31 17:02:24 +00:00
|
|
|
|
file.range = Some(Range::try_from(child.clone())?);
|
2017-10-31 16:25:01 +00:00
|
|
|
|
} else if child.is("hash", ns::HASHES) {
|
|
|
|
|
file.hashes.push(Hash::try_from(child.clone())?);
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Unknown element in JingleFT file."));
|
2017-04-22 16:39:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 16:25:01 +00:00
|
|
|
|
Ok(file)
|
2017-05-06 19:30:52 +00:00
|
|
|
|
}
|
2017-04-22 16:39:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<File> for Element {
|
|
|
|
|
fn from(file: File) -> Element {
|
2019-09-06 14:03:58 +00:00
|
|
|
|
Element::builder("file")
|
|
|
|
|
.ns(ns::JINGLE_FT)
|
2019-10-22 23:32:41 +00:00
|
|
|
|
.append_all(file.date.map(|date| Element::builder("date").append(date)))
|
|
|
|
|
.append_all(
|
|
|
|
|
file.media_type
|
|
|
|
|
.map(|media_type| Element::builder("media-type").append(media_type)),
|
|
|
|
|
)
|
|
|
|
|
.append_all(file.name.map(|name| Element::builder("name").append(name)))
|
|
|
|
|
.append_all(file.descs.into_iter().map(|(lang, desc)| {
|
2019-09-06 14:03:58 +00:00
|
|
|
|
Element::builder("desc")
|
|
|
|
|
.attr("xml:lang", lang)
|
2019-10-22 23:32:41 +00:00
|
|
|
|
.append(desc.0)
|
|
|
|
|
}))
|
|
|
|
|
.append_all(
|
|
|
|
|
file.size
|
|
|
|
|
.map(|size| Element::builder("size").append(format!("{}", size))),
|
|
|
|
|
)
|
2019-09-06 14:03:58 +00:00
|
|
|
|
.append_all(file.range)
|
|
|
|
|
.append_all(file.hashes)
|
|
|
|
|
.build()
|
2017-04-24 18:25:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// A wrapper element for a file.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Description {
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// The actual file descriptor.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub file: File,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for Description {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = Error;
|
2017-10-31 16:25:01 +00:00
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Description, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "description", JINGLE_FT, "JingleFT description");
|
2017-10-31 16:25:01 +00:00
|
|
|
|
check_no_attributes!(elem, "JingleFT description");
|
|
|
|
|
let mut file = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if file.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"JingleFT description element must have exactly one child.",
|
|
|
|
|
));
|
2017-10-31 16:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
file = Some(File::try_from(child.clone())?);
|
|
|
|
|
}
|
|
|
|
|
if file.is_none() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"JingleFT description element must have exactly one child.",
|
|
|
|
|
));
|
2017-10-31 16:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
Ok(Description {
|
|
|
|
|
file: file.unwrap(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-24 18:25:00 +00:00
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Description> for Element {
|
|
|
|
|
fn from(description: Description) -> Element {
|
2017-05-06 19:30:52 +00:00
|
|
|
|
Element::builder("description")
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.ns(ns::JINGLE_FT)
|
2019-07-24 22:20:38 +00:00
|
|
|
|
.append(Node::Element(description.file.into()))
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.build()
|
2017-05-06 19:30:52 +00:00
|
|
|
|
}
|
2017-04-24 18:25:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// A checksum for checking that the file has been transferred correctly.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Checksum {
|
2018-08-08 18:52:27 +00:00
|
|
|
|
/// The identifier of the file transfer content.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub name: ContentId,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The creator of this file transfer.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub creator: Creator,
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The file being checksummed.
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub file: File,
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 16:41:22 +00:00
|
|
|
|
impl TryFrom<Element> for Checksum {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = Error;
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Checksum, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "checksum", JINGLE_FT);
|
2017-10-31 16:41:22 +00:00
|
|
|
|
check_no_unknown_attributes!(elem, "checksum", ["name", "creator"]);
|
|
|
|
|
let mut file = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if file.is_some() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"JingleFT checksum element must have exactly one child.",
|
|
|
|
|
));
|
2017-10-31 16:41:22 +00:00
|
|
|
|
}
|
|
|
|
|
file = Some(File::try_from(child.clone())?);
|
|
|
|
|
}
|
|
|
|
|
if file.is_none() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"JingleFT checksum element must have exactly one child.",
|
|
|
|
|
));
|
2017-10-31 16:41:22 +00:00
|
|
|
|
}
|
|
|
|
|
Ok(Checksum {
|
2019-02-24 19:48:19 +00:00
|
|
|
|
name: get_attr!(elem, "name", Required),
|
|
|
|
|
creator: get_attr!(elem, "creator", Required),
|
2017-10-31 16:41:22 +00:00
|
|
|
|
file: file.unwrap(),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Checksum> for Element {
|
|
|
|
|
fn from(checksum: Checksum) -> Element {
|
|
|
|
|
Element::builder("checksum")
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.ns(ns::JINGLE_FT)
|
|
|
|
|
.attr("name", checksum.name)
|
|
|
|
|
.attr("creator", checksum.creator)
|
2019-07-24 22:20:38 +00:00
|
|
|
|
.append(Node::Element(checksum.file.into()))
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.build()
|
2017-10-31 16:41:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-08 18:52:27 +00:00
|
|
|
|
generate_element!(
|
|
|
|
|
/// A notice that the file transfer has been completed.
|
|
|
|
|
Received, "received", JINGLE_FT,
|
|
|
|
|
attributes: [
|
|
|
|
|
/// The content identifier of this Jingle session.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
name: Required<ContentId> = "name",
|
2018-08-08 18:52:27 +00:00
|
|
|
|
|
|
|
|
|
/// The creator of this file transfer.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
creator: Required<Creator> = "creator",
|
2018-08-08 18:52:27 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-10-31 16:25:01 +00:00
|
|
|
|
|
2017-04-22 16:39:21 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 19:30:52 +00:00
|
|
|
|
use super::*;
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::hashes::Algo;
|
2017-04-22 16:39:21 +00:00
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Range, 40);
|
|
|
|
|
assert_size!(File, 128);
|
|
|
|
|
assert_size!(Description, 128);
|
|
|
|
|
assert_size!(Checksum, 144);
|
|
|
|
|
assert_size!(Received, 16);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Range, 48);
|
|
|
|
|
assert_size!(File, 184);
|
|
|
|
|
assert_size!(Description, 184);
|
|
|
|
|
assert_size!(Checksum, 216);
|
|
|
|
|
assert_size!(Received, 32);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-22 16:39:21 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_description() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
|
|
|
|
<file>
|
|
|
|
|
<media-type>text/plain</media-type>
|
|
|
|
|
<name>test.txt</name>
|
2017-06-16 19:37:48 +00:00
|
|
|
|
<date>2015-07-26T21:46:00+01:00</date>
|
2017-04-22 16:39:21 +00:00
|
|
|
|
<size>6144</size>
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
</file>
|
|
|
|
|
</description>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2017-04-22 16:39:21 +00:00
|
|
|
|
assert_eq!(desc.file.media_type, Some(String::from("text/plain")));
|
|
|
|
|
assert_eq!(desc.file.name, Some(String::from("test.txt")));
|
2017-08-27 00:22:15 +00:00
|
|
|
|
assert_eq!(desc.file.descs, BTreeMap::new());
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
desc.file.date,
|
|
|
|
|
DateTime::from_str("2015-07-26T21:46:00+01:00").ok()
|
|
|
|
|
);
|
2017-04-24 18:52:41 +00:00
|
|
|
|
assert_eq!(desc.file.size, Some(6144u64));
|
2017-04-22 16:39:21 +00:00
|
|
|
|
assert_eq!(desc.file.range, None);
|
2017-05-18 22:09:29 +00:00
|
|
|
|
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
desc.file.hashes[0].hash,
|
|
|
|
|
base64::decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
|
|
|
|
|
);
|
2017-04-22 16:39:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_request() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
|
|
|
|
<file>
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
</file>
|
|
|
|
|
</description>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2017-04-22 16:39:21 +00:00
|
|
|
|
assert_eq!(desc.file.media_type, None);
|
|
|
|
|
assert_eq!(desc.file.name, None);
|
2017-08-27 00:22:15 +00:00
|
|
|
|
assert_eq!(desc.file.descs, BTreeMap::new());
|
2017-04-22 16:39:21 +00:00
|
|
|
|
assert_eq!(desc.file.date, None);
|
|
|
|
|
assert_eq!(desc.file.size, None);
|
|
|
|
|
assert_eq!(desc.file.range, None);
|
2017-05-18 22:09:29 +00:00
|
|
|
|
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
desc.file.hashes[0].hash,
|
|
|
|
|
base64::decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
|
|
|
|
|
);
|
2017-04-22 16:39:21 +00:00
|
|
|
|
}
|
2017-08-27 00:22:15 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_descs() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
|
|
|
|
<file>
|
|
|
|
|
<media-type>text/plain</media-type>
|
|
|
|
|
<desc xml:lang='fr'>Fichier secret !</desc>
|
|
|
|
|
<desc xml:lang='en'>Secret file!</desc>
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
</file>
|
|
|
|
|
</description>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-08-27 00:22:15 +00:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
desc.file.descs.keys().cloned().collect::<Vec<_>>(),
|
|
|
|
|
["en", "fr"]
|
|
|
|
|
);
|
2017-08-27 00:22:15 +00:00
|
|
|
|
assert_eq!(desc.file.descs["en"], Desc(String::from("Secret file!")));
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
desc.file.descs["fr"],
|
|
|
|
|
Desc(String::from("Fichier secret !"))
|
|
|
|
|
);
|
2017-08-27 00:22:15 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
|
|
|
|
<file>
|
|
|
|
|
<media-type>text/plain</media-type>
|
|
|
|
|
<desc xml:lang='fr'>Fichier secret !</desc>
|
|
|
|
|
<desc xml:lang='fr'>Secret file!</desc>
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
</file>
|
|
|
|
|
</description>
|
2018-12-18 14:32:05 +00:00
|
|
|
|
"#
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-08-27 00:22:15 +00:00
|
|
|
|
let error = Description::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Desc element present twice for the same xml:lang.");
|
|
|
|
|
}
|
2017-10-31 16:09:28 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_received() {
|
|
|
|
|
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'/>".parse().unwrap();
|
|
|
|
|
let received = Received::try_from(elem).unwrap();
|
2017-10-31 16:11:09 +00:00
|
|
|
|
assert_eq!(received.name, ContentId(String::from("coucou")));
|
2017-10-31 16:09:28 +00:00
|
|
|
|
assert_eq!(received.creator, Creator::Initiator);
|
|
|
|
|
let elem2 = Element::from(received.clone());
|
|
|
|
|
let received2 = Received::try_from(elem2).unwrap();
|
2017-10-31 16:11:09 +00:00
|
|
|
|
assert_eq!(received2.name, ContentId(String::from("coucou")));
|
2017-10-31 16:09:28 +00:00
|
|
|
|
assert_eq!(received2.creator, Creator::Initiator);
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'><coucou/></received>".parse().unwrap();
|
|
|
|
|
let error = Received::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown child in received element.");
|
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element =
|
|
|
|
|
"<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' creator='initiator'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-10-31 16:09:28 +00:00
|
|
|
|
let error = Received::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Required attribute 'name' missing.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='coucou'/>".parse().unwrap();
|
|
|
|
|
let error = Received::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown value for 'creator' attribute.");
|
|
|
|
|
}
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 19:41:40 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_received() {
|
|
|
|
|
let elem: Element = "<received xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''/>".parse().unwrap();
|
|
|
|
|
let error = Received::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown attribute in received element.");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 16:41:22 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_checksum() {
|
|
|
|
|
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let hash = vec![
|
|
|
|
|
195, 73, 156, 39, 41, 115, 10, 127, 128, 126, 251, 134, 118, 169, 45, 203, 111, 138,
|
|
|
|
|
63, 143,
|
|
|
|
|
];
|
2017-10-31 16:41:22 +00:00
|
|
|
|
let checksum = Checksum::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(checksum.name, ContentId(String::from("coucou")));
|
|
|
|
|
assert_eq!(checksum.creator, Creator::Initiator);
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
checksum.file.hashes,
|
|
|
|
|
vec!(Hash {
|
|
|
|
|
algo: Algo::Sha_1,
|
|
|
|
|
hash: hash.clone()
|
|
|
|
|
})
|
|
|
|
|
);
|
2017-10-31 16:41:22 +00:00
|
|
|
|
let elem2 = Element::from(checksum);
|
|
|
|
|
let checksum2 = Checksum::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(checksum2.name, ContentId(String::from("coucou")));
|
|
|
|
|
assert_eq!(checksum2.creator, Creator::Initiator);
|
2018-12-18 14:32:05 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
checksum2.file.hashes,
|
|
|
|
|
vec!(Hash {
|
|
|
|
|
algo: Algo::Sha_1,
|
|
|
|
|
hash: hash.clone()
|
|
|
|
|
})
|
|
|
|
|
);
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator'><coucou/></checksum>".parse().unwrap();
|
|
|
|
|
let error = Checksum::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "This is not a file element.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' creator='initiator'><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
|
|
|
|
|
let error = Checksum::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Required attribute 'name' missing.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='coucou'><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
|
|
|
|
|
let error = Checksum::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown value for 'creator' attribute.");
|
|
|
|
|
}
|
2017-10-31 17:02:24 +00:00
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 19:41:40 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_checksum() {
|
|
|
|
|
let elem: Element = "<checksum xmlns='urn:xmpp:jingle:apps:file-transfer:5' name='coucou' creator='initiator' coucou=''><file><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash></file></checksum>".parse().unwrap();
|
|
|
|
|
let error = Checksum::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown attribute in checksum element.");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 17:02:24 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_range() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<range xmlns='urn:xmpp:jingle:apps:file-transfer:5'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-10-31 17:02:24 +00:00
|
|
|
|
let range = Range::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(range.offset, 0);
|
|
|
|
|
assert_eq!(range.length, None);
|
|
|
|
|
assert_eq!(range.hashes, vec!());
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<range xmlns='urn:xmpp:jingle:apps:file-transfer:5' offset='2048' length='1024'><hash xmlns='urn:xmpp:hashes:2' algo='sha-1'>kHp5RSzW/h7Gm1etSf90Mr5PC/k=</hash></range>".parse().unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let hashes = vec![Hash {
|
|
|
|
|
algo: Algo::Sha_1,
|
|
|
|
|
hash: vec![
|
|
|
|
|
144, 122, 121, 69, 44, 214, 254, 30, 198, 155, 87, 173, 73, 255, 116, 50, 190, 79,
|
|
|
|
|
11, 249,
|
|
|
|
|
],
|
|
|
|
|
}];
|
2017-10-31 17:02:24 +00:00
|
|
|
|
let range = Range::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(range.offset, 2048);
|
|
|
|
|
assert_eq!(range.length, Some(1024));
|
|
|
|
|
assert_eq!(range.hashes, hashes);
|
|
|
|
|
let elem2 = Element::from(range);
|
|
|
|
|
let range2 = Range::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(range2.offset, 2048);
|
|
|
|
|
assert_eq!(range2.length, Some(1024));
|
|
|
|
|
assert_eq!(range2.hashes, hashes);
|
2019-01-12 19:41:40 +00:00
|
|
|
|
}
|
2017-10-31 17:02:24 +00:00
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 19:41:40 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_range() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<range xmlns='urn:xmpp:jingle:apps:file-transfer:5' coucou=''/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2019-01-12 19:41:40 +00:00
|
|
|
|
let error = Range::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown attribute in range element.");
|
2017-10-31 17:02:24 +00:00
|
|
|
|
}
|
2017-04-22 16:39:21 +00:00
|
|
|
|
}
|