Skip to content

Commit

Permalink
TESTS: Add unit tests for our Blowfish implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Jan 1, 2017
1 parent 3db2a60 commit 1f39b36
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
83 changes: 83 additions & 0 deletions tests/common/blowfish.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* 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 Blowfish implementation.
*/

#include <vector>

#include "gtest/gtest.h"

#include "src/common/util.h"
#include "src/common/error.h"
#include "src/common/memreadstream.h"
#include "src/common/blowfish.h"

static const byte kClearText[] = { 'F', 'o', 'o', 'b', 'a', 'r', ' ', 'B', 'a', 'r', 'f', 'o', 'o' };
static const byte kKey[] = { 'T', 'h', 'i', 's', ' ', 'a', ' ', 'K', 'e', 'y', '!' };
static const byte kCypherText[] = { 0x2B, 0x9B, 0x10, 0x5F, 0xF2, 0x5B, 0x58, 0xB6,
0x20, 0xA8, 0xC2, 0xF8, 0x98, 0x1E, 0xA4, 0xE8 };

static void createKey(std::vector<byte> &key) {
key.resize(ARRAYSIZE(kKey));

for (size_t i = 0; i < ARRAYSIZE(kKey); i++)
key[i] = kKey[i];
}

GTEST_TEST(Blowfish, encrypt) {
Common::MemoryReadStream clearText(kClearText);

std::vector<byte> key;
createKey(key);

Common::MemoryReadStream *cipherText = Common::encryptBlowfishEBC(clearText, key);
ASSERT_EQ(cipherText->size(), ARRAYSIZE(kCypherText));

for (size_t i = 0; i < ARRAYSIZE(kCypherText); i++)
EXPECT_EQ(cipherText->readByte(), kCypherText[i]) << "At index " << i;

delete cipherText;
}

GTEST_TEST(Blowfish, decrypt) {
Common::MemoryReadStream cipherText(kCypherText);

std::vector<byte> key;
createKey(key);

Common::MemoryReadStream *clearText = Common::decryptBlowfishEBC(cipherText, key);
ASSERT_GE(clearText->size(), ARRAYSIZE(kClearText));

for (size_t i = 0; i < ARRAYSIZE(kClearText); i++)
EXPECT_EQ(clearText->readByte(), kClearText[i]) << "At index " << i;

delete clearText;
}

GTEST_TEST(Blowfish, misalign) {
Common::MemoryReadStream cipherText(kCypherText, 7);

std::vector<byte> key;
createKey(key);

EXPECT_THROW(Common::decryptBlowfishEBC(cipherText, key), Common::Exception);
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,8 @@ check_PROGRAMS += tests/common/test_huffman
tests_common_test_huffman_SOURCES = tests/common/huffman.cpp
tests_common_test_huffman_LDADD = $(common_LIBS)
tests_common_test_huffman_CXXFLAGS = $(test_CXXFLAGS)

check_PROGRAMS += tests/common/test_blowfish
tests_common_test_blowfish_SOURCES = tests/common/blowfish.cpp
tests_common_test_blowfish_LDADD = $(common_LIBS)
tests_common_test_blowfish_CXXFLAGS = $(test_CXXFLAGS)

0 comments on commit 1f39b36

Please sign in to comment.