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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 *base::test::ParseJson( | 62 *base::test::ParseJson( |
63 "{ \n" | 63 "{ \n" |
64 " \"resourceType\": [\"main_frame\"], \n" | 64 " \"resourceType\": [\"main_frame\"], \n" |
65 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" | 65 " \"url\": { \"hostSuffix\": \"example.com\" }, \n" |
66 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | 66 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" |
67 "}"), | 67 "}"), |
68 &error); | 68 &error); |
69 EXPECT_EQ("", error); | 69 EXPECT_EQ("", error); |
70 ASSERT_TRUE(result.get()); | 70 ASSERT_TRUE(result.get()); |
71 | 71 |
72 URLMatcherConditionSet::Vector url_matcher_condition_set; | |
73 url_matcher_condition_set.push_back(result->url_matcher_condition_set()); | |
74 matcher.AddConditionSets(url_matcher_condition_set); | |
75 std::set<URLMatcherConditionSet::ID> url_match_ids; | |
76 | |
72 net::TestURLRequestContext context; | 77 net::TestURLRequestContext context; |
73 net::TestURLRequest match_request( | 78 GURL http_url("http://www.example.com"); |
74 GURL("http://www.example.com"), NULL, &context); | 79 net::TestURLRequest match_request(http_url, NULL, &context); |
80 url_match_ids = matcher.MatchURL(http_url); | |
75 content::ResourceRequestInfo::AllocateForTesting(&match_request, | 81 content::ResourceRequestInfo::AllocateForTesting(&match_request, |
76 ResourceType::MAIN_FRAME, NULL, -1, -1); | 82 ResourceType::MAIN_FRAME, NULL, -1, -1); |
77 EXPECT_TRUE(result->IsFulfilled( | 83 EXPECT_TRUE(result->IsFulfilled( |
84 url_match_ids, | |
78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); | 85 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); |
79 | 86 |
80 net::TestURLRequest wrong_resource_type( | 87 GURL https_url("https://www.example.com"); |
81 GURL("https://www.example.com"), NULL, &context); | 88 net::TestURLRequest wrong_resource_type(https_url, NULL, &context); |
89 url_match_ids = matcher.MatchURL(https_url); | |
90 // Make sure IsFulfilled does not fail because of URL matching. | |
91 EXPECT_EQ(1u, url_match_ids.size()); | |
82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, | 92 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, |
83 ResourceType::SUB_FRAME, NULL, -1, -1); | 93 ResourceType::SUB_FRAME, NULL, -1, -1); |
84 EXPECT_FALSE(result->IsFulfilled( | 94 EXPECT_FALSE(result->IsFulfilled( |
95 url_match_ids, | |
85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); | 96 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); |
86 } | 97 } |
87 | 98 |
99 // Conditions without UrlFilter attributes need to be independent of URL | |
100 // matching results. We test here that: | |
101 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | |
102 // attributes are fulfilled. | |
103 // 2. An empty condition (in particular, without UrlFilter attributes) is | |
104 // always fulfilled. | |
105 TEST(WebRequestConditionTest, NoUrlAttributes) { | |
106 // Necessary for TestURLRequest. | |
107 MessageLoop message_loop(MessageLoop::TYPE_IO); | |
108 URLMatcher matcher; | |
109 std::string error; | |
110 | |
111 // The empty condition. | |
112 error.clear(); | |
113 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create( | |
114 matcher.condition_factory(), | |
115 *base::test::ParseJson( | |
116 "{ \n" | |
117 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
118 "}"), | |
119 &error); | |
120 EXPECT_EQ("", error); | |
121 ASSERT_TRUE(condition_empty.get()); | |
122 | |
123 // A condition without a UrlFilter attribute, which is always true. | |
124 error.clear(); | |
125 scoped_ptr<WebRequestCondition> condition_no_url_true = | |
126 WebRequestCondition::Create( | |
127 matcher.condition_factory(), | |
128 *base::test::ParseJson( | |
129 "{ \n" | |
130 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
131 // There is no "first party for cookies URL in the requests below, | |
Jeffrey Yasskin
2012/12/19 08:31:23
Missing close-" in this comment.
vabr (Chromium)
2012/12/19 08:38:19
Done.
| |
132 // therefore all requests are considered first party for cookies. | |
133 " \"thirdPartyForCookies\": false, \n" | |
134 "}"), | |
135 &error); | |
136 EXPECT_EQ("", error); | |
137 ASSERT_TRUE(condition_no_url_true.get()); | |
138 | |
139 // A condition without a UrlFilter attribute, which is always false. | |
140 error.clear(); | |
141 scoped_ptr<WebRequestCondition> condition_no_url_false = | |
142 WebRequestCondition::Create( | |
143 matcher.condition_factory(), | |
144 *base::test::ParseJson( | |
145 "{ \n" | |
146 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" | |
147 " \"thirdPartyForCookies\": true, \n" | |
148 "}"), | |
149 &error); | |
150 EXPECT_EQ("", error); | |
151 ASSERT_TRUE(condition_no_url_false.get()); | |
152 | |
153 net::TestURLRequestContext context; | |
154 net::TestURLRequest https_request( | |
155 GURL("https://www.example.com"), NULL, &context); | |
156 std::set<URLMatcherConditionSet::ID> dummy_match_set; | |
157 | |
158 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its | |
159 // attributes are fulfilled. | |
160 EXPECT_FALSE(condition_no_url_false->IsFulfilled( | |
161 dummy_match_set, | |
162 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
163 | |
164 EXPECT_TRUE(condition_no_url_true->IsFulfilled( | |
165 dummy_match_set, | |
166 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
167 | |
168 // 2. An empty condition (in particular, without UrlFilter attributes) is | |
169 // always fulfilled. | |
170 EXPECT_TRUE(condition_empty->IsFulfilled( | |
171 dummy_match_set, | |
172 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST))); | |
173 } | |
174 | |
88 TEST(WebRequestConditionTest, CreateConditionSet) { | 175 TEST(WebRequestConditionTest, CreateConditionSet) { |
89 // Necessary for TestURLRequest. | 176 // Necessary for TestURLRequest. |
90 MessageLoop message_loop(MessageLoop::TYPE_IO); | 177 MessageLoop message_loop(MessageLoop::TYPE_IO); |
91 URLMatcher matcher; | 178 URLMatcher matcher; |
92 | 179 |
93 WebRequestConditionSet::AnyVector conditions; | 180 WebRequestConditionSet::AnyVector conditions; |
94 | 181 |
95 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 182 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
96 new json_schema_compiler::any::Any); | 183 new json_schema_compiler::any::Any); |
97 condition1->Init(*base::test::ParseJson( | 184 condition1->Init(*base::test::ParseJson( |
(...skipping 27 matching lines...) Expand all Loading... | |
125 EXPECT_EQ("", error); | 212 EXPECT_EQ("", error); |
126 ASSERT_TRUE(result.get()); | 213 ASSERT_TRUE(result.get()); |
127 EXPECT_EQ(2u, result->conditions().size()); | 214 EXPECT_EQ(2u, result->conditions().size()); |
128 | 215 |
129 // Tell the URLMatcher about our shiny new patterns. | 216 // Tell the URLMatcher about our shiny new patterns. |
130 URLMatcherConditionSet::Vector url_matcher_condition_set; | 217 URLMatcherConditionSet::Vector url_matcher_condition_set; |
131 result->GetURLMatcherConditionSets(&url_matcher_condition_set); | 218 result->GetURLMatcherConditionSets(&url_matcher_condition_set); |
132 matcher.AddConditionSets(url_matcher_condition_set); | 219 matcher.AddConditionSets(url_matcher_condition_set); |
133 | 220 |
134 std::set<URLMatcherConditionSet::ID> url_match_ids; | 221 std::set<URLMatcherConditionSet::ID> url_match_ids; |
135 int number_matches = 0; | |
136 | 222 |
137 // Test that the result is correct and matches http://www.example.com and | 223 // Test that the result is correct and matches http://www.example.com and |
138 // https://www.example.com | 224 // https://www.example.com |
139 GURL http_url("http://www.example.com"); | 225 GURL http_url("http://www.example.com"); |
140 net::TestURLRequestContext context; | 226 net::TestURLRequestContext context; |
141 net::TestURLRequest http_request(http_url, NULL, &context); | 227 net::TestURLRequest http_request(http_url, NULL, &context); |
142 url_match_ids = matcher.MatchURL(http_url); | 228 url_match_ids = matcher.MatchURL(http_url); |
143 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 229 EXPECT_TRUE(result->IsFulfilled( |
144 i != url_match_ids.end(); ++i) { | 230 url_match_ids, |
145 if (result->IsFulfilled( | 231 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 | 232 |
151 GURL https_url("https://www.example.com"); | 233 GURL https_url("https://www.example.com"); |
152 url_match_ids = matcher.MatchURL(https_url); | 234 url_match_ids = matcher.MatchURL(https_url); |
153 net::TestURLRequest https_request(https_url, NULL, &context); | 235 net::TestURLRequest https_request(https_url, NULL, &context); |
154 number_matches = 0; | 236 EXPECT_TRUE(result->IsFulfilled( |
155 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 237 url_match_ids, |
156 i != url_match_ids.end(); ++i) { | 238 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 | 239 |
163 // Check that both, hostPrefix and hostSuffix are evaluated. | 240 // Check that both, hostPrefix and hostSuffix are evaluated. |
164 GURL https_foo_url("https://foo.example.com"); | 241 GURL https_foo_url("https://foo.example.com"); |
165 url_match_ids = matcher.MatchURL(https_foo_url); | 242 url_match_ids = matcher.MatchURL(https_foo_url); |
166 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); | 243 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); |
167 number_matches = 0; | 244 EXPECT_FALSE(result->IsFulfilled( |
168 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); | 245 url_match_ids, |
169 i != url_match_ids.end(); ++i) { | 246 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 } | 247 } |
177 | 248 |
178 TEST(WebRequestConditionTest, TestPortFilter) { | 249 TEST(WebRequestConditionTest, TestPortFilter) { |
179 // Necessary for TestURLRequest. | 250 // Necessary for TestURLRequest. |
180 MessageLoop message_loop(MessageLoop::TYPE_IO); | 251 MessageLoop message_loop(MessageLoop::TYPE_IO); |
181 URLMatcher matcher; | 252 URLMatcher matcher; |
182 | 253 |
183 linked_ptr<json_schema_compiler::any::Any> any_condition = | 254 linked_ptr<json_schema_compiler::any::Any> any_condition = |
184 make_linked_ptr(new json_schema_compiler::any::Any); | 255 make_linked_ptr(new json_schema_compiler::any::Any); |
185 any_condition->Init(*base::test::ParseJson( | 256 any_condition->Init(*base::test::ParseJson( |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 // filters. | 329 // filters. |
259 " \"requestHeaders\": [{}], \n" | 330 " \"requestHeaders\": [{}], \n" |
260 " \"responseHeaders\": [{}], \n" | 331 " \"responseHeaders\": [{}], \n" |
261 "}"), | 332 "}"), |
262 &error); | 333 &error); |
263 EXPECT_FALSE(error.empty()); | 334 EXPECT_FALSE(error.empty()); |
264 EXPECT_FALSE(result.get()); | 335 EXPECT_FALSE(result.get()); |
265 } | 336 } |
266 | 337 |
267 } // namespace extensions | 338 } // namespace extensions |
OLD | NEW |