blob: 7d1dad5609967c0c19acb86a6aa6ecf21b278b1a [file] [log] [blame]
[email protected]4dd01a042012-03-26 18:03:261// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[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
[email protected]f63582d2013-06-11 22:52:547#include "base/strings/string16.h"
[email protected]906265872013-06-07 22:40:458#include "base/strings/utf_string_conversions.h"
[email protected]b83ff222011-01-24 17:37:129#include "testing/gtest/include/gtest/gtest.h"
[email protected]cafe0d02013-07-23 15:16:2010#include "url/gurl.h"
[email protected]b83ff222011-01-24 17:37:1211
[email protected]29e2fb42013-07-19 01:13:4712namespace content {
[email protected]b83ff222011-01-24 17:37:1213
[email protected]b83ff222011-01-24 17:37:1214namespace {
15
[email protected]9a60ccb2013-07-19 22:23:3616base::FilePath::CharType kFooPath[] = FILE_PATH_LITERAL("/plugins/foo.plugin");
17base::FilePath::CharType kBarPath[] = FILE_PATH_LITERAL("/plugins/bar.plugin");
18const char* kFooName = "Foo Plugin";
[email protected]cafe0d02013-07-23 15:16:2019const char* kFooMimeType = "application/x-foo-mime-type";
20const char* kFooFileType = "foo";
[email protected]9a60ccb2013-07-19 22:23:3621
[email protected]f7be2cba2011-09-15 15:33:4522bool Equals(const WebPluginInfo& a, const WebPluginInfo& b) {
Lei Zhangd8c53182019-02-06 22:24:3923 return (a.name == b.name && a.path == b.path && a.version == b.version &&
[email protected]f7be2cba2011-09-15 15:33:4524 a.desc == b.desc);
[email protected]b83ff222011-01-24 17:37:1225}
26
27bool Contains(const std::vector<WebPluginInfo>& list,
[email protected]f7be2cba2011-09-15 15:33:4528 const WebPluginInfo& plugin) {
[email protected]b83ff222011-01-24 17:37:1229 for (std::vector<WebPluginInfo>::const_iterator it = list.begin();
30 it != list.end(); ++it) {
[email protected]f7be2cba2011-09-15 15:33:4531 if (Equals(*it, plugin))
[email protected]b83ff222011-01-24 17:37:1232 return true;
33 }
34 return false;
35}
36
[email protected]8f3372122013-07-18 04:34:1437} // namespace
38
[email protected]b83ff222011-01-24 17:37:1239class PluginListTest : public testing::Test {
40 public:
41 PluginListTest()
[email protected]32956122013-12-25 07:29:2442 : foo_plugin_(base::ASCIIToUTF16(kFooName),
[email protected]a3ef4832013-02-02 05:12:3343 base::FilePath(kFooPath),
[email protected]32956122013-12-25 07:29:2444 base::ASCIIToUTF16("1.2.3"),
45 base::ASCIIToUTF16("foo")),
46 bar_plugin_(base::ASCIIToUTF16("Bar Plugin"),
[email protected]a3ef4832013-02-02 05:12:3347 base::FilePath(kBarPath),
[email protected]32956122013-12-25 07:29:2448 base::ASCIIToUTF16("2.3.4"),
Lei Zhangd8c53182019-02-06 22:24:3949 base::ASCIIToUTF16("bar")) {}
[email protected]b83ff222011-01-24 17:37:1250
dcheng60135332015-01-09 02:05:3451 void SetUp() override {
[email protected]8f3372122013-07-18 04:34:1452 plugin_list_.RegisterInternalPlugin(bar_plugin_, false);
[email protected]cafe0d02013-07-23 15:16:2053 foo_plugin_.mime_types.push_back(
54 WebPluginMimeType(kFooMimeType, kFooFileType, std::string()));
[email protected]8f3372122013-07-18 04:34:1455 plugin_list_.RegisterInternalPlugin(foo_plugin_, false);
[email protected]b83ff222011-01-24 17:37:1256 }
57
58 protected:
[email protected]8f3372122013-07-18 04:34:1459 PluginList plugin_list_;
[email protected]b83ff222011-01-24 17:37:1260 WebPluginInfo foo_plugin_;
61 WebPluginInfo bar_plugin_;
62};
63
64TEST_F(PluginListTest, GetPlugins) {
65 std::vector<WebPluginInfo> plugins;
pimane8c57ea2016-04-06 01:19:3666 plugin_list_.GetPlugins(&plugins);
[email protected]b83ff222011-01-24 17:37:1267 EXPECT_EQ(2u, plugins.size());
[email protected]f7be2cba2011-09-15 15:33:4568 EXPECT_TRUE(Contains(plugins, foo_plugin_));
69 EXPECT_TRUE(Contains(plugins, bar_plugin_));
[email protected]b83ff222011-01-24 17:37:1270}
71
[email protected]cd91aff22011-01-25 22:47:0072TEST_F(PluginListTest, BadPluginDescription) {
[email protected]7a8edaf2011-11-28 20:58:4873 WebPluginInfo plugin_3043(
[email protected]82758352013-03-29 19:21:3174 base::string16(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")),
75 base::string16(), base::string16());
[email protected]cd91aff22011-01-25 22:47:0076 // Simulate loading of the plugins.
[email protected]8f3372122013-07-18 04:34:1477 plugin_list_.RegisterInternalPlugin(plugin_3043, false);
[email protected]cd91aff22011-01-25 22:47:0078 // Now we should have them in the state we specified above.
[email protected]68598072011-07-29 08:21:2879 plugin_list_.RefreshPlugins();
[email protected]cd91aff22011-01-25 22:47:0080 std::vector<WebPluginInfo> plugins;
pimane8c57ea2016-04-06 01:19:3681 plugin_list_.GetPlugins(&plugins);
[email protected]f7be2cba2011-09-15 15:33:4582 ASSERT_TRUE(Contains(plugins, plugin_3043));
[email protected]b83ff222011-01-24 17:37:1283}
84
[email protected]cafe0d02013-07-23 15:16:2085TEST_F(PluginListTest, GetPluginInfoArray) {
86 const char kTargetUrl[] = "http://example.com/test.foo";
87 GURL target_url(kTargetUrl);
88 std::vector<WebPluginInfo> plugins;
89 std::vector<std::string> actual_mime_types;
Lei Zhangc923379e2019-02-07 18:13:0890 bool is_stale;
[email protected]cafe0d02013-07-23 15:16:2091
Lei Zhang0960d562019-02-12 22:49:2392 // The PluginList starts out in a stale state.
93 is_stale = plugin_list_.GetPluginInfoArray(
94 target_url, "application/octet-stream",
95 /*allow_wildcard=*/false, &plugins, &actual_mime_types);
96 EXPECT_TRUE(is_stale);
97 EXPECT_EQ(0u, plugins.size());
98 EXPECT_EQ(0u, actual_mime_types.size());
99
100 // Refresh it.
101 plugin_list_.GetPlugins(&plugins);
102 plugins.clear();
103
Lei Zhangc923379e2019-02-07 18:13:08104 // The file type of the URL is supported by |foo_plugin_|. However,
105 // GetPluginInfoArray should not match |foo_plugin_| because the MIME type is
[email protected]cafe0d02013-07-23 15:16:20106 // application/octet-stream.
Lei Zhangc923379e2019-02-07 18:13:08107 is_stale = plugin_list_.GetPluginInfoArray(
108 target_url, "application/octet-stream",
109 /*allow_wildcard=*/false, &plugins, &actual_mime_types);
110 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20111 EXPECT_EQ(0u, plugins.size());
112 EXPECT_EQ(0u, actual_mime_types.size());
113
Lei Zhangc923379e2019-02-07 18:13:08114 // |foo_plugin_| matches due to the MIME type.
[email protected]cafe0d02013-07-23 15:16:20115 plugins.clear();
116 actual_mime_types.clear();
Lei Zhangc923379e2019-02-07 18:13:08117 is_stale = plugin_list_.GetPluginInfoArray(target_url, kFooMimeType,
118 /*allow_wildcard=*/false, &plugins,
119 &actual_mime_types);
120 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20121 EXPECT_EQ(1u, plugins.size());
122 EXPECT_TRUE(Contains(plugins, foo_plugin_));
123 ASSERT_EQ(1u, actual_mime_types.size());
124 EXPECT_EQ(kFooMimeType, actual_mime_types.front());
125
Lei Zhangc923379e2019-02-07 18:13:08126 // |foo_plugin_| matches due to the file type and empty MIME type.
[email protected]cafe0d02013-07-23 15:16:20127 plugins.clear();
128 actual_mime_types.clear();
Lei Zhangc923379e2019-02-07 18:13:08129 is_stale = plugin_list_.GetPluginInfoArray(target_url, "",
130 /*allow_wildcard=*/false, &plugins,
131 &actual_mime_types);
132 EXPECT_FALSE(is_stale);
[email protected]cafe0d02013-07-23 15:16:20133 EXPECT_EQ(1u, plugins.size());
134 EXPECT_TRUE(Contains(plugins, foo_plugin_));
135 ASSERT_EQ(1u, actual_mime_types.size());
136 EXPECT_EQ(kFooMimeType, actual_mime_types.front());
137}
138
[email protected]29e2fb42013-07-19 01:13:47139} // namespace content