lofty-rs/tests/files/zero_sized.rs

82 lines
2.2 KiB
Rust
Raw Normal View History

2022-05-07 16:13:35 +00:00
use lofty::ape::ApeFile;
use lofty::config::{ParseOptions, ParsingMode};
2022-05-07 16:13:35 +00:00
use lofty::flac::FlacFile;
use lofty::iff::aiff::AiffFile;
use lofty::iff::wav::WavFile;
2022-05-07 16:13:35 +00:00
use lofty::mp4::Mp4File;
use lofty::mpeg::MpegFile;
2024-04-10 18:08:28 +00:00
use lofty::prelude::*;
2022-05-07 16:13:35 +00:00
fn read_file_with_properties<A: AudioFile>(path: &str) -> bool {
let res = <A as AudioFile>::read_from(
&mut std::fs::File::open(path).unwrap(),
ParseOptions::new().parsing_mode(ParsingMode::Strict),
);
2022-05-07 16:13:35 +00:00
res.is_ok()
}
fn read_file_no_properties<A: AudioFile>(path: &str) -> bool {
2022-09-24 06:34:22 +00:00
let res = <A as AudioFile>::read_from(
&mut std::fs::File::open(path).unwrap(),
ParseOptions::new().read_properties(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
assert!(!read_file_with_properties::<MpegFile>(path));
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
}