| OLD | NEW |
| 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 std::set<std::string> strings1; |
| 51 EXPECT_EQ(patterns1.size(), patterns2.size()); | 52 EXPECT_EQ(patterns1.size(), patterns2.size()); |
| 52 | 53 |
| 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 |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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::Tab()); |
| 208 api_perm_set1_.insert("bookmarks"); | 209 api_perm_set1_.insert(ExtensionAPIPermission::Bookmark()); |
| 209 api_perm_set1_.insert("something_random"); | |
| 210 | 210 |
| 211 api_perm_set2_.insert("history"); | 211 api_perm_set2_.insert(ExtensionAPIPermission::History()); |
| 212 api_perm_set2_.insert("unknown2"); | |
| 213 | 212 |
| 214 AddPattern(&host_perm_set1_, "http://*.google.com/*"); | 213 AddPattern(&host_perm_set1_, "http://*.google.com/*"); |
| 215 AddPattern(&host_perm_set1_, "http://example.com/*"); | 214 AddPattern(&host_perm_set1_, "http://example.com/*"); |
| 216 AddPattern(&host_perm_set1_, "chrome://favicon/*"); | 215 AddPattern(&host_perm_set1_, "chrome://favicon/*"); |
| 217 | 216 |
| 218 AddPattern(&host_perm_set2_, "https://*.google.com/*"); | 217 AddPattern(&host_perm_set2_, "https://*.google.com/*"); |
| 219 // with duplicate: | 218 // with duplicate: |
| 220 AddPattern(&host_perm_set2_, "http://*.google.com/*"); | 219 AddPattern(&host_perm_set2_, "http://*.google.com/*"); |
| 221 | 220 |
| 222 std::set_union(api_perm_set1_.begin(), api_perm_set1_.end(), | 221 std::set_union(api_perm_set1_.begin(), api_perm_set1_.end(), |
| 223 api_perm_set2_.begin(), api_perm_set2_.end(), | 222 api_perm_set2_.begin(), api_perm_set2_.end(), |
| 224 std::inserter(api_permissions_, api_permissions_.begin())); | 223 std::inserter(api_permissions_, api_permissions_.begin())); |
| 225 | 224 |
| 226 AddPattern(&host_permissions_, "http://*.google.com/*"); | 225 AddPattern(&host_permissions_, "http://*.google.com/*"); |
| 227 AddPattern(&host_permissions_, "http://example.com/*"); | 226 AddPattern(&host_permissions_, "http://example.com/*"); |
| 228 AddPattern(&host_permissions_, "chrome://favicon/*"); | 227 AddPattern(&host_permissions_, "chrome://favicon/*"); |
| 229 AddPattern(&host_permissions_, "https://*.google.com/*"); | 228 AddPattern(&host_permissions_, "https://*.google.com/*"); |
| 230 | 229 |
| 231 std::set<std::string> empty_set; | 230 std::set<ExtensionAPIPermission> empty_set; |
| 232 std::set<std::string> api_perms; | |
| 233 bool full_access = false; | |
| 234 URLPatternSet host_perms; | |
| 235 URLPatternSet empty_extent; | 231 URLPatternSet empty_extent; |
| 232 scoped_ptr<ExtensionPermissionSet> permissions; |
| 233 scoped_ptr<ExtensionPermissionSet> granted_permissions; |
| 236 | 234 |
| 237 // Make sure both granted api and host permissions start empty. | 235 // Make sure both granted api and host permissions start empty. |
| 238 EXPECT_FALSE(prefs()->GetGrantedPermissions( | 236 bool initialized = false; |
| 239 extension_id_, &full_access, &api_perms, &host_perms)); | 237 granted_permissions.reset( |
| 240 | 238 prefs()->GetGrantedPermissions(extension_id_, &initialized)); |
| 241 EXPECT_TRUE(api_perms.empty()); | 239 EXPECT_TRUE(granted_permissions.get()); |
| 242 EXPECT_TRUE(host_perms.is_empty()); | 240 EXPECT_FALSE(initialized); |
| 241 EXPECT_TRUE(granted_permissions->IsEmpty()); |
| 242 permissions.reset( |
| 243 new ExtensionPermissionSet(false, api_perm_set1_, empty_extent)); |
| 243 | 244 |
| 244 // Add part of the api permissions. | 245 // Add part of the api permissions. |
| 245 prefs()->AddGrantedPermissions( | 246 prefs()->AddGrantedPermissions(extension_id_, *permissions); |
| 246 extension_id_, false, api_perm_set1_, empty_extent); | 247 granted_permissions.reset( |
| 247 EXPECT_TRUE(prefs()->GetGrantedPermissions( | 248 prefs()->GetGrantedPermissions(extension_id_, &initialized)); |
| 248 extension_id_, &full_access, &api_perms, &host_perms)); | 249 EXPECT_FALSE(granted_permissions->IsEmpty()); |
| 249 EXPECT_EQ(api_perm_set1_, api_perms); | 250 EXPECT_TRUE(initialized); |
| 250 EXPECT_TRUE(host_perms.is_empty()); | 251 EXPECT_EQ(api_perm_set1_, granted_permissions->apis()); |
| 251 EXPECT_FALSE(full_access); | 252 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty()); |
| 252 host_perms.ClearPatterns(); | 253 EXPECT_FALSE(granted_permissions->native_code()); |
| 253 api_perms.clear(); | 254 granted_permissions.reset(); |
| 254 | 255 |
| 255 // Add part of the host permissions. | 256 // Add part of the host permissions. |
| 256 prefs()->AddGrantedPermissions( | 257 permissions.reset( |
| 257 extension_id_, false, empty_set, host_perm_set1_); | 258 new ExtensionPermissionSet(false, empty_set, host_perm_set1_)); |
| 258 EXPECT_TRUE(prefs()->GetGrantedPermissions( | 259 prefs()->AddGrantedPermissions(extension_id_, *permissions); |
| 259 extension_id_, &full_access, &api_perms, &host_perms)); | 260 |
| 260 EXPECT_FALSE(full_access); | 261 // Make sure we can pass NULL in for initialized. |
| 261 EXPECT_EQ(api_perm_set1_, api_perms); | 262 granted_permissions.reset( |
| 262 AssertEqualExtents(&host_perm_set1_, &host_perms); | 263 prefs()->GetGrantedPermissions(extension_id_, NULL)); |
| 263 host_perms.ClearPatterns(); | 264 EXPECT_FALSE(granted_permissions->IsEmpty()); |
| 264 api_perms.clear(); | 265 EXPECT_FALSE(granted_permissions->native_code()); |
| 266 EXPECT_EQ(api_perm_set1_, granted_permissions->apis()); |
| 267 AssertEqualExtents(host_perm_set1_, granted_permissions->effective_hosts()); |
| 265 | 268 |
| 266 // Add the rest of both the api and host permissions. | 269 // Add the rest of both the api and host permissions. |
| 267 prefs()->AddGrantedPermissions(extension_id_, | 270 permissions.reset( |
| 268 true, | 271 new ExtensionPermissionSet(true, api_perm_set2_, host_perm_set2_)); |
| 269 api_perm_set2_, | 272 prefs()->AddGrantedPermissions(extension_id_, *permissions); |
| 270 host_perm_set2_); | 273 initialized = false; |
| 271 | 274 granted_permissions.reset( |
| 272 EXPECT_TRUE(prefs()->GetGrantedPermissions( | 275 prefs()->GetGrantedPermissions(extension_id_, &initialized)); |
| 273 extension_id_, &full_access, &api_perms, &host_perms)); | 276 EXPECT_FALSE(granted_permissions->IsEmpty()); |
| 274 EXPECT_TRUE(full_access); | 277 EXPECT_TRUE(initialized); |
| 275 EXPECT_EQ(api_permissions_, api_perms); | 278 EXPECT_TRUE(granted_permissions->native_code()); |
| 276 AssertEqualExtents(&host_permissions_, &host_perms); | 279 EXPECT_EQ(api_permissions_, granted_permissions->apis()); |
| 280 AssertEqualExtents(host_permissions_, |
| 281 granted_permissions->effective_hosts()); |
| 277 } | 282 } |
| 278 | 283 |
| 279 virtual void Verify() { | 284 virtual void Verify() { |
| 280 std::set<std::string> api_perms; | 285 bool initialized = false; |
| 281 URLPatternSet host_perms; | 286 scoped_ptr<ExtensionPermissionSet> permissions( |
| 282 bool full_access; | 287 prefs()->GetGrantedPermissions(extension_id_, &initialized)); |
| 283 | 288 EXPECT_TRUE(initialized); |
| 284 EXPECT_TRUE(prefs()->GetGrantedPermissions( | 289 EXPECT_EQ(api_permissions_, permissions->apis()); |
| 285 extension_id_, &full_access, &api_perms, &host_perms)); | 290 EXPECT_TRUE(permissions->native_code()); |
| 286 EXPECT_EQ(api_permissions_, api_perms); | 291 AssertEqualExtents(host_permissions_, permissions->effective_hosts()); |
| 287 EXPECT_TRUE(full_access); | |
| 288 AssertEqualExtents(&host_permissions_, &host_perms); | |
| 289 } | 292 } |
| 290 | 293 |
| 291 private: | 294 private: |
| 292 std::string extension_id_; | 295 std::string extension_id_; |
| 293 std::set<std::string> api_perm_set1_; | 296 std::set<ExtensionAPIPermission> api_perm_set1_; |
| 294 std::set<std::string> api_perm_set2_; | 297 std::set<ExtensionAPIPermission> api_perm_set2_; |
| 295 URLPatternSet host_perm_set1_; | 298 URLPatternSet host_perm_set1_; |
| 296 URLPatternSet host_perm_set2_; | 299 URLPatternSet host_perm_set2_; |
| 297 | 300 |
| 298 | 301 std::set<ExtensionAPIPermission> api_permissions_; |
| 299 std::set<std::string> api_permissions_; | |
| 300 URLPatternSet host_permissions_; | 302 URLPatternSet host_permissions_; |
| 301 }; | 303 }; |
| 302 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {} | 304 TEST_F(ExtensionPrefsGrantedPermissions, GrantedPermissions) {} |
| 303 | 305 |
| 304 // Tests the GetVersionString function. | 306 // Tests the GetVersionString function. |
| 305 class ExtensionPrefsVersionString : public ExtensionPrefsTest { | 307 class ExtensionPrefsVersionString : public ExtensionPrefsTest { |
| 306 public: | 308 public: |
| 307 virtual void Initialize() { | 309 virtual void Initialize() { |
| 308 extension = prefs_.AddExtension("test"); | 310 extension = prefs_.AddExtension("test"); |
| 309 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); | 311 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1000 testing::Mock::VerifyAndClearExpectations(v1i); | 1002 testing::Mock::VerifyAndClearExpectations(v1i); |
| 1001 testing::Mock::VerifyAndClearExpectations(v2); | 1003 testing::Mock::VerifyAndClearExpectations(v2); |
| 1002 testing::Mock::VerifyAndClearExpectations(v2i); | 1004 testing::Mock::VerifyAndClearExpectations(v2i); |
| 1003 } | 1005 } |
| 1004 | 1006 |
| 1005 virtual void Verify() { | 1007 virtual void Verify() { |
| 1006 } | 1008 } |
| 1007 }; | 1009 }; |
| 1008 TEST_F(ExtensionPrefsSetExtensionControlledPref, | 1010 TEST_F(ExtensionPrefsSetExtensionControlledPref, |
| 1009 ExtensionPrefsSetExtensionControlledPref) {} | 1011 ExtensionPrefsSetExtensionControlledPref) {} |
| OLD | NEW |