From b149c1c4cac3fd110f132945b67e30b5212b094b Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 23 May 2018 01:01:48 +0100 Subject: [PATCH] Add unit tests for SHA1 hashing --- test/tests/CMakeLists.txt | 5 ++++ test/tests/HashTests.cpp | 55 +++++++++++++++++++++++++++++++++++++++ test/tests/tests.vcxproj | 1 + 3 files changed, 61 insertions(+) create mode 100644 test/tests/HashTests.cpp diff --git a/test/tests/CMakeLists.txt b/test/tests/CMakeLists.txt index e7cb287a24..07f5951efc 100644 --- a/test/tests/CMakeLists.txt +++ b/test/tests/CMakeLists.txt @@ -153,6 +153,11 @@ add_executable(test_localisation ${STRING_TEST_SOURCES}) target_link_libraries(test_localisation ${GTEST_LIBRARIES} test-common ${LDL} z) add_test(NAME localisation COMMAND test_localisation) +# Hash tests +add_executable(test_hash "${CMAKE_CURRENT_LIST_DIR}/HashTests.cpp") +target_link_libraries(test_hash ${GTEST_LIBRARIES} libopenrct2) +add_test(NAME Hash COMMAND test_hash) + # ImageImporter tests add_executable(test_imageimporter "${CMAKE_CURRENT_LIST_DIR}/ImageImporterTests.cpp" "${CMAKE_CURRENT_LIST_DIR}/TestData.cpp") diff --git a/test/tests/HashTests.cpp b/test/tests/HashTests.cpp new file mode 100644 index 0000000000..afd5143379 --- /dev/null +++ b/test/tests/HashTests.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +class HashTests : public testing::Test +{ +public: + template + void AssertHash(std::string expected, T hash) + { + auto actual = StringToHex(hash); + ASSERT_EQ(expected, actual); + } + + template + std::string StringToHex(T input) + { + std::string result; + result.reserve(input.size() * 2); + for (auto b : input) + { + static_assert(sizeof(b) == 1); + + char buf[3]; + snprintf(buf, 3, "%02x", b); + result.append(buf); + } + return result; + } +}; + +TEST_F(HashTests, SHA1_Basic) +{ + std::string input = "The quick brown fox jumped over the lazy dog."; + auto result = Hash::SHA1(input.data(), input.size()); + AssertHash("c0854fb9fb03c41cce3802cb0d220529e6eef94e", result); +} + +TEST_F(HashTests, SHA1_Multiple) +{ + std::string input[] = { + "Merry-go-round 2 looks too intense for me", + "This park is really clean and tidy", + "This balloon from Balloon Stall 1 is really good value" + }; + + auto alg = Hash::CreateSHA1(); + for (auto s : input) + { + alg->Update(s.data(), s.size()); + } + auto result = alg->Finish(); + + AssertHash("758a238d9a4748f80cc81f12be3885d5e45d34c2", result); +} diff --git a/test/tests/tests.vcxproj b/test/tests/tests.vcxproj index 8b582769ac..beee5a9bac 100644 --- a/test/tests/tests.vcxproj +++ b/test/tests/tests.vcxproj @@ -56,6 +56,7 @@ +