Chris@1698: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1698: Chris@1698: /* Chris@1698: Sonic Visualiser Chris@1698: An audio file viewer and annotation editor. Chris@1698: Centre for Digital Music, Queen Mary, University of London. Chris@1698: Chris@1698: This program is free software; you can redistribute it and/or Chris@1698: modify it under the terms of the GNU General Public License as Chris@1698: published by the Free Software Foundation; either version 2 of the Chris@1698: License, or (at your option) any later version. See the file Chris@1698: COPYING included with this distribution for more information. Chris@1698: */ Chris@1698: Chris@1698: #ifndef SV_BOGUS_AUDIO_FILE_READER_TEST_H Chris@1698: #define SV_BOGUS_AUDIO_FILE_READER_TEST_H Chris@1698: Chris@1698: #include "../AudioFileReaderFactory.h" Chris@1698: Chris@1698: #include "base/TempDirectory.h" Chris@1698: Chris@1698: #include Chris@1698: #include Chris@1698: #include Chris@1698: Chris@1698: // Tests for malformed audio files - primarily to ensure we don't crash Chris@1698: Chris@1698: class BogusAudioFileReaderTest : public QObject Chris@1698: { Chris@1698: Q_OBJECT Chris@1698: Chris@1698: private slots: Chris@1698: void bogus_data() Chris@1698: { Chris@1698: QTest::addColumn("format"); Chris@1698: QTest::addColumn("empty"); Chris@1698: QStringList patterns = AudioFileReaderFactory::getKnownExtensions() Chris@1698: .split(" ", QString::SkipEmptyParts); Chris@1698: Chris@1698: for (auto p: patterns) { Chris@1698: Chris@1698: QStringList bits = p.split("."); Chris@1698: QString extension = bits[bits.size()-1]; Chris@1698: Chris@1698: QString testName = QString("%1, empty").arg(extension); Chris@1698: QTest::newRow(strdup(testName.toLocal8Bit().data())) Chris@1698: << extension << true; Chris@1698: Chris@1698: testName = QString("%1, nonsense").arg(extension); Chris@1698: QTest::newRow(strdup(testName.toLocal8Bit().data())) Chris@1698: << extension << false; Chris@1698: } Chris@1698: } Chris@1698: Chris@1698: void bogus() Chris@1698: { Chris@1698: QFETCH(QString, format); Chris@1698: QFETCH(bool, empty); Chris@1698: Chris@1698: if (format == "au") { // au is headerless, so any file is legal Chris@1698: #if ( QT_VERSION >= 0x050000 ) Chris@1698: QSKIP("Skipping headerless file"); Chris@1698: #else Chris@1698: QSKIP("Skipping headerless file", SkipSingle); Chris@1698: #endif Chris@1698: } Chris@1698: Chris@1698: QString tmpdir = TempDirectory::getInstance()->getPath(); Chris@1698: Chris@1698: QString path = QString("%1/%2.%3") Chris@1698: .arg(tmpdir) Chris@1698: .arg(empty ? "empty" : "nonsense") Chris@1698: .arg(format); Chris@1698: QFile f(path); Chris@1698: if (!f.open(QIODevice::WriteOnly)) { Chris@1698: std::cerr << "Failed to create temporary file " Chris@1698: << path << std::endl; Chris@1698: throw std::runtime_error("Failed to create temporary file"); Chris@1698: } Chris@1698: if (!empty) { Chris@1698: for (int i = 0; i < 1000; ++i) { Chris@1698: f.write("weeble"); Chris@1698: } Chris@1698: } Chris@1698: f.close(); Chris@1698: Chris@1698: AudioFileReader *reader = Chris@1698: AudioFileReaderFactory::createReader(path, {}); Chris@1709: QCOMPARE((void *)reader, (void *)0); Chris@1698: } Chris@1698: Chris@1698: }; Chris@1698: Chris@1698: #endif