PackageTest.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Created by tmd67 on 2020/3/23.
  3. //
  4. #include "gmock/gmock.h"
  5. #include "gtest/gtest.h"
  6. #include "Package.h"
  7. #include <memory>
  8. #include <iostream>
  9. using namespace std;
  10. typedef struct{
  11. int s1;
  12. float s2;
  13. char s3[256];
  14. char s4[256];
  15. }TestPackageStruct;
  16. #define TEST_VAULE_S1 0xFF02
  17. #define TEST_VALUE_S2 5.322
  18. #define TEST_VAULE_S3 "Hello world"
  19. #define TEST_VALUE_S4 "水电费后撒地方"
  20. #define TEST_SERVICE_CODE "__AUTH__"
  21. #define TEST_STRUCT_NAME "AUTH_REQ"
  22. #define TEST_ERROR_MSG "tinaodfiooitoinotntrei"
  23. #define TEST_USER_CODE 12323
  24. #define TEST_ERROR_CODE 357923
  25. TEST(PackageTest, TestAddStruct)
  26. {
  27. TestPackageStruct tmpPack{TEST_VAULE_S1, (float)TEST_VALUE_S2, TEST_VAULE_S3, TEST_VALUE_S4};
  28. std::shared_ptr<CCommPackage> ppkg(new CCommPackage(nullptr, TEST_SERVICE_CODE));
  29. ppkg->AddStruct(TEST_STRUCT_NAME, false, false, (BYTE*)&tmpPack, sizeof(TestPackageStruct));
  30. ASSERT_TRUE(ppkg->GetStructLen(TEST_STRUCT_NAME) == sizeof(TestPackageStruct));
  31. TestPackageStruct tmpBy;
  32. int len, arrayNum;
  33. len = sizeof(TestPackageStruct);
  34. ASSERT_TRUE(ppkg->GetStructData(TEST_STRUCT_NAME, (BYTE*)&tmpBy, &len, &arrayNum));
  35. ASSERT_TRUE(tmpBy.s1 == TEST_VAULE_S1);
  36. const float EPSINON = 0.00001;
  37. ASSERT_TRUE(abs(tmpBy.s2 - (float)TEST_VALUE_S2) <= EPSINON);
  38. ASSERT_TRUE(std::string(tmpBy.s3) == TEST_VAULE_S3);
  39. ASSERT_TRUE(std::string(tmpBy.s4) == TEST_VALUE_S4);
  40. ASSERT_TRUE(TEST_SERVICE_CODE == ppkg->GetServiceCode());
  41. ppkg->SetErrMsg(TEST_ERROR_CODE, TEST_USER_CODE, TEST_ERROR_MSG);
  42. DWORD tmpErrorCode, tmpUserCode;
  43. std::string errMsg;
  44. ASSERT_TRUE(ppkg->GetErrMsg(tmpErrorCode, tmpUserCode, errMsg));
  45. ASSERT_TRUE(tmpErrorCode == TEST_ERROR_CODE);
  46. ASSERT_TRUE(tmpUserCode == TEST_USER_CODE);
  47. ASSERT_TRUE(errMsg == TEST_ERROR_MSG);
  48. byte parasePack[32] = {0xc3, 0xa5, 0x20, 0x00, 0x01, 0x00, 0x5f, 0x48, 0x45, 0x4c, 0x4c, 0x4f, 0x5f, 0x00,
  49. 0xf5, 0x11, 0x7b, 0x5e, 0x00, 0x00, 0xf5, 0x11, 0x7b, 0x5e, 0xc2, 0xd0, 0x7e, 0xbd, 0x56, 0x73, 0x4e, 0xae};
  50. int parasePackLen = 32, HasRecvLen = 32;
  51. std::shared_ptr<CCommPackage> p(new CCommPackage(nullptr, nullptr));
  52. string strErrMsg;
  53. if (p->ParseRecvData(parasePack, &parasePackLen, strErrMsg))
  54. {
  55. // 从缓冲区删除完整包数据
  56. memmove(parasePack, parasePack + parasePackLen, HasRecvLen - parasePackLen);
  57. HasRecvLen -= parasePackLen;
  58. // 检查是否底层错误包
  59. ASSERT_TRUE(strcmp(ppkg->GetServiceCode().c_str(), "__AUTH__") == 0);
  60. }
  61. }