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
3 // found in the LICENSE file.
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_util.h"
16 #include "chrome/browser/extensions/shared_user_script_master.h"
17 #include "chrome/browser/prefs/chrome_pref_service_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/testing_profile.h"
26 #include "chrome/test/base/ui_test_utils.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/web_contents.h"
30 #include "content/public/common/content_switches.h"
31 #include "content/public/test/browser_test_utils.h"
32 #include "extensions/browser/extension_registry.h"
33 #include "extensions/browser/extension_system.h"
34 #include "extensions/common/extension.h"
35 #include "extensions/common/extension_set.h"
36 #include "extensions/common/feature_switch.h"
37 #include "net/base/filename_util.h"
39 using extensions::FeatureSwitch
;
41 // This file contains high-level startup tests for the extensions system. We've
42 // had many silly bugs where command line flags did not get propagated correctly
43 // into the services, so we didn't start correctly.
45 class ExtensionStartupTestBase
: public InProcessBrowserTest
{
47 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) {
48 num_expected_extensions_
= 3;
52 // InProcessBrowserTest
53 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
54 if (load_extensions_
.empty()) {
55 // If no |load_extensions_| were specified, allow unauthenticated
56 // extension settings to be loaded from Preferences as if they had been
57 // authenticated correctly before they were handed to the ExtensionSystem.
58 command_line
->AppendSwitchASCII(
59 switches::kForceFieldTrials
,
62 chrome_prefs::internals::kSettingsEnforcementTrialName
,
63 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement
));
64 #if defined(OFFICIAL_BUILD) && defined(OS_WIN)
65 // In Windows official builds, it is not possible to disable settings
67 unauthenticated_load_allowed_
= false;
70 base::FilePath::StringType paths
= JoinString(load_extensions_
, ',');
71 command_line
->AppendSwitchNative(switches::kLoadExtension
,
73 command_line
->AppendSwitch(switches::kDisableExtensionsFileAccessCheck
);
77 bool SetUpUserDataDirectory() override
{
78 base::FilePath profile_dir
;
79 PathService::Get(chrome::DIR_USER_DATA
, &profile_dir
);
80 profile_dir
= profile_dir
.AppendASCII(TestingProfile::kTestUserProfileDir
);
81 base::CreateDirectory(profile_dir
);
83 preferences_file_
= profile_dir
.Append(chrome::kPreferencesFilename
);
84 user_scripts_dir_
= profile_dir
.AppendASCII("User Scripts");
85 extensions_dir_
= profile_dir
.AppendASCII("Extensions");
87 if (load_extensions_
.empty()) {
88 base::FilePath src_dir
;
89 PathService::Get(chrome::DIR_TEST_DATA
, &src_dir
);
90 src_dir
= src_dir
.AppendASCII("extensions").AppendASCII("good");
92 base::CopyFile(src_dir
.Append(chrome::kPreferencesFilename
),
94 base::CopyDirectory(src_dir
.AppendASCII("Extensions"),
95 profile_dir
, true); // recursive
100 void SetUpInProcessBrowserTestFixture() override
{
101 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
103 // Bots are on a domain, turn off the domain check for settings hardening in
104 // order to be able to test all SettingsEnforcement groups.
105 chrome_prefs::DisableDomainCheckForTesting();
108 void TearDown() override
{
109 EXPECT_TRUE(base::DeleteFile(preferences_file_
, false));
111 // TODO(phajdan.jr): Check return values of the functions below, carefully.
112 base::DeleteFile(user_scripts_dir_
, true);
113 base::DeleteFile(extensions_dir_
, true);
115 InProcessBrowserTest::TearDown();
118 void WaitForServicesToStart(int num_expected_extensions
,
119 bool expect_extensions_enabled
) {
120 extensions::ExtensionRegistry
* registry
=
121 extensions::ExtensionRegistry::Get(browser()->profile());
123 // Count the number of non-component extensions.
124 int found_extensions
= 0;
125 for (const scoped_refptr
<const extensions::Extension
>& extension
:
126 registry
->enabled_extensions()) {
127 if (extension
->location() != extensions::Manifest::COMPONENT
)
131 if (!unauthenticated_load_allowed_
)
132 num_expected_extensions
= 0;
134 ASSERT_EQ(static_cast<uint32
>(num_expected_extensions
),
135 static_cast<uint32
>(found_extensions
));
137 ExtensionService
* service
= extensions::ExtensionSystem::Get(
138 browser()->profile())->extension_service();
139 ASSERT_EQ(expect_extensions_enabled
, service
->extensions_enabled());
141 content::WindowedNotificationObserver
user_scripts_observer(
142 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED
,
143 content::NotificationService::AllSources());
144 extensions::SharedUserScriptMaster
* master
=
145 extensions::ExtensionSystem::Get(browser()->profile())->
146 shared_user_script_master();
147 if (!master
->scripts_ready())
148 user_scripts_observer
.Wait();
149 ASSERT_TRUE(master
->scripts_ready());
152 void TestInjection(bool expect_css
, bool expect_script
) {
153 if (!unauthenticated_load_allowed_
) {
155 expect_script
= false;
158 // Load a page affected by the content script and test to see the effect.
159 base::FilePath test_file
;
160 PathService::Get(chrome::DIR_TEST_DATA
, &test_file
);
161 test_file
= test_file
.AppendASCII("extensions")
162 .AppendASCII("test_file.html");
164 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file
));
167 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
168 browser()->tab_strip_model()->GetActiveWebContents(),
169 "window.domAutomationController.send("
170 " document.defaultView.getComputedStyle(document.body, null)."
171 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
173 EXPECT_EQ(expect_css
, result
);
176 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
177 browser()->tab_strip_model()->GetActiveWebContents(),
178 "window.domAutomationController.send(document.title == 'Modified')",
180 EXPECT_EQ(expect_script
, result
);
183 base::FilePath preferences_file_
;
184 base::FilePath extensions_dir_
;
185 base::FilePath user_scripts_dir_
;
186 // True unless unauthenticated extension settings are not allowed to be
187 // loaded in this configuration.
188 bool unauthenticated_load_allowed_
;
189 // Extensions to load from the command line.
190 std::vector
<base::FilePath::StringType
> load_extensions_
;
192 int num_expected_extensions_
;
196 // ExtensionsStartupTest
197 // Ensures that we can startup the browser with --enable-extensions and some
198 // extensions installed and see them run and do basic things.
199 typedef ExtensionStartupTestBase ExtensionsStartupTest
;
201 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, Test
) {
202 WaitForServicesToStart(num_expected_extensions_
, true);
203 TestInjection(true, true);
206 // Sometimes times out on Mac. http://crbug.com/48151
207 #if defined(OS_MACOSX)
208 #define MAYBE_NoFileAccess DISABLED_NoFileAccess
210 #define MAYBE_NoFileAccess NoFileAccess
212 // Tests that disallowing file access on an extension prevents it from injecting
213 // script into a page with a file URL.
214 IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest
, MAYBE_NoFileAccess
) {
215 WaitForServicesToStart(num_expected_extensions_
, true);
217 // Keep a separate list of extensions for which to disable file access, since
218 // doing so reloads them.
219 std::vector
<const extensions::Extension
*> extension_list
;
221 extensions::ExtensionRegistry
* registry
=
222 extensions::ExtensionRegistry::Get(browser()->profile());
223 for (extensions::ExtensionSet::const_iterator it
=
224 registry
->enabled_extensions().begin();
225 it
!= registry
->enabled_extensions().end(); ++it
) {
226 if ((*it
)->location() == extensions::Manifest::COMPONENT
)
228 if (extensions::util::AllowFileAccess((*it
)->id(), browser()->profile()))
229 extension_list
.push_back(it
->get());
232 for (size_t i
= 0; i
< extension_list
.size(); ++i
) {
233 content::WindowedNotificationObserver
user_scripts_observer(
234 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED
,
235 content::NotificationService::AllSources());
236 extensions::util::SetAllowFileAccess(
237 extension_list
[i
]->id(), browser()->profile(), false);
238 user_scripts_observer
.Wait();
241 TestInjection(false, false);
244 // ExtensionsLoadTest
245 // Ensures that we can startup the browser with --load-extension and see them
247 class ExtensionsLoadTest
: public ExtensionStartupTestBase
{
249 ExtensionsLoadTest() {
250 base::FilePath one_extension_path
;
251 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
252 one_extension_path
= one_extension_path
253 .AppendASCII("extensions")
255 .AppendASCII("Extensions")
256 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
257 .AppendASCII("1.0.0.0");
258 load_extensions_
.push_back(one_extension_path
.value());
262 // Fails inconsistently on Linux x64. http://crbug.com/80961
263 // TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
264 // and act accordingly.
265 IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest
, Test
) {
266 WaitForServicesToStart(1, true);
267 TestInjection(true, true);
270 // ExtensionsLoadMultipleTest
271 // Ensures that we can startup the browser with multiple extensions
272 // via --load-extension=X1,X2,X3.
273 class ExtensionsLoadMultipleTest
: public ExtensionStartupTestBase
{
275 ExtensionsLoadMultipleTest() {
276 base::FilePath one_extension_path
;
277 PathService::Get(chrome::DIR_TEST_DATA
, &one_extension_path
);
278 one_extension_path
= one_extension_path
279 .AppendASCII("extensions")
281 .AppendASCII("Extensions")
282 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
283 .AppendASCII("1.0.0.0");
284 load_extensions_
.push_back(one_extension_path
.value());
286 base::FilePath second_extension_path
;
287 PathService::Get(chrome::DIR_TEST_DATA
, &second_extension_path
);
288 second_extension_path
= second_extension_path
289 .AppendASCII("extensions")
291 load_extensions_
.push_back(second_extension_path
.value());
293 base::FilePath third_extension_path
;
294 PathService::Get(chrome::DIR_TEST_DATA
, &third_extension_path
);
295 third_extension_path
= third_extension_path
296 .AppendASCII("extensions")
297 .AppendASCII("app1");
298 load_extensions_
.push_back(third_extension_path
.value());
300 base::FilePath fourth_extension_path
;
301 PathService::Get(chrome::DIR_TEST_DATA
, &fourth_extension_path
);
302 fourth_extension_path
= fourth_extension_path
303 .AppendASCII("extensions")
304 .AppendASCII("app2");
305 load_extensions_
.push_back(fourth_extension_path
.value());
309 IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest
, Test
) {
310 WaitForServicesToStart(4, true);
311 TestInjection(true, true);