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