| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/declarative_webrequest/webrequest_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/memory/linked_ptr.h" | 9 #include "base/memory/linked_ptr.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 virtual ~WebRequestRulesRegistryTest() {} | 36 virtual ~WebRequestRulesRegistryTest() {} |
| 37 | 37 |
| 38 virtual void TearDown() OVERRIDE { | 38 virtual void TearDown() OVERRIDE { |
| 39 // Make sure that deletion traits of all registries are executed. | 39 // Make sure that deletion traits of all registries are executed. |
| 40 message_loop.RunAllPending(); | 40 message_loop.RunAllPending(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Returns a rule that roughly matches http://*.example.com and | 43 // Returns a rule that roughly matches http://*.example.com and |
| 44 // https://www.example.com and cancels it | 44 // https://www.example.com and cancels it |
| 45 linked_ptr<RulesRegistry::Rule> CreateRule1() { | 45 linked_ptr<RulesRegistry::Rule> CreateRule1() { |
| 46 ListValue* scheme_http = new ListValue(); |
| 47 scheme_http->Append(Value::CreateStringValue("http")); |
| 46 DictionaryValue http_condition_dict; | 48 DictionaryValue http_condition_dict; |
| 47 http_condition_dict.SetString("scheme", "http"); | 49 http_condition_dict.Set(keys::kSchemesKey, scheme_http); |
| 48 http_condition_dict.SetString("host_suffix", "example.com"); | 50 http_condition_dict.SetString("host_suffix", "example.com"); |
| 49 http_condition_dict.SetString(keys::kInstanceTypeKey, | 51 http_condition_dict.SetString(keys::kInstanceTypeKey, |
| 50 keys::kRequestMatcherType); | 52 keys::kRequestMatcherType); |
| 51 | 53 |
| 54 ListValue* scheme_https = new ListValue(); |
| 55 scheme_http->Append(Value::CreateStringValue("https")); |
| 52 DictionaryValue https_condition_dict; | 56 DictionaryValue https_condition_dict; |
| 53 https_condition_dict.SetString("scheme", "https"); | 57 https_condition_dict.Set(keys::kSchemesKey, scheme_https); |
| 54 https_condition_dict.SetString("host_suffix", "example.com"); | 58 https_condition_dict.SetString("host_suffix", "example.com"); |
| 55 https_condition_dict.SetString("host_prefix", "www"); | 59 https_condition_dict.SetString("host_prefix", "www"); |
| 56 https_condition_dict.SetString(keys::kInstanceTypeKey, | 60 https_condition_dict.SetString(keys::kInstanceTypeKey, |
| 57 keys::kRequestMatcherType); | 61 keys::kRequestMatcherType); |
| 58 | 62 |
| 59 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( | 63 linked_ptr<json_schema_compiler::any::Any> condition1 = make_linked_ptr( |
| 60 new json_schema_compiler::any::Any); | 64 new json_schema_compiler::any::Any); |
| 61 condition1->Init(http_condition_dict); | 65 condition1->Init(http_condition_dict); |
| 62 | 66 |
| 63 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( | 67 linked_ptr<json_schema_compiler::any::Any> condition2 = make_linked_ptr( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 118 |
| 115 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { | 119 TEST_F(WebRequestRulesRegistryTest, AddRulesImpl) { |
| 116 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); | 120 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); |
| 117 std::string error; | 121 std::string error; |
| 118 | 122 |
| 119 std::vector<linked_ptr<RulesRegistry::Rule> > rules; | 123 std::vector<linked_ptr<RulesRegistry::Rule> > rules; |
| 120 rules.push_back(CreateRule1()); | 124 rules.push_back(CreateRule1()); |
| 121 rules.push_back(CreateRule2()); | 125 rules.push_back(CreateRule2()); |
| 122 | 126 |
| 123 error = registry->AddRules(kExtensionId, rules); | 127 error = registry->AddRules(kExtensionId, rules); |
| 124 EXPECT_TRUE(error.empty()); | 128 EXPECT_EQ("", error); |
| 125 | 129 |
| 126 std::set<WebRequestRule::GlobalRuleId> matches; | 130 std::set<WebRequestRule::GlobalRuleId> matches; |
| 127 | 131 |
| 128 GURL http_url("http://www.example.com"); | 132 GURL http_url("http://www.example.com"); |
| 129 TestURLRequest http_request(http_url, NULL); | 133 TestURLRequest http_request(http_url, NULL); |
| 130 matches = registry->GetMatches(&http_request, ON_BEFORE_REQUEST); | 134 matches = registry->GetMatches(&http_request, ON_BEFORE_REQUEST); |
| 131 EXPECT_EQ(2u, matches.size()); | 135 EXPECT_EQ(2u, matches.size()); |
| 132 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != | 136 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId1)) != |
| 133 matches.end()); | 137 matches.end()); |
| 134 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 138 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
| 135 matches.end()); | 139 matches.end()); |
| 136 | 140 |
| 137 GURL foobar_url("http://www.foobar.com"); | 141 GURL foobar_url("http://www.foobar.com"); |
| 138 TestURLRequest foobar_request(foobar_url, NULL); | 142 TestURLRequest foobar_request(foobar_url, NULL); |
| 139 matches = registry->GetMatches(&foobar_request, ON_BEFORE_REQUEST); | 143 matches = registry->GetMatches(&foobar_request, ON_BEFORE_REQUEST); |
| 140 EXPECT_EQ(1u, matches.size()); | 144 EXPECT_EQ(1u, matches.size()); |
| 141 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != | 145 EXPECT_TRUE(matches.find(std::make_pair(kExtensionId, kRuleId2)) != |
| 142 matches.end()); | 146 matches.end()); |
| 143 } | 147 } |
| 144 | 148 |
| 145 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { | 149 TEST_F(WebRequestRulesRegistryTest, RemoveRulesImpl) { |
| 146 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); | 150 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); |
| 147 std::string error; | 151 std::string error; |
| 148 | 152 |
| 149 // Setup RulesRegistry to contain two rules. | 153 // Setup RulesRegistry to contain two rules. |
| 150 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; | 154 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add; |
| 151 rules_to_add.push_back(CreateRule1()); | 155 rules_to_add.push_back(CreateRule1()); |
| 152 rules_to_add.push_back(CreateRule2()); | 156 rules_to_add.push_back(CreateRule2()); |
| 153 error = registry->AddRules(kExtensionId, rules_to_add); | 157 error = registry->AddRules(kExtensionId, rules_to_add); |
| 154 EXPECT_TRUE(error.empty()); | 158 EXPECT_EQ("", error); |
| 155 | 159 |
| 156 // Verify initial state. | 160 // Verify initial state. |
| 157 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 161 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
| 158 registry->GetAllRules(kExtensionId, ®istered_rules); | 162 registry->GetAllRules(kExtensionId, ®istered_rules); |
| 159 EXPECT_EQ(2u, registered_rules.size()); | 163 EXPECT_EQ(2u, registered_rules.size()); |
| 160 | 164 |
| 161 // Remove first rule. | 165 // Remove first rule. |
| 162 std::vector<std::string> rules_to_remove; | 166 std::vector<std::string> rules_to_remove; |
| 163 rules_to_remove.push_back(kRuleId1); | 167 rules_to_remove.push_back(kRuleId1); |
| 164 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 168 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
| 165 EXPECT_TRUE(error.empty()); | 169 EXPECT_EQ("", error); |
| 166 | 170 |
| 167 // Verify that only one rule is left. | 171 // Verify that only one rule is left. |
| 168 registered_rules.clear(); | 172 registered_rules.clear(); |
| 169 registry->GetAllRules(kExtensionId, ®istered_rules); | 173 registry->GetAllRules(kExtensionId, ®istered_rules); |
| 170 EXPECT_EQ(1u, registered_rules.size()); | 174 EXPECT_EQ(1u, registered_rules.size()); |
| 171 | 175 |
| 172 // Now rules_to_remove contains both rules, i.e. one that does not exist in | 176 // Now rules_to_remove contains both rules, i.e. one that does not exist in |
| 173 // the rules registry anymore. Effectively we only remove the second rule. | 177 // the rules registry anymore. Effectively we only remove the second rule. |
| 174 rules_to_remove.push_back(kRuleId2); | 178 rules_to_remove.push_back(kRuleId2); |
| 175 error = registry->RemoveRules(kExtensionId, rules_to_remove); | 179 error = registry->RemoveRules(kExtensionId, rules_to_remove); |
| 176 EXPECT_TRUE(error.empty()); | 180 EXPECT_EQ("", error); |
| 177 | 181 |
| 178 // Verify that everything is gone. | 182 // Verify that everything is gone. |
| 179 registered_rules.clear(); | 183 registered_rules.clear(); |
| 180 registry->GetAllRules(kExtensionId, ®istered_rules); | 184 registry->GetAllRules(kExtensionId, ®istered_rules); |
| 181 EXPECT_EQ(0u, registered_rules.size()); | 185 EXPECT_EQ(0u, registered_rules.size()); |
| 182 | 186 |
| 183 EXPECT_TRUE(registry->IsEmpty()); | 187 EXPECT_TRUE(registry->IsEmpty()); |
| 184 } | 188 } |
| 185 | 189 |
| 186 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { | 190 TEST_F(WebRequestRulesRegistryTest, RemoveAllRulesImpl) { |
| 187 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); | 191 scoped_refptr<WebRequestRulesRegistry> registry(new WebRequestRulesRegistry); |
| 188 std::string error; | 192 std::string error; |
| 189 | 193 |
| 190 // Setup RulesRegistry to contain two rules, one for each extension. | 194 // Setup RulesRegistry to contain two rules, one for each extension. |
| 191 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); | 195 std::vector<linked_ptr<RulesRegistry::Rule> > rules_to_add(1); |
| 192 rules_to_add[0] = CreateRule1(); | 196 rules_to_add[0] = CreateRule1(); |
| 193 error = registry->AddRules(kExtensionId, rules_to_add); | 197 error = registry->AddRules(kExtensionId, rules_to_add); |
| 194 EXPECT_TRUE(error.empty()); | 198 EXPECT_EQ("", error); |
| 195 | 199 |
| 196 rules_to_add[0] = CreateRule2(); | 200 rules_to_add[0] = CreateRule2(); |
| 197 error = registry->AddRules(kExtensionId2, rules_to_add); | 201 error = registry->AddRules(kExtensionId2, rules_to_add); |
| 198 EXPECT_TRUE(error.empty()); | 202 EXPECT_EQ("", error); |
| 199 | 203 |
| 200 // Verify initial state. | 204 // Verify initial state. |
| 201 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; | 205 std::vector<linked_ptr<RulesRegistry::Rule> > registered_rules; |
| 202 registry->GetAllRules(kExtensionId, ®istered_rules); | 206 registry->GetAllRules(kExtensionId, ®istered_rules); |
| 203 EXPECT_EQ(1u, registered_rules.size()); | 207 EXPECT_EQ(1u, registered_rules.size()); |
| 204 registered_rules.clear(); | 208 registered_rules.clear(); |
| 205 registry->GetAllRules(kExtensionId2, ®istered_rules); | 209 registry->GetAllRules(kExtensionId2, ®istered_rules); |
| 206 EXPECT_EQ(1u, registered_rules.size()); | 210 EXPECT_EQ(1u, registered_rules.size()); |
| 207 | 211 |
| 208 // Remove rule of first extension. | 212 // Remove rule of first extension. |
| 209 error = registry->RemoveAllRules(kExtensionId); | 213 error = registry->RemoveAllRules(kExtensionId); |
| 210 EXPECT_TRUE(error.empty()); | 214 EXPECT_EQ("", error); |
| 211 | 215 |
| 212 // Verify that only the first rule is deleted. | 216 // Verify that only the first rule is deleted. |
| 213 registered_rules.clear(); | 217 registered_rules.clear(); |
| 214 registry->GetAllRules(kExtensionId, ®istered_rules); | 218 registry->GetAllRules(kExtensionId, ®istered_rules); |
| 215 EXPECT_EQ(0u, registered_rules.size()); | 219 EXPECT_EQ(0u, registered_rules.size()); |
| 216 registered_rules.clear(); | 220 registered_rules.clear(); |
| 217 registry->GetAllRules(kExtensionId2, ®istered_rules); | 221 registry->GetAllRules(kExtensionId2, ®istered_rules); |
| 218 EXPECT_EQ(1u, registered_rules.size()); | 222 EXPECT_EQ(1u, registered_rules.size()); |
| 219 | 223 |
| 220 // Test removing rules if none exist. | 224 // Test removing rules if none exist. |
| 221 error = registry->RemoveAllRules(kExtensionId); | 225 error = registry->RemoveAllRules(kExtensionId); |
| 222 EXPECT_TRUE(error.empty()); | 226 EXPECT_EQ("", error); |
| 223 | 227 |
| 224 // Remove rule from second extension. | 228 // Remove rule from second extension. |
| 225 error = registry->RemoveAllRules(kExtensionId2); | 229 error = registry->RemoveAllRules(kExtensionId2); |
| 226 EXPECT_TRUE(error.empty()); | 230 EXPECT_EQ("", error); |
| 227 | 231 |
| 228 EXPECT_TRUE(registry->IsEmpty()); | 232 EXPECT_TRUE(registry->IsEmpty()); |
| 229 } | 233 } |
| 230 | 234 |
| 231 } // namespace extensions | 235 } // namespace extensions |
| OLD | NEW |