OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/extension_prefs_unittest.h" | 5 #include "chrome/browser/extensions/extension_prefs_unittest.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/prefs/mock_pref_change_callback.h" | 10 #include "base/prefs/mock_pref_change_callback.h" |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 AddPattern(&ehost_permissions_, "chrome://favicon/*"); | 201 AddPattern(&ehost_permissions_, "chrome://favicon/*"); |
202 AddPattern(&ehost_permissions_, "https://*.google.com/*"); | 202 AddPattern(&ehost_permissions_, "https://*.google.com/*"); |
203 | 203 |
204 AddPattern(&shost_permissions_, "http://reddit.com/r/test/*"); | 204 AddPattern(&shost_permissions_, "http://reddit.com/r/test/*"); |
205 AddPattern(&shost_permissions_, "http://somesite.com/*"); | 205 AddPattern(&shost_permissions_, "http://somesite.com/*"); |
206 AddPattern(&shost_permissions_, "http://example.com/*"); | 206 AddPattern(&shost_permissions_, "http://example.com/*"); |
207 | 207 |
208 APIPermissionSet empty_set; | 208 APIPermissionSet empty_set; |
209 ManifestPermissionSet empty_manifest_permissions; | 209 ManifestPermissionSet empty_manifest_permissions; |
210 URLPatternSet empty_extent; | 210 URLPatternSet empty_extent; |
211 scoped_refptr<const PermissionSet> permissions; | |
212 scoped_refptr<const PermissionSet> granted_permissions; | |
213 | 211 |
214 // Make sure both granted api and host permissions start empty. | 212 // Make sure both granted api and host permissions start empty. |
215 granted_permissions = | 213 EXPECT_TRUE(prefs()->GetGrantedPermissions(extension_id_)->IsEmpty()); |
216 prefs()->GetGrantedPermissions(extension_id_); | |
217 EXPECT_TRUE(granted_permissions->IsEmpty()); | |
218 | 214 |
219 permissions = new PermissionSet( | 215 { |
220 api_perm_set1_, empty_manifest_permissions, empty_extent, empty_extent); | 216 PermissionSet permissions(api_perm_set1_, empty_manifest_permissions, |
| 217 empty_extent, empty_extent); |
221 | 218 |
222 // Add part of the api permissions. | 219 // Add part of the api permissions. |
223 prefs()->AddGrantedPermissions(extension_id_, permissions.get()); | 220 prefs()->AddGrantedPermissions(extension_id_, &permissions); |
224 granted_permissions = prefs()->GetGrantedPermissions(extension_id_); | 221 scoped_ptr<const PermissionSet> granted_permissions = |
| 222 prefs()->GetGrantedPermissions(extension_id_); |
225 EXPECT_TRUE(granted_permissions.get()); | 223 EXPECT_TRUE(granted_permissions.get()); |
226 EXPECT_FALSE(granted_permissions->IsEmpty()); | 224 EXPECT_FALSE(granted_permissions->IsEmpty()); |
227 EXPECT_EQ(expected_apis, granted_permissions->apis()); | 225 EXPECT_EQ(expected_apis, granted_permissions->apis()); |
228 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty()); | 226 EXPECT_TRUE(granted_permissions->effective_hosts().is_empty()); |
229 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); | 227 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); |
230 granted_permissions = NULL; | 228 } |
231 | 229 |
| 230 { |
232 // Add part of the explicit host permissions. | 231 // Add part of the explicit host permissions. |
233 permissions = new PermissionSet( | 232 PermissionSet permissions(empty_set, empty_manifest_permissions, |
234 empty_set, empty_manifest_permissions, ehost_perm_set1_, empty_extent); | 233 ehost_perm_set1_, empty_extent); |
235 prefs()->AddGrantedPermissions(extension_id_, permissions.get()); | 234 prefs()->AddGrantedPermissions(extension_id_, &permissions); |
236 granted_permissions = prefs()->GetGrantedPermissions(extension_id_); | 235 scoped_ptr<const PermissionSet> granted_permissions = |
| 236 prefs()->GetGrantedPermissions(extension_id_); |
237 EXPECT_FALSE(granted_permissions->IsEmpty()); | 237 EXPECT_FALSE(granted_permissions->IsEmpty()); |
238 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); | 238 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); |
239 EXPECT_EQ(expected_apis, granted_permissions->apis()); | 239 EXPECT_EQ(expected_apis, granted_permissions->apis()); |
240 EXPECT_EQ(ehost_perm_set1_, | 240 EXPECT_EQ(ehost_perm_set1_, |
241 granted_permissions->explicit_hosts()); | 241 granted_permissions->explicit_hosts()); |
242 EXPECT_EQ(ehost_perm_set1_, | 242 EXPECT_EQ(ehost_perm_set1_, |
243 granted_permissions->effective_hosts()); | 243 granted_permissions->effective_hosts()); |
| 244 } |
244 | 245 |
| 246 { |
245 // Add part of the scriptable host permissions. | 247 // Add part of the scriptable host permissions. |
246 permissions = new PermissionSet( | 248 PermissionSet permissions(empty_set, empty_manifest_permissions, |
247 empty_set, empty_manifest_permissions, empty_extent, shost_perm_set1_); | 249 empty_extent, shost_perm_set1_); |
248 prefs()->AddGrantedPermissions(extension_id_, permissions.get()); | 250 prefs()->AddGrantedPermissions(extension_id_, &permissions); |
249 granted_permissions = prefs()->GetGrantedPermissions(extension_id_); | 251 scoped_ptr<const PermissionSet> granted_permissions = |
| 252 prefs()->GetGrantedPermissions(extension_id_); |
250 EXPECT_FALSE(granted_permissions->IsEmpty()); | 253 EXPECT_FALSE(granted_permissions->IsEmpty()); |
251 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); | 254 EXPECT_FALSE(granted_permissions->HasEffectiveFullAccess()); |
252 EXPECT_EQ(expected_apis, granted_permissions->apis()); | 255 EXPECT_EQ(expected_apis, granted_permissions->apis()); |
253 EXPECT_EQ(ehost_perm_set1_, | 256 EXPECT_EQ(ehost_perm_set1_, |
254 granted_permissions->explicit_hosts()); | 257 granted_permissions->explicit_hosts()); |
255 EXPECT_EQ(shost_perm_set1_, | 258 EXPECT_EQ(shost_perm_set1_, |
256 granted_permissions->scriptable_hosts()); | 259 granted_permissions->scriptable_hosts()); |
257 | 260 |
258 effective_permissions_ = | 261 effective_permissions_ = |
259 URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_); | 262 URLPatternSet::CreateUnion(ehost_perm_set1_, shost_perm_set1_); |
260 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); | 263 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); |
| 264 } |
261 | 265 |
| 266 { |
262 // Add the rest of the permissions. | 267 // Add the rest of the permissions. |
263 permissions = new PermissionSet( | 268 PermissionSet permissions(api_perm_set2_, empty_manifest_permissions, |
264 api_perm_set2_, empty_manifest_permissions, | 269 ehost_perm_set2_, shost_perm_set2_); |
265 ehost_perm_set2_, shost_perm_set2_); | |
266 | 270 |
267 APIPermissionSet::Union(expected_apis, api_perm_set2_, &api_permissions_); | 271 APIPermissionSet::Union(expected_apis, api_perm_set2_, &api_permissions_); |
268 | 272 |
269 prefs()->AddGrantedPermissions(extension_id_, permissions.get()); | 273 prefs()->AddGrantedPermissions(extension_id_, &permissions); |
270 granted_permissions = prefs()->GetGrantedPermissions(extension_id_); | 274 scoped_ptr<const PermissionSet> granted_permissions = |
| 275 prefs()->GetGrantedPermissions(extension_id_); |
271 EXPECT_TRUE(granted_permissions.get()); | 276 EXPECT_TRUE(granted_permissions.get()); |
272 EXPECT_FALSE(granted_permissions->IsEmpty()); | 277 EXPECT_FALSE(granted_permissions->IsEmpty()); |
273 EXPECT_EQ(api_permissions_, granted_permissions->apis()); | 278 EXPECT_EQ(api_permissions_, granted_permissions->apis()); |
274 EXPECT_EQ(ehost_permissions_, | 279 EXPECT_EQ(ehost_permissions_, |
275 granted_permissions->explicit_hosts()); | 280 granted_permissions->explicit_hosts()); |
276 EXPECT_EQ(shost_permissions_, | 281 EXPECT_EQ(shost_permissions_, |
277 granted_permissions->scriptable_hosts()); | 282 granted_permissions->scriptable_hosts()); |
278 effective_permissions_ = | 283 effective_permissions_ = |
279 URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_); | 284 URLPatternSet::CreateUnion(ehost_permissions_, shost_permissions_); |
280 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); | 285 EXPECT_EQ(effective_permissions_, granted_permissions->effective_hosts()); |
| 286 } |
281 } | 287 } |
282 | 288 |
283 void Verify() override { | 289 void Verify() override { |
284 scoped_refptr<const PermissionSet> permissions( | 290 scoped_ptr<const PermissionSet> permissions = |
285 prefs()->GetGrantedPermissions(extension_id_)); | 291 prefs()->GetGrantedPermissions(extension_id_); |
286 EXPECT_TRUE(permissions.get()); | 292 EXPECT_TRUE(permissions.get()); |
287 EXPECT_FALSE(permissions->HasEffectiveFullAccess()); | 293 EXPECT_FALSE(permissions->HasEffectiveFullAccess()); |
288 EXPECT_EQ(api_permissions_, permissions->apis()); | 294 EXPECT_EQ(api_permissions_, permissions->apis()); |
289 EXPECT_EQ(ehost_permissions_, | 295 EXPECT_EQ(ehost_permissions_, |
290 permissions->explicit_hosts()); | 296 permissions->explicit_hosts()); |
291 EXPECT_EQ(shost_permissions_, | 297 EXPECT_EQ(shost_permissions_, |
292 permissions->scriptable_hosts()); | 298 permissions->scriptable_hosts()); |
293 } | 299 } |
294 | 300 |
295 private: | 301 private: |
(...skipping 27 matching lines...) Expand all Loading... |
323 | 329 |
324 URLPatternSet ehosts; | 330 URLPatternSet ehosts; |
325 AddPattern(&ehosts, "http://*.google.com/*"); | 331 AddPattern(&ehosts, "http://*.google.com/*"); |
326 AddPattern(&ehosts, "http://example.com/*"); | 332 AddPattern(&ehosts, "http://example.com/*"); |
327 AddPattern(&ehosts, "chrome://favicon/*"); | 333 AddPattern(&ehosts, "chrome://favicon/*"); |
328 | 334 |
329 URLPatternSet shosts; | 335 URLPatternSet shosts; |
330 AddPattern(&shosts, "https://*.google.com/*"); | 336 AddPattern(&shosts, "https://*.google.com/*"); |
331 AddPattern(&shosts, "http://reddit.com/r/test/*"); | 337 AddPattern(&shosts, "http://reddit.com/r/test/*"); |
332 | 338 |
333 active_perms_ = new PermissionSet( | 339 active_perms_.reset(new PermissionSet(api_perms, empty_manifest_permissions, |
334 api_perms, empty_manifest_permissions, ehosts, shosts); | 340 ehosts, shosts)); |
335 | 341 |
336 // Make sure the active permissions start empty. | 342 // Make sure the active permissions start empty. |
337 scoped_refptr<const PermissionSet> active( | 343 scoped_ptr<const PermissionSet> active = |
338 prefs()->GetActivePermissions(extension_id_)); | 344 prefs()->GetActivePermissions(extension_id_); |
339 EXPECT_TRUE(active->IsEmpty()); | 345 EXPECT_TRUE(active->IsEmpty()); |
340 | 346 |
341 // Set the active permissions. | 347 // Set the active permissions. |
342 prefs()->SetActivePermissions(extension_id_, active_perms_.get()); | 348 prefs()->SetActivePermissions(extension_id_, active_perms_.get()); |
343 active = prefs()->GetActivePermissions(extension_id_); | 349 active = prefs()->GetActivePermissions(extension_id_); |
344 EXPECT_EQ(active_perms_->apis(), active->apis()); | 350 EXPECT_EQ(active_perms_->apis(), active->apis()); |
345 EXPECT_EQ(active_perms_->explicit_hosts(), active->explicit_hosts()); | 351 EXPECT_EQ(active_perms_->explicit_hosts(), active->explicit_hosts()); |
346 EXPECT_EQ(active_perms_->scriptable_hosts(), active->scriptable_hosts()); | 352 EXPECT_EQ(active_perms_->scriptable_hosts(), active->scriptable_hosts()); |
347 EXPECT_EQ(*active_perms_.get(), *active.get()); | 353 EXPECT_EQ(*active_perms_.get(), *active.get()); |
348 } | 354 } |
349 | 355 |
350 void Verify() override { | 356 void Verify() override { |
351 scoped_refptr<const PermissionSet> permissions( | 357 scoped_ptr<const PermissionSet> permissions = |
352 prefs()->GetActivePermissions(extension_id_)); | 358 prefs()->GetActivePermissions(extension_id_); |
353 EXPECT_EQ(*active_perms_.get(), *permissions.get()); | 359 EXPECT_EQ(*active_perms_.get(), *permissions.get()); |
354 } | 360 } |
355 | 361 |
356 private: | 362 private: |
357 std::string extension_id_; | 363 std::string extension_id_; |
358 scoped_refptr<const PermissionSet> active_perms_; | 364 scoped_ptr<const PermissionSet> active_perms_; |
359 }; | 365 }; |
360 TEST_F(ExtensionPrefsActivePermissions, SetAndGetActivePermissions) {} | 366 TEST_F(ExtensionPrefsActivePermissions, SetAndGetActivePermissions) {} |
361 | 367 |
362 // Tests the GetVersionString function. | 368 // Tests the GetVersionString function. |
363 class ExtensionPrefsVersionString : public ExtensionPrefsTest { | 369 class ExtensionPrefsVersionString : public ExtensionPrefsTest { |
364 public: | 370 public: |
365 void Initialize() override { | 371 void Initialize() override { |
366 extension = prefs_.AddExtension("test"); | 372 extension = prefs_.AddExtension("test"); |
367 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); | 373 EXPECT_EQ("0.1", prefs()->GetVersionString(extension->id())); |
368 prefs()->OnExtensionUninstalled(extension->id(), | 374 prefs()->OnExtensionUninstalled(extension->id(), |
(...skipping 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 APIPermissionSet api_perms; | 951 APIPermissionSet api_perms; |
946 api_perms.insert(APIPermission::kTab); | 952 api_perms.insert(APIPermission::kTab); |
947 api_perms.insert(APIPermission::kBookmark); | 953 api_perms.insert(APIPermission::kBookmark); |
948 api_perms.insert(APIPermission::kHistory); | 954 api_perms.insert(APIPermission::kHistory); |
949 | 955 |
950 ManifestPermissionSet empty_manifest_permissions; | 956 ManifestPermissionSet empty_manifest_permissions; |
951 | 957 |
952 URLPatternSet ehosts, shosts; | 958 URLPatternSet ehosts, shosts; |
953 AddPattern(&shosts, "chrome://print/*"); | 959 AddPattern(&shosts, "chrome://print/*"); |
954 | 960 |
955 active_perms_ = new PermissionSet(api_perms, empty_manifest_permissions, | 961 active_perms_.reset(new PermissionSet(api_perms, empty_manifest_permissions, |
956 ehosts, shosts); | 962 ehosts, shosts)); |
957 // Set the active permissions. | 963 // Set the active permissions. |
958 prefs()->SetActivePermissions(component_extension_->id(), | 964 prefs()->SetActivePermissions(component_extension_->id(), |
959 active_perms_.get()); | 965 active_perms_.get()); |
960 prefs()->SetActivePermissions(no_component_extension_->id(), | 966 prefs()->SetActivePermissions(no_component_extension_->id(), |
961 active_perms_.get()); | 967 active_perms_.get()); |
962 } | 968 } |
963 | 969 |
964 void Verify() override { | 970 void Verify() override { |
965 // Component extension can access chrome://print/*. | 971 // Component extension can access chrome://print/*. |
966 scoped_refptr<const PermissionSet> component_permissions( | 972 scoped_ptr<const PermissionSet> component_permissions = |
967 prefs()->GetActivePermissions(component_extension_->id())); | 973 prefs()->GetActivePermissions(component_extension_->id()); |
968 EXPECT_EQ(1u, component_permissions->scriptable_hosts().size()); | 974 EXPECT_EQ(1u, component_permissions->scriptable_hosts().size()); |
969 | 975 |
970 // Non Component extension can not access chrome://print/*. | 976 // Non Component extension can not access chrome://print/*. |
971 scoped_refptr<const PermissionSet> no_component_permissions( | 977 scoped_ptr<const PermissionSet> no_component_permissions = |
972 prefs()->GetActivePermissions(no_component_extension_->id())); | 978 prefs()->GetActivePermissions(no_component_extension_->id()); |
973 EXPECT_EQ(0u, no_component_permissions->scriptable_hosts().size()); | 979 EXPECT_EQ(0u, no_component_permissions->scriptable_hosts().size()); |
974 | 980 |
975 // |URLPattern::SCHEME_CHROMEUI| scheme will be added in valid_schemes for | 981 // |URLPattern::SCHEME_CHROMEUI| scheme will be added in valid_schemes for |
976 // component extensions. | 982 // component extensions. |
977 URLPatternSet scriptable_hosts; | 983 URLPatternSet scriptable_hosts; |
978 std::string pref_key = "active_permissions.scriptable_host"; | 984 std::string pref_key = "active_permissions.scriptable_host"; |
979 int valid_schemes = URLPattern::SCHEME_ALL & ~URLPattern::SCHEME_CHROMEUI; | 985 int valid_schemes = URLPattern::SCHEME_ALL & ~URLPattern::SCHEME_CHROMEUI; |
980 | 986 |
981 EXPECT_TRUE(prefs()->ReadPrefAsURLPatternSet(component_extension_->id(), | 987 EXPECT_TRUE(prefs()->ReadPrefAsURLPatternSet(component_extension_->id(), |
982 pref_key, &scriptable_hosts, | 988 pref_key, &scriptable_hosts, |
983 valid_schemes)); | 989 valid_schemes)); |
984 | 990 |
985 EXPECT_FALSE(prefs()->ReadPrefAsURLPatternSet(no_component_extension_->id(), | 991 EXPECT_FALSE(prefs()->ReadPrefAsURLPatternSet(no_component_extension_->id(), |
986 pref_key, &scriptable_hosts, | 992 pref_key, &scriptable_hosts, |
987 valid_schemes)); | 993 valid_schemes)); |
988 } | 994 } |
989 | 995 |
990 private: | 996 private: |
991 scoped_refptr<const PermissionSet> active_perms_; | 997 scoped_ptr<const PermissionSet> active_perms_; |
992 scoped_refptr<Extension> component_extension_; | 998 scoped_refptr<Extension> component_extension_; |
993 scoped_refptr<Extension> no_component_extension_; | 999 scoped_refptr<Extension> no_component_extension_; |
994 }; | 1000 }; |
995 TEST_F(ExtensionPrefsComponentExtension, ExtensionPrefsComponentExtension) { | 1001 TEST_F(ExtensionPrefsComponentExtension, ExtensionPrefsComponentExtension) { |
996 } | 1002 } |
997 | 1003 |
998 } // namespace extensions | 1004 } // namespace extensions |
OLD | NEW |