Add tests for zero-size files

This commit is contained in:
Serial 2022-05-07 12:13:35 -04:00
parent cc2f806839
commit f0c2252eec
No known key found for this signature in database
GPG key ID: DA95198DC17C4568
11 changed files with 59 additions and 17 deletions

View file

@ -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());
}

View file

@ -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());
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -5,3 +5,4 @@ mod mpeg;
mod ogg;
pub(crate) mod util;
mod wav;
mod zero_sized;

48
tests/files/zero_sized.rs Normal file
View file

@ -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<A: AudioFile + std::fmt::Debug>(path: &str) -> bool {
let res = <A as AudioFile>::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::<AiffFile>("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::<ApeFile>("tests/files/assets/zero/zero.ape"));
}
#[test]
fn zero_audio_flac() {
assert!(read_file::<FlacFile>("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::<Mp3File>("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::<Mp4File>("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::<WavFile>("tests/files/assets/zero/zero.wav"));
}

Binary file not shown.