Debian Codegraph @

tests/test_fileref.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2007 by Lukas Lalinsky
3 email : [email protected]
4 ***************************************************************************/
5
6 /***************************************************************************
7 * This library is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License version *
9 * 2.1 as published by the Free Software Foundation. *
10 * *
11 * This library is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14 * Lesser General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with this library; if not, write to the Free Software *
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
19 * 02110-1301 USA *
20 * *
21 * Alternatively, this file is available under the Mozilla Public *
22 * License Version 1.1. You may obtain a copy of the License at *
23 * http://www.mozilla.org/MPL/ *
24 ***************************************************************************/
25
26 #include <string>
27 #include <cstdio>
28
29 #include "taglib_config.h"
30 #include "tfilestream.h"
31 #include "tbytevectorstream.h"
32 #include "tag.h"
33 #include "fileref.h"
34 #include "mpegfile.h"
35 #ifdef TAGLIB_WITH_VORBIS
36 #include "oggflacfile.h"
37 #include "vorbisfile.h"
38 #include "speexfile.h"
39 #include "flacfile.h"
40 #include "opusfile.h"
41 #endif
42 #ifdef TAGLIB_WITH_APE
43 #include "mpcfile.h"
44 #include "apefile.h"
45 #include "wavpackfile.h"
46 #endif
47 #ifdef TAGLIB_WITH_ASF
48 #include "asffile.h"
49 #endif
50 #ifdef TAGLIB_WITH_TRUEAUDIO
51 #include "trueaudiofile.h"
52 #endif
53 #ifdef TAGLIB_WITH_MP4
54 #include "mp4file.h"
55 #endif
56 #ifdef TAGLIB_WITH_RIFF
57 #include "wavfile.h"
58 #include "aifffile.h"
59 #endif
60 #ifdef TAGLIB_WITH_MOD
61 #include "xmfile.h"
62 #endif
63 #ifdef TAGLIB_WITH_DSF
64 #include "dsffile.h"
65 #include "dsdifffile.h"
66 #endif
67 #include <cppunit/extensions/HelperMacros.h>
68 #include "utils.h"
69
70 using namespace std;
71 using namespace TagLib;
72
73 namespace
74 {
75 #ifdef TAGLIB_WITH_VORBIS
76 class DummyResolver : public FileRef::FileTypeResolver
77 {
78 public:
79 File *createFile(FileName fileName, bool, AudioProperties::ReadStyle) const override
80 {
81 return new Ogg::Vorbis::File(fileName);
82 }
83 };
84 #endif
85
86 #ifdef TAGLIB_WITH_MP4
87 class DummyStreamResolver : public FileRef::StreamTypeResolver
88 {
89 public:
90 File *createFile(FileName, bool, AudioProperties::ReadStyle) const override
91 {
92 return nullptr;
93 }
94
95 File *createFileFromStream(IOStream *s, bool, AudioProperties::ReadStyle) const override
96 {
97 return new MP4::File(s);
98 }
99 };
100 #endif
101 } // namespace
102
103 class TestFileRef : public CppUnit::TestFixture
104 {
105 CPPUNIT_TEST_SUITE(TestFileRef);
106 CPPUNIT_TEST(testMP3);
107 CPPUNIT_TEST(testUnsupported);
108 CPPUNIT_TEST(testAudioProperties);
109 CPPUNIT_TEST(testDefaultFileExtensions);
110 CPPUNIT_TEST(testFileResolver);
111 #ifdef TAGLIB_WITH_ASF
112 CPPUNIT_TEST(testASF);
113 #endif
114 #ifdef TAGLIB_WITH_APE
115 CPPUNIT_TEST(testMusepack);
116 CPPUNIT_TEST(testAPE);
117 CPPUNIT_TEST(testWavPack);
118 #endif
119 #ifdef TAGLIB_WITH_VORBIS
120 CPPUNIT_TEST(testVorbis);
121 CPPUNIT_TEST(testSpeex);
122 CPPUNIT_TEST(testFLAC);
123 CPPUNIT_TEST(testOGA_FLAC);
124 CPPUNIT_TEST(testOGA_Vorbis);
125 CPPUNIT_TEST(testOpus);
126 #endif
127 #ifdef TAGLIB_WITH_MP4
128 CPPUNIT_TEST(testMP4_1);
129 CPPUNIT_TEST(testMP4_2);
130 CPPUNIT_TEST(testMP4_3);
131 CPPUNIT_TEST(testMP4_4);
132 #endif
133 #ifdef TAGLIB_WITH_TRUEAUDIO
134 CPPUNIT_TEST(testTrueAudio);
135 #endif
136 #ifdef TAGLIB_WITH_RIFF
137 CPPUNIT_TEST(testWav);
138 CPPUNIT_TEST(testAIFF_1);
139 CPPUNIT_TEST(testAIFF_2);
140 #endif
141 #ifdef TAGLIB_WITH_DSF
142 CPPUNIT_TEST(testDSF);
143 CPPUNIT_TEST(testDSDIFF);
144 #endif
145 CPPUNIT_TEST_SUITE_END();
146
147 public:
148
149 template <typename T>
150 void fileRefSave(const string &filename, const string &ext)
151 {
152 ScopedFileCopy copy(filename, ext);
153 string newname = copy.fileName();
154
155 {
156 FileRef f(newname.c_str());
157 CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
158 CPPUNIT_ASSERT(!f.isNull());
159 f.tag()->setArtist("test artist");
160 f.tag()->setTitle("test title");
161 f.tag()->setGenre("Test!");
162 f.tag()->setAlbum("albummmm");
163 f.tag()->setComment("a comment");
164 f.tag()->setTrack(5);
165 f.tag()->setYear(2020);
166 f.save();
167 }
168 {
169 FileRef f(newname.c_str());
170 CPPUNIT_ASSERT(!f.isNull());
171 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
172 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
173 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
174 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
175 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
176 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
177 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));
178 f.tag()->setArtist("ttest artist");
179 f.tag()->setTitle("ytest title");
180 f.tag()->setGenre("uTest!");
181 f.tag()->setAlbum("ialbummmm");
182 f.tag()->setComment("another comment");
183 f.tag()->setTrack(7);
184 f.tag()->setYear(2080);
185 f.save();
186 }
187 {
188 FileRef f(newname.c_str());
189 CPPUNIT_ASSERT(!f.isNull());
190 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
191 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
192 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
193 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
194 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
195 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
196 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
197 }
198
199 {
200 FileStream fs(newname.c_str());
201 FileRef f(&fs);
202 CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
203 CPPUNIT_ASSERT(!f.isNull());
204 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
205 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
206 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
207 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
208 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
209 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
210 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
211 f.tag()->setArtist("test artist");
212 f.tag()->setTitle("test title");
213 f.tag()->setGenre("Test!");
214 f.tag()->setAlbum("albummmm");
215 f.tag()->setComment("a comment");
216 f.tag()->setTrack(5);
217 f.tag()->setYear(2020);
218 f.save();
219 }
220
221 ByteVector fileContent;
222 {
223 FileStream fs(newname.c_str());
224 FileRef f(&fs);
225 CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
226 CPPUNIT_ASSERT(!f.isNull());
227 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
228 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
229 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
230 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
231 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
232 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
233 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));
234
235 fs.seek(0);
236 fileContent = fs.readBlock(fs.length());
237 }
238
239 {
240 ByteVectorStream bs(fileContent);
241 FileRef f(&bs);
242 CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
243 CPPUNIT_ASSERT(!f.isNull());
244 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
245 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
246 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
247 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
248 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
249 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
250 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));
251 f.tag()->setArtist("ttest artist");
252 f.tag()->setTitle("ytest title");
253 f.tag()->setGenre("uTest!");
254 f.tag()->setAlbum("ialbummmm");
255 f.tag()->setComment("another comment");
256 f.tag()->setTrack(7);
257 f.tag()->setYear(2080);
258 f.save();
259
260 fileContent = *bs.data();
261 }
262 {
263 ByteVectorStream bs(fileContent);
264 FileRef f(&bs);
265 CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
266 CPPUNIT_ASSERT(!f.isNull());
267 CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
268 CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
269 CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
270 CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
271 CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
272 CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
273 CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
274 }
275 }
276
277 #ifdef TAGLIB_WITH_APE
278 void testMusepack()
279 {
280 fileRefSave<MPC::File>("click", ".mpc");
281 }
282 #endif
283
284 #ifdef TAGLIB_WITH_ASF
285 void testASF()
286 {
287 fileRefSave<ASF::File>("silence-1", ".wma");
288 }
289 #endif
290
291 #ifdef TAGLIB_WITH_VORBIS
292 void testVorbis()
293 {
294 fileRefSave<Ogg::Vorbis::File>("empty", ".ogg");
295 }
296
297 void testSpeex()
298 {
299 fileRefSave<Ogg::Speex::File>("empty", ".spx");
300 }
301
302 void testFLAC()
303 {
304 fileRefSave<FLAC::File>("no-tags", ".flac");
305 }
306 #endif
307
308 void testMP3()
309 {
310 fileRefSave<MPEG::File>("xing", ".mp3");
311 }
312
313 #ifdef TAGLIB_WITH_TRUEAUDIO
314 void testTrueAudio()
315 {
316 fileRefSave<TrueAudio::File>("empty", ".tta");
317 }
318 #endif
319
320 #ifdef TAGLIB_WITH_MP4
321 void testMP4_1()
322 {
323 fileRefSave<MP4::File>("has-tags", ".m4a");
324 }
325
326 void testMP4_2()
327 {
328 fileRefSave<MP4::File>("no-tags", ".m4a");
329 }
330
331 void testMP4_3()
332 {
333 fileRefSave<MP4::File>("no-tags", ".3g2");
334 }
335
336 void testMP4_4()
337 {
338 fileRefSave<MP4::File>("blank_video", ".m4v");
339 }
340 #endif
341
342 #ifdef TAGLIB_WITH_RIFF
343 void testWav()
344 {
345 fileRefSave<RIFF::WAV::File>("empty", ".wav");
346 }
347 #endif
348
349 #ifdef TAGLIB_WITH_VORBIS
350 void testOGA_FLAC()
351 {
352 fileRefSave<Ogg::FLAC::File>("empty_flac", ".oga");
353 }
354
355 void testOGA_Vorbis()
356 {
357 fileRefSave<Ogg::Vorbis::File>("empty_vorbis", ".oga");
358 }
359 #endif
360
361 #ifdef TAGLIB_WITH_APE
362 void testAPE()
363 {
364 fileRefSave<APE::File>("mac-399", ".ape");
365 }
366 #endif
367
368 #ifdef TAGLIB_WITH_RIFF
369 void testAIFF_1()
370 {
371 fileRefSave<RIFF::AIFF::File>("empty", ".aiff");
372 }
373
374 void testAIFF_2()
375 {
376 fileRefSave<RIFF::AIFF::File>("alaw", ".aifc");
377 }
378 #endif
379
380 #ifdef TAGLIB_WITH_APE
381 void testWavPack()
382 {
383 fileRefSave<WavPack::File>("click", ".wv");
384 }
385 #endif
386
387 #ifdef TAGLIB_WITH_VORBIS
388 void testOpus()
389 {
390 fileRefSave<Ogg::Opus::File>("correctness_gain_silent_output", ".opus");
391 }
392 #endif
393
394 #ifdef TAGLIB_WITH_DSF
395 void testDSF()
396 {
397 fileRefSave<DSF::File>("empty10ms",".dsf");
398 }
399
400 void testDSDIFF()
401 {
402 fileRefSave<DSDIFF::File>("empty10ms",".dff");
403 }
404 #endif
405
406 void testUnsupported()
407 {
408 FileRef f1(TEST_FILE_PATH_C("no-extension"));
409 CPPUNIT_ASSERT(f1.isNull());
410
411 FileRef f2(TEST_FILE_PATH_C("unsupported-extension.xx"));
412 CPPUNIT_ASSERT(f2.isNull());
413 }
414
415 void testAudioProperties()
416 {
417 FileRef f(TEST_FILE_PATH_C("xing.mp3"));
418 const AudioProperties *audioProperties = f.audioProperties();
419 CPPUNIT_ASSERT_EQUAL(2, audioProperties->lengthInSeconds());
420 CPPUNIT_ASSERT_EQUAL(2064, audioProperties->lengthInMilliseconds());
421 }
422
423 void testDefaultFileExtensions()
424 {
425 const StringList extensions = FileRef::defaultFileExtensions();
426 #ifdef TAGLIB_WITH_APE
427 CPPUNIT_ASSERT(extensions.contains("mpc"));
428 #endif
429 #ifdef TAGLIB_WITH_ASF
430 CPPUNIT_ASSERT(extensions.contains("wma"));
431 #endif
432 #ifdef TAGLIB_WITH_VORBIS
433 CPPUNIT_ASSERT(extensions.contains("ogg"));
434 CPPUNIT_ASSERT(extensions.contains("spx"));
435 CPPUNIT_ASSERT(extensions.contains("flac"));
436 #endif
437 CPPUNIT_ASSERT(extensions.contains("mp3"));
438 #ifdef TAGLIB_WITH_TRUEAUDIO
439 CPPUNIT_ASSERT(extensions.contains("tta"));
440 #endif
441 #ifdef TAGLIB_WITH_MP4
442 CPPUNIT_ASSERT(extensions.contains("m4a"));
443 CPPUNIT_ASSERT(extensions.contains("3g2"));
444 CPPUNIT_ASSERT(extensions.contains("m4v"));
445 #endif
446 #ifdef TAGLIB_WITH_RIFF
447 CPPUNIT_ASSERT(extensions.contains("wav"));
448 #endif
449 #ifdef TAGLIB_WITH_VORBIS
450 CPPUNIT_ASSERT(extensions.contains("oga"));
451 #endif
452 #ifdef TAGLIB_WITH_APE
453 CPPUNIT_ASSERT(extensions.contains("ape"));
454 #endif
455 #ifdef TAGLIB_WITH_RIFF
456 CPPUNIT_ASSERT(extensions.contains("aiff"));
457 CPPUNIT_ASSERT(extensions.contains("aifc"));
458 #endif
459 #ifdef TAGLIB_WITH_APE
460 CPPUNIT_ASSERT(extensions.contains("wv"));
461 #endif
462 #ifdef TAGLIB_WITH_VORBIS
463 CPPUNIT_ASSERT(extensions.contains("opus"));
464 #endif
465 #ifdef TAGLIB_WITH_MOD
466 CPPUNIT_ASSERT(extensions.contains("xm"));
467 #endif
468 #ifdef TAGLIB_WITH_DSF
469 CPPUNIT_ASSERT(extensions.contains("dsf"));
470 CPPUNIT_ASSERT(extensions.contains("dff"));
471 CPPUNIT_ASSERT(extensions.contains("dsdiff"));
472 #endif
473 #ifdef TAGLIB_WITH_SHORTEN
474 CPPUNIT_ASSERT(extensions.contains("shn"));
475 #endif
476 #ifdef TAGLIB_WITH_MATROSKA
477 CPPUNIT_ASSERT(extensions.contains("mkv"));
478 CPPUNIT_ASSERT(extensions.contains("mka"));
479 CPPUNIT_ASSERT(extensions.contains("webm"));
480 #endif
481 }
482
483 void testFileResolver()
484 {
485 {
486 FileRef f(TEST_FILE_PATH_C("xing.mp3"));
487 CPPUNIT_ASSERT(dynamic_cast<MPEG::File *>(f.file()) != nullptr);
488 }
489
490 #ifdef TAGLIB_WITH_VORBIS
491 DummyResolver resolver;
492 FileRef::addFileTypeResolver(&resolver);
493
494 {
495 FileRef f(TEST_FILE_PATH_C("xing.mp3"));
496 CPPUNIT_ASSERT(dynamic_cast<Ogg::Vorbis::File *>(f.file()) != nullptr);
497 }
498 #endif
499
500 #ifdef TAGLIB_WITH_MP4
501 DummyStreamResolver streamResolver;
502 FileRef::addFileTypeResolver(&streamResolver);
503
504 {
505 FileStream s(TEST_FILE_PATH_C("xing.mp3"));
506 FileRef f(&s);
507 CPPUNIT_ASSERT(dynamic_cast<MP4::File *>(f.file()) != nullptr);
508 }
509 #endif
510
511 FileRef::clearFileTypeResolvers();
512 }
513
514 };
515
516 CPPUNIT_TEST_SUITE_REGISTRATION(TestFileRef);