Debian Codegraph @

tests/test_id3v2.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 <utility>
28 #include <cassert>
29
30 #include "tpropertymap.h"
31 #include "tzlib.h"
32 #include "id3v2tag.h"
33 #include "mpegfile.h"
34 #include "id3v2frame.h"
35 #include "uniquefileidentifierframe.h"
36 #include "textidentificationframe.h"
37 #include "attachedpictureframe.h"
38 #include "unsynchronizedlyricsframe.h"
39 #include "synchronizedlyricsframe.h"
40 #include "eventtimingcodesframe.h"
41 #include "generalencapsulatedobjectframe.h"
42 #include "relativevolumeframe.h"
43 #include "popularimeterframe.h"
44 #include "urllinkframe.h"
45 #include "ownershipframe.h"
46 #include "unknownframe.h"
47 #include "chapterframe.h"
48 #include "tableofcontentsframe.h"
49 #include "commentsframe.h"
50 #include "podcastframe.h"
51 #include "privateframe.h"
52 #include "plainfile.h"
53 #include <cppunit/extensions/HelperMacros.h>
54 #include "utils.h"
55
56 using namespace std;
57 using namespace TagLib;
58
59 class PublicFrame : public ID3v2::Frame
60 {
61 public:
62 PublicFrame() : ID3v2::Frame(ByteVector("XXXX\0\0\0\0\0\0", 10)) {}
63 String readStringField(const ByteVector &data, String::Type encoding,
64 int *position = nullptr)
65 { return ID3v2::Frame::readStringField(data, encoding, position); }
66 String toString() const override { return String(); }
67 void parseFields(const ByteVector &) override {}
68 ByteVector renderFields() const override { return ByteVector(); }
69 };
70
71 class TestID3v2 : public CppUnit::TestFixture
72 {
73 CPPUNIT_TEST_SUITE(TestID3v2);
74 CPPUNIT_TEST(testUnsynchDecode);
75 CPPUNIT_TEST(testDowngradeUTF8ForID3v23_1);
76 CPPUNIT_TEST(testDowngradeUTF8ForID3v23_2);
77 CPPUNIT_TEST(testUTF16BEDelimiter);
78 CPPUNIT_TEST(testUTF16Delimiter);
79 CPPUNIT_TEST(testReadStringField);
80 CPPUNIT_TEST(testParseAPIC);
81 CPPUNIT_TEST(testParseAPIC_UTF16_BOM);
82 CPPUNIT_TEST(testParseAPICv22);
83 CPPUNIT_TEST(testRenderAPIC);
84 CPPUNIT_TEST(testDontRender22);
85 CPPUNIT_TEST(testParseGEOB);
86 CPPUNIT_TEST(testRenderGEOB);
87 CPPUNIT_TEST(testPOPMtoString);
88 CPPUNIT_TEST(testParsePOPM);
89 CPPUNIT_TEST(testParsePOPMWithoutCounter);
90 CPPUNIT_TEST(testRenderPOPM);
91 CPPUNIT_TEST(testPOPMFromFile);
92 CPPUNIT_TEST(testParseRelativeVolumeFrame);
93 CPPUNIT_TEST(testRenderRelativeVolumeFrame);
94 CPPUNIT_TEST(testParseUniqueFileIdentifierFrame);
95 CPPUNIT_TEST(testParseEmptyUniqueFileIdentifierFrame);
96 CPPUNIT_TEST(testRenderUniqueFileIdentifierFrame);
97 CPPUNIT_TEST(testBrokenFrame1);
98 CPPUNIT_TEST(testItunes24FrameSize);
99 CPPUNIT_TEST(testParseUrlLinkFrame);
100 CPPUNIT_TEST(testRenderUrlLinkFrame);
101 CPPUNIT_TEST(testParseUserUrlLinkFrame);
102 CPPUNIT_TEST(testRenderUserUrlLinkFrame);
103 CPPUNIT_TEST(testParseOwnershipFrame);
104 CPPUNIT_TEST(testRenderOwnershipFrame);
105 CPPUNIT_TEST(testParseSynchronizedLyricsFrame);
106 CPPUNIT_TEST(testParseSynchronizedLyricsFrameWithEmptyDescritpion);
107 CPPUNIT_TEST(testRenderSynchronizedLyricsFrame);
108 CPPUNIT_TEST(testParseEventTimingCodesFrame);
109 CPPUNIT_TEST(testRenderEventTimingCodesFrame);
110 CPPUNIT_TEST(testParseCommentsFrame);
111 CPPUNIT_TEST(testRenderCommentsFrame);
112 CPPUNIT_TEST(testParsePodcastFrame);
113 CPPUNIT_TEST(testRenderPodcastFrame);
114 CPPUNIT_TEST(testParsePrivateFrame);
115 CPPUNIT_TEST(testRenderPrivateFrame);
116 CPPUNIT_TEST(testParseUserTextIdentificationFrame);
117 CPPUNIT_TEST(testRenderUserTextIdentificationFrame);
118 CPPUNIT_TEST(testSaveUTF16Comment);
119 CPPUNIT_TEST(testUpdateGenre23_1);
120 CPPUNIT_TEST(testUpdateGenre23_2);
121 CPPUNIT_TEST(testUpdateGenre23_3);
122 CPPUNIT_TEST(testUpdateGenre24);
123 CPPUNIT_TEST(testUpdateDate22);
124 CPPUNIT_TEST(testDowngradeTo23);
125 // CPPUNIT_TEST(testUpdateFullDate22); TODO TYE+TDA should be upgraded to TDRC together
126 CPPUNIT_TEST(testCompressedFrameWithBrokenLength);
127 CPPUNIT_TEST(testW000);
128 CPPUNIT_TEST(testPropertyInterface);
129 CPPUNIT_TEST(testPropertyInterface2);
130 CPPUNIT_TEST(testPropertiesMovement);
131 CPPUNIT_TEST(testPropertyGrouping);
132 CPPUNIT_TEST(testDeleteFrame);
133 CPPUNIT_TEST(testSaveAndStripID3v1ShouldNotAddFrameFromID3v1ToId3v2);
134 CPPUNIT_TEST(testParseChapterFrame);
135 CPPUNIT_TEST(testRenderChapterFrame);
136 CPPUNIT_TEST(testParseTableOfContentsFrame);
137 CPPUNIT_TEST(testRenderTableOfContentsFrame);
138 CPPUNIT_TEST(testShrinkPadding);
139 CPPUNIT_TEST(testEmptyFrame);
140 CPPUNIT_TEST(testDuplicateTags);
141 CPPUNIT_TEST(testParseTOCFrameWithManyChildren);
142 CPPUNIT_TEST(testInvalidID3v2Version);
143 CPPUNIT_TEST_SUITE_END();
144
145 public:
146
147 void testUnsynchDecode()
148 {
149 MPEG::File f(TEST_FILE_PATH_C("unsynch.id3"), false);
150 CPPUNIT_ASSERT(f.tag());
151 CPPUNIT_ASSERT_EQUAL(String("My babe just cares for me"), f.tag()->title());
152 }
153
154 void testDowngradeUTF8ForID3v23_1()
155 {
156 ScopedFileCopy copy("xing", ".mp3");
157 string newname = copy.fileName();
158
159 auto f
160 = new ID3v2::TextIdentificationFrame(ByteVector("TPE1"), String::UTF8);
161 StringList sl;
162 sl.append("Foo");
163 f->setText(sl);
164
165 MPEG::File file(newname.c_str());
166 file.ID3v2Tag(true)->addFrame(f);
167 file.save(MPEG::File::ID3v2, File::StripOthers, ID3v2::v3);
168 CPPUNIT_ASSERT_EQUAL(true, file.hasID3v2Tag());
169
170 ByteVector data = f->render();
171 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4+4+2+1+6+2), data.size());
172
173 ID3v2::TextIdentificationFrame f2(data);
174 CPPUNIT_ASSERT_EQUAL(sl, f2.fieldList());
175 CPPUNIT_ASSERT_EQUAL(String::UTF16, f2.textEncoding());
176 }
177
178 void testDowngradeUTF8ForID3v23_2()
179 {
180 ScopedFileCopy copy("xing", ".mp3");
181
182 auto f = new ID3v2::UnsynchronizedLyricsFrame(String::UTF8);
183 f->setText("Foo");
184
185 MPEG::File file(copy.fileName().c_str());
186 file.ID3v2Tag(true)->addFrame(f);
187 file.save(MPEG::File::ID3v2, File::StripOthers, ID3v2::v3);
188 CPPUNIT_ASSERT(file.hasID3v2Tag());
189
190 ByteVector data = f->render();
191 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4+4+2+1+3+2+2+6+2), data.size());
192
193 ID3v2::UnsynchronizedLyricsFrame f2(data);
194 CPPUNIT_ASSERT_EQUAL(String("Foo"), f2.text());
195 CPPUNIT_ASSERT_EQUAL(String::UTF16, f2.textEncoding());
196 }
197
198 void testUTF16BEDelimiter()
199 {
200 ID3v2::TextIdentificationFrame f(ByteVector("TPE1"), String::UTF16BE);
201 StringList sl;
202 sl.append("Foo");
203 sl.append("Bar");
204 f.setText(sl);
205 ByteVector data = f.render();
206 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4+4+2+1+6+2+6), data.size());
207 ByteVector noBomBeData("TPE1\x00\x00\x00\x0f\x00\x00\x02"
208 "\0F\0o\0o\0\0"
209 "\0B\0a\0r", 25);
210 CPPUNIT_ASSERT_EQUAL(noBomBeData, data);
211 f.setData(data);
212 CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.toString());
213 }
214
215 void testUTF16Delimiter()
216 {
217 ID3v2::TextIdentificationFrame f(ByteVector("TPE1"), String::UTF16);
218 StringList sl;
219 sl.append("Foo");
220 sl.append("Bar");
221 f.setText(sl);
222 ByteVector data = f.render();
223 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4+4+2+1+8+2+8), data.size());
224 ByteVector multiBomLeData("TPE1\x00\x00\x00\x13\x00\x00\x01\xff\xfe"
225 "F\0o\0o\0\0\0" "\xff\xfe"
226 "B\0a\0r\0", 29);
227 CPPUNIT_ASSERT_EQUAL(multiBomLeData, data);
228 f.setData(data);
229 CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.toString());
230
231 ByteVector multiBomBeData("TPE1\x00\x00\x00\x13\x00\x00\x01\xfe\xff"
232 "\0F\0o\0o\0\0" "\xfe\xff"
233 "\0B\0a\0r", 29);
234 f.setData(multiBomBeData);
235 CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.toString());
236
237 ByteVector singleBomLeData("TPE1\x00\x00\x00\x11\x00\x00\x01\xff\xfe"
238 "F\0o\0o\0\0\0"
239 "B\0a\0r\0", 27);
240 f.setData(singleBomLeData);
241 CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.toString());
242
243 ByteVector singleBomBeData("TPE1\x00\x00\x00\x11\x00\x00\x01\xfe\xff"
244 "\0F\0o\0o\0\0"
245 "\0B\0a\0r", 27);
246 f.setData(singleBomBeData);
247 CPPUNIT_ASSERT_EQUAL(String("Foo Bar"), f.toString());
248 }
249
250 void testBrokenFrame1()
251 {
252 MPEG::File f(TEST_FILE_PATH_C("broken-tenc.id3"), false);
253 CPPUNIT_ASSERT(f.tag());
254 CPPUNIT_ASSERT(!f.ID3v2Tag()->frameListMap().contains("TENC"));
255 }
256
257 void testReadStringField()
258 {
259 PublicFrame f;
260 ByteVector data("abc\0", 4);
261 String str = f.readStringField(data, String::Latin1);
262 CPPUNIT_ASSERT_EQUAL(String("abc"), str);
263 }
264
265 // http://bugs.kde.org/show_bug.cgi?id=151078
266 void testParseAPIC()
267 {
268 ID3v2::AttachedPictureFrame f(ByteVector("APIC"
269 "\x00\x00\x00\x07"
270 "\x00\x00"
271 "\x00"
272 "m\x00"
273 "\x01"
274 "d\x00"
275 "\x00", 17));
276 CPPUNIT_ASSERT_EQUAL(String("m"), f.mimeType());
277 CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::FileIcon, f.type());
278 CPPUNIT_ASSERT_EQUAL(String("d"), f.description());
279 }
280
281 void testParseAPIC_UTF16_BOM()
282 {
283 ID3v2::AttachedPictureFrame f(ByteVector(
284 "\x41\x50\x49\x43\x00\x00\x00\x26\x00\x00\x01\x69\x6d\x61\x67\x65"
285 "\x2f\x6a\x70\x65\x67\x00\x00\xfe\xff\x00\x63\x00\x6f\x00\x76\x00"
286 "\x65\x00\x72\x00\x2e\x00\x6a\x00\x70\x00\x67\x00\x00\xff\xd8\xff",
287 16 * 3));
288 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), f.mimeType());
289 CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::Other, f.type());
290 CPPUNIT_ASSERT_EQUAL(String("cover.jpg"), f.description());
291 CPPUNIT_ASSERT_EQUAL(ByteVector("\xff\xd8\xff", 3), f.picture());
292 }
293
294 void testParseAPICv22()
295 {
296 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
297 auto data = ByteVector("PIC"
298 "\x00\x00\x08"
299 "\x00"
300 "JPG"
301 "\x01"
302 "d\x00"
303 "\x00", 14);
304 ID3v2::Header header;
305 header.setMajorVersion(2);
306 auto frame =
307 dynamic_cast<TagLib::ID3v2::AttachedPictureFrame *>(factory->createFrame(data, &header));
308
309 CPPUNIT_ASSERT(frame);
310 assert(frame != nullptr); // to silence the clang analyzer
311 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), frame->mimeType());
312 CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::FileIcon, frame->type());
313 CPPUNIT_ASSERT_EQUAL(String("d"), frame->description());
314
315 delete frame;
316 }
317
318 void testRenderAPIC()
319 {
320 ID3v2::AttachedPictureFrame f;
321 f.setTextEncoding(String::UTF8);
322 f.setMimeType("image/png");
323 f.setType(ID3v2::AttachedPictureFrame::BackCover);
324 f.setDescription("Description");
325 f.setPicture("PNG data");
326 CPPUNIT_ASSERT_EQUAL(
327 ByteVector("APIC"
328 "\x00\x00\x00\x20"
329 "\x00\x00"
330 "\x03"
331 "image/png\x00"
332 "\x04"
333 "Description\x00"
334 "PNG data", 42),
335 f.render());
336 }
337
338 void testDontRender22()
339 {
340 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
341 auto data = ByteVector("FOO"
342 "\x00\x00\x08"
343 "\x00"
344 "JPG"
345 "\x01"
346 "d\x00"
347 "\x00", 14);
348 ID3v2::Header header;
349 header.setMajorVersion(2);
350 auto frame =
351 dynamic_cast<TagLib::ID3v2::UnknownFrame*>(factory->createFrame(data, &header));
352
353 CPPUNIT_ASSERT(frame);
354
355 ID3v2::Tag tag;
356 tag.addFrame(frame);
357 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1034), tag.render().size());
358 }
359
360 // http://bugs.kde.org/show_bug.cgi?id=151078
361 void testParseGEOB()
362 {
363 ID3v2::GeneralEncapsulatedObjectFrame f(ByteVector("GEOB"
364 "\x00\x00\x00\x08"
365 "\x00\x00"
366 "\x00"
367 "m\x00"
368 "f\x00"
369 "d\x00"
370 "\x00", 18));
371 CPPUNIT_ASSERT_EQUAL(String("m"), f.mimeType());
372 CPPUNIT_ASSERT_EQUAL(String("f"), f.fileName());
373 CPPUNIT_ASSERT_EQUAL(String("d"), f.description());
374 }
375
376 void testRenderGEOB()
377 {
378 ID3v2::GeneralEncapsulatedObjectFrame f;
379 f.setTextEncoding(String::Latin1);
380 f.setMimeType("application/octet-stream");
381 f.setFileName("test.bin");
382 f.setDescription("Description");
383 f.setObject(ByteVector(3, '\x01'));
384 CPPUNIT_ASSERT_EQUAL(
385 ByteVector("GEOB"
386 "\x00\x00\x00\x32"
387 "\x00\x00"
388 "\x00"
389 "application/octet-stream\x00"
390 "test.bin\x00"
391 "Description\x00"
392 "\x01\x01\x01", 60),
393 f.render());
394 }
395
396 void testParsePOPM()
397 {
398 ID3v2::PopularimeterFrame f(ByteVector("POPM"
399 "\x00\x00\x00\x17"
400 "\x00\x00"
401 "[email protected]\x00"
402 "\x02"
403 "\x00\x00\x00\x03", 33));
404 CPPUNIT_ASSERT_EQUAL(String("[email protected]"), f.email());
405 CPPUNIT_ASSERT_EQUAL(2, f.rating());
406 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3), f.counter());
407 }
408
409 void testParsePOPMWithoutCounter()
410 {
411 ID3v2::PopularimeterFrame f(ByteVector("POPM"
412 "\x00\x00\x00\x13"
413 "\x00\x00"
414 "[email protected]\x00"
415 "\x02", 29));
416 CPPUNIT_ASSERT_EQUAL(String("[email protected]"), f.email());
417 CPPUNIT_ASSERT_EQUAL(2, f.rating());
418 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), f.counter());
419 }
420
421 void testRenderPOPM()
422 {
423 ID3v2::PopularimeterFrame f;
424 f.setEmail("[email protected]");
425 f.setRating(2);
426 f.setCounter(3);
427 CPPUNIT_ASSERT_EQUAL(
428 ByteVector("POPM"
429 "\x00\x00\x00\x17"
430 "\x00\x00"
431 "[email protected]\x00"
432 "\x02"
433 "\x00\x00\x00\x03", 33),
434 f.render());
435 }
436
437 void testPOPMtoString()
438 {
439 ID3v2::PopularimeterFrame f;
440 f.setEmail("[email protected]");
441 f.setRating(2);
442 f.setCounter(3);
443 CPPUNIT_ASSERT_EQUAL(
444 String("[email protected] rating=2 counter=3"), f.toString());
445 }
446
447 void testPOPMFromFile()
448 {
449 ScopedFileCopy copy("xing", ".mp3");
450 string newname = copy.fileName();
451
452 auto f = new ID3v2::PopularimeterFrame();
453 f->setEmail("[email protected]");
454 f->setRating(200);
455 f->setCounter(3);
456
457 {
458 MPEG::File foo(newname.c_str());
459 foo.ID3v2Tag()->addFrame(f);
460 foo.save();
461 }
462 {
463 MPEG::File bar(newname.c_str());
464 CPPUNIT_ASSERT_EQUAL(String("[email protected]"), dynamic_cast<ID3v2::PopularimeterFrame *>(bar.ID3v2Tag()->frameList("POPM").front())->email());
465 CPPUNIT_ASSERT_EQUAL(200, dynamic_cast<ID3v2::PopularimeterFrame *>(bar.ID3v2Tag()->frameList("POPM").front())->rating());
466 }
467 }
468
469 // http://bugs.kde.org/show_bug.cgi?id=150481
470 void testParseRelativeVolumeFrame()
471 {
472 ID3v2::RelativeVolumeFrame f(
473 ByteVector("RVA2" // Frame ID
474 "\x00\x00\x00\x0B" // Frame size
475 "\x00\x00" // Frame flags
476 "ident\x00" // Identification
477 "\x02" // Type of channel
478 "\x00\x0F" // Volume adjustment
479 "\x08" // Bits representing peak
480 "\x45", 21)); // Peak volume
481 CPPUNIT_ASSERT_EQUAL(String("ident"), f.identification());
482 CPPUNIT_ASSERT_EQUAL(15.0f / 512.0f,
483 f.volumeAdjustment(ID3v2::RelativeVolumeFrame::FrontRight));
484 CPPUNIT_ASSERT_EQUAL(static_cast<short>(15),
485 f.volumeAdjustmentIndex(ID3v2::RelativeVolumeFrame::FrontRight));
486 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned char>(8),
487 f.peakVolume(ID3v2::RelativeVolumeFrame::FrontRight).bitsRepresentingPeak);
488 CPPUNIT_ASSERT_EQUAL(ByteVector("\x45"),
489 f.peakVolume(ID3v2::RelativeVolumeFrame::FrontRight).peakVolume);
490 const List<ID3v2::RelativeVolumeFrame::ChannelType> channels = f.channels();
491 CPPUNIT_ASSERT_EQUAL(1U, channels.size());
492 CPPUNIT_ASSERT_EQUAL(ID3v2::RelativeVolumeFrame::FrontRight, channels[0]);
493 }
494
495 void testRenderRelativeVolumeFrame()
496 {
497 ID3v2::RelativeVolumeFrame f;
498 f.setIdentification("ident");
499 f.setVolumeAdjustment(15.0f / 512.0f, ID3v2::RelativeVolumeFrame::FrontRight);
500 ID3v2::RelativeVolumeFrame::PeakVolume peakVolume;
501 peakVolume.bitsRepresentingPeak = 8;
502 peakVolume.peakVolume.setData("\x45");
503 f.setPeakVolume(peakVolume, ID3v2::RelativeVolumeFrame::FrontRight);
504 CPPUNIT_ASSERT_EQUAL(
505 ByteVector("RVA2"
506 "\x00\x00\x00\x0B"
507 "\x00\x00"
508 "ident\x00"
509 "\x02"
510 "\x00\x0F"
511 "\x08"
512 "\x45", 21),
513 f.render());
514 }
515
516 void testParseUniqueFileIdentifierFrame()
517 {
518 ID3v2::UniqueFileIdentifierFrame f(
519 ByteVector("UFID" // Frame ID
520 "\x00\x00\x00\x09" // Frame size
521 "\x00\x00" // Frame flags
522 "owner\x00" // Owner identifier
523 "\x00\x01\x02", 19)); // Identifier
524 CPPUNIT_ASSERT_EQUAL(String("owner"),
525 f.owner());
526 CPPUNIT_ASSERT_EQUAL(ByteVector("\x00\x01\x02", 3),
527 f.identifier());
528 }
529
530 void testParseEmptyUniqueFileIdentifierFrame()
531 {
532 ID3v2::UniqueFileIdentifierFrame f(
533 ByteVector("UFID" // Frame ID
534 "\x00\x00\x00\x01" // Frame size
535 "\x00\x00" // Frame flags
536 "\x00" // Owner identifier
537 "", 11)); // Identifier
538 CPPUNIT_ASSERT_EQUAL(String(),
539 f.owner());
540 CPPUNIT_ASSERT_EQUAL(ByteVector(),
541 f.identifier());
542 }
543
544 void testRenderUniqueFileIdentifierFrame()
545 {
546 ID3v2::UniqueFileIdentifierFrame f("owner", "\x01\x02\x03");
547 CPPUNIT_ASSERT_EQUAL(
548 ByteVector("UFID"
549 "\x00\x00\x00\x09"
550 "\x00\x00"
551 "owner\x00"
552 "\x01\x02\x03", 19),
553 f.render());
554 }
555
556 void testParseUrlLinkFrame()
557 {
558 ID3v2::UrlLinkFrame f(
559 ByteVector("WOAF" // Frame ID
560 "\x00\x00\x00\x12" // Frame size
561 "\x00\x00" // Frame flags
562 "http://example.com", 28)); // URL
563 CPPUNIT_ASSERT_EQUAL(String("http://example.com"), f.url());
564 }
565
566 void testRenderUrlLinkFrame()
567 {
568 ID3v2::UrlLinkFrame f("WOAF");
569 f.setUrl("http://example.com");
570 CPPUNIT_ASSERT_EQUAL(
571 ByteVector("WOAF" // Frame ID
572 "\x00\x00\x00\x12" // Frame size
573 "\x00\x00" // Frame flags
574 "http://example.com", 28), // URL
575 f.render());
576 }
577
578 void testParseUserUrlLinkFrame()
579 {
580 ID3v2::UserUrlLinkFrame f(
581 ByteVector("WXXX" // Frame ID
582 "\x00\x00\x00\x17" // Frame size
583 "\x00\x00" // Frame flags
584 "\x00" // Text encoding
585 "foo\x00" // Description
586 "http://example.com", 33)); // URL
587 CPPUNIT_ASSERT_EQUAL(String("foo"), f.description());
588 CPPUNIT_ASSERT_EQUAL(String("http://example.com"), f.url());
589 }
590
591 void testRenderUserUrlLinkFrame()
592 {
593 ID3v2::UserUrlLinkFrame f;
594 f.setDescription("foo");
595 f.setUrl("http://example.com");
596 CPPUNIT_ASSERT_EQUAL(
597 ByteVector("WXXX" // Frame ID
598 "\x00\x00\x00\x17" // Frame size
599 "\x00\x00" // Frame flags
600 "\x00" // Text encoding
601 "foo\x00" // Description
602 "http://example.com", 33), // URL
603 f.render());
604 }
605
606 void testParseOwnershipFrame()
607 {
608 ID3v2::OwnershipFrame f(
609 ByteVector("OWNE" // Frame ID
610 "\x00\x00\x00\x19" // Frame size
611 "\x00\x00" // Frame flags
612 "\x00" // Text encoding
613 "GBP1.99\x00" // Price paid
614 "20120905" // Date of purchase
615 "Beatport", 35)); // Seller
616 CPPUNIT_ASSERT_EQUAL(String("GBP1.99"), f.pricePaid());
617 CPPUNIT_ASSERT_EQUAL(String("20120905"), f.datePurchased());
618 CPPUNIT_ASSERT_EQUAL(String("Beatport"), f.seller());
619 }
620
621 void testRenderOwnershipFrame()
622 {
623 ID3v2::OwnershipFrame f;
624 f.setPricePaid("GBP1.99");
625 f.setDatePurchased("20120905");
626 f.setSeller("Beatport");
627 CPPUNIT_ASSERT_EQUAL(
628 ByteVector("OWNE" // Frame ID
629 "\x00\x00\x00\x19" // Frame size
630 "\x00\x00" // Frame flags
631 "\x00" // Text encoding
632 "GBP1.99\x00" // Price paid
633 "20120905" // Date of purchase
634 "Beatport", 35), // URL
635 f.render());
636 }
637
638 void testParseSynchronizedLyricsFrame()
639 {
640 ID3v2::SynchronizedLyricsFrame f(
641 ByteVector("SYLT" // Frame ID
642 "\x00\x00\x00\x21" // Frame size
643 "\x00\x00" // Frame flags
644 "\x00" // Text encoding
645 "eng" // Language
646 "\x02" // Time stamp format
647 "\x01" // Content type
648 "foo\x00" // Content descriptor
649 "Example\x00" // 1st text
650 "\x00\x00\x04\xd2" // 1st time stamp
651 "Lyrics\x00" // 2nd text
652 "\x00\x00\x11\xd7", 43)); // 2nd time stamp
653 CPPUNIT_ASSERT_EQUAL(String::Latin1, f.textEncoding());
654 CPPUNIT_ASSERT_EQUAL(ByteVector("eng", 3), f.language());
655 CPPUNIT_ASSERT_EQUAL(ID3v2::SynchronizedLyricsFrame::AbsoluteMilliseconds,
656 f.timestampFormat());
657 CPPUNIT_ASSERT_EQUAL(ID3v2::SynchronizedLyricsFrame::Lyrics, f.type());
658 CPPUNIT_ASSERT_EQUAL(String("foo"), f.description());
659 ID3v2::SynchronizedLyricsFrame::SynchedTextList stl = f.synchedText();
660 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), stl.size());
661 CPPUNIT_ASSERT_EQUAL(String("Example"), stl[0].text);
662 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1234), stl[0].time);
663 CPPUNIT_ASSERT_EQUAL(String("Lyrics"), stl[1].text);
664 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4567), stl[1].time);
665 }
666
667 void testParseSynchronizedLyricsFrameWithEmptyDescritpion()
668 {
669 ID3v2::SynchronizedLyricsFrame f(
670 ByteVector("SYLT" // Frame ID
671 "\x00\x00\x00\x1e" // Frame size
672 "\x00\x00" // Frame flags
673 "\x00" // Text encoding
674 "eng" // Language
675 "\x02" // Time stamp format
676 "\x01" // Content type
677 "\x00" // Content descriptor
678 "Example\x00" // 1st text
679 "\x00\x00\x04\xd2" // 1st time stamp
680 "Lyrics\x00" // 2nd text
681 "\x00\x00\x11\xd7", 40)); // 2nd time stamp
682 CPPUNIT_ASSERT_EQUAL(String::Latin1, f.textEncoding());
683 CPPUNIT_ASSERT_EQUAL(ByteVector("eng", 3), f.language());
684 CPPUNIT_ASSERT_EQUAL(ID3v2::SynchronizedLyricsFrame::AbsoluteMilliseconds,
685 f.timestampFormat());
686 CPPUNIT_ASSERT_EQUAL(ID3v2::SynchronizedLyricsFrame::Lyrics, f.type());
687 CPPUNIT_ASSERT(f.description().isEmpty());
688 ID3v2::SynchronizedLyricsFrame::SynchedTextList stl = f.synchedText();
689 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), stl.size());
690 CPPUNIT_ASSERT_EQUAL(String("Example"), stl[0].text);
691 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1234), stl[0].time);
692 CPPUNIT_ASSERT_EQUAL(String("Lyrics"), stl[1].text);
693 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4567), stl[1].time);
694 }
695
696 void testRenderSynchronizedLyricsFrame()
697 {
698 ID3v2::SynchronizedLyricsFrame f;
699 f.setTextEncoding(String::Latin1);
700 f.setLanguage(ByteVector("eng", 3));
701 f.setTimestampFormat(ID3v2::SynchronizedLyricsFrame::AbsoluteMilliseconds);
702 f.setType(ID3v2::SynchronizedLyricsFrame::Lyrics);
703 f.setDescription("foo");
704 ID3v2::SynchronizedLyricsFrame::SynchedTextList stl;
705 stl.append(ID3v2::SynchronizedLyricsFrame::SynchedText(1234, "Example"));
706 stl.append(ID3v2::SynchronizedLyricsFrame::SynchedText(4567, "Lyrics"));
707 f.setSynchedText(stl);
708 CPPUNIT_ASSERT_EQUAL(
709 ByteVector("SYLT" // Frame ID
710 "\x00\x00\x00\x21" // Frame size
711 "\x00\x00" // Frame flags
712 "\x00" // Text encoding
713 "eng" // Language
714 "\x02" // Time stamp format
715 "\x01" // Content type
716 "foo\x00" // Content descriptor
717 "Example\x00" // 1st text
718 "\x00\x00\x04\xd2" // 1st time stamp
719 "Lyrics\x00" // 2nd text
720 "\x00\x00\x11\xd7", 43), // 2nd time stamp
721 f.render());
722 }
723
724 void testParseEventTimingCodesFrame()
725 {
726 ID3v2::EventTimingCodesFrame f(
727 ByteVector("ETCO" // Frame ID
728 "\x00\x00\x00\x0b" // Frame size
729 "\x00\x00" // Frame flags
730 "\x02" // Time stamp format
731 "\x02" // 1st event
732 "\x00\x00\xf3\x5c" // 1st time stamp
733 "\xfe" // 2nd event
734 "\x00\x36\xee\x80", 21)); // 2nd time stamp
735 CPPUNIT_ASSERT_EQUAL(ID3v2::EventTimingCodesFrame::AbsoluteMilliseconds,
736 f.timestampFormat());
737 ID3v2::EventTimingCodesFrame::SynchedEventList sel = f.synchedEvents();
738 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), sel.size());
739 CPPUNIT_ASSERT_EQUAL(ID3v2::EventTimingCodesFrame::IntroStart, sel[0].type);
740 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(62300), sel[0].time);
741 CPPUNIT_ASSERT_EQUAL(ID3v2::EventTimingCodesFrame::AudioFileEnds, sel[1].type);
742 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(3600000), sel[1].time);
743 }
744
745 void testRenderEventTimingCodesFrame()
746 {
747 ID3v2::EventTimingCodesFrame f;
748 f.setTimestampFormat(ID3v2::EventTimingCodesFrame::AbsoluteMilliseconds);
749 ID3v2::EventTimingCodesFrame::SynchedEventList sel;
750 sel.append(ID3v2::EventTimingCodesFrame::SynchedEvent(62300, ID3v2::EventTimingCodesFrame::IntroStart));
751 sel.append(ID3v2::EventTimingCodesFrame::SynchedEvent(3600000, ID3v2::EventTimingCodesFrame::AudioFileEnds));
752 f.setSynchedEvents(sel);
753 CPPUNIT_ASSERT_EQUAL(
754 ByteVector("ETCO" // Frame ID
755 "\x00\x00\x00\x0b" // Frame size
756 "\x00\x00" // Frame flags
757 "\x02" // Time stamp format
758 "\x02" // 1st event
759 "\x00\x00\xf3\x5c" // 1st time stamp
760 "\xfe" // 2nd event
761 "\x00\x36\xee\x80", 21), // 2nd time stamp
762 f.render());
763 }
764
765 void testParseCommentsFrame()
766 {
767 ID3v2::CommentsFrame f(
768 ByteVector("COMM"
769 "\x00\x00\x00\x14"
770 "\x00\x00"
771 "\x03"
772 "deu"
773 "Description\x00"
774 "Text", 30));
775 CPPUNIT_ASSERT_EQUAL(String::UTF8, f.textEncoding());
776 CPPUNIT_ASSERT_EQUAL(ByteVector("deu"), f.language());
777 CPPUNIT_ASSERT_EQUAL(String("Description"), f.description());
778 CPPUNIT_ASSERT_EQUAL(String("Text"), f.text());
779 }
780
781 void testRenderCommentsFrame()
782 {
783 ID3v2::CommentsFrame f;
784 f.setTextEncoding(String::UTF16);
785 f.setLanguage("eng");
786 f.setDescription("Description");
787 f.setText("Text");
788 CPPUNIT_ASSERT_EQUAL(
789 ByteVector("COMM"
790 "\x00\x00\x00\x28"
791 "\x00\x00"
792 "\x01"
793 "eng"
794 "\xff\xfe" "D\0e\0s\0c\0r\0i\0p\0t\0i\0o\0n\0" "\x00\x00"
795 "\xff\xfe" "T\0e\0x\0t\0", 50),
796 f.render());
797 }
798
799 void testParsePodcastFrame()
800 {
801 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
802 auto data = ByteVector("PCST"
803 "\x00\x00\x00\x04"
804 "\x00\x00"
805 "\x00\x00\x00\x00", 14);
806 const ID3v2::Header header;
807 ID3v2::Frame *frame = factory->createFrame(data, &header);
808 CPPUNIT_ASSERT(dynamic_cast<ID3v2::PodcastFrame *>(frame));
809 delete frame;
810 }
811
812 void testRenderPodcastFrame()
813 {
814 ID3v2::PodcastFrame f;
815 CPPUNIT_ASSERT_EQUAL(
816 ByteVector("PCST"
817 "\x00\x00\x00\x04"
818 "\x00\x00"
819 "\x00\x00\x00\x00", 14),
820 f.render());
821 }
822
823 void testParsePrivateFrame()
824 {
825 ID3v2::PrivateFrame f(
826 ByteVector("PRIV"
827 "\x00\x00\x00\x0e"
828 "\x00\x00"
829 "WM/Provider\x00"
830 "TL", 24));
831 CPPUNIT_ASSERT_EQUAL(String("WM/Provider"), f.owner());
832 CPPUNIT_ASSERT_EQUAL(ByteVector("TL"), f.data());
833 }
834
835 void testRenderPrivateFrame()
836 {
837 ID3v2::PrivateFrame f;
838 f.setOwner("WM/Provider");
839 f.setData("TL");
840 CPPUNIT_ASSERT_EQUAL(
841 ByteVector("PRIV"
842 "\x00\x00\x00\x0e"
843 "\x00\x00"
844 "WM/Provider\x00"
845 "TL", 24),
846 f.render());
847 }
848
849 void testParseUserTextIdentificationFrame()
850 {
851 ID3v2::UserTextIdentificationFrame frameWithoutDescription(
852 ByteVector("TXXX"
853 "\x00\x00\x00\x06"
854 "\x00\x00\x00"
855 "\x00"
856 "Text", 16));
857 CPPUNIT_ASSERT_EQUAL(String(""), frameWithoutDescription.description());
858 CPPUNIT_ASSERT_EQUAL(String("Text"), frameWithoutDescription.fieldList()[1]);
859
860 ID3v2::UserTextIdentificationFrame frameWithDescription(
861 ByteVector("TXXX"
862 "\x00\x00\x00\x11"
863 "\x00\x00\x00"
864 "Description\x00"
865 "Text", 27));
866 CPPUNIT_ASSERT_EQUAL(String("Description"), frameWithDescription.description());
867 CPPUNIT_ASSERT_EQUAL(String("Text"), frameWithDescription.fieldList()[1]);
868 }
869
870 void testRenderUserTextIdentificationFrame()
871 {
872 ID3v2::UserTextIdentificationFrame f;
873 f.setDescription("");
874 f.setText("Text");
875 CPPUNIT_ASSERT_EQUAL(
876 ByteVector("TXXX"
877 "\x00\x00\x00\x06"
878 "\x00\x00\x00"
879 "\x00"
880 "Text", 16),
881 f.render());
882
883 f.setDescription("Description");
884 f.setText("Text");
885 CPPUNIT_ASSERT_EQUAL(
886 ByteVector("TXXX"
887 "\x00\x00\x00\x11"
888 "\x00\x00\x00"
889 "Description\x00"
890 "Text", 27),
891 f.render());
892 }
893
894 void testItunes24FrameSize()
895 {
896 MPEG::File f(TEST_FILE_PATH_C("005411.id3"), false);
897 CPPUNIT_ASSERT(f.tag());
898 CPPUNIT_ASSERT(f.ID3v2Tag()->frameListMap().contains("TIT2"));
899 CPPUNIT_ASSERT_EQUAL(String("Sunshine Superman"), f.ID3v2Tag()->frameListMap()["TIT2"].front()->toString());
900 }
901
902 void testSaveUTF16Comment()
903 {
904 String::Type defaultEncoding = ID3v2::FrameFactory::instance()->defaultTextEncoding();
905 ScopedFileCopy copy("xing", ".mp3");
906 string newname = copy.fileName();
907 ID3v2::FrameFactory::instance()->setDefaultTextEncoding(String::UTF16);
908 {
909 MPEG::File foo(newname.c_str());
910 foo.strip();
911 foo.tag()->setComment("Test comment!");
912 foo.save();
913 }
914 {
915 MPEG::File bar(newname.c_str());
916 CPPUNIT_ASSERT_EQUAL(String("Test comment!"), bar.tag()->comment());
917 ID3v2::FrameFactory::instance()->setDefaultTextEncoding(defaultEncoding);
918 }
919 }
920
921 void testUpdateGenre23_1()
922 {
923 // "Refinement" is the same as the ID3v1 genre - duplicate
924 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
925 auto data = ByteVector("TCON" // Frame ID
926 "\x00\x00\x00\x10" // Frame size
927 "\x00\x00" // Frame flags
928 "\x00" // Encoding
929 "(22)Death Metal", 26); // Text
930 ID3v2::Header header;
931 header.setMajorVersion(3);
932 auto frame =
933 dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, &header));
934 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), frame->fieldList().size());
935 CPPUNIT_ASSERT_EQUAL(String("Death Metal"), frame->fieldList()[0]);
936
937 ID3v2::Tag tag;
938 tag.addFrame(frame);
939 CPPUNIT_ASSERT_EQUAL(String("Death Metal"), tag.genre());
940 }
941
942 void testUpdateGenre23_2()
943 {
944 // "Refinement" is different from the ID3v1 genre
945 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
946 auto data = ByteVector("TCON" // Frame ID
947 "\x00\x00\x00\x0d" // Frame size
948 "\x00\x00" // Frame flags
949 "\x00" // Encoding
950 "(4)Eurodisco", 23); // Text
951 ID3v2::Header header;
952 header.setMajorVersion(3);
953 auto frame =
954 dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, &header));
955 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), frame->fieldList().size());
956 CPPUNIT_ASSERT_EQUAL(String("4"), frame->fieldList()[0]);
957 CPPUNIT_ASSERT_EQUAL(String("Eurodisco"), frame->fieldList()[1]);
958
959 ID3v2::Tag tag;
960 tag.addFrame(frame);
961 CPPUNIT_ASSERT_EQUAL(String("Disco / Eurodisco"), tag.genre());
962 }
963
964 void testUpdateGenre23_3()
965 {
966 // Multiple references and a refinement
967 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
968 auto data = ByteVector("TCON" // Frame ID
969 "\x00\x00\x00\x15" // Frame size
970 "\x00\x00" // Frame flags
971 "\x00" // Encoding
972 "(9)(138)Viking Metal", 31); // Text
973 ID3v2::Header header;
974 header.setMajorVersion(3);
975 auto frame =
976 dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, &header));
977 CPPUNIT_ASSERT_EQUAL(3U, frame->fieldList().size());
978 CPPUNIT_ASSERT_EQUAL(String("9"), frame->fieldList()[0]);
979 CPPUNIT_ASSERT_EQUAL(String("138"), frame->fieldList()[1]);
980 CPPUNIT_ASSERT_EQUAL(String("Viking Metal"), frame->fieldList()[2]);
981
982 ID3v2::Tag tag;
983 tag.addFrame(frame);
984 CPPUNIT_ASSERT_EQUAL(String("Metal / Black Metal / Viking Metal"), tag.genre());
985 }
986
987 void testUpdateGenre24()
988 {
989 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
990 auto data = ByteVector("TCON" // Frame ID
991 "\x00\x00\x00\x0D" // Frame size
992 "\x00\x00" // Frame flags
993 "\0" // Encoding
994 "14\0Eurodisco", 23); // Text
995 ID3v2::Header header;
996 auto frame =
997 dynamic_cast<TagLib::ID3v2::TextIdentificationFrame*>(factory->createFrame(data, &header));
998 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), frame->fieldList().size());
999 CPPUNIT_ASSERT_EQUAL(String("14"), frame->fieldList()[0]);
1000 CPPUNIT_ASSERT_EQUAL(String("Eurodisco"), frame->fieldList()[1]);
1001
1002 ID3v2::Tag tag;
1003 tag.addFrame(frame);
1004 CPPUNIT_ASSERT_EQUAL(String("R&B / Eurodisco"), tag.genre());
1005 }
1006
1007 void testUpdateDate22()
1008 {
1009 MPEG::File f(TEST_FILE_PATH_C("id3v22-tda.mp3"), false);
1010 CPPUNIT_ASSERT(f.tag());
1011 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2010), f.tag()->year());
1012 }
1013
1014 void testUpdateFullDate22()
1015 {
1016 MPEG::File f(TEST_FILE_PATH_C("id3v22-tda.mp3"), false);
1017 CPPUNIT_ASSERT(f.tag());
1018 CPPUNIT_ASSERT_EQUAL(String("2010-04-03"), f.ID3v2Tag()->frameListMap()["TDRC"].front()->toString());
1019 }
1020
1021 void testDowngradeTo23()
1022 {
1023 ScopedFileCopy copy("xing", ".mp3");
1024 string newname = copy.fileName();
1025
1026 ID3v2::TextIdentificationFrame *tf;
1027 {
1028 MPEG::File foo(newname.c_str());
1029 tf = new ID3v2::TextIdentificationFrame("TDOR", String::Latin1);
1030 tf->setText("2011-03-16");
1031 foo.ID3v2Tag()->addFrame(tf);
1032 tf = new ID3v2::TextIdentificationFrame("TDRC", String::Latin1);
1033 tf->setText("2012-04-17T12:01");
1034 foo.ID3v2Tag()->addFrame(tf);
1035 tf = new ID3v2::TextIdentificationFrame("TMCL", String::Latin1);
1036 tf->setText(StringList().append("Guitar").append("Artist 1").append("Drums").append("Artist 2"));
1037 foo.ID3v2Tag()->addFrame(tf);
1038 tf = new ID3v2::TextIdentificationFrame("TIPL", String::Latin1);
1039 tf->setText(StringList().append("Producer").append("Artist 3").append("Engineer").append("Artist 4"));
1040 foo.ID3v2Tag()->addFrame(tf);
1041 tf = new ID3v2::TextIdentificationFrame("TCON", String::Latin1);
1042 tf->setText(StringList().append("51").append("Noise").append("Power Noise"));
1043 foo.ID3v2Tag()->addFrame(tf);
1044 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TDRL", String::Latin1));
1045 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TDTG", String::Latin1));
1046 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TMOO", String::Latin1));
1047 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TPRO", String::Latin1));
1048 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TSOA", String::Latin1));
1049 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TSOT", String::Latin1));
1050 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TSST", String::Latin1));
1051 foo.ID3v2Tag()->addFrame(new ID3v2::TextIdentificationFrame("TSOP", String::Latin1));
1052 foo.save(MPEG::File::AllTags, File::StripOthers, ID3v2::v3);
1053 }
1054 {
1055 MPEG::File bar(newname.c_str());
1056 tf = dynamic_cast<ID3v2::TextIdentificationFrame *>(bar.ID3v2Tag()->frameList("TDOR").front());
1057 CPPUNIT_ASSERT(tf);
1058 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), tf->fieldList().size());
1059 CPPUNIT_ASSERT_EQUAL(String("2011"), tf->fieldList().front());
1060 tf = dynamic_cast<ID3v2::TextIdentificationFrame *>(bar.ID3v2Tag()->frameList("TDRC").front());
1061 CPPUNIT_ASSERT(tf);
1062 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), tf->fieldList().size());
1063 CPPUNIT_ASSERT_EQUAL(String("2012-04-17T12:01"), tf->fieldList().front());
1064 tf = dynamic_cast<ID3v2::TextIdentificationFrame *>(bar.ID3v2Tag()->frameList("TIPL").front());
1065 CPPUNIT_ASSERT(tf);
1066 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), tf->fieldList().size());
1067 CPPUNIT_ASSERT_EQUAL(String("Producer"), tf->fieldList()[0]);
1068 CPPUNIT_ASSERT_EQUAL(String("Artist 3"), tf->fieldList()[1]);
1069 CPPUNIT_ASSERT_EQUAL(String("Engineer"), tf->fieldList()[2]);
1070 CPPUNIT_ASSERT_EQUAL(String("Artist 4"), tf->fieldList()[3]);
1071 tf = dynamic_cast<ID3v2::TextIdentificationFrame *>(bar.ID3v2Tag()->frameList("TMCL").front());
1072 CPPUNIT_ASSERT(tf);
1073 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(4), tf->fieldList().size());
1074 CPPUNIT_ASSERT_EQUAL(String("Guitar"), tf->fieldList()[0]);
1075 CPPUNIT_ASSERT_EQUAL(String("Artist 1"), tf->fieldList()[1]);
1076 CPPUNIT_ASSERT_EQUAL(String("Drums"), tf->fieldList()[2]);
1077 CPPUNIT_ASSERT_EQUAL(String("Artist 2"), tf->fieldList()[3]);
1078 tf = dynamic_cast<ID3v2::TextIdentificationFrame *>(bar.ID3v2Tag()->frameList("TCON").front());
1079 CPPUNIT_ASSERT(tf);
1080 CPPUNIT_ASSERT_EQUAL(3U, tf->fieldList().size());
1081 CPPUNIT_ASSERT_EQUAL(String("51"), tf->fieldList()[0]);
1082 CPPUNIT_ASSERT_EQUAL(String("39"), tf->fieldList()[1]);
1083 CPPUNIT_ASSERT_EQUAL(String("Power Noise"), tf->fieldList()[2]);
1084 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TDRL"));
1085 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TDTG"));
1086 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TMOO"));
1087 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TPRO"));
1088 #ifdef NO_ITUNES_HACKS
1089 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TSOA"));
1090 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TSOT"));
1091 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TSOP"));
1092 #endif
1093 CPPUNIT_ASSERT(!bar.ID3v2Tag()->frameListMap().contains("TSST"));
1094 }
1095 {
1096 const ByteVector expectedId3v23Data(
1097 "ID3" "\x03\x00\x00\x00\x00\x09\x48"
1098 "TSOA" "\x00\x00\x00\x01\x00\x00\x00"
1099 "TSOT" "\x00\x00\x00\x01\x00\x00\x00"
1100 "TSOP" "\x00\x00\x00\x01\x00\x00\x00"
1101 "TORY" "\x00\x00\x00\x05\x00\x00\x00" "2011"
1102 "TYER" "\x00\x00\x00\x05\x00\x00\x00" "2012"
1103 "TDAT" "\x00\x00\x00\x05\x00\x00\x00" "1704"
1104 "TIME" "\x00\x00\x00\x05\x00\x00\x00" "1201"
1105 "IPLS" "\x00\x00\x00\x43\x00\x00\x00" "Guitar" "\x00"
1106 "Artist 1" "\x00" "Drums" "\x00" "Artist 2" "\x00" "Producer" "\x00"
1107 "Artist 3" "\x00" "Engineer" "\x00" "Artist 4"
1108 "TCON" "\x00\x00\x00\x14\x00\x00\x00" "(51)(39)Power Noise", 210);
1109 const ByteVector actualId3v23Data =
1110 PlainFile(newname.c_str()).readBlock(expectedId3v23Data.size());
1111 CPPUNIT_ASSERT_EQUAL(expectedId3v23Data, actualId3v23Data);
1112 }
1113 {
1114 ScopedFileCopy rareFramesCopy("rare_frames", ".mp3");
1115 MPEG::File f(rareFramesCopy.fileName().c_str());
1116 f.save(MPEG::File::AllTags, File::StripOthers, ID3v2::v3);
1117 f.seek(f.find("TCON") + 11);
1118 CPPUNIT_ASSERT_EQUAL(ByteVector("(13)"), f.readBlock(4));
1119 }
1120 }
1121
1122 void testCompressedFrameWithBrokenLength()
1123 {
1124 MPEG::File f(TEST_FILE_PATH_C("compressed_id3_frame.mp3"), false);
1125 CPPUNIT_ASSERT(f.ID3v2Tag()->frameListMap().contains("APIC"));
1126
1127 if(zlib::isAvailable()) {
1128 auto frame
1129 = dynamic_cast<TagLib::ID3v2::AttachedPictureFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
1130 CPPUNIT_ASSERT(frame);
1131 assert(frame != nullptr); // to silence the clang analyzer
1132 CPPUNIT_ASSERT_EQUAL(String("image/bmp"), frame->mimeType());
1133 CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::Other, frame->type());
1134 CPPUNIT_ASSERT_EQUAL(String(""), frame->description());
1135 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(86414), frame->picture().size());
1136 }
1137 else {
1138 // Skip the test if ZLIB is not installed.
1139 // The message "Compressed frames are currently not supported." will be displayed.
1140
1141 auto frame
1142 = dynamic_cast<TagLib::ID3v2::UnknownFrame*>(f.ID3v2Tag()->frameListMap()["APIC"].front());
1143 CPPUNIT_ASSERT(frame);
1144 }
1145 }
1146
1147 void testW000()
1148 {
1149 MPEG::File f(TEST_FILE_PATH_C("w000.mp3"), false);
1150 CPPUNIT_ASSERT(f.ID3v2Tag()->frameListMap().contains("W000"));
1151 auto frame =
1152 dynamic_cast<TagLib::ID3v2::UrlLinkFrame*>(f.ID3v2Tag()->frameListMap()["W000"].front());
1153 CPPUNIT_ASSERT(frame);
1154 assert(frame != nullptr); // to silence the clang analyzer
1155 CPPUNIT_ASSERT_EQUAL(String("[email protected]____"), frame->url());
1156 }
1157
1158 void testPropertyInterface()
1159 {
1160 ScopedFileCopy copy("rare_frames", ".mp3");
1161 string newname = copy.fileName();
1162 MPEG::File f(newname.c_str());
1163 PropertyMap dict = f.ID3v2Tag(false)->properties();
1164 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(6), dict.size());
1165
1166 CPPUNIT_ASSERT(dict.contains("USERTEXTDESCRIPTION1"));
1167 CPPUNIT_ASSERT(dict.contains("QuodLibet::USERTEXTDESCRIPTION2"));
1168 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), dict["USERTEXTDESCRIPTION1"].size());
1169 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), dict["QuodLibet::USERTEXTDESCRIPTION2"].size());
1170 CPPUNIT_ASSERT_EQUAL(String("userTextData1"), dict["USERTEXTDESCRIPTION1"][0]);
1171 CPPUNIT_ASSERT_EQUAL(String("userTextData2"), dict["USERTEXTDESCRIPTION1"][1]);
1172 CPPUNIT_ASSERT_EQUAL(String("userTextData1"), dict["QuodLibet::USERTEXTDESCRIPTION2"][0]);
1173 CPPUNIT_ASSERT_EQUAL(String("userTextData2"), dict["QuodLibet::USERTEXTDESCRIPTION2"][1]);
1174
1175 CPPUNIT_ASSERT_EQUAL(String("Pop"), dict["GENRE"].front());
1176
1177 CPPUNIT_ASSERT_EQUAL(String("http://a.user.url"), dict["URL:USERURL"].front());
1178
1179 CPPUNIT_ASSERT_EQUAL(String("http://a.user.url/with/empty/description"), dict["URL"].front());
1180 CPPUNIT_ASSERT_EQUAL(String("A COMMENT"), dict["COMMENT"].front());
1181
1182 CPPUNIT_ASSERT_EQUAL(1u, dict.unsupportedData().size());
1183 CPPUNIT_ASSERT_EQUAL(String("UFID/[email protected]"), dict.unsupportedData().front());
1184 }
1185
1186 void testPropertyInterface2()
1187 {
1188 ID3v2::Tag tag;
1189 auto frame1 = new ID3v2::UnsynchronizedLyricsFrame();
1190 frame1->setDescription("test");
1191 frame1->setText("la-la-la test");
1192 tag.addFrame(frame1);
1193
1194 auto frame2 = new ID3v2::UnsynchronizedLyricsFrame();
1195 frame2->setDescription("");
1196 frame2->setText("la-la-la nodescription");
1197 tag.addFrame(frame2);
1198
1199 auto frame3 = new ID3v2::AttachedPictureFrame();
1200 frame3->setDescription("test picture");
1201 tag.addFrame(frame3);
1202
1203 auto frame4 = new ID3v2::TextIdentificationFrame("TIPL");
1204 frame4->setText("single value is invalid for TIPL");
1205 tag.addFrame(frame4);
1206
1207 auto frame5 = new ID3v2::TextIdentificationFrame("TMCL");
1208 StringList tmclData;
1209 tmclData.append("VIOLIN");
1210 tmclData.append("a violinist");
1211 tmclData.append("PIANO");
1212 tmclData.append("a pianist");
1213 frame5->setText(tmclData);
1214 tag.addFrame(frame5);
1215
1216 auto frame6 = new ID3v2::UniqueFileIdentifierFrame("http://musicbrainz.org", "152454b9-19ba-49f3-9fc9-8fc26545cf41");
1217 tag.addFrame(frame6);
1218
1219 auto frame7 = new ID3v2::UniqueFileIdentifierFrame("http://example.com", "123");
1220 tag.addFrame(frame7);
1221
1222 auto frame8 = new ID3v2::UserTextIdentificationFrame();
1223 frame8->setDescription("MusicBrainz Album Id");
1224 frame8->setText("95c454a5-d7e0-4d8f-9900-db04aca98ab3");
1225 tag.addFrame(frame8);
1226
1227 auto frame9 = new ID3v2::UnknownFrame("WXYZ");
1228 tag.addFrame(frame9);
1229
1230 auto frame10 = new ID3v2::CommentsFrame();
1231 frame10->setDescription("iTunNORM");
1232 frame10->setText("00002C3B");
1233 frame10->setLanguage("eng");
1234 tag.addFrame(frame10);
1235
1236 PropertyMap properties = tag.properties();
1237
1238 CPPUNIT_ASSERT_EQUAL(4u, properties.unsupportedData().size());
1239 CPPUNIT_ASSERT(properties.unsupportedData().contains("TIPL"));
1240 CPPUNIT_ASSERT(properties.unsupportedData().contains("APIC"));
1241 CPPUNIT_ASSERT(properties.unsupportedData().contains("UFID/http://example.com"));
1242 CPPUNIT_ASSERT(properties.unsupportedData().contains("UNKNOWN/WXYZ"));
1243
1244 CPPUNIT_ASSERT(properties.contains("PERFORMER:VIOLIN"));
1245 CPPUNIT_ASSERT(properties.contains("PERFORMER:PIANO"));
1246 CPPUNIT_ASSERT_EQUAL(String("a violinist"), properties["PERFORMER:VIOLIN"].front());
1247 CPPUNIT_ASSERT_EQUAL(String("a pianist"), properties["PERFORMER:PIANO"].front());
1248
1249 CPPUNIT_ASSERT(properties.contains("LYRICS"));
1250 CPPUNIT_ASSERT(properties.contains("LYRICS:TEST"));
1251
1252 CPPUNIT_ASSERT(properties.contains("MUSICBRAINZ_TRACKID"));
1253 CPPUNIT_ASSERT_EQUAL(String("152454b9-19ba-49f3-9fc9-8fc26545cf41"), properties["MUSICBRAINZ_TRACKID"].front());
1254
1255 CPPUNIT_ASSERT(properties.contains("MUSICBRAINZ_ALBUMID"));
1256 CPPUNIT_ASSERT_EQUAL(String("95c454a5-d7e0-4d8f-9900-db04aca98ab3"), properties["MUSICBRAINZ_ALBUMID"].front());
1257
1258 CPPUNIT_ASSERT(properties.contains("COMMENT:ITUNNORM"));
1259 CPPUNIT_ASSERT_EQUAL(String("00002C3B"), properties["COMMENT:ITUNNORM"].front());
1260
1261 tag.removeUnsupportedProperties(properties.unsupportedData());
1262 CPPUNIT_ASSERT(tag.frameList("APIC").isEmpty());
1263 CPPUNIT_ASSERT(tag.frameList("TIPL").isEmpty());
1264 CPPUNIT_ASSERT(tag.frameList("WXYZ").isEmpty());
1265 CPPUNIT_ASSERT_EQUAL(static_cast<ID3v2::UniqueFileIdentifierFrame *>(nullptr), ID3v2::UniqueFileIdentifierFrame::findByOwner(&tag, "http://example.com"));
1266 CPPUNIT_ASSERT_EQUAL(frame1, ID3v2::UnsynchronizedLyricsFrame::findByDescription(&tag, "test"));
1267 CPPUNIT_ASSERT_EQUAL(frame6, ID3v2::UniqueFileIdentifierFrame::findByOwner(&tag, "http://musicbrainz.org"));
1268 CPPUNIT_ASSERT_EQUAL(frame8, ID3v2::UserTextIdentificationFrame::find(&tag, "MusicBrainz Album Id"));
1269 CPPUNIT_ASSERT_EQUAL(static_cast<ID3v2::UserTextIdentificationFrame *>(nullptr), ID3v2::UserTextIdentificationFrame::find(&tag, "non existing"));
1270 CPPUNIT_ASSERT_EQUAL(frame10, ID3v2::CommentsFrame::findByDescription(&tag, "iTunNORM"));
1271 CPPUNIT_ASSERT_EQUAL(static_cast<ID3v2::CommentsFrame *>(nullptr), ID3v2::CommentsFrame::findByDescription(&tag, "non existing"));
1272
1273 // Check if the allowed TIPL keys are correctly mapped to properties.
1274 // These are the keys used by MusicBrainz: arranger, engineer, producer, DJ-mix, mix.
1275 // See https://picard-docs.musicbrainz.org/downloads/MusicBrainz_Picard_Tag_Map.html.
1276 // MusicBrainz Picard uses lowercase keys whereas TagLib used uppercase keys.
1277 auto frame11 = new ID3v2::TextIdentificationFrame("TIPL");
1278 StringList tiplData;
1279 tiplData.append("arranger");
1280 tiplData.append("an arranger");
1281 tiplData.append("ENGINEER");
1282 tiplData.append("an engineer");
1283 tiplData.append("producer");
1284 tiplData.append("a producer");
1285 tiplData.append("DJ-mix");
1286 tiplData.append("a DJ mixer");
1287 tiplData.append("mix");
1288 tiplData.append("a mixer");
1289 frame11->setText(tiplData);
1290 tag.addFrame(frame11);
1291
1292 properties = tag.properties();
1293 CPPUNIT_ASSERT_EQUAL(0u, properties.unsupportedData().size());
1294 CPPUNIT_ASSERT(properties.contains("ARRANGER"));
1295 CPPUNIT_ASSERT(properties.contains("ENGINEER"));
1296 CPPUNIT_ASSERT(properties.contains("PRODUCER"));
1297 CPPUNIT_ASSERT(properties.contains("DJMIXER"));
1298 CPPUNIT_ASSERT(properties.contains("MIXER"));
1299 CPPUNIT_ASSERT_EQUAL(String("an arranger"), properties["ARRANGER"].front());
1300 CPPUNIT_ASSERT_EQUAL(String("an engineer"), properties["ENGINEER"].front());
1301 CPPUNIT_ASSERT_EQUAL(String("a producer"), properties["PRODUCER"].front());
1302 CPPUNIT_ASSERT_EQUAL(String("a DJ mixer"), properties["DJMIXER"].front());
1303 CPPUNIT_ASSERT_EQUAL(String("a mixer"), properties["MIXER"].front());
1304 }
1305
1306 void testPropertiesMovement()
1307 {
1308 ID3v2::Tag tag;
1309 auto frameMvnm = new ID3v2::TextIdentificationFrame("MVNM");
1310 frameMvnm->setText("Movement Name");
1311 tag.addFrame(frameMvnm);
1312
1313 auto frameMvin = new ID3v2::TextIdentificationFrame("MVIN");
1314 frameMvin->setText("2/3");
1315 tag.addFrame(frameMvin);
1316
1317 PropertyMap properties = tag.properties();
1318 CPPUNIT_ASSERT(properties.contains("MOVEMENTNAME"));
1319 CPPUNIT_ASSERT(properties.contains("MOVEMENTNUMBER"));
1320 CPPUNIT_ASSERT_EQUAL(String("Movement Name"), properties["MOVEMENTNAME"].front());
1321 CPPUNIT_ASSERT_EQUAL(String("2/3"), properties["MOVEMENTNUMBER"].front());
1322
1323 ByteVector frameDataMvnm("MVNM"
1324 "\x00\x00\x00\x0e"
1325 "\x00\x00"
1326 "\x00"
1327 "Movement Name", 24);
1328 CPPUNIT_ASSERT_EQUAL(frameDataMvnm, frameMvnm->render());
1329 ByteVector frameDataMvin("MVIN"
1330 "\x00\x00\x00\x04"
1331 "\x00\x00"
1332 "\x00"
1333 "2/3", 14);
1334 CPPUNIT_ASSERT_EQUAL(frameDataMvin, frameMvin->render());
1335
1336 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
1337 ID3v2::Header header;
1338 auto parsedFrameMvnm = dynamic_cast<ID3v2::TextIdentificationFrame *>(
1339 factory->createFrame(frameDataMvnm, &header));
1340 auto parsedFrameMvin = dynamic_cast<ID3v2::TextIdentificationFrame *>(
1341 factory->createFrame(frameDataMvin, &header));
1342 CPPUNIT_ASSERT(parsedFrameMvnm);
1343 CPPUNIT_ASSERT(parsedFrameMvin);
1344 CPPUNIT_ASSERT_EQUAL(String("Movement Name"), parsedFrameMvnm->toString());
1345 CPPUNIT_ASSERT_EQUAL(String("2/3"), parsedFrameMvin->toString());
1346
1347 tag.addFrame(parsedFrameMvnm);
1348 tag.addFrame(parsedFrameMvin);
1349 }
1350
1351 void testPropertyGrouping()
1352 {
1353 ID3v2::Tag tag;
1354 auto frameGrp1 = new ID3v2::TextIdentificationFrame("GRP1");
1355 frameGrp1->setText("Grouping");
1356 tag.addFrame(frameGrp1);
1357
1358 PropertyMap properties = tag.properties();
1359 CPPUNIT_ASSERT(properties.contains("GROUPING"));
1360 CPPUNIT_ASSERT_EQUAL(String("Grouping"), properties["GROUPING"].front());
1361
1362 ByteVector frameDataGrp1("GRP1"
1363 "\x00\x00\x00\x09"
1364 "\x00\x00"
1365 "\x00"
1366 "Grouping", 19);
1367 CPPUNIT_ASSERT_EQUAL(frameDataGrp1, frameGrp1->render());
1368
1369 ID3v2::FrameFactory *factory = ID3v2::FrameFactory::instance();
1370 ID3v2::Header header;
1371 auto parsedFrameGrp1 = dynamic_cast<ID3v2::TextIdentificationFrame *>(
1372 factory->createFrame(frameDataGrp1, &header));
1373 CPPUNIT_ASSERT(parsedFrameGrp1);
1374 CPPUNIT_ASSERT_EQUAL(String("Grouping"), parsedFrameGrp1->toString());
1375
1376 tag.addFrame(parsedFrameGrp1);
1377 }
1378
1379 void testDeleteFrame()
1380 {
1381 ScopedFileCopy copy("rare_frames", ".mp3");
1382 string newname = copy.fileName();
1383
1384 {
1385 MPEG::File f(newname.c_str());
1386 ID3v2::Tag *t = f.ID3v2Tag();
1387 ID3v2::Frame *frame = t->frameList("TCON")[0];
1388 CPPUNIT_ASSERT_EQUAL(1u, t->frameList("TCON").size());
1389 t->removeFrame(frame, true);
1390 f.save(MPEG::File::ID3v2);
1391 }
1392 {
1393 MPEG::File f2(newname.c_str());
1394 ID3v2::Tag *t = f2.ID3v2Tag();
1395 CPPUNIT_ASSERT(t->frameList("TCON").isEmpty());
1396 }
1397 }
1398
1399 void testSaveAndStripID3v1ShouldNotAddFrameFromID3v1ToId3v2()
1400 {
1401 ScopedFileCopy copy("xing", ".mp3");
1402 string newname = copy.fileName();
1403
1404 {
1405 MPEG::File foo(newname.c_str());
1406 foo.tag()->setArtist("Artist");
1407 foo.save(MPEG::File::ID3v1 | MPEG::File::ID3v2);
1408 }
1409
1410 {
1411 MPEG::File bar(newname.c_str());
1412 bar.ID3v2Tag()->removeFrames("TPE1");
1413 // Should strip ID3v1 here and not add old values to ID3v2 again
1414 bar.save(MPEG::File::ID3v2, File::StripOthers);
1415 }
1416
1417 MPEG::File f(newname.c_str());
1418 CPPUNIT_ASSERT(!f.ID3v2Tag()->frameListMap().contains("TPE1"));
1419 }
1420
1421 void testParseChapterFrame()
1422 {
1423 ID3v2::Header header;
1424
1425 auto chapterData =
1426 ByteVector("CHAP" // Frame ID
1427 "\x00\x00\x00\x12" // Frame size
1428 "\x00\x00" // Frame flags
1429 "\x43\x00" // Element ID ("C")
1430 "\x00\x00\x00\x03" // Start time
1431 "\x00\x00\x00\x05" // End time
1432 "\x00\x00\x00\x02" // Start offset
1433 "\x00\x00\x00\x03", 28); // End offset
1434 auto embeddedFrameData =
1435 ByteVector("TIT2" // Embedded frame ID
1436 "\x00\x00\x00\x04" // Embedded frame size
1437 "\x00\x00" // Embedded frame flags
1438 "\x00" // TIT2 frame text encoding
1439 "CH1", 14); // Chapter title
1440
1441 ID3v2::ChapterFrame f1(&header, chapterData);
1442
1443 CPPUNIT_ASSERT_EQUAL(ByteVector("C"), f1.elementID());
1444 CPPUNIT_ASSERT(static_cast<unsigned int>(0x03) == f1.startTime());
1445 CPPUNIT_ASSERT(static_cast<unsigned int>(0x05) == f1.endTime());
1446 CPPUNIT_ASSERT(static_cast<unsigned int>(0x02) == f1.startOffset());
1447 CPPUNIT_ASSERT(static_cast<unsigned int>(0x03) == f1.endOffset());
1448 CPPUNIT_ASSERT(static_cast<unsigned int>(0x00) == f1.embeddedFrameList().size());
1449
1450 auto chapWithTit2Data = chapterData + embeddedFrameData;
1451 // Adapt the frame size for the added embedded frame data.
1452 chapWithTit2Data[7] = static_cast<char>(chapterData[7] + embeddedFrameData.size());
1453 ID3v2::ChapterFrame f2(&header, chapWithTit2Data);
1454
1455 CPPUNIT_ASSERT_EQUAL(ByteVector("C"), f2.elementID());
1456 CPPUNIT_ASSERT(static_cast<unsigned int>(0x03) == f2.startTime());
1457 CPPUNIT_ASSERT(static_cast<unsigned int>(0x05) == f2.endTime());
1458 CPPUNIT_ASSERT(static_cast<unsigned int>(0x02) == f2.startOffset());
1459 CPPUNIT_ASSERT(static_cast<unsigned int>(0x03) == f2.endOffset());
1460 CPPUNIT_ASSERT(static_cast<unsigned int>(0x01) == f2.embeddedFrameList().size());
1461 CPPUNIT_ASSERT(f2.embeddedFrameList("TIT2").size() == 1);
1462 CPPUNIT_ASSERT(f2.embeddedFrameList("TIT2")[0]->toString() == "CH1");
1463 }
1464
1465 void testRenderChapterFrame()
1466 {
1467 ID3v2::Header header;
1468 ID3v2::ChapterFrame f1(&header, "CHAP");
1469 f1.setElementID(ByteVector("\x43\x00", 2));
1470 f1.setStartTime(3);
1471 f1.setEndTime(5);
1472 f1.setStartOffset(2);
1473 f1.setEndOffset(3);
1474 auto eF = new ID3v2::TextIdentificationFrame("TIT2");
1475 eF->setText("CH1");
1476 f1.addEmbeddedFrame(eF);
1477
1478 auto expected =
1479 ByteVector("CHAP" // Frame ID
1480 "\x00\x00\x00\x20" // Frame size
1481 "\x00\x00" // Frame flags
1482 "\x43\x00" // Element ID
1483 "\x00\x00\x00\x03" // Start time
1484 "\x00\x00\x00\x05" // End time
1485 "\x00\x00\x00\x02" // Start offset
1486 "\x00\x00\x00\x03" // End offset
1487 "TIT2" // Embedded frame ID
1488 "\x00\x00\x00\x04" // Embedded frame size
1489 "\x00\x00" // Embedded frame flags
1490 "\x00" // TIT2 frame text encoding
1491 "CH1", 42); // Chapter title
1492
1493 CPPUNIT_ASSERT_EQUAL(expected, f1.render());
1494
1495 f1.setElementID("C");
1496
1497 CPPUNIT_ASSERT_EQUAL(expected, f1.render());
1498
1499 ID3v2::FrameList frames;
1500 eF = new ID3v2::TextIdentificationFrame("TIT2");
1501 eF->setText("CH1");
1502 frames.append(eF);
1503
1504 ID3v2::ChapterFrame f2(ByteVector("\x43\x00", 2), 3, 5, 2, 3, frames);
1505 CPPUNIT_ASSERT_EQUAL(expected, f2.render());
1506
1507 frames.clear();
1508 eF = new ID3v2::TextIdentificationFrame("TIT2");
1509 eF->setText("CH1");
1510 frames.append(eF);
1511
1512 ID3v2::ChapterFrame f3(ByteVector("C\x00", 2), 3, 5, 2, 3, frames);
1513 CPPUNIT_ASSERT_EQUAL(expected, f3.render());
1514
1515 frames.clear();
1516 eF = new ID3v2::TextIdentificationFrame("TIT2");
1517 eF->setText("CH1");
1518 frames.append(eF);
1519
1520 ID3v2::ChapterFrame f4("C", 3, 5, 2, 3, frames);
1521 CPPUNIT_ASSERT_EQUAL(expected, f4.render());
1522
1523 CPPUNIT_ASSERT(!f4.toString().isEmpty());
1524
1525 ID3v2::ChapterFrame f5("C", 3, 5, 2, 3);
1526 eF = new ID3v2::TextIdentificationFrame("TIT2");
1527 eF->setText("CH1");
1528 f5.addEmbeddedFrame(eF);
1529 CPPUNIT_ASSERT_EQUAL(expected, f5.render());
1530 }
1531
1532 void testParseTableOfContentsFrame()
1533 {
1534 ID3v2::Header header;
1535 ID3v2::TableOfContentsFrame f(
1536 &header,
1537 ByteVector("CTOC" // Frame ID
1538 "\x00\x00\x00\x16" // Frame size
1539 "\x00\x00" // Frame flags
1540 "\x54\x00" // Element ID ("T")
1541 "\x01" // CTOC flags
1542 "\x02" // Entry count
1543 "\x43\x00" // First entry ("C")
1544 "\x44\x00" // Second entry ("D")
1545 "TIT2" // Embedded frame ID
1546 "\x00\x00\x00\x04" // Embedded frame size
1547 "\x00\x00" // Embedded frame flags
1548 "\x00" // TIT2 frame text encoding
1549 "TC1", 32)); // Table of contents title
1550 CPPUNIT_ASSERT_EQUAL(ByteVector("T"), f.elementID());
1551 CPPUNIT_ASSERT(!f.isTopLevel());
1552 CPPUNIT_ASSERT(f.isOrdered());
1553 CPPUNIT_ASSERT(static_cast<unsigned int>(0x02) == f.entryCount());
1554 CPPUNIT_ASSERT_EQUAL(ByteVector("C"), f.childElements()[0]);
1555 CPPUNIT_ASSERT_EQUAL(ByteVector("D"), f.childElements()[1]);
1556 CPPUNIT_ASSERT(static_cast<unsigned int>(0x01) == f.embeddedFrameList().size());
1557 CPPUNIT_ASSERT(f.embeddedFrameList("TIT2").size() == 1);
1558 CPPUNIT_ASSERT(f.embeddedFrameList("TIT2")[0]->toString() == "TC1");
1559
1560 f.removeChildElement("E"); // not existing
1561 CPPUNIT_ASSERT_EQUAL(2U, f.entryCount());
1562 f.removeChildElement("C");
1563 CPPUNIT_ASSERT_EQUAL(1U, f.entryCount());
1564 CPPUNIT_ASSERT_EQUAL(ByteVector("D"), f.childElements()[0]);
1565
1566 ID3v2::Frame *frame = f.embeddedFrameList("TIT2")[0];
1567 f.removeEmbeddedFrame(frame);
1568 CPPUNIT_ASSERT(f.embeddedFrameList("TIT2").isEmpty());
1569 }
1570
1571 void testRenderTableOfContentsFrame()
1572 {
1573 ID3v2::Header header;
1574 ID3v2::TableOfContentsFrame f(&header, "CTOC");
1575 f.setElementID("T");
1576 f.setIsTopLevel(false);
1577 f.setIsOrdered(true);
1578 f.addChildElement("C");
1579 f.addChildElement("D");
1580 auto eF = new ID3v2::TextIdentificationFrame("TIT2");
1581 eF->setText("TC1");
1582 f.addEmbeddedFrame(eF);
1583 CPPUNIT_ASSERT_EQUAL(
1584 ByteVector("CTOC" // Frame ID
1585 "\x00\x00\x00\x16" // Frame size
1586 "\x00\x00" // Frame flags
1587 "\x54\x00" // Element ID
1588 "\x01" // CTOC flags
1589 "\x02" // Entry count
1590 "\x43\x00" // First entry
1591 "\x44\x00" // Second entry
1592 "TIT2" // Embedded frame ID
1593 "\x00\x00\x00\x04" // Embedded frame size
1594 "\x00\x00" // Embedded frame flags
1595 "\x00" // TIT2 frame text encoding
1596 "TC1", 32), // Table of contents title
1597 f.render());
1598 }
1599
1600 void testShrinkPadding()
1601 {
1602 ScopedFileCopy copy("xing", ".mp3");
1603 string newname = copy.fileName();
1604
1605 {
1606 MPEG::File f(newname.c_str());
1607 f.ID3v2Tag()->setTitle(longText(64 * 1024));
1608 f.save(MPEG::File::ID3v2, File::StripOthers);
1609 }
1610 {
1611 MPEG::File f(newname.c_str());
1612 CPPUNIT_ASSERT(f.hasID3v2Tag());
1613 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(74789), f.length());
1614 f.ID3v2Tag()->setTitle("ABCDEFGHIJ");
1615 f.save(MPEG::File::ID3v2, File::StripOthers);
1616 }
1617 {
1618 MPEG::File f(newname.c_str());
1619 CPPUNIT_ASSERT(f.hasID3v2Tag());
1620 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(9263), f.length());
1621 }
1622 }
1623
1624 void testEmptyFrame()
1625 {
1626 ScopedFileCopy copy("xing", ".mp3");
1627 string newname = copy.fileName();
1628
1629 {
1630 MPEG::File f(newname.c_str());
1631 ID3v2::Tag *tag = f.ID3v2Tag(true);
1632
1633 auto frame1 = new ID3v2::UrlLinkFrame(
1634 ByteVector("WOAF\x00\x00\x00\x01\x00\x00\x00", 11));
1635 tag->addFrame(frame1);
1636
1637 auto frame2 = new ID3v2::TextIdentificationFrame("TIT2");
1638 frame2->setText("Title");
1639 tag->addFrame(frame2);
1640
1641 f.save();
1642 }
1643
1644 {
1645 MPEG::File f(newname.c_str());
1646 CPPUNIT_ASSERT_EQUAL(true, f.hasID3v2Tag());
1647
1648 ID3v2::Tag *tag = f.ID3v2Tag();
1649 CPPUNIT_ASSERT_EQUAL(String("Title"), tag->title());
1650 CPPUNIT_ASSERT_EQUAL(true, tag->frameListMap()["WOAF"].isEmpty());
1651 }
1652 }
1653
1654 void testDuplicateTags()
1655 {
1656 ScopedFileCopy copy("duplicate_id3v2", ".mp3");
1657
1658 ByteVector audioStream;
1659 {
1660 MPEG::File f(copy.fileName().c_str());
1661 f.seek(f.ID3v2Tag()->header()->completeTagSize());
1662 audioStream = f.readBlock(2089);
1663
1664 // duplicate_id3v2.mp3 has duplicate ID3v2 tags.
1665 // Sample rate will be 32000 if we can't skip the second tag.
1666
1667 CPPUNIT_ASSERT(f.hasID3v2Tag());
1668 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(8049), f.ID3v2Tag()->header()->completeTagSize());
1669
1670 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
1671
1672 f.ID3v2Tag()->setArtist("Artist A");
1673 f.save(MPEG::File::ID3v2, File::StripOthers);
1674 }
1675 {
1676 MPEG::File f(copy.fileName().c_str());
1677 CPPUNIT_ASSERT(f.hasID3v2Tag());
1678 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(3594), f.length());
1679 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1505), f.ID3v2Tag()->header()->completeTagSize());
1680 CPPUNIT_ASSERT_EQUAL(String("Artist A"), f.ID3v2Tag()->artist());
1681 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
1682
1683 f.seek(f.ID3v2Tag()->header()->completeTagSize());
1684 CPPUNIT_ASSERT_EQUAL(f.readBlock(2089), audioStream);
1685
1686 }
1687 }
1688
1689 void testParseTOCFrameWithManyChildren()
1690 {
1691 MPEG::File f(TEST_FILE_PATH_C("toc_many_children.mp3"));
1692 CPPUNIT_ASSERT(f.isValid());
1693
1694 ID3v2::Tag *tag = f.ID3v2Tag();
1695 CPPUNIT_ASSERT_EQUAL(130U, tag->frameList().size());
1696 int i = 0;
1697 for(const auto &frame : std::as_const(tag->frameList())) {
1698 if(i > 0) {
1699 CPPUNIT_ASSERT_EQUAL(ByteVector("CHAP"), frame->frameID());
1700 auto chapFrame = dynamic_cast<const ID3v2::ChapterFrame *>(frame);
1701 CPPUNIT_ASSERT_EQUAL(ByteVector("chapter") +
1702 ByteVector(String::number(i - 1).toCString()),
1703 chapFrame->elementID());
1704 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(100 * i),
1705 chapFrame->startTime());
1706 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(100 * i),
1707 chapFrame->endTime());
1708 const ID3v2::FrameList &embeddedFrames = chapFrame->embeddedFrameList();
1709 CPPUNIT_ASSERT_EQUAL(1U, embeddedFrames.size());
1710 auto tit2Frame = dynamic_cast<const ID3v2::TextIdentificationFrame *>(
1711 embeddedFrames.front());
1712 CPPUNIT_ASSERT(tit2Frame);
1713 CPPUNIT_ASSERT_EQUAL(String("Marker ") + String::number(i),
1714 tit2Frame->fieldList().front());
1715 }
1716 else {
1717 CPPUNIT_ASSERT_EQUAL(ByteVector("CTOC"), frame->frameID());
1718 auto ctocFrame = dynamic_cast<const ID3v2::TableOfContentsFrame *>(frame);
1719 CPPUNIT_ASSERT_EQUAL(ByteVector("toc"), ctocFrame->elementID());
1720 CPPUNIT_ASSERT(!ctocFrame->isTopLevel());
1721 CPPUNIT_ASSERT(!ctocFrame->isOrdered());
1722 CPPUNIT_ASSERT_EQUAL(129U, ctocFrame->entryCount());
1723 const ID3v2::FrameList &embeddedFrames = ctocFrame->embeddedFrameList();
1724 CPPUNIT_ASSERT_EQUAL(1U, embeddedFrames.size());
1725 auto tit2Frame = dynamic_cast<const ID3v2::TextIdentificationFrame *>(
1726 embeddedFrames.front());
1727 CPPUNIT_ASSERT(tit2Frame);
1728 CPPUNIT_ASSERT_EQUAL(StringList("toplevel toc"), tit2Frame->fieldList());
1729 }
1730 ++i;
1731 }
1732
1733 CPPUNIT_ASSERT(!ID3v2::ChapterFrame::findByElementID(tag, "chap2"));
1734 CPPUNIT_ASSERT(ID3v2::ChapterFrame::findByElementID(tag, "chapter2"));
1735
1736 CPPUNIT_ASSERT(!ID3v2::TableOfContentsFrame::findTopLevel(tag));
1737 CPPUNIT_ASSERT(!ID3v2::TableOfContentsFrame::findByElementID(tag, "ctoc"));
1738 CPPUNIT_ASSERT(ID3v2::TableOfContentsFrame::findByElementID(tag, "toc"));
1739
1740 auto tocFrame = ID3v2::TableOfContentsFrame::findByElementID(tag, "toc");
1741 CPPUNIT_ASSERT_EQUAL(1U, tocFrame->embeddedFrameList().size());
1742 tocFrame->removeEmbeddedFrames("TIT2");
1743 CPPUNIT_ASSERT(tocFrame->embeddedFrameList().isEmpty());
1744 }
1745
1746 void testInvalidID3v2Version()
1747 {
1748 ID3v2::Header invalidVersionHeader(ByteVector("ID3"
1749 "\xFF\x00"
1750 "\x05"
1751 "\x14\x4F\x00\x32", 10));
1752 CPPUNIT_ASSERT_EQUAL(invalidVersionHeader.tagSize(), 0U);
1753
1754 ID3v2::Header invalidRevisionHeader(ByteVector("ID3"
1755 "\x04\xFF"
1756 "\x05"
1757 "\x14\x4F\x00\x32", 10));
1758 CPPUNIT_ASSERT_EQUAL(invalidRevisionHeader.tagSize(), 0U);
1759 }
1760
1761 };
1762
1763 CPPUNIT_TEST_SUITE_REGISTRATION(TestID3v2);