File: test_fileref.cpp

package info (click to toggle)
taglib 2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192 kB
  • sloc: cpp: 44,736; ansic: 137; makefile: 28
file content (516 lines) | stat: -rw-r--r-- 14,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/***************************************************************************
    copyright           : (C) 2007 by Lukas Lalinsky
    email               : lukas@oxygene.sk
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License version   *
 *   2.1 as published by the Free Software Foundation.                     *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA         *
 *   02110-1301  USA                                                       *
 *                                                                         *
 *   Alternatively, this file is available under the Mozilla Public        *
 *   License Version 1.1.  You may obtain a copy of the License at         *
 *   http://www.mozilla.org/MPL/                                           *
 ***************************************************************************/

#include <string>
#include <cstdio>

#include "taglib_config.h"
#include "tfilestream.h"
#include "tbytevectorstream.h"
#include "tag.h"
#include "fileref.h"
#include "mpegfile.h"
#ifdef TAGLIB_WITH_VORBIS
#include "oggflacfile.h"
#include "vorbisfile.h"
#include "speexfile.h"
#include "flacfile.h"
#include "opusfile.h"
#endif
#ifdef TAGLIB_WITH_APE
#include "mpcfile.h"
#include "apefile.h"
#include "wavpackfile.h"
#endif
#ifdef TAGLIB_WITH_ASF
#include "asffile.h"
#endif
#ifdef TAGLIB_WITH_TRUEAUDIO
#include "trueaudiofile.h"
#endif
#ifdef TAGLIB_WITH_MP4
#include "mp4file.h"
#endif
#ifdef TAGLIB_WITH_RIFF
#include "wavfile.h"
#include "aifffile.h"
#endif
#ifdef TAGLIB_WITH_MOD
#include "xmfile.h"
#endif
#ifdef TAGLIB_WITH_DSF
#include "dsffile.h"
#include "dsdifffile.h"
#endif
#include <cppunit/extensions/HelperMacros.h>
#include "utils.h"

using namespace std;
using namespace TagLib;

namespace
{
#ifdef TAGLIB_WITH_VORBIS
  class DummyResolver : public FileRef::FileTypeResolver
  {
  public:
    File *createFile(FileName fileName, bool, AudioProperties::ReadStyle) const override
    {
      return new Ogg::Vorbis::File(fileName);
    }
  };
#endif

#ifdef TAGLIB_WITH_MP4
  class DummyStreamResolver : public FileRef::StreamTypeResolver
  {
  public:
    File *createFile(FileName, bool, AudioProperties::ReadStyle) const override
    {
      return nullptr;
    }

    File *createFileFromStream(IOStream *s, bool, AudioProperties::ReadStyle) const override
    {
      return new MP4::File(s);
    }
  };
#endif
} // namespace

class TestFileRef : public CppUnit::TestFixture
{
  CPPUNIT_TEST_SUITE(TestFileRef);
  CPPUNIT_TEST(testMP3);
  CPPUNIT_TEST(testUnsupported);
  CPPUNIT_TEST(testAudioProperties);
  CPPUNIT_TEST(testDefaultFileExtensions);
  CPPUNIT_TEST(testFileResolver);
#ifdef TAGLIB_WITH_ASF
  CPPUNIT_TEST(testASF);
#endif
#ifdef TAGLIB_WITH_APE
  CPPUNIT_TEST(testMusepack);
  CPPUNIT_TEST(testAPE);
  CPPUNIT_TEST(testWavPack);
#endif
#ifdef TAGLIB_WITH_VORBIS
  CPPUNIT_TEST(testVorbis);
  CPPUNIT_TEST(testSpeex);
  CPPUNIT_TEST(testFLAC);
  CPPUNIT_TEST(testOGA_FLAC);
  CPPUNIT_TEST(testOGA_Vorbis);
  CPPUNIT_TEST(testOpus);
#endif
#ifdef TAGLIB_WITH_MP4
  CPPUNIT_TEST(testMP4_1);
  CPPUNIT_TEST(testMP4_2);
  CPPUNIT_TEST(testMP4_3);
  CPPUNIT_TEST(testMP4_4);
#endif
#ifdef TAGLIB_WITH_TRUEAUDIO
  CPPUNIT_TEST(testTrueAudio);
#endif
#ifdef TAGLIB_WITH_RIFF
  CPPUNIT_TEST(testWav);
  CPPUNIT_TEST(testAIFF_1);
  CPPUNIT_TEST(testAIFF_2);
#endif
#ifdef TAGLIB_WITH_DSF
  CPPUNIT_TEST(testDSF);
  CPPUNIT_TEST(testDSDIFF);
#endif
  CPPUNIT_TEST_SUITE_END();

public:

  template <typename T>
  void fileRefSave(const string &filename, const string &ext)
  {
    ScopedFileCopy copy(filename, ext);
    string newname = copy.fileName();

    {
      FileRef f(newname.c_str());
      CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
      CPPUNIT_ASSERT(!f.isNull());
      f.tag()->setArtist("test artist");
      f.tag()->setTitle("test title");
      f.tag()->setGenre("Test!");
      f.tag()->setAlbum("albummmm");
      f.tag()->setComment("a comment");
      f.tag()->setTrack(5);
      f.tag()->setYear(2020);
      f.save();
    }
    {
      FileRef f(newname.c_str());
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));
      f.tag()->setArtist("ttest artist");
      f.tag()->setTitle("ytest title");
      f.tag()->setGenre("uTest!");
      f.tag()->setAlbum("ialbummmm");
      f.tag()->setComment("another comment");
      f.tag()->setTrack(7);
      f.tag()->setYear(2080);
      f.save();
    }
    {
      FileRef f(newname.c_str());
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
    }

    {
      FileStream fs(newname.c_str());
      FileRef f(&fs);
      CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
      f.tag()->setArtist("test artist");
      f.tag()->setTitle("test title");
      f.tag()->setGenre("Test!");
      f.tag()->setAlbum("albummmm");
      f.tag()->setComment("a comment");
      f.tag()->setTrack(5);
      f.tag()->setYear(2020);
      f.save();
    }

    ByteVector fileContent;
    {
      FileStream fs(newname.c_str());
      FileRef f(&fs);
      CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));

      fs.seek(0);
      fileContent = fs.readBlock(fs.length());
    }

    {
      ByteVectorStream bs(fileContent);
      FileRef f(&bs);
      CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("test artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("test title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("Test!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("albummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("a comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(5));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2020));
      f.tag()->setArtist("ttest artist");
      f.tag()->setTitle("ytest title");
      f.tag()->setGenre("uTest!");
      f.tag()->setAlbum("ialbummmm");
      f.tag()->setComment("another comment");
      f.tag()->setTrack(7);
      f.tag()->setYear(2080);
      f.save();

      fileContent = *bs.data();
    }
    {
      ByteVectorStream bs(fileContent);
      FileRef f(&bs);
      CPPUNIT_ASSERT(dynamic_cast<T*>(f.file()));
      CPPUNIT_ASSERT(!f.isNull());
      CPPUNIT_ASSERT_EQUAL(f.tag()->artist(), String("ttest artist"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->title(), String("ytest title"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->genre(), String("uTest!"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->album(), String("ialbummmm"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->comment(), String("another comment"));
      CPPUNIT_ASSERT_EQUAL(f.tag()->track(), static_cast<unsigned int>(7));
      CPPUNIT_ASSERT_EQUAL(f.tag()->year(), static_cast<unsigned int>(2080));
    }
  }

#ifdef TAGLIB_WITH_APE
  void testMusepack()
  {
    fileRefSave<MPC::File>("click", ".mpc");
  }
#endif

#ifdef TAGLIB_WITH_ASF
  void testASF()
  {
    fileRefSave<ASF::File>("silence-1", ".wma");
  }
#endif

#ifdef TAGLIB_WITH_VORBIS
  void testVorbis()
  {
    fileRefSave<Ogg::Vorbis::File>("empty", ".ogg");
  }

  void testSpeex()
  {
    fileRefSave<Ogg::Speex::File>("empty", ".spx");
  }

  void testFLAC()
  {
    fileRefSave<FLAC::File>("no-tags", ".flac");
  }
#endif

  void testMP3()
  {
    fileRefSave<MPEG::File>("xing", ".mp3");
  }

#ifdef TAGLIB_WITH_TRUEAUDIO
  void testTrueAudio()
  {
    fileRefSave<TrueAudio::File>("empty", ".tta");
  }
#endif

#ifdef TAGLIB_WITH_MP4
  void testMP4_1()
  {
    fileRefSave<MP4::File>("has-tags", ".m4a");
  }

  void testMP4_2()
  {
    fileRefSave<MP4::File>("no-tags", ".m4a");
  }

  void testMP4_3()
  {
    fileRefSave<MP4::File>("no-tags", ".3g2");
  }

  void testMP4_4()
  {
    fileRefSave<MP4::File>("blank_video", ".m4v");
  }
#endif

#ifdef TAGLIB_WITH_RIFF
  void testWav()
  {
    fileRefSave<RIFF::WAV::File>("empty", ".wav");
  }
#endif

#ifdef TAGLIB_WITH_VORBIS
  void testOGA_FLAC()
  {
    fileRefSave<Ogg::FLAC::File>("empty_flac", ".oga");
  }

  void testOGA_Vorbis()
  {
    fileRefSave<Ogg::Vorbis::File>("empty_vorbis", ".oga");
  }
#endif

#ifdef TAGLIB_WITH_APE
  void testAPE()
  {
    fileRefSave<APE::File>("mac-399", ".ape");
  }
#endif

#ifdef TAGLIB_WITH_RIFF
  void testAIFF_1()
  {
    fileRefSave<RIFF::AIFF::File>("empty", ".aiff");
  }

  void testAIFF_2()
  {
    fileRefSave<RIFF::AIFF::File>("alaw", ".aifc");
  }
#endif

#ifdef TAGLIB_WITH_APE
  void testWavPack()
  {
    fileRefSave<WavPack::File>("click", ".wv");
  }
#endif

#ifdef TAGLIB_WITH_VORBIS
  void testOpus()
  {
    fileRefSave<Ogg::Opus::File>("correctness_gain_silent_output", ".opus");
  }
#endif

#ifdef TAGLIB_WITH_DSF
  void testDSF()
  {
    fileRefSave<DSF::File>("empty10ms",".dsf");
  }

  void testDSDIFF()
  {
    fileRefSave<DSDIFF::File>("empty10ms",".dff");
  }
#endif

  void testUnsupported()
  {
    FileRef f1(TEST_FILE_PATH_C("no-extension"));
    CPPUNIT_ASSERT(f1.isNull());

    FileRef f2(TEST_FILE_PATH_C("unsupported-extension.xx"));
    CPPUNIT_ASSERT(f2.isNull());
  }

  void testAudioProperties()
  {
    FileRef f(TEST_FILE_PATH_C("xing.mp3"));
    const AudioProperties *audioProperties = f.audioProperties();
    CPPUNIT_ASSERT_EQUAL(2, audioProperties->lengthInSeconds());
    CPPUNIT_ASSERT_EQUAL(2064, audioProperties->lengthInMilliseconds());
  }

  void testDefaultFileExtensions()
  {
    const StringList extensions = FileRef::defaultFileExtensions();
#ifdef TAGLIB_WITH_APE
    CPPUNIT_ASSERT(extensions.contains("mpc"));
#endif
#ifdef TAGLIB_WITH_ASF
    CPPUNIT_ASSERT(extensions.contains("wma"));
#endif
#ifdef TAGLIB_WITH_VORBIS
    CPPUNIT_ASSERT(extensions.contains("ogg"));
    CPPUNIT_ASSERT(extensions.contains("spx"));
    CPPUNIT_ASSERT(extensions.contains("flac"));
#endif
    CPPUNIT_ASSERT(extensions.contains("mp3"));
#ifdef TAGLIB_WITH_TRUEAUDIO
    CPPUNIT_ASSERT(extensions.contains("tta"));
#endif
#ifdef TAGLIB_WITH_MP4
    CPPUNIT_ASSERT(extensions.contains("m4a"));
    CPPUNIT_ASSERT(extensions.contains("3g2"));
    CPPUNIT_ASSERT(extensions.contains("m4v"));
#endif
#ifdef TAGLIB_WITH_RIFF
    CPPUNIT_ASSERT(extensions.contains("wav"));
#endif
#ifdef TAGLIB_WITH_VORBIS
    CPPUNIT_ASSERT(extensions.contains("oga"));
#endif
#ifdef TAGLIB_WITH_APE
    CPPUNIT_ASSERT(extensions.contains("ape"));
#endif
#ifdef TAGLIB_WITH_RIFF
    CPPUNIT_ASSERT(extensions.contains("aiff"));
    CPPUNIT_ASSERT(extensions.contains("aifc"));
#endif
#ifdef TAGLIB_WITH_APE
    CPPUNIT_ASSERT(extensions.contains("wv"));
#endif
#ifdef TAGLIB_WITH_VORBIS
    CPPUNIT_ASSERT(extensions.contains("opus"));
#endif
#ifdef TAGLIB_WITH_MOD
    CPPUNIT_ASSERT(extensions.contains("xm"));
#endif
#ifdef TAGLIB_WITH_DSF
    CPPUNIT_ASSERT(extensions.contains("dsf"));
    CPPUNIT_ASSERT(extensions.contains("dff"));
    CPPUNIT_ASSERT(extensions.contains("dsdiff"));
#endif
#ifdef TAGLIB_WITH_SHORTEN
    CPPUNIT_ASSERT(extensions.contains("shn"));
#endif
#ifdef TAGLIB_WITH_MATROSKA
    CPPUNIT_ASSERT(extensions.contains("mkv"));
    CPPUNIT_ASSERT(extensions.contains("mka"));
    CPPUNIT_ASSERT(extensions.contains("webm"));
#endif
  }

  void testFileResolver()
  {
    {
      FileRef f(TEST_FILE_PATH_C("xing.mp3"));
      CPPUNIT_ASSERT(dynamic_cast<MPEG::File *>(f.file()) != nullptr);
    }

#ifdef TAGLIB_WITH_VORBIS
    DummyResolver resolver;
    FileRef::addFileTypeResolver(&resolver);

    {
      FileRef f(TEST_FILE_PATH_C("xing.mp3"));
      CPPUNIT_ASSERT(dynamic_cast<Ogg::Vorbis::File *>(f.file()) != nullptr);
    }
#endif

#ifdef TAGLIB_WITH_MP4
    DummyStreamResolver streamResolver;
    FileRef::addFileTypeResolver(&streamResolver);

    {
      FileStream s(TEST_FILE_PATH_C("xing.mp3"));
      FileRef f(&s);
      CPPUNIT_ASSERT(dynamic_cast<MP4::File *>(f.file()) != nullptr);
    }
#endif

    FileRef::clearFileTypeResolvers();
  }

};

CPPUNIT_TEST_SUITE_REGISTRATION(TestFileRef);