// // Created by tmd67 on 2020/3/23. // #include "gmock/gmock.h" #include "gtest/gtest.h" #include "Package.h" #include #include using namespace std; typedef struct{ int s1; float s2; char s3[256]; char s4[256]; }TestPackageStruct; #define TEST_VAULE_S1 0xFF02 #define TEST_VALUE_S2 5.322 #define TEST_VAULE_S3 "Hello world" #define TEST_VALUE_S4 "水电费后撒地方" #define TEST_SERVICE_CODE "__AUTH__" #define TEST_STRUCT_NAME "AUTH_REQ" #define TEST_ERROR_MSG "tinaodfiooitoinotntrei" #define TEST_USER_CODE 12323 #define TEST_ERROR_CODE 357923 TEST(PackageTest, TestAddStruct) { TestPackageStruct tmpPack{TEST_VAULE_S1, (float)TEST_VALUE_S2, TEST_VAULE_S3, TEST_VALUE_S4}; std::shared_ptr ppkg(new CCommPackage(nullptr, TEST_SERVICE_CODE)); ppkg->AddStruct(TEST_STRUCT_NAME, false, false, (BYTE*)&tmpPack, sizeof(TestPackageStruct)); ASSERT_TRUE(ppkg->GetStructLen(TEST_STRUCT_NAME) == sizeof(TestPackageStruct)); TestPackageStruct tmpBy; int len, arrayNum; len = sizeof(TestPackageStruct); ASSERT_TRUE(ppkg->GetStructData(TEST_STRUCT_NAME, (BYTE*)&tmpBy, &len, &arrayNum)); ASSERT_TRUE(tmpBy.s1 == TEST_VAULE_S1); const float EPSINON = 0.00001; ASSERT_TRUE(abs(tmpBy.s2 - (float)TEST_VALUE_S2) <= EPSINON); ASSERT_TRUE(std::string(tmpBy.s3) == TEST_VAULE_S3); ASSERT_TRUE(std::string(tmpBy.s4) == TEST_VALUE_S4); ASSERT_TRUE(TEST_SERVICE_CODE == ppkg->GetServiceCode()); ppkg->SetErrMsg(TEST_ERROR_CODE, TEST_USER_CODE, TEST_ERROR_MSG); DWORD tmpErrorCode, tmpUserCode; std::string errMsg; ASSERT_TRUE(ppkg->GetErrMsg(tmpErrorCode, tmpUserCode, errMsg)); ASSERT_TRUE(tmpErrorCode == TEST_ERROR_CODE); ASSERT_TRUE(tmpUserCode == TEST_USER_CODE); ASSERT_TRUE(errMsg == TEST_ERROR_MSG); byte parasePack[32] = {0xc3, 0xa5, 0x20, 0x00, 0x01, 0x00, 0x5f, 0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x5f, 0x00, 0xf5, 0x11, 0x7b, 0x5e, 0x00, 0x00, 0xf5, 0x11, 0x7b, 0x5e, 0xc2, 0xd0, 0x7e, 0xbd, 0x56, 0x73, 0x4e, 0xae}; int parasePackLen = 32, HasRecvLen = 32; std::shared_ptr p(new CCommPackage(nullptr, nullptr)); string strErrMsg; if (p->ParseRecvData(parasePack, ¶sePackLen, strErrMsg)) { // 从缓冲区删除完整包数据 memmove(parasePack, parasePack + parasePackLen, HasRecvLen - parasePackLen); HasRecvLen -= parasePackLen; // 检查是否底层错误包 ASSERT_TRUE(strcmp(ppkg->GetServiceCode().c_str(), "__AUTH__") == 0); } }