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