Skip to content

Commit

Permalink
TESTS: Add unit tests for our LZMA decompressor
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Dec 23, 2016
1 parent e90fa9f commit c79cd7a
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
134 changes: 134 additions & 0 deletions tests/common/lzma.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* xoreos is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* xoreos 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with xoreos. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Unit tests for our LZMA decompressor (which uses lzma).
*/

#include "gtest/gtest.h"

#include "src/common/lzma.h"
#include "src/common/memreadstream.h"
#include "src/common/error.h"

// Percy Bysshe Shelley's "Ozymandias"
static const byte kDataUncompressed[] =
"I met a traveller from an antique land\n"
"Who said: Two vast and trunkless legs of stone\n"
"Stand in the desert. Near them, on the sand,\n"
"Half sunk, a shattered visage lies, whose frown,\n"
"And wrinkled lip, and sneer of cold command,\n"
"Tell that its sculptor well those passions read\n"
"Which yet survive, stamped on these lifeless things,\n"
"The hand that mocked them and the heart that fed:\n"
"And on the pedestal these words appear:\n"
"\'My name is Ozymandias, king of kings:\n"
"Look on my works, ye Mighty, and despair!\'\n"
"Nothing beside remains. Round the decay\n"
"Of that colossal wreck, boundless and bare\n"
"The lone and level sands stretch far away.";

// Percy Bysshe Shelley's "Ozymandias", compressed using LZMA
static const byte kDataCompressed[] = {
0x5D,0x00,0x00,0x00,0x01,0x00,0x24,0x88,0x09,0xA6,0x53,0xD8,0x61,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0x38,0x9F,0xBF,0x79,0x81,0x0B,0x3A,0x7B,
0xA1,0x55,0xF2,0xF5,0xF6,0x7E,0xA5,0x47,0x34,0xAF,0x22,0x82,0x9A,0xFF,0xB1,0x93,
0xCF,0x47,0x98,0x63,0xF4,0x11,0xC8,0xD0,0x48,0x3F,0xC5,0xC9,0x1E,0xAD,0x4F,0x88,
0xBF,0x57,0x40,0xB0,0x7E,0xA2,0xB5,0xC8,0xA7,0x0B,0x64,0x83,0xD7,0xAB,0x8A,0x33,
0xA6,0x64,0xEA,0x2B,0xCF,0x41,0x96,0x92,0xF5,0x7B,0x66,0x48,0xA3,0x53,0x9D,0x01,
0x4F,0xC3,0xDF,0xA3,0x85,0x54,0x45,0x65,0xA9,0x3C,0x20,0x31,0x02,0x55,0xDB,0x64,
0x33,0x50,0x19,0x7A,0x58,0x64,0x87,0x72,0xF6,0x12,0x05,0xA3,0x83,0xFC,0xB4,0x0E,
0x28,0x5B,0x5C,0x17,0x57,0xB3,0xD8,0xF7,0xBE,0x1D,0xDF,0x96,0x32,0x36,0xA0,0xFE,
0x51,0x56,0x2D,0x84,0x2B,0xCA,0x2B,0x85,0x53,0x71,0xC1,0x33,0x3B,0xD2,0x77,0x2F,
0xB3,0x9E,0x87,0x71,0x8A,0x01,0x94,0x26,0x53,0x11,0x73,0x21,0xB5,0xD4,0x15,0xFB,
0xAC,0xC3,0xE7,0xA5,0x0A,0x09,0xF4,0x36,0x5B,0x88,0x25,0x51,0x0C,0x12,0xB5,0x09,
0x8A,0x78,0x57,0x5A,0xCC,0x20,0x13,0xC3,0xFD,0xC2,0x1E,0xF9,0xA6,0xF4,0xA1,0x77,
0xB2,0xAD,0xD3,0x6B,0xEF,0xB9,0xF7,0xA1,0x28,0x8A,0xB4,0x3D,0xCE,0x54,0xDA,0x78,
0xFF,0xF8,0x6E,0xCB,0x5F
};

GTEST_TEST(LZMA1, decompressBuf) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = sizeof(kDataUncompressed) - 1; // Ignore the \0

const byte *decompressed =
Common::decompressLZMA1(kDataCompressed, kSizeCompressed, kSizeDecompressed);
ASSERT_NE(decompressed, static_cast<const byte *>(0));

for (size_t i = 0; i < kSizeDecompressed; i++)
EXPECT_EQ(decompressed[i], kDataUncompressed[i]) << "At index " << i;

delete[] decompressed;
}

GTEST_TEST(LZMA1, decompressStream) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = sizeof(kDataUncompressed) - 1; // Ignore the \0

Common::MemoryReadStream compressed(kDataCompressed, kSizeCompressed);

Common::SeekableReadStream *decompressed =
Common::decompressLZMA1(compressed, kSizeCompressed, kSizeDecompressed);
ASSERT_NE(decompressed, static_cast<Common::SeekableReadStream *>(0));

ASSERT_EQ(decompressed->size(), kSizeDecompressed);

for (size_t i = 0; i < kSizeDecompressed; i++)
EXPECT_EQ(decompressed->readByte(), kDataUncompressed[i]) << "At index " << i;

delete decompressed;
}

GTEST_TEST(LZMA1, decompressFailOutputSmall) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = sizeof(kDataUncompressed) / 2;

EXPECT_THROW(Common::decompressLZMA1(kDataCompressed, kSizeCompressed, kSizeDecompressed),
Common::Exception);
}

GTEST_TEST(LZMA1, decompressFailOutputBig) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = sizeof(kDataUncompressed) * 2;

EXPECT_THROW(Common::decompressLZMA1(kDataCompressed, kSizeCompressed, kSizeDecompressed),
Common::Exception);
}

GTEST_TEST(LZMA1, decompressFailInputCut) {
static const size_t kSizeCompressed = sizeof(kDataCompressed) / 2;
static const size_t kSizeDecompressed = sizeof(kDataUncompressed) - 1; // Ignore the \0

EXPECT_THROW(Common::decompressLZMA1(kDataCompressed, kSizeCompressed, kSizeDecompressed),
Common::Exception);
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,8 @@ check_PROGRAMS += tests/common/test_zipfile
tests_common_test_zipfile_SOURCES = tests/common/zipfile.cpp
tests_common_test_zipfile_LDADD = $(common_LIBS)
tests_common_test_zipfile_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_lzma
tests_common_test_lzma_SOURCES = tests/common/lzma.cpp
tests_common_test_lzma_LDADD = $(common_LIBS)
tests_common_test_lzma_CXXFLAGS = $(test_CXXFLAGS)

0 comments on commit c79cd7a

Please sign in to comment.