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 "content/browser/plugin_loader_posix.h"
7 #include "base/at_exit.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/common/plugin_list.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::ASCIIToUTF16
;
22 class MockPluginLoaderPosix
: public PluginLoaderPosix
{
24 MOCK_METHOD0(LoadPluginsInternal
, void(void));
26 size_t number_of_pending_callbacks() {
27 return callbacks_
.size();
30 std::vector
<base::FilePath
>* canonical_list() {
31 return &canonical_list_
;
34 size_t next_load_index() {
35 return next_load_index_
;
38 const std::vector
<WebPluginInfo
>& loaded_plugins() {
39 return loaded_plugins_
;
42 std::vector
<WebPluginInfo
>* internal_plugins() {
43 return &internal_plugins_
;
46 void RealLoadPluginsInternal() {
47 PluginLoaderPosix::LoadPluginsInternal();
50 void TestOnPluginLoaded(uint32 index
, const WebPluginInfo
& plugin
) {
51 OnPluginLoaded(index
, plugin
);
54 void TestOnPluginLoadFailed(uint32 index
, const base::FilePath
& path
) {
55 OnPluginLoadFailed(index
, path
);
59 virtual ~MockPluginLoaderPosix() {}
62 void VerifyCallback(int* run_count
, const std::vector
<WebPluginInfo
>&) {
66 class PluginLoaderPosixTest
: public testing::Test
{
68 PluginLoaderPosixTest()
69 : plugin1_(ASCIIToUTF16("plugin1"), base::FilePath("/tmp/one.plugin"),
70 ASCIIToUTF16("1.0"), base::string16()),
71 plugin2_(ASCIIToUTF16("plugin2"), base::FilePath("/tmp/two.plugin"),
72 ASCIIToUTF16("2.0"), base::string16()),
73 plugin3_(ASCIIToUTF16("plugin3"), base::FilePath("/tmp/three.plugin"),
74 ASCIIToUTF16("3.0"), base::string16()),
75 file_thread_(BrowserThread::FILE, &message_loop_
),
76 io_thread_(BrowserThread::IO
, &message_loop_
),
77 plugin_loader_(new MockPluginLoaderPosix
) {
80 virtual void SetUp() OVERRIDE
{
81 PluginServiceImpl::GetInstance()->Init();
84 base::MessageLoop
* message_loop() { return &message_loop_
; }
85 MockPluginLoaderPosix
* plugin_loader() { return plugin_loader_
.get(); }
87 void AddThreePlugins() {
88 plugin_loader_
->canonical_list()->clear();
89 plugin_loader_
->canonical_list()->push_back(plugin1_
.path
);
90 plugin_loader_
->canonical_list()->push_back(plugin2_
.path
);
91 plugin_loader_
->canonical_list()->push_back(plugin3_
.path
);
94 // Data used for testing.
95 WebPluginInfo plugin1_
;
96 WebPluginInfo plugin2_
;
97 WebPluginInfo plugin3_
;
100 // Destroys PluginService and PluginList.
101 base::ShadowingAtExitManager at_exit_manager_
;
103 base::MessageLoopForIO message_loop_
;
104 BrowserThreadImpl file_thread_
;
105 BrowserThreadImpl io_thread_
;
107 scoped_refptr
<MockPluginLoaderPosix
> plugin_loader_
;
110 TEST_F(PluginLoaderPosixTest
, QueueRequests
) {
111 int did_callback
= 0;
112 PluginService::GetPluginsCallback callback
=
113 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
115 plugin_loader()->GetPlugins(callback
);
116 plugin_loader()->GetPlugins(callback
);
118 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
119 message_loop()->RunUntilIdle();
121 EXPECT_EQ(0, did_callback
);
123 plugin_loader()->canonical_list()->clear();
124 plugin_loader()->canonical_list()->push_back(plugin1_
.path
);
125 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
126 message_loop()->RunUntilIdle();
128 EXPECT_EQ(2, did_callback
);
131 TEST_F(PluginLoaderPosixTest
, QueueRequestsAndInvalidate
) {
132 int did_callback
= 0;
133 PluginService::GetPluginsCallback callback
=
134 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
136 plugin_loader()->GetPlugins(callback
);
138 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
139 message_loop()->RunUntilIdle();
141 EXPECT_EQ(0, did_callback
);
142 ::testing::Mock::VerifyAndClearExpectations(plugin_loader());
144 // Invalidate the plugin list, then queue up another request.
145 PluginList::Singleton()->RefreshPlugins();
146 plugin_loader()->GetPlugins(callback
);
148 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
150 plugin_loader()->canonical_list()->clear();
151 plugin_loader()->canonical_list()->push_back(plugin1_
.path
);
152 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
153 message_loop()->RunUntilIdle();
155 // Only the first request should have been fulfilled.
156 EXPECT_EQ(1, did_callback
);
158 plugin_loader()->canonical_list()->clear();
159 plugin_loader()->canonical_list()->push_back(plugin1_
.path
);
160 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
161 message_loop()->RunUntilIdle();
163 EXPECT_EQ(2, did_callback
);
166 TEST_F(PluginLoaderPosixTest
, ThreeSuccessfulLoads
) {
167 int did_callback
= 0;
168 PluginService::GetPluginsCallback callback
=
169 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
171 plugin_loader()->GetPlugins(callback
);
173 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
174 message_loop()->RunUntilIdle();
178 EXPECT_EQ(0u, plugin_loader()->next_load_index());
180 const std::vector
<WebPluginInfo
>& plugins(plugin_loader()->loaded_plugins());
182 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
183 EXPECT_EQ(1u, plugin_loader()->next_load_index());
184 EXPECT_EQ(1u, plugins
.size());
185 EXPECT_EQ(plugin1_
.name
, plugins
[0].name
);
187 message_loop()->RunUntilIdle();
188 EXPECT_EQ(0, did_callback
);
190 plugin_loader()->TestOnPluginLoaded(1, plugin2_
);
191 EXPECT_EQ(2u, plugin_loader()->next_load_index());
192 EXPECT_EQ(2u, plugins
.size());
193 EXPECT_EQ(plugin2_
.name
, plugins
[1].name
);
195 message_loop()->RunUntilIdle();
196 EXPECT_EQ(0, did_callback
);
198 plugin_loader()->TestOnPluginLoaded(2, plugin3_
);
199 EXPECT_EQ(3u, plugins
.size());
200 EXPECT_EQ(plugin3_
.name
, plugins
[2].name
);
202 message_loop()->RunUntilIdle();
203 EXPECT_EQ(1, did_callback
);
206 TEST_F(PluginLoaderPosixTest
, ThreeSuccessfulLoadsThenCrash
) {
207 int did_callback
= 0;
208 PluginService::GetPluginsCallback callback
=
209 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
211 plugin_loader()->GetPlugins(callback
);
213 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(2);
214 message_loop()->RunUntilIdle();
218 EXPECT_EQ(0u, plugin_loader()->next_load_index());
220 const std::vector
<WebPluginInfo
>& plugins(plugin_loader()->loaded_plugins());
222 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
223 EXPECT_EQ(1u, plugin_loader()->next_load_index());
224 EXPECT_EQ(1u, plugins
.size());
225 EXPECT_EQ(plugin1_
.name
, plugins
[0].name
);
227 message_loop()->RunUntilIdle();
228 EXPECT_EQ(0, did_callback
);
230 plugin_loader()->TestOnPluginLoaded(1, plugin2_
);
231 EXPECT_EQ(2u, plugin_loader()->next_load_index());
232 EXPECT_EQ(2u, plugins
.size());
233 EXPECT_EQ(plugin2_
.name
, plugins
[1].name
);
235 message_loop()->RunUntilIdle();
236 EXPECT_EQ(0, did_callback
);
238 plugin_loader()->TestOnPluginLoaded(2, plugin3_
);
239 EXPECT_EQ(3u, plugins
.size());
240 EXPECT_EQ(plugin3_
.name
, plugins
[2].name
);
242 message_loop()->RunUntilIdle();
243 EXPECT_EQ(1, did_callback
);
245 plugin_loader()->OnProcessCrashed(42);
248 TEST_F(PluginLoaderPosixTest
, TwoFailures
) {
249 int did_callback
= 0;
250 PluginService::GetPluginsCallback callback
=
251 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
253 plugin_loader()->GetPlugins(callback
);
255 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
256 message_loop()->RunUntilIdle();
260 EXPECT_EQ(0u, plugin_loader()->next_load_index());
262 const std::vector
<WebPluginInfo
>& plugins(plugin_loader()->loaded_plugins());
264 plugin_loader()->TestOnPluginLoadFailed(0, plugin1_
.path
);
265 EXPECT_EQ(1u, plugin_loader()->next_load_index());
266 EXPECT_EQ(0u, plugins
.size());
268 message_loop()->RunUntilIdle();
269 EXPECT_EQ(0, did_callback
);
271 plugin_loader()->TestOnPluginLoaded(1, plugin2_
);
272 EXPECT_EQ(2u, plugin_loader()->next_load_index());
273 EXPECT_EQ(1u, plugins
.size());
274 EXPECT_EQ(plugin2_
.name
, plugins
[0].name
);
276 message_loop()->RunUntilIdle();
277 EXPECT_EQ(0, did_callback
);
279 plugin_loader()->TestOnPluginLoadFailed(2, plugin3_
.path
);
280 EXPECT_EQ(1u, plugins
.size());
282 message_loop()->RunUntilIdle();
283 EXPECT_EQ(1, did_callback
);
286 TEST_F(PluginLoaderPosixTest
, CrashedProcess
) {
287 int did_callback
= 0;
288 PluginService::GetPluginsCallback callback
=
289 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
291 plugin_loader()->GetPlugins(callback
);
293 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
294 message_loop()->RunUntilIdle();
298 EXPECT_EQ(0u, plugin_loader()->next_load_index());
300 const std::vector
<WebPluginInfo
>& plugins(plugin_loader()->loaded_plugins());
302 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
303 EXPECT_EQ(1u, plugin_loader()->next_load_index());
304 EXPECT_EQ(1u, plugins
.size());
305 EXPECT_EQ(plugin1_
.name
, plugins
[0].name
);
307 message_loop()->RunUntilIdle();
308 EXPECT_EQ(0, did_callback
);
310 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
311 plugin_loader()->OnProcessCrashed(42);
312 EXPECT_EQ(1u, plugin_loader()->canonical_list()->size());
313 EXPECT_EQ(0u, plugin_loader()->next_load_index());
314 EXPECT_EQ(plugin3_
.path
.value(),
315 plugin_loader()->canonical_list()->at(0).value());
318 TEST_F(PluginLoaderPosixTest
, InternalPlugin
) {
319 int did_callback
= 0;
320 PluginService::GetPluginsCallback callback
=
321 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
323 plugin_loader()->GetPlugins(callback
);
325 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
326 message_loop()->RunUntilIdle();
328 plugin2_
.path
= base::FilePath("/internal/plugin.plugin");
332 plugin_loader()->internal_plugins()->clear();
333 plugin_loader()->internal_plugins()->push_back(plugin2_
);
335 EXPECT_EQ(0u, plugin_loader()->next_load_index());
337 const std::vector
<WebPluginInfo
>& plugins(plugin_loader()->loaded_plugins());
339 plugin_loader()->TestOnPluginLoaded(0, plugin1_
);
340 EXPECT_EQ(1u, plugin_loader()->next_load_index());
341 EXPECT_EQ(1u, plugins
.size());
342 EXPECT_EQ(plugin1_
.name
, plugins
[0].name
);
344 message_loop()->RunUntilIdle();
345 EXPECT_EQ(0, did_callback
);
347 // Internal plugins can fail to load if they're built-in with manual
348 // entrypoint functions.
349 plugin_loader()->TestOnPluginLoadFailed(1, plugin2_
.path
);
350 EXPECT_EQ(2u, plugin_loader()->next_load_index());
351 EXPECT_EQ(2u, plugins
.size());
352 EXPECT_EQ(plugin2_
.name
, plugins
[1].name
);
353 EXPECT_EQ(0u, plugin_loader()->internal_plugins()->size());
355 message_loop()->RunUntilIdle();
356 EXPECT_EQ(0, did_callback
);
358 plugin_loader()->TestOnPluginLoaded(2, plugin3_
);
359 EXPECT_EQ(3u, plugins
.size());
360 EXPECT_EQ(plugin3_
.name
, plugins
[2].name
);
362 message_loop()->RunUntilIdle();
363 EXPECT_EQ(1, did_callback
);
366 TEST_F(PluginLoaderPosixTest
, AllCrashed
) {
367 int did_callback
= 0;
368 PluginService::GetPluginsCallback callback
=
369 base::Bind(&VerifyCallback
, base::Unretained(&did_callback
));
371 plugin_loader()->GetPlugins(callback
);
373 // Spin the loop so that the canonical list of plugins can be set.
374 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(1);
375 message_loop()->RunUntilIdle();
378 EXPECT_EQ(0u, plugin_loader()->next_load_index());
380 // Mock the first two calls like normal.
381 testing::Expectation first
=
382 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal()).Times(2);
383 // On the last call, go through the default impl.
384 EXPECT_CALL(*plugin_loader(), LoadPluginsInternal())
387 testing::Invoke(plugin_loader(),
388 &MockPluginLoaderPosix::RealLoadPluginsInternal
));
389 plugin_loader()->OnProcessCrashed(42);
390 plugin_loader()->OnProcessCrashed(42);
391 plugin_loader()->OnProcessCrashed(42);
393 message_loop()->RunUntilIdle();
394 EXPECT_EQ(1, did_callback
);
396 EXPECT_EQ(0u, plugin_loader()->loaded_plugins().size());
399 } // namespace content