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

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

Issue 9960077: Modify the base::JSONReader interface to take a set of options rather than a boolean flag. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments 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"
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 false)); 217 false));
218 EXPECT_FALSE(root.get()); 218 EXPECT_FALSE(root.get());
219 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"", 219 root.reset(JSONReader().JsonToValue("\"not enough escape chars\\u123\"",
220 false, false)); 220 false, false));
221 EXPECT_FALSE(root.get()); 221 EXPECT_FALSE(root.get());
222 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"", 222 root.reset(JSONReader().JsonToValue("\"extra backslash at end of input\\\"",
223 false, false)); 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]", false)); 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 ListValue* 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, ]", true)); 235 root2.reset(JSONReader::Read("[true, false, null, ]",
236 JSON_ALLOW_TRAILING_COMMAS));
236 EXPECT_TRUE(root->Equals(root2.get())); 237 EXPECT_TRUE(root->Equals(root2.get()));
237 238
238 // Empty array 239 // Empty array
239 root.reset(JSONReader::Read("[]", false)); 240 root.reset(JSONReader::Read("[]"));
240 ASSERT_TRUE(root.get()); 241 ASSERT_TRUE(root.get());
241 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 242 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
242 list = static_cast<ListValue*>(root.get()); 243 list = static_cast<ListValue*>(root.get());
243 EXPECT_EQ(0U, list->GetSize()); 244 EXPECT_EQ(0U, list->GetSize());
244 245
245 // Nested arrays 246 // Nested arrays
246 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]", 247 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null]"));
247 false));
248 ASSERT_TRUE(root.get()); 248 ASSERT_TRUE(root.get());
249 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 249 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
250 list = static_cast<ListValue*>(root.get()); 250 list = static_cast<ListValue*>(root.get());
251 EXPECT_EQ(4U, list->GetSize()); 251 EXPECT_EQ(4U, list->GetSize());
252 252
253 // Lots of trailing commas. 253 // Lots of trailing commas.
254 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]", 254 root2.reset(JSONReader::Read("[[true], [], [false, [], [null, ] , ], null,]",
255 true)); 255 JSON_ALLOW_TRAILING_COMMAS));
256 EXPECT_TRUE(root->Equals(root2.get())); 256 EXPECT_TRUE(root->Equals(root2.get()));
257 257
258 // Invalid, missing close brace. 258 // Invalid, missing close brace.
259 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null", false)); 259 root.reset(JSONReader::Read("[[true], [], [false, [], [null]], null"));
260 EXPECT_FALSE(root.get()); 260 EXPECT_FALSE(root.get());
261 261
262 // Invalid, too many commas 262 // Invalid, too many commas
263 root.reset(JSONReader::Read("[true,, null]", false)); 263 root.reset(JSONReader::Read("[true,, null]"));
264 EXPECT_FALSE(root.get()); 264 EXPECT_FALSE(root.get());
265 root.reset(JSONReader::Read("[true,, null]", true)); 265 root.reset(JSONReader::Read("[true,, null]", JSON_ALLOW_TRAILING_COMMAS));
266 EXPECT_FALSE(root.get()); 266 EXPECT_FALSE(root.get());
267 267
268 // Invalid, no commas 268 // Invalid, no commas
269 root.reset(JSONReader::Read("[true null]", false)); 269 root.reset(JSONReader::Read("[true null]"));
270 EXPECT_FALSE(root.get()); 270 EXPECT_FALSE(root.get());
271 271
272 // Invalid, trailing comma 272 // Invalid, trailing comma
273 root.reset(JSONReader::Read("[true,]", false)); 273 root.reset(JSONReader::Read("[true,]"));
274 EXPECT_FALSE(root.get()); 274 EXPECT_FALSE(root.get());
275 275
276 // Valid if we set |allow_trailing_comma| to true. 276 // Valid if we set |allow_trailing_comma| to true.
277 root.reset(JSONReader::Read("[true,]", true)); 277 root.reset(JSONReader::Read("[true,]", JSON_ALLOW_TRAILING_COMMAS));
278 ASSERT_TRUE(root.get()); 278 ASSERT_TRUE(root.get());
279 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 279 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
280 list = static_cast<ListValue*>(root.get()); 280 list = static_cast<ListValue*>(root.get());
281 EXPECT_EQ(1U, list->GetSize()); 281 EXPECT_EQ(1U, list->GetSize());
282 Value* tmp_value = NULL; 282 Value* tmp_value = NULL;
283 ASSERT_TRUE(list->Get(0, &tmp_value)); 283 ASSERT_TRUE(list->Get(0, &tmp_value));
284 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN)); 284 EXPECT_TRUE(tmp_value->IsType(Value::TYPE_BOOLEAN));
285 bool bool_value = false; 285 bool bool_value = false;
286 EXPECT_TRUE(tmp_value->GetAsBoolean(&bool_value)); 286 EXPECT_TRUE(tmp_value->GetAsBoolean(&bool_value));
287 EXPECT_TRUE(bool_value); 287 EXPECT_TRUE(bool_value);
288 288
289 // Don't allow empty elements, even if |allow_trailing_comma| is 289 // Don't allow empty elements, even if |allow_trailing_comma| is
290 // true. 290 // true.
291 root.reset(JSONReader::Read("[,]", true)); 291 root.reset(JSONReader::Read("[,]", JSON_ALLOW_TRAILING_COMMAS));
292 EXPECT_FALSE(root.get()); 292 EXPECT_FALSE(root.get());
293 root.reset(JSONReader::Read("[true,,]", true)); 293 root.reset(JSONReader::Read("[true,,]", JSON_ALLOW_TRAILING_COMMAS));
294 EXPECT_FALSE(root.get()); 294 EXPECT_FALSE(root.get());
295 root.reset(JSONReader::Read("[,true,]", true)); 295 root.reset(JSONReader::Read("[,true,]", JSON_ALLOW_TRAILING_COMMAS));
296 EXPECT_FALSE(root.get()); 296 EXPECT_FALSE(root.get());
297 root.reset(JSONReader::Read("[true,,false]", true)); 297 root.reset(JSONReader::Read("[true,,false]", JSON_ALLOW_TRAILING_COMMAS));
298 EXPECT_FALSE(root.get()); 298 EXPECT_FALSE(root.get());
299 299
300 // Test objects 300 // Test objects
301 root.reset(JSONReader::Read("{}", false)); 301 root.reset(JSONReader::Read("{}"));
302 ASSERT_TRUE(root.get()); 302 ASSERT_TRUE(root.get());
303 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 303 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
304 304
305 root.reset(JSONReader::Read( 305 root.reset(JSONReader::Read(
306 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }", false)); 306 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\" }"));
307 ASSERT_TRUE(root.get()); 307 ASSERT_TRUE(root.get());
308 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 308 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
309 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get()); 309 DictionaryValue* dict_val = static_cast<DictionaryValue*>(root.get());
310 double_val = 0.0; 310 double_val = 0.0;
311 EXPECT_TRUE(dict_val->GetDouble("number", &double_val)); 311 EXPECT_TRUE(dict_val->GetDouble("number", &double_val));
312 EXPECT_DOUBLE_EQ(9.87654321, double_val); 312 EXPECT_DOUBLE_EQ(9.87654321, double_val);
313 Value* null_val = NULL; 313 Value* null_val = NULL;
314 ASSERT_TRUE(dict_val->Get("null", &null_val)); 314 ASSERT_TRUE(dict_val->Get("null", &null_val));
315 EXPECT_TRUE(null_val->IsType(Value::TYPE_NULL)); 315 EXPECT_TRUE(null_val->IsType(Value::TYPE_NULL));
316 str_val.clear(); 316 str_val.clear();
317 EXPECT_TRUE(dict_val->GetString("S", &str_val)); 317 EXPECT_TRUE(dict_val->GetString("S", &str_val));
318 EXPECT_EQ("str", str_val); 318 EXPECT_EQ("str", str_val);
319 319
320 root2.reset(JSONReader::Read( 320 root2.reset(JSONReader::Read(
321 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }", true)); 321 "{\"number\":9.87654321, \"null\":null , \"\\x53\" : \"str\", }",
322 JSON_ALLOW_TRAILING_COMMAS));
322 ASSERT_TRUE(root2.get()); 323 ASSERT_TRUE(root2.get());
323 EXPECT_TRUE(root->Equals(root2.get())); 324 EXPECT_TRUE(root->Equals(root2.get()));
324 325
325 // Test newline equivalence. 326 // Test newline equivalence.
326 root2.reset(JSONReader::Read( 327 root2.reset(JSONReader::Read(
327 "{\n" 328 "{\n"
328 " \"number\":9.87654321,\n" 329 " \"number\":9.87654321,\n"
329 " \"null\":null,\n" 330 " \"null\":null,\n"
330 " \"\\x53\":\"str\",\n" 331 " \"\\x53\":\"str\",\n"
331 "}\n", true)); 332 "}\n", JSON_ALLOW_TRAILING_COMMAS));
332 ASSERT_TRUE(root2.get()); 333 ASSERT_TRUE(root2.get());
333 EXPECT_TRUE(root->Equals(root2.get())); 334 EXPECT_TRUE(root->Equals(root2.get()));
334 335
335 root2.reset(JSONReader::Read( 336 root2.reset(JSONReader::Read(
336 "{\r\n" 337 "{\r\n"
337 " \"number\":9.87654321,\r\n" 338 " \"number\":9.87654321,\r\n"
338 " \"null\":null,\r\n" 339 " \"null\":null,\r\n"
339 " \"\\x53\":\"str\",\r\n" 340 " \"\\x53\":\"str\",\r\n"
340 "}\r\n", true)); 341 "}\r\n", JSON_ALLOW_TRAILING_COMMAS));
341 ASSERT_TRUE(root2.get()); 342 ASSERT_TRUE(root2.get());
342 EXPECT_TRUE(root->Equals(root2.get())); 343 EXPECT_TRUE(root->Equals(root2.get()));
343 344
344 // Test nesting 345 // Test nesting
345 root.reset(JSONReader::Read( 346 root.reset(JSONReader::Read(
346 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}", false)); 347 "{\"inner\":{\"array\":[true]},\"false\":false,\"d\":{}}"));
347 ASSERT_TRUE(root.get()); 348 ASSERT_TRUE(root.get());
348 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 349 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
349 dict_val = static_cast<DictionaryValue*>(root.get()); 350 dict_val = static_cast<DictionaryValue*>(root.get());
350 DictionaryValue* inner_dict = NULL; 351 DictionaryValue* inner_dict = NULL;
351 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict)); 352 ASSERT_TRUE(dict_val->GetDictionary("inner", &inner_dict));
352 ListValue* inner_array = NULL; 353 ListValue* inner_array = NULL;
353 ASSERT_TRUE(inner_dict->GetList("array", &inner_array)); 354 ASSERT_TRUE(inner_dict->GetList("array", &inner_array));
354 EXPECT_EQ(1U, inner_array->GetSize()); 355 EXPECT_EQ(1U, inner_array->GetSize());
355 bool_value = true; 356 bool_value = true;
356 EXPECT_TRUE(dict_val->GetBoolean("false", &bool_value)); 357 EXPECT_TRUE(dict_val->GetBoolean("false", &bool_value));
357 EXPECT_FALSE(bool_value); 358 EXPECT_FALSE(bool_value);
358 inner_dict = NULL; 359 inner_dict = NULL;
359 EXPECT_TRUE(dict_val->GetDictionary("d", &inner_dict)); 360 EXPECT_TRUE(dict_val->GetDictionary("d", &inner_dict));
360 361
361 root2.reset(JSONReader::Read( 362 root2.reset(JSONReader::Read(
362 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}", true)); 363 "{\"inner\": {\"array\":[true] , },\"false\":false,\"d\":{},}",
364 JSON_ALLOW_TRAILING_COMMAS));
363 EXPECT_TRUE(root->Equals(root2.get())); 365 EXPECT_TRUE(root->Equals(root2.get()));
364 366
365 // Test keys with periods 367 // Test keys with periods
366 root.reset(JSONReader::Read( 368 root.reset(JSONReader::Read(
367 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}", false)); 369 "{\"a.b\":3,\"c\":2,\"d.e.f\":{\"g.h.i.j\":1}}"));
368 ASSERT_TRUE(root.get()); 370 ASSERT_TRUE(root.get());
369 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 371 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
370 dict_val = static_cast<DictionaryValue*>(root.get()); 372 dict_val = static_cast<DictionaryValue*>(root.get());
371 int integer_value = 0; 373 int integer_value = 0;
372 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); 374 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
373 EXPECT_EQ(3, integer_value); 375 EXPECT_EQ(3, integer_value);
374 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value)); 376 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("c", &integer_value));
375 EXPECT_EQ(2, integer_value); 377 EXPECT_EQ(2, integer_value);
376 inner_dict = NULL; 378 inner_dict = NULL;
377 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f", 379 ASSERT_TRUE(dict_val->GetDictionaryWithoutPathExpansion("d.e.f",
378 &inner_dict)); 380 &inner_dict));
379 EXPECT_EQ(1U, inner_dict->size()); 381 EXPECT_EQ(1U, inner_dict->size());
380 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j", 382 EXPECT_TRUE(inner_dict->GetIntegerWithoutPathExpansion("g.h.i.j",
381 &integer_value)); 383 &integer_value));
382 EXPECT_EQ(1, integer_value); 384 EXPECT_EQ(1, integer_value);
383 385
384 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}", false)); 386 root.reset(JSONReader::Read("{\"a\":{\"b\":2},\"a.b\":1}"));
385 ASSERT_TRUE(root.get()); 387 ASSERT_TRUE(root.get());
386 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 388 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
387 dict_val = static_cast<DictionaryValue*>(root.get()); 389 dict_val = static_cast<DictionaryValue*>(root.get());
388 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value)); 390 EXPECT_TRUE(dict_val->GetInteger("a.b", &integer_value));
389 EXPECT_EQ(2, integer_value); 391 EXPECT_EQ(2, integer_value);
390 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value)); 392 EXPECT_TRUE(dict_val->GetIntegerWithoutPathExpansion("a.b", &integer_value));
391 EXPECT_EQ(1, integer_value); 393 EXPECT_EQ(1, integer_value);
392 394
393 // Invalid, no closing brace 395 // Invalid, no closing brace
394 root.reset(JSONReader::Read("{\"a\": true", false)); 396 root.reset(JSONReader::Read("{\"a\": true"));
395 EXPECT_FALSE(root.get()); 397 EXPECT_FALSE(root.get());
396 398
397 // Invalid, keys must be quoted 399 // Invalid, keys must be quoted
398 root.reset(JSONReader::Read("{foo:true}", false)); 400 root.reset(JSONReader::Read("{foo:true}"));
399 EXPECT_FALSE(root.get()); 401 EXPECT_FALSE(root.get());
400 402
401 // Invalid, trailing comma 403 // Invalid, trailing comma
402 root.reset(JSONReader::Read("{\"a\":true,}", false)); 404 root.reset(JSONReader::Read("{\"a\":true,}"));
403 EXPECT_FALSE(root.get()); 405 EXPECT_FALSE(root.get());
404 406
405 // Invalid, too many commas 407 // Invalid, too many commas
406 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", false)); 408 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}"));
407 EXPECT_FALSE(root.get()); 409 EXPECT_FALSE(root.get());
408 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); 410 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}",
411 JSON_ALLOW_TRAILING_COMMAS));
409 EXPECT_FALSE(root.get()); 412 EXPECT_FALSE(root.get());
410 413
411 // Invalid, no separator 414 // Invalid, no separator
412 root.reset(JSONReader::Read("{\"a\" \"b\"}", false)); 415 root.reset(JSONReader::Read("{\"a\" \"b\"}"));
413 EXPECT_FALSE(root.get()); 416 EXPECT_FALSE(root.get());
414 417
415 // Invalid, lone comma. 418 // Invalid, lone comma.
416 root.reset(JSONReader::Read("{,}", false)); 419 root.reset(JSONReader::Read("{,}"));
417 EXPECT_FALSE(root.get()); 420 EXPECT_FALSE(root.get());
418 root.reset(JSONReader::Read("{,}", true)); 421 root.reset(JSONReader::Read("{,}", JSON_ALLOW_TRAILING_COMMAS));
419 EXPECT_FALSE(root.get()); 422 EXPECT_FALSE(root.get());
420 root.reset(JSONReader::Read("{\"a\":true,,}", true)); 423 root.reset(JSONReader::Read("{\"a\":true,,}", JSON_ALLOW_TRAILING_COMMAS));
421 EXPECT_FALSE(root.get()); 424 EXPECT_FALSE(root.get());
422 root.reset(JSONReader::Read("{,\"a\":true}", true)); 425 root.reset(JSONReader::Read("{,\"a\":true}", JSON_ALLOW_TRAILING_COMMAS));
423 EXPECT_FALSE(root.get()); 426 EXPECT_FALSE(root.get());
424 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}", true)); 427 root.reset(JSONReader::Read("{\"a\":true,,\"b\":false}",
428 JSON_ALLOW_TRAILING_COMMAS));
425 EXPECT_FALSE(root.get()); 429 EXPECT_FALSE(root.get());
426 430
427 // Test stack overflow 431 // Test stack overflow
428 std::string evil(1000000, '['); 432 std::string evil(1000000, '[');
429 evil.append(std::string(1000000, ']')); 433 evil.append(std::string(1000000, ']'));
430 root.reset(JSONReader::Read(evil, false)); 434 root.reset(JSONReader::Read(evil));
431 EXPECT_FALSE(root.get()); 435 EXPECT_FALSE(root.get());
432 436
433 // A few thousand adjacent lists is fine. 437 // A few thousand adjacent lists is fine.
434 std::string not_evil("["); 438 std::string not_evil("[");
435 not_evil.reserve(15010); 439 not_evil.reserve(15010);
436 for (int i = 0; i < 5000; ++i) { 440 for (int i = 0; i < 5000; ++i) {
437 not_evil.append("[],"); 441 not_evil.append("[],");
438 } 442 }
439 not_evil.append("[]]"); 443 not_evil.append("[]]");
440 root.reset(JSONReader::Read(not_evil, false)); 444 root.reset(JSONReader::Read(not_evil));
441 ASSERT_TRUE(root.get()); 445 ASSERT_TRUE(root.get());
442 EXPECT_TRUE(root->IsType(Value::TYPE_LIST)); 446 EXPECT_TRUE(root->IsType(Value::TYPE_LIST));
443 list = static_cast<ListValue*>(root.get()); 447 list = static_cast<ListValue*>(root.get());
444 EXPECT_EQ(5001U, list->GetSize()); 448 EXPECT_EQ(5001U, list->GetSize());
445 449
446 // Test utf8 encoded input 450 // Test utf8 encoded input
447 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"", 451 root.reset(JSONReader().JsonToValue("\"\xe7\xbd\x91\xe9\xa1\xb5\"",
448 false, false)); 452 false, false));
449 ASSERT_TRUE(root.get()); 453 ASSERT_TRUE(root.get());
450 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); 454 EXPECT_TRUE(root->IsType(Value::TYPE_STRING));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 "\"\\uzz89\"", // Invalid scalar. 488 "\"\\uzz89\"", // Invalid scalar.
485 "\"\\ud83d\\udca\"", // Invalid lower surrogate. 489 "\"\\ud83d\\udca\"", // Invalid lower surrogate.
486 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. 490 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate.
487 "\"\\ud83foo\"", // No lower surrogate. 491 "\"\\ud83foo\"", // No lower surrogate.
488 "\"\\ud83\\foo\"" // No lower surrogate. 492 "\"\\ud83\\foo\"" // No lower surrogate.
489 }; 493 };
490 for (size_t i = 0; i < arraysize(cases); ++i) { 494 for (size_t i = 0; i < arraysize(cases); ++i) {
491 root.reset(JSONReader().JsonToValue(cases[i], false, false)); 495 root.reset(JSONReader().JsonToValue(cases[i], false, false));
492 EXPECT_FALSE(root.get()) << cases[i]; 496 EXPECT_FALSE(root.get()) << cases[i];
493 } 497 }
494
495 // Test invalid root objects.
496 root.reset(JSONReader::Read("null", false));
497 EXPECT_FALSE(root.get());
498 root.reset(JSONReader::Read("true", false));
499 EXPECT_FALSE(root.get());
500 root.reset(JSONReader::Read("10", false));
501 EXPECT_FALSE(root.get());
502 root.reset(JSONReader::Read("\"root\"", false));
503 EXPECT_FALSE(root.get());
504 } 498 }
505 499
506 TEST(JSONReaderTest, ReadFromFile) { 500 TEST(JSONReaderTest, ReadFromFile) {
507 FilePath path; 501 FilePath path;
508 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path)); 502 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &path));
509 path = path.Append(FILE_PATH_LITERAL("base")) 503 path = path.Append(FILE_PATH_LITERAL("base"))
510 .Append(FILE_PATH_LITERAL("data")) 504 .Append(FILE_PATH_LITERAL("data"))
511 .Append(FILE_PATH_LITERAL("json")); 505 .Append(FILE_PATH_LITERAL("json"));
512 506
513 std::string input; 507 std::string input;
514 ASSERT_TRUE(file_util::ReadFileToString( 508 ASSERT_TRUE(file_util::ReadFileToString(
515 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input)); 509 path.Append(FILE_PATH_LITERAL("bom_feff.json")), &input));
516 510
517 JSONReader reader; 511 JSONReader reader;
518 scoped_ptr<Value> root(reader.JsonToValue(input, false, false)); 512 std::string error_msg;
513 scoped_ptr<Value> root(
514 JSONReader::ReadAndReturnError(input, JSON_PARSE_RFC, NULL, &error_msg));
519 ASSERT_TRUE(root.get()) << reader.GetErrorMessage(); 515 ASSERT_TRUE(root.get()) << reader.GetErrorMessage();
520 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 516 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
521 } 517 }
522 518
523 TEST(JSONReaderTest, ErrorMessages) { 519 TEST(JSONReaderTest, ErrorMessages) {
524 // Error strings should not be modified in case of success. 520 // Error strings should not be modified in case of success.
525 std::string error_message; 521 std::string error_message;
526 int error_code = 0; 522 int error_code = 0;
527 scoped_ptr<Value> root; 523 scoped_ptr<Value> root;
528 root.reset(JSONReader::ReadAndReturnError("[42]", false, 524 root.reset(JSONReader::ReadAndReturnError("[42]", JSON_PARSE_RFC,
529 &error_code, &error_message)); 525 &error_code, &error_message));
530 EXPECT_TRUE(error_message.empty()); 526 EXPECT_TRUE(error_message.empty());
531 EXPECT_EQ(0, error_code); 527 EXPECT_EQ(0, error_code);
532 528
533 // Test line and column counting 529 // Test line and column counting
534 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]"; 530 const char* big_json = "[\n0,\n1,\n2,\n3,4,5,6 7,\n8,\n9\n]";
535 // error here --------------------------------^ 531 // error here --------------------------------^
536 root.reset(JSONReader::ReadAndReturnError(big_json, false, 532 root.reset(JSONReader::ReadAndReturnError(big_json, JSON_PARSE_RFC,
537 &error_code, &error_message)); 533 &error_code, &error_message));
538 EXPECT_FALSE(root.get()); 534 EXPECT_FALSE(root.get());
539 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError), 535 EXPECT_EQ(JSONReader::FormatErrorMessage(5, 9, JSONReader::kSyntaxError),
540 error_message); 536 error_message);
541 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 537 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
542 538
543 // Test each of the error conditions 539 // Test each of the error conditions
544 root.reset(JSONReader::ReadAndReturnError("{},{}", false, 540 root.reset(JSONReader::ReadAndReturnError("{},{}", JSON_PARSE_RFC,
545 &error_code, &error_message)); 541 &error_code, &error_message));
546 EXPECT_FALSE(root.get()); 542 EXPECT_FALSE(root.get());
547 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3, 543 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 3,
548 JSONReader::kUnexpectedDataAfterRoot), error_message); 544 JSONReader::kUnexpectedDataAfterRoot), error_message);
549 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code); 545 EXPECT_EQ(JSONReader::JSON_UNEXPECTED_DATA_AFTER_ROOT, error_code);
550 546
551 std::string nested_json; 547 std::string nested_json;
552 for (int i = 0; i < 101; ++i) { 548 for (int i = 0; i < 101; ++i) {
553 nested_json.insert(nested_json.begin(), '['); 549 nested_json.insert(nested_json.begin(), '[');
554 nested_json.append(1, ']'); 550 nested_json.append(1, ']');
555 } 551 }
556 root.reset(JSONReader::ReadAndReturnError(nested_json, false, 552 root.reset(JSONReader::ReadAndReturnError(nested_json, JSON_PARSE_RFC,
557 &error_code, &error_message)); 553 &error_code, &error_message));
558 EXPECT_FALSE(root.get()); 554 EXPECT_FALSE(root.get());
559 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting), 555 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 101, JSONReader::kTooMuchNesting),
560 error_message); 556 error_message);
561 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code); 557 EXPECT_EQ(JSONReader::JSON_TOO_MUCH_NESTING, error_code);
562 558
563 root.reset(JSONReader::ReadAndReturnError("42", false, 559 root.reset(JSONReader::ReadAndReturnError("[1,]", JSON_PARSE_RFC,
564 &error_code, &error_message));
565 EXPECT_FALSE(root.get());
566 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 1,
567 JSONReader::kBadRootElementType), error_message);
568 EXPECT_EQ(JSONReader::JSON_BAD_ROOT_ELEMENT_TYPE, error_code);
569
570 root.reset(JSONReader::ReadAndReturnError("[1,]", false,
571 &error_code, &error_message)); 560 &error_code, &error_message));
572 EXPECT_FALSE(root.get()); 561 EXPECT_FALSE(root.get());
573 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma), 562 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 4, JSONReader::kTrailingComma),
574 error_message); 563 error_message);
575 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code); 564 EXPECT_EQ(JSONReader::JSON_TRAILING_COMMA, error_code);
576 565
577 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", false, 566 root.reset(JSONReader::ReadAndReturnError("{foo:\"bar\"}", JSON_PARSE_RFC,
578 &error_code, &error_message)); 567 &error_code, &error_message));
579 EXPECT_FALSE(root.get()); 568 EXPECT_FALSE(root.get());
580 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, 569 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2,
581 JSONReader::kUnquotedDictionaryKey), error_message); 570 JSONReader::kUnquotedDictionaryKey), error_message);
582 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code); 571 EXPECT_EQ(JSONReader::JSON_UNQUOTED_DICTIONARY_KEY, error_code);
583 572
584 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}", false, 573 root.reset(JSONReader::ReadAndReturnError("{\"foo\":\"bar\",}",
585 &error_code, &error_message)); 574 JSON_PARSE_RFC,
575 &error_code,
576 &error_message));
586 EXPECT_FALSE(root.get()); 577 EXPECT_FALSE(root.get());
587 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma), 578 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 14, JSONReader::kTrailingComma),
588 error_message); 579 error_message);
589 580
590 root.reset(JSONReader::ReadAndReturnError("[nu]", false, 581 root.reset(JSONReader::ReadAndReturnError("[nu]", JSON_PARSE_RFC,
591 &error_code, &error_message)); 582 &error_code, &error_message));
592 EXPECT_FALSE(root.get()); 583 EXPECT_FALSE(root.get());
593 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError), 584 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 2, JSONReader::kSyntaxError),
594 error_message); 585 error_message);
595 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code); 586 EXPECT_EQ(JSONReader::JSON_SYNTAX_ERROR, error_code);
596 587
597 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", false, 588 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\xq\"]", JSON_PARSE_RFC,
598 &error_code, &error_message)); 589 &error_code, &error_message));
599 EXPECT_FALSE(root.get()); 590 EXPECT_FALSE(root.get());
600 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 591 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
601 error_message); 592 error_message);
602 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 593 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
603 594
604 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", false, 595 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\uq\"]", JSON_PARSE_RFC,
605 &error_code, &error_message)); 596 &error_code, &error_message));
606 EXPECT_FALSE(root.get()); 597 EXPECT_FALSE(root.get());
607 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 598 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
608 error_message); 599 error_message);
609 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 600 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
610 601
611 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, 602 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", JSON_PARSE_RFC,
612 &error_code, &error_message)); 603 &error_code, &error_message));
613 EXPECT_FALSE(root.get()); 604 EXPECT_FALSE(root.get());
614 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), 605 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape),
615 error_message); 606 error_message);
616 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); 607 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code);
617 } 608 }
618 609
619 } // namespace base 610 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698