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

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

Issue 11572061: Create DeclarativeConditionSet, DeclarativeActionSet, and DeclarativeRule templates (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years 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_rules_ registry.h" 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_ registry.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/json/json_reader.h"
9 #include "base/memory/linked_ptr.h" 10 #include "base/memory/linked_ptr.h"
10 #include "base/message_loop.h" 11 #include "base/message_loop.h"
11 #include "base/values.h" 12 #include "base/values.h"
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
14 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
12 #include "chrome/common/extensions/matcher/url_matcher_constants.h" 15 #include "chrome/common/extensions/matcher/url_matcher_constants.h"
13 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_consta nts.h"
14 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rule.h "
15 #include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h"
16 #include "content/public/test/test_browser_thread.h" 16 #include "content/public/test/test_browser_thread.h"
17 #include "net/url_request/url_request_test_util.h" 17 #include "net/url_request/url_request_test_util.h"
18 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 namespace { 21 namespace {
21 const char kExtensionId[] = "ext1"; 22 const char kExtensionId[] = "ext1";
22 const char kExtensionId2[] = "ext2"; 23 const char kExtensionId2[] = "ext2";
23 const char kRuleId1[] = "rule1"; 24 const char kRuleId1[] = "rule1";
24 const char kRuleId2[] = "rule2"; 25 const char kRuleId2[] = "rule2";
25 const char kRuleId3[] = "rule3"; 26 const char kRuleId3[] = "rule3";
26 const char kRuleId4[] = "rule4"; 27 const char kRuleId4[] = "rule4";
27 } // namespace 28 } // namespace
28 29
29 namespace extensions { 30 namespace extensions {
30 31
32 using testing::HasSubstr;
33
31 namespace helpers = extension_web_request_api_helpers; 34 namespace helpers = extension_web_request_api_helpers;
32 namespace keys = declarative_webrequest_constants; 35 namespace keys = declarative_webrequest_constants;
33 namespace keys2 = url_matcher_constants; 36 namespace keys2 = url_matcher_constants;
34 37
35 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { 38 class TestWebRequestRulesRegistry : public WebRequestRulesRegistry {
36 public: 39 public:
37 TestWebRequestRulesRegistry() 40 TestWebRequestRulesRegistry()
38 : WebRequestRulesRegistry(NULL, NULL), 41 : WebRequestRulesRegistry(NULL, NULL),
39 num_clear_cache_calls_(0) {} 42 num_clear_cache_calls_(0) {}
40 43
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 error = registry->AddRules(kExtensionId, rules); 226 error = registry->AddRules(kExtensionId, rules);
224 EXPECT_EQ("", error); 227 EXPECT_EQ("", error);
225 EXPECT_EQ(1, registry->num_clear_cache_calls()); 228 EXPECT_EQ(1, registry->num_clear_cache_calls());
226 229
227 std::set<WebRequestRule::GlobalRuleId> matches; 230 std::set<WebRequestRule::GlobalRuleId> matches;
228 231
229 GURL http_url("http://www.example.com"); 232 GURL http_url("http://www.example.com");
230 net::TestURLRequestContext context; 233 net::TestURLRequestContext context;
231 net::TestURLRequest http_request(http_url, NULL, &context); 234 net::TestURLRequest http_request(http_url, NULL, &context);
232 matches = registry->GetMatches( 235 matches = registry->GetMatches(
233 WebRequestRule::RequestData(&http_request, ON_BEFORE_REQUEST)); 236 DeclarativeWebRequestData(&http_request, ON_BEFORE_REQUEST));
234 EXPECT_EQ(2u, matches.size()); 237 EXPECT_EQ(2u, matches.size());
235 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != 238 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) !=
236 matches.end()); 239 matches.end());
237 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != 240 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) !=
238 matches.end()); 241 matches.end());
239 242
240 GURL foobar_url("http://www.foobar.com"); 243 GURL foobar_url("http://www.foobar.com");
241 net::TestURLRequest foobar_request(foobar_url, NULL, &context); 244 net::TestURLRequest foobar_request(foobar_url, NULL, &context);
242 matches = registry->GetMatches( 245 matches = registry->GetMatches(
243 WebRequestRule::RequestData(&foobar_request, ON_BEFORE_REQUEST)); 246 DeclarativeWebRequestData(&foobar_request, ON_BEFORE_REQUEST));
244 EXPECT_EQ(1u, matches.size()); 247 EXPECT_EQ(1u, matches.size());
245 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != 248 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) !=
246 matches.end()); 249 matches.end());
247 } 250 }
248 251
249 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 252 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) {
250 scoped_refptr<TestWebRequestRulesRegistry> registry( 253 scoped_refptr<TestWebRequestRulesRegistry> registry(
251 new TestWebRequestRulesRegistry()); 254 new TestWebRequestRulesRegistry());
252 std::string error; 255 std::string error;
253 256
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 360 rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com");
358 error = registry->AddRules(kExtensionId2, rules_to_add_2); 361 error = registry->AddRules(kExtensionId2, rules_to_add_2);
359 EXPECT_EQ("", error); 362 EXPECT_EQ("", error);
360 363
361 GURL url("http://www.google.com"); 364 GURL url("http://www.google.com");
362 net::TestURLRequestContext context; 365 net::TestURLRequestContext context;
363 net::TestURLRequest request(url, NULL, &context); 366 net::TestURLRequest request(url, NULL, &context);
364 std::list<LinkedPtrEventResponseDelta> deltas = 367 std::list<LinkedPtrEventResponseDelta> deltas =
365 registry->CreateDeltas( 368 registry->CreateDeltas(
366 NULL, 369 NULL,
367 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), 370 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST),
368 false); 371 false);
369 372
370 // The second extension is installed later and will win for this reason 373 // The second extension is installed later and will win for this reason
371 // in conflict resolution. 374 // in conflict resolution.
372 ASSERT_EQ(2u, deltas.size()); 375 ASSERT_EQ(2u, deltas.size());
373 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); 376 deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder);
374 377
375 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); 378 std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin();
376 LinkedPtrEventResponseDelta winner = *i++; 379 LinkedPtrEventResponseDelta winner = *i++;
377 LinkedPtrEventResponseDelta loser = *i; 380 LinkedPtrEventResponseDelta loser = *i;
(...skipping 29 matching lines...) Expand all
407 rules_to_add_3[0] = CreateIgnoreRule(); 410 rules_to_add_3[0] = CreateIgnoreRule();
408 error = registry->AddRules(kExtensionId, rules_to_add_3); 411 error = registry->AddRules(kExtensionId, rules_to_add_3);
409 EXPECT_EQ("", error); 412 EXPECT_EQ("", error);
410 413
411 GURL url("http://www.google.com/index.html"); 414 GURL url("http://www.google.com/index.html");
412 net::TestURLRequestContext context; 415 net::TestURLRequestContext context;
413 net::TestURLRequest request(url, NULL, &context); 416 net::TestURLRequest request(url, NULL, &context);
414 std::list<LinkedPtrEventResponseDelta> deltas = 417 std::list<LinkedPtrEventResponseDelta> deltas =
415 registry->CreateDeltas( 418 registry->CreateDeltas(
416 NULL, 419 NULL,
417 WebRequestRule::RequestData(&request, ON_BEFORE_REQUEST), 420 DeclarativeWebRequestData(&request, ON_BEFORE_REQUEST),
418 false); 421 false);
419 422
420 // The redirect by the first extension is ignored due to the ignore rule. 423 // The redirect by the first extension is ignored due to the ignore rule.
421 ASSERT_EQ(1u, deltas.size()); 424 ASSERT_EQ(1u, deltas.size());
422 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); 425 LinkedPtrEventResponseDelta effective_rule = *(deltas.begin());
423 426
424 EXPECT_EQ(kExtensionId2, effective_rule->extension_id); 427 EXPECT_EQ(kExtensionId2, effective_rule->extension_id);
425 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), 428 EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2),
426 effective_rule->extension_install_time); 429 effective_rule->extension_install_time);
427 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); 430 EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url);
428 } 431 }
432
433 TEST_F(WebRequestRulesRegistryTest, CheckConsistency) {
434 // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED
435 // but the redirect action can only be executed during ON_BEFORE_REQUEST.
436 // Therefore, this is an inconsistent rule that needs to be flagged.
437 const char kRule[] =
438 "{ \n"
439 " \"id\": \"rule1\", \n"
440 " \"conditions\": [ \n"
441 " { \n"
442 " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"
443 " \"url\": {\"hostSuffix\": \"foo.com\"}, \n"
444 " \"contentType\": [\"image/jpeg\"] \n"
445 " } \n"
446 " ], \n"
447 " \"actions\": [ \n"
448 " { \n"
449 " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n"
450 " \"redirectUrl\": \"http://bar.com\" \n"
451 " } \n"
452 " ], \n"
453 " \"priority\": 200 \n"
454 "} ";
455
456 scoped_ptr<Value> value(base::JSONReader::Read(kRule));
457 ASSERT_TRUE(value.get());
458
459 std::vector<linked_ptr<RulesRegistry::Rule> > rules;
460 rules.push_back(make_linked_ptr(new RulesRegistry::Rule));
461 ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get()));
462
463 scoped_refptr<WebRequestRulesRegistry> registry(
464 new TestWebRequestRulesRegistry());
465
466 URLMatcher matcher;
467 std::string error = registry->AddRulesImpl(kExtensionId, rules);
468 EXPECT_THAT(error, HasSubstr("no time in the request life-cycle"));
469 EXPECT_TRUE(registry->IsEmpty());
470 }
471
429 } // namespace extensions 472 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698