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