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