| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/subresource_filter/core/common/test_ruleset_creator.h" | 5 #include "components/subresource_filter/core/common/test_ruleset_creator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/threading/thread_restrictions.h" |
| 12 #include "components/subresource_filter/core/common/indexed_ruleset.h" | 14 #include "components/subresource_filter/core/common/indexed_ruleset.h" |
| 13 #include "components/subresource_filter/core/common/proto/rules.pb.h" | 15 #include "components/subresource_filter/core/common/proto/rules.pb.h" |
| 14 #include "components/subresource_filter/core/common/test_ruleset_utils.h" | 16 #include "components/subresource_filter/core/common/test_ruleset_utils.h" |
| 15 #include "components/subresource_filter/core/common/unindexed_ruleset.h" | 17 #include "components/subresource_filter/core/common/unindexed_ruleset.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" | 19 #include "third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite
.h" |
| 18 | 20 |
| 19 namespace subresource_filter { | 21 namespace subresource_filter { |
| 20 | 22 |
| 21 namespace { | 23 namespace { |
| 22 | 24 |
| 23 // The methods below assume that char and uint8_t are interchangeable. | 25 // The methods below assume that char and uint8_t are interchangeable. |
| 24 static_assert(CHAR_BIT == 8, "Assumed char was 8 bits."); | 26 static_assert(CHAR_BIT == 8, "Assumed char was 8 bits."); |
| 25 | 27 |
| 26 void WriteRulesetContents(const std::vector<uint8_t>& contents, | 28 void WriteRulesetContents(const std::vector<uint8_t>& contents, |
| 27 base::FilePath path) { | 29 base::FilePath path) { |
| 30 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 28 int ruleset_size_as_int = base::checked_cast<int>(contents.size()); | 31 int ruleset_size_as_int = base::checked_cast<int>(contents.size()); |
| 29 int num_bytes_written = | 32 int num_bytes_written = |
| 30 base::WriteFile(path, reinterpret_cast<const char*>(contents.data()), | 33 base::WriteFile(path, reinterpret_cast<const char*>(contents.data()), |
| 31 ruleset_size_as_int); | 34 ruleset_size_as_int); |
| 32 ASSERT_EQ(ruleset_size_as_int, num_bytes_written); | 35 ASSERT_EQ(ruleset_size_as_int, num_bytes_written); |
| 33 } | 36 } |
| 34 | 37 |
| 35 std::vector<uint8_t> SerializeUnindexedRulesetWithMultipleRules( | 38 std::vector<uint8_t> SerializeUnindexedRulesetWithMultipleRules( |
| 36 const std::vector<proto::UrlRule>& rules) { | 39 const std::vector<proto::UrlRule>& rules) { |
| 37 std::string ruleset_contents; | 40 std::string ruleset_contents; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 | 61 |
| 59 namespace testing { | 62 namespace testing { |
| 60 | 63 |
| 61 // TestRuleset ----------------------------------------------------------------- | 64 // TestRuleset ----------------------------------------------------------------- |
| 62 | 65 |
| 63 TestRuleset::TestRuleset() = default; | 66 TestRuleset::TestRuleset() = default; |
| 64 TestRuleset::~TestRuleset() = default; | 67 TestRuleset::~TestRuleset() = default; |
| 65 | 68 |
| 66 // static | 69 // static |
| 67 base::File TestRuleset::Open(const TestRuleset& ruleset) { | 70 base::File TestRuleset::Open(const TestRuleset& ruleset) { |
| 71 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 68 base::File file; | 72 base::File file; |
| 69 file.Initialize(ruleset.path, base::File::FLAG_OPEN | base::File::FLAG_READ | | 73 file.Initialize(ruleset.path, base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 70 base::File::FLAG_SHARE_DELETE); | 74 base::File::FLAG_SHARE_DELETE); |
| 71 return file; | 75 return file; |
| 72 } | 76 } |
| 73 | 77 |
| 74 // static | 78 // static |
| 75 void TestRuleset::CorruptByTruncating(const TestRuleset& ruleset, | 79 void TestRuleset::CorruptByTruncating(const TestRuleset& ruleset, |
| 76 size_t tail_size) { | 80 size_t tail_size) { |
| 77 ASSERT_GT(tail_size, 0u); | 81 ASSERT_GT(tail_size, 0u); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 98 WriteRulesetContents(new_contents, ruleset.path); | 102 WriteRulesetContents(new_contents, ruleset.path); |
| 99 } | 103 } |
| 100 | 104 |
| 101 // TestRulesetPair ------------------------------------------------------------- | 105 // TestRulesetPair ------------------------------------------------------------- |
| 102 | 106 |
| 103 TestRulesetPair::TestRulesetPair() = default; | 107 TestRulesetPair::TestRulesetPair() = default; |
| 104 TestRulesetPair::~TestRulesetPair() = default; | 108 TestRulesetPair::~TestRulesetPair() = default; |
| 105 | 109 |
| 106 // TestRulesetCreator ---------------------------------------------------------- | 110 // TestRulesetCreator ---------------------------------------------------------- |
| 107 | 111 |
| 108 TestRulesetCreator::TestRulesetCreator() = default; | 112 TestRulesetCreator::TestRulesetCreator() |
| 109 TestRulesetCreator::~TestRulesetCreator() = default; | 113 : scoped_temp_dir_(base::MakeUnique<base::ScopedTempDir>()) {} |
| 114 |
| 115 TestRulesetCreator::~TestRulesetCreator() { |
| 116 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 117 scoped_temp_dir_.reset(); |
| 118 } |
| 110 | 119 |
| 111 void TestRulesetCreator::CreateRulesetToDisallowURLsWithPathSuffix( | 120 void TestRulesetCreator::CreateRulesetToDisallowURLsWithPathSuffix( |
| 112 base::StringPiece suffix, | 121 base::StringPiece suffix, |
| 113 TestRulesetPair* test_ruleset_pair) { | 122 TestRulesetPair* test_ruleset_pair) { |
| 114 DCHECK(test_ruleset_pair); | 123 DCHECK(test_ruleset_pair); |
| 115 proto::UrlRule suffix_rule = CreateSuffixRule(suffix); | 124 proto::UrlRule suffix_rule = CreateSuffixRule(suffix); |
| 116 CreateRulesetWithRules({suffix_rule}, test_ruleset_pair); | 125 CreateRulesetWithRules({suffix_rule}, test_ruleset_pair); |
| 117 } | 126 } |
| 118 | 127 |
| 119 void TestRulesetCreator::CreateUnindexedRulesetToDisallowURLsWithPathSuffix( | 128 void TestRulesetCreator::CreateUnindexedRulesetToDisallowURLsWithPathSuffix( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 void TestRulesetCreator::CreateUnindexedRulesetWithRules( | 163 void TestRulesetCreator::CreateUnindexedRulesetWithRules( |
| 155 const std::vector<proto::UrlRule>& rules, | 164 const std::vector<proto::UrlRule>& rules, |
| 156 TestRuleset* test_unindexed_ruleset) { | 165 TestRuleset* test_unindexed_ruleset) { |
| 157 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( | 166 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( |
| 158 SerializeUnindexedRulesetWithMultipleRules(rules), | 167 SerializeUnindexedRulesetWithMultipleRules(rules), |
| 159 test_unindexed_ruleset)); | 168 test_unindexed_ruleset)); |
| 160 } | 169 } |
| 161 | 170 |
| 162 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) { | 171 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) { |
| 163 DCHECK(path); | 172 DCHECK(path); |
| 164 ASSERT_TRUE(scoped_temp_dir_.IsValid() || | 173 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 165 scoped_temp_dir_.CreateUniqueTempDir()); | 174 ASSERT_TRUE(scoped_temp_dir_->IsValid() || |
| 166 *path = scoped_temp_dir_.GetPath().AppendASCII( | 175 scoped_temp_dir_->CreateUniqueTempDir()); |
| 176 *path = scoped_temp_dir_->GetPath().AppendASCII( |
| 167 base::IntToString(next_unique_file_suffix++)); | 177 base::IntToString(next_unique_file_suffix++)); |
| 168 } | 178 } |
| 169 | 179 |
| 170 void TestRulesetCreator::CreateTestRulesetFromContents( | 180 void TestRulesetCreator::CreateTestRulesetFromContents( |
| 171 std::vector<uint8_t> ruleset_contents, | 181 std::vector<uint8_t> ruleset_contents, |
| 172 TestRuleset* ruleset) { | 182 TestRuleset* ruleset) { |
| 173 DCHECK(ruleset); | 183 DCHECK(ruleset); |
| 174 | 184 |
| 175 ruleset->contents = std::move(ruleset_contents); | 185 ruleset->contents = std::move(ruleset_contents); |
| 176 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); | 186 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); |
| 177 WriteRulesetContents(ruleset->contents, ruleset->path); | 187 WriteRulesetContents(ruleset->contents, ruleset->path); |
| 178 } | 188 } |
| 179 | 189 |
| 180 } // namespace testing | 190 } // namespace testing |
| 181 } // namespace subresource_filter | 191 } // namespace subresource_filter |
| OLD | NEW |