| 1 | use super::*; |
| 2 | |
| 3 | fn png_bytes( |
| 4 | width: u32, |
| 5 | height: u32, |
| 6 | color: png::ColorType, |
| 7 | depth: png::BitDepth, |
| 8 | data: &[u8], |
| 9 | ) -> Vec<u8> { |
| 10 | let mut bytes = Vec::new(); |
| 11 | let mut encoder = png::Encoder::new(&mut bytes, width, height); |
| 12 | encoder.set_color(color); |
| 13 | encoder.set_depth(depth); |
| 14 | encoder |
| 15 | .write_header() |
| 16 | .unwrap() |
| 17 | .write_image_data(data) |
| 18 | .unwrap(); |
| 19 | bytes |
| 20 | } |
| 21 | fn pixel() -> Vec<u8> { |
| 22 | png_bytes( |
| 23 | 1, |
| 24 | 1, |
| 25 | png::ColorType::Rgba, |
| 26 | png::BitDepth::Eight, |
| 27 | &[10, 20, 30, 40], |
| 28 | ) |
| 29 | } |
| 30 | fn chunk(kind: &[u8; 4], bytes: &[u8]) -> Vec<u8> { |
| 31 | let mut result = (bytes.len() as u32).to_be_bytes().to_vec(); |
| 32 | result.extend(kind); |
| 33 | result.extend(bytes); |
| 34 | result.extend(crc32(0, &result[4..]).to_be_bytes()); |
| 35 | result |
| 36 | } |
| 37 | fn run(kind: Kind, bytes: Vec<u8>) -> Result<(PreviewInfo, Vec<u8>)> { |
| 38 | decode(kind, 7, bytes, &mut || Ok(())) |
| 39 | } |
| 40 | |
| 41 | #[test] |
| 42 | fn preview_text_checks_utf8_bom_and_source_boundaries() { |
| 43 | for bytes in [ |
| 44 | Vec::new(), |
| 45 | vec![b'x'; MAX_PREVIEW_TEXT_BYTES as usize], |
| 46 | "한글\n".as_bytes().to_vec(), |
| 47 | vec![0xef, 0xbb, 0xbf], |
| 48 | ] { |
| 49 | let (info, data) = run(Kind::Text, bytes.clone()).unwrap(); |
| 50 | assert!(info.valid()); |
| 51 | assert_eq!(info.entry_id, 7); |
| 52 | assert_eq!(info.total_bytes, data.len() as u64); |
| 53 | assert_eq!(info.format, PreviewFormat::Utf8Text); |
| 54 | assert_eq!( |
| 55 | data, |
| 56 | bytes.strip_prefix(&[0xef, 0xbb, 0xbf]).unwrap_or(&bytes) |
| 57 | ); |
| 58 | } |
| 59 | assert!(matches!( |
| 60 | run(Kind::Text, vec![b'x'; MAX_PREVIEW_TEXT_BYTES as usize + 1]), |
| 61 | Err(ErrorCode::Limit) |
| 62 | )); |
| 63 | assert!(matches!( |
| 64 | run(Kind::Text, vec![0xff]), |
| 65 | Err(ErrorCode::Integrity) |
| 66 | )); |
| 67 | assert!(matches!( |
| 68 | run(Kind::Text, vec![0xef, 0xbb, 0xbf, 0xff]), |
| 69 | Err(ErrorCode::Integrity) |
| 70 | )); |
| 71 | } |
| 72 | #[test] |
| 73 | fn preview_extensions_are_explicit_and_ascii_case_insensitive() { |
| 74 | for name in [ |
| 75 | "a.txt", "a.MD", "a.log", "a.csv", "a.JSON", "a.PnG", "a.jpg", "a.JPEG", |
| 76 | ] { |
| 77 | assert!(Kind::from_name(name).is_ok()); |
| 78 | } |
| 79 | for name in ["a.svg", "a.gif", "a.webp", "a.exe", "a", "folder.txt/child"] { |
| 80 | assert!(Kind::from_name(name).is_err()); |
| 81 | } |
| 82 | } |
| 83 | #[test] |
| 84 | fn preview_png_normalizes_gray_rgb_alpha_and_sixteen_bit_pixels() { |
| 85 | for (color, depth, source, expected) in [ |
| 86 | ( |
| 87 | png::ColorType::Rgba, |
| 88 | png::BitDepth::Eight, |
| 89 | vec![10, 20, 30, 40], |
| 90 | vec![10, 20, 30, 40], |
| 91 | ), |
| 92 | ( |
| 93 | png::ColorType::Rgb, |
| 94 | png::BitDepth::Eight, |
| 95 | vec![10, 20, 30], |
| 96 | vec![10, 20, 30, 255], |
| 97 | ), |
| 98 | ( |
| 99 | png::ColorType::Grayscale, |
| 100 | png::BitDepth::Eight, |
| 101 | vec![10], |
| 102 | vec![10, 10, 10, 255], |
| 103 | ), |
| 104 | ( |
| 105 | png::ColorType::GrayscaleAlpha, |
| 106 | png::BitDepth::Eight, |
| 107 | vec![10, 40], |
| 108 | vec![10, 10, 10, 40], |
| 109 | ), |
| 110 | ( |
| 111 | png::ColorType::GrayscaleAlpha, |
| 112 | png::BitDepth::Sixteen, |
| 113 | vec![10, 99, 40, 99], |
| 114 | vec![10, 10, 10, 40], |
| 115 | ), |
| 116 | ] { |
| 117 | let (info, data) = run(Kind::Png, png_bytes(1, 1, color, depth, &source)).unwrap(); |
| 118 | assert_eq!((info.width, info.height, info.total_bytes), (1, 1, 4)); |
| 119 | assert_eq!(data, expected); |
| 120 | } |
| 121 | } |
| 122 | #[test] |
| 123 | fn preview_png_rejects_crc_truncation_animation_and_format_mismatch() { |
| 124 | let original = pixel(); |
| 125 | for length in [8, original.len() - 12, original.len() - 1] { |
| 126 | assert!(run(Kind::Png, original[..length].to_vec()).is_err()); |
| 127 | } |
| 128 | let mut bad = original.clone(); |
| 129 | bad[29] ^= 1; |
| 130 | assert!(matches!(run(Kind::Png, bad), Err(ErrorCode::Integrity))); |
| 131 | let mut trailing = original.clone(); |
| 132 | trailing.push(0); |
| 133 | assert!(matches!( |
| 134 | run(Kind::Png, trailing), |
| 135 | Err(ErrorCode::Integrity) |
| 136 | )); |
| 137 | let mut animated = original.clone(); |
| 138 | animated.splice(33..33, chunk(b"acTL", &[0, 0, 0, 1, 0, 0, 0, 0])); |
| 139 | assert!(matches!( |
| 140 | run(Kind::Png, animated), |
| 141 | Err(ErrorCode::UnsupportedFeature) |
| 142 | )); |
| 143 | assert!(matches!( |
| 144 | run(Kind::Png, b"GIF89a".to_vec()), |
| 145 | Err(ErrorCode::UnsupportedFormat) |
| 146 | )); |
| 147 | } |
| 148 | #[test] |
| 149 | fn preview_png_checks_dimensions_and_pixels_before_decoding() { |
| 150 | for (width, height) in [ |
| 151 | (8193u32, 1u32), |
| 152 | (1, 8193), |
| 153 | (4001, 4000), |
| 154 | (8192, 8192), |
| 155 | (u32::MAX, u32::MAX), |
| 156 | ] { |
| 157 | let mut bytes = pixel(); |
| 158 | bytes[16..20].copy_from_slice(&width.to_be_bytes()); |
| 159 | bytes[20..24].copy_from_slice(&height.to_be_bytes()); |
| 160 | let crc = crc32(0, &bytes[12..29]); |
| 161 | bytes[29..33].copy_from_slice(&crc.to_be_bytes()); |
| 162 | assert!( |
| 163 | matches!(run(Kind::Png, bytes), Err(ErrorCode::Limit)), |
| 164 | "{width}x{height}" |
| 165 | ); |
| 166 | } |
| 167 | for (width, height) in [(8192, 1), (1, 8192), (4000, 4000)] { |
| 168 | let bytes = png_bytes( |
| 169 | width, |
| 170 | height, |
| 171 | png::ColorType::Rgba, |
| 172 | png::BitDepth::Eight, |
| 173 | &vec![0; (width * height * 4) as usize], |
| 174 | ); |
| 175 | let (info, rgba) = run(Kind::Png, bytes).unwrap(); |
| 176 | assert_eq!((info.width, info.height), (width, height)); |
| 177 | assert_eq!(rgba.len(), (width * height * 4) as usize); |
| 178 | } |
| 179 | } |
| 180 | #[test] |
| 181 | fn preview_image_source_limit_includes_ancillary_bytes() { |
| 182 | let mut bytes = pixel(); |
| 183 | let at = bytes.len() - 12; |
| 184 | let padding = vec![0; MAX_PREVIEW_IMAGE_BYTES as usize - bytes.len() - 12]; |
| 185 | bytes.splice(at..at, chunk(b"teSt", &padding)); |
| 186 | assert_eq!(bytes.len(), MAX_PREVIEW_IMAGE_BYTES as usize); |
| 187 | assert!(run(Kind::Png, bytes.clone()).is_ok()); |
| 188 | bytes.push(0); |
| 189 | assert!(matches!(run(Kind::Png, bytes), Err(ErrorCode::Limit))); |
| 190 | assert!(matches!( |
| 191 | run(Kind::Jpeg, vec![0; MAX_PREVIEW_IMAGE_BYTES as usize + 1]), |
| 192 | Err(ErrorCode::Limit) |
| 193 | )); |
| 194 | } |
| 195 | #[test] |
| 196 | fn preview_jpeg_strictly_decodes_baseline_and_progressive() { |
| 197 | for bytes in [ |
| 198 | include_bytes!("fixtures/preview-baseline.jpg").as_slice(), |
| 199 | include_bytes!("fixtures/preview-progressive.jpg").as_slice(), |
| 200 | ] { |
| 201 | let (info, data) = run(Kind::Jpeg, bytes.to_vec()).unwrap(); |
| 202 | assert_eq!((info.width, info.height, info.total_bytes), (3, 2, 24)); |
| 203 | for pixel in data.chunks_exact(4) { |
| 204 | assert_eq!(pixel[3], 255); |
| 205 | assert!(pixel[0] > 200 && pixel[1] < 60 && pixel[2] < 80); |
| 206 | } |
| 207 | for length in [2, bytes.len() / 2, bytes.len() - 1] { |
| 208 | assert!(run(Kind::Jpeg, bytes[..length].to_vec()).is_err()); |
| 209 | } |
| 210 | let mut incomplete = bytes[..bytes.len() / 2].to_vec(); |
| 211 | incomplete.extend([0xff, 0xd9]); |
| 212 | assert!(run(Kind::Jpeg, incomplete).is_err()); |
| 213 | } |
| 214 | assert!(matches!( |
| 215 | run(Kind::Jpeg, pixel()), |
| 216 | Err(ErrorCode::UnsupportedFormat) |
| 217 | )); |
| 218 | } |
| 219 | #[test] |
| 220 | fn preview_cancellation_discards_decoded_results_and_conversion() { |
| 221 | for kind in [Kind::Text, Kind::Png, Kind::Jpeg] { |
| 222 | let source = match kind { |
| 223 | Kind::Text => b"text".to_vec(), |
| 224 | Kind::Png => pixel(), |
| 225 | Kind::Jpeg => include_bytes!("fixtures/preview-baseline.jpg").to_vec(), |
| 226 | }; |
| 227 | let mut calls = 0; |
| 228 | assert!(matches!( |
| 229 | decode(kind, 7, source, &mut || { |
| 230 | calls += 1; |
| 231 | if calls >= 2 { |
| 232 | Err(ErrorCode::None) |
| 233 | } else { |
| 234 | Ok(()) |
| 235 | } |
| 236 | }), |
| 237 | Err(ErrorCode::None) |
| 238 | )); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn preview_jpeg_checks_dimensions_before_decoder_allocation() { |
| 244 | let original = include_bytes!("fixtures/preview-baseline.jpg"); |
| 245 | let at = original.windows(2).position(|p| p == [0xff, 0xc0]).unwrap(); |
| 246 | for (width, height) in [(8193u16, 1u16), (1, 8193), (4001, 4000), (8192, 8192)] { |
| 247 | let mut bytes = original.to_vec(); |
| 248 | bytes[at + 5..at + 7].copy_from_slice(&height.to_be_bytes()); |
| 249 | bytes[at + 7..at + 9].copy_from_slice(&width.to_be_bytes()); |
| 250 | assert!( |
| 251 | matches!(run(Kind::Jpeg, bytes), Err(ErrorCode::Limit)), |
| 252 | "{width}x{height}" |
| 253 | ); |
| 254 | } |
| 255 | let mut multiple = original.to_vec(); |
| 256 | multiple.extend(original); |
| 257 | assert!(run(Kind::Jpeg, multiple).is_err()); |
| 258 | } |
| 259 | |
| 260 | #[test] |
| 261 | fn preview_jpeg_normalizes_gray_and_cmyk_to_rgba() { |
| 262 | for (bytes, gray) in [ |
| 263 | (include_bytes!("fixtures/preview-gray.jpg").as_slice(), true), |
| 264 | ( |
| 265 | include_bytes!("fixtures/preview-cmyk.jpg").as_slice(), |
| 266 | false, |
| 267 | ), |
| 268 | ] { |
| 269 | let (info, pixels) = run(Kind::Jpeg, bytes.to_vec()).unwrap(); |
| 270 | assert_eq!((info.width, info.height, info.total_bytes), (3, 2, 24)); |
| 271 | for pixel in pixels.chunks_exact(4) { |
| 272 | assert_eq!(pixel[3], 255); |
| 273 | if gray { |
| 274 | assert!(pixel[..3].iter().all(|v| v.abs_diff(80) <= 2)); |
| 275 | } else { |
| 276 | assert!(pixel[0] > 240 && pixel[1] < 20 && pixel[2] < 20); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | #[test] |
| 283 | fn preview_jpeg_rejects_missing_scan_pixels() { |
| 284 | let original = include_bytes!("fixtures/preview-baseline.jpg"); |
| 285 | let mut missing = original[..623].to_vec(); |
| 286 | missing.extend([0xff, 0xd9]); |
| 287 | assert!(matches!( |
| 288 | run(Kind::Jpeg, missing), |
| 289 | Err(ErrorCode::Integrity) |
| 290 | )); |
| 291 | |
| 292 | let mut missing_rows = original.to_vec(); |
| 293 | let sof = original.windows(2).position(|p| p == [0xff, 0xc0]).unwrap(); |
| 294 | missing_rows[sof + 5..sof + 7].copy_from_slice(&32u16.to_be_bytes()); |
| 295 | assert!(matches!( |
| 296 | run(Kind::Jpeg, missing_rows), |
| 297 | Err(ErrorCode::Integrity) |
| 298 | )); |
| 299 | |
| 300 | let sos = original.windows(2).position(|p| p == [0xff, 0xda]).unwrap(); |
| 301 | let mut missing_components = original.to_vec(); |
| 302 | missing_components[sos + 2..sos + 4].copy_from_slice(&8u16.to_be_bytes()); |
| 303 | missing_components[sos + 4] = 1; |
| 304 | missing_components.drain(sos + 7..sos + 11); |
| 305 | assert!(matches!( |
| 306 | jpeg_info(7, &missing_components, &mut || Ok(())), |
| 307 | Err(ErrorCode::Integrity) |
| 308 | )); |
| 309 | } |
| 310 | |
| 311 | #[test] |
| 312 | fn preview_png_rejects_complete_container_with_missing_pixel_rows() { |
| 313 | let mut missing = pixel(); |
| 314 | missing[20..24].copy_from_slice(&2u32.to_be_bytes()); |
| 315 | let crc = crc32(0, &missing[12..29]); |
| 316 | missing[29..33].copy_from_slice(&crc.to_be_bytes()); |
| 317 | assert!(matches!(run(Kind::Png, missing), Err(ErrorCode::Integrity))); |
| 318 | } |
| 319 | |
| 320 | #[test] |
| 321 | fn preview_jpeg_validates_multiline_scans_and_restart_boundaries() { |
| 322 | for bytes in [ |
| 323 | include_bytes!("fixtures/preview-rows-baseline.jpg").as_slice(), |
| 324 | include_bytes!("fixtures/preview-rows-progressive.jpg").as_slice(), |
| 325 | include_bytes!("fixtures/preview-rows-gray.jpg").as_slice(), |
| 326 | include_bytes!("fixtures/preview-rows-cmyk.jpg").as_slice(), |
| 327 | include_bytes!("fixtures/preview-rows-restart.jpg").as_slice(), |
| 328 | ] { |
| 329 | let (info, pixels) = run(Kind::Jpeg, bytes.to_vec()).unwrap(); |
| 330 | assert_eq!((info.width, info.height), (31, 33)); |
| 331 | assert_eq!(pixels.len(), 31 * 33 * 4); |
| 332 | assert!(pixels.chunks_exact(4).all(|pixel| pixel[3] == 255)); |
| 333 | for (sos, _) in bytes |
| 334 | .windows(2) |
| 335 | .enumerate() |
| 336 | .filter(|(_, p)| *p == [0xff, 0xda]) |
| 337 | { |
| 338 | let scan_start = |
| 339 | sos + 2 + usize::from(u16::from_be_bytes([bytes[sos + 2], bytes[sos + 3]])); |
| 340 | let mut missing = bytes[..scan_start].to_vec(); |
| 341 | missing.extend([0xff, 0xd9]); |
| 342 | assert!( |
| 343 | matches!(run(Kind::Jpeg, missing), Err(ErrorCode::Integrity)), |
| 344 | "empty scan={sos}" |
| 345 | ); |
| 346 | } |
| 347 | let sos = bytes.windows(2).position(|p| p == [0xff, 0xda]).unwrap(); |
| 348 | let scan_start = |
| 349 | sos + 2 + usize::from(u16::from_be_bytes([bytes[sos + 2], bytes[sos + 3]])); |
| 350 | for cut in [scan_start + 1, scan_start + 2] { |
| 351 | let mut missing = bytes[..cut].to_vec(); |
| 352 | missing.extend([0xff, 0xd9]); |
| 353 | assert!( |
| 354 | matches!(run(Kind::Jpeg, missing), Err(ErrorCode::Integrity)), |
| 355 | "cut={cut}" |
| 356 | ); |
| 357 | } |
| 358 | } |
| 359 | } |