Skip to content

Commit 46ae746

Browse files
committed
Tests: Add TagLib Musepack tests
1 parent 8a83913 commit 46ae746

File tree

10 files changed

+203
-0
lines changed

10 files changed

+203
-0
lines changed

tests/taglib/data/infloop.mpc

434 Bytes
Binary file not shown.

tests/taglib/data/segfault.mpc

19 Bytes
Binary file not shown.

tests/taglib/data/segfault2.mpc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MPCKSH

tests/taglib/data/sv4_header.mpc

128 Bytes
Binary file not shown.

tests/taglib/data/sv5_header.mpc

128 Bytes
Binary file not shown.

tests/taglib/data/sv8_header.mpc

114 Bytes
Binary file not shown.

tests/taglib/data/zerodiv.mpc

405 Bytes
Binary file not shown.

tests/taglib/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod test_id3v1;
1010
mod test_id3v2;
1111
mod test_info;
1212
mod test_mp4;
13+
mod test_mpc;
1314
mod test_mpeg;
1415
mod test_ogaflac;
1516
mod test_ogg;

tests/taglib/test_mpc.rs

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
use crate::temp_file;
2+
use crate::util::get_file;
3+
4+
use std::io::Seek;
5+
6+
use lofty::ape::ApeTag;
7+
use lofty::id3::v1::Id3v1Tag;
8+
use lofty::musepack::{MpcFile, MpcProperties};
9+
use lofty::{Accessor, AudioFile, ParseOptions, Probe, TagExt};
10+
11+
#[test]
12+
fn test_properties_sv8() {
13+
let f = get_file::<MpcFile>("tests/taglib/data/sv8_header.mpc");
14+
15+
let MpcProperties::Sv8(properties) = f.properties() else {
16+
panic!("Got the wrong properties somehow")
17+
};
18+
19+
assert_eq!(properties.version(), 8);
20+
assert_eq!(properties.duration().as_secs(), 1);
21+
assert_eq!(properties.duration().as_millis(), 1497);
22+
assert_eq!(properties.audio_bitrate(), 1);
23+
assert_eq!(properties.channels(), 2);
24+
assert_eq!(properties.sample_rate(), 44100);
25+
// TODO
26+
// assert_eq!(properties.sample_frames(), 66014);
27+
}
28+
29+
#[test]
30+
fn test_properties_sv7() {
31+
let f = get_file::<MpcFile>("tests/taglib/data/click.mpc");
32+
33+
let MpcProperties::Sv7(properties) = f.properties() else {
34+
panic!("Got the wrong properties somehow")
35+
};
36+
37+
assert_eq!(properties.duration().as_secs(), 0);
38+
assert_eq!(properties.duration().as_millis(), 40);
39+
assert_eq!(properties.audio_bitrate(), 318);
40+
assert_eq!(properties.channels(), 2);
41+
assert_eq!(properties.sample_rate(), 44100);
42+
// TODO
43+
// assert_eq!(properties.sample_frames(), 1760);
44+
45+
assert_eq!(properties.title_gain(), 14221);
46+
assert_eq!(properties.title_peak(), 19848);
47+
assert_eq!(properties.album_gain(), 14221);
48+
assert_eq!(properties.album_peak(), 19848);
49+
}
50+
51+
#[test]
52+
fn test_properties_sv5() {
53+
let f = get_file::<MpcFile>("tests/taglib/data/sv5_header.mpc");
54+
55+
let MpcProperties::Sv4to6(properties) = f.properties() else {
56+
panic!("Got the wrong properties somehow")
57+
};
58+
59+
assert_eq!(properties.stream_version(), 5);
60+
assert_eq!(properties.duration().as_secs(), 26);
61+
assert_eq!(properties.duration().as_millis(), 26371);
62+
assert_eq!(properties.audio_bitrate(), 0);
63+
assert_eq!(properties.channels(), 2);
64+
assert_eq!(properties.sample_rate(), 44100);
65+
// TODO
66+
// assert_eq!(properties.sample_frames(), 1162944);
67+
}
68+
69+
#[test]
70+
fn test_properties_sv4() {
71+
let f = get_file::<MpcFile>("tests/taglib/data/sv4_header.mpc");
72+
73+
let MpcProperties::Sv4to6(properties) = f.properties() else {
74+
panic!("Got the wrong properties somehow")
75+
};
76+
77+
assert_eq!(properties.stream_version(), 4);
78+
assert_eq!(properties.duration().as_secs(), 26);
79+
assert_eq!(properties.duration().as_millis(), 26371);
80+
assert_eq!(properties.audio_bitrate(), 0);
81+
assert_eq!(properties.channels(), 2);
82+
assert_eq!(properties.sample_rate(), 44100);
83+
// TODO
84+
// assert_eq!(properties.sample_frames(), 1162944);
85+
}
86+
87+
#[test]
88+
fn test_fuzzed_file1() {
89+
let _ = Probe::open("tests/taglib/data/zerodiv.mpc")
90+
.unwrap()
91+
.guess_file_type()
92+
.unwrap();
93+
}
94+
95+
#[test]
96+
fn test_fuzzed_file2() {
97+
let _ = Probe::open("tests/taglib/data/infloop.mpc")
98+
.unwrap()
99+
.guess_file_type()
100+
.unwrap();
101+
}
102+
103+
#[test]
104+
fn test_fuzzed_file3() {
105+
let _ = Probe::open("tests/taglib/data/segfault.mpc")
106+
.unwrap()
107+
.guess_file_type()
108+
.unwrap();
109+
}
110+
111+
#[test]
112+
fn test_fuzzed_file4() {
113+
let _ = Probe::open("tests/taglib/data/segfault2.mpc")
114+
.unwrap()
115+
.guess_file_type()
116+
.unwrap();
117+
}
118+
119+
#[test]
120+
fn test_strip_and_properties() {
121+
let mut file = temp_file!("tests/taglib/data/click.mpc");
122+
123+
{
124+
let mut f = MpcFile::read_from(&mut file, ParseOptions::new()).unwrap();
125+
file.rewind().unwrap();
126+
127+
let mut ape = ApeTag::new();
128+
ape.set_title(String::from("APE"));
129+
f.set_ape(ape);
130+
131+
let mut id3v1 = Id3v1Tag::new();
132+
id3v1.set_title(String::from("ID3v1"));
133+
f.set_id3v1(id3v1);
134+
f.save_to(&mut file).unwrap();
135+
}
136+
file.rewind().unwrap();
137+
{
138+
let mut f = MpcFile::read_from(&mut file, ParseOptions::new()).unwrap();
139+
file.rewind().unwrap();
140+
141+
assert_eq!(f.ape().unwrap().title().as_deref(), Some("APE"));
142+
f.ape_mut().unwrap().clear();
143+
assert_eq!(f.id3v1().unwrap().title().as_deref(), Some("ID3v1"));
144+
f.id3v1_mut().unwrap().clear();
145+
f.save_to(&mut file).unwrap();
146+
}
147+
file.rewind().unwrap();
148+
{
149+
let f = MpcFile::read_from(&mut file, ParseOptions::new()).unwrap();
150+
151+
assert!(f.ape().is_none());
152+
assert!(f.id3v1().is_none());
153+
}
154+
}
155+
156+
#[test]
157+
fn test_repeated_save() {
158+
let mut file = temp_file!("tests/taglib/data/click.mpc");
159+
160+
{
161+
let mut f = MpcFile::read_from(&mut file, ParseOptions::new()).unwrap();
162+
file.rewind().unwrap();
163+
164+
assert!(f.ape().is_none());
165+
assert!(f.id3v1().is_none());
166+
167+
let mut ape = ApeTag::new();
168+
ape.set_title(String::from("01234 56789 ABCDE FGHIJ"));
169+
f.set_ape(ape);
170+
171+
f.save_to(&mut file).unwrap();
172+
file.rewind().unwrap();
173+
174+
f.ape_mut().unwrap().set_title(String::from("0"));
175+
176+
f.save_to(&mut file).unwrap();
177+
file.rewind().unwrap();
178+
179+
let mut id3v1 = Id3v1Tag::new();
180+
id3v1.set_title(String::from("01234 56789 ABCDE FGHIJ"));
181+
f.set_id3v1(id3v1);
182+
f.ape_mut().unwrap().set_title(String::from(
183+
"01234 56789 ABCDE FGHIJ 01234 56789 ABCDE FGHIJ 01234 56789",
184+
));
185+
f.save_to(&mut file).unwrap();
186+
}
187+
file.rewind().unwrap();
188+
{
189+
let f = MpcFile::read_from(&mut file, ParseOptions::new()).unwrap();
190+
assert!(f.ape().is_some());
191+
assert!(f.id3v1().is_some());
192+
}
193+
}

tests/taglib/util/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use lofty::{AudioFile, ParseOptions};
2+
use std::fs::File;
3+
14
/// This function tries to simulate TagLibs isValid function
25
// https://github.com/Serial-ATA/lofty-rs/pull/51#discussion_r873171570
36
pub fn get_filetype<P: AsRef<std::path::Path>>(path: P) -> lofty::FileType {
@@ -7,6 +10,11 @@ pub fn get_filetype<P: AsRef<std::path::Path>>(path: P) -> lofty::FileType {
710
lofty::FileType::from_buffer(&buf).unwrap()
811
}
912

13+
pub fn get_file<F: AudioFile>(path: &str) -> F {
14+
let mut file = File::open(path).unwrap();
15+
F::read_from(&mut file, ParseOptions::new()).unwrap()
16+
}
17+
1018
#[macro_export]
1119
macro_rules! assert_delta {
1220
($x:expr, $y:expr, $d:expr) => {

0 commit comments

Comments
 (0)