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 10 matching lines...) Expand all Loading... |
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 MessageLoop message_loop(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 | |
32 // Test wrong condition name passed. | 31 // Test wrong condition name passed. |
33 error.clear(); | 32 error.clear(); |
34 result = WebRequestCondition::Create( | 33 result = WebRequestCondition::Create( |
35 matcher.condition_factory(), | 34 matcher.condition_factory(), |
36 *base::test::ParseJson( | 35 *base::test::ParseJson( |
37 "{ \"invalid\": \"foobar\", \n" | 36 "{ \"invalid\": \"foobar\", \n" |
38 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 37 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
39 "}"), | 38 "}"), |
40 &error); | 39 &error); |
41 EXPECT_FALSE(error.empty()); | 40 EXPECT_FALSE(error.empty()); |
(...skipping 22 matching lines...) Expand all Loading... |
64 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 63 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
65 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 64 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
66 "}"), | 65 "}"), |
67 &error); | 66 &error); |
68 EXPECT_EQ("", error); | 67 EXPECT_EQ("", error); |
69 ASSERT_TRUE(result.get()); | 68 ASSERT_TRUE(result.get()); |
70 | 69 |
71 URLMatcherConditionSet::Vector url_matcher_condition_set; | 70 URLMatcherConditionSet::Vector url_matcher_condition_set; |
72 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 71 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
73 matcher.AddConditionSets(url_matcher_condition_set); | 72 matcher.AddConditionSets(url_matcher_condition_set); |
74 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
75 | 73 |
76 net::TestURLRequestContext context; | 74 net::TestURLRequestContext context; |
77 GURL http_url("http://www.example.com"); | 75 const GURL http_url("http://www.example.com"); |
78 net::TestURLRequest match_request(http_url, NULL, &context); | 76 net::TestURLRequest match_request(http_url, NULL, &context); |
79 url_match_ids = matcher.MatchURL(http_url); | 77 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
80 EXPECT_EQ(1u, url_match_ids.size()); | 78 WebRequestDataWithMatchIds request_data(&data); |
81 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 79 request_data.url_match_ids = matcher.MatchURL(http_url); |
82 ResourceType::MAIN_FRAME, NULL, -1, -1); | 80 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
83 EXPECT_TRUE(result->IsFulfilled( | 81 content::ResourceRequestInfo::AllocateForTesting( |
84 url_match_ids, | 82 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); |
85 DeclarativeWebRequestData(&match_request, ON_BEFORE_REQUEST))); | 83 EXPECT_TRUE(result->IsFulfilled(request_data)); |
86 | 84 |
87 GURL https_url("https://www.example.com"); | 85 const GURL https_url("https://www.example.com"); |
88 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); | 86 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); |
89 url_match_ids = matcher.MatchURL(https_url); | 87 data.request = &wrong_resource_type; |
| 88 request_data.url_match_ids = matcher.MatchURL(http_url); |
90 // Make sure IsFulfilled does not fail because of URL matching. | 89 // Make sure IsFulfilled does not fail because of URL matching. |
91 EXPECT_EQ(1u, url_match_ids.size()); | 90 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
92 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 91 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
93 ResourceType::SUB_FRAME, NULL, -1, -1); | 92 ResourceType::SUB_FRAME, NULL, -1, -1); |
94 EXPECT_FALSE(result->IsFulfilled( | 93 EXPECT_FALSE(result->IsFulfilled(request_data)); |
95 url_match_ids, | 94 } |
96 DeclarativeWebRequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 95 |
| 96 TEST(WebRequestConditionTest, CreateConditionFirstPartyForCookies) { |
| 97 // Necessary for TestURLRequest. |
| 98 MessageLoop message_loop(MessageLoop::TYPE_IO); |
| 99 URLMatcher matcher; |
| 100 |
| 101 std::string error; |
| 102 scoped_ptr<WebRequestCondition> result; |
| 103 |
| 104 result = WebRequestCondition::Create( |
| 105 matcher.condition_factory(), |
| 106 *base::test::ParseJson( |
| 107 "{ \n" |
| 108 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n" |
| 109 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
| 110 "}"), |
| 111 &error); |
| 112 EXPECT_EQ("", error); |
| 113 ASSERT_TRUE(result.get()); |
| 114 |
| 115 URLMatcherConditionSet::Vector url_matcher_condition_set; |
| 116 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
| 117 matcher.AddConditionSets(url_matcher_condition_set); |
| 118 |
| 119 net::TestURLRequestContext context; |
| 120 const GURL http_url("http://www.example.com"); |
| 121 const GURL first_party_url("http://fpfc.example.com"); |
| 122 net::TestURLRequest match_request(http_url, NULL, &context); |
| 123 WebRequestData data(&match_request, ON_BEFORE_REQUEST); |
| 124 WebRequestDataWithMatchIds request_data(&data); |
| 125 request_data.url_match_ids = matcher.MatchURL(http_url); |
| 126 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
| 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()); |
| 129 content::ResourceRequestInfo::AllocateForTesting( |
| 130 &match_request, ResourceType::MAIN_FRAME, NULL, -1, -1); |
| 131 EXPECT_TRUE(result->IsFulfilled(request_data)); |
97 } | 132 } |
98 | 133 |
99 // Conditions without UrlFilter attributes need to be independent of URL | 134 // Conditions without UrlFilter attributes need to be independent of URL |
100 // matching results. We test here that: | 135 // matching results. We test here that: |
101 // 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 |
102 // attributes are fulfilled. | 137 // attributes are fulfilled. |
103 // 2. An empty condition (in particular, without UrlFilter attributes) is | 138 // 2. An empty condition (in particular, without UrlFilter attributes) is |
104 // always fulfilled. | 139 // always fulfilled. |
105 TEST(WebRequestConditionTest, NoUrlAttributes) { | 140 TEST(WebRequestConditionTest, NoUrlAttributes) { |
106 // Necessary for TestURLRequest. | 141 // Necessary for TestURLRequest. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 181 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
147 " \"thirdPartyForCookies\": true, \n" | 182 " \"thirdPartyForCookies\": true, \n" |
148 "}"), | 183 "}"), |
149 &error); | 184 &error); |
150 EXPECT_EQ("", error); | 185 EXPECT_EQ("", error); |
151 ASSERT_TRUE(condition_no_url_false.get()); | 186 ASSERT_TRUE(condition_no_url_false.get()); |
152 | 187 |
153 net::TestURLRequestContext context; | 188 net::TestURLRequestContext context; |
154 net::TestURLRequest https_request( | 189 net::TestURLRequest https_request( |
155 GURL("https://www.example.com"), NULL, &context); | 190 GURL("https://www.example.com"), NULL, &context); |
156 std::set<URLMatcherConditionSet::ID> dummy_match_set; | |
157 | 191 |
158 // 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 |
159 // attributes are fulfilled. | 193 // attributes are fulfilled. |
160 EXPECT_FALSE(condition_no_url_false->IsFulfilled( | 194 WebRequestData data(&https_request, ON_BEFORE_REQUEST); |
161 dummy_match_set, | 195 EXPECT_FALSE( |
162 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 196 condition_no_url_false->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
163 | 197 |
164 EXPECT_TRUE(condition_no_url_true->IsFulfilled( | 198 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
165 dummy_match_set, | 199 EXPECT_TRUE( |
166 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | 200 condition_no_url_true->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
167 | 201 |
168 // 2. An empty condition (in particular, without UrlFilter attributes) is | 202 // 2. An empty condition (in particular, without UrlFilter attributes) is |
169 // always fulfilled. | 203 // always fulfilled. |
170 EXPECT_TRUE(condition_empty->IsFulfilled( | 204 data = WebRequestData(&https_request, ON_BEFORE_REQUEST); |
171 dummy_match_set, | 205 EXPECT_TRUE(condition_empty->IsFulfilled(WebRequestDataWithMatchIds(&data))); |
172 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | |
173 } | 206 } |
174 | 207 |
175 TEST(WebRequestConditionTest, CreateConditionSet) { | 208 TEST(WebRequestConditionTest, CreateConditionSet) { |
176 // Necessary for TestURLRequest. | 209 // Necessary for TestURLRequest. |
177 MessageLoop message_loop(MessageLoop::TYPE_IO); | 210 MessageLoop message_loop(MessageLoop::TYPE_IO); |
178 URLMatcher matcher; | 211 URLMatcher matcher; |
179 | 212 |
180 WebRequestConditionSet::AnyVector conditions; | 213 WebRequestConditionSet::AnyVector conditions; |
181 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 214 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
182 "{ \n" | 215 "{ \n" |
(...skipping 20 matching lines...) Expand all Loading... |
203 conditions, &error); | 236 conditions, &error); |
204 EXPECT_EQ("", error); | 237 EXPECT_EQ("", error); |
205 ASSERT_TRUE(result.get()); | 238 ASSERT_TRUE(result.get()); |
206 EXPECT_EQ(2u, result->conditions().size()); | 239 EXPECT_EQ(2u, result->conditions().size()); |
207 | 240 |
208 // Tell the URLMatcher about our shiny new patterns. | 241 // Tell the URLMatcher about our shiny new patterns. |
209 URLMatcherConditionSet::Vector url_matcher_condition_set; | 242 URLMatcherConditionSet::Vector url_matcher_condition_set; |
210 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 243 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
211 matcher.AddConditionSets(url_matcher_condition_set); | 244 matcher.AddConditionSets(url_matcher_condition_set); |
212 | 245 |
213 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
214 | |
215 // 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 |
216 // https://www.example.com | 247 // https://www.example.com |
217 GURL http_url("http://www.example.com"); | 248 GURL http_url("http://www.example.com"); |
218 net::TestURLRequestContext context; | 249 net::TestURLRequestContext context; |
219 net::TestURLRequest http_request(http_url, NULL, &context); | 250 net::TestURLRequest http_request(http_url, NULL, &context); |
220 url_match_ids = matcher.MatchURL(http_url); | 251 WebRequestData data(&http_request, ON_BEFORE_REQUEST); |
221 EXPECT_EQ(1u, url_match_ids.size()); | 252 WebRequestDataWithMatchIds request_data(&data); |
222 EXPECT_TRUE(result->IsFulfilled( | 253 request_data.url_match_ids = matcher.MatchURL(http_url); |
223 *(url_match_ids.begin()), | 254 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
224 url_match_ids, | 255 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
225 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST))); | 256 request_data)); |
226 | 257 |
227 GURL https_url("https://www.example.com"); | 258 GURL https_url("https://www.example.com"); |
228 url_match_ids = matcher.MatchURL(https_url); | 259 request_data.url_match_ids = matcher.MatchURL(https_url); |
229 EXPECT_EQ(1u, url_match_ids.size()); | 260 EXPECT_EQ(1u, request_data.url_match_ids.size()); |
230 net::TestURLRequest https_request(https_url, NULL, &context); | 261 net::TestURLRequest https_request(https_url, NULL, &context); |
231 EXPECT_TRUE(result->IsFulfilled( | 262 data.request = &https_request; |
232 *(url_match_ids.begin()), | 263 EXPECT_TRUE(result->IsFulfilled(*(request_data.url_match_ids.begin()), |
233 url_match_ids, | 264 request_data)); |
234 DeclarativeWebRequestData(&https_request, ON_BEFORE_REQUEST))); | |
235 | 265 |
236 // Check that both, hostPrefix and hostSuffix are evaluated. | 266 // Check that both, hostPrefix and hostSuffix are evaluated. |
237 GURL https_foo_url("https://foo.example.com"); | 267 GURL https_foo_url("https://foo.example.com"); |
238 url_match_ids = matcher.MatchURL(https_foo_url); | 268 request_data.url_match_ids = matcher.MatchURL(https_foo_url); |
239 EXPECT_EQ(0u, url_match_ids.size()); | 269 EXPECT_EQ(0u, request_data.url_match_ids.size()); |
240 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 270 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
241 EXPECT_FALSE(result->IsFulfilled( | 271 data.request = &https_foo_request; |
242 -1, | 272 EXPECT_FALSE(result->IsFulfilled(-1, request_data)); |
243 url_match_ids, | |
244 DeclarativeWebRequestData(&https_foo_request, ON_BEFORE_REQUEST))); | |
245 } | 273 } |
246 | 274 |
247 TEST(WebRequestConditionTest, TestPortFilter) { | 275 TEST(WebRequestConditionTest, TestPortFilter) { |
248 // Necessary for TestURLRequest. | 276 // Necessary for TestURLRequest. |
249 MessageLoop message_loop(MessageLoop::TYPE_IO); | 277 MessageLoop message_loop(MessageLoop::TYPE_IO); |
250 URLMatcher matcher; | 278 URLMatcher matcher; |
251 | 279 |
252 WebRequestConditionSet::AnyVector conditions; | 280 WebRequestConditionSet::AnyVector conditions; |
253 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( | 281 conditions.push_back(linked_ptr<base::Value>(base::test::ParseJson( |
254 "{ \n" | 282 "{ \n" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 // 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 |
303 // impossible that both conditions are fulfilled at the same time. | 331 // impossible that both conditions are fulfilled at the same time. |
304 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { | 332 TEST(WebRequestConditionTest, ConditionsWithConflictingStages) { |
305 // Necessary for TestURLRequest. | 333 // Necessary for TestURLRequest. |
306 MessageLoop message_loop(MessageLoop::TYPE_IO); | 334 MessageLoop message_loop(MessageLoop::TYPE_IO); |
307 URLMatcher matcher; | 335 URLMatcher matcher; |
308 | 336 |
309 std::string error; | 337 std::string error; |
310 scoped_ptr<WebRequestCondition> result; | 338 scoped_ptr<WebRequestCondition> result; |
311 | 339 |
312 DictionaryValue condition_value; | |
313 condition_value.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); | |
314 | |
315 | |
316 // Test error on incompatible application stages for involved attributes. | 340 // Test error on incompatible application stages for involved attributes. |
317 error.clear(); | 341 error.clear(); |
318 result = WebRequestCondition::Create( | 342 result = WebRequestCondition::Create( |
319 matcher.condition_factory(), | 343 matcher.condition_factory(), |
320 *base::test::ParseJson( | 344 *base::test::ParseJson( |
321 "{ \n" | 345 "{ \n" |
322 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 346 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
323 // 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 |
324 // filters. | 348 // filters. |
325 " \"requestHeaders\": [{}], \n" | 349 " \"requestHeaders\": [{}], \n" |
326 " \"responseHeaders\": [{}], \n" | 350 " \"responseHeaders\": [{}], \n" |
327 "}"), | 351 "}"), |
328 &error); | 352 &error); |
329 EXPECT_FALSE(error.empty()); | 353 EXPECT_FALSE(error.empty()); |
330 EXPECT_FALSE(result.get()); | 354 EXPECT_FALSE(result.get()); |
331 } | 355 } |
332 | 356 |
333 } // namespace extensions | 357 } // namespace extensions |
OLD | NEW |