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

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

Powered by Google App Engine
This is Rietveld 408576698