Fuzz test decodes from u8 to u64
This commit is contained in:
parent
38abcc4a24
commit
d5c4771f0a
@ -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"
|
||||
|
@ -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));
|
22
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_u32_decode.rs
Normal file
22
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_u32_decode.rs
Normal 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));
|
||||
}
|
||||
});
|
31
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_u64_decode.rs
Normal file
31
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_u64_decode.rs
Normal 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));
|
||||
}
|
||||
});
|
@ -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));
|
Loading…
Reference in New Issue
Block a user