comparison data/fileio/test/BogusAudioFileReaderTest.h @ 1698:dbd13eb7dad1

Add tests for audio file readers presented with empty or nonsense input
author Chris Cannam
date Fri, 03 May 2019 13:33:53 +0100
parents
children 8ce940f9e32b
comparison
equal deleted inserted replaced
1690:ea1aa24ebf89 1698:dbd13eb7dad1
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_BOGUS_AUDIO_FILE_READER_TEST_H
16 #define SV_BOGUS_AUDIO_FILE_READER_TEST_H
17
18 #include "../AudioFileReaderFactory.h"
19
20 #include "base/TempDirectory.h"
21
22 #include <QObject>
23 #include <QtTest>
24 #include <QDir>
25
26 // Tests for malformed audio files - primarily to ensure we don't crash
27
28 class BogusAudioFileReaderTest : public QObject
29 {
30 Q_OBJECT
31
32 private slots:
33 void bogus_data()
34 {
35 QTest::addColumn<QString>("format");
36 QTest::addColumn<bool>("empty");
37 QStringList patterns = AudioFileReaderFactory::getKnownExtensions()
38 .split(" ", QString::SkipEmptyParts);
39
40 for (auto p: patterns) {
41
42 QStringList bits = p.split(".");
43 QString extension = bits[bits.size()-1];
44
45 QString testName = QString("%1, empty").arg(extension);
46 QTest::newRow(strdup(testName.toLocal8Bit().data()))
47 << extension << true;
48
49 testName = QString("%1, nonsense").arg(extension);
50 QTest::newRow(strdup(testName.toLocal8Bit().data()))
51 << extension << false;
52 }
53 }
54
55 void bogus()
56 {
57 QFETCH(QString, format);
58 QFETCH(bool, empty);
59
60 if (format == "au") { // au is headerless, so any file is legal
61 #if ( QT_VERSION >= 0x050000 )
62 QSKIP("Skipping headerless file");
63 #else
64 QSKIP("Skipping headerless file", SkipSingle);
65 #endif
66 }
67
68 QString tmpdir = TempDirectory::getInstance()->getPath();
69
70 QString path = QString("%1/%2.%3")
71 .arg(tmpdir)
72 .arg(empty ? "empty" : "nonsense")
73 .arg(format);
74 QFile f(path);
75 if (!f.open(QIODevice::WriteOnly)) {
76 std::cerr << "Failed to create temporary file "
77 << path << std::endl;
78 throw std::runtime_error("Failed to create temporary file");
79 }
80 if (!empty) {
81 for (int i = 0; i < 1000; ++i) {
82 f.write("weeble");
83 }
84 }
85 f.close();
86
87 AudioFileReader *reader =
88 AudioFileReaderFactory::createReader(path, {});
89 QCOMPARE((void *)reader, nullptr);
90 }
91
92 };
93
94 #endif