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 } // namespace UpdaterUt
217