Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Side by Side Diff: chrome/browser/extensions/api/declarative_webrequest/webrequest_condition_unittest.cc

Issue 11569007: Refactoring how conditions without URL attributes are handled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rewritten GetMatches and added the trigger ID to Set::IsFulfilled Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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);
81 EXPECT_EQ(1u, url_match_ids.size());
75 content::ResourceRequestInfo::AllocateForTesting(&match_request, 82 content::ResourceRequestInfo::AllocateForTesting(&match_request,
76 ResourceType::MAIN_FRAME, NULL, -1, -1); 83 ResourceType::MAIN_FRAME, NULL, -1, -1);
77 EXPECT_TRUE(result->IsFulfilled( 84 EXPECT_TRUE(result->IsFulfilled(
85 url_match_ids,
78 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST))); 86 WebRequestRule::RequestData(&match_request, ON_BEFORE_REQUEST)));
79 87
80 net::TestURLRequest wrong_resource_type( 88 GURL https_url("https://www.example.com");
81 GURL("https://www.example.com"), NULL, &context); 89 net::TestURLRequest wrong_resource_type(https_url, NULL, &context);
90 url_match_ids = matcher.MatchURL(https_url);
91 // Make sure IsFulfilled does not fail because of URL matching.
92 EXPECT_EQ(1u, url_match_ids.size());
82 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type, 93 content::ResourceRequestInfo::AllocateForTesting(&wrong_resource_type,
83 ResourceType::SUB_FRAME, NULL, -1, -1); 94 ResourceType::SUB_FRAME, NULL, -1, -1);
84 EXPECT_FALSE(result->IsFulfilled( 95 EXPECT_FALSE(result->IsFulfilled(
96 url_match_ids,
85 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST))); 97 WebRequestRule::RequestData(&wrong_resource_type, ON_BEFORE_REQUEST)));
86 } 98 }
87 99
100 // Conditions without UrlFilter attributes need to be independent of URL
101 // matching results. We test here that:
102 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
103 // attributes are fulfilled.
104 // 2. An empty condition (in particular, without UrlFilter attributes) is
105 // always fulfilled.
106 TEST(WebRequestConditionTest, NoUrlAttributes) {
107 // Necessary for TestURLRequest.
108 MessageLoop message_loop(MessageLoop::TYPE_IO);
109 URLMatcher matcher;
110 std::string error;
111
112 // The empty condition.
113 error.clear();
114 scoped_ptr<WebRequestCondition> condition_empty = WebRequestCondition::Create(
115 matcher.condition_factory(),
116 *base::test::ParseJson(
117 "{ \n"
118 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
119 "}"),
120 &error);
121 EXPECT_EQ("", error);
122 ASSERT_TRUE(condition_empty.get());
123
124 // A condition without a UrlFilter attribute, which is always true.
125 error.clear();
126 scoped_ptr<WebRequestCondition> condition_no_url_true =
127 WebRequestCondition::Create(
128 matcher.condition_factory(),
129 *base::test::ParseJson(
130 "{ \n"
131 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
132 // There is no "1st party for cookies" URL in the requests below,
133 // therefore all requests are considered first party for cookies.
134 " \"thirdPartyForCookies\": false, \n"
135 "}"),
136 &error);
137 EXPECT_EQ("", error);
138 ASSERT_TRUE(condition_no_url_true.get());
139
140 // A condition without a UrlFilter attribute, which is always false.
141 error.clear();
142 scoped_ptr<WebRequestCondition> condition_no_url_false =
143 WebRequestCondition::Create(
144 matcher.condition_factory(),
145 *base::test::ParseJson(
146 "{ \n"
147 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
148 " \"thirdPartyForCookies\": true, \n"
149 "}"),
150 &error);
151 EXPECT_EQ("", error);
152 ASSERT_TRUE(condition_no_url_false.get());
153
154 net::TestURLRequestContext context;
155 net::TestURLRequest https_request(
156 GURL("https://www.example.com"), NULL, &context);
157 std::set<URLMatcherConditionSet::ID> dummy_match_set;
158
159 // 1. A non-empty condition without UrlFilter attributes is fulfilled iff its
160 // attributes are fulfilled.
161 EXPECT_FALSE(condition_no_url_false->IsFulfilled(
162 dummy_match_set,
163 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
164
165 EXPECT_TRUE(condition_no_url_true->IsFulfilled(
166 dummy_match_set,
167 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
168
169 // 2. An empty condition (in particular, without UrlFilter attributes) is
170 // always fulfilled.
171 EXPECT_TRUE(condition_empty->IsFulfilled(
172 dummy_match_set,
173 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
174 }
175
88 TEST(WebRequestConditionTest, CreateConditionSet) { 176 TEST(WebRequestConditionTest, CreateConditionSet) {
89 // Necessary for TestURLRequest. 177 // Necessary for TestURLRequest.
90 MessageLoop message_loop(MessageLoop::TYPE_IO); 178 MessageLoop message_loop(MessageLoop::TYPE_IO);
91 URLMatcher matcher; 179 URLMatcher matcher;
92 180
93 WebRequestConditionSet::AnyVector conditions; 181 WebRequestConditionSet::AnyVector conditions;
94 182
95 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( 183 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr(
96 new json_schema_compiler::any::Any); 184 new json_schema_compiler::any::Any);
97 condition1->Init(*base::test::ParseJson( 185 condition1->Init(*base::test::ParseJson(
(...skipping 27 matching lines...) Expand all
125 EXPECT_EQ("", error); 213 EXPECT_EQ("", error);
126 ASSERT_TRUE(result.get()); 214 ASSERT_TRUE(result.get());
127 EXPECT_EQ(2u, result->conditions().size()); 215 EXPECT_EQ(2u, result->conditions().size());
128 216
129 // Tell the URLMatcher about our shiny new patterns. 217 // Tell the URLMatcher about our shiny new patterns.
130 URLMatcherConditionSet::Vector url_matcher_condition_set; 218 URLMatcherConditionSet::Vector url_matcher_condition_set;
131 result->GetURLMatcherConditionSets(&url_matcher_condition_set); 219 result->GetURLMatcherConditionSets(&url_matcher_condition_set);
132 matcher.AddConditionSets(url_matcher_condition_set); 220 matcher.AddConditionSets(url_matcher_condition_set);
133 221
134 std::set<URLMatcherConditionSet::ID> url_match_ids; 222 std::set<URLMatcherConditionSet::ID> url_match_ids;
135 int number_matches = 0;
136 223
137 // Test that the result is correct and matches http://www.example.com and 224 // Test that the result is correct and matches http://www.example.com and
138 // https://www.example.com 225 // https://www.example.com
139 GURL http_url("http://www.example.com"); 226 GURL http_url("http://www.example.com");
140 net::TestURLRequestContext context; 227 net::TestURLRequestContext context;
141 net::TestURLRequest http_request(http_url, NULL, &context); 228 net::TestURLRequest http_request(http_url, NULL, &context);
142 url_match_ids = matcher.MatchURL(http_url); 229 url_match_ids = matcher.MatchURL(http_url);
143 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); 230 EXPECT_EQ(1u, url_match_ids.size());
144 i != url_match_ids.end(); ++i) { 231 EXPECT_TRUE(result->IsFulfilled(
145 if (result->IsFulfilled( 232 *(url_match_ids.begin()),
146 *i, WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST))) 233 url_match_ids,
147 ++number_matches; 234 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)));
148 }
149 EXPECT_EQ(1, number_matches);
150 235
151 GURL https_url("https://www.example.com"); 236 GURL https_url("https://www.example.com");
152 url_match_ids = matcher.MatchURL(https_url); 237 url_match_ids = matcher.MatchURL(https_url);
238 EXPECT_EQ(1u, url_match_ids.size());
153 net::TestURLRequest https_request(https_url, NULL, &context); 239 net::TestURLRequest https_request(https_url, NULL, &context);
154 number_matches = 0; 240 EXPECT_TRUE(result->IsFulfilled(
155 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); 241 *(url_match_ids.begin()),
156 i != url_match_ids.end(); ++i) { 242 url_match_ids,
157 if (result->IsFulfilled( 243 WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)));
158 *i, WebRequestRule::RequestData(&https_request, ON_BEFORE_REQUEST)))
159 ++number_matches;
160 }
161 EXPECT_EQ(1, number_matches);
162 244
163 // Check that both, hostPrefix and hostSuffix are evaluated. 245 // Check that both, hostPrefix and hostSuffix are evaluated.
164 GURL https_foo_url("https://foo.example.com"); 246 GURL https_foo_url("https://foo.example.com");
165 url_match_ids = matcher.MatchURL(https_foo_url); 247 url_match_ids = matcher.MatchURL(https_foo_url);
248 EXPECT_EQ(0u, url_match_ids.size());
166 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context); 249 net::TestURLRequest https_foo_request(https_foo_url, NULL, &context);
167 number_matches = 0; 250 EXPECT_FALSE(result->IsFulfilled(
168 for (std::set<URLMatcherConditionSet::ID>::iterator i = url_match_ids.begin(); 251 -1,
169 i != url_match_ids.end(); ++i) { 252 url_match_ids,
170 if (result->IsFulfilled( 253 WebRequestRule::RequestData(&https_foo_request, ON_BEFORE_REQUEST)));
171 *i, WebRequestRule::RequestData(
172 &https_foo_request, ON_BEFORE_REQUEST)))
173 ++number_matches;
174 }
175 EXPECT_EQ(0, number_matches);
176 } 254 }
177 255
178 TEST(WebRequestConditionTest, TestPortFilter) { 256 TEST(WebRequestConditionTest, TestPortFilter) {
179 // Necessary for TestURLRequest. 257 // Necessary for TestURLRequest.
180 MessageLoop message_loop(MessageLoop::TYPE_IO); 258 MessageLoop message_loop(MessageLoop::TYPE_IO);
181 URLMatcher matcher; 259 URLMatcher matcher;
182 260
183 linked_ptr<json_schema_compiler::any::Any> any_condition = 261 linked_ptr<json_schema_compiler::any::Any> any_condition =
184 make_linked_ptr(new json_schema_compiler::any::Any); 262 make_linked_ptr(new json_schema_compiler::any::Any);
185 any_condition->Init(*base::test::ParseJson( 263 any_condition->Init(*base::test::ParseJson(
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 // filters. 336 // filters.
259 " \"requestHeaders\": [{}], \n" 337 " \"requestHeaders\": [{}], \n"
260 " \"responseHeaders\": [{}], \n" 338 " \"responseHeaders\": [{}], \n"
261 "}"), 339 "}"),
262 &error); 340 &error);
263 EXPECT_FALSE(error.empty()); 341 EXPECT_FALSE(error.empty());
264 EXPECT_FALSE(result.get()); 342 EXPECT_FALSE(result.get());
265 } 343 }
266 344
267 } // namespace extensions 345 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698