OLD | NEW |
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 "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/string_piece.h" | 9 #include "base/string_piece.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); | 450 EXPECT_EQ(L"\x7f51\x9875", UTF8ToWide(str_val)); |
451 | 451 |
452 // Test invalid utf8 encoded input | 452 // Test invalid utf8 encoded input |
453 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", | 453 root.reset(JSONReader().JsonToValue("\"345\xb0\xa1\xb0\xa2\"", |
454 false, false)); | 454 false, false)); |
455 EXPECT_FALSE(root.get()); | 455 EXPECT_FALSE(root.get()); |
456 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", | 456 root.reset(JSONReader().JsonToValue("\"123\xc0\x81\"", |
457 false, false)); | 457 false, false)); |
458 EXPECT_FALSE(root.get()); | 458 EXPECT_FALSE(root.get()); |
459 | 459 |
| 460 // Test utf16 encoded strings. |
| 461 root.reset(JSONReader().JsonToValue("\"\\u20ac3,14\"", false, false)); |
| 462 ASSERT_TRUE(root.get()); |
| 463 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 464 str_val.clear(); |
| 465 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 466 EXPECT_EQ("\xe2\x82\xac""3,14", str_val); |
| 467 |
| 468 root.reset(JSONReader().JsonToValue("\"\\ud83d\\udca9\\ud83d\\udc6c\"", |
| 469 false, false)); |
| 470 ASSERT_TRUE(root.get()); |
| 471 EXPECT_TRUE(root->IsType(Value::TYPE_STRING)); |
| 472 str_val.clear(); |
| 473 EXPECT_TRUE(root->GetAsString(&str_val)); |
| 474 EXPECT_EQ("\xf0\x9f\x92\xa9\xf0\x9f\x91\xac", str_val); |
| 475 |
| 476 // Test invalid utf16 strings. |
| 477 const char* cases[] = { |
| 478 "\"\\u123\"", // Invalid scalar. |
| 479 "\"\\ud83d\"", // Invalid scalar. |
| 480 "\"\\u$%@!\"", // Invalid scalar. |
| 481 "\"\\uzz89\"", // Invalid scalar. |
| 482 "\"\\ud83d\\udca\"", // Invalid lower surrogate. |
| 483 "\"\\ud83d\\ud83d\"", // Invalid lower surrogate. |
| 484 "\"\\ud83foo\"", // No lower surrogate. |
| 485 "\"\\ud83\\foo\"" // No lower surrogate. |
| 486 }; |
| 487 for (size_t i = 0; i < arraysize(cases); ++i) { |
| 488 root.reset(JSONReader().JsonToValue(cases[i], false, false)); |
| 489 EXPECT_FALSE(root.get()) << cases[i]; |
| 490 } |
| 491 |
460 // Test invalid root objects. | 492 // Test invalid root objects. |
461 root.reset(JSONReader::Read("null", false)); | 493 root.reset(JSONReader::Read("null", false)); |
462 EXPECT_FALSE(root.get()); | 494 EXPECT_FALSE(root.get()); |
463 root.reset(JSONReader::Read("true", false)); | 495 root.reset(JSONReader::Read("true", false)); |
464 EXPECT_FALSE(root.get()); | 496 EXPECT_FALSE(root.get()); |
465 root.reset(JSONReader::Read("10", false)); | 497 root.reset(JSONReader::Read("10", false)); |
466 EXPECT_FALSE(root.get()); | 498 EXPECT_FALSE(root.get()); |
467 root.reset(JSONReader::Read("\"root\"", false)); | 499 root.reset(JSONReader::Read("\"root\"", false)); |
468 EXPECT_FALSE(root.get()); | 500 EXPECT_FALSE(root.get()); |
469 } | 501 } |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 | 590 |
559 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, | 591 root.reset(JSONReader::ReadAndReturnError("[\"xxx\\q\"]", false, |
560 &error_code, &error_message)); | 592 &error_code, &error_message)); |
561 EXPECT_FALSE(root.get()); | 593 EXPECT_FALSE(root.get()); |
562 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), | 594 EXPECT_EQ(JSONReader::FormatErrorMessage(1, 7, JSONReader::kInvalidEscape), |
563 error_message); | 595 error_message); |
564 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); | 596 EXPECT_EQ(JSONReader::JSON_INVALID_ESCAPE, error_code); |
565 } | 597 } |
566 | 598 |
567 } // namespace base | 599 } // namespace base |
OLD | NEW |