1 /*
2 * Copyright (c) 2023 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 #include <gtest/gtest.h>
16
17 #include "clip/clip_plugin.h"
18 #include "config.h"
19 #include "pasteboard_hilog.h"
20 #include "serializable.h"
21
22 namespace OHOS::DistributedData {
23 using namespace testing::ext;
24 using namespace OHOS::MiscServices;
25 class SerializableTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 void CreateConfig(Config &config, const std::string &prefix = "");
32 void CreateComponent(Config::Component &component, int32_t index, const std::string &prefix);
33 Serializable::json ToJson(const std::string &str);
34 bool IsSame(Config &oldConfig, Config &newConfig);
35 static bool IsSame(Config::Component &oldComp, Config::Component &newComp);
36
37 template<typename T>
IsSame(std::vector<T> &olds, std::vector<T> &news)38 static bool IsSame(std::vector<T> &olds, std::vector<T> &news)
39 {
40 if (olds.size() != news.size()) {
41 return false;
42 }
43
44 bool isSame = true;
45 auto len = olds.size();
46 for (int i = 0; i < len; ++i) {
47 isSame = IsSame(olds[i], news[i]) && isSame;
48 }
49 return isSame;
50 }
51 };
52
SetUpTestCase(void)53 void SerializableTest::SetUpTestCase(void) {}
54
TearDownTestCase(void)55 void SerializableTest::TearDownTestCase(void) {}
56
SetUp(void)57 void SerializableTest::SetUp(void) {}
58
TearDown(void)59 void SerializableTest::TearDown(void) {}
60
ToJson(const std::string &str)61 Serializable::json SerializableTest::ToJson(const std::string &str)
62 {
63 return cJSON_Parse(str.c_str());
64 }
65
IsSame(Config::Component &oldComp, Config::Component &newComp)66 bool SerializableTest::IsSame(Config::Component &oldComp, Config::Component &newComp)
67 {
68 bool isSame = true;
69 isSame = oldComp.description == newComp.description;
70 isSame = oldComp.lib == newComp.lib && isSame;
71 isSame = oldComp.constructor == newComp.constructor && isSame;
72 isSame = oldComp.destructor == newComp.destructor && isSame;
73 isSame = oldComp.params == newComp.params && isSame;
74 return isSame;
75 }
76
IsSame(Config &oldConfig, Config &newConfig)77 bool SerializableTest::IsSame(Config &oldConfig, Config &newConfig)
78 {
79 bool isSame = true;
80 isSame = oldConfig.processLabel == newConfig.processLabel;
81 isSame = oldConfig.version == newConfig.version && isSame;
82 isSame = oldConfig.features == newConfig.features && isSame;
83 isSame = oldConfig.plugins == newConfig.plugins && isSame;
84 isSame = IsSame(oldConfig.components, newConfig.components) && isSame;
85 isSame = oldConfig.bundles == newConfig.bundles && isSame;
86 return isSame;
87 }
88
CreateComponent(Config::Component &component, int32_t index, const std::string &prefix)89 void SerializableTest::CreateComponent(Config::Component &component, int32_t index, const std::string &prefix)
90 {
91 component.description = prefix + "description" + std::to_string(index);
92 component.lib = prefix + "lib" + std::to_string(index);
93 component.constructor = prefix + "constructor" + std::to_string(index);
94 component.destructor = prefix + "destructor" + std::to_string(index);
95 component.params = prefix + "params" + std::to_string(index);
96 }
97
CreateConfig(Config &config, const std::string &prefix)98 void SerializableTest::CreateConfig(Config &config, const std::string &prefix)
99 {
100 config.processLabel = prefix + "processLabel";
101 config.version = prefix + "version";
102 config.features = { prefix + "feature1", prefix + "feature2" };
103 config.plugins = { prefix + "plugin1", prefix + "plugin2" };
104 Config::Component component1;
105 Config::Component component2;
106 int32_t index = 0;
107 CreateComponent(component1, index++, prefix);
108 CreateComponent(component2, index++, prefix);
109 config.components = { component1, component2 };
110 }
111
112 /**
113 * @tc.name: SerializableTest001
114 * @tc.desc: test serializable with invalid jsonStr .
115 * @tc.type: FUNC
116 * @tc.require:
117 * @tc.author:
118 */
HWTEST_F(SerializableTest, SerializableTest001, TestSize.Level0)119 HWTEST_F(SerializableTest, SerializableTest001, TestSize.Level0)
120 {
121 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest001 Start.");
122 Config config;
123 std::string jsonStr = "";
124 auto ret = config.Unmarshall(jsonStr);
125 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Unmarshall NULL.");
126 ASSERT_FALSE(ret);
127
128 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Unmarshall not null.");
129 jsonStr = "{ a }";
130 ret = config.Unmarshall(jsonStr);
131 ASSERT_FALSE(ret);
132 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
133 }
134
135 /**
136 * @tc.name: SerializableTest002
137 * @tc.desc: test serializable with valid jsonStr .
138 * @tc.type: FUNC
139 * @tc.require:
140 * @tc.author:
141 */
HWTEST_F(SerializableTest, SerializableTest002, TestSize.Level0)142 HWTEST_F(SerializableTest, SerializableTest002, TestSize.Level0)
143 {
144 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest002 Start.");
145
146 std::vector<uint8_t> vec = { 1, 2, 3, 4, 5 };
147 std::string jsonStr = "{\n"
148 " \"param1\": 2, \n"
149 " \"param2\": 3, \n"
150 " \"param3\": 4, \n"
151 " \"param4\": true, \n"
152 " \"param5\": [1, 2, 3, 4, 5] \n"
153 "}";
154
155 uint32_t res1;
156 int32_t res2;
157 int64_t res3;
158 bool res4;
159 std::vector<uint8_t> res5;
160
161 auto node = ToJson(jsonStr);
162 auto ret = Serializable::GetValue(node, GET_NAME(param1), res1);
163 ASSERT_TRUE(ret);
164 ASSERT_EQ(res1, 2);
165 ret = Serializable::GetValue(node, GET_NAME(param2), res2);
166 ASSERT_TRUE(ret);
167 ASSERT_EQ(res2, 3);
168 ret = Serializable::GetValue(node, GET_NAME(param3), res3);
169 ASSERT_TRUE(ret);
170 ASSERT_EQ(res3, 4);
171 ret = Serializable::GetValue(node, GET_NAME(param4), res4);
172 ASSERT_TRUE(ret);
173 ASSERT_TRUE(res4);
174 ret = Serializable::GetValue(node, GET_NAME(param5), res5);
175 ASSERT_TRUE(ret);
176 ASSERT_EQ(res5.size(), vec.size());
177 for (uint64_t i = 0; i < res5.size(); i++) {
178 ASSERT_EQ(res5[i], vec[i]);
179 }
180
181 cJSON_Delete(node);
182 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
183 }
184
185 /**
186 * @tc.name: SerializableTest003
187 * @tc.desc: test serializable GetValue and SetValue with invalid string value .
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author:
191 */
HWTEST_F(SerializableTest, SerializableTest003, TestSize.Level0)192 HWTEST_F(SerializableTest, SerializableTest003, TestSize.Level0)
193 {
194 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
195 std::string jsonStr = R"({"key":null})";
196 auto json = ToJson(jsonStr);
197 std::string value;
198 auto ret = Serializable::GetValue(json, "key", value);
199 ASSERT_FALSE(ret);
200 cJSON_Delete(json);
201
202 jsonStr = R"({"key":1})";
203 json = ToJson(jsonStr);
204 ret = Serializable::GetValue(json, "key", value);
205 ASSERT_FALSE(ret);
206 cJSON_Delete(json);
207 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
208 }
209
210 /**
211 * @tc.name: SerializableTest004
212 * @tc.desc: test serializable GetValue and SetValue with invalid uint32_t value .
213 * @tc.type: FUNC
214 * @tc.require:
215 * @tc.author:
216 */
HWTEST_F(SerializableTest, SerializableTest004, TestSize.Level0)217 HWTEST_F(SerializableTest, SerializableTest004, TestSize.Level0)
218 {
219 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
220 std::string jsonStr = R"({"key":null})";
221 auto json = ToJson(jsonStr);
222 uint32_t value;
223 auto ret = Serializable::GetValue(json, "key", value);
224 ASSERT_FALSE(ret);
225 cJSON_Delete(json);
226
227 jsonStr = R"({"key":"1"})";
228 json = ToJson(jsonStr);
229 ret = Serializable::GetValue(json, "key", value);
230 ASSERT_FALSE(ret);
231 cJSON_Delete(json);
232 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
233 }
234
235 /**
236 * @tc.name: SerializableTest005
237 * @tc.desc: test serializable GetValue and SetValue with invalid int32_t value .
238 * @tc.type: FUNC
239 * @tc.require:
240 * @tc.author:
241 */
HWTEST_F(SerializableTest, SerializableTest005, TestSize.Level0)242 HWTEST_F(SerializableTest, SerializableTest005, TestSize.Level0)
243 {
244 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
245 std::string jsonStr = R"({"key":null})";
246 auto json = ToJson(jsonStr);
247 int32_t value;
248 auto ret = Serializable::GetValue(json, "key", value);
249 ASSERT_FALSE(ret);
250 cJSON_Delete(json);
251
252 jsonStr = R"({"key":"1"})";
253 json = ToJson(jsonStr);
254 ret = Serializable::GetValue(json, "key", value);
255 ASSERT_FALSE(ret);
256 cJSON_Delete(json);
257 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
258 }
259
260 /**
261 * @tc.name: SerializableTest006
262 * @tc.desc: test serializable GetValue and SetValue with invalid int64_t value .
263 * @tc.type: FUNC
264 * @tc.require:
265 * @tc.author:
266 */
HWTEST_F(SerializableTest, SerializableTest006, TestSize.Level0)267 HWTEST_F(SerializableTest, SerializableTest006, TestSize.Level0)
268 {
269 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
270 std::string jsonStr = R"({"key":null})";
271 auto json = ToJson(jsonStr);
272 int64_t value;
273 auto ret = Serializable::GetValue(json, "key", value);
274 ASSERT_FALSE(ret);
275 cJSON_Delete(json);
276
277 jsonStr = R"({"key":"1"})";
278 json = ToJson(jsonStr);
279 ret = Serializable::GetValue(json, "key", value);
280 ASSERT_FALSE(ret);
281 cJSON_Delete(json);
282 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
283 }
284
285 /**
286 * @tc.name: SerializableTest007
287 * @tc.desc: test serializable GetValue and SetValue with invalid bool value .
288 * @tc.type: FUNC
289 * @tc.require:
290 * @tc.author:
291 */
HWTEST_F(SerializableTest, SerializableTest007, TestSize.Level0)292 HWTEST_F(SerializableTest, SerializableTest007, TestSize.Level0)
293 {
294 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
295 std::string jsonStr = R"({"key":null})";
296 auto json = ToJson(jsonStr);
297 bool value;
298 auto ret = Serializable::GetValue(json, "key", value);
299 ASSERT_FALSE(ret);
300 cJSON_Delete(json);
301
302 jsonStr = R"({"key":1})";
303 json = ToJson(jsonStr);
304 ret = Serializable::GetValue(json, "key", value);
305 ASSERT_FALSE(ret);
306 cJSON_Delete(json);
307 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
308 }
309
310 /**
311 * @tc.name: SerializableTest008
312 * @tc.desc: test serializable GetValue and SetValue with invalid std::vector<uint8_t> value .
313 * @tc.type: FUNC
314 * @tc.require:
315 * @tc.author:
316 */
HWTEST_F(SerializableTest, SerializableTest008, TestSize.Level0)317 HWTEST_F(SerializableTest, SerializableTest008, TestSize.Level0)
318 {
319 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
320 std::string jsonStr = R"({"key":null})";
321 auto json = ToJson(jsonStr);
322 std::vector<uint8_t> value;
323 auto ret = Serializable::GetValue(json, "key", value);
324 ASSERT_FALSE(ret);
325 cJSON_Delete(json);
326
327 jsonStr = R"({"key":"{1, 2, 3}"})";
328 json = ToJson(jsonStr);
329 ret = Serializable::GetValue(json, "key", value);
330 ASSERT_FALSE(ret);
331 cJSON_Delete(json);
332 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
333 }
334
335 /**
336 * @tc.name: SerializableTest009
337 * @tc.desc: test serializable SetValue with valid value.
338 * @tc.type: FUNC
339 * @tc.require:
340 * @tc.author:
341 */
HWTEST_F(SerializableTest, SerializableTest009, TestSize.Level0)342 HWTEST_F(SerializableTest, SerializableTest009, TestSize.Level0)
343 {
344 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
345 uint32_t param1 = 2;
346 int32_t param2 = 3;
347 int64_t param3 = 4;
348 bool param4 = false;
349 std::vector<uint8_t> param5 = { 1, 2, 3, 4, 5 };
350 Config config;
351 CreateConfig(config);
352
353 Serializable::json node = nullptr;
354 config.Marshal(node);
355 auto ret = Serializable::SetValue(node, param1, GET_NAME(param1));
356 ASSERT_TRUE(ret);
357 ret = Serializable::SetValue(node, param2, GET_NAME(param2));
358 ASSERT_TRUE(ret);
359 ret = Serializable::SetValue(node, param3, GET_NAME(param3));
360 ASSERT_TRUE(ret);
361 ret = Serializable::SetValue(node, param4, GET_NAME(param4));
362 ASSERT_TRUE(ret);
363 ret = Serializable::SetValue(node, param5, GET_NAME(param5));
364 ASSERT_TRUE(ret);
365 ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param1)));
366 ret = Serializable::GetValue(node, GET_NAME(param1), param1);
367 ASSERT_TRUE(ret);
368 ASSERT_EQ(param1, 2);
369 ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param2)));
370 ret = Serializable::GetValue(node, GET_NAME(param2), param2);
371 ASSERT_TRUE(ret);
372 ASSERT_EQ(param2, 3);
373 ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param3)));
374 ret = Serializable::GetValue(node, GET_NAME(param3), param3);
375 ASSERT_TRUE(ret);
376 ASSERT_EQ(param3, 4);
377 ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param4)));
378 ret = Serializable::GetValue(node, GET_NAME(param4), param4);
379 ASSERT_TRUE(ret);
380 ASSERT_EQ(param4, false);
381 ASSERT_TRUE(cJSON_HasObjectItem(node, GET_NAME(param5)));
382 cJSON_Delete(node);
383 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
384 }
385
386 /**
387 * @tc.name: SerializableTest010
388 * @tc.desc: test serializable SetValue with valid value.
389 * @tc.type: FUNC
390 * @tc.require:
391 * @tc.author:
392 */
HWTEST_F(SerializableTest, SerializableTest010, TestSize.Level0)393 HWTEST_F(SerializableTest, SerializableTest010, TestSize.Level0)
394 {
395 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "SerializableTest010 Start.");
396 Config config;
397 CreateConfig(config);
398
399 Config newConfig;
400 CreateConfig(newConfig, "new");
401
402 Serializable::json node = nullptr;
403 config.Marshal(node);
404 auto ret = Serializable::SetValue(node, newConfig.version, GET_NAME(version));
405 ASSERT_TRUE(ret);
406 ret = Serializable::SetValue(node, newConfig.processLabel, GET_NAME(processLabel));
407 ASSERT_TRUE(ret);
408 ret = Serializable::SetValue(node, newConfig.features, GET_NAME(features));
409 ASSERT_TRUE(ret);
410 ret = Serializable::SetValue(node, newConfig.plugins, GET_NAME(plugins));
411 ASSERT_TRUE(ret);
412 ret = Serializable::SetValue(node, newConfig.components, GET_NAME(components));
413 ASSERT_TRUE(ret);
414 config.Unmarshal(node);
415 ASSERT_EQ(IsSame(config, newConfig), true);
416 cJSON_Delete(node);
417 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
418 }
419
420 /**
421 * @tc.name: SerializableTest011
422 * @tc.desc: test serializable Unmarshall with valid jsonstr.
423 * @tc.type: FUNC
424 * @tc.require:
425 * @tc.author:
426 */
HWTEST_F(SerializableTest, SerializableTest011, TestSize.Level0)427 HWTEST_F(SerializableTest, SerializableTest011, TestSize.Level0)
428 {
429 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
430 std::string jsonStr = "{\n"
431 " \"processLabel\": \"processLabel\", \n"
432 " \"version\": \"version\", \n"
433 " \"features\": [\"features1\", \"features2\"], \n"
434 " \"plugins\": [\"plugins1\", \"plugins2\"], \n"
435 " \"components\": [{ \n"
436 " \"description\": \"description1\",\n"
437 " \"lib\": \"lib1\", \n"
438 " \"constructor\": \"constructor1\",\n"
439 " \"destructor\": \"destructor1\", \n"
440 " \"params\": \"params1\" \n"
441 " }, { \n"
442 " \"description\": \"description2\",\n"
443 " \"lib\": \"lib2\", \n"
444 " \"constructor\": \"constructor2\",\n"
445 " \"destructor\": \"destructor2\", \n"
446 " \"params\": \"params2\" \n"
447 " }] \n"
448 "}";
449
450 Config config;
451 auto ret = config.Unmarshall(jsonStr);
452 ASSERT_TRUE(ret);
453 ASSERT_EQ(config.processLabel, "processLabel");
454 ASSERT_EQ(config.version, "version");
455 ASSERT_EQ(config.features[0], "features1");
456 ASSERT_EQ(config.features[1], "features2");
457 ASSERT_EQ(config.plugins[0], "plugins1");
458 ASSERT_EQ(config.plugins[1], "plugins2");
459 ASSERT_EQ(config.components[0].description, "description1");
460 ASSERT_EQ(config.components[0].lib, "lib1");
461 ASSERT_EQ(config.components[0].constructor, "constructor1");
462 ASSERT_EQ(config.components[0].destructor, "destructor1");
463 ASSERT_EQ(config.components[0].params, "params1");
464 ASSERT_EQ(config.components[1].description, "description2");
465 ASSERT_EQ(config.components[1].lib, "lib2");
466 ASSERT_EQ(config.components[1].constructor, "constructor2");
467 ASSERT_EQ(config.components[1].destructor, "destructor2");
468 ASSERT_EQ(config.components[1].params, "params2");
469 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
470 }
471
472 /**
473 * @tc.name: GlobalEventTest001
474 * @tc.desc: test GlobalEvent serializable.
475 * @tc.type: FUNC
476 * @tc.require:
477 * @tc.author:
478 */
HWTEST_F(SerializableTest, GlobalEventTest001, TestSize.Level0)479 HWTEST_F(SerializableTest, GlobalEventTest001, TestSize.Level0)
480 {
481 ClipPlugin::GlobalEvent event;
482 event.deviceId = "deviceId";
483 event.expiration = 1;
484 event.seqId = 0;
485 std::string data = event.Marshall();
486 ClipPlugin::GlobalEvent event1;
487 if (!event1.Unmarshall(data)) {
488 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_INNERKIT, "Unmarshall event failed.");
489 }
490 ASSERT_TRUE(event == event1);
491 }
492 } // namespace OHOS::DistributedData