| 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/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/test/values_test_util.h" | 11 #include "base/test/values_test_util.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 13 #include "components/url_matcher/url_matcher_constants.h" | 14 #include "components/url_matcher/url_matcher_constants.h" |
| 14 #include "content/public/browser/resource_request_info.h" | 15 #include "content/public/browser/resource_request_info.h" |
| 15 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
| 17 #include "net/url_request/url_request.h" |
| 16 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 using content::ResourceType; | 21 using content::ResourceType; |
| 20 using url_matcher::URLMatcher; | 22 using url_matcher::URLMatcher; |
| 21 using url_matcher::URLMatcherConditionSet; | 23 using url_matcher::URLMatcherConditionSet; |
| 22 | 24 |
| 23 namespace extensions { | 25 namespace extensions { |
| 24 | 26 |
| 25 TEST(WebRequestConditionTest, CreateCondition) { | 27 TEST(WebRequestConditionTest, CreateCondition) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 &error); | 73 &error); |
| 72 EXPECT_EQ("", error); | 74 EXPECT_EQ("", error); |
| 73 ASSERT_TRUE(result.get()); | 75 ASSERT_TRUE(result.get()); |
| 74 | 76 |
| 75 URLMatcherConditionSet::Vector url_matcher_condition_set; | 77 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 76 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 78 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 77 matcher.AddConditionSets(url_matcher_condition_set); | 79 matcher.AddConditionSets(url_matcher_condition_set); |
| 78 | 80 |
| 79 net::TestURLRequestContext context; | 81 net::TestURLRequestContext context; |
| 80 const GURL http_url("http://www.example.com"); | 82 const GURL http_url("http://www.example.com"); |
| 81 net::TestURLRequest match_request( | 83 scoped_ptr<net::URLRequest> match_request(context.CreateRequest( |
| 82 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 84 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 83 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 85 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
| 84 WebRequestDataWithMatchIds request_data(&data); | 86 WebRequestDataWithMatchIds request_data(&data); |
| 85 request_data.url_match_ids = matcher.MatchURL(http_url); | 87 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 86 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 88 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 87 content::ResourceRequestInfo::AllocateForTesting( | 89 content::ResourceRequestInfo::AllocateForTesting( |
| 88 &match_request, | 90 match_request.get(), |
| 89 content::RESOURCE_TYPE_MAIN_FRAME, | 91 content::RESOURCE_TYPE_MAIN_FRAME, |
| 90 NULL, | 92 NULL, |
| 91 -1, | 93 -1, |
| 92 -1, | 94 -1, |
| 93 -1, | 95 -1, |
| 94 false); | 96 false); |
| 95 EXPECT_TRUE(result->IsFulfilled(request_data)); | 97 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 96 | 98 |
| 97 const GURL https_url("https://www.example.com"); | 99 const GURL https_url("https://www.example.com"); |
| 98 net::TestURLRequest wrong_resource_type( | 100 scoped_ptr<net::URLRequest> wrong_resource_type(context.CreateRequest( |
| 99 https_url, net::DEFAULT_PRIORITY, NULL, &context); | 101 https_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 100 data.request = &wrong_resource_type; | 102 data.request = wrong_resource_type.get(); |
| 101 request_data.url_match_ids = matcher.MatchURL(http_url); | 103 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 102 // Make sure IsFulfilled does not fail because of URL matching. | 104 // Make sure IsFulfilled does not fail because of URL matching. |
| 103 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 105 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 104 content::ResourceRequestInfo::AllocateForTesting( | 106 content::ResourceRequestInfo::AllocateForTesting( |
| 105 &wrong_resource_type, | 107 wrong_resource_type.get(), |
| 106 content::RESOURCE_TYPE_SUB_FRAME, | 108 content::RESOURCE_TYPE_SUB_FRAME, |
| 107 NULL, | 109 NULL, |
| 108 -1, | 110 -1, |
| 109 -1, | 111 -1, |
| 110 -1, | 112 -1, |
| 111 false); | 113 false); |
| 112 EXPECT_FALSE(result->IsFulfilled(request_data)); | 114 EXPECT_FALSE(result->IsFulfilled(request_data)); |
| 113 } | 115 } |
| 114 | 116 |
| 115 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 117 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 132 EXPECT_EQ("", error); | 134 EXPECT_EQ("", error); |
| 133 ASSERT_TRUE(result.get()); | 135 ASSERT_TRUE(result.get()); |
| 134 | 136 |
| 135 URLMatcherConditionSet::Vector url_matcher_condition_set; | 137 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 136 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 138 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 137 matcher.AddConditionSets(url_matcher_condition_set); | 139 matcher.AddConditionSets(url_matcher_condition_set); |
| 138 | 140 |
| 139 net::TestURLRequestContext context; | 141 net::TestURLRequestContext context; |
| 140 const GURL http_url("http://www.example.com"); | 142 const GURL http_url("http://www.example.com"); |
| 141 const GURL first_party_url("http://fpfc.example.com"); | 143 const GURL first_party_url("http://fpfc.example.com"); |
| 142 net::TestURLRequest match_request( | 144 scoped_ptr<net::URLRequest> match_request(context.CreateRequest( |
| 143 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 145 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 144 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 146 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
| 145 WebRequestDataWithMatchIds request_data(&data); | 147 WebRequestDataWithMatchIds request_data(&data); |
| 146 request_data.url_match_ids = matcher.MatchURL(http_url); | 148 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 147 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 149 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 148 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | 150 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); |
| 149 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); | 151 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); |
| 150 content::ResourceRequestInfo::AllocateForTesting( | 152 content::ResourceRequestInfo::AllocateForTesting( |
| 151 &match_request, | 153 match_request.get(), |
| 152 content::RESOURCE_TYPE_MAIN_FRAME, | 154 content::RESOURCE_TYPE_MAIN_FRAME, |
| 153 NULL, | 155 NULL, |
| 154 -1, | 156 -1, |
| 155 -1, | 157 -1, |
| 156 -1, | 158 -1, |
| 157 false); | 159 false); |
| 158 EXPECT_TRUE(result->IsFulfilled(request_data)); | 160 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 159 } | 161 } |
| 160 | 162 |
| 161 // Conditions without UrlFilter attributes need to be independent of URL | 163 // Conditions without UrlFilter attributes need to be independent of URL |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 "{ \n" | 213 "{ \n" |
| 212 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " | 214 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " |
| 213 "\n" | 215 "\n" |
| 214 " \"thirdPartyForCookies\": true, \n" | 216 " \"thirdPartyForCookies\": true, \n" |
| 215 "}"), | 217 "}"), |
| 216 &error); | 218 &error); |
| 217 EXPECT_EQ("", error); | 219 EXPECT_EQ("", error); |
| 218 ASSERT_TRUE(condition_no_url_false.get()); | 220 ASSERT_TRUE(condition_no_url_false.get()); |
| 219 | 221 |
| 220 net::TestURLRequestContext context; | 222 net::TestURLRequestContext context; |
| 221 net::TestURLRequest https_request( | 223 scoped_ptr<net::URLRequest> https_request(context.CreateRequest( |
| 222 GURL("https://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); | 224 GURL("https://www.example.com"), net::DEFAULT_PRIORITY, NULL, NULL)); |
| 223 | 225 |
| 224 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 226 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 225 // attributes are fulfilled. | 227 // attributes are fulfilled. |
| 226 WebRequestData data(&https_request, ON_BEFORE_REQUEST); | 228 WebRequestData data(https_request.get(), ON_BEFORE_REQUEST); |
| 227 EXPECT_FALSE( | 229 EXPECT_FALSE( |
| 228 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 230 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 229 | 231 |
| 230 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 232 data = WebRequestData(https_request.get(), ON_BEFORE_REQUEST); |
| 231 EXPECT_TRUE( | 233 EXPECT_TRUE( |
| 232 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 234 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 233 | 235 |
| 234 // 2. An empty condition (in particular, without UrlFilter attributes) is | 236 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 235 // always fulfilled. | 237 // always fulfilled. |
| 236 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 238 data = WebRequestData(https_request.get(), ON_BEFORE_REQUEST); |
| 237 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 239 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 238 } | 240 } |
| 239 | 241 |
| 240 TEST(WebRequestConditionTest, CreateConditionSet) { | 242 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 241 // Necessary for TestURLRequest. | 243 // Necessary for TestURLRequest. |
| 242 base::MessageLoopForIO message_loop; | 244 base::MessageLoopForIO message_loop; |
| 243 URLMatcher matcher; | 245 URLMatcher matcher; |
| 244 | 246 |
| 245 WebRequestConditionSet::AnyVector conditions; | 247 WebRequestConditionSet::AnyVector conditions; |
| 246 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 248 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 271 | 273 |
| 272 // Tell the URLMatcher about our shiny new patterns. | 274 // Tell the URLMatcher about our shiny new patterns. |
| 273 URLMatcherConditionSet::Vector url_matcher_condition_set; | 275 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 274 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 276 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 275 matcher.AddConditionSets(url_matcher_condition_set); | 277 matcher.AddConditionSets(url_matcher_condition_set); |
| 276 | 278 |
| 277 // Test that the result is correct and matches http://www.example.com and | 279 // Test that the result is correct and matches http://www.example.com and |
| 278 // https://www.example.com | 280 // https://www.example.com |
| 279 GURL http_url("http://www.example.com"); | 281 GURL http_url("http://www.example.com"); |
| 280 net::TestURLRequestContext context; | 282 net::TestURLRequestContext context; |
| 281 net::TestURLRequest http_request( | 283 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
| 282 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 284 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 283 WebRequestData data(&http_request, ON_BEFORE_REQUEST); | 285 WebRequestData data(http_request.get(), ON_BEFORE_REQUEST); |
| 284 WebRequestDataWithMatchIds request_data(&data); | 286 WebRequestDataWithMatchIds request_data(&data); |
| 285 request_data.url_match_ids = matcher.MatchURL(http_url); | 287 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 286 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 288 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 287 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 289 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 288 request_data)); | 290 request_data)); |
| 289 | 291 |
| 290 GURL https_url("https://www.example.com"); | 292 GURL https_url("https://www.example.com"); |
| 291 request_data.url_match_ids = matcher.MatchURL(https_url); | 293 request_data.url_match_ids = matcher.MatchURL(https_url); |
| 292 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 294 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 293 net::TestURLRequest https_request( | 295 scoped_ptr<net::URLRequest> https_request(context.CreateRequest( |
| 294 https_url, net::DEFAULT_PRIORITY, NULL, &context); | 296 https_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 295 data.request = &https_request; | 297 data.request = https_request.get(); |
| 296 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 298 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 297 request_data)); | 299 request_data)); |
| 298 | 300 |
| 299 // Check that both, hostPrefix and hostSuffix are evaluated. | 301 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 300 GURL https_foo_url("https://foo.example.com"); | 302 GURL https_foo_url("https://foo.example.com"); |
| 301 request_data.url_match_ids = matcher.MatchURL(https_foo_url); | 303 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
| 302 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 304 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 303 net::TestURLRequest https_foo_request( | 305 scoped_ptr<net::URLRequest> https_foo_request(context.CreateRequest( |
| 304 https_foo_url, net::DEFAULT_PRIORITY, NULL, &context); | 306 https_foo_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 305 data.request = &https_foo_request; | 307 data.request = https_foo_request.get(); |
| 306 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); | 308 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
| 307 } | 309 } |
| 308 | 310 |
| 309 TEST(WebRequestConditionTest, TestPortFilter) { | 311 TEST(WebRequestConditionTest, TestPortFilter) { |
| 310 // Necessary for TestURLRequest. | 312 // Necessary for TestURLRequest. |
| 311 base::MessageLoopForIO message_loop; | 313 base::MessageLoopForIO message_loop; |
| 312 URLMatcher matcher; | 314 URLMatcher matcher; |
| 313 | 315 |
| 314 WebRequestConditionSet::AnyVector conditions; | 316 WebRequestConditionSet::AnyVector conditions; |
| 315 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 317 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 332 // Tell the URLMatcher about our shiny new patterns. | 334 // Tell the URLMatcher about our shiny new patterns. |
| 333 URLMatcherConditionSet::Vector url_matcher_condition_set; | 335 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 334 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 336 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 335 matcher.AddConditionSets(url_matcher_condition_set); | 337 matcher.AddConditionSets(url_matcher_condition_set); |
| 336 | 338 |
| 337 std::set<URLMatcherConditionSet::ID> url_match_ids; | 339 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 338 | 340 |
| 339 // Test various URLs. | 341 // Test various URLs. |
| 340 GURL http_url("http://www.example.com"); | 342 GURL http_url("http://www.example.com"); |
| 341 net::TestURLRequestContext context; | 343 net::TestURLRequestContext context; |
| 342 net::TestURLRequest http_request( | 344 scoped_ptr<net::URLRequest> http_request(context.CreateRequest( |
| 343 http_url, net::DEFAULT_PRIORITY, NULL, &context); | 345 http_url, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 344 url_match_ids = matcher.MatchURL(http_url); | 346 url_match_ids = matcher.MatchURL(http_url); |
| 345 ASSERT_EQ(1u, url_match_ids.size()); | 347 ASSERT_EQ(1u, url_match_ids.size()); |
| 346 | 348 |
| 347 GURL http_url_80("http://www.example.com:80"); | 349 GURL http_url_80("http://www.example.com:80"); |
| 348 net::TestURLRequest http_request_80( | 350 scoped_ptr<net::URLRequest> http_request_80(context.CreateRequest( |
| 349 http_url_80, net::DEFAULT_PRIORITY, NULL, &context); | 351 http_url_80, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 350 url_match_ids = matcher.MatchURL(http_url_80); | 352 url_match_ids = matcher.MatchURL(http_url_80); |
| 351 ASSERT_EQ(1u, url_match_ids.size()); | 353 ASSERT_EQ(1u, url_match_ids.size()); |
| 352 | 354 |
| 353 GURL http_url_1000("http://www.example.com:1000"); | 355 GURL http_url_1000("http://www.example.com:1000"); |
| 354 net::TestURLRequest http_request_1000( | 356 scoped_ptr<net::URLRequest> http_request_1000(context.CreateRequest( |
| 355 http_url_1000, net::DEFAULT_PRIORITY, NULL, &context); | 357 http_url_1000, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 356 url_match_ids = matcher.MatchURL(http_url_1000); | 358 url_match_ids = matcher.MatchURL(http_url_1000); |
| 357 ASSERT_EQ(1u, url_match_ids.size()); | 359 ASSERT_EQ(1u, url_match_ids.size()); |
| 358 | 360 |
| 359 GURL http_url_2000("http://www.example.com:2000"); | 361 GURL http_url_2000("http://www.example.com:2000"); |
| 360 net::TestURLRequest http_request_2000( | 362 scoped_ptr<net::URLRequest> http_request_2000(context.CreateRequest( |
| 361 http_url_2000, net::DEFAULT_PRIORITY, NULL, &context); | 363 http_url_2000, net::DEFAULT_PRIORITY, NULL, NULL)); |
| 362 url_match_ids = matcher.MatchURL(http_url_2000); | 364 url_match_ids = matcher.MatchURL(http_url_2000); |
| 363 ASSERT_EQ(0u, url_match_ids.size()); | 365 ASSERT_EQ(0u, url_match_ids.size()); |
| 364 } | 366 } |
| 365 | 367 |
| 366 // Create a condition with two attributes: one on the request header and one on | 368 // Create a condition with two attributes: one on the request header and one on |
| 367 // the response header. The Create() method should fail and complain that it is | 369 // the response header. The Create() method should fail and complain that it is |
| 368 // impossible that both conditions are fulfilled at the same time. | 370 // impossible that both conditions are fulfilled at the same time. |
| 369 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 371 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 370 // Necessary for TestURLRequest. | 372 // Necessary for TestURLRequest. |
| 371 base::MessageLoopForIO message_loop; | 373 base::MessageLoopForIO message_loop; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 386 // filters. | 388 // filters. |
| 387 " \"requestHeaders\": [{}], \n" | 389 " \"requestHeaders\": [{}], \n" |
| 388 " \"responseHeaders\": [{}], \n" | 390 " \"responseHeaders\": [{}], \n" |
| 389 "}"), | 391 "}"), |
| 390 &error); | 392 &error); |
| 391 EXPECT_FALSE(error.empty()); | 393 EXPECT_FALSE(error.empty()); |
| 392 EXPECT_FALSE(result.get()); | 394 EXPECT_FALSE(result.get()); |
| 393 } | 395 } |
| 394 | 396 |
| 395 } // namespace extensions | 397 } // namespace extensions |
| OLD | NEW |