xmpp_parsers_gst_meet/
stanza_id.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 crate::message::MessagePayload;
8use jid::Jid;
9
10generate_element!(
11    /// Gives the identifier a service has stamped on this stanza, often in
12    /// order to identify it inside of [an archive](../mam/index.html).
13    StanzaId, "stanza-id", SID,
14    attributes: [
15        /// The id associated to this stanza by another entity.
16        id: Required<String> = "id",
17
18        /// The entity who stamped this stanza-id.
19        by: Required<Jid> = "by",
20    ]
21);
22
23impl MessagePayload for StanzaId {}
24
25generate_element!(
26    /// A hack for MUC before version 1.31 to track a message which may have
27    /// its 'id' attribute changed.
28    OriginId, "origin-id", SID,
29    attributes: [
30        /// The id this client set for this stanza.
31        id: Required<String> = "id",
32    ]
33);
34
35impl MessagePayload for OriginId {}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use crate::util::error::Error;
41    use crate::Element;
42    use jid::BareJid;
43    use std::convert::TryFrom;
44
45    #[cfg(target_pointer_width = "32")]
46    #[test]
47    fn test_size() {
48        assert_size!(StanzaId, 52);
49        assert_size!(OriginId, 12);
50    }
51
52    #[cfg(target_pointer_width = "64")]
53    #[test]
54    fn test_size() {
55        assert_size!(StanzaId, 104);
56        assert_size!(OriginId, 24);
57    }
58
59    #[test]
60    fn test_simple() {
61        let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>"
62            .parse()
63            .unwrap();
64        let stanza_id = StanzaId::try_from(elem).unwrap();
65        assert_eq!(stanza_id.id, String::from("coucou"));
66        assert_eq!(stanza_id.by, BareJid::new("coucou", "coucou"));
67
68        let elem: Element = "<origin-id xmlns='urn:xmpp:sid:0' id='coucou'/>"
69            .parse()
70            .unwrap();
71        let origin_id = OriginId::try_from(elem).unwrap();
72        assert_eq!(origin_id.id, String::from("coucou"));
73    }
74
75    #[test]
76    fn test_invalid_child() {
77        let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'><coucou/></stanza-id>"
78            .parse()
79            .unwrap();
80        let error = StanzaId::try_from(elem).unwrap_err();
81        let message = match error {
82            Error::ParseError(string) => string,
83            _ => panic!(),
84        };
85        assert_eq!(message, "Unknown child in stanza-id element.");
86    }
87
88    #[test]
89    fn test_invalid_id() {
90        let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0'/>".parse().unwrap();
91        let error = StanzaId::try_from(elem).unwrap_err();
92        let message = match error {
93            Error::ParseError(string) => string,
94            _ => panic!(),
95        };
96        assert_eq!(message, "Required attribute 'id' missing.");
97    }
98
99    #[test]
100    fn test_invalid_by() {
101        let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou'/>"
102            .parse()
103            .unwrap();
104        let error = StanzaId::try_from(elem).unwrap_err();
105        let message = match error {
106            Error::ParseError(string) => string,
107            _ => panic!(),
108        };
109        assert_eq!(message, "Required attribute 'by' missing.");
110    }
111
112    #[test]
113    fn test_serialise() {
114        let elem: Element = "<stanza-id xmlns='urn:xmpp:sid:0' id='coucou' by='coucou@coucou'/>"
115            .parse()
116            .unwrap();
117        let stanza_id = StanzaId {
118            id: String::from("coucou"),
119            by: Jid::Bare(BareJid::new("coucou", "coucou")),
120        };
121        let elem2 = stanza_id.into();
122        assert_eq!(elem, elem2);
123    }
124}