Fuzz for address and Hash256
This commit is contained in:
parent
00e5b57166
commit
274bdd491d
@ -8,6 +8,9 @@ publish = false
|
||||
[package.metadata]
|
||||
cargo-fuzz = true
|
||||
|
||||
[dependencies]
|
||||
ethereum-types = "0.4.0"
|
||||
|
||||
[dependencies.ssz]
|
||||
path = ".."
|
||||
[dependencies.libfuzzer-sys]
|
||||
@ -17,6 +20,14 @@ git = "https://github.com/rust-fuzz/libfuzzer-sys.git"
|
||||
[workspace]
|
||||
members = ["."]
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_bool_decode"
|
||||
path = "fuzz_targets/fuzz_target_bool_decode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_bool_encode"
|
||||
path = "fuzz_targets/fuzz_target_bool_encode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_u8_decode"
|
||||
path = "fuzz_targets/fuzz_target_u8_decode.rs"
|
||||
@ -56,3 +67,19 @@ path = "fuzz_targets/fuzz_target_usize_decode.rs"
|
||||
[[bin]]
|
||||
name = "fuzz_target_usize_encode"
|
||||
path = "fuzz_targets/fuzz_target_usize_encode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_hash256_decode"
|
||||
path = "fuzz_targets/fuzz_target_hash256_decode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_hash256_encode"
|
||||
path = "fuzz_targets/fuzz_target_hash256_encode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_address_decode"
|
||||
path = "fuzz_targets/fuzz_target_address_decode.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_target_address_encode"
|
||||
path = "fuzz_targets/fuzz_target_address_encode.rs"
|
||||
|
@ -0,0 +1,21 @@
|
||||
#![no_main]
|
||||
#[macro_use] extern crate libfuzzer_sys;
|
||||
extern crate ethereum_types;
|
||||
extern crate ssz;
|
||||
|
||||
use ethereum_types::Address;
|
||||
use ssz::{DecodeError, Decodable};
|
||||
|
||||
// Fuzz ssz_decode()
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let result: Result<(Address, usize), DecodeError> = Decodable::ssz_decode(data, 0);
|
||||
if data.len() >= 20 {
|
||||
// Should have valid result
|
||||
let (address, index) = result.unwrap();
|
||||
assert_eq!(index, 20);
|
||||
assert_eq!(address, Address::from_slice(&data[..20]));
|
||||
} else {
|
||||
// Length of less than 32 should return error
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
#![no_main]
|
||||
#[macro_use] extern crate libfuzzer_sys;
|
||||
extern crate ethereum_types;
|
||||
extern crate ssz;
|
||||
|
||||
use ethereum_types::Address;
|
||||
use ssz::SszStream;
|
||||
|
||||
// Fuzz ssz_encode (via ssz_append)
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let mut ssz = SszStream::new();
|
||||
if data.len() >= 20 {
|
||||
let hash = Address::from_slice(&data[..20]);
|
||||
ssz.append(&hash);
|
||||
let ssz = ssz.drain();
|
||||
|
||||
assert_eq!(data[..20], ssz[..20]);
|
||||
assert_eq!(ssz.len(), 20);
|
||||
}
|
||||
});
|
28
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_bool_decode.rs
Normal file
28
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_bool_decode.rs
Normal file
@ -0,0 +1,28 @@
|
||||
#![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<(bool, usize), DecodeError> = Decodable::ssz_decode(data, 0);
|
||||
if data.len() >= 1 {
|
||||
// TODO: change to little endian bytes
|
||||
// https://github.com/sigp/lighthouse/issues/215
|
||||
if data[0] == u8::pow(2,7) {
|
||||
let (val_bool, index) = result.unwrap();
|
||||
assert!(val_bool);
|
||||
assert_eq!(index, 1);
|
||||
} else if data[0] == 0 {
|
||||
let (val_bool, index) = result.unwrap();
|
||||
assert!(!val_bool);
|
||||
assert_eq!(index, 1);
|
||||
} else {
|
||||
assert_eq!(result, Err(DecodeError::Invalid));
|
||||
}
|
||||
} else {
|
||||
// Length of 0 should return error
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
});
|
22
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_bool_encode.rs
Normal file
22
eth2/utils/ssz/fuzz/fuzz_targets/fuzz_target_bool_encode.rs
Normal file
@ -0,0 +1,22 @@
|
||||
#![no_main]
|
||||
#[macro_use] extern crate libfuzzer_sys;
|
||||
extern crate ssz;
|
||||
|
||||
use ssz::SszStream;
|
||||
|
||||
// Fuzz ssz_encode (via ssz_append)
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let mut ssz = SszStream::new();
|
||||
let mut val_bool = 0;
|
||||
if data.len() >= 1 {
|
||||
val_bool = data[0] % u8::pow(2, 6);
|
||||
}
|
||||
|
||||
ssz.append(&val_bool);
|
||||
let ssz = ssz.drain();
|
||||
|
||||
// TODO: change to little endian bytes
|
||||
// https://github.com/sigp/lighthouse/issues/215
|
||||
assert_eq!(val_bool, ssz[0] % u8::pow(2, 6));
|
||||
assert_eq!(ssz.len(), 1);
|
||||
});
|
@ -0,0 +1,21 @@
|
||||
#![no_main]
|
||||
#[macro_use] extern crate libfuzzer_sys;
|
||||
extern crate ethereum_types;
|
||||
extern crate ssz;
|
||||
|
||||
use ethereum_types::H256;
|
||||
use ssz::{DecodeError, Decodable};
|
||||
|
||||
// Fuzz ssz_decode()
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let result: Result<(H256, usize), DecodeError> = Decodable::ssz_decode(data, 0);
|
||||
if data.len() >= 32 {
|
||||
// Should have valid result
|
||||
let (hash, index) = result.unwrap();
|
||||
assert_eq!(index, 32);
|
||||
assert_eq!(hash, H256::from_slice(&data[..32]));
|
||||
} else {
|
||||
// Length of less than 32 should return error
|
||||
assert_eq!(result, Err(DecodeError::TooShort));
|
||||
}
|
||||
});
|
@ -0,0 +1,20 @@
|
||||
#![no_main]
|
||||
#[macro_use] extern crate libfuzzer_sys;
|
||||
extern crate ethereum_types;
|
||||
extern crate ssz;
|
||||
|
||||
use ethereum_types::H256;
|
||||
use ssz::SszStream;
|
||||
|
||||
// Fuzz ssz_encode (via ssz_append)
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
let mut ssz = SszStream::new();
|
||||
if data.len() >= 32 {
|
||||
let hash = H256::from_slice(&data[..32]);
|
||||
ssz.append(&hash);
|
||||
let ssz = ssz.drain();
|
||||
|
||||
assert_eq!(data[..32], ssz[..32]);
|
||||
assert_eq!(ssz.len(), 32);
|
||||
}
|
||||
});
|
Loading…
Reference in New Issue
Block a user