Test: Verify we can read zero-sized files with properties=false

This commit is contained in:
Serial 2022-07-05 22:40:15 -04:00
parent e245155f99
commit fa7920638a
No known key found for this signature in database
GPG key ID: DA95198DC17C4568

View file

@ -5,49 +5,71 @@ use lofty::mp3::Mp3File;
use lofty::mp4::Mp4File; use lofty::mp4::Mp4File;
use lofty::AudioFile; use lofty::AudioFile;
// TODO: zero-size mdat mp4
// TODO: zero-size vorbis comments // TODO: zero-size vorbis comments
// TODO: zero-size APE tag
// TODO: zero-size ilst
fn read_file<A: AudioFile>(path: &str) -> bool { fn read_file_with_properties<A: AudioFile>(path: &str) -> bool {
let res = <A as AudioFile>::read_from(&mut std::fs::File::open(path).unwrap(), true); let res = <A as AudioFile>::read_from(&mut std::fs::File::open(path).unwrap(), true);
res.is_ok() res.is_ok()
} }
fn read_file_no_properties<A: AudioFile>(path: &str) -> bool {
let res = <A as AudioFile>::read_from(&mut std::fs::File::open(path).unwrap(), false);
res.is_ok()
}
#[test] #[test]
fn zero_audio_aiff() { fn zero_audio_aiff() {
let path = "tests/files/assets/zero/zero.aiff";
// An AIFF files with a zero-size SSND chunk will error when attempting to read properties // 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")); assert!(!read_file_with_properties::<AiffFile>(path));
assert!(read_file_no_properties::<AiffFile>(path));
} }
#[test] #[test]
fn zero_audio_ape() { fn zero_audio_ape() {
let path = "tests/files/assets/zero/zero.ape";
// An APE file with total_frames = 0 will error when attempting to read properties // An APE file with total_frames = 0 will error when attempting to read properties
assert!(!read_file::<ApeFile>("tests/files/assets/zero/zero.ape")); assert!(!read_file_with_properties::<ApeFile>(path));
assert!(read_file_no_properties::<ApeFile>(path))
} }
#[test] #[test]
fn zero_audio_flac() { fn zero_audio_flac() {
assert!(read_file::<FlacFile>("tests/files/assets/zero/zero.flac")); let path = "tests/files/assets/zero/zero.flac";
assert!(read_file_with_properties::<FlacFile>(path));
assert!(read_file_no_properties::<FlacFile>(path));
} }
#[test] #[test]
fn zero_audio_mp3() { fn zero_audio_mp3() {
let path = "tests/files/assets/zero/zero.mp3";
// A zero-size MP3 will error, since we need MPEG frames to extract audio properties // 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")); assert!(!read_file_with_properties::<Mp3File>(path));
assert!(read_file_no_properties::<Mp3File>(path))
} }
#[test] #[test]
fn zero_audio_mp4() { fn zero_audio_mp4() {
let path = "tests/files/assets/zero/zero.mp4";
// A zero-size MP4 will error, since we need an audio track to extract audio properties // 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")); assert!(!read_file_with_properties::<Mp4File>(path));
assert!(read_file_no_properties::<Mp4File>(path))
} }
// zero-size Vorbis, Opus, and Speex files are invalid // zero-size Vorbis, Opus, and Speex files are invalid
#[test] #[test]
fn zero_audio_wav() { fn zero_audio_wav() {
let path = "tests/files/assets/zero/zero.wav";
// An empty "data" chunk is an error // An empty "data" chunk is an error
assert!(!read_file::<WavFile>("tests/files/assets/zero/zero.wav")); assert!(!read_file_with_properties::<WavFile>(path));
assert!(read_file_no_properties::<WavFile>(path));
} }