Skip to content

Commit

Permalink
UT cases added for Crypto++ and OpenSSL libraries SHA hashing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pankaj authored and arc-vmware committed Jun 13, 2022
1 parent ab7a83b commit 7de3f2b
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 1 deletion.
4 changes: 4 additions & 0 deletions util/include/cryptopp_digest_creator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class CryptoppDigestCreator : public DigestCreator {
CryptoppDigestCreator();
virtual ~CryptoppDigestCreator();

// Do not allow copying.
CryptoppDigestCreator(const CryptoppDigestCreator&) = delete;
CryptoppDigestCreator& operator=(const CryptoppDigestCreator&) = delete;

void update(const char* data, size_t len) override;
void writeDigest(char* outDigest) override;
size_t digestLength() const override;
Expand Down
1 change: 0 additions & 1 deletion util/include/openssl_digest_creator.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <memory>

#include "digest_creator.hpp"
#include "digest.hpp"
#include "sha_hash.hpp"

namespace concord::util::digest {
Expand Down
8 changes: 8 additions & 0 deletions util/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,11 @@ target_link_libraries(synchronized_value_test GTest::Main util)
add_executable(utilization_test utilization_test.cpp)
add_test(utilization_test utilization_test)
target_link_libraries(utilization_test GTest::Main util)

add_executable(cryptopp_digest_creator_test cryptopp_digest_creator_test.cpp)
add_test(cryptopp_digest_creator_test cryptopp_digest_creator_test)
target_link_libraries(cryptopp_digest_creator_test GTest::Main util)

add_executable(openssl_digest_creator_test openssl_digest_creator_test.cpp)
add_test(openssl_digest_creator_test openssl_digest_creator_test)
target_link_libraries(openssl_digest_creator_test GTest::Main util)
99 changes: 99 additions & 0 deletions util/test/cryptopp_digest_creator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Concord
//
// Copyright (c) 2022 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache 2.0 license (the "License").
// You may not use this product except in compliance with the Apache 2.0
// License.
//
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to the
// terms and conditions of the subcomponent's license, as noted in the LICENSE
// file.
//

#include "gtest/gtest.h"
#include "hex_tools.h"
#include "cryptopp_digest_creator.hpp"

using concord::util::digest::CryptoppDigestCreator;

using DigestGeneratorTest = CryptoppDigestCreator;

// Hashes are generated via https://emn178.github.io/online-tools/sha256.html
struct SHA256_Hashes {
std::string input;
std::string hash;
};
SHA256_Hashes hashes[] = {{"sha256", "5d5b09f6dcb2d53a5fffc60c4ac0d55fabdf556069d6631545f42aa6e3500f2e"},
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{"vmware", "592cc302663c0021ffa92186bf3c1a579a97e5e01f8b28f766855e5b121f8bda"}};

namespace {

TEST(cryptopp_digest_creator_test, test_compute) {
char digest[65]{};

DigestGeneratorTest digest_generator;
const size_t digestLen = digest_generator.digestLength();

ASSERT_TRUE(digest_generator.compute(hashes[0].input.c_str(), hashes[0].input.size(), digest, digestLen));
ASSERT_EQ(0, memcmp(hashes[0].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(digest_generator.compute(hashes[1].input.c_str(), hashes[1].input.size(), digest, digestLen));
ASSERT_EQ(0, memcmp(hashes[1].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(digest_generator.compute(hashes[2].input.c_str(), hashes[2].input.size(), digest, digestLen));
ASSERT_EQ(0, memcmp(hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}

TEST(cryptopp_digest_creator_test, test_update) {
char digest[65]{};
{
DigestGeneratorTest digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(hashes[0].input.data(), hashes[0].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0, memcmp(hashes[0].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(hashes[1].input.data(), hashes[1].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0, memcmp(hashes[1].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(hashes[2].input.data(), hashes[2].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0, memcmp(hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
}

TEST(cryptopp_digest_creator_test, test_multiple_updates) {
char digest[65]{};
DigestGeneratorTest digest_generator;
const size_t digestLen = digest_generator.digestLength();

std::string str = "This is a ";
digest_generator.update(str.data(), str.size());

str = "test string.";
digest_generator.update(str.data(), str.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0,
memcmp("3eec256a587cccf72f71d2342b6dfab0bbca01697c7e7014540bdd62b72120da",
concordUtils::bufferToHex(digest, digestLen, false).data(),
digestLen));
}
} // namespace

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
198 changes: 198 additions & 0 deletions util/test/openssl_digest_creator_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Concord
//
// Copyright (c) 2022 VMware, Inc. All Rights Reserved.
//
// This product is licensed to you under the Apache 2.0 license (the "License").
// You may not use this product except in compliance with the Apache 2.0
// License.
//
// This product may include a number of subcomponents with separate copyright
// notices and license terms. Your use of these subcomponents is subject to the
// terms and conditions of the subcomponent's license, as noted in the LICENSE
// file.
//

#include "gtest/gtest.h"
#include "hex_tools.h"
#include "sha_hash.hpp"
#include "openssl_digest_creator.ipp"

using concord::util::SHA2_256;
using concord::util::SHA3_256;
using concord::util::digest::OpenSSLDigestCreator;

using DigestGeneratorTest_SHA2_256 = OpenSSLDigestCreator<SHA2_256>;
using DigestGeneratorTest_SHA3_256 = OpenSSLDigestCreator<SHA3_256>;

// Hashes are generated via https://emn178.github.io/online-tools/sha256.html
struct SHA_Hashes {
std::string input;
std::string hash;
};
SHA_Hashes sha_256_hashes[] = {{"sha256", "5d5b09f6dcb2d53a5fffc60c4ac0d55fabdf556069d6631545f42aa6e3500f2e"},
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
{"vmware", "592cc302663c0021ffa92186bf3c1a579a97e5e01f8b28f766855e5b121f8bda"}};

SHA_Hashes sha3_256_hashes[] = {{"sha3_256", "5ae36d2b4b37209703fb327119f63354c93b5a35a07e1397aad3285b37910fe9"},
{"", "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a"},
{"vmware", "8c1f524205612f55c39b1f96ca6ebd07262b0b3fd3b25bc90d7b8694024524b9"}};

namespace {

// SHA2_256 tests.
TEST(openssl_digest_creator_test, test_compute_sha2_256) {
char digest[65]{};

DigestGeneratorTest_SHA2_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

ASSERT_TRUE(
digest_generator.compute(sha_256_hashes[0].input.c_str(), sha_256_hashes[0].input.size(), digest, digestLen));
ASSERT_EQ(
0,
memcmp(sha_256_hashes[0].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(
digest_generator.compute(sha_256_hashes[1].input.c_str(), sha_256_hashes[1].input.size(), digest, digestLen));
ASSERT_EQ(
0,
memcmp(sha_256_hashes[1].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(
digest_generator.compute(sha_256_hashes[2].input.c_str(), sha_256_hashes[2].input.size(), digest, digestLen));
ASSERT_EQ(
0, memcmp(sha_256_hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}

TEST(openssl_digest_creator_test, test_update_sha2_256) {
char digest[65]{};
{
DigestGeneratorTest_SHA2_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha_256_hashes[0].input.data(), sha_256_hashes[0].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha_256_hashes[0].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest_SHA2_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha_256_hashes[1].input.data(), sha_256_hashes[1].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha_256_hashes[1].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest_SHA2_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha_256_hashes[2].input.data(), sha_256_hashes[2].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha_256_hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
}

TEST(openssl_digest_creator_test, test_multiple_updates_sha2_256) {
char digest[65]{};
DigestGeneratorTest_SHA2_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

std::string str = "This is a ";
digest_generator.update(str.data(), str.size());

str = "test string.";
digest_generator.update(str.data(), str.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0,
memcmp("3eec256a587cccf72f71d2342b6dfab0bbca01697c7e7014540bdd62b72120da",
concordUtils::bufferToHex(digest, digestLen, false).data(),
digestLen));
}

// SHA3_256 tests.
TEST(openssl_digest_creator_test, test_compute_sha3_256) {
char digest[65]{};

DigestGeneratorTest_SHA3_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

ASSERT_TRUE(
digest_generator.compute(sha3_256_hashes[0].input.c_str(), sha3_256_hashes[0].input.size(), digest, digestLen));
ASSERT_EQ(
0,
memcmp(sha3_256_hashes[0].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(
digest_generator.compute(sha3_256_hashes[1].input.c_str(), sha3_256_hashes[1].input.size(), digest, digestLen));
ASSERT_EQ(
0,
memcmp(sha3_256_hashes[1].hash.c_str(), concordUtils::bufferToHex(digest, digestLen, false).c_str(), digestLen));

ASSERT_TRUE(
digest_generator.compute(sha3_256_hashes[2].input.c_str(), sha3_256_hashes[2].input.size(), digest, digestLen));
ASSERT_EQ(
0, memcmp(sha3_256_hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}

TEST(openssl_digest_creator_test, test_update_sha3_256) {
char digest[65]{};
{
DigestGeneratorTest_SHA3_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha3_256_hashes[0].input.data(), sha3_256_hashes[0].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha3_256_hashes[0].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest_SHA3_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha3_256_hashes[1].input.data(), sha3_256_hashes[1].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha3_256_hashes[1].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
{
DigestGeneratorTest_SHA3_256 digest_generator;
const size_t digestLen = digest_generator.digestLength();

digest_generator.update(sha3_256_hashes[2].input.data(), sha3_256_hashes[2].input.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(
0,
memcmp(sha3_256_hashes[2].hash.data(), concordUtils::bufferToHex(digest, digestLen, false).data(), digestLen));
}
}

TEST(openssl_digest_creator_test, test_multiple_updates_sha3_256) {
char digest[65]{};
DigestGeneratorTest_SHA3_256 digest_generator{};
const size_t digestLen = digest_generator.digestLength();

std::string str = "This is a ";
digest_generator.update(str.data(), str.size());

str = "test string.";
digest_generator.update(str.data(), str.size());
digest_generator.writeDigest(digest);
ASSERT_EQ(0,
memcmp("37b3279f63dc35759dfa667d635f486afdff132d148b23269fb4552eaac5749b",
concordUtils::bufferToHex(digest, digestLen, false).data(),
digestLen));
}
} // namespace

int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 7de3f2b

Please sign in to comment.