Skip to content

Commit

Permalink
Add tests for two malformed files
Browse files Browse the repository at this point in the history
Move the malformed macro from the fuzzed test
to the cmp library, and use it in the vals.rs
file.
  • Loading branch information
est31 committed Sep 13, 2018
1 parent db10a40 commit 95fa236
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
35 changes: 35 additions & 0 deletions dev/cmp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down
35 changes: 3 additions & 32 deletions dev/cmp/tests/fuzzed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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!();
Expand All @@ -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))) {
Expand Down
13 changes: 8 additions & 5 deletions dev/cmp/tests/vals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 95fa236

Please sign in to comment.