Debian Codegraph @

test/core/json/json_test.cc

raw
1 //
2 // Copyright 2015-2016 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "src/core/lib/json/json.h"
18
19 #include <string.h>
20
21 #include "absl/status/status.h"
22 #include "absl/strings/match.h"
23 #include "absl/strings/str_cat.h"
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26
27 #include <grpc/support/log.h>
28
29 #include "test/core/util/test_config.h"
30
31 namespace grpc_core {
32
33 void ValidateValue(const Json& actual, const Json& expected);
34
35 void ValidateObject(const Json::Object& actual, const Json::Object& expected) {
36 ASSERT_EQ(actual.size(), expected.size());
37 auto actual_it = actual.begin();
38 for (const auto& p : expected) {
39 EXPECT_EQ(actual_it->first, p.first);
40 ValidateValue(actual_it->second, p.second);
41 ++actual_it;
42 }
43 }
44
45 void ValidateArray(const Json::Array& actual, const Json::Array& expected) {
46 ASSERT_EQ(actual.size(), expected.size());
47 for (size_t i = 0; i < expected.size(); ++i) {
48 ValidateValue(actual[i], expected[i]);
49 }
50 }
51
52 void ValidateValue(const Json& actual, const Json& expected) {
53 ASSERT_EQ(actual.type(), expected.type());
54 switch (expected.type()) {
55 case Json::Type::JSON_NULL:
56 case Json::Type::JSON_TRUE:
57 case Json::Type::JSON_FALSE:
58 break;
59 case Json::Type::STRING:
60 case Json::Type::NUMBER:
61 EXPECT_EQ(actual.string_value(), expected.string_value());
62 break;
63 case Json::Type::OBJECT:
64 ValidateObject(actual.object_value(), expected.object_value());
65 break;
66 case Json::Type::ARRAY:
67 ValidateArray(actual.array_value(), expected.array_value());
68 break;
69 }
70 }
71
72 void RunSuccessTest(const char* input, const Json& expected,
73 const char* expected_output) {
74 gpr_log(GPR_INFO, "parsing string \"%s\" - should succeed", input);
75 auto json = Json::Parse(input);
76 ASSERT_TRUE(json.ok()) << json.status();
77 ValidateValue(*json, expected);
78 std::string output = json->Dump();
79 EXPECT_EQ(output, expected_output);
80 }
81
82 TEST(Json, Whitespace) {
83 RunSuccessTest(" 0 ", 0, "0");
84 RunSuccessTest(" 1 ", 1, "1");
85 RunSuccessTest(" \" \" ", " ", "\" \"");
86 RunSuccessTest(" \"a\" ", "a", "\"a\"");
87 RunSuccessTest(" true ", true, "true");
88 }
89
90 TEST(Json, Utf16) {
91 RunSuccessTest("\"\\u0020\\\\\\u0010\\u000a\\u000D\"", " \\\u0010\n\r",
92 "\" \\\\\\u0010\\n\\r\"");
93 }
94
95 MATCHER(ContainsInvalidUtf8,
96 absl::StrCat(negation ? "Contains" : "Does not contain",
97 " invalid UTF-8 characters.")) {
98 auto json = Json::Parse(arg);
99 return json.status().code() == absl::StatusCode::kInvalidArgument &&
100 absl::StrContains(json.status().message(), "JSON parsing failed");
101 }
102
103 TEST(Json, Utf8) {
104 RunSuccessTest("\"ßâñć௵⇒\"", "ßâñć௵⇒",
105 "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"");
106 RunSuccessTest("\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"", "ßâñć௵⇒",
107 "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"");
108 // Testing UTF-8 character "𝄞", U+11D1E.
109 RunSuccessTest("\"\xf0\x9d\x84\x9e\"", "\xf0\x9d\x84\x9e",
110 "\"\\ud834\\udd1e\"");
111 RunSuccessTest("\"\\ud834\\udd1e\"", "\xf0\x9d\x84\x9e",
112 "\"\\ud834\\udd1e\"");
113 RunSuccessTest("{\"\\ud834\\udd1e\":0}",
114 Json::Object{{"\xf0\x9d\x84\x9e", 0}},
115 "{\"\\ud834\\udd1e\":0}");
116
117 /// For UTF-8 characters with length of 1 byte, the range of it is [0x00,
118 /// 0x7f].
119 EXPECT_THAT("\"\xa0\"", ContainsInvalidUtf8());
120
121 /// For UTF-8 characters with length of 2 bytes, the range of the first byte
122 /// is [0xc2, 0xdf], and the range of the second byte is [0x80, 0xbf].
123 EXPECT_THAT("\"\xc0\xbc\"", ContainsInvalidUtf8());
124 EXPECT_THAT("\"\xbc\xc0\"", ContainsInvalidUtf8());
125
126 /// Corner cases for UTF-8 characters with length of 3 bytes.
127 /// If the first byte is 0xe0, the range of second byte is [0xa0, 0xbf].
128 EXPECT_THAT("\"\xe0\x80\x80\"", ContainsInvalidUtf8());
129 /// If the first byte is 0xed, the range of second byte is [0x80, 0x9f].
130 EXPECT_THAT("\"\xed\xa0\x80\"", ContainsInvalidUtf8());
131
132 /// Corner cases for UTF-8 characters with length of 4 bytes.
133 /// If the first byte is 0xf0, the range of second byte is [0x90, 0xbf].
134 EXPECT_THAT("\"\xf0\x80\x80\x80\"", ContainsInvalidUtf8());
135 /// If the first byte is 0xf4, the range of second byte is [0x80, 0x8f].
136 EXPECT_THAT("\"\xf4\x90\x80\x80\"", ContainsInvalidUtf8());
137 /// The range of the first bytes is [0xf0, 0xf4].
138 EXPECT_THAT("\"\xf5\x80\x80\x80\"", ContainsInvalidUtf8());
139 }
140
141 TEST(Json, NestedEmptyContainers) {
142 RunSuccessTest(" [ [ ] , { } , [ ] ] ",
143 Json::Array{
144 Json::Array(),
145 Json::Object(),
146 Json::Array(),
147 },
148 "[[],{},[]]");
149 }
150
151 TEST(Json, EscapesAndControlCharactersInKeyStrings) {
152 RunSuccessTest(" { \"\\u007f\x7f\\n\\r\\\"\\f\\b\\\\a , b\": 1, \"\": 0 } ",
153 Json::Object{
154 {"\u007f\u007f\n\r\"\f\b\\a , b", 1},
155 {"", 0},
156 },
157 "{\"\":0,\"\\u007f\\u007f\\n\\r\\\"\\f\\b\\\\a , b\":1}");
158 }
159
160 TEST(Json, WriterCutsOffInvalidUtf8) {
161 EXPECT_EQ(Json("abc\xf0\x9d\x24").Dump(), "\"abc\"");
162 EXPECT_EQ(Json("\xff").Dump(), "\"\"");
163 }
164
165 TEST(Json, ValidNumbers) {
166 RunSuccessTest("[0, 42 , 0.0123, 123.456]",
167 Json::Array{
168 0,
169 42,
170 Json("0.0123", /*is_number=*/true),
171 Json("123.456", /*is_number=*/true),
172 },
173 "[0,42,0.0123,123.456]");
174 RunSuccessTest("[1e4,-53.235e-31, 0.3e+3]",
175 Json::Array{
176 Json("1e4", /*is_number=*/true),
177 Json("-53.235e-31", /*is_number=*/true),
178 Json("0.3e+3", /*is_number=*/true),
179 },
180 "[1e4,-53.235e-31,0.3e+3]");
181 }
182
183 TEST(Json, Keywords) {
184 RunSuccessTest("[true, false, null]",
185 Json::Array{
186 Json(true),
187 Json(false),
188 Json(),
189 },
190 "[true,false,null]");
191 }
192
193 void RunParseFailureTest(const char* input) {
194 gpr_log(GPR_INFO, "parsing string \"%s\" - should fail", input);
195 auto json = Json::Parse(input);
196 EXPECT_FALSE(json.ok());
197 }
198
199 TEST(Json, InvalidInput) {
200 RunParseFailureTest("\\");
201 RunParseFailureTest("nu ll");
202 RunParseFailureTest("{\"foo\": bar}");
203 RunParseFailureTest("{\"foo\": bar\"x\"}");
204 RunParseFailureTest("fals");
205 RunParseFailureTest("0,0 ");
206 RunParseFailureTest("\"foo\",[]");
207 }
208
209 TEST(Json, UnterminatedString) { RunParseFailureTest("\"\\x"); }
210
211 TEST(Json, InvalidUtf16) {
212 RunParseFailureTest("\"\\u123x");
213 RunParseFailureTest("{\"\\u123x");
214 }
215
216 TEST(Json, ImbalancedSurrogatePairs) {
217 RunParseFailureTest("\"\\ud834f");
218 RunParseFailureTest("{\"\\ud834f\":0}");
219 RunParseFailureTest("\"\\ud834\\n");
220 RunParseFailureTest("{\"\\ud834\\n\":0}");
221 RunParseFailureTest("\"\\udd1ef");
222 RunParseFailureTest("{\"\\udd1ef\":0}");
223 RunParseFailureTest("\"\\ud834\\ud834\"");
224 RunParseFailureTest("{\"\\ud834\\ud834\"\":0}");
225 RunParseFailureTest("\"\\ud834\\u1234\"");
226 RunParseFailureTest("{\"\\ud834\\u1234\"\":0}");
227 RunParseFailureTest("\"\\ud834]\"");
228 RunParseFailureTest("{\"\\ud834]\"\":0}");
229 RunParseFailureTest("\"\\ud834 \"");
230 RunParseFailureTest("{\"\\ud834 \"\":0}");
231 RunParseFailureTest("\"\\ud834\\\\\"");
232 RunParseFailureTest("{\"\\ud834\\\\\"\":0}");
233 }
234
235 TEST(Json, EmbeddedInvalidWhitechars) {
236 RunParseFailureTest("\"\n\"");
237 RunParseFailureTest("\"\t\"");
238 }
239
240 TEST(Json, EmptyString) { RunParseFailureTest(""); }
241
242 TEST(Json, ExtraCharsAtEndOfParsing) {
243 RunParseFailureTest("{},");
244 RunParseFailureTest("{}x");
245 }
246
247 TEST(Json, ImbalancedContainers) {
248 RunParseFailureTest("{}}");
249 RunParseFailureTest("[]]");
250 RunParseFailureTest("{{}");
251 RunParseFailureTest("[[]");
252 RunParseFailureTest("[}");
253 RunParseFailureTest("{]");
254 }
255
256 TEST(Json, BadContainers) {
257 RunParseFailureTest("{x}");
258 RunParseFailureTest("{x=0,y}");
259 }
260
261 TEST(Json, DuplicateObjectKeys) { RunParseFailureTest("{\"x\": 1, \"x\": 1}"); }
262
263 TEST(Json, TrailingComma) {
264 RunParseFailureTest("{,}");
265 RunParseFailureTest("[1,2,3,4,]");
266 RunParseFailureTest("{\"a\": 1, }");
267 }
268
269 TEST(Json, KeySyntaxInArray) { RunParseFailureTest("[\"x\":0]"); }
270
271 TEST(Json, InvalidNumbers) {
272 RunParseFailureTest("1.");
273 RunParseFailureTest("1e");
274 RunParseFailureTest(".12");
275 RunParseFailureTest("1.x");
276 RunParseFailureTest("1.12x");
277 RunParseFailureTest("1ex");
278 RunParseFailureTest("1e12x");
279 RunParseFailureTest(".12x");
280 RunParseFailureTest("000");
281 };
282
283 TEST(Json, Equality) {
284 // Null.
285 EXPECT_EQ(Json(), Json());
286 // Numbers.
287 EXPECT_EQ(Json(1), Json(1));
288 EXPECT_NE(Json(1), Json(2));
289 EXPECT_EQ(Json(1), Json("1", /*is_number=*/true));
290 EXPECT_EQ(Json("-5e5", /*is_number=*/true), Json("-5e5", /*is_number=*/true));
291 // Booleans.
292 EXPECT_EQ(Json(true), Json(true));
293 EXPECT_EQ(Json(false), Json(false));
294 EXPECT_NE(Json(true), Json(false));
295 // Strings.
296 EXPECT_EQ(Json("foo"), Json("foo"));
297 EXPECT_NE(Json("foo"), Json("bar"));
298 // Arrays.
299 EXPECT_EQ(Json(Json::Array{"foo"}), Json(Json::Array{"foo"}));
300 EXPECT_NE(Json(Json::Array{"foo"}), Json(Json::Array{"bar"}));
301 // Objects.
302 EXPECT_EQ(Json(Json::Object{{"foo", 1}}), Json(Json::Object{{"foo", 1}}));
303 EXPECT_NE(Json(Json::Object{{"foo", 1}}), Json(Json::Object{{"foo", 2}}));
304 EXPECT_NE(Json(Json::Object{{"foo", 1}}), Json(Json::Object{{"bar", 1}}));
305 // Differing types.
306 EXPECT_NE(Json(1), Json("foo"));
307 EXPECT_NE(Json(1), Json(true));
308 EXPECT_NE(Json(1), Json(Json::Array{}));
309 EXPECT_NE(Json(1), Json(Json::Object{}));
310 EXPECT_NE(Json(1), Json());
311 }
312
313 } // namespace grpc_core
314
315 int main(int argc, char** argv) {
316 grpc::testing::TestEnvironment env(&argc, argv);
317 ::testing::InitGoogleTest(&argc, argv);
318 return RUN_ALL_TESTS();
319 }