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 try_from::TryFrom;
8
9use minidom::Element;
10
11use error::Error;
12
13use ns;
14
15generate_element_with_only_attributes!(
16/// Structure representing an `<encryption xmlns='urn:xmpp:eme:0'/>` element.
17ExplicitMessageEncryption, "encryption", ns::EME, [
18 /// Namespace of the encryption scheme used.
19 namespace: String = "namespace" => required,
20
21 /// User-friendly name for the encryption scheme, should be `None` for OTR,
22 /// legacy OpenPGP and OX.
23 name: Option<String> = "name" => optional,
24]);
25
26#[cfg(test)]
27mod tests {
28 use super::*;
29
30 #[test]
31 fn test_simple() {
32 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'/>".parse().unwrap();
33 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
34 assert_eq!(encryption.namespace, "urn:xmpp:otr:0");
35 assert_eq!(encryption.name, None);
36
37 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='some.unknown.mechanism' name='SuperMechanism'/>".parse().unwrap();
38 let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
39 assert_eq!(encryption.namespace, "some.unknown.mechanism");
40 assert_eq!(encryption.name, Some(String::from("SuperMechanism")));
41 }
42
43 #[test]
44 fn test_unknown() {
45 let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
46 let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
47 let message = match error {
48 Error::ParseError(string) => string,
49 _ => panic!(),
50 };
51 assert_eq!(message, "This is not a encryption element.");
52 }
53
54 #[test]
55 fn test_invalid_child() {
56 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0'><coucou/></encryption>".parse().unwrap();
57 let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
58 let message = match error {
59 Error::ParseError(string) => string,
60 _ => panic!(),
61 };
62 assert_eq!(message, "Unknown child in encryption element.");
63 }
64
65 #[test]
66 fn test_serialise() {
67 let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='coucou'/>".parse().unwrap();
68 let eme = ExplicitMessageEncryption { namespace: String::from("coucou"), name: None };
69 let elem2 = eme.into();
70 assert_eq!(elem, elem2);
71 }
72}