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

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

Issue 8493017: Cleanup extension permissions module. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge Created 8 years, 11 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/file_path.h"
6 #include "base/json/json_value_serializer.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/path_service.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_service_unittest.h"
12 #include "chrome/browser/extensions/permissions_updater.h"
13 #include "chrome/common/chrome_notification_types.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/extension_permission_set.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "chrome/test/base/ui_test_utils.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_service.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 namespace extensions {
25
26 namespace {
27
28 // A helper class that listens for NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED.
29 class PermissionsUpdaterListener : public content::NotificationObserver {
30 public:
31 PermissionsUpdaterListener()
32 : received_notification_(false), waiting_(false) {
33 registrar_.Add(this,
34 chrome::NOTIFICATION_EXTENSION_PERMISSIONS_UPDATED,
35 content::NotificationService::AllSources());
36 }
37
38 void Reset() {
39 received_notification_ = false;
40 waiting_ = false;
41 extension_ = NULL;
42 permissions_ = NULL;
43 }
44
45 void Wait() {
46 if (received_notification_)
47 return;
48
49 waiting_ = true;
50 ui_test_utils::RunMessageLoop();
51 }
52
53 bool received_notification() const { return received_notification_; }
54 const Extension* extension() const { return extension_; }
55 const ExtensionPermissionSet* permissions() const { return permissions_; }
56 UpdatedExtensionPermissionsInfo::Reason reason() const {
57 return reason_;
58 }
59
60 private:
61 virtual void Observe(int type,
62 const content::NotificationSource& source,
63 const content::NotificationDetails& details) OVERRIDE {
64 received_notification_ = true;
65 UpdatedExtensionPermissionsInfo* info =
66 content::Details<UpdatedExtensionPermissionsInfo>(details).ptr();
67
68 extension_ = info->extension;
69 permissions_ = info->permissions;
70 reason_ = info->reason;
71
72 if (waiting_) {
73 waiting_ = false;
74 MessageLoopForUI::current()->Quit();
75 }
76 }
77
78 bool received_notification_;
79 bool waiting_;
80 content::NotificationRegistrar registrar_;
81 scoped_refptr<const Extension> extension_;
82 scoped_refptr<const ExtensionPermissionSet> permissions_;
83 UpdatedExtensionPermissionsInfo::Reason reason_;
84 };
85
86 class PermissionsUpdaterTest : public ExtensionServiceTestBase {
87 };
88
89 scoped_refptr<Extension> LoadManifest(std::string* error) {
90 FilePath path;
91 PathService::Get(chrome::DIR_TEST_DATA, &path);
92 path = path.AppendASCII("extensions")
93 .AppendASCII("api_test")
94 .AppendASCII("permissions")
95 .AppendASCII("optional")
96 .AppendASCII("manifest.json");
97
98 JSONFileValueSerializer serializer(path);
99 scoped_ptr<Value> result(serializer.Deserialize(NULL, error));
100 if (!result.get())
101 return NULL;
102
103 scoped_refptr<Extension> extension = Extension::Create(
104 path.DirName(), Extension::INTERNAL,
105 *static_cast<DictionaryValue*>(result.get()), Extension::NO_FLAGS, error);
106 return extension;
107 }
108
109 void AddPattern(URLPatternSet* extent, const std::string& pattern) {
110 int schemes = URLPattern::SCHEME_ALL;
111 extent->AddPattern(URLPattern(schemes, pattern));
112 }
113
114 } // namespace
115
116 // Test that the PermissionUpdater can correctly add and remove active
117 // permissions. This tests all of PermissionsUpdater's public methods because
118 // GrantActivePermissions and UpdateActivePermissions are used by
119 // AddPermissions.
120 TEST_F(PermissionsUpdaterTest, AddAndRemovePermissions) {
121 InitializeEmptyExtensionService();
122
123 // Load the test extension.
124 std::string error;
125 scoped_refptr<Extension> extension = LoadManifest(&error);
126 ASSERT_TRUE(error.empty()) << error;
127
128 ExtensionAPIPermissionSet default_apis;
129 default_apis.insert(ExtensionAPIPermission::kManagement);
130 URLPatternSet default_hosts;
131 AddPattern(&default_hosts, "http://a.com/*");
132 scoped_refptr<ExtensionPermissionSet> default_permissions =
133 new ExtensionPermissionSet(default_apis, default_hosts, URLPatternSet());
134
135 // Make sure it loaded properly.
136 scoped_refptr<const ExtensionPermissionSet> permissions =
137 extension->GetActivePermissions();
138 ASSERT_EQ(*default_permissions, *extension->GetActivePermissions());
139
140 // Add a few permissions.
141 ExtensionAPIPermissionSet apis;
142 apis.insert(ExtensionAPIPermission::kTab);
143 apis.insert(ExtensionAPIPermission::kNotification);
144 URLPatternSet hosts;
145 AddPattern(&hosts, "http://*.c.com/*");
146
147 scoped_refptr<ExtensionPermissionSet> delta =
148 new ExtensionPermissionSet(apis, hosts, URLPatternSet());
149
150 PermissionsUpdaterListener listener;
151 PermissionsUpdater updater(profile_.get());
152 updater.AddPermissions(extension.get(), delta.get());
153
154 listener.Wait();
155
156 // Verify that the permission notification was sent correctly.
157 ASSERT_TRUE(listener.received_notification());
158 ASSERT_EQ(extension, listener.extension());
159 ASSERT_EQ(UpdatedExtensionPermissionsInfo::ADDED, listener.reason());
160 ASSERT_EQ(*delta, *listener.permissions());
161
162 // Make sure the extension's active permissions reflect the change.
163 scoped_refptr<ExtensionPermissionSet> active_permissions =
164 ExtensionPermissionSet::CreateUnion(default_permissions, delta);
165 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions());
166
167 // Verify that the new granted and active permissions were also stored
168 // in the extension preferences. In this case, the granted permissions should
169 // be equal to the active permissions.
170 ExtensionPrefs* prefs = service_->extension_prefs();
171 scoped_refptr<ExtensionPermissionSet> granted_permissions =
172 active_permissions;
173
174 scoped_refptr<ExtensionPermissionSet> from_prefs =
175 prefs->GetActivePermissions(extension->id());
176 ASSERT_EQ(*active_permissions, *from_prefs);
177
178 from_prefs = prefs->GetGrantedPermissions(extension->id());
179 ASSERT_EQ(*active_permissions, *from_prefs);
180
181 // In the second part of the test, we'll remove the permissions that we
182 // just added except for 'notification'.
183 apis.erase(ExtensionAPIPermission::kNotification);
184 delta = new ExtensionPermissionSet(apis, hosts, URLPatternSet());
185
186 listener.Reset();
187 updater.RemovePermissions(extension, delta);
188 listener.Wait();
189
190 // Verify that the notification was correct.
191 ASSERT_TRUE(listener.received_notification());
192 ASSERT_EQ(extension, listener.extension());
193 ASSERT_EQ(UpdatedExtensionPermissionsInfo::REMOVED, listener.reason());
194 ASSERT_EQ(*delta, *listener.permissions());
195
196 // Make sure the extension's active permissions reflect the change.
197 active_permissions =
198 ExtensionPermissionSet::CreateDifference(active_permissions, delta);
199 ASSERT_EQ(*active_permissions, *extension->GetActivePermissions());
200
201 // Verify that the extension prefs hold the new active permissions and the
202 // same granted permissions.
203 from_prefs = prefs->GetActivePermissions(extension->id());
204 ASSERT_EQ(*active_permissions, *from_prefs);
205
206 from_prefs = prefs->GetGrantedPermissions(extension->id());
207 ASSERT_EQ(*granted_permissions, *from_prefs);
208 }
209
210 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/permissions_updater.cc ('k') | chrome/browser/tab_contents/render_view_host_delegate_helper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698