Add some unit tests (#311)

This commit is contained in:
An Tao 2019-12-09 22:16:09 +08:00 committed by GitHub
parent ea43d8127d
commit a67ab1db52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 68 additions and 53 deletions

View File

@ -13,16 +13,15 @@
*/
#include "Md5.h"
#include <iostream>
#include <math.h>
#include <string.h>
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 &param)
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 &param)
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 &param)
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 &param)
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 &param)
void Md5Encode::rotationCalculate(char *data512Ptr, ParamDynamic &param)
{
if (NULL == data512Ptr)
if (nullptr == data512Ptr)
{
return;
}
@ -285,7 +285,7 @@ void Md5Encode::rotationCalculate(char *data512Ptr, ParamDynamic &param)
}
// 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_));

View File

@ -67,15 +67,15 @@ class Md5Encode
static void roundH(char *data512Ptr, ParamDynamic &param);
static void roundI(char *data512Ptr, ParamDynamic &param);
static void rotationCalculate(char *data512Ptr, ParamDynamic &param);
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;
};

View File

@ -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

View File

@ -1,11 +0,0 @@
#include "../lib/src/ssl_funcs/Md5.h"
#include <iostream>
int main()
{
std::cout << Md5Encode::encode(
"123456789012345678901234567890123456789012345"
"678901234567890123456789012345678901234567890"
"1234567890")
<< std::endl;
// The output shoud be '49CB3608E2B33FAD6B65DF8CB8F49668'
}

View File

@ -1,16 +0,0 @@
#include "../lib/src/ssl_funcs/Sha1.h"
#include <stdio.h>
#include <string.h>
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');
}

View File

@ -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)

18
unittest/MD5Unittest.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "../lib/src/ssl_funcs/Md5.h"
#include <gtest/gtest.h>
#include <string>
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();
}

23
unittest/SHA1Unittest.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../lib/src/ssl_funcs/Sha1.h"
#include <gtest/gtest.h>
#include <string>
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();
}