Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/common/extensions/matcher/url_matcher_factory.h" | 5 #include "chrome/common/extensions/matcher/url_matcher_factory.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 8 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| 9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 EXPECT_EQ(1u, matcher.MatchURL(GURL("http://www.example.com:80")).size()); | 92 EXPECT_EQ(1u, matcher.MatchURL(GURL("http://www.example.com:80")).size()); |
| 93 EXPECT_EQ(1u, matcher.MatchURL(GURL("http://www.example.com:1000")).size()); | 93 EXPECT_EQ(1u, matcher.MatchURL(GURL("http://www.example.com:1000")).size()); |
| 94 // Wrong scheme. | 94 // Wrong scheme. |
| 95 EXPECT_EQ(0u, matcher.MatchURL(GURL("https://www.example.com:80")).size()); | 95 EXPECT_EQ(0u, matcher.MatchURL(GURL("https://www.example.com:80")).size()); |
| 96 // Wrong port. | 96 // Wrong port. |
| 97 EXPECT_EQ(0u, matcher.MatchURL(GURL("http://www.example.com:81")).size()); | 97 EXPECT_EQ(0u, matcher.MatchURL(GURL("http://www.example.com:81")).size()); |
| 98 // Unfulfilled host prefix. | 98 // Unfulfilled host prefix. |
| 99 EXPECT_EQ(0u, matcher.MatchURL(GURL("http://mail.example.com:81")).size()); | 99 EXPECT_EQ(0u, matcher.MatchURL(GURL("http://mail.example.com:81")).size()); |
| 100 } | 100 } |
| 101 | 101 |
| 102 // This class wraps a case sensitivity test for a single UrlFilter condition. | |
| 103 class UrlConditionCaseTest { | |
| 104 public: | |
| 105 // The condition is identified by the key |condition_key|. If that key is | |
| 106 // associated with string values, then |use_list_of_strings| should be false, | |
| 107 // if the key is associated with list-of-string values, then | |
| 108 // |use_list_of_strings| should be true. In |url| is the URL to test against. | |
| 109 UrlConditionCaseTest(const char* condition_key, | |
| 110 const bool use_list_of_strings, | |
| 111 const std::string& expected_value, | |
| 112 const std::string& incorrect_case_value, | |
| 113 const bool case_sensitive, | |
| 114 const GURL& url) | |
| 115 : condition_key_(condition_key), | |
| 116 use_list_of_strings_(use_list_of_strings), | |
| 117 expected_value_(expected_value), | |
| 118 incorrect_case_value_(incorrect_case_value), | |
| 119 case_sensitive_(case_sensitive), | |
| 120 url_(url) {} | |
| 121 | |
| 122 ~UrlConditionCaseTest() {} | |
| 123 | |
| 124 // Match the condition against |url_|. Checks via EXPECT_* macros that | |
| 125 // |expected_value_| matches always, and that |incorrect_case_value_| matches | |
| 126 // iff |case_sensitive_| is false. | |
| 127 void Test() const; | |
| 128 | |
| 129 private: | |
| 130 // Check, via EXPECT_* macros, that the the condition |condition_key_|=|value| | |
| 131 // fails against |url_| iff |should_fail| is true. This check is expensive, | |
| 132 // its value should be cached if needed multiple times. | |
| 133 void CheckCondition(const std::string& value, bool should_fail) const; | |
| 134 | |
| 135 const char* condition_key_; | |
| 136 const bool use_list_of_strings_; | |
| 137 const std::string& expected_value_; | |
| 138 const std::string& incorrect_case_value_; | |
| 139 const bool case_sensitive_; | |
| 140 const GURL& url_; | |
| 141 }; | |
|
vabr (Chromium)
2013/01/07 18:33:48
No DISALLOW_COPY_AND_ASSIGN here, because a public
battre
2013/01/07 19:13:01
How about adding a comment of the style
// Allow
vabr (Chromium)
2013/01/08 16:52:47
Done.
| |
| 142 | |
| 143 void UrlConditionCaseTest::Test() const { | |
| 144 CheckCondition(expected_value_, false); | |
| 145 CheckCondition(incorrect_case_value_, case_sensitive_); | |
| 146 } | |
| 147 | |
| 148 void UrlConditionCaseTest::CheckCondition(const std::string& value, | |
| 149 bool should_fail) const { | |
| 150 DictionaryValue condition; | |
| 151 if (use_list_of_strings_) { | |
| 152 ListValue* list = new ListValue(); | |
| 153 list->Append(Value::CreateStringValue(value)); | |
| 154 condition.SetWithoutPathExpansion(condition_key_, list); | |
| 155 } else { | |
| 156 condition.SetStringWithoutPathExpansion(condition_key_, value); | |
| 157 } | |
| 158 | |
| 159 URLMatcher matcher; | |
| 160 std::string error; | |
| 161 scoped_refptr<URLMatcherConditionSet> result; | |
| 162 | |
| 163 error.clear(); | |
|
battre
2013/01/07 19:13:01
nit: I would remove this line.
vabr (Chromium)
2013/01/08 16:52:47
Done.
| |
| 164 result = URLMatcherFactory::CreateFromURLFilterDictionary( | |
| 165 matcher.condition_factory(), &condition, 1, &error); | |
| 166 EXPECT_EQ("", error); | |
| 167 ASSERT_TRUE(result.get()); | |
| 168 | |
| 169 URLMatcherConditionSet::Vector conditions; | |
| 170 conditions.push_back(result); | |
| 171 matcher.AddConditionSets(conditions); | |
| 172 EXPECT_EQ((should_fail ? 0u : 1u), matcher.MatchURL(url_).size()) | |
| 173 << "while matching condition " << condition_key_ << " with value " | |
| 174 << value << " against url " << url_; | |
| 175 } | |
| 176 | |
| 177 // This tests that the UrlFilter handles case sensitivity on various parts of | |
| 178 // URLs correctly. | |
| 179 TEST(URLMatcherFactoryTest, CaseSensitivity) { | |
| 180 const std::string kScheme("https"); | |
| 181 const std::string kSchemeUpper("HTTPS"); | |
| 182 const std::string kHost("www.example.com"); | |
| 183 const std::string kHostUpper("WWW.EXAMPLE.COM"); | |
| 184 const std::string kPath("/path"); | |
| 185 const std::string kPathUpper("/PATH"); | |
| 186 const std::string kQuery("?option=value&A=B"); | |
| 187 const std::string kQueryUpper("?OPTION=VALUE&A=B"); | |
| 188 const std::string kUrl(kScheme + "://" + kHost + ":1234" + kPath + kQuery); | |
| 189 const std::string kUrlUpper( | |
| 190 kSchemeUpper + "://" + kHostUpper + ":1234" + kPathUpper + kQueryUpper); | |
| 191 const GURL url(kUrl); | |
| 192 // Note: according to RFC 3986, and RFC 1034, schema and host, respectively | |
| 193 // should be case insensitive. See crbug.com/160702, comments 6 and 7, for why | |
| 194 // we still require them to be case sensitive in UrlFilter. | |
| 195 const bool kIsSchemeCaseSensitive = true; | |
| 196 const bool kIsHostCaseSensitive = true; | |
| 197 const bool kIsPathCaseSensitive = true; | |
| 198 const bool kIsQueryCaseSensitive = true; | |
| 199 const bool kIsUrlCaseSensitive = kIsSchemeCaseSensitive || | |
| 200 kIsHostCaseSensitive || kIsPathCaseSensitive || kIsQueryCaseSensitive; | |
| 201 | |
| 202 const UrlConditionCaseTest case_tests[] = { | |
| 203 UrlConditionCaseTest(keys::kSchemesKey, true, kScheme, kSchemeUpper, | |
| 204 kIsSchemeCaseSensitive, url), | |
| 205 UrlConditionCaseTest(keys::kHostContainsKey, false, kHost, kHostUpper, | |
| 206 kIsHostCaseSensitive, url), | |
| 207 UrlConditionCaseTest(keys::kHostEqualsKey, false, kHost, kHostUpper, | |
| 208 kIsHostCaseSensitive, url), | |
| 209 UrlConditionCaseTest(keys::kHostPrefixKey, false, kHost, kHostUpper, | |
| 210 kIsHostCaseSensitive, url), | |
| 211 UrlConditionCaseTest(keys::kHostSuffixKey, false, kHost, kHostUpper, | |
| 212 kIsHostCaseSensitive, url), | |
| 213 UrlConditionCaseTest(keys::kPathContainsKey, false, kPath, kPathUpper, | |
| 214 kIsPathCaseSensitive, url), | |
| 215 UrlConditionCaseTest(keys::kPathEqualsKey, false, kPath, kPathUpper, | |
| 216 kIsPathCaseSensitive, url), | |
| 217 UrlConditionCaseTest(keys::kPathPrefixKey, false, kPath, kPathUpper, | |
| 218 kIsPathCaseSensitive, url), | |
| 219 UrlConditionCaseTest(keys::kPathSuffixKey, false, kPath, kPathUpper, | |
| 220 kIsPathCaseSensitive, url), | |
| 221 UrlConditionCaseTest(keys::kQueryContainsKey, false, kQuery, kQueryUpper, | |
| 222 kIsQueryCaseSensitive, url), | |
| 223 UrlConditionCaseTest(keys::kQueryEqualsKey, false, kQuery, kQueryUpper, | |
| 224 kIsQueryCaseSensitive, url), | |
| 225 UrlConditionCaseTest(keys::kQueryPrefixKey, false, kQuery, kQueryUpper, | |
| 226 kIsQueryCaseSensitive, url), | |
| 227 UrlConditionCaseTest(keys::kQuerySuffixKey, false, kQuery, kQueryUpper, | |
| 228 kIsQueryCaseSensitive, url), | |
| 229 // Excluding kURLMatchesKey because case sensitivity can be specified in the | |
| 230 // RE2 expression. | |
| 231 UrlConditionCaseTest(keys::kURLContainsKey, false, kUrl, kUrlUpper, | |
| 232 kIsUrlCaseSensitive, url), | |
| 233 UrlConditionCaseTest(keys::kURLEqualsKey, false, kUrl, kUrlUpper, | |
| 234 kIsUrlCaseSensitive, url), | |
| 235 UrlConditionCaseTest(keys::kURLPrefixKey, false, kUrl, kUrlUpper, | |
| 236 kIsUrlCaseSensitive, url), | |
| 237 UrlConditionCaseTest(keys::kURLSuffixKey, false, kUrl, kUrlUpper, | |
| 238 kIsUrlCaseSensitive, url), | |
| 239 }; | |
| 240 | |
| 241 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(case_tests); ++i) | |
|
battre
2013/01/07 19:13:01
Tip: You may add a line like the following:
SCOPED
vabr (Chromium)
2013/01/08 16:52:47
TIL, thanks!
Done.
On 2013/01/07 19:13:01, battre
| |
| 242 case_tests[i].Test(); | |
| 243 } | |
| 244 | |
| 102 } // namespace extensions | 245 } // namespace extensions |
| OLD | NEW |