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

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

Issue 2166513002: Create --disable-extensions-except switch (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adjusted path to test extensions s.t. fixes trybot failings Created 4 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 (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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base_switches.h" 10 #include "base/base_switches.h"
11 #include "base/command_line.h" 11 #include "base/command_line.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/path_service.h" 14 #include "base/path_service.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "chrome/browser/chrome_notification_types.h" 18 #include "chrome/browser/chrome_notification_types.h"
19 #include "chrome/browser/extensions/extension_browsertest.h"
19 #include "chrome/browser/extensions/extension_service.h" 20 #include "chrome/browser/extensions/extension_service.h"
20 #include "chrome/browser/extensions/extension_util.h" 21 #include "chrome/browser/extensions/extension_util.h"
21 #include "chrome/browser/extensions/shared_user_script_master.h" 22 #include "chrome/browser/extensions/shared_user_script_master.h"
22 #include "chrome/browser/prefs/chrome_pref_service_factory.h" 23 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/browser.h" 25 #include "chrome/browser/ui/browser.h"
25 #include "chrome/browser/ui/tabs/tab_strip_model.h" 26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
26 #include "chrome/common/chrome_constants.h" 27 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths.h" 28 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h" 29 #include "chrome/common/chrome_switches.h"
29 #include "chrome/test/base/in_process_browser_test.h" 30 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/testing_profile.h" 31 #include "chrome/test/base/testing_profile.h"
31 #include "chrome/test/base/ui_test_utils.h" 32 #include "chrome/test/base/ui_test_utils.h"
32 #include "content/public/browser/notification_details.h" 33 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_service.h" 34 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
35 #include "content/public/common/content_switches.h" 36 #include "content/public/common/content_switches.h"
36 #include "content/public/test/browser_test_utils.h" 37 #include "content/public/test/browser_test_utils.h"
37 #include "extensions/browser/extension_registry.h" 38 #include "extensions/browser/extension_registry.h"
38 #include "extensions/browser/extension_system.h" 39 #include "extensions/browser/extension_system.h"
39 #include "extensions/common/extension.h" 40 #include "extensions/common/extension.h"
40 #include "extensions/common/extension_set.h" 41 #include "extensions/common/extension_set.h"
41 #include "extensions/common/feature_switch.h" 42 #include "extensions/common/feature_switch.h"
42 #include "net/base/filename_util.h" 43 #include "net/base/filename_util.h"
43 44
44 using extensions::FeatureSwitch; 45 using extensions::FeatureSwitch;
46 using extensions::ExtensionRegistry;
45 47
46 // This file contains high-level startup tests for the extensions system. We've 48 // This file contains high-level startup tests for the extensions system. We've
47 // had many silly bugs where command line flags did not get propagated correctly 49 // had many silly bugs where command line flags did not get propagated correctly
48 // into the services, so we didn't start correctly. 50 // into the services, so we didn't start correctly.
49 51
50 class ExtensionStartupTestBase : public InProcessBrowserTest { 52 class ExtensionStartupTestBase : public InProcessBrowserTest {
51 public: 53 public:
52 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) { 54 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) {
53 num_expected_extensions_ = 3; 55 num_expected_extensions_ = 3;
54 } 56 }
55 57
56 protected: 58 protected:
57 // InProcessBrowserTest 59 // InProcessBrowserTest
58 void SetUpCommandLine(base::CommandLine* command_line) override { 60 void SetUpCommandLine(base::CommandLine* command_line) override {
59 if (load_extensions_.empty()) { 61 if (load_extensions_.empty()) {
60 // If no |load_extensions_| were specified, allow unauthenticated 62 // If no |load_extensions_| were specified, allow unauthenticated
61 // extension settings to be loaded from Preferences as if they had been 63 // extension settings to be loaded from Preferences as if they had been
62 // authenticated correctly before they were handed to the ExtensionSystem. 64 // authenticated correctly before they were handed to the ExtensionSystem.
63 command_line->AppendSwitchASCII( 65 command_line->AppendSwitchASCII(
64 switches::kForceFieldTrials, 66 switches::kForceFieldTrials,
65 base::StringPrintf( 67 base::StringPrintf(
66 "%s/%s/", 68 "%s/%s/", chrome_prefs::internals::kSettingsEnforcementTrialName,
67 chrome_prefs::internals::kSettingsEnforcementTrialName,
68 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement)); 69 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement));
69 #if defined(OFFICIAL_BUILD) && defined(OS_WIN) 70 #if defined(OFFICIAL_BUILD) && defined(OS_WIN)
70 // In Windows official builds, it is not possible to disable settings 71 // In Windows official builds, it is not possible to disable settings
71 // authentication. 72 // authentication.
72 unauthenticated_load_allowed_ = false; 73 unauthenticated_load_allowed_ = false;
73 #endif 74 #endif
74 } else { 75 } else {
75 base::FilePath::StringType paths = 76 base::FilePath::StringType paths = base::JoinString(
76 base::JoinString(load_extensions_, 77 load_extensions_, base::FilePath::StringType(1, ','));
77 base::FilePath::StringType(1, ',')); 78 command_line->AppendSwitchNative(switches::kLoadExtension, paths);
78 command_line->AppendSwitchNative(switches::kLoadExtension,
79 paths);
80 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck); 79 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
81 } 80 }
82 } 81 }
83 82
84 bool SetUpUserDataDirectory() override { 83 bool SetUpUserDataDirectory() override {
85 base::FilePath profile_dir; 84 base::FilePath profile_dir;
86 PathService::Get(chrome::DIR_USER_DATA, &profile_dir); 85 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
87 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir); 86 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
88 base::CreateDirectory(profile_dir); 87 base::CreateDirectory(profile_dir);
89 88
90 preferences_file_ = profile_dir.Append(chrome::kPreferencesFilename); 89 preferences_file_ = profile_dir.Append(chrome::kPreferencesFilename);
91 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts"); 90 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
92 extensions_dir_ = profile_dir.AppendASCII("Extensions"); 91 extensions_dir_ = profile_dir.AppendASCII("Extensions");
93 92
94 if (load_extensions_.empty()) { 93 if (load_extensions_.empty()) {
95 base::FilePath src_dir; 94 base::FilePath src_dir;
96 PathService::Get(chrome::DIR_TEST_DATA, &src_dir); 95 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
97 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good"); 96 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
98 97
99 base::CopyFile(src_dir.Append(chrome::kPreferencesFilename), 98 base::CopyFile(src_dir.Append(chrome::kPreferencesFilename),
100 preferences_file_); 99 preferences_file_);
101 base::CopyDirectory(src_dir.AppendASCII("Extensions"), 100 base::CopyDirectory(src_dir.AppendASCII("Extensions"), profile_dir,
102 profile_dir, true); // recursive 101 true); // recursive
103 } 102 }
104 return true; 103 return true;
105 } 104 }
106 105
107 void SetUpInProcessBrowserTestFixture() override { 106 void SetUpInProcessBrowserTestFixture() override {
108 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 107 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
109 108
110 // Bots are on a domain, turn off the domain check for settings hardening in 109 // Bots are on a domain, turn off the domain check for settings hardening in
111 // order to be able to test all SettingsEnforcement groups. 110 // order to be able to test all SettingsEnforcement groups.
112 chrome_prefs::DisableDomainCheckForTesting(); 111 chrome_prefs::DisableDomainCheckForTesting();
(...skipping 21 matching lines...) Expand all
134 if (extension->location() != extensions::Manifest::COMPONENT) 133 if (extension->location() != extensions::Manifest::COMPONENT)
135 found_extensions++; 134 found_extensions++;
136 } 135 }
137 136
138 if (!unauthenticated_load_allowed_) 137 if (!unauthenticated_load_allowed_)
139 num_expected_extensions = 0; 138 num_expected_extensions = 0;
140 139
141 ASSERT_EQ(static_cast<uint32_t>(num_expected_extensions), 140 ASSERT_EQ(static_cast<uint32_t>(num_expected_extensions),
142 static_cast<uint32_t>(found_extensions)); 141 static_cast<uint32_t>(found_extensions));
143 142
144 ExtensionService* service = extensions::ExtensionSystem::Get( 143 ExtensionService* service =
145 browser()->profile())->extension_service(); 144 extensions::ExtensionSystem::Get(browser()->profile())
145 ->extension_service();
146 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled()); 146 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
147 147
148 content::WindowedNotificationObserver user_scripts_observer( 148 content::WindowedNotificationObserver user_scripts_observer(
149 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, 149 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
150 content::NotificationService::AllSources()); 150 content::NotificationService::AllSources());
151 extensions::SharedUserScriptMaster* master = 151 extensions::SharedUserScriptMaster* master =
152 extensions::ExtensionSystem::Get(browser()->profile())-> 152 extensions::ExtensionSystem::Get(browser()->profile())
153 shared_user_script_master(); 153 ->shared_user_script_master();
154 if (!master->scripts_ready()) 154 if (!master->scripts_ready())
155 user_scripts_observer.Wait(); 155 user_scripts_observer.Wait();
156 ASSERT_TRUE(master->scripts_ready()); 156 ASSERT_TRUE(master->scripts_ready());
157 } 157 }
158 158
159 void TestInjection(bool expect_css, bool expect_script) { 159 void TestInjection(bool expect_css, bool expect_script) {
160 if (!unauthenticated_load_allowed_) { 160 if (!unauthenticated_load_allowed_) {
161 expect_css = false; 161 expect_css = false;
162 expect_script = false; 162 expect_script = false;
163 } 163 }
164 164
165 // Load a page affected by the content script and test to see the effect. 165 // Load a page affected by the content script and test to see the effect.
166 base::FilePath test_file; 166 base::FilePath test_file;
167 PathService::Get(chrome::DIR_TEST_DATA, &test_file); 167 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
168 test_file = test_file.AppendASCII("extensions") 168 test_file =
169 .AppendASCII("test_file.html"); 169 test_file.AppendASCII("extensions").AppendASCII("test_file.html");
170 170
171 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file)); 171 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
172 172
173 bool result = false; 173 bool result = false;
174 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 174 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
175 browser()->tab_strip_model()->GetActiveWebContents(), 175 browser()->tab_strip_model()->GetActiveWebContents(),
176 "window.domAutomationController.send(" 176 "window.domAutomationController.send("
177 " document.defaultView.getComputedStyle(document.body, null)." 177 " document.defaultView.getComputedStyle(document.body, null)."
178 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')", 178 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
179 &result)); 179 &result));
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 if ((*it)->location() == extensions::Manifest::COMPONENT) 230 if ((*it)->location() == extensions::Manifest::COMPONENT)
231 continue; 231 continue;
232 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile())) 232 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile()))
233 extension_list.push_back(it->get()); 233 extension_list.push_back(it->get());
234 } 234 }
235 235
236 for (size_t i = 0; i < extension_list.size(); ++i) { 236 for (size_t i = 0; i < extension_list.size(); ++i) {
237 content::WindowedNotificationObserver user_scripts_observer( 237 content::WindowedNotificationObserver user_scripts_observer(
238 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED, 238 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
239 content::NotificationService::AllSources()); 239 content::NotificationService::AllSources());
240 extensions::util::SetAllowFileAccess( 240 extensions::util::SetAllowFileAccess(extension_list[i]->id(),
241 extension_list[i]->id(), browser()->profile(), false); 241 browser()->profile(), false);
242 user_scripts_observer.Wait(); 242 user_scripts_observer.Wait();
243 } 243 }
244 244
245 TestInjection(false, false); 245 TestInjection(false, false);
246 } 246 }
247 247
248 // ExtensionsLoadTest 248 // ExtensionsLoadTest
249 // Ensures that we can startup the browser with --load-extension and see them 249 // Ensures that we can startup the browser with --load-extension and see them
250 // run. 250 // run.
251 class ExtensionsLoadTest : public ExtensionStartupTestBase { 251 class ExtensionsLoadTest : public ExtensionStartupTestBase {
252 public: 252 public:
253 ExtensionsLoadTest() { 253 ExtensionsLoadTest() {
254 base::FilePath one_extension_path; 254 base::FilePath one_extension_path;
255 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path); 255 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
256 one_extension_path = one_extension_path 256 one_extension_path = one_extension_path.AppendASCII("extensions")
257 .AppendASCII("extensions") 257 .AppendASCII("good")
258 .AppendASCII("good") 258 .AppendASCII("Extensions")
259 .AppendASCII("Extensions") 259 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
260 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") 260 .AppendASCII("1.0.0.0");
261 .AppendASCII("1.0.0.0");
262 load_extensions_.push_back(one_extension_path.value()); 261 load_extensions_.push_back(one_extension_path.value());
263 } 262 }
264 }; 263 };
265 264
266 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) { 265 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
267 WaitForServicesToStart(1, true); 266 WaitForServicesToStart(1, true);
268 TestInjection(true, true); 267 TestInjection(true, true);
269 } 268 }
270 269
271 // ExtensionsLoadMultipleTest 270 // ExtensionsLoadMultipleTest
272 // Ensures that we can startup the browser with multiple extensions 271 // Ensures that we can startup the browser with multiple extensions
273 // via --load-extension=X1,X2,X3. 272 // via --load-extension=X1,X2,X3.
274 class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase { 273 class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
275 public: 274 public:
276 ExtensionsLoadMultipleTest() { 275 ExtensionsLoadMultipleTest() {
277 base::FilePath one_extension_path; 276 base::FilePath one_extension_path;
278 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path); 277 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
279 one_extension_path = one_extension_path 278 one_extension_path = one_extension_path.AppendASCII("extensions")
280 .AppendASCII("extensions") 279 .AppendASCII("good")
281 .AppendASCII("good") 280 .AppendASCII("Extensions")
282 .AppendASCII("Extensions") 281 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
283 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") 282 .AppendASCII("1.0.0.0");
284 .AppendASCII("1.0.0.0");
285 load_extensions_.push_back(one_extension_path.value()); 283 load_extensions_.push_back(one_extension_path.value());
286 284
287 base::FilePath second_extension_path; 285 base::FilePath second_extension_path;
288 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path); 286 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
289 second_extension_path = second_extension_path 287 second_extension_path =
290 .AppendASCII("extensions") 288 second_extension_path.AppendASCII("extensions").AppendASCII("app");
291 .AppendASCII("app");
292 load_extensions_.push_back(second_extension_path.value()); 289 load_extensions_.push_back(second_extension_path.value());
293 290
294 base::FilePath third_extension_path; 291 base::FilePath third_extension_path;
295 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path); 292 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
296 third_extension_path = third_extension_path 293 third_extension_path =
297 .AppendASCII("extensions") 294 third_extension_path.AppendASCII("extensions").AppendASCII("app1");
298 .AppendASCII("app1");
299 load_extensions_.push_back(third_extension_path.value()); 295 load_extensions_.push_back(third_extension_path.value());
300 296
301 base::FilePath fourth_extension_path; 297 base::FilePath fourth_extension_path;
302 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path); 298 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
303 fourth_extension_path = fourth_extension_path 299 fourth_extension_path =
304 .AppendASCII("extensions") 300 fourth_extension_path.AppendASCII("extensions").AppendASCII("app2");
305 .AppendASCII("app2");
306 load_extensions_.push_back(fourth_extension_path.value()); 301 load_extensions_.push_back(fourth_extension_path.value());
307 } 302 }
308 }; 303 };
309 304
310 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) { 305 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
311 WaitForServicesToStart(4, true); 306 WaitForServicesToStart(4, true);
312 TestInjection(true, true); 307 TestInjection(true, true);
313 } 308 }
309
310 class DisableExtensionsExceptBrowserTest : public ExtensionBrowserTest {
311 public:
312 DisableExtensionsExceptBrowserTest() {}
313
314 void SetUpCommandLine(base::CommandLine* command_line) override;
315
316 ExtensionRegistry* GetExtensionRegistry() {
317 return ExtensionRegistry::Get(browser()->profile());
318 }
319 };
320
321 void DisableExtensionsExceptBrowserTest::SetUpCommandLine(
322 base::CommandLine* command_line) {
323 ExtensionBrowserTest::SetUpCommandLine(command_line);
324 base::FilePath fp1(test_data_dir_.AppendASCII("app_dot_com_app/"));
325 base::FilePath fp2(test_data_dir_.AppendASCII("app/"));
326
327 command_line->AppendSwitchASCII(
328 switches::kDisableExtensionsExcept,
329 fp1.AsUTF8Unsafe() + "," + fp2.AsUTF8Unsafe());
330 }
331
332 // Tests disabling all extensions except those listed
333 // (--disable-extensions-except).
334 IN_PROC_BROWSER_TEST_F(DisableExtensionsExceptBrowserTest,
335 DisableExtensionsExceptFlag) {
336 EXPECT_FALSE(extension_service()->extensions_enabled());
337
338 // Checks that the extensions loaded with the --disable-extensions-except flag
339 // are enabled.
340 bool is_app_dot_com_extension_enabled = false;
341 bool is_app_test_extension_enabled = false;
342 for (const scoped_refptr<const extensions::Extension>& extension :
343 GetExtensionRegistry()->enabled_extensions()) {
344 EXPECT_TRUE(
Devlin 2016/08/10 19:22:31 hmm... maybe: if (extension->name() == "...") ..
catmullings 2016/08/29 20:42:27 Done.
345 extensions::Manifest::IsComponentLocation(extension->location()) ||
346 extension->name() == "App Dot Com: The App" ||
347 extension->name() == "App Test");
348
349 if (extension->name() == "App Dot Com: The App") {
350 is_app_dot_com_extension_enabled = true;
351 }
352
353 if (extension->name() == "App Test") {
354 is_app_test_extension_enabled = true;
355 }
356 }
357 EXPECT_TRUE(is_app_dot_com_extension_enabled);
358 EXPECT_TRUE(is_app_test_extension_enabled);
359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698