| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/declarative/url_matcher.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace extensions { |
| 10 |
| 11 TEST(UrlMatcherConditionTest, Constructors) { |
| 12 UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com"); |
| 13 EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m1.criterion()); |
| 14 EXPECT_EQ("example.com", m1.value()); |
| 15 EXPECT_EQ(SubstringPattern(), m1.substring_pattern()); |
| 16 |
| 17 UrlMatcherCondition m2(UrlMatcherCondition::HOST_PREFIX, "foo.com"); |
| 18 m2 = m1; |
| 19 EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m2.criterion()); |
| 20 EXPECT_EQ("example.com", m2.value()); |
| 21 EXPECT_EQ(SubstringPattern(), m2.substring_pattern()); |
| 22 |
| 23 UrlMatcherCondition m3(m2); |
| 24 EXPECT_EQ(UrlMatcherCondition::HOST_SUFFIX, m3.criterion()); |
| 25 EXPECT_EQ("example.com", m3.value()); |
| 26 EXPECT_EQ(SubstringPattern(), m3.substring_pattern()); |
| 27 } |
| 28 |
| 29 TEST(UrlMatcherConditionTest, IsFullUrlCondition) { |
| 30 EXPECT_FALSE(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX, |
| 31 "example.com").IsFullUrlCondition()); |
| 32 |
| 33 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::HOST_CONTAINS, |
| 34 "example.com").IsFullUrlCondition()); |
| 35 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::PATH_CONTAINS, |
| 36 "example.com").IsFullUrlCondition()); |
| 37 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::QUERY_CONTAINS, |
| 38 "example.com").IsFullUrlCondition()); |
| 39 |
| 40 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_PREFIX, |
| 41 "example.com").IsFullUrlCondition()); |
| 42 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_SUFFIX, |
| 43 "example.com").IsFullUrlCondition()); |
| 44 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_CONTAINS, |
| 45 "example.com").IsFullUrlCondition()); |
| 46 EXPECT_TRUE(UrlMatcherCondition(UrlMatcherCondition::URL_EQUALS, |
| 47 "example.com").IsFullUrlCondition()); |
| 48 } |
| 49 |
| 50 TEST(UrlMatcherConditionTest, SubstringMatching) { |
| 51 UrlComponentPatterns url_component_patterns; |
| 52 |
| 53 // Check that substring_pattern is filled. |
| 54 UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com"); |
| 55 m1.BuildSubstringPattern(&url_component_patterns); |
| 56 EXPECT_EQ(url_component_patterns.CreateHostSuffixPattern("example.com"), |
| 57 m1.substring_pattern()); |
| 58 |
| 59 // Check that patterns are recycled. |
| 60 UrlMatcherCondition m2(UrlMatcherCondition::HOST_SUFFIX, "example.com"); |
| 61 m2.BuildSubstringPattern(&url_component_patterns); |
| 62 EXPECT_EQ(m1.substring_pattern(), m1.substring_pattern()); |
| 63 |
| 64 // Check that SubstringPattern::ID are handled correctly. |
| 65 GURL url("http://www.example.com/index.html"); |
| 66 |
| 67 std::set<SubstringPattern::ID> not_matching; |
| 68 not_matching.insert(m1.substring_pattern().id() + 1); |
| 69 EXPECT_FALSE(m1.IsMatch(not_matching, url)); |
| 70 |
| 71 std::set<SubstringPattern::ID> matching; |
| 72 matching.insert(m1.substring_pattern().id()); |
| 73 EXPECT_TRUE(m1.IsMatch(matching, url)); |
| 74 |
| 75 // Check that URLs are tested correctly for containment. |
| 76 UrlMatcherCondition m3(UrlMatcherCondition::HOST_CONTAINS, "example"); |
| 77 m3.BuildSubstringPattern(&url_component_patterns); |
| 78 matching.insert(m3.substring_pattern().id()); |
| 79 EXPECT_TRUE(m3.IsFullUrlCondition()); |
| 80 EXPECT_FALSE(m3.IsMatch(matching, GURL("http://www.foo.com/example"))); |
| 81 EXPECT_TRUE(m3.IsMatch(matching, GURL("http://www.example.com/foo"))); |
| 82 } |
| 83 |
| 84 TEST(UrlMatcherConditionSetTest, Constructors) { |
| 85 UrlComponentPatterns url_component_patterns; |
| 86 UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com"); |
| 87 m1.BuildSubstringPattern(&url_component_patterns); |
| 88 UrlMatcherCondition m2(UrlMatcherCondition::PATH_CONTAINS, "foo"); |
| 89 m2.BuildSubstringPattern(&url_component_patterns); |
| 90 |
| 91 std::vector<UrlMatcherCondition> conditions; |
| 92 conditions.push_back(m1); |
| 93 conditions.push_back(m2); |
| 94 |
| 95 UrlMatcherConditionSet condition_set(1, conditions); |
| 96 EXPECT_EQ(1, condition_set.id()); |
| 97 EXPECT_EQ(2u, condition_set.conditions().size()); |
| 98 |
| 99 std::vector<UrlMatcherCondition> other_conditions; |
| 100 other_conditions.push_back(m1); |
| 101 UrlMatcherConditionSet condition_set2(2, other_conditions); |
| 102 condition_set2 = condition_set; |
| 103 EXPECT_EQ(1, condition_set2.id()); |
| 104 EXPECT_EQ(2u, condition_set2.conditions().size()); |
| 105 |
| 106 UrlMatcherConditionSet condition_set3(condition_set); |
| 107 EXPECT_EQ(1, condition_set2.id()); |
| 108 EXPECT_EQ(2u, condition_set2.conditions().size()); |
| 109 } |
| 110 |
| 111 TEST(UrlMatcherConditionSetTest, Matching) { |
| 112 GURL url1("http://www.example.com/foo?bar=1"); |
| 113 GURL url2("http://foo.example.com/index.html"); |
| 114 |
| 115 UrlComponentPatterns url_component_patterns; |
| 116 UrlMatcherCondition m1(UrlMatcherCondition::HOST_SUFFIX, "example.com"); |
| 117 m1.BuildSubstringPattern(&url_component_patterns); |
| 118 UrlMatcherCondition m2(UrlMatcherCondition::PATH_CONTAINS, "foo"); |
| 119 m2.BuildSubstringPattern(&url_component_patterns); |
| 120 |
| 121 std::vector<UrlMatcherCondition> conditions; |
| 122 conditions.push_back(m1); |
| 123 conditions.push_back(m2); |
| 124 |
| 125 UrlMatcherConditionSet condition_set(1, conditions); |
| 126 EXPECT_EQ(1, condition_set.id()); |
| 127 EXPECT_EQ(2u, condition_set.conditions().size()); |
| 128 |
| 129 std::set<SubstringPattern::ID> matching_substring_patterns; |
| 130 matching_substring_patterns.insert(m1.substring_pattern().id()); |
| 131 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url1)); |
| 132 |
| 133 matching_substring_patterns.insert(m2.substring_pattern().id()); |
| 134 EXPECT_TRUE(condition_set.IsMatch(matching_substring_patterns, url1)); |
| 135 EXPECT_FALSE(condition_set.IsMatch(matching_substring_patterns, url2)); |
| 136 } |
| 137 |
| 138 TEST(UrlMatcherTest, FullTest) { |
| 139 GURL url1("http://www.example.com/foo?bar=1"); |
| 140 GURL url2("http://foo.example.com/index.html"); |
| 141 |
| 142 UrlMatcher matcher; |
| 143 |
| 144 // First insert. |
| 145 std::vector<UrlMatcherCondition> conditions1; |
| 146 conditions1.push_back(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX, |
| 147 "example.com")); |
| 148 conditions1.push_back(UrlMatcherCondition(UrlMatcherCondition::PATH_CONTAINS, |
| 149 "foo")); |
| 150 UrlMatcherConditionSet condition_set1(1, conditions1); |
| 151 std::vector<UrlMatcherConditionSet> insert1; |
| 152 insert1.push_back(condition_set1); |
| 153 matcher.AddConditionSets(insert1); |
| 154 EXPECT_EQ(1u, matcher.MatchUrl(url1).size()); |
| 155 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 156 |
| 157 // Second insert. |
| 158 std::vector<UrlMatcherCondition> conditions2; |
| 159 conditions2.push_back(UrlMatcherCondition(UrlMatcherCondition::HOST_SUFFIX, |
| 160 "example.com")); |
| 161 UrlMatcherConditionSet condition_set2(2, conditions2); |
| 162 std::vector<UrlMatcherConditionSet> insert2; |
| 163 insert2.push_back(condition_set2); |
| 164 matcher.AddConditionSets(insert2); |
| 165 EXPECT_EQ(2u, matcher.MatchUrl(url1).size()); |
| 166 EXPECT_EQ(1u, matcher.MatchUrl(url2).size()); |
| 167 |
| 168 // This should be the cached singleton in matcher.url_component_patterns_. |
| 169 SubstringPattern pattern1 = |
| 170 matcher.url_component_patterns_.CreateHostSuffixPattern("example.com"); |
| 171 |
| 172 // Removal of last insert. |
| 173 std::vector<UrlMatcherConditionSet::ID> remove2; |
| 174 remove2.push_back(condition_set2.id()); |
| 175 matcher.RemoveConditionSets(remove2); |
| 176 EXPECT_EQ(1u, matcher.MatchUrl(url1).size()); |
| 177 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 178 |
| 179 // Removal of first insert. |
| 180 std::vector<UrlMatcherConditionSet::ID> remove1; |
| 181 remove1.push_back(condition_set1.id()); |
| 182 matcher.RemoveConditionSets(remove1); |
| 183 EXPECT_EQ(0u, matcher.MatchUrl(url1).size()); |
| 184 EXPECT_EQ(0u, matcher.MatchUrl(url2).size()); |
| 185 |
| 186 // The cached singleton in matcher.url_component_patterns_ should be |
| 187 // destroyed to free memory. |
| 188 SubstringPattern pattern2 = |
| 189 matcher.url_component_patterns_.CreateHostSuffixPattern("example.com"); |
| 190 // If pattern1 and pattern2 are different that indicates that |
| 191 // matcher.url_component_patterns_ does not leak memory. |
| 192 EXPECT_NE(pattern1, pattern2); |
| 193 } |
| 194 |
| 195 } // namespace extensions |
| OLD | NEW |