[email protected] | 4dd01a04 | 2012-03-26 18:03:26 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[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 | |
[email protected] | f63582d | 2013-06-11 22:52:54 | [diff] [blame] | 7 | #include "base/strings/string16.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 10 | #include "url/gurl.h" |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 11 | |
[email protected] | 29e2fb4 | 2013-07-19 01:13:47 | [diff] [blame] | 12 | namespace content { |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 13 | |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 14 | namespace { |
| 15 | |
[email protected] | 9a60ccb | 2013-07-19 22:23:36 | [diff] [blame] | 16 | base::FilePath::CharType kFooPath[] = FILE_PATH_LITERAL("/plugins/foo.plugin"); |
| 17 | base::FilePath::CharType kBarPath[] = FILE_PATH_LITERAL("/plugins/bar.plugin"); |
| 18 | const char* kFooName = "Foo Plugin"; |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 19 | const char* kFooMimeType = "application/x-foo-mime-type"; |
| 20 | const char* kFooFileType = "foo"; |
[email protected] | 9a60ccb | 2013-07-19 22:23:36 | [diff] [blame] | 21 | |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 22 | bool Equals(const WebPluginInfo& a, const WebPluginInfo& b) { |
Lei Zhang | d8c5318 | 2019-02-06 22:24:39 | [diff] [blame] | 23 | return (a.name == b.name && a.path == b.path && a.version == b.version && |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 24 | a.desc == b.desc); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | bool Contains(const std::vector<WebPluginInfo>& list, |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 28 | const WebPluginInfo& plugin) { |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 29 | for (std::vector<WebPluginInfo>::const_iterator it = list.begin(); |
| 30 | it != list.end(); ++it) { |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 31 | if (Equals(*it, plugin)) |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 32 | return true; |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 37 | } // namespace |
| 38 | |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 39 | class PluginListTest : public testing::Test { |
| 40 | public: |
| 41 | PluginListTest() |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 42 | : foo_plugin_(base::ASCIIToUTF16(kFooName), |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 43 | base::FilePath(kFooPath), |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 44 | base::ASCIIToUTF16("1.2.3"), |
| 45 | base::ASCIIToUTF16("foo")), |
| 46 | bar_plugin_(base::ASCIIToUTF16("Bar Plugin"), |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 47 | base::FilePath(kBarPath), |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 48 | base::ASCIIToUTF16("2.3.4"), |
Lei Zhang | d8c5318 | 2019-02-06 22:24:39 | [diff] [blame] | 49 | base::ASCIIToUTF16("bar")) {} |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 50 | |
dcheng | 6013533 | 2015-01-09 02:05:34 | [diff] [blame] | 51 | void SetUp() override { |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 52 | plugin_list_.RegisterInternalPlugin(bar_plugin_, false); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 53 | foo_plugin_.mime_types.push_back( |
| 54 | WebPluginMimeType(kFooMimeType, kFooFileType, std::string())); |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 55 | plugin_list_.RegisterInternalPlugin(foo_plugin_, false); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 59 | PluginList plugin_list_; |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 60 | WebPluginInfo foo_plugin_; |
| 61 | WebPluginInfo bar_plugin_; |
| 62 | }; |
| 63 | |
| 64 | TEST_F(PluginListTest, GetPlugins) { |
| 65 | std::vector<WebPluginInfo> plugins; |
piman | e8c57ea | 2016-04-06 01:19:36 | [diff] [blame] | 66 | plugin_list_.GetPlugins(&plugins); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 67 | EXPECT_EQ(2u, plugins.size()); |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 68 | EXPECT_TRUE(Contains(plugins, foo_plugin_)); |
| 69 | EXPECT_TRUE(Contains(plugins, bar_plugin_)); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 72 | TEST_F(PluginListTest, BadPluginDescription) { |
[email protected] | 7a8edaf | 2011-11-28 20:58:48 | [diff] [blame] | 73 | WebPluginInfo plugin_3043( |
[email protected] | 8275835 | 2013-03-29 19:21:31 | [diff] [blame] | 74 | base::string16(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), |
| 75 | base::string16(), base::string16()); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 76 | // Simulate loading of the plugins. |
[email protected] | 8f337212 | 2013-07-18 04:34:14 | [diff] [blame] | 77 | plugin_list_.RegisterInternalPlugin(plugin_3043, false); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 78 | // Now we should have them in the state we specified above. |
[email protected] | 6859807 | 2011-07-29 08:21:28 | [diff] [blame] | 79 | plugin_list_.RefreshPlugins(); |
[email protected] | cd91aff2 | 2011-01-25 22:47:00 | [diff] [blame] | 80 | std::vector<WebPluginInfo> plugins; |
piman | e8c57ea | 2016-04-06 01:19:36 | [diff] [blame] | 81 | plugin_list_.GetPlugins(&plugins); |
[email protected] | f7be2cba | 2011-09-15 15:33:45 | [diff] [blame] | 82 | ASSERT_TRUE(Contains(plugins, plugin_3043)); |
[email protected] | b83ff22 | 2011-01-24 17:37:12 | [diff] [blame] | 83 | } |
| 84 | |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 85 | TEST_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 Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 90 | bool is_stale; |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 91 | |
Lei Zhang | 0960d56 | 2019-02-12 22:49:23 | [diff] [blame] | 92 | // 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 Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 104 | // 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] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 106 | // application/octet-stream. |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 107 | 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] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 111 | EXPECT_EQ(0u, plugins.size()); |
| 112 | EXPECT_EQ(0u, actual_mime_types.size()); |
| 113 | |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 114 | // |foo_plugin_| matches due to the MIME type. |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 115 | plugins.clear(); |
| 116 | actual_mime_types.clear(); |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 117 | is_stale = plugin_list_.GetPluginInfoArray(target_url, kFooMimeType, |
| 118 | /*allow_wildcard=*/false, &plugins, |
| 119 | &actual_mime_types); |
| 120 | EXPECT_FALSE(is_stale); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 121 | 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 Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 126 | // |foo_plugin_| matches due to the file type and empty MIME type. |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 127 | plugins.clear(); |
| 128 | actual_mime_types.clear(); |
Lei Zhang | c923379e | 2019-02-07 18:13:08 | [diff] [blame] | 129 | is_stale = plugin_list_.GetPluginInfoArray(target_url, "", |
| 130 | /*allow_wildcard=*/false, &plugins, |
| 131 | &actual_mime_types); |
| 132 | EXPECT_FALSE(is_stale); |
[email protected] | cafe0d0 | 2013-07-23 15:16:20 | [diff] [blame] | 133 | 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] | 29e2fb4 | 2013-07-19 01:13:47 | [diff] [blame] | 139 | } // namespace content |