blob: 462f2ba9d06049f078fcec1192e2ce90b9632be5 [file] [log] [blame]
[email protected]4d2451652012-02-14 23:54:261// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]038d52e12009-10-14 16:53:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
avia2f4804a2015-12-24 23:11:135#include <stddef.h>
6#include <stdint.h>
7
[email protected]f0488f2f2009-07-01 05:25:228#include <vector>
9
brettwf1958912015-10-07 19:43:1210#include "base/base_switches.h"
[email protected]f0488f2f2009-07-01 05:25:2211#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0413#include "base/files/file_util.h"
[email protected]f0488f2f2009-07-01 05:25:2214#include "base/path_service.h"
[email protected]00e7bef2013-06-10 20:35:1715#include "base/strings/string_util.h"
[email protected]60f7d7a2014-04-03 07:15:1116#include "base/strings/stringprintf.h"
avia2f4804a2015-12-24 23:11:1317#include "build/build_config.h"
[email protected]9ea0cd32013-07-12 01:50:3618#include "chrome/browser/chrome_notification_types.h"
[email protected]eaa7dd182010-12-14 11:09:0019#include "chrome/browser/extensions/extension_service.h"
[email protected]a7ff4b72013-10-17 20:56:0220#include "chrome/browser/extensions/extension_util.h"
[email protected]15ad2ee2014-08-15 19:15:2621#include "chrome/browser/extensions/shared_user_script_master.h"
[email protected]60f7d7a2014-04-03 07:15:1122#include "chrome/browser/prefs/chrome_pref_service_factory.h"
[email protected]8ecad5e2010-12-02 21:18:3323#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1024#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4825#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]e96a0602014-02-15 08:27:4226#include "chrome/common/chrome_constants.h"
[email protected]f0488f2f2009-07-01 05:25:2227#include "chrome/common/chrome_paths.h"
28#include "chrome/common/chrome_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3229#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2530#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3231#include "chrome/test/base/ui_test_utils.h"
[email protected]6c2381d2011-10-19 02:52:5332#include "content/public/browser/notification_details.h"
[email protected]ad50def52011-10-19 23:17:0733#include "content/public/browser/notification_service.h"
[email protected]4ca15302012-01-03 05:53:2034#include "content/public/browser/web_contents.h"
[email protected]60f7d7a2014-04-03 07:15:1135#include "content/public/common/content_switches.h"
[email protected]7d478cb2012-07-24 17:19:4236#include "content/public/test/browser_test_utils.h"
[email protected]1d5cf4142014-01-24 18:25:2237#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2438#include "extensions/browser/extension_system.h"
[email protected]289c44b2013-12-17 03:26:5739#include "extensions/common/extension.h"
40#include "extensions/common/extension_set.h"
[email protected]5ef835a2013-11-08 20:42:5741#include "extensions/common/feature_switch.h"
[email protected]d96cf752014-04-09 04:05:2842#include "net/base/filename_util.h"
[email protected]f0488f2f2009-07-01 05:25:2243
[email protected]00b5d0a52012-10-30 13:13:5344using extensions::FeatureSwitch;
45
[email protected]17c4f3c2009-07-04 16:36:2546// 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
48// into the services, so we didn't start correctly.
[email protected]f0488f2f2009-07-01 05:25:2249
[email protected]3e59bac2010-04-08 16:16:5550class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2251 public:
[email protected]60f7d7a2014-04-03 07:15:1152 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) {
[email protected]fe13bf62010-08-26 14:33:1953 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2254 }
55
56 protected:
57 // InProcessBrowserTest
avi3ef9ec9e2014-12-22 22:50:1758 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]60f7d7a2014-04-03 07:15:1159 if (load_extensions_.empty()) {
60 // If no |load_extensions_| were specified, allow unauthenticated
61 // extension settings to be loaded from Preferences as if they had been
62 // authenticated correctly before they were handed to the ExtensionSystem.
63 command_line->AppendSwitchASCII(
64 switches::kForceFieldTrials,
65 base::StringPrintf(
66 "%s/%s/",
67 chrome_prefs::internals::kSettingsEnforcementTrialName,
68 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement));
[email protected]337695e2014-04-17 03:55:4969#if defined(OFFICIAL_BUILD) && defined(OS_WIN)
70 // In Windows official builds, it is not possible to disable settings
[email protected]60f7d7a2014-04-03 07:15:1171 // authentication.
[email protected]55274dd2014-06-23 21:51:1872 unauthenticated_load_allowed_ = false;
[email protected]60f7d7a2014-04-03 07:15:1173#endif
74 } else {
brettwd94a22142015-07-15 05:19:2675 base::FilePath::StringType paths =
76 base::JoinString(load_extensions_,
77 base::FilePath::StringType(1, ','));
[email protected]10f6b142012-04-14 19:22:4378 command_line->AppendSwitchNative(switches::kLoadExtension,
79 paths);
80 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
81 }
82 }
83
dchengae36a4a2014-10-21 12:36:3684 bool SetUpUserDataDirectory() override {
[email protected]650b2d52013-02-10 03:41:4585 base::FilePath profile_dir;
[email protected]f0488f2f2009-07-01 05:25:2286 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
[email protected]89dbb1772012-07-17 13:47:2587 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
[email protected]426d1c92013-12-03 20:08:5488 base::CreateDirectory(profile_dir);
[email protected]f0488f2f2009-07-01 05:25:2289
[email protected]e96a0602014-02-15 08:27:4290 preferences_file_ = profile_dir.Append(chrome::kPreferencesFilename);
[email protected]f0488f2f2009-07-01 05:25:2291 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
92 extensions_dir_ = profile_dir.AppendASCII("Extensions");
93
[email protected]60f7d7a2014-04-03 07:15:1194 if (load_extensions_.empty()) {
[email protected]650b2d52013-02-10 03:41:4595 base::FilePath src_dir;
[email protected]10f6b142012-04-14 19:22:4396 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
97 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2298
[email protected]e96a0602014-02-15 08:27:4299 base::CopyFile(src_dir.Append(chrome::kPreferencesFilename),
100 preferences_file_);
[email protected]f0ff2ad2013-07-09 17:42:26101 base::CopyDirectory(src_dir.AppendASCII("Extensions"),
102 profile_dir, true); // recursive
[email protected]f0488f2f2009-07-01 05:25:22103 }
[email protected]10f6b142012-04-14 19:22:43104 return true;
[email protected]f0488f2f2009-07-01 05:25:22105 }
106
gabbcdefd0cc2015-02-17 18:12:40107 void SetUpInProcessBrowserTestFixture() override {
108 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
109
110 // Bots are on a domain, turn off the domain check for settings hardening in
111 // order to be able to test all SettingsEnforcement groups.
112 chrome_prefs::DisableDomainCheckForTesting();
113 }
114
dcheng72191812014-10-28 20:49:56115 void TearDown() override {
[email protected]dd3aa792013-07-16 19:10:23116 EXPECT_TRUE(base::DeleteFile(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:12117
118 // TODO(phajdan.jr): Check return values of the functions below, carefully.
[email protected]dd3aa792013-07-16 19:10:23119 base::DeleteFile(user_scripts_dir_, true);
120 base::DeleteFile(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:08121
122 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:22123 }
124
[email protected]919ddc82009-07-15 04:30:12125 void WaitForServicesToStart(int num_expected_extensions,
126 bool expect_extensions_enabled) {
reillyga3acbc12014-11-11 23:17:12127 extensions::ExtensionRegistry* registry =
128 extensions::ExtensionRegistry::Get(browser()->profile());
[email protected]919ddc82009-07-15 04:30:12129
[email protected]fe13bf62010-08-26 14:33:19130 // Count the number of non-component extensions.
131 int found_extensions = 0;
reillyga3acbc12014-11-11 23:17:12132 for (const scoped_refptr<const extensions::Extension>& extension :
133 registry->enabled_extensions()) {
134 if (extension->location() != extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19135 found_extensions++;
[email protected]1d5cf4142014-01-24 18:25:22136 }
[email protected]fe13bf62010-08-26 14:33:19137
[email protected]60f7d7a2014-04-03 07:15:11138 if (!unauthenticated_load_allowed_)
139 num_expected_extensions = 0;
140
avia2f4804a2015-12-24 23:11:13141 ASSERT_EQ(static_cast<uint32_t>(num_expected_extensions),
142 static_cast<uint32_t>(found_extensions));
reillyga3acbc12014-11-11 23:17:12143
144 ExtensionService* service = extensions::ExtensionSystem::Get(
145 browser()->profile())->extension_service();
[email protected]919ddc82009-07-15 04:30:12146 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
147
[email protected]a7fe9112012-07-20 02:34:45148 content::WindowedNotificationObserver user_scripts_observer(
[email protected]adf5a102014-07-31 12:44:06149 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]ad50def52011-10-19 23:17:07150 content::NotificationService::AllSources());
[email protected]15ad2ee2014-08-15 19:15:26151 extensions::SharedUserScriptMaster* master =
[email protected]5d272a42012-10-29 13:28:22152 extensions::ExtensionSystem::Get(browser()->profile())->
[email protected]15ad2ee2014-08-15 19:15:26153 shared_user_script_master();
154 if (!master->scripts_ready())
[email protected]120abf132011-09-27 21:38:06155 user_scripts_observer.Wait();
[email protected]15ad2ee2014-08-15 19:15:26156 ASSERT_TRUE(master->scripts_ready());
[email protected]919ddc82009-07-15 04:30:12157 }
158
159 void TestInjection(bool expect_css, bool expect_script) {
[email protected]60f7d7a2014-04-03 07:15:11160 if (!unauthenticated_load_allowed_) {
161 expect_css = false;
162 expect_script = false;
163 }
164
[email protected]919ddc82009-07-15 04:30:12165 // Load a page affected by the content script and test to see the effect.
[email protected]650b2d52013-02-10 03:41:45166 base::FilePath test_file;
[email protected]62771442009-11-22 02:25:04167 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
168 test_file = test_file.AppendASCII("extensions")
169 .AppendASCII("test_file.html");
170
171 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12172
173 bool result = false;
[email protected]b6987e02013-01-04 18:30:43174 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48175 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13176 "window.domAutomationController.send("
[email protected]b6987e02013-01-04 18:30:43177 " document.defaultView.getComputedStyle(document.body, null)."
178 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05179 &result));
[email protected]919ddc82009-07-15 04:30:12180 EXPECT_EQ(expect_css, result);
181
182 result = false;
[email protected]b6987e02013-01-04 18:30:43183 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48184 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13185 "window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05186 &result));
[email protected]919ddc82009-07-15 04:30:12187 EXPECT_EQ(expect_script, result);
188 }
189
[email protected]650b2d52013-02-10 03:41:45190 base::FilePath preferences_file_;
191 base::FilePath extensions_dir_;
192 base::FilePath user_scripts_dir_;
[email protected]60f7d7a2014-04-03 07:15:11193 // True unless unauthenticated extension settings are not allowed to be
194 // loaded in this configuration.
195 bool unauthenticated_load_allowed_;
[email protected]bfc93562011-12-14 19:40:04196 // Extensions to load from the command line.
[email protected]650b2d52013-02-10 03:41:45197 std::vector<base::FilePath::StringType> load_extensions_;
[email protected]e50013c32010-08-18 21:05:24198
199 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22200};
201
202
203// ExtensionsStartupTest
204// Ensures that we can startup the browser with --enable-extensions and some
205// extensions installed and see them run and do basic things.
[email protected]60f7d7a2014-04-03 07:15:11206typedef ExtensionStartupTestBase ExtensionsStartupTest;
[email protected]f0488f2f2009-07-01 05:25:22207
Nico Weber9ce471d42015-04-13 20:52:39208// Broken in official builds, http://crbug.com/474659
209IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, DISABLED_Test) {
[email protected]e50013c32010-08-18 21:05:24210 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12211 TestInjection(true, true);
212}
[email protected]919ddc82009-07-15 04:30:12213
Nico Weber9ce471d42015-04-13 20:52:39214// Broken in official builds, http://crbug.com/474659
[email protected]b2f665f52010-07-02 01:32:09215// Sometimes times out on Mac. http://crbug.com/48151
[email protected]05c82182010-06-24 17:49:08216// Tests that disallowing file access on an extension prevents it from injecting
217// script into a page with a file URL.
Nico Weber9ce471d42015-04-13 20:52:39218IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, DISABLED_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24219 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08220
[email protected]84df8332011-12-06 18:22:46221 // Keep a separate list of extensions for which to disable file access, since
222 // doing so reloads them.
[email protected]1c321ee2012-05-21 03:02:34223 std::vector<const extensions::Extension*> extension_list;
[email protected]84df8332011-12-06 18:22:46224
[email protected]1d5cf4142014-01-24 18:25:22225 extensions::ExtensionRegistry* registry =
226 extensions::ExtensionRegistry::Get(browser()->profile());
[email protected]289c44b2013-12-17 03:26:57227 for (extensions::ExtensionSet::const_iterator it =
[email protected]1d5cf4142014-01-24 18:25:22228 registry->enabled_extensions().begin();
229 it != registry->enabled_extensions().end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:40230 if ((*it)->location() == extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19231 continue;
[email protected]1d5cf4142014-01-24 18:25:22232 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile()))
[email protected]cadac622013-06-11 16:46:36233 extension_list.push_back(it->get());
[email protected]84df8332011-12-06 18:22:46234 }
235
236 for (size_t i = 0; i < extension_list.size(); ++i) {
[email protected]a7fe9112012-07-20 02:34:45237 content::WindowedNotificationObserver user_scripts_observer(
[email protected]adf5a102014-07-31 12:44:06238 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]84df8332011-12-06 18:22:46239 content::NotificationService::AllSources());
[email protected]1d5cf4142014-01-24 18:25:22240 extensions::util::SetAllowFileAccess(
241 extension_list[i]->id(), browser()->profile(), false);
[email protected]84df8332011-12-06 18:22:46242 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08243 }
244
245 TestInjection(false, false);
246}
247
[email protected]919ddc82009-07-15 04:30:12248// ExtensionsLoadTest
249// Ensures that we can startup the browser with --load-extension and see them
250// run.
[email protected]919ddc82009-07-15 04:30:12251class ExtensionsLoadTest : public ExtensionStartupTestBase {
252 public:
253 ExtensionsLoadTest() {
[email protected]650b2d52013-02-10 03:41:45254 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04255 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
256 one_extension_path = one_extension_path
[email protected]919ddc82009-07-15 04:30:12257 .AppendASCII("extensions")
258 .AppendASCII("good")
259 .AppendASCII("Extensions")
260 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
261 .AppendASCII("1.0.0.0");
[email protected]bfc93562011-12-14 19:40:04262 load_extensions_.push_back(one_extension_path.value());
[email protected]f0488f2f2009-07-01 05:25:22263 }
[email protected]919ddc82009-07-15 04:30:12264};
[email protected]f0488f2f2009-07-01 05:25:22265
[email protected]dc128022012-02-17 23:14:58266IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
[email protected]d728e002010-12-08 04:46:23267 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12268 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22269}
[email protected]bfc93562011-12-14 19:40:04270
271// ExtensionsLoadMultipleTest
272// Ensures that we can startup the browser with multiple extensions
273// via --load-extension=X1,X2,X3.
274class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
275 public:
276 ExtensionsLoadMultipleTest() {
[email protected]650b2d52013-02-10 03:41:45277 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04278 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
279 one_extension_path = one_extension_path
280 .AppendASCII("extensions")
281 .AppendASCII("good")
282 .AppendASCII("Extensions")
283 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
284 .AppendASCII("1.0.0.0");
285 load_extensions_.push_back(one_extension_path.value());
286
[email protected]650b2d52013-02-10 03:41:45287 base::FilePath second_extension_path;
[email protected]bfc93562011-12-14 19:40:04288 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
289 second_extension_path = second_extension_path
290 .AppendASCII("extensions")
291 .AppendASCII("app");
292 load_extensions_.push_back(second_extension_path.value());
293
[email protected]650b2d52013-02-10 03:41:45294 base::FilePath third_extension_path;
[email protected]bfc93562011-12-14 19:40:04295 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
296 third_extension_path = third_extension_path
297 .AppendASCII("extensions")
298 .AppendASCII("app1");
299 load_extensions_.push_back(third_extension_path.value());
300
[email protected]650b2d52013-02-10 03:41:45301 base::FilePath fourth_extension_path;
[email protected]bfc93562011-12-14 19:40:04302 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
303 fourth_extension_path = fourth_extension_path
304 .AppendASCII("extensions")
305 .AppendASCII("app2");
306 load_extensions_.push_back(fourth_extension_path.value());
307 }
308};
309
310IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
311 WaitForServicesToStart(4, true);
312 TestInjection(true, true);
313}