/src/image-png/fuzz/fuzz_targets/buf_independent.rs
Line | Count | Source |
1 | | //! This fuzzer tests that decoding results are the same regardless of the |
2 | | //! details of how the `Read` trait exposes the underlying input via |
3 | | //! the `fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize>` |
4 | | //! method: |
5 | | //! |
6 | | //! * Whole slice - `impl Read for &[u8]`: |
7 | | //! - The whole slice is available for reading (if it fits into `buf`) |
8 | | //! - No IO errors are expected |
9 | | //! - Motivation: This is the baseline |
10 | | //! * Byte-by-byte - `SmalBuf<R>`: |
11 | | //! - At most 1 byte can be read in a single call to `read` |
12 | | //! - Motivation: Testing that decoding works regardless of how the input is split |
13 | | //! into multiple `read` calls. (The test checks every possible `read` boundary in the input |
14 | | //! buffer, even though in practice file or network buffers would split the input into only a |
15 | | //! handful of chunks.) |
16 | | //! * TODO: Intermittent EOFs: |
17 | | //! - Intermittently `read` report 0 available bytes. |
18 | | //! - Still no IO errors at the `Read` trait level |
19 | | //! - Motivation: Testing support for decoding a streaming or partial input |
20 | | //! (i.e. scenarios where initially only the first few interlaced passes |
21 | | //! can be decoded, and where decoding is resumed after getting more complete |
22 | | //! input). |
23 | | |
24 | | #![no_main] |
25 | | |
26 | | use libfuzzer_sys::fuzz_target; |
27 | | |
28 | | use std::fmt::Debug; |
29 | | use std::io::{BufRead, BufReader, Cursor}; |
30 | | |
31 | | mod smal_buf { |
32 | | use std::io::{BufRead, Cursor, Read}; |
33 | | |
34 | | /// A reader that returns at most 1 byte in a single call to `read`. |
35 | | pub struct SmalBuf { |
36 | | inner: Cursor<Vec<u8>>, |
37 | | } |
38 | | |
39 | | impl SmalBuf { |
40 | 16.0k | pub fn new(inner: Vec<u8>) -> Self { |
41 | 16.0k | SmalBuf { |
42 | 16.0k | inner: Cursor::new(inner), |
43 | 16.0k | } |
44 | 16.0k | } |
45 | | } |
46 | | |
47 | | impl Read for SmalBuf { |
48 | 22.7M | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
49 | 22.7M | if buf.is_empty() { |
50 | 0 | return Ok(0); |
51 | 22.7M | } |
52 | 22.7M | self.inner.read(&mut buf[..1]) |
53 | 22.7M | } |
54 | | } |
55 | | impl BufRead for SmalBuf { |
56 | 0 | fn fill_buf(&mut self) -> std::io::Result<&[u8]> { |
57 | 0 | let buf = self.inner.fill_buf()?; |
58 | 0 | Ok(&buf[..buf.len().min(1)]) |
59 | 0 | } |
60 | | |
61 | 0 | fn consume(&mut self, amt: usize) { |
62 | 0 | self.inner.consume(amt); |
63 | 0 | } |
64 | | } |
65 | | } |
66 | | |
67 | | mod intermittent_eofs { |
68 | | |
69 | | use std::cell::Cell; |
70 | | use std::io::{BufRead, Read}; |
71 | | use std::rc::Rc; |
72 | | |
73 | | /// A reader that returns `std::io::ErrorKind::UnexpectedEof` errors in every other read. |
74 | | /// EOFs can be temporarily disabled and re-enabled later using the associated `EofController`. |
75 | | pub struct IntermittentEofs<R: BufRead> { |
76 | | inner: R, |
77 | | |
78 | | /// Controls whether intermittent EOFs happen at all. |
79 | | controller: Rc<EofController>, |
80 | | |
81 | | /// Controls whether an intermittent EOF will happen during the next `read` |
82 | | /// (when enabled, intermittent EOFs happen every other `read`). |
83 | | eof_soon: bool, |
84 | | } |
85 | | |
86 | | impl<R: BufRead> IntermittentEofs<R> { |
87 | 8.03k | pub fn new(inner: R) -> Self { |
88 | 8.03k | Self { |
89 | 8.03k | inner, |
90 | 8.03k | controller: Rc::new(EofController::new()), |
91 | 8.03k | eof_soon: true, |
92 | 8.03k | } |
93 | 8.03k | } |
94 | | |
95 | 8.03k | pub fn controller(&self) -> Rc<EofController> { |
96 | 8.03k | self.controller.clone() |
97 | 8.03k | } |
98 | | } |
99 | | |
100 | | impl<R: BufRead> Read for IntermittentEofs<R> { |
101 | 15.4M | fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { |
102 | 15.4M | if self.controller.are_intermittent_eofs_enabled() && self.eof_soon { |
103 | 4.74M | self.eof_soon = false; |
104 | 4.74M | return Ok(0); |
105 | 10.6M | } |
106 | | |
107 | 10.6M | self.eof_soon = true; |
108 | 10.6M | let inner_result = self.inner.read(buf); |
109 | 10.6M | if let Ok(0) = &inner_result { |
110 | 451k | self.controller.mark_inner_eof(); |
111 | 10.2M | } |
112 | | |
113 | 10.6M | inner_result |
114 | 15.4M | } |
115 | | } |
116 | | impl<R: BufRead> BufRead for IntermittentEofs<R> { |
117 | 0 | fn fill_buf(&mut self) -> std::io::Result<&[u8]> { |
118 | 0 | self.inner.fill_buf() |
119 | 0 | } |
120 | | |
121 | 0 | fn consume(&mut self, amt: usize) { |
122 | 0 | self.inner.consume(amt); |
123 | 0 | } |
124 | | } |
125 | | |
126 | | pub struct EofController { |
127 | | are_intermittent_eofs_enabled: Cell<bool>, |
128 | | did_reach_inner_eof: Cell<bool>, |
129 | | } |
130 | | |
131 | | impl EofController { |
132 | 8.03k | fn new() -> Self { |
133 | 8.03k | Self { |
134 | 8.03k | are_intermittent_eofs_enabled: Cell::new(true), |
135 | 8.03k | did_reach_inner_eof: Cell::new(false), |
136 | 8.03k | } |
137 | 8.03k | } |
138 | | |
139 | 4.62k | pub fn enable_intermittent_eofs(&self) { |
140 | 4.62k | self.are_intermittent_eofs_enabled.set(true); |
141 | 4.62k | } |
142 | | |
143 | 8.03k | pub fn disable_intermittent_eofs(&self) { |
144 | 8.03k | self.are_intermittent_eofs_enabled.set(false); |
145 | 8.03k | } |
146 | | |
147 | 15.4M | fn are_intermittent_eofs_enabled(&self) -> bool { |
148 | 15.4M | self.are_intermittent_eofs_enabled.get() |
149 | 15.4M | } |
150 | | |
151 | 451k | fn mark_inner_eof(&self) { |
152 | 451k | self.did_reach_inner_eof.set(true); |
153 | 451k | } |
154 | | |
155 | 4.15M | pub fn did_reach_inner_eof(&self) -> bool { |
156 | 4.15M | self.did_reach_inner_eof.get() |
157 | 4.15M | } |
158 | | } |
159 | | } |
160 | | |
161 | | fuzz_target!(|data: &[u8]| { |
162 | | let _ = test_data(data); |
163 | | }); |
164 | | |
165 | | #[inline(always)] |
166 | 8.03k | fn test_data<'a>(data: &'a [u8]) -> Result<(), ()> { |
167 | 8.03k | let baseline_reader = Box::new(Cursor::new(data)); |
168 | 8.03k | let byte_by_byte_reader = Box::new(smal_buf::SmalBuf::new(data.to_owned())); |
169 | 8.03k | let intermittent_eofs_reader = Box::new(intermittent_eofs::IntermittentEofs::new( |
170 | 8.03k | smal_buf::SmalBuf::new(data.to_owned()), |
171 | | )); |
172 | 8.03k | let intermittent_eofs_controller = intermittent_eofs_reader.controller(); |
173 | | |
174 | | // `Decoder` used to internally wrap the provided reader with a `BufReader`. Now that it has |
175 | | // been removed, fuzzing would be far too slow if we didn't use a BufReader here. |
176 | 8.03k | let data_readers: Vec<BufReader<Box<dyn BufRead>>> = vec![ |
177 | 8.03k | BufReader::new(baseline_reader), |
178 | 8.03k | BufReader::new(byte_by_byte_reader), |
179 | 8.03k | BufReader::new(intermittent_eofs_reader), |
180 | | ]; |
181 | | |
182 | 8.03k | let decoders = data_readers |
183 | 8.03k | .into_iter() |
184 | 24.0k | .map(|data_reader| { |
185 | | // Small limits, we don't need them hopefully. |
186 | 24.0k | let limits = png::Limits { bytes: 1 << 16 }; |
187 | 24.0k | png::Decoder::new_with_limits(data_reader, limits) |
188 | 24.0k | }) |
189 | 8.03k | .collect::<Vec<_>>(); |
190 | | |
191 | | // `Decoder.read_info` consumes `self` and is therefore not resumable. To work around that |
192 | | // let's temporarily disable intermittent EOFs: |
193 | 8.03k | intermittent_eofs_controller.disable_intermittent_eofs(); |
194 | 8.03k | let mut png_readers = decoders |
195 | 8.03k | .into_iter() |
196 | 24.0k | .map(|decoder| decoder.read_info()) |
197 | 8.03k | .assert_all_results_are_consistent() |
198 | 8.03k | .collect::<Result<Vec<_>, _>>() |
199 | 8.03k | .map_err(|_| ())?; |
200 | 4.62k | intermittent_eofs_controller.enable_intermittent_eofs(); |
201 | | |
202 | 4.62k | let info = png_readers |
203 | 4.62k | .iter() |
204 | 13.8k | .map(|r| r.info().clone()) |
205 | 9.24k | .assert_all_items_are_same(|lhs: &png::Info, rhs: &png::Info| { |
206 | 9.24k | assert_same_info(lhs, rhs); |
207 | | |
208 | | // The assert below is somewhat redundant, but we use `raw_bytes` |
209 | | // later on, so let's double-check that it's the same everywhere. |
210 | 9.24k | assert_eq!(lhs.raw_bytes(), rhs.raw_bytes()); |
211 | 9.24k | }); |
212 | 4.62k | if info.raw_bytes() > 5_000_000 { |
213 | 211 | return Err(()); |
214 | 4.41k | } |
215 | | |
216 | 4.41k | let mut buffers = vec![vec![0; info.raw_bytes()]; png_readers.len()]; |
217 | | loop { |
218 | 4.41k | let output_infos = png_readers |
219 | 4.41k | .iter_mut() |
220 | 4.41k | .zip(buffers.iter_mut()) |
221 | 4.41k | .enumerate() |
222 | 13.2k | .map(|(i, (png_reader, buffer))| { |
223 | 13.2k | let eof_controller = if i == 2 { |
224 | 4.41k | Some(&intermittent_eofs_controller) |
225 | | } else { |
226 | 8.82k | None |
227 | | }; |
228 | 4.16M | retry_after_eofs(eof_controller, || { |
229 | 4.16M | png_reader.next_frame(buffer.as_mut_slice()) |
230 | 4.16M | .and_then(|_| png_reader.finish()) |
231 | 4.16M | }) |
232 | 13.2k | }) |
233 | 4.41k | .assert_all_results_are_consistent() |
234 | 4.41k | .collect::<Result<Vec<_>, _>>() |
235 | 4.41k | .map_err(|_| ())?; |
236 | 0 | output_infos.into_iter().assert_all_items_are_equal(); |
237 | 0 | buffers.iter().assert_all_items_are_equal(); |
238 | | } |
239 | 8.03k | } |
240 | | |
241 | 13.2k | fn retry_after_eofs<T>( |
242 | 13.2k | eof_controller: Option<&std::rc::Rc<intermittent_eofs::EofController>>, |
243 | 13.2k | mut f: impl FnMut() -> Result<T, png::DecodingError>, |
244 | 13.2k | ) -> Result<T, png::DecodingError> { |
245 | | loop { |
246 | 4.16M | let result = f(); |
247 | 4.16M | match result.as_ref() { |
248 | 4.15M | Err(png::DecodingError::IoError(e)) => { |
249 | 4.15M | if e.kind() == std::io::ErrorKind::UnexpectedEof { |
250 | 4.15M | if let Some(ctrl) = eof_controller { |
251 | 4.15M | if !ctrl.did_reach_inner_eof() { |
252 | 4.14M | continue; |
253 | 2.68k | } |
254 | 6.02k | } |
255 | 0 | } |
256 | | } |
257 | 4.53k | _ => (), |
258 | | } |
259 | 13.2k | break result; |
260 | | } |
261 | 13.2k | } |
262 | | |
263 | 9.24k | fn assert_same_info(lhs: &png::Info, rhs: &png::Info) { |
264 | | // Check that all decoders report the same `IHDR` fields. |
265 | 9.24k | assert_eq!(lhs.width, rhs.width); |
266 | 9.24k | assert_eq!(lhs.height, rhs.height); |
267 | 9.24k | assert_eq!(lhs.bit_depth, rhs.bit_depth); |
268 | 9.24k | assert_eq!(lhs.color_type, rhs.color_type); |
269 | 9.24k | assert_eq!(lhs.interlaced, rhs.interlaced); |
270 | | |
271 | | // Check all other `Info` fields that implement `Eq`. |
272 | 9.24k | assert_eq!(lhs.chrm_chunk, rhs.chrm_chunk); |
273 | 9.24k | assert_eq!(lhs.gama_chunk, rhs.gama_chunk); |
274 | 9.24k | assert_eq!(lhs.icc_profile, rhs.icc_profile); |
275 | 9.24k | assert_eq!(lhs.palette, rhs.palette); |
276 | 9.24k | assert_eq!(lhs.source_chromaticities, rhs.source_chromaticities); |
277 | 9.24k | assert_eq!(lhs.source_gamma, rhs.source_gamma); |
278 | 9.24k | assert_eq!(lhs.srgb, rhs.srgb); |
279 | 9.24k | assert_eq!(lhs.trns, rhs.trns); |
280 | 9.24k | } |
281 | | |
282 | | trait IteratorExtensionsForFuzzing: Iterator + Sized { |
283 | | /// Verifies that either 1) all items in the iterator are `Ok(_)` or 2) all items in the |
284 | | /// iterator are `Err(_)`. Passes through unmodified iterator items. |
285 | 12.4k | fn assert_all_results_are_consistent<T>(self) -> impl Iterator<Item = Self::Item> |
286 | 12.4k | where |
287 | 12.4k | Self: Iterator<Item = Result<T, png::DecodingError>>, |
288 | | { |
289 | | // Eagerly collect all the items - this makes sure we check consistency of *all* results, |
290 | | // even if a downstream iterator combinator consumes items lazily and never "pumps" some |
291 | | // items via `next`. (`iter.take(2)` is one example of such lazy consumer; |
292 | | // `iter_of_results.collect::<Result<Vec<_>, _>>()` is another.) |
293 | 12.4k | let all_results = self.collect::<Vec<_>>(); |
294 | | |
295 | 21.6k | let any_err = all_results.iter().any(|res| res.is_err()); <core::iter::adapters::map::Map<core::iter::adapters::enumerate::Enumerate<core::iter::adapters::zip::Zip<core::slice::iter::IterMut<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, core::slice::iter::IterMut<alloc::vec::Vec<u8>>>>, buf_independent::test_data::{closure#5}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<()>::{closure#0}Line | Count | Source | 295 | 4.41k | let any_err = all_results.iter().any(|res| res.is_err()); |
<core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<png::decoder::Decoder<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#1}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>::{closure#0}Line | Count | Source | 295 | 17.2k | let any_err = all_results.iter().any(|res| res.is_err()); |
|
296 | 28.0k | let any_ok = all_results.iter().any(|res| res.is_ok()); <core::iter::adapters::map::Map<core::iter::adapters::enumerate::Enumerate<core::iter::adapters::zip::Zip<core::slice::iter::IterMut<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, core::slice::iter::IterMut<alloc::vec::Vec<u8>>>>, buf_independent::test_data::{closure#5}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<()>::{closure#1}Line | Count | Source | 296 | 13.2k | let any_ok = all_results.iter().any(|res| res.is_ok()); |
<core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<png::decoder::Decoder<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#1}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>::{closure#1}Line | Count | Source | 296 | 14.8k | let any_ok = all_results.iter().any(|res| res.is_ok()); |
|
297 | 12.4k | if any_err && any_ok { |
298 | | // Replacing `Self::Item` with an "ok" string, because we want to support items |
299 | | // that do not implement `Debug`. |
300 | 0 | let printable_results = all_results.iter().map(|res| res.as_ref().map(|_| "ok")); Unexecuted instantiation: <core::iter::adapters::map::Map<core::iter::adapters::enumerate::Enumerate<core::iter::adapters::zip::Zip<core::slice::iter::IterMut<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, core::slice::iter::IterMut<alloc::vec::Vec<u8>>>>, buf_independent::test_data::{closure#5}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<()>::{closure#2}Unexecuted instantiation: <core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<png::decoder::Decoder<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#1}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>::{closure#2} |
301 | 0 | for (i, res) in printable_results.enumerate() { |
302 | 0 | eprintln!("Result #{i}: {res:?}"); |
303 | 0 | } |
304 | 0 | panic!("Inconsistent results - some are Ok(_) and some are Err(_)"); |
305 | 12.4k | } |
306 | | |
307 | 12.4k | all_results.into_iter() |
308 | 12.4k | } <core::iter::adapters::map::Map<core::iter::adapters::enumerate::Enumerate<core::iter::adapters::zip::Zip<core::slice::iter::IterMut<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, core::slice::iter::IterMut<alloc::vec::Vec<u8>>>>, buf_independent::test_data::{closure#5}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<()>Line | Count | Source | 285 | 4.41k | fn assert_all_results_are_consistent<T>(self) -> impl Iterator<Item = Self::Item> | 286 | 4.41k | where | 287 | 4.41k | Self: Iterator<Item = Result<T, png::DecodingError>>, | 288 | | { | 289 | | // Eagerly collect all the items - this makes sure we check consistency of *all* results, | 290 | | // even if a downstream iterator combinator consumes items lazily and never "pumps" some | 291 | | // items via `next`. (`iter.take(2)` is one example of such lazy consumer; | 292 | | // `iter_of_results.collect::<Result<Vec<_>, _>>()` is another.) | 293 | 4.41k | let all_results = self.collect::<Vec<_>>(); | 294 | | | 295 | 4.41k | let any_err = all_results.iter().any(|res| res.is_err()); | 296 | 4.41k | let any_ok = all_results.iter().any(|res| res.is_ok()); | 297 | 4.41k | if any_err && any_ok { | 298 | | // Replacing `Self::Item` with an "ok" string, because we want to support items | 299 | | // that do not implement `Debug`. | 300 | 0 | let printable_results = all_results.iter().map(|res| res.as_ref().map(|_| "ok")); | 301 | 0 | for (i, res) in printable_results.enumerate() { | 302 | 0 | eprintln!("Result #{i}: {res:?}"); | 303 | 0 | } | 304 | 0 | panic!("Inconsistent results - some are Ok(_) and some are Err(_)"); | 305 | 4.41k | } | 306 | | | 307 | 4.41k | all_results.into_iter() | 308 | 4.41k | } |
<core::iter::adapters::map::Map<alloc::vec::into_iter::IntoIter<png::decoder::Decoder<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#1}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_results_are_consistent::<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>Line | Count | Source | 285 | 8.03k | fn assert_all_results_are_consistent<T>(self) -> impl Iterator<Item = Self::Item> | 286 | 8.03k | where | 287 | 8.03k | Self: Iterator<Item = Result<T, png::DecodingError>>, | 288 | | { | 289 | | // Eagerly collect all the items - this makes sure we check consistency of *all* results, | 290 | | // even if a downstream iterator combinator consumes items lazily and never "pumps" some | 291 | | // items via `next`. (`iter.take(2)` is one example of such lazy consumer; | 292 | | // `iter_of_results.collect::<Result<Vec<_>, _>>()` is another.) | 293 | 8.03k | let all_results = self.collect::<Vec<_>>(); | 294 | | | 295 | 8.03k | let any_err = all_results.iter().any(|res| res.is_err()); | 296 | 8.03k | let any_ok = all_results.iter().any(|res| res.is_ok()); | 297 | 8.03k | if any_err && any_ok { | 298 | | // Replacing `Self::Item` with an "ok" string, because we want to support items | 299 | | // that do not implement `Debug`. | 300 | 0 | let printable_results = all_results.iter().map(|res| res.as_ref().map(|_| "ok")); | 301 | 0 | for (i, res) in printable_results.enumerate() { | 302 | 0 | eprintln!("Result #{i}: {res:?}"); | 303 | 0 | } | 304 | 0 | panic!("Inconsistent results - some are Ok(_) and some are Err(_)"); | 305 | 8.03k | } | 306 | | | 307 | 8.03k | all_results.into_iter() | 308 | 8.03k | } |
|
309 | | |
310 | | /// Verifies that all items in the iterator are the same (according to their `Eq` |
311 | | /// implementation). Returns one of the items. |
312 | 0 | fn assert_all_items_are_equal(self) -> Self::Item |
313 | 0 | where |
314 | 0 | Self::Item: Debug + Eq, |
315 | | { |
316 | 0 | self.assert_all_items_are_same(|lhs, rhs| assert_eq!(lhs, rhs)) Unexecuted instantiation: <core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}Unexecuted instantiation: <alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0} |
317 | 0 | } Unexecuted instantiation: <alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal Unexecuted instantiation: <core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal |
318 | | |
319 | | /// Verifies that all items in the iterator are the same (according to the `assert_same` |
320 | | /// function. Returns one of the items. |
321 | 4.62k | fn assert_all_items_are_same<F>(self, mut assert_same: F) -> <Self as Iterator>::Item |
322 | 4.62k | where |
323 | 4.62k | F: for<'a, 'b> FnMut(&'a Self::Item, &'b Self::Item), |
324 | | { |
325 | 4.62k | self.enumerate() |
326 | 9.24k | .reduce(|(i, lhs), (j, rhs)| { |
327 | 9.24k | let panic = { |
328 | 9.24k | let mut assert_same = std::panic::AssertUnwindSafe(&mut assert_same); |
329 | 9.24k | let lhs = std::panic::AssertUnwindSafe(&lhs); |
330 | 9.24k | let rhs = std::panic::AssertUnwindSafe(&rhs); |
331 | 9.24k | std::panic::catch_unwind(move || assert_same(*lhs, *rhs)) Unexecuted instantiation: <core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}>::{closure#0}::{closure#0}Unexecuted instantiation: <alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}>::{closure#0}::{closure#0}<core::iter::adapters::map::Map<core::slice::iter::Iter<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#3}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<buf_independent::test_data::{closure#4}>::{closure#0}::{closure#0}Line | Count | Source | 331 | 9.24k | std::panic::catch_unwind(move || assert_same(*lhs, *rhs)) |
|
332 | | }; |
333 | 9.24k | match panic { |
334 | 9.24k | Ok(_) => (), |
335 | 0 | Err(panic) => { |
336 | 0 | eprintln!("Difference found when comparing item #{i} and #{j}."); |
337 | 0 | std::panic::resume_unwind(panic); |
338 | | } |
339 | | } |
340 | 9.24k | ( |
341 | 9.24k | j, lhs, /* Arbitrary - could just as well return `rhs` */ |
342 | 9.24k | ) |
343 | 9.24k | }) Unexecuted instantiation: <core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}>::{closure#0}Unexecuted instantiation: <alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}>::{closure#0}<core::iter::adapters::map::Map<core::slice::iter::Iter<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#3}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<buf_independent::test_data::{closure#4}>::{closure#0}Line | Count | Source | 326 | 9.24k | .reduce(|(i, lhs), (j, rhs)| { | 327 | 9.24k | let panic = { | 328 | 9.24k | let mut assert_same = std::panic::AssertUnwindSafe(&mut assert_same); | 329 | 9.24k | let lhs = std::panic::AssertUnwindSafe(&lhs); | 330 | 9.24k | let rhs = std::panic::AssertUnwindSafe(&rhs); | 331 | 9.24k | std::panic::catch_unwind(move || assert_same(*lhs, *rhs)) | 332 | | }; | 333 | 9.24k | match panic { | 334 | 9.24k | Ok(_) => (), | 335 | 0 | Err(panic) => { | 336 | 0 | eprintln!("Difference found when comparing item #{i} and #{j}."); | 337 | 0 | std::panic::resume_unwind(panic); | 338 | | } | 339 | | } | 340 | 9.24k | ( | 341 | 9.24k | j, lhs, /* Arbitrary - could just as well return `rhs` */ | 342 | 9.24k | ) | 343 | 9.24k | }) |
|
344 | 4.62k | .map(|(_index, item)| item) |
345 | 4.62k | .expect("Expecting a non-empty iterator") |
346 | 4.62k | } <core::iter::adapters::map::Map<core::slice::iter::Iter<png::decoder::Reader<std::io::buffered::bufreader::BufReader<alloc::boxed::Box<dyn std::io::BufRead>>>>, buf_independent::test_data::{closure#3}> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<buf_independent::test_data::{closure#4}>Line | Count | Source | 321 | 4.62k | fn assert_all_items_are_same<F>(self, mut assert_same: F) -> <Self as Iterator>::Item | 322 | 4.62k | where | 323 | 4.62k | F: for<'a, 'b> FnMut(&'a Self::Item, &'b Self::Item), | 324 | | { | 325 | 4.62k | self.enumerate() | 326 | 4.62k | .reduce(|(i, lhs), (j, rhs)| { | 327 | | let panic = { | 328 | | let mut assert_same = std::panic::AssertUnwindSafe(&mut assert_same); | 329 | | let lhs = std::panic::AssertUnwindSafe(&lhs); | 330 | | let rhs = std::panic::AssertUnwindSafe(&rhs); | 331 | | std::panic::catch_unwind(move || assert_same(*lhs, *rhs)) | 332 | | }; | 333 | | match panic { | 334 | | Ok(_) => (), | 335 | | Err(panic) => { | 336 | | eprintln!("Difference found when comparing item #{i} and #{j}."); | 337 | | std::panic::resume_unwind(panic); | 338 | | } | 339 | | } | 340 | | ( | 341 | | j, lhs, /* Arbitrary - could just as well return `rhs` */ | 342 | | ) | 343 | | }) | 344 | 4.62k | .map(|(_index, item)| item) | 345 | 4.62k | .expect("Expecting a non-empty iterator") | 346 | 4.62k | } |
Unexecuted instantiation: <alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<alloc::vec::into_iter::IntoIter<()> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}>Unexecuted instantiation: <core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_same::<<core::slice::iter::Iter<alloc::vec::Vec<u8>> as buf_independent::IteratorExtensionsForFuzzing>::assert_all_items_are_equal::{closure#0}> |
347 | | } |
348 | | |
349 | | impl<T> IteratorExtensionsForFuzzing for T where T: Iterator + Sized {} |