| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 &error); | 66 &error); |
| 67 EXPECT_EQ("", error); | 67 EXPECT_EQ("", error); |
| 68 ASSERT_TRUE(result.get()); | 68 ASSERT_TRUE(result.get()); |
| 69 | 69 |
| 70 URLMatcherConditionSet::Vector url_matcher_condition_set; | 70 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 71 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 71 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 72 matcher.AddConditionSets(url_matcher_condition_set); | 72 matcher.AddConditionSets(url_matcher_condition_set); |
| 73 | 73 |
| 74 net::TestURLRequestContext context; | 74 net::TestURLRequestContext context; |
| 75 const GURL http_url("http://www.example.com"); | 75 const GURL http_url("http://www.example.com"); |
| 76 net::TestURLRequest match_request(http_url, NULL, &context); | 76 net::TestURLRequest match_request(http_url, NULL, &context, NULL); |
| 77 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 77 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
| 78 WebRequestDataWithMatchIds request_data(&data); | 78 WebRequestDataWithMatchIds request_data(&data); |
| 79 request_data.url_match_ids = matcher.MatchURL(http_url); | 79 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 80 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 80 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 81 content::ResourceRequestInfo::AllocateForTesting( | 81 content::ResourceRequestInfo::AllocateForTesting( |
| 82 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); | 82 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 83 EXPECT_TRUE(result->IsFulfilled(request_data)); | 83 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 84 | 84 |
| 85 const GURL https_url("https://www.example.com"); | 85 const GURL https_url("https://www.example.com"); |
| 86 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); | 86 net::TestURLRequest wrong_resource_type(https_url, NULL, &context, NULL); |
| 87 data.request = &wrong_resource_type; | 87 data.request = &wrong_resource_type; |
| 88 request_data.url_match_ids = matcher.MatchURL(http_url); | 88 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 89 // Make sure IsFulfilled does not fail because of URL matching. | 89 // Make sure IsFulfilled does not fail because of URL matching. |
| 90 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 90 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 91 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 91 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
| 92 ResourceType::SUB_FRAME, NULL, -1, -1); | 92 ResourceType::SUB_FRAME, NULL, -1, -1); |
| 93 EXPECT_FALSE(result->IsFulfilled(request_data)); | 93 EXPECT_FALSE(result->IsFulfilled(request_data)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 96 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 EXPECT_EQ("", error); | 112 EXPECT_EQ("", error); |
| 113 ASSERT_TRUE(result.get()); | 113 ASSERT_TRUE(result.get()); |
| 114 | 114 |
| 115 URLMatcherConditionSet::Vector url_matcher_condition_set; | 115 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 116 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 116 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 117 matcher.AddConditionSets(url_matcher_condition_set); | 117 matcher.AddConditionSets(url_matcher_condition_set); |
| 118 | 118 |
| 119 net::TestURLRequestContext context; | 119 net::TestURLRequestContext context; |
| 120 const GURL http_url("http://www.example.com"); | 120 const GURL http_url("http://www.example.com"); |
| 121 const GURL first_party_url("http://fpfc.example.com"); | 121 const GURL first_party_url("http://fpfc.example.com"); |
| 122 net::TestURLRequest match_request(http_url, NULL, &context); | 122 net::TestURLRequest match_request(http_url, NULL, &context, NULL); |
| 123 WebRequestData data(&match_request, ON_BEFORE_REQUEST); | 123 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
| 124 WebRequestDataWithMatchIds request_data(&data); | 124 WebRequestDataWithMatchIds request_data(&data); |
| 125 request_data.url_match_ids = matcher.MatchURL(http_url); | 125 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 126 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 126 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 127 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | 127 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); |
| 128 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); | 128 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); |
| 129 content::ResourceRequestInfo::AllocateForTesting( | 129 content::ResourceRequestInfo::AllocateForTesting( |
| 130 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); | 130 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 131 EXPECT_TRUE(result->IsFulfilled(request_data)); | 131 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 132 } | 132 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 "{ \n" | 180 "{ \n" |
| 181 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 181 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 182 " \"thirdPartyForCookies\": true, \n" | 182 " \"thirdPartyForCookies\": true, \n" |
| 183 "}"), | 183 "}"), |
| 184 &error); | 184 &error); |
| 185 EXPECT_EQ("", error); | 185 EXPECT_EQ("", error); |
| 186 ASSERT_TRUE(condition_no_url_false.get()); | 186 ASSERT_TRUE(condition_no_url_false.get()); |
| 187 | 187 |
| 188 net::TestURLRequestContext context; | 188 net::TestURLRequestContext context; |
| 189 net::TestURLRequest https_request( | 189 net::TestURLRequest https_request( |
| 190 GURL("https://www.example.com"), NULL, &context); | 190 GURL("https://www.example.com"), NULL, &context, NULL); |
| 191 | 191 |
| 192 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 192 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 193 // attributes are fulfilled. | 193 // attributes are fulfilled. |
| 194 WebRequestData data(&https_request, ON_BEFORE_REQUEST); | 194 WebRequestData data(&https_request, ON_BEFORE_REQUEST); |
| 195 EXPECT_FALSE( | 195 EXPECT_FALSE( |
| 196 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 196 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 197 | 197 |
| 198 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 198 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
| 199 EXPECT_TRUE( | 199 EXPECT_TRUE( |
| 200 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 200 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 240 |
| 241 // Tell the URLMatcher about our shiny new patterns. | 241 // Tell the URLMatcher about our shiny new patterns. |
| 242 URLMatcherConditionSet::Vector url_matcher_condition_set; | 242 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 243 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 243 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 244 matcher.AddConditionSets(url_matcher_condition_set); | 244 matcher.AddConditionSets(url_matcher_condition_set); |
| 245 | 245 |
| 246 // Test that the result is correct and matches http://www.example.com and | 246 // Test that the result is correct and matches http://www.example.com and |
| 247 // https://www.example.com | 247 // https://www.example.com |
| 248 GURL http_url("http://www.example.com"); | 248 GURL http_url("http://www.example.com"); |
| 249 net::TestURLRequestContext context; | 249 net::TestURLRequestContext context; |
| 250 net::TestURLRequest http_request(http_url, NULL, &context); | 250 net::TestURLRequest http_request(http_url, NULL, &context, NULL); |
| 251 WebRequestData data(&http_request, ON_BEFORE_REQUEST); | 251 WebRequestData data(&http_request, ON_BEFORE_REQUEST); |
| 252 WebRequestDataWithMatchIds request_data(&data); | 252 WebRequestDataWithMatchIds request_data(&data); |
| 253 request_data.url_match_ids = matcher.MatchURL(http_url); | 253 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 254 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 254 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 255 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 255 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 256 request_data)); | 256 request_data)); |
| 257 | 257 |
| 258 GURL https_url("https://www.example.com"); | 258 GURL https_url("https://www.example.com"); |
| 259 request_data.url_match_ids = matcher.MatchURL(https_url); | 259 request_data.url_match_ids = matcher.MatchURL(https_url); |
| 260 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 260 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 261 net::TestURLRequest https_request(https_url, NULL, &context); | 261 net::TestURLRequest https_request(https_url, NULL, &context, NULL); |
| 262 data.request = &https_request; | 262 data.request = &https_request; |
| 263 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 263 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 264 request_data)); | 264 request_data)); |
| 265 | 265 |
| 266 // Check that both, hostPrefix and hostSuffix are evaluated. | 266 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 267 GURL https_foo_url("https://foo.example.com"); | 267 GURL https_foo_url("https://foo.example.com"); |
| 268 request_data.url_match_ids = matcher.MatchURL(https_foo_url); | 268 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
| 269 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 269 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 270 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 270 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context, NULL); |
| 271 data.request = &https_foo_request; | 271 data.request = &https_foo_request; |
| 272 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); | 272 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
| 273 } | 273 } |
| 274 | 274 |
| 275 TEST(WebRequestConditionTest, TestPortFilter) { | 275 TEST(WebRequestConditionTest, TestPortFilter) { |
| 276 // Necessary for TestURLRequest. | 276 // Necessary for TestURLRequest. |
| 277 MessageLoop message_loop(MessageLoop::TYPE_IO); | 277 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 278 URLMatcher matcher; | 278 URLMatcher matcher; |
| 279 | 279 |
| 280 WebRequestConditionSet::AnyVector conditions; | 280 WebRequestConditionSet::AnyVector conditions; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 299 // Tell the URLMatcher about our shiny new patterns. | 299 // Tell the URLMatcher about our shiny new patterns. |
| 300 URLMatcherConditionSet::Vector url_matcher_condition_set; | 300 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 301 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 301 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 302 matcher.AddConditionSets(url_matcher_condition_set); | 302 matcher.AddConditionSets(url_matcher_condition_set); |
| 303 | 303 |
| 304 std::set<URLMatcherConditionSet::ID> url_match_ids; | 304 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 305 | 305 |
| 306 // Test various URLs. | 306 // Test various URLs. |
| 307 GURL http_url("http://www.example.com"); | 307 GURL http_url("http://www.example.com"); |
| 308 net::TestURLRequestContext context; | 308 net::TestURLRequestContext context; |
| 309 net::TestURLRequest http_request(http_url, NULL, &context); | 309 net::TestURLRequest http_request(http_url, NULL, &context, NULL); |
| 310 url_match_ids = matcher.MatchURL(http_url); | 310 url_match_ids = matcher.MatchURL(http_url); |
| 311 ASSERT_EQ(1u, url_match_ids.size()); | 311 ASSERT_EQ(1u, url_match_ids.size()); |
| 312 | 312 |
| 313 GURL http_url_80("http://www.example.com:80"); | 313 GURL http_url_80("http://www.example.com:80"); |
| 314 net::TestURLRequest http_request_80(http_url_80, NULL, &context); | 314 net::TestURLRequest http_request_80(http_url_80, NULL, &context, NULL); |
| 315 url_match_ids = matcher.MatchURL(http_url_80); | 315 url_match_ids = matcher.MatchURL(http_url_80); |
| 316 ASSERT_EQ(1u, url_match_ids.size()); | 316 ASSERT_EQ(1u, url_match_ids.size()); |
| 317 | 317 |
| 318 GURL http_url_1000("http://www.example.com:1000"); | 318 GURL http_url_1000("http://www.example.com:1000"); |
| 319 net::TestURLRequest http_request_1000(http_url_1000, NULL, &context); | 319 net::TestURLRequest http_request_1000(http_url_1000, NULL, &context, NULL); |
| 320 url_match_ids = matcher.MatchURL(http_url_1000); | 320 url_match_ids = matcher.MatchURL(http_url_1000); |
| 321 ASSERT_EQ(1u, url_match_ids.size()); | 321 ASSERT_EQ(1u, url_match_ids.size()); |
| 322 | 322 |
| 323 GURL http_url_2000("http://www.example.com:2000"); | 323 GURL http_url_2000("http://www.example.com:2000"); |
| 324 net::TestURLRequest http_request_2000(http_url_2000, NULL, &context); | 324 net::TestURLRequest http_request_2000(http_url_2000, NULL, &context, NULL); |
| 325 url_match_ids = matcher.MatchURL(http_url_2000); | 325 url_match_ids = matcher.MatchURL(http_url_2000); |
| 326 ASSERT_EQ(0u, url_match_ids.size()); | 326 ASSERT_EQ(0u, url_match_ids.size()); |
| 327 } | 327 } |
| 328 | 328 |
| 329 // Create a condition with two attributes: one on the request header and one on | 329 // Create a condition with two attributes: one on the request header and one on |
| 330 // the response header. The Create() method should fail and complain that it is | 330 // the response header. The Create() method should fail and complain that it is |
| 331 // impossible that both conditions are fulfilled at the same time. | 331 // impossible that both conditions are fulfilled at the same time. |
| 332 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 332 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 333 // Necessary for TestURLRequest. | 333 // Necessary for TestURLRequest. |
| 334 MessageLoop message_loop(MessageLoop::TYPE_IO); | 334 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 348 // filters. | 348 // filters. |
| 349 " \"requestHeaders\": [{}], \n" | 349 " \"requestHeaders\": [{}], \n" |
| 350 " \"responseHeaders\": [{}], \n" | 350 " \"responseHeaders\": [{}], \n" |
| 351 "}"), | 351 "}"), |
| 352 &error); | 352 &error); |
| 353 EXPECT_FALSE(error.empty()); | 353 EXPECT_FALSE(error.empty()); |
| 354 EXPECT_FALSE(result.get()); | 354 EXPECT_FALSE(result.get()); |
| 355 } | 355 } |
| 356 | 356 |
| 357 } // namespace extensions | 357 } // namespace extensions |
| OLD | NEW |