1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DISTRIBUTEDDB_COMMUNICATOR_COMMON_H 17 #define DISTRIBUTEDDB_COMMUNICATOR_COMMON_H 18 19 #include <set> 20 #include <string> 21 #include <vector> 22 #include "endian_convert.h" 23 #include "message.h" 24 #include "adapter_stub.h" 25 #include "frame_header.h" 26 #include "iprocess_communicator.h" 27 #include "communicator_aggregator.h" 28 #include "store_types.h" 29 30 struct EnvHandle { 31 DistributedDB::AdapterStub *adapterHandle = nullptr; 32 DistributedDB::CommunicatorAggregator *commAggrHandle = nullptr; 33 }; 34 35 struct OnOfflineDevice { 36 std::set<std::string> onlineDevices; 37 std::string latestOnlineDevice; 38 std::string latestOfflineDevice; 39 }; 40 41 bool SetUpEnv(EnvHandle &inEnv, const std::string &inName); 42 void TearDownEnv(EnvHandle &inEnv); 43 44 struct RegedTinyObject { 45 uint32_t placeHolder_ = 0; 46 }; 47 48 struct RegedHugeObject { 49 uint32_t placeHolder_ = 0; 50 }; 51 52 struct RegedGiantObject { 53 std::vector<uint8_t> rawData_; 54 static bool CheckEqual(const RegedGiantObject &inLeft, const RegedGiantObject &inRight); 55 }; 56 57 struct RegedOverSizeObject { 58 uint32_t placeHolder_ = 0; 59 }; 60 61 struct UnRegedTinyObject { 62 uint32_t placeHolder_ = 0; 63 }; 64 65 const uint32_t BUFF_LEN = 16; 66 struct ExtendHeadInfo { 67 uint32_t magic = 0; 68 uint32_t length = 0; 69 uint32_t version = 0; 70 uint8_t userId[BUFF_LEN] = {0}; 71 }; 72 73 class ExtendHeaderHandleTest : public DistributedDB::ExtendHeaderHandle { 74 public: ExtendHeaderHandleTest(const DistributedDB::ExtendInfo & info)75 explicit ExtendHeaderHandleTest(const DistributedDB::ExtendInfo &info) : headSize_(0) 76 { 77 localDbProperty_.appId = info.appId; 78 localDbProperty_.storeId = info.storeId; 79 localDbProperty_.userId = info.userId; 80 localDbProperty_.dstTarget = info.dstTarget; 81 }; ~ExtendHeaderHandleTest()82 ~ExtendHeaderHandleTest() {}; 83 // headSize should be 8 byte align 84 // return OK and headSize = 0 if no need to fill Head Data 85 // return OK and headSize > 0 if permit sync and will call FillHeadData 86 // return NO_PERMISSION if not permit sync GetHeadDataSize(uint32_t & headSize)87 DistributedDB::DBStatus GetHeadDataSize(uint32_t &headSize) override 88 { 89 headSize_ = sizeof(ExtendHeadInfo); 90 headSize_ = BYTE_8_ALIGN(headSize_); 91 headSize = headSize_; 92 return DistributedDB::OK; 93 }; 94 FillHeadData(uint8_t * data,uint32_t headSize,uint32_t totalLen)95 DistributedDB::DBStatus FillHeadData(uint8_t *data, uint32_t headSize, uint32_t totalLen) override 96 { 97 ExtendHeadInfo info = {MAGIC_NUM, headSize_, 0}; 98 DistributedDB::HostToNet(info.magic); 99 DistributedDB::HostToNet(info.length); 100 DistributedDB::HostToNet(info.version); 101 for (uint8_t i = 0; i < BUFF_LEN; i++) { 102 info.userId[i] = localDbProperty_.userId[i]; 103 } 104 auto errCode = memcpy_s(data, totalLen, &info, sizeof(ExtendHeadInfo)); 105 if (errCode != EOK) { 106 return DistributedDB::DB_ERROR; 107 } 108 return DistributedDB::OK; 109 }; 110 static constexpr int MAGIC_NUM = 0xF2; 111 private: 112 DistributedDB::ExtendInfo localDbProperty_; 113 uint32_t headSize_; 114 }; 115 116 const std::string DEVICE_NAME_A = "DeviceA"; 117 const std::string DEVICE_NAME_B = "DeviceB"; 118 const std::string DEVICE_NAME_C = "DeviceC"; 119 constexpr uint64_t LABEL_A = 1234; 120 constexpr uint64_t LABEL_B = 2345; 121 constexpr uint64_t LABEL_C = 3456; 122 constexpr uint32_t REGED_TINY_MSG_ID = 1111; 123 constexpr uint32_t REGED_HUGE_MSG_ID = 2222; 124 constexpr uint32_t REGED_GIANT_MSG_ID = 3333; 125 constexpr uint32_t REGED_OVERSIZE_MSG_ID = 4444; 126 constexpr uint32_t UNREGED_TINY_MSG_ID = 5555; 127 constexpr uint32_t FIXED_SESSIONID = 98765; 128 constexpr uint32_t FIXED_SEQUENCEID = 87654; 129 constexpr uint32_t TINY_SIZE = 100; // 100 Bytes 130 constexpr uint32_t HUGE_SIZE = 4 * 1024 * 1024; // 4 MBytes, 1024 is scale 131 constexpr uint32_t OVER_SIZE = 100 * 1024 * 1024; // 100 MBytes, 1024 is scale 132 constexpr uint32_t HEADER_SIZE = sizeof(DistributedDB::CommPhyHeader) + sizeof(DistributedDB::CommDivergeHeader) + 133 sizeof(DistributedDB::MessageHeader); // 96 Bytes For Header, 32 phyHeader, 40 divergeHeader, 24 msgHeader 134 constexpr uint32_t MAX_CAPACITY = 64 * 1024 * 1024; // 64 MBytes, 1024 is scale 135 136 void DoRegTransformFunction(); 137 138 DistributedDB::Message *BuildRegedTinyMessage(); 139 DistributedDB::Message *BuildRegedHugeMessage(); 140 DistributedDB::Message *BuildRegedGiantMessage(uint32_t length); 141 DistributedDB::Message *BuildRegedOverSizeMessage(); 142 DistributedDB::Message *BuildUnRegedTinyMessage(); 143 144 #define ASSERT_NOT_NULL_AND_ACTIVATE(communicator) \ 145 { \ 146 ASSERT_NE(communicator, nullptr); \ 147 (communicator)->Activate(); \ 148 } 149 150 #endif // DISTRIBUTEDDB_COMMUNICATOR_COMMON_H