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

Side by Side Diff: components/subresource_filter/core/common/test_ruleset_creator.cc

Issue 2793993002: [subresource_filter] Replace KMP by std::search. (Closed)
Patch Set: Fix DCHECK. Created 3 years, 8 months 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
OLDNEW
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"
(...skipping 27 matching lines...) Expand all
38 google::protobuf::io::StringOutputStream output(&ruleset_contents); 38 google::protobuf::io::StringOutputStream output(&ruleset_contents);
39 UnindexedRulesetWriter ruleset_writer(&output); 39 UnindexedRulesetWriter ruleset_writer(&output);
40 for (const auto& rule : rules) 40 for (const auto& rule : rules)
41 ruleset_writer.AddUrlRule(rule); 41 ruleset_writer.AddUrlRule(rule);
42 ruleset_writer.Finish(); 42 ruleset_writer.Finish();
43 43
44 auto* data = reinterpret_cast<const uint8_t*>(ruleset_contents.data()); 44 auto* data = reinterpret_cast<const uint8_t*>(ruleset_contents.data());
45 return std::vector<uint8_t>(data, data + ruleset_contents.size()); 45 return std::vector<uint8_t>(data, data + ruleset_contents.size());
46 } 46 }
47 47
48 std::vector<uint8_t> SerializeUnindexedRulesetWithSingleRule(
49 const proto::UrlRule& rule) {
50 return SerializeUnindexedRulesetWithMultipleRules({rule});
51 }
52
53 std::vector<uint8_t> SerializeIndexedRulesetWithMultipleRules( 48 std::vector<uint8_t> SerializeIndexedRulesetWithMultipleRules(
54 const std::vector<proto::UrlRule>& rules) { 49 const std::vector<proto::UrlRule>& rules) {
55 RulesetIndexer indexer; 50 RulesetIndexer indexer;
56 for (const auto& rule : rules) 51 for (const auto& rule : rules)
57 EXPECT_TRUE(indexer.AddUrlRule(rule)); 52 EXPECT_TRUE(indexer.AddUrlRule(rule));
58 indexer.Finish(); 53 indexer.Finish();
59 return std::vector<uint8_t>(indexer.data(), indexer.data() + indexer.size()); 54 return std::vector<uint8_t>(indexer.data(), indexer.data() + indexer.size());
60 } 55 }
61 56
62 } // namespace 57 } // namespace
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 DCHECK(test_ruleset_pair); 114 DCHECK(test_ruleset_pair);
120 proto::UrlRule suffix_rule = CreateSuffixRule(suffix); 115 proto::UrlRule suffix_rule = CreateSuffixRule(suffix);
121 CreateRulesetWithRules({suffix_rule}, test_ruleset_pair); 116 CreateRulesetWithRules({suffix_rule}, test_ruleset_pair);
122 } 117 }
123 118
124 void TestRulesetCreator::CreateUnindexedRulesetToDisallowURLsWithPathSuffix( 119 void TestRulesetCreator::CreateUnindexedRulesetToDisallowURLsWithPathSuffix(
125 base::StringPiece suffix, 120 base::StringPiece suffix,
126 TestRuleset* test_unindexed_ruleset) { 121 TestRuleset* test_unindexed_ruleset) {
127 DCHECK(test_unindexed_ruleset); 122 DCHECK(test_unindexed_ruleset);
128 proto::UrlRule suffix_rule = CreateSuffixRule(suffix); 123 proto::UrlRule suffix_rule = CreateSuffixRule(suffix);
129 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( 124 ASSERT_NO_FATAL_FAILURE(
130 SerializeUnindexedRulesetWithSingleRule(suffix_rule), 125 CreateUnindexedRulesetWithRules({suffix_rule}, test_unindexed_ruleset));
131 test_unindexed_ruleset));
132 } 126 }
133 127
134 void TestRulesetCreator::CreateRulesetToDisallowURLsWithManySuffixes( 128 void TestRulesetCreator::CreateRulesetToDisallowURLsWithManySuffixes(
135 base::StringPiece suffix, 129 base::StringPiece suffix,
136 int num_of_suffixes, 130 int num_of_suffixes,
137 TestRulesetPair* test_ruleset_pair) { 131 TestRulesetPair* test_ruleset_pair) {
138 DCHECK(test_ruleset_pair); 132 DCHECK(test_ruleset_pair);
139 133
140 std::vector<proto::UrlRule> rules; 134 std::vector<proto::UrlRule> rules;
141 for (int i = 0; i < num_of_suffixes; ++i) { 135 for (int i = 0; i < num_of_suffixes; ++i) {
142 std::string current_suffix = 136 std::string current_suffix =
143 suffix.as_string() + '_' + base::IntToString(i); 137 suffix.as_string() + '_' + base::IntToString(i);
144 rules.push_back(CreateSuffixRule(current_suffix)); 138 rules.push_back(CreateSuffixRule(current_suffix));
145 } 139 }
146 CreateRulesetWithRules(rules, test_ruleset_pair); 140 CreateRulesetWithRules(rules, test_ruleset_pair);
147 } 141 }
148 142
149 void TestRulesetCreator::CreateRulesetWithRules( 143 void TestRulesetCreator::CreateRulesetWithRules(
150 const std::vector<proto::UrlRule>& rules, 144 const std::vector<proto::UrlRule>& rules,
151 TestRulesetPair* test_ruleset_pair) { 145 TestRulesetPair* test_ruleset_pair) {
152 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( 146 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents(
153 SerializeUnindexedRulesetWithMultipleRules(rules), 147 SerializeUnindexedRulesetWithMultipleRules(rules),
154 &test_ruleset_pair->unindexed)); 148 &test_ruleset_pair->unindexed));
155 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( 149 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents(
156 SerializeIndexedRulesetWithMultipleRules(rules), 150 SerializeIndexedRulesetWithMultipleRules(rules),
157 &test_ruleset_pair->indexed)); 151 &test_ruleset_pair->indexed));
158 } 152 }
159 153
154 void TestRulesetCreator::CreateUnindexedRulesetWithRules(
155 const std::vector<proto::UrlRule>& rules,
156 TestRuleset* test_unindexed_ruleset) {
157 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents(
158 SerializeUnindexedRulesetWithMultipleRules(rules),
159 test_unindexed_ruleset));
160 }
161
160 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) { 162 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) {
161 DCHECK(path); 163 DCHECK(path);
162 ASSERT_TRUE(scoped_temp_dir_.IsValid() || 164 ASSERT_TRUE(scoped_temp_dir_.IsValid() ||
163 scoped_temp_dir_.CreateUniqueTempDir()); 165 scoped_temp_dir_.CreateUniqueTempDir());
164 *path = scoped_temp_dir_.GetPath().AppendASCII( 166 *path = scoped_temp_dir_.GetPath().AppendASCII(
165 base::IntToString(next_unique_file_suffix++)); 167 base::IntToString(next_unique_file_suffix++));
166 } 168 }
167 169
168 void TestRulesetCreator::CreateTestRulesetFromContents( 170 void TestRulesetCreator::CreateTestRulesetFromContents(
169 std::vector<uint8_t> ruleset_contents, 171 std::vector<uint8_t> ruleset_contents,
170 TestRuleset* ruleset) { 172 TestRuleset* ruleset) {
171 DCHECK(ruleset); 173 DCHECK(ruleset);
172 174
173 ruleset->contents = std::move(ruleset_contents); 175 ruleset->contents = std::move(ruleset_contents);
174 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); 176 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path));
175 WriteRulesetContents(ruleset->contents, ruleset->path); 177 WriteRulesetContents(ruleset->contents, ruleset->path);
176 } 178 }
177 179
178 } // namespace testing 180 } // namespace testing
179 } // namespace subresource_filter 181 } // namespace subresource_filter
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698