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

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

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

Powered by Google App Engine
This is Rietveld 408576698