| 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 "extensions/browser/api/declarative_webrequest/webrequest_condition.h" | 5 #include "extensions/browser/api/declarative_webrequest/webrequest_condition.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <set> | 8 #include <set> |
| 8 | 9 |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/test/values_test_util.h" | 11 #include "base/test/values_test_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "components/url_matcher/url_matcher_constants.h" | 13 #include "components/url_matcher/url_matcher_constants.h" |
| 14 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
| 15 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" | 15 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h" |
| 16 #include "net/base/request_priority.h" | 16 #include "net/base/request_priority.h" |
| 17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 18 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 using content::ResourceType; | 21 using content::ResourceType; |
| 22 using url_matcher::URLMatcher; | 22 using url_matcher::URLMatcher; |
| 23 using url_matcher::URLMatcherConditionSet; | 23 using url_matcher::URLMatcherConditionSet; |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 | 26 |
| 27 TEST(WebRequestConditionTest, CreateCondition) { | 27 TEST(WebRequestConditionTest, CreateCondition) { |
| 28 // Necessary for TestURLRequest. | 28 // Necessary for TestURLRequest. |
| 29 base::MessageLoopForIO message_loop; | 29 base::MessageLoopForIO message_loop; |
| 30 URLMatcher matcher; | 30 URLMatcher matcher; |
| 31 | 31 |
| 32 std::string error; | 32 std::string error; |
| 33 scoped_ptr<WebRequestCondition> result; | 33 std::unique_ptr<WebRequestCondition> result; |
| 34 | 34 |
| 35 // Test wrong condition name passed. | 35 // Test wrong condition name passed. |
| 36 error.clear(); | 36 error.clear(); |
| 37 result = WebRequestCondition::Create( | 37 result = WebRequestCondition::Create( |
| 38 NULL, | 38 NULL, |
| 39 matcher.condition_factory(), | 39 matcher.condition_factory(), |
| 40 *base::test::ParseJson( | 40 *base::test::ParseJson( |
| 41 "{ \"invalid\": \"foobar\", \n" | 41 "{ \"invalid\": \"foobar\", \n" |
| 42 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 42 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 43 "}"), | 43 "}"), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 73 &error); | 73 &error); |
| 74 EXPECT_EQ("", error); | 74 EXPECT_EQ("", error); |
| 75 ASSERT_TRUE(result.get()); | 75 ASSERT_TRUE(result.get()); |
| 76 | 76 |
| 77 URLMatcherConditionSet::Vector url_matcher_condition_set; | 77 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 78 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 78 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 79 matcher.AddConditionSets(url_matcher_condition_set); | 79 matcher.AddConditionSets(url_matcher_condition_set); |
| 80 | 80 |
| 81 net::TestURLRequestContext context; | 81 net::TestURLRequestContext context; |
| 82 const GURL http_url("http://www.example.com"); | 82 const GURL http_url("http://www.example.com"); |
| 83 scoped_ptr<net::URLRequest> match_request( | 83 std::unique_ptr<net::URLRequest> match_request( |
| 84 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 84 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 85 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); | 85 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
| 86 WebRequestDataWithMatchIds request_data(&data); | 86 WebRequestDataWithMatchIds request_data(&data); |
| 87 request_data.url_match_ids = matcher.MatchURL(http_url); | 87 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 88 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 88 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 89 content::ResourceRequestInfo::AllocateForTesting( | 89 content::ResourceRequestInfo::AllocateForTesting( |
| 90 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, | 90 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, |
| 91 NULL, // context | 91 NULL, // context |
| 92 -1, // render_process_id | 92 -1, // render_process_id |
| 93 -1, // render_view_id | 93 -1, // render_view_id |
| 94 -1, // render_frame_id | 94 -1, // render_frame_id |
| 95 true, // is_main_frame | 95 true, // is_main_frame |
| 96 false, // parent_is_main_frame | 96 false, // parent_is_main_frame |
| 97 true, // allow_download | 97 true, // allow_download |
| 98 false, // is_async | 98 false, // is_async |
| 99 false); // is_using_lofi | 99 false); // is_using_lofi |
| 100 EXPECT_TRUE(result->IsFulfilled(request_data)); | 100 EXPECT_TRUE(result->IsFulfilled(request_data)); |
| 101 | 101 |
| 102 const GURL https_url("https://www.example.com"); | 102 const GURL https_url("https://www.example.com"); |
| 103 scoped_ptr<net::URLRequest> wrong_resource_type( | 103 std::unique_ptr<net::URLRequest> wrong_resource_type( |
| 104 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); | 104 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); |
| 105 data.request = wrong_resource_type.get(); | 105 data.request = wrong_resource_type.get(); |
| 106 request_data.url_match_ids = matcher.MatchURL(http_url); | 106 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 107 // Make sure IsFulfilled does not fail because of URL matching. | 107 // Make sure IsFulfilled does not fail because of URL matching. |
| 108 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 108 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 109 content::ResourceRequestInfo::AllocateForTesting( | 109 content::ResourceRequestInfo::AllocateForTesting( |
| 110 wrong_resource_type.get(), content::RESOURCE_TYPE_SUB_FRAME, | 110 wrong_resource_type.get(), content::RESOURCE_TYPE_SUB_FRAME, |
| 111 NULL, // context | 111 NULL, // context |
| 112 -1, // render_process_id | 112 -1, // render_process_id |
| 113 -1, // render_view_id | 113 -1, // render_view_id |
| 114 -1, // render_frame_id | 114 -1, // render_frame_id |
| 115 false, // is_main_frame | 115 false, // is_main_frame |
| 116 false, // parent_is_main_frame | 116 false, // parent_is_main_frame |
| 117 true, // allow_download | 117 true, // allow_download |
| 118 false, // is_async | 118 false, // is_async |
| 119 false); // is_using_lofi | 119 false); // is_using_lofi |
| 120 EXPECT_FALSE(result->IsFulfilled(request_data)); | 120 EXPECT_FALSE(result->IsFulfilled(request_data)); |
| 121 } | 121 } |
| 122 | 122 |
| 123 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 123 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
| 124 // Necessary for TestURLRequest. | 124 // Necessary for TestURLRequest. |
| 125 base::MessageLoopForIO message_loop; | 125 base::MessageLoopForIO message_loop; |
| 126 URLMatcher matcher; | 126 URLMatcher matcher; |
| 127 | 127 |
| 128 std::string error; | 128 std::string error; |
| 129 scoped_ptr<WebRequestCondition> result; | 129 std::unique_ptr<WebRequestCondition> result; |
| 130 | 130 |
| 131 result = WebRequestCondition::Create( | 131 result = WebRequestCondition::Create( |
| 132 NULL, | 132 NULL, |
| 133 matcher.condition_factory(), | 133 matcher.condition_factory(), |
| 134 *base::test::ParseJson( | 134 *base::test::ParseJson( |
| 135 "{ \n" | 135 "{ \n" |
| 136 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" | 136 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" |
| 137 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 137 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 138 "}"), | 138 "}"), |
| 139 &error); | 139 &error); |
| 140 EXPECT_EQ("", error); | 140 EXPECT_EQ("", error); |
| 141 ASSERT_TRUE(result.get()); | 141 ASSERT_TRUE(result.get()); |
| 142 | 142 |
| 143 URLMatcherConditionSet::Vector url_matcher_condition_set; | 143 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 144 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 144 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 145 matcher.AddConditionSets(url_matcher_condition_set); | 145 matcher.AddConditionSets(url_matcher_condition_set); |
| 146 | 146 |
| 147 net::TestURLRequestContext context; | 147 net::TestURLRequestContext context; |
| 148 const GURL http_url("http://www.example.com"); | 148 const GURL http_url("http://www.example.com"); |
| 149 const GURL first_party_url("http://fpfc.example.com"); | 149 const GURL first_party_url("http://fpfc.example.com"); |
| 150 scoped_ptr<net::URLRequest> match_request( | 150 std::unique_ptr<net::URLRequest> match_request( |
| 151 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 151 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 152 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); | 152 WebRequestData data(match_request.get(), ON_BEFORE_REQUEST); |
| 153 WebRequestDataWithMatchIds request_data(&data); | 153 WebRequestDataWithMatchIds request_data(&data); |
| 154 request_data.url_match_ids = matcher.MatchURL(http_url); | 154 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 155 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 155 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 156 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); | 156 request_data.first_party_url_match_ids = matcher.MatchURL(first_party_url); |
| 157 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); | 157 EXPECT_EQ(1u, request_data.first_party_url_match_ids.size()); |
| 158 content::ResourceRequestInfo::AllocateForTesting( | 158 content::ResourceRequestInfo::AllocateForTesting( |
| 159 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, | 159 match_request.get(), content::RESOURCE_TYPE_MAIN_FRAME, |
| 160 NULL, // context | 160 NULL, // context |
| (...skipping 15 matching lines...) Expand all Loading... |
| 176 // 2. An empty condition (in particular, without UrlFilter attributes) is | 176 // 2. An empty condition (in particular, without UrlFilter attributes) is |
| 177 // always fulfilled. | 177 // always fulfilled. |
| 178 TEST(WebRequestConditionTest, NoUrlAttributes) { | 178 TEST(WebRequestConditionTest, NoUrlAttributes) { |
| 179 // Necessary for TestURLRequest. | 179 // Necessary for TestURLRequest. |
| 180 base::MessageLoopForIO message_loop; | 180 base::MessageLoopForIO message_loop; |
| 181 URLMatcher matcher; | 181 URLMatcher matcher; |
| 182 std::string error; | 182 std::string error; |
| 183 | 183 |
| 184 // The empty condition. | 184 // The empty condition. |
| 185 error.clear(); | 185 error.clear(); |
| 186 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( | 186 std::unique_ptr<WebRequestCondition> condition_empty = |
| 187 NULL, | 187 WebRequestCondition::Create( |
| 188 matcher.condition_factory(), | 188 NULL, matcher.condition_factory(), |
| 189 *base::test::ParseJson( | 189 *base::test::ParseJson( |
| 190 "{ \n" | 190 "{ \n" |
| 191 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 191 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 192 "}"), | 192 "}"), |
| 193 &error); | 193 &error); |
| 194 EXPECT_EQ("", error); | 194 EXPECT_EQ("", error); |
| 195 ASSERT_TRUE(condition_empty.get()); | 195 ASSERT_TRUE(condition_empty.get()); |
| 196 | 196 |
| 197 // A condition without a UrlFilter attribute, which is always true. | 197 // A condition without a UrlFilter attribute, which is always true. |
| 198 error.clear(); | 198 error.clear(); |
| 199 scoped_ptr<WebRequestCondition> condition_no_url_true = | 199 std::unique_ptr<WebRequestCondition> condition_no_url_true = |
| 200 WebRequestCondition::Create( | 200 WebRequestCondition::Create( |
| 201 NULL, | 201 NULL, matcher.condition_factory(), |
| 202 matcher.condition_factory(), | |
| 203 *base::test::ParseJson( | 202 *base::test::ParseJson( |
| 204 "{ \n" | 203 "{ \n" |
| 205 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " | 204 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " |
| 206 "\n" | 205 "\n" |
| 207 // There is no "1st party for cookies" URL in the requests below, | 206 // There is no "1st party for cookies" URL in the requests below, |
| 208 // therefore all requests are considered first party for cookies. | 207 // therefore all requests are considered first party for cookies. |
| 209 " \"thirdPartyForCookies\": false, \n" | 208 " \"thirdPartyForCookies\": false, \n" |
| 210 "}"), | 209 "}"), |
| 211 &error); | 210 &error); |
| 212 EXPECT_EQ("", error); | 211 EXPECT_EQ("", error); |
| 213 ASSERT_TRUE(condition_no_url_true.get()); | 212 ASSERT_TRUE(condition_no_url_true.get()); |
| 214 | 213 |
| 215 // A condition without a UrlFilter attribute, which is always false. | 214 // A condition without a UrlFilter attribute, which is always false. |
| 216 error.clear(); | 215 error.clear(); |
| 217 scoped_ptr<WebRequestCondition> condition_no_url_false = | 216 std::unique_ptr<WebRequestCondition> condition_no_url_false = |
| 218 WebRequestCondition::Create( | 217 WebRequestCondition::Create( |
| 219 NULL, | 218 NULL, matcher.condition_factory(), |
| 220 matcher.condition_factory(), | |
| 221 *base::test::ParseJson( | 219 *base::test::ParseJson( |
| 222 "{ \n" | 220 "{ \n" |
| 223 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " | 221 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", " |
| 224 "\n" | 222 "\n" |
| 225 " \"thirdPartyForCookies\": true, \n" | 223 " \"thirdPartyForCookies\": true, \n" |
| 226 "}"), | 224 "}"), |
| 227 &error); | 225 &error); |
| 228 EXPECT_EQ("", error); | 226 EXPECT_EQ("", error); |
| 229 ASSERT_TRUE(condition_no_url_false.get()); | 227 ASSERT_TRUE(condition_no_url_false.get()); |
| 230 | 228 |
| 231 net::TestURLRequestContext context; | 229 net::TestURLRequestContext context; |
| 232 scoped_ptr<net::URLRequest> https_request(context.CreateRequest( | 230 std::unique_ptr<net::URLRequest> https_request(context.CreateRequest( |
| 233 GURL("https://www.example.com"), net::DEFAULT_PRIORITY, NULL)); | 231 GURL("https://www.example.com"), net::DEFAULT_PRIORITY, NULL)); |
| 234 | 232 |
| 235 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 233 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
| 236 // attributes are fulfilled. | 234 // attributes are fulfilled. |
| 237 WebRequestData data(https_request.get(), ON_BEFORE_REQUEST); | 235 WebRequestData data(https_request.get(), ON_BEFORE_REQUEST); |
| 238 EXPECT_FALSE( | 236 EXPECT_FALSE( |
| 239 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 237 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
| 240 | 238 |
| 241 data = WebRequestData(https_request.get(), ON_BEFORE_REQUEST); | 239 data = WebRequestData(https_request.get(), ON_BEFORE_REQUEST); |
| 242 EXPECT_TRUE( | 240 EXPECT_TRUE( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 267 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 265 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 268 " \"url\": { \n" | 266 " \"url\": { \n" |
| 269 " \"hostSuffix\": \"example.com\", \n" | 267 " \"hostSuffix\": \"example.com\", \n" |
| 270 " \"hostPrefix\": \"www\", \n" | 268 " \"hostPrefix\": \"www\", \n" |
| 271 " \"schemes\": [\"https\"], \n" | 269 " \"schemes\": [\"https\"], \n" |
| 272 " }, \n" | 270 " }, \n" |
| 273 "}")); | 271 "}")); |
| 274 | 272 |
| 275 // Test insertion | 273 // Test insertion |
| 276 std::string error; | 274 std::string error; |
| 277 scoped_ptr<WebRequestConditionSet> result = WebRequestConditionSet::Create( | 275 std::unique_ptr<WebRequestConditionSet> result = |
| 278 NULL, matcher.condition_factory(), conditions, &error); | 276 WebRequestConditionSet::Create(NULL, matcher.condition_factory(), |
| 277 conditions, &error); |
| 279 EXPECT_EQ("", error); | 278 EXPECT_EQ("", error); |
| 280 ASSERT_TRUE(result.get()); | 279 ASSERT_TRUE(result.get()); |
| 281 EXPECT_EQ(2u, result->conditions().size()); | 280 EXPECT_EQ(2u, result->conditions().size()); |
| 282 | 281 |
| 283 // Tell the URLMatcher about our shiny new patterns. | 282 // Tell the URLMatcher about our shiny new patterns. |
| 284 URLMatcherConditionSet::Vector url_matcher_condition_set; | 283 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 285 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 284 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 286 matcher.AddConditionSets(url_matcher_condition_set); | 285 matcher.AddConditionSets(url_matcher_condition_set); |
| 287 | 286 |
| 288 // Test that the result is correct and matches http://www.example.com and | 287 // Test that the result is correct and matches http://www.example.com and |
| 289 // https://www.example.com | 288 // https://www.example.com |
| 290 GURL http_url("http://www.example.com"); | 289 GURL http_url("http://www.example.com"); |
| 291 net::TestURLRequestContext context; | 290 net::TestURLRequestContext context; |
| 292 scoped_ptr<net::URLRequest> http_request( | 291 std::unique_ptr<net::URLRequest> http_request( |
| 293 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 292 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 294 WebRequestData data(http_request.get(), ON_BEFORE_REQUEST); | 293 WebRequestData data(http_request.get(), ON_BEFORE_REQUEST); |
| 295 WebRequestDataWithMatchIds request_data(&data); | 294 WebRequestDataWithMatchIds request_data(&data); |
| 296 request_data.url_match_ids = matcher.MatchURL(http_url); | 295 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 297 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 296 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 298 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 297 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 299 request_data)); | 298 request_data)); |
| 300 | 299 |
| 301 GURL https_url("https://www.example.com"); | 300 GURL https_url("https://www.example.com"); |
| 302 request_data.url_match_ids = matcher.MatchURL(https_url); | 301 request_data.url_match_ids = matcher.MatchURL(https_url); |
| 303 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 302 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
| 304 scoped_ptr<net::URLRequest> https_request( | 303 std::unique_ptr<net::URLRequest> https_request( |
| 305 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); | 304 context.CreateRequest(https_url, net::DEFAULT_PRIORITY, NULL)); |
| 306 data.request = https_request.get(); | 305 data.request = https_request.get(); |
| 307 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), | 306 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
| 308 request_data)); | 307 request_data)); |
| 309 | 308 |
| 310 // Check that both, hostPrefix and hostSuffix are evaluated. | 309 // Check that both, hostPrefix and hostSuffix are evaluated. |
| 311 GURL https_foo_url("https://foo.example.com"); | 310 GURL https_foo_url("https://foo.example.com"); |
| 312 request_data.url_match_ids = matcher.MatchURL(https_foo_url); | 311 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
| 313 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 312 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 314 scoped_ptr<net::URLRequest> https_foo_request( | 313 std::unique_ptr<net::URLRequest> https_foo_request( |
| 315 context.CreateRequest(https_foo_url, net::DEFAULT_PRIORITY, NULL)); | 314 context.CreateRequest(https_foo_url, net::DEFAULT_PRIORITY, NULL)); |
| 316 data.request = https_foo_request.get(); | 315 data.request = https_foo_request.get(); |
| 317 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); | 316 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
| 318 } | 317 } |
| 319 | 318 |
| 320 TEST(WebRequestConditionTest, TestPortFilter) { | 319 TEST(WebRequestConditionTest, TestPortFilter) { |
| 321 // Necessary for TestURLRequest. | 320 // Necessary for TestURLRequest. |
| 322 base::MessageLoopForIO message_loop; | 321 base::MessageLoopForIO message_loop; |
| 323 URLMatcher matcher; | 322 URLMatcher matcher; |
| 324 | 323 |
| 325 WebRequestConditionSet::Values conditions; | 324 WebRequestConditionSet::Values conditions; |
| 326 conditions.push_back(base::test::ParseJson( | 325 conditions.push_back(base::test::ParseJson( |
| 327 "{ \n" | 326 "{ \n" |
| 328 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 327 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 329 " \"url\": { \n" | 328 " \"url\": { \n" |
| 330 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. | 329 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. |
| 331 " \"hostSuffix\": \"example.com\", \n" | 330 " \"hostSuffix\": \"example.com\", \n" |
| 332 " }, \n" | 331 " }, \n" |
| 333 "}")); | 332 "}")); |
| 334 | 333 |
| 335 // Test insertion | 334 // Test insertion |
| 336 std::string error; | 335 std::string error; |
| 337 scoped_ptr<WebRequestConditionSet> result = WebRequestConditionSet::Create( | 336 std::unique_ptr<WebRequestConditionSet> result = |
| 338 NULL, matcher.condition_factory(), conditions, &error); | 337 WebRequestConditionSet::Create(NULL, matcher.condition_factory(), |
| 338 conditions, &error); |
| 339 EXPECT_EQ("", error); | 339 EXPECT_EQ("", error); |
| 340 ASSERT_TRUE(result.get()); | 340 ASSERT_TRUE(result.get()); |
| 341 EXPECT_EQ(1u, result->conditions().size()); | 341 EXPECT_EQ(1u, result->conditions().size()); |
| 342 | 342 |
| 343 // Tell the URLMatcher about our shiny new patterns. | 343 // Tell the URLMatcher about our shiny new patterns. |
| 344 URLMatcherConditionSet::Vector url_matcher_condition_set; | 344 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 345 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 345 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 346 matcher.AddConditionSets(url_matcher_condition_set); | 346 matcher.AddConditionSets(url_matcher_condition_set); |
| 347 | 347 |
| 348 std::set<URLMatcherConditionSet::ID> url_match_ids; | 348 std::set<URLMatcherConditionSet::ID> url_match_ids; |
| 349 | 349 |
| 350 // Test various URLs. | 350 // Test various URLs. |
| 351 GURL http_url("http://www.example.com"); | 351 GURL http_url("http://www.example.com"); |
| 352 net::TestURLRequestContext context; | 352 net::TestURLRequestContext context; |
| 353 scoped_ptr<net::URLRequest> http_request( | 353 std::unique_ptr<net::URLRequest> http_request( |
| 354 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); | 354 context.CreateRequest(http_url, net::DEFAULT_PRIORITY, NULL)); |
| 355 url_match_ids = matcher.MatchURL(http_url); | 355 url_match_ids = matcher.MatchURL(http_url); |
| 356 ASSERT_EQ(1u, url_match_ids.size()); | 356 ASSERT_EQ(1u, url_match_ids.size()); |
| 357 | 357 |
| 358 GURL http_url_80("http://www.example.com:80"); | 358 GURL http_url_80("http://www.example.com:80"); |
| 359 scoped_ptr<net::URLRequest> http_request_80( | 359 std::unique_ptr<net::URLRequest> http_request_80( |
| 360 context.CreateRequest(http_url_80, net::DEFAULT_PRIORITY, NULL)); | 360 context.CreateRequest(http_url_80, net::DEFAULT_PRIORITY, NULL)); |
| 361 url_match_ids = matcher.MatchURL(http_url_80); | 361 url_match_ids = matcher.MatchURL(http_url_80); |
| 362 ASSERT_EQ(1u, url_match_ids.size()); | 362 ASSERT_EQ(1u, url_match_ids.size()); |
| 363 | 363 |
| 364 GURL http_url_1000("http://www.example.com:1000"); | 364 GURL http_url_1000("http://www.example.com:1000"); |
| 365 scoped_ptr<net::URLRequest> http_request_1000( | 365 std::unique_ptr<net::URLRequest> http_request_1000( |
| 366 context.CreateRequest(http_url_1000, net::DEFAULT_PRIORITY, NULL)); | 366 context.CreateRequest(http_url_1000, net::DEFAULT_PRIORITY, NULL)); |
| 367 url_match_ids = matcher.MatchURL(http_url_1000); | 367 url_match_ids = matcher.MatchURL(http_url_1000); |
| 368 ASSERT_EQ(1u, url_match_ids.size()); | 368 ASSERT_EQ(1u, url_match_ids.size()); |
| 369 | 369 |
| 370 GURL http_url_2000("http://www.example.com:2000"); | 370 GURL http_url_2000("http://www.example.com:2000"); |
| 371 scoped_ptr<net::URLRequest> http_request_2000( | 371 std::unique_ptr<net::URLRequest> http_request_2000( |
| 372 context.CreateRequest(http_url_2000, net::DEFAULT_PRIORITY, NULL)); | 372 context.CreateRequest(http_url_2000, net::DEFAULT_PRIORITY, NULL)); |
| 373 url_match_ids = matcher.MatchURL(http_url_2000); | 373 url_match_ids = matcher.MatchURL(http_url_2000); |
| 374 ASSERT_EQ(0u, url_match_ids.size()); | 374 ASSERT_EQ(0u, url_match_ids.size()); |
| 375 } | 375 } |
| 376 | 376 |
| 377 // Create a condition with two attributes: one on the request header and one on | 377 // Create a condition with two attributes: one on the request header and one on |
| 378 // the response header. The Create() method should fail and complain that it is | 378 // the response header. The Create() method should fail and complain that it is |
| 379 // impossible that both conditions are fulfilled at the same time. | 379 // impossible that both conditions are fulfilled at the same time. |
| 380 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 380 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
| 381 // Necessary for TestURLRequest. | 381 // Necessary for TestURLRequest. |
| 382 base::MessageLoopForIO message_loop; | 382 base::MessageLoopForIO message_loop; |
| 383 URLMatcher matcher; | 383 URLMatcher matcher; |
| 384 | 384 |
| 385 std::string error; | 385 std::string error; |
| 386 scoped_ptr<WebRequestCondition> result; | 386 std::unique_ptr<WebRequestCondition> result; |
| 387 | 387 |
| 388 // Test error on incompatible application stages for involved attributes. | 388 // Test error on incompatible application stages for involved attributes. |
| 389 error.clear(); | 389 error.clear(); |
| 390 result = WebRequestCondition::Create( | 390 result = WebRequestCondition::Create( |
| 391 NULL, | 391 NULL, |
| 392 matcher.condition_factory(), | 392 matcher.condition_factory(), |
| 393 *base::test::ParseJson( | 393 *base::test::ParseJson( |
| 394 "{ \n" | 394 "{ \n" |
| 395 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 395 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 396 // Pass a JS array with one empty object to each of the header | 396 // Pass a JS array with one empty object to each of the header |
| 397 // filters. | 397 // filters. |
| 398 " \"requestHeaders\": [{}], \n" | 398 " \"requestHeaders\": [{}], \n" |
| 399 " \"responseHeaders\": [{}], \n" | 399 " \"responseHeaders\": [{}], \n" |
| 400 "}"), | 400 "}"), |
| 401 &error); | 401 &error); |
| 402 EXPECT_FALSE(error.empty()); | 402 EXPECT_FALSE(error.empty()); |
| 403 EXPECT_FALSE(result.get()); | 403 EXPECT_FALSE(result.get()); |
| 404 } | 404 } |
| 405 | 405 |
| 406 } // namespace extensions | 406 } // namespace extensions |
| OLD | NEW |