Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | // Tests for b2g/components/SignInToWebsite.jsm |
michael@0 | 5 | |
michael@0 | 6 | "use strict"; |
michael@0 | 7 | |
michael@0 | 8 | XPCOMUtils.defineLazyModuleGetter(this, "MinimalIDService", |
michael@0 | 9 | "resource://gre/modules/identity/MinimalIdentity.jsm", |
michael@0 | 10 | "IdentityService"); |
michael@0 | 11 | |
michael@0 | 12 | XPCOMUtils.defineLazyModuleGetter(this, "SignInToWebsiteController", |
michael@0 | 13 | "resource://gre/modules/SignInToWebsite.jsm", |
michael@0 | 14 | "SignInToWebsiteController"); |
michael@0 | 15 | |
michael@0 | 16 | Cu.import("resource://gre/modules/identity/LogUtils.jsm"); |
michael@0 | 17 | |
michael@0 | 18 | function log(...aMessageArgs) { |
michael@0 | 19 | Logger.log.apply(Logger, ["test_signintowebsite"].concat(aMessageArgs)); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | function test_overall() { |
michael@0 | 23 | do_check_neq(MinimalIDService, null); |
michael@0 | 24 | run_next_test(); |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | function objectContains(object, subset) { |
michael@0 | 28 | let objectKeys = Object.keys(object); |
michael@0 | 29 | let subsetKeys = Object.keys(subset); |
michael@0 | 30 | |
michael@0 | 31 | // can't have fewer keys than the subset |
michael@0 | 32 | if (objectKeys.length < subsetKeys.length) { |
michael@0 | 33 | return false; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | let key; |
michael@0 | 37 | let success = true; |
michael@0 | 38 | if (subsetKeys.length > 0) { |
michael@0 | 39 | for (let i=0; i<subsetKeys.length; i++) { |
michael@0 | 40 | key = subsetKeys[i]; |
michael@0 | 41 | |
michael@0 | 42 | // key exists in the source object |
michael@0 | 43 | if (typeof object[key] === 'undefined') { |
michael@0 | 44 | success = false; |
michael@0 | 45 | break; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // recursively check object values |
michael@0 | 49 | else if (typeof subset[key] === 'object') { |
michael@0 | 50 | if (typeof object[key] !== 'object') { |
michael@0 | 51 | success = false; |
michael@0 | 52 | break; |
michael@0 | 53 | } |
michael@0 | 54 | if (! objectContains(object[key], subset[key])) { |
michael@0 | 55 | success = false; |
michael@0 | 56 | break; |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | else if (object[key] !== subset[key]) { |
michael@0 | 61 | success = false; |
michael@0 | 62 | break; |
michael@0 | 63 | } |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | return success; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | function test_object_contains() { |
michael@0 | 71 | do_test_pending(); |
michael@0 | 72 | |
michael@0 | 73 | let someObj = { |
michael@0 | 74 | pies: 42, |
michael@0 | 75 | green: "spam", |
michael@0 | 76 | flan: {yes: "please"} |
michael@0 | 77 | }; |
michael@0 | 78 | let otherObj = { |
michael@0 | 79 | pies: 42, |
michael@0 | 80 | flan: {yes: "please"} |
michael@0 | 81 | }; |
michael@0 | 82 | do_check_true(objectContains(someObj, otherObj)); |
michael@0 | 83 | do_test_finished(); |
michael@0 | 84 | run_next_test(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | function test_mock_doc() { |
michael@0 | 88 | do_test_pending(); |
michael@0 | 89 | let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) { |
michael@0 | 90 | do_check_eq(action, 'coffee'); |
michael@0 | 91 | do_test_finished(); |
michael@0 | 92 | run_next_test(); |
michael@0 | 93 | }); |
michael@0 | 94 | |
michael@0 | 95 | // A smoke test to ensure that mockedDoc is functioning correctly. |
michael@0 | 96 | // There is presently no doCoffee method in Persona. |
michael@0 | 97 | mockedDoc.doCoffee(); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | function test_watch() { |
michael@0 | 101 | do_test_pending(); |
michael@0 | 102 | |
michael@0 | 103 | setup_test_identity("pie@food.gov", TEST_CERT, function() { |
michael@0 | 104 | let controller = SignInToWebsiteController; |
michael@0 | 105 | |
michael@0 | 106 | let mockedDoc = mockDoc({loggedInUser: null}, function(action, params) { |
michael@0 | 107 | do_check_eq(action, 'ready'); |
michael@0 | 108 | controller.uninit(); |
michael@0 | 109 | MinimalIDService.RP.unwatch(mockedDoc.id); |
michael@0 | 110 | do_test_finished(); |
michael@0 | 111 | run_next_test(); |
michael@0 | 112 | }); |
michael@0 | 113 | |
michael@0 | 114 | controller.init({pipe: mockReceivingPipe()}); |
michael@0 | 115 | MinimalIDService.RP.watch(mockedDoc, {}); |
michael@0 | 116 | }); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | function test_request_login() { |
michael@0 | 120 | do_test_pending(); |
michael@0 | 121 | |
michael@0 | 122 | setup_test_identity("flan@food.gov", TEST_CERT, function() { |
michael@0 | 123 | let controller = SignInToWebsiteController; |
michael@0 | 124 | |
michael@0 | 125 | let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( |
michael@0 | 126 | function(action, params) { |
michael@0 | 127 | do_check_eq(action, 'ready'); |
michael@0 | 128 | do_check_eq(params, undefined); |
michael@0 | 129 | }, |
michael@0 | 130 | function(action, params) { |
michael@0 | 131 | do_check_eq(action, 'login'); |
michael@0 | 132 | do_check_eq(params, TEST_CERT); |
michael@0 | 133 | controller.uninit(); |
michael@0 | 134 | MinimalIDService.RP.unwatch(mockedDoc.id); |
michael@0 | 135 | do_test_finished(); |
michael@0 | 136 | run_next_test(); |
michael@0 | 137 | } |
michael@0 | 138 | )); |
michael@0 | 139 | |
michael@0 | 140 | controller.init({pipe: mockReceivingPipe()}); |
michael@0 | 141 | MinimalIDService.RP.watch(mockedDoc, {}); |
michael@0 | 142 | MinimalIDService.RP.request(mockedDoc.id, {}); |
michael@0 | 143 | }); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | function test_request_logout() { |
michael@0 | 147 | do_test_pending(); |
michael@0 | 148 | |
michael@0 | 149 | setup_test_identity("flan@food.gov", TEST_CERT, function() { |
michael@0 | 150 | let controller = SignInToWebsiteController; |
michael@0 | 151 | |
michael@0 | 152 | let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( |
michael@0 | 153 | function(action, params) { |
michael@0 | 154 | do_check_eq(action, 'ready'); |
michael@0 | 155 | do_check_eq(params, undefined); |
michael@0 | 156 | }, |
michael@0 | 157 | function(action, params) { |
michael@0 | 158 | do_check_eq(action, 'logout'); |
michael@0 | 159 | do_check_eq(params, undefined); |
michael@0 | 160 | controller.uninit(); |
michael@0 | 161 | MinimalIDService.RP.unwatch(mockedDoc.id); |
michael@0 | 162 | do_test_finished(); |
michael@0 | 163 | run_next_test(); |
michael@0 | 164 | } |
michael@0 | 165 | )); |
michael@0 | 166 | |
michael@0 | 167 | controller.init({pipe: mockReceivingPipe()}); |
michael@0 | 168 | MinimalIDService.RP.watch(mockedDoc, {}); |
michael@0 | 169 | MinimalIDService.RP.logout(mockedDoc.id, {}); |
michael@0 | 170 | }); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | function test_request_login_logout() { |
michael@0 | 174 | do_test_pending(); |
michael@0 | 175 | |
michael@0 | 176 | setup_test_identity("unagi@food.gov", TEST_CERT, function() { |
michael@0 | 177 | let controller = SignInToWebsiteController; |
michael@0 | 178 | |
michael@0 | 179 | let mockedDoc = mockDoc({loggedInUser: null}, call_sequentially( |
michael@0 | 180 | function(action, params) { |
michael@0 | 181 | do_check_eq(action, 'ready'); |
michael@0 | 182 | do_check_eq(params, undefined); |
michael@0 | 183 | }, |
michael@0 | 184 | function(action, params) { |
michael@0 | 185 | do_check_eq(action, 'login'); |
michael@0 | 186 | do_check_eq(params, TEST_CERT); |
michael@0 | 187 | }, |
michael@0 | 188 | function(action, params) { |
michael@0 | 189 | do_check_eq(action, 'logout'); |
michael@0 | 190 | do_check_eq(params, undefined); |
michael@0 | 191 | controller.uninit(); |
michael@0 | 192 | MinimalIDService.RP.unwatch(mockedDoc.id); |
michael@0 | 193 | do_test_finished(); |
michael@0 | 194 | run_next_test(); |
michael@0 | 195 | } |
michael@0 | 196 | )); |
michael@0 | 197 | |
michael@0 | 198 | controller.init({pipe: mockReceivingPipe()}); |
michael@0 | 199 | MinimalIDService.RP.watch(mockedDoc, {}); |
michael@0 | 200 | MinimalIDService.RP.request(mockedDoc.id, {}); |
michael@0 | 201 | MinimalIDService.RP.logout(mockedDoc.id, {}); |
michael@0 | 202 | }); |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | function test_logout_everywhere() { |
michael@0 | 206 | do_test_pending(); |
michael@0 | 207 | let logouts = 0; |
michael@0 | 208 | |
michael@0 | 209 | setup_test_identity("fugu@food.gov", TEST_CERT, function() { |
michael@0 | 210 | let controller = SignInToWebsiteController; |
michael@0 | 211 | |
michael@0 | 212 | let mockedDoc1 = mockDoc({loggedInUser: null}, call_sequentially( |
michael@0 | 213 | function(action, params) { |
michael@0 | 214 | do_check_eq(action, 'ready'); |
michael@0 | 215 | }, |
michael@0 | 216 | function(action, params) { |
michael@0 | 217 | do_check_eq(action, 'login'); |
michael@0 | 218 | }, |
michael@0 | 219 | function(action, params) { |
michael@0 | 220 | // Result of logout from doc2. |
michael@0 | 221 | // We don't know what order the logouts will occur in. |
michael@0 | 222 | do_check_eq(action, 'logout'); |
michael@0 | 223 | if (++logouts === 2) { |
michael@0 | 224 | do_test_finished(); |
michael@0 | 225 | run_next_test(); |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | )); |
michael@0 | 229 | |
michael@0 | 230 | let mockedDoc2 = mockDoc({loggedInUser: null}, call_sequentially( |
michael@0 | 231 | function(action, params) { |
michael@0 | 232 | do_check_eq(action, 'ready'); |
michael@0 | 233 | }, |
michael@0 | 234 | function(action, params) { |
michael@0 | 235 | do_check_eq(action, 'login'); |
michael@0 | 236 | }, |
michael@0 | 237 | function(action, params) { |
michael@0 | 238 | do_check_eq(action, 'logout'); |
michael@0 | 239 | if (++logouts === 2) { |
michael@0 | 240 | do_test_finished(); |
michael@0 | 241 | run_next_test(); |
michael@0 | 242 | } |
michael@0 | 243 | } |
michael@0 | 244 | )); |
michael@0 | 245 | |
michael@0 | 246 | controller.init({pipe: mockReceivingPipe()}); |
michael@0 | 247 | MinimalIDService.RP.watch(mockedDoc1, {}); |
michael@0 | 248 | MinimalIDService.RP.request(mockedDoc1.id, {}); |
michael@0 | 249 | |
michael@0 | 250 | MinimalIDService.RP.watch(mockedDoc2, {}); |
michael@0 | 251 | MinimalIDService.RP.request(mockedDoc2.id, {}); |
michael@0 | 252 | |
michael@0 | 253 | // Logs out of both docs because they share the |
michael@0 | 254 | // same origin. |
michael@0 | 255 | MinimalIDService.RP.logout(mockedDoc2.id, {}); |
michael@0 | 256 | }); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | function test_options_pass_through() { |
michael@0 | 260 | do_test_pending(); |
michael@0 | 261 | |
michael@0 | 262 | // An meaningless structure for testing that RP messages preserve |
michael@0 | 263 | // objects and their parameters as they are passed back and forth. |
michael@0 | 264 | let randomMixedParams = { |
michael@0 | 265 | loggedInUser: "juanita@mozilla.com", |
michael@0 | 266 | forceAuthentication: true, |
michael@0 | 267 | forceIssuer: "foo.com", |
michael@0 | 268 | someThing: { |
michael@0 | 269 | name: "Pertelote", |
michael@0 | 270 | legs: 4, |
michael@0 | 271 | nested: {bee: "Eric", remaining: "1/2"} |
michael@0 | 272 | } |
michael@0 | 273 | }; |
michael@0 | 274 | |
michael@0 | 275 | let mockedDoc = mockDoc(randomMixedParams, function(action, params) {}); |
michael@0 | 276 | |
michael@0 | 277 | function pipeOtherEnd(rpOptions, gaiaOptions) { |
michael@0 | 278 | // Ensure that every time we receive a message, our mixed |
michael@0 | 279 | // random params are contained in that message |
michael@0 | 280 | do_check_true(objectContains(rpOptions, randomMixedParams)); |
michael@0 | 281 | |
michael@0 | 282 | switch (gaiaOptions.message) { |
michael@0 | 283 | case "identity-delegate-watch": |
michael@0 | 284 | MinimalIDService.RP.request(mockedDoc.id, {}); |
michael@0 | 285 | break; |
michael@0 | 286 | case "identity-delegate-request": |
michael@0 | 287 | MinimalIDService.RP.logout(mockedDoc.id, {}); |
michael@0 | 288 | break; |
michael@0 | 289 | case "identity-delegate-logout": |
michael@0 | 290 | do_test_finished(); |
michael@0 | 291 | controller.uninit(); |
michael@0 | 292 | MinimalIDService.RP.unwatch(mockedDoc.id); |
michael@0 | 293 | run_next_test(); |
michael@0 | 294 | break; |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | let controller = SignInToWebsiteController; |
michael@0 | 299 | controller.init({pipe: mockSendingPipe(pipeOtherEnd)}); |
michael@0 | 300 | |
michael@0 | 301 | MinimalIDService.RP.watch(mockedDoc, {}); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | let TESTS = [ |
michael@0 | 305 | test_overall, |
michael@0 | 306 | test_mock_doc, |
michael@0 | 307 | test_object_contains, |
michael@0 | 308 | |
michael@0 | 309 | test_watch, |
michael@0 | 310 | test_request_login, |
michael@0 | 311 | test_request_logout, |
michael@0 | 312 | test_request_login_logout, |
michael@0 | 313 | test_logout_everywhere, |
michael@0 | 314 | |
michael@0 | 315 | test_options_pass_through |
michael@0 | 316 | ]; |
michael@0 | 317 | |
michael@0 | 318 | TESTS.forEach(add_test); |
michael@0 | 319 | |
michael@0 | 320 | function run_test() { |
michael@0 | 321 | run_next_test(); |
michael@0 | 322 | } |