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