From 51765a1bd63f95661b217ea598125306ec169b5b Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Mon, 26 Dec 2016 04:43:46 +0100 Subject: [PATCH] Added hash function tests. --- include/MUtils/Hash.h | 2 +- test/MUtilitiesTest_VS2013.vcxpro.filters | 3 + test/MUtilitiesTest_VS2013.vcxproj | 1 + test/MUtilitiesTest_VS2015.vcxproj | 1 + test/MUtilitiesTest_VS2015.vcxproj.filters | 3 + test/src/HashTest.cpp | 111 +++++++++++++++++++++++++++++ 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 test/src/HashTest.cpp diff --git a/include/MUtils/Hash.h b/include/MUtils/Hash.h index dac0cba..2bf4ce6 100644 --- a/include/MUtils/Hash.h +++ b/include/MUtils/Hash.h @@ -122,7 +122,7 @@ namespace MUtils * \param hashId Specifies the desired hash function. This must be a valid hash algorithm identifier, as defined in the `Hash.h` header file. * * \param key Specifies on optional key that is used to "seed" the hash function. If a key is given, it must be a NULL-terminated string of appropriate length. If set to `NULL`, the optional seeding is skipped. - + * * \return Returns a pointer to a new MUtils::Hash::Hash object that implements the desired hash function. The function throws if an invalid algorithm identifier was specified! */ MUTILS_API Hash *create(const quint16 &hashId, const char *const key = NULL); diff --git a/test/MUtilitiesTest_VS2013.vcxpro.filters b/test/MUtilitiesTest_VS2013.vcxpro.filters index fd182b9..f961ac5 100644 --- a/test/MUtilitiesTest_VS2013.vcxpro.filters +++ b/test/MUtilitiesTest_VS2013.vcxpro.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + diff --git a/test/MUtilitiesTest_VS2013.vcxproj b/test/MUtilitiesTest_VS2013.vcxproj index d454cc8..8d98530 100644 --- a/test/MUtilitiesTest_VS2013.vcxproj +++ b/test/MUtilitiesTest_VS2013.vcxproj @@ -16,6 +16,7 @@ + diff --git a/test/MUtilitiesTest_VS2015.vcxproj b/test/MUtilitiesTest_VS2015.vcxproj index 05cba54..45c6b3a 100644 --- a/test/MUtilitiesTest_VS2015.vcxproj +++ b/test/MUtilitiesTest_VS2015.vcxproj @@ -16,6 +16,7 @@ + diff --git a/test/MUtilitiesTest_VS2015.vcxproj.filters b/test/MUtilitiesTest_VS2015.vcxproj.filters index fd182b9..f961ac5 100644 --- a/test/MUtilitiesTest_VS2015.vcxproj.filters +++ b/test/MUtilitiesTest_VS2015.vcxproj.filters @@ -21,6 +21,9 @@ Source Files + + Source Files + diff --git a/test/src/HashTest.cpp b/test/src/HashTest.cpp new file mode 100644 index 0000000..5de1aef --- /dev/null +++ b/test/src/HashTest.cpp @@ -0,0 +1,111 @@ +/////////////////////////////////////////////////////////////////////////////// +// MuldeR's Utilities for Qt +// Copyright (C) 2004-2016 LoRd_MuldeR +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +// +// http://www.gnu.org/licenses/lgpl-2.1.txt +////////////////////////////////////////////////////////////////////////////////// + +#include "MUtilsTest.h" + +//MUtils +#include + +//Qt +#include + +//=========================================================================== +// TESTBED CLASS +//=========================================================================== + +class HashTest : public Testbed +{ +protected: + virtual void SetUp() + { + } + + virtual void TearDown() + { + } +}; + +//=========================================================================== +// TEST METHODS +//=========================================================================== + +static const char *const TEST_MESSAGE_ORG = "The quick brown fox jumps over the lazy dog"; +static const char *const TEST_MESSAGE_ALT = "The quick brown fox jumps over the lazy fog"; + +static const char* const SEED_KEY = "S73_iT6BTdgNc?kL"; + +#define TEST_HASH_DIRECT(ID, INPUT, DIGEST) do \ +{ \ + QScopedPointer test_1(MUtils::Hash::create(MUtils::Hash::HASH_##ID)); \ + QScopedPointer test_2(MUtils::Hash::create(MUtils::Hash::HASH_##ID, SEED_KEY)); \ + test_1->update(QByteArray(INPUT)); \ + test_2->update(QByteArray(INPUT)); \ + const QByteArray result_1 = test_1->digest(); \ + const QByteArray result_2 = test_2->digest(); \ + ASSERT_STRCASENE(result_1.constData(), result_2.constData()); \ + ASSERT_STRCASEEQ(result_1.constData(), (DIGEST)); \ +} \ +while(0) + +//----------------------------------------------------------------- +// Keccak +//----------------------------------------------------------------- + +TEST_F(HashTest, TestKeccak224) +{ + TEST_HASH_DIRECT(KECCAK_224, "", "f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd"); + TEST_HASH_DIRECT(KECCAK_224, TEST_MESSAGE_ORG, "310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe"); + TEST_HASH_DIRECT(KECCAK_224, TEST_MESSAGE_ALT, "6db4cb22fe56606394880043ce4917a1803ad73b2e2b782768ea713b"); +} + +TEST_F(HashTest, TestKeccak256) +{ + TEST_HASH_DIRECT(KECCAK_256, "", "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); + TEST_HASH_DIRECT(KECCAK_256, TEST_MESSAGE_ORG, "4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15"); + TEST_HASH_DIRECT(KECCAK_256, TEST_MESSAGE_ALT, "4cc2957d93a4a88251a40c48a42225364157567bc81d4aec9389ee7065c042d4"); +} + +TEST_F(HashTest, TestKeccak384) +{ + TEST_HASH_DIRECT(KECCAK_384, "", "2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff"); + TEST_HASH_DIRECT(KECCAK_384, TEST_MESSAGE_ORG, "283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3"); + TEST_HASH_DIRECT(KECCAK_384, TEST_MESSAGE_ALT, "4e16433a5e0cdc2d88a21714a040c92999230a1c3d165a4e9670702b2df26f3587b4d721b6faaf348228f616dd921578"); +} + +TEST_F(HashTest, TestKeccak512) +{ + TEST_HASH_DIRECT(KECCAK_512, "", "0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e"); + TEST_HASH_DIRECT(KECCAK_512, TEST_MESSAGE_ORG, "d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609"); + TEST_HASH_DIRECT(KECCAK_512, TEST_MESSAGE_ALT, "0b46f421465ec602262e0a1044e59b36fbdb5f63f84e712963d2bc61bcb46ab0ebf86e59c14c253717ea558929c251695663226ffa5660ff7a29a5acbdaea901"); +} + +//----------------------------------------------------------------- +// BLAKE2 +//----------------------------------------------------------------- + +TEST_F(HashTest, TestBlake2) +{ + TEST_HASH_DIRECT(BLAKE2_512, "", "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"); + TEST_HASH_DIRECT(BLAKE2_512, TEST_MESSAGE_ORG, "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918"); + TEST_HASH_DIRECT(BLAKE2_512, TEST_MESSAGE_ALT, "a5b8a16391f8e34e16901fc2fd5754523b0c95354c2f22d3efc327c53070504ea062e219c502561f77a4933c18d36633e5f3ecf1f11506159f4b1875abb767c1"); +} + +#undef TEST_HASH_DIRECT -- 2.11.4.GIT