Fuzz test for u8 fails

This commit is contained in:
Kirk Baird 2019-02-20 15:03:32 +11:00
parent 52347d8e6d
commit 38abcc4a24
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
3 changed files with 32 additions and 0 deletions

View File

@ -20,3 +20,7 @@ members = ["."]
[[bin]] [[bin]]
name = "fuzz_target_u8" name = "fuzz_target_u8"
path = "fuzz_targets/fuzz_target_u8.rs" path = "fuzz_targets/fuzz_target_u8.rs"
[[bin]]
name = "fuzz_target_u16"
path = "fuzz_targets/fuzz_target_u16.rs"

View File

@ -0,0 +1,19 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable, Encodable};
// Fuzz ssz_decode(u8)
fuzz_target!(|data: &[u8]| {
let result: Result<(u16, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() > 1 {
// Valid result
let (number_u16, index) = result.unwrap();
assert_eq!(index, 2);
// TODO: add test for number?
} else {
// Length of 0 or 1 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@ -7,4 +7,13 @@ use ssz::{DecodeError, Decodable, Encodable};
// Fuzz ssz_decode(u8) // Fuzz ssz_decode(u8)
fuzz_target!(|data: &[u8]| { fuzz_target!(|data: &[u8]| {
let result: Result<(u8, usize), DecodeError> = Decodable::ssz_decode(data, 0); let result: Result<(u8, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() > 0 {
// Should have valid result
let (number_u8, index) = result.unwrap();
assert_eq!(number_u8, data[0]);
assert_eq!(index, 2);
} else {
// Length of 0 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
}); });