lofty-rs/tests/files/zero_sized.rs

74 lines
2 KiB
Rust
Raw Normal View History

2022-05-07 16:13:35 +00:00
use lofty::ape::ApeFile;
use lofty::flac::FlacFile;
use lofty::iff::{AiffFile, WavFile};
2022-07-24 20:25:08 +00:00
use lofty::mp3::MPEGFile;
2022-05-07 16:13:35 +00:00
use lofty::mp4::Mp4File;
use lofty::AudioFile;
fn read_file_with_properties<A: AudioFile>(path: &str) -> bool {
2022-05-07 16:13:35 +00:00
let res = <A as AudioFile>::read_from(&mut std::fs::File::open(path).unwrap(), true);
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()
}
2022-05-07 16:13:35 +00:00
#[test]
fn zero_audio_aiff() {
let path = "tests/files/assets/zero/zero.aiff";
2022-05-07 16:13:35 +00:00
// An AIFF files with a zero-size SSND chunk will error when attempting to read properties
assert!(!read_file_with_properties::<AiffFile>(path));
assert!(read_file_no_properties::<AiffFile>(path));
2022-05-07 16:13:35 +00:00
}
#[test]
fn zero_audio_ape() {
let path = "tests/files/assets/zero/zero.ape";
2022-05-07 16:13:35 +00:00
// An APE file with total_frames = 0 will error when attempting to read properties
assert!(!read_file_with_properties::<ApeFile>(path));
assert!(read_file_no_properties::<ApeFile>(path))
2022-05-07 16:13:35 +00:00
}
#[test]
fn zero_audio_flac() {
let path = "tests/files/assets/zero/zero.flac";
assert!(read_file_with_properties::<FlacFile>(path));
assert!(read_file_no_properties::<FlacFile>(path));
2022-05-07 16:13:35 +00:00
}
#[test]
fn zero_audio_mp3() {
let path = "tests/files/assets/zero/zero.mp3";
2022-05-07 16:13:35 +00:00
// A zero-size MP3 will error, since we need MPEG frames to extract audio properties
2022-07-24 20:25:08 +00:00
assert!(!read_file_with_properties::<MPEGFile>(path));
2022-07-24 20:25:08 +00:00
assert!(read_file_no_properties::<MPEGFile>(path))
2022-05-07 16:13:35 +00:00
}
#[test]
fn zero_audio_mp4() {
let path = "tests/files/assets/zero/zero.mp4";
2022-05-07 16:13:35 +00:00
// A zero-size MP4 will error, since we need an audio track to extract audio properties
assert!(!read_file_with_properties::<Mp4File>(path));
assert!(read_file_no_properties::<Mp4File>(path))
2022-05-07 16:13:35 +00:00
}
// zero-size Vorbis, Opus, and Speex files are invalid
#[test]
fn zero_audio_wav() {
let path = "tests/files/assets/zero/zero.wav";
2022-05-07 16:13:35 +00:00
// An empty "data" chunk is an error
assert!(!read_file_with_properties::<WavFile>(path));
assert!(read_file_no_properties::<WavFile>(path));
2022-05-07 16:13:35 +00:00
}