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

Side by Side Diff: chrome/browser/extensions/active_script_controller_unittest.cc

Issue 348313003: Create withheld permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Latest master Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 <map> 5 #include <map>
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/browser/extensions/active_script_controller.h" 8 #include "chrome/browser/extensions/active_script_controller.h"
9 #include "chrome/browser/extensions/active_tab_permission_granter.h" 9 #include "chrome/browser/extensions/active_tab_permission_granter.h"
10 #include "chrome/browser/extensions/extension_util.h" 10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/permissions_updater.h"
11 #include "chrome/browser/extensions/tab_helper.h" 12 #include "chrome/browser/extensions/tab_helper.h"
12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 13 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
13 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/browser/navigation_controller.h" 15 #include "content/public/browser/navigation_controller.h"
15 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
17 #include "extensions/browser/extension_registry.h" 18 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/extension.h" 19 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_builder.h" 20 #include "extensions/common/extension_builder.h"
20 #include "extensions/common/feature_switch.h" 21 #include "extensions/common/feature_switch.h"
21 #include "extensions/common/id_util.h" 22 #include "extensions/common/id_util.h"
22 #include "extensions/common/manifest.h" 23 #include "extensions/common/manifest.h"
24 #include "extensions/common/user_script.h"
23 #include "extensions/common/value_builder.h" 25 #include "extensions/common/value_builder.h"
24 26
25 namespace extensions { 27 namespace extensions {
26 28
27 namespace { 29 namespace {
28 30
29 const char kAllHostsPermission[] = "*://*/*"; 31 const char kAllHostsPermission[] = "*://*/*";
30 32
31 } // namespace 33 } // namespace
32 34
33 // Unittests for the ActiveScriptController mostly test the internal logic 35 // Unittests for the ActiveScriptController mostly test the internal logic
34 // of the controller itself (when to allow/deny extension script injection). 36 // of the controller itself (when to allow/deny extension script injection).
35 // Testing real injection is allowed/denied as expected (i.e., that the 37 // Testing real injection is allowed/denied as expected (i.e., that the
36 // ActiveScriptController correctly interfaces in the system) is done in the 38 // ActiveScriptController correctly interfaces in the system) is done in the
37 // ActiveScriptControllerBrowserTests. 39 // ActiveScriptControllerBrowserTests.
38 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { 40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness {
39 protected: 41 protected:
40 ActiveScriptControllerUnitTest(); 42 ActiveScriptControllerUnitTest();
41 virtual ~ActiveScriptControllerUnitTest(); 43 virtual ~ActiveScriptControllerUnitTest();
42 44
43 // Creates an extension with all hosts permission and adds it to the registry. 45 // Creates an extension with all hosts permission and adds it to the registry.
44 const Extension* AddExtension(); 46 const Extension* AddExtension();
45 47
48 // Returns true if the |extension| requires user consent before injecting
49 // a script.
50 bool RequiresUserConsent(const Extension* extension) const;
51
52 // Request an injection for the given |extension|.
53 void RequestInjection(const Extension* extension);
54
55 // Returns the number of times a given extension has had a script execute.
56 size_t GetExecutionCountForExtension(const std::string& extension_id) const;
57
58 ActiveScriptController* controller() const {
59 return active_script_controller_;
60 }
61
62 private:
46 // Returns a closure to use as a script execution for a given extension. 63 // Returns a closure to use as a script execution for a given extension.
47 base::Closure GetExecutionCallbackForExtension( 64 base::Closure GetExecutionCallbackForExtension(
48 const std::string& extension_id); 65 const std::string& extension_id);
49 66
50 // Returns the number of times a given extension has had a script execute.
51 size_t GetExecutionCountForExtension(const std::string& extension_id) const;
52
53 ActiveScriptController* controller() { return active_script_controller_; }
54
55 private:
56 // Increment the number of executions for the given |extension_id|. 67 // Increment the number of executions for the given |extension_id|.
57 void IncrementExecutionCount(const std::string& extension_id); 68 void IncrementExecutionCount(const std::string& extension_id);
58 69
59 virtual void SetUp() OVERRIDE; 70 virtual void SetUp() OVERRIDE;
60 71
61 // Since ActiveScriptController's behavior is behind a flag, override the 72 // Since ActiveScriptController's behavior is behind a flag, override the
62 // feature switch. 73 // feature switch.
63 FeatureSwitch::ScopedOverride feature_override_; 74 FeatureSwitch::ScopedOverride feature_override_;
64 75
65 // The associated ActiveScriptController. 76 // The associated ActiveScriptController.
(...skipping 22 matching lines...) Expand all
88 .Set("description", "an extension") 99 .Set("description", "an extension")
89 .Set("manifest_version", 2) 100 .Set("manifest_version", 2)
90 .Set("version", "1.0.0") 101 .Set("version", "1.0.0")
91 .Set("permissions", 102 .Set("permissions",
92 ListBuilder().Append(kAllHostsPermission))) 103 ListBuilder().Append(kAllHostsPermission)))
93 .SetLocation(Manifest::INTERNAL) 104 .SetLocation(Manifest::INTERNAL)
94 .SetID(kId) 105 .SetID(kId)
95 .Build(); 106 .Build();
96 107
97 ExtensionRegistry::Get(profile())->AddEnabled(extension); 108 ExtensionRegistry::Get(profile())->AddEnabled(extension);
109 PermissionsUpdater(profile()).InitializePermissions(extension);
98 return extension; 110 return extension;
99 } 111 }
100 112
101 base::Closure ActiveScriptControllerUnitTest::GetExecutionCallbackForExtension( 113 bool ActiveScriptControllerUnitTest::RequiresUserConsent(
102 const std::string& extension_id) { 114 const Extension* extension) const {
103 // We use base unretained here, but if this ever gets executed outside of 115 PermissionsData::AccessType access_type =
104 // this test's lifetime, we have a major problem anyway. 116 controller()->RequiresUserConsentForScriptInjectionForTesting(
105 return base::Bind(&ActiveScriptControllerUnitTest::IncrementExecutionCount, 117 extension, UserScript::PROGRAMMATIC_SCRIPT);
106 base::Unretained(this), 118 // We should never downright refuse access in these tests.
107 extension_id); 119 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type);
120 return access_type == PermissionsData::ACCESS_WITHHELD;
121 }
122
123 void ActiveScriptControllerUnitTest::RequestInjection(
124 const Extension* extension) {
125 controller()->RequestScriptInjectionForTesting(
126 extension,
127 GetExecutionCallbackForExtension(extension->id()));
108 } 128 }
109 129
110 size_t ActiveScriptControllerUnitTest::GetExecutionCountForExtension( 130 size_t ActiveScriptControllerUnitTest::GetExecutionCountForExtension(
111 const std::string& extension_id) const { 131 const std::string& extension_id) const {
112 std::map<std::string, int>::const_iterator iter = 132 std::map<std::string, int>::const_iterator iter =
113 extension_executions_.find(extension_id); 133 extension_executions_.find(extension_id);
114 if (iter != extension_executions_.end()) 134 if (iter != extension_executions_.end())
115 return iter->second; 135 return iter->second;
116 return 0u; 136 return 0u;
117 } 137 }
118 138
139 base::Closure ActiveScriptControllerUnitTest::GetExecutionCallbackForExtension(
140 const std::string& extension_id) {
141 // We use base unretained here, but if this ever gets executed outside of
142 // this test's lifetime, we have a major problem anyway.
143 return base::Bind(&ActiveScriptControllerUnitTest::IncrementExecutionCount,
144 base::Unretained(this),
145 extension_id);
146 }
147
119 void ActiveScriptControllerUnitTest::IncrementExecutionCount( 148 void ActiveScriptControllerUnitTest::IncrementExecutionCount(
120 const std::string& extension_id) { 149 const std::string& extension_id) {
121 ++extension_executions_[extension_id]; 150 ++extension_executions_[extension_id];
122 } 151 }
123 152
124 void ActiveScriptControllerUnitTest::SetUp() { 153 void ActiveScriptControllerUnitTest::SetUp() {
125 ChromeRenderViewHostTestHarness::SetUp(); 154 ChromeRenderViewHostTestHarness::SetUp();
126 155
127 TabHelper::CreateForWebContents(web_contents()); 156 TabHelper::CreateForWebContents(web_contents());
128 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents()); 157 TabHelper* tab_helper = TabHelper::FromWebContents(web_contents());
(...skipping 11 matching lines...) Expand all
140 const Extension* extension = AddExtension(); 169 const Extension* extension = AddExtension();
141 ASSERT_TRUE(extension); 170 ASSERT_TRUE(extension);
142 171
143 NavigateAndCommit(GURL("https://www.google.com")); 172 NavigateAndCommit(GURL("https://www.google.com"));
144 173
145 // Ensure that there aren't any executions pending. 174 // Ensure that there aren't any executions pending.
146 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 175 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
147 ASSERT_FALSE(controller()->GetActionForExtension(extension)); 176 ASSERT_FALSE(controller()->GetActionForExtension(extension));
148 177
149 // Since the extension requests all_hosts, we should require user consent. 178 // Since the extension requests all_hosts, we should require user consent.
150 EXPECT_TRUE( 179 EXPECT_TRUE(RequiresUserConsent(extension));
151 controller()->RequiresUserConsentForScriptInjection(extension));
152 180
153 // Request an injection. There should be an action visible, but no executions. 181 // Request an injection. There should be an action visible, but no executions.
154 controller()->RequestScriptInjection( 182 RequestInjection(extension);
155 extension,
156 GetExecutionCallbackForExtension(extension->id()));
157 EXPECT_TRUE(controller()->GetActionForExtension(extension)); 183 EXPECT_TRUE(controller()->GetActionForExtension(extension));
158 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 184 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
159 185
160 // Click to accept the extension executing. 186 // Click to accept the extension executing.
161 controller()->OnClicked(extension); 187 controller()->OnClicked(extension);
162 188
163 // The extension should execute, and the action should go away. 189 // The extension should execute, and the action should go away.
164 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 190 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
165 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 191 EXPECT_FALSE(controller()->GetActionForExtension(extension));
166 192
167 // Since we already executed on the given page, we shouldn't need permission 193 // Since we already executed on the given page, we shouldn't need permission
168 // for a second time. 194 // for a second time.
169 EXPECT_FALSE( 195 EXPECT_FALSE(RequiresUserConsent(extension));
170 controller()->RequiresUserConsentForScriptInjection(extension));
171 196
172 // Reloading should clear those permissions, and we should again require user 197 // Reloading should clear those permissions, and we should again require user
173 // consent. 198 // consent.
174 Reload(); 199 Reload();
175 EXPECT_TRUE( 200 EXPECT_TRUE(RequiresUserConsent(extension));
176 controller()->RequiresUserConsentForScriptInjection(extension));
177 201
178 // Grant access. 202 // Grant access.
179 controller()->RequestScriptInjection( 203 RequestInjection(extension);
180 extension,
181 GetExecutionCallbackForExtension(extension->id()));
182 controller()->OnClicked(extension); 204 controller()->OnClicked(extension);
183 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id())); 205 EXPECT_EQ(2u, GetExecutionCountForExtension(extension->id()));
184 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 206 EXPECT_FALSE(controller()->GetActionForExtension(extension));
185 207
186 // Navigating to another site should also clear the permissions. 208 // Navigating to another site should also clear the permissions.
187 NavigateAndCommit(GURL("https://www.foo.com")); 209 NavigateAndCommit(GURL("https://www.foo.com"));
188 EXPECT_TRUE( 210 EXPECT_TRUE(RequiresUserConsent(extension));
189 controller()->RequiresUserConsentForScriptInjection(extension));
190 } 211 }
191 212
192 // Test that injections that are not executed by the time the user navigates are 213 // Test that injections that are not executed by the time the user navigates are
193 // ignored and never execute. 214 // ignored and never execute.
194 TEST_F(ActiveScriptControllerUnitTest, PendingInjectionsRemovedAtNavigation) { 215 TEST_F(ActiveScriptControllerUnitTest, PendingInjectionsRemovedAtNavigation) {
195 const Extension* extension = AddExtension(); 216 const Extension* extension = AddExtension();
196 ASSERT_TRUE(extension); 217 ASSERT_TRUE(extension);
197 218
198 NavigateAndCommit(GURL("https://www.google.com")); 219 NavigateAndCommit(GURL("https://www.google.com"));
199 220
200 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 221 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
201 222
202 // Request an injection. There should be an action visible, but no executions. 223 // Request an injection. There should be an action visible, but no executions.
203 controller()->RequestScriptInjection( 224 RequestInjection(extension);
204 extension,
205 GetExecutionCallbackForExtension(extension->id()));
206 EXPECT_TRUE(controller()->GetActionForExtension(extension)); 225 EXPECT_TRUE(controller()->GetActionForExtension(extension));
207 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 226 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
208 227
209 // Reload. This should remove the pending injection, and we should not 228 // Reload. This should remove the pending injection, and we should not
210 // execute anything. 229 // execute anything.
211 Reload(); 230 Reload();
212 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 231 EXPECT_FALSE(controller()->GetActionForExtension(extension));
213 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 232 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
214 233
215 // Request and accept a new injection. 234 // Request and accept a new injection.
216 controller()->RequestScriptInjection( 235 RequestInjection(extension);
217 extension,
218 GetExecutionCallbackForExtension(extension->id()));
219 controller()->OnClicked(extension); 236 controller()->OnClicked(extension);
220 237
221 // The extension should only have executed once, even though a grand total 238 // The extension should only have executed once, even though a grand total
222 // of two executions were requested. 239 // of two executions were requested.
223 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 240 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
224 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 241 EXPECT_FALSE(controller()->GetActionForExtension(extension));
225 } 242 }
226 243
227 // Test that queueing multiple pending injections, and then accepting, triggers 244 // Test that queueing multiple pending injections, and then accepting, triggers
228 // them all. 245 // them all.
229 TEST_F(ActiveScriptControllerUnitTest, MultiplePendingInjection) { 246 TEST_F(ActiveScriptControllerUnitTest, MultiplePendingInjection) {
230 const Extension* extension = AddExtension(); 247 const Extension* extension = AddExtension();
231 ASSERT_TRUE(extension); 248 ASSERT_TRUE(extension);
232 NavigateAndCommit(GURL("https://www.google.com")); 249 NavigateAndCommit(GURL("https://www.google.com"));
233 250
234 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id())); 251 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
235 252
236 const size_t kNumInjections = 3u; 253 const size_t kNumInjections = 3u;
237 // Queue multiple pending injections. 254 // Queue multiple pending injections.
238 for (size_t i = 0u; i < kNumInjections; ++i) { 255 for (size_t i = 0u; i < kNumInjections; ++i)
239 controller()->RequestScriptInjection( 256 RequestInjection(extension);
240 extension, 257
241 GetExecutionCallbackForExtension(extension->id()));
242 }
243 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 258 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
244 259
245 controller()->OnClicked(extension); 260 controller()->OnClicked(extension);
246 261
247 // All pending injections should have executed. 262 // All pending injections should have executed.
248 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id())); 263 EXPECT_EQ(kNumInjections, GetExecutionCountForExtension(extension->id()));
249 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 264 EXPECT_FALSE(controller()->GetActionForExtension(extension));
250 } 265 }
251 266
252 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsUseActiveTabPermissions) { 267 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsUseActiveTabPermissions) {
253 const Extension* extension = AddExtension(); 268 const Extension* extension = AddExtension();
254 NavigateAndCommit(GURL("https://www.google.com")); 269 NavigateAndCommit(GURL("https://www.google.com"));
255 270
256 ActiveTabPermissionGranter* active_tab_permission_granter = 271 ActiveTabPermissionGranter* active_tab_permission_granter =
257 TabHelper::FromWebContents(web_contents()) 272 TabHelper::FromWebContents(web_contents())
258 ->active_tab_permission_granter(); 273 ->active_tab_permission_granter();
259 ASSERT_TRUE(active_tab_permission_granter); 274 ASSERT_TRUE(active_tab_permission_granter);
260 // Grant the extension active tab permissions. This normally happens, e.g., 275 // Grant the extension active tab permissions. This normally happens, e.g.,
261 // if the user clicks on a browser action. 276 // if the user clicks on a browser action.
262 active_tab_permission_granter->GrantIfRequested(extension); 277 active_tab_permission_granter->GrantIfRequested(extension);
263 278
264 // Since we have active tab permissions, we shouldn't need user consent 279 // Since we have active tab permissions, we shouldn't need user consent
265 // anymore. 280 // anymore.
266 EXPECT_FALSE(controller()->RequiresUserConsentForScriptInjection(extension)); 281 EXPECT_FALSE(RequiresUserConsent(extension));
267 282
268 // Also test that granting active tab runs any pending tasks. 283 // Also test that granting active tab runs any pending tasks.
269 Reload(); 284 Reload();
270 // Navigating should mean we need permission again. 285 // Navigating should mean we need permission again.
271 EXPECT_TRUE(controller()->RequiresUserConsentForScriptInjection(extension)); 286 EXPECT_TRUE(RequiresUserConsent(extension));
272 287
273 controller()->RequestScriptInjection( 288 RequestInjection(extension);
274 extension,
275 GetExecutionCallbackForExtension(extension->id()));
276 EXPECT_TRUE(controller()->GetActionForExtension(extension)); 289 EXPECT_TRUE(controller()->GetActionForExtension(extension));
277 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id())); 290 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
278 291
279 // Grant active tab. 292 // Grant active tab.
280 active_tab_permission_granter->GrantIfRequested(extension); 293 active_tab_permission_granter->GrantIfRequested(extension);
281 294
282 // The pending injections should have run since active tab permission was 295 // The pending injections should have run since active tab permission was
283 // granted. 296 // granted.
284 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id())); 297 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
285 EXPECT_FALSE(controller()->GetActionForExtension(extension)); 298 EXPECT_FALSE(controller()->GetActionForExtension(extension));
286 } 299 }
287 300
288 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsCanHaveAllUrlsPref) { 301 TEST_F(ActiveScriptControllerUnitTest, ActiveScriptsCanHaveAllUrlsPref) {
289 const Extension* extension = AddExtension(); 302 const Extension* extension = AddExtension();
290 ASSERT_TRUE(extension); 303 ASSERT_TRUE(extension);
291 304
292 NavigateAndCommit(GURL("https://www.google.com")); 305 NavigateAndCommit(GURL("https://www.google.com"));
293 EXPECT_TRUE(controller()->RequiresUserConsentForScriptInjection(extension)); 306 EXPECT_TRUE(RequiresUserConsent(extension));
294 307
295 // Enable the extension on all urls. 308 // Enable the extension on all urls.
296 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), true); 309 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), true);
297 310
298 EXPECT_FALSE(controller()->RequiresUserConsentForScriptInjection(extension)); 311 EXPECT_FALSE(RequiresUserConsent(extension));
299 // This should carry across navigations, and websites. 312 // This should carry across navigations, and websites.
300 NavigateAndCommit(GURL("http://www.foo.com")); 313 NavigateAndCommit(GURL("http://www.foo.com"));
301 EXPECT_FALSE(controller()->RequiresUserConsentForScriptInjection(extension)); 314 EXPECT_FALSE(RequiresUserConsent(extension));
302 315
303 // Turning off the preference should have instant effect. 316 // Turning off the preference should have instant effect.
304 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); 317 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false);
305 EXPECT_TRUE(controller()->RequiresUserConsentForScriptInjection(extension)); 318 EXPECT_TRUE(RequiresUserConsent(extension));
306 319
307 // And should also persist across navigations and websites. 320 // And should also persist across navigations and websites.
308 NavigateAndCommit(GURL("http://www.bar.com")); 321 NavigateAndCommit(GURL("http://www.bar.com"));
309 EXPECT_TRUE(controller()->RequiresUserConsentForScriptInjection(extension)); 322 EXPECT_TRUE(RequiresUserConsent(extension));
310 } 323 }
311 324
312 } // namespace extensions 325 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698