xref: /aosp_15_r20/external/grpc-grpc/test/core/json/json_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <map>
22 #include <string>
23 #include <utility>
24 
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/match.h"
28 #include "absl/strings/str_cat.h"
29 #include "gmock/gmock.h"
30 #include "gtest/gtest.h"
31 
32 #include <grpc/support/log.h>
33 
34 #include "src/core/lib/json/json_reader.h"
35 #include "src/core/lib/json/json_writer.h"
36 #include "test/core/util/test_config.h"
37 
38 namespace grpc_core {
39 
40 void ValidateValue(const Json& actual, const Json& expected);
41 
ValidateObject(const Json::Object & actual,const Json::Object & expected)42 void ValidateObject(const Json::Object& actual, const Json::Object& expected) {
43   ASSERT_EQ(actual.size(), expected.size());
44   auto actual_it = actual.begin();
45   for (const auto& p : expected) {
46     EXPECT_EQ(actual_it->first, p.first);
47     ValidateValue(actual_it->second, p.second);
48     ++actual_it;
49   }
50 }
51 
ValidateArray(const Json::Array & actual,const Json::Array & expected)52 void ValidateArray(const Json::Array& actual, const Json::Array& expected) {
53   ASSERT_EQ(actual.size(), expected.size());
54   for (size_t i = 0; i < expected.size(); ++i) {
55     ValidateValue(actual[i], expected[i]);
56   }
57 }
58 
ValidateValue(const Json & actual,const Json & expected)59 void ValidateValue(const Json& actual, const Json& expected) {
60   ASSERT_EQ(actual.type(), expected.type());
61   switch (expected.type()) {
62     case Json::Type::kNull:
63       break;
64     case Json::Type::kBoolean:
65       EXPECT_EQ(actual.boolean(), expected.boolean());
66       break;
67     case Json::Type::kString:
68     case Json::Type::kNumber:
69       EXPECT_EQ(actual.string(), expected.string());
70       break;
71     case Json::Type::kObject:
72       ValidateObject(actual.object(), expected.object());
73       break;
74     case Json::Type::kArray:
75       ValidateArray(actual.array(), expected.array());
76       break;
77   }
78 }
79 
RunSuccessTest(const char * input,const Json & expected,const char * expected_output)80 void RunSuccessTest(const char* input, const Json& expected,
81                     const char* expected_output) {
82   gpr_log(GPR_INFO, "parsing string \"%s\" - should succeed", input);
83   auto json = JsonParse(input);
84   ASSERT_TRUE(json.ok()) << json.status();
85   ValidateValue(*json, expected);
86   std::string output = JsonDump(*json);
87   EXPECT_EQ(output, expected_output);
88 }
89 
TEST(Json,Whitespace)90 TEST(Json, Whitespace) {
91   RunSuccessTest(" 0 ", Json::FromNumber(0), "0");
92   RunSuccessTest(" 1 ", Json::FromNumber(1), "1");
93   RunSuccessTest(" \"    \" ", Json::FromString("    "), "\"    \"");
94   RunSuccessTest(" \"a\" ", Json::FromString("a"), "\"a\"");
95   RunSuccessTest(" true ", Json::FromBool(true), "true");
96 }
97 
TEST(Json,Utf16)98 TEST(Json, Utf16) {
99   RunSuccessTest("\"\\u0020\\\\\\u0010\\u000a\\u000D\"",
100                  Json::FromString(" \\\u0010\n\r"), "\" \\\\\\u0010\\n\\r\"");
101 }
102 
103 MATCHER(ContainsInvalidUtf8,
104         absl::StrCat(negation ? "Contains" : "Does not contain",
105                      " invalid UTF-8 characters.")) {
106   auto json = JsonParse(arg);
107   return json.status().code() == absl::StatusCode::kInvalidArgument &&
108          absl::StrContains(json.status().message(), "JSON parsing failed");
109 }
110 
TEST(Json,Utf8)111 TEST(Json, Utf8) {
112   RunSuccessTest("\"ßâñć௵⇒\"", Json::FromString("ßâñć௵⇒"),
113                  "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"");
114   RunSuccessTest("\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"",
115                  Json::FromString("ßâñć௵⇒"),
116                  "\"\\u00df\\u00e2\\u00f1\\u0107\\u0bf5\\u21d2\"");
117   // Testing UTF-8 character "��", U+11D1E.
118   RunSuccessTest("\"\xf0\x9d\x84\x9e\"", Json::FromString("\xf0\x9d\x84\x9e"),
119                  "\"\\ud834\\udd1e\"");
120   RunSuccessTest("\"\\ud834\\udd1e\"", Json::FromString("\xf0\x9d\x84\x9e"),
121                  "\"\\ud834\\udd1e\"");
122   RunSuccessTest("{\"\\ud834\\udd1e\":0}",
123                  Json::FromObject({{"\xf0\x9d\x84\x9e", Json::FromNumber(0)}}),
124                  "{\"\\ud834\\udd1e\":0}");
125 
126   /// For UTF-8 characters with length of 1 byte, the range of it is [0x00,
127   /// 0x7f].
128   EXPECT_THAT("\"\xa0\"", ContainsInvalidUtf8());
129 
130   /// For UTF-8 characters with length of 2 bytes, the range of the first byte
131   /// is [0xc2, 0xdf], and the range of the second byte is [0x80, 0xbf].
132   EXPECT_THAT("\"\xc0\xbc\"", ContainsInvalidUtf8());
133   EXPECT_THAT("\"\xbc\xc0\"", ContainsInvalidUtf8());
134 
135   /// Corner cases for UTF-8 characters with length of 3 bytes.
136   /// If the first byte is 0xe0, the range of second byte is [0xa0, 0xbf].
137   EXPECT_THAT("\"\xe0\x80\x80\"", ContainsInvalidUtf8());
138   /// If the first byte is 0xed, the range of second byte is [0x80, 0x9f].
139   EXPECT_THAT("\"\xed\xa0\x80\"", ContainsInvalidUtf8());
140 
141   /// Corner cases for UTF-8 characters with length of 4 bytes.
142   /// If the first byte is 0xf0, the range of second byte is [0x90, 0xbf].
143   EXPECT_THAT("\"\xf0\x80\x80\x80\"", ContainsInvalidUtf8());
144   /// If the first byte is 0xf4, the range of second byte is [0x80, 0x8f].
145   EXPECT_THAT("\"\xf4\x90\x80\x80\"", ContainsInvalidUtf8());
146   /// The range of the first bytes is [0xf0, 0xf4].
147   EXPECT_THAT("\"\xf5\x80\x80\x80\"", ContainsInvalidUtf8());
148 }
149 
TEST(Json,NestedEmptyContainers)150 TEST(Json, NestedEmptyContainers) {
151   RunSuccessTest(" [ [ ] , { } , [ ] ] ",
152                  Json::FromArray({
153                      Json::FromArray({}),
154                      Json::FromObject({}),
155                      Json::FromArray({}),
156                  }),
157                  "[[],{},[]]");
158 }
159 
TEST(Json,EscapesAndControlCharactersInKeyStrings)160 TEST(Json, EscapesAndControlCharactersInKeyStrings) {
161   RunSuccessTest(" { \"\\u007f\x7f\\n\\r\\\"\\f\\b\\\\a , b\": 1, \"\": 0 } ",
162                  Json::FromObject({
163                      {"\u007f\u007f\n\r\"\f\b\\a , b", Json::FromNumber(1)},
164                      {"", Json::FromNumber(0)},
165                  }),
166                  "{\"\":0,\"\\u007f\\u007f\\n\\r\\\"\\f\\b\\\\a , b\":1}");
167 }
168 
TEST(Json,WriterCutsOffInvalidUtf8)169 TEST(Json, WriterCutsOffInvalidUtf8) {
170   EXPECT_EQ(JsonDump(Json::FromString("abc\xf0\x9d\x24")), "\"abc\"");
171   EXPECT_EQ(JsonDump(Json::FromString("\xff")), "\"\"");
172 }
173 
TEST(Json,ValidNumbers)174 TEST(Json, ValidNumbers) {
175   RunSuccessTest("[0, 42 , 0.0123, 123.456]",
176                  Json::FromArray({
177                      Json::FromNumber(0),
178                      Json::FromNumber(42),
179                      Json::FromNumber("0.0123"),
180                      Json::FromNumber("123.456"),
181                  }),
182                  "[0,42,0.0123,123.456]");
183   RunSuccessTest("[1e4,-53.235e-31, 0.3e+3]",
184                  Json::FromArray({
185                      Json::FromNumber("1e4"),
186                      Json::FromNumber("-53.235e-31"),
187                      Json::FromNumber("0.3e+3"),
188                  }),
189                  "[1e4,-53.235e-31,0.3e+3]");
190 }
191 
TEST(Json,Keywords)192 TEST(Json, Keywords) {
193   RunSuccessTest("[true, false, null]",
194                  Json::FromArray({
195                      Json::FromBool(true),
196                      Json::FromBool(false),
197                      Json(),
198                  }),
199                  "[true,false,null]");
200 }
201 
RunParseFailureTest(const char * input)202 void RunParseFailureTest(const char* input) {
203   gpr_log(GPR_INFO, "parsing string \"%s\" - should fail", input);
204   auto json = JsonParse(input);
205   EXPECT_FALSE(json.ok()) << "input: \"" << input << "\"";
206 }
207 
TEST(Json,InvalidInput)208 TEST(Json, InvalidInput) {
209   RunParseFailureTest("\\");
210   RunParseFailureTest("nu ll");
211   RunParseFailureTest("{\"foo\": bar}");
212   RunParseFailureTest("{\"foo\": bar\"x\"}");
213   RunParseFailureTest("fals");
214   RunParseFailureTest("0,0 ");
215   RunParseFailureTest("\"foo\",[]");
216   RunParseFailureTest("{\"field\": {},}");
217   RunParseFailureTest("[{},]");
218   RunParseFailureTest("{\"field\": [],}");
219   RunParseFailureTest("[[],]");
220 }
221 
TEST(Json,UnterminatedString)222 TEST(Json, UnterminatedString) { RunParseFailureTest("\"\\x"); }
223 
TEST(Json,InvalidUtf16)224 TEST(Json, InvalidUtf16) {
225   RunParseFailureTest("\"\\u123x");
226   RunParseFailureTest("{\"\\u123x");
227 }
228 
TEST(Json,ImbalancedSurrogatePairs)229 TEST(Json, ImbalancedSurrogatePairs) {
230   RunParseFailureTest("\"\\ud834f");
231   RunParseFailureTest("{\"\\ud834f\":0}");
232   RunParseFailureTest("\"\\ud834\\n");
233   RunParseFailureTest("{\"\\ud834\\n\":0}");
234   RunParseFailureTest("\"\\udd1ef");
235   RunParseFailureTest("{\"\\udd1ef\":0}");
236   RunParseFailureTest("\"\\ud834\\ud834\"");
237   RunParseFailureTest("{\"\\ud834\\ud834\"\":0}");
238   RunParseFailureTest("\"\\ud834\\u1234\"");
239   RunParseFailureTest("{\"\\ud834\\u1234\"\":0}");
240   RunParseFailureTest("\"\\ud834]\"");
241   RunParseFailureTest("{\"\\ud834]\"\":0}");
242   RunParseFailureTest("\"\\ud834 \"");
243   RunParseFailureTest("{\"\\ud834 \"\":0}");
244   RunParseFailureTest("\"\\ud834\\\\\"");
245   RunParseFailureTest("{\"\\ud834\\\\\"\":0}");
246 }
247 
TEST(Json,EmbeddedInvalidWhitechars)248 TEST(Json, EmbeddedInvalidWhitechars) {
249   RunParseFailureTest("\"\n\"");
250   RunParseFailureTest("\"\t\"");
251 }
252 
TEST(Json,EmptyString)253 TEST(Json, EmptyString) { RunParseFailureTest(""); }
254 
TEST(Json,ExtraCharsAtEndOfParsing)255 TEST(Json, ExtraCharsAtEndOfParsing) {
256   RunParseFailureTest("{},");
257   RunParseFailureTest("{}x");
258 }
259 
TEST(Json,ImbalancedContainers)260 TEST(Json, ImbalancedContainers) {
261   RunParseFailureTest("{}}");
262   RunParseFailureTest("[]]");
263   RunParseFailureTest("{{}");
264   RunParseFailureTest("[[]");
265   RunParseFailureTest("[}");
266   RunParseFailureTest("{]");
267 }
268 
TEST(Json,BadContainers)269 TEST(Json, BadContainers) {
270   RunParseFailureTest("{x}");
271   RunParseFailureTest("{x=0,y}");
272 }
273 
TEST(Json,DuplicateObjectKeys)274 TEST(Json, DuplicateObjectKeys) { RunParseFailureTest("{\"x\": 1, \"x\": 1}"); }
275 
TEST(Json,TrailingComma)276 TEST(Json, TrailingComma) {
277   RunParseFailureTest("{,}");
278   RunParseFailureTest("[1,2,3,4,]");
279   RunParseFailureTest("{\"a\": 1, }");
280 }
281 
TEST(Json,KeySyntaxInArray)282 TEST(Json, KeySyntaxInArray) { RunParseFailureTest("[\"x\":0]"); }
283 
TEST(Json,InvalidNumbers)284 TEST(Json, InvalidNumbers) {
285   RunParseFailureTest("1.");
286   RunParseFailureTest("1e");
287   RunParseFailureTest(".12");
288   RunParseFailureTest("1.x");
289   RunParseFailureTest("1.12x");
290   RunParseFailureTest("1ex");
291   RunParseFailureTest("1e12x");
292   RunParseFailureTest(".12x");
293   RunParseFailureTest("000");
294 };
295 
TEST(Json,Equality)296 TEST(Json, Equality) {
297   // Null.
298   EXPECT_EQ(Json(), Json());
299   // Numbers.
300   EXPECT_EQ(Json::FromNumber(1), Json::FromNumber(1));
301   EXPECT_NE(Json::FromNumber(1), Json::FromNumber(2));
302   EXPECT_EQ(Json::FromNumber(1), Json::FromNumber("1"));
303   EXPECT_EQ(Json::FromNumber("-5e5"), Json::FromNumber("-5e5"));
304   // Booleans.
305   EXPECT_EQ(Json::FromBool(true), Json::FromBool(true));
306   EXPECT_EQ(Json::FromBool(false), Json::FromBool(false));
307   EXPECT_NE(Json::FromBool(true), Json::FromBool(false));
308   // Strings.
309   EXPECT_EQ(Json::FromString("foo"), Json::FromString("foo"));
310   EXPECT_NE(Json::FromString("foo"), Json::FromString("bar"));
311   // Arrays.
312   EXPECT_EQ(Json::FromArray({Json::FromString("foo")}),
313             Json::FromArray({Json::FromString("foo")}));
314   EXPECT_NE(Json::FromArray({Json::FromString("foo")}),
315             Json::FromArray({Json::FromString("bar")}));
316   // Objects.
317   EXPECT_EQ(Json::FromObject({{"foo", Json::FromNumber(1)}}),
318             Json::FromObject({{"foo", Json::FromNumber(1)}}));
319   EXPECT_NE(Json::FromObject({{"foo", Json::FromNumber(1)}}),
320             Json::FromObject({{"foo", Json::FromNumber(2)}}));
321   EXPECT_NE(Json::FromObject({{"foo", Json::FromNumber(1)}}),
322             Json::FromObject({{"bar", Json::FromNumber(1)}}));
323   // Differing types.
324   EXPECT_NE(Json::FromNumber(1), Json::FromString("foo"));
325   EXPECT_NE(Json::FromNumber(1), Json::FromBool(true));
326   EXPECT_NE(Json::FromNumber(1), Json::FromArray({}));
327   EXPECT_NE(Json::FromNumber(1), Json::FromObject({}));
328   EXPECT_NE(Json::FromNumber(1), Json());
329 }
330 
331 }  // namespace grpc_core
332 
main(int argc,char ** argv)333 int main(int argc, char** argv) {
334   grpc::testing::TestEnvironment env(&argc, argv);
335   ::testing::InitGoogleTest(&argc, argv);
336   return RUN_ALL_TESTS();
337 }
338