diff --git a/tests/taglib/main.rs b/tests/taglib/main.rs index 521f552b5..d4d2b9663 100644 --- a/tests/taglib/main.rs +++ b/tests/taglib/main.rs @@ -11,8 +11,9 @@ mod test_info; mod test_mp4; mod test_mpeg; mod test_ogaflac; +mod test_ogg; +mod test_opus; mod test_speex; mod test_wav; mod test_wavpack; mod test_xiphcomment; -mod test_opus; diff --git a/tests/taglib/test_ogg.rs b/tests/taglib/test_ogg.rs new file mode 100644 index 000000000..5a86e319a --- /dev/null +++ b/tests/taglib/test_ogg.rs @@ -0,0 +1,156 @@ +use crate::temp_file; +use lofty::ogg::VorbisFile; +use lofty::{Accessor, AudioFile, ParseOptions}; + +use std::io::{Read, Seek, SeekFrom}; + +use byteorder::{LittleEndian, ReadBytesExt}; + +#[test] +fn test_simple() { + let mut file = temp_file!("tests/taglib/data/empty.ogg"); + + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + f.vorbis_comments_mut() + .set_artist(String::from("The Artist")); + f.save_to(&mut file).unwrap(); + } + file.rewind().unwrap(); + { + let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + assert_eq!(f.vorbis_comments().artist().as_deref(), Some("The Artist")); + } +} + +#[test] +#[ignore] +fn test_split_packets1() { + // Marker test, Lofty doesn't retain packet information +} + +#[test] +fn test_split_packets2() { + let mut file = temp_file!("tests/taglib/data/empty.ogg"); + + let text = "X".repeat(60890); + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + f.vorbis_comments_mut().set_title(text.clone()); + f.save_to(&mut file).unwrap(); + } + file.rewind().unwrap(); + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + assert_eq!(f.vorbis_comments().title().as_deref(), Some(&*text)); + + f.vorbis_comments_mut().set_title(String::from("ABCDE")); + f.save_to(&mut file).unwrap(); + } + file.rewind().unwrap(); + { + let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + assert_eq!(f.vorbis_comments().title().as_deref(), Some("ABCDE")); + } +} + +#[test] +#[ignore] +fn test_dict_interface1() { + // Marker test, Lofty doesn't replicate the dictionary interface +} + +#[test] +#[ignore] +fn test_dict_interface2() { + // Marker test, Lofty doesn't replicate the dictionary interface +} + +#[test] +fn test_audio_properties() { + let mut file = temp_file!("tests/taglib/data/empty.ogg"); + let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + assert_eq!(f.properties().duration().as_secs(), 3); + assert_eq!(f.properties().duration().as_millis(), 3685); + assert_eq!(f.properties().audio_bitrate(), 1); + assert_eq!(f.properties().channels(), 2); + assert_eq!(f.properties().sample_rate(), 44100); + assert_eq!(f.properties().version(), 0); + assert_eq!(f.properties().bitrate_max(), 0); + assert_eq!(f.properties().bitrate_nominal(), 112000); + assert_eq!(f.properties().bitrate_min(), 0); +} + +#[test] +fn test_page_checksum() { + let mut file = temp_file!("tests/taglib/data/empty.ogg"); + + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + f.vorbis_comments_mut() + .set_title(String::from("The Artist")); + f.save_to(&mut file).unwrap(); + + file.seek(SeekFrom::Start(0x50)).unwrap(); + assert_eq!(file.read_u32::().unwrap(), 0x3D3BD92D); + } + file.rewind().unwrap(); + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + f.vorbis_comments_mut() + .set_title(String::from("The Artist 2")); + f.save_to(&mut file).unwrap(); + + file.seek(SeekFrom::Start(0x50)).unwrap(); + assert_eq!(file.read_u32::().unwrap(), 0xD985291C); + } +} + +#[test] +fn test_page_granule_position() { + let mut file = temp_file!("tests/taglib/data/empty.ogg"); + + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + // Force the Vorbis comment packet to span more than one page and + // check if the granule position is -1 indicating that no packets + // finish on this page. + f.vorbis_comments_mut().set_comment("A".repeat(70000)); + f.save_to(&mut file).unwrap(); + + file.seek(SeekFrom::Start(0x3A)).unwrap(); + let mut buf = [0; 6]; + file.read_exact(&mut buf).unwrap(); + assert_eq!(buf, *b"OggS\0\0"); + assert_eq!(file.read_i64::().unwrap(), -1); + } + file.rewind().unwrap(); + { + let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap(); + file.rewind().unwrap(); + + // Use a small Vorbis comment package which ends on the seconds page and + // check if the granule position is zero. + f.vorbis_comments_mut() + .set_comment(String::from("A small comment")); + f.save_to(&mut file).unwrap(); + + file.seek(SeekFrom::Start(0x3A)).unwrap(); + let mut buf = [0; 6]; + file.read_exact(&mut buf).unwrap(); + assert_eq!(buf, *b"OggS\0\0"); + assert_eq!(file.read_i64::().unwrap(), 0); + } +}