Debian Codegraph @

tests/test_fileref_detect.cpp

raw symbols SCIP
1 /***************************************************************************
2 copyright : (C) 2026 by TagLib developers
3 ***************************************************************************/
4
5 /***************************************************************************
6 * This library is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU Lesser General Public License version *
8 * 2.1 as published by the Free Software Foundation. *
9 * *
10 * This library is distributed in the hope that it will be useful, but *
11 * WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Lesser General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with this library; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
18 * 02110-1301 USA *
19 * *
20 * Alternatively, this file is available under the Mozilla Public *
21 * License Version 1.1. You may obtain a copy of the License at *
22 * http://www.mozilla.org/MPL/ *
23 ***************************************************************************/
24
25 #include "taglib_config.h"
26 #include "tfilestream.h"
27 #include "tbytevectorstream.h"
28 #include "fileref.h"
29 #include "mpegfile.h"
30 #ifdef TAGLIB_WITH_VORBIS
31 #include "flacfile.h"
32 #include "oggflacfile.h"
33 #include "opusfile.h"
34 #include "speexfile.h"
35 #include "vorbisfile.h"
36 #endif
37 #ifdef TAGLIB_WITH_APE
38 #include "apefile.h"
39 #include "mpcfile.h"
40 #include "wavpackfile.h"
41 #endif
42 #ifdef TAGLIB_WITH_ASF
43 #include "asffile.h"
44 #endif
45 #ifdef TAGLIB_WITH_TRUEAUDIO
46 #include "trueaudiofile.h"
47 #endif
48 #ifdef TAGLIB_WITH_MP4
49 #include "mp4file.h"
50 #endif
51 #ifdef TAGLIB_WITH_RIFF
52 #include "aifffile.h"
53 #include "wavfile.h"
54 #endif
55 #ifdef TAGLIB_WITH_DSF
56 #include "dsdifffile.h"
57 #include "dsffile.h"
58 #endif
59 #ifdef TAGLIB_WITH_SHORTEN
60 #include "shortenfile.h"
61 #endif
62 #ifdef TAGLIB_WITH_MATROSKA
63 #include "matroskafile.h"
64 #endif
65 #include <cppunit/extensions/HelperMacros.h>
66 #include "utils.h"
67
68 using namespace TagLib;
69
70 // Files not covered by detection tests and the reason why:
71 // (All of these return null because no format's isSupported() matches them)
72 //
73 // MOD/S3M/IT/XM formats have no isSupported() implementation at all,
74 // so content-based detection is impossible for these files:
75 // changed.mod, test.mod, changed.s3m, test.s3m, test.it,
76 // changed.xm, test.xm, stripped.xm
77 //
78 // bare ID3 tag data without any surrounding audio stream:
79 // 005411.id3, broken-tenc.id3, unsynch.id3
80 //
81 // null bytes / truly unsupported binary format:
82 // no-extension, unsupported-extension.xx
83 //
84 // .mp3-named files where MPEG::File::isSupported() returns false because the
85 // MPEG frame scanner cannot find any valid frames in the content:
86 // garbage.mp3 (random binary data with no MPEG sync bytes),
87 // compressed_id3_frame.mp3 (zlib-compressed ID3 frame inflates to garbage
88 // that the frame scanner cannot parse past),
89 // duplicate_id3v2.mp3 (two consecutive ID3v2 headers confuse the size
90 // calculation, shifting the scan past any real frames),
91 // excessive_alloc.mp3 (APIC frame carries a crafted huge size field that
92 // the ID3v2 skip overshoots the actual frames),
93 // extended-header.mp3 (ID3v2.4 extended header flag causes incorrect size
94 // skip so the scanner starts inside the header),
95 // w000.mp3 (malformed file with no discoverable MPEG sync bytes)
96 //
97 // MPC SV4/SV5: MPC::File::isSupported() only recognises "MPCK" (SV8) and
98 // "MP+" (SV7); older SV4/SV5 streams have no standardised magic bytes:
99 // sv4_header.mpc, sv5_header.mpc
100 //
101 // MP4 with 64-bit atom sizes: first box is "moov" rather than the required
102 // "ftyp", so MP4::File::isSupported() returns false:
103 // 64bit.mp4
104 //
105 // corrupt AIFF: the FORM header is present but the sub-type bytes at offset 8
106 // are garbled (0x80 0x46 instead of 'AIFF'/'AIFC'), so
107 // RIFF::AIFF::File::isSupported() returns false:
108 // excessive_alloc.aif
109
110 namespace {
111
112 template <typename T> void detectByContent(const char *testFile) {
113 FileStream fs(TEST_FILE_PATH_C(testFile));
114 CPPUNIT_ASSERT(fs.isOpen());
115 const ByteVector data = fs.readBlock(fs.length());
116 ByteVectorStream bvs(data);
117 const FileRef f(&bvs);
118 CPPUNIT_ASSERT(!f.isNull());
119 CPPUNIT_ASSERT(dynamic_cast<T *>(f.file()) != nullptr);
120 }
121
122 void detectNullByContent(const char *testFile) {
123 FileStream fs(TEST_FILE_PATH_C(testFile));
124 CPPUNIT_ASSERT(fs.isOpen());
125 const ByteVector data = fs.readBlock(fs.length());
126 ByteVectorStream bvs(data);
127 const FileRef f(&bvs);
128 CPPUNIT_ASSERT(f.isNull());
129 }
130
131 } // namespace
132
133 class TestFileRefDetectByContent : public CppUnit::TestFixture
134 {
135 CPPUNIT_TEST_SUITE(TestFileRefDetectByContent);
136
137 // MPEG (always available)
138 CPPUNIT_TEST(testApeId3v1Mp3);
139 CPPUNIT_TEST(testApeId3v2Mp3);
140 CPPUNIT_TEST(testApeMp3);
141 CPPUNIT_TEST(testBladeencMp3);
142 CPPUNIT_TEST(testEmpty1sAac);
143 CPPUNIT_TEST(testId3v22TdaMp3);
144 CPPUNIT_TEST(testInvalidFrames1Mp3);
145 CPPUNIT_TEST(testInvalidFrames2Mp3);
146 CPPUNIT_TEST(testInvalidFrames3Mp3);
147 CPPUNIT_TEST(testItunes10Mp3);
148 CPPUNIT_TEST(testLameCbrMp3);
149 CPPUNIT_TEST(testLameVbrMp3);
150 CPPUNIT_TEST(testMpeg2Mp3);
151 CPPUNIT_TEST(testRareFramesMp3);
152 CPPUNIT_TEST(testTocManyChildrenMp3);
153 CPPUNIT_TEST(testXingMp3);
154
155 #ifdef TAGLIB_WITH_VORBIS
156 // Ogg::Vorbis::File
157 CPPUNIT_TEST(testEmptyOgg);
158 CPPUNIT_TEST(testEmptyVorbisOga);
159 CPPUNIT_TEST(testLowercaseFieldsOgg);
160 CPPUNIT_TEST(testTestOgg);
161 // Ogg::FLAC::File
162 CPPUNIT_TEST(testEmptyFlacOga);
163 // FLAC::File
164 CPPUNIT_TEST(testEmptySeektableFlac);
165 CPPUNIT_TEST(testMultipleVcFlac);
166 CPPUNIT_TEST(testNoTagsFlac);
167 CPPUNIT_TEST(testSilence44SFlac);
168 CPPUNIT_TEST(testSinewaveFlac);
169 CPPUNIT_TEST(testZeroSizedPaddingFlac);
170 CPPUNIT_TEST(testFLACWithMPEGSyncBytes);
171 // Ogg::Speex::File
172 CPPUNIT_TEST(testEmptySpx);
173 // Ogg::Opus::File
174 CPPUNIT_TEST(testCorrectnessGainSilentOutputOpus);
175 // Corrupt files: isSupported() returns true but isValid() returns false
176 CPPUNIT_TEST(testNullSegfaultOga);
177 #endif
178
179 #ifdef TAGLIB_WITH_APE
180 // MPC::File
181 CPPUNIT_TEST(testClickMpc);
182 CPPUNIT_TEST(testInfloopMpc);
183 CPPUNIT_TEST(testSegfaultMpc);
184 CPPUNIT_TEST(testSegfault2Mpc);
185 CPPUNIT_TEST(testSv8HeaderMpc);
186 CPPUNIT_TEST(testZerodivMpc);
187 // WavPack::File
188 CPPUNIT_TEST(testClickWv);
189 CPPUNIT_TEST(testDsdStereoWv);
190 CPPUNIT_TEST(testFourChannelsWv);
191 CPPUNIT_TEST(testInfloopWv);
192 CPPUNIT_TEST(testNoLengthWv);
193 CPPUNIT_TEST(testNonStandardRateWv);
194 CPPUNIT_TEST(testTaggedWv);
195 // APE::File
196 CPPUNIT_TEST(testLongloopApe);
197 CPPUNIT_TEST(testMac390HdrApe);
198 CPPUNIT_TEST(testMac396Ape);
199 CPPUNIT_TEST(testMac399Id3v2Ape);
200 CPPUNIT_TEST(testMac399TaggedApe);
201 CPPUNIT_TEST(testMac399Ape);
202 CPPUNIT_TEST(testZerodivApe);
203 #endif
204
205 #ifdef TAGLIB_WITH_TRUEAUDIO
206 CPPUNIT_TEST(testEmptyTta);
207 CPPUNIT_TEST(testTaggedTta);
208 #endif
209
210 #ifdef TAGLIB_WITH_MP4
211 CPPUNIT_TEST(testBlankVideoM4v);
212 CPPUNIT_TEST(testCovrJunkM4a);
213 CPPUNIT_TEST(testEmptyAlacM4a);
214 CPPUNIT_TEST(testGnreM4a);
215 CPPUNIT_TEST(testHasTagsM4a);
216 CPPUNIT_TEST(testIlstIsLastM4a);
217 CPPUNIT_TEST(testInfloopM4a);
218 CPPUNIT_TEST(testNoTags3g2);
219 CPPUNIT_TEST(testNoTagsM4a);
220 CPPUNIT_TEST(testNonFullMetaM4a);
221 CPPUNIT_TEST(testNonprintableAtomTypeM4a);
222 CPPUNIT_TEST(testZeroLengthMdatM4a);
223 #endif
224
225 #ifdef TAGLIB_WITH_ASF
226 CPPUNIT_TEST(testLosslessWma);
227 CPPUNIT_TEST(testSilence1Wma);
228 #endif
229
230 #ifdef TAGLIB_WITH_RIFF
231 // RIFF::AIFF::File
232 CPPUNIT_TEST(testAlawAifc);
233 CPPUNIT_TEST(testDuplicateId3v2Aiff);
234 CPPUNIT_TEST(testEmptyAiff);
235 CPPUNIT_TEST(testNoiseAif);
236 CPPUNIT_TEST(testNoiseOddAif);
237 CPPUNIT_TEST(testSegfaultAif);
238 // RIFF::WAV::File
239 CPPUNIT_TEST(testAlawWav);
240 CPPUNIT_TEST(testDuplicateTagsWav);
241 CPPUNIT_TEST(testEmptyWav);
242 CPPUNIT_TEST(testFloat64Wav);
243 CPPUNIT_TEST(testInfloopWav);
244 CPPUNIT_TEST(testInvalidChunkWav);
245 CPPUNIT_TEST(testPcmWithFactChunkWav);
246 CPPUNIT_TEST(testSegfaultWav);
247 CPPUNIT_TEST(testUint8weWav);
248 CPPUNIT_TEST(testZeroSizeChunkWav);
249 #endif
250
251 #ifdef TAGLIB_WITH_DSF
252 CPPUNIT_TEST(testEmpty10msDsf);
253 CPPUNIT_TEST(testEmpty10msDff);
254 #endif
255
256 #ifdef TAGLIB_WITH_SHORTEN
257 CPPUNIT_TEST(test2SecSilenceShn);
258 #endif
259
260 #ifdef TAGLIB_WITH_MATROSKA
261 CPPUNIT_TEST(testNoTagsMka);
262 CPPUNIT_TEST(testNoTagsWebm);
263 CPPUNIT_TEST(testOptimizedMkv);
264 CPPUNIT_TEST(testTagsBeforeCuesMkv);
265 #endif
266
267 CPPUNIT_TEST_SUITE_END();
268
269 public:
270 // -- MPEG::File (always available) --
271
272 void testApeId3v1Mp3() { detectByContent<MPEG::File>("ape-id3v1.mp3"); }
273 void testApeId3v2Mp3() { detectByContent<MPEG::File>("ape-id3v2.mp3"); }
274 void testApeMp3() { detectByContent<MPEG::File>("ape.mp3"); }
275 void testBladeencMp3() { detectByContent<MPEG::File>("bladeenc.mp3"); }
276 void testEmpty1sAac() { detectByContent<MPEG::File>("empty1s.aac"); }
277 void testId3v22TdaMp3() { detectByContent<MPEG::File>("id3v22-tda.mp3"); }
278 void testInvalidFrames1Mp3() {
279 detectByContent<MPEG::File>("invalid-frames1.mp3");
280 }
281 void testInvalidFrames2Mp3() {
282 detectByContent<MPEG::File>("invalid-frames2.mp3");
283 }
284 void testInvalidFrames3Mp3() {
285 detectByContent<MPEG::File>("invalid-frames3.mp3");
286 }
287 void testItunes10Mp3() { detectByContent<MPEG::File>("itunes10.mp3"); }
288 void testLameCbrMp3() { detectByContent<MPEG::File>("lame_cbr.mp3"); }
289 void testLameVbrMp3() { detectByContent<MPEG::File>("lame_vbr.mp3"); }
290 void testMpeg2Mp3() { detectByContent<MPEG::File>("mpeg2.mp3"); }
291 void testRareFramesMp3() {
292 detectByContent<MPEG::File>("rare_frames.mp3");
293 }
294 void testTocManyChildrenMp3() {
295 detectByContent<MPEG::File>("toc_many_children.mp3");
296 }
297 void testXingMp3() { detectByContent<MPEG::File>("xing.mp3"); }
298
299 #ifdef TAGLIB_WITH_VORBIS
300 // -- Ogg::Vorbis::File --
301 void testEmptyOgg() { detectByContent<Ogg::Vorbis::File>("empty.ogg"); }
302 void testEmptyVorbisOga() {
303 detectByContent<Ogg::Vorbis::File>("empty_vorbis.oga");
304 }
305 void testLowercaseFieldsOgg() {
306 detectByContent<Ogg::Vorbis::File>("lowercase-fields.ogg");
307 }
308 void testTestOgg() { detectByContent<Ogg::Vorbis::File>("test.ogg"); }
309
310 // -- Ogg::FLAC::File --
311 void testEmptyFlacOga() {
312 detectByContent<Ogg::FLAC::File>("empty_flac.oga");
313 }
314
315 // -- FLAC::File --
316 void testEmptySeektableFlac() {
317 detectByContent<FLAC::File>("empty-seektable.flac");
318 }
319 void testMultipleVcFlac() {
320 detectByContent<FLAC::File>("multiple-vc.flac");
321 }
322 void testNoTagsFlac() { detectByContent<FLAC::File>("no-tags.flac"); }
323 void testSilence44SFlac() {
324 detectByContent<FLAC::File>("silence-44-s.flac");
325 }
326 void testSinewaveFlac() { detectByContent<FLAC::File>("sinewave.flac"); }
327 void testZeroSizedPaddingFlac() {
328 detectByContent<FLAC::File>("zero-sized-padding.flac");
329 }
330 void testFLACWithMPEGSyncBytes() {
331 detectByContent<FLAC::File>("mpeg-sync-flac.flac");
332 }
333
334 // -- Ogg::Speex::File --
335 void testEmptySpx() { detectByContent<Ogg::Speex::File>("empty.spx"); }
336
337 // -- Ogg::Opus::File --
338 void testCorrectnessGainSilentOutputOpus() {
339 detectByContent<Ogg::Opus::File>("correctness_gain_silent_output.opus");
340 }
341
342 // segfault.oga: Ogg::FLAC::File::isSupported() returns true (valid Ogg
343 // container with a fLaC marker), but the FLAC metadata header inside is
344 // corrupt so Ogg::FLAC::File::isValid() returns false.
345 void testNullSegfaultOga() { detectNullByContent("segfault.oga"); }
346 #endif
347
348 #ifdef TAGLIB_WITH_APE
349 // -- MPC::File --
350 void testClickMpc() { detectByContent<MPC::File>("click.mpc"); }
351 void testInfloopMpc() { detectByContent<MPC::File>("infloop.mpc"); }
352 void testSegfaultMpc() { detectByContent<MPC::File>("segfault.mpc"); }
353 void testSegfault2Mpc() { detectByContent<MPC::File>("segfault2.mpc"); }
354 void testSv8HeaderMpc() { detectByContent<MPC::File>("sv8_header.mpc"); }
355 void testZerodivMpc() { detectByContent<MPC::File>("zerodiv.mpc"); }
356
357 // -- WavPack::File --
358 void testClickWv() { detectByContent<WavPack::File>("click.wv"); }
359 void testDsdStereoWv() { detectByContent<WavPack::File>("dsd_stereo.wv"); }
360 void testFourChannelsWv() {
361 detectByContent<WavPack::File>("four_channels.wv");
362 }
363 void testInfloopWv() { detectByContent<WavPack::File>("infloop.wv"); }
364 void testNoLengthWv() { detectByContent<WavPack::File>("no_length.wv"); }
365 void testNonStandardRateWv() {
366 detectByContent<WavPack::File>("non_standard_rate.wv");
367 }
368 void testTaggedWv() { detectByContent<WavPack::File>("tagged.wv"); }
369
370 // -- APE::File --
371 void testLongloopApe() { detectByContent<APE::File>("longloop.ape"); }
372 void testMac390HdrApe() { detectByContent<APE::File>("mac-390-hdr.ape"); }
373 void testMac396Ape() { detectByContent<APE::File>("mac-396.ape"); }
374 void testMac399Id3v2Ape() {
375 detectByContent<APE::File>("mac-399-id3v2.ape");
376 }
377 void testMac399TaggedApe() {
378 detectByContent<APE::File>("mac-399-tagged.ape");
379 }
380 void testMac399Ape() { detectByContent<APE::File>("mac-399.ape"); }
381 void testZerodivApe() { detectByContent<APE::File>("zerodiv.ape"); }
382 #endif
383
384 #ifdef TAGLIB_WITH_TRUEAUDIO
385 // -- TrueAudio::File --
386 void testEmptyTta() { detectByContent<TrueAudio::File>("empty.tta"); }
387 void testTaggedTta() { detectByContent<TrueAudio::File>("tagged.tta"); }
388 #endif
389
390 #ifdef TAGLIB_WITH_MP4
391 // -- MP4::File --
392 void testBlankVideoM4v() { detectByContent<MP4::File>("blank_video.m4v"); }
393 void testCovrJunkM4a() { detectByContent<MP4::File>("covr-junk.m4a"); }
394 void testEmptyAlacM4a() { detectByContent<MP4::File>("empty_alac.m4a"); }
395 void testGnreM4a() { detectByContent<MP4::File>("gnre.m4a"); }
396 void testHasTagsM4a() { detectByContent<MP4::File>("has-tags.m4a"); }
397 void testIlstIsLastM4a() {
398 detectByContent<MP4::File>("ilst-is-last.m4a");
399 }
400 void testInfloopM4a() { detectByContent<MP4::File>("infloop.m4a"); }
401 void testNoTags3g2() { detectByContent<MP4::File>("no-tags.3g2"); }
402 void testNoTagsM4a() { detectByContent<MP4::File>("no-tags.m4a"); }
403 void testNonFullMetaM4a() {
404 detectByContent<MP4::File>("non-full-meta.m4a");
405 }
406 void testNonprintableAtomTypeM4a() {
407 detectByContent<MP4::File>("nonprintable-atom-type.m4a");
408 }
409 void testZeroLengthMdatM4a() {
410 detectByContent<MP4::File>("zero-length-mdat.m4a");
411 }
412 #endif
413
414 #ifdef TAGLIB_WITH_ASF
415 // -- ASF::File --
416 void testLosslessWma() { detectByContent<ASF::File>("lossless.wma"); }
417 void testSilence1Wma() { detectByContent<ASF::File>("silence-1.wma"); }
418 #endif
419
420 #ifdef TAGLIB_WITH_RIFF
421 // -- RIFF::AIFF::File --
422 void testAlawAifc() { detectByContent<RIFF::AIFF::File>("alaw.aifc"); }
423 void testDuplicateId3v2Aiff() {
424 detectByContent<RIFF::AIFF::File>("duplicate_id3v2.aiff");
425 }
426 void testEmptyAiff() { detectByContent<RIFF::AIFF::File>("empty.aiff"); }
427 void testNoiseAif() { detectByContent<RIFF::AIFF::File>("noise.aif"); }
428 void testNoiseOddAif() {
429 detectByContent<RIFF::AIFF::File>("noise_odd.aif");
430 }
431 void testSegfaultAif() {
432 detectByContent<RIFF::AIFF::File>("segfault.aif");
433 }
434
435 // -- RIFF::WAV::File --
436 void testAlawWav() { detectByContent<RIFF::WAV::File>("alaw.wav"); }
437 void testDuplicateTagsWav() {
438 detectByContent<RIFF::WAV::File>("duplicate_tags.wav");
439 }
440 void testEmptyWav() { detectByContent<RIFF::WAV::File>("empty.wav"); }
441 void testFloat64Wav() { detectByContent<RIFF::WAV::File>("float64.wav"); }
442 void testInfloopWav() { detectByContent<RIFF::WAV::File>("infloop.wav"); }
443 void testInvalidChunkWav() {
444 detectByContent<RIFF::WAV::File>("invalid-chunk.wav");
445 }
446 void testPcmWithFactChunkWav() {
447 detectByContent<RIFF::WAV::File>("pcm_with_fact_chunk.wav");
448 }
449 void testSegfaultWav() { detectByContent<RIFF::WAV::File>("segfault.wav"); }
450 void testUint8weWav() { detectByContent<RIFF::WAV::File>("uint8we.wav"); }
451 void testZeroSizeChunkWav() {
452 detectByContent<RIFF::WAV::File>("zero-size-chunk.wav");
453 }
454 #endif
455
456 #ifdef TAGLIB_WITH_DSF
457 // -- DSF::File --
458 void testEmpty10msDsf() { detectByContent<DSF::File>("empty10ms.dsf"); }
459
460 // -- DSDIFF::File --
461 void testEmpty10msDff() { detectByContent<DSDIFF::File>("empty10ms.dff"); }
462 #endif
463
464 #ifdef TAGLIB_WITH_SHORTEN
465 // -- Shorten::File --
466 void test2SecSilenceShn() {
467 detectByContent<Shorten::File>("2sec-silence.shn");
468 }
469 #endif
470
471 #ifdef TAGLIB_WITH_MATROSKA
472 // -- Matroska::File --
473 void testNoTagsMka() { detectByContent<Matroska::File>("no-tags.mka"); }
474 void testNoTagsWebm() { detectByContent<Matroska::File>("no-tags.webm"); }
475 void testOptimizedMkv() {
476 detectByContent<Matroska::File>("optimized.mkv");
477 }
478 void testTagsBeforeCuesMkv() {
479 detectByContent<Matroska::File>("tags-before-cues.mkv");
480 }
481 #endif
482 };
483
484 CPPUNIT_TEST_SUITE_REGISTRATION(TestFileRefDetectByContent);