Skip to content

Commit

Permalink
TESTS: Add unit tests for our ZIP file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Feb 25, 2017
1 parent 11bf5c9 commit 993ca83
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,8 @@ 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)

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)
126 changes: 126 additions & 0 deletions tests/common/zipfile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* Phaethon - A FLOSS resource explorer for BioWare's Aurora engine games
*
* Phaethon is the legal property of its developers, whose names
* can be found in the AUTHORS file distributed with this source
* distribution.
*
* Phaethon 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.
*
* Phaethon 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 Phaethon. If not, see <http://www.gnu.org/licenses/>.
*/

/** @file
* Unit tests for our ZIP file reader.
*/

#include "gtest/gtest.h"

#include "src/common/zipfile.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", within a ZIP file
static const byte kDataCompressed[] = {
0x50,0x4B,0x03,0x04,0x14,0x00,0x02,0x00,0x08,0x00,0x4D,0x10,0x2F,0x49,0x53,0xCA,
0xD2,0xAA,0x77,0x01,0x00,0x00,0x6F,0x02,0x00,0x00,0x0E,0x00,0x1C,0x00,0x4F,0x7A,
0x79,0x6D,0x61,0x6E,0x64,0x69,0x61,0x73,0x2E,0x74,0x78,0x74,0x55,0x54,0x09,0x00,
0x03,0x12,0xE5,0xD9,0x57,0x13,0xE5,0xD9,0x57,0x75,0x78,0x0B,0x00,0x01,0x04,0xE8,
0x03,0x00,0x00,0x04,0xE8,0x03,0x00,0x00,0x2D,0x52,0xC9,0x6E,0xA4,0x30,0x10,0xBD,
0xF3,0x15,0x95,0x53,0x2E,0xA8,0x3F,0xA0,0x6F,0x73,0x9B,0x48,0x59,0xA4,0x24,0xD2,
0x9C,0xAB,0x71,0xD1,0x58,0xD8,0x2E,0xC6,0x65,0x40,0xE4,0xEB,0xF3,0x6C,0x5A,0x42,
0x20,0xD5,0xF2,0xB6,0xE2,0x85,0xA2,0x14,0x62,0x2A,0x99,0x37,0x09,0x41,0x32,0x8D,
0x59,0x23,0x71,0xC2,0x53,0xFC,0xFF,0x55,0x28,0x70,0x72,0xDD,0xBF,0x49,0xC9,0xD8,
0xBB,0x2B,0x7D,0xEF,0x4A,0x1B,0x1B,0x76,0x92,0xC3,0xD6,0x9A,0xE6,0x20,0x66,0x14,
0xE4,0x6E,0xA4,0x23,0x59,0xD1,0x24,0xDD,0x57,0xA9,0x5D,0x9F,0xA8,0x4C,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F6,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,0x50,
0x4B,0x01,0x02,0x1E,0x03,0x14,0x00,0x02,0x00,0x08,0x00,0x4D,0x10,0x2F,0x49,0x53,
0xCA,0xD2,0xAA,0x77,0x01,0x00,0x00,0x6F,0x02,0x00,0x00,0x0E,0x00,0x18,0x00,0x00,
0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xA4,0x81,0x00,0x00,0x00,0x00,0x4F,0x7A,0x79,
0x6D,0x61,0x6E,0x64,0x69,0x61,0x73,0x2E,0x74,0x78,0x74,0x55,0x54,0x05,0x00,0x03,
0x12,0xE5,0xD9,0x57,0x75,0x78,0x0B,0x00,0x01,0x04,0xE8,0x03,0x00,0x00,0x04,0xE8,
0x03,0x00,0x00,0x50,0x4B,0x05,0x06,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x54,
0x00,0x00,0x00,0xBF,0x01,0x00,0x00,0x00,0x00
};

GTEST_TEST(ZIPFile, getFiles) {
Common::MemoryReadStream *stream = new Common::MemoryReadStream(kDataCompressed);
const Common::ZipFile zip(stream);

const Common::ZipFile::FileList &files = zip.getFiles();
ASSERT_EQ(files.size(), 1);

EXPECT_EQ(files.begin()->index, 0);
EXPECT_STREQ(files.begin()->name.c_str(), "ozymandias.txt");
}

GTEST_TEST(ZIPFile, getFileSize) {
Common::MemoryReadStream *stream = new Common::MemoryReadStream(kDataCompressed);
const Common::ZipFile zip(stream);

EXPECT_EQ(zip.getFileSize(0), strlen(kDataUncompressed));

EXPECT_THROW(zip.getFileSize(1), Common::Exception);
}

GTEST_TEST(ZIPFile, getFile) {
Common::MemoryReadStream *stream = new Common::MemoryReadStream(kDataCompressed);
const Common::ZipFile zip(stream);

Common::SeekableReadStream *file = zip.getFile(0);
ASSERT_NE(file, static_cast<Common::SeekableReadStream *>(0));

ASSERT_EQ(file->size(), strlen(kDataUncompressed));

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

delete file;
}

GTEST_TEST(ZIPFile, brokenZIP) {
Common::MemoryReadStream *stream = new Common::MemoryReadStream(kDataCompressed, sizeof(kDataCompressed) / 2);

EXPECT_THROW(Common::ZipFile zip(stream), Common::Exception);
}

0 comments on commit 993ca83

Please sign in to comment.