Debian Codegraph @

tests/test_mpeg.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2007 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 #include <array>
29
30 #include "taglib_config.h"
31 #include "tstring.h"
32 #include "tpropertymap.h"
33 #include "mpegfile.h"
34 #include "id3v2tag.h"
35 #include "id3v1tag.h"
36 #ifdef TAGLIB_WITH_APE
37 #include "apetag.h"
38 #endif
39 #include "mpegproperties.h"
40 #include "xingheader.h"
41 #include "mpegheader.h"
42 #include "id3v2extendedheader.h"
43 #include <cppunit/extensions/HelperMacros.h>
44 #include "utils.h"
45
46 using namespace std;
47 using namespace TagLib;
48
49 class TestMPEG : public CppUnit::TestFixture
50 {
51 CPPUNIT_TEST_SUITE(TestMPEG);
52 CPPUNIT_TEST(testAudioPropertiesXingHeaderCBR);
53 CPPUNIT_TEST(testAudioPropertiesXingHeaderVBR);
54 CPPUNIT_TEST(testAudioPropertiesVBRIHeader);
55 CPPUNIT_TEST(testAudioPropertiesNoVBRHeaders);
56 CPPUNIT_TEST(testAudioPropertiesADTS);
57 CPPUNIT_TEST(testSkipInvalidFrames1);
58 CPPUNIT_TEST(testSkipInvalidFrames2);
59 CPPUNIT_TEST(testSkipInvalidFrames3);
60 CPPUNIT_TEST(testVersion2DurationWithXingHeader);
61 CPPUNIT_TEST(testSaveID3v24);
62 CPPUNIT_TEST(testSaveID3v23);
63 CPPUNIT_TEST(testDuplicateID3v2);
64 CPPUNIT_TEST(testFuzzedFile);
65 CPPUNIT_TEST(testFrameOffset);
66 CPPUNIT_TEST(testStripAndProperties);
67 CPPUNIT_TEST(testProperties);
68 CPPUNIT_TEST(testRepeatedSave1);
69 CPPUNIT_TEST(testRepeatedSave2);
70 CPPUNIT_TEST(testRepeatedSave3);
71 CPPUNIT_TEST(testEmptyID3v2);
72 CPPUNIT_TEST(testEmptyID3v1);
73 #ifdef TAGLIB_WITH_APE
74 CPPUNIT_TEST(testEmptyAPE);
75 #endif
76 CPPUNIT_TEST(testIgnoreGarbage);
77 CPPUNIT_TEST(testExtendedHeader);
78 CPPUNIT_TEST(testReadStyleFast);
79 CPPUNIT_TEST(testID3v22Properties);
80 CPPUNIT_TEST_SUITE_END();
81
82 public:
83
84 void testAudioPropertiesXingHeaderCBR()
85 {
86 MPEG::File f(TEST_FILE_PATH_C("lame_cbr.mp3"));
87 CPPUNIT_ASSERT(f.audioProperties());
88 CPPUNIT_ASSERT_EQUAL(1887, f.audioProperties()->lengthInSeconds());
89 CPPUNIT_ASSERT_EQUAL(1887164, f.audioProperties()->lengthInMilliseconds());
90 CPPUNIT_ASSERT_EQUAL(64, f.audioProperties()->bitrate());
91 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
92 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
93 CPPUNIT_ASSERT_EQUAL(MPEG::XingHeader::Xing, f.audioProperties()->xingHeader()->type());
94 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
95 }
96
97 void testAudioPropertiesXingHeaderVBR()
98 {
99 MPEG::File f(TEST_FILE_PATH_C("lame_vbr.mp3"));
100 CPPUNIT_ASSERT(f.audioProperties());
101 CPPUNIT_ASSERT_EQUAL(1887, f.audioProperties()->lengthInSeconds());
102 CPPUNIT_ASSERT_EQUAL(1887164, f.audioProperties()->lengthInMilliseconds());
103 CPPUNIT_ASSERT_EQUAL(70, f.audioProperties()->bitrate());
104 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
105 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
106 CPPUNIT_ASSERT_EQUAL(MPEG::XingHeader::Xing, f.audioProperties()->xingHeader()->type());
107 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
108 }
109
110 void testAudioPropertiesVBRIHeader()
111 {
112 MPEG::File f(TEST_FILE_PATH_C("rare_frames.mp3"));
113 CPPUNIT_ASSERT(f.audioProperties());
114 CPPUNIT_ASSERT_EQUAL(222, f.audioProperties()->lengthInSeconds());
115 CPPUNIT_ASSERT_EQUAL(222198, f.audioProperties()->lengthInMilliseconds());
116 CPPUNIT_ASSERT_EQUAL(233, f.audioProperties()->bitrate());
117 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
118 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
119 CPPUNIT_ASSERT_EQUAL(MPEG::XingHeader::VBRI, f.audioProperties()->xingHeader()->type());
120 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
121 }
122
123 void testAudioPropertiesNoVBRHeaders()
124 {
125 MPEG::File f(TEST_FILE_PATH_C("bladeenc.mp3"));
126 CPPUNIT_ASSERT(f.audioProperties());
127 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
128 CPPUNIT_ASSERT_EQUAL(3553, f.audioProperties()->lengthInMilliseconds());
129 CPPUNIT_ASSERT_EQUAL(64, f.audioProperties()->bitrate());
130 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
131 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
132 CPPUNIT_ASSERT(!f.audioProperties()->xingHeader());
133 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
134
135 const offset_t last = f.lastFrameOffset();
136 const MPEG::Header lastHeader(&f, last, false);
137
138 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(28213), last);
139 CPPUNIT_ASSERT_EQUAL(209, lastHeader.frameLength());
140 }
141
142 void testAudioPropertiesADTS()
143 {
144 constexpr std::array readStyles = {
145 MPEG::Properties::Fast,
146 MPEG::Properties::Average,
147 MPEG::Properties::Accurate
148 };
149 for(auto readStyle : readStyles) {
150 MPEG::File f(TEST_FILE_PATH_C("empty1s.aac"), true, readStyle);
151 CPPUNIT_ASSERT(f.audioProperties());
152 CPPUNIT_ASSERT_EQUAL(readStyle == MPEG::Properties::Fast ? 0 : 1,
153 f.audioProperties()->lengthInSeconds());
154 CPPUNIT_ASSERT_EQUAL(readStyle == MPEG::Properties::Fast ? 0 : 1176,
155 f.audioProperties()->lengthInMilliseconds());
156 CPPUNIT_ASSERT_EQUAL(readStyle == MPEG::Properties::Fast ? 0 : 1,
157 f.audioProperties()->bitrate());
158 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
159 CPPUNIT_ASSERT_EQUAL(MPEG::Header::FrontCenter,
160 f.audioProperties()->channelConfiguration());
161 CPPUNIT_ASSERT_EQUAL(11025, f.audioProperties()->sampleRate());
162 CPPUNIT_ASSERT(!f.audioProperties()->xingHeader());
163 CPPUNIT_ASSERT(f.audioProperties()->isADTS());
164
165 const offset_t last = f.lastFrameOffset();
166 const MPEG::Header lastHeader(&f, last, false);
167
168 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(136), last);
169 CPPUNIT_ASSERT_EQUAL(11, lastHeader.frameLength());
170 }
171 }
172
173 void testSkipInvalidFrames1()
174 {
175 MPEG::File f(TEST_FILE_PATH_C("invalid-frames1.mp3"));
176 CPPUNIT_ASSERT(f.audioProperties());
177 CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
178 CPPUNIT_ASSERT_EQUAL(392, f.audioProperties()->lengthInMilliseconds());
179 CPPUNIT_ASSERT_EQUAL(160, f.audioProperties()->bitrate());
180 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
181 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
182 CPPUNIT_ASSERT(!f.audioProperties()->xingHeader());
183 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
184 }
185
186 void testSkipInvalidFrames2()
187 {
188 MPEG::File f(TEST_FILE_PATH_C("invalid-frames2.mp3"));
189 CPPUNIT_ASSERT(f.audioProperties());
190 CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
191 CPPUNIT_ASSERT_EQUAL(314, f.audioProperties()->lengthInMilliseconds());
192 CPPUNIT_ASSERT_EQUAL(192, f.audioProperties()->bitrate());
193 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
194 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
195 CPPUNIT_ASSERT(!f.audioProperties()->xingHeader());
196 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
197 }
198
199 void testSkipInvalidFrames3()
200 {
201 MPEG::File f(TEST_FILE_PATH_C("invalid-frames3.mp3"));
202 CPPUNIT_ASSERT(f.audioProperties());
203 CPPUNIT_ASSERT_EQUAL(0, f.audioProperties()->lengthInSeconds());
204 CPPUNIT_ASSERT_EQUAL(183, f.audioProperties()->lengthInMilliseconds());
205 CPPUNIT_ASSERT_EQUAL(320, f.audioProperties()->bitrate());
206 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
207 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
208 CPPUNIT_ASSERT(!f.audioProperties()->xingHeader());
209 CPPUNIT_ASSERT(!f.audioProperties()->isADTS());
210 }
211
212 void testVersion2DurationWithXingHeader()
213 {
214 MPEG::File f(TEST_FILE_PATH_C("mpeg2.mp3"));
215 CPPUNIT_ASSERT(f.audioProperties());
216 CPPUNIT_ASSERT_EQUAL(5387, f.audioProperties()->lengthInSeconds());
217 CPPUNIT_ASSERT_EQUAL(5387285, f.audioProperties()->lengthInMilliseconds());
218 }
219
220 void testSaveID3v24()
221 {
222 ScopedFileCopy copy("xing", ".mp3");
223 string newname = copy.fileName();
224
225 String xxx = ByteVector(254, 'X');
226 {
227 MPEG::File f(newname.c_str());
228 CPPUNIT_ASSERT_EQUAL(false, f.hasID3v2Tag());
229
230 f.tag()->setTitle(xxx);
231 f.tag()->setArtist("Artist A");
232 f.save(MPEG::File::AllTags, File::StripOthers, ID3v2::v4);
233 CPPUNIT_ASSERT_EQUAL(true, f.hasID3v2Tag());
234 }
235 {
236 MPEG::File f2(newname.c_str());
237 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), f2.ID3v2Tag()->header()->majorVersion());
238 CPPUNIT_ASSERT_EQUAL(String("Artist A"), f2.tag()->artist());
239 CPPUNIT_ASSERT_EQUAL(xxx, f2.tag()->title());
240 }
241 }
242
243 void testSaveID3v23()
244 {
245 ScopedFileCopy copy("xing", ".mp3");
246 string newname = copy.fileName();
247
248 String xxx = ByteVector(254, 'X');
249 {
250 MPEG::File f(newname.c_str());
251 CPPUNIT_ASSERT_EQUAL(false, f.hasID3v2Tag());
252
253 f.tag()->setTitle(xxx);
254 f.tag()->setArtist("Artist A");
255 f.save(MPEG::File::AllTags, File::StripOthers, ID3v2::v3);
256 CPPUNIT_ASSERT_EQUAL(true, f.hasID3v2Tag());
257 }
258 {
259 MPEG::File f2(newname.c_str());
260 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), f2.ID3v2Tag()->header()->majorVersion());
261 CPPUNIT_ASSERT_EQUAL(String("Artist A"), f2.tag()->artist());
262 CPPUNIT_ASSERT_EQUAL(xxx, f2.tag()->title());
263 }
264 }
265
266 void testDuplicateID3v2()
267 {
268 MPEG::File f(TEST_FILE_PATH_C("duplicate_id3v2.mp3"));
269
270 // duplicate_id3v2.mp3 has duplicate ID3v2 tags.
271 // Sample rate will be 32000 if can't skip the second tag.
272
273 CPPUNIT_ASSERT(f.hasID3v2Tag());
274 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
275 }
276
277 void testFuzzedFile()
278 {
279 MPEG::File f(TEST_FILE_PATH_C("excessive_alloc.mp3"));
280 CPPUNIT_ASSERT(f.isValid());
281 }
282
283 void testFrameOffset()
284 {
285 {
286 MPEG::File f(TEST_FILE_PATH_C("ape.mp3"));
287 CPPUNIT_ASSERT(f.isValid());
288 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x0000), f.firstFrameOffset());
289 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x1FD6), f.lastFrameOffset());
290 }
291 {
292 MPEG::File f(TEST_FILE_PATH_C("ape-id3v1.mp3"));
293 CPPUNIT_ASSERT(f.isValid());
294 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x0000), f.firstFrameOffset());
295 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x1FD6), f.lastFrameOffset());
296 }
297 {
298 MPEG::File f(TEST_FILE_PATH_C("ape-id3v2.mp3"));
299 CPPUNIT_ASSERT(f.isValid());
300 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x041A), f.firstFrameOffset());
301 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(0x23F0), f.lastFrameOffset());
302 }
303 }
304
305 void testStripAndProperties()
306 {
307 ScopedFileCopy copy("xing", ".mp3");
308
309 {
310 MPEG::File f(copy.fileName().c_str());
311 f.ID3v2Tag(true)->setTitle("ID3v2");
312 #ifdef TAGLIB_WITH_APE
313 f.APETag(true)->setTitle("APE");
314 #endif
315 f.ID3v1Tag(true)->setTitle("ID3v1");
316 f.save();
317 }
318 {
319 MPEG::File f(copy.fileName().c_str());
320 CPPUNIT_ASSERT_EQUAL(String("ID3v2"), f.properties()["TITLE"].front());
321 f.strip(MPEG::File::ID3v2);
322 #ifdef TAGLIB_WITH_APE
323 CPPUNIT_ASSERT_EQUAL(String("APE"), f.properties()["TITLE"].front());
324 f.strip(MPEG::File::APE);
325 #endif
326 CPPUNIT_ASSERT_EQUAL(String("ID3v1"), f.properties()["TITLE"].front());
327 f.strip(MPEG::File::ID3v1);
328 CPPUNIT_ASSERT(f.properties().isEmpty());
329 }
330 }
331
332 void testProperties()
333 {
334 PropertyMap tags;
335 tags["ALBUM"] = StringList("Album");
336 tags["ALBUMARTIST"] = StringList("Album Artist");
337 tags["ALBUMARTISTSORT"] = StringList("Album Artist Sort");
338 tags["ALBUMSORT"] = StringList("Album Sort");
339 tags["ARRANGER"] = StringList("Arranger");
340 tags["ARTIST"] = StringList("Artist");
341 tags["ARTISTSORT"] = StringList("Artist Sort");
342 tags["ARTISTWEBPAGE"] = StringList("Artist Web Page");
343 tags["ASIN"] = StringList("ASIN");
344 tags["AUDIOSOURCEWEBPAGE"] = StringList("Audio Source Web Page");
345 tags["BARCODE"] = StringList("Barcode");
346 tags["BPM"] = StringList("123");
347 tags["CATALOGNUMBER"] = StringList("Catalog Number");
348 tags["COMMENT"] = StringList("Comment");
349 tags["COMMENT:CDESC"] = StringList("Comment with Description");
350 tags["COMPILATION"] = StringList("1");
351 tags["COMPOSER"] = StringList("Composer");
352 tags["COMPOSERSORT"] = StringList("Composer Sort");
353 tags["CONDUCTOR"] = StringList("Conductor");
354 tags["WORK"] = StringList("Content Group");
355 tags["COPYRIGHT"] = StringList("2021 Copyright");
356 tags["COPYRIGHTURL"] = StringList("Copyright URL");
357 tags["DATE"] = StringList("2021-01-03 12:29:23");
358 tags["DISCNUMBER"] = StringList("3/5");
359 tags["DISCSUBTITLE"] = StringList("Disc Subtitle");
360 tags["DJMIXER"] = StringList("DJ Mixer");
361 tags["ENCODEDBY"] = StringList("Encoded by");
362 tags["ENCODING"] = StringList("Encoding");
363 tags["ENCODINGTIME"] = StringList("2021-01-03 13:48:44");
364 tags["ENGINEER"] = StringList("Engineer");
365 tags["FILETYPE"] = StringList("File Type");
366 tags["FILEWEBPAGE"] = StringList("File Web Page");
367 tags["GENRE"] = StringList("Genre");
368 tags["GROUPING"] = StringList("Grouping");
369 tags["INITIALKEY"] = StringList("Dbm");
370 tags["ISRC"] = StringList("UKAAA0500001");
371 tags["LABEL"] = StringList("Label");
372 tags["LANGUAGE"] = StringList("eng");
373 tags["LENGTH"] = StringList("1234");
374 tags["LYRICIST"] = StringList("Lyricist");
375 tags["LYRICS:LDESC"] = StringList("Lyrics");
376 tags["MEDIA"] = StringList("Media");
377 tags["MIXER"] = StringList("Mixer");
378 tags["MOOD"] = StringList("Mood");
379 tags["MOVEMENTNAME"] = StringList("Movement Name");
380 tags["MOVEMENTNUMBER"] = StringList("2");
381 tags["MUSICBRAINZ_ALBUMID"] = StringList("MusicBrainz_AlbumID");
382 tags["MUSICBRAINZ_ALBUMARTISTID"] = StringList("MusicBrainz_AlbumartistID");
383 tags["MUSICBRAINZ_ARTISTID"] = StringList("MusicBrainz_ArtistID");
384 tags["MUSICBRAINZ_RELEASEGROUPID"] = StringList("MusicBrainz_ReleasegroupID");
385 tags["MUSICBRAINZ_RELEASETRACKID"] = StringList("MusicBrainz_ReleasetrackID");
386 tags["MUSICBRAINZ_TRACKID"] = StringList("MusicBrainz_TrackID");
387 tags["MUSICBRAINZ_WORKID"] = StringList("MusicBrainz_WorkID");
388 tags["ORIGINALALBUM"] = StringList("Original Album");
389 tags["ORIGINALARTIST"] = StringList("Original Artist");
390 tags["ORIGINALDATE"] = StringList("2021-01-03 13:52:19");
391 tags["ORIGINALFILENAME"] = StringList("Original Filename");
392 tags["ORIGINALLYRICIST"] = StringList("Original Lyricist");
393 tags["OWNER"] = StringList("Owner");
394 tags["PAYMENTWEBPAGE"] = StringList("Payment Web Page");
395 tags["PERFORMER:DRUMS"] = StringList("Drummer");
396 tags["PERFORMER:GUITAR"] = StringList("Guitarist");
397 tags["PLAYLISTDELAY"] = StringList("10");
398 tags["PODCAST"] = StringList();
399 tags["PODCASTCATEGORY"] = StringList("Podcast Category");
400 tags["PODCASTDESC"] = StringList("Podcast Description");
401 tags["PODCASTID"] = StringList("Podcast ID");
402 tags["PODCASTURL"] = StringList("Podcast URL");
403 tags["PRODUCEDNOTICE"] = StringList("2021 Produced Notice");
404 tags["PRODUCER"] = StringList("Producer");
405 tags["PUBLISHERWEBPAGE"] = StringList("Publisher Web Page");
406 tags["RADIOSTATION"] = StringList("Radio Station");
407 tags["RADIOSTATIONOWNER"] = StringList("Radio Station Owner");
408 tags["RELEASECOUNTRY"] = StringList("Release Country");
409 tags["RELEASESTATUS"] = StringList("Release Status");
410 tags["RELEASETYPE"] = StringList("Release Type");
411 tags["REMIXER"] = StringList("Remixer");
412 tags["SCRIPT"] = StringList("Script");
413 tags["SUBTITLE"] = StringList("Subtitle");
414 tags["TITLE"] = StringList("Title");
415 tags["TITLESORT"] = StringList("Title Sort");
416 tags["TRACKNUMBER"] = StringList("2/4");
417 tags["URL:UDESC"] = StringList("URL");
418
419 ScopedFileCopy copy("xing", ".mp3");
420 {
421 MPEG::File f(copy.fileName().c_str());
422 PropertyMap properties = f.properties();
423 CPPUNIT_ASSERT(properties.isEmpty());
424 f.setProperties(tags);
425 f.save();
426 }
427 {
428 const MPEG::File f(copy.fileName().c_str());
429 PropertyMap properties = f.properties();
430 if (tags != properties) {
431 CPPUNIT_ASSERT_EQUAL(tags.toString(), properties.toString());
432 }
433 CPPUNIT_ASSERT(tags == properties);
434 }
435 }
436
437 void testRepeatedSave1()
438 {
439 ScopedFileCopy copy("xing", ".mp3");
440
441 {
442 MPEG::File f(copy.fileName().c_str());
443 f.ID3v2Tag(true)->setTitle(std::string(4096, 'X').c_str());
444 f.save();
445 }
446 {
447 MPEG::File f(copy.fileName().c_str());
448 f.ID3v2Tag(true)->setTitle("");
449 f.save();
450 f.ID3v2Tag(true)->setTitle(std::string(4096, 'X').c_str());
451 f.save();
452 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(5141), f.firstFrameOffset());
453 }
454 }
455
456 void testRepeatedSave2()
457 {
458 ScopedFileCopy copy("xing", ".mp3");
459
460 MPEG::File f(copy.fileName().c_str());
461 f.ID3v2Tag(true)->setTitle("0123456789");
462 f.save();
463 f.save();
464 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(-1), f.find("ID3", 3));
465 }
466
467 void testRepeatedSave3()
468 {
469 ScopedFileCopy copy("xing", ".mp3");
470
471 {
472 MPEG::File f(copy.fileName().c_str());
473 CPPUNIT_ASSERT(!f.hasAPETag());
474 CPPUNIT_ASSERT(!f.hasID3v1Tag());
475
476 #ifdef TAGLIB_WITH_APE
477 f.APETag(true)->setTitle("01234 56789 ABCDE FGHIJ");
478 f.save();
479 f.APETag()->setTitle("0");
480 f.save();
481 #endif
482 f.ID3v1Tag(true)->setTitle("01234 56789 ABCDE FGHIJ");
483 #ifdef TAGLIB_WITH_APE
484 f.APETag()->setTitle("01234 56789 ABCDE FGHIJ 01234 56789 ABCDE FGHIJ 01234 56789");
485 #endif
486 f.save();
487 }
488 {
489 MPEG::File f(copy.fileName().c_str());
490 #ifdef TAGLIB_WITH_APE
491 CPPUNIT_ASSERT(f.hasAPETag());
492 #endif
493 CPPUNIT_ASSERT(f.hasID3v1Tag());
494 }
495 }
496
497 void testEmptyID3v2()
498 {
499 ScopedFileCopy copy("xing", ".mp3");
500
501 {
502 MPEG::File f(copy.fileName().c_str());
503 f.ID3v2Tag(true)->setTitle("0123456789");
504 f.save(MPEG::File::ID3v2);
505 }
506 {
507 MPEG::File f(copy.fileName().c_str());
508 f.ID3v2Tag(true)->setTitle("");
509 f.save(MPEG::File::ID3v2, File::StripNone);
510 }
511 {
512 MPEG::File f(copy.fileName().c_str());
513 CPPUNIT_ASSERT(!f.hasID3v2Tag());
514 }
515 }
516
517 void testEmptyID3v1()
518 {
519 ScopedFileCopy copy("xing", ".mp3");
520
521 {
522 MPEG::File f(copy.fileName().c_str());
523 f.ID3v1Tag(true)->setTitle("0123456789");
524 f.save(MPEG::File::ID3v1);
525 }
526 {
527 MPEG::File f(copy.fileName().c_str());
528 f.ID3v1Tag(true)->setTitle("");
529 f.save(MPEG::File::ID3v1, File::StripNone);
530 }
531 {
532 MPEG::File f(copy.fileName().c_str());
533 CPPUNIT_ASSERT(!f.hasID3v1Tag());
534 }
535 }
536
537 #ifdef TAGLIB_WITH_APE
538 void testEmptyAPE()
539 {
540 ScopedFileCopy copy("xing", ".mp3");
541
542 {
543 MPEG::File f(copy.fileName().c_str());
544 f.APETag(true)->setTitle("0123456789");
545 f.save(MPEG::File::APE);
546 }
547 {
548 MPEG::File f(copy.fileName().c_str());
549 f.APETag(true)->setTitle("");
550 f.save(MPEG::File::APE, File::StripNone);
551 }
552 {
553 MPEG::File f(copy.fileName().c_str());
554 CPPUNIT_ASSERT(!f.hasAPETag());
555 }
556 }
557 #endif
558
559 void testIgnoreGarbage()
560 {
561 const ScopedFileCopy copy("garbage", ".mp3");
562 {
563 MPEG::File f(copy.fileName().c_str());
564 CPPUNIT_ASSERT(f.isValid());
565 CPPUNIT_ASSERT(f.hasID3v2Tag());
566 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2255), f.firstFrameOffset());
567 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(6015), f.lastFrameOffset());
568 CPPUNIT_ASSERT_EQUAL(String("Title A"), f.ID3v2Tag()->title());
569 f.ID3v2Tag()->setTitle("Title B");
570 f.save();
571 }
572 {
573 MPEG::File f(copy.fileName().c_str());
574 CPPUNIT_ASSERT(f.isValid());
575 CPPUNIT_ASSERT(f.hasID3v2Tag());
576 CPPUNIT_ASSERT_EQUAL(String("Title B"), f.ID3v2Tag()->title());
577 }
578 }
579
580 void testExtendedHeader()
581 {
582 const ScopedFileCopy copy("extended-header", ".mp3");
583 MPEG::File f(copy.fileName().c_str());
584 CPPUNIT_ASSERT(f.isValid());
585 CPPUNIT_ASSERT(f.hasID3v2Tag());
586 ID3v2::Tag *tag = f.ID3v2Tag();
587 ID3v2::ExtendedHeader *ext = tag->extendedHeader();
588 CPPUNIT_ASSERT(ext);
589 CPPUNIT_ASSERT_EQUAL(12U, ext->size());
590 CPPUNIT_ASSERT_EQUAL(String("Druids"), tag->title());
591 CPPUNIT_ASSERT_EQUAL(String("Excelsis"), tag->artist());
592 CPPUNIT_ASSERT_EQUAL(String("Vo Chrieger U Drache"), tag->album());
593 CPPUNIT_ASSERT_EQUAL(2013U, tag->year());
594 CPPUNIT_ASSERT_EQUAL(String("Folk/Power Metal"), tag->genre());
595 CPPUNIT_ASSERT_EQUAL(3U, tag->track());
596 CPPUNIT_ASSERT_EQUAL(String("2013"),
597 f.properties().value("ORIGINALDATE").front());
598 }
599
600 void testReadStyleFast()
601 {
602 const ScopedFileCopy copy("lame_cbr", ".mp3");
603 {
604 MPEG::File f(copy.fileName().c_str(), true, MPEG::Properties::Fast);
605 CPPUNIT_ASSERT(f.audioProperties());
606 CPPUNIT_ASSERT_EQUAL(1887, f.audioProperties()->lengthInSeconds());
607 CPPUNIT_ASSERT_EQUAL(1887164, f.audioProperties()->lengthInMilliseconds());
608 CPPUNIT_ASSERT_EQUAL(64, f.audioProperties()->bitrate());
609 CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
610 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
611 CPPUNIT_ASSERT(f.isValid());
612 CPPUNIT_ASSERT(f.hasID3v2Tag());
613 CPPUNIT_ASSERT_EQUAL(String(""), f.ID3v2Tag()->title());
614 PropertyMap properties = f.properties();
615 CPPUNIT_ASSERT_EQUAL(String("-1.020000 dB"), properties.value("REPLAYGAIN_TRACK_GAIN").front());
616 CPPUNIT_ASSERT_EQUAL(String("0.920032"), properties.value("REPLAYGAIN_TRACK_PEAK").front());
617 properties["TITLE"] = String("A Title");
618 properties["Artist"] = String("An Artist");
619 f.setProperties(properties);
620 f.save();
621 }
622 {
623 MPEG::File f(copy.fileName().c_str(), true, MPEG::Properties::Fast);
624 CPPUNIT_ASSERT(f.isValid());
625 CPPUNIT_ASSERT(f.hasID3v2Tag());
626 CPPUNIT_ASSERT_EQUAL(String("A Title"), f.ID3v2Tag()->title());
627 CPPUNIT_ASSERT_EQUAL(String("An Artist"), f.ID3v2Tag()->artist());
628 }
629 {
630 MPEG::File f(TEST_FILE_PATH_C("garbage.mp3"), true, MPEG::Properties::Fast);
631 CPPUNIT_ASSERT(f.isValid());
632 // Garbage prevents detection of ID3v2 with fast read style
633 CPPUNIT_ASSERT(!f.hasID3v2Tag());
634 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2255), f.firstFrameOffset());
635 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(6015), f.lastFrameOffset());
636 }
637 }
638
639 void testID3v22Properties()
640 {
641 ScopedFileCopy copy("itunes10", ".mp3");
642
643 MPEG::File f(copy.fileName().c_str());
644 PropertyMap expectedProperties(SimplePropertyMap{
645 {"ALBUM", {"Album"}},
646 {"ALBUMARTIST", {"Album Artist"}},
647 {"ALBUMARTISTSORT", {"Sort Album Artist"}},
648 {"ALBUMSORT", {"Sort Album"}},
649 {"ARTIST", {"Artist"}},
650 {"ARTISTSORT", {"Sort Artist"}},
651 {"BPM", {"180"}},
652 {"COMMENT", {"Comments"}},
653 {"COMMENT:ITUNPGAP", {"1"}},
654 {"COMPILATION", {"1"}},
655 {"COMPOSER", {"Composer"}},
656 {"COMPOSERSORT", {"Sort Composer"}},
657 {"DATE", {"2011"}},
658 {"DISCNUMBER", {"1/2"}},
659 {"GENRE", {"Heavy Metal"}},
660 {"LYRICS", {"Lyrics"}},
661 {"SUBTITLE", {"Description"}},
662 {"TITLE", {"iTunes10MP3"}},
663 {"TITLESORT", {"Sort Name"}},
664 {"TRACKNUMBER", {"1/10"}},
665 {"WORK", {"Grouping"}}
666 });
667 expectedProperties.addUnsupportedData("APIC");
668 expectedProperties.addUnsupportedData("UNKNOWN/RVA");
669
670 PropertyMap properties = f.properties();
671 if (expectedProperties != properties) {
672 CPPUNIT_ASSERT_EQUAL(expectedProperties.toString(), properties.toString());
673 }
674 CPPUNIT_ASSERT(expectedProperties == properties);
675
676 const String PICTURE_KEY("PICTURE");
677 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY), f.complexPropertyKeys());
678 auto pictures = f.complexProperties(PICTURE_KEY);
679 CPPUNIT_ASSERT_EQUAL(1U, pictures.size());
680 auto picture = pictures.front();
681 CPPUNIT_ASSERT_EQUAL(String("image/png"), picture.value("mimeType").toString());
682 CPPUNIT_ASSERT(picture.value("description").toString().isEmpty());
683 CPPUNIT_ASSERT_EQUAL(String("Other"), picture.value("pictureType").toString());
684 auto data = picture.value("data").toByteVector();
685 CPPUNIT_ASSERT(data.startsWith("\x89PNG\x0d\x0a\x1a\x0a"));
686 CPPUNIT_ASSERT_EQUAL(2315U, data.size());
687 }
688
689 };
690
691 CPPUNIT_TEST_SUITE_REGISTRATION(TestMPEG);