Index: components/subresource_filter/core/common/test_ruleset_creator.cc |
diff --git a/components/subresource_filter/core/common/test_ruleset_creator.cc b/components/subresource_filter/core/common/test_ruleset_creator.cc |
index 08cd6271e926708d5847ce21c3489635a3a5344d..81aabf832c0e8e7a32ad2d6a81eeb4941258bbe1 100644 |
--- a/components/subresource_filter/core/common/test_ruleset_creator.cc |
+++ b/components/subresource_filter/core/common/test_ruleset_creator.cc |
@@ -4,6 +4,8 @@ |
#include "components/subresource_filter/core/common/test_ruleset_creator.h" |
+#include <string> |
+ |
#include "base/files/file_util.h" |
#include "base/logging.h" |
#include "base/strings/string_number_conversions.h" |
@@ -21,6 +23,15 @@ namespace { |
// The methods below assume that char and uint8_t are interchangeable. |
static_assert(CHAR_BIT == 8, "Assumed char was 8 bits."); |
+void WriteRulesetContents(const std::vector<uint8_t>& contents, |
+ base::FilePath path) { |
+ int ruleset_size_as_int = base::checked_cast<int>(contents.size()); |
+ int num_bytes_written = |
+ base::WriteFile(path, reinterpret_cast<const char*>(contents.data()), |
+ ruleset_size_as_int); |
+ ASSERT_EQ(ruleset_size_as_int, num_bytes_written); |
+} |
+ |
std::vector<uint8_t> SerializeUnindexedRulesetWithMultipleRules( |
const std::vector<proto::UrlRule>& rules) { |
std::string ruleset_contents; |
@@ -65,6 +76,33 @@ base::File TestRuleset::Open(const TestRuleset& ruleset) { |
return file; |
} |
+// static |
+void TestRuleset::CorruptByTruncating(const TestRuleset& ruleset, |
+ size_t tail_size) { |
+ ASSERT_GT(tail_size, 0u); |
+ ASSERT_FALSE(ruleset.contents.empty()); |
+ std::vector<uint8_t> new_contents = ruleset.contents; |
+ |
+ const size_t new_size = |
+ tail_size < new_contents.size() ? new_contents.size() - tail_size : 0; |
+ new_contents.resize(new_size); |
+ WriteRulesetContents(new_contents, ruleset.path); |
+} |
+ |
+// static |
+void TestRuleset::CorruptByFilling(const TestRuleset& ruleset, |
+ size_t from, |
+ size_t to, |
+ uint8_t fill_with) { |
+ ASSERT_LT(from, to); |
+ ASSERT_LE(to, ruleset.contents.size()); |
+ |
+ std::vector<uint8_t> new_contents = ruleset.contents; |
+ for (size_t i = from; i < to; ++i) |
+ new_contents[i] = fill_with; |
+ WriteRulesetContents(new_contents, ruleset.path); |
+} |
+ |
// TestRulesetPair ------------------------------------------------------------- |
TestRulesetPair::TestRulesetPair() = default; |
@@ -93,6 +131,21 @@ void TestRulesetCreator::CreateUnindexedRulesetToDisallowURLsWithPathSuffix( |
test_unindexed_ruleset)); |
} |
+void TestRulesetCreator::CreateRulesetToDisallowURLsWithManySuffixes( |
+ base::StringPiece suffix, |
+ int num_of_suffixes, |
+ TestRulesetPair* test_ruleset_pair) { |
+ DCHECK(test_ruleset_pair); |
+ |
+ std::vector<proto::UrlRule> rules; |
+ for (int i = 0; i < num_of_suffixes; ++i) { |
+ std::string current_suffix = |
+ suffix.as_string() + '_' + base::IntToString(i); |
+ rules.push_back(CreateSuffixRule(current_suffix)); |
+ } |
+ CreateRulesetWithRules(rules, test_ruleset_pair); |
+} |
+ |
void TestRulesetCreator::CreateRulesetWithRules( |
const std::vector<proto::UrlRule>& rules, |
TestRulesetPair* test_ruleset_pair) { |
@@ -119,11 +172,7 @@ void TestRulesetCreator::CreateTestRulesetFromContents( |
ruleset->contents = std::move(ruleset_contents); |
ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); |
- int ruleset_size_as_int = base::checked_cast<int>(ruleset->contents.size()); |
- int num_bytes_written = base::WriteFile( |
- ruleset->path, reinterpret_cast<const char*>(ruleset->contents.data()), |
- ruleset_size_as_int); |
- ASSERT_EQ(ruleset_size_as_int, num_bytes_written); |
+ WriteRulesetContents(ruleset->contents, ruleset->path); |
} |
} // namespace testing |