Skip to content

Commit

Permalink
Tests: Add TagLib OGG tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Apr 10, 2023
1 parent ec47461 commit 64ec405
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/taglib/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
156 changes: 156 additions & 0 deletions tests/taglib/test_ogg.rs
Original file line number Diff line number Diff line change
@@ -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::<LittleEndian>().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::<LittleEndian>().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::<LittleEndian>().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::<LittleEndian>().unwrap(), 0);
}
}

0 comments on commit 64ec405

Please sign in to comment.