diff --git a/Cargo.lock b/Cargo.lock index 35cc40bd78..06c10887df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3813,9 +3813,13 @@ name = "nimiq-fuzz" version = "1.0.0-rc.0" dependencies = [ "afl", + "nimiq-account", + "nimiq-bls", "nimiq-collections", + "nimiq-keys", "nimiq-primitives", "nimiq-serde", + "nimiq-transaction", ] [[package]] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 07b94fccec..eb0747a800 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -16,9 +16,13 @@ workspace = true [dependencies] afl = { version = "0.15.11", optional = true } +nimiq-account = { workspace = true } +nimiq-bls = { workspace = true } nimiq-collections = { workspace = true } +nimiq-keys = { workspace = true } nimiq-primitives = { workspace = true, features = ["key-nibbles", "serde-derive", "trie"] } nimiq-serde = { workspace = true } +nimiq-transaction = { workspace = true } [features] fuzz = ["afl"] diff --git a/fuzz/in/keypair/examplekeys.bin b/fuzz/in/keypair/examplekeys.bin new file mode 100644 index 0000000000..67de4825cc Binary files /dev/null and b/fuzz/in/keypair/examplekeys.bin differ diff --git a/fuzz/in/normal_keypair/examplekeys.bin b/fuzz/in/normal_keypair/examplekeys.bin new file mode 100644 index 0000000000..67de4825cc Binary files /dev/null and b/fuzz/in/normal_keypair/examplekeys.bin differ diff --git a/fuzz/in/staking_contract/example_contract b/fuzz/in/staking_contract/example_contract new file mode 100644 index 0000000000..5bbb39a690 Binary files /dev/null and b/fuzz/in/staking_contract/example_contract differ diff --git a/fuzz/src/bin/bitset.rs b/fuzz/src/bin/bitset.rs index 1ce9a79a71..f4fdd5534f 100644 --- a/fuzz/src/bin/bitset.rs +++ b/fuzz/src/bin/bitset.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_collections::BitSet; use nimiq_serde::Deserialize as _; - let _ = BitSet::deserialize_from_vec(data); }) } diff --git a/fuzz/src/bin/coin.rs b/fuzz/src/bin/coin.rs new file mode 100644 index 0000000000..3332ffc860 --- /dev/null +++ b/fuzz/src/bin/coin.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_primitives::coin::Coin; + let _ = Coin::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/htlc.rs b/fuzz/src/bin/htlc.rs new file mode 100644 index 0000000000..5cc5d319c3 --- /dev/null +++ b/fuzz/src/bin/htlc.rs @@ -0,0 +1,10 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_account::{ + HashedTimeLockedContract + }; + let _ = HashedTimeLockedContract::deserialize_from_vec(data); + }) +} \ No newline at end of file diff --git a/fuzz/src/bin/key_nibbles.rs b/fuzz/src/bin/key_nibbles.rs index ad694d570a..263c21e16a 100644 --- a/fuzz/src/bin/key_nibbles.rs +++ b/fuzz/src/bin/key_nibbles.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_primitives::key_nibbles::KeyNibbles; use nimiq_serde::Deserialize as _; - let _ = KeyNibbles::deserialize_from_vec(data); }) } diff --git a/fuzz/src/bin/keypair.rs b/fuzz/src/bin/keypair.rs new file mode 100644 index 0000000000..1dbfc594f4 --- /dev/null +++ b/fuzz/src/bin/keypair.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_bls::{KeyPair}; + let _ = KeyPair::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/normal_keypair.rs b/fuzz/src/bin/normal_keypair.rs new file mode 100644 index 0000000000..f04e9a82b3 --- /dev/null +++ b/fuzz/src/bin/normal_keypair.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_keys::KeyPair; + let _ = KeyPair::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/staking_contract.rs b/fuzz/src/bin/staking_contract.rs new file mode 100644 index 0000000000..84b0e22437 --- /dev/null +++ b/fuzz/src/bin/staking_contract.rs @@ -0,0 +1,21 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::{Deserialize, Serialize}; + use nimiq_account::StakingContract; + let res = StakingContract::deserialize_from_vec(data); // err I think is of type DeserializeError + // Now check if contract exists. If it does (aka. the original data was a valid staking contract) then try to serialize it back to a vector, then check if the original vector and the new vector are the same, if they aren't then there is a bug in the parsing logic. + // The existence of error implies that contract does not exist. + match res { + Ok(v) => { + let serialized = StakingContract::serialize_to_vec(&v); + assert!((serialized.len() <= data.len()), "The size of the serialized version was bigger than the original vector! This shouldn't happen!"); + let original_data_segment: &[u8] = data[..(serialized.len())].try_into().unwrap(); // This ugly stuff has to be done, because the serialization function ignores extra bytes at the end so we can not compare the byte vectors by themselves. Yuck!!! + assert_eq!(original_data_segment, serialized); + }, + Err(e) => { + return; + }, + } + }) +} \ No newline at end of file diff --git a/fuzz/src/bin/transaction.rs b/fuzz/src/bin/transaction.rs new file mode 100644 index 0000000000..4af537c0cd --- /dev/null +++ b/fuzz/src/bin/transaction.rs @@ -0,0 +1,8 @@ +fn main() { + #[cfg(feature = "fuzz")] + afl::fuzz!(|data: &[u8]| { + use nimiq_serde::Deserialize as _; + use nimiq_transaction::{Transaction}; + let _ = Transaction::deserialize_from_vec(data); + }) +} diff --git a/fuzz/src/bin/trie_node.rs b/fuzz/src/bin/trie_node.rs index dab2f0c23a..b727628b50 100644 --- a/fuzz/src/bin/trie_node.rs +++ b/fuzz/src/bin/trie_node.rs @@ -3,7 +3,6 @@ fn main() { afl::fuzz!(|data: &[u8]| { use nimiq_primitives::trie::trie_node::TrieNode; use nimiq_serde::Deserialize as _; - let _ = TrieNode::deserialize_from_vec(data); }) }