| 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/indexed_ruleset.h" | 5 #include "components/subresource_filter/core/common/indexed_ruleset.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 UrlRuleBuilder(const UrlPattern& url_pattern, | 42 UrlRuleBuilder(const UrlPattern& url_pattern, |
| 43 proto::SourceType source_type, | 43 proto::SourceType source_type, |
| 44 bool is_whitelist) { | 44 bool is_whitelist) { |
| 45 rule_.set_semantics(is_whitelist ? proto::RULE_SEMANTICS_WHITELIST | 45 rule_.set_semantics(is_whitelist ? proto::RULE_SEMANTICS_WHITELIST |
| 46 : proto::RULE_SEMANTICS_BLACKLIST); | 46 : proto::RULE_SEMANTICS_BLACKLIST); |
| 47 | 47 |
| 48 rule_.set_source_type(source_type); | 48 rule_.set_source_type(source_type); |
| 49 rule_.set_element_types(proto::ELEMENT_TYPE_ALL); | 49 rule_.set_element_types(proto::ELEMENT_TYPE_ALL); |
| 50 | 50 |
| 51 rule_.set_url_pattern_type(url_pattern.type); | 51 rule_.set_url_pattern_type(url_pattern.type()); |
| 52 rule_.set_anchor_left(url_pattern.anchor_left); | 52 rule_.set_anchor_left(url_pattern.anchor_left()); |
| 53 rule_.set_anchor_right(url_pattern.anchor_right); | 53 rule_.set_anchor_right(url_pattern.anchor_right()); |
| 54 rule_.set_match_case(url_pattern.match_case); | 54 rule_.set_match_case(url_pattern.match_case()); |
| 55 rule_.set_url_pattern(url_pattern.url_pattern.as_string()); | 55 rule_.set_url_pattern(url_pattern.url_pattern().as_string()); |
| 56 } | 56 } |
| 57 | 57 |
| 58 UrlRuleBuilder& AddDomain(std::string domain_pattern) { | 58 UrlRuleBuilder& AddDomain(std::string domain_pattern) { |
| 59 DCHECK(!domain_pattern.empty()); | 59 DCHECK(!domain_pattern.empty()); |
| 60 auto* domain = rule_.add_domains(); | 60 auto* domain = rule_.add_domains(); |
| 61 if (domain_pattern[0] == '~') { | 61 if (domain_pattern[0] == '~') { |
| 62 domain_pattern.erase(0, 1); | 62 domain_pattern.erase(0, 1); |
| 63 domain->set_exclude(true); | 63 domain->set_exclude(true); |
| 64 } | 64 } |
| 65 domain->set_domain(domain_pattern); | 65 domain->set_domain(domain_pattern); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 // {"/example\\.com/.*\\/[a-zA-Z0-9]{3}/", "http://example.com/abcd/42y", | 284 // {"/example\\.com/.*\\/[a-zA-Z0-9]{3}/", "http://example.com/abcd/42y", |
| 285 // false}, | 285 // false}, |
| 286 // {"/example\\.com/.*\\/[a-zA-Z0-9]{3}/", "http://example.com/abcd/%42y", | 286 // {"/example\\.com/.*\\/[a-zA-Z0-9]{3}/", "http://example.com/abcd/%42y", |
| 287 // true}, | 287 // true}, |
| 288 // {"||example.com^*/test.htm", "http://example.com/unit/test.html", | 288 // {"||example.com^*/test.htm", "http://example.com/unit/test.html", |
| 289 // false}, | 289 // false}, |
| 290 // {"||example.com^*/test.htm", "http://examole.com/test.htm", true}, | 290 // {"||example.com^*/test.htm", "http://examole.com/test.htm", true}, |
| 291 }; | 291 }; |
| 292 | 292 |
| 293 for (const auto& test_case : kTestCases) { | 293 for (const auto& test_case : kTestCases) { |
| 294 SCOPED_TRACE(testing::Message() | 294 SCOPED_TRACE(testing::Message() << "Rule: " << test_case.url_pattern |
| 295 << "Rule: " << test_case.url_pattern.url_pattern | 295 << "; URL: " << test_case.url); |
| 296 << "; URL: " << test_case.url); | |
| 297 | 296 |
| 298 AddBlacklistRule(test_case.url_pattern); | 297 AddBlacklistRule(test_case.url_pattern); |
| 299 Finish(); | 298 Finish(); |
| 300 | 299 |
| 301 EXPECT_EQ(test_case.expect_allowed, ShouldAllow(test_case.url)); | 300 EXPECT_EQ(test_case.expect_allowed, ShouldAllow(test_case.url)); |
| 302 Reset(); | 301 Reset(); |
| 303 } | 302 } |
| 304 } | 303 } |
| 305 | 304 |
| 306 TEST_F(IndexedRulesetTest, OneRuleWithThirdParty) { | 305 TEST_F(IndexedRulesetTest, OneRuleWithThirdParty) { |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 698 EXPECT_FALSE(indexer_.AddUrlRule(builder.rule())); | 697 EXPECT_FALSE(indexer_.AddUrlRule(builder.rule())); |
| 699 | 698 |
| 700 AddSimpleRule(UrlPattern("example.com", kSubstring), false); | 699 AddSimpleRule(UrlPattern("example.com", kSubstring), false); |
| 701 Finish(); | 700 Finish(); |
| 702 | 701 |
| 703 EXPECT_TRUE(ShouldAllow("https://exmpl.com")); | 702 EXPECT_TRUE(ShouldAllow("https://exmpl.com")); |
| 704 EXPECT_FALSE(ShouldAllow("https://example.com")); | 703 EXPECT_FALSE(ShouldAllow("https://example.com")); |
| 705 } | 704 } |
| 706 | 705 |
| 707 } // namespace subresource_filter | 706 } // namespace subresource_filter |
| OLD | NEW |