From 6e6dd5e16dbbab3fe48be308997613e6d7447e42 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Sun, 15 May 2022 10:53:34 +0200 Subject: [PATCH 01/11] Add aiff tests from taglib --- tests/taglib/data/alaw.aifc | Bin 0 -> 1890 bytes tests/taglib/data/empty.aiff | Bin 0 -> 5936 bytes tests/taglib/data/excessive_alloc.aif | Bin 0 -> 2170 bytes tests/taglib/data/segfault.aif | Bin 0 -> 31 bytes tests/taglib/main.rs | 3 + tests/taglib/test_aiff.rs | 95 ++++++++++++++++++++++++++ tests/taglib/utils.rs | 94 +++++++++++++++++++++++++ 7 files changed, 192 insertions(+) create mode 100644 tests/taglib/data/alaw.aifc create mode 100644 tests/taglib/data/empty.aiff create mode 100644 tests/taglib/data/excessive_alloc.aif create mode 100644 tests/taglib/data/segfault.aif create mode 100644 tests/taglib/main.rs create mode 100644 tests/taglib/test_aiff.rs create mode 100644 tests/taglib/utils.rs diff --git a/tests/taglib/data/alaw.aifc b/tests/taglib/data/alaw.aifc new file mode 100644 index 0000000000000000000000000000000000000000..33b4ea2a5734c10af92aa556de6c50aa102efac6 GIT binary patch literal 1890 zcmX|B2~d+~8vb*iBm@uwf(e)eaX^Vl{)7;~EUlJSP=Q*Zvn~oOpa_aq?d(j=eIJC| zaD+e(?1G$0AV6@{+3u+9+6ubuw2Kr$%28cfS9i3#`_plKXTF*5d*0`J-s_uh-@)8$ z0KgQAv-V}~JMwNW0077?$>F`32eTm@NCaR2fIb2Q?a zKcBvT7oW|hi<2skU(6QgegF~P2aub$004iYw2Xd^URhmz1_0o{BBfO&ZxO^>PR$z* z@-62Ll9iK}4`E><0Jsdhyy=CMH5b`&Sjp))nJ`pl;_ z6=mh;>Fksgb|U@%{v2`kyO1AuvAVkCL}hjHDIhN|2dW8u3_^dk**7*b^Kd3_U)FCI zlq!laDQPJ+ISExx0J&9OfuK9vuW`Fj?E_q7&M|5v|8N zJnHJ}bE=3Lse+U=He0}ArH6X_F1vetK;xV8PpAO_9m*w{NCx+S(ZuB%tOC#+?mkw^qLB{nfJR)qOMEjMZg$Gflf^f^6$hJ&1B zPDvZ_1hJB`bRC?0RoxA8l4!k_y;&JwV{y7vvcaO*gMe&I{5)xS=Oq1R_ zG-~hfx!ULUm4PWjA(IuCm_$z{{NnSwT%BG0*ZMmr8o=1Z-El0YkjWHsqD<~hy4^Y9 zH8n?Z81%R}db}`=Po&fhxcYl~dR>#ohTKqWI(>J-t~e&|c)iLnGUe{NcCE{)seu}l zA&iIautaE!XDBf4bh|n`9m7|m*eOf~BR)Q!kxqK$3AkM@$9Q03K+}K%1@W;gW{N18 zhrDhaa@srXj=+FUeFMeKV8qg6nIb+=S+ALNLSuD}jVKFHygeyQ7Cn}cMmpK98xHs# z(4fY23rN050M*A}q;i0l+DWIw;SWqr`XoOi`58i{P>`OH1_l*dy`yfs9SS~Vc!37f z8LU_qL%_iWHyQ@V98glDLmtIl8W|J`p!l$m7(9qbCL)kw&pM=1wW8J5-rjDRKO3VI&>_K`WYjw7k65Xj93h*4p(O7fUan zF9}8yNko8JIlZvUGHHS2tvZEbm779NQvbCdUQ_;AY2<_@LG zsNeLM>o3#LP`BA>86ptH|QPF7>sgN`^HH$ zmb;1bNSLYxg~mHD;Psegs+QG*F+?y;#Np#$FL7(Qqu6izmaV z71vkmuc)*>?}%2?v3NWt3JH+0SR6DyYRQ^art^7ydYPj3<@u@*43q&I-Ld-M6cr%hxjX_1i2jX5W!7p*(Nfo;JzG+I{15Ma_0Ly}|GD$s%vaZ+zxes~7y0{h z4v60oAIv(ixA4&1%^Tm`fAHmZU*Ebpee3?=V;_HfwBT4l!S8ZEdU)^Nmk(!VXa5J= CPx9XY literal 0 HcmV?d00001 diff --git a/tests/taglib/data/empty.aiff b/tests/taglib/data/empty.aiff new file mode 100644 index 0000000000000000000000000000000000000000..849b762da5a8d2b1a366ea8f9d3daa87be7a194c GIT binary patch literal 5936 zcmZvgVQwTv423(HNysh|Li~U$a10O-i66^KWUs@GI3K&&Bs;v%pWW3FjFg$~>ay$k z`Po&S`}cpo+qNHm{Q39$`(NI_%k}o%cC~F^|FwPl(>H(rn)`en9{$MH_mAGy_x0sz zyScn<*V}fvT)yP`^Y+!{al5VYr|q1vZ98T3_VOH$*UY_K+QU_zo-%&QFHdgD@;TnF za>wXZ=GI^QGY3oV^T~Ja>pSx@`n27}{wBYtdWsM3z;u&U&i#iM{krI_O z-E&1cShdOuJrgt&b*~+-=is^xzH>-?$rV|9X-x3RO0RK(Zsl)(nwh7AuZ3<WvJdIXi!x_L_8YF1(~TQkBaalwL`7+%Jk_{SIQmHOgoqJ+?7(IA7am# zGe2!_E+3<6_1kJ~JlH&y`jd~cSotZ&Gf#~=&J|j?2hsTmF|v}L`_)L%&l%w zF+R5fTW6)YsURwMDoLeU&BO{u6*_7&IuWXy&V0=y#$&Zrjw{P!0h3seAFj;9q3Ysi z#oYR+EMZ|bE905fRZ=WW-j_$cPlifo<%bzWSk+pqapfK8d0s1U(;ro0_Qic<9qlZJ zu`h2GcR6SlSH#vGn5_%f^cueXtlr?f;OcsqS{$xHj8ARlTJNo+sxeR%(;};N=qRPW z!ZKMI1mMscD^AB%SEDudgHQCd_@md!ZS#{cVypUL)xO8WOKSaNY93oLs14XdhvP}> z@lx^Nsd2d*KE@YXTeen#ow~Yt=67ClMYgPu)tAhmTuj_mv1O?uuq@TlZ$X~h+v8FA0_OjEip56-DN zuD+z!=Co73%bPpwbFbHHR;GgJu%7rB0p;#JYlWM9FD=8#WA9F=3`=Hk-gvQ`K8MDqx2SL&(V{=p%9`L8`@~HIUm^C1KWce^NcdL%@ zY_?q$%*<6yM&oYsln6P$z)^EmtW`~vPphJ;eCKm|py)s~Cp;sT!@*glA7Q*6AlY!iay zPHdXtmMw=DSTP!p)%LVMK$`>)AUR+4Ytl^@x-ps zn!(SWMeJ9sM|S00O^h1AHCeN!CxYUSfHMQ1dtJ2_N2OEk6W1tC#%i7T@V#v^Ej#S9 zo}j2w?fFigVsTXJsA=U?wfYV`HI8lVeeKN*$dV|HR$F#6xfrEeHxuWqZ~Rmq=Mj}U zc&0M-i^bl1JGVT^UvX&lR+Tbpq-~)k^SDa<6^qp~$`dEi`p&Kb$k*`b)v7^tVI&}t zT@{wQMukp2HPP$ScQUU~{3i!xS-BZS5Zy4fuHqmc?^W`u1}w$wsSJhMBZ#8oZ>)Rd z?Q6VD#O}=gpT=ra3kwkzoOIL4@VOCim9PHz0wQ8U>$X#OC5t;kZmb-lE zC^MMlA6Ca6HRsRPN6#C*$TwAM9g|<)WbWdF5$d0q#8JOlM+|#cZd;hguaS-BMg#fX z^T+&Owp8hjgvtyGJ z;)OYww1SCDotP2)$T>jguA^lD-ij;sWMbtljgwqwxoT5JLHWAtp^0^l13eM?*0IhI zSj)ILr^17#5KTRms)(sa&v{RM6aSufJP{CUD<`uGQ86^i@Z7^{^ay#CbFK6~im4vc z46CCWX~y>);Hj8CvOM~kb=(oxyTn9=4O&rkR2wW)ZCz1$26I$bRDxAv?6 literal 0 HcmV?d00001 diff --git a/tests/taglib/data/excessive_alloc.aif b/tests/taglib/data/excessive_alloc.aif new file mode 100644 index 0000000000000000000000000000000000000000..9cb3a6e10defc7bff06bfa670aae859f687b0193 GIT binary patch literal 2170 zcmcIlT}V@57=EV``LmcKNCe}f=nrExr9=>Moi$x`oX!*NCMb8-oS8>9BPwfyBB8tP zg!C@D>ME$a0;3BtyeR4-qPmIjCdxq#wfuU&?|eIdZeYw7DPb#A}pNZ&U0Ggi=wtF;1DYC2pL>B_VWYar5*4`(#~N z_<=>ZpaSz%LRFxz?*#s;%ZzLB?zWc00MlJ(PbxiV+uWDdGTP|H%-;Aug8v(&+7#`mCyzlL8=zw%sby6T?4)GR&fpIJHN>kPkJSXd++h_!Rj(%2wt8LRHnt1cf4dlqg|ZT(92Nui%@`jT1_y7 zM3`MklA&1^MobLD01Lw!GQ$*GG-@TnVlKyr>l<1Q>z)=@v!ldn+RMb%6x{;fK%-lG znYVc1Z!J5#u1x0+w-T|80KQpMS^#qWXC#Fmo#;ClWUk%d=_*o6Fzle%MC;HDh5_8Q zpv~HFqRkph+S^D=I{qcl&L9=H*{Nubz}Z_#sSql%%2kB(glU@rg2_moC)*h#D-53K z`^VD&FTXx(+ECf8$aX3eKfO-8e>YfO<7I2&qb=N5iA9UNODSZhe;^ZuE| z%5suPsa-N1QIJ{|VvgPJ?yZNlZ3Ti83xrcEv= cyi`A-?dL@P#oEN=*U17yN%yzO_lEu9HE`C_@9WC|0>Qz4EsampleFrames()); + // CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isAiffC()); +} + +#[test] +#[ignore] +fn test_aifc_properties() { + let file = lofty::read_from_path("tests/taglib/data/alaw.aifc", true).unwrap(); + + assert_eq!(file.file_type(), FileType::AIFF); + + let properties = file.properties(); + assert_eq!(properties.duration().as_secs(), 0); + assert_eq!(properties.duration().as_millis(), 37); + assert_eq!(properties.audio_bitrate(), Some(355)); + assert_eq!(properties.sample_rate(), Some(44100)); + assert_eq!(properties.channels(), Some(1)); + assert_eq!(properties.bit_depth(), Some(16)); + //CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames()); + //CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC()); + //CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType()); + //CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName()); +} + +#[test] +#[ignore] +fn test_save_id3v2() { + let mut file = temp_file!("tests/taglib/data/empty.aiff"); + + { + let mut tfile = lofty::read_from(&mut file, true).unwrap(); + + assert_eq!(tfile.file_type(), FileType::AIFF); + + assert!(tfile.tag(&lofty::TagType::Id3v2).is_none()); + + let mut tag = lofty::Tag::new(lofty::TagType::Id3v2); + tag.set_title("TitleXXX".to_string()); + tfile.insert_tag(tag); + tfile.save_to(&mut file).unwrap(); + } + + { + let mut tfile = lofty::read_from(&mut file, true).unwrap(); + + assert_eq!(tfile.file_type(), FileType::AIFF); + + let mut tag = tfile.tag(&lofty::TagType::Id3v2).unwrap().to_owned(); + assert_eq!(tag.title(), Some("TitleXXX")); + tag.set_title("".to_string()); + tfile.insert_tag(tag); + tfile.save_to(&mut file).unwrap(); + } + + { + let tfile = lofty::read_from(&mut file, true).unwrap(); + + assert_eq!(tfile.file_type(), FileType::AIFF); + + assert!(tfile.tag(&lofty::TagType::Id3v2).is_none()); + } +} + +#[test] +#[ignore] +fn test_fuzzed_file1() { + let file = lofty::read_from_path("tests/taglib/data/segfault.aif", true).unwrap(); + + assert_eq!(file.file_type(), FileType::AIFF); +} + +#[test] +#[ignore] +fn test_fuzzed_file2() { + let file = lofty::read_from_path("tests/taglib/data/excessive_alloc.aif", true).unwrap(); + + assert_eq!(file.file_type(), FileType::AIFF); +} diff --git a/tests/taglib/utils.rs b/tests/taglib/utils.rs new file mode 100644 index 000000000..bf5a53f83 --- /dev/null +++ b/tests/taglib/utils.rs @@ -0,0 +1,94 @@ +#[macro_export] +macro_rules! temp_file { + ($path:tt) => {{ + use std::io::{Seek, Write}; + let mut file = tempfile::tempfile().unwrap(); + file.write_all(&std::fs::read($path).unwrap()).unwrap(); + + file.seek(std::io::SeekFrom::Start(0)).unwrap(); + + file + }}; +} + +#[macro_export] +macro_rules! verify_artist { + ($file:ident, $method:ident, $expected_value:literal, $item_count:expr) => {{ + println!("VERIFY: Expecting `{}` to have {} items, with an artist of \"{}\"", stringify!($method), $item_count, $expected_value); + + verify_artist!($file, $method(), $expected_value, $item_count) + }}; + ($file:ident, $method:ident, $arg:path, $expected_value:literal, $item_count:expr) => {{ + println!("VERIFY: Expecting `{}` to have {} items, with an artist of \"{}\"", stringify!($arg), $item_count, $expected_value); + + verify_artist!($file, $method($arg), $expected_value, $item_count) + }}; + ($file:ident, $method:ident($($arg:path)?), $expected_value:literal, $item_count:expr) => {{ + assert!($file.$method($(&$arg)?).is_some()); + + let tag = $file.$method($(&$arg)?).unwrap(); + + assert_eq!(tag.item_count(), $item_count); + + assert_eq!( + tag.get_item_ref(&ItemKey::TrackArtist), + Some(&TagItem::new( + ItemKey::TrackArtist, + ItemValue::Text(String::from($expected_value)) + )) + ); + + tag + }}; +} + +#[macro_export] +macro_rules! set_artist { + ($tagged_file:ident, $method:ident, $expected_value:literal, $item_count:expr => $file_write:ident, $new_value:literal) => { + let tag = verify_artist!($tagged_file, $method, $expected_value, $item_count); + println!( + "WRITE: Writing artist \"{}\" to {}\n", + $new_value, + stringify!($method) + ); + set_artist!($file_write, $new_value, tag) + }; + ($tagged_file:ident, $method:ident, $arg:path, $expected_value:literal, $item_count:expr => $file_write:ident, $new_value:literal) => { + let tag = verify_artist!($tagged_file, $method, $arg, $expected_value, $item_count); + println!( + "WRITE: Writing artist \"{}\" to {}\n", + $new_value, + stringify!($arg) + ); + set_artist!($file_write, $new_value, tag) + }; + ($file_write:ident, $new_value:literal, $tag:ident) => { + $tag.insert_item_unchecked(TagItem::new( + ItemKey::TrackArtist, + ItemValue::Text(String::from($new_value)), + )); + + $file_write.seek(std::io::SeekFrom::Start(0)).unwrap(); + + $tag.save_to(&mut $file_write).unwrap(); + }; +} + +#[macro_export] +macro_rules! remove_tag { + ($path:tt, $tag_type:path) => { + let mut file = temp_file!($path); + + let tagged_file = lofty::read_from(&mut file, false).unwrap(); + assert!(tagged_file.tag(&$tag_type).is_some()); + + file.seek(std::io::SeekFrom::Start(0)).unwrap(); + + $tag_type.remove_from(&mut file).unwrap(); + + file.seek(std::io::SeekFrom::Start(0)).unwrap(); + + let tagged_file = lofty::read_from(&mut file, false).unwrap(); + assert!(tagged_file.tag(&$tag_type).is_none()); + }; +} From fa88fef0f085c78f7b5a651310686fdafad80fe0 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Sun, 15 May 2022 10:56:19 +0200 Subject: [PATCH 02/11] fmt --- tests/taglib/test_aiff.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 69b189244..10e4698fa 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -33,10 +33,10 @@ fn test_aifc_properties() { assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); - //CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames()); - //CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC()); - //CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType()); - //CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName()); + // CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames()); + // CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC()); + // CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType()); + // CPPUNIT_ASSERT_EQUAL(String("SGI CCITT G.711 A-law"), f.audioProperties()->compressionName()); } #[test] From 36e500768c07f89a1d38a2348ea950d42534252e Mon Sep 17 00:00:00 2001 From: sagu <16504129+sagudev@users.noreply.github.com> Date: Sun, 15 May 2022 17:04:52 +0000 Subject: [PATCH 03/11] some fixes on test_aiff --- tests/taglib/test_aiff.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 10e4698fa..d75302ff6 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -1,3 +1,6 @@ +use std::fs::File; +use std::io::Read; + use crate::temp_file; use lofty::{Accessor, AudioFile, FileType}; @@ -10,7 +13,9 @@ fn test_aiff_properties() { let properties = file.properties(); assert_eq!(properties.duration().as_secs(), 0); - assert_eq!(properties.duration().as_millis(), 67); + // Durations are +/- 1ms due to rounding. + // Originaly here is 67ms + assert_eq!(properties.duration().as_millis(), 66); assert_eq!(properties.audio_bitrate(), Some(706)); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); @@ -81,15 +86,21 @@ fn test_save_id3v2() { #[test] #[ignore] fn test_fuzzed_file1() { - let file = lofty::read_from_path("tests/taglib/data/segfault.aif", true).unwrap(); + let mut file = File::open("tests/taglib/data/segfault.aif").unwrap(); - assert_eq!(file.file_type(), FileType::AIFF); + let mut buf = [0; 12]; + file.read_exact(&mut buf).unwrap(); + + assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); } #[test] #[ignore] fn test_fuzzed_file2() { - let file = lofty::read_from_path("tests/taglib/data/excessive_alloc.aif", true).unwrap(); + let mut file = File::open("tests/taglib/data/excessive_alloc.aif").unwrap(); - assert_eq!(file.file_type(), FileType::AIFF); + let mut buf = [0; 12]; + file.read_exact(&mut buf).unwrap(); + + assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); } From db48aac362ec68962ecde1156177a7a6c31dc1e2 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Mon, 11 Jul 2022 16:23:33 +0200 Subject: [PATCH 04/11] fix compile errors --- tests/taglib/test_aiff.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index d75302ff6..b878a7f2c 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -16,7 +16,7 @@ fn test_aiff_properties() { // Durations are +/- 1ms due to rounding. // Originaly here is 67ms assert_eq!(properties.duration().as_millis(), 66); - assert_eq!(properties.audio_bitrate(), Some(706)); + //assert_eq!(properties.audio_bitrate(), Some(706)); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); @@ -54,9 +54,9 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - assert!(tfile.tag(&lofty::TagType::Id3v2).is_none()); + assert!(tfile.tag(&lofty::TagType::ID3v2).is_none()); - let mut tag = lofty::Tag::new(lofty::TagType::Id3v2); + let mut tag = lofty::Tag::new(lofty::TagType::ID3v2); tag.set_title("TitleXXX".to_string()); tfile.insert_tag(tag); tfile.save_to(&mut file).unwrap(); @@ -67,7 +67,7 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - let mut tag = tfile.tag(&lofty::TagType::Id3v2).unwrap().to_owned(); + let mut tag = tfile.tag(&lofty::TagType::ID3v2).unwrap().to_owned(); assert_eq!(tag.title(), Some("TitleXXX")); tag.set_title("".to_string()); tfile.insert_tag(tag); @@ -79,7 +79,7 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - assert!(tfile.tag(&lofty::TagType::Id3v2).is_none()); + assert!(tfile.tag(&lofty::TagType::ID3v2).is_none()); } } From 944cd3e0059efdc74f1e32df48f5e663febf4f38 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Mon, 11 Jul 2022 16:32:00 +0200 Subject: [PATCH 05/11] fix some tests --- tests/taglib/test_aiff.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index b878a7f2c..321775fba 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -13,10 +13,10 @@ fn test_aiff_properties() { let properties = file.properties(); assert_eq!(properties.duration().as_secs(), 0); - // Durations are +/- 1ms due to rounding. - // Originaly here is 67ms + // Originaly here is 67ms ± 1ms due to rounding assert_eq!(properties.duration().as_millis(), 66); - //assert_eq!(properties.audio_bitrate(), Some(706)); + // originaly 706 + assert_eq!(properties.audio_bitrate(), Some(705)); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); @@ -33,7 +33,8 @@ fn test_aifc_properties() { let properties = file.properties(); assert_eq!(properties.duration().as_secs(), 0); - assert_eq!(properties.duration().as_millis(), 37); + // originaly 37 + assert_eq!(properties.duration().as_millis(), 36); assert_eq!(properties.audio_bitrate(), Some(355)); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); From 8bd07bd1c0e07229687e89cb38aa749e070fd257 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Mon, 11 Jul 2022 17:05:10 +0200 Subject: [PATCH 06/11] assert_delta --- tests/taglib/main.rs | 2 +- tests/taglib/test_aiff.rs | 11 ++++------- tests/taglib/{utils.rs => util/mod.rs} | 11 +++++++++++ 3 files changed, 16 insertions(+), 8 deletions(-) rename tests/taglib/{utils.rs => util/mod.rs} (94%) diff --git a/tests/taglib/main.rs b/tests/taglib/main.rs index c5a890af1..9da6db782 100644 --- a/tests/taglib/main.rs +++ b/tests/taglib/main.rs @@ -1,3 +1,3 @@ -pub(crate) mod utils; +pub(crate) mod util; mod test_aiff; diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 321775fba..423fa90b2 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -1,7 +1,7 @@ use std::fs::File; use std::io::Read; -use crate::temp_file; +use crate::{assert_delta, temp_file}; use lofty::{Accessor, AudioFile, FileType}; #[test] @@ -13,10 +13,8 @@ fn test_aiff_properties() { let properties = file.properties(); assert_eq!(properties.duration().as_secs(), 0); - // Originaly here is 67ms ± 1ms due to rounding - assert_eq!(properties.duration().as_millis(), 66); - // originaly 706 - assert_eq!(properties.audio_bitrate(), Some(705)); + assert_delta!(properties.duration().as_millis(), 67, 1); + assert_delta!(properties.audio_bitrate().unwrap(), 706, 1); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); @@ -33,8 +31,7 @@ fn test_aifc_properties() { let properties = file.properties(); assert_eq!(properties.duration().as_secs(), 0); - // originaly 37 - assert_eq!(properties.duration().as_millis(), 36); + assert_delta!(properties.duration().as_millis(), 37, 1); assert_eq!(properties.audio_bitrate(), Some(355)); assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); diff --git a/tests/taglib/utils.rs b/tests/taglib/util/mod.rs similarity index 94% rename from tests/taglib/utils.rs rename to tests/taglib/util/mod.rs index bf5a53f83..c4ae344c6 100644 --- a/tests/taglib/utils.rs +++ b/tests/taglib/util/mod.rs @@ -1,3 +1,14 @@ +#[macro_export] +macro_rules! assert_delta { + ($x:expr, $y:expr, $d:expr) => { + if $x > $y { + assert!($x - $y <= $d) + } else if $y > $x { + assert!($y - $x <= $d) + } + }; +} + #[macro_export] macro_rules! temp_file { ($path:tt) => {{ From 513e1d2979fe6fb394827b957bc065870b3163ba Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 12 Jul 2022 15:55:02 +0200 Subject: [PATCH 07/11] make requested changes --- tests/taglib/test_aiff.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 423fa90b2..1f99edab9 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -92,6 +92,8 @@ fn test_fuzzed_file1() { assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); } +/* +// the file doesn't even have a valid signature #[test] #[ignore] fn test_fuzzed_file2() { @@ -102,3 +104,4 @@ fn test_fuzzed_file2() { assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); } +*/ From e6b14134b041889b5107107a615b1a58fb35a685 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Tue, 12 Jul 2022 15:57:56 +0200 Subject: [PATCH 08/11] fmt --- tests/taglib/test_aiff.rs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 1f99edab9..b16304cb4 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -92,16 +92,14 @@ fn test_fuzzed_file1() { assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); } -/* // the file doesn't even have a valid signature -#[test] -#[ignore] -fn test_fuzzed_file2() { - let mut file = File::open("tests/taglib/data/excessive_alloc.aif").unwrap(); - - let mut buf = [0; 12]; - file.read_exact(&mut buf).unwrap(); - - assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); -} -*/ +// #[test] +// #[ignore] +// fn test_fuzzed_file2() { +// let mut file = File::open("tests/taglib/data/excessive_alloc.aif").unwrap(); +// +// let mut buf = [0; 12]; +// file.read_exact(&mut buf).unwrap(); +// +// assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); +// } From 1786e77fcd32c7be0c81d747cac870536f4eaea9 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Sat, 16 Jul 2022 14:02:52 +0200 Subject: [PATCH 09/11] fix test WIP --- tests/taglib/test_aiff.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index b16304cb4..07b778df7 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -1,5 +1,5 @@ use std::fs::File; -use std::io::Read; +use std::io::{Read, Seek}; use crate::{assert_delta, temp_file}; use lofty::{Accessor, AudioFile, FileType}; @@ -57,9 +57,12 @@ fn test_save_id3v2() { let mut tag = lofty::Tag::new(lofty::TagType::ID3v2); tag.set_title("TitleXXX".to_string()); tfile.insert_tag(tag); + file.rewind().unwrap(); tfile.save_to(&mut file).unwrap(); } + file.rewind().unwrap(); + { let mut tfile = lofty::read_from(&mut file, true).unwrap(); @@ -69,9 +72,12 @@ fn test_save_id3v2() { assert_eq!(tag.title(), Some("TitleXXX")); tag.set_title("".to_string()); tfile.insert_tag(tag); + file.rewind().unwrap(); tfile.save_to(&mut file).unwrap(); } + file.rewind().unwrap(); + { let tfile = lofty::read_from(&mut file, true).unwrap(); From 7ba156b98a031c0b68183efcfe8634f5b6730768 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Thu, 11 Aug 2022 16:56:55 +0200 Subject: [PATCH 10/11] create get_filetype util --- tests/taglib/test_aiff.rs | 20 +++++++++----------- tests/taglib/util/mod.rs | 9 +++++++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index 07b778df7..b2c2cef8c 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -1,6 +1,6 @@ -use std::fs::File; -use std::io::{Read, Seek}; +use std::io::Seek; +use crate::util::get_filetype; use crate::{assert_delta, temp_file}; use lofty::{Accessor, AudioFile, FileType}; @@ -52,7 +52,7 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - assert!(tfile.tag(&lofty::TagType::ID3v2).is_none()); + assert!(tfile.tag(lofty::TagType::ID3v2).is_none()); let mut tag = lofty::Tag::new(lofty::TagType::ID3v2); tag.set_title("TitleXXX".to_string()); @@ -68,7 +68,7 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - let mut tag = tfile.tag(&lofty::TagType::ID3v2).unwrap().to_owned(); + let mut tag = tfile.tag(lofty::TagType::ID3v2).unwrap().to_owned(); assert_eq!(tag.title(), Some("TitleXXX")); tag.set_title("".to_string()); tfile.insert_tag(tag); @@ -83,19 +83,17 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - assert!(tfile.tag(&lofty::TagType::ID3v2).is_none()); + assert!(tfile.tag(lofty::TagType::ID3v2).is_none()); } } #[test] #[ignore] fn test_fuzzed_file1() { - let mut file = File::open("tests/taglib/data/segfault.aif").unwrap(); - - let mut buf = [0; 12]; - file.read_exact(&mut buf).unwrap(); - - assert_eq!(FileType::from_buffer(&buf).unwrap(), FileType::AIFF); + assert_eq!( + get_filetype("tests/taglib/data/segfault.aif"), + FileType::AIFF + ); } // the file doesn't even have a valid signature diff --git a/tests/taglib/util/mod.rs b/tests/taglib/util/mod.rs index c4ae344c6..f81c89ca1 100644 --- a/tests/taglib/util/mod.rs +++ b/tests/taglib/util/mod.rs @@ -1,3 +1,12 @@ +/// This function tries to simulate TagLibs isValid function +// https://github.com/Serial-ATA/lofty-rs/pull/51#discussion_r873171570 +pub fn get_filetype>(path: P) -> lofty::FileType { + let mut file = std::fs::File::open(path).unwrap(); + let mut buf = [0; 12]; + std::io::Read::read_exact(&mut file, &mut buf).unwrap(); + lofty::FileType::from_buffer(&buf).unwrap() +} + #[macro_export] macro_rules! assert_delta { ($x:expr, $y:expr, $d:expr) => { From d251a589a4a987a796d875f6ddc9c995d3d1a8f3 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Thu, 11 Aug 2022 17:08:43 +0200 Subject: [PATCH 11/11] Make test_save_id3v2 more analogous to taglib test --- tests/taglib/test_aiff.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/taglib/test_aiff.rs b/tests/taglib/test_aiff.rs index b2c2cef8c..4361f9bcc 100644 --- a/tests/taglib/test_aiff.rs +++ b/tests/taglib/test_aiff.rs @@ -18,6 +18,7 @@ fn test_aiff_properties() { assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); + // TODO: get those options in lofty // CPPUNIT_ASSERT_EQUAL(2941U, f.audioProperties()->sampleFrames()); // CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isAiffC()); } @@ -36,6 +37,7 @@ fn test_aifc_properties() { assert_eq!(properties.sample_rate(), Some(44100)); assert_eq!(properties.channels(), Some(1)); assert_eq!(properties.bit_depth(), Some(16)); + // TODO: get those options in lofty // CPPUNIT_ASSERT_EQUAL(1622U, f.audioProperties()->sampleFrames()); // CPPUNIT_ASSERT_EQUAL(true, f.audioProperties()->isAiffC()); // CPPUNIT_ASSERT_EQUAL(ByteVector("ALAW"), f.audioProperties()->compressionType()); @@ -59,6 +61,7 @@ fn test_save_id3v2() { tfile.insert_tag(tag); file.rewind().unwrap(); tfile.save_to(&mut file).unwrap(); + assert!(tfile.contains_tag_type(lofty::TagType::ID3v2)); } file.rewind().unwrap(); @@ -74,6 +77,7 @@ fn test_save_id3v2() { tfile.insert_tag(tag); file.rewind().unwrap(); tfile.save_to(&mut file).unwrap(); + assert!(!tfile.contains_tag_type(lofty::TagType::ID3v2)); } file.rewind().unwrap(); @@ -83,10 +87,13 @@ fn test_save_id3v2() { assert_eq!(tfile.file_type(), FileType::AIFF); - assert!(tfile.tag(lofty::TagType::ID3v2).is_none()); + assert!(!tfile.contains_tag_type(lofty::TagType::ID3v2)); } } +// TODO: testSaveID3v23 +// TODO: testDuplicateID3v2 + #[test] #[ignore] fn test_fuzzed_file1() {