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

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

Powered by Google App Engine
This is Rietveld 408576698