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

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

Issue 2862853002: Move UrlPatternIndex-specific tests from indexed_ruleset_unittest. (Closed)
Patch Set: Address comments from engedy@. Created 3 years, 7 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
« no previous file with comments | « components/subresource_filter/core/common/url_pattern_index.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/subresource_filter/core/common/url_pattern_index_unittest.cc
diff --git a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc b/components/subresource_filter/core/common/url_pattern_index_unittest.cc
similarity index 53%
copy from components/subresource_filter/core/common/indexed_ruleset_unittest.cc
copy to components/subresource_filter/core/common/url_pattern_index_unittest.cc
index 41778883a7dda6972823ca7e9f8f48cdd8b334bd..1052d5bab3a0151dbca3d3f2f9b3f342d5f57a38 100644
--- a/components/subresource_filter/core/common/indexed_ruleset_unittest.cc
+++ b/components/subresource_filter/core/common/url_pattern_index_unittest.cc
@@ -1,18 +1,15 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
+// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/subresource_filter/core/common/indexed_ruleset.h"
+#include "components/subresource_filter/core/common/url_pattern_index.h"
#include <memory>
#include <string>
#include <vector>
-#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/string_piece.h"
-#include "components/subresource_filter/core/common/first_party_origin.h"
-#include "components/subresource_filter/core/common/proto/rules.pb.h"
#include "components/subresource_filter/core/common/url_pattern.h"
#include "components/subresource_filter/core/common/url_rule_test_support.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -23,168 +20,177 @@ namespace subresource_filter {
using namespace testing;
-class SubresourceFilterIndexedRulesetTest : public ::testing::Test {
+class UrlPatternIndexTest : public ::testing::Test {
public:
- SubresourceFilterIndexedRulesetTest() { Reset(); }
+ UrlPatternIndexTest() { Reset(); }
protected:
- bool ShouldAllow(base::StringPiece url,
- base::StringPiece document_origin = nullptr,
- proto::ElementType element_type = kOther,
- bool disable_generic_rules = false) const {
- DCHECK(matcher_);
- return !matcher_->ShouldDisallowResourceLoad(
- GURL(url), FirstPartyOrigin(GetOrigin(document_origin)), element_type,
- disable_generic_rules);
- }
-
- bool ShouldDeactivate(
- base::StringPiece document_url,
- base::StringPiece parent_document_origin = nullptr,
- proto::ActivationType activation_type = kNoActivation) const {
- DCHECK(matcher_);
- return matcher_->ShouldDisableFilteringForDocument(
- GURL(document_url), GetOrigin(parent_document_origin), activation_type);
- }
-
bool AddUrlRule(const proto::UrlRule& rule) {
- return indexer_->AddUrlRule(rule);
+ auto offset = SerializeUrlRule(rule, flat_builder_.get());
+ if (offset.o)
+ index_builder_->IndexUrlRule(offset);
+ return !!offset.o;
}
- bool AddSimpleRule(base::StringPiece url_pattern) {
- return AddUrlRule(MakeUrlRule(UrlPattern(url_pattern, kSubstring)));
- }
-
- bool AddSimpleWhitelistRule(base::StringPiece url_pattern) {
- auto rule = MakeUrlRule(UrlPattern(url_pattern, kSubstring));
- rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
- return AddUrlRule(rule);
- }
+ void Finish() {
+ const auto index_offset = index_builder_->Finish();
+ flat_builder_->Finish(index_offset);
- bool AddSimpleWhitelistRule(base::StringPiece url_pattern,
- int32_t activation_types) {
- auto rule = MakeUrlRule(url_pattern);
- rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
- rule.clear_element_types();
- rule.set_activation_types(activation_types);
- return AddUrlRule(rule);
+ const flat::UrlPatternIndex* flat_index =
+ flat::GetUrlPatternIndex(flat_builder_->GetBufferPointer());
+ index_matcher_.reset(new UrlPatternIndexMatcher(flat_index));
}
- void Finish() {
- indexer_->Finish();
- matcher_.reset(
- new IndexedRulesetMatcher(indexer_->data(), indexer_->size()));
+ const flat::UrlRule* FindMatch(
+ base::StringPiece url_string,
+ base::StringPiece document_origin_string = base::StringPiece(),
+ proto::ElementType element_type = kOther,
+ proto::ActivationType activation_type = kNoActivation,
+ bool disable_generic_rules = false) {
+ const GURL url(url_string);
+ const url::Origin document_origin = GetOrigin(document_origin_string);
+ return index_matcher_->FindMatch(
+ url, document_origin, element_type, activation_type,
+ IsThirdParty(url, document_origin), disable_generic_rules);
}
void Reset() {
- matcher_.reset(nullptr);
- indexer_.reset(new RulesetIndexer);
+ index_matcher_.reset();
+ index_builder_.reset();
+ flat_builder_.reset(new flatbuffers::FlatBufferBuilder());
+ index_builder_.reset(new UrlPatternIndexBuilder(flat_builder_.get()));
}
- std::unique_ptr<RulesetIndexer> indexer_;
- std::unique_ptr<IndexedRulesetMatcher> matcher_;
-
private:
- DISALLOW_COPY_AND_ASSIGN(SubresourceFilterIndexedRulesetTest);
+ std::unique_ptr<flatbuffers::FlatBufferBuilder> flat_builder_;
+ std::unique_ptr<UrlPatternIndexBuilder> index_builder_;
+ std::unique_ptr<UrlPatternIndexMatcher> index_matcher_;
+
+ DISALLOW_COPY_AND_ASSIGN(UrlPatternIndexTest);
};
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithoutMetaInfo) {
+TEST_F(UrlPatternIndexTest, EmptyIndex) {
+ Finish();
+ EXPECT_FALSE(FindMatch(base::StringPiece() /* url */));
+ EXPECT_FALSE(FindMatch("http://example.com"));
+ EXPECT_FALSE(FindMatch("http://another.example.com?param=val"));
+}
+
+TEST_F(UrlPatternIndexTest, OneSimpleRule) {
+ ASSERT_TRUE(AddUrlRule(MakeUrlRule(UrlPattern("?param=", kSubstring))));
+ Finish();
+
+ EXPECT_FALSE(FindMatch("https://example.com"));
+ EXPECT_TRUE(FindMatch("http://example.org?param=image1"));
+}
+
+TEST_F(UrlPatternIndexTest, NoRuleApplies) {
+ ASSERT_TRUE(AddUrlRule(MakeUrlRule(UrlPattern("?filter_out=", kSubstring))));
+ ASSERT_TRUE(AddUrlRule(MakeUrlRule(UrlPattern("&filter_out=", kSubstring))));
+ Finish();
+
+ EXPECT_FALSE(FindMatch("http://example.com"));
+ EXPECT_FALSE(FindMatch("http://example.com?filter_not"));
+ EXPECT_FALSE(FindMatch("http://example.com?k=v&filter_not"));
+}
+
+TEST_F(UrlPatternIndexTest, OneRuleWithoutMetaInfo) {
const struct {
UrlPattern url_pattern;
const char* url;
- bool expect_allowed;
+ bool expect_match;
} kTestCases[] = {
// SUBSTRING
- {{"abcd", kSubstring}, "http://ex.com/abcd", false},
- {{"abcd", kSubstring}, "http://ex.com/dcab", true},
- {{"42", kSubstring}, "http://ex.com/adcd/picture42.png", false},
+ {{"abcd", kSubstring}, "http://ex.com/abcd", true},
+ {{"abcd", kSubstring}, "http://ex.com/dcab", false},
+ {{"42", kSubstring}, "http://ex.com/adcd/picture42.png", true},
{{"&test", kSubstring},
- "http://ex.com/params?para1=false&test=true",
- false},
- {{"-test-42.", kSubstring}, "http://ex.com/unit-test-42.1", false},
+ "http://ex.com/params?param1=false&test=true",
+ true},
+ {{"-test-42.", kSubstring}, "http://ex.com/unit-test-42.1", true},
{{"/abcdtest160x600.", kSubstring},
"http://ex.com/abcdtest160x600.png",
- false},
+ true},
// WILDCARDED
{{"http://ex.com/abcd/picture*.png"},
"http://ex.com/abcd/picture42.png",
- false},
- {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", false},
- {{"ex.com", kSubdomain, kAnchorNone}, "http://test.ex.com", false},
- {{"ex.com", kSubdomain, kAnchorNone}, "https://test.ex.com.com", false},
- {{"ex.com", kSubdomain, kAnchorNone}, "https://test.rest.ex.com", false},
- {{"ex.com", kSubdomain, kAnchorNone}, "https://test_ex.com", true},
-
- {{"http://ex.com", kBoundary, kAnchorNone}, "http://ex.com/", false},
- {{"http://ex.com", kBoundary, kAnchorNone}, "http://ex.com/42", false},
+ true},
+ {{"ex.com", kSubdomain, kAnchorNone}, "http://ex.com", true},
+ {{"ex.com", kSubdomain, kAnchorNone}, "http://test.ex.com", true},
+ {{"ex.com", kSubdomain, kAnchorNone}, "https://test.ex.com.com", true},
+ {{"ex.com", kSubdomain, kAnchorNone}, "https://test.rest.ex.com", true},
+ {{"ex.com", kSubdomain, kAnchorNone}, "https://test_ex.com", false},
+
+ {{"http://ex.com", kBoundary, kAnchorNone}, "http://ex.com/", true},
+ {{"http://ex.com", kBoundary, kAnchorNone}, "http://ex.com/42", true},
{{"http://ex.com", kBoundary, kAnchorNone},
"http://ex.com/42/http://ex.com/",
- false},
+ true},
{{"http://ex.com", kBoundary, kAnchorNone},
"http://ex.com/42/http://ex.info/",
- false},
- {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com", false},
- {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/42", true},
+ true},
+ {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com", true},
+ {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/42", false},
{{"http://ex.com/", kBoundary, kBoundary},
"http://ex.info/42/http://ex.com/",
- true},
+ false},
{{"http://ex.com/", kBoundary, kBoundary},
"http://ex.info/42/http://ex.com/",
- true},
- {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/", false},
- {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/42.swf", true},
+ false},
+ {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/", true},
+ {{"http://ex.com/", kBoundary, kBoundary}, "http://ex.com/42.swf", false},
{{"http://ex.com/", kBoundary, kBoundary},
"http://ex.info/redirect/http://ex.com/",
- true},
- {{"pdf", kAnchorNone, kBoundary}, "http://ex.com/abcd.pdf", false},
- {{"pdf", kAnchorNone, kBoundary}, "http://ex.com/pdfium", true},
- {{"http://ex.com^"}, "http://ex.com/", false},
- {{"http://ex.com^"}, "http://ex.com:8000/", false},
- {{"http://ex.com^"}, "http://ex.com.ru", true},
+ false},
+ {{"pdf", kAnchorNone, kBoundary}, "http://ex.com/abcd.pdf", true},
+ {{"pdf", kAnchorNone, kBoundary}, "http://ex.com/pdfium", false},
+ {{"http://ex.com^"}, "http://ex.com/", true},
+ {{"http://ex.com^"}, "http://ex.com:8000/", true},
+ {{"http://ex.com^"}, "http://ex.com.ru", false},
{{"^ex.com^"},
"http://ex.com:8000/42.loss?a=12&b=%D1%82%D0%B5%D1%81%D1%82",
- false},
+ true},
{{"^42.loss^"},
"http://ex.com:8000/42.loss?a=12&b=%D1%82%D0%B5%D1%81%D1%82",
- false},
+ true},
// TODO(pkalinnikov): The '^' at the end should match end-of-string.
//
// {"^%D1%82%D0%B5%D1%81%D1%82^",
// "http://ex.com:8000/42.loss?a=12&b=%D1%82%D0%B5%D1%81%D1%82",
- // false},
- // {"/abcd/*/picture^", "http://ex.com/abcd/42/picture", false},
-
- {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/loss/picture?param", false},
- {{"/abcd/*/picture^"}, "http://ex.com/abcd//picture/42", false},
- {{"/abcd/*/picture^"}, "http://ex.com/abcd/picture", true},
- {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/pictureraph", true},
- {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/picture.swf", true},
+ // true},
+ // {"/abcd/*/picture^", "http://ex.com/abcd/42/picture", true},
+
+ {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/loss/picture?param", true},
+ {{"/abcd/*/picture^"}, "http://ex.com/abcd//picture/42", true},
+ {{"/abcd/*/picture^"}, "http://ex.com/abcd/picture", false},
+ {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/pictureraph", false},
+ {{"/abcd/*/picture^"}, "http://ex.com/abcd/42/picture.swf", false},
{{"test.ex.com^", kSubdomain, kAnchorNone},
"http://test.ex.com/42.swf",
- false},
+ true},
{{"test.ex.com^", kSubdomain, kAnchorNone},
"http://server1.test.ex.com/42.swf",
- false},
+ true},
{{"test.ex.com^", kSubdomain, kAnchorNone},
"https://test.ex.com:8000/",
- false},
+ true},
{{"test.ex.com^", kSubdomain, kAnchorNone},
"http://test.ex.com.ua/42.swf",
- true},
+ false},
{{"test.ex.com^", kSubdomain, kAnchorNone},
"http://ex.com/redirect/http://test.ex.com/",
- true},
+ false},
- {{"/abcd/*"}, "https://ex.com/abcd/", false},
- {{"/abcd/*"}, "http://ex.com/abcd/picture.jpeg", false},
- {{"/abcd/*"}, "https://ex.com/abcd", true},
- {{"/abcd/*"}, "http://abcd.ex.com", true},
- {{"*/abcd/"}, "https://ex.com/abcd/", false},
- {{"*/abcd/"}, "http://ex.com/abcd/picture.jpeg", false},
- {{"*/abcd/"}, "https://ex.com/test-abcd/", true},
- {{"*/abcd/"}, "http://abcd.ex.com", true},
+ {{"/abcd/*"}, "https://ex.com/abcd/", true},
+ {{"/abcd/*"}, "http://ex.com/abcd/picture.jpeg", true},
+ {{"/abcd/*"}, "https://ex.com/abcd", false},
+ {{"/abcd/*"}, "http://abcd.ex.com", false},
+ {{"*/abcd/"}, "https://ex.com/abcd/", true},
+ {{"*/abcd/"}, "http://ex.com/abcd/picture.jpeg", true},
+ {{"*/abcd/"}, "https://ex.com/test-abcd/", false},
+ {{"*/abcd/"}, "http://abcd.ex.com", false},
};
for (const auto& test_case : kTestCases) {
@@ -194,41 +200,41 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithoutMetaInfo) {
ASSERT_TRUE(AddUrlRule(MakeUrlRule(test_case.url_pattern)));
Finish();
- EXPECT_EQ(test_case.expect_allowed, ShouldAllow(test_case.url));
+ EXPECT_EQ(test_case.expect_match, !!FindMatch(test_case.url));
Reset();
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithThirdParty) {
+TEST_F(UrlPatternIndexTest, OneRuleWithThirdParty) {
const struct {
const char* url_pattern;
proto::SourceType source_type;
const char* url;
const char* document_origin;
- bool expect_allowed;
+ bool expect_match;
} kTestCases[] = {
- {"ex.com", kThirdParty, "http://ex.com", "http://exmpl.org", false},
- {"ex.com", kThirdParty, "http://ex.com", "http://ex.com", true},
+ {"ex.com", kThirdParty, "http://ex.com", "http://exmpl.org", true},
+ {"ex.com", kThirdParty, "http://ex.com", "http://ex.com", false},
{"ex.com", kThirdParty, "http://ex.com/path?k=v", "http://exmpl.org",
- false},
- {"ex.com", kThirdParty, "http://ex.com/path?k=v", "http://ex.com", true},
- {"ex.com", kFirstParty, "http://ex.com/path?k=v", "http://ex.com", false},
- {"ex.com", kFirstParty, "http://ex.com/path?k=v", "http://exmpl.com",
true},
- {"ex.com", kAnyParty, "http://ex.com/path?k=v", "http://ex.com", false},
- {"ex.com", kAnyParty, "http://ex.com/path?k=v", "http://exmpl.com",
+ {"ex.com", kThirdParty, "http://ex.com/path?k=v", "http://ex.com", false},
+ {"ex.com", kFirstParty, "http://ex.com/path?k=v", "http://ex.com", true},
+ {"ex.com", kFirstParty, "http://ex.com/path?k=v", "http://exmpl.com",
+ false},
+ {"ex.com", kAnyParty, "http://ex.com/path?k=v", "http://ex.com", true},
+ {"ex.com", kAnyParty, "http://ex.com/path?k=v", "http://exmpl.com", true},
+ {"ex.com", kThirdParty, "http://subdomain.ex.com", "http://ex.com",
false},
- {"ex.com", kThirdParty, "http://subdomain.ex.com", "http://ex.com", true},
- {"ex.com", kThirdParty, "http://ex.com", nullptr, false},
+ {"ex.com", kThirdParty, "http://ex.com", nullptr, true},
// Public Suffix List tests.
- {"ex.com", kThirdParty, "http://two.ex.com", "http://one.ex.com", true},
- {"ex.com", kThirdParty, "http://ex.com", "http://one.ex.com", true},
- {"ex.com", kThirdParty, "http://two.ex.com", "http://ex.com", true},
- {"ex.com", kThirdParty, "http://ex.com", "http://ex.org", false},
+ {"ex.com", kThirdParty, "http://two.ex.com", "http://one.ex.com", false},
+ {"ex.com", kThirdParty, "http://ex.com", "http://one.ex.com", false},
+ {"ex.com", kThirdParty, "http://two.ex.com", "http://ex.com", false},
+ {"ex.com", kThirdParty, "http://ex.com", "http://example.org", true},
{"appspot.com", kThirdParty, "http://two.appspot.org",
- "http://one.appspot.com", true},
+ "http://one.appspot.com", false},
};
for (auto test_case : kTestCases) {
@@ -243,95 +249,95 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithThirdParty) {
ASSERT_TRUE(AddUrlRule(rule));
Finish();
- EXPECT_EQ(test_case.expect_allowed,
- ShouldAllow(test_case.url, test_case.document_origin));
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(test_case.url, test_case.document_origin));
Reset();
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithDomainList) {
+TEST_F(UrlPatternIndexTest, OneRuleWithDomainList) {
constexpr const char* kUrl = "http://example.com";
const struct {
std::vector<std::string> domains;
const char* document_origin;
- bool expect_allowed;
+ bool expect_match;
} kTestCases[] = {
- {std::vector<std::string>(), nullptr, false},
- {std::vector<std::string>(), "http://domain.com", false},
-
- {{"domain.com"}, nullptr, true},
- {{"domain.com"}, "http://domain.com", false},
- {{"ddomain.com"}, "http://domain.com", true},
- {{"domain.com"}, "http://ddomain.com", true},
- {{"domain.com"}, "http://sub.domain.com", false},
- {{"sub.domain.com"}, "http://domain.com", true},
- {{"sub.domain.com"}, "http://sub.domain.com", false},
- {{"sub.domain.com"}, "http://a.b.c.sub.domain.com", false},
- {{"sub.domain.com"}, "http://sub.domain.com.com", true},
+ {std::vector<std::string>(), nullptr, true},
+ {std::vector<std::string>(), "http://domain.com", true},
+
+ {{"domain.com"}, nullptr, false},
+ {{"domain.com"}, "http://domain.com", true},
+ {{"ddomain.com"}, "http://domain.com", false},
+ {{"domain.com"}, "http://ddomain.com", false},
+ {{"domain.com"}, "http://sub.domain.com", true},
+ {{"sub.domain.com"}, "http://domain.com", false},
+ {{"sub.domain.com"}, "http://sub.domain.com", true},
+ {{"sub.domain.com"}, "http://a.b.c.sub.domain.com", true},
+ {{"sub.domain.com"}, "http://sub.domain.com.com", false},
// TODO(pkalinnikov): Probably need to canonicalize domain patterns to
// avoid subtleties like below.
- {{"domain.com"}, "http://domain.com.", false},
- {{"domain.com"}, "http://.domain.com", false},
- {{"domain.com"}, "http://.domain.com.", false},
- {{".domain.com"}, "http://.domain.com", false},
- {{"domain.com."}, "http://domain.com", true},
- {{"domain.com."}, "http://domain.com.", false},
-
- {{"domain..com"}, "http://domain.com", true},
- {{"domain.com"}, "http://domain..com", true},
- {{"domain..com"}, "http://domain..com", false},
-
- {{"~domain.com"}, nullptr, false},
- {{"~domain.com"}, "http://domain.com", true},
- {{"~ddomain.com"}, "http://domain.com", false},
- {{"~domain.com"}, "http://ddomain.com", false},
- {{"~domain.com"}, "http://sub.domain.com", true},
- {{"~sub.domain.com"}, "http://domain.com", false},
- {{"~sub.domain.com"}, "http://sub.domain.com", true},
- {{"~sub.domain.com"}, "http://a.b.c.sub.domain.com", true},
- {{"~sub.domain.com"}, "http://sub.domain.com.com", false},
-
- {{"domain1.com", "domain2.com"}, nullptr, true},
- {{"domain1.com", "domain2.com"}, "http://domain1.com", false},
- {{"domain1.com", "domain2.com"}, "http://domain2.com", false},
- {{"domain1.com", "domain2.com"}, "http://domain3.com", true},
- {{"domain1.com", "domain2.com"}, "http://not_domain1.com", true},
- {{"domain1.com", "domain2.com"}, "http://sub.domain1.com", false},
- {{"domain1.com", "domain2.com"}, "http://a.b.c.sub.domain2.com", false},
-
- {{"~domain1.com", "~domain2.com"}, "http://domain1.com", true},
- {{"~domain1.com", "~domain2.com"}, "http://domain2.com", true},
- {{"~domain1.com", "~domain2.com"}, "http://domain3.com", false},
-
- {{"domain.com", "~sub.domain.com"}, "http://domain.com", false},
- {{"domain.com", "~sub.domain.com"}, "http://sub.domain.com", true},
- {{"domain.com", "~sub.domain.com"}, "http://a.b.sub.domain.com", true},
- {{"domain.com", "~sub.domain.com"}, "http://ssub.domain.com", false},
+ {{"domain.com"}, "http://domain.com.", true},
+ {{"domain.com"}, "http://.domain.com", true},
+ {{"domain.com"}, "http://.domain.com.", true},
+ {{".domain.com"}, "http://.domain.com", true},
+ {{"domain.com."}, "http://domain.com", false},
+ {{"domain.com."}, "http://domain.com.", true},
+
+ {{"domain..com"}, "http://domain.com", false},
+ {{"domain.com"}, "http://domain..com", false},
+ {{"domain..com"}, "http://domain..com", true},
+
+ {{"~domain.com"}, nullptr, true},
+ {{"~domain.com"}, "http://domain.com", false},
+ {{"~ddomain.com"}, "http://domain.com", true},
+ {{"~domain.com"}, "http://ddomain.com", true},
+ {{"~domain.com"}, "http://sub.domain.com", false},
+ {{"~sub.domain.com"}, "http://domain.com", true},
+ {{"~sub.domain.com"}, "http://sub.domain.com", false},
+ {{"~sub.domain.com"}, "http://a.b.c.sub.domain.com", false},
+ {{"~sub.domain.com"}, "http://sub.domain.com.com", true},
+
+ {{"domain1.com", "domain2.com"}, nullptr, false},
+ {{"domain1.com", "domain2.com"}, "http://domain1.com", true},
+ {{"domain1.com", "domain2.com"}, "http://domain2.com", true},
+ {{"domain1.com", "domain2.com"}, "http://domain3.com", false},
+ {{"domain1.com", "domain2.com"}, "http://not_domain1.com", false},
+ {{"domain1.com", "domain2.com"}, "http://sub.domain1.com", true},
+ {{"domain1.com", "domain2.com"}, "http://a.b.c.sub.domain2.com", true},
+
+ {{"~domain1.com", "~domain2.com"}, "http://domain1.com", false},
+ {{"~domain1.com", "~domain2.com"}, "http://domain2.com", false},
+ {{"~domain1.com", "~domain2.com"}, "http://domain3.com", true},
+
+ {{"domain.com", "~sub.domain.com"}, "http://domain.com", true},
+ {{"domain.com", "~sub.domain.com"}, "http://sub.domain.com", false},
+ {{"domain.com", "~sub.domain.com"}, "http://a.b.sub.domain.com", false},
+ {{"domain.com", "~sub.domain.com"}, "http://ssub.domain.com", true},
{{"domain.com", "~a.domain.com", "~b.domain.com"},
"http://domain.com",
- false},
+ true},
{{"domain.com", "~a.domain.com", "~b.domain.com"},
"http://a.domain.com",
- true},
+ false},
{{"domain.com", "~a.domain.com", "~b.domain.com"},
"http://b.domain.com",
- true},
+ false},
{{"domain.com", "~a.domain.com", "b.a.domain.com"},
"http://domain.com",
- false},
+ true},
{{"domain.com", "~a.domain.com", "b.a.domain.com"},
"http://a.domain.com",
- true},
+ false},
{{"domain.com", "~a.domain.com", "b.a.domain.com"},
"http://b.a.domain.com",
- false},
+ true},
{{"domain.com", "~a.domain.com", "b.a.domain.com"},
"http://c.b.a.domain.com",
- false},
+ true},
// The following test addresses a former bug in domain list matcher. When
// "domain.com" was matched, the positive filters lookup stopped, and the
@@ -341,7 +347,7 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithDomainList) {
// be classified as non-matching, which is not correct.
{{"domain.com", "ddomain.com", "~sub.domain.com"},
"http://domain.com",
- false},
+ true},
};
for (const auto& test_case : kTestCases) {
@@ -354,13 +360,13 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithDomainList) {
ASSERT_TRUE(AddUrlRule(rule));
Finish();
- EXPECT_EQ(test_case.expect_allowed,
- ShouldAllow(kUrl, test_case.document_origin));
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(kUrl, test_case.document_origin));
Reset();
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithLongDomainList) {
+TEST_F(UrlPatternIndexTest, OneRuleWithLongDomainList) {
constexpr const char* kUrl = "http://example.com";
constexpr size_t kDomains = 200;
@@ -388,21 +394,21 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithLongDomainList) {
SCOPED_TRACE(::testing::Message() << "Iteration: " << i);
const std::string domain = "domain" + std::to_string(i) + ".com";
- EXPECT_FALSE(ShouldAllow(kUrl, "http://" + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://sub." + domain));
- EXPECT_FALSE(ShouldAllow(kUrl, "http://a.sub." + domain));
- EXPECT_FALSE(ShouldAllow(kUrl, "http://b.sub." + domain));
- EXPECT_FALSE(ShouldAllow(kUrl, "http://c.sub." + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://aa.sub." + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://ab.sub." + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://ba.sub." + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://bb.sub." + domain));
- EXPECT_FALSE(ShouldAllow(kUrl, "http://sub.c.sub." + domain));
- EXPECT_TRUE(ShouldAllow(kUrl, "http://sub.sub.c.sub." + domain));
+ EXPECT_TRUE(FindMatch(kUrl, "http://" + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://sub." + domain));
+ EXPECT_TRUE(FindMatch(kUrl, "http://a.sub." + domain));
+ EXPECT_TRUE(FindMatch(kUrl, "http://b.sub." + domain));
+ EXPECT_TRUE(FindMatch(kUrl, "http://c.sub." + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://aa.sub." + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://ab.sub." + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://ba.sub." + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://bb.sub." + domain));
+ EXPECT_TRUE(FindMatch(kUrl, "http://sub.c.sub." + domain));
+ EXPECT_FALSE(FindMatch(kUrl, "http://sub.sub.c.sub." + domain));
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithElementTypes) {
+TEST_F(UrlPatternIndexTest, OneRuleWithElementTypes) {
constexpr auto kAll = kAllElementTypes;
const struct {
const char* url_pattern;
@@ -410,28 +416,28 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithElementTypes) {
const char* url;
proto::ElementType element_type;
- bool expect_allowed;
+ bool expect_match;
} kTestCases[] = {
- {"ex.com", kAll, "http://ex.com/img.jpg", kImage, false},
- {"ex.com", kAll & ~kPopup, "http://ex.com/img", kPopup, true},
+ {"ex.com", kAll, "http://ex.com/img.jpg", kImage, true},
+ {"ex.com", kAll & ~kPopup, "http://ex.com/img", kPopup, false},
- {"ex.com", kImage, "http://ex.com/img.jpg", kImage, false},
- {"ex.com", kAll & ~kImage, "http://ex.com/img.jpg", kImage, true},
- {"ex.com", kScript, "http://ex.com/img.jpg", kImage, true},
- {"ex.com", kAll & ~kScript, "http://ex.com/img.jpg", kImage, false},
+ {"ex.com", kImage, "http://ex.com/img.jpg", kImage, true},
+ {"ex.com", kAll & ~kImage, "http://ex.com/img.jpg", kImage, false},
+ {"ex.com", kScript, "http://ex.com/img.jpg", kImage, false},
+ {"ex.com", kAll & ~kScript, "http://ex.com/img.jpg", kImage, true},
- {"ex.com", kImage | kFont, "http://ex.com/font", kFont, false},
- {"ex.com", kImage | kFont, "http://ex.com/image", kImage, false},
+ {"ex.com", kImage | kFont, "http://ex.com/font", kFont, true},
+ {"ex.com", kImage | kFont, "http://ex.com/image", kImage, true},
{"ex.com", kImage | kFont, "http://ex.com/video",
- proto::ELEMENT_TYPE_MEDIA, true},
- {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/font", kFont, true},
- {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/scr", kScript, true},
- {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/img", kImage, false},
+ proto::ELEMENT_TYPE_MEDIA, false},
+ {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/font", kFont, false},
+ {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/scr", kScript, false},
+ {"ex.com", kAll & ~kFont & ~kScript, "http://ex.com/img", kImage, true},
- {"ex.com", kAll, "http://ex.com", proto::ELEMENT_TYPE_OTHER, false},
- {"ex.com", kAll, "http://ex.com", proto::ELEMENT_TYPE_UNSPECIFIED, true},
+ {"ex.com", kAll, "http://ex.com", proto::ELEMENT_TYPE_OTHER, true},
+ {"ex.com", kAll, "http://ex.com", proto::ELEMENT_TYPE_UNSPECIFIED, false},
{"ex.com", kWebSocket, "ws://ex.com", proto::ELEMENT_TYPE_WEBSOCKET,
- false},
+ true},
};
for (const auto& test_case : kTestCases) {
@@ -447,31 +453,30 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithElementTypes) {
ASSERT_TRUE(AddUrlRule(rule));
Finish();
- EXPECT_EQ(test_case.expect_allowed,
- ShouldAllow(test_case.url, nullptr /* document_origin */,
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(test_case.url, nullptr /* document_origin_string */,
test_case.element_type));
Reset();
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithActivationTypes) {
- constexpr proto::ActivationType kNone = proto::ACTIVATION_TYPE_UNSPECIFIED;
-
+TEST_F(UrlPatternIndexTest, OneRuleWithActivationTypes) {
const struct {
const char* url_pattern;
int32_t activation_types;
const char* document_url;
proto::ActivationType activation_type;
- bool expect_disabled;
+ bool expect_match;
} 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", kGenericBlock, "http://example.com", kDocument, false},
- {"example.com", kDocument, "http://example.com", kNone, false},
- {"example.com", kGenericBlock, "http://example.com", kNone, false},
+ {"example.com", kDocument, "http://example.com", kNoActivation, false},
+ {"example.com", kGenericBlock, "http://example.com", kNoActivation,
+ false},
// Invalid GURL.
{"example.com", kDocument, "http;//example.com", kDocument, false},
@@ -485,43 +490,48 @@ TEST_F(SubresourceFilterIndexedRulesetTest, OneRuleWithActivationTypes) {
<< "; DocumentURL: " << test_case.document_url
<< "; ActivationType: " << static_cast<int>(test_case.activation_type));
- ASSERT_TRUE(AddSimpleWhitelistRule(test_case.url_pattern,
- test_case.activation_types));
+ auto rule = MakeUrlRule(UrlPattern(test_case.url_pattern, kSubstring));
+ rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
+ rule.clear_element_types();
+ rule.set_activation_types(test_case.activation_types);
+ ASSERT_TRUE(AddUrlRule(rule));
Finish();
- EXPECT_EQ(test_case.expect_disabled,
- ShouldDeactivate(test_case.document_url,
- nullptr /* parent_document_origin */,
- 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));
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(test_case.document_url,
+ nullptr /* parent_document_origin */, kNoElement,
+ test_case.activation_type));
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(test_case.document_url, "http://example.com/",
+ kNoElement, test_case.activation_type));
+ EXPECT_EQ(test_case.expect_match,
+ !!FindMatch(test_case.document_url, "http://xmpl.com/",
+ kNoElement, test_case.activation_type));
Reset();
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, RuleWithElementAndActivationTypes) {
+TEST_F(UrlPatternIndexTest, OneRuleWithElementAndActivationTypes) {
auto rule = MakeUrlRule(UrlPattern("allow.ex.com", kSubstring));
rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
+ rule.set_element_types(kSubdocument);
rule.set_activation_types(kDocument);
-
ASSERT_TRUE(AddUrlRule(rule));
- ASSERT_TRUE(AddSimpleRule("ex.com"));
Finish();
- EXPECT_FALSE(ShouldAllow("http://ex.com"));
- EXPECT_TRUE(ShouldAllow("http://allow.ex.com"));
- EXPECT_FALSE(ShouldDeactivate("http://allow.ex.com",
- nullptr /* parent_document_origin */,
- kGenericBlock));
- EXPECT_TRUE(ShouldDeactivate(
- "http://allow.ex.com", nullptr /* parent_document_origin */, kDocument));
+ EXPECT_FALSE(FindMatch("http://allow.ex.com"));
+ EXPECT_TRUE(FindMatch("http://allow.ex.com",
+ nullptr /*document_origin_string */, kSubdocument));
+
+ EXPECT_FALSE(FindMatch("http://allow.ex.com",
+ nullptr /* document_origin_string */, kNoElement,
+ kGenericBlock));
+ EXPECT_TRUE(FindMatch("http://allow.ex.com",
+ nullptr /* document_origin_string */, kNoElement,
+ kDocument));
}
-TEST_F(SubresourceFilterIndexedRulesetTest, MatchWithDisableGenericRules) {
+TEST_F(UrlPatternIndexTest, MatchWithDisableGenericRules) {
const struct {
const char* url_pattern;
std::vector<std::string> domains;
@@ -552,98 +562,46 @@ TEST_F(SubresourceFilterIndexedRulesetTest, MatchWithDisableGenericRules) {
const struct {
const char* url;
const char* document_origin;
- bool should_allow_with_disable_generic_rules;
- bool should_allow_with_enable_all_rules;
+ bool expect_match_with_enable_all_rules;
+ bool expect_match_with_disable_generic_rules;
} kTestCases[] = {
{"http://ex.com/some_text", "http://example.com", true, false},
- {"http://ex.com/some_text", "http://example1.com", false, false},
+ {"http://ex.com/some_text", "http://example1.com", true, true},
- {"http://ex.com/another_text", "http://example.com", true, true},
+ {"http://ex.com/another_text", "http://example.com", false, false},
{"http://ex.com/another_text", "http://example1.com", true, false},
{"http://ex.com/final_text", "http://example.com", true, false},
- {"http://ex.com/final_text", "http://example1.com", true, true},
- {"http://ex.com/final_text", "http://example2.com", true, true},
+ {"http://ex.com/final_text", "http://example1.com", false, false},
+ {"http://ex.com/final_text", "http://example2.com", false, false},
- {"http://ex.com/more_text", "http://example.com", false, false},
- {"http://ex.com/more_text", "http://exclude.example.com", true, true},
- {"http://ex.com/more_text", "http://example1.com", true, true},
+ {"http://ex.com/more_text", "http://example.com", true, true},
+ {"http://ex.com/more_text", "http://exclude.example.com", false, false},
+ {"http://ex.com/more_text", "http://example1.com", false, false},
- {"http://ex.com/last_text", "http://example.com", true, true},
- {"http://ex.com/last_text", "http://example1.com", false, false},
- {"http://ex.com/last_text", "http://example2.com", true, true},
- {"http://ex.com/last_text", "http://sub.example2.com", false, false},
+ {"http://ex.com/last_text", "http://example.com", false, false},
+ {"http://ex.com/last_text", "http://example1.com", true, true},
+ {"http://ex.com/last_text", "http://example2.com", false, false},
+ {"http://ex.com/last_text", "http://sub.example2.com", true, true},
};
constexpr bool kDisableGenericRules = true;
constexpr bool kEnableAllRules = false;
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(::testing::Message()
- << "URL: " << test_case.url
+ << "UrlPattern: " << test_case.url
<< "; DocumentOrigin: " << test_case.document_origin);
- EXPECT_EQ(test_case.should_allow_with_disable_generic_rules,
- ShouldAllow(test_case.url, test_case.document_origin, kOther,
- kDisableGenericRules));
- EXPECT_EQ(test_case.should_allow_with_enable_all_rules,
- ShouldAllow(test_case.url, test_case.document_origin, kOther,
- kEnableAllRules));
+ EXPECT_EQ(test_case.expect_match_with_disable_generic_rules,
+ !!FindMatch(test_case.url, test_case.document_origin, kOther,
+ kNoActivation, kDisableGenericRules));
+ EXPECT_EQ(test_case.expect_match_with_enable_all_rules,
+ !!FindMatch(test_case.url, test_case.document_origin, kOther,
+ kNoActivation, kEnableAllRules));
}
}
-TEST_F(SubresourceFilterIndexedRulesetTest, EmptyRuleset) {
- Finish();
- EXPECT_TRUE(ShouldAllow(nullptr));
- EXPECT_TRUE(ShouldAllow("http://example.com"));
- EXPECT_TRUE(ShouldAllow("http://another.example.com?param=val"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, NoRuleApplies) {
- ASSERT_TRUE(AddSimpleRule("?filter_out="));
- ASSERT_TRUE(AddSimpleRule("&filter_out="));
- Finish();
-
- EXPECT_TRUE(ShouldAllow("http://example.com"));
- EXPECT_TRUE(ShouldAllow("http://example.com?filter_not"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, SimpleBlacklist) {
- ASSERT_TRUE(AddSimpleRule("?param="));
- Finish();
-
- EXPECT_TRUE(ShouldAllow("https://example.com"));
- EXPECT_FALSE(ShouldAllow("http://example.org?param=image1"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, SimpleWhitelist) {
- ASSERT_TRUE(AddSimpleWhitelistRule("example.com/?filter_out="));
- Finish();
-
- EXPECT_TRUE(ShouldAllow("https://example.com?filter_out=true"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, SimpleBlacklistAndWhitelist) {
- ASSERT_TRUE(AddSimpleRule("?filter="));
- ASSERT_TRUE(AddSimpleWhitelistRule("whitelisted.com/?filter="));
- Finish();
-
- EXPECT_FALSE(ShouldAllow("http://blacklisted.com?filter=on"));
- EXPECT_TRUE(ShouldAllow("https://whitelisted.com?filter=on"));
- EXPECT_TRUE(ShouldAllow("https://notblacklisted.com"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, BlacklistAndActivationType) {
- ASSERT_TRUE(AddSimpleRule("example.com"));
- ASSERT_TRUE(AddSimpleWhitelistRule("example.com", 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"));
-}
-
-TEST_F(SubresourceFilterIndexedRulesetTest, RulesWithUnsupportedTypes) {
+TEST_F(UrlPatternIndexTest, RulesWithUnsupportedTypes) {
const struct {
int element_types;
int activation_types;
@@ -668,15 +626,14 @@ TEST_F(SubresourceFilterIndexedRulesetTest, RulesWithUnsupportedTypes) {
<< "; ActivationTypes: "
<< static_cast<int>(rule_data.activation_types);
}
- ASSERT_TRUE(AddSimpleRule("exmpl.com"));
+ ASSERT_TRUE(AddUrlRule(MakeUrlRule(UrlPattern("exmpl.com", kSubstring))));
Finish();
- EXPECT_TRUE(ShouldAllow("http://example.com/"));
- EXPECT_FALSE(ShouldAllow("https://exmpl.com/"));
+ EXPECT_FALSE(FindMatch("http://example.com/"));
+ EXPECT_TRUE(FindMatch("https://exmpl.com/"));
}
-TEST_F(SubresourceFilterIndexedRulesetTest,
- RulesWithSupportedAndUnsupportedTypes) {
+TEST_F(UrlPatternIndexTest, RulesWithSupportedAndUnsupportedTypes) {
const struct {
int element_types;
int activation_types;
@@ -688,10 +645,9 @@ TEST_F(SubresourceFilterIndexedRulesetTest,
for (const auto& rule_data : kRules) {
auto rule = MakeUrlRule(UrlPattern("example.com", kSubstring));
+ rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
rule.set_element_types(rule_data.element_types);
rule.set_activation_types(rule_data.activation_types);
- if (rule_data.activation_types)
- rule.set_semantics(proto::RULE_SEMANTICS_WHITELIST);
EXPECT_TRUE(AddUrlRule(rule))
<< "ElementTypes: " << static_cast<int>(rule_data.element_types)
<< "; ActivationTypes: "
@@ -699,13 +655,14 @@ TEST_F(SubresourceFilterIndexedRulesetTest,
}
Finish();
- EXPECT_FALSE(ShouldAllow("http://example.com/", nullptr, kImage));
- EXPECT_FALSE(ShouldAllow("http://example.com/", nullptr, kScript));
- EXPECT_TRUE(ShouldAllow("http://example.com/", nullptr, kPopup));
- EXPECT_TRUE(ShouldAllow("http://example.com/"));
+ EXPECT_TRUE(FindMatch("http://example.com/", nullptr, kImage));
+ EXPECT_TRUE(FindMatch("http://example.com/", nullptr, kScript));
+ EXPECT_FALSE(FindMatch("http://example.com/", nullptr, kPopup));
+ EXPECT_FALSE(FindMatch("http://example.com/"));
- EXPECT_TRUE(ShouldDeactivate("http://example.com", nullptr, kDocument));
- EXPECT_FALSE(ShouldDeactivate("http://example.com", nullptr, kGenericBlock));
+ EXPECT_TRUE(FindMatch("http://example.com", nullptr, kNoElement, kDocument));
+ EXPECT_FALSE(
+ FindMatch("http://example.com", nullptr, kNoElement, kGenericBlock));
}
} // namespace subresource_filter
« no previous file with comments | « components/subresource_filter/core/common/url_pattern_index.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698