2019-01-25 01:57:37 +00:00
|
|
|
|
// Copyright (c) 2019 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 crate::hashes::Sha1HexAttribute;
|
2019-01-27 16:40:46 +00:00
|
|
|
|
use crate::pubsub::PubSubPayload;
|
2019-01-27 16:17:12 +00:00
|
|
|
|
use crate::util::helpers::WhitespaceAwareBase64;
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
generate_element!(
|
|
|
|
|
/// Communicates information about an avatar.
|
|
|
|
|
Metadata, "metadata", AVATAR_METADATA,
|
|
|
|
|
children: [
|
|
|
|
|
/// List of information elements describing this avatar.
|
|
|
|
|
infos: Vec<Info> = ("info", AVATAR_METADATA) => Info
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2019-01-27 16:40:46 +00:00
|
|
|
|
impl PubSubPayload for Metadata {}
|
|
|
|
|
|
2019-01-25 01:57:37 +00:00
|
|
|
|
generate_element!(
|
|
|
|
|
/// Communicates avatar metadata.
|
|
|
|
|
Info, "info", AVATAR_METADATA,
|
|
|
|
|
attributes: [
|
|
|
|
|
/// The size of the image data in bytes.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
bytes: Required<u16> = "bytes",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
/// The width of the image in pixels.
|
2019-04-08 19:32:57 +00:00
|
|
|
|
width: Option<u16> = "width",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
/// The height of the image in pixels.
|
2019-04-08 19:32:57 +00:00
|
|
|
|
height: Option<u16> = "height",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
/// The SHA-1 hash of the image data for the specified content-type.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
id: Required<Sha1HexAttribute> = "id",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
/// The IANA-registered content type of the image data.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
type_: Required<String> = "type",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
/// The http: or https: URL at which the image data file is hosted.
|
2019-02-24 19:26:40 +00:00
|
|
|
|
url: Option<String> = "url",
|
2019-01-25 01:57:37 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_element!(
|
|
|
|
|
/// The actual avatar data.
|
|
|
|
|
Data, "data", AVATAR_DATA,
|
|
|
|
|
text: (
|
|
|
|
|
/// Vector of bytes representing the avatar’s image.
|
2019-01-27 16:17:12 +00:00
|
|
|
|
data: WhitespaceAwareBase64<Vec<u8>>
|
2019-01-25 01:57:37 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2019-01-27 16:40:46 +00:00
|
|
|
|
impl PubSubPayload for Data {}
|
|
|
|
|
|
2019-01-25 01:57:37 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::hashes::Algo;
|
2019-09-19 23:41:30 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-25 01:57:37 +00:00
|
|
|
|
use crate::util::error::Error;
|
|
|
|
|
use minidom::Element;
|
2019-04-12 08:58:42 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2019-01-25 01:57:37 +00:00
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Metadata, 12);
|
2019-04-08 19:32:57 +00:00
|
|
|
|
assert_size!(Info, 64);
|
2019-01-25 01:57:37 +00:00
|
|
|
|
assert_size!(Data, 12);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(Metadata, 24);
|
2019-04-08 19:32:57 +00:00
|
|
|
|
assert_size!(Info, 120);
|
2019-01-25 01:57:37 +00:00
|
|
|
|
assert_size!(Data, 24);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
|
|
|
|
let elem: Element = "<metadata xmlns='urn:xmpp:avatar:metadata'>
|
|
|
|
|
<info bytes='12345' width='64' height='64'
|
|
|
|
|
id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'
|
|
|
|
|
type='image/png'/>
|
|
|
|
|
</metadata>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let metadata = Metadata::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(metadata.infos.len(), 1);
|
|
|
|
|
let info = &metadata.infos[0];
|
|
|
|
|
assert_eq!(info.bytes, 12345);
|
|
|
|
|
assert_eq!(info.width, Some(64));
|
|
|
|
|
assert_eq!(info.height, Some(64));
|
|
|
|
|
assert_eq!(info.id.algo, Algo::Sha_1);
|
|
|
|
|
assert_eq!(info.type_, "image/png");
|
|
|
|
|
assert_eq!(info.url, None);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
info.id.hash,
|
|
|
|
|
[
|
|
|
|
|
17, 31, 75, 60, 80, 215, 176, 223, 114, 157, 41, 155, 198, 248, 233, 239, 144, 102,
|
|
|
|
|
151, 31
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<data xmlns='urn:xmpp:avatar:data'>AAAA</data>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let data = Data::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(data.data, b"\0\0\0");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-25 02:45:48 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2019-01-25 01:57:37 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid() {
|
2019-01-25 02:45:48 +00:00
|
|
|
|
let elem: Element = "<data xmlns='urn:xmpp:avatar:data' id='coucou'/>"
|
2019-01-25 01:57:37 +00:00
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let error = Data::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown attribute in data element.")
|
|
|
|
|
}
|
|
|
|
|
}
|