jingle_ibb.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::ibb::{Stanza, StreamId};
 10use crate::ns;
 11
 12/// Describes an [In-Band Bytestream](https://xmpp.org/extensions/xep-0047.html)
 13/// Jingle transport, see also the [IBB module](../ibb.rs).
 14#[derive(FromXml, AsXml, PartialEq, Debug, Clone)]
 15#[xml(namespace = ns::JINGLE_IBB, name = "transport")]
 16pub struct Transport {
 17    /// Maximum size in bytes for each chunk.
 18    #[xml(attribute(name = "block-size"))]
 19    pub block_size: u16,
 20
 21    /// The identifier to be used to create a stream.
 22    #[xml(attribute)]
 23    pub sid: StreamId,
 24
 25    /// Which stanza type to use to exchange data.
 26    #[xml(attribute(default))]
 27    pub stanza: Stanza,
 28}
 29
 30#[cfg(test)]
 31mod tests {
 32    use super::*;
 33    use minidom::Element;
 34    use xso::error::{Error, FromElementError};
 35
 36    #[cfg(target_pointer_width = "32")]
 37    #[test]
 38    fn test_size() {
 39        assert_size!(Transport, 16);
 40    }
 41
 42    #[cfg(target_pointer_width = "64")]
 43    #[test]
 44    fn test_size() {
 45        assert_size!(Transport, 32);
 46    }
 47
 48    #[test]
 49    fn test_simple() {
 50        let elem: Element =
 51            "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='3' sid='coucou'/>"
 52                .parse()
 53                .unwrap();
 54        let transport = Transport::try_from(elem).unwrap();
 55        assert_eq!(transport.block_size, 3);
 56        assert_eq!(transport.sid, StreamId(String::from("coucou")));
 57        assert_eq!(transport.stanza, Stanza::Iq);
 58    }
 59
 60    #[test]
 61    fn test_invalid() {
 62        let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1'/>"
 63            .parse()
 64            .unwrap();
 65        let error = Transport::try_from(elem).unwrap_err();
 66        let message = match error {
 67            FromElementError::Invalid(Error::Other(string)) => string,
 68            _ => panic!(),
 69        };
 70        assert_eq!(
 71            message,
 72            "Required attribute field 'block_size' on Transport element missing."
 73        );
 74
 75        let elem: Element =
 76            "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='65536'/>"
 77                .parse()
 78                .unwrap();
 79        let error = Transport::try_from(elem).unwrap_err();
 80        let message = match error {
 81            FromElementError::Invalid(Error::TextParseError(error))
 82                if error.is::<core::num::ParseIntError>() =>
 83            {
 84                error
 85            }
 86            _ => panic!(),
 87        };
 88        assert_eq!(
 89            message.to_string(),
 90            "number too large to fit in target type"
 91        );
 92
 93        let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='-5'/>"
 94            .parse()
 95            .unwrap();
 96        let error = Transport::try_from(elem).unwrap_err();
 97        let message = match error {
 98            FromElementError::Invalid(Error::TextParseError(error))
 99                if error.is::<core::num::ParseIntError>() =>
100            {
101                error
102            }
103            _ => panic!(),
104        };
105        assert_eq!(message.to_string(), "invalid digit found in string");
106
107        let elem: Element =
108            "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='128'/>"
109                .parse()
110                .unwrap();
111        let error = Transport::try_from(elem).unwrap_err();
112        let message = match error {
113            FromElementError::Invalid(Error::Other(string)) => string,
114            _ => panic!(),
115        };
116        assert_eq!(
117            message,
118            "Required attribute field 'sid' on Transport element missing."
119        );
120    }
121
122    #[test]
123    fn test_invalid_stanza() {
124        let elem: Element = "<transport xmlns='urn:xmpp:jingle:transports:ibb:1' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
125        let error = Transport::try_from(elem).unwrap_err();
126        let message = match error {
127            FromElementError::Invalid(Error::TextParseError(string)) => string,
128            _ => panic!(),
129        };
130        assert_eq!(message.to_string(), "Unknown value for 'stanza' attribute.");
131    }
132}