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