1 /*
2 * Copyright (c) 2021-2022 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 "gtest/gtest.h"
17
18 #include "base/log/log.h"
19 #include "frameworks/bridge/codec/codec_data.h"
20 #include "frameworks/bridge/codec/function_call.h"
21 #include "frameworks/bridge/codec/standard_function_codec.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS::Ace::Framework {
27 namespace {
28
29 const std::string FUNCTION_NAME_VALUE = "getInfo";
30 const std::string FUNCTION_PARA_STRING_VALUE = "onePara";
31 const int32_t FUNCTION_PARA_INT_VALUE = 100;
32 const std::vector<int8_t> FUNCTION_PARA_INT8_ARRAY = { 127, 0, 100 };
33 const std::vector<uint8_t> INT8_ARRAY_ENCODE_RESULT = { 22, 7, 0, 0, 0, 103, 101, 116, 73, 110, 102, 111,
34 1, 25, 3, 0, 0, 0, 127, 0, 100 };
35 const std::vector<int16_t> FUNCTION_PARA_INT16_ARRAY = { 32767, 0, 100 };
36 const std::vector<uint8_t> INT16_ARRAY_ENCODE_RESULT = { 22, 7, 0, 0, 0, 103, 101, 116, 73, 110, 102, 111,
37 1, 26, 3, 0, 0, 0, 255, 127, 0, 0, 100, 0 };
38 const std::vector<int32_t> FUNCTION_PARA_INT32_ARRAY = { 2147483647, 0, 100 };
39 const std::vector<uint8_t> INT32_ARRAY_ENCODE_RESULT = { 22, 7, 0, 0, 0, 103, 101, 116, 73, 110, 102, 111,
40 1, 27, 3, 0, 0, 0, 255, 255, 255, 127, 0, 0, 0, 0, 100, 0, 0, 0 };
41 const std::set<std::string> FUNCTION_PARA_SET = { "1", "2", "3" };
42 const std::vector<uint8_t> SET_ENCODE_RESULT = { 22, 7, 0, 0, 0, 103, 101, 116, 73, 110, 102, 111,
43 1, 24, 3, 0, 0, 0, 1, 0, 0, 0, 49, 1, 0, 0, 0, 50, 1, 0, 0, 0, 51 };
44 const std::map<std::string, std::string> FUNCTION_PARA_MAP = { { "1", "value1" }, { "2", "value2" } };
45 const std::vector<uint8_t> MAP_ENCODE_RESULT = { 22, 7, 0, 0, 0, 103, 101, 116, 73, 110, 102, 111,
46 1, 23, 2, 0, 0, 0, 1, 0, 0, 0, 49, 6, 0, 0, 0, 118, 97, 108, 117, 101, 49, 1, 0, 0, 0, 50, 6,
47 0, 0, 0, 118, 97, 108, 117, 101, 50 };
48
49 } // namespace
50
51 class GroupMessageCodecTest : public testing::Test {
52 public:
53 static void SetUpTestCase();
54 static void TearDownTestCase();
55 void SetUp();
56 void TearDown();
57 };
58
SetUpTestCase()59 void GroupMessageCodecTest::SetUpTestCase() {}
TearDownTestCase()60 void GroupMessageCodecTest::TearDownTestCase() {}
SetUp()61 void GroupMessageCodecTest::SetUp() {}
TearDown()62 void GroupMessageCodecTest::TearDown() {}
63
64 /**
65 * @tc.name: PrimitiveTypeCodecTest001
66 * @tc.desc: Encode a function with one null para to binary, then decode and check decode result.
67 * @tc.type: FUNC
68 */
69 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest001, TestSize.Level1)
70 {
71 /**
72 * @tc.steps: step1. Prepare FunctionCall instance & test data:NULL.
73 * @tc.expected: step1. test data is NULL.
74 */
75 std::vector<CodecData> args;
76 CodecData arg1;
77 ASSERT_TRUE(arg1.IsNull());
78 args.push_back(arg1);
79 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
80
81 /**
82 * @tc.steps: step2. Prepare codec instance & encode the function call.
83 */
84 std::vector<uint8_t> encodeBuf;
85 StandardFunctionCodec codec;
86 codec.EncodeFunctionCall(functionCall, encodeBuf);
87
88 /**
89 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
90 * @tc.expected: step3. check function name & para type and value.
91 */
92 FunctionCall result;
93 codec.DecodeFunctionCall(encodeBuf, result);
94 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
95 std::vector<CodecData> resultArgs = result.GetArgs();
96 ASSERT_TRUE(resultArgs[0].IsNull());
97 }
98
99 /**
100 * @tc.name: PrimitiveTypeCodecTest002
101 * @tc.desc: Encode a function with one True para to binary, then decode and check decode result.
102 * @tc.type: FUNC
103 */
104 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest002, TestSize.Level1)
105 {
106 /**
107 * @tc.steps: step1. Prepare FunctionCall instance & test data:True.
108 * @tc.expected: step1. test data is True.
109 */
110 std::vector<CodecData> args;
111 CodecData arg1(true);
112 ASSERT_TRUE(arg1.IsBool());
113 ASSERT_TRUE(arg1.GetBoolValue());
114 args.push_back(arg1);
115 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
116
117 /**
118 * @tc.steps: step2. Prepare codec instance & encode the function call.
119 */
120 std::vector<uint8_t> encodeBuf;
121 StandardFunctionCodec codec;
122 codec.EncodeFunctionCall(functionCall, encodeBuf);
123
124 /**
125 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
126 * @tc.expected: step3. check function name & para type and value.
127 */
128 FunctionCall result;
129 codec.DecodeFunctionCall(encodeBuf, result);
130 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
131 std::vector<CodecData> resultArgs = result.GetArgs();
132 ASSERT_TRUE(resultArgs[0].IsBool());
133 ASSERT_TRUE(resultArgs[0].GetBoolValue());
134 }
135
136 /**
137 * @tc.name: PrimitiveTypeCodecTest003
138 * @tc.desc: Encode a function with one False para to binary, then decode and check decode result.
139 * @tc.type: FUNC
140 */
141 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest003, TestSize.Level1)
142 {
143 /**
144 * @tc.steps: step1. Prepare FunctionCall instance & test data:False.
145 * @tc.expected: step1. test data is False.
146 */
147 std::vector<CodecData> args;
148 CodecData arg1(false);
149 ASSERT_TRUE(arg1.IsBool());
150 ASSERT_FALSE(arg1.GetBoolValue());
151 args.push_back(arg1);
152 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
153
154 /**
155 * @tc.steps: step2. Prepare codec instance & encode the function call.
156 */
157 std::vector<uint8_t> encodeBuf;
158 StandardFunctionCodec codec;
159 codec.EncodeFunctionCall(functionCall, encodeBuf);
160
161 /**
162 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
163 * @tc.expected: step3. check function name & para type and value.
164 */
165 FunctionCall result;
166 codec.DecodeFunctionCall(encodeBuf, result);
167 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
168 std::vector<CodecData> resultArgs = result.GetArgs();
169 ASSERT_TRUE(resultArgs[0].IsBool());
170 ASSERT_FALSE(resultArgs[0].GetBoolValue());
171 }
172
173 /**
174 * @tc.name: PrimitiveTypeCodecTest004
175 * @tc.desc: Encode a function with one int para to binary, then decode and check decode result.
176 * @tc.type: FUNC
177 */
178 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest004, TestSize.Level1)
179 {
180 /**
181 * @tc.steps: step1. Prepare FunctionCall instance & test data 100(int).
182 * @tc.expected: step1. test data is 100.
183 */
184 std::vector<CodecData> args;
185 CodecData arg1(FUNCTION_PARA_INT_VALUE);
186 ASSERT_TRUE(arg1.IsInt());
187 args.push_back(arg1);
188 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
189
190 /**
191 * @tc.steps: step2. Prepare codec instance & encode the function call.
192 */
193 std::vector<uint8_t> encodeBuf;
194 StandardFunctionCodec codec;
195 codec.EncodeFunctionCall(functionCall, encodeBuf);
196
197 /**
198 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
199 * @tc.expected: step3. check function name & para type and value.
200 */
201 FunctionCall result;
202 codec.DecodeFunctionCall(encodeBuf, result);
203 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
204 std::vector<CodecData> resultArgs = result.GetArgs();
205 ASSERT_TRUE(resultArgs[0].IsInt());
206 ASSERT_EQ(FUNCTION_PARA_INT_VALUE, resultArgs[0].GetIntValue());
207 }
208
209 /**
210 * @tc.name: PrimitiveTypeCodecTest005
211 * @tc.desc: Encode a function with one String para to binary, then decode and check decode result.
212 * @tc.type: FUNC
213 */
214 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest005, TestSize.Level1)
215 {
216 /**
217 * @tc.steps: step1. Prepare FunctionCall instance & test data 'onePara'.
218 * @tc.expected: step1. test data is 'onePara'.
219 */
220 std::vector<CodecData> args;
221 CodecData arg1(FUNCTION_PARA_STRING_VALUE);
222 ASSERT_TRUE(arg1.IsString());
223 args.push_back(arg1);
224 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
225
226 /**
227 * @tc.steps: step2. Prepare codec instance & encode the function call.
228 */
229 std::vector<uint8_t> encodeBuf;
230 StandardFunctionCodec codec;
231 codec.EncodeFunctionCall(functionCall, encodeBuf);
232
233 /**
234 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
235 * @tc.expected: step3. check function name & para type and value.
236 */
237 FunctionCall result;
238 codec.DecodeFunctionCall(encodeBuf, result);
239 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
240 std::vector<CodecData> resultArgs = result.GetArgs();
241 ASSERT_TRUE(resultArgs[0].IsString());
242 ASSERT_EQ(FUNCTION_PARA_STRING_VALUE, resultArgs[0].GetStringValue());
243 }
244
245 /**
246 * @tc.name: PrimitiveTypeCodecTest006
247 * @tc.desc: Encode a function with int8 array para to binary and check the encode result,
248 * then decode and check decode result.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest006, TestSize.Level1)
252 {
253 /**
254 * @tc.steps: step1. Prepare FunctionCall instance & test data .
255 */
256 CodecData arg1(FUNCTION_PARA_INT8_ARRAY);
257 std::vector<CodecData> args;
258 args.push_back(arg1);
259 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
260
261 /**
262 * @tc.steps: step2. Prepare codec instance & encode the function call.
263 * @tc.expected: step2. encode data equals to prepared binary data.
264 */
265 std::vector<uint8_t> encodeBuf;
266 StandardFunctionCodec codec;
267 codec.EncodeFunctionCall(functionCall, encodeBuf);
268 for (size_t idx = 0; idx < encodeBuf.size(); idx++) {
269 ASSERT_EQ(encodeBuf.at(idx), INT8_ARRAY_ENCODE_RESULT.at(idx));
270 }
271
272 /**
273 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
274 * @tc.expected: step3. check function name & para type and value.
275 */
276 FunctionCall result;
277 codec.DecodeFunctionCall(encodeBuf, result);
278 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
279 std::vector<CodecData> resultArgs = result.GetArgs();
280 ASSERT_EQ(resultArgs[0].GetType(), BufferDataType::TYPE_INT8_ARRAY);
281
282 std::vector<int8_t> data = resultArgs[0].GetInt8ArrayValue();
283 for (size_t idx = 0; idx < data.size(); idx++) {
284 ASSERT_EQ(data.at(idx), FUNCTION_PARA_INT8_ARRAY[idx]);
285 }
286 }
287
288 /**
289 * @tc.name: PrimitiveTypeCodecTest007
290 * @tc.desc: Encode a function with int8 array para to binary and check the encode result ,then decode and check decode
291 * result.
292 * @tc.type: FUNC
293 */
294 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest007, TestSize.Level1)
295 {
296 /**
297 * @tc.steps: step1. Prepare FunctionCall instance & test data .
298 */
299 CodecData arg1(FUNCTION_PARA_INT16_ARRAY);
300
301 std::vector<CodecData> args;
302 args.push_back(arg1);
303 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
304
305 /**
306 * @tc.steps: step2. Prepare codec instance & encode the function call.
307 * @tc.expected: step2. encode data equals to prepared binary data.
308 */
309 std::vector<uint8_t> encodeBuf;
310 StandardFunctionCodec codec;
311 codec.EncodeFunctionCall(functionCall, encodeBuf);
312
313 for (size_t idx = 0; idx < encodeBuf.size(); idx++) {
314 ASSERT_EQ(encodeBuf.at(idx), INT16_ARRAY_ENCODE_RESULT.at(idx));
315 }
316
317 /**
318 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
319 * @tc.expected: step3. check function name & para type and value.
320 */
321 FunctionCall result;
322 codec.DecodeFunctionCall(encodeBuf, result);
323 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
324 std::vector<CodecData> resultArgs = result.GetArgs();
325 ASSERT_EQ(resultArgs[0].GetType(), BufferDataType::TYPE_INT16_ARRAY);
326
327 std::vector<int16_t> data = resultArgs[0].GetInt16ArrayValue();
328 for (size_t idx = 0; idx < data.size(); idx++) {
329 ASSERT_EQ(data.at(idx), FUNCTION_PARA_INT16_ARRAY.at(idx));
330 }
331 }
332
333 /**
334 * @tc.name: PrimitiveTypeCodecTest008
335 * @tc.desc: Encode a function with int8 array para to binary and check the encode result ,then decode and check decode
336 * result.
337 * @tc.type: FUNC
338 */
339 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest008, TestSize.Level1)
340 {
341 /**
342 * @tc.steps: step1. Prepare FunctionCall instance & test data .
343 */
344 CodecData arg1(FUNCTION_PARA_INT32_ARRAY);
345 std::vector<CodecData> args;
346 args.push_back(arg1);
347 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
348
349 /**
350 * @tc.steps: step2. Prepare codec instance & encode the function call.
351 * @tc.expected: step2. encode data equals to prepared binary data.
352 */
353 std::vector<uint8_t> encodeBuf;
354 StandardFunctionCodec codec;
355 codec.EncodeFunctionCall(functionCall, encodeBuf);
356 for (size_t idx = 0; idx < encodeBuf.size(); idx++) {
357 ASSERT_EQ(encodeBuf.at(idx), INT32_ARRAY_ENCODE_RESULT.at(idx));
358 }
359
360 /**
361 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
362 * @tc.expected: step3. check function name & para type and value.
363 */
364 FunctionCall result;
365 codec.DecodeFunctionCall(encodeBuf, result);
366 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
367 std::vector<CodecData> resultArgs = result.GetArgs();
368 ASSERT_EQ(resultArgs[0].GetType(), BufferDataType::TYPE_INT32_ARRAY);
369
370 std::vector<int32_t> data = resultArgs[0].GetInt32ArrayValue();
371 for (size_t idx = 0; idx < data.size(); idx++) {
372 ASSERT_EQ(data.at(idx), FUNCTION_PARA_INT32_ARRAY.at(idx));
373 }
374 }
375
376 /**
377 * @tc.name: PrimitiveTypeCodecTest009
378 * @tc.desc: Encode a function with int8 array para to binary and check the encode result ,then decode and check decode
379 * @tc.type: FUNC
380 */
381 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest009, TestSize.Level1)
382 {
383 /**
384 * @tc.steps: step1. Prepare FunctionCall instance & test data .
385 */
386 CodecData arg1(FUNCTION_PARA_SET);
387 std::vector<CodecData> args;
388 args.push_back(arg1);
389 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
390
391 /**
392 * @tc.steps: step2. Prepare codec instance & encode the function call.
393 * @tc.expected: step2. encode data equals to prepared binary data.
394 */
395 std::vector<uint8_t> encodeBuf;
396 StandardFunctionCodec codec;
397 codec.EncodeFunctionCall(functionCall, encodeBuf);
398 std::vector<uint8_t>::iterator encodeIter = encodeBuf.begin();
399 std::vector<uint8_t>::const_iterator encodeIter1 = SET_ENCODE_RESULT.begin();
400
401 while (encodeIter != encodeBuf.end()) {
402 ASSERT_EQ(*encodeIter, *encodeIter1);
403 encodeIter++;
404 encodeIter1++;
405 }
406
407 /**
408 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
409 * @tc.expected: step3. check function name & para type and value.
410 */
411 FunctionCall result;
412 codec.DecodeFunctionCall(encodeBuf, result);
413 std::vector<CodecData> resultArgs = result.GetArgs();
414 std::set<std::string> data = resultArgs[0].GetSetValue();
415 std::set<std::string>::iterator iter = data.begin();
416 std::set<std::string>::iterator iter1 = FUNCTION_PARA_SET.begin();
417
418 while (iter != data.end()) {
419 ASSERT_EQ(*iter, *iter1);
420 iter++;
421 iter1++;
422 }
423 }
424
425 /**
426 * @tc.name: PrimitiveTypeCodecTest010
427 * @tc.desc: Encode a function with int8 array para to binary and check the encode result ,then decode and check decode
428 * @tc.type: FUNC
429 */
430 HWTEST_F(GroupMessageCodecTest, PrimitiveTypeCodecTest010, TestSize.Level1)
431 {
432 /**
433 * @tc.steps: step1. Prepare FunctionCall instance & test data .
434 */
435 CodecData arg1(FUNCTION_PARA_MAP);
436 std::vector<CodecData> args;
437 args.push_back(arg1);
438 FunctionCall functionCall(FUNCTION_NAME_VALUE, args);
439
440 /**
441 * @tc.steps: step2. Prepare codec instance & encode the function call.
442 * @tc.expected: step2. encode data equals to prepared binary data.
443 */
444 std::vector<uint8_t> encodeBuf;
445 StandardFunctionCodec codec;
446 codec.EncodeFunctionCall(functionCall, encodeBuf);
447 std::vector<uint8_t>::iterator encodeIter = encodeBuf.begin();
448 std::vector<uint8_t>::const_iterator encodeIter1 = MAP_ENCODE_RESULT.begin();
449
450 while (encodeIter != encodeBuf.end()) {
451 ASSERT_EQ(*encodeIter, *encodeIter1);
452 encodeIter++;
453 encodeIter1++;
454 }
455
456 /**
457 * @tc.steps: step3. Decode the binary to FunctionCall, check function Name & para type and value.
458 * @tc.expected: step3. check function name & para type and value.
459 */
460 FunctionCall result;
461 codec.DecodeFunctionCall(encodeBuf, result);
462 ASSERT_EQ(FUNCTION_NAME_VALUE, result.GetFuncName());
463 std::vector<CodecData> resultArgs = result.GetArgs();
464 ASSERT_EQ(resultArgs[0].GetType(), BufferDataType::TYPE_MAP);
465
466 std::map<std::string, std::string> data = resultArgs[0].GetMapValue();
467 std::map<std::string, std::string>::iterator iter = data.begin();
468 std::map<std::string, std::string>::const_iterator iter1 = FUNCTION_PARA_MAP.begin();
469
470 while (iter != data.end()) {
471 ASSERT_EQ(iter->first, iter1->first);
472 ASSERT_EQ(iter->second, iter1->second);
473 iter++;
474 iter1++;
475 }
476 }
477
478 } // namespace OHOS::Ace::Framework
479