From a67ab1db52071b6b7919afa1539fd32f7d56be89 Mon Sep 17 00:00:00 2001 From: An Tao Date: Mon, 9 Dec 2019 22:16:09 +0800 Subject: [PATCH] Add some unit tests (#311) --- lib/src/ssl_funcs/Md5.cc | 30 +++++++++++++++--------------- lib/src/ssl_funcs/Md5.h | 12 ++++++------ lib/tests/CMakeLists.txt | 4 ---- lib/tests/Md5Test.cc | 11 ----------- lib/tests/Sha1Test.cc | 16 ---------------- unittest/CMakeLists.txt | 7 ++++++- unittest/MD5Unittest.cpp | 18 ++++++++++++++++++ unittest/SHA1Unittest.cpp | 23 +++++++++++++++++++++++ 8 files changed, 68 insertions(+), 53 deletions(-) delete mode 100644 lib/tests/Md5Test.cc delete mode 100644 lib/tests/Sha1Test.cc create mode 100644 unittest/MD5Unittest.cpp create mode 100644 unittest/SHA1Unittest.cpp diff --git a/lib/src/ssl_funcs/Md5.cc b/lib/src/ssl_funcs/Md5.cc index 4b8903ca..2330e7ff 100644 --- a/lib/src/ssl_funcs/Md5.cc +++ b/lib/src/ssl_funcs/Md5.cc @@ -13,16 +13,15 @@ */ #include "Md5.h" -#include #include #include -const int Md5Encode::kA = 0x67452301; -const int Md5Encode::kB = 0xefcdab89; -const int Md5Encode::kC = 0x98badcfe; -const int Md5Encode::kD = 0x10325476; +const uint32_t Md5Encode::kA = 0x67452301; +const uint32_t Md5Encode::kB = 0xefcdab89; +const uint32_t Md5Encode::kC = 0x98badcfe; +const uint32_t Md5Encode::kD = 0x10325476; -const unsigned long long Md5Encode::tiNumInteger = 4294967296; +const uint64_t Md5Encode::tiNumInteger = 4294967296; // function: CycleMoveLeft // @param srcNum: the number to be moved left @@ -90,7 +89,7 @@ void Md5Encode::roundF(char *data512Ptr, ParamDynamic ¶m) int s[] = {7, 12, 17, 22}; for (int i = 0; i < 16; ++i) { - uint32_t ti = tiNumInteger * abs(sin(i + 1)); + uint64_t ti = tiNumInteger * fabs(sin(i + 1)); if (i % 4 == 0) { FF(param.ua_, param.ub_, param.uc_, param.ud_, M[i], s[i % 4], ti); @@ -116,7 +115,8 @@ void Md5Encode::roundG(char *data512Ptr, ParamDynamic ¶m) int s[] = {5, 9, 14, 20}; for (int i = 0; i < 16; ++i) { - uint32_t ti = tiNumInteger * abs(sin(i + 1 + 16)); + auto sss = sin(i + 1 + 16); + uint64_t ti = tiNumInteger * fabs(sss); int index = (i * 5 + 1) % 16; if (i % 4 == 0) { @@ -167,7 +167,7 @@ void Md5Encode::roundH(char *data512Ptr, ParamDynamic ¶m) int s[] = {4, 11, 16, 23}; for (int i = 0; i < 16; ++i) { - uint32_t ti = tiNumInteger * abs(sin(i + 1 + 32)); + uint64_t ti = tiNumInteger * fabs(sin(i + 1 + 32)); int index = (i * 3 + 5) % 16; if (i % 4 == 0) { @@ -218,7 +218,7 @@ void Md5Encode::roundI(char *data512Ptr, ParamDynamic ¶m) int s[] = {6, 10, 15, 21}; for (int i = 0; i < 16; ++i) { - uint32_t ti = tiNumInteger * abs(sin(i + 1 + 48)); + uint64_t ti = tiNumInteger * fabs(sin(i + 1 + 48)); int index = (i * 7 + 0) % 16; if (i % 4 == 0) { @@ -265,7 +265,7 @@ void Md5Encode::roundI(char *data512Ptr, ParamDynamic ¶m) void Md5Encode::rotationCalculate(char *data512Ptr, ParamDynamic ¶m) { - if (NULL == data512Ptr) + if (nullptr == data512Ptr) { return; } @@ -285,7 +285,7 @@ void Md5Encode::rotationCalculate(char *data512Ptr, ParamDynamic ¶m) } // Convert to hex format string -std::string Md5Encode::getHexStr(unsigned int numStr) +std::string Md5Encode::getHexStr(uint32_t numStr) { std::string hexstr = ""; char szch[] = {'0', @@ -333,16 +333,16 @@ std::string Md5Encode::encode(const std::string &srcInfo) param.vd_last_ = kD; std::string result; - char *outDataPtr = NULL; + char *outDataPtr = nullptr; int totalByte = fillData(srcInfo.c_str(), srcInfo.length(), &outDataPtr); - // char * dataBitOfGroup = outDataPtr; + for (int i = 0; i < totalByte / (BIT_OF_GROUP / BIT_OF_BYTE); ++i) { char *dataBitOfGroup = outDataPtr; dataBitOfGroup += i * (BIT_OF_GROUP / BIT_OF_BYTE); rotationCalculate(dataBitOfGroup, param); } - delete[] outDataPtr, outDataPtr = NULL; + delete[] outDataPtr, outDataPtr = nullptr; result.append(getHexStr(param.ua_)); result.append(getHexStr(param.ub_)); result.append(getHexStr(param.uc_)); diff --git a/lib/src/ssl_funcs/Md5.h b/lib/src/ssl_funcs/Md5.h index 6b49fa3d..07bee842 100644 --- a/lib/src/ssl_funcs/Md5.h +++ b/lib/src/ssl_funcs/Md5.h @@ -67,15 +67,15 @@ class Md5Encode static void roundH(char *data512Ptr, ParamDynamic ¶m); static void roundI(char *data512Ptr, ParamDynamic ¶m); static void rotationCalculate(char *data512Ptr, ParamDynamic ¶m); - static std::string getHexStr(unsigned int numStr); + static std::string getHexStr(uint32_t numStr); static uint32_t fillData(const char *inDataPtr, int dataByteLen, char **outDataPtr); private: - static const int kA; - static const int kB; - static const int kC; - static const int kD; - static const unsigned long long tiNumInteger; + static const uint32_t kA; + static const uint32_t kB; + static const uint32_t kC; + static const uint32_t kD; + static const uint64_t tiNumInteger; }; diff --git a/lib/tests/CMakeLists.txt b/lib/tests/CMakeLists.txt index e9206e01..4c0f6e4e 100755 --- a/lib/tests/CMakeLists.txt +++ b/lib/tests/CMakeLists.txt @@ -4,9 +4,7 @@ add_executable(cache_map_test CacheMapTest.cc) add_executable(cache_map_test2 CacheMapTest2.cc) add_executable(cookies_test CookiesTest.cc) add_executable(class_name_test ClassNameTest.cc) -add_executable(sha1_test Sha1Test.cc ../src/ssl_funcs/Sha1.cc) add_executable(view_data_test HttpViewDataTest.cc) -add_executable(md5_test Md5Test.cc ../src/ssl_funcs/Md5.cc) add_executable(http_full_date_test HttpFullDateTest.cc) add_executable(gzip_test GzipTest.cc) add_executable(url_codec_test UrlCodecTest.cc) @@ -17,9 +15,7 @@ set(test_targets cache_map_test2 cookies_test class_name_test - sha1_test view_data_test - md5_test http_full_date_test gzip_test url_codec_test diff --git a/lib/tests/Md5Test.cc b/lib/tests/Md5Test.cc deleted file mode 100644 index 35c4a5f6..00000000 --- a/lib/tests/Md5Test.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include "../lib/src/ssl_funcs/Md5.h" -#include -int main() -{ - std::cout << Md5Encode::encode( - "123456789012345678901234567890123456789012345" - "678901234567890123456789012345678901234567890" - "1234567890") - << std::endl; - // The output shoud be '49CB3608E2B33FAD6B65DF8CB8F49668' -} \ No newline at end of file diff --git a/lib/tests/Sha1Test.cc b/lib/tests/Sha1Test.cc deleted file mode 100644 index 50c74a0a..00000000 --- a/lib/tests/Sha1Test.cc +++ /dev/null @@ -1,16 +0,0 @@ -#include "../lib/src/ssl_funcs/Sha1.h" -#include -#include - -int main() -{ - unsigned char in[] = - "1234567890123456789012345678901234567890123456789012345" - "678901234567890123456789012345678901234567890"; - unsigned char out[SHA_DIGEST_LENGTH] = {0}; - SHA1(in, strlen((const char *)in), out); - /// fecfd28bbc9345891a66d7c1b8ff46e60192d284 - for (int i = 0; i < SHA_DIGEST_LENGTH; ++i) - printf("%02x", out[i]); - putchar('\n'); -} diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 815b9b4d..8cd2fff6 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -1,11 +1,16 @@ add_executable(msgbuffer_unittest MsgBufferUnittest.cpp) add_executable(drobject_unittest DrObjectUnittest.cpp) add_executable(gzip_unittest GzipUnittest.cpp) +add_executable(md5_unittest MD5Unittest.cpp ../lib/src/ssl_funcs/Md5.cc) +add_executable(sha1_unittest SHA1Unittest.cpp ../lib/src/ssl_funcs/Sha1.cc) set(UNITTEST_TARGETS msgbuffer_unittest drobject_unittest - gzip_unittest) + gzip_unittest + md5_unittest + sha1_unittest) + set_property(TARGET ${UNITTEST_TARGETS} PROPERTY CXX_STANDARD ${DROGON_CXX_STANDARD}) set_property(TARGET ${UNITTEST_TARGETS} PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/unittest/MD5Unittest.cpp b/unittest/MD5Unittest.cpp new file mode 100644 index 00000000..cafd2123 --- /dev/null +++ b/unittest/MD5Unittest.cpp @@ -0,0 +1,18 @@ +#include "../lib/src/ssl_funcs/Md5.h" +#include +#include + +TEST(Md5Test, md5) +{ + // EXPECT_EQ(Md5Encode::encode("123456789012345678901234567890123456789012345" + // "678901234567890123456789012345678901234567890" + // "1234567890"), + // "49CB3608E2B33FAD6B65DF8CB8F49668"); + EXPECT_EQ(Md5Encode::encode("1"), "C4CA4238A0B923820DCC509A6F75849B"); +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/unittest/SHA1Unittest.cpp b/unittest/SHA1Unittest.cpp new file mode 100644 index 00000000..a4facaea --- /dev/null +++ b/unittest/SHA1Unittest.cpp @@ -0,0 +1,23 @@ +#include "../lib/src/ssl_funcs/Sha1.h" +#include +#include + +TEST(SHA1Test, sha1) +{ + unsigned char in[] = + "1234567890123456789012345678901234567890123456789012345" + "678901234567890123456789012345678901234567890"; + unsigned char out[SHA_DIGEST_LENGTH] = {0}; + SHA1(in, strlen((const char *)in), out); + std::string outStr; + outStr.resize(SHA_DIGEST_LENGTH * 2); + for (int i = 0; i < SHA_DIGEST_LENGTH; ++i) + sprintf((char *)(outStr.data() + i * 2), "%02x", out[i]); + EXPECT_EQ(outStr, "fecfd28bbc9345891a66d7c1b8ff46e60192d284"); +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}