diff --git a/dev/cmp/src/lib.rs b/dev/cmp/src/lib.rs index 9afa9da..410fbee 100644 --- a/dev/cmp/src/lib.rs +++ b/dev/cmp/src/lib.rs @@ -152,6 +152,41 @@ pub fn cmp_output(file_path :&str) -> (usize, usize) { return (pcks_with_diffs, n); } +/// Like try, but performs an action if an "expected" error +/// is intercepted +#[macro_export] +macro_rules! try_expected { + ($expr:expr, $expected:pat, $action:tt) => (match $expr { + Ok(val) => val, + Err($expected) => { + $action + }, + Err(e) => { + panic!("Unexpected error: {:?}\nExpected: {:?}", e, stringify!($type)); + }, + }) +} + +/// Ensures that a file is malformed and returns an error, +/// but doesn't panic or crash or anything of the like +#[macro_export] +macro_rules! ensure_malformed { + ($name:expr, $expected:pat) => {{ + use std::fs::File; + use lewton::inside_ogg::OggStreamReader; + // Read the file to memory + let f = File::open(format!("test-assets/{}", $name)).unwrap(); + if let Some(mut ogg_rdr) = try_expected!(OggStreamReader::new(f).map(|v| Some(v)), $expected, None) { + loop { + match try_expected!(ogg_rdr.read_dec_packet_itl(), $expected, break) { + Some(_) => (), + None => panic!("File {} decoded without errors", $name), + }; + } + } + }} +} + use self::test_assets::TestAssetDef; pub fn get_asset_defs() -> [TestAssetDef; 6] { diff --git a/dev/cmp/tests/fuzzed.rs b/dev/cmp/tests/fuzzed.rs index a2a48b4..7e247a2 100644 --- a/dev/cmp/tests/fuzzed.rs +++ b/dev/cmp/tests/fuzzed.rs @@ -7,12 +7,10 @@ // attached to this source distribution for details. extern crate test_assets; +#[macro_use] extern crate cmp; extern crate lewton; -use std::fs::File; -use lewton::inside_ogg::OggStreamReader; - macro_rules! try { ($expr:expr) => (match $expr { $crate::std::result::Result::Ok(val) => val, @@ -22,35 +20,6 @@ macro_rules! try { }) } -macro_rules! etry { - ($expr:expr, $expected:pat, $action:tt) => (match $expr { - Ok(val) => val, - Err($expected) => { - $action - }, - Err(e) => { - panic!("Unexpected error: {:?}\nExpected: {:?}", e, stringify!($type)); - }, - }) -} - -// Ensures that a file is malformed and returns an error, -// but doesn't panic or crash or anything of the like -macro_rules! ensure_malformed { - ($name:expr, $expected:pat) => {{ - // Read the file to memory - let f = try!(File::open(format!("test-assets/{}", $name))); - if let Some(mut ogg_rdr) = etry!(OggStreamReader::new(f).map(|v| Some(v)), $expected, None) { - loop { - match etry!(ogg_rdr.read_dec_packet_itl(), $expected, break) { - Some(_) => (), - None => panic!("File {} decoded without errors", $name), - }; - } - } - }} -} - #[test] fn test_malformed_fuzzed() { println!(); @@ -69,6 +38,8 @@ fn test_malformed_fuzzed() { // Ensures that a file is okay macro_rules! ensure_okay { ($name:expr) => {{ + use std::fs::File; + use lewton::inside_ogg::OggStreamReader; // Read the file to memory let f = try!(File::open(format!("test-assets/{}", $name))); if let Some(mut ogg_rdr) = try!(OggStreamReader::new(f).map(|v| Some(v))) { diff --git a/dev/cmp/tests/vals.rs b/dev/cmp/tests/vals.rs index 32f05a3..70eb2b4 100644 --- a/dev/cmp/tests/vals.rs +++ b/dev/cmp/tests/vals.rs @@ -7,8 +7,14 @@ // attached to this source distribution for details. extern crate test_assets; +extern crate lewton; +#[macro_use] extern crate cmp; +use lewton::VorbisError::*; +use lewton::OggReadError::*; +use lewton::header::HeaderReadError::*; + macro_rules! cmp_output { ($str:expr, $max_diff:expr) => { print!("Comparing output for {} ", $str); @@ -48,8 +54,7 @@ fn test_libnogg_vals() { //cmp_output!("6ch-long-first-packet.ogg", 0); cmp_output!("6ch-moving-sine-floor0.ogg", 0); //cmp_output!("6ch-moving-sine.ogg", 0); - // TODO this is a bad invalid file - //cmp_output!("bad-continued-packet-flag.ogg", 0); + ensure_malformed!("bad-continued-packet-flag.ogg", OggError(InvalidData)); cmp_output!("bitrate-123.ogg", 0); cmp_output!("bitrate-456-0.ogg", 0); cmp_output!("bitrate-456-789.ogg", 0); @@ -61,9 +66,7 @@ fn test_libnogg_vals() { cmp_output!("noise-stereo.ogg", 0); cmp_output!("partial-granule-position.ogg", 2); cmp_output!("sample-rate-max.ogg", 0); - // TODO we are getting Error: BadHeader(HeaderBadFormat) here - // is that expected? - //cmp_output!("single-code-2bits.ogg", 0); + ensure_malformed!("single-code-2bits.ogg", BadHeader(HeaderBadFormat)); // TODO we are getting Error: BadHeader here. // is that expected? //cmp_output!("single-code-nonsparse.ogg", 0);