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 helpers::Base64;
8use iq::IqSetPayload;
9
10generate_id!(
11/// An identifier matching a stream.
12StreamId);
13
14generate_attribute!(
15/// Which stanza type to use to exchange data.
16Stanza, "stanza", {
17 /// `<iq/>` gives a feedback on whether the chunk has been received or not,
18 /// which is useful in the case the recipient might not receive them in a
19 /// timely manner, or to do your own throttling based on the results.
20 Iq => "iq",
21
22 /// `<message/>` can be faster, since it doesn’t require any feedback, but in
23 /// practice it will be throttled by the servers on the way.
24 Message => "message",
25}, Default = Iq);
26
27generate_element!(
28/// Starts an In-Band Bytestream session with the given parameters.
29Open, "open", IBB,
30attributes: [
31 /// Maximum size in bytes for each chunk.
32 block_size: u16 = "block-size" => required,
33
34 /// The identifier to be used to create a stream.
35 sid: StreamId = "sid" => required,
36
37 /// Which stanza type to use to exchange data.
38 stanza: Stanza = "stanza" => default,
39]);
40
41impl IqSetPayload for Open {}
42
43generate_element!(
44/// Exchange a chunk of data in an open stream.
45Data, "data", IBB,
46 attributes: [
47 /// Sequence number of this chunk, must wraparound after 65535.
48 seq: u16 = "seq" => required,
49
50 /// The identifier of the stream on which data is being exchanged.
51 sid: StreamId = "sid" => required
52 ],
53 text: (
54 /// Vector of bytes to be exchanged.
55 data: Base64<Vec<u8>>
56 )
57);
58
59impl IqSetPayload for Data {}
60
61generate_element!(
62/// Close an open stream.
63Close, "close", IBB,
64attributes: [
65 /// The identifier of the stream to be closed.
66 sid: StreamId = "sid" => required,
67]);
68
69impl IqSetPayload for Close {}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74 use try_from::TryFrom;
75 use minidom::Element;
76 use error::Error;
77 use std::error::Error as StdError;
78
79 #[test]
80 fn test_size() {
81 assert_size!(Stanza, 1);
82 assert_size!(Open, 32);
83 assert_size!(Data, 56);
84 assert_size!(Close, 24);
85 }
86
87 #[test]
88 fn test_simple() {
89 let sid = StreamId(String::from("coucou"));
90
91 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
92 let open = Open::try_from(elem).unwrap();
93 assert_eq!(open.block_size, 3);
94 assert_eq!(open.sid, sid);
95 assert_eq!(open.stanza, Stanza::Iq);
96
97 let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
98 let data = Data::try_from(elem).unwrap();
99 assert_eq!(data.seq, 0);
100 assert_eq!(data.sid, sid);
101 assert_eq!(data.data, vec!(0, 0, 0));
102
103 let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
104 let close = Close::try_from(elem).unwrap();
105 assert_eq!(close.sid, sid);
106 }
107
108 #[test]
109 fn test_invalid() {
110 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap();
111 let error = Open::try_from(elem).unwrap_err();
112 let message = match error {
113 Error::ParseError(string) => string,
114 _ => panic!(),
115 };
116 assert_eq!(message, "Required attribute 'block-size' missing.");
117
118 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap();
119 let error = Open::try_from(elem).unwrap_err();
120 let message = match error {
121 Error::ParseIntError(error) => error,
122 _ => panic!(),
123 };
124 assert_eq!(message.description(), "invalid digit found in string");
125
126 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap();
127 let error = Open::try_from(elem).unwrap_err();
128 let message = match error {
129 Error::ParseError(error) => error,
130 _ => panic!(),
131 };
132 assert_eq!(message, "Required attribute 'sid' missing.");
133 }
134
135 #[test]
136 fn test_invalid_stanza() {
137 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
138 let error = Open::try_from(elem).unwrap_err();
139 let message = match error {
140 Error::ParseError(string) => string,
141 _ => panic!(),
142 };
143 assert_eq!(message, "Unknown value for 'stanza' attribute.");
144 }
145}