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

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

Issue 14358004: Almost all actions in Declarative Web Request require all_urls host permissions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: All URLs -> all hosts; also rebased Created 7 years, 8 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) 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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/json/json_file_value_serializer.h" 6 #include "base/json/json_file_value_serializer.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_service.h"
12 #include "chrome/browser/extensions/extension_service_unittest.h" 12 #include "chrome/browser/extensions/extension_service_unittest.h"
13 #include "chrome/browser/extensions/permissions_updater.h" 13 #include "chrome/browser/extensions/permissions_updater.h"
14 #include "chrome/common/chrome_notification_types.h" 14 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/chrome_paths.h" 15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
17 #include "chrome/common/extensions/extension_test_util.h"
17 #include "chrome/common/extensions/permissions/permission_set.h" 18 #include "chrome/common/extensions/permissions/permission_set.h"
18 #include "chrome/test/base/testing_profile.h" 19 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/browser/notification_observer.h" 20 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h" 21 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h" 22 #include "content/public/browser/notification_service.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 24
25 using extension_test_util::LoadManifest;
26
24 namespace extensions { 27 namespace extensions {
25 28
26 namespace { 29 namespace {
27 30
28 // A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED. 31 // A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED.
29 class PermissionsUpdaterListener : public content::NotificationObserver { 32 class PermissionsUpdaterListener : public content::NotificationObserver {
30 public: 33 public:
31 PermissionsUpdaterListener() 34 PermissionsUpdaterListener()
32 : received_notification_(false), waiting_(false) { 35 : received_notification_(false), waiting_(false) {
33 registrar_.Add(this, 36 registrar_.Add(this,
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 bool waiting_; 83 bool waiting_;
81 content::NotificationRegistrar registrar_; 84 content::NotificationRegistrar registrar_;
82 scoped_refptr<const Extension> extension_; 85 scoped_refptr<const Extension> extension_;
83 scoped_refptr<const PermissionSet> permissions_; 86 scoped_refptr<const PermissionSet> permissions_;
84 UpdatedExtensionPermissionsInfo::Reason reason_; 87 UpdatedExtensionPermissionsInfo::Reason reason_;
85 }; 88 };
86 89
87 class PermissionsUpdaterTest : public ExtensionServiceTestBase { 90 class PermissionsUpdaterTest : public ExtensionServiceTestBase {
88 }; 91 };
89 92
90 scoped_refptr<Extension> LoadManifest(std::string* error) { 93 scoped_refptr<Extension> LoadOurManifest() {
91 base::FilePath path; 94 base::FilePath path;
92 PathService::Get(chrome::DIR_TEST_DATA, &path); 95 path = path.AppendASCII("api_test")
93 path = path.AppendASCII("extensions")
94 .AppendASCII("api_test")
95 .AppendASCII("permissions") 96 .AppendASCII("permissions")
96 .AppendASCII("optional") 97 .AppendASCII("optional");
97 .AppendASCII("manifest.json"); 98 return LoadManifest(path.AsUTF8Unsafe(),
98 99 "manifest.json",
99 JSONFileValueSerializer serializer(path); 100 Manifest::INTERNAL,
100 scoped_ptr<Value> result(serializer.Deserialize(NULL, error)); 101 Extension::NO_FLAGS);
101 if (!result)
102 return NULL;
103
104 scoped_refptr<Extension> extension = Extension::Create(
105 path.DirName(), Manifest::INTERNAL,
106 *static_cast<DictionaryValue*>(result.get()), Extension::NO_FLAGS, error);
107 return extension;
108 } 102 }
109 103
110 void AddPattern(URLPatternSet* extent, const std::string& pattern) { 104 void AddPattern(URLPatternSet* extent, const std::string& pattern) {
111 int schemes = URLPattern::SCHEME_ALL; 105 int schemes = URLPattern::SCHEME_ALL;
112 extent->AddPattern(URLPattern(schemes, pattern)); 106 extent->AddPattern(URLPattern(schemes, pattern));
113 } 107 }
114 108
115 } // namespace 109 } // namespace
116 110
117 // Test that the PermissionUpdater can correctly add and remove active 111 // Test that the PermissionUpdater can correctly add and remove active
118 // permissions. This tests all of PermissionsUpdater's public methods because 112 // permissions. This tests all of PermissionsUpdater's public methods because
119 // GrantActivePermissions and UpdateActivePermissions are used by 113 // GrantActivePermissions and UpdateActivePermissions are used by
120 // AddPermissions. 114 // AddPermissions.
121 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) { 115 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) {
122 InitializeEmptyExtensionService(); 116 InitializeEmptyExtensionService();
123 117
124 // Load the test extension. 118 // Load the test extension.
125 std::string error; 119 scoped_refptr<Extension> extension = LoadOurManifest();
126 scoped_refptr<Extension> extension = LoadManifest(&error); 120 ASSERT_TRUE(extension.get());
127 ASSERT_TRUE(error.empty()) << error;
128 121
129 APIPermissionSet default_apis; 122 APIPermissionSet default_apis;
130 default_apis.insert(APIPermission::kManagement); 123 default_apis.insert(APIPermission::kManagement);
131 URLPatternSet default_hosts; 124 URLPatternSet default_hosts;
132 AddPattern(&default_hosts, "http://a.com/*"); 125 AddPattern(&default_hosts, "http://a.com/*");
133 scoped_refptr<PermissionSet> default_permissions = 126 scoped_refptr<PermissionSet> default_permissions =
134 new PermissionSet(default_apis, default_hosts, URLPatternSet()); 127 new PermissionSet(default_apis, default_hosts, URLPatternSet());
135 128
136 // Make sure it loaded properly. 129 // Make sure it loaded properly.
137 scoped_refptr<const PermissionSet> permissions = 130 scoped_refptr<const PermissionSet> permissions =
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 // Verify that the extension prefs hold the new active permissions and the 195 // Verify that the extension prefs hold the new active permissions and the
203 // same granted permissions. 196 // same granted permissions.
204 from_prefs = prefs->GetActivePermissions(extension->id()); 197 from_prefs = prefs->GetActivePermissions(extension->id());
205 ASSERT_EQ(*active_permissions, *from_prefs); 198 ASSERT_EQ(*active_permissions, *from_prefs);
206 199
207 from_prefs = prefs->GetGrantedPermissions(extension->id()); 200 from_prefs = prefs->GetGrantedPermissions(extension->id());
208 ASSERT_EQ(*granted_permissions, *from_prefs); 201 ASSERT_EQ(*granted_permissions, *from_prefs);
209 } 202 }
210 203
211 } // namespace extensions 204 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698