File: plugin_list_unittest.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,084 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/plugin_list.h"

#include <memory>
#include <string>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace content {

namespace {

base::FilePath::CharType kFooPath[] = FILE_PATH_LITERAL("/plugins/foo.plugin");
base::FilePath::CharType kBarPath[] = FILE_PATH_LITERAL("/plugins/bar.plugin");
const char kFooMimeType[] = "application/x-foo-mime-type";
const char kFooFileType[] = "foo";

bool Equals(const WebPluginInfo& a, const WebPluginInfo& b) {
  return (a.name == b.name && a.path == b.path && a.version == b.version &&
          a.desc == b.desc);
}

bool Contains(const std::vector<WebPluginInfo>& list,
              const WebPluginInfo& plugin) {
  for (std::vector<WebPluginInfo>::const_iterator it = list.begin();
       it != list.end(); ++it) {
    if (Equals(*it, plugin)) {
      return true;
    }
  }
  return false;
}

}  // namespace

class PluginListTest : public testing::Test {
 public:
  PluginListTest()
      : plugin_list_(nullptr, PluginListDeleter),
        foo_plugin_(u"Foo PluginListTest",
                    base::FilePath(kFooPath),
                    u"1.2.3",
                    u"foo"),
        bar_plugin_(u"Bar Plugin", base::FilePath(kBarPath), u"2.3.4", u"bar") {
  }

  void SetUp() override {
    // Needed because `PluginList` is normally a singleton and has a private
    // ctor. Also, `plugin_list_` uses a custom deleter.
    plugin_list_.reset(new PluginList());
    foo_plugin_.mime_types.emplace_back(kFooMimeType, kFooFileType,
                                        std::string());
    plugin_list_->RegisterInternalPlugin(foo_plugin_);
    plugin_list_->RegisterInternalPlugin(bar_plugin_);
  }

  // Needed because `PluginList` is normally a singleton and has a private dtor.
  static void PluginListDeleter(PluginList* plugin_list) { delete plugin_list; }

 protected:
  // Must be first.
  BrowserTaskEnvironment task_environment_;

  std::unique_ptr<PluginList, decltype(&PluginListDeleter)> plugin_list_;
  WebPluginInfo foo_plugin_;
  WebPluginInfo bar_plugin_;
};

TEST_F(PluginListTest, GetPlugins) {
  const std::vector<WebPluginInfo>& plugins = plugin_list_->GetPlugins();
  EXPECT_EQ(2u, plugins.size());
  EXPECT_TRUE(Contains(plugins, foo_plugin_));
  EXPECT_TRUE(Contains(plugins, bar_plugin_));
}

TEST_F(PluginListTest, BadPluginDescription) {
  WebPluginInfo plugin_3043(
      std::u16string(), base::FilePath(FILE_PATH_LITERAL("/myplugin.3.0.43")),
      std::u16string(), std::u16string());
  plugin_list_->RegisterInternalPlugin(plugin_3043);
  const std::vector<WebPluginInfo>& plugins = plugin_list_->GetPlugins();
  ASSERT_TRUE(Contains(plugins, plugin_3043));
}

TEST_F(PluginListTest, GetPluginInfoArray) {
  const char kTargetUrl[] = "http://example.com/test.foo";
  GURL target_url(kTargetUrl);
  std::vector<WebPluginInfo> plugins;
  std::vector<std::string> actual_mime_types;

  // Without a GetPlugins() call, the PluginList starts out in an empty state.
  plugin_list_->GetPluginInfoArray(target_url, "application/octet-stream",
                                   &plugins, &actual_mime_types);
  EXPECT_EQ(0u, plugins.size());
  EXPECT_EQ(0u, actual_mime_types.size());

  // Even with the correct MIME type, the empty state means there is no result.
  plugins.clear();
  actual_mime_types.clear();
  plugin_list_->GetPluginInfoArray(target_url, kFooMimeType, &plugins,
                                   &actual_mime_types);
  EXPECT_EQ(0u, plugins.size());
  EXPECT_EQ(0u, actual_mime_types.size());

  plugin_list_->GetPlugins();

  // The file type of the URL is supported by `foo_plugin_`. However,
  // GetPluginInfoArray should not match `foo_plugin_` because the MIME type is
  // application/octet-stream.
  plugin_list_->GetPluginInfoArray(target_url, "application/octet-stream",
                                   &plugins, &actual_mime_types);
  EXPECT_EQ(0u, plugins.size());
  EXPECT_EQ(0u, actual_mime_types.size());

  // `foo_plugin_` matches due to the MIME type.
  plugins.clear();
  actual_mime_types.clear();
  plugin_list_->GetPluginInfoArray(target_url, kFooMimeType, &plugins,
                                   &actual_mime_types);
  EXPECT_EQ(1u, plugins.size());
  EXPECT_TRUE(Contains(plugins, foo_plugin_));
  ASSERT_EQ(1u, actual_mime_types.size());
  EXPECT_EQ(kFooMimeType, actual_mime_types.front());

  // `foo_plugin_` matches due to the file type and empty MIME type.
  plugins.clear();
  actual_mime_types.clear();
  plugin_list_->GetPluginInfoArray(target_url, "", &plugins,
                                   &actual_mime_types);
  EXPECT_EQ(1u, plugins.size());
  EXPECT_TRUE(Contains(plugins, foo_plugin_));
  ASSERT_EQ(1u, actual_mime_types.size());
  EXPECT_EQ(kFooMimeType, actual_mime_types.front());
}

}  // namespace content