annotate data/fileio/test/BogusAudioFileReaderTest.h @ 1700:c1208b211d8c single-point

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