Debian Codegraph @

tests/test_complexproperties.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2023 by Urs Fleisch
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 "taglib_config.h"
27 #include "tbytevector.h"
28 #include "tvariant.h"
29 #include "tzlib.h"
30 #include "fileref.h"
31 #include "id3v1tag.h"
32 #include "id3v2tag.h"
33 #include "attachedpictureframe.h"
34 #include "generalencapsulatedobjectframe.h"
35 #ifdef TAGLIB_WITH_ASF
36 #include "asfpicture.h"
37 #include "asftag.h"
38 #endif
39 #ifdef TAGLIB_WITH_VORBIS
40 #include "flacpicture.h"
41 #include "flacfile.h"
42 #include "xiphcomment.h"
43 #endif
44 #ifdef TAGLIB_WITH_APE
45 #include "apetag.h"
46 #endif
47 #ifdef TAGLIB_WITH_MP4
48 #include "mp4tag.h"
49 #endif
50 #include <cppunit/extensions/HelperMacros.h>
51 #include "utils.h"
52
53 using namespace TagLib;
54
55 namespace {
56
57 const String GEOB_KEY("GENERALOBJECT");
58 const String PICTURE_KEY("PICTURE");
59
60 const VariantMap TEST_PICTURE {
61 {"data", ByteVector(
62 "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x48\x00\x48"
63 "\x00\x00\xff\xdb\x00\x43\x00\x03\x02\x02\x02\x02\x02\x03\x02\x02\x02\x03"
64 "\x03\x03\x03\x04\x06\x04\x04\x04\x04\x04\x08\x06\x06\x05\x06\x09\x08\x0a"
65 "\x0a\x09\x08\x09\x09\x0a\x0c\x0f\x0c\x0a\x0b\x0e\x0b\x09\x09\x0d\x11\x0d"
66 "\x0e\x0f\x10\x10\x11\x10\x0a\x0c\x12\x13\x12\x10\x13\x0f\x10\x10\x10\xff"
67 "\xc9\x00\x0b\x08\x00\x01\x00\x01\x01\x01\x11\x00\xff\xcc\x00\x06\x00\x10"
68 "\x10\x05\xff\xda\x00\x08\x01\x01\x00\x00\x3f\x00\xd2\xcf\x20\xff\xd9",
69 125)},
70 {"mimeType", "image/jpeg"},
71 {"description", "Embedded cover"},
72 {"pictureType", "Front Cover"}
73 };
74
75 } // namespace
76
77 class TestComplexProperties : public CppUnit::TestFixture
78 {
79 CPPUNIT_TEST_SUITE(TestComplexProperties);
80 CPPUNIT_TEST(testReadMp3Picture);
81 CPPUNIT_TEST(testSetGetId3Geob);
82 CPPUNIT_TEST(testSetGetId3Picture);
83 CPPUNIT_TEST(testNonExistent);
84 #ifdef TAGLIB_WITH_MP4
85 CPPUNIT_TEST(testReadM4aPicture);
86 CPPUNIT_TEST(testSetGetMp4Picture);
87 #endif
88 #ifdef TAGLIB_WITH_VORBIS
89 CPPUNIT_TEST(testReadOggPicture);
90 CPPUNIT_TEST(testReadWriteFlacPicture);
91 CPPUNIT_TEST(testReadWriteMultipleProperties);
92 CPPUNIT_TEST(testSetGetXiphPicture);
93 #endif
94 #ifdef TAGLIB_WITH_APE
95 CPPUNIT_TEST(testSetGetApePicture);
96 #endif
97 #ifdef TAGLIB_WITH_ASF
98 CPPUNIT_TEST(testSetGetAsfPicture);
99 #endif
100 CPPUNIT_TEST_SUITE_END();
101
102 public:
103 void testReadMp3Picture()
104 {
105 if(zlib::isAvailable()) {
106 FileRef f(TEST_FILE_PATH_C("compressed_id3_frame.mp3"), false);
107 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY),
108 f.complexPropertyKeys());
109 auto pictures = f.complexProperties(PICTURE_KEY);
110 CPPUNIT_ASSERT_EQUAL(1U, pictures.size());
111 auto picture = pictures.front();
112 CPPUNIT_ASSERT_EQUAL(86414U,
113 picture.value("data").value<ByteVector>().size());
114 CPPUNIT_ASSERT_EQUAL(String(""),
115 picture.value("description").value<String>());
116 CPPUNIT_ASSERT_EQUAL(String("image/bmp"),
117 picture.value("mimeType").value<String>());
118 CPPUNIT_ASSERT_EQUAL(String("Other"),
119 picture.value("pictureType").value<String>());
120 }
121 }
122
123 #ifdef TAGLIB_WITH_MP4
124 void testReadM4aPicture()
125 {
126 const ByteVector expectedData1(
127 "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a\x00\x00\x00\x0d\x49\x48\x44\x52\x00\x00"
128 "\x00\x02\x00\x00\x00\x02\x08\x02\x00\x00\x00\xfd\xd4\x9a\x73\x00\x00\x00"
129 "\x16\x49\x44\x41\x54\x78\x9c\x63\x7c\x9f\xca\xc0\xc0\xc0\xc0\xc4\xc0\xc0"
130 "\xc0\xc0\xc0\x00\x00\x11\x09\x01\x58\xab\x88\xdb\x6f\x00\x00\x00\x00\x49"
131 "\x45\x4e\x44\xae\x42\x60\x82", 79);
132 const ByteVector expectedData2(
133 "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x00\x64\x00\x64"
134 "\x00\x00\xff\xdb\x00\x43\x00\x09\x06\x07\x08\x07\x06\x09\x08\x08\x08\x0a"
135 "\x0a\x09\x0b\x0e\x17\x0f\x0e\x0d\x0d\x0e\x1c\x14\x15\x11\x17\x22\x1e\x23"
136 "\x23\x21\x1e\x20\x20\x25\x2a\x35\x2d\x25\x27\x32\x28\x20\x20\x2e\x3f\x2f"
137 "\x32\x37\x39\x3c\x3c\x3c\x24\x2d\x42\x46\x41\x3a\x46\x35\x3b\x3c\x39\xff"
138 "\xdb\x00\x43\x01\x0a\x0a\x0a\x0e\x0c\x0e\x1b\x0f\x0f\x1b\x39\x26\x20\x26"
139 "\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39"
140 "\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39"
141 "\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\x39\xff\xc0\x00\x11"
142 "\x08\x00\x02\x00\x02\x03\x01\x22\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00"
143 "\x15\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
144 "\x00\x07\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
145 "\x00\x00\x00\x00\x00\x00\xff\xc4\x00\x15\x01\x01\x01\x00\x00\x00\x00\x00"
146 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x06\xff\xc4\x00\x14\x11\x01\x00"
147 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xda\x00"
148 "\x0c\x03\x01\x00\x02\x11\x03\x11\x00\x3f\x00\x8d\x80\xb8\x19\xff\xd9", 287);
149
150 FileRef f(TEST_FILE_PATH_C("has-tags.m4a"), false);
151 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY),
152 f.complexPropertyKeys());
153 auto pictures = f.complexProperties(PICTURE_KEY);
154 CPPUNIT_ASSERT_EQUAL(2U, pictures.size());
155 auto picture = pictures.front();
156 CPPUNIT_ASSERT_EQUAL(expectedData1,
157 picture.value("data").value<ByteVector>());
158 CPPUNIT_ASSERT_EQUAL(String("image/png"),
159 picture.value("mimeType").value<String>());
160 picture = pictures.back();
161 CPPUNIT_ASSERT_EQUAL(expectedData2,
162 picture.value("data").value<ByteVector>());
163 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"),
164 picture.value("mimeType").value<String>());
165 }
166 #endif
167
168 #ifdef TAGLIB_WITH_VORBIS
169 void testReadOggPicture()
170 {
171 FileRef f(TEST_FILE_PATH_C("lowercase-fields.ogg"), false);
172 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY),
173 f.complexPropertyKeys());
174 auto pictures = f.complexProperties(PICTURE_KEY);
175 CPPUNIT_ASSERT_EQUAL(1U, pictures.size());
176 auto picture = pictures.front();
177 CPPUNIT_ASSERT_EQUAL(ByteVector("JPEG data"),
178 picture.value("data").value<ByteVector>());
179 CPPUNIT_ASSERT_EQUAL(String("image/jpeg"),
180 picture.value("mimeType").value<String>());
181 CPPUNIT_ASSERT_EQUAL(String("Back Cover"),
182 picture.value("pictureType").value<String>());
183 CPPUNIT_ASSERT_EQUAL(String("new image"),
184 picture.value("description").value<String>());
185 CPPUNIT_ASSERT_EQUAL(16, picture.value("colorDepth").value<int>());
186 CPPUNIT_ASSERT_EQUAL(7, picture.value("numColors").value<int>());
187 CPPUNIT_ASSERT_EQUAL(5, picture.value("width").value<int>());
188 CPPUNIT_ASSERT_EQUAL(6, picture.value("height").value<int>());
189 }
190
191 void testReadWriteFlacPicture()
192 {
193 VariantMap picture(TEST_PICTURE);
194 picture.insert("colorDepth", 8);
195 picture.insert("numColors", 1);
196 picture.insert("width", 1);
197 picture.insert("height", 1);
198
199 ScopedFileCopy copy("no-tags", ".flac");
200
201 {
202 FLAC::File f(copy.fileName().c_str(), false);
203 CPPUNIT_ASSERT(f.complexPropertyKeys().isEmpty());
204 CPPUNIT_ASSERT(f.pictureList().isEmpty());
205 CPPUNIT_ASSERT(f.setComplexProperties(PICTURE_KEY, {picture}));
206 f.save();
207 }
208 {
209 FLAC::File f(copy.fileName().c_str(), false);
210 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY), f.complexPropertyKeys());
211 CPPUNIT_ASSERT_EQUAL(picture, f.complexProperties(PICTURE_KEY).front());
212 auto flacPictures = f.pictureList();
213 CPPUNIT_ASSERT_EQUAL(1U, flacPictures.size());
214 auto flacPicture = flacPictures.front();
215 CPPUNIT_ASSERT_EQUAL(picture.value("data").value<ByteVector>(),
216 flacPicture->data());
217 CPPUNIT_ASSERT_EQUAL(picture.value("mimeType").value<String>(),
218 flacPicture->mimeType());
219 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::FrontCover, flacPicture->type());
220 CPPUNIT_ASSERT_EQUAL(picture.value("description").value<String>(),
221 flacPicture->description());
222 CPPUNIT_ASSERT_EQUAL(picture.value("colorDepth").value<int>(),
223 flacPicture->colorDepth());
224 CPPUNIT_ASSERT_EQUAL(picture.value("numColors").value<int>(),
225 flacPicture->numColors());
226 CPPUNIT_ASSERT_EQUAL(picture.value("width").value<int>(),
227 flacPicture->width());
228 CPPUNIT_ASSERT_EQUAL(picture.value("height").value<int>(),
229 flacPicture->height());
230
231 CPPUNIT_ASSERT(f.setComplexProperties(PICTURE_KEY, {}));
232 f.save();
233 }
234 {
235 FLAC::File f(copy.fileName().c_str(), false);
236 CPPUNIT_ASSERT(f.complexPropertyKeys().isEmpty());
237 CPPUNIT_ASSERT(f.pictureList().isEmpty());
238 }
239 }
240 #endif
241
242 void testReadWriteMultipleProperties()
243 {
244 const VariantMap picture2 {
245 {"data", ByteVector("PNG data")},
246 {"mimeType", "image/png"},
247 {"description", ""},
248 {"pictureType", "Back Cover"}
249 };
250 const VariantMap geob1 {
251 {"data", ByteVector("First")},
252 {"mimeType", "text/plain"},
253 {"description", "Object 1"},
254 {"fileName", "test1.txt"}
255 };
256 const VariantMap geob2 {
257 {"data", ByteVector("Second")},
258 {"mimeType", "text/plain"},
259 {"description", "Object 2"},
260 {"fileName", "test2.txt"}
261 };
262
263 ScopedFileCopy copy("xing", ".mp3");
264
265 {
266 FileRef f(copy.fileName().c_str(), false);
267 CPPUNIT_ASSERT(f.complexPropertyKeys().isEmpty());
268 f.setComplexProperties(PICTURE_KEY, {TEST_PICTURE, picture2});
269 f.setComplexProperties(GEOB_KEY, {geob1, geob2});
270 f.save();
271 }
272 {
273 FileRef f(copy.fileName().c_str(), false);
274 CPPUNIT_ASSERT_EQUAL(StringList({PICTURE_KEY, GEOB_KEY}),
275 f.complexPropertyKeys());
276 CPPUNIT_ASSERT(List<VariantMap>({TEST_PICTURE, picture2}) ==
277 f.complexProperties(PICTURE_KEY));
278 CPPUNIT_ASSERT(List<VariantMap>({geob1, geob2}) ==
279 f.complexProperties(GEOB_KEY));
280 }
281 }
282
283 void testSetGetId3Geob()
284 {
285 const VariantMap geob {
286 {"data", ByteVector("Just a test")},
287 {"mimeType", "text/plain"},
288 {"description", "Embedded object"},
289 {"fileName", "test.txt"}
290 };
291 ID3v2::Tag tag;
292 CPPUNIT_ASSERT(!tag.frameListMap().contains("GEOB"));
293 CPPUNIT_ASSERT(tag.complexPropertyKeys().isEmpty());
294 CPPUNIT_ASSERT(tag.complexProperties(GEOB_KEY).isEmpty());
295 CPPUNIT_ASSERT(tag.setComplexProperties(GEOB_KEY, {geob}));
296 CPPUNIT_ASSERT_EQUAL(StringList(GEOB_KEY), tag.complexPropertyKeys());
297 CPPUNIT_ASSERT_EQUAL(geob, tag.complexProperties(GEOB_KEY).front());
298 auto frames = tag.frameListMap().value("GEOB");
299 CPPUNIT_ASSERT_EQUAL(1U, frames.size());
300 auto frame =
301 dynamic_cast<ID3v2::GeneralEncapsulatedObjectFrame *>(frames.front());
302 CPPUNIT_ASSERT(frame);
303 CPPUNIT_ASSERT_EQUAL(geob.value("data").value<ByteVector>(), frame->object());
304 CPPUNIT_ASSERT_EQUAL(geob.value("mimeType").value<String>(), frame->mimeType());
305 CPPUNIT_ASSERT_EQUAL(geob.value("description").value<String>(), frame->description());
306 CPPUNIT_ASSERT_EQUAL(geob.value("fileName").value<String>(), frame->fileName());
307 }
308
309 void tagSetGetPicture(Tag &tag, const VariantMap &picture)
310 {
311 CPPUNIT_ASSERT(tag.complexPropertyKeys().isEmpty());
312 CPPUNIT_ASSERT(tag.complexProperties(PICTURE_KEY).isEmpty());
313 CPPUNIT_ASSERT(tag.setComplexProperties(PICTURE_KEY, {picture}));
314 CPPUNIT_ASSERT_EQUAL(StringList(PICTURE_KEY), tag.complexPropertyKeys());
315 CPPUNIT_ASSERT_EQUAL(picture, tag.complexProperties(PICTURE_KEY).front());
316 }
317
318 void testSetGetId3Picture()
319 {
320 const VariantMap picture(TEST_PICTURE);
321 ID3v2::Tag tag;
322 CPPUNIT_ASSERT(!tag.frameListMap().contains("APIC"));
323 tagSetGetPicture(tag, picture);
324 auto frames = tag.frameListMap().value("APIC");
325 CPPUNIT_ASSERT_EQUAL(1U, frames.size());
326 auto frame =
327 dynamic_cast<ID3v2::AttachedPictureFrame *>(frames.front());
328 CPPUNIT_ASSERT(frame);
329 CPPUNIT_ASSERT_EQUAL(picture.value("data").value<ByteVector>(), frame->picture());
330 CPPUNIT_ASSERT_EQUAL(picture.value("mimeType").value<String>(), frame->mimeType());
331 CPPUNIT_ASSERT_EQUAL(picture.value("description").value<String>(), frame->description());
332 CPPUNIT_ASSERT_EQUAL(ID3v2::AttachedPictureFrame::FrontCover, frame->type());
333 }
334
335 #ifdef TAGLIB_WITH_APE
336 void testSetGetApePicture()
337 {
338 const String FRONT_COVER("COVER ART (FRONT)");
339 VariantMap picture(TEST_PICTURE);
340 picture.erase("mimeType");
341 APE::Tag tag;
342 CPPUNIT_ASSERT(!tag.itemListMap().contains(FRONT_COVER));
343 tagSetGetPicture(tag, picture);
344 auto item = tag.itemListMap().value(FRONT_COVER);
345 CPPUNIT_ASSERT_EQUAL(
346 picture.value("description").value<String>().data(String::UTF8)
347 .append('\0')
348 .append(picture.value("data").value<ByteVector>()),
349 item.binaryData());
350 }
351 #endif
352
353 #ifdef TAGLIB_WITH_ASF
354 void testSetGetAsfPicture()
355 {
356 VariantMap picture(TEST_PICTURE);
357 ASF::Tag tag;
358 CPPUNIT_ASSERT(!tag.attributeListMap().contains("WM/Picture"));
359 tagSetGetPicture(tag, picture);
360 auto attributes = tag.attribute("WM/Picture");
361 CPPUNIT_ASSERT_EQUAL(1U, attributes.size());
362 auto asfPicture = attributes.front().toPicture();
363 CPPUNIT_ASSERT_EQUAL(picture.value("data").value<ByteVector>(),
364 asfPicture.picture());
365 CPPUNIT_ASSERT_EQUAL(picture.value("mimeType").value<String>(),
366 asfPicture.mimeType());
367 CPPUNIT_ASSERT_EQUAL(picture.value("description").value<String>(),
368 asfPicture.description());
369 CPPUNIT_ASSERT_EQUAL(ASF::Picture::FrontCover, asfPicture.type());
370 }
371 #endif
372
373 #ifdef TAGLIB_WITH_MP4
374 void testSetGetMp4Picture()
375 {
376 VariantMap picture(TEST_PICTURE);
377 picture.erase("description");
378 picture.erase("pictureType");
379 MP4::Tag tag;
380 CPPUNIT_ASSERT(!tag.itemMap().contains("covr"));
381 tagSetGetPicture(tag, picture);
382 auto covrs = tag.item("covr").toCoverArtList();
383 CPPUNIT_ASSERT_EQUAL(1U, covrs.size());
384 auto covr = covrs.front();
385 CPPUNIT_ASSERT_EQUAL(picture.value("data").value<ByteVector>(),
386 covr.data());
387 CPPUNIT_ASSERT_EQUAL(MP4::CoverArt::JPEG, covr.format());
388 }
389 #endif
390
391 #ifdef TAGLIB_WITH_VORBIS
392 void testSetGetXiphPicture()
393 {
394 VariantMap picture(TEST_PICTURE);
395 picture.insert("colorDepth", 8);
396 picture.insert("numColors", 1);
397 picture.insert("width", 1);
398 picture.insert("height", 1);
399 Ogg::XiphComment tag;
400 CPPUNIT_ASSERT(tag.pictureList().isEmpty());
401 tagSetGetPicture(tag, picture);
402 auto pics = tag.pictureList();
403 CPPUNIT_ASSERT_EQUAL(1U, pics.size());
404 auto pic = pics.front();
405 CPPUNIT_ASSERT_EQUAL(picture.value("data").value<ByteVector>(),
406 pic->data());
407 CPPUNIT_ASSERT_EQUAL(picture.value("mimeType").value<String>(),
408 pic->mimeType());
409 CPPUNIT_ASSERT_EQUAL(picture.value("description").value<String>(),
410 pic->description());
411 CPPUNIT_ASSERT_EQUAL(FLAC::Picture::FrontCover, pic->type());
412 CPPUNIT_ASSERT_EQUAL(8, pic->colorDepth());
413 CPPUNIT_ASSERT_EQUAL(1, pic->numColors());
414 CPPUNIT_ASSERT_EQUAL(1, pic->width());
415 CPPUNIT_ASSERT_EQUAL(1, pic->height());
416 }
417 #endif
418
419 void testNonExistent()
420 {
421 {
422 ID3v2::Tag tag;
423 CPPUNIT_ASSERT(tag.complexPropertyKeys().isEmpty());
424 CPPUNIT_ASSERT(tag.complexProperties(PICTURE_KEY).isEmpty());
425 CPPUNIT_ASSERT(tag.complexProperties(GEOB_KEY).isEmpty());
426 CPPUNIT_ASSERT(tag.complexProperties("NONEXISTENT").isEmpty());
427 CPPUNIT_ASSERT(!tag.setComplexProperties("NONEXISTENT", {{{"description", "test"}}}));
428 CPPUNIT_ASSERT(tag.complexProperties("NONEXISTENT").isEmpty());
429 CPPUNIT_ASSERT(tag.setComplexProperties(PICTURE_KEY, {TEST_PICTURE}));
430 CPPUNIT_ASSERT(!tag.complexProperties(PICTURE_KEY).isEmpty());
431 }
432 {
433 ID3v1::Tag tag;
434 CPPUNIT_ASSERT(tag.complexPropertyKeys().isEmpty());
435 CPPUNIT_ASSERT(tag.complexProperties(PICTURE_KEY).isEmpty());
436 CPPUNIT_ASSERT(tag.complexProperties(GEOB_KEY).isEmpty());
437 CPPUNIT_ASSERT(tag.complexProperties("NONEXISTENT").isEmpty());
438 CPPUNIT_ASSERT(!tag.setComplexProperties("NONEXISTENT", {{{"description", "test"}}}));
439 CPPUNIT_ASSERT(tag.complexProperties("NONEXISTENT").isEmpty());
440 CPPUNIT_ASSERT(!tag.setComplexProperties(PICTURE_KEY, {TEST_PICTURE}));
441 CPPUNIT_ASSERT(tag.complexProperties(PICTURE_KEY).isEmpty());
442 }
443 }
444 };
445
446 CPPUNIT_TEST_SUITE_REGISTRATION(TestComplexProperties);