| 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" |
| 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/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace keys = declarative_webrequest_constants; | 20 namespace keys = declarative_webrequest_constants; |
| 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 base::MessageLoop message_loop(base::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 // Test wrong condition name passed. | 31 // Test wrong condition name passed. |
| 32 error.clear(); | 32 error.clear(); |
| 33 result = WebRequestCondition::Create( | 33 result = WebRequestCondition::Create( |
| 34 matcher.condition_factory(), | 34 matcher.condition_factory(), |
| 35 *base::test::ParseJson( | 35 *base::test::ParseJson( |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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) { |
| 97 // Necessary for TestURLRequest. | 97 // Necessary for TestURLRequest. |
| 98 MessageLoop message_loop(MessageLoop::TYPE_IO); | 98 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 99 URLMatcher matcher; | 99 URLMatcher matcher; |
| 100 | 100 |
| 101 std::string error; | 101 std::string error; |
| 102 scoped_ptr<WebRequestCondition> result; | 102 scoped_ptr<WebRequestCondition> result; |
| 103 | 103 |
| 104 result = WebRequestCondition::Create( | 104 result = WebRequestCondition::Create( |
| 105 matcher.condition_factory(), | 105 matcher.condition_factory(), |
| 106 *base::test::ParseJson( | 106 *base::test::ParseJson( |
| 107 "{ \n" | 107 "{ \n" |
| 108 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" | 108 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Conditions without UrlFilter attributes need to be independent of URL | 134 // Conditions without UrlFilter attributes need to be independent of URL |
| 135 // matching results. We test here that: | 135 // matching results. We test here that: |
| 136 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 136 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 137 // attributes are fulfilled. | 137 // attributes are fulfilled. |
| 138 // 2. An empty condition (in particular, without UrlFilter attributes) is | 138 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 139 // always fulfilled. | 139 // always fulfilled. |
| 140 TEST(WebRequestConditionTest, NoUrlAttributes) { | 140 TEST(WebRequestConditionTest, NoUrlAttributes) { |
| 141 // Necessary for TestURLRequest. | 141 // Necessary for TestURLRequest. |
| 142 MessageLoop message_loop(MessageLoop::TYPE_IO); | 142 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 143 URLMatcher matcher; | 143 URLMatcher matcher; |
| 144 std::string error; | 144 std::string error; |
| 145 | 145 |
| 146 // The empty condition. | 146 // The empty condition. |
| 147 error.clear(); | 147 error.clear(); |
| 148 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( | 148 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( |
| 149 matcher.condition_factory(), | 149 matcher.condition_factory(), |
| 150 *base::test::ParseJson( | 150 *base::test::ParseJson( |
| 151 "{ \n" | 151 "{ \n" |
| 152 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 152 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 200 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 201 | 201 |
| 202 // 2. An empty condition (in particular, without UrlFilter attributes) is | 202 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 203 // always fulfilled. | 203 // always fulfilled. |
| 204 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 204 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
| 205 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 205 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 206 } | 206 } |
| 207 | 207 |
| 208 TEST(WebRequestConditionTest, CreateConditionSet) { | 208 TEST(WebRequestConditionTest, CreateConditionSet) { |
| 209 // Necessary for TestURLRequest. | 209 // Necessary for TestURLRequest. |
| 210 MessageLoop message_loop(MessageLoop::TYPE_IO); | 210 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 211 URLMatcher matcher; | 211 URLMatcher matcher; |
| 212 | 212 |
| 213 WebRequestConditionSet::AnyVector conditions; | 213 WebRequestConditionSet::AnyVector conditions; |
| 214 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 214 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| 215 "{ \n" | 215 "{ \n" |
| 216 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 216 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 217 " \"url\": { \n" | 217 " \"url\": { \n" |
| 218 " \"hostSuffix\": \"example.com\", \n" | 218 " \"hostSuffix\": \"example.com\", \n" |
| 219 " \"schemes\": [\"http\"], \n" | 219 " \"schemes\": [\"http\"], \n" |
| 220 " }, \n" | 220 " }, \n" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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, NULL); | 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 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 278 URLMatcher matcher; | 278 URLMatcher matcher; |
| 279 | 279 |
| 280 WebRequestConditionSet::AnyVector conditions; | 280 WebRequestConditionSet::AnyVector conditions; |
| 281 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 281 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
| 282 "{ \n" | 282 "{ \n" |
| 283 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 283 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 284 " \"url\": { \n" | 284 " \"url\": { \n" |
| 285 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. | 285 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. |
| 286 " \"hostSuffix\": \"example.com\", \n" | 286 " \"hostSuffix\": \"example.com\", \n" |
| 287 " }, \n" | 287 " }, \n" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 net::TestURLRequest http_request_2000(http_url_2000, NULL, &context, NULL); | 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 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| 335 URLMatcher matcher; | 335 URLMatcher matcher; |
| 336 | 336 |
| 337 std::string error; | 337 std::string error; |
| 338 scoped_ptr<WebRequestCondition> result; | 338 scoped_ptr<WebRequestCondition> result; |
| 339 | 339 |
| 340 // Test error on incompatible application stages for involved attributes. | 340 // Test error on incompatible application stages for involved attributes. |
| 341 error.clear(); | 341 error.clear(); |
| 342 result = WebRequestCondition::Create( | 342 result = WebRequestCondition::Create( |
| 343 matcher.condition_factory(), | 343 matcher.condition_factory(), |
| 344 *base::test::ParseJson( | 344 *base::test::ParseJson( |
| 345 "{ \n" | 345 "{ \n" |
| 346 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 346 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 347 // Pass a JS array with one empty object to each of the header | 347 // Pass a JS array with one empty object to each of the header |
| 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 |