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/glue/plugins/plugin_list.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace plugin_test_internal { |
| 11 |
| 12 // This class basically clones PluginList, but without any filesystem IO. There |
| 13 // is also no reason to use |lock_| (but it doesn't hurt either). |
| 14 class TestablePluginList : public NPAPI::PluginList { |
| 15 public: |
| 16 void LoadDummyPlugin(WebPluginInfo* plugin) { |
| 17 if (disabled_plugins_.count(plugin->path)) { |
| 18 plugin->enabled = false; |
| 19 } else { |
| 20 plugin->enabled = true; |
| 21 } |
| 22 AddToPluginGroups(*plugin); |
| 23 } |
| 24 |
| 25 void LoadPlugins(bool refresh) { |
| 26 if (plugins_loaded_ && !refresh) |
| 27 return; |
| 28 |
| 29 for (size_t i = 0; i < plugins_to_load_.size(); ++i) |
| 30 LoadDummyPlugin(&plugins_to_load_[i]); |
| 31 |
| 32 plugins_loaded_ = true; |
| 33 } |
| 34 |
| 35 void PostInitPluginGroup(PluginGroup* group) { |
| 36 group->plugin_list_ = this; |
| 37 } |
| 38 |
| 39 void CreatePluginGroup(const PluginGroupDefinition& definition) { |
| 40 PluginGroup* group = PluginGroup::FromPluginGroupDefinition(definition); |
| 41 PostInitPluginGroup(group); |
| 42 plugin_groups_.insert(std::make_pair(group->identifier(), group)); |
| 43 } |
| 44 |
| 45 std::vector<WebPluginInfo> plugins_to_load_; |
| 46 }; |
| 47 |
| 48 } // namespace plugin_test_internal |
| 49 |
| 50 namespace { |
| 51 |
| 52 bool equals(const WebPluginInfo& a, const WebPluginInfo& b, |
| 53 bool care_about_enabled_status) { |
| 54 return (a.name == b.name && |
| 55 a.path == b.path && |
| 56 a.version == b.version && |
| 57 a.desc == b.desc && |
| 58 (!care_about_enabled_status || a.enabled == b.enabled)); |
| 59 } |
| 60 |
| 61 bool contains(const std::vector<WebPluginInfo>& list, |
| 62 const WebPluginInfo& plugin, |
| 63 bool care_about_enabled_status) { |
| 64 for (size_t i = 0; i < list.size(); ++i) { |
| 65 if (equals(list[i], plugin, care_about_enabled_status)) |
| 66 return true; |
| 67 } |
| 68 return false; |
| 69 } |
| 70 |
| 71 } // namespace |
| 72 |
| 73 namespace NPAPI { |
| 74 |
| 75 class PluginListTest : public testing::Test { |
| 76 public: |
| 77 PluginListTest() |
| 78 : foo_plugin_(ASCIIToUTF16("Foo Plugin"), |
| 79 FilePath(FILE_PATH_LITERAL("/plugins/foo.plugin")), |
| 80 ASCIIToUTF16("1.2.3"), |
| 81 ASCIIToUTF16("foo")), |
| 82 bar_plugin_(ASCIIToUTF16("Bar Plugin"), |
| 83 FilePath(FILE_PATH_LITERAL("/plugins/bar.plugin")), |
| 84 ASCIIToUTF16("2.3.4"), |
| 85 ASCIIToUTF16("bar")) { |
| 86 } |
| 87 |
| 88 virtual void SetUp() { |
| 89 bar_plugin_.enabled = false; |
| 90 plugin_list_.DisablePlugin(bar_plugin_.path, false); |
| 91 plugin_list_.plugins_to_load_.push_back(foo_plugin_); |
| 92 plugin_list_.plugins_to_load_.push_back(bar_plugin_); |
| 93 } |
| 94 |
| 95 protected: |
| 96 plugin_test_internal::TestablePluginList plugin_list_; |
| 97 WebPluginInfo foo_plugin_; |
| 98 WebPluginInfo bar_plugin_; |
| 99 }; |
| 100 |
| 101 TEST_F(PluginListTest, GetPlugins) { |
| 102 std::vector<WebPluginInfo> plugins; |
| 103 plugin_list_.GetPlugins(false, &plugins); |
| 104 EXPECT_EQ(size_t(2), plugins.size()); |
| 105 EXPECT_TRUE(contains(plugins, foo_plugin_, true)); |
| 106 EXPECT_TRUE(contains(plugins, bar_plugin_, true)); |
| 107 } |
| 108 |
| 109 TEST_F(PluginListTest, GetEnabledPlugins) { |
| 110 std::vector<WebPluginInfo> plugins; |
| 111 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 112 EXPECT_EQ(size_t(1), plugins.size()); |
| 113 EXPECT_TRUE(contains(plugins, foo_plugin_, true)); |
| 114 } |
| 115 |
| 116 TEST_F(PluginListTest, GetPluginGroup) { |
| 117 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 118 EXPECT_EQ(foo_group->GetGroupName(), foo_plugin_.name); |
| 119 EXPECT_TRUE(foo_group->Enabled()); |
| 120 // The second request should return a pointer to the same instance. |
| 121 const PluginGroup* foo_group2 = plugin_list_.GetPluginGroup(foo_plugin_); |
| 122 EXPECT_EQ(foo_group, foo_group2); |
| 123 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 124 EXPECT_FALSE(bar_group->Enabled()); |
| 125 } |
| 126 |
| 127 TEST_F(PluginListTest, EnableDisablePlugin) { |
| 128 // Disable "foo" plugin. |
| 129 plugin_list_.DisablePlugin(foo_plugin_.path, false); |
| 130 std::vector<WebPluginInfo> plugins; |
| 131 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 132 EXPECT_FALSE(contains(plugins, foo_plugin_, false)); |
| 133 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 134 EXPECT_FALSE(foo_group->Enabled()); |
| 135 // Enable "bar" plugin. |
| 136 plugin_list_.EnablePlugin(bar_plugin_.path); |
| 137 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 138 EXPECT_TRUE(contains(plugins, bar_plugin_, false)); |
| 139 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 140 EXPECT_TRUE(bar_group->Enabled()); |
| 141 } |
| 142 |
| 143 TEST_F(PluginListTest, EnableGroup) { |
| 144 // Disable "foo" plugin group. |
| 145 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); |
| 146 EXPECT_TRUE(foo_group->Enabled()); |
| 147 EXPECT_TRUE(plugin_list_.EnableGroup(false, foo_group->GetGroupName())); |
| 148 EXPECT_FALSE(foo_group->Enabled()); |
| 149 std::vector<WebPluginInfo> plugins; |
| 150 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 151 EXPECT_FALSE(contains(plugins, foo_plugin_, false)); |
| 152 // Enable "bar" plugin group. |
| 153 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); |
| 154 EXPECT_FALSE(bar_group->Enabled()); |
| 155 plugin_list_.EnableGroup(true, bar_group->GetGroupName()); |
| 156 EXPECT_TRUE(bar_group->Enabled()); |
| 157 plugin_list_.GetEnabledPlugins(false, &plugins); |
| 158 EXPECT_TRUE(contains(plugins, bar_plugin_, false)); |
| 159 } |
| 160 |
| 161 TEST_F(PluginListTest, EmptyGroup) { |
| 162 std::vector<PluginGroup> groups; |
| 163 plugin_list_.GetPluginGroups(false, &groups); |
| 164 for (size_t i = 0; i < groups.size(); ++i) { |
| 165 EXPECT_GE(groups[i].GetPlugins().size(), size_t(1)); |
| 166 } |
| 167 } |
| 168 |
| 169 TEST_F(PluginListTest, DisableOutdated) { |
| 170 VersionRangeDefinition version_range[] = { |
| 171 { "0", "4", "3.0.44" }, |
| 172 { "4", "5", "" } |
| 173 }; |
| 174 PluginGroupDefinition plugin_def = { |
| 175 "myplugin-34", "MyPlugin 3/4", "MyPlugin", version_range, |
| 176 arraysize(version_range), "http://latest" }; |
| 177 WebPluginInfo plugin_3043(ASCIIToUTF16("MyPlugin"), |
| 178 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), |
| 179 ASCIIToUTF16("3.0.43"), |
| 180 ASCIIToUTF16("MyPlugin version 3.0.43")); |
| 181 WebPluginInfo plugin_3045(ASCIIToUTF16("MyPlugin"), |
| 182 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.45")), |
| 183 ASCIIToUTF16("3.0.45"), |
| 184 ASCIIToUTF16("MyPlugin version 3.0.45")); |
| 185 plugin_list_.plugins_to_load_.clear(); |
| 186 plugin_list_.plugins_to_load_.push_back(plugin_3043); |
| 187 plugin_list_.plugins_to_load_.push_back(plugin_3045); |
| 188 plugin_list_.CreatePluginGroup(plugin_def); |
| 189 const PluginGroup* group_3043 = plugin_list_.GetPluginGroup(plugin_3043); |
| 190 const PluginGroup* group_3045 = plugin_list_.GetPluginGroup(plugin_3045); |
| 191 EXPECT_EQ(group_3043, group_3045); |
| 192 EXPECT_EQ(plugin_3043.desc, group_3043->description()); |
| 193 EXPECT_TRUE(group_3043->IsVulnerable()); |
| 194 const_cast<PluginGroup*>(group_3043)->DisableOutdatedPlugins(); |
| 195 EXPECT_EQ(plugin_3045.desc, group_3043->description()); |
| 196 EXPECT_FALSE(group_3043->IsVulnerable()); |
| 197 } |
| 198 |
| 199 } // namespace NPAPI |
OLD | NEW |