Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / plugins / plugin_prefs_unittest.cc
blob49c76c01e314803ed02f50fe644b94ac6d653cae
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.
5 #include "chrome/browser/plugins/plugin_prefs.h"
7 #include "base/at_exit.h"
8 #include "base/bind.h"
9 #include "base/path_service.h"
10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "content/public/browser/plugin_service.h"
15 #include "content/public/browser/render_process_host.h"
16 #include "content/public/common/webplugininfo.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/test_utils.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using base::ASCIIToUTF16;
22 using content::BrowserThread;
23 using content::PluginService;
25 namespace {
27 void CanEnablePluginCallback(const base::Closure& quit_closure,
28 bool expected_can_change,
29 bool did_change) {
30 EXPECT_EQ(expected_can_change, did_change);
31 quit_closure.Run();
34 #if !(defined(OS_LINUX) && defined(USE_AURA))
35 base::FilePath GetComponentUpdatedPepperFlashPath(
36 const base::FilePath::StringType& version) {
37 base::FilePath path;
38 EXPECT_TRUE(PathService::Get(
39 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &path));
40 path = path.Append(version);
41 path = path.Append(chrome::kPepperFlashPluginFilename);
42 return path;
45 base::FilePath GetBundledPepperFlashPath() {
46 base::FilePath path;
47 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path));
48 return path;
50 #endif // !(defined(OS_LINUX) && defined(USE_AURA))
52 void GotPlugins(const base::Closure& quit_closure,
53 const std::vector<content::WebPluginInfo>& plugins) {
54 quit_closure.Run();
57 } // namespace
59 class PluginPrefsTest : public ::testing::Test {
60 public:
61 void SetUp() override { plugin_prefs_ = new PluginPrefs(); }
63 void SetPolicyEnforcedPluginPatterns(
64 const std::set<base::string16>& disabled,
65 const std::set<base::string16>& disabled_exceptions,
66 const std::set<base::string16>& enabled) {
67 plugin_prefs_->SetPolicyEnforcedPluginPatterns(
68 disabled, disabled_exceptions, enabled);
71 protected:
72 void EnablePluginSynchronously(bool enabled,
73 const base::FilePath& path,
74 bool expected_can_change) {
75 base::RunLoop run_loop;
76 plugin_prefs_->EnablePlugin(
77 enabled, path,
78 base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(),
79 expected_can_change));
80 run_loop.Run();
83 void RefreshPluginsSynchronously() {
84 PluginService::GetInstance()->RefreshPlugins();
85 #if !defined(OS_WIN)
86 // Can't go out of process in unit tests.
87 content::RenderProcessHost::SetRunRendererInProcess(true);
88 #endif
89 scoped_refptr<content::MessageLoopRunner> runner =
90 new content::MessageLoopRunner;
91 PluginService::GetInstance()->GetPlugins(
92 base::Bind(&GotPlugins, runner->QuitClosure()));
93 runner->Run();
94 #if !defined(OS_WIN)
95 content::RenderProcessHost::SetRunRendererInProcess(false);
96 #endif
99 scoped_refptr<PluginPrefs> plugin_prefs_;
102 TEST_F(PluginPrefsTest, DisabledByPolicy) {
103 std::set<base::string16> disabled_plugins;
104 disabled_plugins.insert(ASCIIToUTF16("Disable this!"));
105 disabled_plugins.insert(ASCIIToUTF16("*Google*"));
106 SetPolicyEnforcedPluginPatterns(disabled_plugins,
107 std::set<base::string16>(),
108 std::set<base::string16>());
110 EXPECT_EQ(PluginPrefs::NO_POLICY,
111 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
112 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
113 plugin_prefs_->PolicyStatusForPlugin(
114 ASCIIToUTF16("Disable this!")));
115 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
116 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth")));
119 TEST_F(PluginPrefsTest, EnabledByPolicy) {
120 std::set<base::string16> enabled_plugins;
121 enabled_plugins.insert(ASCIIToUTF16("Enable that!"));
122 enabled_plugins.insert(ASCIIToUTF16("PDF*"));
123 SetPolicyEnforcedPluginPatterns(std::set<base::string16>(),
124 std::set<base::string16>(),
125 enabled_plugins);
127 EXPECT_EQ(PluginPrefs::NO_POLICY,
128 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42")));
129 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
130 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!")));
131 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
132 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader")));
135 TEST_F(PluginPrefsTest, EnabledAndDisabledByPolicy) {
136 const base::string16 k42(ASCIIToUTF16("42"));
137 const base::string16 kEnabled(ASCIIToUTF16("Enabled"));
138 const base::string16 kEnabled2(ASCIIToUTF16("Enabled 2"));
139 const base::string16 kEnabled3(ASCIIToUTF16("Enabled 3"));
140 const base::string16 kException(ASCIIToUTF16("Exception"));
141 const base::string16 kException2(ASCIIToUTF16("Exception 2"));
142 const base::string16 kGoogleMars(ASCIIToUTF16("Google Mars"));
143 const base::string16 kGoogleEarth(ASCIIToUTF16("Google Earth"));
145 std::set<base::string16> disabled_plugins;
146 std::set<base::string16> disabled_plugins_exceptions;
147 std::set<base::string16> enabled_plugins;
149 disabled_plugins.insert(kEnabled);
150 disabled_plugins_exceptions.insert(kEnabled);
151 enabled_plugins.insert(kEnabled);
153 disabled_plugins_exceptions.insert(kException);
155 disabled_plugins.insert(kEnabled2);
156 enabled_plugins.insert(kEnabled2);
158 disabled_plugins.insert(kException2);
159 disabled_plugins_exceptions.insert(kException2);
161 disabled_plugins_exceptions.insert(kEnabled3);
162 enabled_plugins.insert(kEnabled3);
164 SetPolicyEnforcedPluginPatterns(disabled_plugins,
165 disabled_plugins_exceptions,
166 enabled_plugins);
168 EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42));
170 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
171 plugin_prefs_->PolicyStatusForPlugin(kEnabled));
172 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
173 plugin_prefs_->PolicyStatusForPlugin(kEnabled2));
174 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
175 plugin_prefs_->PolicyStatusForPlugin(kEnabled3));
177 EXPECT_EQ(PluginPrefs::NO_POLICY,
178 plugin_prefs_->PolicyStatusForPlugin(kException));
179 EXPECT_EQ(PluginPrefs::NO_POLICY,
180 plugin_prefs_->PolicyStatusForPlugin(kException2));
182 disabled_plugins.clear();
183 disabled_plugins_exceptions.clear();
184 enabled_plugins.clear();
186 disabled_plugins.insert(ASCIIToUTF16("*"));
187 disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*"));
188 enabled_plugins.insert(kGoogleEarth);
190 SetPolicyEnforcedPluginPatterns(disabled_plugins,
191 disabled_plugins_exceptions,
192 enabled_plugins);
194 EXPECT_EQ(PluginPrefs::POLICY_ENABLED,
195 plugin_prefs_->PolicyStatusForPlugin(kGoogleEarth));
196 EXPECT_EQ(PluginPrefs::NO_POLICY,
197 plugin_prefs_->PolicyStatusForPlugin(kGoogleMars));
198 EXPECT_EQ(PluginPrefs::POLICY_DISABLED,
199 plugin_prefs_->PolicyStatusForPlugin(k42));
202 // Linux Aura doesn't support NPAPI.
203 #if !(defined(OS_LINUX) && defined(USE_AURA))
205 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) {
206 content::TestBrowserThreadBundle browser_threads;
207 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService.
209 PluginService::GetInstance()->Init();
210 PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
212 base::string16 component_updated_plugin_name(
213 ASCIIToUTF16("Component-updated Pepper Flash"));
214 content::WebPluginInfo component_updated_plugin_1(
215 component_updated_plugin_name,
216 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.227")),
217 ASCIIToUTF16("11.3.31.227"),
218 ASCIIToUTF16(""));
219 content::WebPluginInfo component_updated_plugin_2(
220 component_updated_plugin_name,
221 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.228")),
222 ASCIIToUTF16("11.3.31.228"),
223 ASCIIToUTF16(""));
224 content::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"),
225 GetBundledPepperFlashPath(),
226 ASCIIToUTF16("11.3.31.229"),
227 ASCIIToUTF16(""));
228 component_updated_plugin_1.type =
229 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS;
230 component_updated_plugin_2.type =
231 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS;
232 bundled_plugin.type =
233 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS;
235 PluginService::GetInstance()->RegisterInternalPlugin(
236 component_updated_plugin_1, false);
237 PluginService::GetInstance()->RegisterInternalPlugin(
238 component_updated_plugin_2, false);
239 PluginService::GetInstance()->RegisterInternalPlugin(bundled_plugin, false);
241 RefreshPluginsSynchronously();
243 // Set the state of any of the three plugins will affect the others.
244 EnablePluginSynchronously(true, component_updated_plugin_1.path, true);
245 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
246 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
247 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
249 EnablePluginSynchronously(false, bundled_plugin.path, true);
250 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
251 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
252 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
254 EnablePluginSynchronously(true, component_updated_plugin_2.path, true);
255 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
256 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
257 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
259 std::set<base::string16> disabled_plugins;
260 disabled_plugins.insert(component_updated_plugin_name);
261 SetPolicyEnforcedPluginPatterns(disabled_plugins,
262 std::set<base::string16>(),
263 std::set<base::string16>());
265 // Policy settings should be respected.
266 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
267 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
268 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
270 EnablePluginSynchronously(false, bundled_plugin.path, true);
271 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
273 // Trying to change the state of a policy-enforced plugin should not take
274 // effect. And it shouldn't change the state of other plugins either, even if
275 // they are not restricted by any policy.
276 EnablePluginSynchronously(true, component_updated_plugin_1.path, false);
277 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
278 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
279 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
281 EnablePluginSynchronously(true, bundled_plugin.path, true);
282 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1));
283 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2));
284 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin));
287 #endif