eme.rs

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