1// Copyright (c) 2017 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 crate::message::MessagePayload;
8
9generate_element!(
10 /// Structure representing an `<encryption xmlns='urn:xmpp:eme:0'/>` element.
11 ExplicitMessageEncryption, "encryption", EME,
12 attributes: [
13 /// Namespace of the encryption scheme used.
14 namespace: Required<String> = "namespace",
15
16 /// User-friendly name for the encryption scheme, should be `None` for OTR,
17 /// legacy OpenPGP and OX.
18 name: Option<String> = "name",
19 ]
20);
21
22impl MessagePayload for ExplicitMessageEncryption {}
23
24#[cfg(test)]
25mod tests {
26 use super::*;
27 use crate::util::error::Error;
28 use crate::Element;
29 use std::convert::TryFrom;
30
31 #[cfg(target_pointer_width = "32")]
32 #[test]
33 fn test_size() {
34 assert_size!(ExplicitMessageEncryption, 24);
35 }
36
37 #[cfg(target_pointer_width = "64")]
38 #[test]
39 fn test_size() {
40 assert_size!(ExplicitMessageEncryption, 48);
41 }
42
43 #[test]
44 fn test_simple() {
45 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'/>"
46 .parse()
47 .unwrap();
48 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
49 assert_eq!(encryption.namespace, "urn:xmpp:otr:0");
50 assert_eq!(encryption.name, None);
51
52 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='some.unknown.mechanism' name='SuperMechanism'/>".parse().unwrap();
53 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
54 assert_eq!(encryption.namespace, "some.unknown.mechanism");
55 assert_eq!(encryption.name, Some(String::from("SuperMechanism")));
56 }
57
58 #[test]
59 fn test_unknown() {
60 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
61 .parse()
62 .unwrap();
63 let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
64 let message = match error {
65 Error::ParseError(string) => string,
66 _ => panic!(),
67 };
68 assert_eq!(message, "This is not a encryption element.");
69 }
70
71 #[test]
72 fn test_invalid_child() {
73 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0'><coucou/></encryption>"
74 .parse()
75 .unwrap();
76 let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
77 let message = match error {
78 Error::ParseError(string) => string,
79 _ => panic!(),
80 };
81 assert_eq!(message, "Unknown child in encryption element.");
82 }
83
84 #[test]
85 fn test_serialise() {
86 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='coucou'/>"
87 .parse()
88 .unwrap();
89 let eme = ExplicitMessageEncryption {
90 namespace: String::from("coucou"),
91 name: None,
92 };
93 let elem2 = eme.into();
94 assert_eq!(elem, elem2);
95 }
96}