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 #include "distributeddb_communicator_common.h"
17 #include <gtest/gtest.h>
18 #include "db_errno.h"
19 #include "log_print.h"
20 #include "message_transform.h"
21 #include "securec.h"
22
23 using namespace std;
24 using namespace DistributedDB;
25
SetUpEnv(EnvHandle & inEnv,const string & inName)26 bool SetUpEnv(EnvHandle &inEnv, const string &inName)
27 {
28 if (inEnv.adapterHandle != nullptr || inEnv.commAggrHandle != nullptr) {
29 LOGI("[UT][Common][SetUp] Already Setup for %s", inName.c_str());
30 return false;
31 }
32
33 inEnv.adapterHandle = new (nothrow) AdapterStub(inName);
34 if (inEnv.adapterHandle == nullptr) {
35 LOGI("[UT][Common][SetUp] Create AdapterStub fail for %s", inName.c_str());
36 return false;
37 }
38
39 inEnv.commAggrHandle = new (nothrow) CommunicatorAggregator();
40 if (inEnv.commAggrHandle == nullptr) {
41 LOGI("[UT][Common][SetUp] Create CommunicatorAggregator fail for %s", inName.c_str());
42 return false;
43 }
44
45 int errCode = inEnv.commAggrHandle->Initialize(inEnv.adapterHandle);
46 if (errCode != E_OK) {
47 LOGI("[UT][Common][SetUp] Init CommunicatorAggregator fail for %s", inName.c_str());
48 return false;
49 }
50
51 return true;
52 }
53
TearDownEnv(EnvHandle & inEnv)54 void TearDownEnv(EnvHandle &inEnv)
55 {
56 if (inEnv.commAggrHandle != nullptr) {
57 inEnv.commAggrHandle->Finalize();
58 inEnv.commAggrHandle->DecObjRef(inEnv.commAggrHandle);
59 inEnv.commAggrHandle = nullptr;
60 }
61
62 if (inEnv.adapterHandle != nullptr) {
63 delete inEnv.adapterHandle;
64 inEnv.adapterHandle = nullptr;
65 }
66 }
67
RegFuncForTinyMsg()68 static void RegFuncForTinyMsg()
69 {
70 TransformFunc funcForTinyMsg;
71 funcForTinyMsg.computeFunc = [](const Message *inMsg)->uint32_t{return TINY_SIZE;};
72 funcForTinyMsg.serializeFunc = [](uint8_t *buffer, uint32_t length, const Message *inMsg)->int{
73 const RegedTinyObject *outObj = inMsg->GetObject<RegedTinyObject>();
74 EXPECT_NE(outObj, nullptr);
75 return E_OK;
76 };
77 funcForTinyMsg.deserializeFunc = [](const uint8_t *buffer, uint32_t length, Message *inMsg)->int{
78 int errCode = inMsg->SetCopiedObject(RegedTinyObject());
79 EXPECT_EQ(errCode, E_OK);
80 return E_OK;
81 };
82
83 MessageTransform::RegTransformFunction(REGED_TINY_MSG_ID, funcForTinyMsg);
84 }
85
RegFuncForHugeMsg()86 static void RegFuncForHugeMsg()
87 {
88 TransformFunc funcForHugeMsg;
89 funcForHugeMsg.computeFunc = [](const Message *inMsg)->uint32_t{return HUGE_SIZE;};
90 funcForHugeMsg.serializeFunc = [](uint8_t *buffer, uint32_t length, const Message *inMsg)->int{
91 const RegedHugeObject *outObj = inMsg->GetObject<RegedHugeObject>();
92 EXPECT_NE(outObj, nullptr);
93 return E_OK;
94 };
95 funcForHugeMsg.deserializeFunc = [](const uint8_t *buffer, uint32_t length, Message *inMsg)->int{
96 int errCode = inMsg->SetCopiedObject(RegedHugeObject());
97 EXPECT_EQ(errCode, E_OK);
98 return E_OK;
99 };
100
101 MessageTransform::RegTransformFunction(REGED_HUGE_MSG_ID, funcForHugeMsg);
102 }
103
RegFuncForGiantMsg()104 static void RegFuncForGiantMsg()
105 {
106 TransformFunc funcForGiantMsg;
107 funcForGiantMsg.computeFunc = [](const Message *inMsg)->uint32_t{
108 const RegedGiantObject *outObj = inMsg->GetObject<RegedGiantObject>();
109 if (outObj == nullptr) {
110 return 0;
111 }
112 return outObj->rawData_.size();
113 };
114 funcForGiantMsg.serializeFunc = [](uint8_t *buffer, uint32_t length, const Message *inMsg)->int{
115 const RegedGiantObject *outObj = inMsg->GetObject<RegedGiantObject>();
116 if (outObj == nullptr) {
117 return -E_INVALID_ARGS;
118 }
119 if (outObj->rawData_.size() != length) {
120 return -E_LENGTH_ERROR;
121 }
122 errno_t errCode = memcpy_s(buffer, length, &(outObj->rawData_[0]), length);
123 if (errCode != EOK) {
124 return -E_SECUREC_ERROR;
125 }
126 return E_OK;
127 };
128 funcForGiantMsg.deserializeFunc = [](const uint8_t *buffer, uint32_t length, Message *inMsg)->int{
129 RegedGiantObject *obj = new (nothrow) RegedGiantObject();
130 if (obj == nullptr) {
131 return -E_OUT_OF_MEMORY;
132 }
133 obj->rawData_.resize(length);
134 errno_t retCode = memcpy_s(&(obj->rawData_[0]), length, buffer, length);
135 if (retCode != EOK) {
136 delete obj;
137 return -E_SECUREC_ERROR;
138 }
139 int errCode = inMsg->SetExternalObject(obj);
140 if (errCode != E_OK) {
141 delete obj;
142 return errCode;
143 }
144 return E_OK;
145 };
146
147 MessageTransform::RegTransformFunction(REGED_GIANT_MSG_ID, funcForGiantMsg);
148 }
149
RegFuncForOverSizeMsg()150 static void RegFuncForOverSizeMsg()
151 {
152 TransformFunc funcForOverSizeMsg;
153 funcForOverSizeMsg.computeFunc = [](const Message *inMsg)->uint32_t{return OVER_SIZE;};
154 funcForOverSizeMsg.serializeFunc = [](uint8_t *buffer, uint32_t length, const Message *inMsg)->int{
155 const RegedOverSizeObject *outObj = inMsg->GetObject<RegedOverSizeObject>();
156 EXPECT_NE(outObj, nullptr);
157 return E_OK;
158 };
159 funcForOverSizeMsg.deserializeFunc = [](const uint8_t *buffer, uint32_t length, Message *inMsg)->int{
160 int errCode = inMsg->SetCopiedObject(RegedOverSizeObject());
161 EXPECT_EQ(errCode, E_OK);
162 return E_OK;
163 };
164
165 MessageTransform::RegTransformFunction(REGED_OVERSIZE_MSG_ID, funcForOverSizeMsg);
166 }
167
DoRegTransformFunction()168 void DoRegTransformFunction()
169 {
170 RegFuncForTinyMsg();
171 RegFuncForHugeMsg();
172 RegFuncForGiantMsg();
173 RegFuncForOverSizeMsg();
174 }
175
BuildRegedTinyMessage()176 Message *BuildRegedTinyMessage()
177 {
178 RegedTinyObject *obj = new (nothrow) RegedTinyObject();
179 if (obj == nullptr) {
180 return nullptr;
181 }
182
183 Message *outMsg = new (nothrow) Message(REGED_TINY_MSG_ID);
184 if (outMsg == nullptr) {
185 delete obj;
186 obj = nullptr;
187 return nullptr;
188 }
189
190 int errCode = outMsg->SetExternalObject(obj);
191 if (errCode != E_OK) {
192 delete obj;
193 obj = nullptr;
194 delete outMsg;
195 outMsg = nullptr;
196 return nullptr;
197 }
198 outMsg->SetMessageType(TYPE_REQUEST);
199 outMsg->SetSessionId(FIXED_SESSIONID);
200 outMsg->SetSequenceId(FIXED_SEQUENCEID);
201
202 return outMsg;
203 }
204
BuildRegedHugeMessage()205 Message *BuildRegedHugeMessage()
206 {
207 RegedHugeObject *obj = new (nothrow) RegedHugeObject();
208 if (obj == nullptr) {
209 return nullptr;
210 }
211
212 Message *outMsg = new (nothrow) Message(REGED_HUGE_MSG_ID);
213 if (outMsg == nullptr) {
214 delete obj;
215 return nullptr;
216 }
217
218 int errCode = outMsg->SetExternalObject(obj);
219 if (errCode != E_OK) {
220 delete obj;
221 obj = nullptr;
222 delete outMsg;
223 outMsg = nullptr;
224 return nullptr;
225 }
226 outMsg->SetMessageType(TYPE_RESPONSE);
227 outMsg->SetSessionId(FIXED_SESSIONID);
228 outMsg->SetSequenceId(FIXED_SEQUENCEID);
229
230 return outMsg;
231 }
232
233 // length should be a multiple of four
BuildRegedGiantMessage(uint32_t length)234 Message *BuildRegedGiantMessage(uint32_t length)
235 {
236 uint32_t count = length / sizeof(uint32_t);
237 if (count == 0) {
238 return nullptr;
239 }
240
241 RegedGiantObject *obj = new (nothrow) RegedGiantObject();
242 if (obj == nullptr) {
243 return nullptr;
244 }
245
246 Message *outMsg = new (nothrow) Message(REGED_GIANT_MSG_ID);
247 if (outMsg == nullptr) {
248 delete obj;
249 obj = nullptr;
250 return nullptr;
251 }
252
253 obj->rawData_.resize(count * sizeof(uint32_t));
254 auto dataPtr = reinterpret_cast<uint32_t *>(&(obj->rawData_[0]));
255 uint32_t value = 0;
256 while (value < count) {
257 *dataPtr++ = value++;
258 }
259
260 int errCode = outMsg->SetExternalObject(obj);
261 if (errCode != E_OK) {
262 delete obj;
263 obj = nullptr;
264 delete outMsg;
265 outMsg = nullptr;
266 return nullptr;
267 }
268 outMsg->SetMessageType(TYPE_NOTIFY);
269 outMsg->SetSessionId(FIXED_SESSIONID);
270 outMsg->SetSequenceId(FIXED_SEQUENCEID);
271
272 return outMsg;
273 }
274
BuildRegedOverSizeMessage()275 Message *BuildRegedOverSizeMessage()
276 {
277 RegedOverSizeObject *obj = new (nothrow) RegedOverSizeObject();
278 if (obj == nullptr) {
279 return nullptr;
280 }
281
282 Message *outMsg = new (nothrow) Message(REGED_OVERSIZE_MSG_ID);
283 if (outMsg == nullptr) {
284 delete obj;
285 return nullptr;
286 }
287
288 int errCode = outMsg->SetExternalObject(obj);
289 if (errCode != E_OK) {
290 delete obj;
291 obj = nullptr;
292 delete outMsg;
293 outMsg = nullptr;
294 return nullptr;
295 }
296 outMsg->SetMessageType(TYPE_NOTIFY);
297 outMsg->SetSessionId(FIXED_SESSIONID);
298 outMsg->SetSequenceId(FIXED_SEQUENCEID);
299
300 return outMsg;
301 }
302
BuildUnRegedTinyMessage()303 Message *BuildUnRegedTinyMessage()
304 {
305 UnRegedTinyObject *obj = new (nothrow) UnRegedTinyObject();
306 if (obj == nullptr) {
307 return nullptr;
308 }
309
310 Message *outMsg = new (nothrow) Message(UNREGED_TINY_MSG_ID);
311 if (outMsg == nullptr) {
312 delete obj;
313 return nullptr;
314 }
315
316 int errCode = outMsg->SetExternalObject(obj);
317 if (errCode != E_OK) {
318 delete obj;
319 obj = nullptr;
320 delete outMsg;
321 outMsg = nullptr;
322 return nullptr;
323 }
324 outMsg->SetMessageType(TYPE_NOTIFY);
325 outMsg->SetSessionId(FIXED_SESSIONID);
326 outMsg->SetSequenceId(FIXED_SEQUENCEID);
327
328 return outMsg;
329 }
330
CheckEqual(const RegedGiantObject & inLeft,const RegedGiantObject & inRight)331 bool RegedGiantObject::CheckEqual(const RegedGiantObject &inLeft, const RegedGiantObject &inRight)
332 {
333 if (inLeft.rawData_.size() != inRight.rawData_.size()) {
334 return false;
335 }
336 uint32_t index = 0;
337 for (const auto &left : inLeft.rawData_) {
338 uint8_t right = inRight.rawData_[index];
339 if (left != right) {
340 LOGE("[RegedGiantObject][CheckEqual] RawData unequal at index=%u", index);
341 return false;
342 }
343 index++;
344 }
345 return true;
346 }