OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/plugin_prefs.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/bind.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/path_service.h" | |
11 #include "base/run_loop.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/common/chrome_constants.h" | |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "content/public/browser/plugin_service.h" | |
16 #include "content/public/test/test_browser_thread.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "webkit/plugins/npapi/mock_plugin_list.cc" | |
19 #include "webkit/plugins/webplugininfo.h" | |
20 | |
21 using content::BrowserThread; | |
22 using content::PluginService; | |
23 | |
24 namespace { | |
25 | |
26 void CanEnablePluginCallback(const base::Closure& quit_closure, | |
27 bool expected_can_change, | |
28 bool did_change) { | |
29 EXPECT_EQ(expected_can_change, did_change); | |
30 quit_closure.Run(); | |
31 } | |
32 | |
33 FilePath GetComponentUpdatedPepperFlashPath( | |
34 const FilePath::StringType& version) { | |
35 FilePath path; | |
36 EXPECT_TRUE(PathService::Get( | |
37 chrome::DIR_COMPONENT_UPDATED_PEPPER_FLASH_PLUGIN, &path)); | |
38 path = path.Append(version); | |
39 path = path.Append(chrome::kPepperFlashPluginFilename); | |
40 return path; | |
41 } | |
42 | |
43 FilePath GetBundledPepperFlashPath() { | |
44 FilePath path; | |
45 EXPECT_TRUE(PathService::Get(chrome::FILE_PEPPER_FLASH_PLUGIN, &path)); | |
46 return path; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 class PluginPrefsTest : public ::testing::Test { | |
52 public: | |
53 virtual void SetUp() OVERRIDE { | |
54 plugin_prefs_ = new PluginPrefs(); | |
55 } | |
56 | |
57 void SetPolicyEnforcedPluginPatterns( | |
58 const std::set<string16>& disabled, | |
59 const std::set<string16>& disabled_exceptions, | |
60 const std::set<string16>& enabled) { | |
61 plugin_prefs_->SetPolicyEnforcedPluginPatterns( | |
62 disabled, disabled_exceptions, enabled); | |
63 } | |
64 | |
65 protected: | |
66 void EnablePluginSynchronously(bool enabled, | |
67 const FilePath& path, | |
68 bool expected_can_change) { | |
69 base::RunLoop run_loop; | |
70 plugin_prefs_->EnablePlugin( | |
71 enabled, path, | |
72 base::Bind(&CanEnablePluginCallback, run_loop.QuitClosure(), | |
73 expected_can_change)); | |
74 run_loop.Run(); | |
75 } | |
76 | |
77 scoped_refptr<PluginPrefs> plugin_prefs_; | |
78 }; | |
79 | |
80 TEST_F(PluginPrefsTest, DisabledByPolicy) { | |
81 std::set<string16> disabled_plugins; | |
82 disabled_plugins.insert(ASCIIToUTF16("Disable this!")); | |
83 disabled_plugins.insert(ASCIIToUTF16("*Google*")); | |
84 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
85 std::set<string16>(), | |
86 std::set<string16>()); | |
87 | |
88 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
89 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42"))); | |
90 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
91 plugin_prefs_->PolicyStatusForPlugin( | |
92 ASCIIToUTF16("Disable this!"))); | |
93 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
94 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Google Earth"))); | |
95 } | |
96 | |
97 TEST_F(PluginPrefsTest, EnabledByPolicy) { | |
98 std::set<string16> enabled_plugins; | |
99 enabled_plugins.insert(ASCIIToUTF16("Enable that!")); | |
100 enabled_plugins.insert(ASCIIToUTF16("PDF*")); | |
101 SetPolicyEnforcedPluginPatterns(std::set<string16>(), | |
102 std::set<string16>(), | |
103 enabled_plugins); | |
104 | |
105 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
106 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("42"))); | |
107 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
108 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("Enable that!"))); | |
109 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
110 plugin_prefs_->PolicyStatusForPlugin(ASCIIToUTF16("PDF Reader"))); | |
111 } | |
112 | |
113 TEST_F(PluginPrefsTest, EnabledAndDisabledByPolicy) { | |
114 const string16 k42(ASCIIToUTF16("42")); | |
115 const string16 kEnabled(ASCIIToUTF16("Enabled")); | |
116 const string16 kEnabled2(ASCIIToUTF16("Enabled 2")); | |
117 const string16 kEnabled3(ASCIIToUTF16("Enabled 3")); | |
118 const string16 kException(ASCIIToUTF16("Exception")); | |
119 const string16 kException2(ASCIIToUTF16("Exception 2")); | |
120 const string16 kGoogleMars(ASCIIToUTF16("Google Mars")); | |
121 const string16 kGoogleEarth(ASCIIToUTF16("Google Earth")); | |
122 | |
123 std::set<string16> disabled_plugins; | |
124 std::set<string16> disabled_plugins_exceptions; | |
125 std::set<string16> enabled_plugins; | |
126 | |
127 disabled_plugins.insert(kEnabled); | |
128 disabled_plugins_exceptions.insert(kEnabled); | |
129 enabled_plugins.insert(kEnabled); | |
130 | |
131 disabled_plugins_exceptions.insert(kException); | |
132 | |
133 disabled_plugins.insert(kEnabled2); | |
134 enabled_plugins.insert(kEnabled2); | |
135 | |
136 disabled_plugins.insert(kException2); | |
137 disabled_plugins_exceptions.insert(kException2); | |
138 | |
139 disabled_plugins_exceptions.insert(kEnabled3); | |
140 enabled_plugins.insert(kEnabled3); | |
141 | |
142 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
143 disabled_plugins_exceptions, | |
144 enabled_plugins); | |
145 | |
146 EXPECT_EQ(PluginPrefs::NO_POLICY, plugin_prefs_->PolicyStatusForPlugin(k42)); | |
147 | |
148 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
149 plugin_prefs_->PolicyStatusForPlugin(kEnabled)); | |
150 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
151 plugin_prefs_->PolicyStatusForPlugin(kEnabled2)); | |
152 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
153 plugin_prefs_->PolicyStatusForPlugin(kEnabled3)); | |
154 | |
155 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
156 plugin_prefs_->PolicyStatusForPlugin(kException)); | |
157 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
158 plugin_prefs_->PolicyStatusForPlugin(kException2)); | |
159 | |
160 disabled_plugins.clear(); | |
161 disabled_plugins_exceptions.clear(); | |
162 enabled_plugins.clear(); | |
163 | |
164 disabled_plugins.insert(ASCIIToUTF16("*")); | |
165 disabled_plugins_exceptions.insert(ASCIIToUTF16("*Google*")); | |
166 enabled_plugins.insert(kGoogleEarth); | |
167 | |
168 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
169 disabled_plugins_exceptions, | |
170 enabled_plugins); | |
171 | |
172 EXPECT_EQ(PluginPrefs::POLICY_ENABLED, | |
173 plugin_prefs_->PolicyStatusForPlugin(kGoogleEarth)); | |
174 EXPECT_EQ(PluginPrefs::NO_POLICY, | |
175 plugin_prefs_->PolicyStatusForPlugin(kGoogleMars)); | |
176 EXPECT_EQ(PluginPrefs::POLICY_DISABLED, | |
177 plugin_prefs_->PolicyStatusForPlugin(k42)); | |
178 } | |
179 | |
180 TEST_F(PluginPrefsTest, UnifiedPepperFlashState) { | |
181 base::ShadowingAtExitManager at_exit_manager_; // Destroys the PluginService. | |
182 | |
183 MessageLoop message_loop; | |
184 content::TestBrowserThread ui_thread(BrowserThread::UI, &message_loop); | |
185 webkit::npapi::MockPluginList plugin_list(NULL, 0); | |
186 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list); | |
187 PluginService::GetInstance()->Init(); | |
188 plugin_prefs_->SetPluginListForTesting(&plugin_list); | |
189 | |
190 string16 component_updated_plugin_name( | |
191 ASCIIToUTF16("Component-updated Pepper Flash")); | |
192 webkit::WebPluginInfo component_updated_plugin_1( | |
193 component_updated_plugin_name, | |
194 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.227")), | |
195 ASCIIToUTF16("11.3.31.227"), | |
196 ASCIIToUTF16("")); | |
197 webkit::WebPluginInfo component_updated_plugin_2( | |
198 component_updated_plugin_name, | |
199 GetComponentUpdatedPepperFlashPath(FILE_PATH_LITERAL("11.3.31.228")), | |
200 ASCIIToUTF16("11.3.31.228"), | |
201 ASCIIToUTF16("")); | |
202 webkit::WebPluginInfo bundled_plugin(ASCIIToUTF16("Pepper Flash"), | |
203 GetBundledPepperFlashPath(), | |
204 ASCIIToUTF16("11.3.31.229"), | |
205 ASCIIToUTF16("")); | |
206 | |
207 plugin_list.AddPluginToLoad(component_updated_plugin_1); | |
208 plugin_list.AddPluginToLoad(component_updated_plugin_2); | |
209 plugin_list.AddPluginToLoad(bundled_plugin); | |
210 | |
211 // Set the state of any of the three plugins will affect the others. | |
212 EnablePluginSynchronously(true, component_updated_plugin_1.path, true); | |
213 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
214 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
215 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
216 | |
217 EnablePluginSynchronously(false, bundled_plugin.path, true); | |
218 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
219 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
220 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
221 | |
222 EnablePluginSynchronously(true, component_updated_plugin_2.path, true); | |
223 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
224 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
225 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
226 | |
227 std::set<string16> disabled_plugins; | |
228 disabled_plugins.insert(component_updated_plugin_name); | |
229 SetPolicyEnforcedPluginPatterns(disabled_plugins, | |
230 std::set<string16>(), | |
231 std::set<string16>()); | |
232 | |
233 // Policy settings should be respected. | |
234 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
235 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
236 EXPECT_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
237 | |
238 EnablePluginSynchronously(false, bundled_plugin.path, true); | |
239 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
240 | |
241 // Trying to change the state of a policy-enforced plugin should not take | |
242 // effect. And it shouldn't change the state of other plugins either, even if | |
243 // they are not restricted by any policy. | |
244 EnablePluginSynchronously(true, component_updated_plugin_1.path, false); | |
245 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_1)); | |
246 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(component_updated_plugin_2)); | |
247 EXPECT_FALSE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
248 | |
249 EnablePluginSynchronously(true, 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_TRUE(plugin_prefs_->IsPluginEnabled(bundled_plugin)); | |
253 | |
254 plugin_prefs_->SetPluginListForTesting(NULL); | |
255 PluginService::GetInstance()->SetPluginListForTesting(NULL); | |
256 } | |
OLD | NEW |