Fuzz test decodes from u8 to u64

This commit is contained in:
Kirk Baird 2019-02-20 15:34:15 +11:00
parent 38abcc4a24
commit d5c4771f0a
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
5 changed files with 78 additions and 12 deletions

View File

@ -18,9 +18,17 @@ git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
members = ["."]
[[bin]]
name = "fuzz_target_u8"
path = "fuzz_targets/fuzz_target_u8.rs"
name = "fuzz_target_u8_decode"
path = "fuzz_targets/fuzz_target_u8_decode.rs"
[[bin]]
name = "fuzz_target_u16"
path = "fuzz_targets/fuzz_target_u16.rs"
name = "fuzz_target_u16_decode"
path = "fuzz_targets/fuzz_target_u16_decode.rs"
[[bin]]
name = "fuzz_target_u32_decode"
path = "fuzz_targets/fuzz_target_u32_decode.rs"
[[bin]]
name = "fuzz_target_u64_decode"
path = "fuzz_targets/fuzz_target_u64_decode.rs"

View File

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

View File

@ -0,0 +1,22 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u32, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 4 {
// Valid result
let (number_u32, index) = result.unwrap();
assert_eq!(index, 4);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u32::from_be_bytes([data[0], data[1], data[2], data[3]]);
assert_eq!(number_u32, val);
} else {
// Length less then 4 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@ -0,0 +1,31 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u64, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() >= 8 {
// Valid result
let (number_u64, index) = result.unwrap();
assert_eq!(index, 8);
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
let val = u64::from_be_bytes([
data[0],
data[1],
data[2],
data[3],
data[4],
data[5],
data[6],
data[7],
]);
assert_eq!(number_u64, val);
} else {
// Length less then 4 should return error
assert_eq!(result, Err(DecodeError::TooShort));
}
});

View File

@ -2,16 +2,18 @@
#[macro_use] extern crate libfuzzer_sys;
extern crate ssz;
use ssz::{DecodeError, Decodable, Encodable};
use ssz::{DecodeError, Decodable};
// Fuzz ssz_decode(u8)
// Fuzz ssz_decode()
fuzz_target!(|data: &[u8]| {
let result: Result<(u8, usize), DecodeError> = Decodable::ssz_decode(data, 0);
if data.len() > 0 {
if data.len() >= 1 {
// Should have valid result
let (number_u8, index) = result.unwrap();
// TODO: change to little endian bytes
// https://github.com/sigp/lighthouse/issues/215
assert_eq!(number_u8, data[0]);
assert_eq!(index, 2);
assert_eq!(index, 1);
} else {
// Length of 0 should return error
assert_eq!(result, Err(DecodeError::TooShort));