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_parser.h" | 5 #include "base/json/json_parser.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 class JSONParserTest : public testing::Test { | 18 class JSONParserTest : public testing::Test { |
19 public: | 19 public: |
20 JSONParser* NewTestParser(const std::string& input) { | 20 JSONParser* NewTestParser(const std::string& input, |
21 JSONParser* parser = new JSONParser(JSON_PARSE_RFC); | 21 int options = JSON_PARSE_RFC) { |
| 22 JSONParser* parser = new JSONParser(options); |
22 parser->start_pos_ = input.data(); | 23 parser->start_pos_ = input.data(); |
23 parser->pos_ = parser->start_pos_; | 24 parser->pos_ = parser->start_pos_; |
24 parser->end_pos_ = parser->start_pos_ + input.length(); | 25 parser->end_pos_ = parser->start_pos_ + input.length(); |
25 return parser; | 26 return parser; |
26 } | 27 } |
27 | 28 |
28 void TestLastThree(JSONParser* parser) { | 29 void TestLastThree(JSONParser* parser) { |
29 EXPECT_EQ(',', *parser->NextChar()); | 30 EXPECT_EQ(',', *parser->NextChar()); |
30 EXPECT_EQ('|', *parser->NextChar()); | 31 EXPECT_EQ('|', *parser->NextChar()); |
31 EXPECT_EQ('\0', *parser->NextChar()); | 32 EXPECT_EQ('\0', *parser->NextChar()); |
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]")); | 322 EXPECT_FALSE(JSONReader::Read("[\"\\ufdd0\"]")); |
322 EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]")); | 323 EXPECT_FALSE(JSONReader::Read("[\"\\ufffe\"]")); |
323 EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]")); | 324 EXPECT_FALSE(JSONReader::Read("[\"\\ud83f\\udffe\"]")); |
324 } | 325 } |
325 | 326 |
326 TEST_F(JSONParserTest, DecodeNegativeEscapeSequence) { | 327 TEST_F(JSONParserTest, DecodeNegativeEscapeSequence) { |
327 EXPECT_FALSE(JSONReader::Read("[\"\\x-A\"]")); | 328 EXPECT_FALSE(JSONReader::Read("[\"\\x-A\"]")); |
328 EXPECT_FALSE(JSONReader::Read("[\"\\u-00A\"]")); | 329 EXPECT_FALSE(JSONReader::Read("[\"\\u-00A\"]")); |
329 } | 330 } |
330 | 331 |
| 332 // Verifies invalid utf-8 characters are read back in as is. |
| 333 TEST_F(JSONParserTest, KeepInvalidCharacters) { |
| 334 const std::string bogus_char = ""; |
| 335 const std::string quoted_bogus_char = "\"" + bogus_char + "\""; |
| 336 std::unique_ptr<JSONParser> parser( |
| 337 NewTestParser(quoted_bogus_char, JSON_ALLOW_NON_UTF_STRINGS)); |
| 338 std::unique_ptr<Value> value(parser->ConsumeString()); |
| 339 ASSERT_TRUE(value.get()); |
| 340 std::string str; |
| 341 EXPECT_TRUE(value->GetAsString(&str)); |
| 342 EXPECT_EQ(bogus_char, str); |
| 343 } |
| 344 |
331 } // namespace internal | 345 } // namespace internal |
332 } // namespace base | 346 } // namespace base |
OLD | NEW |