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::jingle::SessionId;
10use crate::ns;
11use minidom::Element;
12
13/// Defines a protocol for broadcasting Jingle requests to all of the clients
14/// of a user.
15#[derive(FromXml, AsXml, Debug, Clone)]
16#[xml(namespace = ns::JINGLE_MESSAGE, exhaustive)]
17pub enum JingleMI {
18 /// Indicates we want to start a Jingle session.
19 #[xml(name = "propose")]
20 Propose {
21 /// The generated session identifier, must be unique between two users.
22 #[xml(attribute = "id")]
23 sid: SessionId,
24
25 /// The application description of the proposed session.
26 // TODO: Use a more specialised type here.
27 #[xml(element)]
28 description: Element,
29 },
30
31 /// Cancels a previously proposed session.
32 #[xml(name = "retract")]
33 Retract(#[xml(attribute = "id")] SessionId),
34
35 /// Accepts a session proposed by the other party.
36 #[xml(name = "accept")]
37 Accept(#[xml(attribute = "id")] SessionId),
38
39 /// Proceed with a previously proposed session.
40 #[xml(name = "proceed")]
41 Proceed(#[xml(attribute = "id")] SessionId),
42
43 /// Rejects a session proposed by the other party.
44 #[xml(name = "reject")]
45 Reject(#[xml(attribute = "id")] SessionId),
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51 use xso::error::{Error, FromElementError};
52
53 #[cfg(target_pointer_width = "32")]
54 #[test]
55 fn test_size() {
56 assert_size!(JingleMI, 72);
57 }
58
59 #[cfg(target_pointer_width = "64")]
60 #[test]
61 fn test_size() {
62 assert_size!(JingleMI, 144);
63 }
64
65 #[test]
66 fn test_simple() {
67 let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
68 .parse()
69 .unwrap();
70 JingleMI::try_from(elem).unwrap();
71 }
72
73 // TODO: Enable this test again once #[xml(element)] supports filtering on the element name.
74 #[test]
75 #[ignore]
76 fn test_invalid_child() {
77 let elem: Element =
78 "<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
79 .parse()
80 .unwrap();
81 let error = JingleMI::try_from(elem).unwrap_err();
82 let message = match error {
83 FromElementError::Invalid(Error::Other(string)) => string,
84 _ => panic!(),
85 };
86 assert_eq!(message, "Unknown child in propose element.");
87 }
88}