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 "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 "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
16 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using url_matcher::URLMatcher; | 19 using url_matcher::URLMatcher; |
20 using url_matcher::URLMatcherConditionSet; | 20 using url_matcher::URLMatcherConditionSet; |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 TEST(WebRequestConditionTest, CreateCondition) { | 24 TEST(WebRequestConditionTest, CreateCondition) { |
25 // Necessary for TestURLRequest. | 25 // Necessary for TestURLRequest. |
26 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 26 base::MessageLoopForIO message_loop; |
27 URLMatcher matcher; | 27 URLMatcher matcher; |
28 | 28 |
29 std::string error; | 29 std::string error; |
30 scoped_ptr<WebRequestCondition> result; | 30 scoped_ptr<WebRequestCondition> result; |
31 | 31 |
32 // Test wrong condition name passed. | 32 // Test wrong condition name passed. |
33 error.clear(); | 33 error.clear(); |
34 result = WebRequestCondition::Create( | 34 result = WebRequestCondition::Create( |
35 NULL, | 35 NULL, |
36 matcher.condition_factory(), | 36 matcher.condition_factory(), |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 request_data.url_match_ids = matcher.MatchURL(http_url); | 94 request_data.url_match_ids = matcher.MatchURL(http_url); |
95 // Make sure IsFulfilled does not fail because of URL matching. | 95 // Make sure IsFulfilled does not fail because of URL matching. |
96 EXPECT_EQ(1u, request_data.url_match_ids.size()); | 96 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
97 content::ResourceRequestInfo::AllocateForTesting( | 97 content::ResourceRequestInfo::AllocateForTesting( |
98 &wrong_resource_type, ResourceType::SUB_FRAME, NULL, -1, -1, -1, false); | 98 &wrong_resource_type, ResourceType::SUB_FRAME, NULL, -1, -1, -1, false); |
99 EXPECT_FALSE(result->IsFulfilled(request_data)); | 99 EXPECT_FALSE(result->IsFulfilled(request_data)); |
100 } | 100 } |
101 | 101 |
102 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { | 102 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
103 // Necessary for TestURLRequest. | 103 // Necessary for TestURLRequest. |
104 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 104 base::MessageLoopForIO message_loop; |
105 URLMatcher matcher; | 105 URLMatcher matcher; |
106 | 106 |
107 std::string error; | 107 std::string error; |
108 scoped_ptr<WebRequestCondition> result; | 108 scoped_ptr<WebRequestCondition> result; |
109 | 109 |
110 result = WebRequestCondition::Create( | 110 result = WebRequestCondition::Create( |
111 NULL, | 111 NULL, |
112 matcher.condition_factory(), | 112 matcher.condition_factory(), |
113 *base::test::ParseJson( | 113 *base::test::ParseJson( |
114 "{ \n" | 114 "{ \n" |
(...skipping 25 matching lines...) Expand all Loading... |
140 } | 140 } |
141 | 141 |
142 // Conditions without UrlFilter attributes need to be independent of URL | 142 // Conditions without UrlFilter attributes need to be independent of URL |
143 // matching results. We test here that: | 143 // matching results. We test here that: |
144 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 144 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
145 // attributes are fulfilled. | 145 // attributes are fulfilled. |
146 // 2. An empty condition (in particular, without UrlFilter attributes) is | 146 // 2. An empty condition (in particular, without UrlFilter attributes) is |
147 // always fulfilled. | 147 // always fulfilled. |
148 TEST(WebRequestConditionTest, NoUrlAttributes) { | 148 TEST(WebRequestConditionTest, NoUrlAttributes) { |
149 // Necessary for TestURLRequest. | 149 // Necessary for TestURLRequest. |
150 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 150 base::MessageLoopForIO message_loop; |
151 URLMatcher matcher; | 151 URLMatcher matcher; |
152 std::string error; | 152 std::string error; |
153 | 153 |
154 // The empty condition. | 154 // The empty condition. |
155 error.clear(); | 155 error.clear(); |
156 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( | 156 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( |
157 NULL, | 157 NULL, |
158 matcher.condition_factory(), | 158 matcher.condition_factory(), |
159 *base::test::ParseJson( | 159 *base::test::ParseJson( |
160 "{ \n" | 160 "{ \n" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 213 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
214 | 214 |
215 // 2. An empty condition (in particular, without UrlFilter attributes) is | 215 // 2. An empty condition (in particular, without UrlFilter attributes) is |
216 // always fulfilled. | 216 // always fulfilled. |
217 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); | 217 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
218 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); | 218 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
219 } | 219 } |
220 | 220 |
221 TEST(WebRequestConditionTest, CreateConditionSet) { | 221 TEST(WebRequestConditionTest, CreateConditionSet) { |
222 // Necessary for TestURLRequest. | 222 // Necessary for TestURLRequest. |
223 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 223 base::MessageLoopForIO message_loop; |
224 URLMatcher matcher; | 224 URLMatcher matcher; |
225 | 225 |
226 WebRequestConditionSet::AnyVector conditions; | 226 WebRequestConditionSet::AnyVector conditions; |
227 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 227 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
228 "{ \n" | 228 "{ \n" |
229 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 229 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
230 " \"url\": { \n" | 230 " \"url\": { \n" |
231 " \"hostSuffix\": \"example.com\", \n" | 231 " \"hostSuffix\": \"example.com\", \n" |
232 " \"schemes\": [\"http\"], \n" | 232 " \"schemes\": [\"http\"], \n" |
233 " }, \n" | 233 " }, \n" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 request_data.url_match_ids = matcher.MatchURL(https_foo_url); | 282 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
283 EXPECT_EQ(0u, request_data.url_match_ids.size()); | 283 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
284 net::TestURLRequest https_foo_request( | 284 net::TestURLRequest https_foo_request( |
285 https_foo_url, net::DEFAULT_PRIORITY, NULL, &context); | 285 https_foo_url, net::DEFAULT_PRIORITY, NULL, &context); |
286 data.request = &https_foo_request; | 286 data.request = &https_foo_request; |
287 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); | 287 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
288 } | 288 } |
289 | 289 |
290 TEST(WebRequestConditionTest, TestPortFilter) { | 290 TEST(WebRequestConditionTest, TestPortFilter) { |
291 // Necessary for TestURLRequest. | 291 // Necessary for TestURLRequest. |
292 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 292 base::MessageLoopForIO message_loop; |
293 URLMatcher matcher; | 293 URLMatcher matcher; |
294 | 294 |
295 WebRequestConditionSet::AnyVector conditions; | 295 WebRequestConditionSet::AnyVector conditions; |
296 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 296 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
297 "{ \n" | 297 "{ \n" |
298 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 298 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
299 " \"url\": { \n" | 299 " \"url\": { \n" |
300 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. | 300 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010. |
301 " \"hostSuffix\": \"example.com\", \n" | 301 " \"hostSuffix\": \"example.com\", \n" |
302 " }, \n" | 302 " }, \n" |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 http_url_2000, net::DEFAULT_PRIORITY, NULL, &context); | 342 http_url_2000, net::DEFAULT_PRIORITY, NULL, &context); |
343 url_match_ids = matcher.MatchURL(http_url_2000); | 343 url_match_ids = matcher.MatchURL(http_url_2000); |
344 ASSERT_EQ(0u, url_match_ids.size()); | 344 ASSERT_EQ(0u, url_match_ids.size()); |
345 } | 345 } |
346 | 346 |
347 // 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 |
348 // 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 |
349 // impossible that both conditions are fulfilled at the same time. | 349 // impossible that both conditions are fulfilled at the same time. |
350 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 350 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
351 // Necessary for TestURLRequest. | 351 // Necessary for TestURLRequest. |
352 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | 352 base::MessageLoopForIO message_loop; |
353 URLMatcher matcher; | 353 URLMatcher matcher; |
354 | 354 |
355 std::string error; | 355 std::string error; |
356 scoped_ptr<WebRequestCondition> result; | 356 scoped_ptr<WebRequestCondition> result; |
357 | 357 |
358 // Test error on incompatible application stages for involved attributes. | 358 // Test error on incompatible application stages for involved attributes. |
359 error.clear(); | 359 error.clear(); |
360 result = WebRequestCondition::Create( | 360 result = WebRequestCondition::Create( |
361 NULL, | 361 NULL, |
362 matcher.condition_factory(), | 362 matcher.condition_factory(), |
363 *base::test::ParseJson( | 363 *base::test::ParseJson( |
364 "{ \n" | 364 "{ \n" |
365 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 365 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
366 // Pass a JS array with one empty object to each of the header | 366 // Pass a JS array with one empty object to each of the header |
367 // filters. | 367 // filters. |
368 " \"requestHeaders\": [{}], \n" | 368 " \"requestHeaders\": [{}], \n" |
369 " \"responseHeaders\": [{}], \n" | 369 " \"responseHeaders\": [{}], \n" |
370 "}"), | 370 "}"), |
371 &error); | 371 &error); |
372 EXPECT_FALSE(error.empty()); | 372 EXPECT_FALSE(error.empty()); |
373 EXPECT_FALSE(result.get()); | 373 EXPECT_FALSE(result.get()); |
374 } | 374 } |
375 | 375 |
376 } // namespace extensions | 376 } // namespace extensions |
OLD | NEW |