|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +use anybytes::Bytes; |
| 4 | +use arbitrary::{Arbitrary, Result as ArbResult, Unstructured}; |
| 5 | +use libfuzzer_sys::fuzz_target; |
| 6 | + |
| 7 | +#[derive(Debug)] |
| 8 | +enum Operation { |
| 9 | + TakePrefix(u16), |
| 10 | + TakeSuffix(u16), |
| 11 | + PopFront, |
| 12 | + PopBack, |
| 13 | +} |
| 14 | + |
| 15 | +impl<'a> Arbitrary<'a> for Operation { |
| 16 | + fn arbitrary(u: &mut Unstructured<'a>) -> ArbResult<Self> { |
| 17 | + let tag = u.int_in_range::<u8>(0..=3)?; |
| 18 | + let op = match tag { |
| 19 | + 0 => Operation::TakePrefix(u.arbitrary()?), |
| 20 | + 1 => Operation::TakeSuffix(u.arbitrary()?), |
| 21 | + 2 => Operation::PopFront, |
| 22 | + _ => Operation::PopBack, |
| 23 | + }; |
| 24 | + Ok(op) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug)] |
| 29 | +struct FuzzCase { |
| 30 | + data: Vec<u8>, |
| 31 | + ops: Vec<Operation>, |
| 32 | +} |
| 33 | + |
| 34 | +impl<'a> Arbitrary<'a> for FuzzCase { |
| 35 | + fn arbitrary(u: &mut Unstructured<'a>) -> ArbResult<Self> { |
| 36 | + let len = u.int_in_range::<usize>(0..=64)?; |
| 37 | + let mut data = Vec::with_capacity(len); |
| 38 | + for _ in 0..len { |
| 39 | + data.push(u.arbitrary()?); |
| 40 | + } |
| 41 | + |
| 42 | + let ops_len = u.int_in_range::<usize>(0..=64)?; |
| 43 | + let mut ops = Vec::with_capacity(ops_len); |
| 44 | + for _ in 0..ops_len { |
| 45 | + ops.push(u.arbitrary()?); |
| 46 | + } |
| 47 | + |
| 48 | + Ok(Self { data, ops }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +fuzz_target!(|case: FuzzCase| { |
| 53 | + let mut bytes = Bytes::from(case.data.clone()); |
| 54 | + let mut model = case.data; |
| 55 | + |
| 56 | + for op in case.ops { |
| 57 | + match op { |
| 58 | + Operation::TakePrefix(n) => { |
| 59 | + let len = n as usize; |
| 60 | + let result = bytes.take_prefix(len); |
| 61 | + if len <= model.len() { |
| 62 | + let expected: Vec<u8> = model.drain(..len).collect(); |
| 63 | + let prefix = result.expect("prefix should exist"); |
| 64 | + assert_eq!(prefix.as_ref(), expected.as_slice()); |
| 65 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 66 | + } else { |
| 67 | + assert!(result.is_none()); |
| 68 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 69 | + } |
| 70 | + } |
| 71 | + Operation::TakeSuffix(n) => { |
| 72 | + let len = n as usize; |
| 73 | + let result = bytes.take_suffix(len); |
| 74 | + if len <= model.len() { |
| 75 | + let split = model.len() - len; |
| 76 | + let expected = model.split_off(split); |
| 77 | + let suffix = result.expect("suffix should exist"); |
| 78 | + assert_eq!(suffix.as_ref(), expected.as_slice()); |
| 79 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 80 | + } else { |
| 81 | + assert!(result.is_none()); |
| 82 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 83 | + } |
| 84 | + } |
| 85 | + Operation::PopFront => { |
| 86 | + let expected = if model.is_empty() { |
| 87 | + None |
| 88 | + } else { |
| 89 | + Some(model.remove(0)) |
| 90 | + }; |
| 91 | + let got = bytes.pop_front(); |
| 92 | + assert_eq!(got, expected); |
| 93 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 94 | + } |
| 95 | + Operation::PopBack => { |
| 96 | + let expected = model.pop(); |
| 97 | + let got = bytes.pop_back(); |
| 98 | + assert_eq!(got, expected); |
| 99 | + assert_eq!(bytes.as_ref(), model.as_slice()); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | +}); |
0 commit comments