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.
5 var assertEq = chrome.test.assertEq;
6 var assertFalse = chrome.test.assertFalse;
7 var assertTrue = chrome.test.assertTrue;
8 var assertThrows = chrome.test.assertThrows;
9 var fail = chrome.test.callbackFail;
10 var pass = chrome.test.callbackPass;
11 var listenOnce = chrome.test.listenOnce;
13 var NOT_OPTIONAL_ERROR =
14 "Optional permissions must be listed in extension manifest.";
17 "You cannot remove required permissions.";
19 var NOT_WHITE_LISTED_ERROR =
20 "The optional permissions API does not support '*'.";
22 var UNKNOWN_PERMISSIONS_ERROR =
23 "'*' is not a recognized permission.";
25 var emptyPermissions = {permissions: [], origins: []};
27 var initialPermissions = {
28 permissions: ['management'],
29 origins: ['http://a.com/*']
32 var permissionsWithBookmarks = {
33 permissions: ['management', 'bookmarks'],
34 origins: ['http://a.com/*']
37 var permissionsWithOrigin = {
38 permissions: ['management'],
39 origins: ['http://a.com/*', 'http://*.c.com/*']
42 function checkEqualSets(set1, set2) {
43 if (set1.length != set2.length)
46 for (var x = 0; x < set1.length; x++) {
47 if (!set2.some(function(v) { return v == set1[x]; }))
54 function checkPermSetsEq(set1, set2) {
55 return checkEqualSets(set1.permissions, set2.permissions) &&
56 checkEqualSets(set1.origins, set2.origins);
59 chrome.test.getConfig(function(config) {
61 function doReq(domain, callback) {
62 var req = new XMLHttpRequest();
63 var url = domain + ":PORT/extensions/test_file.txt";
64 url = url.replace(/PORT/, config.testServer.port);
66 chrome.test.log("Requesting url: " + url);
67 req.open("GET", url, true);
69 req.onload = function() {
70 assertEq(200, req.status);
71 assertEq("Hello!", req.responseText);
75 req.onerror = function() {
76 chrome.test.log("status: " + req.status);
77 chrome.test.log("text: " + req.responseText);
84 chrome.test.runTests([
86 chrome.permissions.contains(
87 {permissions: ['management'], origins: ['http://a.com/*']},
88 pass(function(result) { assertTrue(result); }));
89 chrome.permissions.contains(
90 {permissions: ['devtools'], origins: ['http://a.com/*']},
91 pass(function(result) { assertFalse(result); }));
92 chrome.permissions.contains(
93 {permissions: ['management']},
94 pass(function(result) { assertTrue(result); }));
95 chrome.permissions.contains(
96 {permissions: ['management']},
97 pass(function(result) { assertTrue(result); }));
101 chrome.permissions.getAll(pass(function(permissions) {
102 assertTrue(checkPermSetsEq(initialPermissions, permissions));
106 // Nothing should happen if we request permission we already have
107 function requestNoOp() {
108 chrome.permissions.request(
109 {permissions:['management'], origins:['http://a.com/*']},
110 pass(function(granted) { assertTrue(granted); }));
113 // We should get an error when requesting permissions that haven't been
114 // defined in "optional_permissions".
115 function requestNonOptional() {
116 chrome.permissions.request(
117 {permissions: ['history']}, fail(NOT_OPTIONAL_ERROR));
118 chrome.permissions.request(
119 {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR));
120 chrome.permissions.request(
121 {permissions: ['history'], origins: ['http://*.b.com/*']},
122 fail(NOT_OPTIONAL_ERROR));
125 // We should be able to request the bookmarks API since it's in the granted
126 // permissions list (see permissions_apitest.cc).
127 function requestBookmarks() {
128 assertEq(undefined, chrome.bookmarks);
129 listenOnce(chrome.permissions.onAdded,
130 function(permissions) {
131 assertTrue(permissions.permissions.length == 1);
132 assertTrue(permissions.permissions[0] == 'bookmarks');
134 chrome.permissions.request(
135 {permissions:['bookmarks']},
136 pass(function(granted) {
138 chrome.bookmarks.getTree(pass(function(result) {
141 chrome.permissions.getAll(pass(function(permissions) {
142 assertTrue(checkPermSetsEq(permissionsWithBookmarks,
148 // These permissions should be on the granted list because they're on the
149 // extension's default permission list.
150 function requestGrantedPermission() {
151 chrome.permissions.request(
152 {permissions: ['management'], origins: ['http://a.com/*']},
153 pass(function(granted) {
158 // You can't remove required permissions.
159 function removeRequired() {
160 chrome.permissions.remove(
161 {permissions: ['management']}, fail(REQUIRED_ERROR));
162 chrome.permissions.remove(
163 {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR));
164 chrome.permissions.remove(
165 {permissions: ['bookmarks'], origins: ['http://a.com/*']},
166 fail(REQUIRED_ERROR));
169 // You can remove permissions you don't have (nothing happens).
170 function removeNoOp() {
171 chrome.permissions.remove(
172 {permissions:['background']},
173 pass(function(removed) { assertTrue(removed); }));
174 chrome.permissions.remove(
175 {origins:['http://*.c.com/*']},
176 pass(function(removed) { assertTrue(removed); }));
177 chrome.permissions.remove(
178 {permissions:['background'], origins:['http://*.c.com/*']},
179 pass(function(removed) { assertTrue(removed); }));
182 function removeBookmarks() {
183 chrome.bookmarks.getTree(pass(function(result) {
186 listenOnce(chrome.permissions.onRemoved,
187 function(permissions) {
188 assertTrue(permissions.permissions.length == 1);
189 assertTrue(permissions.permissions[0] == 'bookmarks');
191 chrome.permissions.remove(
192 {permissions:['bookmarks']},
194 chrome.permissions.getAll(pass(function(permissions) {
195 assertTrue(checkPermSetsEq(initialPermissions, permissions));
197 assertTrue(typeof chrome.bookmarks == 'object' &&
198 chrome.bookmarks != null);
200 chrome.bookmarks.getTree, [function(){}],
201 "'bookmarks' requires a different Feature that is not present.");
206 // The user shouldn't have to approve permissions that have no warnings.
207 function noPromptForNoWarnings() {
208 chrome.permissions.request(
209 {permissions: ['notifications']},
210 pass(function(granted) {
213 // Remove the notifications permission to return to normal.
214 chrome.permissions.remove(
215 {permissions: ['notifications']},
216 pass(function(removed) { assertTrue(removed); }));
220 // Make sure you can only access the white listed permissions.
221 function whitelist() {
222 var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'cloudPrintPrivate');
223 chrome.permissions.request(
224 {permissions: ['cloudPrintPrivate']}, fail(error_msg));
225 chrome.permissions.remove(
226 {permissions: ['cloudPrintPrivate']}, fail(error_msg));
229 function unknownPermission() {
230 var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf');
231 chrome.permissions.request(
232 {permissions: ['asdf']}, fail(error_msg));
235 function requestOrigin() {
236 doReq('http://c.com', pass(function(success) { assertFalse(success); }));
238 chrome.permissions.getAll(pass(function(permissions) {
239 assertTrue(checkPermSetsEq(initialPermissions, permissions));
242 listenOnce(chrome.permissions.onAdded,
243 function(permissions) {
244 assertTrue(permissions.permissions.length == 0);
245 assertTrue(permissions.origins.length == 1);
246 assertTrue(permissions.origins[0] == 'http://*.c.com/*');
248 chrome.permissions.request(
249 {origins: ['http://*.c.com/*']},
250 pass(function(granted) {
252 chrome.permissions.getAll(pass(function(permissions) {
253 assertTrue(checkPermSetsEq(permissionsWithOrigin, permissions));
255 chrome.permissions.contains(
256 {origins:['http://*.c.com/*']},
257 pass(function(result) { assertTrue(result); }));
258 doReq('http://c.com', pass(function(result) { assertTrue(result); }));
262 function removeOrigin() {
263 doReq('http://c.com', pass(function(result) { assertTrue(result); }));
265 listenOnce(chrome.permissions.onRemoved,
266 function(permissions) {
267 assertTrue(permissions.permissions.length == 0);
268 assertTrue(permissions.origins.length == 1);
269 assertTrue(permissions.origins[0] == 'http://*.c.com/*');
271 chrome.permissions.remove(
272 {origins: ['http://*.c.com/*']},
273 pass(function(removed) {
275 chrome.permissions.getAll(pass(function(permissions) {
276 assertTrue(checkPermSetsEq(initialPermissions, permissions));
278 chrome.permissions.contains(
279 {origins:['http://*.c.com/*']},
280 pass(function(result) { assertFalse(result); }));
281 doReq('http://c.com', pass(function(result) { assertFalse(result); }));
285 // Tests that the changed permissions have taken effect from inside the
286 // onAdded and onRemoved event listeners.
287 function eventListenerPermissions() {
288 listenOnce(chrome.permissions.onAdded,
289 function(permissions) {
290 chrome.bookmarks.getTree(pass(function() {
294 listenOnce(chrome.permissions.onRemoved,
295 function(permissions) {
296 assertTrue(typeof chrome.bookmarks == 'object' &&
297 chrome.bookmarks != null);
299 chrome.bookmarks.getTree, [function(){}],
300 "'bookmarks' requires a different Feature that is not present.");
303 chrome.permissions.request(
304 {permissions: ['bookmarks', 'management']}, pass(function(granted) {
306 chrome.permissions.remove(
307 {permissions: ['bookmarks']}, pass(function() {