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/values.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16 namespace internal {
17
18 class JSONParserTest : public testing::Test {
19 public:
NewTestParser(const std::string & input)20 JSONParser* NewTestParser(const std::string& input) {
21 JSONParser* parser = new JSONParser(JSON_PARSE_RFC);
22 parser->start_pos_ = input.data();
23 parser->pos_ = parser->start_pos_;
24 parser->end_pos_ = parser->start_pos_ + input.length();
25 return parser;
26 }
27
TestLastThree(JSONParser * parser)28 void TestLastThree(JSONParser* parser) {
29 EXPECT_EQ(',', *parser->NextChar());
30 EXPECT_EQ('|', *parser->NextChar());
31 EXPECT_EQ('\0', *parser->NextChar());
32 EXPECT_EQ(parser->end_pos_, parser->pos_);
33 }
34 };
35
TEST_F(JSONParserTest,NextChar)36 TEST_F(JSONParserTest, NextChar) {
37 std::string input("Hello world");
38 std::unique_ptr<JSONParser> parser(NewTestParser(input));
39
40 EXPECT_EQ('H', *parser->pos_);
41 for (size_t i = 1; i < input.length(); ++i) {
42 EXPECT_EQ(input[i], *parser->NextChar());
43 }
44 EXPECT_EQ(parser->end_pos_, parser->NextChar());
45 }
46
TEST_F(JSONParserTest,ConsumeString)47 TEST_F(JSONParserTest, ConsumeString) {
48 std::string input("\"test\",|");
49 std::unique_ptr<JSONParser> parser(NewTestParser(input));
50 std::unique_ptr<Value> value(parser->ConsumeString());
51 EXPECT_EQ('"', *parser->pos_);
52
53 TestLastThree(parser.get());
54
55 ASSERT_TRUE(value.get());
56 std::string str;
57 EXPECT_TRUE(value->GetAsString(&str));
58 EXPECT_EQ("test", str);
59 }
60
TEST_F(JSONParserTest,ConsumeList)61 TEST_F(JSONParserTest, ConsumeList) {
62 std::string input("[true, false],|");
63 std::unique_ptr<JSONParser> parser(NewTestParser(input));
64 std::unique_ptr<Value> value(parser->ConsumeList());
65 EXPECT_EQ(']', *parser->pos_);
66
67 TestLastThree(parser.get());
68
69 ASSERT_TRUE(value.get());
70 base::ListValue* list;
71 EXPECT_TRUE(value->GetAsList(&list));
72 EXPECT_EQ(2u, list->GetSize());
73 }
74
TEST_F(JSONParserTest,ConsumeDictionary)75 TEST_F(JSONParserTest, ConsumeDictionary) {
76 std::string input("{\"abc\":\"def\"},|");
77 std::unique_ptr<JSONParser> parser(NewTestParser(input));
78 std::unique_ptr<Value> value(parser->ConsumeDictionary());
79 EXPECT_EQ('}', *parser->pos_);
80
81 TestLastThree(parser.get());
82
83 ASSERT_TRUE(value.get());
84 base::DictionaryValue* dict;
85 EXPECT_TRUE(value->GetAsDictionary(&dict));
86 std::string str;
87 EXPECT_TRUE(dict->GetString("abc", &str));
88 EXPECT_EQ("def", str);
89 }
90
TEST_F(JSONParserTest,ConsumeLiterals)91 TEST_F(JSONParserTest, ConsumeLiterals) {
92 // Literal |true|.
93 std::string input("true,|");
94 std::unique_ptr<JSONParser> parser(NewTestParser(input));
95 std::unique_ptr<Value> value(parser->ConsumeLiteral());
96 EXPECT_EQ('e', *parser->pos_);
97
98 TestLastThree(parser.get());
99
100 ASSERT_TRUE(value.get());
101 bool bool_value = false;
102 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
103 EXPECT_TRUE(bool_value);
104
105 // Literal |false|.
106 input = "false,|";
107 parser.reset(NewTestParser(input));
108 value.reset(parser->ConsumeLiteral());
109 EXPECT_EQ('e', *parser->pos_);
110
111 TestLastThree(parser.get());
112
113 ASSERT_TRUE(value.get());
114 EXPECT_TRUE(value->GetAsBoolean(&bool_value));
115 EXPECT_FALSE(bool_value);
116
117 // Literal |null|.
118 input = "null,|";
119 parser.reset(NewTestParser(input));
120 value.reset(parser->ConsumeLiteral());
121 EXPECT_EQ('l', *parser->pos_);
122
123 TestLastThree(parser.get());
124
125 ASSERT_TRUE(value.get());
126 EXPECT_TRUE(value->IsType(Value::TYPE_NULL));
127 }
128
TEST_F(JSONParserTest,ConsumeNumbers)129 TEST_F(JSONParserTest, ConsumeNumbers) {
130 // Integer.
131 std::string input("1234,|");
132 std::unique_ptr<JSONParser> parser(NewTestParser(input));
133 std::unique_ptr<Value> value(parser->ConsumeNumber());
134 EXPECT_EQ('4', *parser->pos_);
135
136 TestLastThree(parser.get());
137
138 ASSERT_TRUE(value.get());
139 int number_i;
140 EXPECT_TRUE(value->GetAsInteger(&number_i));
141 EXPECT_EQ(1234, number_i);
142
143 // Negative integer.
144 input = "-1234,|";
145 parser.reset(NewTestParser(input));
146 value.reset(parser->ConsumeNumber());
147 EXPECT_EQ('4', *parser->pos_);
148
149 TestLastThree(parser.get());
150
151 ASSERT_TRUE(value.get());
152 EXPECT_TRUE(value->GetAsInteger(&number_i));
153 EXPECT_EQ(-1234, number_i);
154
155 // Double.
156 input = "12.34,|";
157 parser.reset(NewTestParser(input));
158 value.reset(parser->ConsumeNumber());
159 EXPECT_EQ('4', *parser->pos_);
160
161 TestLastThree(parser.get());
162
163 ASSERT_TRUE(value.get());
164 double number_d;
165 EXPECT_TRUE(value->GetAsDouble(&number_d));
166 EXPECT_EQ(12.34, number_d);
167
168 // Scientific.
169 input = "42e3,|";
170 parser.reset(NewTestParser(input));
171 value.reset(parser->ConsumeNumber());
172 EXPECT_EQ('3', *parser->pos_);
173
174 TestLastThree(parser.get());
175
176 ASSERT_TRUE(value.get());
177 EXPECT_TRUE(value->GetAsDouble(&number_d));
178 EXPECT_EQ(42000, number_d);
179
180 // Negative scientific.
181 input = "314159e-5,|";
182 parser.reset(NewTestParser(input));
183 value.reset(parser->ConsumeNumber());
184 EXPECT_EQ('5', *parser->pos_);
185
186 TestLastThree(parser.get());
187
188 ASSERT_TRUE(value.get());
189 EXPECT_TRUE(value->GetAsDouble(&number_d));
190 EXPECT_EQ(3.14159, number_d);
191
192 // Positive scientific.
193 input = "0.42e+3,|";
194 parser.reset(NewTestParser(input));
195 value.reset(parser->ConsumeNumber());
196 EXPECT_EQ('3', *parser->pos_);
197
198 TestLastThree(parser.get());
199
200 ASSERT_TRUE(value.get());
201 EXPECT_TRUE(value->GetAsDouble(&number_d));
202 EXPECT_EQ(420, number_d);
203 }
204
TEST_F(JSONParserTest,ErrorMessages)205 TEST_F(JSONParserTest, ErrorMessages) {
206 // Error strings should not be modified in case of success.
207 std::string error_message;
208 int error_code = 0;
209 std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
210 "[42]", JSON_PARSE_RFC, &error_code, &error_message);
211 EXPECT_TRUE(error_message.empty());
212 EXPECT_EQ(0, error_code);
213
214 // Test line and column counting
215 const char big_json[] = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
216 // error here ----------------------------------^
217 root = JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC, &error_code,
218 &error_message);
219 EXPECT_FALSE(root.get());
220 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
221 error_message);
222 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
223
224 error_code = 0;
225 error_message = "";
226 // Test line and column counting with "\r\n" line ending
227 const char big_json_crlf[] =
228 "[\r\n0,\r\n1,\r\n2,\r\n3,4,5,6 7,\r\n8,\r\n9\r\n]";
229 // error here ----------------------^
230 root = JSONReader::ReadAndReturnError(big_json_crlf, JSON_PARSE_RFC,
231 &error_code, &error_message);
232 EXPECT_FALSE(root.get());
233 EXPECT_EQ(JSONParser::FormatErrorMessage(5, 10, JSONReader::kSyntaxError),
234 error_message);
235 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
236
237 // Test each of the error conditions
238 root = JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC, &error_code,
239 &error_message);
240 EXPECT_FALSE(root.get());
241 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 3,
242 JSONReader::kUnexpectedDataAfterRoot), error_message);
243 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
244
245 std::string nested_json;
246 for (int i = 0; i < 101; ++i) {
247 nested_json.insert(nested_json.begin(), '[');
248 nested_json.append(1, ']');
249 }
250 root = JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
251 &error_code, &error_message);
252 EXPECT_FALSE(root.get());
253 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 100, JSONReader::kTooMuchNesting),
254 error_message);
255 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
256
257 root = JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC, &error_code,
258 &error_message);
259 EXPECT_FALSE(root.get());
260 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
261 error_message);
262 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
263
264 root = JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC,
265 &error_code, &error_message);
266 EXPECT_FALSE(root.get());
267 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2,
268 JSONReader::kUnquotedDictionaryKey), error_message);
269 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
270
271 root = JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", JSON_PARSE_RFC,
272 &error_code, &error_message);
273 EXPECT_FALSE(root.get());
274 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
275 error_message);
276
277 root = JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC, &error_code,
278 &error_message);
279 EXPECT_FALSE(root.get());
280 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
281 error_message);
282 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
283
284 root = JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC,
285 &error_code, &error_message);
286 EXPECT_FALSE(root.get());
287 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
288 error_message);
289 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
290
291 root = JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC,
292 &error_code, &error_message);
293 EXPECT_FALSE(root.get());
294 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
295 error_message);
296 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
297
298 root = JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC,
299 &error_code, &error_message);
300 EXPECT_FALSE(root.get());
301 EXPECT_EQ(JSONParser::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
302 error_message);
303 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
304 }
305
TEST_F(JSONParserTest,Decode4ByteUtf8Char)306 TEST_F(JSONParserTest, Decode4ByteUtf8Char) {
307 // This test strings contains a 4 byte unicode character (a smiley!) that the
308 // reader should be able to handle (the character is \xf0\x9f\x98\x87).
309 const char kUtf8Data[] =
310 "[\"\",[],[],[],{\"google:suggesttype\":[]}]";
311 std::string error_message;
312 int error_code = 0;
313 std::unique_ptr<Value> root = JSONReader::ReadAndReturnError(
314 kUtf8Data, JSON_PARSE_RFC, &error_code, &error_message);
315 EXPECT_TRUE(root.get()) << error_message;
316 }
317
TEST_F(JSONParserTest,DecodeUnicodeNonCharacter)318 TEST_F(JSONParserTest, DecodeUnicodeNonCharacter) {
319 // Tests Unicode code points (encoded as escaped UTF-16) that are not valid
320 // characters.
321 EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]"));
322 EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]"));
323 EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]"));
324 }
325
326 } // namespace internal
327 } // namespace base
328