From 17f5e2d7a13e840a83ad7a67642a77c23e9b987b Mon Sep 17 00:00:00 2001 From: Sam Gwilym Date: Fri, 12 Jul 2024 07:46:25 +0100 Subject: [PATCH] Add UintBE fuzz tests --- data-model/src/encoding/unsigned_int.rs | 4 ++ fuzz/Cargo.toml | 56 ++++++++++++++++++++++ fuzz/fuzz_targets/u16be_encoding.rs | 14 ++++++ fuzz/fuzz_targets/u16be_encoding_random.rs | 11 +++++ fuzz/fuzz_targets/u32be_encoding.rs | 14 ++++++ fuzz/fuzz_targets/u32be_encoding_random.rs | 11 +++++ fuzz/fuzz_targets/u64be_encoding.rs | 14 ++++++ fuzz/fuzz_targets/u64be_encoding_random.rs | 11 +++++ fuzz/fuzz_targets/u8be_encoding.rs | 14 ++++++ fuzz/fuzz_targets/u8be_encoding_random.rs | 11 +++++ 10 files changed, 160 insertions(+) create mode 100644 fuzz/fuzz_targets/u16be_encoding.rs create mode 100644 fuzz/fuzz_targets/u16be_encoding_random.rs create mode 100644 fuzz/fuzz_targets/u32be_encoding.rs create mode 100644 fuzz/fuzz_targets/u32be_encoding_random.rs create mode 100644 fuzz/fuzz_targets/u64be_encoding.rs create mode 100644 fuzz/fuzz_targets/u64be_encoding_random.rs create mode 100644 fuzz/fuzz_targets/u8be_encoding.rs create mode 100644 fuzz/fuzz_targets/u8be_encoding_random.rs diff --git a/data-model/src/encoding/unsigned_int.rs b/data-model/src/encoding/unsigned_int.rs index 019d219..e4e35f3 100644 --- a/data-model/src/encoding/unsigned_int.rs +++ b/data-model/src/encoding/unsigned_int.rs @@ -5,6 +5,7 @@ use crate::encoding::error::{DecodeError, EncodingConsumerError}; use crate::encoding::parameters::{Decoder, Encoder}; /// A `u8` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer. +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct U8BE(u8); impl Encoder for U8BE { @@ -46,6 +47,7 @@ impl From for u64 { } /// A `u16` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer. +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct U16BE(u16); impl Encoder for U16BE { @@ -87,6 +89,7 @@ impl From for u64 { } /// A `u32` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer. +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct U32BE(u32); impl Encoder for U32BE { @@ -128,6 +131,7 @@ impl From for u64 { } /// A `u64` wrapper that implements [`Encoding`] and [`Decoding`] by encoding as a big-endian fixed-width integer. +#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)] pub struct U64BE(u64); impl Encoder for U64BE { diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 39605c1..5177dd8 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -88,3 +88,59 @@ path = "fuzz_targets/entry_encoding_random.rs" test = false doc = false bench = false + +[[bin]] +name = "u8be_encoding" +path = "fuzz_targets/u8be_encoding.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u8be_encoding_random" +path = "fuzz_targets/u8be_encoding_random.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u16be_encoding" +path = "fuzz_targets/u16be_encoding.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u16be_encoding_random" +path = "fuzz_targets/u16be_encoding_random.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u32be_encoding" +path = "fuzz_targets/u32be_encoding.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u32be_encoding_random" +path = "fuzz_targets/u32be_encoding_random.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u64be_encoding" +path = "fuzz_targets/u64be_encoding.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "u64be_encoding_random" +path = "fuzz_targets/u64be_encoding_random.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/u16be_encoding.rs b/fuzz/fuzz_targets/u16be_encoding.rs new file mode 100644 index 0000000..48c929d --- /dev/null +++ b/fuzz/fuzz_targets/u16be_encoding.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use ufotofu::local_nb::consumer::TestConsumer; +use willow_data_model::encoding::unsigned_int::U16BE; +use willow_data_model_fuzz::encoding_roundtrip; + +fuzz_target!(|data: (u16, TestConsumer)| { + let (n, mut consumer) = data; + + smol::block_on(async { + encoding_roundtrip::<_, TestConsumer>(U16BE::from(n), &mut consumer).await; + }); +}); diff --git a/fuzz/fuzz_targets/u16be_encoding_random.rs b/fuzz/fuzz_targets/u16be_encoding_random.rs new file mode 100644 index 0000000..e8eb070 --- /dev/null +++ b/fuzz/fuzz_targets/u16be_encoding_random.rs @@ -0,0 +1,11 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use willow_data_model::encoding::unsigned_int::U16BE; +use willow_data_model_fuzz::encoding_random; + +fuzz_target!(|data: &[u8]| { + smol::block_on(async { + encoding_random::(data).await; + }); +}); diff --git a/fuzz/fuzz_targets/u32be_encoding.rs b/fuzz/fuzz_targets/u32be_encoding.rs new file mode 100644 index 0000000..fb85775 --- /dev/null +++ b/fuzz/fuzz_targets/u32be_encoding.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use ufotofu::local_nb::consumer::TestConsumer; +use willow_data_model::encoding::unsigned_int::U32BE; +use willow_data_model_fuzz::encoding_roundtrip; + +fuzz_target!(|data: (u32, TestConsumer)| { + let (n, mut consumer) = data; + + smol::block_on(async { + encoding_roundtrip::<_, TestConsumer>(U32BE::from(n), &mut consumer).await; + }); +}); diff --git a/fuzz/fuzz_targets/u32be_encoding_random.rs b/fuzz/fuzz_targets/u32be_encoding_random.rs new file mode 100644 index 0000000..0dfe412 --- /dev/null +++ b/fuzz/fuzz_targets/u32be_encoding_random.rs @@ -0,0 +1,11 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use willow_data_model::encoding::unsigned_int::U32BE; +use willow_data_model_fuzz::encoding_random; + +fuzz_target!(|data: &[u8]| { + smol::block_on(async { + encoding_random::(data).await; + }); +}); diff --git a/fuzz/fuzz_targets/u64be_encoding.rs b/fuzz/fuzz_targets/u64be_encoding.rs new file mode 100644 index 0000000..a04e134 --- /dev/null +++ b/fuzz/fuzz_targets/u64be_encoding.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use ufotofu::local_nb::consumer::TestConsumer; +use willow_data_model::encoding::unsigned_int::U64BE; +use willow_data_model_fuzz::encoding_roundtrip; + +fuzz_target!(|data: (u64, TestConsumer)| { + let (n, mut consumer) = data; + + smol::block_on(async { + encoding_roundtrip::<_, TestConsumer>(U64BE::from(n), &mut consumer).await; + }); +}); diff --git a/fuzz/fuzz_targets/u64be_encoding_random.rs b/fuzz/fuzz_targets/u64be_encoding_random.rs new file mode 100644 index 0000000..1393fe0 --- /dev/null +++ b/fuzz/fuzz_targets/u64be_encoding_random.rs @@ -0,0 +1,11 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use willow_data_model::encoding::unsigned_int::U64BE; +use willow_data_model_fuzz::encoding_random; + +fuzz_target!(|data: &[u8]| { + smol::block_on(async { + encoding_random::(data).await; + }); +}); diff --git a/fuzz/fuzz_targets/u8be_encoding.rs b/fuzz/fuzz_targets/u8be_encoding.rs new file mode 100644 index 0000000..606d46b --- /dev/null +++ b/fuzz/fuzz_targets/u8be_encoding.rs @@ -0,0 +1,14 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use ufotofu::local_nb::consumer::TestConsumer; +use willow_data_model::encoding::unsigned_int::U8BE; +use willow_data_model_fuzz::encoding_roundtrip; + +fuzz_target!(|data: (u8, TestConsumer)| { + let (n, mut consumer) = data; + + smol::block_on(async { + encoding_roundtrip::<_, TestConsumer>(U8BE::from(n), &mut consumer).await; + }); +}); diff --git a/fuzz/fuzz_targets/u8be_encoding_random.rs b/fuzz/fuzz_targets/u8be_encoding_random.rs new file mode 100644 index 0000000..120295a --- /dev/null +++ b/fuzz/fuzz_targets/u8be_encoding_random.rs @@ -0,0 +1,11 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use willow_data_model::encoding::unsigned_int::U8BE; +use willow_data_model_fuzz::encoding_random; + +fuzz_target!(|data: &[u8]| { + smol::block_on(async { + encoding_random::(data).await; + }); +});