1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/json/json_parser.h"
6
7 #include <stddef.h>
8
9 #include <memory>
10
11 #include "base/json/json_reader.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/values.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace base {
18 namespace internal {
19
20 class JSONParserTest : public testing::Test {
21 public:
NewTestParser(const std::string & input,int options=JSON_PARSE_RFC)22 JSONParser* NewTestParser(const std::string& input,
23 int options = JSON_PARSE_RFC) {
24 JSONParser* parser = new JSONParser(options);
25 parser->start_pos_ = input.data();
26 parser->pos_ = parser->start_pos_;
27 parser->end_pos_ = parser->start_pos_ + input.length();
28 return parser;
29 }
30
TestLastThree(JSONParser * parser)31 void TestLastThree(JSONParser* parser) {
32 EXPECT_EQ(',', *parser->NextChar());
33 EXPECT_EQ('|', *parser->NextChar());
34 EXPECT_EQ('\0', *parser->NextChar());
35 EXPECT_EQ(parser->end_pos_, parser->pos_);
36 }
37 };
38
TEST_F(JSONParserTest,NextChar)39 TEST_F(JSONParserTest, NextChar) {
40 std::string input("Hello world");
41 std::unique_ptr<JSONParser> parser(NewTestParser(input));
42
43 EXPECT_EQ('H', *parser->pos_);
44 for (size_t i = 1; i < input.length(); ++i) {
45 EXPECT_EQ(input[i], *parser->NextChar());
46 }
47 EXPECT_EQ(parser->end_pos_, parser->NextChar());
48 }
49
TEST_F(JSONParserTest,ConsumeString)50 TEST_F(JSONParserTest, ConsumeString) {
51 std::string input("\"test\",|");
52 std::unique_ptr<JSONParser> parser(NewTestParser(input));
53 std::unique_ptr<Value> value(parser->ConsumeString());
54 EXPECT_EQ('"', *parser->pos_);
55
56 TestLastThree(parser.get());
57
58 ASSERT_TRUE(value.get());
59 std::string str;
60 EXPECT_TRUE(value->GetAsString(&str));
61 EXPECT_EQ("test", str);
62 }
63
TEST_F(JSONParserTest,ConsumeList)64 TEST_F(JSONParserTest, ConsumeList) {
65 std::string input("[true, false],|");
66 std::unique_ptr<JSONParser> parser(NewTestParser(input));
67 std::unique_ptr<Value> value(parser->ConsumeList());
68 EXPECT_EQ(']', *parser->pos_);
69
70 TestLastThree(parser.get());
71
72 ASSERT_TRUE(value.get());
73 base::ListValue* list;
74 EXPECT_TRUE(value->GetAsList(&list));
75 EXPECT_EQ(2u, list->GetSize());
76 }
77
TEST_F(JSONParserTest,ConsumeDictionary)78 TEST_F(JSONParserTest, ConsumeDictionary) {
79 std::string input("{\"abc\":\"def\"},|");
80 std::unique_ptr<JSONParser> parser(NewTestParser(input));
81 std::unique_ptr<Value> value(parser->ConsumeDictionary());
82 EXPECT_EQ('}', *parser->pos_);
83
84 TestLastThree(parser.get());
85
86 ASSERT_TRUE(value.get());
87 base::DictionaryValue* dict;
88 EXPECT_TRUE(value->GetAsDictionary(&dict));
89 std::string str;
90 EXPECT_TRUE(dict->GetString("abc", &str));
91 EXPECT_EQ("def", str);
92 }
93
TEST_F(JSONParserTest,ConsumeLiterals)94 TEST_F(JSONParserTest, ConsumeLiterals) {
95 // Literal |true|.
96 std::string input("true,|");
97 std::unique_ptr<JSONParser> parser(NewTestParser(input));
98 std::unique_ptr<Value> value(parser->ConsumeLiteral());
99 EXPECT_EQ('e', *parser->pos_);
100
101 TestLastThree(parser.get());
102
103 ASSERT_TRUE(value.get());
104 bool bool_value = false;
105 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
106 EXPECT_TRUE(bool_value);
107
108 // Literal |false|.
109 input = "false,|";
110 parser.reset(NewTestParser(input));
111 value = parser->ConsumeLiteral();
112 EXPECT_EQ('e', *parser->pos_);
113
114 TestLastThree(parser.get());
115
116 ASSERT_TRUE(value.get());
117 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
118 EXPECT_FALSE(bool_value);
119
120 // Literal |null|.
121 input = "null,|";
122 parser.reset(NewTestParser(input));
123 value = parser->ConsumeLiteral();
124 EXPECT_EQ('l', *parser->pos_);
125
126 TestLastThree(parser.get());
127
128 ASSERT_TRUE(value.get());
129 EXPECT_TRUE(value->IsType(Value::Type::NONE));
130 }
131
TEST_F(JSONParserTest,ConsumeNumbers)132 TEST_F(JSONParserTest, ConsumeNumbers) {
133 // Integer.
134 std::string input("1234,|");
135 std::unique_ptr<JSONParser> parser(NewTestParser(input));
136 std::unique_ptr<Value> value(parser->ConsumeNumber());
137 EXPECT_EQ('4', *parser->pos_);
138
139 TestLastThree(parser.get());
140
141 ASSERT_TRUE(value.get());
142 int number_i;
143 EXPECT_TRUE(value->GetAsInteger(&number_i));
144 EXPECT_EQ(1234, number_i);
145
146 // Negative integer.
147 input = "-1234,|";
148 parser.reset(NewTestParser(input));
149 value = parser->ConsumeNumber();
150 EXPECT_EQ('4', *parser->pos_);
151
152 TestLastThree(parser.get());
153
154 ASSERT_TRUE(value.get());
155 EXPECT_TRUE(value->GetAsInteger(&number_i));
156 EXPECT_EQ(-1234, number_i);
157
158 // Double.
159 input = "12.34,|";
160 parser.reset(NewTestParser(input));
161 value = parser->ConsumeNumber();
162 EXPECT_EQ('4', *parser->pos_);
163
164 TestLastThree(parser.get());
165
166 ASSERT_TRUE(value.get());
167 double number_d;
168 EXPECT_TRUE(value->GetAsDouble(&number_d));
169 EXPECT_EQ(12.34, number_d);
170
171 // Scientific.
172 input = "42e3,|";
173 parser.reset(NewTestParser(input));
174 value = parser->ConsumeNumber();
175 EXPECT_EQ('3', *parser->pos_);
176
177 TestLastThree(parser.get());
178
179 ASSERT_TRUE(value.get());
180 EXPECT_TRUE(value->GetAsDouble(&number_d));
181 EXPECT_EQ(42000, number_d);
182
183 // Negative scientific.
184 input = "314159e-5,|";
185 parser.reset(NewTestParser(input));
186 value = parser->ConsumeNumber();
187 EXPECT_EQ('5', *parser->pos_);
188
189 TestLastThree(parser.get());
190
191 ASSERT_TRUE(value.get());
192 EXPECT_TRUE(value->GetAsDouble(&number_d));
193 EXPECT_EQ(3.14159, number_d);
194
195 // Positive scientific.
196 input = "0.42e+3,|";
197 parser.reset(NewTestParser(input));
198 value = parser->ConsumeNumber();
199 EXPECT_EQ('3', *parser->pos_);
200
201 TestLastThree(parser.get());
202
203 ASSERT_TRUE(value.get());
204 EXPECT_TRUE(value->GetAsDouble(&number_d));
205 EXPECT_EQ(420, number_d);
206 }
207
TEST_F(JSONParserTest,ErrorMessages)208 TEST_F(JSONParserTest, ErrorMessages) {
209 // Error strings should not be modified in case of success.
210 std::string error_message;
211 int error_code = 0;
212 std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
213 "[42]", JSON_PARSE_RFC, &error_code, &error_message);
214 EXPECT_TRUE(error_message.empty());
215 EXPECT_EQ(0, error_code);
216
217 // Test line and column counting
218 const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
219 // error here ----------------------------------^
220 root = JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC, &error_code,
221 &error_message);
222 EXPECT_FALSE(root.get());
223 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
224 error_message);
225 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
226
227 error_code = 0;
228 error_message = "";
229 // Test line and column counting with "\r\n" line ending
230 const char big_json_crlf[] =
231 "[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
232 // error here ----------------------^
233 root = JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC,
234 &error_code, &error_message);
235 EXPECT_FALSE(root.get());
236 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
237 error_message);
238 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
239
240 // Test each of the error conditions
241 root = JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC, &error_code,
242 &error_message);
243 EXPECT_FALSE(root.get());
244 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
245 JSONReader::kUnexpectedDataAfterRoot), error_message);
246 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
247
248 std::string nested_json;
249 for (int i = 0; i < 201; ++i) {
250 nested_json.insert(nested_json.begin(), '[');
251 nested_json.append(1, ']');
252 }
253 root = JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
254 &error_code, &error_message);
255 EXPECT_FALSE(root.get());
256 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 200, JSONReader::kTooMuchNesting),
257 error_message);
258 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
259
260 root = JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC, &error_code,
261 &error_message);
262 EXPECT_FALSE(root.get());
263 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
264 error_message);
265 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
266
267 root = JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC,
268 &error_code, &error_message);
269 EXPECT_FALSE(root.get());
270 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
271 JSONReader::kUnquotedDictionaryKey), error_message);
272 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
273
274 root = JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", JSON_PARSE_RFC,
275 &error_code, &error_message);
276 EXPECT_FALSE(root.get());
277 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
278 error_message);
279
280 root = JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC, &error_code,
281 &error_message);
282 EXPECT_FALSE(root.get());
283 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
284 error_message);
285 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
286
287 root = JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC,
288 &error_code, &error_message);
289 EXPECT_FALSE(root.get());
290 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
291 error_message);
292 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
293
294 root = JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC,
295 &error_code, &error_message);
296 EXPECT_FALSE(root.get());
297 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
298 error_message);
299 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
300
301 root = JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC,
302 &error_code, &error_message);
303 EXPECT_FALSE(root.get());
304 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
305 error_message);
306 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
307 }
308
TEST_F(JSONParserTest,Decode4ByteUtf8Char)309 TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
310 // This test strings contains a 4 byte unicode character (a smiley!) that the
311 // reader should be able to handle (the character is \xf0\x9f\x98\x87).
312 const char kUtf8Data[] =
313 "[\"\",[],[],[],{\"google:suggesttype\":[]}]";
314 std::string error_message;
315 int error_code = 0;
316 std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
317 kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message);
318 EXPECT_TRUE(root.get()) << error_message;
319 }
320
TEST_F(JSONParserTest,DecodeUnicodeNonCharacter)321 TEST_F(JSONParserTest, DecodeUnicodeNonCharacter) {
322 // Tests Unicode code points (encoded as escaped UTF-16) that are not valid
323 // characters.
324 EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]"));
325 EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]"));
326 EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]"));
327 }
328
TEST_F(JSONParserTest,DecodeNegativeEscapeSequence)329 TEST_F(JSONParserTest, DecodeNegativeEscapeSequence) {
330 EXPECT_FALSE(JSONReader::Read("[\"\\x-A\"]"));
331 EXPECT_FALSE(JSONReader::Read("[\"\\u-00A\"]"));
332 }
333
334 // Verifies invalid utf-8 characters are replaced.
TEST_F(JSONParserTest,ReplaceInvalidCharacters)335 TEST_F(JSONParserTest, ReplaceInvalidCharacters) {
336 const std::string bogus_char = "";
337 const std::string quoted_bogus_char = "\"" + bogus_char + "\"";
338 std::unique_ptr<JSONParser> parser(
339 NewTestParser(quoted_bogus_char, JSON_REPLACE_INVALID_CHARACTERS));
340 std::unique_ptr<Value> value(parser->ConsumeString());
341 ASSERT_TRUE(value.get());
342 std::string str;
343 EXPECT_TRUE(value->GetAsString(&str));
344 EXPECT_EQ(kUnicodeReplacementString, str);
345 }
346
TEST_F(JSONParserTest,ParseNumberErrors)347 TEST_F(JSONParserTest, ParseNumberErrors) {
348 const struct {
349 const char* input;
350 bool parse_success;
351 double value;
352 } kCases[] = {
353 // clang-format off
354 {"1", true, 1},
355 {"2.", false, 0},
356 {"42", true, 42},
357 {"6e", false, 0},
358 {"43e2", true, 4300},
359 {"43e-", false, 0},
360 {"9e-3", true, 0.009},
361 {"2e+", false, 0},
362 {"2e+2", true, 200},
363 // clang-format on
364 };
365
366 for (unsigned int i = 0; i < arraysize(kCases); ++i) {
367 auto test_case = kCases[i];
368 SCOPED_TRACE(StringPrintf("case %u: \"%s\"", i, test_case.input));
369
370 // MSan will do a better job detecting over-read errors if the input is
371 // not nul-terminated on the heap.
372 size_t str_len = strlen(test_case.input);
373 auto non_nul_termianted = MakeUnique<char[]>(str_len);
374 memcpy(non_nul_termianted.get(), test_case.input, str_len);
375
376 StringPiece string_piece(non_nul_termianted.get(), str_len);
377 std::unique_ptr<Value> result = JSONReader::Read(string_piece);
378 if (test_case.parse_success) {
379 EXPECT_TRUE(result);
380 } else {
381 EXPECT_FALSE(result);
382 }
383
384 if (!result)
385 continue;
386
387 double double_value = 0;
388 EXPECT_TRUE(result->GetAsDouble(&double_value));
389 EXPECT_EQ(test_case.value, double_value);
390 }
391 }
392
393 } // namespace internal
394 } // namespace base
395