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