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 xso::{AsXml, FromXml};
  8
  9use crate::message::MessagePayload;
 10use crate::ns;
 11
 12/// Structure representing an `<encryption xmlns='urn:xmpp:eme:0'/>` element.
 13#[derive(FromXml, AsXml, Debug, Clone, PartialEq)]
 14#[xml(namespace = ns::EME, name = "encryption")]
 15pub struct ExplicitMessageEncryption {
 16    /// Namespace of the encryption scheme used.
 17    #[xml(attribute)]
 18    pub namespace: String,
 19
 20    /// User-friendly name for the encryption scheme, should be `None` for OTR,
 21    /// legacy OpenPGP and OX.
 22    #[xml(attribute(default))]
 23    pub name: Option<String>,
 24}
 25
 26impl MessagePayload for ExplicitMessageEncryption {}
 27
 28#[cfg(test)]
 29mod tests {
 30    use super::*;
 31    use minidom::Element;
 32    use xso::error::{Error, FromElementError};
 33
 34    #[cfg(target_pointer_width = "32")]
 35    #[test]
 36    fn test_size() {
 37        assert_size!(ExplicitMessageEncryption, 24);
 38    }
 39
 40    #[cfg(target_pointer_width = "64")]
 41    #[test]
 42    fn test_size() {
 43        assert_size!(ExplicitMessageEncryption, 48);
 44    }
 45
 46    #[test]
 47    fn test_simple() {
 48        let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'/>"
 49            .parse()
 50            .unwrap();
 51        let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
 52        assert_eq!(encryption.namespace, "urn:xmpp:otr:0");
 53        assert_eq!(encryption.name, None);
 54
 55        let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='some.unknown.mechanism' name='SuperMechanism'/>".parse().unwrap();
 56        let encryption = ExplicitMessageEncryption::try_from(elem).unwrap();
 57        assert_eq!(encryption.namespace, "some.unknown.mechanism");
 58        assert_eq!(encryption.name, Some(String::from("SuperMechanism")));
 59    }
 60
 61    #[test]
 62    fn test_unknown() {
 63        let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>"
 64            .parse()
 65            .unwrap();
 66        let error = ExplicitMessageEncryption::try_from(elem.clone()).unwrap_err();
 67        let returned_elem = match error {
 68            FromElementError::Mismatch(elem) => elem,
 69            _ => panic!(),
 70        };
 71        assert_eq!(elem, returned_elem);
 72    }
 73
 74    #[test]
 75    #[cfg_attr(feature = "disable-validation", should_panic = "Result::unwrap_err")]
 76    fn test_invalid_child() {
 77        let elem: Element =
 78            "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'><coucou/></encryption>"
 79                .parse()
 80                .unwrap();
 81        let error = ExplicitMessageEncryption::try_from(elem).unwrap_err();
 82        let message = match error {
 83            FromElementError::Invalid(Error::Other(string)) => string,
 84            _ => panic!(),
 85        };
 86        assert_eq!(
 87            message,
 88            "Unknown child in ExplicitMessageEncryption element."
 89        );
 90    }
 91
 92    #[test]
 93    fn test_serialise() {
 94        let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='coucou'/>"
 95            .parse()
 96            .unwrap();
 97        let eme = ExplicitMessageEncryption {
 98            namespace: String::from("coucou"),
 99            name: None,
100        };
101        let elem2 = eme.into();
102        assert_eq!(elem, elem2);
103    }
104}