1// Copyright (c) 2019 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use xso::{
8 text::{Base64, StripWhitespace, TextCodec},
9 AsXml, FromXml,
10};
11
12use crate::hashes::Sha1HexAttribute;
13use crate::ns;
14use crate::pubsub::PubSubPayload;
15
16/// Communicates information about an avatar.
17#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
18#[xml(namespace = ns::AVATAR_METADATA, name = "metadata")]
19pub struct Metadata {
20 /// List of information elements describing this avatar.
21 #[xml(child(n = ..))]
22 pub infos: Vec<Info>,
23}
24
25impl PubSubPayload for Metadata {}
26
27/// Communicates avatar metadata.
28#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
29#[xml(namespace = ns::AVATAR_METADATA, name = "info")]
30pub struct Info {
31 /// The size of the image data in bytes.
32 #[xml(attribute)]
33 pub bytes: u32,
34
35 /// The width of the image in pixels.
36 #[xml(attribute(default))]
37 pub width: Option<u16>,
38
39 /// The height of the image in pixels.
40 #[xml(attribute(default))]
41 pub height: Option<u16>,
42
43 /// The SHA-1 hash of the image data for the specified content-type.
44 #[xml(attribute)]
45 pub id: Sha1HexAttribute,
46
47 /// The IANA-registered content type of the image data.
48 #[xml(attribute = "type")]
49 pub type_: String,
50
51 /// The http: or https: URL at which the image data file is hosted.
52 #[xml(attribute(default))]
53 pub url: Option<String>,
54}
55
56/// The actual avatar data.
57#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
58#[xml(namespace = ns::AVATAR_DATA, name = "data")]
59pub struct Data {
60 /// Vector of bytes representing the avatar’s image.
61 #[xml(text(codec = Base64.filtered(StripWhitespace)))]
62 pub data: Vec<u8>,
63}
64
65impl PubSubPayload for Data {}
66
67#[cfg(test)]
68mod tests {
69 use super::*;
70 use crate::hashes::Algo;
71 use minidom::Element;
72 #[cfg(not(feature = "disable-validation"))]
73 use xso::error::{Error, FromElementError};
74
75 #[cfg(target_pointer_width = "32")]
76 #[test]
77 fn test_size() {
78 assert_size!(Metadata, 12);
79 assert_size!(Info, 60);
80 assert_size!(Data, 12);
81 }
82
83 #[cfg(target_pointer_width = "64")]
84 #[test]
85 fn test_size() {
86 assert_size!(Metadata, 24);
87 assert_size!(Info, 112);
88 assert_size!(Data, 24);
89 }
90
91 #[test]
92 fn test_simple() {
93 let elem: Element = "<metadata xmlns='urn:xmpp:avatar:metadata'>
94 <info bytes='12345' width='64' height='64'
95 id='111f4b3c50d7b0df729d299bc6f8e9ef9066971f'
96 type='image/png'/>
97 </metadata>"
98 .parse()
99 .unwrap();
100 let metadata = Metadata::try_from(elem).unwrap();
101 assert_eq!(metadata.infos.len(), 1);
102 let info = &metadata.infos[0];
103 assert_eq!(info.bytes, 12345);
104 assert_eq!(info.width, Some(64));
105 assert_eq!(info.height, Some(64));
106 assert_eq!(info.id.algo, Algo::Sha_1);
107 assert_eq!(info.type_, "image/png");
108 assert_eq!(info.url, None);
109 assert_eq!(
110 info.id.hash,
111 [
112 17, 31, 75, 60, 80, 215, 176, 223, 114, 157, 41, 155, 198, 248, 233, 239, 144, 102,
113 151, 31
114 ]
115 );
116
117 let elem: Element = "<data xmlns='urn:xmpp:avatar:data'>AAAA</data>"
118 .parse()
119 .unwrap();
120 let data = Data::try_from(elem).unwrap();
121 assert_eq!(data.data, b"\0\0\0");
122 }
123
124 #[cfg(not(feature = "disable-validation"))]
125 #[test]
126 fn test_invalid() {
127 let elem: Element = "<data xmlns='urn:xmpp:avatar:data' id='coucou'/>"
128 .parse()
129 .unwrap();
130 let error = Data::try_from(elem).unwrap_err();
131 let message = match error {
132 FromElementError::Invalid(Error::Other(string)) => string,
133 _ => panic!(),
134 };
135 assert_eq!(message, "Unknown attribute in Data element.")
136 }
137}