1// Copyright (c) 2011 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 "chrome/browser/chromeos/plugin_selection_policy.h"
6
7#include <string>
8#include <vector>
9
10#include "base/file_path.h"
11#include "base/file_util.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_temp_dir.h"
14#include "content/browser/browser_thread.h"
15#include "googleurl/src/gurl.h"
16#include "testing/gtest/include/gtest/gtest.h"
17#include "testing/platform_test.h"
18
19using std::string;
20using std::vector;
21
22#if !defined(OS_CHROMEOS)
23#error This file is meant to be compiled on ChromeOS only.
24#endif
25
26namespace chromeos {
27
28const char kBasicPolicy[] = "# This is a basic policy\n"
29                            "plugin test.so\n"
30                            "allow foo.com\n"
31                            "deny bar.com\n";
32
33const char kNoPluginPolicy[] = "# This is a policy with missing plugin.\n"
34                               "# Missing plugin test.so\n"
35                               "allow foo.com\n"
36                               "deny bar.com\n";
37const char kNoRulesPolicy[] = "# This is a policy with no rules\n"
38                              "plugin test.so\n";
39
40const char kEmptyPolicy[] = "# This is an empty policy\n";
41
42const char kGlobalPolicy[] = "# This is a test with global deny/allow\n"
43                             "plugin test.so\n"
44                             "deny\n"
45                             "plugin test1.so\n"
46                             "allow\n";
47
48const char kCommentTestPolicy[] = "# This is a policy with inline comments.\n"
49                                  "plugin test.so# like this\n"
50                                  "allow foo.com # and this\n"
51                                  "deny bar.com  # and this\n";
52
53const char kMultiPluginTestPolicy[] =
54    "# This is a policy with multiple plugins.\n"
55    "plugin allow_foo.so\n"
56    "allow foo.com\n"
57    "deny bar.com\n"
58    "plugin allow_baz_bim1.so\n"
59    "deny google.com\n"
60    "allow baz.com\n"
61    "allow bim.com\n"
62    "plugin allow_baz_bim2.so\n"
63    "deny google.com\n"
64    "allow baz.com\n"
65    "allow bim.com\n";
66
67const char kWhitespaceTestPolicy[] = "# This is a policy with odd whitespace.\n"
68                                     "  plugin\ttest.so# like this\n"
69                                     "\n\n \n   allow\t\tfoo.com # and this\n"
70                                     "\tdeny     bar.com\t\t\t# and this    \n";
71
72class PluginSelectionPolicyTest : public PlatformTest {
73 public:
74  PluginSelectionPolicyTest()
75      : loop_(MessageLoop::TYPE_DEFAULT),
76        file_thread_(BrowserThread::FILE, &loop_) {}
77
78  virtual void SetUp() {
79    PlatformTest::SetUp();
80    // Create a policy file to test with.
81    ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
82  }
83
84 protected:
85  bool CreatePolicy(const std::string& name,
86                    const std::string& contents,
87                    FilePath* path) {
88    FilePath policy_file = GetPolicyPath(name);
89    size_t bytes_written = file_util::WriteFile(policy_file,
90                                                contents.c_str(),
91                                                contents.size());
92    if (path)
93      *path = policy_file;
94
95    return bytes_written == contents.size();
96  }
97
98  FilePath GetPolicyPath(const std::string& name) {
99    FilePath policy_file(temp_dir_.path());
100    return policy_file.Append(FilePath(name));
101  }
102
103 private:
104  ScopedTempDir temp_dir_;
105  MessageLoop loop_;
106  BrowserThread file_thread_;
107};
108
109TEST_F(PluginSelectionPolicyTest, Basic) {
110  FilePath path;
111  ASSERT_TRUE(CreatePolicy("basic", kBasicPolicy, &path));
112  scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
113  EXPECT_TRUE(policy->InitFromFile(path));
114}
115
116TEST_F(PluginSelectionPolicyTest, InitFromFile) {
117  {
118    FilePath path;
119    ASSERT_TRUE(CreatePolicy("basic", kBasicPolicy, &path));
120    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
121    EXPECT_TRUE(policy->InitFromFile(path));
122  }
123
124  {
125    FilePath path;
126    ASSERT_TRUE(CreatePolicy("no_plugin", kNoPluginPolicy, &path));
127    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
128    EXPECT_FALSE(policy->InitFromFile(path));
129  }
130
131  {
132    FilePath path;
133    ASSERT_TRUE(CreatePolicy("no_rules", kNoRulesPolicy, &path));
134    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
135    EXPECT_TRUE(policy->InitFromFile(path));
136  }
137
138  {
139    FilePath path;
140    ASSERT_TRUE(CreatePolicy("empty", kEmptyPolicy, &path));
141    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
142    EXPECT_TRUE(policy->InitFromFile(path));
143  }
144
145  {
146    FilePath path;
147    ASSERT_TRUE(CreatePolicy("global", kGlobalPolicy, &path));
148    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
149    EXPECT_TRUE(policy->InitFromFile(path));
150  }
151
152  {
153    FilePath path;
154    ASSERT_TRUE(CreatePolicy("comment", kCommentTestPolicy, &path));
155    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
156    EXPECT_TRUE(policy->InitFromFile(path));
157  }
158
159  {
160    FilePath path;
161    ASSERT_TRUE(CreatePolicy("multi_plugin", kMultiPluginTestPolicy, &path));
162    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
163    EXPECT_TRUE(policy->InitFromFile(path));
164  }
165
166  {
167    FilePath path;
168    ASSERT_TRUE(CreatePolicy("whitespace", kWhitespaceTestPolicy, &path));
169    scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
170    EXPECT_TRUE(policy->InitFromFile(path));
171  }
172}
173
174TEST_F(PluginSelectionPolicyTest, IsAllowed) {
175  FilePath path;
176  ASSERT_TRUE(CreatePolicy("basic", kBasicPolicy, &path));
177
178  scoped_refptr<PluginSelectionPolicy> policy1 = new PluginSelectionPolicy;
179  ASSERT_TRUE(policy1->InitFromFile(path));
180  EXPECT_TRUE(policy1->IsAllowed(GURL("http://www.foo.com/blah.html"),
181                                 FilePath("/usr/local/bin/test.so")));
182  EXPECT_FALSE(policy1->IsAllowed(GURL("http://www.bar.com/blah.html"),
183                                  FilePath("/usr/local/bin/test.so")));
184  EXPECT_FALSE(policy1->IsAllowed(GURL("http://www.baz.com/blah.html"),
185                                  FilePath("/usr/local/bin/test.so")));
186  EXPECT_TRUE(policy1->IsAllowed(GURL("http://www.baz.com/blah.html"),
187                                 FilePath("/usr/local/bin/real.so")));
188
189  scoped_refptr<PluginSelectionPolicy> policy2 = new PluginSelectionPolicy;
190  ASSERT_TRUE(CreatePolicy("no_rules", kNoRulesPolicy, &path));
191  ASSERT_TRUE(policy2->InitFromFile(path));
192  EXPECT_FALSE(policy2->IsAllowed(GURL("http://www.foo.com/blah.html"),
193                                  FilePath("/usr/local/bin/test.so")));
194  EXPECT_FALSE(policy2->IsAllowed(GURL("http://www.bar.com/blah.html"),
195                                  FilePath("/usr/local/bin/test.so")));
196  EXPECT_FALSE(policy2->IsAllowed(GURL("http://www.baz.com/blah.html"),
197                                  FilePath("/usr/local/bin/test.so")));
198  EXPECT_TRUE(policy2->IsAllowed(GURL("http://www.baz.com/blah.html"),
199                                 FilePath("/usr/local/bin/real.so")));
200
201  scoped_refptr<PluginSelectionPolicy> policy3 = new PluginSelectionPolicy;
202  ASSERT_TRUE(CreatePolicy("empty", kEmptyPolicy, &path));
203  ASSERT_TRUE(policy3->InitFromFile(path));
204  EXPECT_TRUE(policy3->IsAllowed(GURL("http://www.foo.com/blah.html"),
205                                 FilePath("/usr/local/bin/test.so")));
206  EXPECT_TRUE(policy3->IsAllowed(GURL("http://www.bar.com/blah.html"),
207                                 FilePath("/usr/local/bin/test.so")));
208  EXPECT_TRUE(policy3->IsAllowed(GURL("http://www.baz.com/blah.html"),
209                                 FilePath("/usr/local/bin/test.so")));
210  EXPECT_TRUE(policy3->IsAllowed(GURL("http://www.baz.com/blah.html"),
211                                 FilePath("/usr/local/bin/real.so")));
212}
213
214TEST_F(PluginSelectionPolicyTest, MissingFile) {
215  // Don't create any policy file, just get the path to one.
216  FilePath path = GetPolicyPath("missing_file");
217  scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
218  ASSERT_FALSE(policy->InitFromFile(path));
219  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.foo.com/blah.html"),
220                                FilePath("/usr/local/bin/allow_foo.so")));
221  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.bar.com/blah.html"),
222                                FilePath("/usr/local/bin/allow_foo.so")));
223}
224
225TEST_F(PluginSelectionPolicyTest, FindFirstAllowed) {
226  FilePath path;
227  ASSERT_TRUE(CreatePolicy("multi", kMultiPluginTestPolicy, &path));
228  scoped_refptr<PluginSelectionPolicy> policy = new PluginSelectionPolicy;
229  ASSERT_TRUE(policy->InitFromFile(path));
230  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.foo.com/blah.html"),
231                                FilePath("/usr/local/bin/allow_foo.so")));
232  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.bar.com/blah.html"),
233                                 FilePath("/usr/local/bin/allow_foo.so")));
234  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.baz.com/blah.html"),
235                                 FilePath("/usr/local/bin/allow_foo.so")));
236  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.bim.com/blah.html"),
237                                 FilePath("/usr/local/bin/allow_foo.so")));
238  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.foo.com/blah.html"),
239                                 FilePath("/usr/local/bin/allow_baz_bim1.so")));
240  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.bar.com/blah.html"),
241                                 FilePath("/usr/local/bin/allow_baz_bim1.so")));
242  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.baz.com/blah.html"),
243                                FilePath("/usr/local/bin/allow_baz_bim1.so")));
244  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.bim.com/blah.html"),
245                                FilePath("/usr/local/bin/allow_baz_bim1.so")));
246  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.google.com/blah.html"),
247                                 FilePath("/usr/local/bin/allow_baz_bim1.so")));
248  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.foo.com/blah.html"),
249                                 FilePath("/usr/local/bin/allow_baz_bim2.so")));
250  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.bar.com/blah.html"),
251                                 FilePath("/usr/local/bin/allow_baz_bim2.so")));
252  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.baz.com/blah.html"),
253                                FilePath("/usr/local/bin/allow_baz_bim2.so")));
254  EXPECT_TRUE(policy->IsAllowed(GURL("http://www.bim.com/blah.html"),
255                                FilePath("/usr/local/bin/allow_baz_bim2.so")));
256  EXPECT_FALSE(policy->IsAllowed(GURL("http://www.google.com/blah.html"),
257                                 FilePath("/usr/local/bin/allow_baz_bim2.so")));
258  std::vector<webkit::npapi::WebPluginInfo> info_vector;
259  webkit::npapi::WebPluginInfo info;
260  // First we test that the one without any policy gets
261  // selected for all if it's first.
262  info.path = FilePath("/usr/local/bin/no_policy.so");
263  info_vector.push_back(info);
264  info.path = FilePath("/usr/local/bin/allow_foo.so");
265  info_vector.push_back(info);
266  info.path = FilePath("/usr/local/bin/allow_baz_bim1.so");
267  info_vector.push_back(info);
268  info.path = FilePath("/usr/local/bin/allow_baz_bim2.so");
269  info_vector.push_back(info);
270  EXPECT_EQ(0, policy->FindFirstAllowed(GURL("http://www.baz.com/blah.html"),
271                                        info_vector));
272  EXPECT_EQ(0, policy->FindFirstAllowed(GURL("http://www.foo.com/blah.html"),
273                                        info_vector));
274  EXPECT_EQ(0, policy->FindFirstAllowed(GURL("http://www.bling.com/blah.html"),
275                                        info_vector));
276  EXPECT_EQ(0, policy->FindFirstAllowed(GURL("http://www.google.com/blah.html"),
277                                        info_vector));
278
279  // Now move the plugin without any policy to the end.
280  info_vector.clear();
281  info.path = FilePath("/usr/local/bin/allow_foo.so");
282  info_vector.push_back(info);
283  info.path = FilePath("/usr/local/bin/allow_baz_bim1.so");
284  info_vector.push_back(info);
285  info.path = FilePath("/usr/local/bin/allow_baz_bim2.so");
286  info_vector.push_back(info);
287  info.path = FilePath("/usr/local/bin/no_policy.so");
288  info_vector.push_back(info);
289  EXPECT_EQ(1, policy->FindFirstAllowed(GURL("http://www.baz.com/blah.html"),
290                                        info_vector));
291  EXPECT_EQ(0, policy->FindFirstAllowed(GURL("http://www.foo.com/blah.html"),
292                                        info_vector));
293  EXPECT_EQ(3, policy->FindFirstAllowed(GURL("http://www.bling.com/blah.html"),
294                                        info_vector));
295  EXPECT_EQ(3, policy->FindFirstAllowed(GURL("http://www.google.com/blah.html"),
296                                        info_vector));
297}
298
299}  // namespace chromeos
300