15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_rules_registry.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string> 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/json/json_reader.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/linked_ptr.h" 139ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h" 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/stl_util.h" 152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/test/values_test_util.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/api/declarative_webrequest/webrequest_constants.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/extensions/api/web_request/web_request_api_helpers.h" 19c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "chrome/common/extensions/extension_test_util.h" 20d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)#include "components/url_matcher/url_matcher_constants.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_browser_thread.h" 220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "net/base/request_priority.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_test_util.h" 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h" 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "testing/gtest/include/gtest/gtest-message.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 28d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using base::Value; 29d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using extension_test_util::LoadManifest; 30d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using extension_test_util::LoadManifestUnchecked; 31d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using testing::HasSubstr; 32d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)using url_matcher::URLMatcher; 33d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kExtensionId[] = "ext1"; 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kExtensionId2[] = "ext2"; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kRuleId1[] = "rule1"; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kRuleId2[] = "rule2"; 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kRuleId3[] = "rule3"; 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kRuleId4[] = "rule4"; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions { 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace helpers = extension_web_request_api_helpers; 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace keys = declarative_webrequest_constants; 47d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)namespace keys2 = url_matcher::url_matcher_constants; 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TestWebRequestRulesRegistry : public WebRequestRulesRegistry { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 51f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) TestWebRequestRulesRegistry( 52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) scoped_refptr<InfoMap> extension_info_map) 53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) : WebRequestRulesRegistry(NULL /*profile*/, 54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) NULL /* cache_delegate */, 55f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) WebViewKey(0, 0)), 56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) num_clear_cache_calls_(0) { 57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SetExtensionInfoMapForTesting(extension_info_map); 58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns how often the in-memory caches of the renderers were instructed 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // to be cleared. 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int num_clear_cache_calls() const { return num_clear_cache_calls_; } 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // How many rules are there which have some conditions not triggered by URL 652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // matches. 662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t RulesWithoutTriggers() const { 672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return rules_with_untriggered_conditions_for_test().size(); 682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~TestWebRequestRulesRegistry() {} 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual void ClearCacheOnNavigation() OVERRIDE { 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ++num_clear_cache_calls_; 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int num_clear_cache_calls_; 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WebRequestRulesRegistryTest : public testing::Test { 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) WebRequestRulesRegistryTest() 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : message_loop_(base::MessageLoop::TYPE_IO), 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ui_(content::BrowserThread::UI, &message_loop_), 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) io_(content::BrowserThread::IO, &message_loop_) {} 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~WebRequestRulesRegistryTest() {} 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) virtual void SetUp() OVERRIDE; 91c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void TearDown() OVERRIDE { 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Make sure that deletion traits of all registries are executed. 94868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) message_loop_.RunUntilIdle(); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a rule that roughly matches http://*.example.com and 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // https://www.example.com and cancels it 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) linked_ptr<RulesRegistry::Rule> CreateRule1() { 100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::ListValue* scheme_http = new base::ListValue(); 1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scheme_http->Append(new base::StringValue("http")); 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) http_condition_dict->Set(keys2::kSchemesKey, scheme_http); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) http_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue http_condition_url_filter; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) http_condition_url_filter.Set(keys::kUrlKey, http_condition_dict); 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) http_condition_url_filter.SetString(keys::kInstanceTypeKey, 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) keys::kRequestMatcherType); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::ListValue* scheme_https = new base::ListValue(); 1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scheme_http->Append(new base::StringValue("https")); 112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue* https_condition_dict = new base::DictionaryValue(); 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) https_condition_dict->Set(keys2::kSchemesKey, scheme_https); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) https_condition_dict->SetString(keys2::kHostSuffixKey, "example.com"); 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) https_condition_dict->SetString(keys2::kHostPrefixKey, "www"); 116eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue https_condition_url_filter; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) https_condition_url_filter.Set(keys::kUrlKey, https_condition_dict); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) https_condition_url_filter.SetString(keys::kInstanceTypeKey, 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) keys::kRequestMatcherType); 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 121eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue action_dict; 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->id.reset(new std::string(kRuleId1)); 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->priority.reset(new int(100)); 1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); 1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back( 1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value>(http_condition_url_filter.DeepCopy())); 1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back( 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value>(https_condition_url_filter.DeepCopy())); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return rule; 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a rule that matches anything and cancels it. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) linked_ptr<RulesRegistry::Rule> CreateRule2() { 137eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue condition_dict; 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue action_dict; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->id.reset(new std::string(kRuleId2)); 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->priority.reset(new int(100)); 1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); 1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back( 1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value>(condition_dict.DeepCopy())); 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return rule; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) linked_ptr<RulesRegistry::Rule> CreateRedirectRule( 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const std::string& destination) { 154eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue condition_dict; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 157eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue action_dict; 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetString(keys::kInstanceTypeKey, keys::kRedirectRequestType); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetString(keys::kRedirectUrlKey, destination); 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->id.reset(new std::string(kRuleId3)); 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->priority.reset(new int(100)); 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); 1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back( 1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value>(condition_dict.DeepCopy())); 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return rule; 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Create a rule to ignore all other rules for a destination that 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // contains index.html. 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) linked_ptr<RulesRegistry::Rule> CreateIgnoreRule() { 173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue condition_dict; 174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue* http_condition_dict = new base::DictionaryValue(); 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) http_condition_dict->SetString(keys2::kPathContainsKey, "index.html"); 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) condition_dict.SetString(keys::kInstanceTypeKey, keys::kRequestMatcherType); 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) condition_dict.Set(keys::kUrlKey, http_condition_dict); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 179eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue action_dict; 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetString(keys::kInstanceTypeKey, keys::kIgnoreRulesType); 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) action_dict.SetInteger(keys::kLowerPriorityThanKey, 150); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->id.reset(new std::string(kRuleId4)); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rule->priority.reset(new int(200)); 1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); 1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back( 1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value>(condition_dict.DeepCopy())); 1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return rule; 1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Create a condition with the attributes specified. An example value of 1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". 1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<base::Value> CreateCondition(const std::string& attributes) { 1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string json_description = 1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "{ \n" 1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n"; 1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) json_description += attributes; 1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) json_description += "}"; 2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return linked_ptr<base::Value>( 2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::test::ParseJson(json_description).release()); 2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Create a rule with the ID |rule_id| and with conditions created from the 2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // |attributes| specified (one entry one condition). An example value of a 2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // string from |attributes| is: "\"resourceType\": [\"stylesheet\"], \n". 2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> CreateCancellingRule( 2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const char* rule_id, 2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::vector<const std::string*>& attributes) { 211eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch base::DictionaryValue action_dict; 2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) action_dict.SetString(keys::kInstanceTypeKey, keys::kCancelRequestType); 2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) linked_ptr<RulesRegistry::Rule> rule(new RulesRegistry::Rule); 2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->id.reset(new std::string(rule_id)); 2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->priority.reset(new int(1)); 2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->actions.push_back(linked_ptr<base::Value>(action_dict.DeepCopy())); 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (std::vector<const std::string*>::const_iterator it = 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.begin(); 2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) it != attributes.end(); ++it) 2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rule->conditions.push_back(CreateCondition(**it)); 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return rule; 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 226868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::MessageLoop message_loop_; 227868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) content::TestBrowserThread ui_; 228868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) content::TestBrowserThread io_; 229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Two extensions with host permissions for all URLs and the DWR permission. 230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Installation times will be so that |extension_| is older than 231c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // |extension2_|. 232c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<Extension> extension_; 233c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<Extension> extension2_; 234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) scoped_refptr<InfoMap> extension_info_map_; 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 237c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void WebRequestRulesRegistryTest::SetUp() { 238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) testing::Test::SetUp(); 239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string error; 241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_ = LoadManifestUnchecked("permissions", 242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "web_request_all_host_permissions.json", 243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Manifest::INVALID_LOCATION, 244c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) Extension::NO_FLAGS, 245c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) kExtensionId, 246c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) &error); 247868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(extension_.get()) << error; 248868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) extension2_ = LoadManifestUnchecked("permissions", 249868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) "web_request_all_host_permissions.json", 250868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Manifest::INVALID_LOCATION, 251868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Extension::NO_FLAGS, 252868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) kExtensionId2, 253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) &error); 254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(extension2_.get()) << error; 255f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) extension_info_map_ = new InfoMap; 256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ASSERT_TRUE(extension_info_map_.get()); 257c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_info_map_->AddExtension(extension_.get(), 258c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::Time() + base::TimeDelta::FromDays(1), 259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) false /*incognito_enabled*/, 260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) false /*notifications_disabled*/); 261c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_info_map_->AddExtension(extension2_.get(), 262c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) base::Time() + base::TimeDelta::FromDays(2), 263f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) false /*incognito_enabled*/, 264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) false /*notifications_disabled*/); 265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 267c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<TestWebRequestRulesRegistry> registry( 270c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string error; 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules.push_back(CreateRule1()); 2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules.push_back(CreateRule2()); 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules); 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1, registry->num_clear_cache_calls()); 2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::set<const WebRequestRule*> matches; 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL http_url("http://www.example.com"); 2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::TestURLRequestContext context; 2850f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest http_request( 2860f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) http_url, net::DEFAULT_PRIORITY, NULL, &context); 2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches = registry->GetMatches(request_data); 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2u, matches.size()); 2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::set<WebRequestRule::GlobalRuleId> matches_ids; 2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (std::set<const WebRequestRule*>::const_iterator it = matches.begin(); 2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) it != matches.end(); ++it) 2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches_ids.insert((*it)->id()); 2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId1))); 2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_TRUE(ContainsKey(matches_ids, std::make_pair(kExtensionId, kRuleId2))); 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL foobar_url("http://www.foobar.com"); 2990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest foobar_request( 3000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) foobar_url, net::DEFAULT_PRIORITY, NULL, &context); 3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) request_data.request = &foobar_request; 3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches = registry->GetMatches(request_data); 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, matches.size()); 3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestRule::GlobalRuleId expected_pair = 3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::make_pair(kExtensionId, kRuleId2); 3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(expected_pair, (*matches.begin())->id()); 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<TestWebRequestRulesRegistry> registry( 311c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string error; 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Setup RulesRegistry to contain two rules. 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add.push_back(CreateRule1()); 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add.push_back(CreateRule2()); 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules_to_add); 3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1, registry->num_clear_cache_calls()); 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Verify initial state. 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId, ®istered_rules); 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2u, registered_rules.size()); 3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1u, registry->RulesWithoutTriggers()); 3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove first rule. 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<std::string> rules_to_remove; 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_remove.push_back(kRuleId1); 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->RemoveRules(kExtensionId, rules_to_remove); 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2, registry->num_clear_cache_calls()); 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Verify that only one rule is left. 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_rules.clear(); 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId, ®istered_rules); 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, registered_rules.size()); 3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1u, registry->RulesWithoutTriggers()); 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Now rules_to_remove contains both rules, i.e. one that does not exist in 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the rules registry anymore. Effectively we only remove the second rule. 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_remove.push_back(kRuleId2); 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->RemoveRules(kExtensionId, rules_to_remove); 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(3, registry->num_clear_cache_calls()); 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Verify that everything is gone. 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_rules.clear(); 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId, ®istered_rules); 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(0u, registered_rules.size()); 3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(0u, registry->RulesWithoutTriggers()); 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(registry->IsEmpty()); 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { 3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<TestWebRequestRulesRegistry> registry( 359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string error; 3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Setup RulesRegistry to contain two rules, one for each extension. 3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); 3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add[0] = CreateRule1(); 3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules_to_add); 3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1, registry->num_clear_cache_calls()); 3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add[0] = CreateRule2(); 3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId2, rules_to_add); 3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2, registry->num_clear_cache_calls()); 3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Verify initial state. 3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; 3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId, ®istered_rules); 3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, registered_rules.size()); 3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_rules.clear(); 3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId2, ®istered_rules); 3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, registered_rules.size()); 3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove rule of first extension. 3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->RemoveAllRules(kExtensionId); 3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(3, registry->num_clear_cache_calls()); 3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Verify that only the first rule is deleted. 3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_rules.clear(); 3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId, ®istered_rules); 3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(0u, registered_rules.size()); 3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registered_rules.clear(); 3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) registry->GetAllRules(kExtensionId2, ®istered_rules); 3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, registered_rules.size()); 3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test removing rules if none exist. 3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->RemoveAllRules(kExtensionId); 3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(4, registry->num_clear_cache_calls()); 3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove rule from second extension. 4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->RemoveAllRules(kExtensionId2); 4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(5, registry->num_clear_cache_calls()); 4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(registry->IsEmpty()); 4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test precedences between extensions. 4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, Precedences) { 4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<WebRequestRulesRegistry> registry( 411c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string error; 4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); 4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); 4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules_to_add_1); 4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); 4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId2, rules_to_add_2); 4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://www.google.com"); 4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::TestURLRequestContext context; 4260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); 4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestData request_data(&request, ON_BEFORE_REQUEST); 4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::list<LinkedPtrEventResponseDelta> deltas = 4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->CreateDeltas(NULL, request_data, false); 4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The second extension is installed later and will win for this reason 4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // in conflict resolution. 4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(2u, deltas.size()); 4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) deltas.sort(&helpers::InDecreasingExtensionInstallationTimeOrder); 4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::list<LinkedPtrEventResponseDelta>::iterator i = deltas.begin(); 4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LinkedPtrEventResponseDelta winner = *i++; 4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LinkedPtrEventResponseDelta loser = *i; 4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kExtensionId2, winner->extension_id); 4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), 4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) winner->extension_install_time); 4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(GURL("http://www.bar.com"), winner->new_url); 4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kExtensionId, loser->extension_id); 4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(1), 4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) loser->extension_install_time); 4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(GURL("http://www.foo.com"), loser->new_url); 4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test priorities of rules within one extension. 4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, Priorities) { 4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<WebRequestRulesRegistry> registry( 454c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string error; 4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_1(1); 4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add_1[0] = CreateRedirectRule("http://www.foo.com"); 4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules_to_add_1); 4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_2(1); 4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add_2[0] = CreateRedirectRule("http://www.bar.com"); 4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId2, rules_to_add_2); 4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add_3(1); 4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) rules_to_add_3[0] = CreateIgnoreRule(); 4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) error = registry->AddRules(kExtensionId, rules_to_add_3); 4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ("", error); 4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://www.google.com/index.html"); 4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::TestURLRequestContext context; 4740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); 4752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestData request_data(&request, ON_BEFORE_REQUEST); 4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::list<LinkedPtrEventResponseDelta> deltas = 4772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) registry->CreateDeltas(NULL, request_data, false); 4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The redirect by the first extension is ignored due to the ignore rule. 4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, deltas.size()); 4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) LinkedPtrEventResponseDelta effective_rule = *(deltas.begin()); 4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kExtensionId2, effective_rule->extension_id); 4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(base::Time() + base::TimeDelta::FromDays(2), 4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) effective_rule->extension_install_time); 4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(GURL("http://www.bar.com"), effective_rule->new_url); 4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 489c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// Test ignoring of rules by tag. 490c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, IgnoreRulesByTag) { 491c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const char kRule1[] = 492c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "{ \n" 493c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"id\": \"rule1\", \n" 494c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"tags\": [\"non_matching_tag\", \"ignore_tag\"], \n" 495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"conditions\": [ \n" 496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"url\": {\"hostSuffix\": \"foo.com\"} \n" 499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 501c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"actions\": [ \n" 502c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 503c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 504c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"redirectUrl\": \"http://bar.com\" \n" 505c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 507c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"priority\": 200 \n" 508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "} "; 509c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const char kRule2[] = 511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "{ \n" 512c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"id\": \"rule2\", \n" 513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"conditions\": [ \n" 514c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 516c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"url\": {\"pathPrefix\": \"/test\"} \n" 517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 518c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"actions\": [ \n" 520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.IgnoreRules\", \n" 522c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"hasTag\": \"ignore_tag\" \n" 523c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 524c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 525c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"priority\": 300 \n" 526c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "} "; 527c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 528c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<Value> value1(base::JSONReader::Read(kRule1)); 529c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(value1.get()); 530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<Value> value2(base::JSONReader::Read(kRule2)); 531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(value2.get()); 532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(RulesRegistry::Rule::Populate(*value1, rules[0].get())); 537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(RulesRegistry::Rule::Populate(*value2, rules[1].get())); 538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<WebRequestRulesRegistry> registry( 540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 541c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string error = registry->AddRulesImpl(kExtensionId, rules); 542c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ("", error); 543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_FALSE(registry->IsEmpty()); 544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GURL url("http://www.foo.com/test"); 546c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::TestURLRequestContext context; 5470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest request(url, net::DEFAULT_PRIORITY, NULL, &context); 548c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebRequestData request_data(&request, ON_BEFORE_REQUEST); 549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::list<LinkedPtrEventResponseDelta> deltas = 550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) registry->CreateDeltas(NULL, request_data, false); 551c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 552c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // The redirect by the redirect rule is ignored due to the ignore rule. 553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::set<const WebRequestRule*> matches = registry->GetMatches(request_data); 554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(2u, matches.size()); 555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_EQ(0u, deltas.size()); 556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 557c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test that rules failing IsFulfilled on their conditions are never returned by 5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// GetMatches. 5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, GetMatchesCheckFulfilled) { 5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_refptr<TestWebRequestRulesRegistry> registry( 562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string kMatchingUrlAttribute( 5642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "\"url\": { \"pathContains\": \"\" }, \n"); 5652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string kNonMatchingNonUrlAttribute( 5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "\"resourceType\": [\"stylesheet\"], \n"); 5672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string kBothAttributes(kMatchingUrlAttribute + 5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) kNonMatchingNonUrlAttribute); 5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string error; 5702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<const std::string*> attributes; 5712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Rules 1 and 2 have one condition, neither of them should fire. 5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kNonMatchingNonUrlAttribute); 5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(CreateCancellingRule(kRuleId1, attributes)); 5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.clear(); 5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kBothAttributes); 5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(CreateCancellingRule(kRuleId2, attributes)); 5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Rule 3 has two conditions, one with a matching URL attribute, and one 5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // with a non-matching non-URL attribute. 5832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.clear(); 5842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kMatchingUrlAttribute); 5852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kNonMatchingNonUrlAttribute); 5862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(CreateCancellingRule(kRuleId3, attributes)); 5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) error = registry->AddRules(kExtensionId, rules); 5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ("", error); 5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1, registry->num_clear_cache_calls()); 5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::set<const WebRequestRule*> matches; 5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GURL http_url("http://www.example.com"); 5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::TestURLRequestContext context; 5960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest http_request( 5970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) http_url, net::DEFAULT_PRIORITY, NULL, &context); 5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 5992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches = registry->GetMatches(request_data); 6002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1u, matches.size()); 6012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestRule::GlobalRuleId expected_pair = std::make_pair(kExtensionId, 6022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) kRuleId3); 6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(expected_pair, (*matches.begin())->id()); 6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Test that the url and firstPartyForCookiesUrl attributes are evaluated 6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// against corresponding URLs. Tested on requests where these URLs actually 6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// differ. 6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, GetMatchesDifferentUrls) { 6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_refptr<TestWebRequestRulesRegistry> registry( 611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 6122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string kUrlAttribute( 6132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "\"url\": { \"hostContains\": \"url\" }, \n"); 6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const std::string kFirstPartyUrlAttribute( 6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "\"firstPartyForCookiesUrl\": { \"hostContains\": \"fpfc\" }, \n"); 6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string error; 6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<const std::string*> attributes; 6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Rule 1 has one condition, with a url attribute 6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kUrlAttribute); 6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(CreateCancellingRule(kRuleId1, attributes)); 6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Rule 2 has one condition, with a firstPartyForCookiesUrl attribute 6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.clear(); 6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) attributes.push_back(&kFirstPartyUrlAttribute); 6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(CreateCancellingRule(kRuleId2, attributes)); 6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) error = registry->AddRules(kExtensionId, rules); 6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ("", error); 6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1, registry->num_clear_cache_calls()); 6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::set<const WebRequestRule*> matches; 6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL urls[] = { 6362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GURL("http://url.example.com"), // matching 6372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GURL("http://www.example.com") // non-matching 6382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const GURL firstPartyUrls[] = { 6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GURL("http://www.example.com"), // non-matching 6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GURL("http://fpfc.example.com") // matching 6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Which rules should match in subsequent test iterations. 6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const char* matchingRuleIds[] = { kRuleId1, kRuleId2 }; 6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) COMPILE_ASSERT(arraysize(urls) == arraysize(firstPartyUrls), 6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) urls_and_firstPartyUrls_need_to_have_the_same_size); 6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) COMPILE_ASSERT(arraysize(urls) == arraysize(matchingRuleIds), 6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) urls_and_matchingRuleIds_need_to_have_the_same_size); 6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) net::TestURLRequestContext context; 6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (size_t i = 0; i < arraysize(matchingRuleIds); ++i) { 6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Construct the inputs. 6530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest http_request( 6540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) urls[i], net::DEFAULT_PRIORITY, NULL, &context); 6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) WebRequestData request_data(&http_request, ON_BEFORE_REQUEST); 6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) http_request.set_first_party_for_cookies(firstPartyUrls[i]); 6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Now run both rules on the input. 6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matches = registry->GetMatches(request_data); 6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SCOPED_TRACE(testing::Message("i = ") << i << ", rule id = " 6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) << matchingRuleIds[i]); 6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Make sure that the right rule succeeded. 6622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(1u, matches.size()); 6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) EXPECT_EQ(WebRequestRule::GlobalRuleId(std::make_pair(kExtensionId, 6642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) matchingRuleIds[i])), 6652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) (*matches.begin())->id()); 6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(WebRequestRulesRegistrySimpleTest, StageChecker) { 6702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The contentType condition can only be evaluated during ON_HEADERS_RECEIVED 6712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // but the redirect action can only be executed during ON_BEFORE_REQUEST. 6722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Therefore, this is an inconsistent rule that needs to be flagged. 6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) const char kRule[] = 6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "{ \n" 6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"id\": \"rule1\", \n" 6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"conditions\": [ \n" 6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " { \n" 6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"url\": {\"hostSuffix\": \"foo.com\"}, \n" 6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"contentType\": [\"image/jpeg\"] \n" 6812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " } \n" 6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " ], \n" 6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"actions\": [ \n" 6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " { \n" 6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 6862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"redirectUrl\": \"http://bar.com\" \n" 6872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " } \n" 6882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " ], \n" 6892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) " \"priority\": 200 \n" 6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) "} "; 6912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_ptr<Value> value(base::JSONReader::Read(kRule)); 693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(value); 694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) RulesRegistry::Rule rule; 696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, &rule)); 697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string error; 699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) URLMatcher matcher; 700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<WebRequestConditionSet> conditions = 701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebRequestConditionSet::Create( 7028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) NULL, matcher.condition_factory(), rule.conditions, &error); 703c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(error.empty()) << error; 704c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(conditions); 705c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 706c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool bad_message = false; 707c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<WebRequestActionSet> actions = 7088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) WebRequestActionSet::Create(NULL, rule.actions, &error, &bad_message); 709c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(error.empty()) << error; 710c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_FALSE(bad_message); 711c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(actions); 712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_FALSE(WebRequestRulesRegistry::StageChecker( 714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) conditions.get(), actions.get(), &error)); 715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr("no time in the request life-cycle")); 716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr(actions->actions().back()->GetName())); 717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 719c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST(WebRequestRulesRegistrySimpleTest, HostPermissionsChecker) { 720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const char kAction[] = // This action requires all URLs host permission. 721c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "{ \n" 722c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 723c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"redirectUrl\": \"http://bar.com\" \n" 724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "} "; 725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<Value> action_value(base::JSONReader::Read(kAction)); 726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(action_value); 727c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 728c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebRequestActionSet::AnyVector actions; 729c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) actions.push_back(linked_ptr<base::Value>(action_value.release())); 730c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(actions.back().get()); 731c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 732c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::string error; 733c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bool bad_message = false; 734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<WebRequestActionSet> action_set( 7358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) WebRequestActionSet::Create(NULL, actions, &error, &bad_message)); 736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(error.empty()) << error; 737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_FALSE(bad_message); 738c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ASSERT_TRUE(action_set); 739c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<Extension> extension_no_url( 741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadManifest("permissions", "web_request_no_host.json")); 742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<Extension> extension_some_urls( 743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadManifest("permissions", "web_request_com_host_permissions.json")); 744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_refptr<Extension> extension_all_urls( 745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) LoadManifest("permissions", "web_request_all_host_permissions.json")); 746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_TRUE(WebRequestRulesRegistry::HostPermissionsChecker( 748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_all_urls.get(), action_set.get(), &error)); 749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_TRUE(error.empty()) << error; 750c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_FALSE(WebRequestRulesRegistry::HostPermissionsChecker( 752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_some_urls.get(), action_set.get(), &error)); 753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr("permission for all")); 754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr(action_set->actions().back()->GetName())); 755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_FALSE(WebRequestRulesRegistry::HostPermissionsChecker( 757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) extension_no_url.get(), action_set.get(), &error)); 758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr("permission for all")); 759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_THAT(error, HasSubstr(action_set->actions().back()->GetName())); 760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)TEST_F(WebRequestRulesRegistryTest, CheckOriginAndPathRegEx) { 763c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) const char kRule[] = 764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "{ \n" 765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"id\": \"rule1\", \n" 766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"conditions\": [ \n" 767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 768c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RequestMatcher\", \n" 769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"url\": {\"originAndPathMatches\": \"fo+.com\"} \n" 770c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 771c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"actions\": [ \n" 773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " { \n" 774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"instanceType\": \"declarativeWebRequest.RedirectRequest\",\n" 775c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"redirectUrl\": \"http://bar.com\" \n" 776c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " } \n" 777c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " ], \n" 778c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) " \"priority\": 200 \n" 779c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) "} "; 780c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 781c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) scoped_ptr<Value> value(base::JSONReader::Read(kRule)); 7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ASSERT_TRUE(value.get()); 7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<linked_ptr<RulesRegistry::Rule> > rules; 7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) rules.push_back(make_linked_ptr(new RulesRegistry::Rule)); 7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ASSERT_TRUE(RulesRegistry::Rule::Populate(*value, rules.back().get())); 7872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) scoped_refptr<WebRequestRulesRegistry> registry( 789c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) new TestWebRequestRulesRegistry(extension_info_map_)); 7902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 7912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) URLMatcher matcher; 7922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::string error = registry->AddRulesImpl(kExtensionId, rules); 793c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ("", error); 7942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 795c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) net::TestURLRequestContext context; 796c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) std::list<LinkedPtrEventResponseDelta> deltas; 797c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 798c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // No match because match is in the query parameter. 799c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GURL url1("http://bar.com/index.html?foo.com"); 8000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest request1(url1, net::DEFAULT_PRIORITY, NULL, &context); 801c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebRequestData request_data1(&request1, ON_BEFORE_REQUEST); 802c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) deltas = registry->CreateDeltas(NULL, request_data1, false); 803c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(0u, deltas.size()); 804c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 805c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // This is a correct match. 806c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) GURL url2("http://foo.com/index.html"); 8070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) net::TestURLRequest request2(url2, net::DEFAULT_PRIORITY, NULL, &context); 808c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) WebRequestData request_data2(&request2, ON_BEFORE_REQUEST); 809c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) deltas = registry->CreateDeltas(NULL, request_data2, false); 810c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(1u, deltas.size()); 811c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extensions 814