blob: f8040d99edb242a6d0db86d360c88ecaf7c3b889 [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
[email protected]f0488f2f2009-07-01 05:25:225#include <vector>
6
7#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:528#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:049#include "base/files/file_util.h"
[email protected]f0488f2f2009-07-01 05:25:2210#include "base/path_service.h"
[email protected]00e7bef2013-06-10 20:35:1711#include "base/strings/string_util.h"
[email protected]60f7d7a2014-04-03 07:15:1112#include "base/strings/stringprintf.h"
[email protected]9ea0cd32013-07-12 01:50:3613#include "chrome/browser/chrome_notification_types.h"
[email protected]eaa7dd182010-12-14 11:09:0014#include "chrome/browser/extensions/extension_service.h"
[email protected]a7ff4b72013-10-17 20:56:0215#include "chrome/browser/extensions/extension_util.h"
[email protected]15ad2ee2014-08-15 19:15:2616#include "chrome/browser/extensions/shared_user_script_master.h"
[email protected]60f7d7a2014-04-03 07:15:1117#include "chrome/browser/prefs/chrome_pref_service_factory.h"
[email protected]8ecad5e2010-12-02 21:18:3318#include "chrome/browser/profiles/profile.h"
[email protected]7b5dc002010-11-16 23:08:1019#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4820#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]e96a0602014-02-15 08:27:4221#include "chrome/common/chrome_constants.h"
[email protected]f0488f2f2009-07-01 05:25:2222#include "chrome/common/chrome_paths.h"
23#include "chrome/common/chrome_switches.h"
[email protected]af44e7fb2011-07-29 18:32:3224#include "chrome/test/base/in_process_browser_test.h"
[email protected]89dbb1772012-07-17 13:47:2525#include "chrome/test/base/testing_profile.h"
[email protected]af44e7fb2011-07-29 18:32:3226#include "chrome/test/base/ui_test_utils.h"
[email protected]6c2381d2011-10-19 02:52:5327#include "content/public/browser/notification_details.h"
[email protected]ad50def52011-10-19 23:17:0728#include "content/public/browser/notification_service.h"
[email protected]4ca15302012-01-03 05:53:2029#include "content/public/browser/web_contents.h"
[email protected]60f7d7a2014-04-03 07:15:1130#include "content/public/common/content_switches.h"
[email protected]7d478cb2012-07-24 17:19:4231#include "content/public/test/browser_test_utils.h"
[email protected]1d5cf4142014-01-24 18:25:2232#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2433#include "extensions/browser/extension_system.h"
[email protected]289c44b2013-12-17 03:26:5734#include "extensions/common/extension.h"
35#include "extensions/common/extension_set.h"
[email protected]5ef835a2013-11-08 20:42:5736#include "extensions/common/feature_switch.h"
[email protected]d96cf752014-04-09 04:05:2837#include "net/base/filename_util.h"
[email protected]f0488f2f2009-07-01 05:25:2238
[email protected]00b5d0a52012-10-30 13:13:5339using extensions::FeatureSwitch;
40
[email protected]17c4f3c2009-07-04 16:36:2541// 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.
[email protected]f0488f2f2009-07-01 05:25:2244
[email protected]3e59bac2010-04-08 16:16:5545class ExtensionStartupTestBase : public InProcessBrowserTest {
[email protected]f0488f2f2009-07-01 05:25:2246 public:
[email protected]60f7d7a2014-04-03 07:15:1147 ExtensionStartupTestBase() : unauthenticated_load_allowed_(true) {
[email protected]fe13bf62010-08-26 14:33:1948 num_expected_extensions_ = 3;
[email protected]f0488f2f2009-07-01 05:25:2249 }
50
51 protected:
52 // InProcessBrowserTest
avi3ef9ec9e2014-12-22 22:50:1753 void SetUpCommandLine(base::CommandLine* command_line) override {
[email protected]60f7d7a2014-04-03 07:15:1154 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,
60 base::StringPrintf(
61 "%s/%s/",
62 chrome_prefs::internals::kSettingsEnforcementTrialName,
63 chrome_prefs::internals::kSettingsEnforcementGroupNoEnforcement));
[email protected]337695e2014-04-17 03:55:4964#if defined(OFFICIAL_BUILD) && defined(OS_WIN)
65 // In Windows official builds, it is not possible to disable settings
[email protected]60f7d7a2014-04-03 07:15:1166 // authentication.
[email protected]55274dd2014-06-23 21:51:1867 unauthenticated_load_allowed_ = false;
[email protected]60f7d7a2014-04-03 07:15:1168#endif
69 } else {
[email protected]dd6d36672013-05-17 22:50:0270 base::FilePath::StringType paths = JoinString(load_extensions_, ',');
[email protected]10f6b142012-04-14 19:22:4371 command_line->AppendSwitchNative(switches::kLoadExtension,
72 paths);
73 command_line->AppendSwitch(switches::kDisableExtensionsFileAccessCheck);
74 }
75 }
76
dchengae36a4a2014-10-21 12:36:3677 bool SetUpUserDataDirectory() override {
[email protected]650b2d52013-02-10 03:41:4578 base::FilePath profile_dir;
[email protected]f0488f2f2009-07-01 05:25:2279 PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
[email protected]89dbb1772012-07-17 13:47:2580 profile_dir = profile_dir.AppendASCII(TestingProfile::kTestUserProfileDir);
[email protected]426d1c92013-12-03 20:08:5481 base::CreateDirectory(profile_dir);
[email protected]f0488f2f2009-07-01 05:25:2282
[email protected]e96a0602014-02-15 08:27:4283 preferences_file_ = profile_dir.Append(chrome::kPreferencesFilename);
[email protected]f0488f2f2009-07-01 05:25:2284 user_scripts_dir_ = profile_dir.AppendASCII("User Scripts");
85 extensions_dir_ = profile_dir.AppendASCII("Extensions");
86
[email protected]60f7d7a2014-04-03 07:15:1187 if (load_extensions_.empty()) {
[email protected]650b2d52013-02-10 03:41:4588 base::FilePath src_dir;
[email protected]10f6b142012-04-14 19:22:4389 PathService::Get(chrome::DIR_TEST_DATA, &src_dir);
90 src_dir = src_dir.AppendASCII("extensions").AppendASCII("good");
[email protected]f0488f2f2009-07-01 05:25:2291
[email protected]e96a0602014-02-15 08:27:4292 base::CopyFile(src_dir.Append(chrome::kPreferencesFilename),
93 preferences_file_);
[email protected]f0ff2ad2013-07-09 17:42:2694 base::CopyDirectory(src_dir.AppendASCII("Extensions"),
95 profile_dir, true); // recursive
[email protected]f0488f2f2009-07-01 05:25:2296 }
[email protected]10f6b142012-04-14 19:22:4397 return true;
[email protected]f0488f2f2009-07-01 05:25:2298 }
99
dcheng72191812014-10-28 20:49:56100 void TearDown() override {
[email protected]dd3aa792013-07-16 19:10:23101 EXPECT_TRUE(base::DeleteFile(preferences_file_, false));
[email protected]020f49e2010-04-08 19:51:12102
103 // TODO(phajdan.jr): Check return values of the functions below, carefully.
[email protected]dd3aa792013-07-16 19:10:23104 base::DeleteFile(user_scripts_dir_, true);
105 base::DeleteFile(extensions_dir_, true);
[email protected]36fb2c7c2011-04-04 15:49:08106
107 InProcessBrowserTest::TearDown();
[email protected]f0488f2f2009-07-01 05:25:22108 }
109
[email protected]919ddc82009-07-15 04:30:12110 void WaitForServicesToStart(int num_expected_extensions,
111 bool expect_extensions_enabled) {
reillyga3acbc12014-11-11 23:17:12112 extensions::ExtensionRegistry* registry =
113 extensions::ExtensionRegistry::Get(browser()->profile());
[email protected]919ddc82009-07-15 04:30:12114
[email protected]fe13bf62010-08-26 14:33:19115 // Count the number of non-component extensions.
116 int found_extensions = 0;
reillyga3acbc12014-11-11 23:17:12117 for (const scoped_refptr<const extensions::Extension>& extension :
118 registry->enabled_extensions()) {
119 if (extension->location() != extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19120 found_extensions++;
[email protected]1d5cf4142014-01-24 18:25:22121 }
[email protected]fe13bf62010-08-26 14:33:19122
[email protected]60f7d7a2014-04-03 07:15:11123 if (!unauthenticated_load_allowed_)
124 num_expected_extensions = 0;
125
[email protected]919ddc82009-07-15 04:30:12126 ASSERT_EQ(static_cast<uint32>(num_expected_extensions),
[email protected]fe13bf62010-08-26 14:33:19127 static_cast<uint32>(found_extensions));
reillyga3acbc12014-11-11 23:17:12128
129 ExtensionService* service = extensions::ExtensionSystem::Get(
130 browser()->profile())->extension_service();
[email protected]919ddc82009-07-15 04:30:12131 ASSERT_EQ(expect_extensions_enabled, service->extensions_enabled());
132
[email protected]a7fe9112012-07-20 02:34:45133 content::WindowedNotificationObserver user_scripts_observer(
[email protected]adf5a102014-07-31 12:44:06134 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]ad50def52011-10-19 23:17:07135 content::NotificationService::AllSources());
[email protected]15ad2ee2014-08-15 19:15:26136 extensions::SharedUserScriptMaster* master =
[email protected]5d272a42012-10-29 13:28:22137 extensions::ExtensionSystem::Get(browser()->profile())->
[email protected]15ad2ee2014-08-15 19:15:26138 shared_user_script_master();
139 if (!master->scripts_ready())
[email protected]120abf132011-09-27 21:38:06140 user_scripts_observer.Wait();
[email protected]15ad2ee2014-08-15 19:15:26141 ASSERT_TRUE(master->scripts_ready());
[email protected]919ddc82009-07-15 04:30:12142 }
143
144 void TestInjection(bool expect_css, bool expect_script) {
[email protected]60f7d7a2014-04-03 07:15:11145 if (!unauthenticated_load_allowed_) {
146 expect_css = false;
147 expect_script = false;
148 }
149
[email protected]919ddc82009-07-15 04:30:12150 // Load a page affected by the content script and test to see the effect.
[email protected]650b2d52013-02-10 03:41:45151 base::FilePath test_file;
[email protected]62771442009-11-22 02:25:04152 PathService::Get(chrome::DIR_TEST_DATA, &test_file);
153 test_file = test_file.AppendASCII("extensions")
154 .AppendASCII("test_file.html");
155
156 ui_test_utils::NavigateToURL(browser(), net::FilePathToFileURL(test_file));
[email protected]919ddc82009-07-15 04:30:12157
158 bool result = false;
[email protected]b6987e02013-01-04 18:30:43159 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48160 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13161 "window.domAutomationController.send("
[email protected]b6987e02013-01-04 18:30:43162 " document.defaultView.getComputedStyle(document.body, null)."
163 " getPropertyValue('background-color') == 'rgb(245, 245, 220)')",
[email protected]9fabbf72010-09-30 21:50:05164 &result));
[email protected]919ddc82009-07-15 04:30:12165 EXPECT_EQ(expect_css, result);
166
167 result = false;
[email protected]b6987e02013-01-04 18:30:43168 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
[email protected]47ae23372013-01-29 01:50:48169 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:13170 "window.domAutomationController.send(document.title == 'Modified')",
[email protected]9fabbf72010-09-30 21:50:05171 &result));
[email protected]919ddc82009-07-15 04:30:12172 EXPECT_EQ(expect_script, result);
173 }
174
[email protected]650b2d52013-02-10 03:41:45175 base::FilePath preferences_file_;
176 base::FilePath extensions_dir_;
177 base::FilePath user_scripts_dir_;
[email protected]60f7d7a2014-04-03 07:15:11178 // True unless unauthenticated extension settings are not allowed to be
179 // loaded in this configuration.
180 bool unauthenticated_load_allowed_;
[email protected]bfc93562011-12-14 19:40:04181 // Extensions to load from the command line.
[email protected]650b2d52013-02-10 03:41:45182 std::vector<base::FilePath::StringType> load_extensions_;
[email protected]e50013c32010-08-18 21:05:24183
184 int num_expected_extensions_;
[email protected]f0488f2f2009-07-01 05:25:22185};
186
187
188// ExtensionsStartupTest
189// Ensures that we can startup the browser with --enable-extensions and some
190// extensions installed and see them run and do basic things.
[email protected]60f7d7a2014-04-03 07:15:11191typedef ExtensionStartupTestBase ExtensionsStartupTest;
[email protected]f0488f2f2009-07-01 05:25:22192
[email protected]c5b2c6112010-06-11 00:02:14193IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, Test) {
[email protected]e50013c32010-08-18 21:05:24194 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]919ddc82009-07-15 04:30:12195 TestInjection(true, true);
196}
[email protected]919ddc82009-07-15 04:30:12197
[email protected]b2f665f52010-07-02 01:32:09198// Sometimes times out on Mac. http://crbug.com/48151
[email protected]11170a772013-12-13 11:38:32199#if defined(OS_MACOSX)
[email protected]b2f665f52010-07-02 01:32:09200#define MAYBE_NoFileAccess DISABLED_NoFileAccess
201#else
202#define MAYBE_NoFileAccess NoFileAccess
203#endif
[email protected]05c82182010-06-24 17:49:08204// Tests that disallowing file access on an extension prevents it from injecting
205// script into a page with a file URL.
[email protected]b2f665f52010-07-02 01:32:09206IN_PROC_BROWSER_TEST_F(ExtensionsStartupTest, MAYBE_NoFileAccess) {
[email protected]e50013c32010-08-18 21:05:24207 WaitForServicesToStart(num_expected_extensions_, true);
[email protected]05c82182010-06-24 17:49:08208
[email protected]84df8332011-12-06 18:22:46209 // Keep a separate list of extensions for which to disable file access, since
210 // doing so reloads them.
[email protected]1c321ee2012-05-21 03:02:34211 std::vector<const extensions::Extension*> extension_list;
[email protected]84df8332011-12-06 18:22:46212
[email protected]1d5cf4142014-01-24 18:25:22213 extensions::ExtensionRegistry* registry =
214 extensions::ExtensionRegistry::Get(browser()->profile());
[email protected]289c44b2013-12-17 03:26:57215 for (extensions::ExtensionSet::const_iterator it =
[email protected]1d5cf4142014-01-24 18:25:22216 registry->enabled_extensions().begin();
217 it != registry->enabled_extensions().end(); ++it) {
[email protected]1d5e58b2013-01-31 08:41:40218 if ((*it)->location() == extensions::Manifest::COMPONENT)
[email protected]fe13bf62010-08-26 14:33:19219 continue;
[email protected]1d5cf4142014-01-24 18:25:22220 if (extensions::util::AllowFileAccess((*it)->id(), browser()->profile()))
[email protected]cadac622013-06-11 16:46:36221 extension_list.push_back(it->get());
[email protected]84df8332011-12-06 18:22:46222 }
223
224 for (size_t i = 0; i < extension_list.size(); ++i) {
[email protected]a7fe9112012-07-20 02:34:45225 content::WindowedNotificationObserver user_scripts_observer(
[email protected]adf5a102014-07-31 12:44:06226 extensions::NOTIFICATION_USER_SCRIPTS_UPDATED,
[email protected]84df8332011-12-06 18:22:46227 content::NotificationService::AllSources());
[email protected]1d5cf4142014-01-24 18:25:22228 extensions::util::SetAllowFileAccess(
229 extension_list[i]->id(), browser()->profile(), false);
[email protected]84df8332011-12-06 18:22:46230 user_scripts_observer.Wait();
[email protected]05c82182010-06-24 17:49:08231 }
232
233 TestInjection(false, false);
234}
235
[email protected]919ddc82009-07-15 04:30:12236// ExtensionsLoadTest
237// Ensures that we can startup the browser with --load-extension and see them
238// run.
[email protected]919ddc82009-07-15 04:30:12239class ExtensionsLoadTest : public ExtensionStartupTestBase {
240 public:
241 ExtensionsLoadTest() {
[email protected]650b2d52013-02-10 03:41:45242 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04243 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
244 one_extension_path = one_extension_path
[email protected]919ddc82009-07-15 04:30:12245 .AppendASCII("extensions")
246 .AppendASCII("good")
247 .AppendASCII("Extensions")
248 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
249 .AppendASCII("1.0.0.0");
[email protected]bfc93562011-12-14 19:40:04250 load_extensions_.push_back(one_extension_path.value());
[email protected]f0488f2f2009-07-01 05:25:22251 }
[email protected]919ddc82009-07-15 04:30:12252};
[email protected]f0488f2f2009-07-01 05:25:22253
[email protected]4b109052011-04-29 12:53:24254// Fails inconsistently on Linux x64. http://crbug.com/80961
[email protected]dc128022012-02-17 23:14:58255// TODO(dpapad): Has not failed since October 2011, let's reenable, monitor
256// and act accordingly.
257IN_PROC_BROWSER_TEST_F(ExtensionsLoadTest, Test) {
[email protected]d728e002010-12-08 04:46:23258 WaitForServicesToStart(1, true);
[email protected]919ddc82009-07-15 04:30:12259 TestInjection(true, true);
[email protected]f0488f2f2009-07-01 05:25:22260}
[email protected]bfc93562011-12-14 19:40:04261
262// ExtensionsLoadMultipleTest
263// Ensures that we can startup the browser with multiple extensions
264// via --load-extension=X1,X2,X3.
265class ExtensionsLoadMultipleTest : public ExtensionStartupTestBase {
266 public:
267 ExtensionsLoadMultipleTest() {
[email protected]650b2d52013-02-10 03:41:45268 base::FilePath one_extension_path;
[email protected]bfc93562011-12-14 19:40:04269 PathService::Get(chrome::DIR_TEST_DATA, &one_extension_path);
270 one_extension_path = one_extension_path
271 .AppendASCII("extensions")
272 .AppendASCII("good")
273 .AppendASCII("Extensions")
274 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj")
275 .AppendASCII("1.0.0.0");
276 load_extensions_.push_back(one_extension_path.value());
277
[email protected]650b2d52013-02-10 03:41:45278 base::FilePath second_extension_path;
[email protected]bfc93562011-12-14 19:40:04279 PathService::Get(chrome::DIR_TEST_DATA, &second_extension_path);
280 second_extension_path = second_extension_path
281 .AppendASCII("extensions")
282 .AppendASCII("app");
283 load_extensions_.push_back(second_extension_path.value());
284
[email protected]650b2d52013-02-10 03:41:45285 base::FilePath third_extension_path;
[email protected]bfc93562011-12-14 19:40:04286 PathService::Get(chrome::DIR_TEST_DATA, &third_extension_path);
287 third_extension_path = third_extension_path
288 .AppendASCII("extensions")
289 .AppendASCII("app1");
290 load_extensions_.push_back(third_extension_path.value());
291
[email protected]650b2d52013-02-10 03:41:45292 base::FilePath fourth_extension_path;
[email protected]bfc93562011-12-14 19:40:04293 PathService::Get(chrome::DIR_TEST_DATA, &fourth_extension_path);
294 fourth_extension_path = fourth_extension_path
295 .AppendASCII("extensions")
296 .AppendASCII("app2");
297 load_extensions_.push_back(fourth_extension_path.value());
298 }
299};
300
301IN_PROC_BROWSER_TEST_F(ExtensionsLoadMultipleTest, Test) {
302 WaitForServicesToStart(4, true);
303 TestInjection(true, true);
304}