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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); |
79 | 79 |
80 net::TestURLRequest wrong_resource_type( | 80 net::TestURLRequest wrong_resource_type( |
81 GURL("https://www.example.com"), NULL, &context); | 81 GURL("https://www.example.com"), NULL, &context); |
82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
83 ResourceType::SUB_FRAME, NULL, -1, -1); | 83 ResourceType::SUB_FRAME, NULL, -1, -1); |
84 EXPECT_FALSE(result->IsFulfilled( | 84 EXPECT_FALSE(result->IsFulfilled( |
85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
86 } | 86 } |
87 | 87 |
88 // The IsFulfilledIndependentlyOfURL method tests that a condition has no | |
89 // UrlFilter attributes, and all its (non-UrlFilter) attributes are satisfied. | |
90 // We test here that: | |
91 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | |
92 // attributes are fulfilled. | |
93 // 2. An empty condition (in particular, without UrlFilter attributes) is | |
94 // always fulfilled. | |
95 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never | |
96 // fulfilled. | |
97 // 4. A condition with only a UrlFilter attribute is never fulfilled. | |
98 TEST(WebRequestConditionTest, IsFulfilledIndependentlyOfURL) { | |
99 // Necessary for TestURLRequest. | |
100 MessageLoop message_loop(MessageLoop::TYPE_IO); | |
101 URLMatcher matcher; | |
102 std::string error; | |
103 | |
104 // The empty condition. | |
105 error.clear(); | |
106 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( | |
107 matcher.condition_factory(), | |
108 *base::test::ParseJson( | |
109 "{ \n" | |
110 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
111 "}"), | |
112 &error); | |
113 EXPECT_EQ("", error); | |
114 ASSERT_TRUE(condition_empty.get()); | |
115 | |
116 // A condition without a UrlFilter attribute, which is always true. | |
117 error.clear(); | |
118 scoped_ptr<WebRequestCondition> condition_no_url_true = | |
119 WebRequestCondition::Create( | |
120 matcher.condition_factory(), | |
121 *base::test::ParseJson( | |
122 "{ \n" | |
123 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
124 // There is no "first party for cookies URL in the requests below, | |
125 // therefore all requests are considered first party for cookies. | |
126 " \"thirdPartyForCookies\": false, \n" | |
127 "}"), | |
128 &error); | |
129 EXPECT_EQ("", error); | |
130 ASSERT_TRUE(condition_no_url_true.get()); | |
131 | |
132 // A condition without a UrlFilter attribute, which is always false. | |
133 error.clear(); | |
134 scoped_ptr<WebRequestCondition> condition_no_url_false = | |
135 WebRequestCondition::Create( | |
136 matcher.condition_factory(), | |
137 *base::test::ParseJson( | |
138 "{ \n" | |
139 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
140 " \"thirdPartyForCookies\": true, \n" | |
141 "}"), | |
142 &error); | |
143 EXPECT_EQ("", error); | |
144 ASSERT_TRUE(condition_no_url_false.get()); | |
145 | |
146 // MATCHING_URL_FILTER must always match kUrl. | |
147 static const char kUrl[] = "https://www.example.com"; | |
148 #define MATCHING_URL_FILTER \ | |
Jeffrey Yasskin
2012/12/18 21:56:02
It'll work to define this as a std::string, and co
vabr (Chromium)
2012/12/19 08:38:19
Agreed, only this part of the test is no longer ne
| |
149 " \"url\": { \n" \ | |
150 " \"hostSuffix\": \"example.com\", \n" \ | |
151 " \"hostPrefix\": \"www\", \n" \ | |
152 " \"schemes\": [\"https\"], \n" \ | |
153 " }, \n" | |
154 | |
155 // A condition with only a UrlFilter attribute. | |
156 error.clear(); | |
157 scoped_ptr<WebRequestCondition> condition_url = WebRequestCondition::Create( | |
158 matcher.condition_factory(), | |
159 *base::test::ParseJson( | |
160 "{ \n" | |
161 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
162 MATCHING_URL_FILTER | |
163 "}"), | |
164 &error); | |
165 EXPECT_EQ("", error); | |
166 ASSERT_TRUE(condition_url.get()); | |
167 | |
168 // A condition with a UrlFilter attribute and an always true non-UrlFilter | |
169 // attribute. | |
170 error.clear(); | |
171 scoped_ptr<WebRequestCondition> condition_both = WebRequestCondition::Create( | |
172 matcher.condition_factory(), | |
173 *base::test::ParseJson( | |
174 "{ \n" | |
175 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
176 MATCHING_URL_FILTER | |
177 " \"thirdPartyForCookies\": false, \n" | |
178 "}"), | |
179 &error); | |
180 EXPECT_EQ("", error); | |
181 ASSERT_TRUE(condition_both.get()); | |
182 #undef MATCHING_URL_FILTER | |
183 | |
184 net::TestURLRequestContext context; | |
185 net::TestURLRequest https_request(GURL(kUrl), NULL, &context); | |
186 | |
187 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | |
188 // attributes are fulfilled. | |
189 EXPECT_FALSE(condition_no_url_false->IsFulfilledIndependentlyOfURL( | |
190 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
191 | |
192 EXPECT_TRUE(condition_no_url_true->IsFulfilledIndependentlyOfURL( | |
193 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
194 | |
195 // 2. An empty condition (in particular, without UrlFilter attributes) is | |
196 // always fulfilled. | |
197 EXPECT_TRUE(condition_empty->IsFulfilledIndependentlyOfURL( | |
198 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
199 | |
200 // 3. A condition with a UrlFilter and a non-UrlFilter attribute is never | |
201 // fulfilled. | |
202 EXPECT_FALSE(condition_both->IsFulfilledIndependentlyOfURL( | |
203 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
204 | |
205 // 4. A condition with only a UrlFilter attribute is never fulfilled. | |
206 EXPECT_FALSE(condition_url->IsFulfilledIndependentlyOfURL( | |
207 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
208 } | |
209 | |
88 TEST(WebRequestConditionTest, CreateConditionSet) { | 210 TEST(WebRequestConditionTest, CreateConditionSet) { |
89 // Necessary for TestURLRequest. | 211 // Necessary for TestURLRequest. |
90 MessageLoop message_loop(MessageLoop::TYPE_IO); | 212 MessageLoop message_loop(MessageLoop::TYPE_IO); |
91 URLMatcher matcher; | 213 URLMatcher matcher; |
92 | 214 |
93 WebRequestConditionSet::AnyVector conditions; | 215 WebRequestConditionSet::AnyVector conditions; |
94 | 216 |
95 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 217 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
96 new json_schema_compiler::any::Any); | 218 new json_schema_compiler::any::Any); |
97 condition1->Init(*base::test::ParseJson( | 219 condition1->Init(*base::test::ParseJson( |
(...skipping 27 matching lines...) Expand all Loading... | |
125 EXPECT_EQ("", error); | 247 EXPECT_EQ("", error); |
126 ASSERT_TRUE(result.get()); | 248 ASSERT_TRUE(result.get()); |
127 EXPECT_EQ(2u, result->conditions().size()); | 249 EXPECT_EQ(2u, result->conditions().size()); |
128 | 250 |
129 // Tell the URLMatcher about our shiny new patterns. | 251 // Tell the URLMatcher about our shiny new patterns. |
130 URLMatcherConditionSet::Vector url_matcher_condition_set; | 252 URLMatcherConditionSet::Vector url_matcher_condition_set; |
131 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 253 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
132 matcher.AddConditionSets(url_matcher_condition_set); | 254 matcher.AddConditionSets(url_matcher_condition_set); |
133 | 255 |
134 std::set<URLMatcherConditionSet::ID> url_match_ids; | 256 std::set<URLMatcherConditionSet::ID> url_match_ids; |
135 int number_matches = 0; | |
136 | 257 |
137 // Test that the result is correct and matches http://www.example.com and | 258 // Test that the result is correct and matches http://www.example.com and |
138 // https://www.example.com | 259 // https://www.example.com |
139 GURL http_url("http://www.example.com"); | 260 GURL http_url("http://www.example.com"); |
140 net::TestURLRequestContext context; | 261 net::TestURLRequestContext context; |
141 net::TestURLRequest http_request(http_url, NULL, &context); | 262 net::TestURLRequest http_request(http_url, NULL, &context); |
142 url_match_ids = matcher.MatchURL(http_url); | 263 url_match_ids = matcher.MatchURL(http_url); |
143 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 264 EXPECT_TRUE(result->IsFulfilled( |
144 i != url_match_ids.end(); ++i) { | 265 url_match_ids, |
145 if (result->IsFulfilled( | 266 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))); |
146 *i, WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))) | |
147 ++number_matches; | |
148 } | |
149 EXPECT_EQ(1, number_matches); | |
150 | 267 |
151 GURL https_url("https://www.example.com"); | 268 GURL https_url("https://www.example.com"); |
152 url_match_ids = matcher.MatchURL(https_url); | 269 url_match_ids = matcher.MatchURL(https_url); |
153 net::TestURLRequest https_request(https_url, NULL, &context); | 270 net::TestURLRequest https_request(https_url, NULL, &context); |
154 number_matches = 0; | 271 EXPECT_TRUE(result->IsFulfilled( |
155 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 272 url_match_ids, |
156 i != url_match_ids.end(); ++i) { | 273 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); |
157 if (result->IsFulfilled( | |
158 *i, WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))) | |
159 ++number_matches; | |
160 } | |
161 EXPECT_EQ(1, number_matches); | |
162 | 274 |
163 // Check that both, hostPrefix and hostSuffix are evaluated. | 275 // Check that both, hostPrefix and hostSuffix are evaluated. |
164 GURL https_foo_url("https://foo.example.com"); | 276 GURL https_foo_url("https://foo.example.com"); |
165 url_match_ids = matcher.MatchURL(https_foo_url); | 277 url_match_ids = matcher.MatchURL(https_foo_url); |
166 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 278 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
167 number_matches = 0; | 279 EXPECT_FALSE(result->IsFulfilled( |
168 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 280 url_match_ids, |
169 i != url_match_ids.end(); ++i) { | 281 WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST))); |
170 if (result->IsFulfilled( | |
171 *i, WebRequestRule::RequestData( | |
172 &https_foo_request, ON_BEFORE_REQUEST))) | |
173 ++number_matches; | |
174 } | |
175 EXPECT_EQ(0, number_matches); | |
176 } | 282 } |
177 | 283 |
178 TEST(WebRequestConditionTest, TestPortFilter) { | 284 TEST(WebRequestConditionTest, TestPortFilter) { |
179 // Necessary for TestURLRequest. | 285 // Necessary for TestURLRequest. |
180 MessageLoop message_loop(MessageLoop::TYPE_IO); | 286 MessageLoop message_loop(MessageLoop::TYPE_IO); |
181 URLMatcher matcher; | 287 URLMatcher matcher; |
182 | 288 |
183 linked_ptr<json_schema_compiler::any::Any> any_condition = | 289 linked_ptr<json_schema_compiler::any::Any> any_condition = |
184 make_linked_ptr(new json_schema_compiler::any::Any); | 290 make_linked_ptr(new json_schema_compiler::any::Any); |
185 any_condition->Init(*base::test::ParseJson( | 291 any_condition->Init(*base::test::ParseJson( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 // filters. | 364 // filters. |
259 " \"requestHeaders\": [{}], \n" | 365 " \"requestHeaders\": [{}], \n" |
260 " \"responseHeaders\": [{}], \n" | 366 " \"responseHeaders\": [{}], \n" |
261 "}"), | 367 "}"), |
262 &error); | 368 &error); |
263 EXPECT_FALSE(error.empty()); | 369 EXPECT_FALSE(error.empty()); |
264 EXPECT_FALSE(result.get()); | 370 EXPECT_FALSE(result.get()); |
265 } | 371 } |
266 | 372 |
267 } // namespace extensions | 373 } // namespace extensions |
OLD | NEW |