1 /*
2 * Copyright (c) 2022 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
16 #include "utils_json_test.h"
17 #include <fstream>
18 #include "cJSON.h"
19 #include "json_node.h"
20 #include "utils.h"
21
22 using namespace std::literals;
23 using namespace std;
24 using namespace testing::ext;
25 using namespace Updater;
26 using namespace Utils;
27
28 namespace UpdaterUt {
29 // do something at the each function begining
SetUp(void)30 void UtilsJsonNodeUnitTest::SetUp(void)
31 {
32 cout << "Updater Unit UtilsJsonNodeUnitTest Begin!" << endl;
33 }
34
35 // do something at the each function end
TearDown(void)36 void UtilsJsonNodeUnitTest::TearDown(void)
37 {
38 cout << "Updater Unit UtilsJsonNodeUnitTest End!" << endl;
39 }
40
41 // init
SetUpTestCase(void)42 void UtilsJsonNodeUnitTest::SetUpTestCase(void)
43 {
44 cout << "SetUpTestCase" << endl;
45 }
46
47 // end
TearDownTestCase(void)48 void UtilsJsonNodeUnitTest::TearDownTestCase(void)
49 {
50 cout << "TearDownTestCase" << endl;
51 }
52
53 HWTEST_F(UtilsJsonNodeUnitTest, TestGetKey, TestSize.Level0)
54 {
55 {
56 std::string str = R"({"key": "value1"})";
57 JsonNode node(str);
58 EXPECT_EQ(node.Key(), std::nullopt);
59 EXPECT_EQ(node["key"].Key(), "key");
60 }
61 {
62 std::string str = R"({"key"})";
63 JsonNode node(str);
64 EXPECT_EQ(node.Key(), std::nullopt);
65 }
66 }
67
68 HWTEST_F(UtilsJsonNodeUnitTest, TestEqualTypeNotMatched, TestSize.Level0)
69 {
70 std::string str = R"({"key": "value1"})";
71 JsonNode node(str);
72 EXPECT_EQ((node["key"] == 1), false);
73 }
74
75 HWTEST_F(UtilsJsonNodeUnitTest, TestStrNode, TestSize.Level0)
76 {
77 std::string str = R"({"key": "value1"})";
78 JsonNode node(str);
79 EXPECT_EQ(node["key"].Type(), NodeType::STRING);
80 EXPECT_EQ(node["key"], "value1");
81 }
82
83 HWTEST_F(UtilsJsonNodeUnitTest, TestIntNode, TestSize.Level0)
84 {
85 std::string str = R"({"key": 1})";
86 JsonNode node(str);
87 EXPECT_EQ(node["key"].Type(), NodeType::INT);
88 EXPECT_EQ(node["key"], 1);
89 }
90
91 HWTEST_F(UtilsJsonNodeUnitTest, TestBoolNode, TestSize.Level0)
92 {
93 std::string str = R"({"key": true})";
94 JsonNode node(str);
95 EXPECT_EQ(node["key"].Type(), NodeType::BOOL);
96 EXPECT_EQ(node["key"], true);
97 }
98
99 HWTEST_F(UtilsJsonNodeUnitTest, TestArrNode, TestSize.Level0)
100 {
101 std::string str = R"({"key": [1, true, "value"]})";
102 JsonNode node(str);
103 EXPECT_EQ(node["key"].Type(), NodeType::ARRAY);
104 EXPECT_EQ(node["key"][0], 1);
105 EXPECT_EQ(node["key"][1], true);
106 EXPECT_EQ(node["key"][2], "value");
107 }
108
109
110 HWTEST_F(UtilsJsonNodeUnitTest, TestObjNode, TestSize.Level0)
111 {
112 std::string str = R"({"key": {"key" : "value"}}})";
113 JsonNode node(str);
114 EXPECT_EQ(node["key"].Type(), NodeType::OBJECT);
115 EXPECT_EQ(node["key"]["key"], "value");
116 }
117
118 HWTEST_F(UtilsJsonNodeUnitTest, TestNULNode, TestSize.Level0)
119 {
120 std::string str = R"({"key1": null})";
121 JsonNode node(str);
122 EXPECT_EQ(node["key1"].Type(), NodeType::NUL);
123 }
124
125 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidNode, TestSize.Level0)
126 {
127 std::string str = R"({"key":})";
128 JsonNode node(str);
129 EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
130 }
131
132 HWTEST_F(UtilsJsonNodeUnitTest, TestAll, TestSize.Level0)
133 {
134 static const std::string str = R"(
135 {
136 "A": "B",
137 "C": {
138 "D": "E",
139 "F": {
140 "G": {
141 "H": "I",
142 "J": 8879,
143 "K": {
144 "L": "M",
145 "N": ["O", "P"]
146 },
147 "L": true
148 }
149 }
150 }
151 })";
152 JsonNode node(str);
153 const JsonNode &nodeC = node["C"];
154 const JsonNode &nodeG = nodeC["F"]["G"];
155 EXPECT_EQ(node.Type(), NodeType::OBJECT);
156 EXPECT_EQ(node["A"], "B");
157 EXPECT_EQ(nodeC["D"], "E");
158 EXPECT_EQ(nodeG["H"], "I");
159 EXPECT_EQ(nodeG["J"], 8879);
160 EXPECT_EQ(nodeG["K"]["L"], "M");
161 EXPECT_EQ(nodeG["K"]["N"][0], "O");
162 EXPECT_EQ(nodeG["K"]["N"][1], "P");
163 EXPECT_EQ(nodeG["L"], true);
164 }
165
166 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidKey, TestSize.Level0)
167 {
168 std::string str = R"({"key": "value1"})";
169 JsonNode node(str);
170 EXPECT_EQ(node["key1"].Type(), NodeType::UNKNOWN);
171 }
172
173 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidIndex, TestSize.Level0)
174 {
175 {
176 std::string str = R"({"key": "value1"})";
177 JsonNode node(str);
178 EXPECT_EQ(node["key"].Type(), NodeType::STRING);
179 EXPECT_EQ(node["key1"].Type(), NodeType::UNKNOWN);
180 EXPECT_EQ(node["key"].Type(), NodeType::STRING);
181 }
182 {
183 std::string str = R"({"key": [1]})";
184 JsonNode node(str);
185 EXPECT_EQ(node["key"].Type(), NodeType::ARRAY);
186 EXPECT_EQ(node["key"][0].Type(), NodeType::INT);
187 EXPECT_EQ(node["key"][1].Type(), NodeType::UNKNOWN);
188 }
189 }
190
191 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath0, TestSize.Level0)
192 {
193 JsonNode node(Fs::path {R"(\invalid)"});
194 EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
195 }
196
197 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath1, TestSize.Level0)
198 {
199 JsonNode node(Fs::path {"/data/noexist"});
200 EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
201 }
202
203 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidPath2, TestSize.Level0)
204 {
205 constexpr auto invalidContent = R"({ "key" : "value")";
206 constexpr auto invalidJsonPath = "/tmp/tmp.json";
207 {
208 std::ofstream ofs(Fs::path {invalidJsonPath});
209 ofs << invalidContent;
210 ofs.flush();
211 }
212 JsonNode node(Fs::path {invalidJsonPath});
213 EXPECT_EQ(node.Type(), NodeType::UNKNOWN);
214 DeleteFile(invalidJsonPath);
215 }
216
217 HWTEST_F(UtilsJsonNodeUnitTest, TestVectorAssign, TestSize.Level0)
218 {
219 std::string str = R"({"key":[1, true, "value"]})";
220 JsonNode node {str};
221 constexpr int intVal = 2;
222 constexpr bool boolVal = false;
223 const char *strVal = "newValue";
224 int idx = 0;
225 node["key"][idx++] = intVal;
226 node["key"][idx++] = boolVal;
227 node["key"][idx++] = strVal;
228 EXPECT_EQ(node["key"][--idx], strVal);
229 EXPECT_EQ(node["key"][--idx], boolVal);
230 EXPECT_EQ(node["key"][--idx], intVal);
231 }
232
233 HWTEST_F(UtilsJsonNodeUnitTest, TestIntAssign, TestSize.Level0)
234 {
235 std::string str = R"({"key":1})";
236 JsonNode node {str};
237 constexpr int intVal = 2;
238 node["key"] = intVal;
239 EXPECT_EQ(node["key"], intVal);
240 }
241
242 HWTEST_F(UtilsJsonNodeUnitTest, TestBoolAssign, TestSize.Level0)
243 {
244 std::string str = R"({"key":true})";
245 JsonNode node {str};
246 constexpr bool boolVal = false;
247 node["key"] = boolVal;
248 EXPECT_EQ(node["key"], boolVal);
249 }
250
251 HWTEST_F(UtilsJsonNodeUnitTest, TestStrAssign, TestSize.Level0)
252 {
253 std::string str = R"({"key":"value"})";
254 JsonNode node {str};
255 const char *strVal = "newValue";
256 node["key"] = strVal;
257 EXPECT_EQ(node["key"], strVal);
258 }
259
260 HWTEST_F(UtilsJsonNodeUnitTest, TestMultiLevelAssign, TestSize.Level0)
261 {
262 std::string str = R"({"key1":{
263 "key2":1,
264 "key3":"value"
265 }})";
266 JsonNode node {str};
267 constexpr int newValue = 2;
268 node["key1"]["key2"] = newValue;
269 node["key1"]["key3"] = "value2";
270 EXPECT_EQ(node["key1"]["key2"], newValue);
271 EXPECT_EQ(node["key1"]["key3"], "value2");
272 }
273
274 HWTEST_F(UtilsJsonNodeUnitTest, TestInvalidAssign, TestSize.Level0)
275 {
276 std::string str = R"({"key" : 1})";
277 JsonNode node {str};
278 constexpr int value = 1;
279 node["key"] = false;
280 EXPECT_EQ(node["key"], value);
281 node["key"] = "newValue";
282 EXPECT_EQ(node["key"], value);
283 }
284 } // namespace UpdaterUt
285