From 1fb857985d4715673954e9abae1fe8d1da5ac670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 9 Jan 2012 19:13:27 +0100 Subject: [PATCH] TGAImporter: hardened file opening, added unit tests. --- src/Plugins/TGAImporter/CMakeLists.txt | 7 ++ src/Plugins/TGAImporter/TGAImporter.cpp | 10 ++ src/Plugins/TGAImporter/Test/CMakeLists.txt | 2 + .../TGAImporter/Test/TGAImporterTest.cpp | 98 +++++++++++++++++++ .../TGAImporter/Test/TGAImporterTest.h | 35 +++++++ 5 files changed, 152 insertions(+) create mode 100644 src/Plugins/TGAImporter/Test/CMakeLists.txt create mode 100644 src/Plugins/TGAImporter/Test/TGAImporterTest.cpp create mode 100644 src/Plugins/TGAImporter/Test/TGAImporterTest.h diff --git a/src/Plugins/TGAImporter/CMakeLists.txt b/src/Plugins/TGAImporter/CMakeLists.txt index 79482c1ca..e69d1f923 100644 --- a/src/Plugins/TGAImporter/CMakeLists.txt +++ b/src/Plugins/TGAImporter/CMakeLists.txt @@ -1,2 +1,9 @@ corrade_add_plugin(TGAImporter ${MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR} TGAImporter.conf TGAImporter.cpp) target_link_libraries(TGAImporter ${MAGNUM_LIBRARY}) + +if(BUILD_TESTS) + add_library(TGAImporterTestLib TGAImporter.cpp) + target_link_libraries(TGAImporterTestLib ${MAGNUM_LIBRARY} ${CORRADE_UTILITY_LIBRARY} ${CORRADE_PLUGINMANAGER_LIBRARY}) + enable_testing() + add_subdirectory(Test) +endif() diff --git a/src/Plugins/TGAImporter/TGAImporter.cpp b/src/Plugins/TGAImporter/TGAImporter.cpp index 315540565..b73434062 100644 --- a/src/Plugins/TGAImporter/TGAImporter.cpp +++ b/src/Plugins/TGAImporter/TGAImporter.cpp @@ -39,6 +39,16 @@ bool TGAImporter::open(std::istream& in) { header.width = Endianness::littleEndian(header.width); header.height = Endianness::littleEndian(header.height); + if(header.colorMapType != 0) { + Error() << "TGAImporter: paletted files are not supported"; + return false; + } + + if(header.imageType != 2) { + Error() << "TGAImporter: non-RGB files are not supported"; + return false; + } + ColorFormat colorFormat; switch(header.bpp) { case 24: diff --git a/src/Plugins/TGAImporter/Test/CMakeLists.txt b/src/Plugins/TGAImporter/Test/CMakeLists.txt new file mode 100644 index 000000000..791ea97db --- /dev/null +++ b/src/Plugins/TGAImporter/Test/CMakeLists.txt @@ -0,0 +1,2 @@ +corrade_add_test(TGAImporterTest TGAImporterTest.h TGAImporterTest.cpp) +target_link_libraries(TGAImporterTest TGAImporterTestLib) diff --git a/src/Plugins/TGAImporter/Test/TGAImporterTest.cpp b/src/Plugins/TGAImporter/Test/TGAImporterTest.cpp new file mode 100644 index 000000000..809dc1623 --- /dev/null +++ b/src/Plugins/TGAImporter/Test/TGAImporterTest.cpp @@ -0,0 +1,98 @@ +/* + Copyright © 2010, 2011 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum 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 version 3 for more details. +*/ + +#include "TGAImporterTest.h" + +#include + +#include "Utility/Debug.h" +#include "../TGAImporter.h" + +using namespace std; +using namespace Corrade::Utility; + +QTEST_APPLESS_MAIN(Magnum::Plugins::TGAImporter::Test::TGAImporterTest) + +namespace Magnum { namespace Plugins { namespace TGAImporter { namespace Test { + +void TGAImporterTest::paletted() { + const char data[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + istringstream in(string(data, sizeof(data))); + + ostringstream debug; + Error::setOutput(&debug); + + TGAImporter importer; + QVERIFY(!importer.open(in)); + QCOMPARE(debug.str().c_str(), "TGAImporter: paletted files are not supported\n"); +} + +void TGAImporterTest::nonRgb() { + const char data[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + istringstream in(string(data, sizeof(data))); + + ostringstream debug; + Error::setOutput(&debug); + + TGAImporter importer; + QVERIFY(!importer.open(in)); + QCOMPARE(debug.str().c_str(), "TGAImporter: non-RGB files are not supported\n"); +} + +void TGAImporterTest::bits16() { + const char data[] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0 }; + istringstream in(string(data, sizeof(data))); + + ostringstream debug; + Error::setOutput(&debug); + + TGAImporter importer; + QVERIFY(!importer.open(in)); + QCOMPARE(debug.str().c_str(), "TGAImporter: unsupported bits-per-pixel: 16\n"); +} + +void TGAImporterTest::bits24() { + const char data[] = { + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 24, 0, + 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6, 7, 6, 7, 8 + }; + std::istringstream in(string(data, sizeof(data))); + + TGAImporter importer; + QVERIFY(importer.open(in)); + auto image = importer.image2D(0); + QVERIFY(image->colorFormat() == ColorFormat::BGR); + QVERIFY(image->dimensions() == Math::Vector2(2, 3)); + QVERIFY(image->type() == TypeTraits::glType()); + QVERIFY(string(static_cast(image->data())) == string(data + 18, 2*3*3)); +} + +void TGAImporterTest::bits32() { + const char data[] = { + 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 3, 0, 32, 0, + 1, 2, 3, 1, 2, 3, 4, 1, 3, 4, 5, 1, 4, 5, 6, 1, 5, 6, 7, 1, 6, 7, 8, 1 + }; + std::istringstream in(string(data, sizeof(data))); + + TGAImporter importer; + QVERIFY(importer.open(in)); + auto image = importer.image2D(0); + QVERIFY(image->colorFormat() == ColorFormat::BGRA); + QVERIFY(image->dimensions() == Math::Vector2(2, 3)); + QVERIFY(image->type() == TypeTraits::glType()); + QVERIFY(string(static_cast(image->data()), 2*3*3) == string(data + 18, 2*3*3)); +} + +}}}} diff --git a/src/Plugins/TGAImporter/Test/TGAImporterTest.h b/src/Plugins/TGAImporter/Test/TGAImporterTest.h new file mode 100644 index 000000000..5e912bfee --- /dev/null +++ b/src/Plugins/TGAImporter/Test/TGAImporterTest.h @@ -0,0 +1,35 @@ +#ifndef Magnum_Plugins_TGAImporter_Test_TGAImporterTest_h +#define Magnum_Plugins_TGAImporter_Test_TGAImporterTest_h +/* + Copyright © 2010, 2011 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum 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 version 3 for more details. +*/ + +#include + +namespace Magnum { namespace Plugins { namespace TGAImporter { namespace Test { + +class TGAImporterTest: public QObject { + Q_OBJECT + + private slots: + void paletted(); + void nonRgb(); + void bits16(); + void bits24(); + void bits32(); +}; + +}}}} + +#endif