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