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 "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h " | |
14 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 13 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
15 #include "content/public/browser/resource_request_info.h" | 14 #include "content/public/browser/resource_request_info.h" |
16 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
18 | 17 |
19 namespace extensions { | 18 namespace extensions { |
20 | 19 |
21 namespace keys = declarative_webrequest_constants; | 20 namespace keys = declarative_webrequest_constants; |
22 namespace keys2 = url_matcher_constants; | 21 namespace keys2 = url_matcher_constants; |
23 | 22 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 64 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
66 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
67 "}"), | 66 "}"), |
68 &error); | 67 &error); |
69 EXPECT_EQ("", error); | 68 EXPECT_EQ("", error); |
70 ASSERT_TRUE(result.get()); | 69 ASSERT_TRUE(result.get()); |
71 | 70 |
72 net::TestURLRequestContext context; | 71 net::TestURLRequestContext context; |
73 net::TestURLRequest match_request( | 72 net::TestURLRequest match_request( |
74 GURL("http://www.example.com"), NULL, &context); | 73 GURL("http://www.example.com"), NULL, &context); |
74 URLMatcherConditionSet::Vector condition_sets; | |
75 result->GetURLMatcherConditionSets(&condition_sets); | |
76 ASSERT_EQ(1U, condition_sets.size()); | |
75 std::set<URLMatcherConditionSet::ID> url_matches; | 77 std::set<URLMatcherConditionSet::ID> url_matches; |
76 url_matches.insert(result->url_matcher_condition_set_id()); | 78 url_matches.insert(condition_sets[0]->id()); |
77 | 79 |
78 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 80 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
79 ResourceType::MAIN_FRAME, NULL, -1, -1); | 81 ResourceType::MAIN_FRAME, NULL, -1, -1); |
80 EXPECT_TRUE(result->IsFulfilled( | 82 DeclarativeWebRequestData request_data(&match_request, ON_BEFORE_REQUEST); |
81 url_matches, | 83 request_data.url_matches = &url_matches; |
82 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 84 EXPECT_TRUE(result->IsFulfilled(request_data)); |
83 | 85 |
84 net::TestURLRequest wrong_resource_type( | 86 net::TestURLRequest wrong_resource_type( |
85 GURL("https://www.example.com"), NULL, &context); | 87 GURL("https://www.example.com"), NULL, &context); |
86 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 88 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
87 ResourceType::SUB_FRAME, NULL, -1, -1); | 89 ResourceType::SUB_FRAME, NULL, -1, -1); |
88 EXPECT_FALSE(result->IsFulfilled( | 90 request_data.request = &wrong_resource_type; |
89 url_matches, | 91 EXPECT_FALSE(result->IsFulfilled(request_data)); |
90 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | |
91 } | 92 } |
92 | 93 |
93 // The IsFulfilledIndependentlyOfURL method tests that a condition has no | 94 // The IsFulfilledIndependentlyOfURL method tests that a condition has no |
94 // UrlFilter attributes, and all its (non-UrlFilter) attributes are satisfied. | 95 // UrlFilter attributes, and all its (non-UrlFilter) attributes are satisfied. |
95 // We test here that: | 96 // We test here that: |
96 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 97 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
97 // attributes are fulfilled. | 98 // attributes are fulfilled. |
98 // 2. An empty condition (in particular, without UrlFilter attributes) is | 99 // 2. An empty condition (in particular, without UrlFilter attributes) is |
99 // always fulfilled. | 100 // always fulfilled. |
100 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never | 101 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 " \"thirdPartyForCookies\": false, \n" | 183 " \"thirdPartyForCookies\": false, \n" |
183 "}"), | 184 "}"), |
184 &error); | 185 &error); |
185 EXPECT_EQ("", error); | 186 EXPECT_EQ("", error); |
186 ASSERT_TRUE(condition_both.get()); | 187 ASSERT_TRUE(condition_both.get()); |
187 #undef MATCHING_URL_FILTER | 188 #undef MATCHING_URL_FILTER |
188 | 189 |
189 net::TestURLRequestContext context; | 190 net::TestURLRequestContext context; |
190 net::TestURLRequest https_request(GURL(kUrl), NULL, &context); | 191 net::TestURLRequest https_request(GURL(kUrl), NULL, &context); |
191 std::set<URLMatcherConditionSet::ID> empty_url_matches; | 192 std::set<URLMatcherConditionSet::ID> empty_url_matches; |
193 DeclarativeWebRequestData request_data(NULL, ON_BEFORE_REQUEST); | |
194 request_data.url_matches = &empty_url_matches; | |
Jeffrey Yasskin
2012/12/19 08:23:06
These assignments into request_data are somewhat a
Jeffrey Yasskin
2012/12/20 01:27:35
And I've reverted them since ConditionSet needed t
| |
192 | 195 |
193 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | 196 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its |
194 // attributes are fulfilled. | 197 // attributes are fulfilled. |
195 EXPECT_FALSE(condition_no_url_false->IsFulfilled( | 198 request_data.request = &https_request; |
196 empty_url_matches, | 199 EXPECT_FALSE(condition_no_url_false->IsFulfilled(request_data)); |
197 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
198 | 200 |
199 EXPECT_TRUE(condition_no_url_true->IsFulfilled( | 201 EXPECT_TRUE(condition_no_url_true->IsFulfilled(request_data)); |
200 empty_url_matches, | |
201 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
202 | 202 |
203 // 2. An empty condition (in particular, without UrlFilter attributes) is | 203 // 2. An empty condition (in particular, without UrlFilter attributes) is |
204 // always fulfilled. | 204 // always fulfilled. |
205 EXPECT_TRUE(condition_empty->IsFulfilled( | 205 EXPECT_TRUE(condition_empty->IsFulfilled(request_data)); |
206 empty_url_matches, | |
207 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
208 | 206 |
209 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never | 207 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never |
210 // fulfilled. | 208 // fulfilled. |
211 EXPECT_FALSE(condition_both->IsFulfilled( | 209 EXPECT_FALSE(condition_both->IsFulfilled(request_data)); |
212 empty_url_matches, | |
213 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
214 | 210 |
215 // 4. A condition with only a UrlFilter attribute is never fulfilled. | 211 // 4. A condition with only a UrlFilter attribute is never fulfilled. |
216 EXPECT_FALSE(condition_url->IsFulfilled( | 212 EXPECT_FALSE(condition_url->IsFulfilled(request_data)); |
217 empty_url_matches, | |
218 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
219 } | 213 } |
220 | 214 |
221 TEST(WebRequestConditionTest, CreateConditionSet) { | 215 TEST(WebRequestConditionTest, CreateConditionSet) { |
222 // Necessary for TestURLRequest. | 216 // Necessary for TestURLRequest. |
223 MessageLoop message_loop(MessageLoop::TYPE_IO); | 217 MessageLoop message_loop(MessageLoop::TYPE_IO); |
224 URLMatcher matcher; | 218 URLMatcher matcher; |
225 | 219 |
226 WebRequestConditionSet::AnyVector conditions; | 220 WebRequestConditionSet::AnyVector conditions; |
227 | 221 |
228 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 222 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 matcher.AddConditionSets(url_matcher_condition_set); | 259 matcher.AddConditionSets(url_matcher_condition_set); |
266 | 260 |
267 std::set<URLMatcherConditionSet::ID> url_match_ids; | 261 std::set<URLMatcherConditionSet::ID> url_match_ids; |
268 | 262 |
269 // Test that the result is correct and matches http://www.example.com and | 263 // Test that the result is correct and matches http://www.example.com and |
270 // https://www.example.com | 264 // https://www.example.com |
271 GURL http_url("http://www.example.com"); | 265 GURL http_url("http://www.example.com"); |
272 net::TestURLRequestContext context; | 266 net::TestURLRequestContext context; |
273 net::TestURLRequest http_request(http_url, NULL, &context); | 267 net::TestURLRequest http_request(http_url, NULL, &context); |
274 url_match_ids = matcher.MatchURL(http_url); | 268 url_match_ids = matcher.MatchURL(http_url); |
275 EXPECT_TRUE(result->IsFulfilled( | 269 DeclarativeWebRequestData request_data(NULL, ON_BEFORE_REQUEST); |
276 url_match_ids, | 270 |
277 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))); | 271 request_data.request = &http_request; |
272 request_data.url_matches = &url_match_ids; | |
273 EXPECT_TRUE(result->IsFulfilled(request_data)); | |
278 | 274 |
279 GURL https_url("https://www.example.com"); | 275 GURL https_url("https://www.example.com"); |
280 url_match_ids = matcher.MatchURL(https_url); | 276 url_match_ids = matcher.MatchURL(https_url); |
281 net::TestURLRequest https_request(https_url, NULL, &context); | 277 net::TestURLRequest https_request(https_url, NULL, &context); |
282 EXPECT_TRUE(result->IsFulfilled( | 278 request_data.request = &https_request; |
283 url_match_ids, | 279 request_data.url_matches = &url_match_ids; |
284 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | 280 EXPECT_TRUE(result->IsFulfilled(request_data)); |
285 | 281 |
286 // Check that both, hostPrefix and hostSuffix are evaluated. | 282 // Check that both, hostPrefix and hostSuffix are evaluated. |
287 GURL https_foo_url("https://foo.example.com"); | 283 GURL https_foo_url("https://foo.example.com"); |
288 url_match_ids = matcher.MatchURL(https_foo_url); | 284 url_match_ids = matcher.MatchURL(https_foo_url); |
289 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 285 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
290 EXPECT_FALSE(result->IsFulfilled( | 286 request_data.request = &https_foo_request; |
291 url_match_ids, | 287 request_data.url_matches = &url_match_ids; |
292 WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST))); | 288 EXPECT_FALSE(result->IsFulfilled(request_data)); |
293 } | 289 } |
294 | 290 |
295 TEST(WebRequestConditionTest, TestPortFilter) { | 291 TEST(WebRequestConditionTest, TestPortFilter) { |
296 // Necessary for TestURLRequest. | 292 // Necessary for TestURLRequest. |
297 MessageLoop message_loop(MessageLoop::TYPE_IO); | 293 MessageLoop message_loop(MessageLoop::TYPE_IO); |
298 URLMatcher matcher; | 294 URLMatcher matcher; |
299 | 295 |
300 linked_ptr<json_schema_compiler::any::Any> any_condition = | 296 linked_ptr<json_schema_compiler::any::Any> any_condition = |
301 make_linked_ptr(new json_schema_compiler::any::Any); | 297 make_linked_ptr(new json_schema_compiler::any::Any); |
302 any_condition->Init(*base::test::ParseJson( | 298 any_condition->Init(*base::test::ParseJson( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
375 // filters. | 371 // filters. |
376 " \"requestHeaders\": [{}], \n" | 372 " \"requestHeaders\": [{}], \n" |
377 " \"responseHeaders\": [{}], \n" | 373 " \"responseHeaders\": [{}], \n" |
378 "}"), | 374 "}"), |
379 &error); | 375 &error); |
380 EXPECT_FALSE(error.empty()); | 376 EXPECT_FALSE(error.empty()); |
381 EXPECT_FALSE(result.get()); | 377 EXPECT_FALSE(result.get()); |
382 } | 378 } |
383 | 379 |
384 } // namespace extensions | 380 } // namespace extensions |
OLD | NEW |