1 use super::*;
2
3 #[test]
test_goodbye_unmarshal()4 fn test_goodbye_unmarshal() {
5 let tests = vec![
6 (
7 "valid",
8 Bytes::from_static(&[
9 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12
10 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
11 0x03, 0x46, 0x4f, 0x4f, // len=3, text=FOO
12 ]),
13 Goodbye {
14 sources: vec![0x902f9e2e],
15 reason: Bytes::from_static(b"FOO"),
16 },
17 None,
18 ),
19 (
20 "invalid octet count",
21 Bytes::from_static(&[
22 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12
23 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
24 0x04, 0x46, 0x4f, 0x4f, // len=4, text=FOO
25 ]),
26 Goodbye {
27 sources: vec![],
28 reason: Bytes::from_static(b""),
29 },
30 Some(Error::PacketTooShort),
31 ),
32 (
33 "wrong type",
34 Bytes::from_static(&[
35 0x81, 0xca, 0x00, 0x0c, // v=2, p=0, count=1, SDES, len=12
36 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
37 0x03, 0x46, 0x4f, 0x4f, // len=3, text=FOO
38 ]),
39 Goodbye {
40 sources: vec![],
41 reason: Bytes::from_static(b""),
42 },
43 Some(Error::WrongType),
44 ),
45 (
46 "short reason",
47 Bytes::from_static(&[
48 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12
49 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
50 0x01, 0x46, 0x00, 0x00, // len=3, text=F + padding
51 ]),
52 Goodbye {
53 sources: vec![0x902f9e2e],
54 reason: Bytes::from_static(b"F"),
55 },
56 None,
57 ),
58 (
59 "not byte aligned",
60 Bytes::from_static(&[
61 0x81, 0xcb, 0x00, 0x0a, // v=2, p=0, count=1, BYE, len=10
62 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
63 0x01, 0x46, // len=1, text=F
64 ]),
65 Goodbye {
66 sources: vec![],
67 reason: Bytes::from_static(b""),
68 },
69 Some(Error::PacketTooShort),
70 ),
71 (
72 "bad count in header",
73 Bytes::from_static(&[
74 0x82, 0xcb, 0x00, 0x0c, // v=2, p=0, count=2, BYE, len=8
75 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e
76 ]),
77 Goodbye {
78 sources: vec![],
79 reason: Bytes::from_static(b""),
80 },
81 Some(Error::PacketTooShort),
82 ),
83 (
84 "empty packet",
85 Bytes::from_static(&[
86 // v=2, p=0, count=0, BYE, len=4
87 0x80, 0xcb, 0x00, 0x04,
88 ]),
89 Goodbye {
90 sources: vec![],
91 reason: Bytes::from_static(b""),
92 },
93 None,
94 ),
95 (
96 "nil",
97 Bytes::from_static(&[]),
98 Goodbye {
99 sources: vec![],
100 reason: Bytes::from_static(b""),
101 },
102 Some(Error::PacketTooShort),
103 ),
104 ];
105
106 for (name, mut data, want, want_error) in tests {
107 let got = Goodbye::unmarshal(&mut data);
108
109 assert_eq!(
110 got.is_err(),
111 want_error.is_some(),
112 "Unmarshal {name} bye: err = {got:?}, want {want_error:?}"
113 );
114
115 if let Some(err) = want_error {
116 let got_err = got.err().unwrap();
117 assert_eq!(
118 err, got_err,
119 "Unmarshal {name} rr: err = {got_err:?}, want {err:?}",
120 );
121 } else {
122 let actual = got.unwrap();
123 assert_eq!(
124 actual, want,
125 "Unmarshal {name} rr: got {actual:?}, want {want:?}"
126 );
127 }
128 }
129 }
130
131 #[test]
test_goodbye_round_trip()132 fn test_goodbye_round_trip() {
133 let too_many_sources = vec![0u32; 1 << 5];
134
135 let mut too_long_text = String::new();
136 for _ in 0..1 << 8 {
137 too_long_text.push('x');
138 }
139
140 let tests = vec![
141 (
142 "empty",
143 Goodbye {
144 sources: vec![],
145 ..Default::default()
146 },
147 None,
148 ),
149 (
150 "valid",
151 Goodbye {
152 sources: vec![0x01020304, 0x05060708],
153 reason: Bytes::from_static(b"because"),
154 },
155 None,
156 ),
157 (
158 "empty reason",
159 Goodbye {
160 sources: vec![0x01020304],
161 reason: Bytes::from_static(b""),
162 },
163 None,
164 ),
165 (
166 "reason no source",
167 Goodbye {
168 sources: vec![],
169 reason: Bytes::from_static(b"foo"),
170 },
171 None,
172 ),
173 (
174 "short reason",
175 Goodbye {
176 sources: vec![],
177 reason: Bytes::from_static(b"f"),
178 },
179 None,
180 ),
181 (
182 "count overflow",
183 Goodbye {
184 sources: too_many_sources,
185 reason: Bytes::from_static(b""),
186 },
187 Some(Error::TooManySources),
188 ),
189 (
190 "reason too long",
191 Goodbye {
192 sources: vec![],
193 reason: Bytes::copy_from_slice(too_long_text.as_bytes()),
194 },
195 Some(Error::ReasonTooLong),
196 ),
197 ];
198
199 for (name, want, want_error) in tests {
200 let got = want.marshal();
201
202 assert_eq!(
203 got.is_ok(),
204 want_error.is_none(),
205 "Marshal {name}: err = {got:?}, want {want_error:?}"
206 );
207
208 if let Some(err) = want_error {
209 let got_err = got.err().unwrap();
210 assert_eq!(
211 err, got_err,
212 "Unmarshal {name} rr: err = {got_err:?}, want {err:?}",
213 );
214 } else {
215 let mut data = got.ok().unwrap();
216 let actual =
217 Goodbye::unmarshal(&mut data).unwrap_or_else(|_| panic!("Unmarshal {name}"));
218
219 assert_eq!(
220 actual, want,
221 "{name} round trip: got {actual:?}, want {want:?}"
222 )
223 }
224 }
225 }
226