blob: 8f7d9480066b8a01c29610d6df8896c5173da5e9 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]b83ff222011-01-24 17:37:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lei Zhangd8c53182019-02-06 22:24:395#include "content/browser/plugin_list.h"
[email protected]b83ff222011-01-24 17:37:126
Jan Wilken Dörriead587c32021-03-11 14:09:277#include <string>
8
Lei Zhang8403fa62022-10-12 20:15:269#include "base/files/file_path.h"
Keishi Hattoric1b00232022-11-22 09:04:2610#include "base/memory/raw_ptr.h"
Lei Zhang8403fa62022-10-12 20:15:2611#include "content/public/test/browser_task_environment.h"
[email protected]b83ff222011-01-24 17:37:1212#include "testing/gtest/include/gtest/gtest.h"
[email protected]cafe0d02013-07-23 15:16:2013#include "url/gurl.h"
[email protected]b83ff222011-01-24 17:37:1214
[email protected]29e2fb42013-07-19 01:13:4715namespace content {
[email protected]b83ff222011-01-24 17:37:1216
[email protected]b83ff222011-01-24 17:37:1217namespace {
18
[email protected]9a60ccb2013-07-19 22:23:3619base::FilePath::CharType kFooPath[] = FILE_PATH_LITERAL("/plugins/foo.plugin");
20base::FilePath::CharType kBarPath[] = FILE_PATH_LITERAL("/plugins/bar.plugin");
Lei Zhang8403fa62022-10-12 20:15:2621const char kFooMimeType[] = "application/x-foo-mime-type";
22const char kFooFileType[] = "foo";
[email protected]9a60ccb2013-07-19 22:23:3623
[email protected]f7be2cba2011-09-15 15:33:4524bool Equals(const WebPluginInfo& a, const WebPluginInfo& b) {
Lei Zhangd8c53182019-02-06 22:24:3925 return (a.name == b.name && a.path == b.path && a.version == b.version &&
[email protected]f7be2cba2011-09-15 15:33:4526 a.desc == b.desc);
[email protected]b83ff222011-01-24 17:37:1227}
28
29bool Contains(const std::vector<WebPluginInfo>& list,
[email protected]f7be2cba2011-09-15 15:33:4530 const WebPluginInfo& plugin) {
[email protected]b83ff222011-01-24 17:37:1231 for (std::vector<WebPluginInfo>::const_iterator it = list.begin();
32 it != list.end(); ++it) {
akshayf97041c52023-10-23 13:00:0433 if (Equals(*it, plugin)) {
[email protected]b83ff222011-01-24 17:37:1234 return true;
akshayf97041c52023-10-23 13:00:0435 }
[email protected]b83ff222011-01-24 17:37:1236 }
37 return false;
38}
39
[email protected]8f3372122013-07-18 04:34:1440} // namespace
41
[email protected]b83ff222011-01-24 17:37:1242class PluginListTest : public testing::Test {
43 public:
44 PluginListTest()
akshayf97041c52023-10-23 13:00:0445 : plugin_list_(nullptr, PluginListDeleter),
46 foo_plugin_(u"Foo PluginListTest",
[email protected]a3ef4832013-02-02 05:12:3347 base::FilePath(kFooPath),
Jan Wilken Dörrie8aeb5742021-03-23 19:27:0248 u"1.2.3",
49 u"foo"),
50 bar_plugin_(u"Bar Plugin", base::FilePath(kBarPath), u"2.3.4", u"bar") {
51 }
[email protected]b83ff222011-01-24 17:37:1252
dcheng60135332015-01-09 02:05:3453 void SetUp() override {
akshayf97041c52023-10-23 13:00:0454 plugin_list_.reset(new PluginList());
Lei Zhang8403fa62022-10-12 20:15:2655 plugin_list_->RegisterInternalPlugin(bar_plugin_, false);
Lei Zhang9ffa3d12022-10-11 01:05:4556 foo_plugin_.mime_types.emplace_back(kFooMimeType, kFooFileType,
57 std::string());
Lei Zhang8403fa62022-10-12 20:15:2658 plugin_list_->RegisterInternalPlugin(foo_plugin_, false);
59 }
60
akshayf97041c52023-10-23 13:00:0461 static void PluginListDeleter(PluginList* plugin_list) { delete plugin_list; }
[email protected]b83ff222011-01-24 17:37:1262
63 protected:
Lei Zhang8403fa62022-10-12 20:15:2664 // Must be first.
65 BrowserTaskEnvironment task_environment_;
66
akshayf97041c52023-10-23 13:00:0467 std::unique_ptr<PluginList, decltype(&PluginListDeleter)> plugin_list_;
[email protected]b83ff222011-01-24 17:37:1268 WebPluginInfo foo_plugin_;
69 WebPluginInfo bar_plugin_;
70};
71
72TEST_F(PluginListTest, GetPlugins) {
73 std::vector<WebPluginInfo> plugins;
Lei Zhang8403fa62022-10-12 20:15:2674 plugin_list_->GetPlugins(&plugins);
[email protected]b83ff222011-01-24 17:37:1275 EXPECT_EQ(2u, plugins.size());
[email protected]f7be2cba2011-09-15 15:33:4576 EXPECT_TRUE(Contains(plugins, foo_plugin_));
77 EXPECT_TRUE(Contains(plugins, bar_plugin_));
[email protected]b83ff222011-01-24 17:37:1278}
79
[email protected]cd91aff22011-01-25 22:47:0080TEST_F(PluginListTest, BadPluginDescription) {
[email protected]7a8edaf2011-11-28 20:58:4881 WebPluginInfo plugin_3043(
Jan Wilken Dörrieaace0cfef2021-03-11 22:01:5882 std::u16string(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")),
83 std::u16string(), std::u16string());
[email protected]cd91aff22011-01-25 22:47:0084 // Simulate loading of the plugins.
Lei Zhang8403fa62022-10-12 20:15:2685 plugin_list_->RegisterInternalPlugin(plugin_3043, false);
[email protected]cd91aff22011-01-25 22:47:0086 // Now we should have them in the state we specified above.
Lei Zhang8403fa62022-10-12 20:15:2687 plugin_list_->RefreshPlugins();
[email protected]cd91aff22011-01-25 22:47:0088 std::vector<WebPluginInfo> plugins;
Lei Zhang8403fa62022-10-12 20:15:2689 plugin_list_->GetPlugins(&plugins);
[email protected]f7be2cba2011-09-15 15:33:4590 ASSERT_TRUE(Contains(plugins, plugin_3043));
[email protected]b83ff222011-01-24 17:37:1291}
92
[email protected]cafe0d02013-07-23 15:16:2093TEST_F(PluginListTest, GetPluginInfoArray) {
94 const char kTargetUrl[] = "http://example.com/test.foo";
95 GURL target_url(kTargetUrl);
96 std::vector<WebPluginInfo> plugins;
97 std::vector<std::string> actual_mime_types;
Lei Zhangc923379e2019-02-07 18:13:0898 bool is_stale;
[email protected]cafe0d02013-07-23 15:16:2099
Lei Zhang0960d562019-02-12 22:49:23100 // The PluginList starts out in a stale state.
Lei Zhang8403fa62022-10-12 20:15:26101 is_stale = plugin_list_->GetPluginInfoArray(
Lei Zhang0960d562019-02-12 22:49:23102 target_url, "application/octet-stream",
103 /*allow_wildcard=*/false, &plugins, &actual_mime_types);
104 EXPECT_TRUE(is_stale);
105 EXPECT_EQ(0u, plugins.size());
106 EXPECT_EQ(0u, actual_mime_types.size());
107
108 // Refresh it.
Lei Zhang8403fa62022-10-12 20:15:26109 plugin_list_->GetPlugins(&plugins);
Lei Zhang0960d562019-02-12 22:49:23110 plugins.clear();
111
Lei Zhangc923379e2019-02-07 18:13:08112 // The file type of the URL is supported by |foo_plugin_|. However,
113 // GetPluginInfoArray should not match |foo_plugin_| because the MIME type is
[email protected]cafe0d02013-07-23 15:16:20114 // application/octet-stream.
Lei Zhang8403fa62022-10-12 20:15:26115 is_stale = plugin_list_->GetPluginInfoArray(
Lei Zhangc923379e2019-02-07 18:13:08116 target_url, "application/octet-stream",
117 /*allow_wildcard=*/false, &plugins, &actual_mime_types);
118 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20119 EXPECT_EQ(0u, plugins.size());
120 EXPECT_EQ(0u, actual_mime_types.size());
121
Lei Zhangc923379e2019-02-07 18:13:08122 // |foo_plugin_| matches due to the MIME type.
[email protected]cafe0d02013-07-23 15:16:20123 plugins.clear();
124 actual_mime_types.clear();
Lei Zhang8403fa62022-10-12 20:15:26125 is_stale = plugin_list_->GetPluginInfoArray(target_url, kFooMimeType,
126 /*allow_wildcard=*/false,
127 &plugins, &actual_mime_types);
Lei Zhangc923379e2019-02-07 18:13:08128 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20129 EXPECT_EQ(1u, plugins.size());
130 EXPECT_TRUE(Contains(plugins, foo_plugin_));
131 ASSERT_EQ(1u, actual_mime_types.size());
132 EXPECT_EQ(kFooMimeType, actual_mime_types.front());
133
Lei Zhangc923379e2019-02-07 18:13:08134 // |foo_plugin_| matches due to the file type and empty MIME type.
[email protected]cafe0d02013-07-23 15:16:20135 plugins.clear();
136 actual_mime_types.clear();
Lei Zhang8403fa62022-10-12 20:15:26137 is_stale = plugin_list_->GetPluginInfoArray(target_url, "",
138 /*allow_wildcard=*/false,
139 &plugins, &actual_mime_types);
Lei Zhangc923379e2019-02-07 18:13:08140 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20141 EXPECT_EQ(1u, plugins.size());
142 EXPECT_TRUE(Contains(plugins, foo_plugin_));
143 ASSERT_EQ(1u, actual_mime_types.size());
144 EXPECT_EQ(kFooMimeType, actual_mime_types.front());
145}
146
[email protected]29e2fb42013-07-19 01:13:47147} // namespace content