Skip to content

Commit

Permalink
TESTS: Add unit tests for our MD5 digest implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Dec 23, 2016
1 parent b73ca59 commit a391013
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
122 changes: 122 additions & 0 deletions tests/common/md5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* 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 MD5 digest implementation.
*/

#include <cstring>

#include <vector>

#include "gtest/gtest.h"

#include "src/common/md5.h"
#include "src/common/ustring.h"
#include "src/common/memreadstream.h"

static const char *kString = "Foobar";
static const byte kData[] = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

static const byte kDigestString[] = { 0x89, 0xD5, 0x73, 0x9B, 0xAA, 0xBB, 0xBE, 0x65,
0xBE, 0x35, 0xCB, 0xE6, 0x1C, 0x88, 0xE0, 0x6D };
static const byte kDigestData [] = { 0x79, 0x9D, 0x4F, 0x0B, 0xEC, 0x27, 0xAF, 0xF8,
0x9D, 0xE3, 0x0B, 0x35, 0xE3, 0xCA, 0x43, 0x32 };

static void createVector(std::vector<byte> &v, const byte *data, size_t n) {
v.resize(n);

memcpy(&v[0], data, n);
}

static void compareData(const std::vector<byte> &v, const byte *data, size_t n) {
ASSERT_EQ(v.size(), n);

for (size_t i = 0; i < n; i++)
EXPECT_EQ(v[i], data[i]) << "At index " << i;
}

GTEST_TEST(MD5, hashString) {
std::vector<byte> digest;

Common::hashMD5(kString, digest);

compareData(digest, kDigestString, sizeof(kDigestString));
}

GTEST_TEST(MD5, hashData) {
std::vector<byte> digest;

Common::hashMD5(kData, sizeof(kData), digest);

compareData(digest, kDigestData, sizeof(kDigestData));
}

GTEST_TEST(MD5, hashStream) {
std::vector<byte> digest;

Common::MemoryReadStream stream(kData, sizeof(kData));
Common::hashMD5(stream, digest);

compareData(digest, kDigestData, sizeof(kDigestData));
}

GTEST_TEST(MD5, hashVector) {
std::vector<byte> digest;

std::vector<byte> data;
createVector(data, kData, sizeof(kData));

Common::hashMD5(data, digest);

compareData(digest, kDigestData, sizeof(kDigestData));
}

GTEST_TEST(MD5, compareString) {
std::vector<byte> digest;
createVector(digest, kDigestString, sizeof(kDigestString));

EXPECT_TRUE(Common::compareMD5Digest(kString, digest));
}

GTEST_TEST(MD5, compareData) {
std::vector<byte> digest;
createVector(digest, kDigestData, sizeof(kDigestData));

EXPECT_TRUE(Common::compareMD5Digest(kData, sizeof(kData), digest));
}

GTEST_TEST(MD5, compareStream) {
std::vector<byte> digest;
createVector(digest, kDigestData, sizeof(kDigestData));

Common::MemoryReadStream stream(kData, sizeof(kData));
EXPECT_TRUE(Common::compareMD5Digest(stream, digest));
}

GTEST_TEST(MD5, compareVector) {
std::vector<byte> digest;
createVector(digest, kDigestData, sizeof(kDigestData));

std::vector<byte> data;
createVector(data, kData, sizeof(kData));

EXPECT_TRUE(Common::compareMD5Digest(data, digest));
}
5 changes: 5 additions & 0 deletions tests/common/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,8 @@ check_PROGRAMS += tests/common/test_hash
tests_common_test_hash_SOURCES = tests/common/hash.cpp
tests_common_test_hash_LDADD = $(common_LIBS)
tests_common_test_hash_CXXFLAGS = $(test_CXXFLAGS)

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)

0 comments on commit a391013

Please sign in to comment.