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

Unified Diff: components/subresource_filter/core/common/indexed_ruleset_unittest.cc

Issue 2183883002: Implement deactivating SubresourceFilter for document. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@integrate_ruleset
Patch Set: Address more comments from engedy@ Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: components/subresource_filter/core/common/indexed_ruleset_unittest.cc
diff --git a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc b/components/subresource_filter/core/common/indexed_ruleset_unittest.cc
index 4dd8079b53362512c2775967e55772c1d01f3a00..d9a3ff48f29b7ac30cbe1f398591ef4f41058b9c 100644
--- a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc
+++ b/components/subresource_filter/core/common/indexed_ruleset_unittest.cc
@@ -44,7 +44,18 @@ class IndexedRulesetTest : public testing::Test {
proto::ElementType element_type = proto::ELEMENT_TYPE_OTHER) const {
DCHECK_NE(matcher_.get(), nullptr);
url::Origin origin = GetOrigin(initiator);
- return matcher_->IsAllowed(GURL(url), origin, element_type);
+ return !matcher_->ShouldDisallowResourceLoad(GURL(url), origin,
+ element_type);
+ }
+
+ bool ShouldDeactivate(const char* document_url,
+ const char* initiator = nullptr,
+ proto::ActivationType activation_type =
+ proto::ACTIVATION_TYPE_UNSPECIFIED) const {
+ DCHECK(matcher_);
+ url::Origin origin = GetOrigin(initiator);
+ return matcher_->ShouldDisableFilteringForDocument(GURL(document_url),
+ origin, activation_type);
}
proto::UrlRule CreateRule(const UrlPattern& url_pattern,
@@ -82,6 +93,14 @@ class IndexedRulesetTest : public testing::Test {
indexer_.AddUrlRule(rule);
}
+ void AddWhitelistRuleWithActivationTypes(const UrlPattern& url_pattern,
+ int32_t activation_types) {
+ proto::UrlRule rule = CreateRule(url_pattern, kAnyParty, true);
+ rule.set_element_types(proto::ELEMENT_TYPE_UNSPECIFIED);
+ rule.set_activation_types(activation_types);
+ indexer_.AddUrlRule(rule);
+ }
+
void AddBlacklistRule(const UrlPattern& url_pattern,
const std::vector<std::string>& domains) {
proto::UrlRule rule = CreateRule(url_pattern, kAnyParty, false);
@@ -472,6 +491,55 @@ TEST_F(IndexedRulesetTest, OneRuleWithElementTypes) {
}
}
+TEST_F(IndexedRulesetTest, OneRuleWithActivationTypes) {
+ constexpr proto::ActivationType kNone = proto::ACTIVATION_TYPE_UNSPECIFIED;
+ constexpr proto::ActivationType kDocument = proto::ACTIVATION_TYPE_DOCUMENT;
+
+ const struct {
+ const char* url_pattern;
+ int32_t activation_types;
+
+ const char* document_url;
+ proto::ActivationType activation_type;
+ bool expect_disabled;
+ } kTestCases[] = {
+ {"example.com", kDocument, "http://example.com", kDocument, true},
+ {"xample.com", kDocument, "http://example.com", kDocument, true},
+ {"exampl.com", kDocument, "http://example.com", kDocument, false},
+
+ {"example.com", kNone, "http://example.com", kDocument, false},
+ {"example.com", kDocument, "http://example.com", kNone, false},
+ {"example.com", kNone, "http://example.com", kNone, false},
+
+ // Invalid GURL.
+ {"example.com", kDocument, "http;//example.com", kDocument, false},
+ };
+
+ for (const auto& test_case : kTestCases) {
+ SCOPED_TRACE(testing::Message()
+ << "Rule: " << test_case.url_pattern
+ << "; ActivationTypes: " << (int)test_case.activation_types
+ << "; DocURL: " << test_case.document_url
+ << "; ActivationType: " << (int)test_case.activation_type);
+
+ AddWhitelistRuleWithActivationTypes(
+ UrlPattern(test_case.url_pattern, kSubstring),
+ test_case.activation_types);
+ Finish();
+
+ EXPECT_EQ(test_case.expect_disabled,
+ ShouldDeactivate(test_case.document_url, nullptr /* initiator */,
+ test_case.activation_type));
+ EXPECT_EQ(test_case.expect_disabled,
+ ShouldDeactivate(test_case.document_url, "http://example.com/",
+ test_case.activation_type));
+ EXPECT_EQ(test_case.expect_disabled,
+ ShouldDeactivate(test_case.document_url, "http://xmpl.com/",
+ test_case.activation_type));
+ Reset();
+ }
+}
+
TEST_F(IndexedRulesetTest, EmptyRuleset) {
Finish();
EXPECT_TRUE(ShouldAllow("http://example.com"));
@@ -513,4 +581,18 @@ TEST_F(IndexedRulesetTest, BlacklistWhitelist) {
EXPECT_FALSE(ShouldAllow("http://blacklisted.com?filter=on"));
}
+TEST_F(IndexedRulesetTest, BlacklistAndActivationType) {
+ const auto kDocument = proto::ACTIVATION_TYPE_DOCUMENT;
+
+ AddSimpleRule(UrlPattern("example.com", kSubstring), false);
+ AddWhitelistRuleWithActivationTypes(UrlPattern("example.com", kSubstring),
+ kDocument);
+ Finish();
+
+ EXPECT_TRUE(ShouldDeactivate("https://example.com", nullptr, kDocument));
+ EXPECT_FALSE(ShouldDeactivate("https://xample.com", nullptr, kDocument));
+ EXPECT_FALSE(ShouldAllow("https://example.com"));
+ EXPECT_TRUE(ShouldAllow("https://xample.com"));
+}
+
} // namespace subresource_filter

Powered by Google App Engine
This is Rietveld 408576698