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

Unified Diff: chrome/browser/extensions/api/declarative/url_matcher_unittest.cc

Issue 9390018: Implementation of a Matching strategy for URLs in the Declarative WebRequest API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactored for memory improvements Created 8 years, 10 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: chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
diff --git a/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc b/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..23d562fa70be94d1e8c6bdf4be62fa3e15b04bb6
--- /dev/null
+++ b/chrome/browser/extensions/api/declarative/url_matcher_unittest.cc
@@ -0,0 +1,355 @@
+// Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative/url_matcher.h"
+
+#include "base/string_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace extensions {
+
+TEST(URLMatcherConditionTest, Constructor) {
+ SubstringPattern pattern("example.com", 1);
+ URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, &pattern);
+ EXPECT_EQ(URLMatcherCondition::HOST_SUFFIX, m1.criterion());
+ EXPECT_EQ(&pattern, m1.substring_pattern());
+}
+
+TEST(URLMatcherConditionTest, IsFullUrlCondition) {
+ SubstringPattern pattern("example.com", 1);
+ EXPECT_FALSE(URLMatcherCondition(URLMatcherCondition::HOST_SUFFIX,
+ &pattern).IsFullUrlCondition());
+
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::HOST_CONTAINS,
+ &pattern).IsFullUrlCondition());
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::PATH_CONTAINS,
+ &pattern).IsFullUrlCondition());
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::QUERY_CONTAINS,
+ &pattern).IsFullUrlCondition());
+
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_PREFIX,
+ &pattern).IsFullUrlCondition());
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_SUFFIX,
+ &pattern).IsFullUrlCondition());
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_CONTAINS,
+ &pattern).IsFullUrlCondition());
+ EXPECT_TRUE(URLMatcherCondition(URLMatcherCondition::URL_EQUALS,
+ &pattern).IsFullUrlCondition());
+}
+
+TEST(URLMatcherConditionTest, SubstringMatching) {
+ // TODO(battre): Re-implement this.
+ /*
+ URLMatcherConditionFactory condition_factory;
+
+ // Check that substring_pattern is filled.
+ URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com");
+ EXPECT_EQ(condition_factory.CreateHostSuffixPattern("example.com"),
+ m1.substring_pattern());
+
+ // Check that patterns are recycled.
+ URLMatcherCondition m2(URLMatcherCondition::HOST_SUFFIX, "example.com");
+ m2.BuildSubstringPattern(&condition_factory);
+ EXPECT_EQ(m1.substring_pattern(), m1.substring_pattern());
+
+ // Check that SubstringPattern::ID are handled correctly.
+ GURL url("http://www.example.com/index.html");
+
+ std::set<SubstringPattern::ID> not_matching;
+ not_matching.insert(m1.substring_pattern().id() + 1);
+ EXPECT_FALSE(m1.IsMatch(not_matching, url));
+
+ std::set<SubstringPattern::ID> matching;
+ matching.insert(m1.substring_pattern().id());
+ EXPECT_TRUE(m1.IsMatch(matching, url));
+
+ // Check that URLs are tested correctly for containment.
+ URLMatcherCondition m3(URLMatcherCondition::HOST_CONTAINS, "example");
+ m3.BuildSubstringPattern(&condition_factory);
+ matching.insert(m3.substring_pattern().id());
+ EXPECT_TRUE(m3.IsFullUrlCondition());
+ EXPECT_FALSE(m3.IsMatch(matching, GURL("http://www.foo.com/example")));
+ EXPECT_TRUE(m3.IsMatch(matching, GURL("http://www.example.com/foo")));
+ */
+}
+
+
+namespace {
+
+bool Matches(scoped_ptr<URLMatcherCondition> condition, std::string text) {
+ return text.find(condition->substring_pattern()->pattern()) !=
+ std::string::npos;
+}
+
+} // namespace
+
+TEST(URLMatcherConditionFactoryTest, GURLCharacterSet) {
+ // GURL guarantees that neither domain, nor path, nor query may contain
+ // non ASCII-7 characters. We test this here, because a change to this
+ // guarantee breaks this implementation horribly.
+ GURL url("http://www.föö.com/föö?föö#föö");
+ EXPECT_TRUE(IsStringASCII(url.host()));
+ EXPECT_TRUE(IsStringASCII(url.path()));
+ EXPECT_TRUE(IsStringASCII(url.query()));
+ EXPECT_FALSE(IsStringASCII(url.ref()));
+}
+
+// Basic tests of SubstringPattern objects
+TEST(URLMatcherConditionFactoryTest, TestSingletonProperty) {
+ // TODO(battre): Re-implement this.
+ /*URLMatcherConditionFactory matcher;
+ SubstringPattern p1 = matcher.CreateHostEqualsCondition("www.google.com");
+ SubstringPattern p2 = matcher.CreateHostEqualsCondition("www.google.com");
+ EXPECT_EQ(p1, p2);
+ SubstringPattern p3 = matcher.CreateHostEqualsCondition("www.google.de");
+ EXPECT_NE(p2.id(), p3.id());
+ EXPECT_NE(p2.pattern(), p3.pattern());*/
+}
+
+TEST(URLMatcherConditionFactoryTest, TestComponentSearches) {
+ GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8"
+ "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
+ URLMatcherConditionFactory matcher;
+ std::string url = matcher.CanonlicalizeURLForComponentSearches(gurl);
+
+ // Test host component.
+ EXPECT_TRUE(Matches(matcher.CreateHostPrefixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateHostPrefixCondition("www.goog"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateHostPrefixCondition("www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateHostPrefixCondition(".www.google.com"), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostPrefixCondition("google.com"), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreateHostPrefixCondition("www.google.com/"), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostPrefixCondition("webhp"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition("com"), url));
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixCondition(".com"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateHostSuffixCondition("www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateHostSuffixCondition(".www.google.com"), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostSuffixCondition("www"), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreateHostSuffixCondition("www.google.com/"), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostSuffixCondition("webhp"), url));
+
+ EXPECT_FALSE(Matches(matcher.CreateHostEqualsCondition(""), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostEqualsCondition("www"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateHostEqualsCondition("www.google.com"), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreateHostEqualsCondition("www.google.com/"), url));
+
+
+ // Test path component.
+ EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition("/web"), url));
+ EXPECT_TRUE(Matches(matcher.CreatePathPrefixCondition("/webhp"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("webhp"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("/webhp?"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition("webhp"), url));
+ EXPECT_TRUE(Matches(matcher.CreatePathSuffixCondition("/webhp"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathSuffixCondition("/web"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathSuffixCondition("/webhp?"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreatePathEqualsCondition("/webhp"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathEqualsCondition("webhp"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathEqualsCondition("/webhp?"), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreatePathEqualsCondition("www.google.com"), url));
+
+
+ // Test query component.
+ EXPECT_TRUE(Matches(matcher.CreateQueryPrefixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateQueryPrefixCondition("?sourceid"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("sourceid"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateQuerySuffixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateQuerySuffixCondition("ion=1"), url));
+ EXPECT_FALSE(Matches(matcher.CreatePathPrefixCondition("?sourceid"), url));
+ EXPECT_FALSE(Matches(matcher.CreateQuerySuffixCondition("www"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateQueryEqualsCondition(
+ "?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
+ EXPECT_FALSE(Matches(matcher.CreateQueryEqualsCondition(
+ "sourceid=chrome-instant&ie=UTF-8&ion="), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreateQueryEqualsCondition("www.google.com"), url));
+
+
+ // Test adjacent components
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition(
+ "google.com", "/webhp"), url));
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition(
+ "", "/webhp"), url));
+ EXPECT_TRUE(Matches(matcher.CreateHostSuffixPathPrefixCondition(
+ "google.com", ""), url));
+ EXPECT_FALSE(Matches(matcher.CreateHostSuffixPathPrefixCondition(
+ "www", ""), url));
+}
+
+TEST(URLMatcherConditionFactoryTest, TestFullSearches) {
+ GURL gurl("https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8"
+ "&ion=1#hl=en&output=search&sclient=psy-ab&q=chrome%20is%20awesome");
+ URLMatcherConditionFactory matcher;
+ std::string url = matcher.CanonlicalizeURLForFullSearches(gurl);
+
+ EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition("www.goog"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLPrefixCondition("www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateURLPrefixCondition(".www.google.com"), url));
+ EXPECT_TRUE(
+ Matches(matcher.CreateURLPrefixCondition("www.google.com/"), url));
+ EXPECT_FALSE(Matches(matcher.CreateURLPrefixCondition("webhp"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateURLSuffixCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLSuffixCondition("ion=1"), url));
+ EXPECT_FALSE(Matches(matcher.CreateURLSuffixCondition("www"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition(""), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("www.goog"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition(".www.goog"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("webhp"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("?"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("sourceid"), url));
+ EXPECT_TRUE(Matches(matcher.CreateURLContainsCondition("ion=1"), url));
+ EXPECT_FALSE(Matches(matcher.CreateURLContainsCondition("foobar"), url));
+ EXPECT_FALSE(Matches(matcher.CreateURLContainsCondition("search"), url));
+
+ EXPECT_TRUE(Matches(matcher.CreateURLEqualsCondition(
+ "www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8&ion=1"), url));
+ EXPECT_FALSE(
+ Matches(matcher.CreateURLEqualsCondition("www.google.com"), url));
+}
+
+/*
+// TODO(battre): Re-implement this.
+TEST(URLMatcherConditionSetTest, Constructors) {
+ URLMatcherConditionFactory condition_factory;
+ URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com");
+ m1.BuildSubstringPattern(&condition_factory);
+ URLMatcherCondition m2(URLMatcherCondition::PATH_CONTAINS, "foo");
+ m2.BuildSubstringPattern(&condition_factory);
+
+ std::vector<URLMatcherCondition> conditions;
+ conditions.push_back(m1);
+ conditions.push_back(m2);
+
+ URLMatcherConditionSet condition_set(1, conditions);
+ EXPECT_EQ(1, condition_set.id());
+ EXPECT_EQ(2u, condition_set.conditions().size());
+
+ std::vector<URLMatcherCondition> other_conditions;
+ other_conditions.push_back(m1);
+ URLMatcherConditionSet condition_set2(2, other_conditions);
+ condition_set2 = condition_set;
+ EXPECT_EQ(1, condition_set2.id());
+ EXPECT_EQ(2u, condition_set2.conditions().size());
+
+ URLMatcherConditionSet condition_set3(condition_set);
+ EXPECT_EQ(1, condition_set2.id());
+ EXPECT_EQ(2u, condition_set2.conditions().size());
+}
+
+TEST(URLMatcherConditionSetTest, Matching) {
+ GURL url1("http://www.example.com/foo?bar=1");
+ GURL url2("http://foo.example.com/index.html");
+
+ URLMatcherConditionFactory condition_factory;
+ URLMatcherCondition m1(URLMatcherCondition::HOST_SUFFIX, "example.com");
+ m1.BuildSubstringPattern(&condition_factory);
+ URLMatcherCondition m2(URLMatcherCondition::PATH_CONTAINS, "foo");
+ m2.BuildSubstringPattern(&condition_factory);
+
+ std::vector<URLMatcherCondition> conditions;
+ conditions.push_back(m1);
+ conditions.push_back(m2);
+
+ URLMatcherConditionSet condition_set(1, conditions);
+ EXPECT_EQ(1, condition_set.id());
+ EXPECT_EQ(2u, condition_set.conditions().size());
+
+ std::set<SubstringPattern::ID> matching_substring_patterns;
+ matching_substring_patterns.insert(m1.substring_pattern().id());
+ EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url1));
+
+ matching_substring_patterns.insert(m2.substring_pattern().id());
+ EXPECT_TRUE(condition_set.IsMatch(matching_substring_patterns, url1));
+ EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url2));
+}
+*/
+
+TEST(URLMatcherTest, FullTest) {
+ GURL url1("http://www.example.com/foo?bar=1");
+ GURL url2("http://foo.example.com/index.html");
+
+ URLMatcher matcher;
+ URLMatcherConditionFactory* factory = matcher.condition_factory();
+
+ // First insert.
+ scoped_ptr<URLMatcherConditionSet::Conditions> conditions1(
+ new URLMatcherConditionSet::Conditions());
+ conditions1->push_back(
+ factory->CreateHostSuffixCondition("example.com").release());
+ conditions1->push_back(
+ factory->CreatePathContainsCondition("foo").release());
+
+ const int kConditionSetId1 = 1;
+ scoped_ptr<ScopedVector<const URLMatcherConditionSet> > insert1(
+ new ScopedVector<const URLMatcherConditionSet>());
+ insert1->push_back(
+ new URLMatcherConditionSet(kConditionSetId1, conditions1.Pass()));
+ matcher.AddConditionSets(insert1.Pass());
+ EXPECT_EQ(1u, matcher.MatchUrl(url1).size());
+ EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
+
+ // Second insert.
+ scoped_ptr<URLMatcherConditionSet::Conditions> conditions2(
+ new URLMatcherConditionSet::Conditions());
+ conditions2->push_back(
+ factory->CreateHostSuffixCondition("example.com").release());
+
+ const int kConditionSetId2 = 2;
+ scoped_ptr<ScopedVector<const URLMatcherConditionSet> > insert2(
+ new ScopedVector<const URLMatcherConditionSet>());
+ insert2->push_back(
+ new URLMatcherConditionSet(kConditionSetId2, conditions2.Pass()));
+ matcher.AddConditionSets(insert2.Pass());
+ EXPECT_EQ(2u, matcher.MatchUrl(url1).size());
+ EXPECT_EQ(1u, matcher.MatchUrl(url2).size());
+
+ // This should be the cached singleton.
+ int patternId1 = factory->CreateHostSuffixCondition(
+ "example.com")->substring_pattern()->id();
+
+ // Removal of last insert.
+ std::vector<URLMatcherConditionSet::ID> remove2;
+ remove2.push_back(kConditionSetId2);
+ matcher.RemoveConditionSets(remove2);
+ EXPECT_EQ(1u, matcher.MatchUrl(url1).size());
+ EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
+
+ // Removal of first insert.
+ std::vector<URLMatcherConditionSet::ID> remove1;
+ remove1.push_back(kConditionSetId1);
+ matcher.RemoveConditionSets(remove1);
+ EXPECT_EQ(0u, matcher.MatchUrl(url1).size());
+ EXPECT_EQ(0u, matcher.MatchUrl(url2).size());
+
+ // The cached singleton in matcher.condition_factory_ should be destroyed to
+ // free memory.
+ int patternId2 = factory->CreateHostSuffixCondition(
+ "example.com")->substring_pattern()->id();
+ // If patternId1 and patternId2 are different that indicates that
+ // matcher.condition_factory_ does not leak memory.
+ EXPECT_NE(patternId1, patternId2);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698