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