• 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_reader.h"
6 
7 #include <stddef.h>
8 
9 #if !defined(__ANDROID__) && !defined(__ANDROID_HOST__)
10 #include "base/base_paths.h"
11 #include "base/path_service.h"
12 #endif
13 
14 #include "base/files/file_util.h"
15 #include "base/logging.h"
16 #include "base/macros.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/strings/string_piece.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/values.h"
21 #include "build/build_config.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 
24 namespace base {
25 
TEST(JSONReaderTest,Reading)26 TEST(JSONReaderTest, Reading) {
27   // some whitespace checking
28   scoped_ptr<Value> root = JSONReader().ReadToValue("   null   ");
29   ASSERT_TRUE(root.get());
30   EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
31 
32   // Invalid JSON string
33   root = JSONReader().ReadToValue("nu");
34   EXPECT_FALSE(root.get());
35 
36   // Simple bool
37   root = JSONReader().ReadToValue("true  ");
38   ASSERT_TRUE(root.get());
39   EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
40 
41   // Embedded comment
42   root = JSONReader().ReadToValue("/* comment */null");
43   ASSERT_TRUE(root.get());
44   EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
45   root = JSONReader().ReadToValue("40 /* comment */");
46   ASSERT_TRUE(root.get());
47   EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
48   root = JSONReader().ReadToValue("true // comment");
49   ASSERT_TRUE(root.get());
50   EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
51   root = JSONReader().ReadToValue("/* comment */\"sample string\"");
52   ASSERT_TRUE(root.get());
53   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
54   std::string value;
55   EXPECT_TRUE(root->GetAsString(&value));
56   EXPECT_EQ("sample string", value);
57   root = JSONReader().ReadToValue("[1, /* comment, 2 ] */ \n 3]");
58   ASSERT_TRUE(root.get());
59   ListValue* list = static_cast<ListValue*>(root.get());
60   EXPECT_EQ(2u, list->GetSize());
61   int int_val = 0;
62   EXPECT_TRUE(list->GetInteger(0, &int_val));
63   EXPECT_EQ(1, int_val);
64   EXPECT_TRUE(list->GetInteger(1, &int_val));
65   EXPECT_EQ(3, int_val);
66   root = JSONReader().ReadToValue("[1, /*a*/2, 3]");
67   ASSERT_TRUE(root.get());
68   list = static_cast<ListValue*>(root.get());
69   EXPECT_EQ(3u, list->GetSize());
70   root = JSONReader().ReadToValue("/* comment **/42");
71   ASSERT_TRUE(root.get());
72   EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
73   EXPECT_TRUE(root->GetAsInteger(&int_val));
74   EXPECT_EQ(42, int_val);
75   root = JSONReader().ReadToValue(
76       "/* comment **/\n"
77       "// */ 43\n"
78       "44");
79   ASSERT_TRUE(root.get());
80   EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
81   EXPECT_TRUE(root->GetAsInteger(&int_val));
82   EXPECT_EQ(44, int_val);
83 
84   // Test number formats
85   root = JSONReader().ReadToValue("43");
86   ASSERT_TRUE(root.get());
87   EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
88   EXPECT_TRUE(root->GetAsInteger(&int_val));
89   EXPECT_EQ(43, int_val);
90 
91   // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
92   root = JSONReader().ReadToValue("043");
93   EXPECT_FALSE(root.get());
94   root = JSONReader().ReadToValue("0x43");
95   EXPECT_FALSE(root.get());
96   root = JSONReader().ReadToValue("00");
97   EXPECT_FALSE(root.get());
98 
99   // Test 0 (which needs to be special cased because of the leading zero
100   // clause).
101   root = JSONReader().ReadToValue("0");
102   ASSERT_TRUE(root.get());
103   EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
104   int_val = 1;
105   EXPECT_TRUE(root->GetAsInteger(&int_val));
106   EXPECT_EQ(0, int_val);
107 
108   // Numbers that overflow ints should succeed, being internally promoted to
109   // storage as doubles
110   root = JSONReader().ReadToValue("2147483648");
111   ASSERT_TRUE(root.get());
112   double double_val;
113   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
114   double_val = 0.0;
115   EXPECT_TRUE(root->GetAsDouble(&double_val));
116   EXPECT_DOUBLE_EQ(2147483648.0, double_val);
117   root = JSONReader().ReadToValue("-2147483649");
118   ASSERT_TRUE(root.get());
119   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
120   double_val = 0.0;
121   EXPECT_TRUE(root->GetAsDouble(&double_val));
122   EXPECT_DOUBLE_EQ(-2147483649.0, double_val);
123 
124   // Parse a double
125   root = JSONReader().ReadToValue("43.1");
126   ASSERT_TRUE(root.get());
127   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
128   double_val = 0.0;
129   EXPECT_TRUE(root->GetAsDouble(&double_val));
130   EXPECT_DOUBLE_EQ(43.1, double_val);
131 
132   root = JSONReader().ReadToValue("4.3e-1");
133   ASSERT_TRUE(root.get());
134   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
135   double_val = 0.0;
136   EXPECT_TRUE(root->GetAsDouble(&double_val));
137   EXPECT_DOUBLE_EQ(.43, double_val);
138 
139   root = JSONReader().ReadToValue("2.1e0");
140   ASSERT_TRUE(root.get());
141   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
142   double_val = 0.0;
143   EXPECT_TRUE(root->GetAsDouble(&double_val));
144   EXPECT_DOUBLE_EQ(2.1, double_val);
145 
146   root = JSONReader().ReadToValue("2.1e+0001");
147   ASSERT_TRUE(root.get());
148   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
149   double_val = 0.0;
150   EXPECT_TRUE(root->GetAsDouble(&double_val));
151   EXPECT_DOUBLE_EQ(21.0, double_val);
152 
153   root = JSONReader().ReadToValue("0.01");
154   ASSERT_TRUE(root.get());
155   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
156   double_val = 0.0;
157   EXPECT_TRUE(root->GetAsDouble(&double_val));
158   EXPECT_DOUBLE_EQ(0.01, double_val);
159 
160   root = JSONReader().ReadToValue("1.00");
161   ASSERT_TRUE(root.get());
162   EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
163   double_val = 0.0;
164   EXPECT_TRUE(root->GetAsDouble(&double_val));
165   EXPECT_DOUBLE_EQ(1.0, double_val);
166 
167   // Fractional parts must have a digit before and after the decimal point.
168   root = JSONReader().ReadToValue("1.");
169   EXPECT_FALSE(root.get());
170   root = JSONReader().ReadToValue(".1");
171   EXPECT_FALSE(root.get());
172   root = JSONReader().ReadToValue("1.e10");
173   EXPECT_FALSE(root.get());
174 
175   // Exponent must have a digit following the 'e'.
176   root = JSONReader().ReadToValue("1e");
177   EXPECT_FALSE(root.get());
178   root = JSONReader().ReadToValue("1E");
179   EXPECT_FALSE(root.get());
180   root = JSONReader().ReadToValue("1e1.");
181   EXPECT_FALSE(root.get());
182   root = JSONReader().ReadToValue("1e1.0");
183   EXPECT_FALSE(root.get());
184 
185   // INF/-INF/NaN are not valid
186   root = JSONReader().ReadToValue("1e1000");
187   EXPECT_FALSE(root.get());
188   root = JSONReader().ReadToValue("-1e1000");
189   EXPECT_FALSE(root.get());
190   root = JSONReader().ReadToValue("NaN");
191   EXPECT_FALSE(root.get());
192   root = JSONReader().ReadToValue("nan");
193   EXPECT_FALSE(root.get());
194   root = JSONReader().ReadToValue("inf");
195   EXPECT_FALSE(root.get());
196 
197   // Invalid number formats
198   root = JSONReader().ReadToValue("4.3.1");
199   EXPECT_FALSE(root.get());
200   root = JSONReader().ReadToValue("4e3.1");
201   EXPECT_FALSE(root.get());
202 
203   // Test string parser
204   root = JSONReader().ReadToValue("\"hello world\"");
205   ASSERT_TRUE(root.get());
206   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
207   std::string str_val;
208   EXPECT_TRUE(root->GetAsString(&str_val));
209   EXPECT_EQ("hello world", str_val);
210 
211   // Empty string
212   root = JSONReader().ReadToValue("\"\"");
213   ASSERT_TRUE(root.get());
214   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
215   str_val.clear();
216   EXPECT_TRUE(root->GetAsString(&str_val));
217   EXPECT_EQ("", str_val);
218 
219   // Test basic string escapes
220   root = JSONReader().ReadToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"");
221   ASSERT_TRUE(root.get());
222   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
223   str_val.clear();
224   EXPECT_TRUE(root->GetAsString(&str_val));
225   EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val);
226 
227   // Test hex and unicode escapes including the null character.
228   root = JSONReader().ReadToValue("\"\\x41\\x00\\u1234\"");
229   ASSERT_TRUE(root.get());
230   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
231   str_val.clear();
232   EXPECT_TRUE(root->GetAsString(&str_val));
233   EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val));
234 
235   // Test invalid strings
236   root = JSONReader().ReadToValue("\"no closing quote");
237   EXPECT_FALSE(root.get());
238   root = JSONReader().ReadToValue("\"\\z invalid escape char\"");
239   EXPECT_FALSE(root.get());
240   root = JSONReader().ReadToValue("\"\\xAQ invalid hex code\"");
241   EXPECT_FALSE(root.get());
242   root = JSONReader().ReadToValue("not enough hex chars\\x1\"");
243   EXPECT_FALSE(root.get());
244   root = JSONReader().ReadToValue("\"not enough escape chars\\u123\"");
245   EXPECT_FALSE(root.get());
246   root = JSONReader().ReadToValue("\"extra backslash at end of input\\\"");
247   EXPECT_FALSE(root.get());
248 
249   // Basic array
250   root = JSONReader::Read("[true, false, null]");
251   ASSERT_TRUE(root.get());
252   EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
253   list = static_cast<ListValue*>(root.get());
254   EXPECT_EQ(3U, list->GetSize());
255 
256   // Test with trailing comma.  Should be parsed the same as above.
257   scoped_ptr<Value> root2 =
258       JSONReader::Read("[true, false, null, ]", JSON_ALLOW_TRAILING_COMMAS);
259   EXPECT_TRUE(root->Equals(root2.get()));
260 
261   // Empty array
262   root = JSONReader::Read("[]");
263   ASSERT_TRUE(root.get());
264   EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
265   list = static_cast<ListValue*>(root.get());
266   EXPECT_EQ(0U, list->GetSize());
267 
268   // Nested arrays
269   root = JSONReader::Read("[[true], [], [false, [], [null]], null]");
270   ASSERT_TRUE(root.get());
271   EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
272   list = static_cast<ListValue*>(root.get());
273   EXPECT_EQ(4U, list->GetSize());
274 
275   // Lots of trailing commas.
276   root2 = JSONReader::Read("[[true], [], [false, [], [null, ]  , ], null,]",
277                            JSON_ALLOW_TRAILING_COMMAS);
278   EXPECT_TRUE(root->Equals(root2.get()));
279 
280   // Invalid, missing close brace.
281   root = JSONReader::Read("[[true], [], [false, [], [null]], null");
282   EXPECT_FALSE(root.get());
283 
284   // Invalid, too many commas
285   root = JSONReader::Read("[true,, null]");
286   EXPECT_FALSE(root.get());
287   root = JSONReader::Read("[true,, null]", JSON_ALLOW_TRAILING_COMMAS);
288   EXPECT_FALSE(root.get());
289 
290   // Invalid, no commas
291   root = JSONReader::Read("[true null]");
292   EXPECT_FALSE(root.get());
293 
294   // Invalid, trailing comma
295   root = JSONReader::Read("[true,]");
296   EXPECT_FALSE(root.get());
297 
298   // Valid if we set |allow_trailing_comma| to true.
299   root = JSONReader::Read("[true,]", JSON_ALLOW_TRAILING_COMMAS);
300   ASSERT_TRUE(root.get());
301   EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
302   list = static_cast<ListValue*>(root.get());
303   EXPECT_EQ(1U, list->GetSize());
304   Value* tmp_value = NULL;
305   ASSERT_TRUE(list->Get(0, &tmp_value));
306   EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
307   bool bool_value = false;
308   EXPECT_TRUE(tmp_value->GetAsBoolean(&bool_value));
309   EXPECT_TRUE(bool_value);
310 
311   // Don't allow empty elements, even if |allow_trailing_comma| is
312   // true.
313   root = JSONReader::Read("[,]", JSON_ALLOW_TRAILING_COMMAS);
314   EXPECT_FALSE(root.get());
315   root = JSONReader::Read("[true,,]", JSON_ALLOW_TRAILING_COMMAS);
316   EXPECT_FALSE(root.get());
317   root = JSONReader::Read("[,true,]", JSON_ALLOW_TRAILING_COMMAS);
318   EXPECT_FALSE(root.get());
319   root = JSONReader::Read("[true,,false]", JSON_ALLOW_TRAILING_COMMAS);
320   EXPECT_FALSE(root.get());
321 
322   // Test objects
323   root = JSONReader::Read("{}");
324   ASSERT_TRUE(root.get());
325   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
326 
327   root = JSONReader::Read(
328       "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }");
329   ASSERT_TRUE(root.get());
330   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
331   DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
332   double_val = 0.0;
333   EXPECT_TRUE(dict_val->GetDouble("number", &double_val));
334   EXPECT_DOUBLE_EQ(9.87654321, double_val);
335   Value* null_val = NULL;
336   ASSERT_TRUE(dict_val->Get("null", &null_val));
337   EXPECT_TRUE(null_val->IsType(Value::TYPE_NULL));
338   str_val.clear();
339   EXPECT_TRUE(dict_val->GetString("S", &str_val));
340   EXPECT_EQ("str", str_val);
341 
342   root2 = JSONReader::Read(
343       "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }",
344       JSON_ALLOW_TRAILING_COMMAS);
345   ASSERT_TRUE(root2.get());
346   EXPECT_TRUE(root->Equals(root2.get()));
347 
348   // Test newline equivalence.
349   root2 = JSONReader::Read(
350       "{\n"
351       "  \"number\":9.87654321,\n"
352       "  \"null\":null,\n"
353       "  \"\\x53\":\"str\",\n"
354       "}\n",
355       JSON_ALLOW_TRAILING_COMMAS);
356   ASSERT_TRUE(root2.get());
357   EXPECT_TRUE(root->Equals(root2.get()));
358 
359   root2 = JSONReader::Read(
360       "{\r\n"
361       "  \"number\":9.87654321,\r\n"
362       "  \"null\":null,\r\n"
363       "  \"\\x53\":\"str\",\r\n"
364       "}\r\n",
365       JSON_ALLOW_TRAILING_COMMAS);
366   ASSERT_TRUE(root2.get());
367   EXPECT_TRUE(root->Equals(root2.get()));
368 
369   // Test nesting
370   root = JSONReader::Read(
371       "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}");
372   ASSERT_TRUE(root.get());
373   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
374   dict_val = static_cast<DictionaryValue*>(root.get());
375   DictionaryValue* inner_dict = NULL;
376   ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict));
377   ListValue* inner_array = NULL;
378   ASSERT_TRUE(inner_dict->GetList("array", &inner_array));
379   EXPECT_EQ(1U, inner_array->GetSize());
380   bool_value = true;
381   EXPECT_TRUE(dict_val->GetBoolean("false", &bool_value));
382   EXPECT_FALSE(bool_value);
383   inner_dict = NULL;
384   EXPECT_TRUE(dict_val->GetDictionary("d", &inner_dict));
385 
386   root2 = JSONReader::Read(
387       "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}",
388       JSON_ALLOW_TRAILING_COMMAS);
389   EXPECT_TRUE(root->Equals(root2.get()));
390 
391   // Test keys with periods
392   root = JSONReader::Read("{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}");
393   ASSERT_TRUE(root.get());
394   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
395   dict_val = static_cast<DictionaryValue*>(root.get());
396   int integer_value = 0;
397   EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
398   EXPECT_EQ(3, integer_value);
399   EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value));
400   EXPECT_EQ(2, integer_value);
401   inner_dict = NULL;
402   ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f",
403                                                           &inner_dict));
404   EXPECT_EQ(1U, inner_dict->size());
405   EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j",
406                                                          &integer_value));
407   EXPECT_EQ(1, integer_value);
408 
409   root = JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}");
410   ASSERT_TRUE(root.get());
411   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
412   dict_val = static_cast<DictionaryValue*>(root.get());
413   EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value));
414   EXPECT_EQ(2, integer_value);
415   EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
416   EXPECT_EQ(1, integer_value);
417 
418   // Invalid, no closing brace
419   root = JSONReader::Read("{\"a\": true");
420   EXPECT_FALSE(root.get());
421 
422   // Invalid, keys must be quoted
423   root = JSONReader::Read("{foo:true}");
424   EXPECT_FALSE(root.get());
425 
426   // Invalid, trailing comma
427   root = JSONReader::Read("{\"a\":true,}");
428   EXPECT_FALSE(root.get());
429 
430   // Invalid, too many commas
431   root = JSONReader::Read("{\"a\":true,,\"b\":false}");
432   EXPECT_FALSE(root.get());
433   root =
434       JSONReader::Read("{\"a\":true,,\"b\":false}", JSON_ALLOW_TRAILING_COMMAS);
435   EXPECT_FALSE(root.get());
436 
437   // Invalid, no separator
438   root = JSONReader::Read("{\"a\" \"b\"}");
439   EXPECT_FALSE(root.get());
440 
441   // Invalid, lone comma.
442   root = JSONReader::Read("{,}");
443   EXPECT_FALSE(root.get());
444   root = JSONReader::Read("{,}", JSON_ALLOW_TRAILING_COMMAS);
445   EXPECT_FALSE(root.get());
446   root = JSONReader::Read("{\"a\":true,,}", JSON_ALLOW_TRAILING_COMMAS);
447   EXPECT_FALSE(root.get());
448   root = JSONReader::Read("{,\"a\":true}", JSON_ALLOW_TRAILING_COMMAS);
449   EXPECT_FALSE(root.get());
450   root =
451       JSONReader::Read("{\"a\":true,,\"b\":false}", JSON_ALLOW_TRAILING_COMMAS);
452   EXPECT_FALSE(root.get());
453 
454   // Test stack overflow
455   std::string evil(1000000, '[');
456   evil.append(std::string(1000000, ']'));
457   root = JSONReader::Read(evil);
458   EXPECT_FALSE(root.get());
459 
460   // A few thousand adjacent lists is fine.
461   std::string not_evil("[");
462   not_evil.reserve(15010);
463   for (int i = 0; i < 5000; ++i) {
464     not_evil.append("[],");
465   }
466   not_evil.append("[]]");
467   root = JSONReader::Read(not_evil);
468   ASSERT_TRUE(root.get());
469   EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
470   list = static_cast<ListValue*>(root.get());
471   EXPECT_EQ(5001U, list->GetSize());
472 
473   // Test utf8 encoded input
474   root = JSONReader().ReadToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"");
475   ASSERT_TRUE(root.get());
476   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
477   str_val.clear();
478   EXPECT_TRUE(root->GetAsString(&str_val));
479   EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val));
480 
481   root = JSONReader().ReadToValue(
482       "{\"path\": \"/tmp/\xc3\xa0\xc3\xa8\xc3\xb2.png\"}");
483   ASSERT_TRUE(root.get());
484   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
485   EXPECT_TRUE(root->GetAsDictionary(&dict_val));
486   EXPECT_TRUE(dict_val->GetString("path", &str_val));
487   EXPECT_EQ("/tmp/\xC3\xA0\xC3\xA8\xC3\xB2.png", str_val);
488 
489   // Test invalid utf8 encoded input
490   root = JSONReader().ReadToValue("\"345\xb0\xa1\xb0\xa2\"");
491   EXPECT_FALSE(root.get());
492   root = JSONReader().ReadToValue("\"123\xc0\x81\"");
493   EXPECT_FALSE(root.get());
494   root = JSONReader().ReadToValue("\"abc\xc0\xae\"");
495   EXPECT_FALSE(root.get());
496 
497   // Test utf16 encoded strings.
498   root = JSONReader().ReadToValue("\"\\u20ac3,14\"");
499   ASSERT_TRUE(root.get());
500   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
501   str_val.clear();
502   EXPECT_TRUE(root->GetAsString(&str_val));
503   EXPECT_EQ("\xe2\x82\xac""3,14", str_val);
504 
505   root = JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"");
506   ASSERT_TRUE(root.get());
507   EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
508   str_val.clear();
509   EXPECT_TRUE(root->GetAsString(&str_val));
510   EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val);
511 
512   // Test invalid utf16 strings.
513   const char* const cases[] = {
514     "\"\\u123\"",  // Invalid scalar.
515     "\"\\ud83d\"",  // Invalid scalar.
516     "\"\\u$%@!\"",  // Invalid scalar.
517     "\"\\uzz89\"",  // Invalid scalar.
518     "\"\\ud83d\\udca\"",  // Invalid lower surrogate.
519     "\"\\ud83d\\ud83d\"",  // Invalid lower surrogate.
520     "\"\\ud83foo\"",  // No lower surrogate.
521     "\"\\ud83\\foo\""  // No lower surrogate.
522   };
523   for (size_t i = 0; i < arraysize(cases); ++i) {
524     root = JSONReader().ReadToValue(cases[i]);
525     EXPECT_FALSE(root.get()) << cases[i];
526   }
527 
528   // Test literal root objects.
529   root = JSONReader::Read("null");
530   EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
531 
532   root = JSONReader::Read("true");
533   ASSERT_TRUE(root.get());
534   EXPECT_TRUE(root->GetAsBoolean(&bool_value));
535   EXPECT_TRUE(bool_value);
536 
537   root = JSONReader::Read("10");
538   ASSERT_TRUE(root.get());
539   EXPECT_TRUE(root->GetAsInteger(&integer_value));
540   EXPECT_EQ(10, integer_value);
541 
542   root = JSONReader::Read("\"root\"");
543   ASSERT_TRUE(root.get());
544   EXPECT_TRUE(root->GetAsString(&str_val));
545   EXPECT_EQ("root", str_val);
546 }
547 
548 #if !defined(__ANDROID__) && !defined(__ANDROID_HOST__)
TEST(JSONReaderTest,ReadFromFile)549 TEST(JSONReaderTest, ReadFromFile) {
550   FilePath path;
551   ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &path));
552   path = path.AppendASCII("json");
553   ASSERT_TRUE(base::PathExists(path));
554 
555   std::string input;
556   ASSERT_TRUE(ReadFileToString(
557       path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input));
558 
559   JSONReader reader;
560   scoped_ptr<Value> root(reader.ReadToValue(input));
561   ASSERT_TRUE(root.get()) << reader.GetErrorMessage();
562   EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
563 }
564 #endif  // !__ANDROID__ && !__ANDROID_HOST__
565 
566 // Tests that the root of a JSON object can be deleted safely while its
567 // children outlive it.
TEST(JSONReaderTest,StringOptimizations)568 TEST(JSONReaderTest, StringOptimizations) {
569   scoped_ptr<Value> dict_literal_0;
570   scoped_ptr<Value> dict_literal_1;
571   scoped_ptr<Value> dict_string_0;
572   scoped_ptr<Value> dict_string_1;
573   scoped_ptr<Value> list_value_0;
574   scoped_ptr<Value> list_value_1;
575 
576   {
577     scoped_ptr<Value> root = JSONReader::Read(
578         "{"
579         "  \"test\": {"
580         "    \"foo\": true,"
581         "    \"bar\": 3.14,"
582         "    \"baz\": \"bat\","
583         "    \"moo\": \"cow\""
584         "  },"
585         "  \"list\": ["
586         "    \"a\","
587         "    \"b\""
588         "  ]"
589         "}",
590         JSON_DETACHABLE_CHILDREN);
591     ASSERT_TRUE(root.get());
592 
593     DictionaryValue* root_dict = NULL;
594     ASSERT_TRUE(root->GetAsDictionary(&root_dict));
595 
596     DictionaryValue* dict = NULL;
597     ListValue* list = NULL;
598 
599     ASSERT_TRUE(root_dict->GetDictionary("test", &dict));
600     ASSERT_TRUE(root_dict->GetList("list", &list));
601 
602     EXPECT_TRUE(dict->Remove("foo", &dict_literal_0));
603     EXPECT_TRUE(dict->Remove("bar", &dict_literal_1));
604     EXPECT_TRUE(dict->Remove("baz", &dict_string_0));
605     EXPECT_TRUE(dict->Remove("moo", &dict_string_1));
606 
607     ASSERT_EQ(2u, list->GetSize());
608     EXPECT_TRUE(list->Remove(0, &list_value_0));
609     EXPECT_TRUE(list->Remove(0, &list_value_1));
610   }
611 
612   bool b = false;
613   double d = 0;
614   std::string s;
615 
616   EXPECT_TRUE(dict_literal_0->GetAsBoolean(&b));
617   EXPECT_TRUE(b);
618 
619   EXPECT_TRUE(dict_literal_1->GetAsDouble(&d));
620   EXPECT_EQ(3.14, d);
621 
622   EXPECT_TRUE(dict_string_0->GetAsString(&s));
623   EXPECT_EQ("bat", s);
624 
625   EXPECT_TRUE(dict_string_1->GetAsString(&s));
626   EXPECT_EQ("cow", s);
627 
628   EXPECT_TRUE(list_value_0->GetAsString(&s));
629   EXPECT_EQ("a", s);
630   EXPECT_TRUE(list_value_1->GetAsString(&s));
631   EXPECT_EQ("b", s);
632 }
633 
634 // A smattering of invalid JSON designed to test specific portions of the
635 // parser implementation against buffer overflow. Best run with DCHECKs so
636 // that the one in NextChar fires.
TEST(JSONReaderTest,InvalidSanity)637 TEST(JSONReaderTest, InvalidSanity) {
638   const char* const invalid_json[] = {
639       "/* test *",
640       "{\"foo\"",
641       "{\"foo\":",
642       "  [",
643       "\"\\u123g\"",
644       "{\n\"eh:\n}",
645   };
646 
647   for (size_t i = 0; i < arraysize(invalid_json); ++i) {
648     JSONReader reader;
649     LOG(INFO) << "Sanity test " << i << ": <" << invalid_json[i] << ">";
650     EXPECT_FALSE(reader.ReadToValue(invalid_json[i]));
651     EXPECT_NE(JSONReader::JSON_NO_ERROR, reader.error_code());
652     EXPECT_NE("", reader.GetErrorMessage());
653   }
654 }
655 
TEST(JSONReaderTest,IllegalTrailingNull)656 TEST(JSONReaderTest, IllegalTrailingNull) {
657   const char json[] = { '"', 'n', 'u', 'l', 'l', '"', '\0' };
658   std::string json_string(json, sizeof(json));
659   JSONReader reader;
660   EXPECT_FALSE(reader.ReadToValue(json_string));
661   EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, reader.error_code());
662 }
663 
664 }  // namespace base
665