Chromium Code Reviews| 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 void CreatePluginGroup(const PluginGroupDefinition& definition) { | |
| 20 PluginGroup* group = PluginGroup::FromPluginGroupDefinition(definition); | |
| 21 ProcessGroupAfterInitialize(group); | |
| 22 plugin_groups_.push_back(group); | |
| 23 } | |
| 24 | |
| 25 void LoadDummyPlugin(WebPluginInfo* plugin) { | |
| 26 // EnforceGroupPolicy will force the groups' enabled field to be correctly | |
| 27 // intialized. | |
| 28 AddToPluginGroups(*plugin, &plugin_groups_); | |
| 29 } | |
| 30 | |
| 31 // NPAPI::PluginList methods: | |
| 32 | |
| 33 virtual void LoadPlugins(bool refresh) { | |
| 34 if (plugins_loaded_ && !refresh) | |
| 35 return; | |
| 36 | |
| 37 for (size_t i = 0; i < plugins_to_load_.size(); ++i) | |
| 38 LoadDummyPlugin(&plugins_to_load_[i]); | |
|
Bernhard Bauer
2011/01/21 17:29:37
Can't you just directly call AddToPluginGroups?
pastarmovj
2011/01/24 10:47:31
Done.
| |
| 39 | |
| 40 plugins_loaded_ = true; | |
| 41 } | |
| 42 | |
| 43 virtual void ProcessGroupAfterInitialize(PluginGroup* group) { | |
| 44 } | |
| 45 | |
| 46 std::vector<WebPluginInfo> plugins_to_load_; | |
| 47 }; | |
| 48 | |
| 49 } // namespace plugin_test_internal | |
| 50 | |
| 51 namespace { | |
| 52 | |
| 53 bool Equals(const WebPluginInfo& a, const WebPluginInfo& b, | |
| 54 bool care_about_enabled_status) { | |
| 55 return (a.name == b.name && | |
| 56 a.path == b.path && | |
| 57 a.version == b.version && | |
| 58 a.desc == b.desc && | |
| 59 (!care_about_enabled_status || a.enabled == b.enabled)); | |
| 60 } | |
| 61 | |
| 62 bool Contains(const std::vector<WebPluginInfo>& list, | |
| 63 const WebPluginInfo& plugin, | |
| 64 bool care_about_enabled_status) { | |
| 65 for (std::vector<WebPluginInfo>::const_iterator it = list.begin(); | |
| 66 it != list.end(); ++it) { | |
| 67 if (Equals(*it, plugin, care_about_enabled_status)) | |
| 68 return true; | |
| 69 } | |
| 70 return false; | |
| 71 } | |
| 72 | |
| 73 } // namespace | |
| 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 = WebPluginInfo::USER_DISABLED_POLICY_UNMANAGED; | |
| 90 plugin_list_.DisablePlugin(bar_plugin_.path); | |
| 91 plugin_list_.plugins_to_load_.push_back(foo_plugin_); | |
| 92 plugin_list_.plugins_to_load_.push_back(bar_plugin_); | |
| 93 plugin_list_.LoadPlugins(true); | |
| 94 } | |
| 95 | |
| 96 protected: | |
| 97 plugin_test_internal::PluginListWithoutFileIO plugin_list_; | |
| 98 WebPluginInfo foo_plugin_; | |
| 99 WebPluginInfo bar_plugin_; | |
| 100 }; | |
| 101 | |
| 102 TEST_F(PluginListTest, GetPlugins) { | |
| 103 std::vector<WebPluginInfo> plugins; | |
| 104 plugin_list_.GetPlugins(false, &plugins); | |
| 105 EXPECT_EQ(2u, plugins.size()); | |
| 106 EXPECT_TRUE(Contains(plugins, foo_plugin_, true)); | |
| 107 EXPECT_TRUE(Contains(plugins, bar_plugin_, true)); | |
| 108 } | |
| 109 | |
| 110 TEST_F(PluginListTest, GetEnabledPlugins) { | |
| 111 std::vector<WebPluginInfo> plugins; | |
| 112 plugin_list_.GetEnabledPlugins(false, &plugins); | |
| 113 EXPECT_EQ(1u, plugins.size()); | |
| 114 EXPECT_TRUE(Contains(plugins, foo_plugin_, true)); | |
| 115 } | |
| 116 | |
| 117 TEST_F(PluginListTest, GetPluginGroup) { | |
| 118 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); | |
| 119 EXPECT_EQ(foo_group->GetGroupName(), foo_plugin_.name); | |
| 120 EXPECT_TRUE(foo_group->Enabled()); | |
| 121 // The second request should return a pointer to the same instance. | |
| 122 const PluginGroup* foo_group2 = plugin_list_.GetPluginGroup(foo_plugin_); | |
| 123 EXPECT_EQ(foo_group, foo_group2); | |
| 124 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); | |
| 125 EXPECT_FALSE(bar_group->Enabled()); | |
| 126 } | |
| 127 | |
| 128 TEST_F(PluginListTest, EnableDisablePlugin) { | |
| 129 // Disable "foo" plugin. | |
| 130 plugin_list_.DisablePlugin(foo_plugin_.path); | |
| 131 std::vector<WebPluginInfo> plugins; | |
| 132 plugin_list_.GetEnabledPlugins(false, &plugins); | |
| 133 EXPECT_FALSE(Contains(plugins, foo_plugin_, false)); | |
| 134 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); | |
| 135 EXPECT_FALSE(foo_group->Enabled()); | |
| 136 // Enable "bar" plugin. | |
| 137 plugin_list_.EnablePlugin(bar_plugin_.path); | |
| 138 plugin_list_.GetEnabledPlugins(false, &plugins); | |
| 139 EXPECT_TRUE(Contains(plugins, bar_plugin_, false)); | |
| 140 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); | |
| 141 EXPECT_TRUE(bar_group->Enabled()); | |
| 142 } | |
| 143 | |
| 144 TEST_F(PluginListTest, EnableGroup) { | |
| 145 // Disable "foo" plugin group. | |
| 146 const PluginGroup* foo_group = plugin_list_.GetPluginGroup(foo_plugin_); | |
| 147 EXPECT_TRUE(foo_group->Enabled()); | |
| 148 EXPECT_TRUE(plugin_list_.EnableGroup(false, foo_group->GetGroupName())); | |
| 149 EXPECT_FALSE(foo_group->Enabled()); | |
| 150 std::vector<WebPluginInfo> plugins; | |
| 151 plugin_list_.GetEnabledPlugins(false, &plugins); | |
| 152 EXPECT_EQ(0u, plugins.size()); | |
| 153 EXPECT_FALSE(Contains(plugins, foo_plugin_, false)); | |
| 154 // Enable "bar" plugin group. | |
| 155 const PluginGroup* bar_group = plugin_list_.GetPluginGroup(bar_plugin_); | |
| 156 EXPECT_FALSE(bar_group->Enabled()); | |
| 157 plugin_list_.EnableGroup(true, bar_group->GetGroupName()); | |
| 158 EXPECT_TRUE(bar_group->Enabled()); | |
| 159 plugin_list_.GetEnabledPlugins(false, &plugins); | |
| 160 EXPECT_TRUE(Contains(plugins, bar_plugin_, false)); | |
| 161 } | |
| 162 | |
| 163 TEST_F(PluginListTest, EmptyGroup) { | |
| 164 std::vector<PluginGroup> groups; | |
| 165 plugin_list_.GetPluginGroups(false, &groups); | |
| 166 for (size_t i = 0; i < groups.size(); ++i) { | |
| 167 EXPECT_GE(1U, groups[i].web_plugins_info().size()); | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 TEST_F(PluginListTest, DisableOutdated) { | |
| 172 VersionRangeDefinition version_range[] = { | |
| 173 { "0", "4", "3.0.44" }, | |
| 174 { "4", "5", "" } | |
| 175 }; | |
| 176 PluginGroupDefinition plugin_def = { | |
| 177 "myplugin-34", "MyPlugin 3/4", "MyPlugin", version_range, | |
| 178 arraysize(version_range), "http://latest" }; | |
| 179 WebPluginInfo plugin_3043(ASCIIToUTF16("MyPlugin"), | |
| 180 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")), | |
| 181 ASCIIToUTF16("3.0.43"), | |
| 182 ASCIIToUTF16("MyPlugin version 3.0.43")); | |
| 183 WebPluginInfo plugin_3045(ASCIIToUTF16("MyPlugin"), | |
| 184 FilePath(FILE_PATH_LITERAL("/myplugin.3.0.45")), | |
| 185 ASCIIToUTF16("3.0.45"), | |
| 186 ASCIIToUTF16("MyPlugin version 3.0.45")); | |
| 187 plugin_list_.plugins_to_load_.clear(); | |
| 188 plugin_list_.plugins_to_load_.push_back(plugin_3043); | |
| 189 plugin_list_.plugins_to_load_.push_back(plugin_3045); | |
| 190 plugin_list_.CreatePluginGroup(plugin_def); | |
| 191 plugin_list_.LoadPlugins(true); | |
| 192 const PluginGroup* group_3043 = plugin_list_.GetPluginGroup(plugin_3043); | |
| 193 const PluginGroup* group_3045 = plugin_list_.GetPluginGroup(plugin_3045); | |
| 194 EXPECT_EQ(group_3043, group_3045); | |
| 195 EXPECT_EQ(plugin_3043.desc, group_3043->description()); | |
| 196 EXPECT_TRUE(group_3043->IsVulnerable()); | |
| 197 const_cast<PluginGroup*>(group_3043)->DisableOutdatedPlugins(); | |
| 198 EXPECT_EQ(plugin_3045.desc, group_3043->description()); | |
| 199 EXPECT_FALSE(group_3043->IsVulnerable()); | |
| 200 } | |
| 201 | |
| 202 } // namespace npapi | |
| 203 } // namespace webkit | |
| OLD | NEW |