aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_targets
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2024-06-23 17:15:39 -0700
committeriximeow <me@iximeow.net>2024-06-23 17:15:39 -0700
commitf3f9141def4a6281452b087a6b57a2c2c026ffb7 (patch)
tree92e19ec199d2557860ee6153cc4f92b89ec47cd1 /fuzz/fuzz_targets
parent9402dc008deaf993a3ac969f56af772cc545b8e8 (diff)
add fuzz target for larger write helpers
Diffstat (limited to 'fuzz/fuzz_targets')
-rw-r--r--fuzz/fuzz_targets/write_helpers_are_correct.rs96
1 files changed, 96 insertions, 0 deletions
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);
+ },
+ _ => {}
+ }
+});