1 /*
2 * Copyright (c) 2021-2025 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 <cstdio>
17
18 #include "utils/json_parser.h"
19 #include <gtest/gtest.h>
20
21 namespace ark::json_parser::test {
TEST(JsonParser,ParsePrimitive)22 TEST(JsonParser, ParsePrimitive)
23 {
24 auto str = R"(
25 {
26 "key_0" : "key_0.value",
27 "key_1" : "\"key_1\"\\.\u0020value\n"
28 }
29 )";
30
31 JsonObject obj(str);
32 ASSERT_TRUE(obj.IsValid());
33
34 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_0"), nullptr);
35 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_0"), "key_0.value");
36
37 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_1"), nullptr);
38 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_1"), "\"key_1\"\\. value\n");
39 }
40
41 // Issue: #14733
TEST(JsonParser,DISABLED_Arrays)42 TEST(JsonParser, DISABLED_Arrays)
43 {
44 auto str = R"(
45 {
46 "key_0" :
47 [
48 "elem0",
49 [ "elem1.0", "elem1.1" ],
50 "elem2"
51 ],
52 "key_1": []
53 }
54 )";
55
56 JsonObject obj(str);
57 ASSERT_TRUE(obj.IsValid());
58
59 ASSERT_NE(obj.GetValue<JsonObject::ArrayT>("key_0"), nullptr);
60 auto &mainArray = *obj.GetValue<JsonObject::ArrayT>("key_0");
61
62 // Check [0]:
63 ASSERT_NE(mainArray[0U].Get<JsonObject::StringT>(), nullptr);
64 ASSERT_EQ(*mainArray[0U].Get<JsonObject::StringT>(), "elem0");
65
66 // Check [1]:
67 ASSERT_NE(mainArray[1U].Get<JsonObject::ArrayT>(), nullptr);
68 auto &innerArray = *mainArray[1U].Get<JsonObject::ArrayT>();
69
70 ASSERT_NE(innerArray[0U].Get<JsonObject::StringT>(), nullptr);
71 ASSERT_EQ(*innerArray[0U].Get<JsonObject::StringT>(), "elem1.0");
72
73 ASSERT_NE(innerArray[1U].Get<JsonObject::StringT>(), nullptr);
74 ASSERT_EQ(*innerArray[1U].Get<JsonObject::StringT>(), "elem1.1");
75
76 // Check [2]:
77 ASSERT_NE(mainArray[2U].Get<JsonObject::StringT>(), nullptr);
78 ASSERT_EQ(*mainArray[2U].Get<JsonObject::StringT>(), "elem2");
79
80 ASSERT_NE(obj.GetValue<JsonObject::ArrayT>("key_1"), nullptr);
81 auto &emptyArray = *obj.GetValue<JsonObject::ArrayT>("key_1");
82
83 // Check [3]:
84 ASSERT_EQ(emptyArray.size(), 0U);
85 }
86
87 // Issue: #14733
TEST(JsonParser,DISABLED_NestedObject)88 TEST(JsonParser, DISABLED_NestedObject)
89 {
90 auto str = R"(
91 {
92 "key_0" : "key_0.value",
93 "repeated_key_1" : "repeated_key_1.value0",
94 "key_1" :
95 {
96 "key_0.0" : "key_0.0.value",
97 "repeated_key_1" : "repeated_key_1.value1",
98 "repeated_key_2" : "repeated_key_2.value0"
99 },
100 "repeated_key_2" : "repeated_key_2.value1",
101 "key_2" : {}
102 }
103 )";
104
105 JsonObject obj(str);
106 ASSERT_TRUE(obj.IsValid());
107
108 // Check key_0:
109 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_0"), nullptr);
110 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_0"), "key_0.value");
111
112 // Check repeated_key_1 (in main obj):
113 ASSERT_NE(obj.GetValue<JsonObject::StringT>("repeated_key_1"), nullptr);
114 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("repeated_key_1"), "repeated_key_1.value0");
115
116 // Inner object:
117 ASSERT_NE(obj.GetValue<JsonObject::JsonObjPointer>("key_1"), nullptr);
118 const auto *innerObj = obj.GetValue<JsonObject::JsonObjPointer>("key_1")->get();
119 ASSERT_NE(innerObj, nullptr);
120 ASSERT_TRUE(innerObj->IsValid());
121
122 // Check key_0.0:
123 ASSERT_NE(innerObj->GetValue<JsonObject::StringT>("key_0.0"), nullptr);
124 ASSERT_EQ(*innerObj->GetValue<JsonObject::StringT>("key_0.0"), "key_0.0.value");
125
126 // Check repeated_key_1:
127 ASSERT_NE(innerObj->GetValue<JsonObject::StringT>("repeated_key_1"), nullptr);
128 ASSERT_EQ(*innerObj->GetValue<JsonObject::StringT>("repeated_key_1"), "repeated_key_1.value1");
129
130 // Check repeated_key_2:
131 ASSERT_NE(innerObj->GetValue<JsonObject::StringT>("repeated_key_2"), nullptr);
132 ASSERT_EQ(*innerObj->GetValue<JsonObject::StringT>("repeated_key_2"), "repeated_key_2.value0");
133
134 // Check repeated_key_2 (in main obj):
135 ASSERT_NE(obj.GetValue<JsonObject::StringT>("repeated_key_2"), nullptr);
136 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("repeated_key_2"), "repeated_key_2.value1");
137
138 // Check empty inner object (key_2):
139 ASSERT_NE(obj.GetValue<JsonObject::JsonObjPointer>("key_2"), nullptr);
140 const auto *emptyObj = obj.GetValue<JsonObject::JsonObjPointer>("key_2")->get();
141 ASSERT_NE(emptyObj, nullptr);
142 ASSERT_TRUE(emptyObj->IsValid());
143 ASSERT_EQ(emptyObj->GetSize(), 0U);
144 }
145
146 // Issue: #14733
TEST(JsonParser,DISABLED_Null)147 TEST(JsonParser, DISABLED_Null)
148 {
149 auto str = R"(
150 {
151 "key": null
152 }
153 )";
154
155 JsonObject obj(str);
156 ASSERT_TRUE(obj.IsValid());
157
158 ASSERT_NE(obj.GetValue<JsonObject::JsonObjPointer>("key"), nullptr);
159 ASSERT_EQ(*obj.GetValue<JsonObject::JsonObjPointer>("key"), nullptr);
160 }
161
162 // Issue: #14733
TEST(JsonParser,DISABLED_Numbers)163 TEST(JsonParser, DISABLED_Numbers)
164 {
165 auto str = R"(
166 {
167 "key_0" : 0,
168 "key_1" : 128,
169 "key_2" : -256,
170 "key_3" : .512,
171 "key_4" : 1.024,
172 "key_5" : -204.8
173 }
174 )";
175
176 JsonObject obj(str);
177 ASSERT_TRUE(obj.IsValid());
178
179 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_0"), nullptr);
180 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_0"), 0U);
181
182 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_1"), nullptr);
183 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_1"), 128U);
184
185 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_2"), nullptr);
186 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_2"), -256L);
187
188 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_3"), nullptr);
189 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_3"), .512);
190
191 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_4"), nullptr);
192 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_4"), 1.024);
193
194 ASSERT_NE(obj.GetValue<JsonObject::NumT>("key_5"), nullptr);
195 ASSERT_EQ(*obj.GetValue<JsonObject::NumT>("key_5"), -204.8);
196 }
197
TEST(JsonParser,Boolean)198 TEST(JsonParser, Boolean)
199 {
200 auto str = R"(
201 {
202 "key_0" : true,
203 "key_1" : false
204 }
205 )";
206
207 JsonObject obj(str);
208 ASSERT_TRUE(obj.IsValid());
209
210 ASSERT_NE(obj.GetValue<JsonObject::BoolT>("key_0"), nullptr);
211 ASSERT_EQ(*obj.GetValue<JsonObject::BoolT>("key_0"), true);
212
213 ASSERT_NE(obj.GetValue<JsonObject::BoolT>("key_1"), nullptr);
214 ASSERT_EQ(*obj.GetValue<JsonObject::BoolT>("key_1"), false);
215 }
216
TEST(JsonParser,InvalidJson)217 TEST(JsonParser, InvalidJson)
218 {
219 auto repeatedKeys = R"(
220 {
221 "key_0" : "key_0.value0",
222 "key_0" : "key_0.value1",
223 }
224 )";
225
226 JsonObject obj(repeatedKeys);
227 ASSERT_FALSE(obj.IsValid());
228 }
229
230 /**
231 * @tc.name: UnescapeString
232 * @tc.desc: Verify the json parser with unescape string.
233 * @tc.type: FUNC
234 * @tc.require:
235 */
TEST(JsonParser,UnescapeString)236 TEST(JsonParser, UnescapeString)
237 {
238 std::stringstream ss(R"(
239 {
240 "key_0" : "\ttest_string1\r\n",
241 "key_1" : "\ftest_string2\b"
242 }
243 )");
244
245 JsonObject obj(ss.rdbuf());
246 ASSERT_TRUE(obj.IsValid());
247
248 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_0"), nullptr);
249 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_0"), "\ttest_string1\r\n");
250
251 ASSERT_NE(obj.GetValue<JsonObject::StringT>("key_1"), nullptr);
252 ASSERT_EQ(*obj.GetValue<JsonObject::StringT>("key_1"), "\ftest_string2\b");
253
254 auto invalidUnescapeStr(R"(
255 {
256 "key_0" : "\a"
257 }
258 )");
259 JsonObject invalidObj(invalidUnescapeStr);
260 ASSERT_FALSE(invalidObj.IsValid());
261 }
262 } // namespace ark::json_parser::test
263