Chromium Code Reviews| 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" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 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 Loading... | |
| 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 CreateRulesetWithRules({suffix_rule}, test_unindexed_ruleset); |
|
engedy
2017/04/05 10:55:10
nit: Enclose this in ASSERT_NO_FATAL_FAILURE too,
pkalinnikov
2017/04/05 13:29:21
Done.
| |
| 130 SerializeUnindexedRulesetWithSingleRule(suffix_rule), | |
| 131 test_unindexed_ruleset)); | |
| 132 } | 125 } |
| 133 | 126 |
| 134 void TestRulesetCreator::CreateRulesetToDisallowURLsWithManySuffixes( | 127 void TestRulesetCreator::CreateRulesetToDisallowURLsWithManySuffixes( |
| 135 base::StringPiece suffix, | 128 base::StringPiece suffix, |
| 136 int num_of_suffixes, | 129 int num_of_suffixes, |
| 137 TestRulesetPair* test_ruleset_pair) { | 130 TestRulesetPair* test_ruleset_pair) { |
| 138 DCHECK(test_ruleset_pair); | 131 DCHECK(test_ruleset_pair); |
| 139 | 132 |
| 140 std::vector<proto::UrlRule> rules; | 133 std::vector<proto::UrlRule> rules; |
| 141 for (int i = 0; i < num_of_suffixes; ++i) { | 134 for (int i = 0; i < num_of_suffixes; ++i) { |
| 142 std::string current_suffix = | 135 std::string current_suffix = |
| 143 suffix.as_string() + '_' + base::IntToString(i); | 136 suffix.as_string() + '_' + base::IntToString(i); |
| 144 rules.push_back(CreateSuffixRule(current_suffix)); | 137 rules.push_back(CreateSuffixRule(current_suffix)); |
| 145 } | 138 } |
| 146 CreateRulesetWithRules(rules, test_ruleset_pair); | 139 CreateRulesetWithRules(rules, test_ruleset_pair); |
| 147 } | 140 } |
| 148 | 141 |
| 149 void TestRulesetCreator::CreateRulesetWithRules( | 142 void TestRulesetCreator::CreateRulesetWithRules( |
| 150 const std::vector<proto::UrlRule>& rules, | 143 const std::vector<proto::UrlRule>& rules, |
| 151 TestRulesetPair* test_ruleset_pair) { | 144 TestRulesetPair* test_ruleset_pair) { |
| 152 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( | 145 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( |
| 153 SerializeUnindexedRulesetWithMultipleRules(rules), | 146 SerializeUnindexedRulesetWithMultipleRules(rules), |
| 154 &test_ruleset_pair->unindexed)); | 147 &test_ruleset_pair->unindexed)); |
| 155 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( | 148 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( |
| 156 SerializeIndexedRulesetWithMultipleRules(rules), | 149 SerializeIndexedRulesetWithMultipleRules(rules), |
| 157 &test_ruleset_pair->indexed)); | 150 &test_ruleset_pair->indexed)); |
| 158 } | 151 } |
| 159 | 152 |
| 153 void TestRulesetCreator::CreateRulesetWithRules( | |
| 154 const std::vector<proto::UrlRule>& rules, | |
| 155 TestRuleset* test_unindexed_ruleset) { | |
| 156 ASSERT_NO_FATAL_FAILURE(CreateTestRulesetFromContents( | |
| 157 SerializeUnindexedRulesetWithMultipleRules(rules), | |
| 158 test_unindexed_ruleset)); | |
| 159 } | |
| 160 | |
| 160 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) { | 161 void TestRulesetCreator::GetUniqueTemporaryPath(base::FilePath* path) { |
| 161 DCHECK(path); | 162 DCHECK(path); |
| 162 ASSERT_TRUE(scoped_temp_dir_.IsValid() || | 163 ASSERT_TRUE(scoped_temp_dir_.IsValid() || |
| 163 scoped_temp_dir_.CreateUniqueTempDir()); | 164 scoped_temp_dir_.CreateUniqueTempDir()); |
| 164 *path = scoped_temp_dir_.GetPath().AppendASCII( | 165 *path = scoped_temp_dir_.GetPath().AppendASCII( |
| 165 base::IntToString(next_unique_file_suffix++)); | 166 base::IntToString(next_unique_file_suffix++)); |
| 166 } | 167 } |
| 167 | 168 |
| 168 void TestRulesetCreator::CreateTestRulesetFromContents( | 169 void TestRulesetCreator::CreateTestRulesetFromContents( |
| 169 std::vector<uint8_t> ruleset_contents, | 170 std::vector<uint8_t> ruleset_contents, |
| 170 TestRuleset* ruleset) { | 171 TestRuleset* ruleset) { |
| 171 DCHECK(ruleset); | 172 DCHECK(ruleset); |
| 172 | 173 |
| 173 ruleset->contents = std::move(ruleset_contents); | 174 ruleset->contents = std::move(ruleset_contents); |
| 174 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); | 175 ASSERT_NO_FATAL_FAILURE(GetUniqueTemporaryPath(&ruleset->path)); |
| 175 WriteRulesetContents(ruleset->contents, ruleset->path); | 176 WriteRulesetContents(ruleset->contents, ruleset->path); |
| 176 } | 177 } |
| 177 | 178 |
| 178 } // namespace testing | 179 } // namespace testing |
| 179 } // namespace subresource_filter | 180 } // namespace subresource_filter |
| OLD | NEW |