Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 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 var ERROR = 'Optional permissions must be listed in extension manifest.'; | |
| 6 var test = chrome.test; | |
| 7 | |
| 8 // The URL patterns that we've supposedly been granted access to so far. Use | |
| 9 // this to verify queried hosts for each test. | |
| 10 var grantedHosts = []; | |
| 11 | |
| 12 // Pushes a value onto an array, asserting that it's unique in the array. | |
| 13 function pushUniqueValue(array, value) { | |
| 14 test.assertEq(-1, array.indexOf(value)); | |
| 15 array.push(value); | |
| 16 } | |
| 17 | |
| 18 // Removes a value from an array, asserting that it's unique in the array. | |
| 19 function removeUniqueValue(array, value) { | |
| 20 var indexOfValue = array.indexOf(value); | |
| 21 test.assertTrue(indexOfValue >= 0); | |
| 22 array.splice(indexOfValue, 1); | |
| 23 test.assertEq(-1, array.indexOf(value)); | |
| 24 } | |
| 25 | |
| 26 // Asserts that two arrays are equal when treated as sets. | |
| 27 function assertSetEq(expected, actual) { | |
| 28 if (!actual) | |
| 29 actual = []; | |
| 30 test.assertEq(expected.slice().sort(), actual.slice().sort()); | |
| 31 } | |
| 32 | |
| 33 // Validates that the origin permissions returned from getAll() and contains() | |
| 34 // match |grantedHosts|. | |
| 35 function checkGrantedHosts(callback) { | |
| 36 chrome.permissions.getAll(test.callbackPass(function(permissions) { | |
| 37 assertSetEq(grantedHosts, permissions.origins); | |
| 38 var countDown = grantedHosts.length; | |
| 39 if (countDown == 0) { | |
| 40 callback(); | |
| 41 return; | |
| 42 } | |
| 43 grantedHosts.forEach(function(host) { | |
| 44 chrome.permissions.contains({origins: [host]}, | |
| 45 test.callbackPass(function(contains) { | |
| 46 test.assertTrue(contains); | |
| 47 if (--countDown == 0) | |
| 48 callback(); | |
| 49 })); | |
| 50 }); | |
| 51 })); | |
| 52 } | |
| 53 | |
| 54 // Returns a function which requests access to a host, and afterwards checks | |
| 55 // that our expected host permissions agree with Chrome's. | |
| 56 function requestHost(host, expectedGranted, expectedError) { | |
| 57 return function(callback) { | |
| 58 chrome.permissions.request({origins: [host]}, | |
| 59 test.callback(function(granted) { | |
| 60 if (granted) | |
| 61 pushUniqueValue(grantedHosts, host); | |
| 62 if (expectedGranted) { | |
| 63 test.assertTrue( | |
| 64 granted, | |
| 65 "Access to " + host + " was not granted, but should have been"); | |
| 66 } else { | |
| 67 test.assertFalse( | |
| 68 granted, | |
| 69 "Access to " + host + " was granted, but should not have been"); | |
| 70 } | |
| 71 checkGrantedHosts(callback); | |
| 72 }, expectedError)); | |
| 73 }; | |
| 74 } | |
| 75 | |
| 76 // Returns a function which removes access to a host, and afterwards checks | |
| 77 // that our expected host permissions agree with Chrome's. | |
| 78 function removeHost(host, expectedRemoved) { | |
| 79 return function(callback) { | |
| 80 chrome.permissions.remove({origins: [host]}, | |
| 81 test.callbackPass(function(removed) { | |
| 82 if (removed) { | |
| 83 test.assertTrue(expectedRemoved, "Access to " + host + " removed"); | |
| 84 removeUniqueValue(grantedHosts, host); | |
| 85 } else { | |
| 86 test.assertFalse(expectedRemoved, "Access to " + host + " not removed"); | |
| 87 } | |
| 88 checkGrantedHosts(callback); | |
| 89 })); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // Returns a function which checks that permissions.contains(host) returns | |
| 94 // |expected|. | |
| 95 function contains(host, expected) { | |
| 96 return function(callback) { | |
| 97 chrome.permissions.contains({origins: [host]}, | |
| 98 test.callbackPass(function(result) { | |
| 99 test.assertEq(expected, result); | |
| 100 callback(); | |
| 101 })); | |
| 102 }; | |
| 103 } | |
| 104 | |
| 105 // Calls every function in |chain| passing each the next function to run as a | |
| 106 // single argument - except for the last function, which is *not* expected to | |
| 107 // have a callback. | |
| 108 // | |
| 109 // The test is kept alive until all callbacks in the chain have been run. | |
| 110 function chain(callbacks) { | |
| 111 var head = callbacks[0], tail = callbacks.slice(1); | |
| 112 if (tail.length == 0) { | |
| 113 head(); | |
| 114 return; | |
| 115 } | |
| 116 var callbackCompleted = chrome.test.callbackAdded(); | |
| 117 head(function() { | |
| 118 try { | |
| 119 chain(tail); | |
| 120 } finally { | |
| 121 callbackCompleted(); | |
| 122 } | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 function main() { | |
| 127 chain([ | |
| 128 // Simple request #1. | |
| 129 contains('http://google.com/*', false), | |
| 130 requestHost('http://google.com/*', true), | |
| 131 contains('http://google.com/*', true), | |
| 132 contains('http://google.com/foo/*', true), | |
| 133 contains('http://google.com:1234/foo/*', true), | |
| 134 contains('http://www.google.com/*', false), | |
| 135 | |
| 136 // Simple request #2. | |
| 137 requestHost('http://*.yahoo.com/*', true), | |
| 138 contains('http://yahoo.com/*', true), | |
| 139 contains('http://yahoo.com/foo/*', true), | |
| 140 contains('http://www.yahoo.com/*', true), | |
| 141 contains('http://www.yahoo.com/foo/*', true), | |
| 142 contains('http://www.yahoo.com:5678/foo/*', true), | |
| 143 | |
| 144 // Can request access to a subset of a host that's allowed. | |
| 145 requestHost('http://yahoo.com/*', true), | |
| 146 | |
| 147 // Can remove it. | |
| 148 removeHost('http://yahoo.com/*', true), | |
| 149 | |
| 150 // However, since it's still granted by the *.yahoo.com/* | |
| 151 // permission, it's still considered to contain it. | |
| 152 contains('http://yahoo.com/*', true), | |
| 153 | |
| 154 // Can remove the whole host, though. | |
| 155 removeHost('http://*.yahoo.com/*', true), | |
| 156 contains('http://google.com/*', true), | |
| 157 contains('http://yahoo.com/*', false), | |
| 158 contains('http://www.yahoo.com/*', false), | |
| 159 | |
| 160 // Widening the net (and then deleting it). | |
| 161 requestHost('http://*.google.com/*', true), | |
| 162 contains('http://google.com/*', true), | |
| 163 contains('http://www.google.com/*', true), | |
| 164 removeHost('http://google.com/*', true), | |
| 165 contains('http://google.com/*', true), | |
| 166 contains('http://www.google.com/*', true), | |
| 167 removeHost('http://*.google.com/*', true), | |
| 168 contains('http://google.com/*', false), | |
| 169 contains('http://www.google.com/*', false), | |
| 170 | |
| 171 // https schemes should fail because they're not covered by | |
| 172 // this extension's optional permission pattern. | |
| 173 requestHost('https://google.com/*', false, ERROR), | |
| 174 requestHost('https://*.yahoo.com/*', false, ERROR), | |
| 175 | |
| 176 // There is a sneaky ftp://ftp.example.com/* pattern in the | |
| 177 // manifest. Test it. | |
| 178 requestHost('ftp://example.com/*', false, ERROR), | |
| 179 contains('ftp://example.com/*', false), | |
| 180 requestHost('ftp://www.example.com/*', false, ERROR), | |
| 181 contains('ftp://www.example.com/*', false), | |
| 182 requestHost('ftp://secret.ftp.example.com/*', false, ERROR), | |
| 183 contains('ftp://secret.ftp.example.com/*', false), | |
| 184 requestHost('ftp://ftp.example.com/*', true), | |
| 185 contains('ftp://ftp.example.com/*', true), | |
| 186 removeHost('ftp://ftp.example.com/*', true), | |
| 187 contains('ftp://ftp.example.com/*', false), | |
| 188 | |
| 189 // And finally... | |
| 190 test.succeed]); | |
| 191 } | |
| 192 | |
| 193 test.runTests([main]); | |
|
Yoyo Zhou
2013/02/12 06:46:25
Not super important, but this doesn't have to be a
not at google - send to devlin
2013/02/12 17:06:43
True, but that'd mess up the beautiful chain of co
| |
| OLD | NEW |