• 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 "config.h"
16 #include "pasteboard_hilog.h"
17 #include "serializable.h"
18 #include <gtest/gtest.h>
19 
20 namespace OHOS::DistributedData {
21 using namespace testing::ext;
22 using namespace OHOS::MiscServices;
23 class SerializableTest : public testing::Test {
24 public:
25     static void SetUpTestCase(void);
26     static void TearDownTestCase(void);
27     void SetUp();
28     void TearDown();
29     void CreateConfig(Config &config);
30     void CreateComponent(Config::Component &component, int32_t index);
31 };
32 
SetUpTestCase(void)33 void SerializableTest::SetUpTestCase(void)
34 {
35 }
36 
TearDownTestCase(void)37 void SerializableTest::TearDownTestCase(void)
38 {
39 }
40 
SetUp(void)41 void SerializableTest::SetUp(void)
42 {
43 }
44 
TearDown(void)45 void SerializableTest::TearDown(void)
46 {
47 }
48 
CreateComponent(Config::Component & component,int32_t index)49 void SerializableTest::CreateComponent(Config::Component &component, int32_t index)
50 {
51     component.description = "description" + std::to_string(index);
52     component.lib = "lib" + std::to_string(index);
53     component.constructor = "constructor" + std::to_string(index);
54     component.destructor = "destructor" + std::to_string(index);
55     component.params = "params" + std::to_string(index);
56 }
57 
CreateConfig(Config & config)58 void SerializableTest::CreateConfig(Config &config)
59 {
60     config.processLabel = "processLabel";
61     config.version = "version";
62     config.features = { "feature1", "feature2" };
63     config.plugins = { "plugin1", "plugin2" };
64     Config::Component component1;
65     Config::Component component2;
66     int32_t index = 0;
67     CreateComponent(component1, index++);
68     CreateComponent(component2, index++);
69     config.components = { component1, component2 };
70 }
71 
72 /**
73 * @tc.name: SerializableTest001
74 * @tc.desc: test serializable with invalid jsonStr .
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author:
78 */
79 HWTEST_F(SerializableTest, SerializableTest001, TestSize.Level0)
80 {
81     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
82     std::string jsonStr;
83     auto json = Serializable::ToJson(jsonStr);
84     ASSERT_TRUE(cJSON_IsNull(&json));
85 
86     Config config;
87     auto ret = config.Unmarshall(jsonStr);
88     ASSERT_FALSE(ret);
89 
90     jsonStr = "{ a }";
91     json = Serializable::ToJson(jsonStr);
92     ASSERT_TRUE(cJSON_IsNull(&json));
93 
94     ret = config.Unmarshall(jsonStr);
95     ASSERT_FALSE(ret);
96     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
97 }
98 
99 /**
100 * @tc.name: SerializableTest002
101 * @tc.desc: test serializable with valid jsonStr .
102 * @tc.type: FUNC
103 * @tc.require:
104 * @tc.author:
105 */
106 HWTEST_F(SerializableTest, SerializableTest002, TestSize.Level0)
107 {
108     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
109 
110     std::vector<uint8_t> vec = { 1, 2, 3, 4, 5 };
111     std::string jsonStr = "{\n"
112                           "        \"param1\":       2,                                \n"
113                           "        \"param2\":       3,                                \n"
114                           "        \"param3\":       4,                                \n"
115                           "        \"param4\":       true,                             \n"
116                           "        \"param5\":       [1, 2, 3, 4, 5]                   \n"
117                           "}";
118 
119     uint32_t res1;
120     int32_t res2;
121     int64_t res3;
122     bool res4;
123     std::vector<uint8_t> res5;
124 
125     auto node = Serializable::ToJson(jsonStr);
126 
127     auto ret = Serializable::GetValue(node, GET_NAME(param1), res1);
128     ASSERT_TRUE(ret);
129     ASSERT_EQ(res1, 2);
130     ret = Serializable::GetValue(node, GET_NAME(param2), res2);
131     ASSERT_TRUE(ret);
132     ASSERT_EQ(res2, 3);
133     ret = Serializable::GetValue(node, GET_NAME(param3), res3);
134     ASSERT_TRUE(ret);
135     ASSERT_EQ(res3, 4);
136     ret = Serializable::GetValue(node, GET_NAME(param4), res4);
137     ASSERT_TRUE(ret);
138     ASSERT_TRUE(res4);
139     ret = Serializable::GetValue(node, GET_NAME(param5), res5);
140     ASSERT_TRUE(ret);
141     ASSERT_EQ(res5.size(), vec.size());
142     for (uint64_t i = 0; i < res5.size(); i++) {
143         ASSERT_EQ(res5[i], vec[i]);
144     }
145     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
146 }
147 
148 /**
149 * @tc.name: SerializableTest003
150 * @tc.desc: test serializable GetValue and SetValue with invalid string value .
151 * @tc.type: FUNC
152 * @tc.require:
153 * @tc.author:
154 */
155 HWTEST_F(SerializableTest, SerializableTest003, TestSize.Level0)
156 {
157     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
158     std::string jsonStr = R"({"key":null})";
159     auto json = Serializable::ToJson(jsonStr);
160     std::string value;
161     auto ret = Serializable::GetValue(json, "key", value);
162     ASSERT_FALSE(ret);
163 
164     jsonStr = R"({"key":1})";
165     json = Serializable::ToJson(jsonStr);
166     ret = Serializable::GetValue(json, "key", value);
167     ASSERT_FALSE(ret);
168     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
169 }
170 
171 /**
172 * @tc.name: SerializableTest004
173 * @tc.desc: test serializable GetValue and SetValue with invalid uint32_t value .
174 * @tc.type: FUNC
175 * @tc.require:
176 * @tc.author:
177 */
178 HWTEST_F(SerializableTest, SerializableTest004, TestSize.Level0)
179 {
180     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
181     std::string jsonStr = R"({"key":null})";
182     auto json = Config::ToJson(jsonStr);
183     uint32_t value;
184     auto ret = Serializable::GetValue(json, "key", value);
185     ASSERT_FALSE(ret);
186 
187     jsonStr = R"({"key":"1"})";
188     json = Serializable::ToJson(jsonStr);
189     ret = Serializable::GetValue(json, "key", value);
190     ASSERT_FALSE(ret);
191     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
192 }
193 
194 /**
195 * @tc.name: SerializableTest005
196 * @tc.desc: test serializable GetValue and SetValue with invalid int32_t value .
197 * @tc.type: FUNC
198 * @tc.require:
199 * @tc.author:
200 */
201 HWTEST_F(SerializableTest, SerializableTest005, TestSize.Level0)
202 {
203     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
204     std::string jsonStr = R"({"key":null})";
205     auto json = Config::ToJson(jsonStr);
206     int32_t value;
207     auto ret = Serializable::GetValue(json, "key", value);
208     ASSERT_FALSE(ret);
209 
210     jsonStr = R"({"key":"1"})";
211     json = Serializable::ToJson(jsonStr);
212     ret = Serializable::GetValue(json, "key", value);
213     ASSERT_FALSE(ret);
214     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
215 }
216 
217 /**
218 * @tc.name: SerializableTest006
219 * @tc.desc: test serializable GetValue and SetValue with invalid int64_t value .
220 * @tc.type: FUNC
221 * @tc.require:
222 * @tc.author:
223 */
224 HWTEST_F(SerializableTest, SerializableTest006, TestSize.Level0)
225 {
226     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
227     std::string jsonStr = R"({"key":null})";
228     auto json = Serializable::ToJson(jsonStr);
229     int64_t value;
230     auto ret = Serializable::GetValue(json, "key", value);
231     ASSERT_FALSE(ret);
232 
233     jsonStr = R"({"key":"1"})";
234     json = Serializable::ToJson(jsonStr);
235     ret = Serializable::GetValue(json, "key", value);
236     ASSERT_FALSE(ret);
237     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
238 }
239 
240 /**
241 * @tc.name: SerializableTest007
242 * @tc.desc: test serializable GetValue and SetValue with invalid bool value .
243 * @tc.type: FUNC
244 * @tc.require:
245 * @tc.author:
246 */
247 HWTEST_F(SerializableTest, SerializableTest007, TestSize.Level0)
248 {
249     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
250     std::string jsonStr = R"({"key":null})";
251     auto json = Serializable::ToJson(jsonStr);
252     bool value;
253     auto ret = Serializable::GetValue(json, "key", value);
254     ASSERT_FALSE(ret);
255 
256     jsonStr = R"({"key":1})";
257     json = Serializable::ToJson(jsonStr);
258     ret = Serializable::GetValue(json, "key", value);
259     ASSERT_FALSE(ret);
260     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
261 }
262 
263 /**
264 * @tc.name: SerializableTest008
265 * @tc.desc: test serializable GetValue and SetValue with invalid std::vector<uint8_t> value .
266 * @tc.type: FUNC
267 * @tc.require:
268 * @tc.author:
269 */
270 HWTEST_F(SerializableTest, SerializableTest008, TestSize.Level0)
271 {
272     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
273     std::string jsonStr = R"({"key":null})";
274     auto json = Serializable::ToJson(jsonStr);
275     std::vector<uint8_t> value;
276     auto ret = Serializable::GetValue(json, "key", value);
277     ASSERT_FALSE(ret);
278 
279     jsonStr = R"({"key":"{1, 2, 3}"})";
280     json = Serializable::ToJson(jsonStr);
281     ret = Serializable::GetValue(json, "key", value);
282     ASSERT_FALSE(ret);
283     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
284 }
285 
286 /**
287 * @tc.name: SerializableTest009
288 * @tc.desc: test serializable SetValue with valid value.
289 * @tc.type: FUNC
290 * @tc.require:
291 * @tc.author:
292 */
293 HWTEST_F(SerializableTest, SerializableTest009, TestSize.Level0)
294 {
295     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
296     uint32_t param1 = 2;
297     int32_t param2 = 3;
298     int64_t param3 = 4;
299     bool param4 = false;
300     std::vector<uint8_t> param5 = { 1, 2, 3, 4, 5 };
301     Config config;
302     CreateConfig(config);
303 
304     auto node = config.Marshall();
305     ASSERT_FALSE(cJSON_IsNull(&node));
306 
307     auto ret = Serializable::SetValue(node, param1, GET_NAME(param1));
308     ASSERT_TRUE(ret);
309     ret = Serializable::SetValue(node, param2, GET_NAME(param2));
310     ASSERT_TRUE(ret);
311     ret = Serializable::SetValue(node, param3, GET_NAME(param3));
312     ASSERT_TRUE(ret);
313     ret = Serializable::SetValue(node, param4, GET_NAME(param4));
314     ASSERT_TRUE(ret);
315     ret = Serializable::SetValue(node, param5, GET_NAME(param5));
316     ASSERT_TRUE(ret);
317     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(processLabel)));
318     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(version)));
319     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(features)));
320     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(plugins)));
321     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(components)));
322     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(param1)));
323     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(param2)));
324     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(param3)));
325     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(param4)));
326     ASSERT_TRUE(cJSON_HasObjectItem(&node, GET_NAME(param5)));
327     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
328 }
329 
330 /**
331 * @tc.name: SerializableTest010
332 * @tc.desc: test serializable Unmarshall with valid jsonstr.
333 * @tc.type: FUNC
334 * @tc.require:
335 * @tc.author:
336 */
337 HWTEST_F(SerializableTest, SerializableTest010, TestSize.Level0)
338 {
339     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
340     std::string jsonStr = "{\n"
341                           "        \"processLabel\": \"processLabel\",                \n"
342                           "        \"version\":      \"version\",                     \n"
343                           "        \"features\":     [\"features1\", \"features2\"], \n"
344                           "        \"plugins\":      [\"plugins1\", \"plugins2\"],   \n"
345                           "        \"components\":   [{                                \n"
346                           "                        \"description\":  \"description1\",\n"
347                           "                        \"lib\":  \"lib1\",                \n"
348                           "                        \"constructor\":  \"constructor1\",\n"
349                           "                        \"destructor\":   \"destructor1\", \n"
350                           "                        \"params\":       \"params1\"      \n"
351                           "                }, {                                      \n"
352                           "                        \"description\":  \"description2\",\n"
353                           "                        \"lib\":  \"lib2\",                \n"
354                           "                        \"constructor\":  \"constructor2\",\n"
355                           "                        \"destructor\":   \"destructor2\", \n"
356                           "                        \"params\":       \"params2\"      \n"
357                           "                }]                                      \n"
358                           "}";
359 
360     Config config;
361     auto ret = config.Unmarshall(jsonStr);
362     ASSERT_TRUE(ret);
363     ASSERT_EQ(config.processLabel, "processLabel");
364     ASSERT_EQ(config.version, "version");
365     ASSERT_EQ(config.features[0], "features1");
366     ASSERT_EQ(config.features[1], "features2");
367     ASSERT_EQ(config.plugins[0], "plugins1");
368     ASSERT_EQ(config.plugins[1], "plugins2");
369     ASSERT_EQ(config.components[0].description, "description1");
370     ASSERT_EQ(config.components[0].lib, "lib1");
371     ASSERT_EQ(config.components[0].constructor, "constructor1");
372     ASSERT_EQ(config.components[0].destructor, "destructor1");
373     ASSERT_EQ(config.components[0].params, "params1");
374     ASSERT_EQ(config.components[1].description, "description2");
375     ASSERT_EQ(config.components[1].lib, "lib2");
376     ASSERT_EQ(config.components[1].constructor, "constructor2");
377     ASSERT_EQ(config.components[1].destructor, "destructor2");
378     ASSERT_EQ(config.components[1].params, "params2");
379     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
380 }
381 } // namespace OHOS::DistributedData