Browse Source

TGAImporter: hardened file opening, added unit tests.

pull/34/head
Vladimír Vondruš 15 years ago
parent
commit
1fb857985d
  1. 7
      src/Plugins/TGAImporter/CMakeLists.txt
  2. 10
      src/Plugins/TGAImporter/TGAImporter.cpp
  3. 2
      src/Plugins/TGAImporter/Test/CMakeLists.txt
  4. 98
      src/Plugins/TGAImporter/Test/TGAImporterTest.cpp
  5. 35
      src/Plugins/TGAImporter/Test/TGAImporterTest.h

7
src/Plugins/TGAImporter/CMakeLists.txt

@ -1,2 +1,9 @@
corrade_add_plugin(TGAImporter ${MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR} TGAImporter.conf TGAImporter.cpp) corrade_add_plugin(TGAImporter ${MAGNUM_PLUGINS_IMPORTER_INSTALL_DIR} TGAImporter.conf TGAImporter.cpp)
target_link_libraries(TGAImporter ${MAGNUM_LIBRARY}) 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()

10
src/Plugins/TGAImporter/TGAImporter.cpp

@ -39,6 +39,16 @@ bool TGAImporter::open(std::istream& in) {
header.width = Endianness::littleEndian<unsigned short>(header.width); header.width = Endianness::littleEndian<unsigned short>(header.width);
header.height = Endianness::littleEndian<unsigned short>(header.height); header.height = Endianness::littleEndian<unsigned short>(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; ColorFormat colorFormat;
switch(header.bpp) { switch(header.bpp) {
case 24: case 24:

2
src/Plugins/TGAImporter/Test/CMakeLists.txt

@ -0,0 +1,2 @@
corrade_add_test(TGAImporterTest TGAImporterTest.h TGAImporterTest.cpp)
target_link_libraries(TGAImporterTest TGAImporterTestLib)

98
src/Plugins/TGAImporter/Test/TGAImporterTest.cpp

@ -0,0 +1,98 @@
/*
Copyright © 2010, 2011 Vladimír Vondruš <mosra@centrum.cz>
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 <QtTest/QTest>
#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<GLsizei>(2, 3));
QVERIFY(image->type() == TypeTraits<GLubyte>::glType());
QVERIFY(string(static_cast<const char*>(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<GLsizei>(2, 3));
QVERIFY(image->type() == TypeTraits<GLubyte>::glType());
QVERIFY(string(static_cast<const char*>(image->data()), 2*3*3) == string(data + 18, 2*3*3));
}
}}}}

35
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š <mosra@centrum.cz>
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 <QtCore/QObject>
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
Loading…
Cancel
Save