1use xso::{AsXml, FromXml};
8
9use crate::message::MessagePayload;
10use crate::ns;
11
12#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
14#[xml(namespace = ns::EME, name = "encryption")]
15pub struct ExplicitMessageEncryption {
16 #[xml(attribute)]
18 pub namespace: String,
19
20 #[xml(attribute(default))]
23 pub name: Option<String>,
24}
25
26impl MessagePayload for ExplicitMessageEncryption {}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31 use minidom::Element;
32 use xso::error::{Error, FromElementError};
33
34 #[cfg(target_pointer_width = "32")]
35 #[test]
36 fn test_size() {
37 assert_size!(ExplicitMessageEncryption, 24);
38 }
39
40 #[cfg(target_pointer_width = "64")]
41 #[test]
42 fn test_size() {
43 assert_size!(ExplicitMessageEncryption, 48);
44 }
45
46 #[test]
47 fn test_simple() {
48 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'/>"
49 .parse()
50 .unwrap();
51 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
52 assert_eq!(encryption.namespace, "urn:xmpp:otr:0");
53 assert_eq!(encryption.name, None);
54
55 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='some.unknown.mechanism' name='SuperMechanism'/>".parse().unwrap();
56 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
57 assert_eq!(encryption.namespace, "some.unknown.mechanism");
58 assert_eq!(encryption.name, Some(String::from("SuperMechanism")));
59 }
60
61 #[test]
62 fn test_unknown() {
63 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
64 .parse()
65 .unwrap();
66 let error = ExplicitMessageEncryption::try_from(elem.clone()).unwrap_err();
67 let returned_elem = match error {
68 FromElementError::Mismatch(elem) => elem,
69 _ => panic!(),
70 };
71 assert_eq!(elem, returned_elem);
72 }
73
74 #[test]
75 #[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
76 fn test_invalid_child() {
77 let elem: Element =
78 "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'><coucou/></encryption>"
79 .parse()
80 .unwrap();
81 let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
82 let message = match error {
83 FromElementError::Invalid(Error::Other(string)) => string,
84 _ => panic!(),
85 };
86 assert_eq!(
87 message,
88 "Unknown child in ExplicitMessageEncryption element."
89 );
90 }
91
92 #[test]
93 fn test_serialise() {
94 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='coucou'/>"
95 .parse()
96 .unwrap();
97 let eme = ExplicitMessageEncryption {
98 namespace: String::from("coucou"),
99 name: None,
100 };
101 let elem2 = eme.into();
102 assert_eq!(elem, elem2);
103 }
104}