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/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" | 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condit ion.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
| 22 | 22 |
| 23 TEST(WebRequestConditionTest, CreateCondition) { | 23 TEST(WebRequestConditionTest, CreateCondition) { |
| 24 // Necessary for TestURLRequest. | 24 // Necessary for TestURLRequest. |
| 25 MessageLoop message_loop(MessageLoop::TYPE_IO); | 25 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 26 URLMatcher matcher; | 26 URLMatcher matcher; |
| 27 | 27 |
| 28 std::string error; | 28 std::string error; |
| 29 scoped_ptr<WebRequestCondition> result; | 29 scoped_ptr<WebRequestCondition> result; |
| 30 | 30 |
| 31 | |
| 32 // Test wrong condition name passed. | 31 // Test wrong condition name passed. |
| 33 error.clear(); | 32 error.clear(); |
| 34 result = WebRequestCondition::Create( | 33 result = WebRequestCondition::Create( |
| 35 matcher.condition_factory(), | 34 matcher.condition_factory(), |
| 36 *base::test::ParseJson( | 35 *base::test::ParseJson( |
| 37 "{ \"invalid\": \"foobar\", \n" | 36 "{ \"invalid\": \"foobar\", \n" |
| 38 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 37 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 39 "}"), | 38 "}"), |
| 40 &error); | 39 &error); |
| 41 EXPECT_FALSE(error.empty()); | 40 EXPECT_FALSE(error.empty()); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 55 EXPECT_FALSE(result.get()); | 54 EXPECT_FALSE(result.get()); |
| 56 | 55 |
| 57 // Test success (can we support multiple criteria?) | 56 // Test success (can we support multiple criteria?) |
| 58 error.clear(); | 57 error.clear(); |
| 59 result = WebRequestCondition::Create( | 58 result = WebRequestCondition::Create( |
| 60 matcher.condition_factory(), | 59 matcher.condition_factory(), |
| 61 *base::test::ParseJson( | 60 *base::test::ParseJson( |
| 62 "{ \n" | 61 "{ \n" |
| 63 " \"resourceType\": [\"main_frame\"], \n" | 62 " \"resourceType\": [\"main_frame\"], \n" |
| 64 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 63 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
| 64 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" | |
|
Jeffrey Yasskin
2013/01/24 00:39:17
Instead of extending this test, I'd add a separate
vabr (Chromium)
2013/01/24 18:24:08
Done.
| |
| 65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 66 "}"), | 66 "}"), |
| 67 &error); | 67 &error); |
| 68 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
| 69 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
| 70 | 70 |
| 71 URLMatcherConditionSet::Vector url_matcher_condition_set; | 71 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 73 matcher.AddConditionSets(url_matcher_condition_set); | 73 matcher.AddConditionSets(url_matcher_condition_set); |
| 74 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
| 75 | 74 |
| 76 net::TestURLRequestContext context; | 75 net::TestURLRequestContext context; |
| 77 GURL http_url("http://www.example.com"); | 76 GURL http_url("http://www.example.com"); |
| 77 GURL first_party_url("http://fpfc.example.com"); | |
| 78 net::TestURLRequest match_request(http_url, NULL, &context); | 78 net::TestURLRequest match_request(http_url, NULL, &context); |
| 79 url_match_ids = matcher.MatchURL(http_url); | 79 DeclarativeWebRequestData request_data(&match_request, ON_BEFORE_REQUEST); |
| 80 EXPECT_EQ(1u, url_match_ids.size()); | 80 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 81 EXPECT_EQ(1u, request_data.url_match_ids.size()); | |
| 82 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | |
| 83 EXPECT_EQ(1u, request_data.url_match_ids.size()); | |
| 81 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 84 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
| 82 ResourceType::MAIN_FRAME, NULL, -1, -1); | 85 ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 83 EXPECT_TRUE(result->IsFulfilled( | 86 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 84 url_match_ids, | |
| 85 DeclarativeWebRequestData(&match_request, ON_BEFORE_REQUEST))); | |
| 86 | 87 |
| 87 GURL https_url("https://www.example.com"); | 88 GURL https_url("https://www.example.com"); |
| 88 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); | 89 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); |
| 89 url_match_ids = matcher.MatchURL(https_url); | 90 request_data.request = &wrong_resource_type; |
| 91 request_data.url_match_ids = matcher.MatchURL(http_url); | |
| 90 // Make sure IsFulfilled does not fail because of URL matching. | 92 // Make sure IsFulfilled does not fail because of URL matching. |
| 91 EXPECT_EQ(1u, url_match_ids.size()); | 93 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 92 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 94 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 93 ResourceType::SUB_FRAME, NULL, -1, -1); | 95 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 94 EXPECT_FALSE(result->IsFulfilled( | 96 EXPECT_FALSE(result->IsFulfilled(request_data)); |
| 95 url_match_ids, | |
| 96 DeclarativeWebRequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Conditions without UrlFilter attributes need to be independent of URL | 99 // Conditions without UrlFilter attributes need to be independent of URL |
| 100 // matching results. We test here that: | 100 // matching results. We test here that: |
| 101 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 101 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 102 // attributes are fulfilled. | 102 // attributes are fulfilled. |
| 103 // 2. An empty condition (in particular, without UrlFilter attributes) is | 103 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 104 // always fulfilled. | 104 // always fulfilled. |
| 105 TEST(WebRequestConditionTest, NoUrlAttributes) { | 105 TEST(WebRequestConditionTest, NoUrlAttributes) { |
| 106 // Necessary for TestURLRequest. | 106 // Necessary for TestURLRequest. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 146 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 147 " \"thirdPartyForCookies\": true, \n" | 147 " \"thirdPartyForCookies\": true, \n" |
| 148 "}"), | 148 "}"), |
| 149 &error); | 149 &error); |
| 150 EXPECT_EQ("", error); | 150 EXPECT_EQ("", error); |
| 151 ASSERT_TRUE(condition_no_url_false.get()); | 151 ASSERT_TRUE(condition_no_url_false.get()); |
| 152 | 152 |
| 153 net::TestURLRequestContext context; | 153 net::TestURLRequestContext context; |
| 154 net::TestURLRequest https_request( | 154 net::TestURLRequest https_request( |
| 155 GURL("https://www.example.com"), NULL, &context); | 155 GURL("https://www.example.com"), NULL, &context); |
| 156 std::set<URLMatcherConditionSet::ID> dummy_match_set; | |
| 157 | 156 |
| 158 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 157 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 159 // attributes are fulfilled. | 158 // attributes are fulfilled. |
| 160 EXPECT_FALSE(condition_no_url_false->IsFulfilled( | 159 EXPECT_FALSE(condition_no_url_false->IsFulfilled( |
| 161 dummy_match_set, | |
| 162 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 160 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 163 | 161 |
| 164 EXPECT_TRUE(condition_no_url_true->IsFulfilled( | 162 EXPECT_TRUE(condition_no_url_true->IsFulfilled( |
| 165 dummy_match_set, | |
| 166 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 163 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 167 | 164 |
| 168 // 2. An empty condition (in particular, without UrlFilter attributes) is | 165 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 169 // always fulfilled. | 166 // always fulfilled. |
| 170 EXPECT_TRUE(condition_empty->IsFulfilled( | 167 EXPECT_TRUE(condition_empty->IsFulfilled( |
| 171 dummy_match_set, | |
| 172 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 168 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); |
| 173 } | 169 } |
| 174 | 170 |
| 175 TEST(WebRequestConditionTest, CreateConditionSet) { | 171 TEST(WebRequestConditionTest, CreateConditionSet) { |
|
Jeffrey Yasskin
2013/01/24 00:39:17
Consider removing this test entirely, although not
vabr (Chromium)
2013/01/24 18:24:08
I don't think removing this test should be done in
| |
| 176 // Necessary for TestURLRequest. | 172 // Necessary for TestURLRequest. |
| 177 MessageLoop message_loop(MessageLoop::TYPE_IO); | 173 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 178 URLMatcher matcher; | 174 URLMatcher matcher; |
| 179 | 175 |
| 180 WebRequestConditionSet::AnyVector conditions; | 176 WebRequestConditionSet::AnyVector conditions; |
| 181 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 177 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| 182 "{ \n" | 178 "{ \n" |
| 183 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 179 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 184 " \"url\": { \n" | 180 " \"url\": { \n" |
| 185 " \"hostSuffix\": \"example.com\", \n" | 181 " \"hostSuffix\": \"example.com\", \n" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 203 conditions, &error); | 199 conditions, &error); |
| 204 EXPECT_EQ("", error); | 200 EXPECT_EQ("", error); |
| 205 ASSERT_TRUE(result.get()); | 201 ASSERT_TRUE(result.get()); |
| 206 EXPECT_EQ(2u, result->conditions().size()); | 202 EXPECT_EQ(2u, result->conditions().size()); |
| 207 | 203 |
| 208 // Tell the URLMatcher about our shiny new patterns. | 204 // Tell the URLMatcher about our shiny new patterns. |
| 209 URLMatcherConditionSet::Vector url_matcher_condition_set; | 205 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 210 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 206 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 211 matcher.AddConditionSets(url_matcher_condition_set); | 207 matcher.AddConditionSets(url_matcher_condition_set); |
| 212 | 208 |
| 213 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
| 214 | |
| 215 // Test that the result is correct and matches http://www.example.com and | 209 // Test that the result is correct and matches http://www.example.com and |
| 216 // https://www.example.com | 210 // https://www.example.com |
| 217 GURL http_url("http://www.example.com"); | 211 GURL http_url("http://www.example.com"); |
| 218 net::TestURLRequestContext context; | 212 net::TestURLRequestContext context; |
| 219 net::TestURLRequest http_request(http_url, NULL, &context); | 213 net::TestURLRequest http_request(http_url, NULL, &context); |
| 220 url_match_ids = matcher.MatchURL(http_url); | 214 DeclarativeWebRequestData request_data(&http_request, ON_BEFORE_REQUEST); |
| 221 EXPECT_EQ(1u, url_match_ids.size()); | 215 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 222 EXPECT_TRUE(result->IsFulfilled( | 216 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 223 *(url_match_ids.begin()), | 217 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 224 url_match_ids, | 218 request_data)); |
| 225 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST))); | |
| 226 | 219 |
| 227 GURL https_url("https://www.example.com"); | 220 GURL https_url("https://www.example.com"); |
| 228 url_match_ids = matcher.MatchURL(https_url); | 221 request_data.url_match_ids = matcher.MatchURL(https_url); |
| 229 EXPECT_EQ(1u, url_match_ids.size()); | 222 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 230 net::TestURLRequest https_request(https_url, NULL, &context); | 223 net::TestURLRequest https_request(https_url, NULL, &context); |
| 231 EXPECT_TRUE(result->IsFulfilled( | 224 request_data.request = &https_request; |
| 232 *(url_match_ids.begin()), | 225 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 233 url_match_ids, | 226 request_data)); |
| 234 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | |
| 235 | 227 |
| 236 // Check that both, hostPrefix and hostSuffix are evaluated. | 228 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 237 GURL https_foo_url("https://foo.example.com"); | 229 GURL https_foo_url("https://foo.example.com"); |
| 238 url_match_ids = matcher.MatchURL(https_foo_url); | 230 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
| 239 EXPECT_EQ(0u, url_match_ids.size()); | 231 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 240 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 232 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
| 241 EXPECT_FALSE(result->IsFulfilled( | 233 request_data.request = &https_foo_request; |
| 242 -1, | 234 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
| 243 url_match_ids, | |
| 244 DeclarativeWebRequestData(&https_foo_request, ON_BEFORE_REQUEST))); | |
| 245 } | 235 } |
| 246 | 236 |
| 247 TEST(WebRequestConditionTest, TestPortFilter) { | 237 TEST(WebRequestConditionTest, TestPortFilter) { |
| 248 // Necessary for TestURLRequest. | 238 // Necessary for TestURLRequest. |
| 249 MessageLoop message_loop(MessageLoop::TYPE_IO); | 239 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 250 URLMatcher matcher; | 240 URLMatcher matcher; |
| 251 | 241 |
| 252 WebRequestConditionSet::AnyVector conditions; | 242 WebRequestConditionSet::AnyVector conditions; |
| 253 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 243 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| 254 "{ \n" | 244 "{ \n" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 302 // the response header. The Create() method should fail and complain that it is | 292 // the response header. The Create() method should fail and complain that it is |
| 303 // impossible that both conditions are fulfilled at the same time. | 293 // impossible that both conditions are fulfilled at the same time. |
| 304 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 294 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 305 // Necessary for TestURLRequest. | 295 // Necessary for TestURLRequest. |
| 306 MessageLoop message_loop(MessageLoop::TYPE_IO); | 296 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 307 URLMatcher matcher; | 297 URLMatcher matcher; |
| 308 | 298 |
| 309 std::string error; | 299 std::string error; |
| 310 scoped_ptr<WebRequestCondition> result; | 300 scoped_ptr<WebRequestCondition> result; |
| 311 | 301 |
| 312 DictionaryValue condition_value; | |
| 313 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
| 314 | |
| 315 | |
| 316 // Test error on incompatible application stages for involved attributes. | 302 // Test error on incompatible application stages for involved attributes. |
| 317 error.clear(); | 303 error.clear(); |
| 318 result = WebRequestCondition::Create( | 304 result = WebRequestCondition::Create( |
| 319 matcher.condition_factory(), | 305 matcher.condition_factory(), |
| 320 *base::test::ParseJson( | 306 *base::test::ParseJson( |
| 321 "{ \n" | 307 "{ \n" |
| 322 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 308 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 323 // Pass a JS array with one empty object to each of the header | 309 // Pass a JS array with one empty object to each of the header |
| 324 // filters. | 310 // filters. |
| 325 " \"requestHeaders\": [{}], \n" | 311 " \"requestHeaders\": [{}], \n" |
| 326 " \"responseHeaders\": [{}], \n" | 312 " \"responseHeaders\": [{}], \n" |
| 327 "}"), | 313 "}"), |
| 328 &error); | 314 &error); |
| 329 EXPECT_FALSE(error.empty()); | 315 EXPECT_FALSE(error.empty()); |
| 330 EXPECT_FALSE(result.get()); | 316 EXPECT_FALSE(result.get()); |
| 331 } | 317 } |
| 332 | 318 |
| 333 } // namespace extensions | 319 } // namespace extensions |
| OLD | NEW |