Skip to content

Commit 5f8dc0d

Browse files
committed
attribute/password: add test for incorrectly sized data
1 parent 11457b8 commit 5f8dc0d

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

stun-types/src/attribute/password_algorithm.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ impl PasswordAlgorithmValue {
3737
}
3838

3939
fn read(data: &[u8]) -> Result<Self, StunParseError> {
40+
// checked externally that we have at least 4 bytes
4041
let ty = BigEndian::read_u16(&data[..2]);
4142
let len = BigEndian::read_u16(&data[2..4]);
43+
// all currently know algorithms don't ahve any extra data
4244
if len != 0 {
4345
return Err(StunParseError::TooLarge {
4446
expected: 4,
45-
actual: data.len(),
47+
actual: 4 + len as usize,
4648
});
4749
}
4850
Ok(match ty {
@@ -268,4 +270,35 @@ mod tests {
268270
Err(StunParseError::WrongAttributeImplementation)
269271
));
270272
}
273+
274+
#[test]
275+
fn password_algorithm_value_too_large() {
276+
init();
277+
let val = PasswordAlgorithmValue::SHA256;
278+
let attr = PasswordAlgorithm::new(val);
279+
let raw: RawAttribute = attr.into();
280+
let mut data = raw.to_bytes();
281+
data[7] = 100;
282+
assert!(matches!(
283+
PasswordAlgorithm::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
284+
Err(StunParseError::TooLarge {
285+
expected: 4,
286+
actual: 104
287+
})
288+
));
289+
}
290+
291+
#[test]
292+
fn password_algorithm_value_unknown() {
293+
init();
294+
let val = PasswordAlgorithmValue::SHA256;
295+
let attr = PasswordAlgorithm::new(val);
296+
let raw: RawAttribute = attr.into();
297+
let mut data = raw.to_bytes();
298+
data[5] = 0x80;
299+
assert!(matches!(
300+
PasswordAlgorithm::try_from(&RawAttribute::from_bytes(data.as_ref()).unwrap()),
301+
Err(StunParseError::InvalidAttributeData)
302+
));
303+
}
271304
}

0 commit comments

Comments
 (0)