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

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: fix mac test failure Created 10 years 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_.begin()));
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 bool full_access = false;
201 ExtensionExtent host_perms;
202 ExtensionExtent empty_extent;
203
204 // Make sure both granted api and host permissions start empty.
205 EXPECT_FALSE(prefs()->GetGrantedPermissions(
206 extension_id_, &full_access, &api_perms, &host_perms));
207
208 EXPECT_TRUE(api_perms.empty());
209 EXPECT_TRUE(host_perms.is_empty());
210
211 // Add part of the api permissions.
212 prefs()->AddGrantedPermissions(
213 extension_id_, false, api_perm_set1_, empty_extent);
214 EXPECT_TRUE(prefs()->GetGrantedPermissions(
215 extension_id_, &full_access, &api_perms, &host_perms));
216 EXPECT_EQ(api_perm_set1_, api_perms);
217 EXPECT_TRUE(host_perms.is_empty());
218 EXPECT_FALSE(full_access);
219 host_perms.ClearPaths();
220 api_perms.clear();
221
222 // Add part of the host permissions.
223 prefs()->AddGrantedPermissions(
224 extension_id_, false, empty_set, host_perm_set1_);
225 EXPECT_TRUE(prefs()->GetGrantedPermissions(
226 extension_id_, &full_access, &api_perms, &host_perms));
227 EXPECT_FALSE(full_access);
228 EXPECT_EQ(api_perm_set1_, api_perms);
229 AssertEqualExtents(&host_perm_set1_, &host_perms);
230 host_perms.ClearPaths();
231 api_perms.clear();
232
233 // Add the rest of both the api and host permissions.
234 prefs()->AddGrantedPermissions(extension_id_,
235 true,
236 api_perm_set2_,
237 host_perm_set2_);
238
239 EXPECT_TRUE(prefs()->GetGrantedPermissions(
240 extension_id_, &full_access, &api_perms, &host_perms));
241 EXPECT_TRUE(full_access);
242 EXPECT_EQ(api_permissions_, api_perms);
243 AssertEqualExtents(&host_permissions_, &host_perms);
244 }
245
246 virtual void Verify() {
247 std::set<std::string> api_perms;
248 ExtensionExtent host_perms;
249 bool full_access;
250
251 EXPECT_TRUE(prefs()->GetGrantedPermissions(
252 extension_id_, &full_access, &api_perms, &host_perms));
253 EXPECT_EQ(api_permissions_, api_perms);
254 EXPECT_TRUE(full_access);
255 AssertEqualExtents(&host_permissions_, &host_perms);
256 }
257
258 private:
259 std::string extension_id_;
260 std::set<std::string> api_perm_set1_;
261 std::set<std::string> api_perm_set2_;
262 ExtensionExtent host_perm_set1_;
263 ExtensionExtent host_perm_set2_;
264
265
266 std::set<std::string> api_permissions_;
267 ExtensionExtent host_permissions_;
268 };
269 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
148 270
149 // Tests the GetVersionString function. 271 // Tests the GetVersionString function.
150 class ExtensionPrefsVersionString : public ExtensionPrefsTest { 272 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
151 public: 273 public:
152 virtual void Initialize() { 274 virtual void Initialize() {
153 extension = prefs_.AddExtension("test"); 275 extension = prefs_.AddExtension("test");
154 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); 276 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
155 prefs()->OnExtensionUninstalled(extension->id(), 277 prefs()->OnExtensionUninstalled(extension->id(),
156 Extension::INTERNAL, false); 278 Extension::INTERNAL, false);
157 } 279 }
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 EXPECT_EQ(launch_index + 1, new_launch_index); 518 EXPECT_EQ(launch_index + 1, new_launch_index);
397 519
398 // This extension doesn't exist, so it should return -1. 520 // This extension doesn't exist, so it should return -1.
399 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo")); 521 EXPECT_EQ(-1, prefs()->GetAppLaunchIndex("foo"));
400 } 522 }
401 523
402 private: 524 private:
403 scoped_refptr<Extension> extension_; 525 scoped_refptr<Extension> extension_;
404 }; 526 };
405 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {} 527 TEST_F(ExtensionPrefsAppLaunchIndex, ExtensionPrefsAppLaunchIndex) {}
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_prefs.cc ('k') | chrome/browser/extensions/extensions_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698