| 1 | /*************************************************************************** |
| 2 | copyright : (C) 2008 by Lukas Lalinsky |
| 3 | email : [email protected] |
| 4 | ***************************************************************************/ |
| 5 | |
| 6 | /*************************************************************************** |
| 7 | * This library is free software; you can redistribute it and/or modify * |
| 8 | * it under the terms of the GNU Lesser General Public License version * |
| 9 | * 2.1 as published by the Free Software Foundation. * |
| 10 | * * |
| 11 | * This library is distributed in the hope that it will be useful, but * |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 14 | * Lesser General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Lesser General Public * |
| 17 | * License along with this library; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * |
| 19 | * 02110-1301 USA * |
| 20 | * * |
| 21 | * Alternatively, this file is available under the Mozilla Public * |
| 22 | * License Version 1.1. You may obtain a copy of the License at * |
| 23 | * http://www.mozilla.org/MPL/ * |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #include <string> |
| 27 | #include <cstdio> |
| 28 | |
| 29 | #include "tstringlist.h" |
| 30 | #include "tbytevectorlist.h" |
| 31 | #include "tpropertymap.h" |
| 32 | #include "tag.h" |
| 33 | #include "asffile.h" |
| 34 | #include <cppunit/extensions/HelperMacros.h> |
| 35 | #include "utils.h" |
| 36 | |
| 37 | using namespace std; |
| 38 | using namespace TagLib; |
| 39 | |
| 40 | class TestASF : public CppUnit::TestFixture |
| 41 | { |
| 42 | CPPUNIT_TEST_SUITE(TestASF); |
| 43 | CPPUNIT_TEST(testAudioProperties); |
| 44 | CPPUNIT_TEST(testLosslessProperties); |
| 45 | CPPUNIT_TEST(testRead); |
| 46 | CPPUNIT_TEST(testSaveMultipleValues); |
| 47 | CPPUNIT_TEST(testSaveStream); |
| 48 | CPPUNIT_TEST(testSaveLanguage); |
| 49 | CPPUNIT_TEST(testDWordTrackNumber); |
| 50 | CPPUNIT_TEST(testSaveLargeValue); |
| 51 | CPPUNIT_TEST(testSavePicture); |
| 52 | CPPUNIT_TEST(testSaveMultiplePictures); |
| 53 | CPPUNIT_TEST(testProperties); |
| 54 | CPPUNIT_TEST(testPropertiesAllSupported); |
| 55 | CPPUNIT_TEST(testRepeatedSave); |
| 56 | CPPUNIT_TEST_SUITE_END(); |
| 57 | |
| 58 | public: |
| 59 | |
| 60 | void testAudioProperties() |
| 61 | { |
| 62 | ASF::File f(TEST_FILE_PATH_C("silence-1.wma")); |
| 63 | CPPUNIT_ASSERT(f.audioProperties()); |
| 64 | CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds()); |
| 65 | CPPUNIT_ASSERT_EQUAL(3712, f.audioProperties()->lengthInMilliseconds()); |
| 66 | CPPUNIT_ASSERT_EQUAL(64, f.audioProperties()->bitrate()); |
| 67 | CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels()); |
| 68 | CPPUNIT_ASSERT_EQUAL(48000, f.audioProperties()->sampleRate()); |
| 69 | CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample()); |
| 70 | CPPUNIT_ASSERT_EQUAL(ASF::Properties::WMA2, f.audioProperties()->codec()); |
| 71 | CPPUNIT_ASSERT_EQUAL(String("Windows Media Audio 9.1"), f.audioProperties()->codecName()); |
| 72 | CPPUNIT_ASSERT_EQUAL(String("64 kbps, 48 kHz, stereo 2-pass CBR"), f.audioProperties()->codecDescription()); |
| 73 | CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted()); |
| 74 | } |
| 75 | |
| 76 | void testLosslessProperties() |
| 77 | { |
| 78 | ASF::File f(TEST_FILE_PATH_C("lossless.wma")); |
| 79 | CPPUNIT_ASSERT(f.audioProperties()); |
| 80 | CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds()); |
| 81 | CPPUNIT_ASSERT_EQUAL(3549, f.audioProperties()->lengthInMilliseconds()); |
| 82 | CPPUNIT_ASSERT_EQUAL(1152, f.audioProperties()->bitrate()); |
| 83 | CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels()); |
| 84 | CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate()); |
| 85 | CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample()); |
| 86 | CPPUNIT_ASSERT_EQUAL(ASF::Properties::WMA9Lossless, f.audioProperties()->codec()); |
| 87 | CPPUNIT_ASSERT_EQUAL(String("Windows Media Audio 9.2 Lossless"), f.audioProperties()->codecName()); |
| 88 | CPPUNIT_ASSERT_EQUAL(String("VBR Quality 100, 44 kHz, 2 channel 16 bit 1-pass VBR"), f.audioProperties()->codecDescription()); |
| 89 | CPPUNIT_ASSERT_EQUAL(false, f.audioProperties()->isEncrypted()); |
| 90 | } |
| 91 | |
| 92 | void testRead() |
| 93 | { |
| 94 | ASF::File f(TEST_FILE_PATH_C("silence-1.wma")); |
| 95 | CPPUNIT_ASSERT_EQUAL(String("test"), f.tag()->title()); |
| 96 | } |
| 97 | |
| 98 | void testSaveMultipleValues() |
| 99 | { |
| 100 | ScopedFileCopy copy("silence-1", ".wma"); |
| 101 | string newname = copy.fileName(); |
| 102 | |
| 103 | { |
| 104 | ASF::File f(newname.c_str()); |
| 105 | ASF::AttributeList values; |
| 106 | values.append("Foo"); |
| 107 | values.append("Bar"); |
| 108 | f.tag()->setAttribute("WM/AlbumTitle", values); |
| 109 | f.save(); |
| 110 | } |
| 111 | { |
| 112 | ASF::File f(newname.c_str()); |
| 113 | CPPUNIT_ASSERT_EQUAL(2, static_cast<int>( |
| 114 | f.tag()->attributeListMap()["WM/AlbumTitle"].size())); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void testDWordTrackNumber() |
| 119 | { |
| 120 | ScopedFileCopy copy("silence-1", ".wma"); |
| 121 | string newname = copy.fileName(); |
| 122 | |
| 123 | { |
| 124 | ASF::File f(newname.c_str()); |
| 125 | CPPUNIT_ASSERT(!f.tag()->contains("WM/TrackNumber")); |
| 126 | f.tag()->setAttribute("WM/TrackNumber", static_cast<unsigned int>(123)); |
| 127 | f.save(); |
| 128 | } |
| 129 | { |
| 130 | ASF::File f(newname.c_str()); |
| 131 | CPPUNIT_ASSERT(f.tag()->contains("WM/TrackNumber")); |
| 132 | CPPUNIT_ASSERT_EQUAL(ASF::Attribute::DWordType, |
| 133 | f.tag()->attribute("WM/TrackNumber").front().type()); |
| 134 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(123), f.tag()->track()); |
| 135 | f.tag()->setTrack(234); |
| 136 | f.save(); |
| 137 | } |
| 138 | { |
| 139 | ASF::File f(newname.c_str()); |
| 140 | CPPUNIT_ASSERT(f.tag()->contains("WM/TrackNumber")); |
| 141 | CPPUNIT_ASSERT_EQUAL(ASF::Attribute::UnicodeType, |
| 142 | f.tag()->attribute("WM/TrackNumber").front().type()); |
| 143 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(234), f.tag()->track()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void testSaveStream() |
| 148 | { |
| 149 | ScopedFileCopy copy("silence-1", ".wma"); |
| 150 | string newname = copy.fileName(); |
| 151 | |
| 152 | { |
| 153 | ASF::File f(newname.c_str()); |
| 154 | ASF::Attribute attr("Foo"); |
| 155 | attr.setStream(43); |
| 156 | f.tag()->setAttribute("WM/AlbumTitle", attr); |
| 157 | f.save(); |
| 158 | } |
| 159 | |
| 160 | { |
| 161 | ASF::File f(newname.c_str()); |
| 162 | CPPUNIT_ASSERT_EQUAL(43, f.tag()->attribute("WM/AlbumTitle").front().stream()); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void testSaveLanguage() |
| 167 | { |
| 168 | ScopedFileCopy copy("silence-1", ".wma"); |
| 169 | string newname = copy.fileName(); |
| 170 | |
| 171 | { |
| 172 | ASF::File f(newname.c_str()); |
| 173 | ASF::Attribute attr("Foo"); |
| 174 | attr.setStream(32); |
| 175 | attr.setLanguage(56); |
| 176 | f.tag()->setAttribute("WM/AlbumTitle", attr); |
| 177 | f.save(); |
| 178 | } |
| 179 | { |
| 180 | ASF::File f(newname.c_str()); |
| 181 | CPPUNIT_ASSERT_EQUAL(32, f.tag()->attribute("WM/AlbumTitle").front().stream()); |
| 182 | CPPUNIT_ASSERT_EQUAL(56, f.tag()->attribute("WM/AlbumTitle").front().language()); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void testSaveLargeValue() |
| 187 | { |
| 188 | ScopedFileCopy copy("silence-1", ".wma"); |
| 189 | string newname = copy.fileName(); |
| 190 | |
| 191 | { |
| 192 | ASF::File f(newname.c_str()); |
| 193 | ASF::Attribute attr(ByteVector(70000, 'x')); |
| 194 | f.tag()->setAttribute("WM/Blob", attr); |
| 195 | f.save(); |
| 196 | } |
| 197 | { |
| 198 | ASF::File f(newname.c_str()); |
| 199 | CPPUNIT_ASSERT_EQUAL(ByteVector(70000, 'x'), |
| 200 | f.tag()->attribute("WM/Blob").front().toByteVector()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void testSavePicture() |
| 205 | { |
| 206 | ScopedFileCopy copy("silence-1", ".wma"); |
| 207 | string newname = copy.fileName(); |
| 208 | |
| 209 | { |
| 210 | ASF::File f(newname.c_str()); |
| 211 | ASF::Picture picture; |
| 212 | picture.setMimeType("image/jpeg"); |
| 213 | picture.setType(ASF::Picture::FrontCover); |
| 214 | picture.setDescription("description"); |
| 215 | picture.setPicture("data"); |
| 216 | f.tag()->setAttribute("WM/Picture", picture); |
| 217 | f.save(); |
| 218 | } |
| 219 | { |
| 220 | ASF::File f(newname.c_str()); |
| 221 | ASF::AttributeList values2 = f.tag()->attribute("WM/Picture"); |
| 222 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), values2.size()); |
| 223 | ASF::Attribute attr2 = values2.front(); |
| 224 | ASF::Picture picture2 = attr2.toPicture(); |
| 225 | CPPUNIT_ASSERT(picture2.isValid()); |
| 226 | CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), picture2.mimeType()); |
| 227 | CPPUNIT_ASSERT_EQUAL(ASF::Picture::FrontCover, picture2.type()); |
| 228 | CPPUNIT_ASSERT_EQUAL(String("description"), picture2.description()); |
| 229 | CPPUNIT_ASSERT_EQUAL(ByteVector("data"), picture2.picture()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void testSaveMultiplePictures() |
| 234 | { |
| 235 | ScopedFileCopy copy("silence-1", ".wma"); |
| 236 | string newname = copy.fileName(); |
| 237 | |
| 238 | { |
| 239 | ASF::File f(newname.c_str()); |
| 240 | ASF::AttributeList values; |
| 241 | ASF::Picture picture; |
| 242 | picture.setMimeType("image/jpeg"); |
| 243 | picture.setType(ASF::Picture::FrontCover); |
| 244 | picture.setDescription("description"); |
| 245 | picture.setPicture("data"); |
| 246 | values.append(ASF::Attribute(picture)); |
| 247 | ASF::Picture picture2; |
| 248 | picture2.setMimeType("image/png"); |
| 249 | picture2.setType(ASF::Picture::BackCover); |
| 250 | picture2.setDescription("back cover"); |
| 251 | picture2.setPicture("PNG data"); |
| 252 | values.append(ASF::Attribute(picture2)); |
| 253 | f.tag()->setAttribute("WM/Picture", values); |
| 254 | f.save(); |
| 255 | } |
| 256 | { |
| 257 | ASF::File f(newname.c_str()); |
| 258 | ASF::AttributeList values2 = f.tag()->attribute("WM/Picture"); |
| 259 | CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), values2.size()); |
| 260 | ASF::Picture picture3 = values2[1].toPicture(); |
| 261 | CPPUNIT_ASSERT(picture3.isValid()); |
| 262 | CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), picture3.mimeType()); |
| 263 | CPPUNIT_ASSERT_EQUAL(ASF::Picture::FrontCover, picture3.type()); |
| 264 | CPPUNIT_ASSERT_EQUAL(String("description"), picture3.description()); |
| 265 | CPPUNIT_ASSERT_EQUAL(ByteVector("data"), picture3.picture()); |
| 266 | ASF::Picture picture4 = values2[0].toPicture(); |
| 267 | CPPUNIT_ASSERT(picture4.isValid()); |
| 268 | CPPUNIT_ASSERT_EQUAL(String("image/png"), picture4.mimeType()); |
| 269 | CPPUNIT_ASSERT_EQUAL(ASF::Picture::BackCover, picture4.type()); |
| 270 | CPPUNIT_ASSERT_EQUAL(String("back cover"), picture4.description()); |
| 271 | CPPUNIT_ASSERT_EQUAL(ByteVector("PNG data"), picture4.picture()); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void testProperties() |
| 276 | { |
| 277 | ASF::File f(TEST_FILE_PATH_C("silence-1.wma")); |
| 278 | |
| 279 | PropertyMap tags = f.properties(); |
| 280 | |
| 281 | tags["TRACKNUMBER"] = StringList("2"); |
| 282 | tags["DISCNUMBER"] = StringList("3"); |
| 283 | tags["BPM"] = StringList("123"); |
| 284 | tags["ARTIST"] = StringList("Foo Bar"); |
| 285 | f.setProperties(tags); |
| 286 | |
| 287 | tags = f.properties(); |
| 288 | |
| 289 | CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.tag()->artist()); |
| 290 | CPPUNIT_ASSERT_EQUAL(StringList("Foo Bar"), tags["ARTIST"]); |
| 291 | |
| 292 | CPPUNIT_ASSERT(f.tag()->contains("WM/BeatsPerMinute")); |
| 293 | CPPUNIT_ASSERT_EQUAL(1u, f.tag()->attributeListMap()["WM/BeatsPerMinute"].size()); |
| 294 | CPPUNIT_ASSERT_EQUAL(String("123"), f.tag()->attribute("WM/BeatsPerMinute").front().toString()); |
| 295 | CPPUNIT_ASSERT_EQUAL(StringList("123"), tags["BPM"]); |
| 296 | |
| 297 | CPPUNIT_ASSERT(f.tag()->contains("WM/TrackNumber")); |
| 298 | CPPUNIT_ASSERT_EQUAL(1u, f.tag()->attributeListMap()["WM/TrackNumber"].size()); |
| 299 | CPPUNIT_ASSERT_EQUAL(String("2"), f.tag()->attribute("WM/TrackNumber").front().toString()); |
| 300 | CPPUNIT_ASSERT_EQUAL(StringList("2"), tags["TRACKNUMBER"]); |
| 301 | |
| 302 | CPPUNIT_ASSERT(f.tag()->contains("WM/PartOfSet")); |
| 303 | CPPUNIT_ASSERT_EQUAL(1u, f.tag()->attributeListMap()["WM/PartOfSet"].size()); |
| 304 | CPPUNIT_ASSERT_EQUAL(String("3"), f.tag()->attribute("WM/PartOfSet").front().toString()); |
| 305 | CPPUNIT_ASSERT_EQUAL(StringList("3"), tags["DISCNUMBER"]); |
| 306 | } |
| 307 | |
| 308 | void testPropertiesAllSupported() |
| 309 | { |
| 310 | PropertyMap tags; |
| 311 | tags["ACOUSTID_ID"] = StringList("Acoustid ID"); |
| 312 | tags["ACOUSTID_FINGERPRINT"] = StringList("Acoustid Fingerprint"); |
| 313 | tags["ALBUM"] = StringList("Album"); |
| 314 | tags["ALBUMARTIST"] = StringList("Album Artist"); |
| 315 | tags["ALBUMARTISTSORT"] = StringList("Album Artist Sort"); |
| 316 | tags["ALBUMSORT"] = StringList("Album Sort"); |
| 317 | tags["ARTIST"] = StringList("Artist"); |
| 318 | tags["ARTISTS"] = StringList("Artists"); |
| 319 | tags["ARTISTSORT"] = StringList("Artist Sort"); |
| 320 | tags["ARTISTWEBPAGE"] = StringList("Artist Webpage"); |
| 321 | tags["ASIN"] = StringList("ASIN"); |
| 322 | tags["BARCODE"] = StringList("Barcode"); |
| 323 | tags["BPM"] = StringList("123"); |
| 324 | tags["CATALOGNUMBER"] = StringList("Catalog Number"); |
| 325 | tags["COMMENT"] = StringList("Comment"); |
| 326 | tags["COMPOSER"] = StringList("Composer"); |
| 327 | tags["CONDUCTOR"] = StringList("Conductor"); |
| 328 | tags["COPYRIGHT"] = StringList("2021 Copyright"); |
| 329 | tags["DATE"] = StringList("2021-01-03 12:29:23"); |
| 330 | tags["DISCNUMBER"] = StringList("3/5"); |
| 331 | tags["DISCSUBTITLE"] = StringList("Disc Subtitle"); |
| 332 | tags["ENCODEDBY"] = StringList("Encoded by"); |
| 333 | tags["ENCODING"] = StringList("Encoding"); |
| 334 | tags["ENCODINGTIME"] = StringList("2021-01-03 11:52:19"); |
| 335 | tags["FILEWEBPAGE"] = StringList("File Webpage"); |
| 336 | tags["GENRE"] = StringList("Genre"); |
| 337 | tags["WORK"] = StringList("Grouping"); |
| 338 | tags["INITIALKEY"] = StringList("Initial Key"); |
| 339 | tags["ISRC"] = StringList("UKAAA0500001"); |
| 340 | tags["LABEL"] = StringList("Label"); |
| 341 | tags["LANGUAGE"] = StringList("eng"); |
| 342 | tags["LYRICIST"] = StringList("Lyricist"); |
| 343 | tags["LYRICS"] = StringList("Lyrics"); |
| 344 | tags["MEDIA"] = StringList("Media"); |
| 345 | tags["MOOD"] = StringList("Mood"); |
| 346 | tags["MUSICBRAINZ_ALBUMARTISTID"] = StringList("MusicBrainz_AlbumartistID"); |
| 347 | tags["MUSICBRAINZ_ALBUMID"] = StringList("MusicBrainz_AlbumID"); |
| 348 | tags["MUSICBRAINZ_ARTISTID"] = StringList("MusicBrainz_ArtistID"); |
| 349 | tags["MUSICBRAINZ_RELEASEGROUPID"] = StringList("MusicBrainz_ReleasegroupID"); |
| 350 | tags["MUSICBRAINZ_RELEASETRACKID"] = StringList("MusicBrainz_ReleasetrackID"); |
| 351 | tags["MUSICBRAINZ_TRACKID"] = StringList("MusicBrainz_TrackID"); |
| 352 | tags["MUSICBRAINZ_WORKID"] = StringList("MusicBrainz_WorkID"); |
| 353 | tags["MUSICIP_PUID"] = StringList("MusicIP PUID"); |
| 354 | tags["ORIGINALALBUM"] = StringList("Original Album"); |
| 355 | tags["ORIGINALARTIST"] = StringList("Original Artist"); |
| 356 | tags["ORIGINALFILENAME"] = StringList("Original Filename"); |
| 357 | tags["ORIGINALLYRICIST"] = StringList("Original Lyricist"); |
| 358 | tags["ORIGINALDATE"] = StringList("2021-01-03 13:52:19"); |
| 359 | tags["PRODUCER"] = StringList("Producer"); |
| 360 | tags["RELEASECOUNTRY"] = StringList("Release Country"); |
| 361 | tags["RELEASESTATUS"] = StringList("Release Status"); |
| 362 | tags["RELEASETYPE"] = StringList("Release Type"); |
| 363 | tags["REMIXER"] = StringList("Remixer"); |
| 364 | tags["SCRIPT"] = StringList("Script"); |
| 365 | tags["SUBTITLE"] = StringList("Subtitle"); |
| 366 | tags["TITLE"] = StringList("Title"); |
| 367 | tags["TITLESORT"] = StringList("Title Sort"); |
| 368 | tags["TRACKNUMBER"] = StringList("2/4"); |
| 369 | |
| 370 | ScopedFileCopy copy("silence-1", ".wma"); |
| 371 | { |
| 372 | ASF::File f(copy.fileName().c_str()); |
| 373 | ASF::Tag *asfTag = f.tag(); |
| 374 | asfTag->setTitle(""); |
| 375 | asfTag->attributeListMap().clear(); |
| 376 | f.save(); |
| 377 | } |
| 378 | { |
| 379 | ASF::File f(copy.fileName().c_str()); |
| 380 | PropertyMap properties = f.properties(); |
| 381 | CPPUNIT_ASSERT(properties.isEmpty()); |
| 382 | f.setProperties(tags); |
| 383 | f.save(); |
| 384 | } |
| 385 | { |
| 386 | const ASF::File f(copy.fileName().c_str()); |
| 387 | PropertyMap properties = f.properties(); |
| 388 | if (tags != properties) { |
| 389 | CPPUNIT_ASSERT_EQUAL(tags.toString(), properties.toString()); |
| 390 | } |
| 391 | CPPUNIT_ASSERT(tags == properties); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void testRepeatedSave() |
| 396 | { |
| 397 | ScopedFileCopy copy("silence-1", ".wma"); |
| 398 | ASF::File f(copy.fileName().c_str()); |
| 399 | f.tag()->setTitle(longText(128 * 1024)); |
| 400 | f.save(); |
| 401 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(297578), f.length()); |
| 402 | f.tag()->setTitle(longText(16 * 1024)); |
| 403 | f.save(); |
| 404 | CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(68202), f.length()); |
| 405 | } |
| 406 | |
| 407 | }; |
| 408 | |
| 409 | CPPUNIT_TEST_SUITE_REGISTRATION(TestASF); |