Debian Codegraph @

tests/test_flac.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2009 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 "tpropertymap.h"
31 #include "tbytevectorstream.h"
32 #include "tag.h"
33 #include "flacfile.h"
34 #include "xiphcomment.h"
35 #include "id3v1tag.h"
36 #include "id3v2tag.h"
37 #include "plainfile.h"
38 #include <cppunit/extensions/HelperMacros.h>
39 #include "utils.h"
40
41 using namespace std;
42 using namespace TagLib;
43
44 class TestFLAC : public CppUnit::TestFixture
45 {
46 CPPUNIT_TEST_SUITE(TestFLAC);
47 CPPUNIT_TEST(testSignature);
48 CPPUNIT_TEST(testMultipleCommentBlocks);
49 CPPUNIT_TEST(testReadPicture);
50 CPPUNIT_TEST(testAddPicture);
51 CPPUNIT_TEST(testReplacePicture);
52 CPPUNIT_TEST(testRemoveAllPictures);
53 CPPUNIT_TEST(testRepeatedSave1);
54 CPPUNIT_TEST(testRepeatedSave2);
55 CPPUNIT_TEST(testRepeatedSave3);
56 CPPUNIT_TEST(testSaveMultipleValues);
57 CPPUNIT_TEST(testDict);
58 CPPUNIT_TEST(testProperties);
59 CPPUNIT_TEST(testInvalid);
60 CPPUNIT_TEST(testAudioProperties);
61 CPPUNIT_TEST(testZeroSizedPadding1);
62 CPPUNIT_TEST(testZeroSizedPadding2);
63 CPPUNIT_TEST(testShrinkPadding);
64 CPPUNIT_TEST(testSaveID3v1);
65 CPPUNIT_TEST(testUpdateID3v2);
66 CPPUNIT_TEST(testEmptyID3v2);
67 CPPUNIT_TEST(testStripTags);
68 CPPUNIT_TEST(testRemoveXiphField);
69 CPPUNIT_TEST(testEmptySeekTable);
70 CPPUNIT_TEST(testPictureStoredAfterComment);
71 CPPUNIT_TEST(testReadiXMLDirect);
72 CPPUNIT_TEST(testReadiXMLRiffWrapped);
73 CPPUNIT_TEST(testReadBEXTDirect);
74 CPPUNIT_TEST(testReadBEXTRiffWrapped);
75 CPPUNIT_TEST(testWriteiXMLAndBEXT);
76 CPPUNIT_TEST(testWriteEmptyClearsiXMLAndBEXT);
77 CPPUNIT_TEST(testRoundTripPreservesUnknownApplicationBlock);
78 CPPUNIT_TEST_SUITE_END();
79
80 public:
81
82 void testSignature()
83 {
84 FLAC::File f(TEST_FILE_PATH_C("no-tags.flac"));
85 CPPUNIT_ASSERT_EQUAL(ByteVector("a1b141f766e9849ac3db1030a20a3c77"), f.audioProperties()->signature().toHex());
86 }
87
88 void testMultipleCommentBlocks()
89 {
90 ScopedFileCopy copy("multiple-vc", ".flac");
91 string newname = copy.fileName();
92
93 {
94 FLAC::File f(newname.c_str());
95 CPPUNIT_ASSERT_EQUAL(String("Artist 1"), f.tag()->artist());
96 f.tag()->setArtist("The Artist");
97 f.save();
98 }
99 {
100 FLAC::File f(newname.c_str());
101 CPPUNIT_ASSERT_EQUAL(String("The Artist"), f.tag()->artist());
102 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(69), f.find("Artist"));
103 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(-1), f.find("Artist", 70));
104 }
105 }
106
107 void testReadPicture()
108 {
109 ScopedFileCopy copy("silence-44-s", ".flac");
110 string newname = copy.fileName();
111
112 FLAC::File f(newname.c_str());
113 List<FLAC::Picture *> lst = f.pictureList();
114 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size());
115
116 FLAC::Picture *pic = lst.front();
117 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::FrontCover, pic->type());
118 CPPUNIT_ASSERT_EQUAL(1, pic->width());
119 CPPUNIT_ASSERT_EQUAL(1, pic->height());
120 CPPUNIT_ASSERT_EQUAL(24, pic->colorDepth());
121 CPPUNIT_ASSERT_EQUAL(0, pic->numColors());
122 CPPUNIT_ASSERT_EQUAL(String("image/png"), pic->mimeType());
123 CPPUNIT_ASSERT_EQUAL(String("A pixel."), pic->description());
124 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(150), pic->data().size());
125 }
126
127 void testAddPicture()
128 {
129 ScopedFileCopy copy("silence-44-s", ".flac");
130 string newname = copy.fileName();
131
132 {
133 FLAC::File f(newname.c_str());
134 List<FLAC::Picture *> lst = f.pictureList();
135 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size());
136
137 auto newpic = new FLAC::Picture();
138 newpic->setType(FLAC::Picture::BackCover);
139 newpic->setWidth(5);
140 newpic->setHeight(6);
141 newpic->setColorDepth(16);
142 newpic->setNumColors(7);
143 newpic->setMimeType("image/jpeg");
144 newpic->setDescription("new image");
145 newpic->setData("JPEG data");
146 f.addPicture(newpic);
147 f.save();
148 }
149 {
150 FLAC::File f(newname.c_str());
151 List<FLAC::Picture *> lst = f.pictureList();
152 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), lst.size());
153
154 FLAC::Picture *pic = lst[0];
155 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::FrontCover, pic->type());
156 CPPUNIT_ASSERT_EQUAL(1, pic->width());
157 CPPUNIT_ASSERT_EQUAL(1, pic->height());
158 CPPUNIT_ASSERT_EQUAL(24, pic->colorDepth());
159 CPPUNIT_ASSERT_EQUAL(0, pic->numColors());
160 CPPUNIT_ASSERT_EQUAL(String("image/png"), pic->mimeType());
161 CPPUNIT_ASSERT_EQUAL(String("A pixel."), pic->description());
162 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(150), pic->data().size());
163
164 pic = lst[1];
165 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::BackCover, pic->type());
166 CPPUNIT_ASSERT_EQUAL(5, pic->width());
167 CPPUNIT_ASSERT_EQUAL(6, pic->height());
168 CPPUNIT_ASSERT_EQUAL(16, pic->colorDepth());
169 CPPUNIT_ASSERT_EQUAL(7, pic->numColors());
170 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), pic->mimeType());
171 CPPUNIT_ASSERT_EQUAL(String("new image"), pic->description());
172 CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG data"), pic->data());
173 }
174 }
175
176 void testReplacePicture()
177 {
178 ScopedFileCopy copy("silence-44-s", ".flac");
179 string newname = copy.fileName();
180
181 {
182 FLAC::File f(newname.c_str());
183 List<FLAC::Picture *> lst = f.pictureList();
184 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size());
185
186 auto newpic = new FLAC::Picture();
187 newpic->setType(FLAC::Picture::BackCover);
188 newpic->setWidth(5);
189 newpic->setHeight(6);
190 newpic->setColorDepth(16);
191 newpic->setNumColors(7);
192 newpic->setMimeType("image/jpeg");
193 newpic->setDescription("new image");
194 newpic->setData("JPEG data");
195 f.removePictures();
196 f.addPicture(newpic);
197 f.save();
198 }
199 {
200 FLAC::File f(newname.c_str());
201 List<FLAC::Picture *> lst = f.pictureList();
202 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size());
203
204 FLAC::Picture *pic = lst[0];
205 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::BackCover, pic->type());
206 CPPUNIT_ASSERT_EQUAL(5, pic->width());
207 CPPUNIT_ASSERT_EQUAL(6, pic->height());
208 CPPUNIT_ASSERT_EQUAL(16, pic->colorDepth());
209 CPPUNIT_ASSERT_EQUAL(7, pic->numColors());
210 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"), pic->mimeType());
211 CPPUNIT_ASSERT_EQUAL(String("new image"), pic->description());
212 CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG data"), pic->data());
213 }
214 }
215
216 void testRemoveAllPictures()
217 {
218 ScopedFileCopy copy("silence-44-s", ".flac");
219 string newname = copy.fileName();
220
221 {
222 FLAC::File f(newname.c_str());
223 List<FLAC::Picture *> lst = f.pictureList();
224 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), lst.size());
225
226 f.removePictures();
227 f.save();
228 }
229 {
230 FLAC::File f(newname.c_str());
231 List<FLAC::Picture *> lst = f.pictureList();
232 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), lst.size());
233 }
234 }
235
236 void testRepeatedSave1()
237 {
238 ScopedFileCopy copy("silence-44-s", ".flac");
239 string newname = copy.fileName();
240
241 {
242 FLAC::File f(newname.c_str());
243 CPPUNIT_ASSERT_EQUAL(String("Silence"), f.tag()->title());
244 f.tag()->setTitle("NEW TITLE");
245 f.save();
246 CPPUNIT_ASSERT_EQUAL(String("NEW TITLE"), f.tag()->title());
247 f.tag()->setTitle("NEW TITLE 2");
248 f.save();
249 CPPUNIT_ASSERT_EQUAL(String("NEW TITLE 2"), f.tag()->title());
250 }
251 {
252 FLAC::File f(newname.c_str());
253 CPPUNIT_ASSERT_EQUAL(String("NEW TITLE 2"), f.tag()->title());
254 }
255 }
256
257 void testRepeatedSave2()
258 {
259 ScopedFileCopy copy("no-tags", ".flac");
260
261 FLAC::File f(copy.fileName().c_str());
262 f.ID3v2Tag(true)->setTitle("0123456789");
263 f.save();
264 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(5735), f.length());
265 f.save();
266 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(5735), f.length());
267 CPPUNIT_ASSERT(f.find("fLaC") >= 0);
268 }
269
270 void testRepeatedSave3()
271 {
272 ScopedFileCopy copy("no-tags", ".flac");
273
274 FLAC::File f(copy.fileName().c_str());
275 f.xiphComment()->setTitle(longText(8 * 1024));
276 f.save();
277 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(12862), f.length());
278 f.save();
279 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(12862), f.length());
280 }
281
282 void testSaveMultipleValues()
283 {
284 ScopedFileCopy copy("silence-44-s", ".flac");
285 string newname = copy.fileName();
286
287 {
288 FLAC::File f(newname.c_str());
289 f.xiphComment(true)->addField("ARTIST", "artist 1", true);
290 f.xiphComment(true)->addField("ARTIST", "artist 2", false);
291 f.save();
292 }
293 {
294 FLAC::File f(newname.c_str());
295 Ogg::FieldListMap m = f.xiphComment()->fieldListMap();
296 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), m["ARTIST"].size());
297 CPPUNIT_ASSERT_EQUAL(String("artist 1"), m["ARTIST"][0]);
298 CPPUNIT_ASSERT_EQUAL(String("artist 2"), m["ARTIST"][1]);
299 }
300 }
301
302 void testDict()
303 {
304 // test unicode & multiple values with dict interface
305 ScopedFileCopy copy("silence-44-s", ".flac");
306 string newname = copy.fileName();
307
308 {
309 FLAC::File f(newname.c_str());
310 PropertyMap dict;
311 dict["ARTIST"].append("artøst 1");
312 dict["ARTIST"].append("artöst 2");
313 f.setProperties(dict);
314 f.save();
315 }
316 {
317 FLAC::File f(newname.c_str());
318 PropertyMap dict = f.properties();
319 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(2), dict["ARTIST"].size());
320 CPPUNIT_ASSERT_EQUAL(String("artøst 1"), dict["ARTIST"][0]);
321 CPPUNIT_ASSERT_EQUAL(String("artöst 2"), dict["ARTIST"][1]);
322 }
323 }
324
325 void testProperties()
326 {
327 PropertyMap tags;
328 tags["ALBUM"] = StringList("Album");
329 tags["ALBUMARTIST"] = StringList("Album Artist");
330 tags["ALBUMARTISTSORT"] = StringList("Album Artist Sort");
331 tags["ALBUMSORT"] = StringList("Album Sort");
332 tags["ARTIST"] = StringList("Artist");
333 tags["ARTISTS"] = StringList("Artists");
334 tags["ARTISTSORT"] = StringList("Artist Sort");
335 tags["ASIN"] = StringList("ASIN");
336 tags["BARCODE"] = StringList("Barcode");
337 tags["CATALOGNUMBER"] = StringList("Catalog Number 1").append("Catalog Number 2");
338 tags["COMMENT"] = StringList("Comment");
339 tags["DATE"] = StringList("2021-01-10");
340 tags["DISCNUMBER"] = StringList("3");
341 tags["DISCTOTAL"] = StringList("5");
342 tags["GENRE"] = StringList("Genre");
343 tags["ISRC"] = StringList("UKAAA0500001");
344 tags["LABEL"] = StringList("Label 1").append("Label 2");
345 tags["MEDIA"] = StringList("Media");
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["ORIGINALDATE"] = StringList("2021-01-09");
353 tags["RELEASECOUNTRY"] = StringList("Release Country");
354 tags["RELEASESTATUS"] = StringList("Release Status");
355 tags["RELEASETYPE"] = StringList("Release Type");
356 tags["SCRIPT"] = StringList("Script");
357 tags["TITLE"] = StringList("Title");
358 tags["TRACKNUMBER"] = StringList("2");
359 tags["TRACKTOTAL"] = StringList("4");
360
361 ScopedFileCopy copy("no-tags", ".flac");
362 {
363 FLAC::File f(copy.fileName().c_str());
364 PropertyMap properties = f.properties();
365 CPPUNIT_ASSERT(properties.isEmpty());
366 f.setProperties(tags);
367 f.save();
368 }
369 {
370 const FLAC::File f(copy.fileName().c_str());
371 PropertyMap properties = f.properties();
372 if (tags != properties) {
373 CPPUNIT_ASSERT_EQUAL(tags.toString(), properties.toString());
374 }
375 CPPUNIT_ASSERT(tags == properties);
376 }
377 }
378
379 void testInvalid()
380 {
381 ScopedFileCopy copy("silence-44-s", ".flac");
382 PropertyMap map;
383 map[L"H\x00c4\x00d6"] = String("bla");
384 FLAC::File f(copy.fileName().c_str());
385 PropertyMap invalid = f.setProperties(map);
386 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(1), invalid.size());
387 CPPUNIT_ASSERT_EQUAL(static_cast<unsigned int>(0), f.properties().size());
388 }
389
390 void testAudioProperties()
391 {
392 FLAC::File f(TEST_FILE_PATH_C("sinewave.flac"));
393 CPPUNIT_ASSERT(f.audioProperties());
394 CPPUNIT_ASSERT_EQUAL(3, f.audioProperties()->lengthInSeconds());
395 CPPUNIT_ASSERT_EQUAL(3550, f.audioProperties()->lengthInMilliseconds());
396 CPPUNIT_ASSERT_EQUAL(145, f.audioProperties()->bitrate());
397 CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
398 CPPUNIT_ASSERT_EQUAL(2, f.audioProperties()->channels());
399 CPPUNIT_ASSERT_EQUAL(16, f.audioProperties()->bitsPerSample());
400 CPPUNIT_ASSERT_EQUAL(156556ULL, f.audioProperties()->sampleFrames());
401 CPPUNIT_ASSERT_EQUAL(
402 ByteVector("\xcf\xe3\xd9\xda\xba\xde\xab\x2c\xbf\x2c\xa2\x35\x27\x4b\x7f\x76"),
403 f.audioProperties()->signature());
404 }
405
406 void testZeroSizedPadding1()
407 {
408 ScopedFileCopy copy("zero-sized-padding", ".flac");
409
410 FLAC::File f(copy.fileName().c_str());
411 CPPUNIT_ASSERT(f.isValid());
412 }
413
414 void testZeroSizedPadding2()
415 {
416 ScopedFileCopy copy("silence-44-s", ".flac");
417
418 {
419 FLAC::File f(copy.fileName().c_str());
420 f.xiphComment()->setTitle("ABC");
421 f.save();
422 }
423 {
424 FLAC::File f(copy.fileName().c_str());
425 f.xiphComment()->setTitle(std::string(3067, 'X').c_str());
426 f.save();
427 }
428 {
429 FLAC::File f(copy.fileName().c_str());
430 CPPUNIT_ASSERT(f.isValid());
431 }
432 }
433
434 void testShrinkPadding()
435 {
436 ScopedFileCopy copy("no-tags", ".flac");
437
438 {
439 FLAC::File f(copy.fileName().c_str());
440 f.xiphComment()->setTitle(longText(128 * 1024));
441 f.save();
442 CPPUNIT_ASSERT(f.length() > 128 * 1024);
443 }
444 {
445 FLAC::File f(copy.fileName().c_str());
446 f.xiphComment()->setTitle("0123456789");
447 f.save();
448 CPPUNIT_ASSERT(f.length() < 8 * 1024);
449 }
450 }
451
452 void testSaveID3v1()
453 {
454 ScopedFileCopy copy("no-tags", ".flac");
455 FLAC::File f(copy.fileName().c_str());
456 CPPUNIT_ASSERT(!f.hasID3v1Tag());
457 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4692), f.length());
458
459 f.seek(0x0100);
460 ByteVector audioStream = f.readBlock(4436);
461
462 f.ID3v1Tag(true)->setTitle("01234 56789 ABCDE FGHIJ");
463 f.save();
464 CPPUNIT_ASSERT(f.hasID3v1Tag());
465 CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(4820), f.length());
466
467 f.seek(0x0100);
468 CPPUNIT_ASSERT_EQUAL(audioStream, f.readBlock(4436));
469 }
470
471 void testUpdateID3v2()
472 {
473 ScopedFileCopy copy("no-tags", ".flac");
474
475 {
476 FLAC::File f(copy.fileName().c_str());
477 f.ID3v2Tag(true)->setTitle("0123456789");
478 f.save();
479 }
480 {
481 FLAC::File f(copy.fileName().c_str());
482 f.ID3v2Tag()->setTitle("ABCDEFGHIJ");
483 f.save();
484 }
485 {
486 FLAC::File f(copy.fileName().c_str());
487 CPPUNIT_ASSERT_EQUAL(String("ABCDEFGHIJ"), f.ID3v2Tag()->title());
488 }
489 }
490
491 void testEmptyID3v2()
492 {
493 ScopedFileCopy copy("no-tags", ".flac");
494
495 {
496 FLAC::File f(copy.fileName().c_str());
497 f.ID3v2Tag(true);
498 f.save();
499 }
500 {
501 FLAC::File f(copy.fileName().c_str());
502 CPPUNIT_ASSERT(!f.hasID3v2Tag());
503 }
504 }
505
506 void testStripTags()
507 {
508 ScopedFileCopy copy("silence-44-s", ".flac");
509
510 {
511 FLAC::File f(copy.fileName().c_str());
512 f.xiphComment(true)->setTitle("XiphComment Title");
513 f.ID3v1Tag(true)->setTitle("ID3v1 Title");
514 f.ID3v2Tag(true)->setTitle("ID3v2 Title");
515 f.save();
516 }
517 {
518 FLAC::File f(copy.fileName().c_str());
519 CPPUNIT_ASSERT(f.hasXiphComment());
520 CPPUNIT_ASSERT(f.hasID3v1Tag());
521 CPPUNIT_ASSERT(f.hasID3v2Tag());
522 CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
523 CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
524 CPPUNIT_ASSERT_EQUAL(String("ID3v2 Title"), f.ID3v2Tag()->title());
525 f.strip(FLAC::File::ID3v2);
526 f.save();
527 }
528 {
529 FLAC::File f(copy.fileName().c_str());
530 CPPUNIT_ASSERT(f.hasXiphComment());
531 CPPUNIT_ASSERT(f.hasID3v1Tag());
532 CPPUNIT_ASSERT(!f.hasID3v2Tag());
533 CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
534 CPPUNIT_ASSERT_EQUAL(String("ID3v1 Title"), f.ID3v1Tag()->title());
535 f.strip(FLAC::File::ID3v1);
536 f.save();
537 }
538 {
539 FLAC::File f(copy.fileName().c_str());
540 CPPUNIT_ASSERT(f.hasXiphComment());
541 CPPUNIT_ASSERT(!f.hasID3v1Tag());
542 CPPUNIT_ASSERT(!f.hasID3v2Tag());
543 CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
544 f.strip(FLAC::File::XiphComment);
545 f.save();
546 }
547 {
548 FLAC::File f(copy.fileName().c_str());
549 CPPUNIT_ASSERT(f.hasXiphComment());
550 CPPUNIT_ASSERT(!f.hasID3v1Tag());
551 CPPUNIT_ASSERT(!f.hasID3v2Tag());
552 CPPUNIT_ASSERT(f.xiphComment()->isEmpty());
553 CPPUNIT_ASSERT_EQUAL(String("reference libFLAC 1.1.0 20030126"), f.xiphComment()->vendorID());
554 }
555 }
556
557 void testRemoveXiphField()
558 {
559 ScopedFileCopy copy("silence-44-s", ".flac");
560
561 {
562 FLAC::File f(copy.fileName().c_str());
563 f.xiphComment(true)->setTitle("XiphComment Title");
564 f.ID3v2Tag(true)->setTitle("ID3v2 Title");
565 f.save();
566 }
567 {
568 FLAC::File f(copy.fileName().c_str());
569 CPPUNIT_ASSERT_EQUAL(String("XiphComment Title"), f.xiphComment()->title());
570 f.xiphComment()->removeFields("TITLE");
571 f.save();
572 }
573 {
574 FLAC::File f(copy.fileName().c_str());
575 CPPUNIT_ASSERT_EQUAL(String(), f.xiphComment()->title());
576 }
577 }
578
579 void testEmptySeekTable()
580 {
581 ScopedFileCopy copy("empty-seektable", ".flac");
582 {
583 FLAC::File f(copy.fileName().c_str());
584 CPPUNIT_ASSERT(f.isValid());
585 f.xiphComment(true)->setTitle("XiphComment Title");
586 f.save();
587 }
588 {
589 FLAC::File f(copy.fileName().c_str());
590 CPPUNIT_ASSERT(f.isValid());
591 f.seek(42);
592 const ByteVector data = f.readBlock(4);
593 CPPUNIT_ASSERT_EQUAL(ByteVector("\x03\x00\x00\x00", 4), data);
594 }
595 }
596
597 void testPictureStoredAfterComment()
598 {
599 // Blank.png from https://commons.wikimedia.org/wiki/File:Blank.png
600 const unsigned char blankPngData[] = {
601 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
602 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02,
603 0x08, 0x06, 0x00, 0x00, 0x00, 0x9d, 0x74, 0x66, 0x1a, 0x00, 0x00, 0x00,
604 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
605 0x00, 0x04, 0x67, 0x41, 0x4d, 0x41, 0x00, 0x00, 0xb1, 0x8f, 0x0b, 0xfc,
606 0x61, 0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
607 0x0e, 0xc3, 0x00, 0x00, 0x0e, 0xc3, 0x01, 0xc7, 0x6f, 0xa8, 0x64, 0x00,
608 0x00, 0x00, 0x0c, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0xc0, 0x01,
609 0x18, 0x18, 0x00, 0x00, 0x1a, 0x00, 0x01, 0x82, 0x92, 0x4d, 0x60, 0x00,
610 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
611 };
612 const ByteVector picData(reinterpret_cast<const char *>(blankPngData),
613 sizeof(blankPngData));
614
615 ScopedFileCopy copy("no-tags", ".flac");
616 {
617 FLAC::File f(copy.fileName().c_str());
618 CPPUNIT_ASSERT(!f.hasID3v1Tag());
619 CPPUNIT_ASSERT(!f.hasID3v2Tag());
620 CPPUNIT_ASSERT(!f.hasXiphComment());
621 CPPUNIT_ASSERT(f.pictureList().isEmpty());
622
623 auto pic = new FLAC::Picture;
624 pic->setData(picData);
625 pic->setType(FLAC::Picture::FrontCover);
626 pic->setMimeType("image/png");
627 pic->setDescription("blank.png");
628 pic->setWidth(3);
629 pic->setHeight(2);
630 pic->setColorDepth(32);
631 pic->setNumColors(0);
632 f.addPicture(pic);
633 f.xiphComment(true)->setTitle("Title");
634 f.save();
635 }
636 {
637 FLAC::File f(copy.fileName().c_str());
638 CPPUNIT_ASSERT(!f.hasID3v1Tag());
639 CPPUNIT_ASSERT(!f.hasID3v2Tag());
640 CPPUNIT_ASSERT(f.hasXiphComment());
641 const List<FLAC::Picture *> pictures = f.pictureList();
642 CPPUNIT_ASSERT_EQUAL(1U, pictures.size());
643 CPPUNIT_ASSERT_EQUAL(picData, pictures[0]->data());
644 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::FrontCover, pictures[0]->type());
645 CPPUNIT_ASSERT_EQUAL(String("image/png"), pictures[0]->mimeType());
646 CPPUNIT_ASSERT_EQUAL(String("blank.png"), pictures[0]->description());
647 CPPUNIT_ASSERT_EQUAL(3, pictures[0]->width());
648 CPPUNIT_ASSERT_EQUAL(2, pictures[0]->height());
649 CPPUNIT_ASSERT_EQUAL(32, pictures[0]->colorDepth());
650 CPPUNIT_ASSERT_EQUAL(0, pictures[0]->numColors());
651 CPPUNIT_ASSERT_EQUAL(String("Title"), f.xiphComment(false)->title());
652 }
653
654 constexpr unsigned char expectedHeadData[] = {
655 'f', 'L', 'a', 'C', 0x00, 0x00, 0x00, 0x22, 0x12, 0x00, 0x12, 0x00,
656 0x00, 0x00, 0x0e, 0x00, 0x00, 0x10, 0x0a, 0xc4, 0x42, 0xf0, 0x00, 0x02,
657 0x7a, 0xc0, 0xa1, 0xb1, 0x41, 0xf7, 0x66, 0xe9, 0x84, 0x9a, 0xc3, 0xdb,
658 0x10, 0x30, 0xa2, 0x0a, 0x3c, 0x77, 0x04, 0x00, 0x00, 0x17, 0x00, 0x00,
659 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 'T', 'I',
660 'T', 'L', 'E', '=', 'T', 'i', 't', 'l', 'e', 0x06, 0x00, 0x00,
661 0xa9, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x09, 'i', 'm', 'a',
662 'g', 'e', '/', 'p', 'n', 'g', 0x00, 0x00, 0x00, 0x09, 'b', 'l',
663 'a', 'n', 'k', '.', 'p', 'n', 'g', 0x00, 0x00, 0x00, 0x03, 0x00,
664 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
665 0x00, 0x00, 0x77
666 };
667 ByteVector expectedData(reinterpret_cast<const char *>(expectedHeadData),
668 sizeof(expectedHeadData));
669 expectedData.append(picData);
670 const ByteVector fileData = PlainFile(copy.fileName().c_str()).readAll();
671 CPPUNIT_ASSERT(fileData.startsWith(expectedData));
672 }
673
674 // Build a 4-byte FLAC metadata-block header:
675 // <1 bit last><7 bit type><24 bit length, big-endian>.
676 static ByteVector flacBlockHeader(unsigned int payloadSize, int blockType, bool isLast)
677 {
678 ByteVector h = ByteVector::fromUInt(payloadSize);
679 h[0] = static_cast<char>(blockType | (isLast ? 0x80 : 0x00));
680 return h;
681 }
682
683 // Build the body of an APPLICATION/"riff"-wrapped RIFF chunk:
684 // [appID="riff"][FOURCC][LE size][data].
685 static ByteVector riffWrappedAppData(const ByteVector &fourcc, const ByteVector &data)
686 {
687 ByteVector body("riff", 4);
688 body.append(fourcc);
689 body.append(ByteVector::fromUInt(data.size(), false));
690 body.append(data);
691 return body;
692 }
693
694 // Build a minimal synthetic FLAC stream: "fLaC" + zero-init STREAMINFO +
695 // one APPLICATION block (which gets the last-block flag). Caller passes
696 // the full APPLICATION block payload starting with the 4-byte appID.
697 static ByteVector synthFlacWithApp(const ByteVector &appPayload)
698 {
699 ByteVector flac("fLaC", 4);
700 flac.append(flacBlockHeader(34, 0, false)); // STREAMINFO header
701 flac.append(ByteVector(34, '\0')); // STREAMINFO body
702 flac.append(flacBlockHeader(appPayload.size(), 2, true));
703 flac.append(appPayload);
704 return flac;
705 }
706
707 void testReadiXMLDirect()
708 {
709 const String xml("<BWFXML><IXML_VERSION>1.0</IXML_VERSION></BWFXML>");
710 ByteVector appPayload("iXML", 4);
711 appPayload.append(xml.data(String::UTF8));
712
713 ByteVector data = synthFlacWithApp(appPayload);
714 ByteVectorStream stream(data);
715 FLAC::File f(&stream, false);
716
717 CPPUNIT_ASSERT(f.isValid());
718 CPPUNIT_ASSERT(f.hasiXMLData());
719 CPPUNIT_ASSERT(!f.hasBEXTData());
720 CPPUNIT_ASSERT_EQUAL(xml, f.iXMLData());
721 }
722
723 void testReadiXMLRiffWrapped()
724 {
725 const String xml("<BWFXML><SCENE>1</SCENE></BWFXML>");
726 const ByteVector appPayload =
727 riffWrappedAppData("iXML", xml.data(String::UTF8));
728
729 ByteVector data = synthFlacWithApp(appPayload);
730 ByteVectorStream stream(data);
731 FLAC::File f(&stream, false);
732
733 CPPUNIT_ASSERT(f.isValid());
734 CPPUNIT_ASSERT(f.hasiXMLData());
735 CPPUNIT_ASSERT_EQUAL(xml, f.iXMLData());
736 }
737
738 void testReadBEXTDirect()
739 {
740 const ByteVector bext("test bext data");
741 ByteVector appPayload("bext", 4);
742 appPayload.append(bext);
743
744 ByteVector data = synthFlacWithApp(appPayload);
745 ByteVectorStream stream(data);
746 FLAC::File f(&stream, false);
747
748 CPPUNIT_ASSERT(f.isValid());
749 CPPUNIT_ASSERT(f.hasBEXTData());
750 CPPUNIT_ASSERT(!f.hasiXMLData());
751 CPPUNIT_ASSERT_EQUAL(bext, f.BEXTData());
752 }
753
754 void testReadBEXTRiffWrapped()
755 {
756 const ByteVector bext("test bext data");
757 const ByteVector appPayload = riffWrappedAppData("bext", bext);
758
759 ByteVector data = synthFlacWithApp(appPayload);
760 ByteVectorStream stream(data);
761 FLAC::File f(&stream, false);
762
763 CPPUNIT_ASSERT(f.isValid());
764 CPPUNIT_ASSERT(f.hasBEXTData());
765 CPPUNIT_ASSERT_EQUAL(bext, f.BEXTData());
766 }
767
768 void testWriteiXMLAndBEXT()
769 {
770 ScopedFileCopy copy("silence-44-s", ".flac");
771 const string newname = copy.fileName();
772
773 const String xml("<BWFXML><IXML_VERSION>1.6</IXML_VERSION></BWFXML>");
774 const ByteVector bext("bext payload bytes");
775
776 {
777 FLAC::File f(newname.c_str());
778 CPPUNIT_ASSERT(!f.hasiXMLData());
779 CPPUNIT_ASSERT(!f.hasBEXTData());
780 f.setiXMLData(xml);
781 f.setBEXTData(bext);
782 f.save();
783 }
784 {
785 FLAC::File f(newname.c_str());
786 CPPUNIT_ASSERT(f.hasiXMLData());
787 CPPUNIT_ASSERT(f.hasBEXTData());
788 CPPUNIT_ASSERT_EQUAL(xml, f.iXMLData());
789 CPPUNIT_ASSERT_EQUAL(bext, f.BEXTData());
790 }
791
792 // On-disk format check: written blocks must use the IANA-registered
793 // "riff" wrapper, not the direct "iXML"/"bext" application IDs.
794 const ByteVector fileBytes = PlainFile(newname.c_str()).readAll();
795 ByteVector expectediXMLApp("riff", 4);
796 expectediXMLApp.append("iXML");
797 expectediXMLApp.append(ByteVector::fromUInt(xml.data(String::UTF8).size(), false));
798 expectediXMLApp.append(xml.data(String::UTF8));
799 CPPUNIT_ASSERT(fileBytes.find(expectediXMLApp) >= 0);
800
801 ByteVector expectedBEXTApp("riff", 4);
802 expectedBEXTApp.append("bext");
803 expectedBEXTApp.append(ByteVector::fromUInt(bext.size(), false));
804 expectedBEXTApp.append(bext);
805 CPPUNIT_ASSERT(fileBytes.find(expectedBEXTApp) >= 0);
806 }
807
808 void testWriteEmptyClearsiXMLAndBEXT()
809 {
810 ScopedFileCopy copy("silence-44-s", ".flac");
811 const string newname = copy.fileName();
812
813 {
814 FLAC::File f(newname.c_str());
815 f.setiXMLData("<BWFXML/>");
816 f.setBEXTData(ByteVector("bext"));
817 f.save();
818 }
819 {
820 FLAC::File f(newname.c_str());
821 CPPUNIT_ASSERT(f.hasiXMLData());
822 CPPUNIT_ASSERT(f.hasBEXTData());
823 f.setiXMLData(String());
824 f.setBEXTData(ByteVector());
825 f.save();
826 }
827 {
828 FLAC::File f(newname.c_str());
829 CPPUNIT_ASSERT(!f.hasiXMLData());
830 CPPUNIT_ASSERT(!f.hasBEXTData());
831 CPPUNIT_ASSERT(f.iXMLData().isEmpty());
832 CPPUNIT_ASSERT(f.BEXTData().isEmpty());
833 }
834 }
835
836 void testRoundTripPreservesUnknownApplicationBlock()
837 {
838 // Source: silence-44-s with an extra APPLICATION/"SMED" block injected
839 // just before its existing VORBIS_COMMENT block. Goal: setting iXML and
840 // saving must not disturb the SMED block (it's an unrecognized app ID).
841 const ByteVector smedAppPayload("SMED", 4);
842 ByteVector smedExtra("opaque sequoia metadata payload");
843 ByteVector smedBlock = smedAppPayload;
844 smedBlock.append(smedExtra);
845
846 // Splice a fresh APPLICATION/SMED block into a synthetic FLAC. Use the
847 // file we just built as the input stream so we don't have to mutate a
848 // real FLAC's seek table / picture offsets.
849 ByteVector flac("fLaC", 4);
850 flac.append(flacBlockHeader(34, 0, false));
851 flac.append(ByteVector(34, '\0'));
852 flac.append(flacBlockHeader(smedBlock.size(), 2, true));
853 flac.append(smedBlock);
854
855 ByteVectorStream stream(flac);
856 {
857 FLAC::File f(&stream, false);
858 CPPUNIT_ASSERT(f.isValid());
859 CPPUNIT_ASSERT(!f.hasiXMLData());
860 f.setiXMLData("<BWFXML/>");
861 f.save();
862 }
863
864 // SMED block must still be present on disk after save.
865 ByteVector saved = *stream.data();
866 CPPUNIT_ASSERT(saved.find(smedAppPayload) >= 0);
867 CPPUNIT_ASSERT(saved.find(smedExtra) >= 0);
868
869 // And the iXML data must round-trip.
870 ByteVectorStream stream2(saved);
871 FLAC::File f2(&stream2, false);
872 CPPUNIT_ASSERT(f2.isValid());
873 CPPUNIT_ASSERT(f2.hasiXMLData());
874 CPPUNIT_ASSERT_EQUAL(String("<BWFXML/>"), f2.iXMLData());
875 }
876
877 };
878
879 CPPUNIT_TEST_SUITE_REGISTRATION(TestFLAC);