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