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-08-20 02:27:42 +02:00
|
|
|
|
use xso::{error::Error, AsXml, FromXml};
|
|
|
|
|
|
|
2018-12-18 15:27:30 +01:00
|
|
|
|
use crate::date::DateTime;
|
2018-12-18 15:32:05 +01:00
|
|
|
|
use crate::hashes::Hash;
|
|
|
|
|
|
use crate::jingle::{ContentId, Creator};
|
2018-12-18 15:27:30 +01:00
|
|
|
|
use crate::ns;
|
2018-12-18 15:32:05 +01:00
|
|
|
|
use std::collections::BTreeMap;
|
2019-10-23 01:32:41 +02:00
|
|
|
|
use std::str::FromStr;
|
2017-04-22 17:39:21 +01:00
|
|
|
|
|
2024-08-03 14:41:33 +02:00
|
|
|
|
/// Represents a range in a file.
|
|
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone, Default)]
|
|
|
|
|
|
#[xml(namespace = ns::JINGLE_FT, name = "range")]
|
|
|
|
|
|
pub struct Range {
|
|
|
|
|
|
/// The offset in bytes from the beginning of the file.
|
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub offset: u64,
|
|
|
|
|
|
|
|
|
|
|
|
/// The length in bytes of the range, or None to be the entire
|
|
|
|
|
|
/// remaining of the file.
|
|
|
|
|
|
#[xml(attribute(default))]
|
|
|
|
|
|
pub length: Option<u64>,
|
|
|
|
|
|
|
|
|
|
|
|
/// List of hashes for this range.
|
|
|
|
|
|
#[xml(child(n = ..))]
|
|
|
|
|
|
pub hashes: Vec<Hash>,
|
|
|
|
|
|
}
|
2017-04-24 19:52:41 +01:00
|
|
|
|
|
2018-05-04 19:10:45 +02:00
|
|
|
|
impl Range {
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Creates a new range.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn new() -> Range {
|
|
|
|
|
|
Default::default()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-27 01:22:15 +01:00
|
|
|
|
type Lang = String;
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Represents a file to be transferred.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, Default)]
|
|
|
|
|
|
#[xml(namespace = ns::JINGLE_FT, name = "file")]
|
2017-04-22 17:39:21 +01:00
|
|
|
|
pub struct File {
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// The date of last modification of this file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(extract(default, fields(text(type_ = DateTime))))]
|
2017-10-31 19:41:45 +00:00
|
|
|
|
pub date: Option<DateTime>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The MIME type of this file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(extract(default, name = "media-type", fields(text(type_ = String))))]
|
2017-04-22 17:39:21 +01:00
|
|
|
|
pub media_type: Option<String>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The name of this file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(extract(default, fields(text(type_ = String))))]
|
2017-04-22 17:39:21 +01:00
|
|
|
|
pub name: Option<String>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The description of this file, possibly localised.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(extract(n = .., name = "desc", fields(
|
|
|
|
|
|
attribute(name = "xml:lang", type_ = String),
|
|
|
|
|
|
text(type_ = String)
|
|
|
|
|
|
)))]
|
|
|
|
|
|
pub descs: BTreeMap<Lang, String>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The size of this file, in bytes.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(extract(default, fields(text(type_ = u64))))]
|
2017-04-24 19:52:41 +01:00
|
|
|
|
pub size: Option<u64>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// Used to request only a part of this file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(child(default))]
|
2017-04-22 17:39:21 +01:00
|
|
|
|
pub range: Option<Range>,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// A list of hashes matching this entire file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(child(n = ..))]
|
2017-04-22 17:39:21 +01:00
|
|
|
|
pub hashes: Vec<Hash>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-05-04 19:10:45 +02:00
|
|
|
|
impl File {
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Creates a new file descriptor.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn new() -> File {
|
2019-02-21 21:00:58 +01:00
|
|
|
|
File::default()
|
2018-05-04 19:10:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets the date of last modification on this file.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_date(mut self, date: DateTime) -> File {
|
|
|
|
|
|
self.date = Some(date);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets the date of last modification on this file from an ISO-8601
|
|
|
|
|
|
/// string.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_date_str(mut self, date: &str) -> Result<File, Error> {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
self.date = Some(DateTime::from_str(date).map_err(Error::text_parse_error)?);
|
2018-05-04 19:10:45 +02:00
|
|
|
|
Ok(self)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets the MIME type of this file.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_media_type(mut self, media_type: String) -> File {
|
|
|
|
|
|
self.media_type = Some(media_type);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets the name of this file.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_name(mut self, name: String) -> File {
|
|
|
|
|
|
self.name = Some(name);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets a description for this file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
pub fn add_desc(mut self, lang: &str, desc: String) -> File {
|
2018-05-04 19:10:45 +02:00
|
|
|
|
self.descs.insert(Lang::from(lang), desc);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Sets the file size of this file, in bytes.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_size(mut self, size: u64) -> File {
|
|
|
|
|
|
self.size = Some(size);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Request only a range of this file.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn with_range(mut self, range: Range) -> File {
|
|
|
|
|
|
self.range = Some(range);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// Add a hash on this file.
|
2018-05-04 19:10:45 +02:00
|
|
|
|
pub fn add_hash(mut self, hash: Hash) -> File {
|
|
|
|
|
|
self.hashes.push(hash);
|
|
|
|
|
|
self
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// A wrapper element for a file.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone, Default)]
|
|
|
|
|
|
#[xml(namespace = ns::JINGLE_FT, name = "description")]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub struct Description {
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// The actual file descriptor.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(child)]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub file: File,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// A checksum for checking that the file has been transferred correctly.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, Debug, Clone)]
|
|
|
|
|
|
#[xml(namespace = ns::JINGLE_FT, name = "checksum")]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub struct Checksum {
|
2018-08-08 20:52:27 +02:00
|
|
|
|
/// The identifier of the file transfer content.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(attribute)]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub name: ContentId,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The creator of this file transfer.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(attribute)]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub creator: Creator,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
|
|
|
|
|
/// The file being checksummed.
|
2024-08-20 02:27:42 +02:00
|
|
|
|
#[xml(child)]
|
2017-10-31 16:25:01 +00:00
|
|
|
|
pub file: File,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-06-26 13:13:02 +02:00
|
|
|
|
/// A notice that the file transfer has been completed.
|
2024-07-09 17:01:42 +02:00
|
|
|
|
#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
|
2024-06-26 13:13:02 +02:00
|
|
|
|
#[xml(namespace = ns::JINGLE_FT, name = "received")]
|
|
|
|
|
|
pub struct Received {
|
|
|
|
|
|
/// The content identifier of this Jingle session.
|
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
|
pub name: ContentId,
|
2018-08-08 20:52:27 +02:00
|
|
|
|
|
2024-06-26 13:13:02 +02:00
|
|
|
|
/// The creator of this file transfer.
|
|
|
|
|
|
#[xml(attribute)]
|
|
|
|
|
|
pub creator: Creator,
|
|
|
|
|
|
}
|
2017-10-31 16:25:01 +00:00
|
|
|
|
|
2017-04-22 17:39:21 +01:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
2017-05-06 20:30:52 +01:00
|
|
|
|
use super::*;
|
2018-12-18 15:27:30 +01:00
|
|
|
|
use crate::hashes::Algo;
|
2023-04-03 11:21:03 +02:00
|
|
|
|
use base64::{engine::general_purpose::STANDARD as Base64, Engine};
|
2024-08-20 02:27:42 +02:00
|
|
|
|
use minidom::Element;
|
|
|
|
|
|
use xso::error::FromElementError;
|
2017-04-22 17:39:21 +01:00
|
|
|
|
|
2022-04-24 13:26:20 +02:00
|
|
|
|
// Apparently, i686 and AArch32/PowerPC seem to disagree here. So instead
|
|
|
|
|
|
// of trying to figure this out now, we just ignore the test.
|
2018-10-28 13:10:48 +01:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
|
#[test]
|
2022-04-24 13:26:20 +02:00
|
|
|
|
#[ignore]
|
2018-10-28 13:10:48 +01:00
|
|
|
|
fn test_size() {
|
2022-04-23 15:28:49 +02:00
|
|
|
|
assert_size!(Range, 32);
|
2024-03-08 15:58:21 +01:00
|
|
|
|
assert_size!(File, 104);
|
|
|
|
|
|
assert_size!(Description, 104);
|
2022-04-23 15:28:49 +02:00
|
|
|
|
assert_size!(Checksum, 128);
|
2018-10-28 13:10:48 +01:00
|
|
|
|
assert_size!(Received, 16);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 14:26:16 +02:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_size() {
|
|
|
|
|
|
assert_size!(Range, 48);
|
2024-03-08 15:58:21 +01:00
|
|
|
|
assert_size!(File, 176);
|
|
|
|
|
|
assert_size!(Description, 176);
|
|
|
|
|
|
assert_size!(Checksum, 208);
|
2018-10-26 14:26:16 +02:00
|
|
|
|
assert_size!(Received, 32);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-22 17:39:21 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_description() {
|
2022-03-22 23:29:25 +01:00
|
|
|
|
let elem: Element = r#"<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
2017-04-22 17:39:21 +01:00
|
|
|
|
<file>
|
|
|
|
|
|
<media-type>text/plain</media-type>
|
|
|
|
|
|
<name>test.txt</name>
|
2017-06-16 20:37:48 +01:00
|
|
|
|
<date>2015-07-26T21:46:00+01:00</date>
|
2017-04-22 17:39:21 +01:00
|
|
|
|
<size>6144</size>
|
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
|
</file>
|
|
|
|
|
|
</description>
|
2018-12-18 15:32:05 +01:00
|
|
|
|
"#
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2017-04-22 17:39:21 +01: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 01:22:15 +01:00
|
|
|
|
assert_eq!(desc.file.descs, BTreeMap::new());
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
desc.file.date,
|
|
|
|
|
|
DateTime::from_str("2015-07-26T21:46:00+01:00").ok()
|
|
|
|
|
|
);
|
2017-04-24 19:52:41 +01:00
|
|
|
|
assert_eq!(desc.file.size, Some(6144u64));
|
2017-04-22 17:39:21 +01:00
|
|
|
|
assert_eq!(desc.file.range, None);
|
2017-05-18 23:09:29 +01:00
|
|
|
|
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
desc.file.hashes[0].hash,
|
2023-04-03 11:21:03 +02:00
|
|
|
|
Base64.decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
|
2018-12-18 15:32:05 +01:00
|
|
|
|
);
|
2017-04-22 17:39:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_request() {
|
2022-03-22 23:29:25 +01:00
|
|
|
|
let elem: Element = r#"<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
2017-04-22 17:39:21 +01:00
|
|
|
|
<file>
|
|
|
|
|
|
<hash xmlns='urn:xmpp:hashes:2'
|
|
|
|
|
|
algo='sha-1'>w0mcJylzCn+AfvuGdqkty2+KP48=</hash>
|
|
|
|
|
|
</file>
|
|
|
|
|
|
</description>
|
2018-12-18 15:32:05 +01:00
|
|
|
|
"#
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-05-23 23:31:33 +01:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2017-04-22 17:39:21 +01:00
|
|
|
|
assert_eq!(desc.file.media_type, None);
|
|
|
|
|
|
assert_eq!(desc.file.name, None);
|
2017-08-27 01:22:15 +01:00
|
|
|
|
assert_eq!(desc.file.descs, BTreeMap::new());
|
2017-04-22 17:39:21 +01:00
|
|
|
|
assert_eq!(desc.file.date, None);
|
|
|
|
|
|
assert_eq!(desc.file.size, None);
|
|
|
|
|
|
assert_eq!(desc.file.range, None);
|
2017-05-18 23:09:29 +01:00
|
|
|
|
assert_eq!(desc.file.hashes[0].algo, Algo::Sha_1);
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
desc.file.hashes[0].hash,
|
2023-04-03 11:21:03 +02:00
|
|
|
|
Base64.decode("w0mcJylzCn+AfvuGdqkty2+KP48=").unwrap()
|
2018-12-18 15:32:05 +01:00
|
|
|
|
);
|
2017-04-22 17:39:21 +01:00
|
|
|
|
}
|
2017-08-27 01:22:15 +01:00
|
|
|
|
|
|
|
|
|
|
#[test]
|
2024-08-20 02:27:42 +02:00
|
|
|
|
// TODO: Reenable that test once we correctly treat same @xml:lang as errors!
|
|
|
|
|
|
#[ignore]
|
2017-08-27 01:22:15 +01:00
|
|
|
|
fn test_descs() {
|
2022-03-22 23:29:25 +01:00
|
|
|
|
let elem: Element = r#"<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
2017-08-27 01:22:15 +01:00
|
|
|
|
<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 15:32:05 +01:00
|
|
|
|
"#
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-08-27 01:22:15 +01:00
|
|
|
|
let desc = Description::try_from(elem).unwrap();
|
2018-12-18 15:32:05 +01:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
desc.file.descs.keys().cloned().collect::<Vec<_>>(),
|
|
|
|
|
|
["en", "fr"]
|
|
|
|
|
|
);
|
2024-08-20 02:27:42 +02:00
|
|
|
|
assert_eq!(desc.file.descs["en"], String::from("Secret file!"));
|
|
|
|
|
|
assert_eq!(desc.file.descs["fr"], String::from("Fichier secret !"));
|
2017-08-27 01:22:15 +01:00
|
|
|
|
|
2022-03-22 23:29:25 +01:00
|
|
|
|
let elem: Element = r#"<description xmlns='urn:xmpp:jingle:apps:file-transfer:5'>
|
2017-08-27 01:22:15 +01:00
|
|
|
|
<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 15:32:05 +01:00
|
|
|
|
"#
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2017-08-27 01:22:15 +01:00
|
|
|
|
let error = Description::try_from(elem).unwrap_err();
|
|
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-08-27 01:22:15 +01:00
|
|
|
|
_ => 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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 16:09:28 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-26 13:13:02 +02:00
|
|
|
|
assert_eq!(message, "Unknown child in Received element.");
|
2017-10-31 16:09:28 +00:00
|
|
|
|
|
2018-12-18 15:32:05 +01: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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 16:09:28 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-26 13:13:02 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'name' on Received element missing."
|
|
|
|
|
|
);
|
2017-10-31 16:09:28 +00:00
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string)) => string,
|
2017-10-31 16:09:28 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-21 16:27:43 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message.to_string(),
|
|
|
|
|
|
"Unknown value for 'creator' attribute."
|
|
|
|
|
|
);
|
2017-10-31 16:09:28 +00:00
|
|
|
|
}
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 20:41:40 +01: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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-01-12 20:41:40 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-26 13:13:02 +02:00
|
|
|
|
assert_eq!(message, "Unknown attribute in Received element.");
|
2019-01-12 20:41:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
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 15:32:05 +01: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 15:32:05 +01: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 15:32:05 +01: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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2024-03-02 09:19:49 +01:00
|
|
|
|
other => panic!("unexpected error: {:?}", other),
|
2017-10-31 16:41:22 +00:00
|
|
|
|
};
|
2024-08-20 02:27:42 +02:00
|
|
|
|
assert_eq!(message, "Unknown child in Checksum element.");
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2017-10-31 16:41:22 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-20 02:27:42 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message,
|
|
|
|
|
|
"Required attribute field 'name' on Checksum element missing."
|
|
|
|
|
|
);
|
2017-10-31 16:41:22 +00:00
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::TextParseError(string)) => string,
|
2017-10-31 16:41:22 +00:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-06-21 16:27:43 +02:00
|
|
|
|
assert_eq!(
|
|
|
|
|
|
message.to_string(),
|
|
|
|
|
|
"Unknown value for 'creator' attribute."
|
|
|
|
|
|
);
|
2017-10-31 16:41:22 +00:00
|
|
|
|
}
|
2017-10-31 17:02:24 +00:00
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 20:41:40 +01: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 {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-01-12 20:41:40 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-20 02:27:42 +02:00
|
|
|
|
assert_eq!(message, "Unknown attribute in Checksum element.");
|
2019-01-12 20:41:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-31 17:02:24 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_range() {
|
2018-12-18 15:32:05 +01: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 15:32:05 +01: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 20:41:40 +01:00
|
|
|
|
}
|
2017-10-31 17:02:24 +00:00
|
|
|
|
|
2019-01-12 22:00:46 +01:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-12 20:41:40 +01:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_invalid_range() {
|
2018-12-18 15:32:05 +01:00
|
|
|
|
let elem: Element = "<range xmlns='urn:xmpp:jingle:apps:file-transfer:5' coucou=''/>"
|
|
|
|
|
|
.parse()
|
|
|
|
|
|
.unwrap();
|
2019-01-12 20:41:40 +01:00
|
|
|
|
let error = Range::try_from(elem).unwrap_err();
|
|
|
|
|
|
let message = match error {
|
2024-06-21 16:27:43 +02:00
|
|
|
|
FromElementError::Invalid(Error::Other(string)) => string,
|
2019-01-12 20:41:40 +01:00
|
|
|
|
_ => panic!(),
|
|
|
|
|
|
};
|
2024-08-03 14:41:33 +02:00
|
|
|
|
assert_eq!(message, "Unknown attribute in Range element.");
|
2017-10-31 17:02:24 +00:00
|
|
|
|
}
|
2017-04-22 17:39:21 +01:00
|
|
|
|
}
|