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