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

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

Issue 396033002: Support "always allow" for runtime script execution (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ReloadExtension in unittest Created 6 years, 4 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
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"
(...skipping 24 matching lines...) Expand all
35 // Unittests for the ActiveScriptController mostly test the internal logic 35 // Unittests for the ActiveScriptController mostly test the internal logic
36 // of the controller itself (when to allow/deny extension script injection). 36 // of the controller itself (when to allow/deny extension script injection).
37 // 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
38 // ActiveScriptController correctly interfaces in the system) is done in the 38 // ActiveScriptController correctly interfaces in the system) is done in the
39 // ActiveScriptControllerBrowserTests. 39 // ActiveScriptControllerBrowserTests.
40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness { 40 class ActiveScriptControllerUnitTest : public ChromeRenderViewHostTestHarness {
41 protected: 41 protected:
42 ActiveScriptControllerUnitTest(); 42 ActiveScriptControllerUnitTest();
43 virtual ~ActiveScriptControllerUnitTest(); 43 virtual ~ActiveScriptControllerUnitTest();
44 44
45 // Creates an extension with all hosts permission and adds it to the registry. 45 // Adds an extension with a generated id.
46 const Extension* AddExtension(); 46 const Extension* AddExtension();
47 47
48 // Creates an extension with |id| and all host permission and adds it to the
49 // registry.
50 const Extension* AddExtension(const std::string id);
Devlin 2014/08/14 01:20:18 const &
51
52 // Reloads the extension with |id| by removing it from the registry and
53 // readding it.
54 const Extension* ReloadExtension(const std::string id);
Devlin 2014/08/14 01:20:18 const &
55
48 // Returns true if the |extension| requires user consent before injecting 56 // Returns true if the |extension| requires user consent before injecting
49 // a script. 57 // a script.
50 bool RequiresUserConsent(const Extension* extension) const; 58 bool RequiresUserConsent(const Extension* extension) const;
51 59
52 // Request an injection for the given |extension|. 60 // Request an injection for the given |extension|.
53 void RequestInjection(const Extension* extension); 61 void RequestInjection(const Extension* extension);
54 62
55 // Returns the number of times a given extension has had a script execute. 63 // Returns the number of times a given extension has had a script execute.
56 size_t GetExecutionCountForExtension(const std::string& extension_id) const; 64 size_t GetExecutionCountForExtension(const std::string& extension_id) const;
57 65
(...skipping 25 matching lines...) Expand all
83 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest() 91 ActiveScriptControllerUnitTest::ActiveScriptControllerUnitTest()
84 : feature_override_(FeatureSwitch::scripts_require_action(), 92 : feature_override_(FeatureSwitch::scripts_require_action(),
85 FeatureSwitch::OVERRIDE_ENABLED), 93 FeatureSwitch::OVERRIDE_ENABLED),
86 active_script_controller_(NULL) { 94 active_script_controller_(NULL) {
87 } 95 }
88 96
89 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() { 97 ActiveScriptControllerUnitTest::~ActiveScriptControllerUnitTest() {
90 } 98 }
91 99
92 const Extension* ActiveScriptControllerUnitTest::AddExtension() { 100 const Extension* ActiveScriptControllerUnitTest::AddExtension() {
93 const std::string kId = id_util::GenerateId("all_hosts_extension"); 101 return AddExtension(id_util::GenerateId("all_hosts_extension"));
Devlin 2014/08/14 01:20:18 All of these ids will always be the same, so why d
gpdavis 2014/08/14 20:05:43 Kalman suggested making an AddExtension method tha
not at google - send to devlin 2014/08/14 20:08:07 I prefer it being explicit. One version generates
102 }
103
104 const Extension* ActiveScriptControllerUnitTest::AddExtension(
105 const std::string id) {
Devlin 2014/08/14 01:20:18 const &
94 scoped_refptr<const Extension> extension = 106 scoped_refptr<const Extension> extension =
95 ExtensionBuilder() 107 ExtensionBuilder()
96 .SetManifest( 108 .SetManifest(
97 DictionaryBuilder() 109 DictionaryBuilder()
98 .Set("name", "all_hosts_extension") 110 .Set("name", "all_hosts_extension")
99 .Set("description", "an extension") 111 .Set("description", "an extension")
100 .Set("manifest_version", 2) 112 .Set("manifest_version", 2)
101 .Set("version", "1.0.0") 113 .Set("version", "1.0.0")
102 .Set("permissions", 114 .Set("permissions",
103 ListBuilder().Append(kAllHostsPermission))) 115 ListBuilder().Append(kAllHostsPermission)))
104 .SetLocation(Manifest::INTERNAL) 116 .SetLocation(Manifest::INTERNAL)
105 .SetID(kId) 117 .SetID(id)
106 .Build(); 118 .Build();
107 119
108 ExtensionRegistry::Get(profile())->AddEnabled(extension); 120 ExtensionRegistry::Get(profile())->AddEnabled(extension);
109 PermissionsUpdater(profile()).InitializePermissions(extension); 121 PermissionsUpdater(profile()).InitializePermissions(extension);
110 return extension; 122 return extension;
111 } 123 }
112 124
125 const Extension* ActiveScriptControllerUnitTest::ReloadExtension(
126 const std::string id) {
Devlin 2014/08/14 01:20:18 const &
gpdavis 2014/08/14 20:05:43 This occurred to me, but since ReloadExtension rem
not at google - send to devlin 2014/08/14 20:08:07 Good point, though the solution here is for the ca
gpdavis 2014/08/14 20:19:20 Sweet, I like this idea.
127 ExtensionRegistry::Get(profile())->RemoveEnabled(id);
128 return AddExtension(id);
129 }
130
113 bool ActiveScriptControllerUnitTest::RequiresUserConsent( 131 bool ActiveScriptControllerUnitTest::RequiresUserConsent(
114 const Extension* extension) const { 132 const Extension* extension) const {
115 PermissionsData::AccessType access_type = 133 PermissionsData::AccessType access_type =
116 controller()->RequiresUserConsentForScriptInjectionForTesting( 134 controller()->RequiresUserConsentForScriptInjectionForTesting(
117 extension, UserScript::PROGRAMMATIC_SCRIPT); 135 extension, UserScript::PROGRAMMATIC_SCRIPT);
118 // We should never downright refuse access in these tests. 136 // We should never downright refuse access in these tests.
119 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type); 137 DCHECK_NE(PermissionsData::ACCESS_DENIED, access_type);
120 return access_type == PermissionsData::ACCESS_WITHHELD; 138 return access_type == PermissionsData::ACCESS_WITHHELD;
121 } 139 }
122 140
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 333
316 // Turning off the preference should have instant effect. 334 // Turning off the preference should have instant effect.
317 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false); 335 util::SetAllowedScriptingOnAllUrls(extension->id(), profile(), false);
318 EXPECT_TRUE(RequiresUserConsent(extension)); 336 EXPECT_TRUE(RequiresUserConsent(extension));
319 337
320 // And should also persist across navigations and websites. 338 // And should also persist across navigations and websites.
321 NavigateAndCommit(GURL("http://www.bar.com")); 339 NavigateAndCommit(GURL("http://www.bar.com"));
322 EXPECT_TRUE(RequiresUserConsent(extension)); 340 EXPECT_TRUE(RequiresUserConsent(extension));
323 } 341 }
324 342
343 TEST_F(ActiveScriptControllerUnitTest, TestAlwaysRun) {
344 const Extension* extension = AddExtension();
345 ASSERT_TRUE(extension);
346
347 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl"));
348
349 // Ensure that there aren't any executions pending.
350 ASSERT_EQ(0u, GetExecutionCountForExtension(extension->id()));
351 ASSERT_FALSE(controller()->GetActionForExtension(extension));
352
353 // Since the extension requests all_hosts, we should require user consent.
354 EXPECT_TRUE(RequiresUserConsent(extension));
355
356 // Request an injection. There should be an action visible, but no executions.
357 RequestInjection(extension);
358 EXPECT_TRUE(controller()->GetActionForExtension(extension));
359 EXPECT_EQ(0u, GetExecutionCountForExtension(extension->id()));
360
361 // Simulate clicking "always run" menu item.
Devlin 2014/08/14 01:20:18 Abstract away "clicking the menu item".
gpdavis 2014/08/14 20:05:43 Done.
362 controller()->AlwaysRunOnVisibleHost(extension);
363
364 // The extension should execute, and the action should go away.
365 EXPECT_EQ(1u, GetExecutionCountForExtension(extension->id()));
366 EXPECT_FALSE(controller()->GetActionForExtension(extension));
367
368 // Since we already executed on the given page, we shouldn't need permission
369 // for a second time.
370 EXPECT_FALSE(RequiresUserConsent(extension));
371
372 // Navigating to another site that hasn't been granted a persisted permission
373 // should necessitate user consent.
374 NavigateAndCommit(GURL("https://www.foo.com/bar"));
375 EXPECT_TRUE(RequiresUserConsent(extension));
376
377 // We shouldn't need user permission upon returning to the original host.
378 NavigateAndCommit(GURL("https://www.google.com/?gws_rd=ssl"));
Devlin 2014/08/14 01:20:18 We should check on returning to a different site w
gpdavis 2014/08/14 20:05:43 Good idea. Done.
379 EXPECT_FALSE(RequiresUserConsent(extension));
380
381 // Reloading the extension should clear active permissions, but not persisted
382 // permissions.
383 extension = ReloadExtension(extension->id());
384 Reload();
385 EXPECT_FALSE(RequiresUserConsent(extension));
386 }
387
325 } // namespace extensions 388 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698