| 1 |
//! Testing utilities for testing `fdeflate::Decompressor`. |
| 2 |
//! |
| 3 |
//! These utilities are used by: |
| 4 |
//! |
| 5 |
//! * Unit tests (e.g. `#[test]` tests in `src/decompress.rs`) |
| 6 |
//! * Fuzzers (e.g. `fuzz/fuzz_targets/inflate_bytewise3.rs`) |
| 7 |
|
| 8 |
#[cfg(test)] |
| 9 |
use crate as fdeflate; |
| 10 |
|
| 11 |
use fdeflate::{DecompressionError, Decompressor}; |
| 12 |
|
| 13 |
#[derive(Debug, PartialEq)] |
| 14 |
pub enum TestDecompressionError { |
| 15 |
ProdError(DecompressionError), |
| 16 |
TestError(TestErrorKind), |
| 17 |
} |
| 18 |
|
| 19 |
#[derive(Debug, Eq, PartialEq)] |
| 20 |
pub enum TestErrorKind { |
| 21 |
OutputTooLarge, |
| 22 |
TooManyIterations, |
| 23 |
} |
| 24 |
|
| 25 |
impl From<DecompressionError> for TestDecompressionError { |
| 26 |
fn from(e: DecompressionError) -> Self { |
| 27 |
Self::ProdError(e) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
impl From<TestErrorKind> for TestDecompressionError { |
| 32 |
fn from(kind: TestErrorKind) -> Self { |
| 33 |
Self::TestError(kind) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/// Decompresses `input` when feeding it into a `Decompressor::read` in `chunks`. |
| 38 |
/// |
| 39 |
/// `chunks` typically can be used to decode the whole input at once (setting `chunks` to |
| 40 |
/// `vec![input.len]`) or byte-by-byte (setting `chunks` to `std::iter::repeat(1)`). |
| 41 |
/// But `chunks` can also be used to replicate arbitrary chunking patterns (such as may be |
| 42 |
/// used by some fuzzing-based repros from the `png` crate). |
| 43 |
/// |
| 44 |
/// `early_eof` is used to the last `end_of_input` argument of `Decompressor::read` calls. |
| 45 |
/// When `early_eof` is `false`, then `end_of_input` is `false` until the whole input is |
| 46 |
/// consumed (and then is `Decompressor::is_done` is still false, then `Decompressor::read` |
| 47 |
/// is called one or more times with empty input slice and `end_of_input` set to true). |
| 48 |
/// When `early_eof` is `true` then `end_of_input` is set to `true` as soon as the slice |
| 49 |
/// fed to `Decompressor::read` "reaches" the end of the whole input. |
| 50 |
/// |
| 51 |
/// Unlike the `png` crate, this testing helper uses a big, fixed-size output buffer. |
| 52 |
/// (i.e. there is no simulation of `ZlibStream.compact_out_buffer_if_needed` from the `png` |
| 53 |
/// crate). |
| 54 |
pub fn decompress_by_chunks( |
| 55 |
input: &[u8], |
| 56 |
chunks: impl IntoIterator<Item = usize>, |
| 57 |
early_eof: bool, |
| 58 |
) -> Result<Vec<u8>, TestDecompressionError> { |
| 59 |
let mut chunks = chunks.into_iter(); |
| 60 |
|
| 61 |
// `iteration_counter` helps to prevent infinite loops (which may happen with `chunks` such |
| 62 |
// as `std::iter::repeat(0)`). |
| 63 |
let mut iteration_counter = 0; |
| 64 |
|
| 65 |
// Ignoring checksums so that we can work with inputs generated by fuzzing. (Fuzzing |
| 66 |
// typically ignores checksums to make it easier to explore the space of possible inputs.) |
| 67 |
let mut d = Decompressor::new(); |
| 68 |
d.ignore_adler32(); |
| 69 |
|
| 70 |
let mut out_buf = vec![0; 1_000_000]; |
| 71 |
let mut in_pos = 0; |
| 72 |
let mut out_pos = 0; |
| 73 |
while !d.is_done() { |
| 74 |
iteration_counter += 1; |
| 75 |
if iteration_counter > 5000 { |
| 76 |
return Err(TestErrorKind::TooManyIterations.into()); |
| 77 |
} |
| 78 |
|
| 79 |
let chunk_size = chunks.next().unwrap_or(0); |
| 80 |
let start = in_pos; |
| 81 |
let end = std::cmp::min(start + chunk_size, input.len()); |
| 82 |
|
| 83 |
let eof = if early_eof { |
| 84 |
end == input.len() |
| 85 |
} else { |
| 86 |
start == input.len() |
| 87 |
}; |
| 88 |
|
| 89 |
let (in_consumed, out_written) = |
| 90 |
d.read(&input[start..end], out_buf.as_mut_slice(), out_pos, eof)?; |
| 91 |
|
| 92 |
in_pos += in_consumed; |
| 93 |
out_pos += out_written; |
| 94 |
if out_pos == out_buf.len() && in_consumed == 0 && !d.is_done() { |
| 95 |
return Err(TestErrorKind::OutputTooLarge.into()); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
out_buf.resize(out_pos, 0xFF); |
| 100 |
Ok(out_buf) |
| 101 |
} |