From f3f9141def4a6281452b087a6b57a2c2c026ffb7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 23 Jun 2024 17:15:39 -0700 Subject: add fuzz target for larger write helpers --- fuzz/fuzz_targets/write_helpers_are_correct.rs | 96 ++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 fuzz/fuzz_targets/write_helpers_are_correct.rs (limited to 'fuzz/fuzz_targets') diff --git a/fuzz/fuzz_targets/write_helpers_are_correct.rs b/fuzz/fuzz_targets/write_helpers_are_correct.rs new file mode 100644 index 0000000..41e27bd --- /dev/null +++ b/fuzz/fuzz_targets/write_helpers_are_correct.rs @@ -0,0 +1,96 @@ +#![no_main] +use libfuzzer_sys::fuzz_target; +use yaxpeax_arch::display::DisplaySink; + +use std::convert::TryInto; + +fuzz_target!(|data: &[u8]| { + let mut buf = String::new(); + match data.len() { + 1 => { + let i = data[0]; + + buf.clear(); + buf.write_u8(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u8(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i8) < 0 { + format!("-0x{:x}", (i as i8).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i8(i as i8).expect("write succeeds"); + assert_eq!(buf, expected); + }, + 2 => { + let i: u16 = u16::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u16(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u16(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i16) < 0 { + format!("-0x{:x}", (i as i16).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i16(i as i16).expect("write succeeds"); + assert_eq!(buf, expected); + } + 4 => { + let i: u32 = u32::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u32(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u32(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i32) < 0 { + format!("-0x{:x}", (i as i32).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i32(i as i32).expect("write succeeds"); + assert_eq!(buf, expected); + }, + 8 => { + let i: u64 = u64::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u64(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u64(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i64) < 0 { + format!("-0x{:x}", (i as i64).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i64(i as i64).expect("write succeeds"); + assert_eq!(buf, expected); + }, + _ => {} + } +}); -- cgit v1.1