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::helpers::Base64;
8use crate::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 crate::error::Error;
77 use std::error::Error as StdError;
78
79 #[cfg(target_pointer_width = "32")]
80 #[test]
81 fn test_size() {
82 assert_size!(Stanza, 1);
83 assert_size!(Open, 16);
84 assert_size!(Data, 28);
85 assert_size!(Close, 12);
86 }
87
88 #[cfg(target_pointer_width = "64")]
89 #[test]
90 fn test_size() {
91 assert_size!(Stanza, 1);
92 assert_size!(Open, 32);
93 assert_size!(Data, 56);
94 assert_size!(Close, 24);
95 }
96
97 #[test]
98 fn test_simple() {
99 let sid = StreamId(String::from("coucou"));
100
101 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='3' sid='coucou'/>".parse().unwrap();
102 let open = Open::try_from(elem).unwrap();
103 assert_eq!(open.block_size, 3);
104 assert_eq!(open.sid, sid);
105 assert_eq!(open.stanza, Stanza::Iq);
106
107 let elem: Element = "<data xmlns='http://jabber.org/protocol/ibb' seq='0' sid='coucou'>AAAA</data>".parse().unwrap();
108 let data = Data::try_from(elem).unwrap();
109 assert_eq!(data.seq, 0);
110 assert_eq!(data.sid, sid);
111 assert_eq!(data.data, vec!(0, 0, 0));
112
113 let elem: Element = "<close xmlns='http://jabber.org/protocol/ibb' sid='coucou'/>".parse().unwrap();
114 let close = Close::try_from(elem).unwrap();
115 assert_eq!(close.sid, sid);
116 }
117
118 #[test]
119 fn test_invalid() {
120 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb'/>".parse().unwrap();
121 let error = Open::try_from(elem).unwrap_err();
122 let message = match error {
123 Error::ParseError(string) => string,
124 _ => panic!(),
125 };
126 assert_eq!(message, "Required attribute 'block-size' missing.");
127
128 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='-5'/>".parse().unwrap();
129 let error = Open::try_from(elem).unwrap_err();
130 let message = match error {
131 Error::ParseIntError(error) => error,
132 _ => panic!(),
133 };
134 assert_eq!(message.description(), "invalid digit found in string");
135
136 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128'/>".parse().unwrap();
137 let error = Open::try_from(elem).unwrap_err();
138 let message = match error {
139 Error::ParseError(error) => error,
140 _ => panic!(),
141 };
142 assert_eq!(message, "Required attribute 'sid' missing.");
143 }
144
145 #[test]
146 fn test_invalid_stanza() {
147 let elem: Element = "<open xmlns='http://jabber.org/protocol/ibb' block-size='128' sid='coucou' stanza='fdsq'/>".parse().unwrap();
148 let error = Open::try_from(elem).unwrap_err();
149 let message = match error {
150 Error::ParseError(string) => string,
151 _ => panic!(),
152 };
153 assert_eq!(message, "Unknown value for 'stanza' attribute.");
154 }
155}