From 1eb79a16140f8b123a5b9af684e290584381ee7b Mon Sep 17 00:00:00 2001 From: KAMADA Ken'ichi Date: Sat, 22 Oct 2016 20:22:06 +0900 Subject: [PATCH] Test jpeg::get_exif_attr(). --- src/jpeg.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tmacro.rs | 9 +++++++++ 2 files changed, 58 insertions(+) diff --git a/src/jpeg.rs b/src/jpeg.rs index 209814d..654e455 100644 --- a/src/jpeg.rs +++ b/src/jpeg.rs @@ -98,3 +98,52 @@ fn get_exif_attr_sub(reader: &mut R) } } } + +#[cfg(test)] +mod tests { + use std::io::Cursor; + use error::Error; + use super::*; + + #[test] + fn truncated() { + let sets: &[&[u8]] = &[ + b"", + b"\xff", + b"\xff\xd8", + b"\xff\xd8\x00", + b"\xff\xd8\xff", + b"\xff\xd8\xff\xe1\x00\x08\x03\x04", + ]; + for &data in sets { + assert_err_pat!(get_exif_attr(&mut Cursor::new(data)), + Error::InvalidFormat("Broken JPEG file")); + } + } + + #[test] + fn no_exif() { + let data = b"\xff\xd8\xff\xd9"; + assert_err_pat!(get_exif_attr(&mut Cursor::new(data)), + Error::NotFound(_)); + } + + #[test] + fn out_of_sync() { + let data = b"\xff\xd8\x01\x02\x03\xff\x00\xff\xd9"; + assert_err_pat!(get_exif_attr(&mut Cursor::new(data)), + Error::NotFound(_)); + } + + #[test] + fn empty() { + let data = b"\xff\xd8\xff\xe1\x00\x08Exif\0\0\xff\xd9"; + assert_ok!(get_exif_attr(&mut Cursor::new(data)), []); + } + + #[test] + fn non_empty() { + let data = b"\xff\xd8\xff\xe1\x00\x0aExif\0\0\xbe\xad\xff\xd9"; + assert_ok!(get_exif_attr(&mut Cursor::new(data)), [0xbe, 0xad]); + } +} diff --git a/src/tmacro.rs b/src/tmacro.rs index 2c8474f..6a1b090 100644 --- a/src/tmacro.rs +++ b/src/tmacro.rs @@ -35,6 +35,15 @@ macro_rules! assert_ok { ) } +macro_rules! assert_err_pat { + ($expr:expr, $variant:pat) => ( + match $expr { + Err($variant) => {}, + r => panic!("assertion failed: unexpected {:?}", r), + } + ) +} + // This macro is intended to be used with std::io::Error, but other // types with kind() will also work. macro_rules! assert_err_kind {