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

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

Issue 2475583002: Adds option for JSON reader to allow invalid utf characters (Closed)
Patch Set: comment Created 4 years, 1 month 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_parser.cc ('k') | base/json/json_reader.h » ('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_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
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 replaced.
333 TEST_F(JSONParserTest, ReplaceInvalidCharacters) {
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_REPLACE_INVALID_CHARACTERS));
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(kUnicodeReplacementString, str);
343 }
344
331 } // namespace internal 345 } // namespace internal
332 } // namespace base 346 } // namespace base
OLDNEW
« no previous file with comments | « base/json/json_parser.cc ('k') | base/json/json_reader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698