| 1 | |
| 2 | |
| 3 | |
| 4 | mod common; |
| 5 | use common::*; |
| 6 | use ec_core::Error; |
| 7 | |
| 8 | struct Rng(u64); |
| 9 | impl Rng { |
| 10 | fn next(&mut self) -> u64 { |
| 11 | self.0 ^= self.0 << 13; |
| 12 | self.0 ^= self.0 >> 7; |
| 13 | self.0 ^= self.0 << 17; |
| 14 | self.0 |
| 15 | } |
| 16 | fn below(&mut self, n: usize) -> usize { |
| 17 | (self.next() % n.max(1) as u64) as usize |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | |
| 22 | fn frames() -> Option<Vec<Vec<u8>>> { |
| 23 | static CACHE: std::sync::OnceLock<Option<Vec<Vec<u8>>>> = std::sync::OnceLock::new(); |
| 24 | CACHE.get_or_init(make_frames).clone() |
| 25 | } |
| 26 | |
| 27 | fn make_frames() -> Option<Vec<Vec<u8>>> { |
| 28 | if !ffmpeg_or_skip() { |
| 29 | return None; |
| 30 | } |
| 31 | let dir = scratch("robust"); |
| 32 | let mut out = Vec::new(); |
| 33 | for (i, (prof, fmt, extra)) in [ |
| 34 | ("3", "yuv422p10le", ""), |
| 35 | ("1", "yuv422p10le", "+ildct"), |
| 36 | ("4", "yuva444p10le", ""), |
| 37 | ] |
| 38 | .into_iter() |
| 39 | .enumerate() |
| 40 | { |
| 41 | let mov = dir.join(format!("r{i}.mov")); |
| 42 | let mut a = vec![ |
| 43 | "-f", "lavfi", "-i", "testsrc2=size=100x70:rate=25,noise=alls=20:allf=t", "-frames:v", "1", |
| 44 | "-c:v", "prores_ks", "-profile", prof, "-pix_fmt", fmt, |
| 45 | ]; |
| 46 | if !extra.is_empty() { |
| 47 | a.extend(["-flags", extra]); |
| 48 | } |
| 49 | a.push(mov.to_str().unwrap()); |
| 50 | assert!(run(&a)); |
| 51 | out.push(packets(&std::fs::read(&mov).unwrap()).remove(0)); |
| 52 | } |
| 53 | let _ = std::fs::remove_dir_all(dir); |
| 54 | Some(out) |
| 55 | } |
| 56 | |
| 57 | #[test] |
| 58 | fn truncations_never_panic() { |
| 59 | let Some(fs) = frames() else { return }; |
| 60 | for f in &fs { |
| 61 | let mut d = decoder(2); |
| 62 | assert!(d.decode(f).is_ok()); |
| 63 | for n in (0..f.len()).step_by(7).chain(0..40) { |
| 64 | let _ = d.decode(&f[..n.min(f.len())]); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn corruptions_never_panic() { |
| 71 | let Some(fs) = frames() else { return }; |
| 72 | let mut rng = Rng(0x9E3779B97F4A7C15); |
| 73 | for f in &fs { |
| 74 | let mut d = decoder(3); |
| 75 | for round in 0..400 { |
| 76 | let mut g = f.clone(); |
| 77 | let flips = 1 + rng.below(8); |
| 78 | for _ in 0..flips { |
| 79 | |
| 80 | let at = if round % 2 == 0 { rng.below(g.len().min(200)) } else { rng.below(g.len()) }; |
| 81 | g[at] ^= 1 << rng.below(8); |
| 82 | } |
| 83 | let _ = d.decode(&g); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn garbage_is_refused_by_name() { |
| 90 | let mut d = decoder(1); |
| 91 | let mut rng = Rng(7); |
| 92 | for len in [0usize, 1, 7, 8, 27, 28, 200, 5000] { |
| 93 | let g: Vec<u8> = (0..len).map(|_| rng.next() as u8).collect(); |
| 94 | assert!(d.decode(&g).is_err()); |
| 95 | } |
| 96 | |
| 97 | let mut g = vec![0u8; 300]; |
| 98 | g[..4].copy_from_slice(&300u32.to_be_bytes()); |
| 99 | g[4..8].copy_from_slice(b"icpf"); |
| 100 | match d.decode(&g) { |
| 101 | Err(Error::Unsupported { .. } | Error::Corrupt { .. }) => {} |
| 102 | other => panic!("expected a named refusal, got {:?}", other.map(|f| f.width)), |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[test] |
| 107 | fn hostile_dimensions_are_bounded() { |
| 108 | let Some(fs) = frames() else { return }; |
| 109 | let mut g = fs[0].clone(); |
| 110 | |
| 111 | g[16..20].copy_from_slice(&[0xFF, 0xFF, 0xFF, 0xFF]); |
| 112 | let t = std::time::Instant::now(); |
| 113 | assert!(decoder(1).decode(&g).is_err()); |
| 114 | assert!(t.elapsed().as_secs() < 5); |
| 115 | g[16..20].copy_from_slice(&[0, 0, 0, 0]); |
| 116 | assert!(decoder(1).decode(&g).is_err()); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn wrong_codec_is_unsupported() { |
| 121 | use ec_core::registry::{CodecId, CodecParameters}; |
| 122 | match ec_prores::ProResDecoder::new(CodecParameters::new(CodecId::H264)) { |
| 123 | Err(Error::Unsupported { .. }) => {} |
| 124 | _ => panic!("expected Unsupported"), |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | |
| 129 | #[test] |
| 130 | #[ignore] |
| 131 | fn dump_seeds() { |
| 132 | let dir = std::env::var("EC_SEED_DIR").expect("EC_SEED_DIR"); |
| 133 | std::fs::create_dir_all(&dir).unwrap(); |
| 134 | let Some(fs) = frames() else { return }; |
| 135 | for (i, f) in fs.iter().enumerate() { |
| 136 | std::fs::write(format!("{dir}/seed{i}.bin"), f).unwrap(); |
| 137 | } |
| 138 | } |