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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_condition.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/test/values_test_util.h"
12 #include "base/values.h"
13 #include "components/url_matcher/url_matcher_constants.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "extensions/browser/api/declarative_webrequest/webrequest_constants.h"
16 #include "net/base/request_priority.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using content::ResourceType
;
22 using url_matcher::URLMatcher
;
23 using url_matcher::URLMatcherConditionSet
;
25 namespace extensions
{
27 TEST(WebRequestConditionTest
, CreateCondition
) {
28 // Necessary for TestURLRequest.
29 base::MessageLoopForIO message_loop
;
33 scoped_ptr
<WebRequestCondition
> result
;
35 // Test wrong condition name passed.
37 result
= WebRequestCondition::Create(
39 matcher
.condition_factory(),
40 *base::test::ParseJson(
41 "{ \"invalid\": \"foobar\", \n"
42 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
45 EXPECT_FALSE(error
.empty());
46 EXPECT_FALSE(result
.get());
48 // Test wrong datatype in host_suffix.
50 result
= WebRequestCondition::Create(
52 matcher
.condition_factory(),
53 *base::test::ParseJson(
56 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
59 EXPECT_FALSE(error
.empty());
60 EXPECT_FALSE(result
.get());
62 // Test success (can we support multiple criteria?)
64 result
= WebRequestCondition::Create(
66 matcher
.condition_factory(),
67 *base::test::ParseJson(
69 " \"resourceType\": [\"main_frame\"], \n"
70 " \"url\": { \"hostSuffix\": \"example.com\" }, \n"
71 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
75 ASSERT_TRUE(result
.get());
77 URLMatcherConditionSet::Vector url_matcher_condition_set
;
78 result
->GetURLMatcherConditionSets(&url_matcher_condition_set
);
79 matcher
.AddConditionSets(url_matcher_condition_set
);
81 net::TestURLRequestContext context
;
82 const GURL
http_url("http://www.example.com");
83 scoped_ptr
<net::URLRequest
> match_request(context
.CreateRequest(
84 http_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
85 WebRequestData
data(match_request
.get(), ON_BEFORE_REQUEST
);
86 WebRequestDataWithMatchIds
request_data(&data
);
87 request_data
.url_match_ids
= matcher
.MatchURL(http_url
);
88 EXPECT_EQ(1u, request_data
.url_match_ids
.size());
89 content::ResourceRequestInfo::AllocateForTesting(
91 content::RESOURCE_TYPE_MAIN_FRAME
,
97 EXPECT_TRUE(result
->IsFulfilled(request_data
));
99 const GURL
https_url("https://www.example.com");
100 scoped_ptr
<net::URLRequest
> wrong_resource_type(context
.CreateRequest(
101 https_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
102 data
.request
= wrong_resource_type
.get();
103 request_data
.url_match_ids
= matcher
.MatchURL(http_url
);
104 // Make sure IsFulfilled does not fail because of URL matching.
105 EXPECT_EQ(1u, request_data
.url_match_ids
.size());
106 content::ResourceRequestInfo::AllocateForTesting(
107 wrong_resource_type
.get(),
108 content::RESOURCE_TYPE_SUB_FRAME
,
114 EXPECT_FALSE(result
->IsFulfilled(request_data
));
117 TEST(WebRequestConditionTest
, CreateConditionFirstPartyForCookies
) {
118 // Necessary for TestURLRequest.
119 base::MessageLoopForIO message_loop
;
123 scoped_ptr
<WebRequestCondition
> result
;
125 result
= WebRequestCondition::Create(
127 matcher
.condition_factory(),
128 *base::test::ParseJson(
130 " \"firstPartyForCookiesUrl\": { \"hostPrefix\": \"fpfc\"}, \n"
131 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
134 EXPECT_EQ("", error
);
135 ASSERT_TRUE(result
.get());
137 URLMatcherConditionSet::Vector url_matcher_condition_set
;
138 result
->GetURLMatcherConditionSets(&url_matcher_condition_set
);
139 matcher
.AddConditionSets(url_matcher_condition_set
);
141 net::TestURLRequestContext context
;
142 const GURL
http_url("http://www.example.com");
143 const GURL
first_party_url("http://fpfc.example.com");
144 scoped_ptr
<net::URLRequest
> match_request(context
.CreateRequest(
145 http_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
146 WebRequestData
data(match_request
.get(), ON_BEFORE_REQUEST
);
147 WebRequestDataWithMatchIds
request_data(&data
);
148 request_data
.url_match_ids
= matcher
.MatchURL(http_url
);
149 EXPECT_EQ(0u, request_data
.url_match_ids
.size());
150 request_data
.first_party_url_match_ids
= matcher
.MatchURL(first_party_url
);
151 EXPECT_EQ(1u, request_data
.first_party_url_match_ids
.size());
152 content::ResourceRequestInfo::AllocateForTesting(
154 content::RESOURCE_TYPE_MAIN_FRAME
,
160 EXPECT_TRUE(result
->IsFulfilled(request_data
));
163 // Conditions without UrlFilter attributes need to be independent of URL
164 // matching results. We test here that:
165 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
166 // attributes are fulfilled.
167 // 2. An empty condition (in particular, without UrlFilter attributes) is
169 TEST(WebRequestConditionTest
, NoUrlAttributes
) {
170 // Necessary for TestURLRequest.
171 base::MessageLoopForIO message_loop
;
175 // The empty condition.
177 scoped_ptr
<WebRequestCondition
> condition_empty
= WebRequestCondition::Create(
179 matcher
.condition_factory(),
180 *base::test::ParseJson(
182 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
185 EXPECT_EQ("", error
);
186 ASSERT_TRUE(condition_empty
.get());
188 // A condition without a UrlFilter attribute, which is always true.
190 scoped_ptr
<WebRequestCondition
> condition_no_url_true
=
191 WebRequestCondition::Create(
193 matcher
.condition_factory(),
194 *base::test::ParseJson(
196 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", "
198 // There is no "1st party for cookies" URL in the requests below,
199 // therefore all requests are considered first party for cookies.
200 " \"thirdPartyForCookies\": false, \n"
203 EXPECT_EQ("", error
);
204 ASSERT_TRUE(condition_no_url_true
.get());
206 // A condition without a UrlFilter attribute, which is always false.
208 scoped_ptr
<WebRequestCondition
> condition_no_url_false
=
209 WebRequestCondition::Create(
211 matcher
.condition_factory(),
212 *base::test::ParseJson(
214 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", "
216 " \"thirdPartyForCookies\": true, \n"
219 EXPECT_EQ("", error
);
220 ASSERT_TRUE(condition_no_url_false
.get());
222 net::TestURLRequestContext context
;
223 scoped_ptr
<net::URLRequest
> https_request(context
.CreateRequest(
224 GURL("https://www.example.com"), net::DEFAULT_PRIORITY
, NULL
, NULL
));
226 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
227 // attributes are fulfilled.
228 WebRequestData
data(https_request
.get(), ON_BEFORE_REQUEST
);
230 condition_no_url_false
->IsFulfilled(WebRequestDataWithMatchIds(&data
)));
232 data
= WebRequestData(https_request
.get(), ON_BEFORE_REQUEST
);
234 condition_no_url_true
->IsFulfilled(WebRequestDataWithMatchIds(&data
)));
236 // 2. An empty condition (in particular, without UrlFilter attributes) is
238 data
= WebRequestData(https_request
.get(), ON_BEFORE_REQUEST
);
239 EXPECT_TRUE(condition_empty
->IsFulfilled(WebRequestDataWithMatchIds(&data
)));
242 TEST(WebRequestConditionTest
, CreateConditionSet
) {
243 // Necessary for TestURLRequest.
244 base::MessageLoopForIO message_loop
;
247 WebRequestConditionSet::AnyVector conditions
;
248 conditions
.push_back(linked_ptr
<base::Value
>(base::test::ParseJson(
250 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
252 " \"hostSuffix\": \"example.com\", \n"
253 " \"schemes\": [\"http\"], \n"
256 conditions
.push_back(linked_ptr
<base::Value
>(base::test::ParseJson(
258 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
260 " \"hostSuffix\": \"example.com\", \n"
261 " \"hostPrefix\": \"www\", \n"
262 " \"schemes\": [\"https\"], \n"
268 scoped_ptr
<WebRequestConditionSet
> result
= WebRequestConditionSet::Create(
269 NULL
, matcher
.condition_factory(), conditions
, &error
);
270 EXPECT_EQ("", error
);
271 ASSERT_TRUE(result
.get());
272 EXPECT_EQ(2u, result
->conditions().size());
274 // Tell the URLMatcher about our shiny new patterns.
275 URLMatcherConditionSet::Vector url_matcher_condition_set
;
276 result
->GetURLMatcherConditionSets(&url_matcher_condition_set
);
277 matcher
.AddConditionSets(url_matcher_condition_set
);
279 // Test that the result is correct and matches http://www.example.com and
280 // https://www.example.com
281 GURL
http_url("http://www.example.com");
282 net::TestURLRequestContext context
;
283 scoped_ptr
<net::URLRequest
> http_request(context
.CreateRequest(
284 http_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
285 WebRequestData
data(http_request
.get(), ON_BEFORE_REQUEST
);
286 WebRequestDataWithMatchIds
request_data(&data
);
287 request_data
.url_match_ids
= matcher
.MatchURL(http_url
);
288 EXPECT_EQ(1u, request_data
.url_match_ids
.size());
289 EXPECT_TRUE(result
->IsFulfilled(*(request_data
.url_match_ids
.begin()),
292 GURL
https_url("https://www.example.com");
293 request_data
.url_match_ids
= matcher
.MatchURL(https_url
);
294 EXPECT_EQ(1u, request_data
.url_match_ids
.size());
295 scoped_ptr
<net::URLRequest
> https_request(context
.CreateRequest(
296 https_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
297 data
.request
= https_request
.get();
298 EXPECT_TRUE(result
->IsFulfilled(*(request_data
.url_match_ids
.begin()),
301 // Check that both, hostPrefix and hostSuffix are evaluated.
302 GURL
https_foo_url("https://foo.example.com");
303 request_data
.url_match_ids
= matcher
.MatchURL(https_foo_url
);
304 EXPECT_EQ(0u, request_data
.url_match_ids
.size());
305 scoped_ptr
<net::URLRequest
> https_foo_request(context
.CreateRequest(
306 https_foo_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
307 data
.request
= https_foo_request
.get();
308 EXPECT_FALSE(result
->IsFulfilled(-1, request_data
));
311 TEST(WebRequestConditionTest
, TestPortFilter
) {
312 // Necessary for TestURLRequest.
313 base::MessageLoopForIO message_loop
;
316 WebRequestConditionSet::AnyVector conditions
;
317 conditions
.push_back(linked_ptr
<base::Value
>(base::test::ParseJson(
319 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
321 " \"ports\": [80, [1000, 1010]], \n" // Allow 80;1000-1010.
322 " \"hostSuffix\": \"example.com\", \n"
328 scoped_ptr
<WebRequestConditionSet
> result
= WebRequestConditionSet::Create(
329 NULL
, matcher
.condition_factory(), conditions
, &error
);
330 EXPECT_EQ("", error
);
331 ASSERT_TRUE(result
.get());
332 EXPECT_EQ(1u, result
->conditions().size());
334 // Tell the URLMatcher about our shiny new patterns.
335 URLMatcherConditionSet::Vector url_matcher_condition_set
;
336 result
->GetURLMatcherConditionSets(&url_matcher_condition_set
);
337 matcher
.AddConditionSets(url_matcher_condition_set
);
339 std::set
<URLMatcherConditionSet::ID
> url_match_ids
;
341 // Test various URLs.
342 GURL
http_url("http://www.example.com");
343 net::TestURLRequestContext context
;
344 scoped_ptr
<net::URLRequest
> http_request(context
.CreateRequest(
345 http_url
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
346 url_match_ids
= matcher
.MatchURL(http_url
);
347 ASSERT_EQ(1u, url_match_ids
.size());
349 GURL
http_url_80("http://www.example.com:80");
350 scoped_ptr
<net::URLRequest
> http_request_80(context
.CreateRequest(
351 http_url_80
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
352 url_match_ids
= matcher
.MatchURL(http_url_80
);
353 ASSERT_EQ(1u, url_match_ids
.size());
355 GURL
http_url_1000("http://www.example.com:1000");
356 scoped_ptr
<net::URLRequest
> http_request_1000(context
.CreateRequest(
357 http_url_1000
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
358 url_match_ids
= matcher
.MatchURL(http_url_1000
);
359 ASSERT_EQ(1u, url_match_ids
.size());
361 GURL
http_url_2000("http://www.example.com:2000");
362 scoped_ptr
<net::URLRequest
> http_request_2000(context
.CreateRequest(
363 http_url_2000
, net::DEFAULT_PRIORITY
, NULL
, NULL
));
364 url_match_ids
= matcher
.MatchURL(http_url_2000
);
365 ASSERT_EQ(0u, url_match_ids
.size());
368 // Create a condition with two attributes: one on the request header and one on
369 // the response header. The Create() method should fail and complain that it is
370 // impossible that both conditions are fulfilled at the same time.
371 TEST(WebRequestConditionTest
, ConditionsWithConflictingStages
) {
372 // Necessary for TestURLRequest.
373 base::MessageLoopForIO message_loop
;
377 scoped_ptr
<WebRequestCondition
> result
;
379 // Test error on incompatible application stages for involved attributes.
381 result
= WebRequestCondition::Create(
383 matcher
.condition_factory(),
384 *base::test::ParseJson(
386 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
387 // Pass a JS array with one empty object to each of the header
389 " \"requestHeaders\": [{}], \n"
390 " \"responseHeaders\": [{}], \n"
393 EXPECT_FALSE(error
.empty());
394 EXPECT_FALSE(result
.get());
397 } // namespace extensions