OLD | NEW |
1 <script> | 1 <script> |
2 | 2 |
| 3 var assertEq = chrome.test.assertEq; |
3 var assertFalse = chrome.test.assertFalse; | 4 var assertFalse = chrome.test.assertFalse; |
4 var assertTrue = chrome.test.assertTrue; | 5 var assertTrue = chrome.test.assertTrue; |
5 var fail = chrome.test.callbackFail; | 6 var fail = chrome.test.callbackFail; |
6 var pass = chrome.test.callbackPass; | 7 var pass = chrome.test.callbackPass; |
7 var listenOnce = chrome.test.listenOnce; | 8 var listenOnce = chrome.test.listenOnce; |
8 | 9 |
9 var NOT_OPTIONAL_ERROR = | 10 var NOT_OPTIONAL_ERROR = |
10 "Optional permissions must be listed in extension manifest."; | 11 "Optional permissions must be listed in extension manifest."; |
11 | 12 |
12 var NO_TABS_PERMISSION = | 13 var NO_TABS_PERMISSION = |
13 "You do not have permission to use 'windows.getAll'."; | 14 "You do not have permission to use 'windows.getAll'."; |
14 | 15 |
15 var REQUIRED_ERROR = | 16 var REQUIRED_ERROR = |
16 "You cannot remove required permissions."; | 17 "You cannot remove required permissions."; |
17 | 18 |
18 var NOT_WHITE_LISTED_ERROR = | 19 var NOT_WHITE_LISTED_ERROR = |
19 "The optional permissions API does not support '*'."; | 20 "The optional permissions API does not support '*'."; |
20 | 21 |
21 var UNKNOWN_PERMISSIONS_ERROR = | 22 var UNKNOWN_PERMISSIONS_ERROR = |
22 "'*' is not a recognized permission."; | 23 "'*' is not a recognized permission."; |
23 | 24 |
24 var emptyPermissions = {permissions: []}; | 25 var emptyPermissions = {permissions: [], origins: []}; |
25 | 26 |
26 var initialPermissions = { | 27 var initialPermissions = { |
27 permissions: ['experimental', 'management', 'permissions'] | 28 permissions: ['experimental', 'management', 'permissions'], |
| 29 origins: ['http://a.com/*'] |
28 }; | 30 }; |
29 | 31 |
30 var permissionsWithTabs = { | 32 var permissionsWithTabs = { |
31 permissions: ['experimental', 'management', 'permissions', 'tabs'] | 33 permissions: ['experimental', 'management', 'permissions', 'tabs'], |
| 34 origins: ['http://a.com/*'] |
| 35 } |
| 36 |
| 37 var permissionsWithOrigin = { |
| 38 permissions: ['experimental', 'management', 'permissions'], |
| 39 origins: ['http://a.com/*', 'http://*.c.com/*'] |
32 } | 40 } |
33 | 41 |
34 function checkEqualSets(set1, set2) { | 42 function checkEqualSets(set1, set2) { |
35 if (set1.length != set2.length) | 43 if (set1.length != set2.length) |
36 return false; | 44 return false; |
37 | 45 |
38 for (var x = 0; x < set1.length; x++) { | 46 for (var x = 0; x < set1.length; x++) { |
39 if (!set2.some(function(v) { return v == set1[x]; })) | 47 if (!set2.some(function(v) { return v == set1[x]; })) |
40 return false; | 48 return false; |
41 } | 49 } |
42 | 50 |
43 return true; | 51 return true; |
44 } | 52 } |
45 | 53 |
46 function checkPermSetsEq(set1, set2) { | 54 function checkPermSetsEq(set1, set2) { |
47 return checkEqualSets(set1.permissions, set2.permissions); | 55 return checkEqualSets(set1.permissions, set2.permissions) && |
48 } | 56 checkEqualSets(set1.origins, set2.origins); |
49 | 57 } |
50 chrome.test.runTests([ | 58 |
51 function contains() { | 59 chrome.test.getConfig(function(config) { |
52 chrome.experimental.permissions.contains( | 60 |
53 {permissions: ['management']}, | 61 function doReq(domain, callback) { |
54 pass(function(result) { | 62 var req = new XMLHttpRequest(); |
55 assertTrue(result); | 63 var url = domain + ":PORT/files/extensions/test_file.txt"; |
56 })); | 64 url = url.replace(/PORT/, config.testServer.port); |
57 chrome.experimental.permissions.contains( | 65 |
58 {permissions: ['devtools']}, | 66 chrome.test.log("Requesting url: " + url); |
59 pass(function(result) { | 67 req.open("GET", url, true); |
60 assertFalse(result); | 68 |
61 })); | 69 req.onload = function() { |
62 chrome.experimental.permissions.contains( | 70 assertEq(200, req.status); |
63 {permissions: ['permissions', 'management']}, | 71 assertEq("Hello!", req.responseText); |
64 pass(function(result) { | 72 callback(true); |
65 assertTrue(result); | 73 }; |
66 })); | 74 |
67 chrome.experimental.permissions.contains( | 75 req.onerror = function() { |
68 {permissions: ['management', 'permissions']}, | 76 chrome.test.log("status: " + req.status); |
69 pass(function(result) { | 77 chrome.test.log("text: " + req.responseText); |
70 assertTrue(result); | 78 callback(false); |
71 })); | 79 }; |
72 }, | 80 |
73 | 81 req.send(null); |
74 function getAll() { | 82 } |
75 chrome.experimental.permissions.getAll(pass(function(permissions) { | 83 |
76 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | 84 chrome.test.runTests([ |
77 })); | 85 function contains() { |
78 }, | 86 chrome.experimental.permissions.contains( |
79 | 87 {permissions: ['management'], origins: ['http://a.com/*']}, |
80 // Nothing should happen if we request permission we already have | 88 pass(function(result) { assertTrue(result); })); |
81 function requestNoOp() { | 89 chrome.experimental.permissions.contains( |
82 chrome.experimental.permissions.request( | 90 {permissions: ['devtools'], origins: ['http://a.com/*']}, |
83 {permissions:['management']}, | 91 pass(function(result) { assertFalse(result); })); |
84 pass(function(granted) { | 92 chrome.experimental.permissions.contains( |
| 93 {permissions: ['permissions', 'management']}, |
| 94 pass(function(result) { assertTrue(result); })); |
| 95 chrome.experimental.permissions.contains( |
| 96 {permissions: ['management', 'permissions']}, |
| 97 pass(function(result) { assertTrue(result); })); |
| 98 }, |
| 99 |
| 100 function getAll() { |
| 101 chrome.experimental.permissions.getAll(pass(function(permissions) { |
| 102 assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
| 103 })); |
| 104 }, |
| 105 |
| 106 // Nothing should happen if we request permission we already have |
| 107 function requestNoOp() { |
| 108 chrome.experimental.permissions.request( |
| 109 {permissions:['management'], origins:['http://a.com/*']}, |
| 110 pass(function(granted) { assertTrue(granted); })); |
| 111 }, |
| 112 |
| 113 // We should get an error when requesting permissions that haven't been |
| 114 // defined in "optional_permissions". |
| 115 function requestNonOptional() { |
| 116 chrome.experimental.permissions.request( |
| 117 {permissions: ['debugger']}, fail(NOT_OPTIONAL_ERROR)); |
| 118 chrome.experimental.permissions.request( |
| 119 {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR)); |
| 120 chrome.experimental.permissions.request( |
| 121 {permissions: ['tabs'], origins: ['http://*.b.com/*']}, |
| 122 fail(NOT_OPTIONAL_ERROR)); |
| 123 }, |
| 124 |
| 125 // We should be able to request the tabs API since it's in the granted |
| 126 // permissions list (see permissions_apitest.cc). |
| 127 function requestTabs() { |
| 128 try { |
| 129 chrome.windows.getAll({populate: true}, function() { |
| 130 chrome.test.fail("Should not have tabs API permission."); |
| 131 }); |
| 132 } catch (e) { |
| 133 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); |
| 134 } |
| 135 listenOnce(chrome.experimental.permissions.onAdded, |
| 136 function(permissions) { |
| 137 assertTrue(permissions.permissions.length == 1); |
| 138 assertTrue(permissions.permissions[0] == 'tabs'); |
| 139 }); |
| 140 chrome.experimental.permissions.request( |
| 141 {permissions:['tabs']}, |
| 142 pass(function(granted) { |
| 143 assertTrue(granted); |
| 144 chrome.windows.getAll({populate: true}, pass(function(windows) { |
| 145 assertTrue(true); |
| 146 })); |
| 147 chrome.experimental.permissions.getAll(pass(function(permissions) { |
| 148 assertTrue(checkPermSetsEq(permissionsWithTabs, permissions)); |
| 149 })); |
| 150 })); |
| 151 }, |
| 152 |
| 153 // You can't remove required permissions. |
| 154 function removeRequired() { |
| 155 chrome.experimental.permissions.remove( |
| 156 {permissions: ['management']}, fail(REQUIRED_ERROR)); |
| 157 chrome.experimental.permissions.remove( |
| 158 {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR)); |
| 159 chrome.experimental.permissions.remove( |
| 160 {permissions: ['tabs'], origins: ['http://a.com/*']}, |
| 161 fail(REQUIRED_ERROR)); |
| 162 }, |
| 163 |
| 164 // You can remove permissions you don't have (nothing happens). |
| 165 function removeNoOp() { |
| 166 chrome.experimental.permissions.remove( |
| 167 {permissions:['background']}, |
| 168 pass(function(removed) { assertTrue(removed); })); |
| 169 chrome.experimental.permissions.remove( |
| 170 {origins:['http://*.c.com/*']}, |
| 171 pass(function(removed) { assertTrue(removed); })); |
| 172 chrome.experimental.permissions.remove( |
| 173 {permissions:['background'], origins:['http://*.c.com/*']}, |
| 174 pass(function(removed) { assertTrue(removed); })); |
| 175 }, |
| 176 |
| 177 function removeTabs() { |
| 178 chrome.windows.getAll({populate: true}, pass(function(windows) { |
| 179 assertTrue(true); |
| 180 })); |
| 181 listenOnce(chrome.experimental.permissions.onRemoved, |
| 182 function(permissions) { |
| 183 assertTrue(permissions.permissions.length == 1); |
| 184 assertTrue(permissions.permissions[0] == 'tabs'); |
| 185 }); |
| 186 chrome.experimental.permissions.remove( |
| 187 {permissions:['tabs']}, |
| 188 pass(function() { |
| 189 chrome.experimental.permissions.getAll(pass(function(permissions) { |
| 190 assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
| 191 })); |
| 192 try { |
| 193 chrome.windows.getAll({populate: true}, function() { |
| 194 chrome.test.fail("Should not have tabs API permission."); |
| 195 }); |
| 196 } catch (e) { |
| 197 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); |
| 198 } |
| 199 })); |
| 200 }, |
| 201 |
| 202 // The user shouldn't have to approve permissions that have no warnings. |
| 203 function noPromptForNoWarnings() { |
| 204 chrome.experimental.permissions.request( |
| 205 {permissions: ['notifications']}, |
| 206 pass(function(granted) { |
85 assertTrue(granted); | 207 assertTrue(granted); |
86 })); | 208 |
87 }, | 209 // Remove the notifications permission to return to normal. |
88 | 210 chrome.experimental.permissions.remove( |
89 // We should get an error when requesting permissions that haven't been | 211 {permissions: ['notifications']}, |
90 // defined in "optional_permissions". | 212 pass(function(removed) { assertTrue(removed); })); |
91 function requestNonOptional() { | 213 })); |
92 chrome.experimental.permissions.request( | 214 }, |
93 {permissions:['debugger']}, fail(NOT_OPTIONAL_ERROR)); | 215 |
94 }, | 216 // Make sure you can only access the white listed permissions. |
95 | 217 function whitelist() { |
96 // We should be able to request the tabs API since it's in the granted | 218 var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'chromeAuthPrivate'); |
97 // permissions list (see permissions_apitest.cc). | 219 chrome.experimental.permissions.request( |
98 function requestTabs() { | 220 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); |
99 try { | 221 chrome.experimental.permissions.remove( |
100 chrome.windows.getAll({populate: true}, function() { | 222 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); |
101 chrome.test.fail("Should not have tabs API permission."); | 223 }, |
102 }); | 224 |
103 } catch (e) { | 225 function unknownPermission() { |
104 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | 226 var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf'); |
105 } | 227 chrome.experimental.permissions.request( |
106 listenOnce(chrome.experimental.permissions.onAdded, function(permissions) { | 228 {permissions: ['asdf']}, fail(error_msg)); |
107 assertTrue(permissions.permissions.length == 1); | 229 }, |
108 assertTrue(permissions.permissions[0] == 'tabs'); | 230 |
109 }); | 231 function requestOrigin() { |
110 chrome.experimental.permissions.request( | 232 doReq('http://c.com', pass(function(success) { assertFalse(success); })); |
111 {permissions:['tabs']}, | 233 |
112 pass(function(granted) { | 234 chrome.experimental.permissions.getAll(pass(function(permissions) { |
| 235 assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
| 236 })); |
| 237 |
| 238 listenOnce(chrome.experimental.permissions.onAdded, |
| 239 function(permissions) { |
| 240 assertTrue(permissions.permissions.length == 0); |
| 241 assertTrue(permissions.origins.length == 1); |
| 242 assertTrue(permissions.origins[0] == 'http://*.c.com/*'); |
| 243 }); |
| 244 chrome.experimental.permissions.request( |
| 245 {origins: ['http://*.c.com/*']}, |
| 246 pass(function(granted) { |
113 assertTrue(granted); | 247 assertTrue(granted); |
114 chrome.windows.getAll({populate: true}, pass(function(windows) { | 248 chrome.experimental.permissions.getAll(pass(function(permissions) { |
115 assertTrue(true); | 249 assertTrue(checkPermSetsEq(permissionsWithOrigin, permissions)); |
116 })); | 250 })); |
117 chrome.experimental.permissions.getAll(pass(function(permissions) { | 251 chrome.experimental.permissions.contains( |
118 assertTrue(checkPermSetsEq(permissionsWithTabs, permissions)); | 252 {origins:['http://*.c.com/*']}, |
119 })); | 253 pass(function(result) { assertTrue(result); })); |
120 })); | 254 doReq('http://c.com', pass(function(result) { assertTrue(result); })); |
121 }, | 255 })); |
122 | 256 }, |
123 // You can't remove required permissions. | 257 |
124 function removeRequired() { | 258 function removeOrigin() { |
125 chrome.experimental.permissions.remove( | 259 doReq('http://c.com', pass(function(result) { assertTrue(result); })); |
126 {permissions:['management']}, fail(REQUIRED_ERROR)); | 260 |
127 }, | 261 listenOnce(chrome.experimental.permissions.onRemoved, |
128 | 262 function(permissions) { |
129 // You can remove permissions you don't have (nothing happens). | 263 assertTrue(permissions.permissions.length == 0); |
130 function removeNoOp() { | 264 assertTrue(permissions.origins.length == 1); |
131 chrome.experimental.permissions.remove( | 265 assertTrue(permissions.origins[0] == 'http://*.c.com/*'); |
132 {permissions:['background']}, | 266 }); |
133 pass(function(removed) { | 267 chrome.experimental.permissions.remove( |
| 268 {origins: ['http://*.c.com/*']}, |
| 269 pass(function(removed) { |
134 assertTrue(removed); | 270 assertTrue(removed); |
135 })); | |
136 }, | |
137 | |
138 function removeTabs() { | |
139 chrome.windows.getAll({populate: true}, pass(function(windows) { | |
140 assertTrue(true); | |
141 })); | |
142 listenOnce(chrome.experimental.permissions.onRemoved, | |
143 function(permissions) { | |
144 assertTrue(permissions.permissions.length == 1); | |
145 assertTrue(permissions.permissions[0] == 'tabs'); | |
146 }); | |
147 chrome.experimental.permissions.remove( | |
148 {permissions:['tabs']}, | |
149 pass(function() { | |
150 chrome.experimental.permissions.getAll(pass(function(permissions) { | 271 chrome.experimental.permissions.getAll(pass(function(permissions) { |
151 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | 272 assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
152 })); | 273 })); |
153 try { | 274 chrome.experimental.permissions.contains( |
154 chrome.windows.getAll({populate: true}, function() { | 275 {origins:['http://*.c.com/*']}, |
155 chrome.test.fail("Should not have tabs API permission."); | 276 pass(function(result) { assertFalse(result); })); |
156 }); | 277 doReq('http://c.com', pass(function(result) { assertFalse(result); })); |
157 } catch (e) { | 278 })); |
158 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | 279 } |
159 } | 280 |
160 })); | 281 ]); |
161 }, | 282 }); |
162 | |
163 // The user shouldn't have to approve permissions that have no warnings. | |
164 // TODO(jstritar): move to its own test and set a low timeout | |
165 function noPromptForNoWarnings() { | |
166 chrome.experimental.permissions.request( | |
167 {permissions: ['notifications']}, | |
168 pass(function(granted) { | |
169 assertTrue(granted); | |
170 })); | |
171 }, | |
172 | |
173 // Make sure you can only access the white listed permissions. | |
174 function whitelist() { | |
175 var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'chromeAuthPrivate'); | |
176 chrome.experimental.permissions.request( | |
177 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); | |
178 chrome.experimental.permissions.remove( | |
179 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); | |
180 }, | |
181 | |
182 function unknownPermission() { | |
183 var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf'); | |
184 chrome.experimental.permissions.request( | |
185 {permissions: ['asdf']}, fail(error_msg)); | |
186 } | |
187 ]); | |
188 </script> | 283 </script> |
OLD | NEW |