Skip to content

Commit

Permalink
TESTS: Add unit tests for our DEFLATE decompressor
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Feb 25, 2017
1 parent 645f703 commit 0a1caa2
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
133 changes: 133 additions & 0 deletions tests/common/deflate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* 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 DEFLATE decompressor (which uses zlib).
*/

#include "gtest/gtest.h"

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

// Percy Bysshe Shelley's "Ozymandias"
static const char *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 DEFLATE
static const byte kDataCompressed[] = {
0x2D,0x52,0xC9,0x6E,0xA4,0x30,0x10,0xBD,0xF3,0x15,0x95,0x53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C3,0x2C,0xAE,0x59,0x3D,0x53,0xAA,0x5D,0x98,0x2F,
0x67,0x7B,0x14,0x77,0x6D,0x1E,0x1E,0x29,0x00,0x5A,0x40,0x11,0x1E,0xE0,0xBB,0x66,
0x67,0xC4,0xCB,0x82,0x8D,0x6B,0xF7,0xFC,0x76,0x50,0xE2,0x28,0xE4,0x8D,0x3E,0x7E,
0x8E,0xEA,0xC9,0x33,0x02,0x99,0x41,0x5C,0xCD,0xD6,0xAF,0x5D,0xBB,0x57,0xD5,0xB9,
0xE2,0xC5,0xA3,0xEE,0xCF,0x18,0x38,0x84,0xDE,0xFC,0x7D,0x2A,0xC7,0x99,0x0E,0x28,
0x16,0xF6,0xF9,0xE9,0xB9,0x7B,0xD7,0xA6,0x9A,0x6E,0x62,0xDE,0x09,0x4C,0x47,0xF6,
0xC9,0x2E,0xF4,0xA9,0xEB,0x43,0xAB,0x93,0x81,0x8F,0xEE,0x63,0x3C,0xE5,0x22,0x4E,
0x35,0x83,0xBC,0x3D,0xCB,0x80,0x1B,0xDD,0xEA,0x5C,0x73,0x5F,0x71,0x6F,0x9C,0xA5,
0xD9,0x0F,0xB8,0x7B,0xAB,0x04,0xC1,0x8F,0xD4,0x4E,0x8B,0xB8,0x4B,0x96,0x82,0x38,
0x47,0x5C,0x9E,0x77,0x3E,0x2E,0xBF
};

GTEST_TEST(DEFLATE, decompressBuf) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = strlen(kDataUncompressed);

const byte *decompressed =
Common::decompressDeflate(kDataCompressed, kSizeCompressed, kSizeDecompressed, Common::kWindowBitsMaxRaw);
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(DEFLATE, decompressStream) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = strlen(kDataUncompressed);

Common::MemoryReadStream compressed(kDataCompressed);

Common::SeekableReadStream *decompressed =
Common::decompressDeflate(compressed, kSizeCompressed, kSizeDecompressed, Common::kWindowBitsMaxRaw);
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(DEFLATE, decompressFailOutputSmall) {
static const size_t kSizeCompressed = sizeof(kDataCompressed);
static const size_t kSizeDecompressed = strlen(kDataUncompressed) / 2;

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

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

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

GTEST_TEST(DEFLATE, decompressFailInputCut) {
static const size_t kSizeCompressed = sizeof(kDataCompressed) / 2;
static const size_t kSizeDecompressed = strlen(kDataUncompressed);

EXPECT_THROW(Common::decompressDeflate(kDataCompressed, kSizeCompressed,
kSizeDecompressed, Common::kWindowBitsMaxRaw),
Common::Exception);
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,8 @@ check_PROGRAMS += tests/common/test_md5
tests_common_test_md5_SOURCES = tests/common/md5.cpp
tests_common_test_md5_LDADD = $(common_LIBS)
tests_common_test_md5_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_deflate
tests_common_test_deflate_SOURCES = tests/common/deflate.cpp
tests_common_test_deflate_LDADD = $(common_LIBS)
tests_common_test_deflate_CXXFLAGS = $(test_CXXFLAGS)

0 comments on commit 0a1caa2

Please sign in to comment.