| 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/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/test/values_test_util.h" | 10 #include "base/test/values_test_util.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" | 12 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta
nts.h" |
| 13 #include "content/public/browser/resource_request_info.h" | 13 #include "content/public/browser/resource_request_info.h" |
| 14 #include "extensions/common/matcher/url_matcher_constants.h" | 14 #include "extensions/common/matcher/url_matcher_constants.h" |
| 15 #include "net/base/request_priority.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace keys = declarative_webrequest_constants; | 21 namespace keys = declarative_webrequest_constants; |
| 21 namespace keys2 = url_matcher_constants; | 22 namespace keys2 = url_matcher_constants; |
| 22 | 23 |
| 23 TEST(WebRequestConditionTest, CreateCondition) { | 24 TEST(WebRequestConditionTest, CreateCondition) { |
| 24 // Necessary for TestURLRequest. | 25 // Necessary for TestURLRequest. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 &error); | 70 &error); |
| 70 EXPECT_EQ("", error); | 71 EXPECT_EQ("", error); |
| 71 ASSERT_TRUE(result.get()); | 72 ASSERT_TRUE(result.get()); |
| 72 | 73 |
| 73 URLMatcherConditionSet::Vector url_matcher_condition_set; | 74 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 74 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 75 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 75 matcher.AddConditionSets(url_matcher_condition_set); | 76 matcher.AddConditionSets(url_matcher_condition_set); |
| 76 | 77 |
| 77 net::TestURLRequestContext context; | 78 net::TestURLRequestContext context; |
| 78 const GURL http_url("http://www.example.com"); | 79 const GURL http_url("http://www.example.com"); |
| 79 net::TestURLRequest match_request(http_url, NULL, &context, NULL); | 80 net::TestURLRequest match_request( |
| 81 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 80 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 82 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
| 81 WebRequestDataWithMatchIds request_data(&data); | 83 WebRequestDataWithMatchIds request_data(&data); |
| 82 request_data.url_match_ids = matcher.MatchURL(http_url); | 84 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 83 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 85 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 84 content::ResourceRequestInfo::AllocateForTesting( | 86 content::ResourceRequestInfo::AllocateForTesting( |
| 85 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1, false); | 87 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1, false); |
| 86 EXPECT_TRUE(result->IsFulfilled(request_data)); | 88 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 87 | 89 |
| 88 const GURL https_url("https://www.example.com"); | 90 const GURL https_url("https://www.example.com"); |
| 89 net::TestURLRequest wrong_resource_type(https_url, NULL, &context, NULL); | 91 net::TestURLRequest wrong_resource_type( |
| 92 https_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 90 data.request = &wrong_resource_type; | 93 data.request = &wrong_resource_type; |
| 91 request_data.url_match_ids = matcher.MatchURL(http_url); | 94 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 92 // Make sure IsFulfilled does not fail because of URL matching. | 95 // Make sure IsFulfilled does not fail because of URL matching. |
| 93 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 96 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 94 content::ResourceRequestInfo::AllocateForTesting( | 97 content::ResourceRequestInfo::AllocateForTesting( |
| 95 &wrong_resource_type, ResourceType::SUB_FRAME, NULL, -1, -1, false); | 98 &wrong_resource_type, ResourceType::SUB_FRAME, NULL, -1, -1, false); |
| 96 EXPECT_FALSE(result->IsFulfilled(request_data)); | 99 EXPECT_FALSE(result->IsFulfilled(request_data)); |
| 97 } | 100 } |
| 98 | 101 |
| 99 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 102 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 116 EXPECT_EQ("", error); | 119 EXPECT_EQ("", error); |
| 117 ASSERT_TRUE(result.get()); | 120 ASSERT_TRUE(result.get()); |
| 118 | 121 |
| 119 URLMatcherConditionSet::Vector url_matcher_condition_set; | 122 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 120 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 123 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 121 matcher.AddConditionSets(url_matcher_condition_set); | 124 matcher.AddConditionSets(url_matcher_condition_set); |
| 122 | 125 |
| 123 net::TestURLRequestContext context; | 126 net::TestURLRequestContext context; |
| 124 const GURL http_url("http://www.example.com"); | 127 const GURL http_url("http://www.example.com"); |
| 125 const GURL first_party_url("http://fpfc.example.com"); | 128 const GURL first_party_url("http://fpfc.example.com"); |
| 126 net::TestURLRequest match_request(http_url, NULL, &context, NULL); | 129 net::TestURLRequest match_request( |
| 130 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 127 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 131 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
| 128 WebRequestDataWithMatchIds request_data(&data); | 132 WebRequestDataWithMatchIds request_data(&data); |
| 129 request_data.url_match_ids = matcher.MatchURL(http_url); | 133 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 130 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 134 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 131 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | 135 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); |
| 132 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); | 136 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); |
| 133 content::ResourceRequestInfo::AllocateForTesting( | 137 content::ResourceRequestInfo::AllocateForTesting( |
| 134 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1, false); | 138 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1, false); |
| 135 EXPECT_TRUE(result->IsFulfilled(request_data)); | 139 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 136 } | 140 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " | 193 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " |
| 190 "\n" | 194 "\n" |
| 191 " \"thirdPartyForCookies\": true, \n" | 195 " \"thirdPartyForCookies\": true, \n" |
| 192 "}"), | 196 "}"), |
| 193 &error); | 197 &error); |
| 194 EXPECT_EQ("", error); | 198 EXPECT_EQ("", error); |
| 195 ASSERT_TRUE(condition_no_url_false.get()); | 199 ASSERT_TRUE(condition_no_url_false.get()); |
| 196 | 200 |
| 197 net::TestURLRequestContext context; | 201 net::TestURLRequestContext context; |
| 198 net::TestURLRequest https_request( | 202 net::TestURLRequest https_request( |
| 199 GURL("https://www.example.com"), NULL, &context, NULL); | 203 GURL("https://www.example.com"), net::DEFAULT_PRIORITY, NULL, &context); |
| 200 | 204 |
| 201 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 205 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 202 // attributes are fulfilled. | 206 // attributes are fulfilled. |
| 203 WebRequestData data(&https_request, ON_BEFORE_REQUEST); | 207 WebRequestData data(&https_request, ON_BEFORE_REQUEST); |
| 204 EXPECT_FALSE( | 208 EXPECT_FALSE( |
| 205 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 209 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 206 | 210 |
| 207 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 211 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
| 208 EXPECT_TRUE( | 212 EXPECT_TRUE( |
| 209 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 213 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 | 252 |
| 249 // Tell the URLMatcher about our shiny new patterns. | 253 // Tell the URLMatcher about our shiny new patterns. |
| 250 URLMatcherConditionSet::Vector url_matcher_condition_set; | 254 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 251 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 255 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 252 matcher.AddConditionSets(url_matcher_condition_set); | 256 matcher.AddConditionSets(url_matcher_condition_set); |
| 253 | 257 |
| 254 // Test that the result is correct and matches http://www.example.com and | 258 // Test that the result is correct and matches http://www.example.com and |
| 255 // https://www.example.com | 259 // https://www.example.com |
| 256 GURL http_url("http://www.example.com"); | 260 GURL http_url("http://www.example.com"); |
| 257 net::TestURLRequestContext context; | 261 net::TestURLRequestContext context; |
| 258 net::TestURLRequest http_request(http_url, NULL, &context, NULL); | 262 net::TestURLRequest http_request( |
| 263 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 259 WebRequestData data(&http_request, ON_BEFORE_REQUEST); | 264 WebRequestData data(&http_request, ON_BEFORE_REQUEST); |
| 260 WebRequestDataWithMatchIds request_data(&data); | 265 WebRequestDataWithMatchIds request_data(&data); |
| 261 request_data.url_match_ids = matcher.MatchURL(http_url); | 266 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 262 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 267 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 263 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 268 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 264 request_data)); | 269 request_data)); |
| 265 | 270 |
| 266 GURL https_url("https://www.example.com"); | 271 GURL https_url("https://www.example.com"); |
| 267 request_data.url_match_ids = matcher.MatchURL(https_url); | 272 request_data.url_match_ids = matcher.MatchURL(https_url); |
| 268 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 273 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 269 net::TestURLRequest https_request(https_url, NULL, &context, NULL); | 274 net::TestURLRequest https_request( |
| 275 https_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 270 data.request = &https_request; | 276 data.request = &https_request; |
| 271 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 277 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 272 request_data)); | 278 request_data)); |
| 273 | 279 |
| 274 // Check that both, hostPrefix and hostSuffix are evaluated. | 280 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 275 GURL https_foo_url("https://foo.example.com"); | 281 GURL https_foo_url("https://foo.example.com"); |
| 276 request_data.url_match_ids = matcher.MatchURL(https_foo_url); | 282 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
| 277 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 283 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 278 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context, NULL); | 284 net::TestURLRequest https_foo_request( |
| 285 https_foo_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 279 data.request = &https_foo_request; | 286 data.request = &https_foo_request; |
| 280 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); | 287 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
| 281 } | 288 } |
| 282 | 289 |
| 283 TEST(WebRequestConditionTest, TestPortFilter) { | 290 TEST(WebRequestConditionTest, TestPortFilter) { |
| 284 // Necessary for TestURLRequest. | 291 // Necessary for TestURLRequest. |
| 285 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 292 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 286 URLMatcher matcher; | 293 URLMatcher matcher; |
| 287 | 294 |
| 288 WebRequestConditionSet::AnyVector conditions; | 295 WebRequestConditionSet::AnyVector conditions; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 306 // Tell the URLMatcher about our shiny new patterns. | 313 // Tell the URLMatcher about our shiny new patterns. |
| 307 URLMatcherConditionSet::Vector url_matcher_condition_set; | 314 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 308 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 315 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 309 matcher.AddConditionSets(url_matcher_condition_set); | 316 matcher.AddConditionSets(url_matcher_condition_set); |
| 310 | 317 |
| 311 std::set<URLMatcherConditionSet::ID> url_match_ids; | 318 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 312 | 319 |
| 313 // Test various URLs. | 320 // Test various URLs. |
| 314 GURL http_url("http://www.example.com"); | 321 GURL http_url("http://www.example.com"); |
| 315 net::TestURLRequestContext context; | 322 net::TestURLRequestContext context; |
| 316 net::TestURLRequest http_request(http_url, NULL, &context, NULL); | 323 net::TestURLRequest http_request( |
| 324 http_url, net::DEFAULT_PRIORITY, NULL, &context); |
| 317 url_match_ids = matcher.MatchURL(http_url); | 325 url_match_ids = matcher.MatchURL(http_url); |
| 318 ASSERT_EQ(1u, url_match_ids.size()); | 326 ASSERT_EQ(1u, url_match_ids.size()); |
| 319 | 327 |
| 320 GURL http_url_80("http://www.example.com:80"); | 328 GURL http_url_80("http://www.example.com:80"); |
| 321 net::TestURLRequest http_request_80(http_url_80, NULL, &context, NULL); | 329 net::TestURLRequest http_request_80( |
| 330 http_url_80, net::DEFAULT_PRIORITY, NULL, &context); |
| 322 url_match_ids = matcher.MatchURL(http_url_80); | 331 url_match_ids = matcher.MatchURL(http_url_80); |
| 323 ASSERT_EQ(1u, url_match_ids.size()); | 332 ASSERT_EQ(1u, url_match_ids.size()); |
| 324 | 333 |
| 325 GURL http_url_1000("http://www.example.com:1000"); | 334 GURL http_url_1000("http://www.example.com:1000"); |
| 326 net::TestURLRequest http_request_1000(http_url_1000, NULL, &context, NULL); | 335 net::TestURLRequest http_request_1000( |
| 336 http_url_1000, net::DEFAULT_PRIORITY, NULL, &context); |
| 327 url_match_ids = matcher.MatchURL(http_url_1000); | 337 url_match_ids = matcher.MatchURL(http_url_1000); |
| 328 ASSERT_EQ(1u, url_match_ids.size()); | 338 ASSERT_EQ(1u, url_match_ids.size()); |
| 329 | 339 |
| 330 GURL http_url_2000("http://www.example.com:2000"); | 340 GURL http_url_2000("http://www.example.com:2000"); |
| 331 net::TestURLRequest http_request_2000(http_url_2000, NULL, &context, NULL); | 341 net::TestURLRequest http_request_2000( |
| 342 http_url_2000, net::DEFAULT_PRIORITY, NULL, &context); |
| 332 url_match_ids = matcher.MatchURL(http_url_2000); | 343 url_match_ids = matcher.MatchURL(http_url_2000); |
| 333 ASSERT_EQ(0u, url_match_ids.size()); | 344 ASSERT_EQ(0u, url_match_ids.size()); |
| 334 } | 345 } |
| 335 | 346 |
| 336 // Create a condition with two attributes: one on the request header and one on | 347 // Create a condition with two attributes: one on the request header and one on |
| 337 // the response header. The Create() method should fail and complain that it is | 348 // the response header. The Create() method should fail and complain that it is |
| 338 // impossible that both conditions are fulfilled at the same time. | 349 // impossible that both conditions are fulfilled at the same time. |
| 339 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 350 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 340 // Necessary for TestURLRequest. | 351 // Necessary for TestURLRequest. |
| 341 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 352 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 356 // filters. | 367 // filters. |
| 357 " \"requestHeaders\": [{}], \n" | 368 " \"requestHeaders\": [{}], \n" |
| 358 " \"responseHeaders\": [{}], \n" | 369 " \"responseHeaders\": [{}], \n" |
| 359 "}"), | 370 "}"), |
| 360 &error); | 371 &error); |
| 361 EXPECT_FALSE(error.empty()); | 372 EXPECT_FALSE(error.empty()); |
| 362 EXPECT_FALSE(result.get()); | 373 EXPECT_FALSE(result.get()); |
| 363 } | 374 } |
| 364 | 375 |
| 365 } // namespace extensions | 376 } // namespace extensions |
| OLD | NEW |