| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | #[allow(dead_code)] |
| 8 | #[path = "it/common.rs"] |
| 9 | mod ivf; |
| 10 | |
| 11 | use std::panic::{AssertUnwindSafe, catch_unwind}; |
| 12 | |
| 13 | use ec_vp8::stream::decode_stream; |
| 14 | |
| 15 | |
| 16 | const FIXTURES: &[&str] = &[ |
| 17 | "kf-16x16-black-q0.ivf", |
| 18 | "kf-32x16-q35.ivf", |
| 19 | "kf-76x52-q30.ivf", |
| 20 | "kf-96x80-q63.ivf", |
| 21 | "kf-160x96-q20.ivf", |
| 22 | "kf-160x96-q40.ivf", |
| 23 | "kf-172x144-q25.ivf", |
| 24 | "gop-96x80-v1.ivf", |
| 25 | "gop-160x96.ivf", |
| 26 | "mparts-160x96.ivf", |
| 27 | "altref-160x96.ivf", |
| 28 | "clip-obs-320x192.ivf", |
| 29 | ]; |
| 30 | |
| 31 | const TRUNCATIONS: usize = 10; |
| 32 | const CORRUPTIONS: usize = 50; |
| 33 | const SEED: u64 = 0x5EED_0F08; |
| 34 | |
| 35 | |
| 36 | struct Rng(u64); |
| 37 | |
| 38 | impl Rng { |
| 39 | fn next(&mut self) -> u64 { |
| 40 | self.0 = self.0.wrapping_add(0x9e37_79b9_7f4a_7c15); |
| 41 | let mut z = self.0; |
| 42 | z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9); |
| 43 | z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb); |
| 44 | z ^ (z >> 31) |
| 45 | } |
| 46 | |
| 47 | fn below(&mut self, n: usize) -> usize { |
| 48 | (self.next() % n as u64) as usize |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | fn decode_must_not_panic(data: &[u8], what: &str) { |
| 54 | let outcome = catch_unwind(AssertUnwindSafe(|| { |
| 55 | let _ = decode_stream(data); |
| 56 | })); |
| 57 | assert!(outcome.is_ok(), "decoder panicked on {what}"); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn truncated_and_corrupted_fixtures_never_panic() { |
| 62 | let Some(dir) = ivf::fixture_dir() else { |
| 63 | panic!("fixtures missing; run scripts/gen_vp8_fixtures.sh"); |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | let hook = std::panic::take_hook(); |
| 68 | std::panic::set_hook(Box::new(|_| {})); |
| 69 | |
| 70 | let mut rng = Rng(SEED); |
| 71 | let mut failures = Vec::new(); |
| 72 | for name in FIXTURES { |
| 73 | let bytes = std::fs::read(dir.join(name)).unwrap(); |
| 74 | assert!(bytes.len() > 32, "{name}: fixture too small"); |
| 75 | |
| 76 | for k in 1..=TRUNCATIONS { |
| 77 | let cut = bytes.len() * k / (TRUNCATIONS + 1); |
| 78 | let what = format!("{name} truncated to {cut} of {} bytes", bytes.len()); |
| 79 | if catch_unwind(AssertUnwindSafe(|| { |
| 80 | let _ = decode_stream(&bytes[..cut]); |
| 81 | })) |
| 82 | .is_err() |
| 83 | { |
| 84 | failures.push(what); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for i in 0..CORRUPTIONS { |
| 89 | let at = rng.below(bytes.len()); |
| 90 | let flip = (rng.below(255) + 1) as u8; |
| 91 | let mut damaged = bytes.clone(); |
| 92 | damaged[at] ^= flip; |
| 93 | let what = format!("{name} corruption {i}: byte {at} ^= {flip:#04x}"); |
| 94 | if catch_unwind(AssertUnwindSafe(|| { |
| 95 | let _ = decode_stream(&damaged); |
| 96 | })) |
| 97 | .is_err() |
| 98 | { |
| 99 | failures.push(what); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | std::panic::set_hook(hook); |
| 105 | assert!(failures.is_empty(), "decoder panicked on {} inputs: {failures:#?}", failures.len()); |
| 106 | } |
| 107 | |
| 108 | #[test] |
| 109 | fn decode_must_not_panic_helper_accepts_errors() { |
| 110 | |
| 111 | decode_must_not_panic(&[0xff; 64], "all-0xff buffer"); |
| 112 | decode_must_not_panic(&[], "empty buffer"); |
| 113 | } |