Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: chrome/browser/extensions/extension_prefs_unittest.cc

Issue 4687005: Track permissions granted to extensions in prefs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: incorporate feedback Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/message_loop.h" 5 #include "base/message_loop.h"
6 #include "base/path_service.h" 6 #include "base/path_service.h"
7 #include "base/scoped_temp_dir.h" 7 #include "base/scoped_temp_dir.h"
8 #include "base/stl_util-inl.h" 8 #include "base/stl_util-inl.h"
9 #include "base/string_number_conversions.h" 9 #include "base/string_number_conversions.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "chrome/browser/browser_thread.h" 11 #include "chrome/browser/browser_thread.h"
12 #include "chrome/browser/extensions/extension_prefs.h" 12 #include "chrome/browser/extensions/extension_prefs.h"
13 #include "chrome/browser/extensions/test_extension_prefs.h" 13 #include "chrome/browser/extensions/test_extension_prefs.h"
14 #include "chrome/browser/prefs/pref_service.h" 14 #include "chrome/browser/prefs/pref_service.h"
15 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/extension_constants.h" 16 #include "chrome/common/extensions/extension_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 using base::Time; 19 using base::Time;
20 using base::TimeDelta; 20 using base::TimeDelta;
21 21
22 static void AddPattern(ExtensionExtent* extent, const std::string& pattern) {
23 int schemes = URLPattern::SCHEME_ALL;
24 extent->AddPattern(URLPattern(schemes, pattern));
25 }
26
27 static void AssertEqualExtents(ExtensionExtent* extent1,
28 ExtensionExtent* extent2) {
29 std::vector<URLPattern> patterns1 = extent1->patterns();
30 std::vector<URLPattern> patterns2 = extent2->patterns();
31 std::set<std::string> strings1;
32 EXPECT_EQ(patterns1.size(), patterns2.size());
33
34 for (size_t i = 0; i < patterns1.size(); ++i)
35 strings1.insert(patterns1.at(i).GetAsString());
36
37 std::set<std::string> strings2;
38 for (size_t i = 0; i < patterns2.size(); ++i)
39 strings2.insert(patterns2.at(i).GetAsString());
40
41 EXPECT_EQ(strings1, strings2);
42 }
43
22 // Base class for tests. 44 // Base class for tests.
23 class ExtensionPrefsTest : public testing::Test { 45 class ExtensionPrefsTest : public testing::Test {
24 public: 46 public:
25 ExtensionPrefsTest() {} 47 ExtensionPrefsTest() {}
26 48
27 // This function will get called once, and is the right place to do operations 49 // This function will get called once, and is the right place to do operations
28 // on ExtensionPrefs that write data. 50 // on ExtensionPrefs that write data.
29 virtual void Initialize() = 0; 51 virtual void Initialize() = 0;
30 52
31 // This function will be called twice - once while the original ExtensionPrefs 53 // This function will be called twice - once while the original ExtensionPrefs
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 160
139 virtual void Verify() { 161 virtual void Verify() {
140 EXPECT_EQ(true, prefs()->DidExtensionEscalatePermissions(extension->id())); 162 EXPECT_EQ(true, prefs()->DidExtensionEscalatePermissions(extension->id()));
141 } 163 }
142 164
143 private: 165 private:
144 scoped_refptr<Extension> extension; 166 scoped_refptr<Extension> extension;
145 }; 167 };
146 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {} 168 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {}
147 169
170 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
171 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest {
172 public:
173 virtual void Initialize() {
174 extension_id_ = prefs_.AddExtensionAndReturnId("test");
175
176 api_perm_set1_.insert("tabs");
177 api_perm_set1_.insert("bookmarks");
178 api_perm_set1_.insert("something_random");
179
180 api_perm_set2_.insert("history");
181 api_perm_set2_.insert("unknown2");
182
183 AddPattern(&host_perm_set1_, "http://*.google.com/*");
184 AddPattern(&host_perm_set1_, "http://example.com/*");
185
186 AddPattern(&host_perm_set2_, "https://*.google.com/*");
187 // with duplicate:
188 AddPattern(&host_perm_set2_, "http://*.google.com/*");
189
190 std::set_union(api_perm_set1_.begin(), api_perm_set1_.end(),
191 api_perm_set2_.begin(), api_perm_set2_.end(),
192 std::inserter(api_permissions_, api_permissions_.end()));
193
194 AddPattern(&host_permissions_, "http://*.google.com/*");
195 AddPattern(&host_permissions_, "http://example.com/*");
196 AddPattern(&host_permissions_, "https://*.google.com/*");
197
198 std::set<std::string> empty_set;
199 std::set<std::string> api_perms;
200 ExtensionExtent host_perms;
201 ExtensionExtent empty_extent;
202
203 // Make sure both granted api and host permissions start empty.
204 EXPECT_FALSE(prefs()->GetGrantedPermissions(extension_id_,
205 &api_perms,
206 &host_perms));
207
208 EXPECT_TRUE(api_perms.empty());
209 EXPECT_TRUE(host_perms.is_empty());
210
211
212 // Add part of the api permissions.
213 prefs()->AddGrantedPermissions(extension_id_, api_perm_set1_, empty_extent);
214 EXPECT_TRUE(prefs()->GetGrantedPermissions(extension_id_,
215 &api_perms,
216 &host_perms));
217 EXPECT_EQ(api_perm_set1_, api_perms);
218 EXPECT_TRUE(host_perms.is_empty());
219 host_perms.ClearPaths();
220 api_perms.clear();
221
222 // Add part of the host permissions.
223 prefs()->AddGrantedPermissions(extension_id_, empty_set, host_perm_set1_);
224 EXPECT_TRUE(prefs()->GetGrantedPermissions(extension_id_,
225 &api_perms,
226 &host_perms));
227 EXPECT_EQ(api_perm_set1_, api_perms);
228 AssertEqualExtents(&host_perm_set1_, &host_perms);
229 host_perms.ClearPaths();
230 api_perms.clear();
231
232 // Add the rest of both the api and host permissions.
233 prefs()->AddGrantedPermissions(extension_id_,
234 api_perm_set2_,
235 host_perm_set2_);
236
237 EXPECT_TRUE(prefs()->GetGrantedPermissions(extension_id_,
238 &api_perms,
239 &host_perms));
240 EXPECT_EQ(api_permissions_, api_perms);
241 AssertEqualExtents(&host_permissions_, &host_perms);
242 }
243
244 virtual void Verify() {
245 std::set<std::string> api_perms;
246 ExtensionExtent host_perms;
247
248 EXPECT_TRUE(prefs()->GetGrantedPermissions(extension_id_,
249 &api_perms,
250 &host_perms));
251 EXPECT_EQ(api_permissions_, api_perms);
252 AssertEqualExtents(&host_permissions_, &host_perms);
253 }
254
255 private:
256 std::string extension_id_;
257 std::set<std::string> api_perm_set1_;
258 std::set<std::string> api_perm_set2_;
259 ExtensionExtent host_perm_set1_;
260 ExtensionExtent host_perm_set2_;
261
262
263 std::set<std::string> api_permissions_;
264 ExtensionExtent host_permissions_;
265 };
266 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
148 267
149 // Tests the GetVersionString function. 268 // Tests the GetVersionString function.
150 class ExtensionPrefsVersionString : public ExtensionPrefsTest { 269 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
151 public: 270 public:
152 virtual void Initialize() { 271 virtual void Initialize() {
153 extension = prefs_.AddExtension("test"); 272 extension = prefs_.AddExtension("test");
154 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); 273 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
155 prefs()->OnExtensionUninstalled(extension->id(), 274 prefs()->OnExtensionUninstalled(extension->id(),
156 Extension::INTERNAL, false); 275 Extension::INTERNAL, false);
157 } 276 }
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 EXPECT_EQ(launch_index + 1, new_launch_index); 515 EXPECT_EQ(launch_index + 1, new_launch_index);
397 516
398 // This extension doesn't exist, so it should return -1. 517 // This extension doesn't exist, so it should return -1.
399 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); 518 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo"));
400 } 519 }
401 520
402 private: 521 private:
403 scoped_refptr<Extension> extension_; 522 scoped_refptr<Extension> extension_;
404 }; 523 };
405 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} 524 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698