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