Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lei Zhang | d8c5318 | 2019-02-06 22:24:39 | [diff] [blame] | 5 | #include "content/browser/plugin_list.h" |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 6 | |
Jan Wilken Dörrie | ad587c3 | 2021-03-11 14:09:27 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
Keishi Hattori | c1b0023 | 2022-11-22 09:04:26 | [diff] [blame] | 10 | #include "base/memory/raw_ptr.h" |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 11 | #include "content/public/test/browser_task_environment.h" |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 13 | #include "url/gurl.h" |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 14 | |
[email protected] | 29e2fb4 | 2013-07-19 01:13:47 | [diff] [blame] | 15 | namespace content { |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 16 | |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 17 | namespace { |
| 18 | |
[email protected] | 9a60ccb | 2013-07-19 22:23:36 | [diff] [blame] | 19 | base::FilePath::CharType kFooPath[] = FILE_PATH_LITERAL("/plugins/foo.plugin"); |
| 20 | base::FilePath::CharType kBarPath[] = FILE_PATH_LITERAL("/plugins/bar.plugin"); |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 21 | const char kFooMimeType[] = "application/x-foo-mime-type"; |
| 22 | const char kFooFileType[] = "foo"; |
[email protected] | 9a60ccb | 2013-07-19 22:23:36 | [diff] [blame] | 23 | |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 24 | bool Equals(const WebPluginInfo& a, const WebPluginInfo& b) { |
Lei Zhang | d8c5318 | 2019-02-06 22:24:39 | [diff] [blame] | 25 | return (a.name == b.name && a.path == b.path && a.version == b.version && |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 26 | a.desc == b.desc); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | bool Contains(const std::vector<WebPluginInfo>& list, |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 30 | const WebPluginInfo& plugin) { |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 31 | for (std::vector<WebPluginInfo>::const_iterator it = list.begin(); |
| 32 | it != list.end(); ++it) { |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 33 | if (Equals(*it, plugin)) { |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 34 | return true; |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 35 | } |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 36 | } |
| 37 | return false; |
| 38 | } |
| 39 | |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 40 | } // namespace |
| 41 | |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 42 | class PluginListTest : public testing::Test { |
| 43 | public: |
| 44 | PluginListTest() |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 45 | : plugin_list_(nullptr, PluginListDeleter), |
| 46 | foo_plugin_(u"Foo PluginListTest", |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 47 | base::FilePath(kFooPath), |
Jan Wilken Dörrie | 8aeb574 | 2021-03-23 19:27:02 | [diff] [blame] | 48 | 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] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 52 | |
dcheng | 6013533 | 2015-01-09 02:05:34 | [diff] [blame] | 53 | void SetUp() override { |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 54 | plugin_list_.reset(new PluginList()); |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 55 | plugin_list_->RegisterInternalPlugin(bar_plugin_, false); |
Lei Zhang | 9ffa3d1 | 2022-10-11 01:05:45 | [diff] [blame] | 56 | foo_plugin_.mime_types.emplace_back(kFooMimeType, kFooFileType, |
| 57 | std::string()); |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 58 | plugin_list_->RegisterInternalPlugin(foo_plugin_, false); |
| 59 | } |
| 60 | |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 61 | static void PluginListDeleter(PluginList* plugin_list) { delete plugin_list; } |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 62 | |
| 63 | protected: |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 64 | // Must be first. |
| 65 | BrowserTaskEnvironment task_environment_; |
| 66 | |
akshay | f97041c5 | 2023-10-23 13:00:04 | [diff] [blame] | 67 | std::unique_ptr<PluginList, decltype(&PluginListDeleter)> plugin_list_; |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 68 | WebPluginInfo foo_plugin_; |
| 69 | WebPluginInfo bar_plugin_; |
| 70 | }; |
| 71 | |
| 72 | TEST_F(PluginListTest, GetPlugins) { |
| 73 | std::vector<WebPluginInfo> plugins; |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 74 | plugin_list_->GetPlugins(&plugins); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 75 | EXPECT_EQ(2u, plugins.size()); |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 76 | EXPECT_TRUE(Contains(plugins, foo_plugin_)); |
| 77 | EXPECT_TRUE(Contains(plugins, bar_plugin_)); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 80 | TEST_F(PluginListTest, BadPluginDescription) { |
[email protected] | 7a8edaf | 2011-11-28 20:58:48 | [diff] [blame] | 81 | WebPluginInfo plugin_3043( |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 82 | std::u16string(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), |
| 83 | std::u16string(), std::u16string()); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 84 | // Simulate loading of the plugins. |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 85 | plugin_list_->RegisterInternalPlugin(plugin_3043, false); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 86 | // Now we should have them in the state we specified above. |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 87 | plugin_list_->RefreshPlugins(); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 88 | std::vector<WebPluginInfo> plugins; |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 89 | plugin_list_->GetPlugins(&plugins); |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 90 | ASSERT_TRUE(Contains(plugins, plugin_3043)); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 93 | TEST_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 Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 98 | bool is_stale; |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 99 | |
Lei Zhang | 0960d56 | 2019-02-12 22:49:23 | [diff] [blame] | 100 | // The PluginList starts out in a stale state. |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 101 | is_stale = plugin_list_->GetPluginInfoArray( |
Lei Zhang | 0960d56 | 2019-02-12 22:49:23 | [diff] [blame] | 102 | 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 Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 109 | plugin_list_->GetPlugins(&plugins); |
Lei Zhang | 0960d56 | 2019-02-12 22:49:23 | [diff] [blame] | 110 | plugins.clear(); |
| 111 | |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 112 | // 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] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 114 | // application/octet-stream. |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 115 | is_stale = plugin_list_->GetPluginInfoArray( |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 116 | target_url, "application/octet-stream", |
| 117 | /*allow_wildcard=*/false, &plugins, &actual_mime_types); |
| 118 | EXPECT_FALSE(is_stale); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 119 | EXPECT_EQ(0u, plugins.size()); |
| 120 | EXPECT_EQ(0u, actual_mime_types.size()); |
| 121 | |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 122 | // |foo_plugin_| matches due to the MIME type. |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 123 | plugins.clear(); |
| 124 | actual_mime_types.clear(); |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 125 | is_stale = plugin_list_->GetPluginInfoArray(target_url, kFooMimeType, |
| 126 | /*allow_wildcard=*/false, |
| 127 | &plugins, &actual_mime_types); |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 128 | EXPECT_FALSE(is_stale); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 129 | 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 Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 134 | // |foo_plugin_| matches due to the file type and empty MIME type. |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 135 | plugins.clear(); |
| 136 | actual_mime_types.clear(); |
Lei Zhang | 8403fa6 | 2022-10-12 20:15:26 | [diff] [blame] | 137 | is_stale = plugin_list_->GetPluginInfoArray(target_url, "", |
| 138 | /*allow_wildcard=*/false, |
| 139 | &plugins, &actual_mime_types); |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 140 | EXPECT_FALSE(is_stale); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 141 | 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] | 29e2fb4 | 2013-07-19 01:13:47 | [diff] [blame] | 147 | } // namespace content |