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 "adapter_stub.h"
23 #include "communicator_aggregator.h"
24 #include "endian_convert.h"
25 #include "frame_header.h"
26 #include "iprocess_communicator.h"
27 #include "message.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     const std::shared_ptr<DistributedDB::DBStatusAdapter> &adapter);
43 bool SetUpEnv(EnvHandle &inEnv, const std::string &inName);
44 void TearDownEnv(EnvHandle &inEnv);
45 
46 struct RegedTinyObject {
47     uint32_t placeHolder_ = 0;
48 };
49 
50 struct RegedHugeObject {
51     uint32_t placeHolder_ = 0;
52 };
53 
54 struct RegedGiantObject {
55     std::vector<uint8_t> rawData_;
56     static bool CheckEqual(const RegedGiantObject &inLeft, const RegedGiantObject &inRight);
57 };
58 
59 struct RegedOverSizeObject {
60     uint32_t placeHolder_ = 0;
61 };
62 
63 struct UnRegedTinyObject {
64     uint32_t placeHolder_ = 0;
65 };
66 
67 const uint32_t BUFF_LEN = 16;
68 struct ExtendHeadInfo {
69     uint32_t magic = 0;
70     uint32_t length = 0;
71     uint32_t version = 0;
72     uint8_t userId[BUFF_LEN] = {0};
73 };
74 
75 class ExtendHeaderHandleTest : public DistributedDB::ExtendHeaderHandle {
76 public:
ExtendHeaderHandleTest(const DistributedDB::ExtendInfo & info)77     explicit ExtendHeaderHandleTest(const DistributedDB::ExtendInfo &info) : headSize_(0)
78     {
79         localDbProperty_.appId = info.appId;
80         localDbProperty_.storeId = info.storeId;
81         localDbProperty_.userId = info.userId;
82         localDbProperty_.dstTarget = info.dstTarget;
83     };
~ExtendHeaderHandleTest()84     ~ExtendHeaderHandleTest() {};
85     // headSize should be 8 byte align
86     // return OK and headSize = 0 if no need to fill Head Data
87     // return OK and headSize > 0 if permit sync and will call FillHeadData
88     // return NO_PERMISSION if not permit sync
GetHeadDataSize(uint32_t & headSize)89     DistributedDB::DBStatus GetHeadDataSize(uint32_t &headSize) override
90     {
91         headSize_ = sizeof(ExtendHeadInfo);
92         headSize_ = BYTE_8_ALIGN(headSize_);
93         headSize = headSize_;
94         return DistributedDB::OK;
95     };
96 
FillHeadData(uint8_t * data,uint32_t headSize,uint32_t totalLen)97     DistributedDB::DBStatus FillHeadData(uint8_t *data, uint32_t headSize, uint32_t totalLen) override
98     {
99         ExtendHeadInfo info = {MAGIC_NUM, headSize_, 0};
100         DistributedDB::HostToNet(info.magic);
101         DistributedDB::HostToNet(info.length);
102         DistributedDB::HostToNet(info.version);
103         for (uint8_t i = 0; i < BUFF_LEN; i++) {
104             info.userId[i] = static_cast<uint8_t>(localDbProperty_.userId[i]);
105         }
106         auto errCode = memcpy_s(data, totalLen, &info, sizeof(ExtendHeadInfo));
107         if (errCode != EOK) {
108             return DistributedDB::DB_ERROR;
109         }
110         return DistributedDB::OK;
111     };
112     static constexpr int MAGIC_NUM = 0xF2;
113 private:
114     DistributedDB::ExtendInfo localDbProperty_;
115     uint32_t headSize_;
116 };
117 
118 const std::string DEVICE_NAME_A = "DeviceA";
119 const std::string DEVICE_NAME_B = "DeviceB";
120 const std::string DEVICE_NAME_C = "DeviceC";
121 constexpr uint64_t LABEL_A = 1234;
122 constexpr uint64_t LABEL_B = 2345;
123 constexpr uint64_t LABEL_C = 3456;
124 constexpr uint32_t REGED_TINY_MSG_ID = 1111;
125 constexpr uint32_t REGED_HUGE_MSG_ID = 2222;
126 constexpr uint32_t REGED_GIANT_MSG_ID = 3333;
127 constexpr uint32_t REGED_OVERSIZE_MSG_ID = 4444;
128 constexpr uint32_t UNREGED_TINY_MSG_ID = 5555;
129 constexpr uint32_t FIXED_SESSIONID = 98765;
130 constexpr uint32_t FIXED_SEQUENCEID = 87654;
131 constexpr uint32_t TINY_SIZE = 100; // 100 Bytes
132 constexpr uint32_t HUGE_SIZE = 4 * 1024 * 1024; // 4 MBytes, 1024 is scale
133 constexpr uint32_t OVER_SIZE = 100 * 1024 * 1024; // 100 MBytes, 1024 is scale
134 constexpr uint32_t HEADER_SIZE = sizeof(DistributedDB::CommPhyHeader) + sizeof(DistributedDB::CommDivergeHeader) +
135     sizeof(DistributedDB::MessageHeader); // 96 Bytes For Header, 32 phyHeader, 40 divergeHeader, 24 msgHeader
136 constexpr uint32_t MAX_CAPACITY = 64 * 1024 * 1024; // 64 MBytes, 1024 is scale
137 
138 void DoRegTransformFunction();
139 
140 DistributedDB::Message *BuildRegedTinyMessage();
141 DistributedDB::Message *BuildRegedHugeMessage();
142 DistributedDB::Message *BuildRegedGiantMessage(uint32_t length);
143 DistributedDB::Message *BuildRegedOverSizeMessage();
144 DistributedDB::Message *BuildUnRegedTinyMessage();
145 
146 #define ASSERT_NOT_NULL_AND_ACTIVATE(communicator) \
147 { \
148     ASSERT_NE(communicator, nullptr); \
149     (communicator)->Activate(); \
150 }
151 
152 #endif // DISTRIBUTEDDB_COMMUNICATOR_COMMON_H