Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(181)

Side by Side Diff: base/json/json_reader_unittest.cc

Issue 10035042: Rewrite base::JSONReader to be 35-40% faster, depending on the input string. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: '' Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/json/json_reader.h" 5 #include "base/json/json_reader.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/string_piece.h" 11 #include "base/string_piece.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 #include "build/build_config.h" 14 #include "build/build_config.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 namespace base { 17 namespace base {
18 18
19 TEST(JSONReaderTest, Reading) { 19 TEST(JSONReaderTest, Reading) {
20 // some whitespace checking 20 // some whitespace checking
21 scoped_ptr<Value> root; 21 scoped_ptr<Value> root;
22 root.reset(JSONReader().JsonToValue(" null ", false, false)); 22 root.reset(JSONReader().ReadToValue(" null "));
23 ASSERT_TRUE(root.get()); 23 ASSERT_TRUE(root.get());
24 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); 24 EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
25 25
26 // Invalid JSON string 26 // Invalid JSON string
27 root.reset(JSONReader().JsonToValue("nu", false, false)); 27 root.reset(JSONReader().ReadToValue("nu"));
28 EXPECT_FALSE(root.get()); 28 EXPECT_FALSE(root.get());
29 29
30 // Simple bool 30 // Simple bool
31 root.reset(JSONReader().JsonToValue("true ", false, false)); 31 root.reset(JSONReader().ReadToValue("true "));
32 ASSERT_TRUE(root.get()); 32 ASSERT_TRUE(root.get());
33 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); 33 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
34 34
35 // Embedded comment 35 // Embedded comment
36 root.reset(JSONReader().JsonToValue("/* comment */null", false, false)); 36 root.reset(JSONReader().ReadToValue("/* comment */null"));
37 ASSERT_TRUE(root.get()); 37 ASSERT_TRUE(root.get());
38 EXPECT_TRUE(root->IsType(Value::TYPE_NULL)); 38 EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
39 root.reset(JSONReader().JsonToValue("40 /* comment */", false, false)); 39 root.reset(JSONReader().ReadToValue("40 /* comment */"));
40 ASSERT_TRUE(root.get()); 40 ASSERT_TRUE(root.get());
41 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 41 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
42 root.reset(JSONReader().JsonToValue("true // comment", false, false)); 42 root.reset(JSONReader().ReadToValue("true // comment"));
43 ASSERT_TRUE(root.get()); 43 ASSERT_TRUE(root.get());
44 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN)); 44 EXPECT_TRUE(root->IsType(Value::TYPE_BOOLEAN));
45 root.reset(JSONReader().JsonToValue("/* comment */\"sample string\"", 45 root.reset(JSONReader().ReadToValue("/* comment */\"sample string\""));
46 false, false));
47 ASSERT_TRUE(root.get()); 46 ASSERT_TRUE(root.get());
48 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 47 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
49 std::string value; 48 std::string value;
50 EXPECT_TRUE(root->GetAsString(&value)); 49 EXPECT_TRUE(root->GetAsString(&value));
51 EXPECT_EQ("sample string", value); 50 EXPECT_EQ("sample string", value);
51 root.reset(JSONReader().ReadToValue("[1, /* comment, 2 ] */ \n 3]"));
52 ASSERT_TRUE(root.get());
53 ListValue* list = static_cast<ListValue*>(root.get());
54 EXPECT_EQ(2u, list->GetSize());
55 int int_val = 0;
56 EXPECT_TRUE(list->GetInteger(0, &int_val));
57 EXPECT_EQ(1, int_val);
58 EXPECT_TRUE(list->GetInteger(1, &int_val));
59 EXPECT_EQ(3, int_val);
52 60
53 // Test number formats 61 // Test number formats
54 root.reset(JSONReader().JsonToValue("43", false, false)); 62 root.reset(JSONReader().ReadToValue("43"));
55 ASSERT_TRUE(root.get()); 63 ASSERT_TRUE(root.get());
56 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 64 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
57 int int_val = 0;
58 EXPECT_TRUE(root->GetAsInteger(&int_val)); 65 EXPECT_TRUE(root->GetAsInteger(&int_val));
59 EXPECT_EQ(43, int_val); 66 EXPECT_EQ(43, int_val);
60 67
61 // According to RFC4627, oct, hex, and leading zeros are invalid JSON. 68 // According to RFC4627, oct, hex, and leading zeros are invalid JSON.
62 root.reset(JSONReader().JsonToValue("043", false, false)); 69 root.reset(JSONReader().ReadToValue("043"));
63 EXPECT_FALSE(root.get()); 70 EXPECT_FALSE(root.get());
64 root.reset(JSONReader().JsonToValue("0x43", false, false)); 71 root.reset(JSONReader().ReadToValue("0x43"));
65 EXPECT_FALSE(root.get()); 72 EXPECT_FALSE(root.get());
66 root.reset(JSONReader().JsonToValue("00", false, false)); 73 root.reset(JSONReader().ReadToValue("00"));
67 EXPECT_FALSE(root.get()); 74 EXPECT_FALSE(root.get());
68 75
69 // Test 0 (which needs to be special cased because of the leading zero 76 // Test 0 (which needs to be special cased because of the leading zero
70 // clause). 77 // clause).
71 root.reset(JSONReader().JsonToValue("0", false, false)); 78 root.reset(JSONReader().ReadToValue("0"));
72 ASSERT_TRUE(root.get()); 79 ASSERT_TRUE(root.get());
73 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER)); 80 EXPECT_TRUE(root->IsType(Value::TYPE_INTEGER));
74 int_val = 1; 81 int_val = 1;
75 EXPECT_TRUE(root->GetAsInteger(&int_val)); 82 EXPECT_TRUE(root->GetAsInteger(&int_val));
76 EXPECT_EQ(0, int_val); 83 EXPECT_EQ(0, int_val);
77 84
78 // Numbers that overflow ints should succeed, being internally promoted to 85 // Numbers that overflow ints should succeed, being internally promoted to
79 // storage as doubles 86 // storage as doubles
80 root.reset(JSONReader().JsonToValue("2147483648", false, false)); 87 root.reset(JSONReader().ReadToValue("2147483648"));
81 ASSERT_TRUE(root.get()); 88 ASSERT_TRUE(root.get());
82 double double_val; 89 double double_val;
83 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 90 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
84 double_val = 0.0; 91 double_val = 0.0;
85 EXPECT_TRUE(root->GetAsDouble(&double_val)); 92 EXPECT_TRUE(root->GetAsDouble(&double_val));
86 EXPECT_DOUBLE_EQ(2147483648.0, double_val); 93 EXPECT_DOUBLE_EQ(2147483648.0, double_val);
87 root.reset(JSONReader().JsonToValue("-2147483649", false, false)); 94 root.reset(JSONReader().ReadToValue("-2147483649"));
88 ASSERT_TRUE(root.get()); 95 ASSERT_TRUE(root.get());
89 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 96 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
90 double_val = 0.0; 97 double_val = 0.0;
91 EXPECT_TRUE(root->GetAsDouble(&double_val)); 98 EXPECT_TRUE(root->GetAsDouble(&double_val));
92 EXPECT_DOUBLE_EQ(-2147483649.0, double_val); 99 EXPECT_DOUBLE_EQ(-2147483649.0, double_val);
93 100
94 // Parse a double 101 // Parse a double
95 root.reset(JSONReader().JsonToValue("43.1", false, false)); 102 root.reset(JSONReader().ReadToValue("43.1"));
96 ASSERT_TRUE(root.get()); 103 ASSERT_TRUE(root.get());
97 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 104 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
98 double_val = 0.0; 105 double_val = 0.0;
99 EXPECT_TRUE(root->GetAsDouble(&double_val)); 106 EXPECT_TRUE(root->GetAsDouble(&double_val));
100 EXPECT_DOUBLE_EQ(43.1, double_val); 107 EXPECT_DOUBLE_EQ(43.1, double_val);
101 108
102 root.reset(JSONReader().JsonToValue("4.3e-1", false, false)); 109 root.reset(JSONReader().ReadToValue("4.3e-1"));
103 ASSERT_TRUE(root.get()); 110 ASSERT_TRUE(root.get());
104 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 111 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
105 double_val = 0.0; 112 double_val = 0.0;
106 EXPECT_TRUE(root->GetAsDouble(&double_val)); 113 EXPECT_TRUE(root->GetAsDouble(&double_val));
107 EXPECT_DOUBLE_EQ(.43, double_val); 114 EXPECT_DOUBLE_EQ(.43, double_val);
108 115
109 root.reset(JSONReader().JsonToValue("2.1e0", false, false)); 116 root.reset(JSONReader().ReadToValue("2.1e0"));
110 ASSERT_TRUE(root.get()); 117 ASSERT_TRUE(root.get());
111 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 118 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
112 double_val = 0.0; 119 double_val = 0.0;
113 EXPECT_TRUE(root->GetAsDouble(&double_val)); 120 EXPECT_TRUE(root->GetAsDouble(&double_val));
114 EXPECT_DOUBLE_EQ(2.1, double_val); 121 EXPECT_DOUBLE_EQ(2.1, double_val);
115 122
116 root.reset(JSONReader().JsonToValue("2.1e+0001", false, false)); 123 root.reset(JSONReader().ReadToValue("2.1e+0001"));
117 ASSERT_TRUE(root.get()); 124 ASSERT_TRUE(root.get());
118 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 125 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
119 double_val = 0.0; 126 double_val = 0.0;
120 EXPECT_TRUE(root->GetAsDouble(&double_val)); 127 EXPECT_TRUE(root->GetAsDouble(&double_val));
121 EXPECT_DOUBLE_EQ(21.0, double_val); 128 EXPECT_DOUBLE_EQ(21.0, double_val);
122 129
123 root.reset(JSONReader().JsonToValue("0.01", false, false)); 130 root.reset(JSONReader().ReadToValue("0.01"));
124 ASSERT_TRUE(root.get()); 131 ASSERT_TRUE(root.get());
125 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 132 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
126 double_val = 0.0; 133 double_val = 0.0;
127 EXPECT_TRUE(root->GetAsDouble(&double_val)); 134 EXPECT_TRUE(root->GetAsDouble(&double_val));
128 EXPECT_DOUBLE_EQ(0.01, double_val); 135 EXPECT_DOUBLE_EQ(0.01, double_val);
129 136
130 root.reset(JSONReader().JsonToValue("1.00", false, false)); 137 root.reset(JSONReader().ReadToValue("1.00"));
131 ASSERT_TRUE(root.get()); 138 ASSERT_TRUE(root.get());
132 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE)); 139 EXPECT_TRUE(root->IsType(Value::TYPE_DOUBLE));
133 double_val = 0.0; 140 double_val = 0.0;
134 EXPECT_TRUE(root->GetAsDouble(&double_val)); 141 EXPECT_TRUE(root->GetAsDouble(&double_val));
135 EXPECT_DOUBLE_EQ(1.0, double_val); 142 EXPECT_DOUBLE_EQ(1.0, double_val);
136 143
137 // Fractional parts must have a digit before and after the decimal point. 144 // Fractional parts must have a digit before and after the decimal point.
138 root.reset(JSONReader().JsonToValue("1.", false, false)); 145 root.reset(JSONReader().ReadToValue("1."));
139 EXPECT_FALSE(root.get()); 146 EXPECT_FALSE(root.get());
140 root.reset(JSONReader().JsonToValue(".1", false, false)); 147 root.reset(JSONReader().ReadToValue(".1"));
141 EXPECT_FALSE(root.get()); 148 EXPECT_FALSE(root.get());
142 root.reset(JSONReader().JsonToValue("1.e10", false, false)); 149 root.reset(JSONReader().ReadToValue("1.e10"));
143 EXPECT_FALSE(root.get()); 150 EXPECT_FALSE(root.get());
144 151
145 // Exponent must have a digit following the 'e'. 152 // Exponent must have a digit following the 'e'.
146 root.reset(JSONReader().JsonToValue("1e", false, false)); 153 root.reset(JSONReader().ReadToValue("1e"));
147 EXPECT_FALSE(root.get()); 154 EXPECT_FALSE(root.get());
148 root.reset(JSONReader().JsonToValue("1E", false, false)); 155 root.reset(JSONReader().ReadToValue("1E"));
149 EXPECT_FALSE(root.get()); 156 EXPECT_FALSE(root.get());
150 root.reset(JSONReader().JsonToValue("1e1.", false, false)); 157 root.reset(JSONReader().ReadToValue("1e1."));
151 EXPECT_FALSE(root.get()); 158 EXPECT_FALSE(root.get());
152 root.reset(JSONReader().JsonToValue("1e1.0", false, false)); 159 root.reset(JSONReader().ReadToValue("1e1.0"));
153 EXPECT_FALSE(root.get()); 160 EXPECT_FALSE(root.get());
154 161
155 // INF/-INF/NaN are not valid 162 // INF/-INF/NaN are not valid
156 root.reset(JSONReader().JsonToValue("1e1000", false, false)); 163 root.reset(JSONReader().ReadToValue("1e1000"));
157 EXPECT_FALSE(root.get()); 164 EXPECT_FALSE(root.get());
158 root.reset(JSONReader().JsonToValue("-1e1000", false, false)); 165 root.reset(JSONReader().ReadToValue("-1e1000"));
159 EXPECT_FALSE(root.get()); 166 EXPECT_FALSE(root.get());
160 root.reset(JSONReader().JsonToValue("NaN", false, false)); 167 root.reset(JSONReader().ReadToValue("NaN"));
161 EXPECT_FALSE(root.get()); 168 EXPECT_FALSE(root.get());
162 root.reset(JSONReader().JsonToValue("nan", false, false)); 169 root.reset(JSONReader().ReadToValue("nan"));
163 EXPECT_FALSE(root.get()); 170 EXPECT_FALSE(root.get());
164 root.reset(JSONReader().JsonToValue("inf", false, false)); 171 root.reset(JSONReader().ReadToValue("inf"));
165 EXPECT_FALSE(root.get()); 172 EXPECT_FALSE(root.get());
166 173
167 // Invalid number formats 174 // Invalid number formats
168 root.reset(JSONReader().JsonToValue("4.3.1", false, false)); 175 root.reset(JSONReader().ReadToValue("4.3.1"));
169 EXPECT_FALSE(root.get()); 176 EXPECT_FALSE(root.get());
170 root.reset(JSONReader().JsonToValue("4e3.1", false, false)); 177 root.reset(JSONReader().ReadToValue("4e3.1"));
171 EXPECT_FALSE(root.get()); 178 EXPECT_FALSE(root.get());
172 179
173 // Test string parser 180 // Test string parser
174 root.reset(JSONReader().JsonToValue("\"hello world\"", false, false)); 181 root.reset(JSONReader().ReadToValue("\"hello world\""));
175 ASSERT_TRUE(root.get()); 182 ASSERT_TRUE(root.get());
176 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 183 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
177 std::string str_val; 184 std::string str_val;
178 EXPECT_TRUE(root->GetAsString(&str_val)); 185 EXPECT_TRUE(root->GetAsString(&str_val));
179 EXPECT_EQ("hello world", str_val); 186 EXPECT_EQ("hello world", str_val);
180 187
181 // Empty string 188 // Empty string
182 root.reset(JSONReader().JsonToValue("\"\"", false, false)); 189 root.reset(JSONReader().ReadToValue("\"\""));
183 ASSERT_TRUE(root.get()); 190 ASSERT_TRUE(root.get());
184 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 191 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
185 str_val.clear(); 192 str_val.clear();
186 EXPECT_TRUE(root->GetAsString(&str_val)); 193 EXPECT_TRUE(root->GetAsString(&str_val));
187 EXPECT_EQ("", str_val); 194 EXPECT_EQ("", str_val);
188 195
189 // Test basic string escapes 196 // Test basic string escapes
190 root.reset(JSONReader().JsonToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\"", 197 root.reset(JSONReader().ReadToValue("\" \\\"\\\\\\/\\b\\f\\n\\r\\t\\v\""));
191 false, false));
192 ASSERT_TRUE(root.get()); 198 ASSERT_TRUE(root.get());
193 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 199 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
194 str_val.clear(); 200 str_val.clear();
195 EXPECT_TRUE(root->GetAsString(&str_val)); 201 EXPECT_TRUE(root->GetAsString(&str_val));
196 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val); 202 EXPECT_EQ(" \"\\/\b\f\n\r\t\v", str_val);
197 203
198 // Test hex and unicode escapes including the null character. 204 // Test hex and unicode escapes including the null character.
199 root.reset(JSONReader().JsonToValue("\"\\x41\\x00\\u1234\"", false, 205 root.reset(JSONReader().ReadToValue("\"\\x41\\x00\\u1234\""));
200 false));
201 ASSERT_TRUE(root.get()); 206 ASSERT_TRUE(root.get());
202 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 207 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
203 str_val.clear(); 208 str_val.clear();
204 EXPECT_TRUE(root->GetAsString(&str_val)); 209 EXPECT_TRUE(root->GetAsString(&str_val));
205 EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val)); 210 EXPECT_EQ(std::wstring(L"A\0\x1234", 3), UTF8ToWide(str_val));
206 211
207 // Test invalid strings 212 // Test invalid strings
208 root.reset(JSONReader().JsonToValue("\"no closing quote", false, false)); 213 root.reset(JSONReader().ReadToValue("\"no closing quote"));
209 EXPECT_FALSE(root.get()); 214 EXPECT_FALSE(root.get());
210 root.reset(JSONReader().JsonToValue("\"\\z invalid escape char\"", false, 215 root.reset(JSONReader().ReadToValue("\"\\z invalid escape char\""));
211 false));
212 EXPECT_FALSE(root.get()); 216 EXPECT_FALSE(root.get());
213 root.reset(JSONReader().JsonToValue("\"\\xAQ invalid hex code\"", false, 217 root.reset(JSONReader().ReadToValue("\"\\xAQ invalid hex code\""));
214 false));
215 EXPECT_FALSE(root.get()); 218 EXPECT_FALSE(root.get());
216 root.reset(JSONReader().JsonToValue("not enough hex chars\\x1\"", false, 219 root.reset(JSONReader().ReadToValue("not enough hex chars\\x1\""));
217 false));
218 EXPECT_FALSE(root.get()); 220 EXPECT_FALSE(root.get());
219 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", 221 root.reset(JSONReader().ReadToValue("\"not enough escape chars\\u123\""));
220 false, false));
221 EXPECT_FALSE(root.get()); 222 EXPECT_FALSE(root.get());
222 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", 223 root.reset(JSONReader().ReadToValue("\"extra backslash at end of input\\\""));
223 false, false));
224 EXPECT_FALSE(root.get()); 224 EXPECT_FALSE(root.get());
225 225
226 // Basic array 226 // Basic array
227 root.reset(JSONReader::Read("[true, false, null]")); 227 root.reset(JSONReader::Read("[true, false, null]"));
228 ASSERT_TRUE(root.get()); 228 ASSERT_TRUE(root.get());
229 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 229 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
230 ListValue* list = static_cast<ListValue*>(root.get()); 230 list = static_cast<ListValue*>(root.get());
231 EXPECT_EQ(3U, list->GetSize()); 231 EXPECT_EQ(3U, list->GetSize());
232 232
233 // Test with trailing comma. Should be parsed the same as above. 233 // Test with trailing comma. Should be parsed the same as above.
234 scoped_ptr<Value> root2; 234 scoped_ptr<Value> root2;
235 root2.reset(JSONReader::Read("[true, false, null, ]", 235 root2.reset(JSONReader::Read("[true, false, null, ]",
236 JSON_ALLOW_TRAILING_COMMAS)); 236 JSON_ALLOW_TRAILING_COMMAS));
237 EXPECT_TRUE(root->Equals(root2.get())); 237 EXPECT_TRUE(root->Equals(root2.get()));
238 238
239 // Empty array 239 // Empty array
240 root.reset(JSONReader::Read("[]")); 240 root.reset(JSONReader::Read("[]"));
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 not_evil.append("[],"); 441 not_evil.append("[],");
442 } 442 }
443 not_evil.append("[]]"); 443 not_evil.append("[]]");
444 root.reset(JSONReader::Read(not_evil)); 444 root.reset(JSONReader::Read(not_evil));
445 ASSERT_TRUE(root.get()); 445 ASSERT_TRUE(root.get());
446 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 446 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
447 list = static_cast<ListValue*>(root.get()); 447 list = static_cast<ListValue*>(root.get());
448 EXPECT_EQ(5001U, list->GetSize()); 448 EXPECT_EQ(5001U, list->GetSize());
449 449
450 // Test utf8 encoded input 450 // Test utf8 encoded input
451 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", 451 root.reset(JSONReader().ReadToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\""));
452 false, false));
453 ASSERT_TRUE(root.get()); 452 ASSERT_TRUE(root.get());
454 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 453 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
455 str_val.clear(); 454 str_val.clear();
456 EXPECT_TRUE(root->GetAsString(&str_val)); 455 EXPECT_TRUE(root->GetAsString(&str_val));
457 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); 456 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val));
458 457
458 root.reset(JSONReader().ReadToValue(
459 "{\"path\": \"/tmp/\xc3\xa0\xc3\xa8\xc3\xb2.png\"}"));
460 ASSERT_TRUE(root.get());
461 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
462 EXPECT_TRUE(root->GetAsDictionary(&dict_val));
463 EXPECT_TRUE(dict_val->GetString("path", &str_val));
464 EXPECT_EQ("/tmp/\xC3\xA0\xC3\xA8\xC3\xB2.png", str_val);
465
459 // Test invalid utf8 encoded input 466 // Test invalid utf8 encoded input
460 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", 467 root.reset(JSONReader().ReadToValue("\"345\xb0\xa1\xb0\xa2\""));
461 false, false));
462 EXPECT_FALSE(root.get()); 468 EXPECT_FALSE(root.get());
463 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", 469 root.reset(JSONReader().ReadToValue("\"123\xc0\x81\""));
464 false, false)); 470 EXPECT_FALSE(root.get());
471 root.reset(JSONReader().ReadToValue("\"abc\xc0\xae\""));
465 EXPECT_FALSE(root.get()); 472 EXPECT_FALSE(root.get());
466 473
467 // Test utf16 encoded strings. 474 // Test utf16 encoded strings.
468 root.reset(JSONReader().JsonToValue("\"\\u20ac3,14\"", false, false)); 475 root.reset(JSONReader().ReadToValue("\"\\u20ac3,14\""));
469 ASSERT_TRUE(root.get()); 476 ASSERT_TRUE(root.get());
470 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 477 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
471 str_val.clear(); 478 str_val.clear();
472 EXPECT_TRUE(root->GetAsString(&str_val)); 479 EXPECT_TRUE(root->GetAsString(&str_val));
473 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); 480 EXPECT_EQ("\xe2\x82\xac""3,14", str_val);
474 481
475 root.reset(JSONReader().JsonToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"", 482 root.reset(JSONReader().ReadToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\""));
476 false, false));
477 ASSERT_TRUE(root.get()); 483 ASSERT_TRUE(root.get());
478 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 484 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
479 str_val.clear(); 485 str_val.clear();
480 EXPECT_TRUE(root->GetAsString(&str_val)); 486 EXPECT_TRUE(root->GetAsString(&str_val));
481 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); 487 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val);
482 488
483 // Test invalid utf16 strings. 489 // Test invalid utf16 strings.
484 const char* cases[] = { 490 const char* cases[] = {
485 "\"\\u123\"", // Invalid scalar. 491 "\"\\u123\"", // Invalid scalar.
486 "\"\\ud83d\"", // Invalid scalar. 492 "\"\\ud83d\"", // Invalid scalar.
487 "\"\\u$%@!\"", // Invalid scalar. 493 "\"\\u$%@!\"", // Invalid scalar.
488 "\"\\uzz89\"", // Invalid scalar. 494 "\"\\uzz89\"", // Invalid scalar.
489 "\"\\ud83d\\udca\"", // Invalid lower surrogate. 495 "\"\\ud83d\\udca\"", // Invalid lower surrogate.
490 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. 496 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate.
491 "\"\\ud83foo\"", // No lower surrogate. 497 "\"\\ud83foo\"", // No lower surrogate.
492 "\"\\ud83\\foo\"" // No lower surrogate. 498 "\"\\ud83\\foo\"" // No lower surrogate.
493 }; 499 };
494 for (size_t i = 0; i < arraysize(cases); ++i) { 500 for (size_t i = 0; i < arraysize(cases); ++i) {
495 root.reset(JSONReader().JsonToValue(cases[i], false, false)); 501 root.reset(JSONReader().ReadToValue(cases[i]));
496 EXPECT_FALSE(root.get()) << cases[i]; 502 EXPECT_FALSE(root.get()) << cases[i];
497 } 503 }
504
505 // Test literal root objects.
506 root.reset(JSONReader::Read("null"));
507 EXPECT_TRUE(root->IsType(Value::TYPE_NULL));
508
509 root.reset(JSONReader::Read("true"));
510 ASSERT_TRUE(root.get());
511 EXPECT_TRUE(root->GetAsBoolean(&bool_value));
512 EXPECT_TRUE(bool_value);
513
514 root.reset(JSONReader::Read("10"));
515 ASSERT_TRUE(root.get());
516 EXPECT_TRUE(root->GetAsInteger(&integer_value));
517 EXPECT_EQ(10, integer_value);
518
519 root.reset(JSONReader::Read("\"root\""));
520 ASSERT_TRUE(root.get());
521 EXPECT_TRUE(root->GetAsString(&str_val));
522 EXPECT_EQ("root", str_val);
498 } 523 }
499 524
500 TEST(JSONReaderTest, ReadFromFile) { 525 TEST(JSONReaderTest, ReadFromFile) {
501 FilePath path; 526 FilePath path;
502 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); 527 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
503 path = path.Append(FILE_PATH_LITERAL("base")) 528 path = path.Append(FILE_PATH_LITERAL("base"))
504 .Append(FILE_PATH_LITERAL("data")) 529 .Append(FILE_PATH_LITERAL("data"))
505 .Append(FILE_PATH_LITERAL("json")); 530 .Append(FILE_PATH_LITERAL("json"));
506 531
507 std::string input; 532 std::string input;
508 ASSERT_TRUE(file_util::ReadFileToString( 533 ASSERT_TRUE(file_util::ReadFileToString(
509 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input)); 534 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input));
510 535
511 JSONReader reader; 536 JSONReader reader;
512 std::string error_msg; 537 scoped_ptr<Value> root(reader.ReadToValue(input));
513 scoped_ptr<Value> root(
514 JSONReader::ReadAndReturnError(input, JSON_PARSE_RFC, NULL, &error_msg));
515 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); 538 ASSERT_TRUE(root.get()) << reader.GetErrorMessage();
516 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 539 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
517 } 540 }
518 541
519 TEST(JSONReaderTest, ErrorMessages) { 542 // Tests that the root of a JSON object can be deleted safely while its
520 // Error strings should not be modified in case of success. 543 // children outlive it.
521 std::string error_message; 544 TEST(JSONReaderTest, StringOptimizations) {
522 int error_code = 0; 545 Value* dict_literals[2] = {0};
523 scoped_ptr<Value> root; 546 Value* dict_strings[2] = {0};
524 root.reset(JSONReader::ReadAndReturnError("[42]", JSON_PARSE_RFC, 547 Value* list_values[2] = {0};
525 &error_code, &error_message));
526 EXPECT_TRUE(error_message.empty());
527 EXPECT_EQ(0, error_code);
528 548
529 // Test line and column counting 549 {
530 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; 550 scoped_ptr<Value> root(JSONReader::Read(
531 // error here --------------------------------^ 551 "{"
532 root.reset(JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC, 552 " \"test\": {"
533 &error_code, &error_message)); 553 " \"foo\": true,"
534 EXPECT_FALSE(root.get()); 554 " \"bar\": 3.14,"
535 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError), 555 " \"baz\": \"bat\","
536 error_message); 556 " \"moo\": \"cow\""
537 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 557 " },"
558 " \"list\": ["
559 " \"a\","
560 " \"b\""
561 " ]"
562 "}", JSON_DETACHABLE_CHILDREN));
563 ASSERT_TRUE(root.get());
538 564
539 // Test each of the error conditions 565 DictionaryValue* root_dict = NULL;
540 root.reset(JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC, 566 ASSERT_TRUE(root->GetAsDictionary(&root_dict));
541 &error_code, &error_message));
542 EXPECT_FALSE(root.get());
543 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
544 JSONReader::kUnexpectedDataAfterRoot), error_message);
545 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
546 567
547 std::string nested_json; 568 DictionaryValue* dict = NULL;
548 for (int i = 0; i < 101; ++i) { 569 ListValue* list = NULL;
549 nested_json.insert(nested_json.begin(), '['); 570
550 nested_json.append(1, ']'); 571 ASSERT_TRUE(root_dict->GetDictionary("test", &dict));
572 ASSERT_TRUE(root_dict->GetList("list", &list));
573
574 EXPECT_TRUE(dict->Remove("foo", &dict_literals[0]));
575 EXPECT_TRUE(dict->Remove("bar", &dict_literals[1]));
576 EXPECT_TRUE(dict->Remove("baz", &dict_strings[0]));
577 EXPECT_TRUE(dict->Remove("moo", &dict_strings[1]));
578
579 ASSERT_EQ(2u, list->GetSize());
580 EXPECT_TRUE(list->Remove(0, &list_values[0]));
581 EXPECT_TRUE(list->Remove(0, &list_values[1]));
551 } 582 }
552 root.reset(JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
553 &error_code, &error_message));
554 EXPECT_FALSE(root.get());
555 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
556 error_message);
557 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
558 583
559 root.reset(JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC, 584 bool b = false;
560 &error_code, &error_message)); 585 double d = 0;
561 EXPECT_FALSE(root.get()); 586 std::string s;
562 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
563 error_message);
564 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
565 587
566 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC, 588 EXPECT_TRUE(dict_literals[0]->GetAsBoolean(&b));
567 &error_code, &error_message)); 589 EXPECT_TRUE(b);
568 EXPECT_FALSE(root.get());
569 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
570 JSONReader::kUnquotedDictionaryKey), error_message);
571 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
572 590
573 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", 591 EXPECT_TRUE(dict_literals[1]->GetAsDouble(&d));
574 JSON_PARSE_RFC, 592 EXPECT_EQ(3.14, d);
575 &error_code,
576 &error_message));
577 EXPECT_FALSE(root.get());
578 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
579 error_message);
580 593
581 root.reset(JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC, 594 EXPECT_TRUE(dict_strings[0]->GetAsString(&s));
582 &error_code, &error_message)); 595 EXPECT_EQ("bat", s);
583 EXPECT_FALSE(root.get());
584 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
585 error_message);
586 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
587 596
588 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC, 597 EXPECT_TRUE(dict_strings[1]->GetAsString(&s));
589 &error_code, &error_message)); 598 EXPECT_EQ("cow", s);
590 EXPECT_FALSE(root.get());
591 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
592 error_message);
593 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
594 599
595 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC, 600 EXPECT_TRUE(list_values[0]->GetAsString(&s));
596 &error_code, &error_message)); 601 EXPECT_EQ("a", s);
597 EXPECT_FALSE(root.get()); 602 EXPECT_TRUE(list_values[1]->GetAsString(&s));
598 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 603 EXPECT_EQ("b", s);
599 error_message);
600 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
601 604
602 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC, 605 delete dict_literals[0];
603 &error_code, &error_message)); 606 delete dict_literals[1];
604 EXPECT_FALSE(root.get()); 607 delete dict_strings[0];
605 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 608 delete dict_strings[1];
606 error_message); 609 delete list_values[0];
607 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 610 delete list_values[1];
611 }
612
613 // A smattering of invalid JSON designed to test specific portions of the
614 // parser implementation against buffer overflow. Best run with DCHECKs so
615 // that the one in NextChar fires.
616 TEST(JSONReaderTest, InvalidSanity) {
617 const char* invalid_json[] = {
618 "/* test *",
619 "{\"foo\"",
620 "{\"foo\":",
621 " [",
622 "\"\\u123g\"",
623 "{\n\"eh:\n}",
624 };
625
626 for (size_t i = 0; i < arraysize(invalid_json); ++i) {
627 JSONReader reader;
628 LOG(INFO) << "Sanity test " << i << ": <" << invalid_json[i] << ">";
629 EXPECT_FALSE(reader.ReadToValue(invalid_json[i]));
630 EXPECT_NE(JSONReader::JSON_NO_ERROR, reader.error_code());
631 EXPECT_NE("", reader.GetErrorMessage());
632 }
608 } 633 }
609 634
610 } // namespace base 635 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698