diff --git a/src/id3/v2/read.rs b/src/id3/v2/read.rs index 88fedb7e..072cd044 100644 --- a/src/id3/v2/read.rs +++ b/src/id3/v2/read.rs @@ -31,3 +31,13 @@ where Ok(tag) } + +#[test] +fn zero_size_id3v2() { + use crate::id3::v2::read_id3v2_header; + use std::io::Cursor; + + let mut f = Cursor::new(std::fs::read("tests/tags/assets/id3v2/zero.id3v2").unwrap()); + let header = read_id3v2_header(&mut f).unwrap(); + assert!(parse_id3v2(&mut f, header).is_ok()); +} diff --git a/src/mp3/read.rs b/src/mp3/read.rs index db1f650f..d8834749 100644 --- a/src/mp3/read.rs +++ b/src/mp3/read.rs @@ -172,20 +172,3 @@ where Ok(file) } - -#[cfg(test)] -mod tests { - use crate::file::AudioFile; - use crate::mp3::Mp3File; - use std::fs::File; - - #[test] - fn issue_39() { - // MP3 file that only consists of an ID3v2 tag - assert!(Mp3File::read_from( - &mut File::open("tests/files/assets/issue_39.mp3").unwrap(), - true, - ) - .is_err()); - } -} diff --git a/tests/files/assets/zero/zero.aiff b/tests/files/assets/zero/zero.aiff new file mode 100644 index 00000000..0cae2a5d Binary files /dev/null and b/tests/files/assets/zero/zero.aiff differ diff --git a/tests/files/assets/zero/zero.ape b/tests/files/assets/zero/zero.ape new file mode 100644 index 00000000..787b9c83 Binary files /dev/null and b/tests/files/assets/zero/zero.ape differ diff --git a/tests/files/assets/zero/zero.flac b/tests/files/assets/zero/zero.flac new file mode 100644 index 00000000..7312dd63 Binary files /dev/null and b/tests/files/assets/zero/zero.flac differ diff --git a/tests/files/assets/issue_39.mp3 b/tests/files/assets/zero/zero.mp3 similarity index 100% rename from tests/files/assets/issue_39.mp3 rename to tests/files/assets/zero/zero.mp3 diff --git a/tests/files/assets/zero/zero.mp4 b/tests/files/assets/zero/zero.mp4 new file mode 100644 index 00000000..4cd44adc Binary files /dev/null and b/tests/files/assets/zero/zero.mp4 differ diff --git a/tests/files/assets/zero/zero.wav b/tests/files/assets/zero/zero.wav new file mode 100644 index 00000000..7b48f245 Binary files /dev/null and b/tests/files/assets/zero/zero.wav differ diff --git a/tests/files/main.rs b/tests/files/main.rs index 90018d27..d3b029cf 100644 --- a/tests/files/main.rs +++ b/tests/files/main.rs @@ -5,3 +5,4 @@ mod mpeg; mod ogg; pub(crate) mod util; mod wav; +mod zero_sized; diff --git a/tests/files/zero_sized.rs b/tests/files/zero_sized.rs new file mode 100644 index 00000000..c95cc158 --- /dev/null +++ b/tests/files/zero_sized.rs @@ -0,0 +1,48 @@ +use lofty::ape::ApeFile; +use lofty::flac::FlacFile; +use lofty::iff::{AiffFile, WavFile}; +use lofty::mp3::Mp3File; +use lofty::mp4::Mp4File; +use lofty::AudioFile; + +fn read_file(path: &str) -> bool { + let res = ::read_from(&mut std::fs::File::open(path).unwrap(), true); + res.is_ok() +} + +#[test] +fn zero_audio_aiff() { + // An AIFF files with a zero-size SSND chunk will error when attempting to read properties + assert!(!read_file::("tests/files/assets/zero/zero.aiff")); +} + +#[test] +fn zero_audio_ape() { + // An APE file with total_frames = 0 will error when attempting to read properties + assert!(!read_file::("tests/files/assets/zero/zero.ape")); +} + +#[test] +fn zero_audio_flac() { + assert!(read_file::("tests/files/assets/zero/zero.flac")); +} + +#[test] +fn zero_audio_mp3() { + // A zero-size MP3 will error, since we need MPEG frames to extract audio properties + assert!(!read_file::("tests/files/assets/zero/zero.mp3")); +} + +#[test] +fn zero_audio_mp4() { + // A zero-size MP4 will error, since we need an audio track to extract audio properties + assert!(!read_file::("tests/files/assets/zero/zero.mp4")); +} + +// zero-size Vorbis, Opus, and Speex files are invalid + +#[test] +fn zero_audio_wav() { + // An empty "data" chunk is an error + assert!(!read_file::("tests/files/assets/zero/zero.wav")); +} diff --git a/tests/tags/assets/id3v2/zero.id3v2 b/tests/tags/assets/id3v2/zero.id3v2 new file mode 100644 index 00000000..401c63b6 Binary files /dev/null and b/tests/tags/assets/id3v2/zero.id3v2 differ