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

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

Issue 7003098: Start refractoring extension permissions into ExtensionPermissionSet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup and update ExtensionPermissionSet data model. Created 9 years, 6 months 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) 2011 The Chromium Authors. All rights reserved. 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 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/extensions/extension_prefs.h" 11 #include "chrome/browser/extensions/extension_prefs.h"
12 #include "chrome/browser/extensions/test_extension_prefs.h" 12 #include "chrome/browser/extensions/test_extension_prefs.h"
13 #include "chrome/browser/prefs/pref_change_registrar.h" 13 #include "chrome/browser/prefs/pref_change_registrar.h"
14 #include "chrome/browser/prefs/scoped_user_pref_update.h" 14 #include "chrome/browser/prefs/scoped_user_pref_update.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 "chrome/common/extensions/extension_permission_set.h"
17 #include "content/browser/browser_thread.h" 18 #include "content/browser/browser_thread.h"
18 #include "content/common/notification_details.h" 19 #include "content/common/notification_details.h"
19 #include "content/common/notification_observer_mock.h" 20 #include "content/common/notification_observer_mock.h"
20 #include "content/common/notification_source.h" 21 #include "content/common/notification_source.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 using base::Time; 24 using base::Time;
24 using base::TimeDelta; 25 using base::TimeDelta;
25 26
26 namespace { 27 namespace {
27 28
28 const char kPref1[] = "path1.subpath"; 29 const char kPref1[] = "path1.subpath";
29 const char kPref2[] = "path2"; 30 const char kPref2[] = "path2";
30 const char kPref3[] = "path3"; 31 const char kPref3[] = "path3";
31 const char kPref4[] = "path4"; 32 const char kPref4[] = "path4";
32 33
33 // Default values in case an extension pref value is not overridden. 34 // Default values in case an extension pref value is not overridden.
34 const char kDefaultPref1[] = "default pref 1"; 35 const char kDefaultPref1[] = "default pref 1";
35 const char kDefaultPref2[] = "default pref 2"; 36 const char kDefaultPref2[] = "default pref 2";
36 const char kDefaultPref3[] = "default pref 3"; 37 const char kDefaultPref3[] = "default pref 3";
37 const char kDefaultPref4[] = "default pref 4"; 38 const char kDefaultPref4[] = "default pref 4";
38 39
39 } // namespace 40 } // namespace
40 41
41 static void AddPattern(URLPatternSet* extent, const std::string& pattern) { 42 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
42 int schemes = URLPattern::SCHEME_ALL; 43 int schemes = URLPattern::SCHEME_ALL;
43 extent->AddPattern(URLPattern(schemes, pattern)); 44 extent->AddPattern(URLPattern(schemes, pattern));
44 } 45 }
45 46
46 static void AssertEqualExtents(URLPatternSet* extent1, 47 static void AssertEqualExtents(const URLPatternSet& extent1,
47 URLPatternSet* extent2) { 48 const URLPatternSet& extent2) {
48 URLPatternList patterns1 = extent1->patterns(); 49 URLPatternList patterns1 = extent1.patterns();
49 URLPatternList patterns2 = extent2->patterns(); 50 URLPatternList patterns2 = extent2.patterns();
50 std::set<std::string> strings1;
51 EXPECT_EQ(patterns1.size(), patterns2.size()); 51 EXPECT_EQ(patterns1.size(), patterns2.size());
52 52
53 std::set<std::string> strings1;
53 for (size_t i = 0; i < patterns1.size(); ++i) 54 for (size_t i = 0; i < patterns1.size(); ++i)
54 strings1.insert(patterns1.at(i).GetAsString()); 55 strings1.insert(patterns1.at(i).GetAsString());
55 56
56 std::set<std::string> strings2; 57 std::set<std::string> strings2;
57 for (size_t i = 0; i < patterns2.size(); ++i) 58 for (size_t i = 0; i < patterns2.size(); ++i)
58 strings2.insert(patterns2.at(i).GetAsString()); 59 strings2.insert(patterns2.at(i).GetAsString());
59 60
60 EXPECT_EQ(strings1, strings2); 61 EXPECT_EQ(strings1, strings2);
61 } 62 }
62 63
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 scoped_refptr<Extension> extension; 198 scoped_refptr<Extension> extension;
198 }; 199 };
199 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {} 200 TEST_F(ExtensionPrefsEscalatePermissions, EscalatePermissions) {}
200 201
201 // Tests the AddGrantedPermissions / GetGrantedPermissions functions. 202 // Tests the AddGrantedPermissions / GetGrantedPermissions functions.
202 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest { 203 class ExtensionPrefsGrantedPermissions : public ExtensionPrefsTest {
203 public: 204 public:
204 virtual void Initialize() { 205 virtual void Initialize() {
205 extension_id_ = prefs_.AddExtensionAndReturnId("test"); 206 extension_id_ = prefs_.AddExtensionAndReturnId("test");
206 207
207 api_perm_set1_.insert("tabs"); 208 api_perm_set1_.insert(ExtensionAPIPermission::kTab);
208 api_perm_set1_.insert("bookmarks"); 209 api_perm_set1_.insert(ExtensionAPIPermission::kBookmark);
209 api_perm_set1_.insert("something_random");
210 210
211 api_perm_set2_.insert("history"); 211 api_perm_set2_.insert(ExtensionAPIPermission::kHistory);
212 api_perm_set2_.insert("unknown2");
213 212
214 AddPattern(&host_perm_set1_, "http://*.google.com/*"); 213 AddPattern(&ehost_perm_set1_, "http://*.google.com/*");
215 AddPattern(&host_perm_set1_, "http://example.com/*"); 214 AddPattern(&ehost_perm_set1_, "http://example.com/*");
216 AddPattern(&host_perm_set1_, "chrome://favicon/*"); 215 AddPattern(&ehost_perm_set1_, "chrome://favicon/*");
217 216
218 AddPattern(&host_perm_set2_, "https://*.google.com/*"); 217 AddPattern(&ehost_perm_set2_, "https://*.google.com/*");
219 // with duplicate: 218 // with duplicate:
220 AddPattern(&host_perm_set2_, "http://*.google.com/*"); 219 AddPattern(&ehost_perm_set2_, "http://*.google.com/*");
221 220
222 std::set_union(api_perm_set1_.begin(), api_perm_set1_.end(), 221 AddPattern(&shost_perm_set1_, "http://reddit.com/r/test/*");
222 AddPattern(&shost_perm_set2_, "http://reddit.com/r/test/*");
223 AddPattern(&shost_perm_set2_, "http://somesite.com/*");
224 AddPattern(&shost_perm_set2_, "http://example.com/*");
225
226 ExtensionAPIPermissionSet expected_apis = api_perm_set1_;
227
228 AddPattern(&ehost_permissions_, "http://*.google.com/*");
229 AddPattern(&ehost_permissions_, "http://example.com/*");
230 AddPattern(&ehost_permissions_, "chrome://favicon/*");
231 AddPattern(&ehost_permissions_, "https://*.google.com/*");
232
233 AddPattern(&shost_permissions_, "http://reddit.com/r/test/*");
234 AddPattern(&shost_permissions_, "http://somesite.com/*");
235 AddPattern(&shost_permissions_, "http://example.com/*");
236
237 ExtensionAPIPermissionSet empty_set;
238 URLPatternSet empty_extent;
239 scoped_ptr<ExtensionPermissionSet> permissions;
240 scoped_ptr<ExtensionPermissionSet> granted_permissions;
241
242 // Make sure both granted api and host permissions start empty.
243 granted_permissions.reset(
244 prefs()->GetGrantedPermissions(extension_id_));
245 EXPECT_TRUE(granted_permissions->IsDefault());
246
247 permissions.reset(new ExtensionPermissionSet(
248 api_perm_set1_, empty_extent, empty_extent));
249
250 // Add part of the api permissions.
251 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
252 granted_permissions.reset(prefs()->GetGrantedPermissions(extension_id_));
253 EXPECT_TRUE(granted_permissions.get());
254 EXPECT_FALSE(granted_permissions->IsDefault());
255 EXPECT_EQ(expected_apis, granted_permissions->apis());
256 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty());
257 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
258 granted_permissions.reset();
259
260 // Add part of the explicit host permissions.
261 permissions.reset(new ExtensionPermissionSet(
262 empty_set, ehost_perm_set1_, empty_extent));
263 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
264 granted_permissions.reset(prefs()->GetGrantedPermissions(extension_id_));
265 EXPECT_FALSE(granted_permissions->IsDefault());
266 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
267 EXPECT_EQ(expected_apis, granted_permissions->apis());
268 AssertEqualExtents(ehost_perm_set1_,
269 granted_permissions->explicit_hosts());
270 AssertEqualExtents(ehost_perm_set1_,
271 granted_permissions->effective_hosts());
272
273 // Add part of the scriptable host permissions.
274 permissions.reset(new ExtensionPermissionSet(
275 empty_set, empty_extent, shost_perm_set1_));
276 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
277 granted_permissions.reset(prefs()->GetGrantedPermissions(extension_id_));
278 EXPECT_FALSE(granted_permissions->IsDefault());
279 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess());
280 EXPECT_EQ(expected_apis, granted_permissions->apis());
281 AssertEqualExtents(ehost_perm_set1_,
282 granted_permissions->explicit_hosts());
283 AssertEqualExtents(shost_perm_set1_,
284 granted_permissions->scriptable_hosts());
285 URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_,
286 &effective_permissions_);
287 AssertEqualExtents(effective_permissions_,
288 granted_permissions->effective_hosts());
289
290 // Add the rest of both the permissions.
291 permissions.reset(new ExtensionPermissionSet(
292 api_perm_set2_, ehost_perm_set2_, shost_perm_set2_));
293
294 std::set_union(expected_apis.begin(), expected_apis.end(),
223 api_perm_set2_.begin(), api_perm_set2_.end(), 295 api_perm_set2_.begin(), api_perm_set2_.end(),
224 std::inserter(api_permissions_, api_permissions_.begin())); 296 std::inserter(api_permissions_, api_permissions_.begin()));
225 297
226 AddPattern(&host_permissions_, "http://*.google.com/*"); 298 prefs()->AddGrantedPermissions(extension_id_, permissions.get());
227 AddPattern(&host_permissions_, "http://example.com/*"); 299 granted_permissions.reset(prefs()->GetGrantedPermissions(extension_id_));
228 AddPattern(&host_permissions_, "chrome://favicon/*"); 300 EXPECT_TRUE(granted_permissions.get());
229 AddPattern(&host_permissions_, "https://*.google.com/*"); 301 EXPECT_FALSE(granted_permissions->IsDefault());
230 302 EXPECT_EQ(api_permissions_, granted_permissions->apis());
231 std::set<std::string> empty_set; 303 AssertEqualExtents(ehost_permissions_,
232 std::set<std::string> api_perms; 304 granted_permissions->explicit_hosts());
233 bool full_access = false; 305 AssertEqualExtents(shost_permissions_,
234 URLPatternSet host_perms; 306 granted_permissions->scriptable_hosts());
235 URLPatternSet empty_extent; 307 effective_permissions_.ClearPatterns();
236 308 URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_,
237 // Make sure both granted api and host permissions start empty. 309 &effective_permissions_);
238 EXPECT_FALSE(prefs()->GetGrantedPermissions( 310 AssertEqualExtents(effective_permissions_,
239 extension_id_, &full_access, &api_perms, &host_perms)); 311 granted_permissions->effective_hosts());
240
241 EXPECT_TRUE(api_perms.empty());
242 EXPECT_TRUE(host_perms.is_empty());
243
244 // Add part of the api permissions.
245 prefs()->AddGrantedPermissions(
246 extension_id_, false, api_perm_set1_, empty_extent);
247 EXPECT_TRUE(prefs()->GetGrantedPermissions(
248 extension_id_, &full_access, &api_perms, &host_perms));
249 EXPECT_EQ(api_perm_set1_, api_perms);
250 EXPECT_TRUE(host_perms.is_empty());
251 EXPECT_FALSE(full_access);
252 host_perms.ClearPatterns();
253 api_perms.clear();
254
255 // Add part of the host permissions.
256 prefs()->AddGrantedPermissions(
257 extension_id_, false, empty_set, host_perm_set1_);
258 EXPECT_TRUE(prefs()->GetGrantedPermissions(
259 extension_id_, &full_access, &api_perms, &host_perms));
260 EXPECT_FALSE(full_access);
261 EXPECT_EQ(api_perm_set1_, api_perms);
262 AssertEqualExtents(&host_perm_set1_, &host_perms);
263 host_perms.ClearPatterns();
264 api_perms.clear();
265
266 // Add the rest of both the api and host permissions.
267 prefs()->AddGrantedPermissions(extension_id_,
268 true,
269 api_perm_set2_,
270 host_perm_set2_);
271
272 EXPECT_TRUE(prefs()->GetGrantedPermissions(
273 extension_id_, &full_access, &api_perms, &host_perms));
274 EXPECT_TRUE(full_access);
275 EXPECT_EQ(api_permissions_, api_perms);
276 AssertEqualExtents(&host_permissions_, &host_perms);
277 } 312 }
278 313
279 virtual void Verify() { 314 virtual void Verify() {
280 std::set<std::string> api_perms; 315 scoped_ptr<ExtensionPermissionSet> permissions(
281 URLPatternSet host_perms; 316 prefs()->GetGrantedPermissions(extension_id_));
282 bool full_access; 317 EXPECT_TRUE(permissions.get());
283 318 EXPECT_FALSE(permissions->HasEffectiveFullAccess());
284 EXPECT_TRUE(prefs()->GetGrantedPermissions( 319 EXPECT_EQ(api_permissions_, permissions->apis());
285 extension_id_, &full_access, &api_perms, &host_perms)); 320 AssertEqualExtents(ehost_permissions_, permissions->explicit_hosts());
286 EXPECT_EQ(api_permissions_, api_perms); 321 AssertEqualExtents(shost_permissions_, permissions->scriptable_hosts());
287 EXPECT_TRUE(full_access);
288 AssertEqualExtents(&host_permissions_, &host_perms);
289 } 322 }
290 323
291 private: 324 private:
292 std::string extension_id_; 325 std::string extension_id_;
293 std::set<std::string> api_perm_set1_; 326 ExtensionAPIPermissionSet api_perm_set1_;
294 std::set<std::string> api_perm_set2_; 327 ExtensionAPIPermissionSet api_perm_set2_;
295 URLPatternSet host_perm_set1_; 328 URLPatternSet ehost_perm_set1_;
296 URLPatternSet host_perm_set2_; 329 URLPatternSet ehost_perm_set2_;
330 URLPatternSet shost_perm_set1_;
331 URLPatternSet shost_perm_set2_;
297 332
298 333 ExtensionAPIPermissionSet api_permissions_;
299 std::set<std::string> api_permissions_; 334 URLPatternSet ehost_permissions_;
300 URLPatternSet host_permissions_; 335 URLPatternSet shost_permissions_;
336 URLPatternSet effective_permissions_;
301 }; 337 };
302 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {} 338 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {}
303 339
304 // Tests the GetVersionString function. 340 // Tests the GetVersionString function.
305 class ExtensionPrefsVersionString : public ExtensionPrefsTest { 341 class ExtensionPrefsVersionString : public ExtensionPrefsTest {
306 public: 342 public:
307 virtual void Initialize() { 343 virtual void Initialize() {
308 extension = prefs_.AddExtension("test"); 344 extension = prefs_.AddExtension("test");
309 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); 345 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id()));
310 prefs()->OnExtensionUninstalled(extension->id(), 346 prefs()->OnExtensionUninstalled(extension->id(),
(...skipping 689 matching lines...) Expand 10 before | Expand all | Expand 10 after
1000 testing::Mock::VerifyAndClearExpectations(v1i); 1036 testing::Mock::VerifyAndClearExpectations(v1i);
1001 testing::Mock::VerifyAndClearExpectations(v2); 1037 testing::Mock::VerifyAndClearExpectations(v2);
1002 testing::Mock::VerifyAndClearExpectations(v2i); 1038 testing::Mock::VerifyAndClearExpectations(v2i);
1003 } 1039 }
1004 1040
1005 virtual void Verify() { 1041 virtual void Verify() {
1006 } 1042 }
1007 }; 1043 };
1008 TEST_F(ExtensionPrefsSetExtensionControlledPref, 1044 TEST_F(ExtensionPrefsSetExtensionControlledPref,
1009 ExtensionPrefsSetExtensionControlledPref) {} 1045 ExtensionPrefsSetExtensionControlledPref) {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698