| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/safe_json/json_sanitizer.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 5 #include "base/bind.h" | 9 #include "base/bind.h" |
| 6 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 7 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 11 #include "base/values.h" | 14 #include "base/values.h" |
| 12 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 13 #include "components/safe_json/json_sanitizer.h" | |
| 14 #include "components/safe_json/safe_json_parser.h" | 16 #include "components/safe_json/safe_json_parser.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 18 |
| 17 #if !defined(OS_ANDROID) | 19 #if !defined(OS_ANDROID) |
| 18 #include "components/safe_json/testing_json_parser.h" | 20 #include "components/safe_json/testing_json_parser.h" |
| 19 #endif | 21 #endif |
| 20 | 22 |
| 21 namespace safe_json { | 23 namespace safe_json { |
| 22 | 24 |
| 23 class JsonSanitizerTest : public ::testing::Test { | 25 class JsonSanitizerTest : public ::testing::Test { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 47 base::MessageLoop message_loop_; | 49 base::MessageLoop message_loop_; |
| 48 | 50 |
| 49 #if !defined(OS_ANDROID) | 51 #if !defined(OS_ANDROID) |
| 50 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; | 52 safe_json::TestingJsonParser::ScopedFactoryOverride factory_override_; |
| 51 #endif | 53 #endif |
| 52 | 54 |
| 53 std::string result_; | 55 std::string result_; |
| 54 std::string error_; | 56 std::string error_; |
| 55 State state_; | 57 State state_; |
| 56 | 58 |
| 57 scoped_ptr<base::RunLoop> run_loop_; | 59 std::unique_ptr<base::RunLoop> run_loop_; |
| 58 }; | 60 }; |
| 59 | 61 |
| 60 void JsonSanitizerTest::CheckSuccess(const std::string& json) { | 62 void JsonSanitizerTest::CheckSuccess(const std::string& json) { |
| 61 SCOPED_TRACE(json); | 63 SCOPED_TRACE(json); |
| 62 Sanitize(json); | 64 Sanitize(json); |
| 63 scoped_ptr<base::Value> parsed = base::JSONReader::Read(json); | 65 std::unique_ptr<base::Value> parsed = base::JSONReader::Read(json); |
| 64 ASSERT_TRUE(parsed); | 66 ASSERT_TRUE(parsed); |
| 65 EXPECT_EQ(State::STATE_SUCCESS, state_) << "Error: " << error_; | 67 EXPECT_EQ(State::STATE_SUCCESS, state_) << "Error: " << error_; |
| 66 | 68 |
| 67 // The JSON parser should accept the result. | 69 // The JSON parser should accept the result. |
| 68 int error_code; | 70 int error_code; |
| 69 std::string error; | 71 std::string error; |
| 70 scoped_ptr<base::Value> reparsed = base::JSONReader::ReadAndReturnError( | 72 std::unique_ptr<base::Value> reparsed = base::JSONReader::ReadAndReturnError( |
| 71 result_, base::JSON_PARSE_RFC, &error_code, &error); | 73 result_, base::JSON_PARSE_RFC, &error_code, &error); |
| 72 EXPECT_TRUE(reparsed) | 74 EXPECT_TRUE(reparsed) |
| 73 << "Invalid result: " << error; | 75 << "Invalid result: " << error; |
| 74 | 76 |
| 75 // The parsed values should be equal. | 77 // The parsed values should be equal. |
| 76 EXPECT_TRUE(reparsed->Equals(parsed.get())); | 78 EXPECT_TRUE(reparsed->Equals(parsed.get())); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void JsonSanitizerTest::CheckError(const std::string& json) { | 81 void JsonSanitizerTest::CheckError(const std::string& json) { |
| 80 SCOPED_TRACE(json); | 82 SCOPED_TRACE(json); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // A low surrogate followed by a high surrogate. | 178 // A low surrogate followed by a high surrogate. |
| 177 CheckError("[\"\\ude03\\ud83d\"]"); | 179 CheckError("[\"\\ude03\\ud83d\"]"); |
| 178 | 180 |
| 179 // Valid escaped UTF-16 that encodes non-characters: | 181 // Valid escaped UTF-16 that encodes non-characters: |
| 180 CheckError("[\"\\ufdd0\"]"); | 182 CheckError("[\"\\ufdd0\"]"); |
| 181 CheckError("[\"\\ufffe\"]"); | 183 CheckError("[\"\\ufffe\"]"); |
| 182 CheckError("[\"\\ud83f\\udffe\"]"); | 184 CheckError("[\"\\ud83f\\udffe\"]"); |
| 183 } | 185 } |
| 184 | 186 |
| 185 } // namespace safe_json | 187 } // namespace safe_json |
| OLD | NEW |