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