Added unit tests for decoding errors

This commit is contained in:
Victor Koenders 2021-09-19 11:02:33 +02:00
parent ca353b9882
commit 1313e989ad
1 changed files with 154 additions and 0 deletions

View File

@ -213,6 +213,38 @@ fn test_decode_u16() {
let found = varint_decode_u16(&mut reader, Endian::Big).unwrap();
assert_eq!(expected_be, found);
}
let errors: &[(&[u8], DecodeError)] = &[
(
&[U32_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U16,
found: IntegerType::U32,
},
),
(
&[U64_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U16,
found: IntegerType::U64,
},
),
(
&[U128_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U16,
found: IntegerType::U128,
},
),
(&[U16_BYTE], DecodeError::UnexpectedEnd),
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd),
];
for (slice, expected) in errors {
let mut reader = crate::de::read::SliceReader::new(slice);
let found = varint_decode_u16(&mut reader, Endian::Little).unwrap_err();
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found));
}
}
#[test]
@ -232,6 +264,35 @@ fn test_decode_u32() {
let found = varint_decode_u32(&mut reader, Endian::Big).unwrap();
assert_eq!(expected_be, found);
}
let errors: &[(&[u8], DecodeError)] = &[
(
&[U64_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U32,
found: IntegerType::U64,
},
),
(
&[U128_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U32,
found: IntegerType::U128,
},
),
(&[U16_BYTE], DecodeError::UnexpectedEnd),
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
];
for (slice, expected) in errors {
let mut reader = crate::de::read::SliceReader::new(slice);
let found = varint_decode_u32(&mut reader, Endian::Little).unwrap_err();
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found));
}
}
#[test]
@ -256,6 +317,36 @@ fn test_decode_u64() {
let found = varint_decode_u64(&mut reader, Endian::Big).unwrap();
assert_eq!(expected_be, found);
}
let errors: &[(&[u8], DecodeError)] = &[
(
&[U128_BYTE],
DecodeError::InvalidIntegerType {
expected: IntegerType::U64,
found: IntegerType::U128,
},
),
(&[U16_BYTE], DecodeError::UnexpectedEnd),
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
];
for (slice, expected) in errors {
let mut reader = crate::de::read::SliceReader::new(slice);
let found = varint_decode_u64(&mut reader, Endian::Little).unwrap_err();
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found));
}
}
#[test]
@ -285,4 +376,67 @@ fn test_decode_u128() {
let found = varint_decode_u128(&mut reader, Endian::Big).unwrap();
assert_eq!(expected_be, found);
}
let errors: &[(&[u8], DecodeError)] = &[
(&[U16_BYTE], DecodeError::UnexpectedEnd),
(&[U16_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U32_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U64_BYTE, 0, 0, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(&[U128_BYTE, 0, 0, 0, 0, 0, 0], DecodeError::UnexpectedEnd),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
(
&[U128_BYTE, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
DecodeError::UnexpectedEnd,
),
];
for (slice, expected) in errors {
let mut reader = crate::de::read::SliceReader::new(slice);
let found = varint_decode_u128(&mut reader, Endian::Little).unwrap_err();
std::dbg!(slice);
assert_eq!(std::format!("{:?}", expected), std::format!("{:?}", found));
}
}