OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "webkit/plugins/npapi/plugin_list.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace webkit { |
| 11 namespace npapi { |
| 12 |
| 13 namespace plugin_test_internal { |
| 14 |
| 15 // A PluginList for tests that avoids file system IO. There is also no reason |
| 16 // to use |lock_| (but it doesn't hurt either). |
| 17 class PluginListWithoutFileIO : public PluginList { |
| 18 public: |
| 19 std::vector<WebPluginInfo> plugins_to_load_; |
| 20 |
| 21 private: |
| 22 // NPAPI::PluginList methods: |
| 23 virtual void LoadPluginsInternal(ScopedVector<PluginGroup>* plugin_groups) { |
| 24 for (size_t i = 0; i < plugins_to_load_.size(); ++i) |
| 25 AddToPluginGroups(plugins_to_load_[i], plugin_groups); |
| 26 |
| 27 return; |
| 28 } |
| 29 }; |
| 30 |
| 31 } // namespace plugin_test_internal |
| 32 |
| 33 namespace { |
| 34 |
| 35 bool Equals(const WebPluginInfo& a, const WebPluginInfo& b, |
| 36 bool care_about_enabled_status) { |
| 37 return (a.name == b.name && |
| 38 a.path == b.path && |
| 39 a.version == b.version && |
| 40 a.desc == b.desc && |
| 41 (!care_about_enabled_status || a.enabled == b.enabled)); |
| 42 } |
| 43 |
| 44 bool Contains(const std::vector<WebPluginInfo>& list, |
| 45 const WebPluginInfo& plugin, |
| 46 bool care_about_enabled_status) { |
| 47 for (std::vector<WebPluginInfo>::const_iterator it = list.begin(); |
| 48 it != list.end(); ++it) { |
| 49 if (Equals(*it, plugin, care_about_enabled_status)) |
| 50 return true; |
| 51 } |
| 52 return false; |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 class PluginListTest : public testing::Test { |
| 58 public: |
| 59 PluginListTest() |
| 60 : foo_plugin_(ASCIIToUTF16("Foo Plugin"), |
| 61 FilePath(FILE_PATH_LITERAL("/plugins/foo.plugin")), |
| 62 ASCIIToUTF16("1.2.3"), |
| 63 ASCIIToUTF16("foo")), |
| 64 bar_plugin_(ASCIIToUTF16("Bar Plugin"), |
| 65 FilePath(FILE_PATH_LITERAL("/plugins/bar.plugin")), |
| 66 ASCIIToUTF16("2.3.4"), |
| 67 ASCIIToUTF16("bar")) { |
| 68 } |
| 69 |
| 70 virtual void SetUp() { |
| 71 bar_plugin_.enabled = WebPluginInfo::USER_DISABLED_POLICY_UNMANAGED; |
| 72 plugin_list_.DisablePlugin(bar_plugin_.path); |
| 73 plugin_list_.plugins_to_load_.push_back(foo_plugin_); |
| 74 plugin_list_.plugins_to_load_.push_back(bar_plugin_); |
| 75 plugin_list_.LoadPlugins(true); |
| 76 } |
| 77 |
| 78 protected: |
| 79 plugin_test_internal::PluginListWithoutFileIO plugin_list_; |
| 80 WebPluginInfo foo_plugin_; |
| 81 WebPluginInfo bar_plugin_; |
| 82 }; |
| 83 |
| 84 TEST_F(PluginListTest, GetPlugins) { |
| 85 std::vector<WebPluginInfo> plugins; |
| 86 plugin_list_.GetPlugins(false, &plugins); |
| 87 EXPECT_EQ(2u, plugins.size()); |
| 88 EXPECT_TRUE(Contains(plugins, foo_plugin_, true)); |
| 89 EXPECT_TRUE(Contains(plugins, bar_plugin_, true)); |
| 90 } |
| 91 |
| 92 TEST_F(PluginListTest, GetEnabledPlugins) { |
| 93 std::vector<WebPluginInfo> plugins; |
| 94 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 95 EXPECT_EQ(1u, plugins.size()); |
| 96 EXPECT_TRUE(Contains(plugins, foo_plugin_, true)); |
| 97 } |
| 98 |
| 99 TEST_F(PluginListTest, GetPluginGroup) { |
| 100 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 101 EXPECT_EQ(foo_group->GetGroupName(), foo_plugin_.name); |
| 102 EXPECT_TRUE(foo_group->Enabled()); |
| 103 // The second request should return a pointer to the same instance. |
| 104 const PluginGroup* foo_group2 = plugin_list_.GetPluginGroup(foo_plugin_); |
| 105 EXPECT_EQ(foo_group, foo_group2); |
| 106 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 107 EXPECT_FALSE(bar_group->Enabled()); |
| 108 } |
| 109 |
| 110 TEST_F(PluginListTest, EnableDisablePlugin) { |
| 111 // Disable "foo" plugin. |
| 112 plugin_list_.DisablePlugin(foo_plugin_.path); |
| 113 std::vector<WebPluginInfo> plugins; |
| 114 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 115 EXPECT_FALSE(Contains(plugins, foo_plugin_, false)); |
| 116 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 117 EXPECT_FALSE(foo_group->Enabled()); |
| 118 // Enable "bar" plugin. |
| 119 plugin_list_.EnablePlugin(bar_plugin_.path); |
| 120 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 121 EXPECT_TRUE(Contains(plugins, bar_plugin_, false)); |
| 122 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 123 EXPECT_TRUE(bar_group->Enabled()); |
| 124 } |
| 125 |
| 126 TEST_F(PluginListTest, EnableGroup) { |
| 127 // Disable "foo" plugin group. |
| 128 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 129 EXPECT_TRUE(foo_group->Enabled()); |
| 130 EXPECT_TRUE(plugin_list_.EnableGroup(false, foo_group->GetGroupName())); |
| 131 EXPECT_FALSE(foo_group->Enabled()); |
| 132 std::vector<WebPluginInfo> plugins; |
| 133 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 134 EXPECT_EQ(0u, plugins.size()); |
| 135 EXPECT_FALSE(Contains(plugins, foo_plugin_, false)); |
| 136 // Enable "bar" plugin group. |
| 137 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 138 EXPECT_FALSE(bar_group->Enabled()); |
| 139 plugin_list_.EnableGroup(true, bar_group->GetGroupName()); |
| 140 EXPECT_TRUE(bar_group->Enabled()); |
| 141 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 142 EXPECT_TRUE(Contains(plugins, bar_plugin_, false)); |
| 143 } |
| 144 |
| 145 TEST_F(PluginListTest, EmptyGroup) { |
| 146 std::vector<PluginGroup> groups; |
| 147 plugin_list_.GetPluginGroups(false, &groups); |
| 148 for (size_t i = 0; i < groups.size(); ++i) { |
| 149 EXPECT_GE(1U, groups[i].web_plugins_info().size()); |
| 150 } |
| 151 } |
| 152 |
| 153 TEST_F(PluginListTest, DisableOutdated) { |
| 154 VersionRangeDefinition version_range[] = { |
| 155 { "0", "4", "3.0.44" }, |
| 156 { "4", "5", "" } |
| 157 }; |
| 158 WebPluginInfo plugin_3043(ASCIIToUTF16("MyPlugin"), |
| 159 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), |
| 160 ASCIIToUTF16("3.0.43"), |
| 161 ASCIIToUTF16("MyPlugin version 3.0.43")); |
| 162 WebPluginInfo plugin_3045(ASCIIToUTF16("MyPlugin"), |
| 163 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.45")), |
| 164 ASCIIToUTF16("3.0.45"), |
| 165 ASCIIToUTF16("MyPlugin version 3.0.45")); |
| 166 plugin_list_.plugins_to_load_.clear(); |
| 167 plugin_list_.plugins_to_load_.push_back(plugin_3043); |
| 168 plugin_list_.plugins_to_load_.push_back(plugin_3045); |
| 169 // Enfore the load to run. |
| 170 std::vector<WebPluginInfo> plugins; |
| 171 plugin_list_.GetPlugins(true, &plugins); |
| 172 PluginGroup* group_3043 = |
| 173 const_cast<PluginGroup*>(plugin_list_.GetPluginGroup(plugin_3043)); |
| 174 const PluginGroup* group_3045 = plugin_list_.GetPluginGroup(plugin_3045); |
| 175 EXPECT_EQ(group_3043, group_3045); |
| 176 group_3043->version_ranges_.push_back(VersionRange(version_range[0])); |
| 177 group_3043->version_ranges_.push_back(VersionRange(version_range[1])); |
| 178 EXPECT_EQ(plugin_3043.desc, group_3043->description()); |
| 179 EXPECT_TRUE(group_3043->IsVulnerable()); |
| 180 group_3043->DisableOutdatedPlugins(); |
| 181 EXPECT_EQ(plugin_3045.desc, group_3043->description()); |
| 182 EXPECT_FALSE(group_3043->IsVulnerable()); |
| 183 } |
| 184 |
| 185 TEST_F(PluginListTest, DisableAndEnableBeforeLoad) { |
| 186 WebPluginInfo plugin_3043(ASCIIToUTF16("MyPlugin"), |
| 187 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), |
| 188 ASCIIToUTF16("3.0.43"), |
| 189 ASCIIToUTF16("MyPlugin version 3.0.43")); |
| 190 WebPluginInfo plugin_3045(ASCIIToUTF16("MyPlugin"), |
| 191 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.45")), |
| 192 ASCIIToUTF16("3.0.45"), |
| 193 ASCIIToUTF16("MyPlugin version 3.0.45")); |
| 194 // Disable the first one and disable and then enable the second one. |
| 195 EXPECT_TRUE(plugin_list_.DisablePlugin(plugin_3043.path)); |
| 196 EXPECT_TRUE(plugin_list_.DisablePlugin(plugin_3045.path)); |
| 197 EXPECT_TRUE(plugin_list_.EnablePlugin(plugin_3045.path)); |
| 198 // Simulate loading of the plugins. |
| 199 plugin_list_.plugins_to_load_.clear(); |
| 200 plugin_list_.plugins_to_load_.push_back(plugin_3043); |
| 201 plugin_list_.plugins_to_load_.push_back(plugin_3045); |
| 202 // Now we should have them in the state we specified above. |
| 203 std::vector<WebPluginInfo> plugins; |
| 204 plugin_list_.GetPlugins(true, &plugins); |
| 205 plugin_3043.enabled = WebPluginInfo::USER_DISABLED_POLICY_UNMANAGED; |
| 206 ASSERT_TRUE(Contains(plugins, plugin_3043, true)); |
| 207 ASSERT_TRUE(Contains(plugins, plugin_3045, true)); |
| 208 } |
| 209 |
| 210 } // namespace npapi |
| 211 } // namespace webkit |
OLD | NEW |