• 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 };
30 
SetUpTestCase(void)31 void SerializableTest::SetUpTestCase(void)
32 {
33 }
34 
TearDownTestCase(void)35 void SerializableTest::TearDownTestCase(void)
36 {
37 }
38 
SetUp(void)39 void SerializableTest::SetUp(void)
40 {
41 }
42 
TearDown(void)43 void SerializableTest::TearDown(void)
44 {
45 }
46 
47 /**
48 * @tc.name: SerializableTest001
49 * @tc.desc: test serializable with invalid jsonStr .
50 * @tc.type: FUNC
51 * @tc.require:
52 * @tc.author:
53 */
54 HWTEST_F(SerializableTest, SerializableTest001, TestSize.Level0)
55 {
56     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
57     std::string jsonStr;
58     auto json = Config::ToJson(jsonStr);
59     ASSERT_TRUE(json.is_null());
60 
61     Config config;
62     auto ret = config.Unmarshall(jsonStr);
63     ASSERT_FALSE(ret);
64 
65     jsonStr = "{ a }";
66     json = Config::ToJson(jsonStr);
67     ASSERT_TRUE(json.is_null());
68 
69     ret = config.Unmarshall(jsonStr);
70     ASSERT_FALSE(ret);
71     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
72 }
73 
74 /**
75 * @tc.name: SerializableTest002
76 * @tc.desc: test serializable with valid jsonStr .
77 * @tc.type: FUNC
78 * @tc.require:
79 * @tc.author:
80 */
81 HWTEST_F(SerializableTest, SerializableTest002, TestSize.Level0)
82 {
83     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
84     std::string jsonStr = R"({"key":"key1", "passwd":"*******"})";
85     auto json = Config::ToJson(jsonStr);
86     ASSERT_TRUE(!json.is_null());
87     std::string value1;
88     auto ret = Config::GetValue(json, "key", value1);
89     ASSERT_TRUE(ret);
90     ASSERT_EQ(value1, "key1");
91 
92     ret = Config::GetValue(json, "passwd", value1);
93     ASSERT_TRUE(ret);
94     ASSERT_EQ(value1, "*******");
95     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
96 }
97 
98 /**
99 * @tc.name: SerializableTest003
100 * @tc.desc: test serializable GetValue and SetValue with invalid string value .
101 * @tc.type: FUNC
102 * @tc.require:
103 * @tc.author:
104 */
105 HWTEST_F(SerializableTest, SerializableTest003, TestSize.Level0)
106 {
107     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
108     std::string jsonStr = R"({"key":null})";
109     auto json = Config::ToJson(jsonStr);
110     std::string value;
111     auto ret = Config::GetValue(json, "key", value);
112     ASSERT_FALSE(ret);
113 
114     jsonStr = R"({"key":1})";
115     json = Config::ToJson(jsonStr);
116     ret = Config::GetValue(json, "key", value);
117     ASSERT_FALSE(ret);
118     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
119 }
120 
121 /**
122 * @tc.name: SerializableTest004
123 * @tc.desc: test serializable GetValue and SetValue with invalid uint32_t value .
124 * @tc.type: FUNC
125 * @tc.require:
126 * @tc.author:
127 */
128 HWTEST_F(SerializableTest, SerializableTest004, TestSize.Level0)
129 {
130     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
131     std::string jsonStr = R"({"key":null})";
132     auto json = Config::ToJson(jsonStr);
133     uint32_t value;
134     auto ret = Config::GetValue(json, "key", value);
135     ASSERT_FALSE(ret);
136 
137     jsonStr = R"({"key":-1})";
138     json = Config::ToJson(jsonStr);
139     ret = Config::GetValue(json, "key", value);
140     ASSERT_FALSE(ret);
141     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
142 }
143 
144 /**
145 * @tc.name: SerializableTest005
146 * @tc.desc: test serializable GetValue and SetValue with invalid int32_t value .
147 * @tc.type: FUNC
148 * @tc.require:
149 * @tc.author:
150 */
151 HWTEST_F(SerializableTest, SerializableTest005, TestSize.Level0)
152 {
153     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
154     std::string jsonStr = R"({"key":null})";
155     auto json = Config::ToJson(jsonStr);
156     int32_t value;
157     auto ret = Config::GetValue(json, "key", value);
158     ASSERT_FALSE(ret);
159 
160     jsonStr = R"({"key":"1"})";
161     json = Config::ToJson(jsonStr);
162     ret = Config::GetValue(json, "key", value);
163     ASSERT_FALSE(ret);
164     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
165 }
166 
167 /**
168 * @tc.name: SerializableTest006
169 * @tc.desc: test serializable GetValue and SetValue with invalid int64_t value .
170 * @tc.type: FUNC
171 * @tc.require:
172 * @tc.author:
173 */
174 HWTEST_F(SerializableTest, SerializableTest006, TestSize.Level0)
175 {
176     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
177     std::string jsonStr = R"({"key":null})";
178     auto json = Config::ToJson(jsonStr);
179     int64_t value;
180     auto ret = Config::GetValue(json, "key", value);
181     ASSERT_FALSE(ret);
182 
183     jsonStr = R"({"key":"1"})";
184     json = Config::ToJson(jsonStr);
185     ret = Config::GetValue(json, "key", value);
186     ASSERT_FALSE(ret);
187     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
188 }
189 
190 /**
191 * @tc.name: SerializableTest007
192 * @tc.desc: test serializable GetValue and SetValue with invalid bool value .
193 * @tc.type: FUNC
194 * @tc.require:
195 * @tc.author:
196 */
197 HWTEST_F(SerializableTest, SerializableTest007, TestSize.Level0)
198 {
199     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
200     std::string jsonStr = R"({"key":null})";
201     auto json = Config::ToJson(jsonStr);
202     bool value;
203     auto ret = Config::GetValue(json, "key", value);
204     ASSERT_FALSE(ret);
205 
206     jsonStr = R"({"key":1})";
207     json = Config::ToJson(jsonStr);
208     ret = Config::GetValue(json, "key", value);
209     ASSERT_FALSE(ret);
210     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
211 }
212 
213 /**
214 * @tc.name: SerializableTest008
215 * @tc.desc: test serializable GetValue and SetValue with invalid std::vector<uint8_t> value .
216 * @tc.type: FUNC
217 * @tc.require:
218 * @tc.author:
219 */
220 HWTEST_F(SerializableTest, SerializableTest008, TestSize.Level0)
221 {
222     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
223     std::string jsonStr = R"({"key":null})";
224     auto json = Config::ToJson(jsonStr);
225     std::vector<uint8_t> value;
226     auto ret = Config::GetValue(json, "key", value);
227     ASSERT_FALSE(ret);
228 
229     jsonStr = R"({"key":"{1, 2, 3}"})";
230     json = Config::ToJson(jsonStr);
231     ret = Config::GetValue(json, "key", value);
232     ASSERT_FALSE(ret);
233     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
234 }
235 
236 /**
237 * @tc.name: SerializableTest009
238 * @tc.desc: test serializable GetValue and SetValue with valid sub node.
239 * @tc.type: FUNC
240 * @tc.require:
241 * @tc.author:
242 */
243 HWTEST_F(SerializableTest, SerializableTest009, TestSize.Level0)
244 {
245     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "Start.");
246     std::string param1 = "test";
247     uint32_t param2 = 2;
248     int32_t param3 = 3;
249     int64_t param4 = 4;
250     bool param5 = false;
251     std::vector<uint8_t> param6 = { 1, 2, 3, 4, 5 };
252     Serializable::json node;
253     auto ret = Config::SetValue(node[GET_NAME(param1)], param1);
254     ASSERT_TRUE(ret);
255     ret = Config::SetValue(node[GET_NAME(param2)], param2);
256     ASSERT_TRUE(ret);
257     ret = Config::SetValue(node[GET_NAME(param3)], param3);
258     ASSERT_TRUE(ret);
259     ret = Config::SetValue(node[GET_NAME(param4)], param4);
260     ASSERT_TRUE(ret);
261     ret = Config::SetValue(node[GET_NAME(param5)], param5);
262     ASSERT_TRUE(ret);
263     ret = Config::SetValue(node[GET_NAME(param6)], param6);
264     ASSERT_TRUE(ret);
265 
266     std::string res1;
267     uint32_t res2;
268     int32_t res3;
269     int64_t res4;
270     bool res5;
271     std::vector<uint8_t> res6;
272     ret = Config::GetValue(node, GET_NAME(param1), res1);
273     ASSERT_TRUE(ret);
274     ASSERT_EQ(res1, param1);
275     ret = Config::GetValue(node, GET_NAME(param2), res2);
276     ASSERT_TRUE(ret);
277     ASSERT_EQ(res2, param2);
278     ret = Config::GetValue(node, GET_NAME(param3), res3);
279     ASSERT_TRUE(ret);
280     ASSERT_EQ(res3, param3);
281     ret = Config::GetValue(node, GET_NAME(param4), res4);
282     ASSERT_TRUE(ret);
283     ASSERT_EQ(res4, param4);
284     ret = Config::GetValue(node, GET_NAME(param5), res5);
285     ASSERT_TRUE(ret);
286     ASSERT_EQ(res5, param5);
287     ret = Config::GetValue(node, GET_NAME(param6), res6);
288     ASSERT_TRUE(ret);
289     ASSERT_EQ(res1.size(), param1.size());
290     for (uint64_t i = 0; i < res1.size(); i++) {
291         ASSERT_EQ(res1[i], param1[i]);
292     }
293     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_INNERKIT, "End.");
294 }
295 } // namespace OHOS::DistributedData